]> git.ipfire.org Git - thirdparty/iw.git/blame - scan.c
iw: change install command to make it compatible with ancient coreutils
[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 }
731 default:
732 break;
733 }
734
735 data += sublen + 4;
736 len -= sublen + 4;
737 }
738
739 if (len != 0) {
740 printf("\t\t * bogus tail data (%d):", len);
741 while (len) {
742 printf(" %.2x", *data);
743 data++;
744 len--;
745 }
746 printf("\n");
747 }
748}
749
83b4934c 750static const struct ie_print wifiprinters[] = {
febeb0c0
JB
751 [1] = { "WPA", print_wifi_wpa, 2, 255, BIT(PRINT_SCAN), },
752 [2] = { "WMM", print_wifi_wmm, 1, 255, BIT(PRINT_SCAN), },
753 [4] = { "WPS", print_wifi_wps, 0, 255, BIT(PRINT_SCAN), },
4673a894
JB
754};
755
764fe753 756static void print_vendor(unsigned char len, unsigned char *data,
febeb0c0 757 bool unknown, enum print_ie_type ptype)
3563f4c5
JB
758{
759 int i;
760
fbf80af5 761 if (len < 3) {
4673a894 762 printf("\tVendor specific: <too short> data:");
fbf80af5
JB
763 for(i = 0; i < len; i++)
764 printf(" %.02x", data[i]);
765 printf("\n");
766 return;
767 }
768
857d966e 769 if (len >= 4 && memcmp(data, wifi_oui, 3) == 0) {
febeb0c0
JB
770 if (data[3] < ARRAY_SIZE(wifiprinters) &&
771 wifiprinters[data[3]].name &&
772 wifiprinters[data[3]].flags & BIT(ptype)) {
83b4934c
JB
773 print_ie(&wifiprinters[data[3]], data[3], len - 4, data + 4);
774 return;
775 }
febeb0c0 776 if (!unknown)
4673a894 777 return;
857d966e 778 printf("\tWiFi OUI %#.2x, data:", data[3]);
4673a894
JB
779 for(i = 0; i < len - 4; i++)
780 printf(" %.02x", data[i + 4]);
781 printf("\n");
782 return;
783 }
784
febeb0c0 785 if (!unknown)
764fe753
JB
786 return;
787
fbf80af5 788 printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:",
3563f4c5 789 data[0], data[1], data[2]);
fbf80af5
JB
790 for (i = 3; i < len; i++)
791 printf(" %.2x", data[i]);
3563f4c5
JB
792 printf("\n");
793}
794
febeb0c0
JB
795void print_ies(unsigned char *ie, int ielen, bool unknown,
796 enum print_ie_type ptype)
3563f4c5
JB
797{
798 while (ielen >= 2 && ielen >= ie[1]) {
febeb0c0
JB
799 if (ie[0] < ARRAY_SIZE(ieprinters) &&
800 ieprinters[ie[0]].name &&
801 ieprinters[ie[0]].flags & BIT(ptype)) {
83b4934c 802 print_ie(&ieprinters[ie[0]], ie[0], ie[1], ie + 2);
764fe753 803 } else if (ie[0] == 221 /* vendor */) {
febeb0c0
JB
804 print_vendor(ie[1], ie + 2, unknown, ptype);
805 } else if (unknown) {
3563f4c5
JB
806 int i;
807
8086b700 808 printf("\tUnknown IE (%d):", ie[0]);
3563f4c5 809 for (i=0; i<ie[1]; i++)
8086b700 810 printf(" %.2x", ie[2+i]);
3563f4c5
JB
811 printf("\n");
812 }
813 ielen -= ie[1] + 2;
814 ie += ie[1] + 2;
815 }
816}
817
818static int print_bss_handler(struct nl_msg *msg, void *arg)
819{
820 struct nlattr *tb[NL80211_ATTR_MAX + 1];
821 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
822 struct nlattr *bss[NL80211_BSS_MAX + 1];
823 char mac_addr[20], dev[20];
824 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
825 [NL80211_BSS_TSF] = { .type = NLA_U64 },
826 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
827 [NL80211_BSS_BSSID] = { },
828 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
829 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
830 [NL80211_BSS_INFORMATION_ELEMENTS] = { },
f2e17e1f
JB
831 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
832 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
a56117a6 833 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
c04a78df 834 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
575280cc 835 [NL80211_BSS_BEACON_IES] = { },
3563f4c5 836 };
febeb0c0 837 struct scan_params *params = arg;
1c5bcd9c 838 int show = params->show_both_ie_sets ? 2 : 1;
3563f4c5
JB
839
840 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
841 genlmsg_attrlen(gnlh, 0), NULL);
842
843 if (!tb[NL80211_ATTR_BSS]) {
5fe70c0e 844 fprintf(stderr, "bss info missing!\n");
3563f4c5
JB
845 return NL_SKIP;
846 }
847 if (nla_parse_nested(bss, NL80211_BSS_MAX,
848 tb[NL80211_ATTR_BSS],
849 bss_policy)) {
5fe70c0e 850 fprintf(stderr, "failed to parse nested attributes!\n");
3563f4c5
JB
851 return NL_SKIP;
852 }
853
854 if (!bss[NL80211_BSS_BSSID])
855 return NL_SKIP;
856
857 mac_addr_n2a(mac_addr, nla_data(bss[NL80211_BSS_BSSID]));
858 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
a56117a6
JB
859 printf("BSS %s (on %s)", mac_addr, dev);
860
861 if (bss[NL80211_BSS_STATUS]) {
862 switch (nla_get_u32(bss[NL80211_BSS_STATUS])) {
863 case NL80211_BSS_STATUS_AUTHENTICATED:
864 printf(" -- authenticated");
865 break;
866 case NL80211_BSS_STATUS_ASSOCIATED:
867 printf(" -- associated");
868 break;
869 case NL80211_BSS_STATUS_IBSS_JOINED:
870 printf(" -- joined");
871 break;
872 default:
873 printf(" -- unknown status: %d",
874 nla_get_u32(bss[NL80211_BSS_STATUS]));
875 break;
876 }
877 }
878 printf("\n");
3563f4c5 879
e7109a8a
JB
880 if (bss[NL80211_BSS_TSF]) {
881 unsigned long long tsf;
882 tsf = (unsigned long long)nla_get_u64(bss[NL80211_BSS_TSF]);
883 printf("\tTSF: %llu usec (%llud, %.2lld:%.2llu:%.2llu)\n",
884 tsf, tsf/1000/1000/60/60/24, (tsf/1000/1000/60/60) % 24,
885 (tsf/1000/1000/60) % 60, (tsf/1000/1000) % 60);
886 }
3563f4c5
JB
887 if (bss[NL80211_BSS_FREQUENCY])
888 printf("\tfreq: %d\n",
889 nla_get_u32(bss[NL80211_BSS_FREQUENCY]));
890 if (bss[NL80211_BSS_BEACON_INTERVAL])
891 printf("\tbeacon interval: %d\n",
892 nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]));
92a04ecd
MH
893 if (bss[NL80211_BSS_CAPABILITY]) {
894 __u16 capa = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
895 printf("\tcapability:");
896 if (capa & WLAN_CAPABILITY_ESS)
897 printf(" ESS");
898 if (capa & WLAN_CAPABILITY_IBSS)
899 printf(" IBSS");
900 if (capa & WLAN_CAPABILITY_PRIVACY)
901 printf(" Privacy");
902 if (capa & WLAN_CAPABILITY_SHORT_PREAMBLE)
903 printf(" ShortPreamble");
904 if (capa & WLAN_CAPABILITY_PBCC)
905 printf(" PBCC");
906 if (capa & WLAN_CAPABILITY_CHANNEL_AGILITY)
907 printf(" ChannelAgility");
908 if (capa & WLAN_CAPABILITY_SPECTRUM_MGMT)
909 printf(" SpectrumMgmt");
910 if (capa & WLAN_CAPABILITY_QOS)
911 printf(" QoS");
912 if (capa & WLAN_CAPABILITY_SHORT_SLOT_TIME)
913 printf(" ShortSlotTime");
914 if (capa & WLAN_CAPABILITY_APSD)
915 printf(" APSD");
916 if (capa & WLAN_CAPABILITY_DSSS_OFDM)
917 printf(" DSSS-OFDM");
918 printf(" (0x%.4x)\n", capa);
919 }
f2e17e1f
JB
920 if (bss[NL80211_BSS_SIGNAL_MBM]) {
921 int s = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
922 printf("\tsignal: %d.%.2d dBm\n", s/100, s%100);
923 }
924 if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
925 unsigned char s = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
926 printf("\tsignal: %d/100\n", s);
927 }
c04a78df
HS
928 if (bss[NL80211_BSS_SEEN_MS_AGO]) {
929 int age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
930 printf("\tlast seen: %d ms ago\n", age);
931 }
1c5bcd9c
JB
932
933 if (bss[NL80211_BSS_INFORMATION_ELEMENTS] && show--) {
575280cc
JM
934 if (bss[NL80211_BSS_BEACON_IES])
935 printf("\tInformation elements from Probe Response "
936 "frame:\n");
3563f4c5 937 print_ies(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
764fe753 938 nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
febeb0c0 939 params->unknown, params->type);
575280cc 940 }
1c5bcd9c 941 if (bss[NL80211_BSS_BEACON_IES] && show--) {
575280cc
JM
942 printf("\tInformation elements from Beacon frame:\n");
943 print_ies(nla_data(bss[NL80211_BSS_BEACON_IES]),
944 nla_len(bss[NL80211_BSS_BEACON_IES]),
945 params->unknown, params->type);
946 }
3563f4c5
JB
947
948 return NL_SKIP;
949}
950
764fe753 951static struct scan_params scan_params;
3563f4c5 952
7c37a24d
JB
953static int handle_scan_dump(struct nl80211_state *state,
954 struct nl_cb *cb,
3563f4c5
JB
955 struct nl_msg *msg,
956 int argc, char **argv)
957{
764fe753
JB
958 if (argc > 1)
959 return 1;
960
1c5bcd9c
JB
961 memset(&scan_params, 0, sizeof(scan_params));
962
764fe753
JB
963 if (argc == 1 && !strcmp(argv[0], "-u"))
964 scan_params.unknown = true;
575280cc
JM
965 else if (argc == 1 && !strcmp(argv[0], "-b"))
966 scan_params.show_both_ie_sets = true;
764fe753 967
febeb0c0
JB
968 scan_params.type = PRINT_SCAN;
969
764fe753
JB
970 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_bss_handler,
971 &scan_params);
3563f4c5
JB
972 return 0;
973}
a5fe4ef2
JB
974
975static int handle_scan_combined(struct nl80211_state *state,
976 struct nl_cb *cb,
977 struct nl_msg *msg,
978 int argc, char **argv)
979{
559a1713 980 char **trig_argv;
a5fe4ef2
JB
981 static char *dump_argv[] = {
982 NULL,
983 "scan",
984 "dump",
92649eab 985 NULL,
a5fe4ef2
JB
986 };
987 static const __u32 cmds[] = {
988 NL80211_CMD_NEW_SCAN_RESULTS,
989 NL80211_CMD_SCAN_ABORTED,
990 };
559a1713 991 int trig_argc, dump_argc, err;
a5fe4ef2 992
559a1713
JB
993 if (argc >= 3 && !strcmp(argv[2], "-u")) {
994 dump_argc = 4;
995 dump_argv[3] = "-u";
575280cc
JM
996 } else if (argc >= 3 && !strcmp(argv[2], "-b")) {
997 dump_argc = 4;
998 dump_argv[3] = "-b";
559a1713
JB
999 } else
1000 dump_argc = 3;
1001
1002 trig_argc = 3 + (argc - 2) + (3 - dump_argc);
1003 trig_argv = calloc(trig_argc, sizeof(*trig_argv));
1004 if (!trig_argv)
1005 return -ENOMEM;
a5fe4ef2 1006 trig_argv[0] = argv[0];
559a1713
JB
1007 trig_argv[1] = "scan";
1008 trig_argv[2] = "trigger";
1009 int i;
1010 for (i = 0; i < argc - 2 - (dump_argc - 3); i++)
1011 trig_argv[i + 3] = argv[i + 2 + (dump_argc - 3)];
1012 err = handle_cmd(state, II_NETDEV, trig_argc, trig_argv);
1013 free(trig_argv);
a5fe4ef2
JB
1014 if (err)
1015 return err;
1016
61725dbe
JB
1017 /*
1018 * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION
1019 *
1020 * This code has a bug, which requires creating a separate
1021 * nl80211 socket to fix:
1022 * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or
1023 * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel
1024 * before (!) we listen to it, because we only start listening
1025 * after we send our scan request.
1026 *
1027 * Doing it the other way around has a race condition as well,
1028 * if you first open the events socket you may get a notification
1029 * for a previous scan.
1030 *
1031 * The only proper way to fix this would be to listen to events
1032 * before sending the command, and for the kernel to send the
1033 * scan request along with the event, so that you can match up
1034 * whether the scan you requested was finished or aborted (this
1035 * may result in processing a scan that another application
1036 * requested, but that doesn't seem to be a problem).
1037 *
1038 * Alas, the kernel doesn't do that (yet).
1039 */
1040
a5fe4ef2
JB
1041 if (listen_events(state, ARRAY_SIZE(cmds), cmds) ==
1042 NL80211_CMD_SCAN_ABORTED) {
1043 printf("scan aborted!\n");
1044 return 0;
1045 }
1046
1047 dump_argv[0] = argv[0];
92649eab 1048 return handle_cmd(state, II_NETDEV, dump_argc, dump_argv);
a5fe4ef2 1049}
6ca98d28
JB
1050TOPLEVEL(scan, "[-u] [freq <freq>*] [ssid <ssid>*|passive]", 0, 0,
1051 CIB_NETDEV, handle_scan_combined,
1052 "Scan on the given frequencies and probe for the given SSIDs\n"
1053 "(or wildcard if not given) unless passive scanning is requested.\n"
1054 "If -u is specified print unknown data in the scan results.");
4698bfc2
JB
1055COMMAND(scan, dump, "[-u]",
1056 NL80211_CMD_GET_SCAN, NLM_F_DUMP, CIB_NETDEV, handle_scan_dump,
1057 "Dump the current scan results. If -u is specified, print unknown\n"
1058 "data in scan results.");
1059COMMAND(scan, trigger, "[freq <freq>*] [ssid <ssid>*|passive]",
1060 NL80211_CMD_TRIGGER_SCAN, 0, CIB_NETDEV, handle_scan,
1061 "Trigger a scan on the given frequencies with probing for the given\n"
1062 "SSIDs (or wildcard if not given) unless passive scanning is requested.");