]> git.ipfire.org Git - thirdparty/iw.git/blame - scan.c
fix a reason string
[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]",
6ca98d28
JB
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.");
3563f4c5 114
857d966e
MH
115static void tab_on_first(bool *first)
116{
117 if (!*first)
118 printf("\t");
119 else
120 *first = false;
121}
122
83b4934c 123static void print_ssid(const uint8_t type, uint8_t len, const uint8_t *data)
3563f4c5 124{
83b4934c 125 printf(" ");
748f8489 126 print_ssid_escaped(len, data);
3563f4c5
JB
127 printf("\n");
128}
129
83b4934c 130static void print_supprates(const uint8_t type, uint8_t len, const uint8_t *data)
3563f4c5
JB
131{
132 int i;
133
83b4934c 134 printf(" ");
3563f4c5 135
83b4934c 136 for (i = 0; i < len; i++) {
3563f4c5
JB
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
83b4934c 143static void print_ds(const uint8_t type, uint8_t len, const uint8_t *data)
3563f4c5 144{
83b4934c 145 printf(" channel %d\n", data[0]);
3563f4c5
JB
146}
147
83b4934c 148static void print_country(const uint8_t type, uint8_t len, const uint8_t *data)
b7e8fa37
MH
149{
150 int i;
151
83b4934c 152 printf(" %.*s", 2, data);
b7e8fa37
MH
153 switch (data[2]) {
154 case 'I':
155 printf(" (indoor)");
156 break;
157 case 'O':
158 printf(" (outdoor)");
159 break;
b6c0d634
JB
160 case ' ':
161 printf(" (in/outdoor)");
162 break;
163 default:
164 printf(" (invalid environment)");
165 break;
b7e8fa37
MH
166 }
167 printf(", data:");
168 for(i=0; i<len-3; i++)
169 printf(" %.02x", data[i + 3]);
170 printf("\n");
171}
172
d1563a1b
MH
173static void print_powerconstraint(const uint8_t type, uint8_t len, const uint8_t *data)
174{
175 printf(" %d dB\n", data[0]);
176}
177
83b4934c 178static void print_erp(const uint8_t type, uint8_t len, const uint8_t *data)
fc4d1484
MH
179{
180 if (data[0] == 0x00)
83b4934c 181 printf(" <no flags>");
fc4d1484
MH
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
83b4934c 191static void print_cipher(const uint8_t *data)
857d966e
MH
192{
193 if (memcmp(data, wifi_oui, 3) == 0) {
194 switch (data[3]) {
510e0e2f 195 case 0:
857d966e
MH
196 printf("Use group cipher suite");
197 break;
510e0e2f 198 case 1:
857d966e
MH
199 printf("WEP-40");
200 break;
510e0e2f 201 case 2:
857d966e
MH
202 printf("TKIP");
203 break;
510e0e2f 204 case 4:
857d966e
MH
205 printf("CCMP");
206 break;
510e0e2f 207 case 5:
857d966e
MH
208 printf("WEP-104");
209 break;
210 default:
332769c6 211 printf("%.02x-%.02x-%.02x:%d",
5594fd23 212 data[0], data[1] ,data[2], data[3]);
857d966e
MH
213 break;
214 }
215 } else if (memcmp(data, ieee80211_oui, 3) == 0) {
216 switch (data[3]) {
510e0e2f 217 case 0:
857d966e
MH
218 printf("Use group cipher suite");
219 break;
510e0e2f 220 case 1:
857d966e
MH
221 printf("WEP-40");
222 break;
510e0e2f 223 case 2:
857d966e
MH
224 printf("TKIP");
225 break;
510e0e2f 226 case 4:
857d966e
MH
227 printf("CCMP");
228 break;
510e0e2f 229 case 5:
857d966e
MH
230 printf("WEP-104");
231 break;
510e0e2f 232 case 6:
857d966e
MH
233 printf("AES-128-CMAC");
234 break;
235 default:
332769c6 236 printf("%.02x-%.02x-%.02x:%d",
5594fd23 237 data[0], data[1] ,data[2], data[3]);
857d966e
MH
238 break;
239 }
240 } else
332769c6 241 printf("%.02x-%.02x-%.02x:%d",
5594fd23 242 data[0], data[1] ,data[2], data[3]);
857d966e
MH
243}
244
83b4934c 245static void print_auth(const uint8_t *data)
857d966e
MH
246{
247 if (memcmp(data, wifi_oui, 3) == 0) {
248 switch (data[3]) {
510e0e2f 249 case 1:
857d966e
MH
250 printf("IEEE 802.1X");
251 break;
510e0e2f 252 case 2:
857d966e
MH
253 printf("PSK");
254 break;
255 default:
332769c6 256 printf("%.02x-%.02x-%.02x:%d",
5594fd23 257 data[0], data[1] ,data[2], data[3]);
857d966e
MH
258 break;
259 }
260 } else if (memcmp(data, ieee80211_oui, 3) == 0) {
261 switch (data[3]) {
510e0e2f 262 case 1:
857d966e
MH
263 printf("IEEE 802.1X");
264 break;
510e0e2f 265 case 2:
857d966e
MH
266 printf("PSK");
267 break;
0fe1c415
JB
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;
857d966e 280 default:
332769c6 281 printf("%.02x-%.02x-%.02x:%d",
5594fd23 282 data[0], data[1] ,data[2], data[3]);
857d966e
MH
283 break;
284 }
285 } else
332769c6 286 printf("%.02x-%.02x-%.02x:%d",
5594fd23 287 data[0], data[1] ,data[2], data[3]);
857d966e
MH
288}
289
83b4934c
JB
290static void print_rsn_ie(const char *defcipher, const char *defauth,
291 uint8_t len, const uint8_t *data)
857d966e
MH
292{
293 bool first = true;
294 __u16 version, count, capa;
295 int i;
296
857d966e
MH
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);
31d477fb
MH
326 if (2 + (count * 4) > len)
327 goto invalid;
328
857d966e
MH
329 tab_on_first(&first);
330 printf("\t * Pairwise ciphers:");
31d477fb 331 for (i = 0; i < count; i++) {
857d966e
MH
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);
31d477fb
MH
347 if (2 + (count * 4) > len)
348 goto invalid;
349
857d966e
MH
350 tab_on_first(&first);
351 printf("\t * Authentication suites:");
83b4934c 352 for (i = 0; i < count; i++) {
857d966e
MH
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
6a4f24e8
JB
361 if (len >= 2) {
362 capa = data[0] | (data[1] << 8);
363 tab_on_first(&first);
cadbe89a
JB
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);
6a4f24e8
JB
406 data += 2;
407 len -= 2;
408 }
31d477fb 409
cadbe89a 410 invalid:
31d477fb
MH
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 }
857d966e
MH
420}
421
83b4934c 422static void print_rsn(const uint8_t type, uint8_t len, const uint8_t *data)
857d966e 423{
83b4934c 424 print_rsn_ie("CCMP", "IEEE 802.1X", len, data);
857d966e
MH
425}
426
83b4934c 427static void print_capabilities(const uint8_t type, uint8_t len, const uint8_t *data)
9b880b00 428{
31d2d259
JB
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 }
9b880b00 473
9b880b00
MH
474 printf("\n");
475}
476
83b4934c
JB
477struct 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;
764fe753
JB
481};
482
83b4934c
JB
483static void print_ie(const struct ie_print *p, const uint8_t type,
484 uint8_t len, const uint8_t *data)
4673a894 485{
83b4934c
JB
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, \
4673a894
JB
513}
514
83b4934c
JB
515static 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, },
d1563a1b 521 [32] = { "Power constraint", print_powerconstraint, 1, 1, },
83b4934c
JB
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
528static 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
533static void print_wifi_wmm(const uint8_t type, uint8_t len, const uint8_t *data)
6ff0c93a
MH
534{
535 int i;
536
6ff0c93a
MH
537 switch (data[0]) {
538 case 0x00:
83b4934c 539 printf(" information:");
6ff0c93a
MH
540 break;
541 case 0x01:
83b4934c 542 printf(" parameter:");
6ff0c93a
MH
543 break;
544 default:
83b4934c 545 printf(" type %d:", data[0]);
6ff0c93a
MH
546 break;
547 }
548
83b4934c 549 for(i = 0; i < len - 1; i++)
6ff0c93a
MH
550 printf(" %.02x", data[i + 1]);
551 printf("\n");
552}
553
83b4934c 554static void print_wifi_wps(const uint8_t type, uint8_t len, const uint8_t *data)
4673a894
JB
555{
556 bool first = true;
557 __u16 subtype, sublen;
558
4673a894
JB
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);
dffc6750 568 printf("\t * Version: %d.%d\n", data[4] >> 4, data[4] & 0xF);
4673a894
JB
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;
7ee5a865 582 case 0x1057: {
fe31a22e 583 __u8 val = data[4];
7ee5a865 584 tab_on_first(&first);
fe31a22e 585 printf("\t * AP setup locked: 0x%.2x\n", val);
7ee5a865
JB
586 break;
587 }
4673a894
JB
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
83b4934c
JB
632static 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, },
4673a894
JB
636};
637
764fe753
JB
638static void print_vendor(unsigned char len, unsigned char *data,
639 struct scan_params *params)
3563f4c5
JB
640{
641 int i;
642
fbf80af5 643 if (len < 3) {
4673a894 644 printf("\tVendor specific: <too short> data:");
fbf80af5
JB
645 for(i = 0; i < len; i++)
646 printf(" %.02x", data[i]);
647 printf("\n");
648 return;
649 }
650
857d966e 651 if (len >= 4 && memcmp(data, wifi_oui, 3) == 0) {
83b4934c
JB
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 }
4673a894
JB
656 if (!params->unknown)
657 return;
857d966e 658 printf("\tWiFi OUI %#.2x, data:", data[3]);
4673a894
JB
659 for(i = 0; i < len - 4; i++)
660 printf(" %.02x", data[i + 4]);
661 printf("\n");
662 return;
663 }
664
764fe753
JB
665 if (!params->unknown)
666 return;
667
fbf80af5 668 printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:",
3563f4c5 669 data[0], data[1], data[2]);
fbf80af5
JB
670 for (i = 3; i < len; i++)
671 printf(" %.2x", data[i]);
3563f4c5
JB
672 printf("\n");
673}
674
764fe753 675static void print_ies(unsigned char *ie, int ielen, struct scan_params *params)
3563f4c5
JB
676{
677 while (ielen >= 2 && ielen >= ie[1]) {
83b4934c
JB
678 if (ie[0] < ARRAY_SIZE(ieprinters) && ieprinters[ie[0]].name) {
679 print_ie(&ieprinters[ie[0]], ie[0], ie[1], ie + 2);
764fe753
JB
680 } else if (ie[0] == 221 /* vendor */) {
681 print_vendor(ie[1], ie + 2, params);
682 } else if (params->unknown) {
3563f4c5
JB
683 int i;
684
8086b700 685 printf("\tUnknown IE (%d):", ie[0]);
3563f4c5 686 for (i=0; i<ie[1]; i++)
8086b700 687 printf(" %.2x", ie[2+i]);
3563f4c5
JB
688 printf("\n");
689 }
690 ielen -= ie[1] + 2;
691 ie += ie[1] + 2;
692 }
693}
694
695static 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] = { },
f2e17e1f
JB
708 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
709 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
3563f4c5
JB
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
e7109a8a
JB
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 }
3563f4c5
JB
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]));
92a04ecd
MH
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 }
f2e17e1f
JB
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 }
3563f4c5
JB
781 if (bss[NL80211_BSS_INFORMATION_ELEMENTS])
782 print_ies(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
764fe753
JB
783 nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
784 arg);
3563f4c5
JB
785
786 return NL_SKIP;
787}
788
764fe753 789static struct scan_params scan_params;
3563f4c5 790
7c37a24d
JB
791static int handle_scan_dump(struct nl80211_state *state,
792 struct nl_cb *cb,
3563f4c5
JB
793 struct nl_msg *msg,
794 int argc, char **argv)
795{
764fe753
JB
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);
3563f4c5
JB
805 return 0;
806}
764fe753 807COMMAND(scan, dump, "[-u]",
6ca98d28
JB
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.");
a5fe4ef2
JB
811
812static int handle_scan_combined(struct nl80211_state *state,
813 struct nl_cb *cb,
814 struct nl_msg *msg,
815 int argc, char **argv)
816{
559a1713 817 char **trig_argv;
a5fe4ef2
JB
818 static char *dump_argv[] = {
819 NULL,
820 "scan",
821 "dump",
92649eab 822 NULL,
a5fe4ef2
JB
823 };
824 static const __u32 cmds[] = {
825 NL80211_CMD_NEW_SCAN_RESULTS,
826 NL80211_CMD_SCAN_ABORTED,
827 };
559a1713 828 int trig_argc, dump_argc, err;
a5fe4ef2 829
559a1713
JB
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;
a5fe4ef2 840 trig_argv[0] = argv[0];
559a1713
JB
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);
a5fe4ef2
JB
848 if (err)
849 return err;
850
61725dbe
JB
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
a5fe4ef2
JB
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];
92649eab 882 return handle_cmd(state, II_NETDEV, dump_argc, dump_argv);
a5fe4ef2 883}
6ca98d28
JB
884TOPLEVEL(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.");