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