]> git.ipfire.org Git - thirdparty/iw.git/blame - scan.c
iw: fix 'iw list' MCS set print
[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;
764fe753
JB
36};
37
7c37a24d
JB
38static int handle_scan(struct nl80211_state *state,
39 struct nl_cb *cb,
3563f4c5
JB
40 struct nl_msg *msg,
41 int argc, char **argv)
42{
559a1713
JB
43 struct nl_msg *ssids = NULL, *freqs = NULL;
44 char *eptr;
3563f4c5 45 int err = -ENOBUFS;
559a1713
JB
46 int i;
47 enum {
48 NONE,
49 FREQ,
50 SSID,
51 DONE,
52 } parse = NONE;
53 int freq;
54 bool passive = false, have_ssids = false, have_freqs = false;
3563f4c5
JB
55
56 ssids = nlmsg_alloc();
57 if (!ssids)
58 return -ENOMEM;
559a1713
JB
59
60 freqs = nlmsg_alloc();
61 if (!freqs) {
62 nlmsg_free(ssids);
63 return -ENOMEM;
64 }
65
66 for (i = 0; i < argc; i++) {
1ddf11eb 67 if (parse == NONE && strcmp(argv[i], "freq") == 0) {
559a1713
JB
68 parse = FREQ;
69 have_freqs = true;
70 continue;
1ddf11eb 71 } else if (parse < SSID && strcmp(argv[i], "ssid") == 0) {
559a1713
JB
72 parse = SSID;
73 have_ssids = true;
74 continue;
1ddf11eb 75 } else if (parse < SSID && strcmp(argv[i], "passive") == 0) {
559a1713
JB
76 parse = DONE;
77 passive = true;
78 continue;
79 }
80
81 switch (parse) {
82 case NONE:
83 case DONE:
84 return 1;
85 case FREQ:
86 freq = strtoul(argv[i], &eptr, 10);
87 if (eptr != argv[i] + strlen(argv[i]))
88 return 1;
89 NLA_PUT_U32(freqs, i, freq);
90 break;
91 case SSID:
92 NLA_PUT(ssids, i, strlen(argv[i]), argv[i]);
93 break;
94 }
95 }
96
97 if (!have_ssids)
98 NLA_PUT(ssids, 1, 0, "");
99 if (!passive)
100 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
101
102 if (have_freqs)
103 nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
3563f4c5
JB
104
105 err = 0;
106 nla_put_failure:
107 nlmsg_free(ssids);
559a1713 108 nlmsg_free(freqs);
3563f4c5
JB
109 return err;
110}
3563f4c5 111
857d966e
MH
112static void tab_on_first(bool *first)
113{
114 if (!*first)
115 printf("\t");
116 else
117 *first = false;
118}
119
83b4934c 120static void print_ssid(const uint8_t type, uint8_t len, const uint8_t *data)
3563f4c5 121{
83b4934c 122 printf(" ");
748f8489 123 print_ssid_escaped(len, data);
3563f4c5
JB
124 printf("\n");
125}
126
83b4934c 127static void print_supprates(const uint8_t type, uint8_t len, const uint8_t *data)
3563f4c5
JB
128{
129 int i;
130
83b4934c 131 printf(" ");
3563f4c5 132
83b4934c 133 for (i = 0; i < len; i++) {
3563f4c5
JB
134 int r = data[i] & 0x7f;
135 printf("%d.%d%s ", r/2, 5*(r&1), data[i] & 0x80 ? "*":"");
136 }
137 printf("\n");
138}
139
83b4934c 140static void print_ds(const uint8_t type, uint8_t len, const uint8_t *data)
3563f4c5 141{
83b4934c 142 printf(" channel %d\n", data[0]);
3563f4c5
JB
143}
144
83b4934c 145static void print_country(const uint8_t type, uint8_t len, const uint8_t *data)
b7e8fa37
MH
146{
147 int i;
148
83b4934c 149 printf(" %.*s", 2, data);
b7e8fa37
MH
150 switch (data[2]) {
151 case 'I':
152 printf(" (indoor)");
153 break;
154 case 'O':
155 printf(" (outdoor)");
156 break;
b6c0d634
JB
157 case ' ':
158 printf(" (in/outdoor)");
159 break;
160 default:
161 printf(" (invalid environment)");
162 break;
b7e8fa37
MH
163 }
164 printf(", data:");
165 for(i=0; i<len-3; i++)
166 printf(" %.02x", data[i + 3]);
167 printf("\n");
168}
169
d1563a1b
MH
170static void print_powerconstraint(const uint8_t type, uint8_t len, const uint8_t *data)
171{
172 printf(" %d dB\n", data[0]);
173}
174
83b4934c 175static void print_erp(const uint8_t type, uint8_t len, const uint8_t *data)
fc4d1484
MH
176{
177 if (data[0] == 0x00)
83b4934c 178 printf(" <no flags>");
fc4d1484
MH
179 if (data[0] & 0x01)
180 printf(" NonERP_Present");
181 if (data[0] & 0x02)
182 printf(" Use_Protection");
183 if (data[0] & 0x04)
184 printf(" Barker_Preamble_Mode");
185 printf("\n");
186}
187
83b4934c 188static void print_cipher(const uint8_t *data)
857d966e
MH
189{
190 if (memcmp(data, wifi_oui, 3) == 0) {
191 switch (data[3]) {
510e0e2f 192 case 0:
857d966e
MH
193 printf("Use group cipher suite");
194 break;
510e0e2f 195 case 1:
857d966e
MH
196 printf("WEP-40");
197 break;
510e0e2f 198 case 2:
857d966e
MH
199 printf("TKIP");
200 break;
510e0e2f 201 case 4:
857d966e
MH
202 printf("CCMP");
203 break;
510e0e2f 204 case 5:
857d966e
MH
205 printf("WEP-104");
206 break;
207 default:
332769c6 208 printf("%.02x-%.02x-%.02x:%d",
5594fd23 209 data[0], data[1] ,data[2], data[3]);
857d966e
MH
210 break;
211 }
212 } else if (memcmp(data, ieee80211_oui, 3) == 0) {
213 switch (data[3]) {
510e0e2f 214 case 0:
857d966e
MH
215 printf("Use group cipher suite");
216 break;
510e0e2f 217 case 1:
857d966e
MH
218 printf("WEP-40");
219 break;
510e0e2f 220 case 2:
857d966e
MH
221 printf("TKIP");
222 break;
510e0e2f 223 case 4:
857d966e
MH
224 printf("CCMP");
225 break;
510e0e2f 226 case 5:
857d966e
MH
227 printf("WEP-104");
228 break;
510e0e2f 229 case 6:
857d966e
MH
230 printf("AES-128-CMAC");
231 break;
232 default:
332769c6 233 printf("%.02x-%.02x-%.02x:%d",
5594fd23 234 data[0], data[1] ,data[2], data[3]);
857d966e
MH
235 break;
236 }
237 } else
332769c6 238 printf("%.02x-%.02x-%.02x:%d",
5594fd23 239 data[0], data[1] ,data[2], data[3]);
857d966e
MH
240}
241
83b4934c 242static void print_auth(const uint8_t *data)
857d966e
MH
243{
244 if (memcmp(data, wifi_oui, 3) == 0) {
245 switch (data[3]) {
510e0e2f 246 case 1:
857d966e
MH
247 printf("IEEE 802.1X");
248 break;
510e0e2f 249 case 2:
857d966e
MH
250 printf("PSK");
251 break;
252 default:
332769c6 253 printf("%.02x-%.02x-%.02x:%d",
5594fd23 254 data[0], data[1] ,data[2], data[3]);
857d966e
MH
255 break;
256 }
257 } else if (memcmp(data, ieee80211_oui, 3) == 0) {
258 switch (data[3]) {
510e0e2f 259 case 1:
857d966e
MH
260 printf("IEEE 802.1X");
261 break;
510e0e2f 262 case 2:
857d966e
MH
263 printf("PSK");
264 break;
0fe1c415
JB
265 case 3:
266 printf("FT/IEEE 802.1X");
267 break;
268 case 4:
269 printf("FT/PSK");
270 break;
271 case 5:
272 printf("IEEE 802.1X/SHA-256");
273 break;
274 case 6:
275 printf("PSK/SHA-256");
276 break;
857d966e 277 default:
332769c6 278 printf("%.02x-%.02x-%.02x:%d",
5594fd23 279 data[0], data[1] ,data[2], data[3]);
857d966e
MH
280 break;
281 }
282 } else
332769c6 283 printf("%.02x-%.02x-%.02x:%d",
5594fd23 284 data[0], data[1] ,data[2], data[3]);
857d966e
MH
285}
286
83b4934c
JB
287static void print_rsn_ie(const char *defcipher, const char *defauth,
288 uint8_t len, const uint8_t *data)
857d966e
MH
289{
290 bool first = true;
291 __u16 version, count, capa;
292 int i;
293
857d966e
MH
294 version = data[0] + (data[1] << 8);
295 tab_on_first(&first);
296 printf("\t * Version: %d\n", version);
297
298 data += 2;
299 len -= 2;
300
301 if (len < 4) {
302 tab_on_first(&first);
303 printf("\t * Group cipher: %s\n", defcipher);
304 printf("\t * Pairwise ciphers: %s\n", defcipher);
305 return;
306 }
307
308 tab_on_first(&first);
309 printf("\t * Group cipher: ");
310 print_cipher(data);
311 printf("\n");
312
313 data += 4;
314 len -= 4;
315
316 if (len < 2) {
317 tab_on_first(&first);
318 printf("\t * Pairwise ciphers: %s\n", defcipher);
319 return;
320 }
321
322 count = data[0] | (data[1] << 8);
31d477fb
MH
323 if (2 + (count * 4) > len)
324 goto invalid;
325
857d966e
MH
326 tab_on_first(&first);
327 printf("\t * Pairwise ciphers:");
31d477fb 328 for (i = 0; i < count; i++) {
857d966e
MH
329 printf(" ");
330 print_cipher(data + 2 + (i * 4));
331 }
332 printf("\n");
333
334 data += 2 + (count * 4);
335 len -= 2 + (count * 4);
336
337 if (len < 2) {
338 tab_on_first(&first);
339 printf("\t * Authentication suites: %s\n", defauth);
340 return;
341 }
342
343 count = data[0] | (data[1] << 8);
31d477fb
MH
344 if (2 + (count * 4) > len)
345 goto invalid;
346
857d966e
MH
347 tab_on_first(&first);
348 printf("\t * Authentication suites:");
83b4934c 349 for (i = 0; i < count; i++) {
857d966e
MH
350 printf(" ");
351 print_auth(data + 2 + (i * 4));
352 }
353 printf("\n");
354
355 data += 2 + (count * 4);
356 len -= 2 + (count * 4);
357
6a4f24e8
JB
358 if (len >= 2) {
359 capa = data[0] | (data[1] << 8);
360 tab_on_first(&first);
cadbe89a
JB
361 printf("\t * Capabilities:");
362 if (capa & 0x0001)
363 printf(" PreAuth");
364 if (capa & 0x0002)
365 printf(" NoPairwise");
366 switch ((capa & 0x000c) >> 2) {
367 case 0:
368 break;
369 case 1:
370 printf(" 2-PTKSA-RC");
371 break;
372 case 2:
373 printf(" 4-PTKSA-RC");
374 break;
375 case 3:
376 printf(" 16-PTKSA-RC");
377 break;
378 }
379 switch ((capa & 0x0030) >> 4) {
380 case 0:
381 break;
382 case 1:
383 printf(" 2-GTKSA-RC");
384 break;
385 case 2:
386 printf(" 4-GTKSA-RC");
387 break;
388 case 3:
389 printf(" 16-GTKSA-RC");
390 break;
391 }
392 if (capa & 0x0040)
393 printf(" MFP-required");
394 if (capa & 0x0080)
395 printf(" MFP-capable");
396 if (capa & 0x0200)
397 printf(" Peerkey-enabled");
398 if (capa & 0x0400)
399 printf(" SPP-AMSDU-capable");
400 if (capa & 0x0800)
401 printf(" SPP-AMSDU-required");
402 printf(" (0x%.4x)\n", capa);
6a4f24e8
JB
403 data += 2;
404 len -= 2;
405 }
31d477fb 406
cadbe89a 407 invalid:
31d477fb
MH
408 if (len != 0) {
409 printf("\t\t * bogus tail data (%d):", len);
410 while (len) {
411 printf(" %.2x", *data);
412 data++;
413 len--;
414 }
415 printf("\n");
416 }
857d966e
MH
417}
418
83b4934c 419static void print_rsn(const uint8_t type, uint8_t len, const uint8_t *data)
857d966e 420{
83b4934c 421 print_rsn_ie("CCMP", "IEEE 802.1X", len, data);
857d966e
MH
422}
423
0c445c24
LR
424/*
425 * There are only 4 possible values, we just use a case instead of computing it,
426 * but technically this can also be computed through the formula:
427 *
428 * Max AMPDU length = (2 ^ (13 + exponent)) - 1 bytes
429 */
430__u32 compute_ampdu_length(__u8 exponent)
431{
432 switch (exponent) {
433 case 0: return 8191; /* (2 ^(13 + 0)) -1 */
434 case 1: return 16383; /* (2 ^(13 + 1)) -1 */
435 case 2: return 32767; /* (2 ^(13 + 2)) -1 */
436 case 3: return 65535; /* (2 ^(13 + 3)) -1 */
437 default: return 0;
438 }
439}
440
441const char *print_ampdu_space(__u8 space)
442{
443 switch (space) {
444 case 0: return "No restriction";
445 case 1: return "1/4 usec";
446 case 2: return "1/2 usec";
447 case 3: return "1 usec";
448 case 4: return "2 usec";
449 case 5: return "4 usec";
450 case 6: return "8 usec";
451 case 7: return "16 usec";
452 default:
453 return "Uknown";
454 }
455}
456
457static void print_ht_capa(const uint8_t type, uint8_t len, const uint8_t *data)
458{
459#define PRINT_HT_CAP(_cond, _str) \
460 do { \
461 if (_cond) \
462 printf("\t\t\t" _str "\n"); \
463 } while (0)
464 struct ht_cap_data {
465 __u16 cap;
466 __u8 ampdu_params;
467 struct {
468 __u8 rx_mcs_bitmask[10]; /* last 3 bits reserved */
469 __u16 max_rx_rate_1mbps: 10,
470 reserved_0: 6;
471 __u8 tx_rx_mcs_defined:1,
472 tx_rx_mcs_not_equal:1,
473 tx_max_streams:2,
474 tx_unequal_modulation:1,
475 reserved_1:3; /* 3 reserved bits here */
476 __u8 reserved_2[3]; /* 24 reserved bits here = 27 */
477 } mcs_set;
478 __u16 ht_extend_cap;
479 __u32 tx_beamform_cap;
480 __u8 asel_cap;
481 } __attribute__((packed)) ht_cap;
482 struct ht_cap_data *htc = &ht_cap;
483 __u8 ampdu_exponent, ampdu_spacing, bit;
484 __u32 max_ampdu_length, i;
485 bool tx_rx_mcs_equal = false;
486
487 if (len != 26) {
488 printf("\n\t\tHT Capability IE len != expected 26 bytes, skipping parse\n");
489 return;
490 }
491
492 memcpy(&ht_cap, data, 26);
493
494 printf("\n\t\tCapabilities: %#.4x\n", htc->cap);
495
496 PRINT_HT_CAP((htc->cap & BIT(0)), "RX LDCP");
497 PRINT_HT_CAP((htc->cap & BIT(1)), "HT20/HT40");
498 PRINT_HT_CAP(!(htc->cap & BIT(1)), "HT20");
499
500 PRINT_HT_CAP(((htc->cap >> 2) & 0x3) == 0, "Static SM Power Save");
501 PRINT_HT_CAP(((htc->cap >> 2) & 0x3) == 1, "Dynamic SM Power Save");
502 PRINT_HT_CAP(((htc->cap >> 2) & 0x3) == 3, "SM Power Save disabled");
503
504 PRINT_HT_CAP((htc->cap & BIT(4)), "RX Greenfield");
505 PRINT_HT_CAP((htc->cap & BIT(5)), "RX HT20 SGI");
506 PRINT_HT_CAP((htc->cap & BIT(6)), "RX HT40 SGI");
507 PRINT_HT_CAP((htc->cap & BIT(7)), "TX STBC");
508
509 PRINT_HT_CAP(((htc->cap >> 8) & 0x3) == 0, "No RX STBC");
510 PRINT_HT_CAP(((htc->cap >> 8) & 0x3) == 1, "RX STBC 1-stream");
511 PRINT_HT_CAP(((htc->cap >> 8) & 0x3) == 2, "RX STBC 2-streams");
512 PRINT_HT_CAP(((htc->cap >> 8) & 0x3) == 3, "RX STBC 3-streams");
513
514 PRINT_HT_CAP((htc->cap & BIT(10)), "HT Delayed Block Ack");
515
516 PRINT_HT_CAP((htc->cap & BIT(11)), "Max AMSDU length: 3839 bytes");
517 PRINT_HT_CAP(!(htc->cap & BIT(11)), "Max AMSDU length: 7935 bytes");
518
519 /*
520 * For beacons and probe response this would mean the BSS
521 * does or does not allow the usage of DSSS/CCK HT40.
522 * Otherwise it means the STA does or does not use
523 * DSSS/CCK HT40.
524 */
525 PRINT_HT_CAP((htc->cap & BIT(12)), "DSSS/CCK HT40");
526 PRINT_HT_CAP(!(htc->cap & BIT(12)), "No DSSS/CCK HT40");
527
528 /* BIT(13) is reserved */
529
530 PRINT_HT_CAP((htc->cap & BIT(14)), "40 MHz Intolerant");
531
532 PRINT_HT_CAP((htc->cap & BIT(15)), "L-SIG TXOP protection");
533
534 ampdu_exponent = htc->ampdu_params & 0x3;
535 max_ampdu_length = compute_ampdu_length(ampdu_exponent);
536 if (max_ampdu_length) {
537 printf("\t\tMaximum RX AMPDU length %d bytes (exponent: 0x0%02x)\n",
538 compute_ampdu_length(ampdu_exponent), ampdu_exponent);
539 }
540 else
541 printf("\t\tMaximum RX AMPDU length: unrecognized bytes "
542 "(exponent: %d)\n", ampdu_exponent);
543
544
545 ampdu_spacing = (htc->ampdu_params >> 2) & 0x3 ;
546 printf("\t\tMinimum RX AMPDU time spacing: %s (0x%02x)\n",
547 print_ampdu_space(ampdu_spacing), ampdu_spacing);
548
549 /* This is the whole MCS set, which is 16 bytes */
deb3501c 550 printf("\t\tMCS set:");
0c445c24 551 data+=2;
deb3501c 552 print_mcs_set(data);
0c445c24
LR
553 printf("\n");
554
555 if (htc->mcs_set.tx_rx_mcs_defined && htc->mcs_set.tx_rx_mcs_not_equal)
556 tx_rx_mcs_equal = true;
557 if (tx_rx_mcs_equal)
558 printf("\t\tSupported TX/RX MCS Indexes:\n");
559 else
560 printf("\t\tSupported RX MCS Indexes:\n");
561 /*
562 * Parses the RX MCS rates. Only 10 bits correspond to actual MCS rates
563 * MCS [0-76]
564 */
565 for (i = 0; i < 10; i++) {
566 for (bit = 0; bit < 8; bit++) {
567 /* Only bits 0-76 are valid, bits 76-79 are reserved */
568 if (((i * 8) + bit) > 76)
569 break;
570 if (htc->mcs_set.rx_mcs_bitmask[i] & BIT(bit))
571 printf("\t\t\tMCS Index %d\n",
572 (i * 8) + bit);
573 }
574 }
575
576 if (!htc->mcs_set.tx_rx_mcs_defined) {
577 /* This is actually quite common */
578 printf("\t\tNo TX MCS set defined\n");
579 goto out;
580 }
581
582 if (htc->mcs_set.tx_rx_mcs_not_equal) {
583 printf("\t\tMaximum supported TX spatial streams: %d\n",
584 htc->mcs_set.tx_max_streams);
585 printf("\t\tTX unequal modulation ");
586 if (htc->mcs_set.tx_unequal_modulation)
587 printf("supported\n");
588 else
589 printf("unsupported\n");
590 }
591
592out:
593 return;
594}
595
83b4934c 596static void print_capabilities(const uint8_t type, uint8_t len, const uint8_t *data)
9b880b00 597{
31d2d259
JB
598 int i, base, bit;
599 bool first = true;
600
601
602 for (i = 0; i < len; i++) {
603 base = i * 8;
604
605 for (bit = 0; bit < 8; bit++) {
606 if (!(data[i] & (1 << bit)))
607 continue;
608
609 if (!first)
610 printf(",");
611 else
612 first = false;
613
614 switch (bit + base) {
615 case 0:
616 printf(" HT Information Exchange Supported");
617 break;
618 case 1:
619 printf(" On-demand Beacon");
620 break;
621 case 2:
622 printf(" Extended Channel Switching");
623 break;
624 case 3:
625 printf(" Wave Indication");
626 break;
627 case 4:
628 printf(" PSMP Capability");
629 break;
630 case 5:
631 printf(" Service Interval Granularity");
632 break;
633 case 6:
634 printf(" S-PSMP Capability");
635 break;
636 default:
637 printf(" %d", bit);
638 break;
639 }
640 }
641 }
9b880b00 642
9b880b00
MH
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), },
83b4934c 689 [5] = PRINT_IGN,
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), },
0c445c24 693 [45] = { "HT capabilities", print_ht_capa, 1, 255, BIT(PRINT_SCAN), },
febeb0c0
JB
694 [48] = { "RSN", print_rsn, 2, 255, BIT(PRINT_SCAN), },
695 [50] = { "Extended supported rates", print_supprates, 0, 255, BIT(PRINT_SCAN), },
696 [127] = { "Extended capabilities", print_capabilities, 0, 255, BIT(PRINT_SCAN), },
83b4934c
JB
697};
698
699static void print_wifi_wpa(const uint8_t type, uint8_t len, const uint8_t *data)
700{
701 print_rsn_ie("TKIP", "IEEE 802.1X", len, data);
702}
703
1cab57eb
JB
704static bool print_wifi_wmm_param(const uint8_t *data, uint8_t len)
705{
706 int i;
707 static const char *aci_tbl[] = { "BE", "BK", "VI", "VO" };
708
709 if (len < 19)
710 goto invalid;
711
712 if (data[0] != 1) {
cee4fe20 713 printf("Parameter: not version 1: ");
1cab57eb
JB
714 return false;
715 }
716
cee4fe20 717 printf("\t* Parameter version 1");
1cab57eb
JB
718
719 data++;
720
721 if (data[0] & 0x80)
722 printf("\n\t\t* u-APSD");
723
724 data += 2;
725
726 for (i = 0; i < 4; i++) {
eeac6099 727 printf("\n\t\t* %s:", aci_tbl[(data[0] >> 5) & 3]);
1cab57eb
JB
728 if (data[4] & 0x10)
729 printf(" acm");
730 printf(" CW %d-%d", (1 << (data[1] & 0xf)) - 1,
731 (1 << (data[1] >> 4)) - 1);
a2a4c265 732 printf(", AIFSN %d", data[0] & 0xf);
1cab57eb 733 if (data[2] | data[3])
cee4fe20 734 printf(", TXOP %d usec", (data[2] + (data[3] << 8)) * 32);
1cab57eb
JB
735 data += 4;
736 }
737
738 printf("\n");
739 return true;
740
741 invalid:
742 printf("invalid: ");
743 return false;
744}
745
83b4934c 746static void print_wifi_wmm(const uint8_t type, uint8_t len, const uint8_t *data)
6ff0c93a
MH
747{
748 int i;
749
6ff0c93a
MH
750 switch (data[0]) {
751 case 0x00:
83b4934c 752 printf(" information:");
6ff0c93a
MH
753 break;
754 case 0x01:
1cab57eb
JB
755 if (print_wifi_wmm_param(data + 1, len - 1))
756 return;
6ff0c93a
MH
757 break;
758 default:
83b4934c 759 printf(" type %d:", data[0]);
6ff0c93a
MH
760 break;
761 }
762
1cab57eb
JB
763 for(i = 1; i < len; i++)
764 printf(" %.02x", data[i]);
6ff0c93a
MH
765 printf("\n");
766}
767
83b4934c 768static void print_wifi_wps(const uint8_t type, uint8_t len, const uint8_t *data)
4673a894
JB
769{
770 bool first = true;
771 __u16 subtype, sublen;
772
4673a894
JB
773 while (len >= 4) {
774 subtype = (data[0] << 8) + data[1];
775 sublen = (data[2] << 8) + data[3];
776 if (sublen > len)
777 break;
778
779 switch (subtype) {
780 case 0x104a:
781 tab_on_first(&first);
dffc6750 782 printf("\t * Version: %d.%d\n", data[4] >> 4, data[4] & 0xF);
4673a894
JB
783 break;
784 case 0x1011:
785 tab_on_first(&first);
786 printf("\t * Device name: %.*s\n", sublen, data + 4);
787 break;
788 case 0x1021:
789 tab_on_first(&first);
790 printf("\t * Manufacturer: %.*s\n", sublen, data + 4);
791 break;
792 case 0x1023:
793 tab_on_first(&first);
794 printf("\t * Model: %.*s\n", sublen, data + 4);
795 break;
7ee5a865 796 case 0x1057: {
fe31a22e 797 __u8 val = data[4];
7ee5a865 798 tab_on_first(&first);
fe31a22e 799 printf("\t * AP setup locked: 0x%.2x\n", val);
7ee5a865
JB
800 break;
801 }
4673a894
JB
802 case 0x1008: {
803 __u16 meth = (data[4] << 8) + data[5];
804 bool comma = false;
805 tab_on_first(&first);
806 printf("\t * Config methods:");
807#define T(bit, name) do { \
808 if (meth & (1<<bit)) { \
809 if (comma) \
810 printf(","); \
811 comma = true; \
812 printf(" " name); \
813 } } while (0)
814 T(0, "USB");
815 T(1, "Ethernet");
816 T(2, "Label");
817 T(3, "Display");
818 T(4, "Ext. NFC");
819 T(5, "Int. NFC");
820 T(6, "NFC Intf.");
821 T(7, "PBC");
822 T(8, "Keypad");
823 printf("\n");
824 break;
825#undef T
826 }
827 default:
828 break;
829 }
830
831 data += sublen + 4;
832 len -= sublen + 4;
833 }
834
835 if (len != 0) {
836 printf("\t\t * bogus tail data (%d):", len);
837 while (len) {
838 printf(" %.2x", *data);
839 data++;
840 len--;
841 }
842 printf("\n");
843 }
844}
845
83b4934c 846static const struct ie_print wifiprinters[] = {
febeb0c0
JB
847 [1] = { "WPA", print_wifi_wpa, 2, 255, BIT(PRINT_SCAN), },
848 [2] = { "WMM", print_wifi_wmm, 1, 255, BIT(PRINT_SCAN), },
849 [4] = { "WPS", print_wifi_wps, 0, 255, BIT(PRINT_SCAN), },
4673a894
JB
850};
851
764fe753 852static void print_vendor(unsigned char len, unsigned char *data,
febeb0c0 853 bool unknown, enum print_ie_type ptype)
3563f4c5
JB
854{
855 int i;
856
fbf80af5 857 if (len < 3) {
4673a894 858 printf("\tVendor specific: <too short> data:");
fbf80af5
JB
859 for(i = 0; i < len; i++)
860 printf(" %.02x", data[i]);
861 printf("\n");
862 return;
863 }
864
857d966e 865 if (len >= 4 && memcmp(data, wifi_oui, 3) == 0) {
febeb0c0
JB
866 if (data[3] < ARRAY_SIZE(wifiprinters) &&
867 wifiprinters[data[3]].name &&
868 wifiprinters[data[3]].flags & BIT(ptype)) {
83b4934c
JB
869 print_ie(&wifiprinters[data[3]], data[3], len - 4, data + 4);
870 return;
871 }
febeb0c0 872 if (!unknown)
4673a894 873 return;
857d966e 874 printf("\tWiFi OUI %#.2x, data:", data[3]);
4673a894
JB
875 for(i = 0; i < len - 4; i++)
876 printf(" %.02x", data[i + 4]);
877 printf("\n");
878 return;
879 }
880
febeb0c0 881 if (!unknown)
764fe753
JB
882 return;
883
fbf80af5 884 printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:",
3563f4c5 885 data[0], data[1], data[2]);
fbf80af5
JB
886 for (i = 3; i < len; i++)
887 printf(" %.2x", data[i]);
3563f4c5
JB
888 printf("\n");
889}
890
febeb0c0
JB
891void print_ies(unsigned char *ie, int ielen, bool unknown,
892 enum print_ie_type ptype)
3563f4c5
JB
893{
894 while (ielen >= 2 && ielen >= ie[1]) {
febeb0c0
JB
895 if (ie[0] < ARRAY_SIZE(ieprinters) &&
896 ieprinters[ie[0]].name &&
897 ieprinters[ie[0]].flags & BIT(ptype)) {
83b4934c 898 print_ie(&ieprinters[ie[0]], ie[0], ie[1], ie + 2);
764fe753 899 } else if (ie[0] == 221 /* vendor */) {
febeb0c0
JB
900 print_vendor(ie[1], ie + 2, unknown, ptype);
901 } else if (unknown) {
3563f4c5
JB
902 int i;
903
8086b700 904 printf("\tUnknown IE (%d):", ie[0]);
3563f4c5 905 for (i=0; i<ie[1]; i++)
8086b700 906 printf(" %.2x", ie[2+i]);
3563f4c5
JB
907 printf("\n");
908 }
909 ielen -= ie[1] + 2;
910 ie += ie[1] + 2;
911 }
912}
913
914static int print_bss_handler(struct nl_msg *msg, void *arg)
915{
916 struct nlattr *tb[NL80211_ATTR_MAX + 1];
917 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
918 struct nlattr *bss[NL80211_BSS_MAX + 1];
919 char mac_addr[20], dev[20];
920 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
921 [NL80211_BSS_TSF] = { .type = NLA_U64 },
922 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
923 [NL80211_BSS_BSSID] = { },
924 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
925 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
926 [NL80211_BSS_INFORMATION_ELEMENTS] = { },
f2e17e1f
JB
927 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
928 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
a56117a6 929 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
c04a78df 930 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
3563f4c5 931 };
febeb0c0 932 struct scan_params *params = arg;
3563f4c5
JB
933
934 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
935 genlmsg_attrlen(gnlh, 0), NULL);
936
937 if (!tb[NL80211_ATTR_BSS]) {
5fe70c0e 938 fprintf(stderr, "bss info missing!\n");
3563f4c5
JB
939 return NL_SKIP;
940 }
941 if (nla_parse_nested(bss, NL80211_BSS_MAX,
942 tb[NL80211_ATTR_BSS],
943 bss_policy)) {
5fe70c0e 944 fprintf(stderr, "failed to parse nested attributes!\n");
3563f4c5
JB
945 return NL_SKIP;
946 }
947
948 if (!bss[NL80211_BSS_BSSID])
949 return NL_SKIP;
950
951 mac_addr_n2a(mac_addr, nla_data(bss[NL80211_BSS_BSSID]));
952 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
a56117a6
JB
953 printf("BSS %s (on %s)", mac_addr, dev);
954
955 if (bss[NL80211_BSS_STATUS]) {
956 switch (nla_get_u32(bss[NL80211_BSS_STATUS])) {
957 case NL80211_BSS_STATUS_AUTHENTICATED:
958 printf(" -- authenticated");
959 break;
960 case NL80211_BSS_STATUS_ASSOCIATED:
961 printf(" -- associated");
962 break;
963 case NL80211_BSS_STATUS_IBSS_JOINED:
964 printf(" -- joined");
965 break;
966 default:
967 printf(" -- unknown status: %d",
968 nla_get_u32(bss[NL80211_BSS_STATUS]));
969 break;
970 }
971 }
972 printf("\n");
3563f4c5 973
e7109a8a
JB
974 if (bss[NL80211_BSS_TSF]) {
975 unsigned long long tsf;
976 tsf = (unsigned long long)nla_get_u64(bss[NL80211_BSS_TSF]);
977 printf("\tTSF: %llu usec (%llud, %.2lld:%.2llu:%.2llu)\n",
978 tsf, tsf/1000/1000/60/60/24, (tsf/1000/1000/60/60) % 24,
979 (tsf/1000/1000/60) % 60, (tsf/1000/1000) % 60);
980 }
3563f4c5
JB
981 if (bss[NL80211_BSS_FREQUENCY])
982 printf("\tfreq: %d\n",
983 nla_get_u32(bss[NL80211_BSS_FREQUENCY]));
984 if (bss[NL80211_BSS_BEACON_INTERVAL])
985 printf("\tbeacon interval: %d\n",
986 nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]));
92a04ecd
MH
987 if (bss[NL80211_BSS_CAPABILITY]) {
988 __u16 capa = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
989 printf("\tcapability:");
990 if (capa & WLAN_CAPABILITY_ESS)
991 printf(" ESS");
992 if (capa & WLAN_CAPABILITY_IBSS)
993 printf(" IBSS");
994 if (capa & WLAN_CAPABILITY_PRIVACY)
995 printf(" Privacy");
996 if (capa & WLAN_CAPABILITY_SHORT_PREAMBLE)
997 printf(" ShortPreamble");
998 if (capa & WLAN_CAPABILITY_PBCC)
999 printf(" PBCC");
1000 if (capa & WLAN_CAPABILITY_CHANNEL_AGILITY)
1001 printf(" ChannelAgility");
1002 if (capa & WLAN_CAPABILITY_SPECTRUM_MGMT)
1003 printf(" SpectrumMgmt");
1004 if (capa & WLAN_CAPABILITY_QOS)
1005 printf(" QoS");
1006 if (capa & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1007 printf(" ShortSlotTime");
1008 if (capa & WLAN_CAPABILITY_APSD)
1009 printf(" APSD");
1010 if (capa & WLAN_CAPABILITY_DSSS_OFDM)
1011 printf(" DSSS-OFDM");
1012 printf(" (0x%.4x)\n", capa);
1013 }
f2e17e1f
JB
1014 if (bss[NL80211_BSS_SIGNAL_MBM]) {
1015 int s = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
1016 printf("\tsignal: %d.%.2d dBm\n", s/100, s%100);
1017 }
1018 if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
1019 unsigned char s = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
1020 printf("\tsignal: %d/100\n", s);
1021 }
c04a78df
HS
1022 if (bss[NL80211_BSS_SEEN_MS_AGO]) {
1023 int age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
1024 printf("\tlast seen: %d ms ago\n", age);
1025 }
3563f4c5
JB
1026 if (bss[NL80211_BSS_INFORMATION_ELEMENTS])
1027 print_ies(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
764fe753 1028 nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
febeb0c0 1029 params->unknown, params->type);
3563f4c5
JB
1030
1031 return NL_SKIP;
1032}
1033
764fe753 1034static struct scan_params scan_params;
3563f4c5 1035
7c37a24d
JB
1036static int handle_scan_dump(struct nl80211_state *state,
1037 struct nl_cb *cb,
3563f4c5
JB
1038 struct nl_msg *msg,
1039 int argc, char **argv)
1040{
764fe753
JB
1041 if (argc > 1)
1042 return 1;
1043
1044 scan_params.unknown = false;
1045 if (argc == 1 && !strcmp(argv[0], "-u"))
1046 scan_params.unknown = true;
1047
febeb0c0
JB
1048 scan_params.type = PRINT_SCAN;
1049
764fe753
JB
1050 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_bss_handler,
1051 &scan_params);
3563f4c5
JB
1052 return 0;
1053}
a5fe4ef2
JB
1054
1055static int handle_scan_combined(struct nl80211_state *state,
1056 struct nl_cb *cb,
1057 struct nl_msg *msg,
1058 int argc, char **argv)
1059{
559a1713 1060 char **trig_argv;
a5fe4ef2
JB
1061 static char *dump_argv[] = {
1062 NULL,
1063 "scan",
1064 "dump",
92649eab 1065 NULL,
a5fe4ef2
JB
1066 };
1067 static const __u32 cmds[] = {
1068 NL80211_CMD_NEW_SCAN_RESULTS,
1069 NL80211_CMD_SCAN_ABORTED,
1070 };
559a1713 1071 int trig_argc, dump_argc, err;
a5fe4ef2 1072
559a1713
JB
1073 if (argc >= 3 && !strcmp(argv[2], "-u")) {
1074 dump_argc = 4;
1075 dump_argv[3] = "-u";
1076 } else
1077 dump_argc = 3;
1078
1079 trig_argc = 3 + (argc - 2) + (3 - dump_argc);
1080 trig_argv = calloc(trig_argc, sizeof(*trig_argv));
1081 if (!trig_argv)
1082 return -ENOMEM;
a5fe4ef2 1083 trig_argv[0] = argv[0];
559a1713
JB
1084 trig_argv[1] = "scan";
1085 trig_argv[2] = "trigger";
1086 int i;
1087 for (i = 0; i < argc - 2 - (dump_argc - 3); i++)
1088 trig_argv[i + 3] = argv[i + 2 + (dump_argc - 3)];
1089 err = handle_cmd(state, II_NETDEV, trig_argc, trig_argv);
1090 free(trig_argv);
a5fe4ef2
JB
1091 if (err)
1092 return err;
1093
61725dbe
JB
1094 /*
1095 * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION
1096 *
1097 * This code has a bug, which requires creating a separate
1098 * nl80211 socket to fix:
1099 * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or
1100 * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel
1101 * before (!) we listen to it, because we only start listening
1102 * after we send our scan request.
1103 *
1104 * Doing it the other way around has a race condition as well,
1105 * if you first open the events socket you may get a notification
1106 * for a previous scan.
1107 *
1108 * The only proper way to fix this would be to listen to events
1109 * before sending the command, and for the kernel to send the
1110 * scan request along with the event, so that you can match up
1111 * whether the scan you requested was finished or aborted (this
1112 * may result in processing a scan that another application
1113 * requested, but that doesn't seem to be a problem).
1114 *
1115 * Alas, the kernel doesn't do that (yet).
1116 */
1117
a5fe4ef2
JB
1118 if (listen_events(state, ARRAY_SIZE(cmds), cmds) ==
1119 NL80211_CMD_SCAN_ABORTED) {
1120 printf("scan aborted!\n");
1121 return 0;
1122 }
1123
1124 dump_argv[0] = argv[0];
92649eab 1125 return handle_cmd(state, II_NETDEV, dump_argc, dump_argv);
a5fe4ef2 1126}
6ca98d28
JB
1127TOPLEVEL(scan, "[-u] [freq <freq>*] [ssid <ssid>*|passive]", 0, 0,
1128 CIB_NETDEV, handle_scan_combined,
1129 "Scan on the given frequencies and probe for the given SSIDs\n"
1130 "(or wildcard if not given) unless passive scanning is requested.\n"
1131 "If -u is specified print unknown data in the scan results.");
4698bfc2
JB
1132COMMAND(scan, dump, "[-u]",
1133 NL80211_CMD_GET_SCAN, NLM_F_DUMP, CIB_NETDEV, handle_scan_dump,
1134 "Dump the current scan results. If -u is specified, print unknown\n"
1135 "data in scan results.");
1136COMMAND(scan, trigger, "[freq <freq>*] [ssid <ssid>*|passive]",
1137 NL80211_CMD_TRIGGER_SCAN, 0, CIB_NETDEV, handle_scan,
1138 "Trigger a scan on the given frequencies with probing for the given\n"
1139 "SSIDs (or wildcard if not given) unless passive scanning is requested.");