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