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