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