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