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