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