]> git.ipfire.org Git - thirdparty/iw.git/blame - scan.c
HT fixups
[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
424static void print_ht_capa(const uint8_t type, uint8_t len, const uint8_t *data)
425{
426#define PRINT_HT_CAP(_cond, _str) \
427 do { \
428 if (_cond) \
429 printf("\t\t\t" _str "\n"); \
430 } while (0)
431 struct ht_cap_data {
432 __u16 cap;
433 __u8 ampdu_params;
434 struct {
435 __u8 rx_mcs_bitmask[10]; /* last 3 bits reserved */
436 __u16 max_rx_rate_1mbps: 10,
437 reserved_0: 6;
438 __u8 tx_rx_mcs_defined:1,
439 tx_rx_mcs_not_equal:1,
440 tx_max_streams:2,
441 tx_unequal_modulation:1,
442 reserved_1:3; /* 3 reserved bits here */
443 __u8 reserved_2[3]; /* 24 reserved bits here = 27 */
444 } mcs_set;
445 __u16 ht_extend_cap;
446 __u32 tx_beamform_cap;
447 __u8 asel_cap;
448 } __attribute__((packed)) ht_cap;
449 struct ht_cap_data *htc = &ht_cap;
04953e90 450 __u8 ampdu_exponent, ampdu_spacing;
0c445c24
LR
451 bool tx_rx_mcs_equal = false;
452
453 if (len != 26) {
454 printf("\n\t\tHT Capability IE len != expected 26 bytes, skipping parse\n");
455 return;
456 }
457
458 memcpy(&ht_cap, data, 26);
459
357c1a5d
LR
460 printf("\n");
461 print_ht_capability(htc->cap);
0c445c24 462
0c445c24
LR
463
464 ampdu_exponent = htc->ampdu_params & 0x3;
0950993f 465 print_ampdu_length(ampdu_exponent);
0c445c24 466
0950993f
LR
467 ampdu_spacing = (htc->ampdu_params >> 2) & 0x3;
468 print_ampdu_spacing(ampdu_spacing);
0c445c24 469
0c445c24
LR
470 if (htc->mcs_set.tx_rx_mcs_defined && htc->mcs_set.tx_rx_mcs_not_equal)
471 tx_rx_mcs_equal = true;
472 if (tx_rx_mcs_equal)
04953e90 473 printf("\t\tSupported TX/RX MCS Indexes:");
0c445c24 474 else
04953e90
JB
475 printf("\t\tSupported RX MCS Indexes:");
476
477 print_mcs_index(htc->mcs_set.rx_mcs_bitmask);
0c445c24
LR
478
479 if (!htc->mcs_set.tx_rx_mcs_defined) {
480 /* This is actually quite common */
481 printf("\t\tNo TX MCS set defined\n");
482 goto out;
483 }
484
485 if (htc->mcs_set.tx_rx_mcs_not_equal) {
486 printf("\t\tMaximum supported TX spatial streams: %d\n",
487 htc->mcs_set.tx_max_streams);
488 printf("\t\tTX unequal modulation ");
489 if (htc->mcs_set.tx_unequal_modulation)
490 printf("supported\n");
491 else
492 printf("unsupported\n");
493 }
494
495out:
496 return;
497}
498
83b4934c 499static void print_capabilities(const uint8_t type, uint8_t len, const uint8_t *data)
9b880b00 500{
31d2d259
JB
501 int i, base, bit;
502 bool first = true;
503
504
505 for (i = 0; i < len; i++) {
506 base = i * 8;
507
508 for (bit = 0; bit < 8; bit++) {
509 if (!(data[i] & (1 << bit)))
510 continue;
511
512 if (!first)
513 printf(",");
514 else
515 first = false;
516
517 switch (bit + base) {
518 case 0:
519 printf(" HT Information Exchange Supported");
520 break;
521 case 1:
522 printf(" On-demand Beacon");
523 break;
524 case 2:
525 printf(" Extended Channel Switching");
526 break;
527 case 3:
528 printf(" Wave Indication");
529 break;
530 case 4:
531 printf(" PSMP Capability");
532 break;
533 case 5:
534 printf(" Service Interval Granularity");
535 break;
536 case 6:
537 printf(" S-PSMP Capability");
538 break;
539 default:
540 printf(" %d", bit);
541 break;
542 }
543 }
544 }
9b880b00 545
9b880b00
MH
546 printf("\n");
547}
548
83b4934c
JB
549struct ie_print {
550 const char *name;
551 void (*print)(const uint8_t type, uint8_t len, const uint8_t *data);
552 uint8_t minlen, maxlen;
febeb0c0 553 uint8_t flags;
764fe753
JB
554};
555
83b4934c
JB
556static void print_ie(const struct ie_print *p, const uint8_t type,
557 uint8_t len, const uint8_t *data)
4673a894 558{
83b4934c
JB
559 int i;
560
561 if (!p->print)
562 return;
563
564 printf("\t%s:", p->name);
565 if (len < p->minlen || len > p->maxlen) {
566 if (len > 1) {
567 printf(" <invalid: %d bytes:", len);
568 for (i = 0; i < len; i++)
569 printf(" %.02x", data[i]);
570 printf(">\n");
571 } else if (len)
572 printf(" <invalid: 1 byte: %.02x>\n", data[0]);
573 else
574 printf(" <invalid: no data>\n");
575 return;
576 }
577
578 p->print(type, len, data);
579}
580
581#define PRINT_IGN { \
582 .name = "IGNORE", \
583 .print = NULL, \
584 .minlen = 0, \
585 .maxlen = 255, \
4673a894
JB
586}
587
83b4934c 588static const struct ie_print ieprinters[] = {
febeb0c0
JB
589 [0] = { "SSID", print_ssid, 0, 32, BIT(PRINT_SCAN) | BIT(PRINT_LINK), },
590 [1] = { "Supported rates", print_supprates, 0, 255, BIT(PRINT_SCAN), },
014d581a 591 [3] = { "DS Parameter set", print_ds, 1, 1, BIT(PRINT_SCAN), },
83b4934c 592 [5] = PRINT_IGN,
febeb0c0
JB
593 [7] = { "Country", print_country, 3, 255, BIT(PRINT_SCAN), },
594 [32] = { "Power constraint", print_powerconstraint, 1, 1, BIT(PRINT_SCAN), },
595 [42] = { "ERP", print_erp, 1, 255, BIT(PRINT_SCAN), },
0c445c24 596 [45] = { "HT capabilities", print_ht_capa, 1, 255, BIT(PRINT_SCAN), },
febeb0c0
JB
597 [48] = { "RSN", print_rsn, 2, 255, BIT(PRINT_SCAN), },
598 [50] = { "Extended supported rates", print_supprates, 0, 255, BIT(PRINT_SCAN), },
599 [127] = { "Extended capabilities", print_capabilities, 0, 255, BIT(PRINT_SCAN), },
83b4934c
JB
600};
601
602static void print_wifi_wpa(const uint8_t type, uint8_t len, const uint8_t *data)
603{
604 print_rsn_ie("TKIP", "IEEE 802.1X", len, data);
605}
606
1cab57eb
JB
607static bool print_wifi_wmm_param(const uint8_t *data, uint8_t len)
608{
609 int i;
610 static const char *aci_tbl[] = { "BE", "BK", "VI", "VO" };
611
612 if (len < 19)
613 goto invalid;
614
615 if (data[0] != 1) {
cee4fe20 616 printf("Parameter: not version 1: ");
1cab57eb
JB
617 return false;
618 }
619
cee4fe20 620 printf("\t* Parameter version 1");
1cab57eb
JB
621
622 data++;
623
624 if (data[0] & 0x80)
625 printf("\n\t\t* u-APSD");
626
627 data += 2;
628
629 for (i = 0; i < 4; i++) {
eeac6099 630 printf("\n\t\t* %s:", aci_tbl[(data[0] >> 5) & 3]);
1cab57eb
JB
631 if (data[4] & 0x10)
632 printf(" acm");
633 printf(" CW %d-%d", (1 << (data[1] & 0xf)) - 1,
634 (1 << (data[1] >> 4)) - 1);
a2a4c265 635 printf(", AIFSN %d", data[0] & 0xf);
1cab57eb 636 if (data[2] | data[3])
cee4fe20 637 printf(", TXOP %d usec", (data[2] + (data[3] << 8)) * 32);
1cab57eb
JB
638 data += 4;
639 }
640
641 printf("\n");
642 return true;
643
644 invalid:
645 printf("invalid: ");
646 return false;
647}
648
83b4934c 649static void print_wifi_wmm(const uint8_t type, uint8_t len, const uint8_t *data)
6ff0c93a
MH
650{
651 int i;
652
6ff0c93a
MH
653 switch (data[0]) {
654 case 0x00:
83b4934c 655 printf(" information:");
6ff0c93a
MH
656 break;
657 case 0x01:
1cab57eb
JB
658 if (print_wifi_wmm_param(data + 1, len - 1))
659 return;
6ff0c93a
MH
660 break;
661 default:
83b4934c 662 printf(" type %d:", data[0]);
6ff0c93a
MH
663 break;
664 }
665
1cab57eb
JB
666 for(i = 1; i < len; i++)
667 printf(" %.02x", data[i]);
6ff0c93a
MH
668 printf("\n");
669}
670
83b4934c 671static void print_wifi_wps(const uint8_t type, uint8_t len, const uint8_t *data)
4673a894
JB
672{
673 bool first = true;
674 __u16 subtype, sublen;
675
4673a894
JB
676 while (len >= 4) {
677 subtype = (data[0] << 8) + data[1];
678 sublen = (data[2] << 8) + data[3];
679 if (sublen > len)
680 break;
681
682 switch (subtype) {
683 case 0x104a:
684 tab_on_first(&first);
dffc6750 685 printf("\t * Version: %d.%d\n", data[4] >> 4, data[4] & 0xF);
4673a894
JB
686 break;
687 case 0x1011:
688 tab_on_first(&first);
689 printf("\t * Device name: %.*s\n", sublen, data + 4);
690 break;
691 case 0x1021:
692 tab_on_first(&first);
693 printf("\t * Manufacturer: %.*s\n", sublen, data + 4);
694 break;
695 case 0x1023:
696 tab_on_first(&first);
697 printf("\t * Model: %.*s\n", sublen, data + 4);
698 break;
7ee5a865 699 case 0x1057: {
fe31a22e 700 __u8 val = data[4];
7ee5a865 701 tab_on_first(&first);
fe31a22e 702 printf("\t * AP setup locked: 0x%.2x\n", val);
7ee5a865
JB
703 break;
704 }
4673a894
JB
705 case 0x1008: {
706 __u16 meth = (data[4] << 8) + data[5];
707 bool comma = false;
708 tab_on_first(&first);
709 printf("\t * Config methods:");
710#define T(bit, name) do { \
711 if (meth & (1<<bit)) { \
712 if (comma) \
713 printf(","); \
714 comma = true; \
715 printf(" " name); \
716 } } while (0)
717 T(0, "USB");
718 T(1, "Ethernet");
719 T(2, "Label");
720 T(3, "Display");
721 T(4, "Ext. NFC");
722 T(5, "Int. NFC");
723 T(6, "NFC Intf.");
724 T(7, "PBC");
725 T(8, "Keypad");
726 printf("\n");
727 break;
728#undef T
729 }
730 default:
731 break;
732 }
733
734 data += sublen + 4;
735 len -= sublen + 4;
736 }
737
738 if (len != 0) {
739 printf("\t\t * bogus tail data (%d):", len);
740 while (len) {
741 printf(" %.2x", *data);
742 data++;
743 len--;
744 }
745 printf("\n");
746 }
747}
748
83b4934c 749static const struct ie_print wifiprinters[] = {
febeb0c0
JB
750 [1] = { "WPA", print_wifi_wpa, 2, 255, BIT(PRINT_SCAN), },
751 [2] = { "WMM", print_wifi_wmm, 1, 255, BIT(PRINT_SCAN), },
752 [4] = { "WPS", print_wifi_wps, 0, 255, BIT(PRINT_SCAN), },
4673a894
JB
753};
754
764fe753 755static void print_vendor(unsigned char len, unsigned char *data,
febeb0c0 756 bool unknown, enum print_ie_type ptype)
3563f4c5
JB
757{
758 int i;
759
fbf80af5 760 if (len < 3) {
4673a894 761 printf("\tVendor specific: <too short> data:");
fbf80af5
JB
762 for(i = 0; i < len; i++)
763 printf(" %.02x", data[i]);
764 printf("\n");
765 return;
766 }
767
857d966e 768 if (len >= 4 && memcmp(data, wifi_oui, 3) == 0) {
febeb0c0
JB
769 if (data[3] < ARRAY_SIZE(wifiprinters) &&
770 wifiprinters[data[3]].name &&
771 wifiprinters[data[3]].flags & BIT(ptype)) {
83b4934c
JB
772 print_ie(&wifiprinters[data[3]], data[3], len - 4, data + 4);
773 return;
774 }
febeb0c0 775 if (!unknown)
4673a894 776 return;
857d966e 777 printf("\tWiFi OUI %#.2x, data:", data[3]);
4673a894
JB
778 for(i = 0; i < len - 4; i++)
779 printf(" %.02x", data[i + 4]);
780 printf("\n");
781 return;
782 }
783
febeb0c0 784 if (!unknown)
764fe753
JB
785 return;
786
fbf80af5 787 printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:",
3563f4c5 788 data[0], data[1], data[2]);
fbf80af5
JB
789 for (i = 3; i < len; i++)
790 printf(" %.2x", data[i]);
3563f4c5
JB
791 printf("\n");
792}
793
febeb0c0
JB
794void print_ies(unsigned char *ie, int ielen, bool unknown,
795 enum print_ie_type ptype)
3563f4c5
JB
796{
797 while (ielen >= 2 && ielen >= ie[1]) {
febeb0c0
JB
798 if (ie[0] < ARRAY_SIZE(ieprinters) &&
799 ieprinters[ie[0]].name &&
800 ieprinters[ie[0]].flags & BIT(ptype)) {
83b4934c 801 print_ie(&ieprinters[ie[0]], ie[0], ie[1], ie + 2);
764fe753 802 } else if (ie[0] == 221 /* vendor */) {
febeb0c0
JB
803 print_vendor(ie[1], ie + 2, unknown, ptype);
804 } else if (unknown) {
3563f4c5
JB
805 int i;
806
8086b700 807 printf("\tUnknown IE (%d):", ie[0]);
3563f4c5 808 for (i=0; i<ie[1]; i++)
8086b700 809 printf(" %.2x", ie[2+i]);
3563f4c5
JB
810 printf("\n");
811 }
812 ielen -= ie[1] + 2;
813 ie += ie[1] + 2;
814 }
815}
816
817static int print_bss_handler(struct nl_msg *msg, void *arg)
818{
819 struct nlattr *tb[NL80211_ATTR_MAX + 1];
820 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
821 struct nlattr *bss[NL80211_BSS_MAX + 1];
822 char mac_addr[20], dev[20];
823 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
824 [NL80211_BSS_TSF] = { .type = NLA_U64 },
825 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
826 [NL80211_BSS_BSSID] = { },
827 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
828 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
829 [NL80211_BSS_INFORMATION_ELEMENTS] = { },
f2e17e1f
JB
830 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
831 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
a56117a6 832 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
c04a78df 833 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
3563f4c5 834 };
febeb0c0 835 struct scan_params *params = arg;
3563f4c5
JB
836
837 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
838 genlmsg_attrlen(gnlh, 0), NULL);
839
840 if (!tb[NL80211_ATTR_BSS]) {
5fe70c0e 841 fprintf(stderr, "bss info missing!\n");
3563f4c5
JB
842 return NL_SKIP;
843 }
844 if (nla_parse_nested(bss, NL80211_BSS_MAX,
845 tb[NL80211_ATTR_BSS],
846 bss_policy)) {
5fe70c0e 847 fprintf(stderr, "failed to parse nested attributes!\n");
3563f4c5
JB
848 return NL_SKIP;
849 }
850
851 if (!bss[NL80211_BSS_BSSID])
852 return NL_SKIP;
853
854 mac_addr_n2a(mac_addr, nla_data(bss[NL80211_BSS_BSSID]));
855 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
a56117a6
JB
856 printf("BSS %s (on %s)", mac_addr, dev);
857
858 if (bss[NL80211_BSS_STATUS]) {
859 switch (nla_get_u32(bss[NL80211_BSS_STATUS])) {
860 case NL80211_BSS_STATUS_AUTHENTICATED:
861 printf(" -- authenticated");
862 break;
863 case NL80211_BSS_STATUS_ASSOCIATED:
864 printf(" -- associated");
865 break;
866 case NL80211_BSS_STATUS_IBSS_JOINED:
867 printf(" -- joined");
868 break;
869 default:
870 printf(" -- unknown status: %d",
871 nla_get_u32(bss[NL80211_BSS_STATUS]));
872 break;
873 }
874 }
875 printf("\n");
3563f4c5 876
e7109a8a
JB
877 if (bss[NL80211_BSS_TSF]) {
878 unsigned long long tsf;
879 tsf = (unsigned long long)nla_get_u64(bss[NL80211_BSS_TSF]);
880 printf("\tTSF: %llu usec (%llud, %.2lld:%.2llu:%.2llu)\n",
881 tsf, tsf/1000/1000/60/60/24, (tsf/1000/1000/60/60) % 24,
882 (tsf/1000/1000/60) % 60, (tsf/1000/1000) % 60);
883 }
3563f4c5
JB
884 if (bss[NL80211_BSS_FREQUENCY])
885 printf("\tfreq: %d\n",
886 nla_get_u32(bss[NL80211_BSS_FREQUENCY]));
887 if (bss[NL80211_BSS_BEACON_INTERVAL])
888 printf("\tbeacon interval: %d\n",
889 nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]));
92a04ecd
MH
890 if (bss[NL80211_BSS_CAPABILITY]) {
891 __u16 capa = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
892 printf("\tcapability:");
893 if (capa & WLAN_CAPABILITY_ESS)
894 printf(" ESS");
895 if (capa & WLAN_CAPABILITY_IBSS)
896 printf(" IBSS");
897 if (capa & WLAN_CAPABILITY_PRIVACY)
898 printf(" Privacy");
899 if (capa & WLAN_CAPABILITY_SHORT_PREAMBLE)
900 printf(" ShortPreamble");
901 if (capa & WLAN_CAPABILITY_PBCC)
902 printf(" PBCC");
903 if (capa & WLAN_CAPABILITY_CHANNEL_AGILITY)
904 printf(" ChannelAgility");
905 if (capa & WLAN_CAPABILITY_SPECTRUM_MGMT)
906 printf(" SpectrumMgmt");
907 if (capa & WLAN_CAPABILITY_QOS)
908 printf(" QoS");
909 if (capa & WLAN_CAPABILITY_SHORT_SLOT_TIME)
910 printf(" ShortSlotTime");
911 if (capa & WLAN_CAPABILITY_APSD)
912 printf(" APSD");
913 if (capa & WLAN_CAPABILITY_DSSS_OFDM)
914 printf(" DSSS-OFDM");
915 printf(" (0x%.4x)\n", capa);
916 }
f2e17e1f
JB
917 if (bss[NL80211_BSS_SIGNAL_MBM]) {
918 int s = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
919 printf("\tsignal: %d.%.2d dBm\n", s/100, s%100);
920 }
921 if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
922 unsigned char s = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
923 printf("\tsignal: %d/100\n", s);
924 }
c04a78df
HS
925 if (bss[NL80211_BSS_SEEN_MS_AGO]) {
926 int age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
927 printf("\tlast seen: %d ms ago\n", age);
928 }
3563f4c5
JB
929 if (bss[NL80211_BSS_INFORMATION_ELEMENTS])
930 print_ies(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
764fe753 931 nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
febeb0c0 932 params->unknown, params->type);
3563f4c5
JB
933
934 return NL_SKIP;
935}
936
764fe753 937static struct scan_params scan_params;
3563f4c5 938
7c37a24d
JB
939static int handle_scan_dump(struct nl80211_state *state,
940 struct nl_cb *cb,
3563f4c5
JB
941 struct nl_msg *msg,
942 int argc, char **argv)
943{
764fe753
JB
944 if (argc > 1)
945 return 1;
946
947 scan_params.unknown = false;
948 if (argc == 1 && !strcmp(argv[0], "-u"))
949 scan_params.unknown = true;
950
febeb0c0
JB
951 scan_params.type = PRINT_SCAN;
952
764fe753
JB
953 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_bss_handler,
954 &scan_params);
3563f4c5
JB
955 return 0;
956}
a5fe4ef2
JB
957
958static int handle_scan_combined(struct nl80211_state *state,
959 struct nl_cb *cb,
960 struct nl_msg *msg,
961 int argc, char **argv)
962{
559a1713 963 char **trig_argv;
a5fe4ef2
JB
964 static char *dump_argv[] = {
965 NULL,
966 "scan",
967 "dump",
92649eab 968 NULL,
a5fe4ef2
JB
969 };
970 static const __u32 cmds[] = {
971 NL80211_CMD_NEW_SCAN_RESULTS,
972 NL80211_CMD_SCAN_ABORTED,
973 };
559a1713 974 int trig_argc, dump_argc, err;
a5fe4ef2 975
559a1713
JB
976 if (argc >= 3 && !strcmp(argv[2], "-u")) {
977 dump_argc = 4;
978 dump_argv[3] = "-u";
979 } else
980 dump_argc = 3;
981
982 trig_argc = 3 + (argc - 2) + (3 - dump_argc);
983 trig_argv = calloc(trig_argc, sizeof(*trig_argv));
984 if (!trig_argv)
985 return -ENOMEM;
a5fe4ef2 986 trig_argv[0] = argv[0];
559a1713
JB
987 trig_argv[1] = "scan";
988 trig_argv[2] = "trigger";
989 int i;
990 for (i = 0; i < argc - 2 - (dump_argc - 3); i++)
991 trig_argv[i + 3] = argv[i + 2 + (dump_argc - 3)];
992 err = handle_cmd(state, II_NETDEV, trig_argc, trig_argv);
993 free(trig_argv);
a5fe4ef2
JB
994 if (err)
995 return err;
996
61725dbe
JB
997 /*
998 * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION
999 *
1000 * This code has a bug, which requires creating a separate
1001 * nl80211 socket to fix:
1002 * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or
1003 * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel
1004 * before (!) we listen to it, because we only start listening
1005 * after we send our scan request.
1006 *
1007 * Doing it the other way around has a race condition as well,
1008 * if you first open the events socket you may get a notification
1009 * for a previous scan.
1010 *
1011 * The only proper way to fix this would be to listen to events
1012 * before sending the command, and for the kernel to send the
1013 * scan request along with the event, so that you can match up
1014 * whether the scan you requested was finished or aborted (this
1015 * may result in processing a scan that another application
1016 * requested, but that doesn't seem to be a problem).
1017 *
1018 * Alas, the kernel doesn't do that (yet).
1019 */
1020
a5fe4ef2
JB
1021 if (listen_events(state, ARRAY_SIZE(cmds), cmds) ==
1022 NL80211_CMD_SCAN_ABORTED) {
1023 printf("scan aborted!\n");
1024 return 0;
1025 }
1026
1027 dump_argv[0] = argv[0];
92649eab 1028 return handle_cmd(state, II_NETDEV, dump_argc, dump_argv);
a5fe4ef2 1029}
6ca98d28
JB
1030TOPLEVEL(scan, "[-u] [freq <freq>*] [ssid <ssid>*|passive]", 0, 0,
1031 CIB_NETDEV, handle_scan_combined,
1032 "Scan on the given frequencies and probe for the given SSIDs\n"
1033 "(or wildcard if not given) unless passive scanning is requested.\n"
1034 "If -u is specified print unknown data in the scan results.");
4698bfc2
JB
1035COMMAND(scan, dump, "[-u]",
1036 NL80211_CMD_GET_SCAN, NLM_F_DUMP, CIB_NETDEV, handle_scan_dump,
1037 "Dump the current scan results. If -u is specified, print unknown\n"
1038 "data in scan results.");
1039COMMAND(scan, trigger, "[freq <freq>*] [ssid <ssid>*|passive]",
1040 NL80211_CMD_TRIGGER_SCAN, 0, CIB_NETDEV, handle_scan,
1041 "Trigger a scan on the given frequencies with probing for the given\n"
1042 "SSIDs (or wildcard if not given) unless passive scanning is requested.");