]> git.ipfire.org Git - thirdparty/iw.git/blame - scan.c
don't emit usage to stderr
[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);
a2a4c265 563 printf(", AIFSN %d", data[0] & 0xf);
1cab57eb 564 if (data[2] | data[3])
cee4fe20 565 printf(", TXOP %d usec", (data[2] + (data[3] << 8)) * 32);
1cab57eb
JB
566 data += 4;
567 }
568
569 printf("\n");
570 return true;
571
572 invalid:
573 printf("invalid: ");
574 return false;
575}
576
83b4934c 577static void print_wifi_wmm(const uint8_t type, uint8_t len, const uint8_t *data)
6ff0c93a
MH
578{
579 int i;
580
6ff0c93a
MH
581 switch (data[0]) {
582 case 0x00:
83b4934c 583 printf(" information:");
6ff0c93a
MH
584 break;
585 case 0x01:
1cab57eb
JB
586 if (print_wifi_wmm_param(data + 1, len - 1))
587 return;
6ff0c93a
MH
588 break;
589 default:
83b4934c 590 printf(" type %d:", data[0]);
6ff0c93a
MH
591 break;
592 }
593
1cab57eb
JB
594 for(i = 1; i < len; i++)
595 printf(" %.02x", data[i]);
6ff0c93a
MH
596 printf("\n");
597}
598
83b4934c 599static void print_wifi_wps(const uint8_t type, uint8_t len, const uint8_t *data)
4673a894
JB
600{
601 bool first = true;
602 __u16 subtype, sublen;
603
4673a894
JB
604 while (len >= 4) {
605 subtype = (data[0] << 8) + data[1];
606 sublen = (data[2] << 8) + data[3];
607 if (sublen > len)
608 break;
609
610 switch (subtype) {
611 case 0x104a:
612 tab_on_first(&first);
dffc6750 613 printf("\t * Version: %d.%d\n", data[4] >> 4, data[4] & 0xF);
4673a894
JB
614 break;
615 case 0x1011:
616 tab_on_first(&first);
617 printf("\t * Device name: %.*s\n", sublen, data + 4);
618 break;
619 case 0x1021:
620 tab_on_first(&first);
621 printf("\t * Manufacturer: %.*s\n", sublen, data + 4);
622 break;
623 case 0x1023:
624 tab_on_first(&first);
625 printf("\t * Model: %.*s\n", sublen, data + 4);
626 break;
7ee5a865 627 case 0x1057: {
fe31a22e 628 __u8 val = data[4];
7ee5a865 629 tab_on_first(&first);
fe31a22e 630 printf("\t * AP setup locked: 0x%.2x\n", val);
7ee5a865
JB
631 break;
632 }
4673a894
JB
633 case 0x1008: {
634 __u16 meth = (data[4] << 8) + data[5];
635 bool comma = false;
636 tab_on_first(&first);
637 printf("\t * Config methods:");
638#define T(bit, name) do { \
639 if (meth & (1<<bit)) { \
640 if (comma) \
641 printf(","); \
642 comma = true; \
643 printf(" " name); \
644 } } while (0)
645 T(0, "USB");
646 T(1, "Ethernet");
647 T(2, "Label");
648 T(3, "Display");
649 T(4, "Ext. NFC");
650 T(5, "Int. NFC");
651 T(6, "NFC Intf.");
652 T(7, "PBC");
653 T(8, "Keypad");
654 printf("\n");
655 break;
656#undef T
657 }
658 default:
659 break;
660 }
661
662 data += sublen + 4;
663 len -= sublen + 4;
664 }
665
666 if (len != 0) {
667 printf("\t\t * bogus tail data (%d):", len);
668 while (len) {
669 printf(" %.2x", *data);
670 data++;
671 len--;
672 }
673 printf("\n");
674 }
675}
676
83b4934c 677static const struct ie_print wifiprinters[] = {
febeb0c0
JB
678 [1] = { "WPA", print_wifi_wpa, 2, 255, BIT(PRINT_SCAN), },
679 [2] = { "WMM", print_wifi_wmm, 1, 255, BIT(PRINT_SCAN), },
680 [4] = { "WPS", print_wifi_wps, 0, 255, BIT(PRINT_SCAN), },
4673a894
JB
681};
682
764fe753 683static void print_vendor(unsigned char len, unsigned char *data,
febeb0c0 684 bool unknown, enum print_ie_type ptype)
3563f4c5
JB
685{
686 int i;
687
fbf80af5 688 if (len < 3) {
4673a894 689 printf("\tVendor specific: <too short> data:");
fbf80af5
JB
690 for(i = 0; i < len; i++)
691 printf(" %.02x", data[i]);
692 printf("\n");
693 return;
694 }
695
857d966e 696 if (len >= 4 && memcmp(data, wifi_oui, 3) == 0) {
febeb0c0
JB
697 if (data[3] < ARRAY_SIZE(wifiprinters) &&
698 wifiprinters[data[3]].name &&
699 wifiprinters[data[3]].flags & BIT(ptype)) {
83b4934c
JB
700 print_ie(&wifiprinters[data[3]], data[3], len - 4, data + 4);
701 return;
702 }
febeb0c0 703 if (!unknown)
4673a894 704 return;
857d966e 705 printf("\tWiFi OUI %#.2x, data:", data[3]);
4673a894
JB
706 for(i = 0; i < len - 4; i++)
707 printf(" %.02x", data[i + 4]);
708 printf("\n");
709 return;
710 }
711
febeb0c0 712 if (!unknown)
764fe753
JB
713 return;
714
fbf80af5 715 printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:",
3563f4c5 716 data[0], data[1], data[2]);
fbf80af5
JB
717 for (i = 3; i < len; i++)
718 printf(" %.2x", data[i]);
3563f4c5
JB
719 printf("\n");
720}
721
febeb0c0
JB
722void print_ies(unsigned char *ie, int ielen, bool unknown,
723 enum print_ie_type ptype)
3563f4c5
JB
724{
725 while (ielen >= 2 && ielen >= ie[1]) {
febeb0c0
JB
726 if (ie[0] < ARRAY_SIZE(ieprinters) &&
727 ieprinters[ie[0]].name &&
728 ieprinters[ie[0]].flags & BIT(ptype)) {
83b4934c 729 print_ie(&ieprinters[ie[0]], ie[0], ie[1], ie + 2);
764fe753 730 } else if (ie[0] == 221 /* vendor */) {
febeb0c0
JB
731 print_vendor(ie[1], ie + 2, unknown, ptype);
732 } else if (unknown) {
3563f4c5
JB
733 int i;
734
8086b700 735 printf("\tUnknown IE (%d):", ie[0]);
3563f4c5 736 for (i=0; i<ie[1]; i++)
8086b700 737 printf(" %.2x", ie[2+i]);
3563f4c5
JB
738 printf("\n");
739 }
740 ielen -= ie[1] + 2;
741 ie += ie[1] + 2;
742 }
743}
744
745static int print_bss_handler(struct nl_msg *msg, void *arg)
746{
747 struct nlattr *tb[NL80211_ATTR_MAX + 1];
748 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
749 struct nlattr *bss[NL80211_BSS_MAX + 1];
750 char mac_addr[20], dev[20];
751 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
752 [NL80211_BSS_TSF] = { .type = NLA_U64 },
753 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
754 [NL80211_BSS_BSSID] = { },
755 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
756 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
757 [NL80211_BSS_INFORMATION_ELEMENTS] = { },
f2e17e1f
JB
758 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
759 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
a56117a6 760 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
3563f4c5 761 };
febeb0c0 762 struct scan_params *params = arg;
3563f4c5
JB
763
764 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
765 genlmsg_attrlen(gnlh, 0), NULL);
766
767 if (!tb[NL80211_ATTR_BSS]) {
768 fprintf(stderr, "bss info missing!");
769 return NL_SKIP;
770 }
771 if (nla_parse_nested(bss, NL80211_BSS_MAX,
772 tb[NL80211_ATTR_BSS],
773 bss_policy)) {
774 fprintf(stderr, "failed to parse nested attributes!");
775 return NL_SKIP;
776 }
777
778 if (!bss[NL80211_BSS_BSSID])
779 return NL_SKIP;
780
781 mac_addr_n2a(mac_addr, nla_data(bss[NL80211_BSS_BSSID]));
782 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
a56117a6
JB
783 printf("BSS %s (on %s)", mac_addr, dev);
784
785 if (bss[NL80211_BSS_STATUS]) {
786 switch (nla_get_u32(bss[NL80211_BSS_STATUS])) {
787 case NL80211_BSS_STATUS_AUTHENTICATED:
788 printf(" -- authenticated");
789 break;
790 case NL80211_BSS_STATUS_ASSOCIATED:
791 printf(" -- associated");
792 break;
793 case NL80211_BSS_STATUS_IBSS_JOINED:
794 printf(" -- joined");
795 break;
796 default:
797 printf(" -- unknown status: %d",
798 nla_get_u32(bss[NL80211_BSS_STATUS]));
799 break;
800 }
801 }
802 printf("\n");
3563f4c5 803
e7109a8a
JB
804 if (bss[NL80211_BSS_TSF]) {
805 unsigned long long tsf;
806 tsf = (unsigned long long)nla_get_u64(bss[NL80211_BSS_TSF]);
807 printf("\tTSF: %llu usec (%llud, %.2lld:%.2llu:%.2llu)\n",
808 tsf, tsf/1000/1000/60/60/24, (tsf/1000/1000/60/60) % 24,
809 (tsf/1000/1000/60) % 60, (tsf/1000/1000) % 60);
810 }
3563f4c5
JB
811 if (bss[NL80211_BSS_FREQUENCY])
812 printf("\tfreq: %d\n",
813 nla_get_u32(bss[NL80211_BSS_FREQUENCY]));
814 if (bss[NL80211_BSS_BEACON_INTERVAL])
815 printf("\tbeacon interval: %d\n",
816 nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]));
92a04ecd
MH
817 if (bss[NL80211_BSS_CAPABILITY]) {
818 __u16 capa = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
819 printf("\tcapability:");
820 if (capa & WLAN_CAPABILITY_ESS)
821 printf(" ESS");
822 if (capa & WLAN_CAPABILITY_IBSS)
823 printf(" IBSS");
824 if (capa & WLAN_CAPABILITY_PRIVACY)
825 printf(" Privacy");
826 if (capa & WLAN_CAPABILITY_SHORT_PREAMBLE)
827 printf(" ShortPreamble");
828 if (capa & WLAN_CAPABILITY_PBCC)
829 printf(" PBCC");
830 if (capa & WLAN_CAPABILITY_CHANNEL_AGILITY)
831 printf(" ChannelAgility");
832 if (capa & WLAN_CAPABILITY_SPECTRUM_MGMT)
833 printf(" SpectrumMgmt");
834 if (capa & WLAN_CAPABILITY_QOS)
835 printf(" QoS");
836 if (capa & WLAN_CAPABILITY_SHORT_SLOT_TIME)
837 printf(" ShortSlotTime");
838 if (capa & WLAN_CAPABILITY_APSD)
839 printf(" APSD");
840 if (capa & WLAN_CAPABILITY_DSSS_OFDM)
841 printf(" DSSS-OFDM");
842 printf(" (0x%.4x)\n", capa);
843 }
f2e17e1f
JB
844 if (bss[NL80211_BSS_SIGNAL_MBM]) {
845 int s = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
846 printf("\tsignal: %d.%.2d dBm\n", s/100, s%100);
847 }
848 if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
849 unsigned char s = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
850 printf("\tsignal: %d/100\n", s);
851 }
3563f4c5
JB
852 if (bss[NL80211_BSS_INFORMATION_ELEMENTS])
853 print_ies(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
764fe753 854 nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
febeb0c0 855 params->unknown, params->type);
3563f4c5
JB
856
857 return NL_SKIP;
858}
859
764fe753 860static struct scan_params scan_params;
3563f4c5 861
7c37a24d
JB
862static int handle_scan_dump(struct nl80211_state *state,
863 struct nl_cb *cb,
3563f4c5
JB
864 struct nl_msg *msg,
865 int argc, char **argv)
866{
764fe753
JB
867 if (argc > 1)
868 return 1;
869
870 scan_params.unknown = false;
871 if (argc == 1 && !strcmp(argv[0], "-u"))
872 scan_params.unknown = true;
873
febeb0c0
JB
874 scan_params.type = PRINT_SCAN;
875
764fe753
JB
876 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_bss_handler,
877 &scan_params);
3563f4c5
JB
878 return 0;
879}
764fe753 880COMMAND(scan, dump, "[-u]",
6ca98d28
JB
881 NL80211_CMD_GET_SCAN, NLM_F_DUMP, CIB_NETDEV, handle_scan_dump,
882 "Dump the current scan results. If -u is specified, print unknown\n"
883 "data in scan results.");
a5fe4ef2
JB
884
885static int handle_scan_combined(struct nl80211_state *state,
886 struct nl_cb *cb,
887 struct nl_msg *msg,
888 int argc, char **argv)
889{
559a1713 890 char **trig_argv;
a5fe4ef2
JB
891 static char *dump_argv[] = {
892 NULL,
893 "scan",
894 "dump",
92649eab 895 NULL,
a5fe4ef2
JB
896 };
897 static const __u32 cmds[] = {
898 NL80211_CMD_NEW_SCAN_RESULTS,
899 NL80211_CMD_SCAN_ABORTED,
900 };
559a1713 901 int trig_argc, dump_argc, err;
a5fe4ef2 902
559a1713
JB
903 if (argc >= 3 && !strcmp(argv[2], "-u")) {
904 dump_argc = 4;
905 dump_argv[3] = "-u";
906 } else
907 dump_argc = 3;
908
909 trig_argc = 3 + (argc - 2) + (3 - dump_argc);
910 trig_argv = calloc(trig_argc, sizeof(*trig_argv));
911 if (!trig_argv)
912 return -ENOMEM;
a5fe4ef2 913 trig_argv[0] = argv[0];
559a1713
JB
914 trig_argv[1] = "scan";
915 trig_argv[2] = "trigger";
916 int i;
917 for (i = 0; i < argc - 2 - (dump_argc - 3); i++)
918 trig_argv[i + 3] = argv[i + 2 + (dump_argc - 3)];
919 err = handle_cmd(state, II_NETDEV, trig_argc, trig_argv);
920 free(trig_argv);
a5fe4ef2
JB
921 if (err)
922 return err;
923
61725dbe
JB
924 /*
925 * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION
926 *
927 * This code has a bug, which requires creating a separate
928 * nl80211 socket to fix:
929 * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or
930 * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel
931 * before (!) we listen to it, because we only start listening
932 * after we send our scan request.
933 *
934 * Doing it the other way around has a race condition as well,
935 * if you first open the events socket you may get a notification
936 * for a previous scan.
937 *
938 * The only proper way to fix this would be to listen to events
939 * before sending the command, and for the kernel to send the
940 * scan request along with the event, so that you can match up
941 * whether the scan you requested was finished or aborted (this
942 * may result in processing a scan that another application
943 * requested, but that doesn't seem to be a problem).
944 *
945 * Alas, the kernel doesn't do that (yet).
946 */
947
a5fe4ef2
JB
948 if (listen_events(state, ARRAY_SIZE(cmds), cmds) ==
949 NL80211_CMD_SCAN_ABORTED) {
950 printf("scan aborted!\n");
951 return 0;
952 }
953
954 dump_argv[0] = argv[0];
92649eab 955 return handle_cmd(state, II_NETDEV, dump_argc, dump_argv);
a5fe4ef2 956}
6ca98d28
JB
957TOPLEVEL(scan, "[-u] [freq <freq>*] [ssid <ssid>*|passive]", 0, 0,
958 CIB_NETDEV, handle_scan_combined,
959 "Scan on the given frequencies and probe for the given SSIDs\n"
960 "(or wildcard if not given) unless passive scanning is requested.\n"
961 "If -u is specified print unknown data in the scan results.");