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