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