]> git.ipfire.org Git - thirdparty/iw.git/blame - scan.c
add basic support for parsing WMM information element
[thirdparty/iw.git] / scan.c
CommitLineData
3563f4c5
JB
1#include <net/if.h>
2#include <errno.h>
3#include <string.h>
4#include <ctype.h>
764fe753 5#include <stdbool.h>
3563f4c5
JB
6
7#include <netlink/genl/genl.h>
8#include <netlink/genl/family.h>
9#include <netlink/genl/ctrl.h>
10#include <netlink/msg.h>
11#include <netlink/attr.h>
12
13#include "nl80211.h"
14#include "iw.h"
15
92a04ecd
MH
16#define WLAN_CAPABILITY_ESS (1<<0)
17#define WLAN_CAPABILITY_IBSS (1<<1)
18#define WLAN_CAPABILITY_CF_POLLABLE (1<<2)
19#define WLAN_CAPABILITY_CF_POLL_REQUEST (1<<3)
20#define WLAN_CAPABILITY_PRIVACY (1<<4)
21#define WLAN_CAPABILITY_SHORT_PREAMBLE (1<<5)
22#define WLAN_CAPABILITY_PBCC (1<<6)
23#define WLAN_CAPABILITY_CHANNEL_AGILITY (1<<7)
24#define WLAN_CAPABILITY_SPECTRUM_MGMT (1<<8)
25#define WLAN_CAPABILITY_QOS (1<<9)
26#define WLAN_CAPABILITY_SHORT_SLOT_TIME (1<<10)
27#define WLAN_CAPABILITY_APSD (1<<11)
28#define WLAN_CAPABILITY_DSSS_OFDM (1<<13)
29
764fe753
JB
30struct scan_params {
31 bool unknown;
32};
33
7c37a24d
JB
34static int handle_scan(struct nl80211_state *state,
35 struct nl_cb *cb,
3563f4c5
JB
36 struct nl_msg *msg,
37 int argc, char **argv)
38{
39 struct nl_msg *ssids = NULL;
40 int err = -ENOBUFS;
41
42 ssids = nlmsg_alloc();
43 if (!ssids)
44 return -ENOMEM;
45 NLA_PUT(ssids, 1, 0, "");
46 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
47
48 err = 0;
49 nla_put_failure:
50 nlmsg_free(ssids);
51 return err;
52}
53COMMAND(scan, trigger, NULL,
54 NL80211_CMD_TRIGGER_SCAN, 0, CIB_NETDEV, handle_scan);
55
56typedef void (*printfn)(unsigned char type, unsigned char len, unsigned char *data);
57
58static void print_ssid(unsigned char type, unsigned char len, unsigned char *data)
59{
60 int i;
61 printf("\tSSID: ");
62 for (i=0; i<len; i++) {
63 if (isprint(data[i]))
64 printf("%c", data[i]);
65 else
66 printf("\\x%.2x", data[i]);
67 }
68 printf("\n");
69}
70
71static void print_supprates(unsigned char type, unsigned char len, unsigned char *data)
72{
73 int i;
74
75 if (type == 1)
76 printf("\tSupported rates: ");
77 else
78 printf("\tExtended supported rates: ");
79
80 for (i=0; i<len; i++) {
81 int r = data[i] & 0x7f;
82 printf("%d.%d%s ", r/2, 5*(r&1), data[i] & 0x80 ? "*":"");
83 }
84 printf("\n");
85}
86
87static void print_ds(unsigned char type, unsigned char len, unsigned char *data)
88{
89 printf("\tDS Parameter set: channel %d\n", data[0]);
90}
91
92static void print_ign(unsigned char type, unsigned char len, unsigned char *data)
93{
94 /* ignore for now, not too useful */
95}
96
b7e8fa37
MH
97static void print_country(unsigned char type, unsigned char len, unsigned char *data)
98{
99 int i;
100
101 printf("\tCountry: %.*s", 2, data);
102 switch (data[2]) {
103 case 'I':
104 printf(" (indoor)");
105 break;
106 case 'O':
107 printf(" (outdoor)");
108 break;
109 }
110 printf(", data:");
111 for(i=0; i<len-3; i++)
112 printf(" %.02x", data[i + 3]);
113 printf("\n");
114}
115
fc4d1484
MH
116static void print_erp(unsigned char type, unsigned char len, unsigned char *data)
117{
118 if (data[0] == 0x00)
119 return;
120
121 printf("\tERP:");
122 if (data[0] & 0x01)
123 printf(" NonERP_Present");
124 if (data[0] & 0x02)
125 printf(" Use_Protection");
126 if (data[0] & 0x04)
127 printf(" Barker_Preamble_Mode");
128 printf("\n");
129}
130
9b880b00
MH
131static void print_capabilities(unsigned char type, unsigned char len, unsigned char *data)
132{
133 int i;
134
135 printf("\tExtended capabilties:");
136 for(i=0; i<len; i++)
137 printf(" %.02x", data[i]);
138 printf("\n");
139}
140
764fe753
JB
141static const printfn ieprinters[] = {
142 [0] = print_ssid,
143 [1] = print_supprates,
144 [3] = print_ds,
145 [5] = print_ign,
b7e8fa37 146 [7] = print_country,
fc4d1484 147 [42] = print_erp,
764fe753 148 [50] = print_supprates,
9b880b00 149 [127] = print_capabilities,
764fe753
JB
150};
151
4673a894
JB
152static void tab_on_first(bool *first)
153{
154 if (!*first)
155 printf("\t");
156 else
157 *first = false;
158}
159
6ff0c93a
MH
160static void print_wifi_wmm(unsigned char type, unsigned char len, unsigned char *data)
161{
162 int i;
163
164 printf("\tWMM ");
165 switch (data[0]) {
166 case 0x00:
167 printf("information:");
168 break;
169 case 0x01:
170 printf("parameter:");
171 break;
172 default:
173 printf("type %d:", data[0]);
174 break;
175 }
176
177 for(i=0; i<len-1; i++)
178 printf(" %.02x", data[i + 1]);
179 printf("\n");
180}
181
4673a894
JB
182static void print_wifi_wps(unsigned char type, unsigned char len, unsigned char *data)
183{
184 bool first = true;
185 __u16 subtype, sublen;
186
187 printf("\tWPS:");
188
189 while (len >= 4) {
190 subtype = (data[0] << 8) + data[1];
191 sublen = (data[2] << 8) + data[3];
192 if (sublen > len)
193 break;
194
195 switch (subtype) {
196 case 0x104a:
197 tab_on_first(&first);
198 printf("\t * Version: %#.2x\n", data[4]);
199 break;
200 case 0x1011:
201 tab_on_first(&first);
202 printf("\t * Device name: %.*s\n", sublen, data + 4);
203 break;
204 case 0x1021:
205 tab_on_first(&first);
206 printf("\t * Manufacturer: %.*s\n", sublen, data + 4);
207 break;
208 case 0x1023:
209 tab_on_first(&first);
210 printf("\t * Model: %.*s\n", sublen, data + 4);
211 break;
7ee5a865
JB
212 case 0x1057: {
213 __u16 val = (data[4] << 8) | data[5];
214 tab_on_first(&first);
215 printf("\t * AP setup locked: 0x%.4x\n", val);
216 break;
217 }
4673a894
JB
218 case 0x1008: {
219 __u16 meth = (data[4] << 8) + data[5];
220 bool comma = false;
221 tab_on_first(&first);
222 printf("\t * Config methods:");
223#define T(bit, name) do { \
224 if (meth & (1<<bit)) { \
225 if (comma) \
226 printf(","); \
227 comma = true; \
228 printf(" " name); \
229 } } while (0)
230 T(0, "USB");
231 T(1, "Ethernet");
232 T(2, "Label");
233 T(3, "Display");
234 T(4, "Ext. NFC");
235 T(5, "Int. NFC");
236 T(6, "NFC Intf.");
237 T(7, "PBC");
238 T(8, "Keypad");
239 printf("\n");
240 break;
241#undef T
242 }
243 default:
244 break;
245 }
246
247 data += sublen + 4;
248 len -= sublen + 4;
249 }
250
251 if (len != 0) {
252 printf("\t\t * bogus tail data (%d):", len);
253 while (len) {
254 printf(" %.2x", *data);
255 data++;
256 len--;
257 }
258 printf("\n");
259 }
260}
261
262static const printfn wifiprinters[] = {
6ff0c93a 263 [2] = print_wifi_wmm,
4673a894
JB
264 [4] = print_wifi_wps,
265};
266
764fe753
JB
267static void print_vendor(unsigned char len, unsigned char *data,
268 struct scan_params *params)
3563f4c5
JB
269{
270 int i;
271
fbf80af5 272 if (len < 3) {
4673a894 273 printf("\tVendor specific: <too short> data:");
fbf80af5
JB
274 for(i = 0; i < len; i++)
275 printf(" %.02x", data[i]);
276 printf("\n");
277 return;
278 }
279
4673a894
JB
280 if (len >= 4 && data[0] == 0x00 && data[1] == 0x50 && data[2] == 0xF2) {
281 if (data[3] < ARRAY_SIZE(wifiprinters) && wifiprinters[data[3]])
282 return wifiprinters[data[3]](data[3], len - 4, data + 4);
283 if (!params->unknown)
284 return;
285 printf("\tWiFi OUI %#.2x data:", data[3]);
286 for(i = 0; i < len - 4; i++)
287 printf(" %.02x", data[i + 4]);
288 printf("\n");
289 return;
290 }
291
764fe753
JB
292 if (!params->unknown)
293 return;
294
fbf80af5 295 printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:",
3563f4c5 296 data[0], data[1], data[2]);
fbf80af5
JB
297 for (i = 3; i < len; i++)
298 printf(" %.2x", data[i]);
3563f4c5
JB
299 printf("\n");
300}
301
764fe753 302static void print_ies(unsigned char *ie, int ielen, struct scan_params *params)
3563f4c5
JB
303{
304 while (ielen >= 2 && ielen >= ie[1]) {
97ebbaf5 305 if (ie[0] < ARRAY_SIZE(ieprinters) && ieprinters[ie[0]]) {
3563f4c5 306 ieprinters[ie[0]](ie[0], ie[1], ie + 2);
764fe753
JB
307 } else if (ie[0] == 221 /* vendor */) {
308 print_vendor(ie[1], ie + 2, params);
309 } else if (params->unknown) {
3563f4c5
JB
310 int i;
311
8086b700 312 printf("\tUnknown IE (%d):", ie[0]);
3563f4c5 313 for (i=0; i<ie[1]; i++)
8086b700 314 printf(" %.2x", ie[2+i]);
3563f4c5
JB
315 printf("\n");
316 }
317 ielen -= ie[1] + 2;
318 ie += ie[1] + 2;
319 }
320}
321
322static int print_bss_handler(struct nl_msg *msg, void *arg)
323{
324 struct nlattr *tb[NL80211_ATTR_MAX + 1];
325 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
326 struct nlattr *bss[NL80211_BSS_MAX + 1];
327 char mac_addr[20], dev[20];
328 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
329 [NL80211_BSS_TSF] = { .type = NLA_U64 },
330 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
331 [NL80211_BSS_BSSID] = { },
332 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
333 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
334 [NL80211_BSS_INFORMATION_ELEMENTS] = { },
f2e17e1f
JB
335 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
336 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
3563f4c5
JB
337 };
338
339 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
340 genlmsg_attrlen(gnlh, 0), NULL);
341
342 if (!tb[NL80211_ATTR_BSS]) {
343 fprintf(stderr, "bss info missing!");
344 return NL_SKIP;
345 }
346 if (nla_parse_nested(bss, NL80211_BSS_MAX,
347 tb[NL80211_ATTR_BSS],
348 bss_policy)) {
349 fprintf(stderr, "failed to parse nested attributes!");
350 return NL_SKIP;
351 }
352
353 if (!bss[NL80211_BSS_BSSID])
354 return NL_SKIP;
355
356 mac_addr_n2a(mac_addr, nla_data(bss[NL80211_BSS_BSSID]));
357 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
358 printf("BSS %s (on %s)\n", mac_addr, dev);
359
e7109a8a
JB
360 if (bss[NL80211_BSS_TSF]) {
361 unsigned long long tsf;
362 tsf = (unsigned long long)nla_get_u64(bss[NL80211_BSS_TSF]);
363 printf("\tTSF: %llu usec (%llud, %.2lld:%.2llu:%.2llu)\n",
364 tsf, tsf/1000/1000/60/60/24, (tsf/1000/1000/60/60) % 24,
365 (tsf/1000/1000/60) % 60, (tsf/1000/1000) % 60);
366 }
3563f4c5
JB
367 if (bss[NL80211_BSS_FREQUENCY])
368 printf("\tfreq: %d\n",
369 nla_get_u32(bss[NL80211_BSS_FREQUENCY]));
370 if (bss[NL80211_BSS_BEACON_INTERVAL])
371 printf("\tbeacon interval: %d\n",
372 nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]));
92a04ecd
MH
373 if (bss[NL80211_BSS_CAPABILITY]) {
374 __u16 capa = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
375 printf("\tcapability:");
376 if (capa & WLAN_CAPABILITY_ESS)
377 printf(" ESS");
378 if (capa & WLAN_CAPABILITY_IBSS)
379 printf(" IBSS");
380 if (capa & WLAN_CAPABILITY_PRIVACY)
381 printf(" Privacy");
382 if (capa & WLAN_CAPABILITY_SHORT_PREAMBLE)
383 printf(" ShortPreamble");
384 if (capa & WLAN_CAPABILITY_PBCC)
385 printf(" PBCC");
386 if (capa & WLAN_CAPABILITY_CHANNEL_AGILITY)
387 printf(" ChannelAgility");
388 if (capa & WLAN_CAPABILITY_SPECTRUM_MGMT)
389 printf(" SpectrumMgmt");
390 if (capa & WLAN_CAPABILITY_QOS)
391 printf(" QoS");
392 if (capa & WLAN_CAPABILITY_SHORT_SLOT_TIME)
393 printf(" ShortSlotTime");
394 if (capa & WLAN_CAPABILITY_APSD)
395 printf(" APSD");
396 if (capa & WLAN_CAPABILITY_DSSS_OFDM)
397 printf(" DSSS-OFDM");
398 printf(" (0x%.4x)\n", capa);
399 }
f2e17e1f
JB
400 if (bss[NL80211_BSS_SIGNAL_MBM]) {
401 int s = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
402 printf("\tsignal: %d.%.2d dBm\n", s/100, s%100);
403 }
404 if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
405 unsigned char s = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
406 printf("\tsignal: %d/100\n", s);
407 }
3563f4c5
JB
408 if (bss[NL80211_BSS_INFORMATION_ELEMENTS])
409 print_ies(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
764fe753
JB
410 nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
411 arg);
3563f4c5
JB
412
413 return NL_SKIP;
414}
415
764fe753 416static struct scan_params scan_params;
3563f4c5 417
7c37a24d
JB
418static int handle_scan_dump(struct nl80211_state *state,
419 struct nl_cb *cb,
3563f4c5
JB
420 struct nl_msg *msg,
421 int argc, char **argv)
422{
764fe753
JB
423 if (argc > 1)
424 return 1;
425
426 scan_params.unknown = false;
427 if (argc == 1 && !strcmp(argv[0], "-u"))
428 scan_params.unknown = true;
429
430 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_bss_handler,
431 &scan_params);
3563f4c5
JB
432 return 0;
433}
764fe753 434COMMAND(scan, dump, "[-u]",
3563f4c5 435 NL80211_CMD_GET_SCAN, NLM_F_DUMP, CIB_NETDEV, handle_scan_dump);
a5fe4ef2
JB
436
437static int handle_scan_combined(struct nl80211_state *state,
438 struct nl_cb *cb,
439 struct nl_msg *msg,
440 int argc, char **argv)
441{
442 static char *trig_argv[] = {
443 NULL,
444 "scan",
445 "trigger",
446 };
447 static char *dump_argv[] = {
448 NULL,
449 "scan",
450 "dump",
92649eab 451 NULL,
a5fe4ef2
JB
452 };
453 static const __u32 cmds[] = {
454 NL80211_CMD_NEW_SCAN_RESULTS,
455 NL80211_CMD_SCAN_ABORTED,
456 };
92649eab 457 int dump_argc, err;
a5fe4ef2
JB
458
459 trig_argv[0] = argv[0];
460 err = handle_cmd(state, II_NETDEV, ARRAY_SIZE(trig_argv), trig_argv);
461 if (err)
462 return err;
463
61725dbe
JB
464 /*
465 * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION
466 *
467 * This code has a bug, which requires creating a separate
468 * nl80211 socket to fix:
469 * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or
470 * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel
471 * before (!) we listen to it, because we only start listening
472 * after we send our scan request.
473 *
474 * Doing it the other way around has a race condition as well,
475 * if you first open the events socket you may get a notification
476 * for a previous scan.
477 *
478 * The only proper way to fix this would be to listen to events
479 * before sending the command, and for the kernel to send the
480 * scan request along with the event, so that you can match up
481 * whether the scan you requested was finished or aborted (this
482 * may result in processing a scan that another application
483 * requested, but that doesn't seem to be a problem).
484 *
485 * Alas, the kernel doesn't do that (yet).
486 */
487
a5fe4ef2
JB
488 if (listen_events(state, ARRAY_SIZE(cmds), cmds) ==
489 NL80211_CMD_SCAN_ABORTED) {
490 printf("scan aborted!\n");
491 return 0;
492 }
493
92649eab
MH
494 if (argc == 3 && !strcmp(argv[2], "-u")) {
495 dump_argc = 4;
496 dump_argv[3] = "-u";
497 } else
498 dump_argc = 3;
499
a5fe4ef2 500 dump_argv[0] = argv[0];
92649eab 501 return handle_cmd(state, II_NETDEV, dump_argc, dump_argv);
a5fe4ef2 502}
92649eab 503TOPLEVEL(scan, "[-u]", 0, 0, CIB_NETDEV, handle_scan_combined);