]>
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 | ||
83b4934c | 424 | static void print_capabilities(const uint8_t type, uint8_t len, const uint8_t *data) |
9b880b00 | 425 | { |
31d2d259 JB |
426 | int i, base, bit; |
427 | bool first = true; | |
428 | ||
429 | ||
430 | for (i = 0; i < len; i++) { | |
431 | base = i * 8; | |
432 | ||
433 | for (bit = 0; bit < 8; bit++) { | |
434 | if (!(data[i] & (1 << bit))) | |
435 | continue; | |
436 | ||
437 | if (!first) | |
438 | printf(","); | |
439 | else | |
440 | first = false; | |
441 | ||
442 | switch (bit + base) { | |
443 | case 0: | |
444 | printf(" HT Information Exchange Supported"); | |
445 | break; | |
446 | case 1: | |
447 | printf(" On-demand Beacon"); | |
448 | break; | |
449 | case 2: | |
450 | printf(" Extended Channel Switching"); | |
451 | break; | |
452 | case 3: | |
453 | printf(" Wave Indication"); | |
454 | break; | |
455 | case 4: | |
456 | printf(" PSMP Capability"); | |
457 | break; | |
458 | case 5: | |
459 | printf(" Service Interval Granularity"); | |
460 | break; | |
461 | case 6: | |
462 | printf(" S-PSMP Capability"); | |
463 | break; | |
464 | default: | |
465 | printf(" %d", bit); | |
466 | break; | |
467 | } | |
468 | } | |
469 | } | |
9b880b00 | 470 | |
9b880b00 MH |
471 | printf("\n"); |
472 | } | |
473 | ||
83b4934c JB |
474 | struct ie_print { |
475 | const char *name; | |
476 | void (*print)(const uint8_t type, uint8_t len, const uint8_t *data); | |
477 | uint8_t minlen, maxlen; | |
febeb0c0 | 478 | uint8_t flags; |
764fe753 JB |
479 | }; |
480 | ||
83b4934c JB |
481 | static void print_ie(const struct ie_print *p, const uint8_t type, |
482 | uint8_t len, const uint8_t *data) | |
4673a894 | 483 | { |
83b4934c JB |
484 | int i; |
485 | ||
486 | if (!p->print) | |
487 | return; | |
488 | ||
489 | printf("\t%s:", p->name); | |
490 | if (len < p->minlen || len > p->maxlen) { | |
491 | if (len > 1) { | |
492 | printf(" <invalid: %d bytes:", len); | |
493 | for (i = 0; i < len; i++) | |
494 | printf(" %.02x", data[i]); | |
495 | printf(">\n"); | |
496 | } else if (len) | |
497 | printf(" <invalid: 1 byte: %.02x>\n", data[0]); | |
498 | else | |
499 | printf(" <invalid: no data>\n"); | |
500 | return; | |
501 | } | |
502 | ||
503 | p->print(type, len, data); | |
504 | } | |
505 | ||
506 | #define PRINT_IGN { \ | |
507 | .name = "IGNORE", \ | |
508 | .print = NULL, \ | |
509 | .minlen = 0, \ | |
510 | .maxlen = 255, \ | |
4673a894 JB |
511 | } |
512 | ||
83b4934c | 513 | static const struct ie_print ieprinters[] = { |
febeb0c0 JB |
514 | [0] = { "SSID", print_ssid, 0, 32, BIT(PRINT_SCAN) | BIT(PRINT_LINK), }, |
515 | [1] = { "Supported rates", print_supprates, 0, 255, BIT(PRINT_SCAN), }, | |
014d581a | 516 | [3] = { "DS Parameter set", print_ds, 1, 1, BIT(PRINT_SCAN), }, |
83b4934c | 517 | [5] = PRINT_IGN, |
febeb0c0 JB |
518 | [7] = { "Country", print_country, 3, 255, BIT(PRINT_SCAN), }, |
519 | [32] = { "Power constraint", print_powerconstraint, 1, 1, BIT(PRINT_SCAN), }, | |
520 | [42] = { "ERP", print_erp, 1, 255, BIT(PRINT_SCAN), }, | |
521 | [48] = { "RSN", print_rsn, 2, 255, BIT(PRINT_SCAN), }, | |
522 | [50] = { "Extended supported rates", print_supprates, 0, 255, BIT(PRINT_SCAN), }, | |
523 | [127] = { "Extended capabilities", print_capabilities, 0, 255, BIT(PRINT_SCAN), }, | |
83b4934c JB |
524 | }; |
525 | ||
526 | static void print_wifi_wpa(const uint8_t type, uint8_t len, const uint8_t *data) | |
527 | { | |
528 | print_rsn_ie("TKIP", "IEEE 802.1X", len, data); | |
529 | } | |
530 | ||
1cab57eb JB |
531 | static bool print_wifi_wmm_param(const uint8_t *data, uint8_t len) |
532 | { | |
533 | int i; | |
534 | static const char *aci_tbl[] = { "BE", "BK", "VI", "VO" }; | |
535 | ||
536 | if (len < 19) | |
537 | goto invalid; | |
538 | ||
539 | if (data[0] != 1) { | |
cee4fe20 | 540 | printf("Parameter: not version 1: "); |
1cab57eb JB |
541 | return false; |
542 | } | |
543 | ||
cee4fe20 | 544 | printf("\t* Parameter version 1"); |
1cab57eb JB |
545 | |
546 | data++; | |
547 | ||
548 | if (data[0] & 0x80) | |
549 | printf("\n\t\t* u-APSD"); | |
550 | ||
551 | data += 2; | |
552 | ||
553 | for (i = 0; i < 4; i++) { | |
eeac6099 | 554 | printf("\n\t\t* %s:", aci_tbl[(data[0] >> 5) & 3]); |
1cab57eb JB |
555 | if (data[4] & 0x10) |
556 | printf(" acm"); | |
557 | printf(" CW %d-%d", (1 << (data[1] & 0xf)) - 1, | |
558 | (1 << (data[1] >> 4)) - 1); | |
a2a4c265 | 559 | printf(", AIFSN %d", data[0] & 0xf); |
1cab57eb | 560 | if (data[2] | data[3]) |
cee4fe20 | 561 | printf(", TXOP %d usec", (data[2] + (data[3] << 8)) * 32); |
1cab57eb JB |
562 | data += 4; |
563 | } | |
564 | ||
565 | printf("\n"); | |
566 | return true; | |
567 | ||
568 | invalid: | |
569 | printf("invalid: "); | |
570 | return false; | |
571 | } | |
572 | ||
83b4934c | 573 | static void print_wifi_wmm(const uint8_t type, uint8_t len, const uint8_t *data) |
6ff0c93a MH |
574 | { |
575 | int i; | |
576 | ||
6ff0c93a MH |
577 | switch (data[0]) { |
578 | case 0x00: | |
83b4934c | 579 | printf(" information:"); |
6ff0c93a MH |
580 | break; |
581 | case 0x01: | |
1cab57eb JB |
582 | if (print_wifi_wmm_param(data + 1, len - 1)) |
583 | return; | |
6ff0c93a MH |
584 | break; |
585 | default: | |
83b4934c | 586 | printf(" type %d:", data[0]); |
6ff0c93a MH |
587 | break; |
588 | } | |
589 | ||
1cab57eb JB |
590 | for(i = 1; i < len; i++) |
591 | printf(" %.02x", data[i]); | |
6ff0c93a MH |
592 | printf("\n"); |
593 | } | |
594 | ||
83b4934c | 595 | static void print_wifi_wps(const uint8_t type, uint8_t len, const uint8_t *data) |
4673a894 JB |
596 | { |
597 | bool first = true; | |
598 | __u16 subtype, sublen; | |
599 | ||
4673a894 JB |
600 | while (len >= 4) { |
601 | subtype = (data[0] << 8) + data[1]; | |
602 | sublen = (data[2] << 8) + data[3]; | |
603 | if (sublen > len) | |
604 | break; | |
605 | ||
606 | switch (subtype) { | |
607 | case 0x104a: | |
608 | tab_on_first(&first); | |
dffc6750 | 609 | printf("\t * Version: %d.%d\n", data[4] >> 4, data[4] & 0xF); |
4673a894 JB |
610 | break; |
611 | case 0x1011: | |
612 | tab_on_first(&first); | |
613 | printf("\t * Device name: %.*s\n", sublen, data + 4); | |
614 | break; | |
615 | case 0x1021: | |
616 | tab_on_first(&first); | |
617 | printf("\t * Manufacturer: %.*s\n", sublen, data + 4); | |
618 | break; | |
619 | case 0x1023: | |
620 | tab_on_first(&first); | |
621 | printf("\t * Model: %.*s\n", sublen, data + 4); | |
622 | break; | |
7ee5a865 | 623 | case 0x1057: { |
fe31a22e | 624 | __u8 val = data[4]; |
7ee5a865 | 625 | tab_on_first(&first); |
fe31a22e | 626 | printf("\t * AP setup locked: 0x%.2x\n", val); |
7ee5a865 JB |
627 | break; |
628 | } | |
4673a894 JB |
629 | case 0x1008: { |
630 | __u16 meth = (data[4] << 8) + data[5]; | |
631 | bool comma = false; | |
632 | tab_on_first(&first); | |
633 | printf("\t * Config methods:"); | |
634 | #define T(bit, name) do { \ | |
635 | if (meth & (1<<bit)) { \ | |
636 | if (comma) \ | |
637 | printf(","); \ | |
638 | comma = true; \ | |
639 | printf(" " name); \ | |
640 | } } while (0) | |
641 | T(0, "USB"); | |
642 | T(1, "Ethernet"); | |
643 | T(2, "Label"); | |
644 | T(3, "Display"); | |
645 | T(4, "Ext. NFC"); | |
646 | T(5, "Int. NFC"); | |
647 | T(6, "NFC Intf."); | |
648 | T(7, "PBC"); | |
649 | T(8, "Keypad"); | |
650 | printf("\n"); | |
651 | break; | |
652 | #undef T | |
653 | } | |
654 | default: | |
655 | break; | |
656 | } | |
657 | ||
658 | data += sublen + 4; | |
659 | len -= sublen + 4; | |
660 | } | |
661 | ||
662 | if (len != 0) { | |
663 | printf("\t\t * bogus tail data (%d):", len); | |
664 | while (len) { | |
665 | printf(" %.2x", *data); | |
666 | data++; | |
667 | len--; | |
668 | } | |
669 | printf("\n"); | |
670 | } | |
671 | } | |
672 | ||
83b4934c | 673 | static const struct ie_print wifiprinters[] = { |
febeb0c0 JB |
674 | [1] = { "WPA", print_wifi_wpa, 2, 255, BIT(PRINT_SCAN), }, |
675 | [2] = { "WMM", print_wifi_wmm, 1, 255, BIT(PRINT_SCAN), }, | |
676 | [4] = { "WPS", print_wifi_wps, 0, 255, BIT(PRINT_SCAN), }, | |
4673a894 JB |
677 | }; |
678 | ||
764fe753 | 679 | static void print_vendor(unsigned char len, unsigned char *data, |
febeb0c0 | 680 | bool unknown, enum print_ie_type ptype) |
3563f4c5 JB |
681 | { |
682 | int i; | |
683 | ||
fbf80af5 | 684 | if (len < 3) { |
4673a894 | 685 | printf("\tVendor specific: <too short> data:"); |
fbf80af5 JB |
686 | for(i = 0; i < len; i++) |
687 | printf(" %.02x", data[i]); | |
688 | printf("\n"); | |
689 | return; | |
690 | } | |
691 | ||
857d966e | 692 | if (len >= 4 && memcmp(data, wifi_oui, 3) == 0) { |
febeb0c0 JB |
693 | if (data[3] < ARRAY_SIZE(wifiprinters) && |
694 | wifiprinters[data[3]].name && | |
695 | wifiprinters[data[3]].flags & BIT(ptype)) { | |
83b4934c JB |
696 | print_ie(&wifiprinters[data[3]], data[3], len - 4, data + 4); |
697 | return; | |
698 | } | |
febeb0c0 | 699 | if (!unknown) |
4673a894 | 700 | return; |
857d966e | 701 | printf("\tWiFi OUI %#.2x, data:", data[3]); |
4673a894 JB |
702 | for(i = 0; i < len - 4; i++) |
703 | printf(" %.02x", data[i + 4]); | |
704 | printf("\n"); | |
705 | return; | |
706 | } | |
707 | ||
febeb0c0 | 708 | if (!unknown) |
764fe753 JB |
709 | return; |
710 | ||
fbf80af5 | 711 | printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:", |
3563f4c5 | 712 | data[0], data[1], data[2]); |
fbf80af5 JB |
713 | for (i = 3; i < len; i++) |
714 | printf(" %.2x", data[i]); | |
3563f4c5 JB |
715 | printf("\n"); |
716 | } | |
717 | ||
febeb0c0 JB |
718 | void print_ies(unsigned char *ie, int ielen, bool unknown, |
719 | enum print_ie_type ptype) | |
3563f4c5 JB |
720 | { |
721 | while (ielen >= 2 && ielen >= ie[1]) { | |
febeb0c0 JB |
722 | if (ie[0] < ARRAY_SIZE(ieprinters) && |
723 | ieprinters[ie[0]].name && | |
724 | ieprinters[ie[0]].flags & BIT(ptype)) { | |
83b4934c | 725 | print_ie(&ieprinters[ie[0]], ie[0], ie[1], ie + 2); |
764fe753 | 726 | } else if (ie[0] == 221 /* vendor */) { |
febeb0c0 JB |
727 | print_vendor(ie[1], ie + 2, unknown, ptype); |
728 | } else if (unknown) { | |
3563f4c5 JB |
729 | int i; |
730 | ||
8086b700 | 731 | printf("\tUnknown IE (%d):", ie[0]); |
3563f4c5 | 732 | for (i=0; i<ie[1]; i++) |
8086b700 | 733 | printf(" %.2x", ie[2+i]); |
3563f4c5 JB |
734 | printf("\n"); |
735 | } | |
736 | ielen -= ie[1] + 2; | |
737 | ie += ie[1] + 2; | |
738 | } | |
739 | } | |
740 | ||
741 | static int print_bss_handler(struct nl_msg *msg, void *arg) | |
742 | { | |
743 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
744 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
745 | struct nlattr *bss[NL80211_BSS_MAX + 1]; | |
746 | char mac_addr[20], dev[20]; | |
747 | static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = { | |
748 | [NL80211_BSS_TSF] = { .type = NLA_U64 }, | |
749 | [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 }, | |
750 | [NL80211_BSS_BSSID] = { }, | |
751 | [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 }, | |
752 | [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 }, | |
753 | [NL80211_BSS_INFORMATION_ELEMENTS] = { }, | |
f2e17e1f JB |
754 | [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 }, |
755 | [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 }, | |
a56117a6 | 756 | [NL80211_BSS_STATUS] = { .type = NLA_U32 }, |
3563f4c5 | 757 | }; |
febeb0c0 | 758 | struct scan_params *params = arg; |
3563f4c5 JB |
759 | |
760 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
761 | genlmsg_attrlen(gnlh, 0), NULL); | |
762 | ||
763 | if (!tb[NL80211_ATTR_BSS]) { | |
764 | fprintf(stderr, "bss info missing!"); | |
765 | return NL_SKIP; | |
766 | } | |
767 | if (nla_parse_nested(bss, NL80211_BSS_MAX, | |
768 | tb[NL80211_ATTR_BSS], | |
769 | bss_policy)) { | |
770 | fprintf(stderr, "failed to parse nested attributes!"); | |
771 | return NL_SKIP; | |
772 | } | |
773 | ||
774 | if (!bss[NL80211_BSS_BSSID]) | |
775 | return NL_SKIP; | |
776 | ||
777 | mac_addr_n2a(mac_addr, nla_data(bss[NL80211_BSS_BSSID])); | |
778 | if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev); | |
a56117a6 JB |
779 | printf("BSS %s (on %s)", mac_addr, dev); |
780 | ||
781 | if (bss[NL80211_BSS_STATUS]) { | |
782 | switch (nla_get_u32(bss[NL80211_BSS_STATUS])) { | |
783 | case NL80211_BSS_STATUS_AUTHENTICATED: | |
784 | printf(" -- authenticated"); | |
785 | break; | |
786 | case NL80211_BSS_STATUS_ASSOCIATED: | |
787 | printf(" -- associated"); | |
788 | break; | |
789 | case NL80211_BSS_STATUS_IBSS_JOINED: | |
790 | printf(" -- joined"); | |
791 | break; | |
792 | default: | |
793 | printf(" -- unknown status: %d", | |
794 | nla_get_u32(bss[NL80211_BSS_STATUS])); | |
795 | break; | |
796 | } | |
797 | } | |
798 | printf("\n"); | |
3563f4c5 | 799 | |
e7109a8a JB |
800 | if (bss[NL80211_BSS_TSF]) { |
801 | unsigned long long tsf; | |
802 | tsf = (unsigned long long)nla_get_u64(bss[NL80211_BSS_TSF]); | |
803 | printf("\tTSF: %llu usec (%llud, %.2lld:%.2llu:%.2llu)\n", | |
804 | tsf, tsf/1000/1000/60/60/24, (tsf/1000/1000/60/60) % 24, | |
805 | (tsf/1000/1000/60) % 60, (tsf/1000/1000) % 60); | |
806 | } | |
3563f4c5 JB |
807 | if (bss[NL80211_BSS_FREQUENCY]) |
808 | printf("\tfreq: %d\n", | |
809 | nla_get_u32(bss[NL80211_BSS_FREQUENCY])); | |
810 | if (bss[NL80211_BSS_BEACON_INTERVAL]) | |
811 | printf("\tbeacon interval: %d\n", | |
812 | nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL])); | |
92a04ecd MH |
813 | if (bss[NL80211_BSS_CAPABILITY]) { |
814 | __u16 capa = nla_get_u16(bss[NL80211_BSS_CAPABILITY]); | |
815 | printf("\tcapability:"); | |
816 | if (capa & WLAN_CAPABILITY_ESS) | |
817 | printf(" ESS"); | |
818 | if (capa & WLAN_CAPABILITY_IBSS) | |
819 | printf(" IBSS"); | |
820 | if (capa & WLAN_CAPABILITY_PRIVACY) | |
821 | printf(" Privacy"); | |
822 | if (capa & WLAN_CAPABILITY_SHORT_PREAMBLE) | |
823 | printf(" ShortPreamble"); | |
824 | if (capa & WLAN_CAPABILITY_PBCC) | |
825 | printf(" PBCC"); | |
826 | if (capa & WLAN_CAPABILITY_CHANNEL_AGILITY) | |
827 | printf(" ChannelAgility"); | |
828 | if (capa & WLAN_CAPABILITY_SPECTRUM_MGMT) | |
829 | printf(" SpectrumMgmt"); | |
830 | if (capa & WLAN_CAPABILITY_QOS) | |
831 | printf(" QoS"); | |
832 | if (capa & WLAN_CAPABILITY_SHORT_SLOT_TIME) | |
833 | printf(" ShortSlotTime"); | |
834 | if (capa & WLAN_CAPABILITY_APSD) | |
835 | printf(" APSD"); | |
836 | if (capa & WLAN_CAPABILITY_DSSS_OFDM) | |
837 | printf(" DSSS-OFDM"); | |
838 | printf(" (0x%.4x)\n", capa); | |
839 | } | |
f2e17e1f JB |
840 | if (bss[NL80211_BSS_SIGNAL_MBM]) { |
841 | int s = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]); | |
842 | printf("\tsignal: %d.%.2d dBm\n", s/100, s%100); | |
843 | } | |
844 | if (bss[NL80211_BSS_SIGNAL_UNSPEC]) { | |
845 | unsigned char s = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]); | |
846 | printf("\tsignal: %d/100\n", s); | |
847 | } | |
3563f4c5 JB |
848 | if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) |
849 | print_ies(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]), | |
764fe753 | 850 | nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]), |
febeb0c0 | 851 | params->unknown, params->type); |
3563f4c5 JB |
852 | |
853 | return NL_SKIP; | |
854 | } | |
855 | ||
764fe753 | 856 | static struct scan_params scan_params; |
3563f4c5 | 857 | |
7c37a24d JB |
858 | static int handle_scan_dump(struct nl80211_state *state, |
859 | struct nl_cb *cb, | |
3563f4c5 JB |
860 | struct nl_msg *msg, |
861 | int argc, char **argv) | |
862 | { | |
764fe753 JB |
863 | if (argc > 1) |
864 | return 1; | |
865 | ||
866 | scan_params.unknown = false; | |
867 | if (argc == 1 && !strcmp(argv[0], "-u")) | |
868 | scan_params.unknown = true; | |
869 | ||
febeb0c0 JB |
870 | scan_params.type = PRINT_SCAN; |
871 | ||
764fe753 JB |
872 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_bss_handler, |
873 | &scan_params); | |
3563f4c5 JB |
874 | return 0; |
875 | } | |
a5fe4ef2 JB |
876 | |
877 | static int handle_scan_combined(struct nl80211_state *state, | |
878 | struct nl_cb *cb, | |
879 | struct nl_msg *msg, | |
880 | int argc, char **argv) | |
881 | { | |
559a1713 | 882 | char **trig_argv; |
a5fe4ef2 JB |
883 | static char *dump_argv[] = { |
884 | NULL, | |
885 | "scan", | |
886 | "dump", | |
92649eab | 887 | NULL, |
a5fe4ef2 JB |
888 | }; |
889 | static const __u32 cmds[] = { | |
890 | NL80211_CMD_NEW_SCAN_RESULTS, | |
891 | NL80211_CMD_SCAN_ABORTED, | |
892 | }; | |
559a1713 | 893 | int trig_argc, dump_argc, err; |
a5fe4ef2 | 894 | |
559a1713 JB |
895 | if (argc >= 3 && !strcmp(argv[2], "-u")) { |
896 | dump_argc = 4; | |
897 | dump_argv[3] = "-u"; | |
898 | } else | |
899 | dump_argc = 3; | |
900 | ||
901 | trig_argc = 3 + (argc - 2) + (3 - dump_argc); | |
902 | trig_argv = calloc(trig_argc, sizeof(*trig_argv)); | |
903 | if (!trig_argv) | |
904 | return -ENOMEM; | |
a5fe4ef2 | 905 | trig_argv[0] = argv[0]; |
559a1713 JB |
906 | trig_argv[1] = "scan"; |
907 | trig_argv[2] = "trigger"; | |
908 | int i; | |
909 | for (i = 0; i < argc - 2 - (dump_argc - 3); i++) | |
910 | trig_argv[i + 3] = argv[i + 2 + (dump_argc - 3)]; | |
911 | err = handle_cmd(state, II_NETDEV, trig_argc, trig_argv); | |
912 | free(trig_argv); | |
a5fe4ef2 JB |
913 | if (err) |
914 | return err; | |
915 | ||
61725dbe JB |
916 | /* |
917 | * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION | |
918 | * | |
919 | * This code has a bug, which requires creating a separate | |
920 | * nl80211 socket to fix: | |
921 | * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or | |
922 | * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel | |
923 | * before (!) we listen to it, because we only start listening | |
924 | * after we send our scan request. | |
925 | * | |
926 | * Doing it the other way around has a race condition as well, | |
927 | * if you first open the events socket you may get a notification | |
928 | * for a previous scan. | |
929 | * | |
930 | * The only proper way to fix this would be to listen to events | |
931 | * before sending the command, and for the kernel to send the | |
932 | * scan request along with the event, so that you can match up | |
933 | * whether the scan you requested was finished or aborted (this | |
934 | * may result in processing a scan that another application | |
935 | * requested, but that doesn't seem to be a problem). | |
936 | * | |
937 | * Alas, the kernel doesn't do that (yet). | |
938 | */ | |
939 | ||
a5fe4ef2 JB |
940 | if (listen_events(state, ARRAY_SIZE(cmds), cmds) == |
941 | NL80211_CMD_SCAN_ABORTED) { | |
942 | printf("scan aborted!\n"); | |
943 | return 0; | |
944 | } | |
945 | ||
946 | dump_argv[0] = argv[0]; | |
92649eab | 947 | return handle_cmd(state, II_NETDEV, dump_argc, dump_argv); |
a5fe4ef2 | 948 | } |
6ca98d28 JB |
949 | TOPLEVEL(scan, "[-u] [freq <freq>*] [ssid <ssid>*|passive]", 0, 0, |
950 | CIB_NETDEV, handle_scan_combined, | |
951 | "Scan on the given frequencies and probe for the given SSIDs\n" | |
952 | "(or wildcard if not given) unless passive scanning is requested.\n" | |
953 | "If -u is specified print unknown data in the scan results."); | |
4698bfc2 JB |
954 | COMMAND(scan, dump, "[-u]", |
955 | NL80211_CMD_GET_SCAN, NLM_F_DUMP, CIB_NETDEV, handle_scan_dump, | |
956 | "Dump the current scan results. If -u is specified, print unknown\n" | |
957 | "data in scan results."); | |
958 | COMMAND(scan, trigger, "[freq <freq>*] [ssid <ssid>*|passive]", | |
959 | NL80211_CMD_TRIGGER_SCAN, 0, CIB_NETDEV, handle_scan, | |
960 | "Trigger a scan on the given frequencies with probing for the given\n" | |
961 | "SSIDs (or wildcard if not given) unless passive scanning is requested."); |