]> git.ipfire.org Git - thirdparty/iw.git/blame - scan.c
scan: print WPS UUID
[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;
575280cc 36 bool show_both_ie_sets;
764fe753
JB
37};
38
2b690f0a
LR
39#define IEEE80211_COUNTRY_EXTENSION_ID 201
40
72725719
JB
41union ieee80211_country_ie_triplet {
42 struct {
43 __u8 first_channel;
44 __u8 num_channels;
45 __s8 max_power;
46 } __attribute__ ((packed)) chans;
47 struct {
48 __u8 reg_extension_id;
49 __u8 reg_class;
50 __u8 coverage_class;
51 } __attribute__ ((packed)) ext;
2b690f0a
LR
52} __attribute__ ((packed));
53
7c37a24d
JB
54static int handle_scan(struct nl80211_state *state,
55 struct nl_cb *cb,
3563f4c5
JB
56 struct nl_msg *msg,
57 int argc, char **argv)
58{
559a1713
JB
59 struct nl_msg *ssids = NULL, *freqs = NULL;
60 char *eptr;
3563f4c5 61 int err = -ENOBUFS;
559a1713
JB
62 int i;
63 enum {
64 NONE,
65 FREQ,
64797a7f 66 IES,
559a1713
JB
67 SSID,
68 DONE,
69 } parse = NONE;
70 int freq;
71 bool passive = false, have_ssids = false, have_freqs = false;
64797a7f
JB
72 size_t tmp;
73 unsigned char *ies;
3563f4c5
JB
74
75 ssids = nlmsg_alloc();
76 if (!ssids)
77 return -ENOMEM;
559a1713
JB
78
79 freqs = nlmsg_alloc();
80 if (!freqs) {
81 nlmsg_free(ssids);
82 return -ENOMEM;
83 }
84
85 for (i = 0; i < argc; i++) {
559a1713
JB
86 switch (parse) {
87 case NONE:
64797a7f
JB
88 if (strcmp(argv[i], "freq") == 0) {
89 parse = FREQ;
90 have_freqs = true;
91 break;
92 } else if (strcmp(argv[i], "ies") == 0) {
93 parse = IES;
94 break;
95 } else if (strcmp(argv[i], "ssid") == 0) {
96 parse = SSID;
97 have_ssids = true;
98 break;
99 } else if (strcmp(argv[i], "passive") == 0) {
100 parse = DONE;
101 passive = true;
102 break;
103 }
559a1713
JB
104 case DONE:
105 return 1;
106 case FREQ:
107 freq = strtoul(argv[i], &eptr, 10);
cc12e895
JB
108 if (eptr != argv[i] + strlen(argv[i])) {
109 /* failed to parse as number -- maybe a tag? */
110 i--;
111 parse = NONE;
112 continue;
113 }
559a1713 114 NLA_PUT_U32(freqs, i, freq);
64797a7f
JB
115 break;
116 case IES:
117 ies = parse_hex(argv[i], &tmp);
118 if (!ies)
119 goto nla_put_failure;
120 NLA_PUT(msg, NL80211_ATTR_IE, tmp, ies);
121 free(ies);
122 parse = NONE;
559a1713
JB
123 break;
124 case SSID:
125 NLA_PUT(ssids, i, strlen(argv[i]), argv[i]);
126 break;
127 }
128 }
129
130 if (!have_ssids)
131 NLA_PUT(ssids, 1, 0, "");
132 if (!passive)
133 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
134
135 if (have_freqs)
136 nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
3563f4c5
JB
137
138 err = 0;
139 nla_put_failure:
140 nlmsg_free(ssids);
559a1713 141 nlmsg_free(freqs);
3563f4c5
JB
142 return err;
143}
3563f4c5 144
857d966e
MH
145static void tab_on_first(bool *first)
146{
147 if (!*first)
148 printf("\t");
149 else
150 *first = false;
151}
152
83b4934c 153static void print_ssid(const uint8_t type, uint8_t len, const uint8_t *data)
3563f4c5 154{
83b4934c 155 printf(" ");
748f8489 156 print_ssid_escaped(len, data);
3563f4c5
JB
157 printf("\n");
158}
159
83b4934c 160static void print_supprates(const uint8_t type, uint8_t len, const uint8_t *data)
3563f4c5
JB
161{
162 int i;
163
83b4934c 164 printf(" ");
3563f4c5 165
83b4934c 166 for (i = 0; i < len; i++) {
3563f4c5
JB
167 int r = data[i] & 0x7f;
168 printf("%d.%d%s ", r/2, 5*(r&1), data[i] & 0x80 ? "*":"");
169 }
170 printf("\n");
171}
172
83b4934c 173static void print_ds(const uint8_t type, uint8_t len, const uint8_t *data)
3563f4c5 174{
83b4934c 175 printf(" channel %d\n", data[0]);
3563f4c5
JB
176}
177
2b690f0a 178static const char *country_env_str(char environment)
b7e8fa37 179{
2b690f0a 180 switch (environment) {
b7e8fa37 181 case 'I':
2b690f0a 182 return "Indoor only";
b7e8fa37 183 case 'O':
2b690f0a 184 return "Outdoor only";
b6c0d634 185 case ' ':
2b690f0a 186 return "Indoor/Outdoor";
b6c0d634 187 default:
2b690f0a 188 return "bogus";
b7e8fa37 189 }
2b690f0a
LR
190}
191
192static void print_country(const uint8_t type, uint8_t len, const uint8_t *data)
193{
194 printf(" %.*s", 2, data);
195
196 printf("\tEnvironment: %s\n", country_env_str(data[2]));
197
198 data += 3;
199 len -= 3;
200
201 if (len < 3) {
202 printf("\t\tNo country IE triplets present\n");
203 return;
204 }
205
206 while (len >= 3) {
207 int end_channel;
72725719 208 union ieee80211_country_ie_triplet *triplet = (void *) data;
2b690f0a
LR
209
210 if (triplet->ext.reg_extension_id >= IEEE80211_COUNTRY_EXTENSION_ID) {
211 printf("\t\tExtension ID: %d Regulatory Class: %d Coverage class: %d (up to %dm)\n",
212 triplet->ext.reg_extension_id,
213 triplet->ext.reg_class,
214 triplet->ext.coverage_class,
215 triplet->ext.coverage_class * 450);
216
217 data += 3;
218 len -= 3;
219 continue;
220 }
221
222 /* 2 GHz */
223 if (triplet->chans.first_channel <= 14)
224 end_channel = triplet->chans.first_channel + (triplet->chans.num_channels - 1);
225 else
226 end_channel = triplet->chans.first_channel + (4 * (triplet->chans.num_channels - 1));
227
a9859d30 228 printf("\t\tChannels [%d - %d] @ %d dBm\n", triplet->chans.first_channel, end_channel, triplet->chans.max_power);
2b690f0a
LR
229
230 data += 3;
231 len -= 3;
232 }
233
234 return;
b7e8fa37
MH
235}
236
d1563a1b
MH
237static void print_powerconstraint(const uint8_t type, uint8_t len, const uint8_t *data)
238{
239 printf(" %d dB\n", data[0]);
240}
241
83b4934c 242static void print_erp(const uint8_t type, uint8_t len, const uint8_t *data)
fc4d1484
MH
243{
244 if (data[0] == 0x00)
83b4934c 245 printf(" <no flags>");
fc4d1484
MH
246 if (data[0] & 0x01)
247 printf(" NonERP_Present");
248 if (data[0] & 0x02)
249 printf(" Use_Protection");
250 if (data[0] & 0x04)
251 printf(" Barker_Preamble_Mode");
252 printf("\n");
253}
254
83b4934c 255static void print_cipher(const uint8_t *data)
857d966e
MH
256{
257 if (memcmp(data, wifi_oui, 3) == 0) {
258 switch (data[3]) {
510e0e2f 259 case 0:
857d966e
MH
260 printf("Use group cipher suite");
261 break;
510e0e2f 262 case 1:
857d966e
MH
263 printf("WEP-40");
264 break;
510e0e2f 265 case 2:
857d966e
MH
266 printf("TKIP");
267 break;
510e0e2f 268 case 4:
857d966e
MH
269 printf("CCMP");
270 break;
510e0e2f 271 case 5:
857d966e
MH
272 printf("WEP-104");
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 if (memcmp(data, ieee80211_oui, 3) == 0) {
280 switch (data[3]) {
510e0e2f 281 case 0:
857d966e
MH
282 printf("Use group cipher suite");
283 break;
510e0e2f 284 case 1:
857d966e
MH
285 printf("WEP-40");
286 break;
510e0e2f 287 case 2:
857d966e
MH
288 printf("TKIP");
289 break;
510e0e2f 290 case 4:
857d966e
MH
291 printf("CCMP");
292 break;
510e0e2f 293 case 5:
857d966e
MH
294 printf("WEP-104");
295 break;
510e0e2f 296 case 6:
857d966e
MH
297 printf("AES-128-CMAC");
298 break;
299 default:
332769c6 300 printf("%.02x-%.02x-%.02x:%d",
5594fd23 301 data[0], data[1] ,data[2], data[3]);
857d966e
MH
302 break;
303 }
304 } else
332769c6 305 printf("%.02x-%.02x-%.02x:%d",
5594fd23 306 data[0], data[1] ,data[2], data[3]);
857d966e
MH
307}
308
83b4934c 309static void print_auth(const uint8_t *data)
857d966e
MH
310{
311 if (memcmp(data, wifi_oui, 3) == 0) {
312 switch (data[3]) {
510e0e2f 313 case 1:
857d966e
MH
314 printf("IEEE 802.1X");
315 break;
510e0e2f 316 case 2:
857d966e
MH
317 printf("PSK");
318 break;
319 default:
332769c6 320 printf("%.02x-%.02x-%.02x:%d",
5594fd23 321 data[0], data[1] ,data[2], data[3]);
857d966e
MH
322 break;
323 }
324 } else if (memcmp(data, ieee80211_oui, 3) == 0) {
325 switch (data[3]) {
510e0e2f 326 case 1:
857d966e
MH
327 printf("IEEE 802.1X");
328 break;
510e0e2f 329 case 2:
857d966e
MH
330 printf("PSK");
331 break;
0fe1c415
JB
332 case 3:
333 printf("FT/IEEE 802.1X");
334 break;
335 case 4:
336 printf("FT/PSK");
337 break;
338 case 5:
339 printf("IEEE 802.1X/SHA-256");
340 break;
341 case 6:
342 printf("PSK/SHA-256");
343 break;
857d966e 344 default:
332769c6 345 printf("%.02x-%.02x-%.02x:%d",
5594fd23 346 data[0], data[1] ,data[2], data[3]);
857d966e
MH
347 break;
348 }
349 } else
332769c6 350 printf("%.02x-%.02x-%.02x:%d",
5594fd23 351 data[0], data[1] ,data[2], data[3]);
857d966e
MH
352}
353
83b4934c
JB
354static void print_rsn_ie(const char *defcipher, const char *defauth,
355 uint8_t len, const uint8_t *data)
857d966e
MH
356{
357 bool first = true;
358 __u16 version, count, capa;
359 int i;
360
857d966e
MH
361 version = data[0] + (data[1] << 8);
362 tab_on_first(&first);
363 printf("\t * Version: %d\n", version);
364
365 data += 2;
366 len -= 2;
367
368 if (len < 4) {
369 tab_on_first(&first);
370 printf("\t * Group cipher: %s\n", defcipher);
371 printf("\t * Pairwise ciphers: %s\n", defcipher);
372 return;
373 }
374
375 tab_on_first(&first);
376 printf("\t * Group cipher: ");
377 print_cipher(data);
378 printf("\n");
379
380 data += 4;
381 len -= 4;
382
383 if (len < 2) {
384 tab_on_first(&first);
385 printf("\t * Pairwise ciphers: %s\n", defcipher);
386 return;
387 }
388
389 count = data[0] | (data[1] << 8);
31d477fb
MH
390 if (2 + (count * 4) > len)
391 goto invalid;
392
857d966e
MH
393 tab_on_first(&first);
394 printf("\t * Pairwise ciphers:");
31d477fb 395 for (i = 0; i < count; i++) {
857d966e
MH
396 printf(" ");
397 print_cipher(data + 2 + (i * 4));
398 }
399 printf("\n");
400
401 data += 2 + (count * 4);
402 len -= 2 + (count * 4);
403
404 if (len < 2) {
405 tab_on_first(&first);
406 printf("\t * Authentication suites: %s\n", defauth);
407 return;
408 }
409
410 count = data[0] | (data[1] << 8);
31d477fb
MH
411 if (2 + (count * 4) > len)
412 goto invalid;
413
857d966e
MH
414 tab_on_first(&first);
415 printf("\t * Authentication suites:");
83b4934c 416 for (i = 0; i < count; i++) {
857d966e
MH
417 printf(" ");
418 print_auth(data + 2 + (i * 4));
419 }
420 printf("\n");
421
422 data += 2 + (count * 4);
423 len -= 2 + (count * 4);
424
6a4f24e8
JB
425 if (len >= 2) {
426 capa = data[0] | (data[1] << 8);
427 tab_on_first(&first);
cadbe89a
JB
428 printf("\t * Capabilities:");
429 if (capa & 0x0001)
430 printf(" PreAuth");
431 if (capa & 0x0002)
432 printf(" NoPairwise");
433 switch ((capa & 0x000c) >> 2) {
434 case 0:
435 break;
436 case 1:
437 printf(" 2-PTKSA-RC");
438 break;
439 case 2:
440 printf(" 4-PTKSA-RC");
441 break;
442 case 3:
443 printf(" 16-PTKSA-RC");
444 break;
445 }
446 switch ((capa & 0x0030) >> 4) {
447 case 0:
448 break;
449 case 1:
450 printf(" 2-GTKSA-RC");
451 break;
452 case 2:
453 printf(" 4-GTKSA-RC");
454 break;
455 case 3:
456 printf(" 16-GTKSA-RC");
457 break;
458 }
459 if (capa & 0x0040)
460 printf(" MFP-required");
461 if (capa & 0x0080)
462 printf(" MFP-capable");
463 if (capa & 0x0200)
464 printf(" Peerkey-enabled");
465 if (capa & 0x0400)
466 printf(" SPP-AMSDU-capable");
467 if (capa & 0x0800)
468 printf(" SPP-AMSDU-required");
469 printf(" (0x%.4x)\n", capa);
6a4f24e8
JB
470 data += 2;
471 len -= 2;
472 }
31d477fb 473
5ba3f523
JB
474 if (len >= 2) {
475 int pmkid_count = data[0] | (data[1] << 8);
476
477 if (len >= 2 + 16 * pmkid_count) {
478 tab_on_first(&first);
479 printf("\t * %d PMKIDs\n", pmkid_count);
480 /* not printing PMKID values */
481 data += 2 + 16 * pmkid_count;
482 len -= 2 + 16 * pmkid_count;
483 } else
484 goto invalid;
485 }
486
487 if (len >= 4) {
488 tab_on_first(&first);
489 printf("\t * Group mgmt cipher suite: ");
490 print_cipher(data);
491 printf("\n");
492 data += 4;
493 len -= 4;
494 }
495
cadbe89a 496 invalid:
31d477fb
MH
497 if (len != 0) {
498 printf("\t\t * bogus tail data (%d):", len);
499 while (len) {
500 printf(" %.2x", *data);
501 data++;
502 len--;
503 }
504 printf("\n");
505 }
857d966e
MH
506}
507
83b4934c 508static void print_rsn(const uint8_t type, uint8_t len, const uint8_t *data)
857d966e 509{
83b4934c 510 print_rsn_ie("CCMP", "IEEE 802.1X", len, data);
857d966e
MH
511}
512
0c445c24
LR
513static void print_ht_capa(const uint8_t type, uint8_t len, const uint8_t *data)
514{
357c1a5d 515 printf("\n");
7ddfb679
JB
516 print_ht_capability(data[0] | (data[1] << 8));
517 print_ampdu_length(data[2] & 3);
c79c7464 518 print_ampdu_spacing((data[2] >> 2) & 7);
7ddfb679 519 print_ht_mcs(data + 3);
0c445c24
LR
520}
521
be7602fb
JB
522static void print_ht_op(const uint8_t type, uint8_t len, const uint8_t *data)
523{
524 static const char *offset[4] = {
525 "no secondary",
526 "above",
527 "[reserved!]",
528 "below",
529 };
530 static const char *protection[4] = {
531 "no",
532 "nonmember",
533 "20 MHz",
534 "non-HT mixed",
535 };
536 static const char *sta_chan_width[2] = {
537 "20 MHz",
538 "any",
539 };
540
541 printf("\n");
542 printf("\t\t * primary channel: %d\n", data[0]);
543 printf("\t\t * secondary channel offset: %s\n",
544 offset[data[1] & 0x3]);
545 printf("\t\t * STA channel width: %s\n", sta_chan_width[(data[1] & 0x4)>>2]);
546 printf("\t\t * RIFS: %d\n", (data[1] & 0x8)>>3);
547 printf("\t\t * HT protection: %s\n", protection[data[2] & 0x3]);
548 printf("\t\t * non-GF present: %d\n", (data[2] & 0x4) >> 2);
549 printf("\t\t * OBSS non-GF present: %d\n", (data[2] & 0x10) >> 4);
550 printf("\t\t * dual beacon: %d\n", (data[4] & 0x40) >> 6);
551 printf("\t\t * dual CTS protection: %d\n", (data[4] & 0x80) >> 7);
552 printf("\t\t * STBC beacon: %d\n", data[5] & 0x1);
553 printf("\t\t * L-SIG TXOP Prot: %d\n", (data[5] & 0x2) >> 1);
554 printf("\t\t * PCO active: %d\n", (data[5] & 0x4) >> 2);
555 printf("\t\t * PCO phase: %d\n", (data[5] & 0x8) >> 3);
556}
557
83b4934c 558static void print_capabilities(const uint8_t type, uint8_t len, const uint8_t *data)
9b880b00 559{
31d2d259
JB
560 int i, base, bit;
561 bool first = true;
562
563
564 for (i = 0; i < len; i++) {
565 base = i * 8;
566
567 for (bit = 0; bit < 8; bit++) {
568 if (!(data[i] & (1 << bit)))
569 continue;
570
571 if (!first)
572 printf(",");
573 else
574 first = false;
575
576 switch (bit + base) {
577 case 0:
578 printf(" HT Information Exchange Supported");
579 break;
580 case 1:
581 printf(" On-demand Beacon");
582 break;
583 case 2:
584 printf(" Extended Channel Switching");
585 break;
586 case 3:
587 printf(" Wave Indication");
588 break;
589 case 4:
590 printf(" PSMP Capability");
591 break;
592 case 5:
593 printf(" Service Interval Granularity");
594 break;
595 case 6:
596 printf(" S-PSMP Capability");
597 break;
598 default:
599 printf(" %d", bit);
600 break;
601 }
602 }
603 }
9b880b00 604
9b880b00
MH
605 printf("\n");
606}
607
575280cc
JM
608static void print_tim(const uint8_t type, uint8_t len, const uint8_t *data)
609{
610 printf(" DTIM Count %u DTIM Period %u Bitmap Control 0x%x "
611 "Bitmap[0] 0x%x",
612 data[0], data[1], data[2], data[3]);
613 if (len - 4)
614 printf(" (+ %u octet%s)", len - 4, len - 4 == 1 ? "" : "s");
615 printf("\n");
616}
617
83b4934c
JB
618struct ie_print {
619 const char *name;
620 void (*print)(const uint8_t type, uint8_t len, const uint8_t *data);
621 uint8_t minlen, maxlen;
febeb0c0 622 uint8_t flags;
764fe753
JB
623};
624
83b4934c
JB
625static void print_ie(const struct ie_print *p, const uint8_t type,
626 uint8_t len, const uint8_t *data)
4673a894 627{
83b4934c
JB
628 int i;
629
630 if (!p->print)
631 return;
632
633 printf("\t%s:", p->name);
634 if (len < p->minlen || len > p->maxlen) {
635 if (len > 1) {
636 printf(" <invalid: %d bytes:", len);
637 for (i = 0; i < len; i++)
638 printf(" %.02x", data[i]);
639 printf(">\n");
640 } else if (len)
641 printf(" <invalid: 1 byte: %.02x>\n", data[0]);
642 else
643 printf(" <invalid: no data>\n");
644 return;
645 }
646
647 p->print(type, len, data);
648}
649
650#define PRINT_IGN { \
651 .name = "IGNORE", \
652 .print = NULL, \
653 .minlen = 0, \
654 .maxlen = 255, \
4673a894
JB
655}
656
83b4934c 657static const struct ie_print ieprinters[] = {
febeb0c0
JB
658 [0] = { "SSID", print_ssid, 0, 32, BIT(PRINT_SCAN) | BIT(PRINT_LINK), },
659 [1] = { "Supported rates", print_supprates, 0, 255, BIT(PRINT_SCAN), },
014d581a 660 [3] = { "DS Parameter set", print_ds, 1, 1, BIT(PRINT_SCAN), },
575280cc 661 [5] = { "TIM", print_tim, 4, 255, BIT(PRINT_SCAN), },
febeb0c0
JB
662 [7] = { "Country", print_country, 3, 255, BIT(PRINT_SCAN), },
663 [32] = { "Power constraint", print_powerconstraint, 1, 1, BIT(PRINT_SCAN), },
664 [42] = { "ERP", print_erp, 1, 255, BIT(PRINT_SCAN), },
a2e61861 665 [45] = { "HT capabilities", print_ht_capa, 26, 26, BIT(PRINT_SCAN), },
be7602fb 666 [61] = { "HT operation", print_ht_op, 22, 22, BIT(PRINT_SCAN), },
febeb0c0
JB
667 [48] = { "RSN", print_rsn, 2, 255, BIT(PRINT_SCAN), },
668 [50] = { "Extended supported rates", print_supprates, 0, 255, BIT(PRINT_SCAN), },
669 [127] = { "Extended capabilities", print_capabilities, 0, 255, BIT(PRINT_SCAN), },
83b4934c
JB
670};
671
672static void print_wifi_wpa(const uint8_t type, uint8_t len, const uint8_t *data)
673{
674 print_rsn_ie("TKIP", "IEEE 802.1X", len, data);
675}
676
1cab57eb
JB
677static bool print_wifi_wmm_param(const uint8_t *data, uint8_t len)
678{
679 int i;
680 static const char *aci_tbl[] = { "BE", "BK", "VI", "VO" };
681
682 if (len < 19)
683 goto invalid;
684
685 if (data[0] != 1) {
cee4fe20 686 printf("Parameter: not version 1: ");
1cab57eb
JB
687 return false;
688 }
689
89ea706f 690 printf("\t * Parameter version 1");
1cab57eb
JB
691
692 data++;
693
694 if (data[0] & 0x80)
89ea706f 695 printf("\n\t\t * u-APSD");
1cab57eb
JB
696
697 data += 2;
698
699 for (i = 0; i < 4; i++) {
89ea706f 700 printf("\n\t\t * %s:", aci_tbl[(data[0] >> 5) & 3]);
1cab57eb
JB
701 if (data[4] & 0x10)
702 printf(" acm");
703 printf(" CW %d-%d", (1 << (data[1] & 0xf)) - 1,
704 (1 << (data[1] >> 4)) - 1);
a2a4c265 705 printf(", AIFSN %d", data[0] & 0xf);
1cab57eb 706 if (data[2] | data[3])
cee4fe20 707 printf(", TXOP %d usec", (data[2] + (data[3] << 8)) * 32);
1cab57eb
JB
708 data += 4;
709 }
710
711 printf("\n");
712 return true;
713
714 invalid:
715 printf("invalid: ");
716 return false;
717}
718
83b4934c 719static void print_wifi_wmm(const uint8_t type, uint8_t len, const uint8_t *data)
6ff0c93a
MH
720{
721 int i;
722
6ff0c93a
MH
723 switch (data[0]) {
724 case 0x00:
83b4934c 725 printf(" information:");
6ff0c93a
MH
726 break;
727 case 0x01:
1cab57eb
JB
728 if (print_wifi_wmm_param(data + 1, len - 1))
729 return;
6ff0c93a
MH
730 break;
731 default:
83b4934c 732 printf(" type %d:", data[0]);
6ff0c93a
MH
733 break;
734 }
735
1cab57eb
JB
736 for(i = 1; i < len; i++)
737 printf(" %.02x", data[i]);
6ff0c93a
MH
738 printf("\n");
739}
740
a6816965
JM
741static const char * wifi_wps_dev_passwd_id(uint16_t id)
742{
743 switch (id) {
744 case 0:
745 return "Default (PIN)";
746 case 1:
747 return "User-specified";
748 case 2:
749 return "Machine-specified";
750 case 3:
751 return "Rekey";
752 case 4:
753 return "PushButton";
754 case 5:
755 return "Registrar-specified";
756 default:
757 return "??";
758 }
759}
760
83b4934c 761static void print_wifi_wps(const uint8_t type, uint8_t len, const uint8_t *data)
4673a894
JB
762{
763 bool first = true;
764 __u16 subtype, sublen;
765
4673a894
JB
766 while (len >= 4) {
767 subtype = (data[0] << 8) + data[1];
768 sublen = (data[2] << 8) + data[3];
769 if (sublen > len)
770 break;
771
772 switch (subtype) {
773 case 0x104a:
774 tab_on_first(&first);
dffc6750 775 printf("\t * Version: %d.%d\n", data[4] >> 4, data[4] & 0xF);
4673a894
JB
776 break;
777 case 0x1011:
778 tab_on_first(&first);
779 printf("\t * Device name: %.*s\n", sublen, data + 4);
780 break;
a6816965
JM
781 case 0x1012: {
782 uint16_t id;
783 tab_on_first(&first);
784 if (sublen != 2) {
785 printf("\t * Device Password ID: (invalid "
786 "length %d)\n", sublen);
787 break;
788 }
789 id = data[4] << 8 | data[5];
790 printf("\t * Device Password ID: %u (%s)\n",
791 id, wifi_wps_dev_passwd_id(id));
792 break;
793 }
4673a894
JB
794 case 0x1021:
795 tab_on_first(&first);
796 printf("\t * Manufacturer: %.*s\n", sublen, data + 4);
797 break;
798 case 0x1023:
799 tab_on_first(&first);
800 printf("\t * Model: %.*s\n", sublen, data + 4);
801 break;
a6816965
JM
802 case 0x1024:
803 tab_on_first(&first);
804 printf("\t * Model Number: %.*s\n", sublen, data + 4);
805 break;
806 case 0x103b: {
807 __u8 val = data[4];
808 tab_on_first(&first);
809 printf("\t * Response Type: %d%s\n",
810 val, val == 3 ? " (AP)" : "");
811 break;
812 }
813 case 0x103c: {
814 __u8 val = data[4];
815 tab_on_first(&first);
816 printf("\t * RF Bands: 0x%x\n", val);
817 break;
818 }
819 case 0x1041: {
820 __u8 val = data[4];
821 tab_on_first(&first);
822 printf("\t * Selected Registrar: 0x%x\n", val);
823 break;
824 }
825 case 0x1042:
826 tab_on_first(&first);
827 printf("\t * Serial Number: %.*s\n", sublen, data + 4);
828 break;
829 case 0x1044: {
830 __u8 val = data[4];
831 tab_on_first(&first);
832 printf("\t * Wi-Fi Protected Setup State: %d%s%s\n",
833 val,
834 val == 1 ? " (Unconfigured)" : "",
835 val == 2 ? " (Configured)" : "");
836 break;
837 }
09fe09dc
JB
838 case 0x1047:
839 tab_on_first(&first);
840 printf("\t * UUID: ");
841 if (sublen != 16) {
842 printf("(invalid, length=%d)\n", sublen);
843 break;
844 }
845 printf("%02x%02x%02x%02x-%02x%02x-%02x%02x-"
846 "%02x%02x-%02x%02x%02x%02x%02x%02x\n",
847 data[4], data[5], data[6], data[7],
848 data[8], data[9], data[10], data[11],
849 data[12], data[13], data[14], data[15],
850 data[16], data[17], data[18], data[19]);
851 break;
a6816965
JM
852 case 0x1054: {
853 tab_on_first(&first);
854 if (sublen != 8) {
855 printf("\t * Primary Device Type: (invalid "
856 "length %d)\n", sublen);
857 break;
858 }
859 printf("\t * Primary Device Type: "
860 "%u-%02x%02x%02x%02x-%u\n",
861 data[4] << 8 | data[5],
862 data[6], data[7], data[8], data[9],
863 data[10] << 8 | data[11]);
864 break;
865 }
7ee5a865 866 case 0x1057: {
fe31a22e 867 __u8 val = data[4];
7ee5a865 868 tab_on_first(&first);
fe31a22e 869 printf("\t * AP setup locked: 0x%.2x\n", val);
7ee5a865
JB
870 break;
871 }
a6816965
JM
872 case 0x1008:
873 case 0x1053: {
4673a894
JB
874 __u16 meth = (data[4] << 8) + data[5];
875 bool comma = false;
876 tab_on_first(&first);
a6816965
JM
877 printf("\t * %sConfig methods:",
878 subtype == 0x1053 ? "Selected Registrar ": "");
4673a894
JB
879#define T(bit, name) do { \
880 if (meth & (1<<bit)) { \
881 if (comma) \
882 printf(","); \
883 comma = true; \
884 printf(" " name); \
885 } } while (0)
886 T(0, "USB");
887 T(1, "Ethernet");
888 T(2, "Label");
889 T(3, "Display");
890 T(4, "Ext. NFC");
891 T(5, "Int. NFC");
892 T(6, "NFC Intf.");
893 T(7, "PBC");
894 T(8, "Keypad");
895 printf("\n");
896 break;
897#undef T
898 }
2650d46b
JB
899 default: {
900 const __u8 *subdata = data + 4;
901 __u16 tmplen = sublen;
902
903 tab_on_first(&first);
904 printf("\t * Unknown TLV (%#.4x, %d bytes):",
905 subtype, tmplen);
906 while (tmplen) {
907 printf(" %.2x", *subdata);
908 subdata++;
909 tmplen--;
910 }
911 printf("\n");
4673a894
JB
912 break;
913 }
2650d46b 914 }
4673a894
JB
915
916 data += sublen + 4;
917 len -= sublen + 4;
918 }
919
920 if (len != 0) {
921 printf("\t\t * bogus tail data (%d):", len);
922 while (len) {
923 printf(" %.2x", *data);
924 data++;
925 len--;
926 }
927 printf("\n");
928 }
929}
930
83b4934c 931static const struct ie_print wifiprinters[] = {
febeb0c0
JB
932 [1] = { "WPA", print_wifi_wpa, 2, 255, BIT(PRINT_SCAN), },
933 [2] = { "WMM", print_wifi_wmm, 1, 255, BIT(PRINT_SCAN), },
934 [4] = { "WPS", print_wifi_wps, 0, 255, BIT(PRINT_SCAN), },
4673a894
JB
935};
936
764fe753 937static void print_vendor(unsigned char len, unsigned char *data,
febeb0c0 938 bool unknown, enum print_ie_type ptype)
3563f4c5
JB
939{
940 int i;
941
fbf80af5 942 if (len < 3) {
4673a894 943 printf("\tVendor specific: <too short> data:");
fbf80af5
JB
944 for(i = 0; i < len; i++)
945 printf(" %.02x", data[i]);
946 printf("\n");
947 return;
948 }
949
857d966e 950 if (len >= 4 && memcmp(data, wifi_oui, 3) == 0) {
febeb0c0
JB
951 if (data[3] < ARRAY_SIZE(wifiprinters) &&
952 wifiprinters[data[3]].name &&
953 wifiprinters[data[3]].flags & BIT(ptype)) {
83b4934c
JB
954 print_ie(&wifiprinters[data[3]], data[3], len - 4, data + 4);
955 return;
956 }
febeb0c0 957 if (!unknown)
4673a894 958 return;
857d966e 959 printf("\tWiFi OUI %#.2x, data:", data[3]);
4673a894
JB
960 for(i = 0; i < len - 4; i++)
961 printf(" %.02x", data[i + 4]);
962 printf("\n");
963 return;
964 }
965
febeb0c0 966 if (!unknown)
764fe753
JB
967 return;
968
fbf80af5 969 printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:",
3563f4c5 970 data[0], data[1], data[2]);
fbf80af5
JB
971 for (i = 3; i < len; i++)
972 printf(" %.2x", data[i]);
3563f4c5
JB
973 printf("\n");
974}
975
febeb0c0
JB
976void print_ies(unsigned char *ie, int ielen, bool unknown,
977 enum print_ie_type ptype)
3563f4c5
JB
978{
979 while (ielen >= 2 && ielen >= ie[1]) {
febeb0c0
JB
980 if (ie[0] < ARRAY_SIZE(ieprinters) &&
981 ieprinters[ie[0]].name &&
982 ieprinters[ie[0]].flags & BIT(ptype)) {
83b4934c 983 print_ie(&ieprinters[ie[0]], ie[0], ie[1], ie + 2);
764fe753 984 } else if (ie[0] == 221 /* vendor */) {
febeb0c0
JB
985 print_vendor(ie[1], ie + 2, unknown, ptype);
986 } else if (unknown) {
3563f4c5
JB
987 int i;
988
8086b700 989 printf("\tUnknown IE (%d):", ie[0]);
3563f4c5 990 for (i=0; i<ie[1]; i++)
8086b700 991 printf(" %.2x", ie[2+i]);
3563f4c5
JB
992 printf("\n");
993 }
994 ielen -= ie[1] + 2;
995 ie += ie[1] + 2;
996 }
997}
998
999static int print_bss_handler(struct nl_msg *msg, void *arg)
1000{
1001 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1002 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1003 struct nlattr *bss[NL80211_BSS_MAX + 1];
1004 char mac_addr[20], dev[20];
1005 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1006 [NL80211_BSS_TSF] = { .type = NLA_U64 },
1007 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
1008 [NL80211_BSS_BSSID] = { },
1009 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
1010 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
1011 [NL80211_BSS_INFORMATION_ELEMENTS] = { },
f2e17e1f
JB
1012 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
1013 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
a56117a6 1014 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
c04a78df 1015 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
575280cc 1016 [NL80211_BSS_BEACON_IES] = { },
3563f4c5 1017 };
febeb0c0 1018 struct scan_params *params = arg;
1c5bcd9c 1019 int show = params->show_both_ie_sets ? 2 : 1;
3563f4c5
JB
1020
1021 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1022 genlmsg_attrlen(gnlh, 0), NULL);
1023
1024 if (!tb[NL80211_ATTR_BSS]) {
5fe70c0e 1025 fprintf(stderr, "bss info missing!\n");
3563f4c5
JB
1026 return NL_SKIP;
1027 }
1028 if (nla_parse_nested(bss, NL80211_BSS_MAX,
1029 tb[NL80211_ATTR_BSS],
1030 bss_policy)) {
5fe70c0e 1031 fprintf(stderr, "failed to parse nested attributes!\n");
3563f4c5
JB
1032 return NL_SKIP;
1033 }
1034
1035 if (!bss[NL80211_BSS_BSSID])
1036 return NL_SKIP;
1037
1038 mac_addr_n2a(mac_addr, nla_data(bss[NL80211_BSS_BSSID]));
1039 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
a56117a6
JB
1040 printf("BSS %s (on %s)", mac_addr, dev);
1041
1042 if (bss[NL80211_BSS_STATUS]) {
1043 switch (nla_get_u32(bss[NL80211_BSS_STATUS])) {
1044 case NL80211_BSS_STATUS_AUTHENTICATED:
1045 printf(" -- authenticated");
1046 break;
1047 case NL80211_BSS_STATUS_ASSOCIATED:
1048 printf(" -- associated");
1049 break;
1050 case NL80211_BSS_STATUS_IBSS_JOINED:
1051 printf(" -- joined");
1052 break;
1053 default:
1054 printf(" -- unknown status: %d",
1055 nla_get_u32(bss[NL80211_BSS_STATUS]));
1056 break;
1057 }
1058 }
1059 printf("\n");
3563f4c5 1060
e7109a8a
JB
1061 if (bss[NL80211_BSS_TSF]) {
1062 unsigned long long tsf;
1063 tsf = (unsigned long long)nla_get_u64(bss[NL80211_BSS_TSF]);
1064 printf("\tTSF: %llu usec (%llud, %.2lld:%.2llu:%.2llu)\n",
1065 tsf, tsf/1000/1000/60/60/24, (tsf/1000/1000/60/60) % 24,
1066 (tsf/1000/1000/60) % 60, (tsf/1000/1000) % 60);
1067 }
3563f4c5
JB
1068 if (bss[NL80211_BSS_FREQUENCY])
1069 printf("\tfreq: %d\n",
1070 nla_get_u32(bss[NL80211_BSS_FREQUENCY]));
1071 if (bss[NL80211_BSS_BEACON_INTERVAL])
1072 printf("\tbeacon interval: %d\n",
1073 nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]));
92a04ecd
MH
1074 if (bss[NL80211_BSS_CAPABILITY]) {
1075 __u16 capa = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
1076 printf("\tcapability:");
1077 if (capa & WLAN_CAPABILITY_ESS)
1078 printf(" ESS");
1079 if (capa & WLAN_CAPABILITY_IBSS)
1080 printf(" IBSS");
1081 if (capa & WLAN_CAPABILITY_PRIVACY)
1082 printf(" Privacy");
1083 if (capa & WLAN_CAPABILITY_SHORT_PREAMBLE)
1084 printf(" ShortPreamble");
1085 if (capa & WLAN_CAPABILITY_PBCC)
1086 printf(" PBCC");
1087 if (capa & WLAN_CAPABILITY_CHANNEL_AGILITY)
1088 printf(" ChannelAgility");
1089 if (capa & WLAN_CAPABILITY_SPECTRUM_MGMT)
1090 printf(" SpectrumMgmt");
1091 if (capa & WLAN_CAPABILITY_QOS)
1092 printf(" QoS");
1093 if (capa & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1094 printf(" ShortSlotTime");
1095 if (capa & WLAN_CAPABILITY_APSD)
1096 printf(" APSD");
1097 if (capa & WLAN_CAPABILITY_DSSS_OFDM)
1098 printf(" DSSS-OFDM");
1099 printf(" (0x%.4x)\n", capa);
1100 }
f2e17e1f
JB
1101 if (bss[NL80211_BSS_SIGNAL_MBM]) {
1102 int s = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
1103 printf("\tsignal: %d.%.2d dBm\n", s/100, s%100);
1104 }
1105 if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
1106 unsigned char s = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
1107 printf("\tsignal: %d/100\n", s);
1108 }
c04a78df
HS
1109 if (bss[NL80211_BSS_SEEN_MS_AGO]) {
1110 int age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
1111 printf("\tlast seen: %d ms ago\n", age);
1112 }
1c5bcd9c
JB
1113
1114 if (bss[NL80211_BSS_INFORMATION_ELEMENTS] && show--) {
575280cc
JM
1115 if (bss[NL80211_BSS_BEACON_IES])
1116 printf("\tInformation elements from Probe Response "
1117 "frame:\n");
3563f4c5 1118 print_ies(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
764fe753 1119 nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
febeb0c0 1120 params->unknown, params->type);
575280cc 1121 }
1c5bcd9c 1122 if (bss[NL80211_BSS_BEACON_IES] && show--) {
575280cc
JM
1123 printf("\tInformation elements from Beacon frame:\n");
1124 print_ies(nla_data(bss[NL80211_BSS_BEACON_IES]),
1125 nla_len(bss[NL80211_BSS_BEACON_IES]),
1126 params->unknown, params->type);
1127 }
3563f4c5
JB
1128
1129 return NL_SKIP;
1130}
1131
764fe753 1132static struct scan_params scan_params;
3563f4c5 1133
7c37a24d
JB
1134static int handle_scan_dump(struct nl80211_state *state,
1135 struct nl_cb *cb,
3563f4c5
JB
1136 struct nl_msg *msg,
1137 int argc, char **argv)
1138{
764fe753
JB
1139 if (argc > 1)
1140 return 1;
1141
1c5bcd9c
JB
1142 memset(&scan_params, 0, sizeof(scan_params));
1143
764fe753
JB
1144 if (argc == 1 && !strcmp(argv[0], "-u"))
1145 scan_params.unknown = true;
575280cc
JM
1146 else if (argc == 1 && !strcmp(argv[0], "-b"))
1147 scan_params.show_both_ie_sets = true;
764fe753 1148
febeb0c0
JB
1149 scan_params.type = PRINT_SCAN;
1150
764fe753
JB
1151 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_bss_handler,
1152 &scan_params);
3563f4c5
JB
1153 return 0;
1154}
a5fe4ef2
JB
1155
1156static int handle_scan_combined(struct nl80211_state *state,
1157 struct nl_cb *cb,
1158 struct nl_msg *msg,
1159 int argc, char **argv)
1160{
559a1713 1161 char **trig_argv;
a5fe4ef2
JB
1162 static char *dump_argv[] = {
1163 NULL,
1164 "scan",
1165 "dump",
92649eab 1166 NULL,
a5fe4ef2
JB
1167 };
1168 static const __u32 cmds[] = {
1169 NL80211_CMD_NEW_SCAN_RESULTS,
1170 NL80211_CMD_SCAN_ABORTED,
1171 };
559a1713 1172 int trig_argc, dump_argc, err;
a5fe4ef2 1173
559a1713
JB
1174 if (argc >= 3 && !strcmp(argv[2], "-u")) {
1175 dump_argc = 4;
1176 dump_argv[3] = "-u";
575280cc
JM
1177 } else if (argc >= 3 && !strcmp(argv[2], "-b")) {
1178 dump_argc = 4;
1179 dump_argv[3] = "-b";
559a1713
JB
1180 } else
1181 dump_argc = 3;
1182
1183 trig_argc = 3 + (argc - 2) + (3 - dump_argc);
1184 trig_argv = calloc(trig_argc, sizeof(*trig_argv));
1185 if (!trig_argv)
1186 return -ENOMEM;
a5fe4ef2 1187 trig_argv[0] = argv[0];
559a1713
JB
1188 trig_argv[1] = "scan";
1189 trig_argv[2] = "trigger";
1190 int i;
1191 for (i = 0; i < argc - 2 - (dump_argc - 3); i++)
1192 trig_argv[i + 3] = argv[i + 2 + (dump_argc - 3)];
1193 err = handle_cmd(state, II_NETDEV, trig_argc, trig_argv);
1194 free(trig_argv);
a5fe4ef2
JB
1195 if (err)
1196 return err;
1197
61725dbe
JB
1198 /*
1199 * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION
1200 *
1201 * This code has a bug, which requires creating a separate
1202 * nl80211 socket to fix:
1203 * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or
1204 * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel
1205 * before (!) we listen to it, because we only start listening
1206 * after we send our scan request.
1207 *
1208 * Doing it the other way around has a race condition as well,
1209 * if you first open the events socket you may get a notification
1210 * for a previous scan.
1211 *
1212 * The only proper way to fix this would be to listen to events
1213 * before sending the command, and for the kernel to send the
1214 * scan request along with the event, so that you can match up
1215 * whether the scan you requested was finished or aborted (this
1216 * may result in processing a scan that another application
1217 * requested, but that doesn't seem to be a problem).
1218 *
1219 * Alas, the kernel doesn't do that (yet).
1220 */
1221
a5fe4ef2
JB
1222 if (listen_events(state, ARRAY_SIZE(cmds), cmds) ==
1223 NL80211_CMD_SCAN_ABORTED) {
1224 printf("scan aborted!\n");
1225 return 0;
1226 }
1227
1228 dump_argv[0] = argv[0];
92649eab 1229 return handle_cmd(state, II_NETDEV, dump_argc, dump_argv);
a5fe4ef2 1230}
64797a7f 1231TOPLEVEL(scan, "[-u] [freq <freq>*] [ies <hex as 00:11:..>] [ssid <ssid>*|passive]", 0, 0,
6ca98d28
JB
1232 CIB_NETDEV, handle_scan_combined,
1233 "Scan on the given frequencies and probe for the given SSIDs\n"
1234 "(or wildcard if not given) unless passive scanning is requested.\n"
64797a7f
JB
1235 "If -u is specified print unknown data in the scan results.\n"
1236 "Specified (vendor) IEs must be well-formed.");
4698bfc2
JB
1237COMMAND(scan, dump, "[-u]",
1238 NL80211_CMD_GET_SCAN, NLM_F_DUMP, CIB_NETDEV, handle_scan_dump,
1239 "Dump the current scan results. If -u is specified, print unknown\n"
1240 "data in scan results.");
64797a7f 1241COMMAND(scan, trigger, "[freq <freq>*] [ies <hex as 00:11:..>] [ssid <ssid>*|passive]",
4698bfc2
JB
1242 NL80211_CMD_TRIGGER_SCAN, 0, CIB_NETDEV, handle_scan,
1243 "Trigger a scan on the given frequencies with probing for the given\n"
1244 "SSIDs (or wildcard if not given) unless passive scanning is requested.");