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