]>
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 | ||
3bd60ef1 JB |
30 | static unsigned char ms_oui[3] = { 0x00, 0x50, 0xf2 }; |
31 | static unsigned char ieee80211_oui[3] = { 0x00, 0x0f, 0xac }; | |
9a22374a | 32 | static unsigned char wfa_oui[3] = { 0x50, 0x6f, 0x9a }; |
857d966e | 33 | |
764fe753 JB |
34 | struct scan_params { |
35 | bool unknown; | |
febeb0c0 | 36 | enum print_ie_type type; |
575280cc | 37 | bool show_both_ie_sets; |
764fe753 JB |
38 | }; |
39 | ||
2b690f0a LR |
40 | #define IEEE80211_COUNTRY_EXTENSION_ID 201 |
41 | ||
72725719 JB |
42 | union ieee80211_country_ie_triplet { |
43 | struct { | |
44 | __u8 first_channel; | |
45 | __u8 num_channels; | |
46 | __s8 max_power; | |
47 | } __attribute__ ((packed)) chans; | |
48 | struct { | |
49 | __u8 reg_extension_id; | |
50 | __u8 reg_class; | |
51 | __u8 coverage_class; | |
52 | } __attribute__ ((packed)) ext; | |
2b690f0a LR |
53 | } __attribute__ ((packed)); |
54 | ||
7c37a24d JB |
55 | static int handle_scan(struct nl80211_state *state, |
56 | struct nl_cb *cb, | |
3563f4c5 | 57 | struct nl_msg *msg, |
05514f95 JB |
58 | int argc, char **argv, |
59 | enum id_input id) | |
3563f4c5 | 60 | { |
559a1713 JB |
61 | struct nl_msg *ssids = NULL, *freqs = NULL; |
62 | char *eptr; | |
3563f4c5 | 63 | int err = -ENOBUFS; |
559a1713 JB |
64 | int i; |
65 | enum { | |
66 | NONE, | |
67 | FREQ, | |
64797a7f | 68 | IES, |
559a1713 JB |
69 | SSID, |
70 | DONE, | |
71 | } parse = NONE; | |
72 | int freq; | |
73 | bool passive = false, have_ssids = false, have_freqs = false; | |
64797a7f JB |
74 | size_t tmp; |
75 | unsigned char *ies; | |
fe862239 | 76 | int flags = 0; |
3563f4c5 JB |
77 | |
78 | ssids = nlmsg_alloc(); | |
79 | if (!ssids) | |
80 | return -ENOMEM; | |
559a1713 JB |
81 | |
82 | freqs = nlmsg_alloc(); | |
83 | if (!freqs) { | |
84 | nlmsg_free(ssids); | |
85 | return -ENOMEM; | |
86 | } | |
87 | ||
88 | for (i = 0; i < argc; i++) { | |
559a1713 JB |
89 | switch (parse) { |
90 | case NONE: | |
64797a7f JB |
91 | if (strcmp(argv[i], "freq") == 0) { |
92 | parse = FREQ; | |
93 | have_freqs = true; | |
94 | break; | |
95 | } else if (strcmp(argv[i], "ies") == 0) { | |
96 | parse = IES; | |
97 | break; | |
fe862239 SL |
98 | } else if (strcmp(argv[i], "lowpri") == 0) { |
99 | parse = NONE; | |
100 | flags |= NL80211_SCAN_FLAG_LOW_PRIORITY; | |
101 | break; | |
102 | } else if (strcmp(argv[i], "flush") == 0) { | |
103 | parse = NONE; | |
104 | flags |= NL80211_SCAN_FLAG_FLUSH; | |
105 | break; | |
ced94d5f AQ |
106 | } else if (strcmp(argv[i], "ap-force") == 0) { |
107 | parse = NONE; | |
108 | flags |= NL80211_SCAN_FLAG_AP; | |
109 | break; | |
64797a7f JB |
110 | } else if (strcmp(argv[i], "ssid") == 0) { |
111 | parse = SSID; | |
112 | have_ssids = true; | |
113 | break; | |
114 | } else if (strcmp(argv[i], "passive") == 0) { | |
115 | parse = DONE; | |
116 | passive = true; | |
117 | break; | |
118 | } | |
559a1713 JB |
119 | case DONE: |
120 | return 1; | |
121 | case FREQ: | |
122 | freq = strtoul(argv[i], &eptr, 10); | |
cc12e895 JB |
123 | if (eptr != argv[i] + strlen(argv[i])) { |
124 | /* failed to parse as number -- maybe a tag? */ | |
125 | i--; | |
126 | parse = NONE; | |
127 | continue; | |
128 | } | |
559a1713 | 129 | NLA_PUT_U32(freqs, i, freq); |
64797a7f JB |
130 | break; |
131 | case IES: | |
132 | ies = parse_hex(argv[i], &tmp); | |
133 | if (!ies) | |
134 | goto nla_put_failure; | |
135 | NLA_PUT(msg, NL80211_ATTR_IE, tmp, ies); | |
136 | free(ies); | |
137 | parse = NONE; | |
559a1713 JB |
138 | break; |
139 | case SSID: | |
140 | NLA_PUT(ssids, i, strlen(argv[i]), argv[i]); | |
141 | break; | |
142 | } | |
143 | } | |
144 | ||
145 | if (!have_ssids) | |
146 | NLA_PUT(ssids, 1, 0, ""); | |
147 | if (!passive) | |
148 | nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids); | |
149 | ||
150 | if (have_freqs) | |
151 | nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs); | |
fe862239 SL |
152 | if (flags) |
153 | NLA_PUT_U32(msg, NL80211_ATTR_SCAN_FLAGS, flags); | |
3563f4c5 JB |
154 | |
155 | err = 0; | |
156 | nla_put_failure: | |
157 | nlmsg_free(ssids); | |
559a1713 | 158 | nlmsg_free(freqs); |
3563f4c5 JB |
159 | return err; |
160 | } | |
3563f4c5 | 161 | |
857d966e MH |
162 | static void tab_on_first(bool *first) |
163 | { | |
164 | if (!*first) | |
165 | printf("\t"); | |
166 | else | |
167 | *first = false; | |
168 | } | |
169 | ||
83b4934c | 170 | static void print_ssid(const uint8_t type, uint8_t len, const uint8_t *data) |
3563f4c5 | 171 | { |
83b4934c | 172 | printf(" "); |
748f8489 | 173 | print_ssid_escaped(len, data); |
3563f4c5 JB |
174 | printf("\n"); |
175 | } | |
176 | ||
1fd19c39 CL |
177 | #define BSS_MEMBERSHIP_SELECTOR_HT_PHY 127 |
178 | ||
83b4934c | 179 | static void print_supprates(const uint8_t type, uint8_t len, const uint8_t *data) |
3563f4c5 JB |
180 | { |
181 | int i; | |
182 | ||
83b4934c | 183 | printf(" "); |
3563f4c5 | 184 | |
83b4934c | 185 | for (i = 0; i < len; i++) { |
3563f4c5 | 186 | int r = data[i] & 0x7f; |
1fd19c39 | 187 | |
2201d500 | 188 | if (r == BSS_MEMBERSHIP_SELECTOR_HT_PHY && data[i] & 0x80) |
1fd19c39 CL |
189 | printf("HT"); |
190 | else | |
191 | printf("%d.%d", r/2, 5*(r&1)); | |
192 | ||
193 | printf("%s ", data[i] & 0x80 ? "*" : ""); | |
3563f4c5 JB |
194 | } |
195 | printf("\n"); | |
196 | } | |
197 | ||
83b4934c | 198 | static void print_ds(const uint8_t type, uint8_t len, const uint8_t *data) |
3563f4c5 | 199 | { |
83b4934c | 200 | printf(" channel %d\n", data[0]); |
3563f4c5 JB |
201 | } |
202 | ||
2b690f0a | 203 | static const char *country_env_str(char environment) |
b7e8fa37 | 204 | { |
2b690f0a | 205 | switch (environment) { |
b7e8fa37 | 206 | case 'I': |
2b690f0a | 207 | return "Indoor only"; |
b7e8fa37 | 208 | case 'O': |
2b690f0a | 209 | return "Outdoor only"; |
b6c0d634 | 210 | case ' ': |
2b690f0a | 211 | return "Indoor/Outdoor"; |
b6c0d634 | 212 | default: |
2b690f0a | 213 | return "bogus"; |
b7e8fa37 | 214 | } |
2b690f0a LR |
215 | } |
216 | ||
217 | static void print_country(const uint8_t type, uint8_t len, const uint8_t *data) | |
218 | { | |
219 | printf(" %.*s", 2, data); | |
220 | ||
221 | printf("\tEnvironment: %s\n", country_env_str(data[2])); | |
222 | ||
223 | data += 3; | |
224 | len -= 3; | |
225 | ||
226 | if (len < 3) { | |
227 | printf("\t\tNo country IE triplets present\n"); | |
228 | return; | |
229 | } | |
230 | ||
231 | while (len >= 3) { | |
232 | int end_channel; | |
72725719 | 233 | union ieee80211_country_ie_triplet *triplet = (void *) data; |
2b690f0a LR |
234 | |
235 | if (triplet->ext.reg_extension_id >= IEEE80211_COUNTRY_EXTENSION_ID) { | |
236 | printf("\t\tExtension ID: %d Regulatory Class: %d Coverage class: %d (up to %dm)\n", | |
237 | triplet->ext.reg_extension_id, | |
238 | triplet->ext.reg_class, | |
239 | triplet->ext.coverage_class, | |
240 | triplet->ext.coverage_class * 450); | |
241 | ||
242 | data += 3; | |
243 | len -= 3; | |
244 | continue; | |
245 | } | |
246 | ||
247 | /* 2 GHz */ | |
248 | if (triplet->chans.first_channel <= 14) | |
249 | end_channel = triplet->chans.first_channel + (triplet->chans.num_channels - 1); | |
250 | else | |
251 | end_channel = triplet->chans.first_channel + (4 * (triplet->chans.num_channels - 1)); | |
252 | ||
a9859d30 | 253 | printf("\t\tChannels [%d - %d] @ %d dBm\n", triplet->chans.first_channel, end_channel, triplet->chans.max_power); |
2b690f0a LR |
254 | |
255 | data += 3; | |
256 | len -= 3; | |
257 | } | |
258 | ||
259 | return; | |
b7e8fa37 MH |
260 | } |
261 | ||
d1563a1b MH |
262 | static void print_powerconstraint(const uint8_t type, uint8_t len, const uint8_t *data) |
263 | { | |
264 | printf(" %d dB\n", data[0]); | |
265 | } | |
266 | ||
83b4934c | 267 | static void print_erp(const uint8_t type, uint8_t len, const uint8_t *data) |
fc4d1484 MH |
268 | { |
269 | if (data[0] == 0x00) | |
83b4934c | 270 | printf(" <no flags>"); |
fc4d1484 MH |
271 | if (data[0] & 0x01) |
272 | printf(" NonERP_Present"); | |
273 | if (data[0] & 0x02) | |
274 | printf(" Use_Protection"); | |
275 | if (data[0] & 0x04) | |
276 | printf(" Barker_Preamble_Mode"); | |
277 | printf("\n"); | |
278 | } | |
279 | ||
83b4934c | 280 | static void print_cipher(const uint8_t *data) |
857d966e | 281 | { |
3bd60ef1 | 282 | if (memcmp(data, ms_oui, 3) == 0) { |
857d966e | 283 | switch (data[3]) { |
510e0e2f | 284 | case 0: |
857d966e MH |
285 | printf("Use group cipher suite"); |
286 | break; | |
510e0e2f | 287 | case 1: |
857d966e MH |
288 | printf("WEP-40"); |
289 | break; | |
510e0e2f | 290 | case 2: |
857d966e MH |
291 | printf("TKIP"); |
292 | break; | |
510e0e2f | 293 | case 4: |
857d966e MH |
294 | printf("CCMP"); |
295 | break; | |
510e0e2f | 296 | case 5: |
857d966e MH |
297 | printf("WEP-104"); |
298 | break; | |
299 | default: | |
332769c6 | 300 | printf("%.02x-%.02x-%.02x:%d", |
5594fd23 | 301 | data[0], data[1] ,data[2], data[3]); |
857d966e MH |
302 | break; |
303 | } | |
304 | } else if (memcmp(data, ieee80211_oui, 3) == 0) { | |
305 | switch (data[3]) { | |
510e0e2f | 306 | case 0: |
857d966e MH |
307 | printf("Use group cipher suite"); |
308 | break; | |
510e0e2f | 309 | case 1: |
857d966e MH |
310 | printf("WEP-40"); |
311 | break; | |
510e0e2f | 312 | case 2: |
857d966e MH |
313 | printf("TKIP"); |
314 | break; | |
510e0e2f | 315 | case 4: |
857d966e MH |
316 | printf("CCMP"); |
317 | break; | |
510e0e2f | 318 | case 5: |
857d966e MH |
319 | printf("WEP-104"); |
320 | break; | |
510e0e2f | 321 | case 6: |
857d966e MH |
322 | printf("AES-128-CMAC"); |
323 | break; | |
a8b3da9d VK |
324 | case 8: |
325 | printf("GCMP"); | |
326 | break; | |
857d966e | 327 | default: |
332769c6 | 328 | printf("%.02x-%.02x-%.02x:%d", |
5594fd23 | 329 | data[0], data[1] ,data[2], data[3]); |
857d966e MH |
330 | break; |
331 | } | |
332 | } else | |
332769c6 | 333 | printf("%.02x-%.02x-%.02x:%d", |
5594fd23 | 334 | data[0], data[1] ,data[2], data[3]); |
857d966e MH |
335 | } |
336 | ||
83b4934c | 337 | static void print_auth(const uint8_t *data) |
857d966e | 338 | { |
3bd60ef1 | 339 | if (memcmp(data, ms_oui, 3) == 0) { |
857d966e | 340 | switch (data[3]) { |
510e0e2f | 341 | case 1: |
857d966e MH |
342 | printf("IEEE 802.1X"); |
343 | break; | |
510e0e2f | 344 | case 2: |
857d966e MH |
345 | printf("PSK"); |
346 | break; | |
347 | default: | |
332769c6 | 348 | printf("%.02x-%.02x-%.02x:%d", |
5594fd23 | 349 | data[0], data[1] ,data[2], data[3]); |
857d966e MH |
350 | break; |
351 | } | |
352 | } else if (memcmp(data, ieee80211_oui, 3) == 0) { | |
353 | switch (data[3]) { | |
510e0e2f | 354 | case 1: |
857d966e MH |
355 | printf("IEEE 802.1X"); |
356 | break; | |
510e0e2f | 357 | case 2: |
857d966e MH |
358 | printf("PSK"); |
359 | break; | |
0fe1c415 JB |
360 | case 3: |
361 | printf("FT/IEEE 802.1X"); | |
362 | break; | |
363 | case 4: | |
364 | printf("FT/PSK"); | |
365 | break; | |
366 | case 5: | |
367 | printf("IEEE 802.1X/SHA-256"); | |
368 | break; | |
369 | case 6: | |
370 | printf("PSK/SHA-256"); | |
371 | break; | |
857d966e | 372 | default: |
332769c6 | 373 | printf("%.02x-%.02x-%.02x:%d", |
5594fd23 | 374 | data[0], data[1] ,data[2], data[3]); |
857d966e MH |
375 | break; |
376 | } | |
377 | } else | |
332769c6 | 378 | printf("%.02x-%.02x-%.02x:%d", |
5594fd23 | 379 | data[0], data[1] ,data[2], data[3]); |
857d966e MH |
380 | } |
381 | ||
83b4934c JB |
382 | static void print_rsn_ie(const char *defcipher, const char *defauth, |
383 | uint8_t len, const uint8_t *data) | |
857d966e MH |
384 | { |
385 | bool first = true; | |
386 | __u16 version, count, capa; | |
387 | int i; | |
388 | ||
857d966e MH |
389 | version = data[0] + (data[1] << 8); |
390 | tab_on_first(&first); | |
391 | printf("\t * Version: %d\n", version); | |
392 | ||
393 | data += 2; | |
394 | len -= 2; | |
395 | ||
396 | if (len < 4) { | |
397 | tab_on_first(&first); | |
398 | printf("\t * Group cipher: %s\n", defcipher); | |
399 | printf("\t * Pairwise ciphers: %s\n", defcipher); | |
400 | return; | |
401 | } | |
402 | ||
403 | tab_on_first(&first); | |
404 | printf("\t * Group cipher: "); | |
405 | print_cipher(data); | |
406 | printf("\n"); | |
407 | ||
408 | data += 4; | |
409 | len -= 4; | |
410 | ||
411 | if (len < 2) { | |
412 | tab_on_first(&first); | |
413 | printf("\t * Pairwise ciphers: %s\n", defcipher); | |
414 | return; | |
415 | } | |
416 | ||
417 | count = data[0] | (data[1] << 8); | |
31d477fb MH |
418 | if (2 + (count * 4) > len) |
419 | goto invalid; | |
420 | ||
857d966e MH |
421 | tab_on_first(&first); |
422 | printf("\t * Pairwise ciphers:"); | |
31d477fb | 423 | for (i = 0; i < count; i++) { |
857d966e MH |
424 | printf(" "); |
425 | print_cipher(data + 2 + (i * 4)); | |
426 | } | |
427 | printf("\n"); | |
428 | ||
429 | data += 2 + (count * 4); | |
430 | len -= 2 + (count * 4); | |
431 | ||
432 | if (len < 2) { | |
433 | tab_on_first(&first); | |
434 | printf("\t * Authentication suites: %s\n", defauth); | |
435 | return; | |
436 | } | |
437 | ||
438 | count = data[0] | (data[1] << 8); | |
31d477fb MH |
439 | if (2 + (count * 4) > len) |
440 | goto invalid; | |
441 | ||
857d966e MH |
442 | tab_on_first(&first); |
443 | printf("\t * Authentication suites:"); | |
83b4934c | 444 | for (i = 0; i < count; i++) { |
857d966e MH |
445 | printf(" "); |
446 | print_auth(data + 2 + (i * 4)); | |
447 | } | |
448 | printf("\n"); | |
449 | ||
450 | data += 2 + (count * 4); | |
451 | len -= 2 + (count * 4); | |
452 | ||
6a4f24e8 JB |
453 | if (len >= 2) { |
454 | capa = data[0] | (data[1] << 8); | |
455 | tab_on_first(&first); | |
cadbe89a JB |
456 | printf("\t * Capabilities:"); |
457 | if (capa & 0x0001) | |
458 | printf(" PreAuth"); | |
459 | if (capa & 0x0002) | |
460 | printf(" NoPairwise"); | |
461 | switch ((capa & 0x000c) >> 2) { | |
462 | case 0: | |
463 | break; | |
464 | case 1: | |
465 | printf(" 2-PTKSA-RC"); | |
466 | break; | |
467 | case 2: | |
468 | printf(" 4-PTKSA-RC"); | |
469 | break; | |
470 | case 3: | |
471 | printf(" 16-PTKSA-RC"); | |
472 | break; | |
473 | } | |
474 | switch ((capa & 0x0030) >> 4) { | |
475 | case 0: | |
476 | break; | |
477 | case 1: | |
478 | printf(" 2-GTKSA-RC"); | |
479 | break; | |
480 | case 2: | |
481 | printf(" 4-GTKSA-RC"); | |
482 | break; | |
483 | case 3: | |
484 | printf(" 16-GTKSA-RC"); | |
485 | break; | |
486 | } | |
487 | if (capa & 0x0040) | |
488 | printf(" MFP-required"); | |
489 | if (capa & 0x0080) | |
490 | printf(" MFP-capable"); | |
491 | if (capa & 0x0200) | |
492 | printf(" Peerkey-enabled"); | |
493 | if (capa & 0x0400) | |
494 | printf(" SPP-AMSDU-capable"); | |
495 | if (capa & 0x0800) | |
496 | printf(" SPP-AMSDU-required"); | |
497 | printf(" (0x%.4x)\n", capa); | |
6a4f24e8 JB |
498 | data += 2; |
499 | len -= 2; | |
500 | } | |
31d477fb | 501 | |
5ba3f523 JB |
502 | if (len >= 2) { |
503 | int pmkid_count = data[0] | (data[1] << 8); | |
504 | ||
505 | if (len >= 2 + 16 * pmkid_count) { | |
506 | tab_on_first(&first); | |
507 | printf("\t * %d PMKIDs\n", pmkid_count); | |
508 | /* not printing PMKID values */ | |
509 | data += 2 + 16 * pmkid_count; | |
510 | len -= 2 + 16 * pmkid_count; | |
511 | } else | |
512 | goto invalid; | |
513 | } | |
514 | ||
515 | if (len >= 4) { | |
516 | tab_on_first(&first); | |
517 | printf("\t * Group mgmt cipher suite: "); | |
518 | print_cipher(data); | |
519 | printf("\n"); | |
520 | data += 4; | |
521 | len -= 4; | |
522 | } | |
523 | ||
cadbe89a | 524 | invalid: |
31d477fb MH |
525 | if (len != 0) { |
526 | printf("\t\t * bogus tail data (%d):", len); | |
527 | while (len) { | |
528 | printf(" %.2x", *data); | |
529 | data++; | |
530 | len--; | |
531 | } | |
532 | printf("\n"); | |
533 | } | |
857d966e MH |
534 | } |
535 | ||
83b4934c | 536 | static void print_rsn(const uint8_t type, uint8_t len, const uint8_t *data) |
857d966e | 537 | { |
83b4934c | 538 | print_rsn_ie("CCMP", "IEEE 802.1X", len, data); |
857d966e MH |
539 | } |
540 | ||
0c445c24 LR |
541 | static void print_ht_capa(const uint8_t type, uint8_t len, const uint8_t *data) |
542 | { | |
357c1a5d | 543 | printf("\n"); |
7ddfb679 JB |
544 | print_ht_capability(data[0] | (data[1] << 8)); |
545 | print_ampdu_length(data[2] & 3); | |
c79c7464 | 546 | print_ampdu_spacing((data[2] >> 2) & 7); |
7ddfb679 | 547 | print_ht_mcs(data + 3); |
0c445c24 LR |
548 | } |
549 | ||
be7602fb JB |
550 | static void print_ht_op(const uint8_t type, uint8_t len, const uint8_t *data) |
551 | { | |
552 | static const char *offset[4] = { | |
553 | "no secondary", | |
554 | "above", | |
555 | "[reserved!]", | |
556 | "below", | |
557 | }; | |
558 | static const char *protection[4] = { | |
559 | "no", | |
560 | "nonmember", | |
561 | "20 MHz", | |
562 | "non-HT mixed", | |
563 | }; | |
564 | static const char *sta_chan_width[2] = { | |
565 | "20 MHz", | |
566 | "any", | |
567 | }; | |
568 | ||
569 | printf("\n"); | |
570 | printf("\t\t * primary channel: %d\n", data[0]); | |
571 | printf("\t\t * secondary channel offset: %s\n", | |
572 | offset[data[1] & 0x3]); | |
573 | printf("\t\t * STA channel width: %s\n", sta_chan_width[(data[1] & 0x4)>>2]); | |
574 | printf("\t\t * RIFS: %d\n", (data[1] & 0x8)>>3); | |
575 | printf("\t\t * HT protection: %s\n", protection[data[2] & 0x3]); | |
576 | printf("\t\t * non-GF present: %d\n", (data[2] & 0x4) >> 2); | |
577 | printf("\t\t * OBSS non-GF present: %d\n", (data[2] & 0x10) >> 4); | |
578 | printf("\t\t * dual beacon: %d\n", (data[4] & 0x40) >> 6); | |
579 | printf("\t\t * dual CTS protection: %d\n", (data[4] & 0x80) >> 7); | |
580 | printf("\t\t * STBC beacon: %d\n", data[5] & 0x1); | |
581 | printf("\t\t * L-SIG TXOP Prot: %d\n", (data[5] & 0x2) >> 1); | |
582 | printf("\t\t * PCO active: %d\n", (data[5] & 0x4) >> 2); | |
583 | printf("\t\t * PCO phase: %d\n", (data[5] & 0x8) >> 3); | |
584 | } | |
585 | ||
83b4934c | 586 | static void print_capabilities(const uint8_t type, uint8_t len, const uint8_t *data) |
9b880b00 | 587 | { |
31d2d259 JB |
588 | int i, base, bit; |
589 | bool first = true; | |
590 | ||
591 | ||
592 | for (i = 0; i < len; i++) { | |
593 | base = i * 8; | |
594 | ||
595 | for (bit = 0; bit < 8; bit++) { | |
596 | if (!(data[i] & (1 << bit))) | |
597 | continue; | |
598 | ||
599 | if (!first) | |
600 | printf(","); | |
601 | else | |
602 | first = false; | |
603 | ||
70c649d2 JB |
604 | #define CAPA(bit, name) case bit: printf(" " name); break |
605 | ||
31d2d259 | 606 | switch (bit + base) { |
70c649d2 JB |
607 | CAPA(0, "HT Information Exchange Supported"); |
608 | CAPA(1, "reserved (On-demand Beacon)"); | |
609 | CAPA(2, "Extended Channel Switching"); | |
610 | CAPA(3, "reserved (Wave Indication)"); | |
611 | CAPA(4, "PSMP Capability"); | |
612 | CAPA(5, "reserved (Service Interval Granularity)"); | |
613 | CAPA(6, "S-PSMP Capability"); | |
614 | CAPA(7, "Event"); | |
615 | CAPA(8, "Diagnostics"); | |
616 | CAPA(9, "Multicast Diagnostics"); | |
617 | CAPA(10, "Location Tracking"); | |
618 | CAPA(11, "FMS"); | |
619 | CAPA(12, "Proxy ARP Service"); | |
620 | CAPA(13, "Collocated Interference Reporting"); | |
621 | CAPA(14, "Civic Location"); | |
622 | CAPA(15, "Geospatial Location"); | |
623 | CAPA(16, "TFS"); | |
624 | CAPA(17, "WNM-Sleep Mode"); | |
625 | CAPA(18, "TIM Broadcast"); | |
626 | CAPA(19, "BSS Transition"); | |
627 | CAPA(20, "QoS Traffic Capability"); | |
628 | CAPA(21, "AC Station Count"); | |
629 | CAPA(22, "Multiple BSSID"); | |
630 | CAPA(23, "Timing Measurement"); | |
631 | CAPA(24, "Channel Usage"); | |
632 | CAPA(25, "SSID List"); | |
633 | CAPA(26, "DMS"); | |
634 | CAPA(27, "UTC TSF Offset"); | |
635 | CAPA(28, "TDLS Peer U-APSD Buffer STA Support"); | |
636 | CAPA(29, "TDLS Peer PSM Support"); | |
637 | CAPA(30, "TDLS channel switching"); | |
638 | CAPA(31, "Interworking"); | |
639 | CAPA(32, "QoS Map"); | |
640 | CAPA(33, "EBR"); | |
641 | CAPA(34, "SSPN Interface"); | |
642 | CAPA(35, "Reserved"); | |
643 | CAPA(36, "MSGCF Capability"); | |
644 | CAPA(37, "TDLS Support"); | |
645 | CAPA(38, "TDLS Prohibited"); | |
646 | CAPA(39, "TDLS Channel Switching Prohibited"); | |
647 | CAPA(40, "Reject Unadmitted Frame"); | |
648 | CAPA(44, "Identifier Location"); | |
649 | CAPA(45, "U-APSD Coexistence"); | |
650 | CAPA(46, "WNM-Notification"); | |
651 | CAPA(47, "Reserved"); | |
652 | CAPA(48, "UTF-8 SSID"); | |
31d2d259 JB |
653 | default: |
654 | printf(" %d", bit); | |
655 | break; | |
656 | } | |
70c649d2 | 657 | #undef CAPA |
31d2d259 JB |
658 | } |
659 | } | |
9b880b00 | 660 | |
9b880b00 MH |
661 | printf("\n"); |
662 | } | |
663 | ||
575280cc JM |
664 | static void print_tim(const uint8_t type, uint8_t len, const uint8_t *data) |
665 | { | |
666 | printf(" DTIM Count %u DTIM Period %u Bitmap Control 0x%x " | |
667 | "Bitmap[0] 0x%x", | |
668 | data[0], data[1], data[2], data[3]); | |
669 | if (len - 4) | |
670 | printf(" (+ %u octet%s)", len - 4, len - 4 == 1 ? "" : "s"); | |
671 | printf("\n"); | |
672 | } | |
673 | ||
83b4934c JB |
674 | struct ie_print { |
675 | const char *name; | |
676 | void (*print)(const uint8_t type, uint8_t len, const uint8_t *data); | |
677 | uint8_t minlen, maxlen; | |
febeb0c0 | 678 | uint8_t flags; |
764fe753 JB |
679 | }; |
680 | ||
83b4934c JB |
681 | static void print_ie(const struct ie_print *p, const uint8_t type, |
682 | uint8_t len, const uint8_t *data) | |
4673a894 | 683 | { |
83b4934c JB |
684 | int i; |
685 | ||
686 | if (!p->print) | |
687 | return; | |
688 | ||
689 | printf("\t%s:", p->name); | |
690 | if (len < p->minlen || len > p->maxlen) { | |
691 | if (len > 1) { | |
692 | printf(" <invalid: %d bytes:", len); | |
693 | for (i = 0; i < len; i++) | |
694 | printf(" %.02x", data[i]); | |
695 | printf(">\n"); | |
696 | } else if (len) | |
697 | printf(" <invalid: 1 byte: %.02x>\n", data[0]); | |
698 | else | |
699 | printf(" <invalid: no data>\n"); | |
700 | return; | |
701 | } | |
702 | ||
703 | p->print(type, len, data); | |
704 | } | |
705 | ||
706 | #define PRINT_IGN { \ | |
707 | .name = "IGNORE", \ | |
708 | .print = NULL, \ | |
709 | .minlen = 0, \ | |
710 | .maxlen = 255, \ | |
4673a894 JB |
711 | } |
712 | ||
83b4934c | 713 | static const struct ie_print ieprinters[] = { |
febeb0c0 JB |
714 | [0] = { "SSID", print_ssid, 0, 32, BIT(PRINT_SCAN) | BIT(PRINT_LINK), }, |
715 | [1] = { "Supported rates", print_supprates, 0, 255, BIT(PRINT_SCAN), }, | |
014d581a | 716 | [3] = { "DS Parameter set", print_ds, 1, 1, BIT(PRINT_SCAN), }, |
575280cc | 717 | [5] = { "TIM", print_tim, 4, 255, BIT(PRINT_SCAN), }, |
febeb0c0 JB |
718 | [7] = { "Country", print_country, 3, 255, BIT(PRINT_SCAN), }, |
719 | [32] = { "Power constraint", print_powerconstraint, 1, 1, BIT(PRINT_SCAN), }, | |
720 | [42] = { "ERP", print_erp, 1, 255, BIT(PRINT_SCAN), }, | |
a2e61861 | 721 | [45] = { "HT capabilities", print_ht_capa, 26, 26, BIT(PRINT_SCAN), }, |
be7602fb | 722 | [61] = { "HT operation", print_ht_op, 22, 22, BIT(PRINT_SCAN), }, |
febeb0c0 JB |
723 | [48] = { "RSN", print_rsn, 2, 255, BIT(PRINT_SCAN), }, |
724 | [50] = { "Extended supported rates", print_supprates, 0, 255, BIT(PRINT_SCAN), }, | |
720583e9 | 725 | [114] = { "MESH ID", print_ssid, 0, 32, BIT(PRINT_SCAN) | BIT(PRINT_LINK), }, |
febeb0c0 | 726 | [127] = { "Extended capabilities", print_capabilities, 0, 255, BIT(PRINT_SCAN), }, |
83b4934c JB |
727 | }; |
728 | ||
729 | static void print_wifi_wpa(const uint8_t type, uint8_t len, const uint8_t *data) | |
730 | { | |
731 | print_rsn_ie("TKIP", "IEEE 802.1X", len, data); | |
732 | } | |
733 | ||
1cab57eb JB |
734 | static bool print_wifi_wmm_param(const uint8_t *data, uint8_t len) |
735 | { | |
736 | int i; | |
737 | static const char *aci_tbl[] = { "BE", "BK", "VI", "VO" }; | |
738 | ||
739 | if (len < 19) | |
740 | goto invalid; | |
741 | ||
742 | if (data[0] != 1) { | |
cee4fe20 | 743 | printf("Parameter: not version 1: "); |
1cab57eb JB |
744 | return false; |
745 | } | |
746 | ||
89ea706f | 747 | printf("\t * Parameter version 1"); |
1cab57eb JB |
748 | |
749 | data++; | |
750 | ||
751 | if (data[0] & 0x80) | |
89ea706f | 752 | printf("\n\t\t * u-APSD"); |
1cab57eb JB |
753 | |
754 | data += 2; | |
755 | ||
756 | for (i = 0; i < 4; i++) { | |
89ea706f | 757 | printf("\n\t\t * %s:", aci_tbl[(data[0] >> 5) & 3]); |
87181516 | 758 | if (data[0] & 0x10) |
1cab57eb JB |
759 | printf(" acm"); |
760 | printf(" CW %d-%d", (1 << (data[1] & 0xf)) - 1, | |
761 | (1 << (data[1] >> 4)) - 1); | |
a2a4c265 | 762 | printf(", AIFSN %d", data[0] & 0xf); |
1cab57eb | 763 | if (data[2] | data[3]) |
cee4fe20 | 764 | printf(", TXOP %d usec", (data[2] + (data[3] << 8)) * 32); |
1cab57eb JB |
765 | data += 4; |
766 | } | |
767 | ||
768 | printf("\n"); | |
769 | return true; | |
770 | ||
771 | invalid: | |
772 | printf("invalid: "); | |
773 | return false; | |
774 | } | |
775 | ||
83b4934c | 776 | static void print_wifi_wmm(const uint8_t type, uint8_t len, const uint8_t *data) |
6ff0c93a MH |
777 | { |
778 | int i; | |
779 | ||
6ff0c93a MH |
780 | switch (data[0]) { |
781 | case 0x00: | |
83b4934c | 782 | printf(" information:"); |
6ff0c93a MH |
783 | break; |
784 | case 0x01: | |
1cab57eb JB |
785 | if (print_wifi_wmm_param(data + 1, len - 1)) |
786 | return; | |
6ff0c93a MH |
787 | break; |
788 | default: | |
83b4934c | 789 | printf(" type %d:", data[0]); |
6ff0c93a MH |
790 | break; |
791 | } | |
792 | ||
1cab57eb JB |
793 | for(i = 1; i < len; i++) |
794 | printf(" %.02x", data[i]); | |
6ff0c93a MH |
795 | printf("\n"); |
796 | } | |
797 | ||
a6816965 JM |
798 | static const char * wifi_wps_dev_passwd_id(uint16_t id) |
799 | { | |
800 | switch (id) { | |
801 | case 0: | |
802 | return "Default (PIN)"; | |
803 | case 1: | |
804 | return "User-specified"; | |
805 | case 2: | |
806 | return "Machine-specified"; | |
807 | case 3: | |
808 | return "Rekey"; | |
809 | case 4: | |
810 | return "PushButton"; | |
811 | case 5: | |
812 | return "Registrar-specified"; | |
813 | default: | |
814 | return "??"; | |
815 | } | |
816 | } | |
817 | ||
83b4934c | 818 | static void print_wifi_wps(const uint8_t type, uint8_t len, const uint8_t *data) |
4673a894 JB |
819 | { |
820 | bool first = true; | |
821 | __u16 subtype, sublen; | |
822 | ||
4673a894 JB |
823 | while (len >= 4) { |
824 | subtype = (data[0] << 8) + data[1]; | |
825 | sublen = (data[2] << 8) + data[3]; | |
826 | if (sublen > len) | |
827 | break; | |
828 | ||
829 | switch (subtype) { | |
830 | case 0x104a: | |
831 | tab_on_first(&first); | |
dffc6750 | 832 | printf("\t * Version: %d.%d\n", data[4] >> 4, data[4] & 0xF); |
4673a894 JB |
833 | break; |
834 | case 0x1011: | |
835 | tab_on_first(&first); | |
836 | printf("\t * Device name: %.*s\n", sublen, data + 4); | |
837 | break; | |
a6816965 JM |
838 | case 0x1012: { |
839 | uint16_t id; | |
840 | tab_on_first(&first); | |
841 | if (sublen != 2) { | |
842 | printf("\t * Device Password ID: (invalid " | |
843 | "length %d)\n", sublen); | |
844 | break; | |
845 | } | |
846 | id = data[4] << 8 | data[5]; | |
847 | printf("\t * Device Password ID: %u (%s)\n", | |
848 | id, wifi_wps_dev_passwd_id(id)); | |
849 | break; | |
850 | } | |
4673a894 JB |
851 | case 0x1021: |
852 | tab_on_first(&first); | |
853 | printf("\t * Manufacturer: %.*s\n", sublen, data + 4); | |
854 | break; | |
855 | case 0x1023: | |
856 | tab_on_first(&first); | |
857 | printf("\t * Model: %.*s\n", sublen, data + 4); | |
858 | break; | |
a6816965 JM |
859 | case 0x1024: |
860 | tab_on_first(&first); | |
861 | printf("\t * Model Number: %.*s\n", sublen, data + 4); | |
862 | break; | |
863 | case 0x103b: { | |
864 | __u8 val = data[4]; | |
865 | tab_on_first(&first); | |
866 | printf("\t * Response Type: %d%s\n", | |
867 | val, val == 3 ? " (AP)" : ""); | |
868 | break; | |
869 | } | |
870 | case 0x103c: { | |
871 | __u8 val = data[4]; | |
872 | tab_on_first(&first); | |
873 | printf("\t * RF Bands: 0x%x\n", val); | |
874 | break; | |
875 | } | |
876 | case 0x1041: { | |
877 | __u8 val = data[4]; | |
878 | tab_on_first(&first); | |
879 | printf("\t * Selected Registrar: 0x%x\n", val); | |
880 | break; | |
881 | } | |
882 | case 0x1042: | |
883 | tab_on_first(&first); | |
884 | printf("\t * Serial Number: %.*s\n", sublen, data + 4); | |
885 | break; | |
886 | case 0x1044: { | |
887 | __u8 val = data[4]; | |
888 | tab_on_first(&first); | |
889 | printf("\t * Wi-Fi Protected Setup State: %d%s%s\n", | |
890 | val, | |
891 | val == 1 ? " (Unconfigured)" : "", | |
892 | val == 2 ? " (Configured)" : ""); | |
893 | break; | |
894 | } | |
09fe09dc JB |
895 | case 0x1047: |
896 | tab_on_first(&first); | |
897 | printf("\t * UUID: "); | |
898 | if (sublen != 16) { | |
899 | printf("(invalid, length=%d)\n", sublen); | |
900 | break; | |
901 | } | |
902 | printf("%02x%02x%02x%02x-%02x%02x-%02x%02x-" | |
903 | "%02x%02x-%02x%02x%02x%02x%02x%02x\n", | |
904 | data[4], data[5], data[6], data[7], | |
905 | data[8], data[9], data[10], data[11], | |
906 | data[12], data[13], data[14], data[15], | |
907 | data[16], data[17], data[18], data[19]); | |
908 | break; | |
a6816965 JM |
909 | case 0x1054: { |
910 | tab_on_first(&first); | |
911 | if (sublen != 8) { | |
912 | printf("\t * Primary Device Type: (invalid " | |
913 | "length %d)\n", sublen); | |
914 | break; | |
915 | } | |
916 | printf("\t * Primary Device Type: " | |
917 | "%u-%02x%02x%02x%02x-%u\n", | |
918 | data[4] << 8 | data[5], | |
919 | data[6], data[7], data[8], data[9], | |
920 | data[10] << 8 | data[11]); | |
921 | break; | |
922 | } | |
7ee5a865 | 923 | case 0x1057: { |
fe31a22e | 924 | __u8 val = data[4]; |
7ee5a865 | 925 | tab_on_first(&first); |
fe31a22e | 926 | printf("\t * AP setup locked: 0x%.2x\n", val); |
7ee5a865 JB |
927 | break; |
928 | } | |
a6816965 JM |
929 | case 0x1008: |
930 | case 0x1053: { | |
4673a894 JB |
931 | __u16 meth = (data[4] << 8) + data[5]; |
932 | bool comma = false; | |
933 | tab_on_first(&first); | |
a6816965 JM |
934 | printf("\t * %sConfig methods:", |
935 | subtype == 0x1053 ? "Selected Registrar ": ""); | |
4673a894 JB |
936 | #define T(bit, name) do { \ |
937 | if (meth & (1<<bit)) { \ | |
938 | if (comma) \ | |
939 | printf(","); \ | |
940 | comma = true; \ | |
941 | printf(" " name); \ | |
942 | } } while (0) | |
943 | T(0, "USB"); | |
944 | T(1, "Ethernet"); | |
945 | T(2, "Label"); | |
946 | T(3, "Display"); | |
947 | T(4, "Ext. NFC"); | |
948 | T(5, "Int. NFC"); | |
949 | T(6, "NFC Intf."); | |
950 | T(7, "PBC"); | |
951 | T(8, "Keypad"); | |
952 | printf("\n"); | |
953 | break; | |
954 | #undef T | |
955 | } | |
2650d46b JB |
956 | default: { |
957 | const __u8 *subdata = data + 4; | |
958 | __u16 tmplen = sublen; | |
959 | ||
960 | tab_on_first(&first); | |
961 | printf("\t * Unknown TLV (%#.4x, %d bytes):", | |
962 | subtype, tmplen); | |
963 | while (tmplen) { | |
964 | printf(" %.2x", *subdata); | |
965 | subdata++; | |
966 | tmplen--; | |
967 | } | |
968 | printf("\n"); | |
4673a894 JB |
969 | break; |
970 | } | |
2650d46b | 971 | } |
4673a894 JB |
972 | |
973 | data += sublen + 4; | |
974 | len -= sublen + 4; | |
975 | } | |
976 | ||
977 | if (len != 0) { | |
978 | printf("\t\t * bogus tail data (%d):", len); | |
979 | while (len) { | |
980 | printf(" %.2x", *data); | |
981 | data++; | |
982 | len--; | |
983 | } | |
984 | printf("\n"); | |
985 | } | |
986 | } | |
987 | ||
83b4934c | 988 | static const struct ie_print wifiprinters[] = { |
febeb0c0 JB |
989 | [1] = { "WPA", print_wifi_wpa, 2, 255, BIT(PRINT_SCAN), }, |
990 | [2] = { "WMM", print_wifi_wmm, 1, 255, BIT(PRINT_SCAN), }, | |
991 | [4] = { "WPS", print_wifi_wps, 0, 255, BIT(PRINT_SCAN), }, | |
4673a894 JB |
992 | }; |
993 | ||
9a22374a JB |
994 | static inline void print_p2p(const uint8_t type, uint8_t len, const uint8_t *data) |
995 | { | |
996 | bool first = true; | |
997 | __u8 subtype; | |
998 | __u16 sublen; | |
999 | ||
1000 | while (len >= 3) { | |
1001 | subtype = data[0]; | |
1002 | sublen = (data[2] << 8) + data[1]; | |
1003 | ||
1004 | if (sublen > len - 3) | |
1005 | break; | |
1006 | ||
1007 | switch (subtype) { | |
1008 | case 0x02: /* capability */ | |
1009 | tab_on_first(&first); | |
1010 | if (sublen < 2) { | |
1011 | printf("\t * malformed capability\n"); | |
1012 | break; | |
1013 | } | |
1014 | printf("\t * Group capa: 0x%.2x, Device capa: 0x%.2x\n", | |
1015 | data[3], data[4]); | |
1016 | break; | |
1017 | case 0x0d: /* device info */ | |
1018 | if (sublen < 6 + 2 + 8 + 1) { | |
1019 | printf("\t * malformed device info\n"); | |
1020 | break; | |
1021 | } | |
1022 | /* fall through for now */ | |
1023 | case 0x00: /* status */ | |
1024 | case 0x01: /* minor reason */ | |
1025 | case 0x03: /* device ID */ | |
1026 | case 0x04: /* GO intent */ | |
1027 | case 0x05: /* configuration timeout */ | |
1028 | case 0x06: /* listen channel */ | |
1029 | case 0x07: /* group BSSID */ | |
1030 | case 0x08: /* ext listen timing */ | |
1031 | case 0x09: /* intended interface address */ | |
1032 | case 0x0a: /* manageability */ | |
1033 | case 0x0b: /* channel list */ | |
1034 | case 0x0c: /* NoA */ | |
1035 | case 0x0e: /* group info */ | |
1036 | case 0x0f: /* group ID */ | |
1037 | case 0x10: /* interface */ | |
1038 | case 0x11: /* operating channel */ | |
1039 | case 0x12: /* invitation flags */ | |
1040 | case 0xdd: /* vendor specific */ | |
1041 | default: { | |
1042 | const __u8 *subdata = data + 4; | |
1043 | __u16 tmplen = sublen; | |
1044 | ||
1045 | tab_on_first(&first); | |
1046 | printf("\t * Unknown TLV (%#.2x, %d bytes):", | |
1047 | subtype, tmplen); | |
1048 | while (tmplen) { | |
1049 | printf(" %.2x", *subdata); | |
1050 | subdata++; | |
1051 | tmplen--; | |
1052 | } | |
1053 | printf("\n"); | |
1054 | break; | |
1055 | } | |
1056 | } | |
1057 | ||
1058 | data += sublen + 3; | |
1059 | len -= sublen + 3; | |
1060 | } | |
1061 | ||
1062 | if (len != 0) { | |
1063 | tab_on_first(&first); | |
1064 | printf("\t * bogus tail data (%d):", len); | |
1065 | while (len) { | |
1066 | printf(" %.2x", *data); | |
1067 | data++; | |
1068 | len--; | |
1069 | } | |
1070 | printf("\n"); | |
1071 | } | |
1072 | } | |
1073 | ||
1074 | static const struct ie_print wfa_printers[] = { | |
1075 | [9] = { "P2P", print_p2p, 2, 255, BIT(PRINT_SCAN), }, | |
1076 | }; | |
1077 | ||
764fe753 | 1078 | static void print_vendor(unsigned char len, unsigned char *data, |
febeb0c0 | 1079 | bool unknown, enum print_ie_type ptype) |
3563f4c5 JB |
1080 | { |
1081 | int i; | |
1082 | ||
fbf80af5 | 1083 | if (len < 3) { |
4673a894 | 1084 | printf("\tVendor specific: <too short> data:"); |
fbf80af5 JB |
1085 | for(i = 0; i < len; i++) |
1086 | printf(" %.02x", data[i]); | |
1087 | printf("\n"); | |
1088 | return; | |
1089 | } | |
1090 | ||
3bd60ef1 | 1091 | if (len >= 4 && memcmp(data, ms_oui, 3) == 0) { |
febeb0c0 JB |
1092 | if (data[3] < ARRAY_SIZE(wifiprinters) && |
1093 | wifiprinters[data[3]].name && | |
1094 | wifiprinters[data[3]].flags & BIT(ptype)) { | |
83b4934c JB |
1095 | print_ie(&wifiprinters[data[3]], data[3], len - 4, data + 4); |
1096 | return; | |
1097 | } | |
febeb0c0 | 1098 | if (!unknown) |
4673a894 | 1099 | return; |
3bd60ef1 | 1100 | printf("\tMS/WiFi %#.2x, data:", data[3]); |
4673a894 JB |
1101 | for(i = 0; i < len - 4; i++) |
1102 | printf(" %.02x", data[i + 4]); | |
1103 | printf("\n"); | |
1104 | return; | |
1105 | } | |
1106 | ||
9a22374a JB |
1107 | if (len >= 4 && memcmp(data, wfa_oui, 3) == 0) { |
1108 | if (data[3] < ARRAY_SIZE(wfa_printers) && | |
1109 | wfa_printers[data[3]].name && | |
1110 | wfa_printers[data[3]].flags & BIT(ptype)) { | |
1111 | print_ie(&wfa_printers[data[3]], data[3], len - 4, data + 4); | |
1112 | return; | |
1113 | } | |
1114 | if (!unknown) | |
1115 | return; | |
1116 | printf("\tWFA %#.2x, data:", data[3]); | |
1117 | for(i = 0; i < len - 4; i++) | |
1118 | printf(" %.02x", data[i + 4]); | |
1119 | printf("\n"); | |
1120 | return; | |
1121 | } | |
1122 | ||
febeb0c0 | 1123 | if (!unknown) |
764fe753 JB |
1124 | return; |
1125 | ||
fbf80af5 | 1126 | printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:", |
3563f4c5 | 1127 | data[0], data[1], data[2]); |
fbf80af5 JB |
1128 | for (i = 3; i < len; i++) |
1129 | printf(" %.2x", data[i]); | |
3563f4c5 JB |
1130 | printf("\n"); |
1131 | } | |
1132 | ||
febeb0c0 JB |
1133 | void print_ies(unsigned char *ie, int ielen, bool unknown, |
1134 | enum print_ie_type ptype) | |
3563f4c5 JB |
1135 | { |
1136 | while (ielen >= 2 && ielen >= ie[1]) { | |
febeb0c0 JB |
1137 | if (ie[0] < ARRAY_SIZE(ieprinters) && |
1138 | ieprinters[ie[0]].name && | |
1139 | ieprinters[ie[0]].flags & BIT(ptype)) { | |
83b4934c | 1140 | print_ie(&ieprinters[ie[0]], ie[0], ie[1], ie + 2); |
764fe753 | 1141 | } else if (ie[0] == 221 /* vendor */) { |
febeb0c0 JB |
1142 | print_vendor(ie[1], ie + 2, unknown, ptype); |
1143 | } else if (unknown) { | |
3563f4c5 JB |
1144 | int i; |
1145 | ||
8086b700 | 1146 | printf("\tUnknown IE (%d):", ie[0]); |
3563f4c5 | 1147 | for (i=0; i<ie[1]; i++) |
8086b700 | 1148 | printf(" %.2x", ie[2+i]); |
3563f4c5 JB |
1149 | printf("\n"); |
1150 | } | |
1151 | ielen -= ie[1] + 2; | |
1152 | ie += ie[1] + 2; | |
1153 | } | |
1154 | } | |
1155 | ||
1156 | static int print_bss_handler(struct nl_msg *msg, void *arg) | |
1157 | { | |
1158 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
1159 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
1160 | struct nlattr *bss[NL80211_BSS_MAX + 1]; | |
1161 | char mac_addr[20], dev[20]; | |
1162 | static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = { | |
1163 | [NL80211_BSS_TSF] = { .type = NLA_U64 }, | |
1164 | [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 }, | |
1165 | [NL80211_BSS_BSSID] = { }, | |
1166 | [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 }, | |
1167 | [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 }, | |
1168 | [NL80211_BSS_INFORMATION_ELEMENTS] = { }, | |
f2e17e1f JB |
1169 | [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 }, |
1170 | [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 }, | |
a56117a6 | 1171 | [NL80211_BSS_STATUS] = { .type = NLA_U32 }, |
c04a78df | 1172 | [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 }, |
575280cc | 1173 | [NL80211_BSS_BEACON_IES] = { }, |
3563f4c5 | 1174 | }; |
febeb0c0 | 1175 | struct scan_params *params = arg; |
1c5bcd9c | 1176 | int show = params->show_both_ie_sets ? 2 : 1; |
3563f4c5 JB |
1177 | |
1178 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
1179 | genlmsg_attrlen(gnlh, 0), NULL); | |
1180 | ||
1181 | if (!tb[NL80211_ATTR_BSS]) { | |
5fe70c0e | 1182 | fprintf(stderr, "bss info missing!\n"); |
3563f4c5 JB |
1183 | return NL_SKIP; |
1184 | } | |
1185 | if (nla_parse_nested(bss, NL80211_BSS_MAX, | |
1186 | tb[NL80211_ATTR_BSS], | |
1187 | bss_policy)) { | |
5fe70c0e | 1188 | fprintf(stderr, "failed to parse nested attributes!\n"); |
3563f4c5 JB |
1189 | return NL_SKIP; |
1190 | } | |
1191 | ||
1192 | if (!bss[NL80211_BSS_BSSID]) | |
1193 | return NL_SKIP; | |
1194 | ||
1195 | mac_addr_n2a(mac_addr, nla_data(bss[NL80211_BSS_BSSID])); | |
1196 | if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev); | |
a56117a6 JB |
1197 | printf("BSS %s (on %s)", mac_addr, dev); |
1198 | ||
1199 | if (bss[NL80211_BSS_STATUS]) { | |
1200 | switch (nla_get_u32(bss[NL80211_BSS_STATUS])) { | |
1201 | case NL80211_BSS_STATUS_AUTHENTICATED: | |
1202 | printf(" -- authenticated"); | |
1203 | break; | |
1204 | case NL80211_BSS_STATUS_ASSOCIATED: | |
1205 | printf(" -- associated"); | |
1206 | break; | |
1207 | case NL80211_BSS_STATUS_IBSS_JOINED: | |
1208 | printf(" -- joined"); | |
1209 | break; | |
1210 | default: | |
1211 | printf(" -- unknown status: %d", | |
1212 | nla_get_u32(bss[NL80211_BSS_STATUS])); | |
1213 | break; | |
1214 | } | |
1215 | } | |
1216 | printf("\n"); | |
3563f4c5 | 1217 | |
e7109a8a JB |
1218 | if (bss[NL80211_BSS_TSF]) { |
1219 | unsigned long long tsf; | |
1220 | tsf = (unsigned long long)nla_get_u64(bss[NL80211_BSS_TSF]); | |
1221 | printf("\tTSF: %llu usec (%llud, %.2lld:%.2llu:%.2llu)\n", | |
1222 | tsf, tsf/1000/1000/60/60/24, (tsf/1000/1000/60/60) % 24, | |
1223 | (tsf/1000/1000/60) % 60, (tsf/1000/1000) % 60); | |
1224 | } | |
3563f4c5 JB |
1225 | if (bss[NL80211_BSS_FREQUENCY]) |
1226 | printf("\tfreq: %d\n", | |
1227 | nla_get_u32(bss[NL80211_BSS_FREQUENCY])); | |
1228 | if (bss[NL80211_BSS_BEACON_INTERVAL]) | |
1229 | printf("\tbeacon interval: %d\n", | |
1230 | nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL])); | |
92a04ecd MH |
1231 | if (bss[NL80211_BSS_CAPABILITY]) { |
1232 | __u16 capa = nla_get_u16(bss[NL80211_BSS_CAPABILITY]); | |
1233 | printf("\tcapability:"); | |
1234 | if (capa & WLAN_CAPABILITY_ESS) | |
1235 | printf(" ESS"); | |
1236 | if (capa & WLAN_CAPABILITY_IBSS) | |
1237 | printf(" IBSS"); | |
1238 | if (capa & WLAN_CAPABILITY_PRIVACY) | |
1239 | printf(" Privacy"); | |
1240 | if (capa & WLAN_CAPABILITY_SHORT_PREAMBLE) | |
1241 | printf(" ShortPreamble"); | |
1242 | if (capa & WLAN_CAPABILITY_PBCC) | |
1243 | printf(" PBCC"); | |
1244 | if (capa & WLAN_CAPABILITY_CHANNEL_AGILITY) | |
1245 | printf(" ChannelAgility"); | |
1246 | if (capa & WLAN_CAPABILITY_SPECTRUM_MGMT) | |
1247 | printf(" SpectrumMgmt"); | |
1248 | if (capa & WLAN_CAPABILITY_QOS) | |
1249 | printf(" QoS"); | |
1250 | if (capa & WLAN_CAPABILITY_SHORT_SLOT_TIME) | |
1251 | printf(" ShortSlotTime"); | |
1252 | if (capa & WLAN_CAPABILITY_APSD) | |
1253 | printf(" APSD"); | |
1254 | if (capa & WLAN_CAPABILITY_DSSS_OFDM) | |
1255 | printf(" DSSS-OFDM"); | |
1256 | printf(" (0x%.4x)\n", capa); | |
1257 | } | |
f2e17e1f JB |
1258 | if (bss[NL80211_BSS_SIGNAL_MBM]) { |
1259 | int s = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]); | |
1260 | printf("\tsignal: %d.%.2d dBm\n", s/100, s%100); | |
1261 | } | |
1262 | if (bss[NL80211_BSS_SIGNAL_UNSPEC]) { | |
1263 | unsigned char s = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]); | |
1264 | printf("\tsignal: %d/100\n", s); | |
1265 | } | |
c04a78df HS |
1266 | if (bss[NL80211_BSS_SEEN_MS_AGO]) { |
1267 | int age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]); | |
1268 | printf("\tlast seen: %d ms ago\n", age); | |
1269 | } | |
c551449a | 1270 | |
1c5bcd9c | 1271 | if (bss[NL80211_BSS_INFORMATION_ELEMENTS] && show--) { |
575280cc JM |
1272 | if (bss[NL80211_BSS_BEACON_IES]) |
1273 | printf("\tInformation elements from Probe Response " | |
1274 | "frame:\n"); | |
3563f4c5 | 1275 | print_ies(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]), |
764fe753 | 1276 | nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]), |
febeb0c0 | 1277 | params->unknown, params->type); |
575280cc | 1278 | } |
1c5bcd9c | 1279 | if (bss[NL80211_BSS_BEACON_IES] && show--) { |
575280cc JM |
1280 | printf("\tInformation elements from Beacon frame:\n"); |
1281 | print_ies(nla_data(bss[NL80211_BSS_BEACON_IES]), | |
1282 | nla_len(bss[NL80211_BSS_BEACON_IES]), | |
1283 | params->unknown, params->type); | |
1284 | } | |
3563f4c5 JB |
1285 | |
1286 | return NL_SKIP; | |
1287 | } | |
1288 | ||
764fe753 | 1289 | static struct scan_params scan_params; |
3563f4c5 | 1290 | |
7c37a24d JB |
1291 | static int handle_scan_dump(struct nl80211_state *state, |
1292 | struct nl_cb *cb, | |
3563f4c5 | 1293 | struct nl_msg *msg, |
05514f95 JB |
1294 | int argc, char **argv, |
1295 | enum id_input id) | |
3563f4c5 | 1296 | { |
764fe753 JB |
1297 | if (argc > 1) |
1298 | return 1; | |
1299 | ||
1c5bcd9c JB |
1300 | memset(&scan_params, 0, sizeof(scan_params)); |
1301 | ||
764fe753 JB |
1302 | if (argc == 1 && !strcmp(argv[0], "-u")) |
1303 | scan_params.unknown = true; | |
575280cc JM |
1304 | else if (argc == 1 && !strcmp(argv[0], "-b")) |
1305 | scan_params.show_both_ie_sets = true; | |
764fe753 | 1306 | |
febeb0c0 JB |
1307 | scan_params.type = PRINT_SCAN; |
1308 | ||
764fe753 JB |
1309 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_bss_handler, |
1310 | &scan_params); | |
3563f4c5 JB |
1311 | return 0; |
1312 | } | |
a5fe4ef2 JB |
1313 | |
1314 | static int handle_scan_combined(struct nl80211_state *state, | |
1315 | struct nl_cb *cb, | |
1316 | struct nl_msg *msg, | |
05514f95 JB |
1317 | int argc, char **argv, |
1318 | enum id_input id) | |
a5fe4ef2 | 1319 | { |
559a1713 | 1320 | char **trig_argv; |
a5fe4ef2 JB |
1321 | static char *dump_argv[] = { |
1322 | NULL, | |
1323 | "scan", | |
1324 | "dump", | |
92649eab | 1325 | NULL, |
a5fe4ef2 JB |
1326 | }; |
1327 | static const __u32 cmds[] = { | |
1328 | NL80211_CMD_NEW_SCAN_RESULTS, | |
1329 | NL80211_CMD_SCAN_ABORTED, | |
1330 | }; | |
559a1713 | 1331 | int trig_argc, dump_argc, err; |
a5fe4ef2 | 1332 | |
559a1713 JB |
1333 | if (argc >= 3 && !strcmp(argv[2], "-u")) { |
1334 | dump_argc = 4; | |
1335 | dump_argv[3] = "-u"; | |
575280cc JM |
1336 | } else if (argc >= 3 && !strcmp(argv[2], "-b")) { |
1337 | dump_argc = 4; | |
1338 | dump_argv[3] = "-b"; | |
559a1713 JB |
1339 | } else |
1340 | dump_argc = 3; | |
1341 | ||
1342 | trig_argc = 3 + (argc - 2) + (3 - dump_argc); | |
1343 | trig_argv = calloc(trig_argc, sizeof(*trig_argv)); | |
1344 | if (!trig_argv) | |
1345 | return -ENOMEM; | |
a5fe4ef2 | 1346 | trig_argv[0] = argv[0]; |
559a1713 JB |
1347 | trig_argv[1] = "scan"; |
1348 | trig_argv[2] = "trigger"; | |
1349 | int i; | |
1350 | for (i = 0; i < argc - 2 - (dump_argc - 3); i++) | |
1351 | trig_argv[i + 3] = argv[i + 2 + (dump_argc - 3)]; | |
75f4204c | 1352 | err = handle_cmd(state, id, trig_argc, trig_argv); |
559a1713 | 1353 | free(trig_argv); |
a5fe4ef2 JB |
1354 | if (err) |
1355 | return err; | |
1356 | ||
61725dbe JB |
1357 | /* |
1358 | * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION | |
1359 | * | |
1360 | * This code has a bug, which requires creating a separate | |
1361 | * nl80211 socket to fix: | |
1362 | * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or | |
1363 | * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel | |
1364 | * before (!) we listen to it, because we only start listening | |
1365 | * after we send our scan request. | |
1366 | * | |
1367 | * Doing it the other way around has a race condition as well, | |
1368 | * if you first open the events socket you may get a notification | |
1369 | * for a previous scan. | |
1370 | * | |
1371 | * The only proper way to fix this would be to listen to events | |
1372 | * before sending the command, and for the kernel to send the | |
1373 | * scan request along with the event, so that you can match up | |
1374 | * whether the scan you requested was finished or aborted (this | |
1375 | * may result in processing a scan that another application | |
1376 | * requested, but that doesn't seem to be a problem). | |
1377 | * | |
1378 | * Alas, the kernel doesn't do that (yet). | |
1379 | */ | |
1380 | ||
a5fe4ef2 JB |
1381 | if (listen_events(state, ARRAY_SIZE(cmds), cmds) == |
1382 | NL80211_CMD_SCAN_ABORTED) { | |
1383 | printf("scan aborted!\n"); | |
1384 | return 0; | |
1385 | } | |
1386 | ||
1387 | dump_argv[0] = argv[0]; | |
75f4204c | 1388 | return handle_cmd(state, id, dump_argc, dump_argv); |
a5fe4ef2 | 1389 | } |
ced94d5f | 1390 | TOPLEVEL(scan, "[-u] [freq <freq>*] [ies <hex as 00:11:..>] [lowpri,flush,ap-force] [ssid <ssid>*|passive]", 0, 0, |
6ca98d28 JB |
1391 | CIB_NETDEV, handle_scan_combined, |
1392 | "Scan on the given frequencies and probe for the given SSIDs\n" | |
1393 | "(or wildcard if not given) unless passive scanning is requested.\n" | |
64797a7f JB |
1394 | "If -u is specified print unknown data in the scan results.\n" |
1395 | "Specified (vendor) IEs must be well-formed."); | |
4698bfc2 JB |
1396 | COMMAND(scan, dump, "[-u]", |
1397 | NL80211_CMD_GET_SCAN, NLM_F_DUMP, CIB_NETDEV, handle_scan_dump, | |
1398 | "Dump the current scan results. If -u is specified, print unknown\n" | |
1399 | "data in scan results."); | |
ced94d5f | 1400 | COMMAND(scan, trigger, "[freq <freq>*] [ies <hex as 00:11:..>] [lowpri,flush,ap-force] [ssid <ssid>*|passive]", |
4698bfc2 JB |
1401 | NL80211_CMD_TRIGGER_SCAN, 0, CIB_NETDEV, handle_scan, |
1402 | "Trigger a scan on the given frequencies with probing for the given\n" | |
1403 | "SSIDs (or wildcard if not given) unless passive scanning is requested."); |