]> git.ipfire.org Git - thirdparty/iw.git/blame - scan.c
some wmm parameter improvements
[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
857d966e
MH
30static unsigned char wifi_oui[3] = { 0x00, 0x50, 0xf2 };
31static unsigned char ieee80211_oui[3] = { 0x00, 0x0f, 0xac };
32
764fe753
JB
33struct scan_params {
34 bool unknown;
febeb0c0 35 enum print_ie_type type;
764fe753
JB
36};
37
7c37a24d
JB
38static int handle_scan(struct nl80211_state *state,
39 struct nl_cb *cb,
3563f4c5
JB
40 struct nl_msg *msg,
41 int argc, char **argv)
42{
559a1713
JB
43 struct nl_msg *ssids = NULL, *freqs = NULL;
44 char *eptr;
3563f4c5 45 int err = -ENOBUFS;
559a1713
JB
46 int i;
47 enum {
48 NONE,
49 FREQ,
50 SSID,
51 DONE,
52 } parse = NONE;
53 int freq;
54 bool passive = false, have_ssids = false, have_freqs = false;
3563f4c5
JB
55
56 ssids = nlmsg_alloc();
57 if (!ssids)
58 return -ENOMEM;
559a1713
JB
59
60 freqs = nlmsg_alloc();
61 if (!freqs) {
62 nlmsg_free(ssids);
63 return -ENOMEM;
64 }
65
66 for (i = 0; i < argc; i++) {
1ddf11eb 67 if (parse == NONE && strcmp(argv[i], "freq") == 0) {
559a1713
JB
68 parse = FREQ;
69 have_freqs = true;
70 continue;
1ddf11eb 71 } else if (parse < SSID && strcmp(argv[i], "ssid") == 0) {
559a1713
JB
72 parse = SSID;
73 have_ssids = true;
74 continue;
1ddf11eb 75 } else if (parse < SSID && strcmp(argv[i], "passive") == 0) {
559a1713
JB
76 parse = DONE;
77 passive = true;
78 continue;
79 }
80
81 switch (parse) {
82 case NONE:
83 case DONE:
84 return 1;
85 case FREQ:
86 freq = strtoul(argv[i], &eptr, 10);
87 if (eptr != argv[i] + strlen(argv[i]))
88 return 1;
89 NLA_PUT_U32(freqs, i, freq);
90 break;
91 case SSID:
92 NLA_PUT(ssids, i, strlen(argv[i]), argv[i]);
93 break;
94 }
95 }
96
97 if (!have_ssids)
98 NLA_PUT(ssids, 1, 0, "");
99 if (!passive)
100 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
101
102 if (have_freqs)
103 nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
3563f4c5
JB
104
105 err = 0;
106 nla_put_failure:
107 nlmsg_free(ssids);
559a1713 108 nlmsg_free(freqs);
3563f4c5
JB
109 return err;
110}
559a1713 111COMMAND(scan, trigger, "[freq <freq>*] [ssid <ssid>*|passive]",
6ca98d28
JB
112 NL80211_CMD_TRIGGER_SCAN, 0, CIB_NETDEV, handle_scan,
113 "Trigger a scan on the given frequencies with probing for the given\n"
114 "SSIDs (or wildcard if not given) unless passive scanning is requested.");
3563f4c5 115
857d966e
MH
116static void tab_on_first(bool *first)
117{
118 if (!*first)
119 printf("\t");
120 else
121 *first = false;
122}
123
83b4934c 124static void print_ssid(const uint8_t type, uint8_t len, const uint8_t *data)
3563f4c5 125{
83b4934c 126 printf(" ");
748f8489 127 print_ssid_escaped(len, data);
3563f4c5
JB
128 printf("\n");
129}
130
83b4934c 131static void print_supprates(const uint8_t type, uint8_t len, const uint8_t *data)
3563f4c5
JB
132{
133 int i;
134
83b4934c 135 printf(" ");
3563f4c5 136
83b4934c 137 for (i = 0; i < len; i++) {
3563f4c5
JB
138 int r = data[i] & 0x7f;
139 printf("%d.%d%s ", r/2, 5*(r&1), data[i] & 0x80 ? "*":"");
140 }
141 printf("\n");
142}
143
83b4934c 144static void print_ds(const uint8_t type, uint8_t len, const uint8_t *data)
3563f4c5 145{
83b4934c 146 printf(" channel %d\n", data[0]);
3563f4c5
JB
147}
148
83b4934c 149static void print_country(const uint8_t type, uint8_t len, const uint8_t *data)
b7e8fa37
MH
150{
151 int i;
152
83b4934c 153 printf(" %.*s", 2, data);
b7e8fa37
MH
154 switch (data[2]) {
155 case 'I':
156 printf(" (indoor)");
157 break;
158 case 'O':
159 printf(" (outdoor)");
160 break;
b6c0d634
JB
161 case ' ':
162 printf(" (in/outdoor)");
163 break;
164 default:
165 printf(" (invalid environment)");
166 break;
b7e8fa37
MH
167 }
168 printf(", data:");
169 for(i=0; i<len-3; i++)
170 printf(" %.02x", data[i + 3]);
171 printf("\n");
172}
173
d1563a1b
MH
174static void print_powerconstraint(const uint8_t type, uint8_t len, const uint8_t *data)
175{
176 printf(" %d dB\n", data[0]);
177}
178
83b4934c 179static void print_erp(const uint8_t type, uint8_t len, const uint8_t *data)
fc4d1484
MH
180{
181 if (data[0] == 0x00)
83b4934c 182 printf(" <no flags>");
fc4d1484
MH
183 if (data[0] & 0x01)
184 printf(" NonERP_Present");
185 if (data[0] & 0x02)
186 printf(" Use_Protection");
187 if (data[0] & 0x04)
188 printf(" Barker_Preamble_Mode");
189 printf("\n");
190}
191
83b4934c 192static void print_cipher(const uint8_t *data)
857d966e
MH
193{
194 if (memcmp(data, wifi_oui, 3) == 0) {
195 switch (data[3]) {
510e0e2f 196 case 0:
857d966e
MH
197 printf("Use group cipher suite");
198 break;
510e0e2f 199 case 1:
857d966e
MH
200 printf("WEP-40");
201 break;
510e0e2f 202 case 2:
857d966e
MH
203 printf("TKIP");
204 break;
510e0e2f 205 case 4:
857d966e
MH
206 printf("CCMP");
207 break;
510e0e2f 208 case 5:
857d966e
MH
209 printf("WEP-104");
210 break;
211 default:
332769c6 212 printf("%.02x-%.02x-%.02x:%d",
5594fd23 213 data[0], data[1] ,data[2], data[3]);
857d966e
MH
214 break;
215 }
216 } else if (memcmp(data, ieee80211_oui, 3) == 0) {
217 switch (data[3]) {
510e0e2f 218 case 0:
857d966e
MH
219 printf("Use group cipher suite");
220 break;
510e0e2f 221 case 1:
857d966e
MH
222 printf("WEP-40");
223 break;
510e0e2f 224 case 2:
857d966e
MH
225 printf("TKIP");
226 break;
510e0e2f 227 case 4:
857d966e
MH
228 printf("CCMP");
229 break;
510e0e2f 230 case 5:
857d966e
MH
231 printf("WEP-104");
232 break;
510e0e2f 233 case 6:
857d966e
MH
234 printf("AES-128-CMAC");
235 break;
236 default:
332769c6 237 printf("%.02x-%.02x-%.02x:%d",
5594fd23 238 data[0], data[1] ,data[2], data[3]);
857d966e
MH
239 break;
240 }
241 } else
332769c6 242 printf("%.02x-%.02x-%.02x:%d",
5594fd23 243 data[0], data[1] ,data[2], data[3]);
857d966e
MH
244}
245
83b4934c 246static void print_auth(const uint8_t *data)
857d966e
MH
247{
248 if (memcmp(data, wifi_oui, 3) == 0) {
249 switch (data[3]) {
510e0e2f 250 case 1:
857d966e
MH
251 printf("IEEE 802.1X");
252 break;
510e0e2f 253 case 2:
857d966e
MH
254 printf("PSK");
255 break;
256 default:
332769c6 257 printf("%.02x-%.02x-%.02x:%d",
5594fd23 258 data[0], data[1] ,data[2], data[3]);
857d966e
MH
259 break;
260 }
261 } else if (memcmp(data, ieee80211_oui, 3) == 0) {
262 switch (data[3]) {
510e0e2f 263 case 1:
857d966e
MH
264 printf("IEEE 802.1X");
265 break;
510e0e2f 266 case 2:
857d966e
MH
267 printf("PSK");
268 break;
0fe1c415
JB
269 case 3:
270 printf("FT/IEEE 802.1X");
271 break;
272 case 4:
273 printf("FT/PSK");
274 break;
275 case 5:
276 printf("IEEE 802.1X/SHA-256");
277 break;
278 case 6:
279 printf("PSK/SHA-256");
280 break;
857d966e 281 default:
332769c6 282 printf("%.02x-%.02x-%.02x:%d",
5594fd23 283 data[0], data[1] ,data[2], data[3]);
857d966e
MH
284 break;
285 }
286 } else
332769c6 287 printf("%.02x-%.02x-%.02x:%d",
5594fd23 288 data[0], data[1] ,data[2], data[3]);
857d966e
MH
289}
290
83b4934c
JB
291static void print_rsn_ie(const char *defcipher, const char *defauth,
292 uint8_t len, const uint8_t *data)
857d966e
MH
293{
294 bool first = true;
295 __u16 version, count, capa;
296 int i;
297
857d966e
MH
298 version = data[0] + (data[1] << 8);
299 tab_on_first(&first);
300 printf("\t * Version: %d\n", version);
301
302 data += 2;
303 len -= 2;
304
305 if (len < 4) {
306 tab_on_first(&first);
307 printf("\t * Group cipher: %s\n", defcipher);
308 printf("\t * Pairwise ciphers: %s\n", defcipher);
309 return;
310 }
311
312 tab_on_first(&first);
313 printf("\t * Group cipher: ");
314 print_cipher(data);
315 printf("\n");
316
317 data += 4;
318 len -= 4;
319
320 if (len < 2) {
321 tab_on_first(&first);
322 printf("\t * Pairwise ciphers: %s\n", defcipher);
323 return;
324 }
325
326 count = data[0] | (data[1] << 8);
31d477fb
MH
327 if (2 + (count * 4) > len)
328 goto invalid;
329
857d966e
MH
330 tab_on_first(&first);
331 printf("\t * Pairwise ciphers:");
31d477fb 332 for (i = 0; i < count; i++) {
857d966e
MH
333 printf(" ");
334 print_cipher(data + 2 + (i * 4));
335 }
336 printf("\n");
337
338 data += 2 + (count * 4);
339 len -= 2 + (count * 4);
340
341 if (len < 2) {
342 tab_on_first(&first);
343 printf("\t * Authentication suites: %s\n", defauth);
344 return;
345 }
346
347 count = data[0] | (data[1] << 8);
31d477fb
MH
348 if (2 + (count * 4) > len)
349 goto invalid;
350
857d966e
MH
351 tab_on_first(&first);
352 printf("\t * Authentication suites:");
83b4934c 353 for (i = 0; i < count; i++) {
857d966e
MH
354 printf(" ");
355 print_auth(data + 2 + (i * 4));
356 }
357 printf("\n");
358
359 data += 2 + (count * 4);
360 len -= 2 + (count * 4);
361
6a4f24e8
JB
362 if (len >= 2) {
363 capa = data[0] | (data[1] << 8);
364 tab_on_first(&first);
cadbe89a
JB
365 printf("\t * Capabilities:");
366 if (capa & 0x0001)
367 printf(" PreAuth");
368 if (capa & 0x0002)
369 printf(" NoPairwise");
370 switch ((capa & 0x000c) >> 2) {
371 case 0:
372 break;
373 case 1:
374 printf(" 2-PTKSA-RC");
375 break;
376 case 2:
377 printf(" 4-PTKSA-RC");
378 break;
379 case 3:
380 printf(" 16-PTKSA-RC");
381 break;
382 }
383 switch ((capa & 0x0030) >> 4) {
384 case 0:
385 break;
386 case 1:
387 printf(" 2-GTKSA-RC");
388 break;
389 case 2:
390 printf(" 4-GTKSA-RC");
391 break;
392 case 3:
393 printf(" 16-GTKSA-RC");
394 break;
395 }
396 if (capa & 0x0040)
397 printf(" MFP-required");
398 if (capa & 0x0080)
399 printf(" MFP-capable");
400 if (capa & 0x0200)
401 printf(" Peerkey-enabled");
402 if (capa & 0x0400)
403 printf(" SPP-AMSDU-capable");
404 if (capa & 0x0800)
405 printf(" SPP-AMSDU-required");
406 printf(" (0x%.4x)\n", capa);
6a4f24e8
JB
407 data += 2;
408 len -= 2;
409 }
31d477fb 410
cadbe89a 411 invalid:
31d477fb
MH
412 if (len != 0) {
413 printf("\t\t * bogus tail data (%d):", len);
414 while (len) {
415 printf(" %.2x", *data);
416 data++;
417 len--;
418 }
419 printf("\n");
420 }
857d966e
MH
421}
422
83b4934c 423static void print_rsn(const uint8_t type, uint8_t len, const uint8_t *data)
857d966e 424{
83b4934c 425 print_rsn_ie("CCMP", "IEEE 802.1X", len, data);
857d966e
MH
426}
427
83b4934c 428static void print_capabilities(const uint8_t type, uint8_t len, const uint8_t *data)
9b880b00 429{
31d2d259
JB
430 int i, base, bit;
431 bool first = true;
432
433
434 for (i = 0; i < len; i++) {
435 base = i * 8;
436
437 for (bit = 0; bit < 8; bit++) {
438 if (!(data[i] & (1 << bit)))
439 continue;
440
441 if (!first)
442 printf(",");
443 else
444 first = false;
445
446 switch (bit + base) {
447 case 0:
448 printf(" HT Information Exchange Supported");
449 break;
450 case 1:
451 printf(" On-demand Beacon");
452 break;
453 case 2:
454 printf(" Extended Channel Switching");
455 break;
456 case 3:
457 printf(" Wave Indication");
458 break;
459 case 4:
460 printf(" PSMP Capability");
461 break;
462 case 5:
463 printf(" Service Interval Granularity");
464 break;
465 case 6:
466 printf(" S-PSMP Capability");
467 break;
468 default:
469 printf(" %d", bit);
470 break;
471 }
472 }
473 }
9b880b00 474
9b880b00
MH
475 printf("\n");
476}
477
83b4934c
JB
478struct ie_print {
479 const char *name;
480 void (*print)(const uint8_t type, uint8_t len, const uint8_t *data);
481 uint8_t minlen, maxlen;
febeb0c0 482 uint8_t flags;
764fe753
JB
483};
484
83b4934c
JB
485static void print_ie(const struct ie_print *p, const uint8_t type,
486 uint8_t len, const uint8_t *data)
4673a894 487{
83b4934c
JB
488 int i;
489
490 if (!p->print)
491 return;
492
493 printf("\t%s:", p->name);
494 if (len < p->minlen || len > p->maxlen) {
495 if (len > 1) {
496 printf(" <invalid: %d bytes:", len);
497 for (i = 0; i < len; i++)
498 printf(" %.02x", data[i]);
499 printf(">\n");
500 } else if (len)
501 printf(" <invalid: 1 byte: %.02x>\n", data[0]);
502 else
503 printf(" <invalid: no data>\n");
504 return;
505 }
506
507 p->print(type, len, data);
508}
509
510#define PRINT_IGN { \
511 .name = "IGNORE", \
512 .print = NULL, \
513 .minlen = 0, \
514 .maxlen = 255, \
4673a894
JB
515}
516
83b4934c 517static const struct ie_print ieprinters[] = {
febeb0c0
JB
518 [0] = { "SSID", print_ssid, 0, 32, BIT(PRINT_SCAN) | BIT(PRINT_LINK), },
519 [1] = { "Supported rates", print_supprates, 0, 255, BIT(PRINT_SCAN), },
014d581a 520 [3] = { "DS Parameter set", print_ds, 1, 1, BIT(PRINT_SCAN), },
83b4934c 521 [5] = PRINT_IGN,
febeb0c0
JB
522 [7] = { "Country", print_country, 3, 255, BIT(PRINT_SCAN), },
523 [32] = { "Power constraint", print_powerconstraint, 1, 1, BIT(PRINT_SCAN), },
524 [42] = { "ERP", print_erp, 1, 255, BIT(PRINT_SCAN), },
525 [48] = { "RSN", print_rsn, 2, 255, BIT(PRINT_SCAN), },
526 [50] = { "Extended supported rates", print_supprates, 0, 255, BIT(PRINT_SCAN), },
527 [127] = { "Extended capabilities", print_capabilities, 0, 255, BIT(PRINT_SCAN), },
83b4934c
JB
528};
529
530static void print_wifi_wpa(const uint8_t type, uint8_t len, const uint8_t *data)
531{
532 print_rsn_ie("TKIP", "IEEE 802.1X", len, data);
533}
534
1cab57eb
JB
535static bool print_wifi_wmm_param(const uint8_t *data, uint8_t len)
536{
537 int i;
538 static const char *aci_tbl[] = { "BE", "BK", "VI", "VO" };
539
540 if (len < 19)
541 goto invalid;
542
543 if (data[0] != 1) {
cee4fe20 544 printf("Parameter: not version 1: ");
1cab57eb
JB
545 return false;
546 }
547
cee4fe20 548 printf("\t* Parameter version 1");
1cab57eb
JB
549
550 data++;
551
552 if (data[0] & 0x80)
553 printf("\n\t\t* u-APSD");
554
555 data += 2;
556
557 for (i = 0; i < 4; i++) {
eeac6099 558 printf("\n\t\t* %s:", aci_tbl[(data[0] >> 5) & 3]);
1cab57eb
JB
559 if (data[4] & 0x10)
560 printf(" acm");
561 printf(" CW %d-%d", (1 << (data[1] & 0xf)) - 1,
562 (1 << (data[1] >> 4)) - 1);
563 if (data[2] | data[3])
cee4fe20 564 printf(", TXOP %d usec", (data[2] + (data[3] << 8)) * 32);
1cab57eb
JB
565 data += 4;
566 }
567
568 printf("\n");
569 return true;
570
571 invalid:
572 printf("invalid: ");
573 return false;
574}
575
83b4934c 576static void print_wifi_wmm(const uint8_t type, uint8_t len, const uint8_t *data)
6ff0c93a
MH
577{
578 int i;
579
6ff0c93a
MH
580 switch (data[0]) {
581 case 0x00:
83b4934c 582 printf(" information:");
6ff0c93a
MH
583 break;
584 case 0x01:
1cab57eb
JB
585 if (print_wifi_wmm_param(data + 1, len - 1))
586 return;
6ff0c93a
MH
587 break;
588 default:
83b4934c 589 printf(" type %d:", data[0]);
6ff0c93a
MH
590 break;
591 }
592
1cab57eb
JB
593 for(i = 1; i < len; i++)
594 printf(" %.02x", data[i]);
6ff0c93a
MH
595 printf("\n");
596}
597
83b4934c 598static void print_wifi_wps(const uint8_t type, uint8_t len, const uint8_t *data)
4673a894
JB
599{
600 bool first = true;
601 __u16 subtype, sublen;
602
4673a894
JB
603 while (len >= 4) {
604 subtype = (data[0] << 8) + data[1];
605 sublen = (data[2] << 8) + data[3];
606 if (sublen > len)
607 break;
608
609 switch (subtype) {
610 case 0x104a:
611 tab_on_first(&first);
dffc6750 612 printf("\t * Version: %d.%d\n", data[4] >> 4, data[4] & 0xF);
4673a894
JB
613 break;
614 case 0x1011:
615 tab_on_first(&first);
616 printf("\t * Device name: %.*s\n", sublen, data + 4);
617 break;
618 case 0x1021:
619 tab_on_first(&first);
620 printf("\t * Manufacturer: %.*s\n", sublen, data + 4);
621 break;
622 case 0x1023:
623 tab_on_first(&first);
624 printf("\t * Model: %.*s\n", sublen, data + 4);
625 break;
7ee5a865 626 case 0x1057: {
fe31a22e 627 __u8 val = data[4];
7ee5a865 628 tab_on_first(&first);
fe31a22e 629 printf("\t * AP setup locked: 0x%.2x\n", val);
7ee5a865
JB
630 break;
631 }
4673a894
JB
632 case 0x1008: {
633 __u16 meth = (data[4] << 8) + data[5];
634 bool comma = false;
635 tab_on_first(&first);
636 printf("\t * Config methods:");
637#define T(bit, name) do { \
638 if (meth & (1<<bit)) { \
639 if (comma) \
640 printf(","); \
641 comma = true; \
642 printf(" " name); \
643 } } while (0)
644 T(0, "USB");
645 T(1, "Ethernet");
646 T(2, "Label");
647 T(3, "Display");
648 T(4, "Ext. NFC");
649 T(5, "Int. NFC");
650 T(6, "NFC Intf.");
651 T(7, "PBC");
652 T(8, "Keypad");
653 printf("\n");
654 break;
655#undef T
656 }
657 default:
658 break;
659 }
660
661 data += sublen + 4;
662 len -= sublen + 4;
663 }
664
665 if (len != 0) {
666 printf("\t\t * bogus tail data (%d):", len);
667 while (len) {
668 printf(" %.2x", *data);
669 data++;
670 len--;
671 }
672 printf("\n");
673 }
674}
675
83b4934c 676static const struct ie_print wifiprinters[] = {
febeb0c0
JB
677 [1] = { "WPA", print_wifi_wpa, 2, 255, BIT(PRINT_SCAN), },
678 [2] = { "WMM", print_wifi_wmm, 1, 255, BIT(PRINT_SCAN), },
679 [4] = { "WPS", print_wifi_wps, 0, 255, BIT(PRINT_SCAN), },
4673a894
JB
680};
681
764fe753 682static void print_vendor(unsigned char len, unsigned char *data,
febeb0c0 683 bool unknown, enum print_ie_type ptype)
3563f4c5
JB
684{
685 int i;
686
fbf80af5 687 if (len < 3) {
4673a894 688 printf("\tVendor specific: <too short> data:");
fbf80af5
JB
689 for(i = 0; i < len; i++)
690 printf(" %.02x", data[i]);
691 printf("\n");
692 return;
693 }
694
857d966e 695 if (len >= 4 && memcmp(data, wifi_oui, 3) == 0) {
febeb0c0
JB
696 if (data[3] < ARRAY_SIZE(wifiprinters) &&
697 wifiprinters[data[3]].name &&
698 wifiprinters[data[3]].flags & BIT(ptype)) {
83b4934c
JB
699 print_ie(&wifiprinters[data[3]], data[3], len - 4, data + 4);
700 return;
701 }
febeb0c0 702 if (!unknown)
4673a894 703 return;
857d966e 704 printf("\tWiFi OUI %#.2x, data:", data[3]);
4673a894
JB
705 for(i = 0; i < len - 4; i++)
706 printf(" %.02x", data[i + 4]);
707 printf("\n");
708 return;
709 }
710
febeb0c0 711 if (!unknown)
764fe753
JB
712 return;
713
fbf80af5 714 printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:",
3563f4c5 715 data[0], data[1], data[2]);
fbf80af5
JB
716 for (i = 3; i < len; i++)
717 printf(" %.2x", data[i]);
3563f4c5
JB
718 printf("\n");
719}
720
febeb0c0
JB
721void print_ies(unsigned char *ie, int ielen, bool unknown,
722 enum print_ie_type ptype)
3563f4c5
JB
723{
724 while (ielen >= 2 && ielen >= ie[1]) {
febeb0c0
JB
725 if (ie[0] < ARRAY_SIZE(ieprinters) &&
726 ieprinters[ie[0]].name &&
727 ieprinters[ie[0]].flags & BIT(ptype)) {
83b4934c 728 print_ie(&ieprinters[ie[0]], ie[0], ie[1], ie + 2);
764fe753 729 } else if (ie[0] == 221 /* vendor */) {
febeb0c0
JB
730 print_vendor(ie[1], ie + 2, unknown, ptype);
731 } else if (unknown) {
3563f4c5
JB
732 int i;
733
8086b700 734 printf("\tUnknown IE (%d):", ie[0]);
3563f4c5 735 for (i=0; i<ie[1]; i++)
8086b700 736 printf(" %.2x", ie[2+i]);
3563f4c5
JB
737 printf("\n");
738 }
739 ielen -= ie[1] + 2;
740 ie += ie[1] + 2;
741 }
742}
743
744static int print_bss_handler(struct nl_msg *msg, void *arg)
745{
746 struct nlattr *tb[NL80211_ATTR_MAX + 1];
747 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
748 struct nlattr *bss[NL80211_BSS_MAX + 1];
749 char mac_addr[20], dev[20];
750 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
751 [NL80211_BSS_TSF] = { .type = NLA_U64 },
752 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
753 [NL80211_BSS_BSSID] = { },
754 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
755 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
756 [NL80211_BSS_INFORMATION_ELEMENTS] = { },
f2e17e1f
JB
757 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
758 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
a56117a6 759 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
3563f4c5 760 };
febeb0c0 761 struct scan_params *params = arg;
3563f4c5
JB
762
763 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
764 genlmsg_attrlen(gnlh, 0), NULL);
765
766 if (!tb[NL80211_ATTR_BSS]) {
767 fprintf(stderr, "bss info missing!");
768 return NL_SKIP;
769 }
770 if (nla_parse_nested(bss, NL80211_BSS_MAX,
771 tb[NL80211_ATTR_BSS],
772 bss_policy)) {
773 fprintf(stderr, "failed to parse nested attributes!");
774 return NL_SKIP;
775 }
776
777 if (!bss[NL80211_BSS_BSSID])
778 return NL_SKIP;
779
780 mac_addr_n2a(mac_addr, nla_data(bss[NL80211_BSS_BSSID]));
781 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
a56117a6
JB
782 printf("BSS %s (on %s)", mac_addr, dev);
783
784 if (bss[NL80211_BSS_STATUS]) {
785 switch (nla_get_u32(bss[NL80211_BSS_STATUS])) {
786 case NL80211_BSS_STATUS_AUTHENTICATED:
787 printf(" -- authenticated");
788 break;
789 case NL80211_BSS_STATUS_ASSOCIATED:
790 printf(" -- associated");
791 break;
792 case NL80211_BSS_STATUS_IBSS_JOINED:
793 printf(" -- joined");
794 break;
795 default:
796 printf(" -- unknown status: %d",
797 nla_get_u32(bss[NL80211_BSS_STATUS]));
798 break;
799 }
800 }
801 printf("\n");
3563f4c5 802
e7109a8a
JB
803 if (bss[NL80211_BSS_TSF]) {
804 unsigned long long tsf;
805 tsf = (unsigned long long)nla_get_u64(bss[NL80211_BSS_TSF]);
806 printf("\tTSF: %llu usec (%llud, %.2lld:%.2llu:%.2llu)\n",
807 tsf, tsf/1000/1000/60/60/24, (tsf/1000/1000/60/60) % 24,
808 (tsf/1000/1000/60) % 60, (tsf/1000/1000) % 60);
809 }
3563f4c5
JB
810 if (bss[NL80211_BSS_FREQUENCY])
811 printf("\tfreq: %d\n",
812 nla_get_u32(bss[NL80211_BSS_FREQUENCY]));
813 if (bss[NL80211_BSS_BEACON_INTERVAL])
814 printf("\tbeacon interval: %d\n",
815 nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]));
92a04ecd
MH
816 if (bss[NL80211_BSS_CAPABILITY]) {
817 __u16 capa = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
818 printf("\tcapability:");
819 if (capa & WLAN_CAPABILITY_ESS)
820 printf(" ESS");
821 if (capa & WLAN_CAPABILITY_IBSS)
822 printf(" IBSS");
823 if (capa & WLAN_CAPABILITY_PRIVACY)
824 printf(" Privacy");
825 if (capa & WLAN_CAPABILITY_SHORT_PREAMBLE)
826 printf(" ShortPreamble");
827 if (capa & WLAN_CAPABILITY_PBCC)
828 printf(" PBCC");
829 if (capa & WLAN_CAPABILITY_CHANNEL_AGILITY)
830 printf(" ChannelAgility");
831 if (capa & WLAN_CAPABILITY_SPECTRUM_MGMT)
832 printf(" SpectrumMgmt");
833 if (capa & WLAN_CAPABILITY_QOS)
834 printf(" QoS");
835 if (capa & WLAN_CAPABILITY_SHORT_SLOT_TIME)
836 printf(" ShortSlotTime");
837 if (capa & WLAN_CAPABILITY_APSD)
838 printf(" APSD");
839 if (capa & WLAN_CAPABILITY_DSSS_OFDM)
840 printf(" DSSS-OFDM");
841 printf(" (0x%.4x)\n", capa);
842 }
f2e17e1f
JB
843 if (bss[NL80211_BSS_SIGNAL_MBM]) {
844 int s = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
845 printf("\tsignal: %d.%.2d dBm\n", s/100, s%100);
846 }
847 if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
848 unsigned char s = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
849 printf("\tsignal: %d/100\n", s);
850 }
3563f4c5
JB
851 if (bss[NL80211_BSS_INFORMATION_ELEMENTS])
852 print_ies(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
764fe753 853 nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
febeb0c0 854 params->unknown, params->type);
3563f4c5
JB
855
856 return NL_SKIP;
857}
858
764fe753 859static struct scan_params scan_params;
3563f4c5 860
7c37a24d
JB
861static int handle_scan_dump(struct nl80211_state *state,
862 struct nl_cb *cb,
3563f4c5
JB
863 struct nl_msg *msg,
864 int argc, char **argv)
865{
764fe753
JB
866 if (argc > 1)
867 return 1;
868
869 scan_params.unknown = false;
870 if (argc == 1 && !strcmp(argv[0], "-u"))
871 scan_params.unknown = true;
872
febeb0c0
JB
873 scan_params.type = PRINT_SCAN;
874
764fe753
JB
875 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_bss_handler,
876 &scan_params);
3563f4c5
JB
877 return 0;
878}
764fe753 879COMMAND(scan, dump, "[-u]",
6ca98d28
JB
880 NL80211_CMD_GET_SCAN, NLM_F_DUMP, CIB_NETDEV, handle_scan_dump,
881 "Dump the current scan results. If -u is specified, print unknown\n"
882 "data in scan results.");
a5fe4ef2
JB
883
884static int handle_scan_combined(struct nl80211_state *state,
885 struct nl_cb *cb,
886 struct nl_msg *msg,
887 int argc, char **argv)
888{
559a1713 889 char **trig_argv;
a5fe4ef2
JB
890 static char *dump_argv[] = {
891 NULL,
892 "scan",
893 "dump",
92649eab 894 NULL,
a5fe4ef2
JB
895 };
896 static const __u32 cmds[] = {
897 NL80211_CMD_NEW_SCAN_RESULTS,
898 NL80211_CMD_SCAN_ABORTED,
899 };
559a1713 900 int trig_argc, dump_argc, err;
a5fe4ef2 901
559a1713
JB
902 if (argc >= 3 && !strcmp(argv[2], "-u")) {
903 dump_argc = 4;
904 dump_argv[3] = "-u";
905 } else
906 dump_argc = 3;
907
908 trig_argc = 3 + (argc - 2) + (3 - dump_argc);
909 trig_argv = calloc(trig_argc, sizeof(*trig_argv));
910 if (!trig_argv)
911 return -ENOMEM;
a5fe4ef2 912 trig_argv[0] = argv[0];
559a1713
JB
913 trig_argv[1] = "scan";
914 trig_argv[2] = "trigger";
915 int i;
916 for (i = 0; i < argc - 2 - (dump_argc - 3); i++)
917 trig_argv[i + 3] = argv[i + 2 + (dump_argc - 3)];
918 err = handle_cmd(state, II_NETDEV, trig_argc, trig_argv);
919 free(trig_argv);
a5fe4ef2
JB
920 if (err)
921 return err;
922
61725dbe
JB
923 /*
924 * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION
925 *
926 * This code has a bug, which requires creating a separate
927 * nl80211 socket to fix:
928 * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or
929 * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel
930 * before (!) we listen to it, because we only start listening
931 * after we send our scan request.
932 *
933 * Doing it the other way around has a race condition as well,
934 * if you first open the events socket you may get a notification
935 * for a previous scan.
936 *
937 * The only proper way to fix this would be to listen to events
938 * before sending the command, and for the kernel to send the
939 * scan request along with the event, so that you can match up
940 * whether the scan you requested was finished or aborted (this
941 * may result in processing a scan that another application
942 * requested, but that doesn't seem to be a problem).
943 *
944 * Alas, the kernel doesn't do that (yet).
945 */
946
a5fe4ef2
JB
947 if (listen_events(state, ARRAY_SIZE(cmds), cmds) ==
948 NL80211_CMD_SCAN_ABORTED) {
949 printf("scan aborted!\n");
950 return 0;
951 }
952
953 dump_argv[0] = argv[0];
92649eab 954 return handle_cmd(state, II_NETDEV, dump_argc, dump_argv);
a5fe4ef2 955}
6ca98d28
JB
956TOPLEVEL(scan, "[-u] [freq <freq>*] [ssid <ssid>*|passive]", 0, 0,
957 CIB_NETDEV, handle_scan_combined,
958 "Scan on the given frequencies and probe for the given SSIDs\n"
959 "(or wildcard if not given) unless passive scanning is requested.\n"
960 "If -u is specified print unknown data in the scan results.");