]>
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; | |
35 | }; | |
36 | ||
7c37a24d JB |
37 | static int handle_scan(struct nl80211_state *state, |
38 | struct nl_cb *cb, | |
3563f4c5 JB |
39 | struct nl_msg *msg, |
40 | int argc, char **argv) | |
41 | { | |
559a1713 JB |
42 | struct nl_msg *ssids = NULL, *freqs = NULL; |
43 | char *eptr; | |
3563f4c5 | 44 | int err = -ENOBUFS; |
559a1713 JB |
45 | int i; |
46 | enum { | |
47 | NONE, | |
48 | FREQ, | |
49 | SSID, | |
50 | DONE, | |
51 | } parse = NONE; | |
52 | int freq; | |
53 | bool passive = false, have_ssids = false, have_freqs = false; | |
3563f4c5 JB |
54 | |
55 | ssids = nlmsg_alloc(); | |
56 | if (!ssids) | |
57 | return -ENOMEM; | |
559a1713 JB |
58 | |
59 | freqs = nlmsg_alloc(); | |
60 | if (!freqs) { | |
61 | nlmsg_free(ssids); | |
62 | return -ENOMEM; | |
63 | } | |
64 | ||
65 | for (i = 0; i < argc; i++) { | |
1ddf11eb | 66 | if (parse == NONE && strcmp(argv[i], "freq") == 0) { |
559a1713 JB |
67 | parse = FREQ; |
68 | have_freqs = true; | |
69 | continue; | |
1ddf11eb | 70 | } else if (parse < SSID && strcmp(argv[i], "ssid") == 0) { |
559a1713 JB |
71 | parse = SSID; |
72 | have_ssids = true; | |
73 | continue; | |
1ddf11eb | 74 | } else if (parse < SSID && strcmp(argv[i], "passive") == 0) { |
559a1713 JB |
75 | parse = DONE; |
76 | passive = true; | |
77 | continue; | |
78 | } | |
79 | ||
80 | switch (parse) { | |
81 | case NONE: | |
82 | case DONE: | |
83 | return 1; | |
84 | case FREQ: | |
85 | freq = strtoul(argv[i], &eptr, 10); | |
86 | if (eptr != argv[i] + strlen(argv[i])) | |
87 | return 1; | |
88 | NLA_PUT_U32(freqs, i, freq); | |
89 | break; | |
90 | case SSID: | |
91 | NLA_PUT(ssids, i, strlen(argv[i]), argv[i]); | |
92 | break; | |
93 | } | |
94 | } | |
95 | ||
96 | if (!have_ssids) | |
97 | NLA_PUT(ssids, 1, 0, ""); | |
98 | if (!passive) | |
99 | nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids); | |
100 | ||
101 | if (have_freqs) | |
102 | nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs); | |
3563f4c5 JB |
103 | |
104 | err = 0; | |
105 | nla_put_failure: | |
106 | nlmsg_free(ssids); | |
559a1713 | 107 | nlmsg_free(freqs); |
3563f4c5 JB |
108 | return err; |
109 | } | |
559a1713 | 110 | COMMAND(scan, trigger, "[freq <freq>*] [ssid <ssid>*|passive]", |
3563f4c5 JB |
111 | NL80211_CMD_TRIGGER_SCAN, 0, CIB_NETDEV, handle_scan); |
112 | ||
857d966e MH |
113 | static void tab_on_first(bool *first) |
114 | { | |
115 | if (!*first) | |
116 | printf("\t"); | |
117 | else | |
118 | *first = false; | |
119 | } | |
120 | ||
83b4934c | 121 | static void print_ssid(const uint8_t type, uint8_t len, const uint8_t *data) |
3563f4c5 JB |
122 | { |
123 | int i; | |
83b4934c JB |
124 | |
125 | printf(" "); | |
126 | ||
127 | for (i = 0; i < len; i++) { | |
3563f4c5 JB |
128 | if (isprint(data[i])) |
129 | printf("%c", data[i]); | |
130 | else | |
131 | printf("\\x%.2x", data[i]); | |
132 | } | |
133 | printf("\n"); | |
134 | } | |
135 | ||
83b4934c | 136 | static void print_supprates(const uint8_t type, uint8_t len, const uint8_t *data) |
3563f4c5 JB |
137 | { |
138 | int i; | |
139 | ||
83b4934c | 140 | printf(" "); |
3563f4c5 | 141 | |
83b4934c | 142 | for (i = 0; i < len; i++) { |
3563f4c5 JB |
143 | int r = data[i] & 0x7f; |
144 | printf("%d.%d%s ", r/2, 5*(r&1), data[i] & 0x80 ? "*":""); | |
145 | } | |
146 | printf("\n"); | |
147 | } | |
148 | ||
83b4934c | 149 | static void print_ds(const uint8_t type, uint8_t len, const uint8_t *data) |
3563f4c5 | 150 | { |
83b4934c | 151 | printf(" channel %d\n", data[0]); |
3563f4c5 JB |
152 | } |
153 | ||
83b4934c | 154 | static void print_country(const uint8_t type, uint8_t len, const uint8_t *data) |
b7e8fa37 MH |
155 | { |
156 | int i; | |
157 | ||
83b4934c | 158 | printf(" %.*s", 2, data); |
b7e8fa37 MH |
159 | switch (data[2]) { |
160 | case 'I': | |
161 | printf(" (indoor)"); | |
162 | break; | |
163 | case 'O': | |
164 | printf(" (outdoor)"); | |
165 | break; | |
b6c0d634 JB |
166 | case ' ': |
167 | printf(" (in/outdoor)"); | |
168 | break; | |
169 | default: | |
170 | printf(" (invalid environment)"); | |
171 | break; | |
b7e8fa37 MH |
172 | } |
173 | printf(", data:"); | |
174 | for(i=0; i<len-3; i++) | |
175 | printf(" %.02x", data[i + 3]); | |
176 | printf("\n"); | |
177 | } | |
178 | ||
d1563a1b MH |
179 | static void print_powerconstraint(const uint8_t type, uint8_t len, const uint8_t *data) |
180 | { | |
181 | printf(" %d dB\n", data[0]); | |
182 | } | |
183 | ||
83b4934c | 184 | static void print_erp(const uint8_t type, uint8_t len, const uint8_t *data) |
fc4d1484 MH |
185 | { |
186 | if (data[0] == 0x00) | |
83b4934c | 187 | printf(" <no flags>"); |
fc4d1484 MH |
188 | if (data[0] & 0x01) |
189 | printf(" NonERP_Present"); | |
190 | if (data[0] & 0x02) | |
191 | printf(" Use_Protection"); | |
192 | if (data[0] & 0x04) | |
193 | printf(" Barker_Preamble_Mode"); | |
194 | printf("\n"); | |
195 | } | |
196 | ||
83b4934c | 197 | static void print_cipher(const uint8_t *data) |
857d966e MH |
198 | { |
199 | if (memcmp(data, wifi_oui, 3) == 0) { | |
200 | switch (data[3]) { | |
510e0e2f | 201 | case 0: |
857d966e MH |
202 | printf("Use group cipher suite"); |
203 | break; | |
510e0e2f | 204 | case 1: |
857d966e MH |
205 | printf("WEP-40"); |
206 | break; | |
510e0e2f | 207 | case 2: |
857d966e MH |
208 | printf("TKIP"); |
209 | break; | |
510e0e2f | 210 | case 4: |
857d966e MH |
211 | printf("CCMP"); |
212 | break; | |
510e0e2f | 213 | case 5: |
857d966e MH |
214 | printf("WEP-104"); |
215 | break; | |
216 | default: | |
332769c6 | 217 | printf("%.02x-%.02x-%.02x:%d", |
5594fd23 | 218 | data[0], data[1] ,data[2], data[3]); |
857d966e MH |
219 | break; |
220 | } | |
221 | } else if (memcmp(data, ieee80211_oui, 3) == 0) { | |
222 | switch (data[3]) { | |
510e0e2f | 223 | case 0: |
857d966e MH |
224 | printf("Use group cipher suite"); |
225 | break; | |
510e0e2f | 226 | case 1: |
857d966e MH |
227 | printf("WEP-40"); |
228 | break; | |
510e0e2f | 229 | case 2: |
857d966e MH |
230 | printf("TKIP"); |
231 | break; | |
510e0e2f | 232 | case 4: |
857d966e MH |
233 | printf("CCMP"); |
234 | break; | |
510e0e2f | 235 | case 5: |
857d966e MH |
236 | printf("WEP-104"); |
237 | break; | |
510e0e2f | 238 | case 6: |
857d966e MH |
239 | printf("AES-128-CMAC"); |
240 | break; | |
241 | default: | |
332769c6 | 242 | printf("%.02x-%.02x-%.02x:%d", |
5594fd23 | 243 | data[0], data[1] ,data[2], data[3]); |
857d966e MH |
244 | break; |
245 | } | |
246 | } else | |
332769c6 | 247 | printf("%.02x-%.02x-%.02x:%d", |
5594fd23 | 248 | data[0], data[1] ,data[2], data[3]); |
857d966e MH |
249 | } |
250 | ||
83b4934c | 251 | static void print_auth(const uint8_t *data) |
857d966e MH |
252 | { |
253 | if (memcmp(data, wifi_oui, 3) == 0) { | |
254 | switch (data[3]) { | |
510e0e2f | 255 | case 1: |
857d966e MH |
256 | printf("IEEE 802.1X"); |
257 | break; | |
510e0e2f | 258 | case 2: |
857d966e MH |
259 | printf("PSK"); |
260 | break; | |
261 | default: | |
332769c6 | 262 | printf("%.02x-%.02x-%.02x:%d", |
5594fd23 | 263 | data[0], data[1] ,data[2], data[3]); |
857d966e MH |
264 | break; |
265 | } | |
266 | } else if (memcmp(data, ieee80211_oui, 3) == 0) { | |
267 | switch (data[3]) { | |
510e0e2f | 268 | case 1: |
857d966e MH |
269 | printf("IEEE 802.1X"); |
270 | break; | |
510e0e2f | 271 | case 2: |
857d966e MH |
272 | printf("PSK"); |
273 | break; | |
0fe1c415 JB |
274 | case 3: |
275 | printf("FT/IEEE 802.1X"); | |
276 | break; | |
277 | case 4: | |
278 | printf("FT/PSK"); | |
279 | break; | |
280 | case 5: | |
281 | printf("IEEE 802.1X/SHA-256"); | |
282 | break; | |
283 | case 6: | |
284 | printf("PSK/SHA-256"); | |
285 | break; | |
857d966e | 286 | default: |
332769c6 | 287 | printf("%.02x-%.02x-%.02x:%d", |
5594fd23 | 288 | data[0], data[1] ,data[2], data[3]); |
857d966e MH |
289 | break; |
290 | } | |
291 | } else | |
332769c6 | 292 | printf("%.02x-%.02x-%.02x:%d", |
5594fd23 | 293 | data[0], data[1] ,data[2], data[3]); |
857d966e MH |
294 | } |
295 | ||
83b4934c JB |
296 | static void print_rsn_ie(const char *defcipher, const char *defauth, |
297 | uint8_t len, const uint8_t *data) | |
857d966e MH |
298 | { |
299 | bool first = true; | |
300 | __u16 version, count, capa; | |
301 | int i; | |
302 | ||
857d966e MH |
303 | version = data[0] + (data[1] << 8); |
304 | tab_on_first(&first); | |
305 | printf("\t * Version: %d\n", version); | |
306 | ||
307 | data += 2; | |
308 | len -= 2; | |
309 | ||
310 | if (len < 4) { | |
311 | tab_on_first(&first); | |
312 | printf("\t * Group cipher: %s\n", defcipher); | |
313 | printf("\t * Pairwise ciphers: %s\n", defcipher); | |
314 | return; | |
315 | } | |
316 | ||
317 | tab_on_first(&first); | |
318 | printf("\t * Group cipher: "); | |
319 | print_cipher(data); | |
320 | printf("\n"); | |
321 | ||
322 | data += 4; | |
323 | len -= 4; | |
324 | ||
325 | if (len < 2) { | |
326 | tab_on_first(&first); | |
327 | printf("\t * Pairwise ciphers: %s\n", defcipher); | |
328 | return; | |
329 | } | |
330 | ||
331 | count = data[0] | (data[1] << 8); | |
31d477fb MH |
332 | if (2 + (count * 4) > len) |
333 | goto invalid; | |
334 | ||
857d966e MH |
335 | tab_on_first(&first); |
336 | printf("\t * Pairwise ciphers:"); | |
31d477fb | 337 | for (i = 0; i < count; i++) { |
857d966e MH |
338 | printf(" "); |
339 | print_cipher(data + 2 + (i * 4)); | |
340 | } | |
341 | printf("\n"); | |
342 | ||
343 | data += 2 + (count * 4); | |
344 | len -= 2 + (count * 4); | |
345 | ||
346 | if (len < 2) { | |
347 | tab_on_first(&first); | |
348 | printf("\t * Authentication suites: %s\n", defauth); | |
349 | return; | |
350 | } | |
351 | ||
352 | count = data[0] | (data[1] << 8); | |
31d477fb MH |
353 | if (2 + (count * 4) > len) |
354 | goto invalid; | |
355 | ||
857d966e MH |
356 | tab_on_first(&first); |
357 | printf("\t * Authentication suites:"); | |
83b4934c | 358 | for (i = 0; i < count; i++) { |
857d966e MH |
359 | printf(" "); |
360 | print_auth(data + 2 + (i * 4)); | |
361 | } | |
362 | printf("\n"); | |
363 | ||
364 | data += 2 + (count * 4); | |
365 | len -= 2 + (count * 4); | |
366 | ||
6a4f24e8 JB |
367 | if (len >= 2) { |
368 | capa = data[0] | (data[1] << 8); | |
369 | tab_on_first(&first); | |
370 | printf("\t * Capabilities: 0x%.4x\n", capa); | |
31d477fb | 371 | |
6a4f24e8 JB |
372 | data += 2; |
373 | len -= 2; | |
374 | } | |
31d477fb MH |
375 | |
376 | invalid: | |
377 | if (len != 0) { | |
378 | printf("\t\t * bogus tail data (%d):", len); | |
379 | while (len) { | |
380 | printf(" %.2x", *data); | |
381 | data++; | |
382 | len--; | |
383 | } | |
384 | printf("\n"); | |
385 | } | |
857d966e MH |
386 | } |
387 | ||
83b4934c | 388 | static void print_rsn(const uint8_t type, uint8_t len, const uint8_t *data) |
857d966e | 389 | { |
83b4934c | 390 | print_rsn_ie("CCMP", "IEEE 802.1X", len, data); |
857d966e MH |
391 | } |
392 | ||
83b4934c | 393 | static void print_capabilities(const uint8_t type, uint8_t len, const uint8_t *data) |
9b880b00 MH |
394 | { |
395 | int i; | |
396 | ||
83b4934c | 397 | for(i = 0; i < len; i++) |
9b880b00 MH |
398 | printf(" %.02x", data[i]); |
399 | printf("\n"); | |
400 | } | |
401 | ||
83b4934c JB |
402 | struct ie_print { |
403 | const char *name; | |
404 | void (*print)(const uint8_t type, uint8_t len, const uint8_t *data); | |
405 | uint8_t minlen, maxlen; | |
764fe753 JB |
406 | }; |
407 | ||
83b4934c JB |
408 | static void print_ie(const struct ie_print *p, const uint8_t type, |
409 | uint8_t len, const uint8_t *data) | |
4673a894 | 410 | { |
83b4934c JB |
411 | int i; |
412 | ||
413 | if (!p->print) | |
414 | return; | |
415 | ||
416 | printf("\t%s:", p->name); | |
417 | if (len < p->minlen || len > p->maxlen) { | |
418 | if (len > 1) { | |
419 | printf(" <invalid: %d bytes:", len); | |
420 | for (i = 0; i < len; i++) | |
421 | printf(" %.02x", data[i]); | |
422 | printf(">\n"); | |
423 | } else if (len) | |
424 | printf(" <invalid: 1 byte: %.02x>\n", data[0]); | |
425 | else | |
426 | printf(" <invalid: no data>\n"); | |
427 | return; | |
428 | } | |
429 | ||
430 | p->print(type, len, data); | |
431 | } | |
432 | ||
433 | #define PRINT_IGN { \ | |
434 | .name = "IGNORE", \ | |
435 | .print = NULL, \ | |
436 | .minlen = 0, \ | |
437 | .maxlen = 255, \ | |
4673a894 JB |
438 | } |
439 | ||
83b4934c JB |
440 | static const struct ie_print ieprinters[] = { |
441 | [0] = { "SSID", print_ssid, 0, 32, }, | |
442 | [1] = { "Supported rates", print_supprates, 0, 255, }, | |
443 | [3] = { "DS Paramater set", print_ds, 1, 1, }, | |
444 | [5] = PRINT_IGN, | |
445 | [7] = { "Country", print_country, 3, 255, }, | |
d1563a1b | 446 | [32] = { "Power constraint", print_powerconstraint, 1, 1, }, |
83b4934c JB |
447 | [42] = { "ERP", print_erp, 1, 255, }, |
448 | [48] = { "RSN", print_rsn, 2, 255, }, | |
449 | [50] = { "Extended supported rates", print_supprates, 0, 255, }, | |
450 | [127] = { "Extended capabilities", print_capabilities, 0, 255, }, | |
451 | }; | |
452 | ||
453 | static void print_wifi_wpa(const uint8_t type, uint8_t len, const uint8_t *data) | |
454 | { | |
455 | print_rsn_ie("TKIP", "IEEE 802.1X", len, data); | |
456 | } | |
457 | ||
458 | static void print_wifi_wmm(const uint8_t type, uint8_t len, const uint8_t *data) | |
6ff0c93a MH |
459 | { |
460 | int i; | |
461 | ||
6ff0c93a MH |
462 | switch (data[0]) { |
463 | case 0x00: | |
83b4934c | 464 | printf(" information:"); |
6ff0c93a MH |
465 | break; |
466 | case 0x01: | |
83b4934c | 467 | printf(" parameter:"); |
6ff0c93a MH |
468 | break; |
469 | default: | |
83b4934c | 470 | printf(" type %d:", data[0]); |
6ff0c93a MH |
471 | break; |
472 | } | |
473 | ||
83b4934c | 474 | for(i = 0; i < len - 1; i++) |
6ff0c93a MH |
475 | printf(" %.02x", data[i + 1]); |
476 | printf("\n"); | |
477 | } | |
478 | ||
83b4934c | 479 | static void print_wifi_wps(const uint8_t type, uint8_t len, const uint8_t *data) |
4673a894 JB |
480 | { |
481 | bool first = true; | |
482 | __u16 subtype, sublen; | |
483 | ||
4673a894 JB |
484 | while (len >= 4) { |
485 | subtype = (data[0] << 8) + data[1]; | |
486 | sublen = (data[2] << 8) + data[3]; | |
487 | if (sublen > len) | |
488 | break; | |
489 | ||
490 | switch (subtype) { | |
491 | case 0x104a: | |
492 | tab_on_first(&first); | |
dffc6750 | 493 | printf("\t * Version: %d.%d\n", data[4] >> 4, data[4] & 0xF); |
4673a894 JB |
494 | break; |
495 | case 0x1011: | |
496 | tab_on_first(&first); | |
497 | printf("\t * Device name: %.*s\n", sublen, data + 4); | |
498 | break; | |
499 | case 0x1021: | |
500 | tab_on_first(&first); | |
501 | printf("\t * Manufacturer: %.*s\n", sublen, data + 4); | |
502 | break; | |
503 | case 0x1023: | |
504 | tab_on_first(&first); | |
505 | printf("\t * Model: %.*s\n", sublen, data + 4); | |
506 | break; | |
7ee5a865 JB |
507 | case 0x1057: { |
508 | __u16 val = (data[4] << 8) | data[5]; | |
509 | tab_on_first(&first); | |
510 | printf("\t * AP setup locked: 0x%.4x\n", val); | |
511 | break; | |
512 | } | |
4673a894 JB |
513 | case 0x1008: { |
514 | __u16 meth = (data[4] << 8) + data[5]; | |
515 | bool comma = false; | |
516 | tab_on_first(&first); | |
517 | printf("\t * Config methods:"); | |
518 | #define T(bit, name) do { \ | |
519 | if (meth & (1<<bit)) { \ | |
520 | if (comma) \ | |
521 | printf(","); \ | |
522 | comma = true; \ | |
523 | printf(" " name); \ | |
524 | } } while (0) | |
525 | T(0, "USB"); | |
526 | T(1, "Ethernet"); | |
527 | T(2, "Label"); | |
528 | T(3, "Display"); | |
529 | T(4, "Ext. NFC"); | |
530 | T(5, "Int. NFC"); | |
531 | T(6, "NFC Intf."); | |
532 | T(7, "PBC"); | |
533 | T(8, "Keypad"); | |
534 | printf("\n"); | |
535 | break; | |
536 | #undef T | |
537 | } | |
538 | default: | |
539 | break; | |
540 | } | |
541 | ||
542 | data += sublen + 4; | |
543 | len -= sublen + 4; | |
544 | } | |
545 | ||
546 | if (len != 0) { | |
547 | printf("\t\t * bogus tail data (%d):", len); | |
548 | while (len) { | |
549 | printf(" %.2x", *data); | |
550 | data++; | |
551 | len--; | |
552 | } | |
553 | printf("\n"); | |
554 | } | |
555 | } | |
556 | ||
83b4934c JB |
557 | static const struct ie_print wifiprinters[] = { |
558 | [1] = { "WPA", print_wifi_wpa, 2, 255, }, | |
559 | [2] = { "WMM", print_wifi_wmm, 1, 255, }, | |
560 | [4] = { "WPS", print_wifi_wps, 0, 255, }, | |
4673a894 JB |
561 | }; |
562 | ||
764fe753 JB |
563 | static void print_vendor(unsigned char len, unsigned char *data, |
564 | struct scan_params *params) | |
3563f4c5 JB |
565 | { |
566 | int i; | |
567 | ||
fbf80af5 | 568 | if (len < 3) { |
4673a894 | 569 | printf("\tVendor specific: <too short> data:"); |
fbf80af5 JB |
570 | for(i = 0; i < len; i++) |
571 | printf(" %.02x", data[i]); | |
572 | printf("\n"); | |
573 | return; | |
574 | } | |
575 | ||
857d966e | 576 | if (len >= 4 && memcmp(data, wifi_oui, 3) == 0) { |
83b4934c JB |
577 | if (data[3] < ARRAY_SIZE(wifiprinters) && wifiprinters[data[3]].name) { |
578 | print_ie(&wifiprinters[data[3]], data[3], len - 4, data + 4); | |
579 | return; | |
580 | } | |
4673a894 JB |
581 | if (!params->unknown) |
582 | return; | |
857d966e | 583 | printf("\tWiFi OUI %#.2x, data:", data[3]); |
4673a894 JB |
584 | for(i = 0; i < len - 4; i++) |
585 | printf(" %.02x", data[i + 4]); | |
586 | printf("\n"); | |
587 | return; | |
588 | } | |
589 | ||
764fe753 JB |
590 | if (!params->unknown) |
591 | return; | |
592 | ||
fbf80af5 | 593 | printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:", |
3563f4c5 | 594 | data[0], data[1], data[2]); |
fbf80af5 JB |
595 | for (i = 3; i < len; i++) |
596 | printf(" %.2x", data[i]); | |
3563f4c5 JB |
597 | printf("\n"); |
598 | } | |
599 | ||
764fe753 | 600 | static void print_ies(unsigned char *ie, int ielen, struct scan_params *params) |
3563f4c5 JB |
601 | { |
602 | while (ielen >= 2 && ielen >= ie[1]) { | |
83b4934c JB |
603 | if (ie[0] < ARRAY_SIZE(ieprinters) && ieprinters[ie[0]].name) { |
604 | print_ie(&ieprinters[ie[0]], ie[0], ie[1], ie + 2); | |
764fe753 JB |
605 | } else if (ie[0] == 221 /* vendor */) { |
606 | print_vendor(ie[1], ie + 2, params); | |
607 | } else if (params->unknown) { | |
3563f4c5 JB |
608 | int i; |
609 | ||
8086b700 | 610 | printf("\tUnknown IE (%d):", ie[0]); |
3563f4c5 | 611 | for (i=0; i<ie[1]; i++) |
8086b700 | 612 | printf(" %.2x", ie[2+i]); |
3563f4c5 JB |
613 | printf("\n"); |
614 | } | |
615 | ielen -= ie[1] + 2; | |
616 | ie += ie[1] + 2; | |
617 | } | |
618 | } | |
619 | ||
620 | static int print_bss_handler(struct nl_msg *msg, void *arg) | |
621 | { | |
622 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
623 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
624 | struct nlattr *bss[NL80211_BSS_MAX + 1]; | |
625 | char mac_addr[20], dev[20]; | |
626 | static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = { | |
627 | [NL80211_BSS_TSF] = { .type = NLA_U64 }, | |
628 | [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 }, | |
629 | [NL80211_BSS_BSSID] = { }, | |
630 | [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 }, | |
631 | [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 }, | |
632 | [NL80211_BSS_INFORMATION_ELEMENTS] = { }, | |
f2e17e1f JB |
633 | [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 }, |
634 | [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 }, | |
3563f4c5 JB |
635 | }; |
636 | ||
637 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
638 | genlmsg_attrlen(gnlh, 0), NULL); | |
639 | ||
640 | if (!tb[NL80211_ATTR_BSS]) { | |
641 | fprintf(stderr, "bss info missing!"); | |
642 | return NL_SKIP; | |
643 | } | |
644 | if (nla_parse_nested(bss, NL80211_BSS_MAX, | |
645 | tb[NL80211_ATTR_BSS], | |
646 | bss_policy)) { | |
647 | fprintf(stderr, "failed to parse nested attributes!"); | |
648 | return NL_SKIP; | |
649 | } | |
650 | ||
651 | if (!bss[NL80211_BSS_BSSID]) | |
652 | return NL_SKIP; | |
653 | ||
654 | mac_addr_n2a(mac_addr, nla_data(bss[NL80211_BSS_BSSID])); | |
655 | if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev); | |
656 | printf("BSS %s (on %s)\n", mac_addr, dev); | |
657 | ||
e7109a8a JB |
658 | if (bss[NL80211_BSS_TSF]) { |
659 | unsigned long long tsf; | |
660 | tsf = (unsigned long long)nla_get_u64(bss[NL80211_BSS_TSF]); | |
661 | printf("\tTSF: %llu usec (%llud, %.2lld:%.2llu:%.2llu)\n", | |
662 | tsf, tsf/1000/1000/60/60/24, (tsf/1000/1000/60/60) % 24, | |
663 | (tsf/1000/1000/60) % 60, (tsf/1000/1000) % 60); | |
664 | } | |
3563f4c5 JB |
665 | if (bss[NL80211_BSS_FREQUENCY]) |
666 | printf("\tfreq: %d\n", | |
667 | nla_get_u32(bss[NL80211_BSS_FREQUENCY])); | |
668 | if (bss[NL80211_BSS_BEACON_INTERVAL]) | |
669 | printf("\tbeacon interval: %d\n", | |
670 | nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL])); | |
92a04ecd MH |
671 | if (bss[NL80211_BSS_CAPABILITY]) { |
672 | __u16 capa = nla_get_u16(bss[NL80211_BSS_CAPABILITY]); | |
673 | printf("\tcapability:"); | |
674 | if (capa & WLAN_CAPABILITY_ESS) | |
675 | printf(" ESS"); | |
676 | if (capa & WLAN_CAPABILITY_IBSS) | |
677 | printf(" IBSS"); | |
678 | if (capa & WLAN_CAPABILITY_PRIVACY) | |
679 | printf(" Privacy"); | |
680 | if (capa & WLAN_CAPABILITY_SHORT_PREAMBLE) | |
681 | printf(" ShortPreamble"); | |
682 | if (capa & WLAN_CAPABILITY_PBCC) | |
683 | printf(" PBCC"); | |
684 | if (capa & WLAN_CAPABILITY_CHANNEL_AGILITY) | |
685 | printf(" ChannelAgility"); | |
686 | if (capa & WLAN_CAPABILITY_SPECTRUM_MGMT) | |
687 | printf(" SpectrumMgmt"); | |
688 | if (capa & WLAN_CAPABILITY_QOS) | |
689 | printf(" QoS"); | |
690 | if (capa & WLAN_CAPABILITY_SHORT_SLOT_TIME) | |
691 | printf(" ShortSlotTime"); | |
692 | if (capa & WLAN_CAPABILITY_APSD) | |
693 | printf(" APSD"); | |
694 | if (capa & WLAN_CAPABILITY_DSSS_OFDM) | |
695 | printf(" DSSS-OFDM"); | |
696 | printf(" (0x%.4x)\n", capa); | |
697 | } | |
f2e17e1f JB |
698 | if (bss[NL80211_BSS_SIGNAL_MBM]) { |
699 | int s = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]); | |
700 | printf("\tsignal: %d.%.2d dBm\n", s/100, s%100); | |
701 | } | |
702 | if (bss[NL80211_BSS_SIGNAL_UNSPEC]) { | |
703 | unsigned char s = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]); | |
704 | printf("\tsignal: %d/100\n", s); | |
705 | } | |
3563f4c5 JB |
706 | if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) |
707 | print_ies(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]), | |
764fe753 JB |
708 | nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]), |
709 | arg); | |
3563f4c5 JB |
710 | |
711 | return NL_SKIP; | |
712 | } | |
713 | ||
764fe753 | 714 | static struct scan_params scan_params; |
3563f4c5 | 715 | |
7c37a24d JB |
716 | static int handle_scan_dump(struct nl80211_state *state, |
717 | struct nl_cb *cb, | |
3563f4c5 JB |
718 | struct nl_msg *msg, |
719 | int argc, char **argv) | |
720 | { | |
764fe753 JB |
721 | if (argc > 1) |
722 | return 1; | |
723 | ||
724 | scan_params.unknown = false; | |
725 | if (argc == 1 && !strcmp(argv[0], "-u")) | |
726 | scan_params.unknown = true; | |
727 | ||
728 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_bss_handler, | |
729 | &scan_params); | |
3563f4c5 JB |
730 | return 0; |
731 | } | |
764fe753 | 732 | COMMAND(scan, dump, "[-u]", |
3563f4c5 | 733 | NL80211_CMD_GET_SCAN, NLM_F_DUMP, CIB_NETDEV, handle_scan_dump); |
a5fe4ef2 JB |
734 | |
735 | static int handle_scan_combined(struct nl80211_state *state, | |
736 | struct nl_cb *cb, | |
737 | struct nl_msg *msg, | |
738 | int argc, char **argv) | |
739 | { | |
559a1713 JB |
740 | char **trig_argv; |
741 | /* static char *trig_argv[] = { | |
a5fe4ef2 JB |
742 | NULL, |
743 | "scan", | |
744 | "trigger", | |
559a1713 | 745 | };*/ |
a5fe4ef2 JB |
746 | static char *dump_argv[] = { |
747 | NULL, | |
748 | "scan", | |
749 | "dump", | |
92649eab | 750 | NULL, |
a5fe4ef2 JB |
751 | }; |
752 | static const __u32 cmds[] = { | |
753 | NL80211_CMD_NEW_SCAN_RESULTS, | |
754 | NL80211_CMD_SCAN_ABORTED, | |
755 | }; | |
559a1713 | 756 | int trig_argc, dump_argc, err; |
a5fe4ef2 | 757 | |
559a1713 JB |
758 | if (argc >= 3 && !strcmp(argv[2], "-u")) { |
759 | dump_argc = 4; | |
760 | dump_argv[3] = "-u"; | |
761 | } else | |
762 | dump_argc = 3; | |
763 | ||
764 | trig_argc = 3 + (argc - 2) + (3 - dump_argc); | |
765 | trig_argv = calloc(trig_argc, sizeof(*trig_argv)); | |
766 | if (!trig_argv) | |
767 | return -ENOMEM; | |
a5fe4ef2 | 768 | trig_argv[0] = argv[0]; |
559a1713 JB |
769 | trig_argv[1] = "scan"; |
770 | trig_argv[2] = "trigger"; | |
771 | int i; | |
772 | for (i = 0; i < argc - 2 - (dump_argc - 3); i++) | |
773 | trig_argv[i + 3] = argv[i + 2 + (dump_argc - 3)]; | |
774 | err = handle_cmd(state, II_NETDEV, trig_argc, trig_argv); | |
775 | free(trig_argv); | |
a5fe4ef2 JB |
776 | if (err) |
777 | return err; | |
778 | ||
61725dbe JB |
779 | /* |
780 | * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION | |
781 | * | |
782 | * This code has a bug, which requires creating a separate | |
783 | * nl80211 socket to fix: | |
784 | * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or | |
785 | * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel | |
786 | * before (!) we listen to it, because we only start listening | |
787 | * after we send our scan request. | |
788 | * | |
789 | * Doing it the other way around has a race condition as well, | |
790 | * if you first open the events socket you may get a notification | |
791 | * for a previous scan. | |
792 | * | |
793 | * The only proper way to fix this would be to listen to events | |
794 | * before sending the command, and for the kernel to send the | |
795 | * scan request along with the event, so that you can match up | |
796 | * whether the scan you requested was finished or aborted (this | |
797 | * may result in processing a scan that another application | |
798 | * requested, but that doesn't seem to be a problem). | |
799 | * | |
800 | * Alas, the kernel doesn't do that (yet). | |
801 | */ | |
802 | ||
a5fe4ef2 JB |
803 | if (listen_events(state, ARRAY_SIZE(cmds), cmds) == |
804 | NL80211_CMD_SCAN_ABORTED) { | |
805 | printf("scan aborted!\n"); | |
806 | return 0; | |
807 | } | |
808 | ||
809 | dump_argv[0] = argv[0]; | |
92649eab | 810 | return handle_cmd(state, II_NETDEV, dump_argc, dump_argv); |
a5fe4ef2 | 811 | } |
559a1713 | 812 | TOPLEVEL(scan, "[-u] [freq <freq>*] [ssid <ssid>*|passive]", 0, 0, CIB_NETDEV, handle_scan_combined); |