]> git.ipfire.org Git - thirdparty/iw.git/blame - scan.c
always use libnl 3.2 as 3.2, not 3.0
[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), },
678 [127] = { "Extended capabilities", print_capabilities, 0, 255, BIT(PRINT_SCAN), },
83b4934c
JB
679};
680
681static void print_wifi_wpa(const uint8_t type, uint8_t len, const uint8_t *data)
682{
683 print_rsn_ie("TKIP", "IEEE 802.1X", len, data);
684}
685
1cab57eb
JB
686static bool print_wifi_wmm_param(const uint8_t *data, uint8_t len)
687{
688 int i;
689 static const char *aci_tbl[] = { "BE", "BK", "VI", "VO" };
690
691 if (len < 19)
692 goto invalid;
693
694 if (data[0] != 1) {
cee4fe20 695 printf("Parameter: not version 1: ");
1cab57eb
JB
696 return false;
697 }
698
89ea706f 699 printf("\t * Parameter version 1");
1cab57eb
JB
700
701 data++;
702
703 if (data[0] & 0x80)
89ea706f 704 printf("\n\t\t * u-APSD");
1cab57eb
JB
705
706 data += 2;
707
708 for (i = 0; i < 4; i++) {
89ea706f 709 printf("\n\t\t * %s:", aci_tbl[(data[0] >> 5) & 3]);
1cab57eb
JB
710 if (data[4] & 0x10)
711 printf(" acm");
712 printf(" CW %d-%d", (1 << (data[1] & 0xf)) - 1,
713 (1 << (data[1] >> 4)) - 1);
a2a4c265 714 printf(", AIFSN %d", data[0] & 0xf);
1cab57eb 715 if (data[2] | data[3])
cee4fe20 716 printf(", TXOP %d usec", (data[2] + (data[3] << 8)) * 32);
1cab57eb
JB
717 data += 4;
718 }
719
720 printf("\n");
721 return true;
722
723 invalid:
724 printf("invalid: ");
725 return false;
726}
727
83b4934c 728static void print_wifi_wmm(const uint8_t type, uint8_t len, const uint8_t *data)
6ff0c93a
MH
729{
730 int i;
731
6ff0c93a
MH
732 switch (data[0]) {
733 case 0x00:
83b4934c 734 printf(" information:");
6ff0c93a
MH
735 break;
736 case 0x01:
1cab57eb
JB
737 if (print_wifi_wmm_param(data + 1, len - 1))
738 return;
6ff0c93a
MH
739 break;
740 default:
83b4934c 741 printf(" type %d:", data[0]);
6ff0c93a
MH
742 break;
743 }
744
1cab57eb
JB
745 for(i = 1; i < len; i++)
746 printf(" %.02x", data[i]);
6ff0c93a
MH
747 printf("\n");
748}
749
a6816965
JM
750static const char * wifi_wps_dev_passwd_id(uint16_t id)
751{
752 switch (id) {
753 case 0:
754 return "Default (PIN)";
755 case 1:
756 return "User-specified";
757 case 2:
758 return "Machine-specified";
759 case 3:
760 return "Rekey";
761 case 4:
762 return "PushButton";
763 case 5:
764 return "Registrar-specified";
765 default:
766 return "??";
767 }
768}
769
83b4934c 770static void print_wifi_wps(const uint8_t type, uint8_t len, const uint8_t *data)
4673a894
JB
771{
772 bool first = true;
773 __u16 subtype, sublen;
774
4673a894
JB
775 while (len >= 4) {
776 subtype = (data[0] << 8) + data[1];
777 sublen = (data[2] << 8) + data[3];
778 if (sublen > len)
779 break;
780
781 switch (subtype) {
782 case 0x104a:
783 tab_on_first(&first);
dffc6750 784 printf("\t * Version: %d.%d\n", data[4] >> 4, data[4] & 0xF);
4673a894
JB
785 break;
786 case 0x1011:
787 tab_on_first(&first);
788 printf("\t * Device name: %.*s\n", sublen, data + 4);
789 break;
a6816965
JM
790 case 0x1012: {
791 uint16_t id;
792 tab_on_first(&first);
793 if (sublen != 2) {
794 printf("\t * Device Password ID: (invalid "
795 "length %d)\n", sublen);
796 break;
797 }
798 id = data[4] << 8 | data[5];
799 printf("\t * Device Password ID: %u (%s)\n",
800 id, wifi_wps_dev_passwd_id(id));
801 break;
802 }
4673a894
JB
803 case 0x1021:
804 tab_on_first(&first);
805 printf("\t * Manufacturer: %.*s\n", sublen, data + 4);
806 break;
807 case 0x1023:
808 tab_on_first(&first);
809 printf("\t * Model: %.*s\n", sublen, data + 4);
810 break;
a6816965
JM
811 case 0x1024:
812 tab_on_first(&first);
813 printf("\t * Model Number: %.*s\n", sublen, data + 4);
814 break;
815 case 0x103b: {
816 __u8 val = data[4];
817 tab_on_first(&first);
818 printf("\t * Response Type: %d%s\n",
819 val, val == 3 ? " (AP)" : "");
820 break;
821 }
822 case 0x103c: {
823 __u8 val = data[4];
824 tab_on_first(&first);
825 printf("\t * RF Bands: 0x%x\n", val);
826 break;
827 }
828 case 0x1041: {
829 __u8 val = data[4];
830 tab_on_first(&first);
831 printf("\t * Selected Registrar: 0x%x\n", val);
832 break;
833 }
834 case 0x1042:
835 tab_on_first(&first);
836 printf("\t * Serial Number: %.*s\n", sublen, data + 4);
837 break;
838 case 0x1044: {
839 __u8 val = data[4];
840 tab_on_first(&first);
841 printf("\t * Wi-Fi Protected Setup State: %d%s%s\n",
842 val,
843 val == 1 ? " (Unconfigured)" : "",
844 val == 2 ? " (Configured)" : "");
845 break;
846 }
09fe09dc
JB
847 case 0x1047:
848 tab_on_first(&first);
849 printf("\t * UUID: ");
850 if (sublen != 16) {
851 printf("(invalid, length=%d)\n", sublen);
852 break;
853 }
854 printf("%02x%02x%02x%02x-%02x%02x-%02x%02x-"
855 "%02x%02x-%02x%02x%02x%02x%02x%02x\n",
856 data[4], data[5], data[6], data[7],
857 data[8], data[9], data[10], data[11],
858 data[12], data[13], data[14], data[15],
859 data[16], data[17], data[18], data[19]);
860 break;
a6816965
JM
861 case 0x1054: {
862 tab_on_first(&first);
863 if (sublen != 8) {
864 printf("\t * Primary Device Type: (invalid "
865 "length %d)\n", sublen);
866 break;
867 }
868 printf("\t * Primary Device Type: "
869 "%u-%02x%02x%02x%02x-%u\n",
870 data[4] << 8 | data[5],
871 data[6], data[7], data[8], data[9],
872 data[10] << 8 | data[11]);
873 break;
874 }
7ee5a865 875 case 0x1057: {
fe31a22e 876 __u8 val = data[4];
7ee5a865 877 tab_on_first(&first);
fe31a22e 878 printf("\t * AP setup locked: 0x%.2x\n", val);
7ee5a865
JB
879 break;
880 }
a6816965
JM
881 case 0x1008:
882 case 0x1053: {
4673a894
JB
883 __u16 meth = (data[4] << 8) + data[5];
884 bool comma = false;
885 tab_on_first(&first);
a6816965
JM
886 printf("\t * %sConfig methods:",
887 subtype == 0x1053 ? "Selected Registrar ": "");
4673a894
JB
888#define T(bit, name) do { \
889 if (meth & (1<<bit)) { \
890 if (comma) \
891 printf(","); \
892 comma = true; \
893 printf(" " name); \
894 } } while (0)
895 T(0, "USB");
896 T(1, "Ethernet");
897 T(2, "Label");
898 T(3, "Display");
899 T(4, "Ext. NFC");
900 T(5, "Int. NFC");
901 T(6, "NFC Intf.");
902 T(7, "PBC");
903 T(8, "Keypad");
904 printf("\n");
905 break;
906#undef T
907 }
2650d46b
JB
908 default: {
909 const __u8 *subdata = data + 4;
910 __u16 tmplen = sublen;
911
912 tab_on_first(&first);
913 printf("\t * Unknown TLV (%#.4x, %d bytes):",
914 subtype, tmplen);
915 while (tmplen) {
916 printf(" %.2x", *subdata);
917 subdata++;
918 tmplen--;
919 }
920 printf("\n");
4673a894
JB
921 break;
922 }
2650d46b 923 }
4673a894
JB
924
925 data += sublen + 4;
926 len -= sublen + 4;
927 }
928
929 if (len != 0) {
930 printf("\t\t * bogus tail data (%d):", len);
931 while (len) {
932 printf(" %.2x", *data);
933 data++;
934 len--;
935 }
936 printf("\n");
937 }
938}
939
83b4934c 940static const struct ie_print wifiprinters[] = {
febeb0c0
JB
941 [1] = { "WPA", print_wifi_wpa, 2, 255, BIT(PRINT_SCAN), },
942 [2] = { "WMM", print_wifi_wmm, 1, 255, BIT(PRINT_SCAN), },
943 [4] = { "WPS", print_wifi_wps, 0, 255, BIT(PRINT_SCAN), },
4673a894
JB
944};
945
9a22374a
JB
946static inline void print_p2p(const uint8_t type, uint8_t len, const uint8_t *data)
947{
948 bool first = true;
949 __u8 subtype;
950 __u16 sublen;
951
952 while (len >= 3) {
953 subtype = data[0];
954 sublen = (data[2] << 8) + data[1];
955
956 if (sublen > len - 3)
957 break;
958
959 switch (subtype) {
960 case 0x02: /* capability */
961 tab_on_first(&first);
962 if (sublen < 2) {
963 printf("\t * malformed capability\n");
964 break;
965 }
966 printf("\t * Group capa: 0x%.2x, Device capa: 0x%.2x\n",
967 data[3], data[4]);
968 break;
969 case 0x0d: /* device info */
970 if (sublen < 6 + 2 + 8 + 1) {
971 printf("\t * malformed device info\n");
972 break;
973 }
974 /* fall through for now */
975 case 0x00: /* status */
976 case 0x01: /* minor reason */
977 case 0x03: /* device ID */
978 case 0x04: /* GO intent */
979 case 0x05: /* configuration timeout */
980 case 0x06: /* listen channel */
981 case 0x07: /* group BSSID */
982 case 0x08: /* ext listen timing */
983 case 0x09: /* intended interface address */
984 case 0x0a: /* manageability */
985 case 0x0b: /* channel list */
986 case 0x0c: /* NoA */
987 case 0x0e: /* group info */
988 case 0x0f: /* group ID */
989 case 0x10: /* interface */
990 case 0x11: /* operating channel */
991 case 0x12: /* invitation flags */
992 case 0xdd: /* vendor specific */
993 default: {
994 const __u8 *subdata = data + 4;
995 __u16 tmplen = sublen;
996
997 tab_on_first(&first);
998 printf("\t * Unknown TLV (%#.2x, %d bytes):",
999 subtype, tmplen);
1000 while (tmplen) {
1001 printf(" %.2x", *subdata);
1002 subdata++;
1003 tmplen--;
1004 }
1005 printf("\n");
1006 break;
1007 }
1008 }
1009
1010 data += sublen + 3;
1011 len -= sublen + 3;
1012 }
1013
1014 if (len != 0) {
1015 tab_on_first(&first);
1016 printf("\t * bogus tail data (%d):", len);
1017 while (len) {
1018 printf(" %.2x", *data);
1019 data++;
1020 len--;
1021 }
1022 printf("\n");
1023 }
1024}
1025
1026static const struct ie_print wfa_printers[] = {
1027 [9] = { "P2P", print_p2p, 2, 255, BIT(PRINT_SCAN), },
1028};
1029
764fe753 1030static void print_vendor(unsigned char len, unsigned char *data,
febeb0c0 1031 bool unknown, enum print_ie_type ptype)
3563f4c5
JB
1032{
1033 int i;
1034
fbf80af5 1035 if (len < 3) {
4673a894 1036 printf("\tVendor specific: <too short> data:");
fbf80af5
JB
1037 for(i = 0; i < len; i++)
1038 printf(" %.02x", data[i]);
1039 printf("\n");
1040 return;
1041 }
1042
3bd60ef1 1043 if (len >= 4 && memcmp(data, ms_oui, 3) == 0) {
febeb0c0
JB
1044 if (data[3] < ARRAY_SIZE(wifiprinters) &&
1045 wifiprinters[data[3]].name &&
1046 wifiprinters[data[3]].flags & BIT(ptype)) {
83b4934c
JB
1047 print_ie(&wifiprinters[data[3]], data[3], len - 4, data + 4);
1048 return;
1049 }
febeb0c0 1050 if (!unknown)
4673a894 1051 return;
3bd60ef1 1052 printf("\tMS/WiFi %#.2x, data:", data[3]);
4673a894
JB
1053 for(i = 0; i < len - 4; i++)
1054 printf(" %.02x", data[i + 4]);
1055 printf("\n");
1056 return;
1057 }
1058
9a22374a
JB
1059 if (len >= 4 && memcmp(data, wfa_oui, 3) == 0) {
1060 if (data[3] < ARRAY_SIZE(wfa_printers) &&
1061 wfa_printers[data[3]].name &&
1062 wfa_printers[data[3]].flags & BIT(ptype)) {
1063 print_ie(&wfa_printers[data[3]], data[3], len - 4, data + 4);
1064 return;
1065 }
1066 if (!unknown)
1067 return;
1068 printf("\tWFA %#.2x, data:", data[3]);
1069 for(i = 0; i < len - 4; i++)
1070 printf(" %.02x", data[i + 4]);
1071 printf("\n");
1072 return;
1073 }
1074
febeb0c0 1075 if (!unknown)
764fe753
JB
1076 return;
1077
fbf80af5 1078 printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:",
3563f4c5 1079 data[0], data[1], data[2]);
fbf80af5
JB
1080 for (i = 3; i < len; i++)
1081 printf(" %.2x", data[i]);
3563f4c5
JB
1082 printf("\n");
1083}
1084
febeb0c0
JB
1085void print_ies(unsigned char *ie, int ielen, bool unknown,
1086 enum print_ie_type ptype)
3563f4c5
JB
1087{
1088 while (ielen >= 2 && ielen >= ie[1]) {
febeb0c0
JB
1089 if (ie[0] < ARRAY_SIZE(ieprinters) &&
1090 ieprinters[ie[0]].name &&
1091 ieprinters[ie[0]].flags & BIT(ptype)) {
83b4934c 1092 print_ie(&ieprinters[ie[0]], ie[0], ie[1], ie + 2);
764fe753 1093 } else if (ie[0] == 221 /* vendor */) {
febeb0c0
JB
1094 print_vendor(ie[1], ie + 2, unknown, ptype);
1095 } else if (unknown) {
3563f4c5
JB
1096 int i;
1097
8086b700 1098 printf("\tUnknown IE (%d):", ie[0]);
3563f4c5 1099 for (i=0; i<ie[1]; i++)
8086b700 1100 printf(" %.2x", ie[2+i]);
3563f4c5
JB
1101 printf("\n");
1102 }
1103 ielen -= ie[1] + 2;
1104 ie += ie[1] + 2;
1105 }
1106}
1107
1108static int print_bss_handler(struct nl_msg *msg, void *arg)
1109{
1110 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1111 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1112 struct nlattr *bss[NL80211_BSS_MAX + 1];
1113 char mac_addr[20], dev[20];
1114 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1115 [NL80211_BSS_TSF] = { .type = NLA_U64 },
1116 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
1117 [NL80211_BSS_BSSID] = { },
1118 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
1119 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
1120 [NL80211_BSS_INFORMATION_ELEMENTS] = { },
f2e17e1f
JB
1121 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
1122 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
a56117a6 1123 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
c04a78df 1124 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
575280cc 1125 [NL80211_BSS_BEACON_IES] = { },
3563f4c5 1126 };
febeb0c0 1127 struct scan_params *params = arg;
1c5bcd9c 1128 int show = params->show_both_ie_sets ? 2 : 1;
3563f4c5
JB
1129
1130 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1131 genlmsg_attrlen(gnlh, 0), NULL);
1132
1133 if (!tb[NL80211_ATTR_BSS]) {
5fe70c0e 1134 fprintf(stderr, "bss info missing!\n");
3563f4c5
JB
1135 return NL_SKIP;
1136 }
1137 if (nla_parse_nested(bss, NL80211_BSS_MAX,
1138 tb[NL80211_ATTR_BSS],
1139 bss_policy)) {
5fe70c0e 1140 fprintf(stderr, "failed to parse nested attributes!\n");
3563f4c5
JB
1141 return NL_SKIP;
1142 }
1143
1144 if (!bss[NL80211_BSS_BSSID])
1145 return NL_SKIP;
1146
1147 mac_addr_n2a(mac_addr, nla_data(bss[NL80211_BSS_BSSID]));
1148 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
a56117a6
JB
1149 printf("BSS %s (on %s)", mac_addr, dev);
1150
1151 if (bss[NL80211_BSS_STATUS]) {
1152 switch (nla_get_u32(bss[NL80211_BSS_STATUS])) {
1153 case NL80211_BSS_STATUS_AUTHENTICATED:
1154 printf(" -- authenticated");
1155 break;
1156 case NL80211_BSS_STATUS_ASSOCIATED:
1157 printf(" -- associated");
1158 break;
1159 case NL80211_BSS_STATUS_IBSS_JOINED:
1160 printf(" -- joined");
1161 break;
1162 default:
1163 printf(" -- unknown status: %d",
1164 nla_get_u32(bss[NL80211_BSS_STATUS]));
1165 break;
1166 }
1167 }
1168 printf("\n");
3563f4c5 1169
e7109a8a
JB
1170 if (bss[NL80211_BSS_TSF]) {
1171 unsigned long long tsf;
1172 tsf = (unsigned long long)nla_get_u64(bss[NL80211_BSS_TSF]);
1173 printf("\tTSF: %llu usec (%llud, %.2lld:%.2llu:%.2llu)\n",
1174 tsf, tsf/1000/1000/60/60/24, (tsf/1000/1000/60/60) % 24,
1175 (tsf/1000/1000/60) % 60, (tsf/1000/1000) % 60);
1176 }
3563f4c5
JB
1177 if (bss[NL80211_BSS_FREQUENCY])
1178 printf("\tfreq: %d\n",
1179 nla_get_u32(bss[NL80211_BSS_FREQUENCY]));
1180 if (bss[NL80211_BSS_BEACON_INTERVAL])
1181 printf("\tbeacon interval: %d\n",
1182 nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]));
92a04ecd
MH
1183 if (bss[NL80211_BSS_CAPABILITY]) {
1184 __u16 capa = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
1185 printf("\tcapability:");
1186 if (capa & WLAN_CAPABILITY_ESS)
1187 printf(" ESS");
1188 if (capa & WLAN_CAPABILITY_IBSS)
1189 printf(" IBSS");
1190 if (capa & WLAN_CAPABILITY_PRIVACY)
1191 printf(" Privacy");
1192 if (capa & WLAN_CAPABILITY_SHORT_PREAMBLE)
1193 printf(" ShortPreamble");
1194 if (capa & WLAN_CAPABILITY_PBCC)
1195 printf(" PBCC");
1196 if (capa & WLAN_CAPABILITY_CHANNEL_AGILITY)
1197 printf(" ChannelAgility");
1198 if (capa & WLAN_CAPABILITY_SPECTRUM_MGMT)
1199 printf(" SpectrumMgmt");
1200 if (capa & WLAN_CAPABILITY_QOS)
1201 printf(" QoS");
1202 if (capa & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1203 printf(" ShortSlotTime");
1204 if (capa & WLAN_CAPABILITY_APSD)
1205 printf(" APSD");
1206 if (capa & WLAN_CAPABILITY_DSSS_OFDM)
1207 printf(" DSSS-OFDM");
1208 printf(" (0x%.4x)\n", capa);
1209 }
f2e17e1f
JB
1210 if (bss[NL80211_BSS_SIGNAL_MBM]) {
1211 int s = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
1212 printf("\tsignal: %d.%.2d dBm\n", s/100, s%100);
1213 }
1214 if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
1215 unsigned char s = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
1216 printf("\tsignal: %d/100\n", s);
1217 }
c04a78df
HS
1218 if (bss[NL80211_BSS_SEEN_MS_AGO]) {
1219 int age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
1220 printf("\tlast seen: %d ms ago\n", age);
1221 }
1c5bcd9c
JB
1222
1223 if (bss[NL80211_BSS_INFORMATION_ELEMENTS] && show--) {
575280cc
JM
1224 if (bss[NL80211_BSS_BEACON_IES])
1225 printf("\tInformation elements from Probe Response "
1226 "frame:\n");
3563f4c5 1227 print_ies(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
764fe753 1228 nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
febeb0c0 1229 params->unknown, params->type);
575280cc 1230 }
1c5bcd9c 1231 if (bss[NL80211_BSS_BEACON_IES] && show--) {
575280cc
JM
1232 printf("\tInformation elements from Beacon frame:\n");
1233 print_ies(nla_data(bss[NL80211_BSS_BEACON_IES]),
1234 nla_len(bss[NL80211_BSS_BEACON_IES]),
1235 params->unknown, params->type);
1236 }
3563f4c5
JB
1237
1238 return NL_SKIP;
1239}
1240
764fe753 1241static struct scan_params scan_params;
3563f4c5 1242
7c37a24d
JB
1243static int handle_scan_dump(struct nl80211_state *state,
1244 struct nl_cb *cb,
3563f4c5
JB
1245 struct nl_msg *msg,
1246 int argc, char **argv)
1247{
764fe753
JB
1248 if (argc > 1)
1249 return 1;
1250
1c5bcd9c
JB
1251 memset(&scan_params, 0, sizeof(scan_params));
1252
764fe753
JB
1253 if (argc == 1 && !strcmp(argv[0], "-u"))
1254 scan_params.unknown = true;
575280cc
JM
1255 else if (argc == 1 && !strcmp(argv[0], "-b"))
1256 scan_params.show_both_ie_sets = true;
764fe753 1257
febeb0c0
JB
1258 scan_params.type = PRINT_SCAN;
1259
764fe753
JB
1260 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_bss_handler,
1261 &scan_params);
3563f4c5
JB
1262 return 0;
1263}
a5fe4ef2
JB
1264
1265static int handle_scan_combined(struct nl80211_state *state,
1266 struct nl_cb *cb,
1267 struct nl_msg *msg,
1268 int argc, char **argv)
1269{
559a1713 1270 char **trig_argv;
a5fe4ef2
JB
1271 static char *dump_argv[] = {
1272 NULL,
1273 "scan",
1274 "dump",
92649eab 1275 NULL,
a5fe4ef2
JB
1276 };
1277 static const __u32 cmds[] = {
1278 NL80211_CMD_NEW_SCAN_RESULTS,
1279 NL80211_CMD_SCAN_ABORTED,
1280 };
559a1713 1281 int trig_argc, dump_argc, err;
a5fe4ef2 1282
559a1713
JB
1283 if (argc >= 3 && !strcmp(argv[2], "-u")) {
1284 dump_argc = 4;
1285 dump_argv[3] = "-u";
575280cc
JM
1286 } else if (argc >= 3 && !strcmp(argv[2], "-b")) {
1287 dump_argc = 4;
1288 dump_argv[3] = "-b";
559a1713
JB
1289 } else
1290 dump_argc = 3;
1291
1292 trig_argc = 3 + (argc - 2) + (3 - dump_argc);
1293 trig_argv = calloc(trig_argc, sizeof(*trig_argv));
1294 if (!trig_argv)
1295 return -ENOMEM;
a5fe4ef2 1296 trig_argv[0] = argv[0];
559a1713
JB
1297 trig_argv[1] = "scan";
1298 trig_argv[2] = "trigger";
1299 int i;
1300 for (i = 0; i < argc - 2 - (dump_argc - 3); i++)
1301 trig_argv[i + 3] = argv[i + 2 + (dump_argc - 3)];
1302 err = handle_cmd(state, II_NETDEV, trig_argc, trig_argv);
1303 free(trig_argv);
a5fe4ef2
JB
1304 if (err)
1305 return err;
1306
61725dbe
JB
1307 /*
1308 * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION
1309 *
1310 * This code has a bug, which requires creating a separate
1311 * nl80211 socket to fix:
1312 * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or
1313 * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel
1314 * before (!) we listen to it, because we only start listening
1315 * after we send our scan request.
1316 *
1317 * Doing it the other way around has a race condition as well,
1318 * if you first open the events socket you may get a notification
1319 * for a previous scan.
1320 *
1321 * The only proper way to fix this would be to listen to events
1322 * before sending the command, and for the kernel to send the
1323 * scan request along with the event, so that you can match up
1324 * whether the scan you requested was finished or aborted (this
1325 * may result in processing a scan that another application
1326 * requested, but that doesn't seem to be a problem).
1327 *
1328 * Alas, the kernel doesn't do that (yet).
1329 */
1330
a5fe4ef2
JB
1331 if (listen_events(state, ARRAY_SIZE(cmds), cmds) ==
1332 NL80211_CMD_SCAN_ABORTED) {
1333 printf("scan aborted!\n");
1334 return 0;
1335 }
1336
1337 dump_argv[0] = argv[0];
92649eab 1338 return handle_cmd(state, II_NETDEV, dump_argc, dump_argv);
a5fe4ef2 1339}
64797a7f 1340TOPLEVEL(scan, "[-u] [freq <freq>*] [ies <hex as 00:11:..>] [ssid <ssid>*|passive]", 0, 0,
6ca98d28
JB
1341 CIB_NETDEV, handle_scan_combined,
1342 "Scan on the given frequencies and probe for the given SSIDs\n"
1343 "(or wildcard if not given) unless passive scanning is requested.\n"
64797a7f
JB
1344 "If -u is specified print unknown data in the scan results.\n"
1345 "Specified (vendor) IEs must be well-formed.");
4698bfc2
JB
1346COMMAND(scan, dump, "[-u]",
1347 NL80211_CMD_GET_SCAN, NLM_F_DUMP, CIB_NETDEV, handle_scan_dump,
1348 "Dump the current scan results. If -u is specified, print unknown\n"
1349 "data in scan results.");
64797a7f 1350COMMAND(scan, trigger, "[freq <freq>*] [ies <hex as 00:11:..>] [ssid <ssid>*|passive]",
4698bfc2
JB
1351 NL80211_CMD_TRIGGER_SCAN, 0, CIB_NETDEV, handle_scan,
1352 "Trigger a scan on the given frequencies with probing for the given\n"
1353 "SSIDs (or wildcard if not given) unless passive scanning is requested.");