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