]>
Commit | Line | Data |
---|---|---|
1 | #include <net/if.h> | |
2 | #include <errno.h> | |
3 | #include <string.h> | |
4 | #include <ctype.h> | |
5 | #include <stdbool.h> | |
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 | ||
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 | ||
30 | static unsigned char wifi_oui[3] = { 0x00, 0x50, 0xf2 }; | |
31 | static unsigned char ieee80211_oui[3] = { 0x00, 0x0f, 0xac }; | |
32 | ||
33 | struct scan_params { | |
34 | bool unknown; | |
35 | enum print_ie_type type; | |
36 | }; | |
37 | ||
38 | static int handle_scan(struct nl80211_state *state, | |
39 | struct nl_cb *cb, | |
40 | struct nl_msg *msg, | |
41 | int argc, char **argv) | |
42 | { | |
43 | struct nl_msg *ssids = NULL, *freqs = NULL; | |
44 | char *eptr; | |
45 | int err = -ENOBUFS; | |
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; | |
55 | ||
56 | ssids = nlmsg_alloc(); | |
57 | if (!ssids) | |
58 | return -ENOMEM; | |
59 | ||
60 | freqs = nlmsg_alloc(); | |
61 | if (!freqs) { | |
62 | nlmsg_free(ssids); | |
63 | return -ENOMEM; | |
64 | } | |
65 | ||
66 | for (i = 0; i < argc; i++) { | |
67 | if (parse == NONE && strcmp(argv[i], "freq") == 0) { | |
68 | parse = FREQ; | |
69 | have_freqs = true; | |
70 | continue; | |
71 | } else if (parse < SSID && strcmp(argv[i], "ssid") == 0) { | |
72 | parse = SSID; | |
73 | have_ssids = true; | |
74 | continue; | |
75 | } else if (parse < SSID && strcmp(argv[i], "passive") == 0) { | |
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); | |
104 | ||
105 | err = 0; | |
106 | nla_put_failure: | |
107 | nlmsg_free(ssids); | |
108 | nlmsg_free(freqs); | |
109 | return err; | |
110 | } | |
111 | ||
112 | static void tab_on_first(bool *first) | |
113 | { | |
114 | if (!*first) | |
115 | printf("\t"); | |
116 | else | |
117 | *first = false; | |
118 | } | |
119 | ||
120 | static void print_ssid(const uint8_t type, uint8_t len, const uint8_t *data) | |
121 | { | |
122 | printf(" "); | |
123 | print_ssid_escaped(len, data); | |
124 | printf("\n"); | |
125 | } | |
126 | ||
127 | static void print_supprates(const uint8_t type, uint8_t len, const uint8_t *data) | |
128 | { | |
129 | int i; | |
130 | ||
131 | printf(" "); | |
132 | ||
133 | for (i = 0; i < len; i++) { | |
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 | ||
140 | static void print_ds(const uint8_t type, uint8_t len, const uint8_t *data) | |
141 | { | |
142 | printf(" channel %d\n", data[0]); | |
143 | } | |
144 | ||
145 | static void print_country(const uint8_t type, uint8_t len, const uint8_t *data) | |
146 | { | |
147 | int i; | |
148 | ||
149 | printf(" %.*s", 2, data); | |
150 | switch (data[2]) { | |
151 | case 'I': | |
152 | printf(" (indoor)"); | |
153 | break; | |
154 | case 'O': | |
155 | printf(" (outdoor)"); | |
156 | break; | |
157 | case ' ': | |
158 | printf(" (in/outdoor)"); | |
159 | break; | |
160 | default: | |
161 | printf(" (invalid environment)"); | |
162 | break; | |
163 | } | |
164 | printf(", data:"); | |
165 | for(i=0; i<len-3; i++) | |
166 | printf(" %.02x", data[i + 3]); | |
167 | printf("\n"); | |
168 | } | |
169 | ||
170 | static void print_powerconstraint(const uint8_t type, uint8_t len, const uint8_t *data) | |
171 | { | |
172 | printf(" %d dB\n", data[0]); | |
173 | } | |
174 | ||
175 | static void print_erp(const uint8_t type, uint8_t len, const uint8_t *data) | |
176 | { | |
177 | if (data[0] == 0x00) | |
178 | printf(" <no flags>"); | |
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 | ||
188 | static void print_cipher(const uint8_t *data) | |
189 | { | |
190 | if (memcmp(data, wifi_oui, 3) == 0) { | |
191 | switch (data[3]) { | |
192 | case 0: | |
193 | printf("Use group cipher suite"); | |
194 | break; | |
195 | case 1: | |
196 | printf("WEP-40"); | |
197 | break; | |
198 | case 2: | |
199 | printf("TKIP"); | |
200 | break; | |
201 | case 4: | |
202 | printf("CCMP"); | |
203 | break; | |
204 | case 5: | |
205 | printf("WEP-104"); | |
206 | break; | |
207 | default: | |
208 | printf("%.02x-%.02x-%.02x:%d", | |
209 | data[0], data[1] ,data[2], data[3]); | |
210 | break; | |
211 | } | |
212 | } else if (memcmp(data, ieee80211_oui, 3) == 0) { | |
213 | switch (data[3]) { | |
214 | case 0: | |
215 | printf("Use group cipher suite"); | |
216 | break; | |
217 | case 1: | |
218 | printf("WEP-40"); | |
219 | break; | |
220 | case 2: | |
221 | printf("TKIP"); | |
222 | break; | |
223 | case 4: | |
224 | printf("CCMP"); | |
225 | break; | |
226 | case 5: | |
227 | printf("WEP-104"); | |
228 | break; | |
229 | case 6: | |
230 | printf("AES-128-CMAC"); | |
231 | break; | |
232 | default: | |
233 | printf("%.02x-%.02x-%.02x:%d", | |
234 | data[0], data[1] ,data[2], data[3]); | |
235 | break; | |
236 | } | |
237 | } else | |
238 | printf("%.02x-%.02x-%.02x:%d", | |
239 | data[0], data[1] ,data[2], data[3]); | |
240 | } | |
241 | ||
242 | static void print_auth(const uint8_t *data) | |
243 | { | |
244 | if (memcmp(data, wifi_oui, 3) == 0) { | |
245 | switch (data[3]) { | |
246 | case 1: | |
247 | printf("IEEE 802.1X"); | |
248 | break; | |
249 | case 2: | |
250 | printf("PSK"); | |
251 | break; | |
252 | default: | |
253 | printf("%.02x-%.02x-%.02x:%d", | |
254 | data[0], data[1] ,data[2], data[3]); | |
255 | break; | |
256 | } | |
257 | } else if (memcmp(data, ieee80211_oui, 3) == 0) { | |
258 | switch (data[3]) { | |
259 | case 1: | |
260 | printf("IEEE 802.1X"); | |
261 | break; | |
262 | case 2: | |
263 | printf("PSK"); | |
264 | break; | |
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; | |
277 | default: | |
278 | printf("%.02x-%.02x-%.02x:%d", | |
279 | data[0], data[1] ,data[2], data[3]); | |
280 | break; | |
281 | } | |
282 | } else | |
283 | printf("%.02x-%.02x-%.02x:%d", | |
284 | data[0], data[1] ,data[2], data[3]); | |
285 | } | |
286 | ||
287 | static void print_rsn_ie(const char *defcipher, const char *defauth, | |
288 | uint8_t len, const uint8_t *data) | |
289 | { | |
290 | bool first = true; | |
291 | __u16 version, count, capa; | |
292 | int i; | |
293 | ||
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); | |
323 | if (2 + (count * 4) > len) | |
324 | goto invalid; | |
325 | ||
326 | tab_on_first(&first); | |
327 | printf("\t * Pairwise ciphers:"); | |
328 | for (i = 0; i < count; i++) { | |
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); | |
344 | if (2 + (count * 4) > len) | |
345 | goto invalid; | |
346 | ||
347 | tab_on_first(&first); | |
348 | printf("\t * Authentication suites:"); | |
349 | for (i = 0; i < count; i++) { | |
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 | ||
358 | if (len >= 2) { | |
359 | capa = data[0] | (data[1] << 8); | |
360 | tab_on_first(&first); | |
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); | |
403 | data += 2; | |
404 | len -= 2; | |
405 | } | |
406 | ||
407 | invalid: | |
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 | } | |
417 | } | |
418 | ||
419 | static void print_rsn(const uint8_t type, uint8_t len, const uint8_t *data) | |
420 | { | |
421 | print_rsn_ie("CCMP", "IEEE 802.1X", len, data); | |
422 | } | |
423 | ||
424 | static void print_ht_capa(const uint8_t type, uint8_t len, const uint8_t *data) | |
425 | { | |
426 | if (len != 26) { | |
427 | printf("\n\t\tHT Capability IE len != expected 26 bytes, skipping parse\n"); | |
428 | return; | |
429 | } | |
430 | printf("\n"); | |
431 | print_ht_capability(data[0] | (data[1] << 8)); | |
432 | print_ampdu_length(data[2] & 3); | |
433 | print_ampdu_spacing((data[2] >> 2) & 3); | |
434 | print_ht_mcs(data + 3); | |
435 | } | |
436 | ||
437 | static void print_capabilities(const uint8_t type, uint8_t len, const uint8_t *data) | |
438 | { | |
439 | int i, base, bit; | |
440 | bool first = true; | |
441 | ||
442 | ||
443 | for (i = 0; i < len; i++) { | |
444 | base = i * 8; | |
445 | ||
446 | for (bit = 0; bit < 8; bit++) { | |
447 | if (!(data[i] & (1 << bit))) | |
448 | continue; | |
449 | ||
450 | if (!first) | |
451 | printf(","); | |
452 | else | |
453 | first = false; | |
454 | ||
455 | switch (bit + base) { | |
456 | case 0: | |
457 | printf(" HT Information Exchange Supported"); | |
458 | break; | |
459 | case 1: | |
460 | printf(" On-demand Beacon"); | |
461 | break; | |
462 | case 2: | |
463 | printf(" Extended Channel Switching"); | |
464 | break; | |
465 | case 3: | |
466 | printf(" Wave Indication"); | |
467 | break; | |
468 | case 4: | |
469 | printf(" PSMP Capability"); | |
470 | break; | |
471 | case 5: | |
472 | printf(" Service Interval Granularity"); | |
473 | break; | |
474 | case 6: | |
475 | printf(" S-PSMP Capability"); | |
476 | break; | |
477 | default: | |
478 | printf(" %d", bit); | |
479 | break; | |
480 | } | |
481 | } | |
482 | } | |
483 | ||
484 | printf("\n"); | |
485 | } | |
486 | ||
487 | struct ie_print { | |
488 | const char *name; | |
489 | void (*print)(const uint8_t type, uint8_t len, const uint8_t *data); | |
490 | uint8_t minlen, maxlen; | |
491 | uint8_t flags; | |
492 | }; | |
493 | ||
494 | static void print_ie(const struct ie_print *p, const uint8_t type, | |
495 | uint8_t len, const uint8_t *data) | |
496 | { | |
497 | int i; | |
498 | ||
499 | if (!p->print) | |
500 | return; | |
501 | ||
502 | printf("\t%s:", p->name); | |
503 | if (len < p->minlen || len > p->maxlen) { | |
504 | if (len > 1) { | |
505 | printf(" <invalid: %d bytes:", len); | |
506 | for (i = 0; i < len; i++) | |
507 | printf(" %.02x", data[i]); | |
508 | printf(">\n"); | |
509 | } else if (len) | |
510 | printf(" <invalid: 1 byte: %.02x>\n", data[0]); | |
511 | else | |
512 | printf(" <invalid: no data>\n"); | |
513 | return; | |
514 | } | |
515 | ||
516 | p->print(type, len, data); | |
517 | } | |
518 | ||
519 | #define PRINT_IGN { \ | |
520 | .name = "IGNORE", \ | |
521 | .print = NULL, \ | |
522 | .minlen = 0, \ | |
523 | .maxlen = 255, \ | |
524 | } | |
525 | ||
526 | static const struct ie_print ieprinters[] = { | |
527 | [0] = { "SSID", print_ssid, 0, 32, BIT(PRINT_SCAN) | BIT(PRINT_LINK), }, | |
528 | [1] = { "Supported rates", print_supprates, 0, 255, BIT(PRINT_SCAN), }, | |
529 | [3] = { "DS Parameter set", print_ds, 1, 1, BIT(PRINT_SCAN), }, | |
530 | [5] = PRINT_IGN, | |
531 | [7] = { "Country", print_country, 3, 255, BIT(PRINT_SCAN), }, | |
532 | [32] = { "Power constraint", print_powerconstraint, 1, 1, BIT(PRINT_SCAN), }, | |
533 | [42] = { "ERP", print_erp, 1, 255, BIT(PRINT_SCAN), }, | |
534 | [45] = { "HT capabilities", print_ht_capa, 1, 255, BIT(PRINT_SCAN), }, | |
535 | [48] = { "RSN", print_rsn, 2, 255, BIT(PRINT_SCAN), }, | |
536 | [50] = { "Extended supported rates", print_supprates, 0, 255, BIT(PRINT_SCAN), }, | |
537 | [127] = { "Extended capabilities", print_capabilities, 0, 255, BIT(PRINT_SCAN), }, | |
538 | }; | |
539 | ||
540 | static void print_wifi_wpa(const uint8_t type, uint8_t len, const uint8_t *data) | |
541 | { | |
542 | print_rsn_ie("TKIP", "IEEE 802.1X", len, data); | |
543 | } | |
544 | ||
545 | static bool print_wifi_wmm_param(const uint8_t *data, uint8_t len) | |
546 | { | |
547 | int i; | |
548 | static const char *aci_tbl[] = { "BE", "BK", "VI", "VO" }; | |
549 | ||
550 | if (len < 19) | |
551 | goto invalid; | |
552 | ||
553 | if (data[0] != 1) { | |
554 | printf("Parameter: not version 1: "); | |
555 | return false; | |
556 | } | |
557 | ||
558 | printf("\t * Parameter version 1"); | |
559 | ||
560 | data++; | |
561 | ||
562 | if (data[0] & 0x80) | |
563 | printf("\n\t\t * u-APSD"); | |
564 | ||
565 | data += 2; | |
566 | ||
567 | for (i = 0; i < 4; i++) { | |
568 | printf("\n\t\t * %s:", aci_tbl[(data[0] >> 5) & 3]); | |
569 | if (data[4] & 0x10) | |
570 | printf(" acm"); | |
571 | printf(" CW %d-%d", (1 << (data[1] & 0xf)) - 1, | |
572 | (1 << (data[1] >> 4)) - 1); | |
573 | printf(", AIFSN %d", data[0] & 0xf); | |
574 | if (data[2] | data[3]) | |
575 | printf(", TXOP %d usec", (data[2] + (data[3] << 8)) * 32); | |
576 | data += 4; | |
577 | } | |
578 | ||
579 | printf("\n"); | |
580 | return true; | |
581 | ||
582 | invalid: | |
583 | printf("invalid: "); | |
584 | return false; | |
585 | } | |
586 | ||
587 | static void print_wifi_wmm(const uint8_t type, uint8_t len, const uint8_t *data) | |
588 | { | |
589 | int i; | |
590 | ||
591 | switch (data[0]) { | |
592 | case 0x00: | |
593 | printf(" information:"); | |
594 | break; | |
595 | case 0x01: | |
596 | if (print_wifi_wmm_param(data + 1, len - 1)) | |
597 | return; | |
598 | break; | |
599 | default: | |
600 | printf(" type %d:", data[0]); | |
601 | break; | |
602 | } | |
603 | ||
604 | for(i = 1; i < len; i++) | |
605 | printf(" %.02x", data[i]); | |
606 | printf("\n"); | |
607 | } | |
608 | ||
609 | static void print_wifi_wps(const uint8_t type, uint8_t len, const uint8_t *data) | |
610 | { | |
611 | bool first = true; | |
612 | __u16 subtype, sublen; | |
613 | ||
614 | while (len >= 4) { | |
615 | subtype = (data[0] << 8) + data[1]; | |
616 | sublen = (data[2] << 8) + data[3]; | |
617 | if (sublen > len) | |
618 | break; | |
619 | ||
620 | switch (subtype) { | |
621 | case 0x104a: | |
622 | tab_on_first(&first); | |
623 | printf("\t * Version: %d.%d\n", data[4] >> 4, data[4] & 0xF); | |
624 | break; | |
625 | case 0x1011: | |
626 | tab_on_first(&first); | |
627 | printf("\t * Device name: %.*s\n", sublen, data + 4); | |
628 | break; | |
629 | case 0x1021: | |
630 | tab_on_first(&first); | |
631 | printf("\t * Manufacturer: %.*s\n", sublen, data + 4); | |
632 | break; | |
633 | case 0x1023: | |
634 | tab_on_first(&first); | |
635 | printf("\t * Model: %.*s\n", sublen, data + 4); | |
636 | break; | |
637 | case 0x1057: { | |
638 | __u8 val = data[4]; | |
639 | tab_on_first(&first); | |
640 | printf("\t * AP setup locked: 0x%.2x\n", val); | |
641 | break; | |
642 | } | |
643 | case 0x1008: { | |
644 | __u16 meth = (data[4] << 8) + data[5]; | |
645 | bool comma = false; | |
646 | tab_on_first(&first); | |
647 | printf("\t * Config methods:"); | |
648 | #define T(bit, name) do { \ | |
649 | if (meth & (1<<bit)) { \ | |
650 | if (comma) \ | |
651 | printf(","); \ | |
652 | comma = true; \ | |
653 | printf(" " name); \ | |
654 | } } while (0) | |
655 | T(0, "USB"); | |
656 | T(1, "Ethernet"); | |
657 | T(2, "Label"); | |
658 | T(3, "Display"); | |
659 | T(4, "Ext. NFC"); | |
660 | T(5, "Int. NFC"); | |
661 | T(6, "NFC Intf."); | |
662 | T(7, "PBC"); | |
663 | T(8, "Keypad"); | |
664 | printf("\n"); | |
665 | break; | |
666 | #undef T | |
667 | } | |
668 | default: | |
669 | break; | |
670 | } | |
671 | ||
672 | data += sublen + 4; | |
673 | len -= sublen + 4; | |
674 | } | |
675 | ||
676 | if (len != 0) { | |
677 | printf("\t\t * bogus tail data (%d):", len); | |
678 | while (len) { | |
679 | printf(" %.2x", *data); | |
680 | data++; | |
681 | len--; | |
682 | } | |
683 | printf("\n"); | |
684 | } | |
685 | } | |
686 | ||
687 | static const struct ie_print wifiprinters[] = { | |
688 | [1] = { "WPA", print_wifi_wpa, 2, 255, BIT(PRINT_SCAN), }, | |
689 | [2] = { "WMM", print_wifi_wmm, 1, 255, BIT(PRINT_SCAN), }, | |
690 | [4] = { "WPS", print_wifi_wps, 0, 255, BIT(PRINT_SCAN), }, | |
691 | }; | |
692 | ||
693 | static void print_vendor(unsigned char len, unsigned char *data, | |
694 | bool unknown, enum print_ie_type ptype) | |
695 | { | |
696 | int i; | |
697 | ||
698 | if (len < 3) { | |
699 | printf("\tVendor specific: <too short> data:"); | |
700 | for(i = 0; i < len; i++) | |
701 | printf(" %.02x", data[i]); | |
702 | printf("\n"); | |
703 | return; | |
704 | } | |
705 | ||
706 | if (len >= 4 && memcmp(data, wifi_oui, 3) == 0) { | |
707 | if (data[3] < ARRAY_SIZE(wifiprinters) && | |
708 | wifiprinters[data[3]].name && | |
709 | wifiprinters[data[3]].flags & BIT(ptype)) { | |
710 | print_ie(&wifiprinters[data[3]], data[3], len - 4, data + 4); | |
711 | return; | |
712 | } | |
713 | if (!unknown) | |
714 | return; | |
715 | printf("\tWiFi OUI %#.2x, data:", data[3]); | |
716 | for(i = 0; i < len - 4; i++) | |
717 | printf(" %.02x", data[i + 4]); | |
718 | printf("\n"); | |
719 | return; | |
720 | } | |
721 | ||
722 | if (!unknown) | |
723 | return; | |
724 | ||
725 | printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:", | |
726 | data[0], data[1], data[2]); | |
727 | for (i = 3; i < len; i++) | |
728 | printf(" %.2x", data[i]); | |
729 | printf("\n"); | |
730 | } | |
731 | ||
732 | void print_ies(unsigned char *ie, int ielen, bool unknown, | |
733 | enum print_ie_type ptype) | |
734 | { | |
735 | while (ielen >= 2 && ielen >= ie[1]) { | |
736 | if (ie[0] < ARRAY_SIZE(ieprinters) && | |
737 | ieprinters[ie[0]].name && | |
738 | ieprinters[ie[0]].flags & BIT(ptype)) { | |
739 | print_ie(&ieprinters[ie[0]], ie[0], ie[1], ie + 2); | |
740 | } else if (ie[0] == 221 /* vendor */) { | |
741 | print_vendor(ie[1], ie + 2, unknown, ptype); | |
742 | } else if (unknown) { | |
743 | int i; | |
744 | ||
745 | printf("\tUnknown IE (%d):", ie[0]); | |
746 | for (i=0; i<ie[1]; i++) | |
747 | printf(" %.2x", ie[2+i]); | |
748 | printf("\n"); | |
749 | } | |
750 | ielen -= ie[1] + 2; | |
751 | ie += ie[1] + 2; | |
752 | } | |
753 | } | |
754 | ||
755 | static int print_bss_handler(struct nl_msg *msg, void *arg) | |
756 | { | |
757 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
758 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
759 | struct nlattr *bss[NL80211_BSS_MAX + 1]; | |
760 | char mac_addr[20], dev[20]; | |
761 | static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = { | |
762 | [NL80211_BSS_TSF] = { .type = NLA_U64 }, | |
763 | [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 }, | |
764 | [NL80211_BSS_BSSID] = { }, | |
765 | [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 }, | |
766 | [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 }, | |
767 | [NL80211_BSS_INFORMATION_ELEMENTS] = { }, | |
768 | [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 }, | |
769 | [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 }, | |
770 | [NL80211_BSS_STATUS] = { .type = NLA_U32 }, | |
771 | [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 }, | |
772 | }; | |
773 | struct scan_params *params = arg; | |
774 | ||
775 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
776 | genlmsg_attrlen(gnlh, 0), NULL); | |
777 | ||
778 | if (!tb[NL80211_ATTR_BSS]) { | |
779 | fprintf(stderr, "bss info missing!\n"); | |
780 | return NL_SKIP; | |
781 | } | |
782 | if (nla_parse_nested(bss, NL80211_BSS_MAX, | |
783 | tb[NL80211_ATTR_BSS], | |
784 | bss_policy)) { | |
785 | fprintf(stderr, "failed to parse nested attributes!\n"); | |
786 | return NL_SKIP; | |
787 | } | |
788 | ||
789 | if (!bss[NL80211_BSS_BSSID]) | |
790 | return NL_SKIP; | |
791 | ||
792 | mac_addr_n2a(mac_addr, nla_data(bss[NL80211_BSS_BSSID])); | |
793 | if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev); | |
794 | printf("BSS %s (on %s)", mac_addr, dev); | |
795 | ||
796 | if (bss[NL80211_BSS_STATUS]) { | |
797 | switch (nla_get_u32(bss[NL80211_BSS_STATUS])) { | |
798 | case NL80211_BSS_STATUS_AUTHENTICATED: | |
799 | printf(" -- authenticated"); | |
800 | break; | |
801 | case NL80211_BSS_STATUS_ASSOCIATED: | |
802 | printf(" -- associated"); | |
803 | break; | |
804 | case NL80211_BSS_STATUS_IBSS_JOINED: | |
805 | printf(" -- joined"); | |
806 | break; | |
807 | default: | |
808 | printf(" -- unknown status: %d", | |
809 | nla_get_u32(bss[NL80211_BSS_STATUS])); | |
810 | break; | |
811 | } | |
812 | } | |
813 | printf("\n"); | |
814 | ||
815 | if (bss[NL80211_BSS_TSF]) { | |
816 | unsigned long long tsf; | |
817 | tsf = (unsigned long long)nla_get_u64(bss[NL80211_BSS_TSF]); | |
818 | printf("\tTSF: %llu usec (%llud, %.2lld:%.2llu:%.2llu)\n", | |
819 | tsf, tsf/1000/1000/60/60/24, (tsf/1000/1000/60/60) % 24, | |
820 | (tsf/1000/1000/60) % 60, (tsf/1000/1000) % 60); | |
821 | } | |
822 | if (bss[NL80211_BSS_FREQUENCY]) | |
823 | printf("\tfreq: %d\n", | |
824 | nla_get_u32(bss[NL80211_BSS_FREQUENCY])); | |
825 | if (bss[NL80211_BSS_BEACON_INTERVAL]) | |
826 | printf("\tbeacon interval: %d\n", | |
827 | nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL])); | |
828 | if (bss[NL80211_BSS_CAPABILITY]) { | |
829 | __u16 capa = nla_get_u16(bss[NL80211_BSS_CAPABILITY]); | |
830 | printf("\tcapability:"); | |
831 | if (capa & WLAN_CAPABILITY_ESS) | |
832 | printf(" ESS"); | |
833 | if (capa & WLAN_CAPABILITY_IBSS) | |
834 | printf(" IBSS"); | |
835 | if (capa & WLAN_CAPABILITY_PRIVACY) | |
836 | printf(" Privacy"); | |
837 | if (capa & WLAN_CAPABILITY_SHORT_PREAMBLE) | |
838 | printf(" ShortPreamble"); | |
839 | if (capa & WLAN_CAPABILITY_PBCC) | |
840 | printf(" PBCC"); | |
841 | if (capa & WLAN_CAPABILITY_CHANNEL_AGILITY) | |
842 | printf(" ChannelAgility"); | |
843 | if (capa & WLAN_CAPABILITY_SPECTRUM_MGMT) | |
844 | printf(" SpectrumMgmt"); | |
845 | if (capa & WLAN_CAPABILITY_QOS) | |
846 | printf(" QoS"); | |
847 | if (capa & WLAN_CAPABILITY_SHORT_SLOT_TIME) | |
848 | printf(" ShortSlotTime"); | |
849 | if (capa & WLAN_CAPABILITY_APSD) | |
850 | printf(" APSD"); | |
851 | if (capa & WLAN_CAPABILITY_DSSS_OFDM) | |
852 | printf(" DSSS-OFDM"); | |
853 | printf(" (0x%.4x)\n", capa); | |
854 | } | |
855 | if (bss[NL80211_BSS_SIGNAL_MBM]) { | |
856 | int s = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]); | |
857 | printf("\tsignal: %d.%.2d dBm\n", s/100, s%100); | |
858 | } | |
859 | if (bss[NL80211_BSS_SIGNAL_UNSPEC]) { | |
860 | unsigned char s = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]); | |
861 | printf("\tsignal: %d/100\n", s); | |
862 | } | |
863 | if (bss[NL80211_BSS_SEEN_MS_AGO]) { | |
864 | int age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]); | |
865 | printf("\tlast seen: %d ms ago\n", age); | |
866 | } | |
867 | if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) | |
868 | print_ies(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]), | |
869 | nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]), | |
870 | params->unknown, params->type); | |
871 | ||
872 | return NL_SKIP; | |
873 | } | |
874 | ||
875 | static struct scan_params scan_params; | |
876 | ||
877 | static int handle_scan_dump(struct nl80211_state *state, | |
878 | struct nl_cb *cb, | |
879 | struct nl_msg *msg, | |
880 | int argc, char **argv) | |
881 | { | |
882 | if (argc > 1) | |
883 | return 1; | |
884 | ||
885 | scan_params.unknown = false; | |
886 | if (argc == 1 && !strcmp(argv[0], "-u")) | |
887 | scan_params.unknown = true; | |
888 | ||
889 | scan_params.type = PRINT_SCAN; | |
890 | ||
891 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_bss_handler, | |
892 | &scan_params); | |
893 | return 0; | |
894 | } | |
895 | ||
896 | static int handle_scan_combined(struct nl80211_state *state, | |
897 | struct nl_cb *cb, | |
898 | struct nl_msg *msg, | |
899 | int argc, char **argv) | |
900 | { | |
901 | char **trig_argv; | |
902 | static char *dump_argv[] = { | |
903 | NULL, | |
904 | "scan", | |
905 | "dump", | |
906 | NULL, | |
907 | }; | |
908 | static const __u32 cmds[] = { | |
909 | NL80211_CMD_NEW_SCAN_RESULTS, | |
910 | NL80211_CMD_SCAN_ABORTED, | |
911 | }; | |
912 | int trig_argc, dump_argc, err; | |
913 | ||
914 | if (argc >= 3 && !strcmp(argv[2], "-u")) { | |
915 | dump_argc = 4; | |
916 | dump_argv[3] = "-u"; | |
917 | } else | |
918 | dump_argc = 3; | |
919 | ||
920 | trig_argc = 3 + (argc - 2) + (3 - dump_argc); | |
921 | trig_argv = calloc(trig_argc, sizeof(*trig_argv)); | |
922 | if (!trig_argv) | |
923 | return -ENOMEM; | |
924 | trig_argv[0] = argv[0]; | |
925 | trig_argv[1] = "scan"; | |
926 | trig_argv[2] = "trigger"; | |
927 | int i; | |
928 | for (i = 0; i < argc - 2 - (dump_argc - 3); i++) | |
929 | trig_argv[i + 3] = argv[i + 2 + (dump_argc - 3)]; | |
930 | err = handle_cmd(state, II_NETDEV, trig_argc, trig_argv); | |
931 | free(trig_argv); | |
932 | if (err) | |
933 | return err; | |
934 | ||
935 | /* | |
936 | * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION | |
937 | * | |
938 | * This code has a bug, which requires creating a separate | |
939 | * nl80211 socket to fix: | |
940 | * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or | |
941 | * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel | |
942 | * before (!) we listen to it, because we only start listening | |
943 | * after we send our scan request. | |
944 | * | |
945 | * Doing it the other way around has a race condition as well, | |
946 | * if you first open the events socket you may get a notification | |
947 | * for a previous scan. | |
948 | * | |
949 | * The only proper way to fix this would be to listen to events | |
950 | * before sending the command, and for the kernel to send the | |
951 | * scan request along with the event, so that you can match up | |
952 | * whether the scan you requested was finished or aborted (this | |
953 | * may result in processing a scan that another application | |
954 | * requested, but that doesn't seem to be a problem). | |
955 | * | |
956 | * Alas, the kernel doesn't do that (yet). | |
957 | */ | |
958 | ||
959 | if (listen_events(state, ARRAY_SIZE(cmds), cmds) == | |
960 | NL80211_CMD_SCAN_ABORTED) { | |
961 | printf("scan aborted!\n"); | |
962 | return 0; | |
963 | } | |
964 | ||
965 | dump_argv[0] = argv[0]; | |
966 | return handle_cmd(state, II_NETDEV, dump_argc, dump_argv); | |
967 | } | |
968 | TOPLEVEL(scan, "[-u] [freq <freq>*] [ssid <ssid>*|passive]", 0, 0, | |
969 | CIB_NETDEV, handle_scan_combined, | |
970 | "Scan on the given frequencies and probe for the given SSIDs\n" | |
971 | "(or wildcard if not given) unless passive scanning is requested.\n" | |
972 | "If -u is specified print unknown data in the scan results."); | |
973 | COMMAND(scan, dump, "[-u]", | |
974 | NL80211_CMD_GET_SCAN, NLM_F_DUMP, CIB_NETDEV, handle_scan_dump, | |
975 | "Dump the current scan results. If -u is specified, print unknown\n" | |
976 | "data in scan results."); | |
977 | COMMAND(scan, trigger, "[freq <freq>*] [ssid <ssid>*|passive]", | |
978 | NL80211_CMD_TRIGGER_SCAN, 0, CIB_NETDEV, handle_scan, | |
979 | "Trigger a scan on the given frequencies with probing for the given\n" | |
980 | "SSIDs (or wildcard if not given) unless passive scanning is requested."); |