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