]>
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; |
764fe753 JB |
36 | }; |
37 | ||
7c37a24d JB |
38 | static int handle_scan(struct nl80211_state *state, |
39 | struct nl_cb *cb, | |
3563f4c5 JB |
40 | struct nl_msg *msg, |
41 | int argc, char **argv) | |
42 | { | |
559a1713 JB |
43 | struct nl_msg *ssids = NULL, *freqs = NULL; |
44 | char *eptr; | |
3563f4c5 | 45 | int err = -ENOBUFS; |
559a1713 JB |
46 | int i; |
47 | enum { | |
48 | NONE, | |
49 | FREQ, | |
50 | SSID, | |
51 | DONE, | |
52 | } parse = NONE; | |
53 | int freq; | |
54 | bool passive = false, have_ssids = false, have_freqs = false; | |
3563f4c5 JB |
55 | |
56 | ssids = nlmsg_alloc(); | |
57 | if (!ssids) | |
58 | return -ENOMEM; | |
559a1713 JB |
59 | |
60 | freqs = nlmsg_alloc(); | |
61 | if (!freqs) { | |
62 | nlmsg_free(ssids); | |
63 | return -ENOMEM; | |
64 | } | |
65 | ||
66 | for (i = 0; i < argc; i++) { | |
1ddf11eb | 67 | if (parse == NONE && strcmp(argv[i], "freq") == 0) { |
559a1713 JB |
68 | parse = FREQ; |
69 | have_freqs = true; | |
70 | continue; | |
1ddf11eb | 71 | } else if (parse < SSID && strcmp(argv[i], "ssid") == 0) { |
559a1713 JB |
72 | parse = SSID; |
73 | have_ssids = true; | |
74 | continue; | |
1ddf11eb | 75 | } else if (parse < SSID && strcmp(argv[i], "passive") == 0) { |
559a1713 JB |
76 | parse = DONE; |
77 | passive = true; | |
78 | continue; | |
79 | } | |
80 | ||
81 | switch (parse) { | |
82 | case NONE: | |
83 | case DONE: | |
84 | return 1; | |
85 | case FREQ: | |
86 | freq = strtoul(argv[i], &eptr, 10); | |
87 | if (eptr != argv[i] + strlen(argv[i])) | |
88 | return 1; | |
89 | NLA_PUT_U32(freqs, i, freq); | |
90 | break; | |
91 | case SSID: | |
92 | NLA_PUT(ssids, i, strlen(argv[i]), argv[i]); | |
93 | break; | |
94 | } | |
95 | } | |
96 | ||
97 | if (!have_ssids) | |
98 | NLA_PUT(ssids, 1, 0, ""); | |
99 | if (!passive) | |
100 | nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids); | |
101 | ||
102 | if (have_freqs) | |
103 | nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs); | |
3563f4c5 JB |
104 | |
105 | err = 0; | |
106 | nla_put_failure: | |
107 | nlmsg_free(ssids); | |
559a1713 | 108 | nlmsg_free(freqs); |
3563f4c5 JB |
109 | return err; |
110 | } | |
3563f4c5 | 111 | |
857d966e MH |
112 | static void tab_on_first(bool *first) |
113 | { | |
114 | if (!*first) | |
115 | printf("\t"); | |
116 | else | |
117 | *first = false; | |
118 | } | |
119 | ||
83b4934c | 120 | static void print_ssid(const uint8_t type, uint8_t len, const uint8_t *data) |
3563f4c5 | 121 | { |
83b4934c | 122 | printf(" "); |
748f8489 | 123 | print_ssid_escaped(len, data); |
3563f4c5 JB |
124 | printf("\n"); |
125 | } | |
126 | ||
83b4934c | 127 | static void print_supprates(const uint8_t type, uint8_t len, const uint8_t *data) |
3563f4c5 JB |
128 | { |
129 | int i; | |
130 | ||
83b4934c | 131 | printf(" "); |
3563f4c5 | 132 | |
83b4934c | 133 | for (i = 0; i < len; i++) { |
3563f4c5 JB |
134 | int r = data[i] & 0x7f; |
135 | printf("%d.%d%s ", r/2, 5*(r&1), data[i] & 0x80 ? "*":""); | |
136 | } | |
137 | printf("\n"); | |
138 | } | |
139 | ||
83b4934c | 140 | static void print_ds(const uint8_t type, uint8_t len, const uint8_t *data) |
3563f4c5 | 141 | { |
83b4934c | 142 | printf(" channel %d\n", data[0]); |
3563f4c5 JB |
143 | } |
144 | ||
83b4934c | 145 | static void print_country(const uint8_t type, uint8_t len, const uint8_t *data) |
b7e8fa37 MH |
146 | { |
147 | int i; | |
148 | ||
83b4934c | 149 | printf(" %.*s", 2, data); |
b7e8fa37 MH |
150 | switch (data[2]) { |
151 | case 'I': | |
152 | printf(" (indoor)"); | |
153 | break; | |
154 | case 'O': | |
155 | printf(" (outdoor)"); | |
156 | break; | |
b6c0d634 JB |
157 | case ' ': |
158 | printf(" (in/outdoor)"); | |
159 | break; | |
160 | default: | |
161 | printf(" (invalid environment)"); | |
162 | break; | |
b7e8fa37 MH |
163 | } |
164 | printf(", data:"); | |
165 | for(i=0; i<len-3; i++) | |
166 | printf(" %.02x", data[i + 3]); | |
167 | printf("\n"); | |
168 | } | |
169 | ||
d1563a1b MH |
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 | ||
83b4934c | 175 | static void print_erp(const uint8_t type, uint8_t len, const uint8_t *data) |
fc4d1484 MH |
176 | { |
177 | if (data[0] == 0x00) | |
83b4934c | 178 | printf(" <no flags>"); |
fc4d1484 MH |
179 | if (data[0] & 0x01) |
180 | printf(" NonERP_Present"); | |
181 | if (data[0] & 0x02) | |
182 | printf(" Use_Protection"); | |
183 | if (data[0] & 0x04) | |
184 | printf(" Barker_Preamble_Mode"); | |
185 | printf("\n"); | |
186 | } | |
187 | ||
83b4934c | 188 | static void print_cipher(const uint8_t *data) |
857d966e MH |
189 | { |
190 | if (memcmp(data, wifi_oui, 3) == 0) { | |
191 | switch (data[3]) { | |
510e0e2f | 192 | case 0: |
857d966e MH |
193 | printf("Use group cipher suite"); |
194 | break; | |
510e0e2f | 195 | case 1: |
857d966e MH |
196 | printf("WEP-40"); |
197 | break; | |
510e0e2f | 198 | case 2: |
857d966e MH |
199 | printf("TKIP"); |
200 | break; | |
510e0e2f | 201 | case 4: |
857d966e MH |
202 | printf("CCMP"); |
203 | break; | |
510e0e2f | 204 | case 5: |
857d966e MH |
205 | printf("WEP-104"); |
206 | break; | |
207 | default: | |
332769c6 | 208 | printf("%.02x-%.02x-%.02x:%d", |
5594fd23 | 209 | data[0], data[1] ,data[2], data[3]); |
857d966e MH |
210 | break; |
211 | } | |
212 | } else if (memcmp(data, ieee80211_oui, 3) == 0) { | |
213 | switch (data[3]) { | |
510e0e2f | 214 | case 0: |
857d966e MH |
215 | printf("Use group cipher suite"); |
216 | break; | |
510e0e2f | 217 | case 1: |
857d966e MH |
218 | printf("WEP-40"); |
219 | break; | |
510e0e2f | 220 | case 2: |
857d966e MH |
221 | printf("TKIP"); |
222 | break; | |
510e0e2f | 223 | case 4: |
857d966e MH |
224 | printf("CCMP"); |
225 | break; | |
510e0e2f | 226 | case 5: |
857d966e MH |
227 | printf("WEP-104"); |
228 | break; | |
510e0e2f | 229 | case 6: |
857d966e MH |
230 | printf("AES-128-CMAC"); |
231 | break; | |
232 | default: | |
332769c6 | 233 | printf("%.02x-%.02x-%.02x:%d", |
5594fd23 | 234 | data[0], data[1] ,data[2], data[3]); |
857d966e MH |
235 | break; |
236 | } | |
237 | } else | |
332769c6 | 238 | printf("%.02x-%.02x-%.02x:%d", |
5594fd23 | 239 | data[0], data[1] ,data[2], data[3]); |
857d966e MH |
240 | } |
241 | ||
83b4934c | 242 | static void print_auth(const uint8_t *data) |
857d966e MH |
243 | { |
244 | if (memcmp(data, wifi_oui, 3) == 0) { | |
245 | switch (data[3]) { | |
510e0e2f | 246 | case 1: |
857d966e MH |
247 | printf("IEEE 802.1X"); |
248 | break; | |
510e0e2f | 249 | case 2: |
857d966e MH |
250 | printf("PSK"); |
251 | break; | |
252 | default: | |
332769c6 | 253 | printf("%.02x-%.02x-%.02x:%d", |
5594fd23 | 254 | data[0], data[1] ,data[2], data[3]); |
857d966e MH |
255 | break; |
256 | } | |
257 | } else if (memcmp(data, ieee80211_oui, 3) == 0) { | |
258 | switch (data[3]) { | |
510e0e2f | 259 | case 1: |
857d966e MH |
260 | printf("IEEE 802.1X"); |
261 | break; | |
510e0e2f | 262 | case 2: |
857d966e MH |
263 | printf("PSK"); |
264 | break; | |
0fe1c415 JB |
265 | case 3: |
266 | printf("FT/IEEE 802.1X"); | |
267 | break; | |
268 | case 4: | |
269 | printf("FT/PSK"); | |
270 | break; | |
271 | case 5: | |
272 | printf("IEEE 802.1X/SHA-256"); | |
273 | break; | |
274 | case 6: | |
275 | printf("PSK/SHA-256"); | |
276 | break; | |
857d966e | 277 | default: |
332769c6 | 278 | printf("%.02x-%.02x-%.02x:%d", |
5594fd23 | 279 | data[0], data[1] ,data[2], data[3]); |
857d966e MH |
280 | break; |
281 | } | |
282 | } else | |
332769c6 | 283 | printf("%.02x-%.02x-%.02x:%d", |
5594fd23 | 284 | data[0], data[1] ,data[2], data[3]); |
857d966e MH |
285 | } |
286 | ||
83b4934c JB |
287 | static void print_rsn_ie(const char *defcipher, const char *defauth, |
288 | uint8_t len, const uint8_t *data) | |
857d966e MH |
289 | { |
290 | bool first = true; | |
291 | __u16 version, count, capa; | |
292 | int i; | |
293 | ||
857d966e MH |
294 | version = data[0] + (data[1] << 8); |
295 | tab_on_first(&first); | |
296 | printf("\t * Version: %d\n", version); | |
297 | ||
298 | data += 2; | |
299 | len -= 2; | |
300 | ||
301 | if (len < 4) { | |
302 | tab_on_first(&first); | |
303 | printf("\t * Group cipher: %s\n", defcipher); | |
304 | printf("\t * Pairwise ciphers: %s\n", defcipher); | |
305 | return; | |
306 | } | |
307 | ||
308 | tab_on_first(&first); | |
309 | printf("\t * Group cipher: "); | |
310 | print_cipher(data); | |
311 | printf("\n"); | |
312 | ||
313 | data += 4; | |
314 | len -= 4; | |
315 | ||
316 | if (len < 2) { | |
317 | tab_on_first(&first); | |
318 | printf("\t * Pairwise ciphers: %s\n", defcipher); | |
319 | return; | |
320 | } | |
321 | ||
322 | count = data[0] | (data[1] << 8); | |
31d477fb MH |
323 | if (2 + (count * 4) > len) |
324 | goto invalid; | |
325 | ||
857d966e MH |
326 | tab_on_first(&first); |
327 | printf("\t * Pairwise ciphers:"); | |
31d477fb | 328 | for (i = 0; i < count; i++) { |
857d966e MH |
329 | printf(" "); |
330 | print_cipher(data + 2 + (i * 4)); | |
331 | } | |
332 | printf("\n"); | |
333 | ||
334 | data += 2 + (count * 4); | |
335 | len -= 2 + (count * 4); | |
336 | ||
337 | if (len < 2) { | |
338 | tab_on_first(&first); | |
339 | printf("\t * Authentication suites: %s\n", defauth); | |
340 | return; | |
341 | } | |
342 | ||
343 | count = data[0] | (data[1] << 8); | |
31d477fb MH |
344 | if (2 + (count * 4) > len) |
345 | goto invalid; | |
346 | ||
857d966e MH |
347 | tab_on_first(&first); |
348 | printf("\t * Authentication suites:"); | |
83b4934c | 349 | for (i = 0; i < count; i++) { |
857d966e MH |
350 | printf(" "); |
351 | print_auth(data + 2 + (i * 4)); | |
352 | } | |
353 | printf("\n"); | |
354 | ||
355 | data += 2 + (count * 4); | |
356 | len -= 2 + (count * 4); | |
357 | ||
6a4f24e8 JB |
358 | if (len >= 2) { |
359 | capa = data[0] | (data[1] << 8); | |
360 | tab_on_first(&first); | |
cadbe89a JB |
361 | printf("\t * Capabilities:"); |
362 | if (capa & 0x0001) | |
363 | printf(" PreAuth"); | |
364 | if (capa & 0x0002) | |
365 | printf(" NoPairwise"); | |
366 | switch ((capa & 0x000c) >> 2) { | |
367 | case 0: | |
368 | break; | |
369 | case 1: | |
370 | printf(" 2-PTKSA-RC"); | |
371 | break; | |
372 | case 2: | |
373 | printf(" 4-PTKSA-RC"); | |
374 | break; | |
375 | case 3: | |
376 | printf(" 16-PTKSA-RC"); | |
377 | break; | |
378 | } | |
379 | switch ((capa & 0x0030) >> 4) { | |
380 | case 0: | |
381 | break; | |
382 | case 1: | |
383 | printf(" 2-GTKSA-RC"); | |
384 | break; | |
385 | case 2: | |
386 | printf(" 4-GTKSA-RC"); | |
387 | break; | |
388 | case 3: | |
389 | printf(" 16-GTKSA-RC"); | |
390 | break; | |
391 | } | |
392 | if (capa & 0x0040) | |
393 | printf(" MFP-required"); | |
394 | if (capa & 0x0080) | |
395 | printf(" MFP-capable"); | |
396 | if (capa & 0x0200) | |
397 | printf(" Peerkey-enabled"); | |
398 | if (capa & 0x0400) | |
399 | printf(" SPP-AMSDU-capable"); | |
400 | if (capa & 0x0800) | |
401 | printf(" SPP-AMSDU-required"); | |
402 | printf(" (0x%.4x)\n", capa); | |
6a4f24e8 JB |
403 | data += 2; |
404 | len -= 2; | |
405 | } | |
31d477fb | 406 | |
cadbe89a | 407 | invalid: |
31d477fb MH |
408 | if (len != 0) { |
409 | printf("\t\t * bogus tail data (%d):", len); | |
410 | while (len) { | |
411 | printf(" %.2x", *data); | |
412 | data++; | |
413 | len--; | |
414 | } | |
415 | printf("\n"); | |
416 | } | |
857d966e MH |
417 | } |
418 | ||
83b4934c | 419 | static void print_rsn(const uint8_t type, uint8_t len, const uint8_t *data) |
857d966e | 420 | { |
83b4934c | 421 | print_rsn_ie("CCMP", "IEEE 802.1X", len, data); |
857d966e MH |
422 | } |
423 | ||
0c445c24 LR |
424 | static void print_ht_capa(const uint8_t type, uint8_t len, const uint8_t *data) |
425 | { | |
0c445c24 LR |
426 | if (len != 26) { |
427 | printf("\n\t\tHT Capability IE len != expected 26 bytes, skipping parse\n"); | |
428 | return; | |
429 | } | |
357c1a5d | 430 | printf("\n"); |
7ddfb679 JB |
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); | |
0c445c24 LR |
435 | } |
436 | ||
83b4934c | 437 | static void print_capabilities(const uint8_t type, uint8_t len, const uint8_t *data) |
9b880b00 | 438 | { |
31d2d259 JB |
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 | } | |
9b880b00 | 483 | |
9b880b00 MH |
484 | printf("\n"); |
485 | } | |
486 | ||
83b4934c JB |
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; | |
febeb0c0 | 491 | uint8_t flags; |
764fe753 JB |
492 | }; |
493 | ||
83b4934c JB |
494 | static void print_ie(const struct ie_print *p, const uint8_t type, |
495 | uint8_t len, const uint8_t *data) | |
4673a894 | 496 | { |
83b4934c JB |
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, \ | |
4673a894 JB |
524 | } |
525 | ||
83b4934c | 526 | static const struct ie_print ieprinters[] = { |
febeb0c0 JB |
527 | [0] = { "SSID", print_ssid, 0, 32, BIT(PRINT_SCAN) | BIT(PRINT_LINK), }, |
528 | [1] = { "Supported rates", print_supprates, 0, 255, BIT(PRINT_SCAN), }, | |
014d581a | 529 | [3] = { "DS Parameter set", print_ds, 1, 1, BIT(PRINT_SCAN), }, |
83b4934c | 530 | [5] = PRINT_IGN, |
febeb0c0 JB |
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), }, | |
0c445c24 | 534 | [45] = { "HT capabilities", print_ht_capa, 1, 255, BIT(PRINT_SCAN), }, |
febeb0c0 JB |
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), }, | |
83b4934c JB |
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 | ||
1cab57eb JB |
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) { | |
cee4fe20 | 554 | printf("Parameter: not version 1: "); |
1cab57eb JB |
555 | return false; |
556 | } | |
557 | ||
89ea706f | 558 | printf("\t * Parameter version 1"); |
1cab57eb JB |
559 | |
560 | data++; | |
561 | ||
562 | if (data[0] & 0x80) | |
89ea706f | 563 | printf("\n\t\t * u-APSD"); |
1cab57eb JB |
564 | |
565 | data += 2; | |
566 | ||
567 | for (i = 0; i < 4; i++) { | |
89ea706f | 568 | printf("\n\t\t * %s:", aci_tbl[(data[0] >> 5) & 3]); |
1cab57eb JB |
569 | if (data[4] & 0x10) |
570 | printf(" acm"); | |
571 | printf(" CW %d-%d", (1 << (data[1] & 0xf)) - 1, | |
572 | (1 << (data[1] >> 4)) - 1); | |
a2a4c265 | 573 | printf(", AIFSN %d", data[0] & 0xf); |
1cab57eb | 574 | if (data[2] | data[3]) |
cee4fe20 | 575 | printf(", TXOP %d usec", (data[2] + (data[3] << 8)) * 32); |
1cab57eb JB |
576 | data += 4; |
577 | } | |
578 | ||
579 | printf("\n"); | |
580 | return true; | |
581 | ||
582 | invalid: | |
583 | printf("invalid: "); | |
584 | return false; | |
585 | } | |
586 | ||
83b4934c | 587 | static void print_wifi_wmm(const uint8_t type, uint8_t len, const uint8_t *data) |
6ff0c93a MH |
588 | { |
589 | int i; | |
590 | ||
6ff0c93a MH |
591 | switch (data[0]) { |
592 | case 0x00: | |
83b4934c | 593 | printf(" information:"); |
6ff0c93a MH |
594 | break; |
595 | case 0x01: | |
1cab57eb JB |
596 | if (print_wifi_wmm_param(data + 1, len - 1)) |
597 | return; | |
6ff0c93a MH |
598 | break; |
599 | default: | |
83b4934c | 600 | printf(" type %d:", data[0]); |
6ff0c93a MH |
601 | break; |
602 | } | |
603 | ||
1cab57eb JB |
604 | for(i = 1; i < len; i++) |
605 | printf(" %.02x", data[i]); | |
6ff0c93a MH |
606 | printf("\n"); |
607 | } | |
608 | ||
83b4934c | 609 | static void print_wifi_wps(const uint8_t type, uint8_t len, const uint8_t *data) |
4673a894 JB |
610 | { |
611 | bool first = true; | |
612 | __u16 subtype, sublen; | |
613 | ||
4673a894 JB |
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); | |
dffc6750 | 623 | printf("\t * Version: %d.%d\n", data[4] >> 4, data[4] & 0xF); |
4673a894 JB |
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; | |
7ee5a865 | 637 | case 0x1057: { |
fe31a22e | 638 | __u8 val = data[4]; |
7ee5a865 | 639 | tab_on_first(&first); |
fe31a22e | 640 | printf("\t * AP setup locked: 0x%.2x\n", val); |
7ee5a865 JB |
641 | break; |
642 | } | |
4673a894 JB |
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 | ||
83b4934c | 687 | static const struct ie_print wifiprinters[] = { |
febeb0c0 JB |
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), }, | |
4673a894 JB |
691 | }; |
692 | ||
764fe753 | 693 | static void print_vendor(unsigned char len, unsigned char *data, |
febeb0c0 | 694 | bool unknown, enum print_ie_type ptype) |
3563f4c5 JB |
695 | { |
696 | int i; | |
697 | ||
fbf80af5 | 698 | if (len < 3) { |
4673a894 | 699 | printf("\tVendor specific: <too short> data:"); |
fbf80af5 JB |
700 | for(i = 0; i < len; i++) |
701 | printf(" %.02x", data[i]); | |
702 | printf("\n"); | |
703 | return; | |
704 | } | |
705 | ||
857d966e | 706 | if (len >= 4 && memcmp(data, wifi_oui, 3) == 0) { |
febeb0c0 JB |
707 | if (data[3] < ARRAY_SIZE(wifiprinters) && |
708 | wifiprinters[data[3]].name && | |
709 | wifiprinters[data[3]].flags & BIT(ptype)) { | |
83b4934c JB |
710 | print_ie(&wifiprinters[data[3]], data[3], len - 4, data + 4); |
711 | return; | |
712 | } | |
febeb0c0 | 713 | if (!unknown) |
4673a894 | 714 | return; |
857d966e | 715 | printf("\tWiFi OUI %#.2x, data:", data[3]); |
4673a894 JB |
716 | for(i = 0; i < len - 4; i++) |
717 | printf(" %.02x", data[i + 4]); | |
718 | printf("\n"); | |
719 | return; | |
720 | } | |
721 | ||
febeb0c0 | 722 | if (!unknown) |
764fe753 JB |
723 | return; |
724 | ||
fbf80af5 | 725 | printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:", |
3563f4c5 | 726 | data[0], data[1], data[2]); |
fbf80af5 JB |
727 | for (i = 3; i < len; i++) |
728 | printf(" %.2x", data[i]); | |
3563f4c5 JB |
729 | printf("\n"); |
730 | } | |
731 | ||
febeb0c0 JB |
732 | void print_ies(unsigned char *ie, int ielen, bool unknown, |
733 | enum print_ie_type ptype) | |
3563f4c5 JB |
734 | { |
735 | while (ielen >= 2 && ielen >= ie[1]) { | |
febeb0c0 JB |
736 | if (ie[0] < ARRAY_SIZE(ieprinters) && |
737 | ieprinters[ie[0]].name && | |
738 | ieprinters[ie[0]].flags & BIT(ptype)) { | |
83b4934c | 739 | print_ie(&ieprinters[ie[0]], ie[0], ie[1], ie + 2); |
764fe753 | 740 | } else if (ie[0] == 221 /* vendor */) { |
febeb0c0 JB |
741 | print_vendor(ie[1], ie + 2, unknown, ptype); |
742 | } else if (unknown) { | |
3563f4c5 JB |
743 | int i; |
744 | ||
8086b700 | 745 | printf("\tUnknown IE (%d):", ie[0]); |
3563f4c5 | 746 | for (i=0; i<ie[1]; i++) |
8086b700 | 747 | printf(" %.2x", ie[2+i]); |
3563f4c5 JB |
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] = { }, | |
f2e17e1f JB |
768 | [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 }, |
769 | [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 }, | |
a56117a6 | 770 | [NL80211_BSS_STATUS] = { .type = NLA_U32 }, |
c04a78df | 771 | [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 }, |
3563f4c5 | 772 | }; |
febeb0c0 | 773 | struct scan_params *params = arg; |
3563f4c5 JB |
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]) { | |
5fe70c0e | 779 | fprintf(stderr, "bss info missing!\n"); |
3563f4c5 JB |
780 | return NL_SKIP; |
781 | } | |
782 | if (nla_parse_nested(bss, NL80211_BSS_MAX, | |
783 | tb[NL80211_ATTR_BSS], | |
784 | bss_policy)) { | |
5fe70c0e | 785 | fprintf(stderr, "failed to parse nested attributes!\n"); |
3563f4c5 JB |
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); | |
a56117a6 JB |
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"); | |
3563f4c5 | 814 | |
e7109a8a JB |
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 | } | |
3563f4c5 JB |
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])); | |
92a04ecd MH |
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 | } | |
f2e17e1f JB |
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 | } | |
c04a78df HS |
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 | } | |
3563f4c5 JB |
867 | if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) |
868 | print_ies(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]), | |
764fe753 | 869 | nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]), |
febeb0c0 | 870 | params->unknown, params->type); |
3563f4c5 JB |
871 | |
872 | return NL_SKIP; | |
873 | } | |
874 | ||
764fe753 | 875 | static struct scan_params scan_params; |
3563f4c5 | 876 | |
7c37a24d JB |
877 | static int handle_scan_dump(struct nl80211_state *state, |
878 | struct nl_cb *cb, | |
3563f4c5 JB |
879 | struct nl_msg *msg, |
880 | int argc, char **argv) | |
881 | { | |
764fe753 JB |
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 | ||
febeb0c0 JB |
889 | scan_params.type = PRINT_SCAN; |
890 | ||
764fe753 JB |
891 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_bss_handler, |
892 | &scan_params); | |
3563f4c5 JB |
893 | return 0; |
894 | } | |
a5fe4ef2 JB |
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 | { | |
559a1713 | 901 | char **trig_argv; |
a5fe4ef2 JB |
902 | static char *dump_argv[] = { |
903 | NULL, | |
904 | "scan", | |
905 | "dump", | |
92649eab | 906 | NULL, |
a5fe4ef2 JB |
907 | }; |
908 | static const __u32 cmds[] = { | |
909 | NL80211_CMD_NEW_SCAN_RESULTS, | |
910 | NL80211_CMD_SCAN_ABORTED, | |
911 | }; | |
559a1713 | 912 | int trig_argc, dump_argc, err; |
a5fe4ef2 | 913 | |
559a1713 JB |
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; | |
a5fe4ef2 | 924 | trig_argv[0] = argv[0]; |
559a1713 JB |
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); | |
a5fe4ef2 JB |
932 | if (err) |
933 | return err; | |
934 | ||
61725dbe JB |
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 | ||
a5fe4ef2 JB |
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]; | |
92649eab | 966 | return handle_cmd(state, II_NETDEV, dump_argc, dump_argv); |
a5fe4ef2 | 967 | } |
6ca98d28 JB |
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."); | |
4698bfc2 JB |
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."); |