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