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