]>
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 | ||
764fe753 JB |
16 | struct scan_params { |
17 | bool unknown; | |
18 | }; | |
19 | ||
7c37a24d JB |
20 | static int handle_scan(struct nl80211_state *state, |
21 | struct nl_cb *cb, | |
3563f4c5 JB |
22 | struct nl_msg *msg, |
23 | int argc, char **argv) | |
24 | { | |
25 | struct nl_msg *ssids = NULL; | |
26 | int err = -ENOBUFS; | |
27 | ||
28 | ssids = nlmsg_alloc(); | |
29 | if (!ssids) | |
30 | return -ENOMEM; | |
31 | NLA_PUT(ssids, 1, 0, ""); | |
32 | nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids); | |
33 | ||
34 | err = 0; | |
35 | nla_put_failure: | |
36 | nlmsg_free(ssids); | |
37 | return err; | |
38 | } | |
39 | COMMAND(scan, trigger, NULL, | |
40 | NL80211_CMD_TRIGGER_SCAN, 0, CIB_NETDEV, handle_scan); | |
41 | ||
42 | typedef void (*printfn)(unsigned char type, unsigned char len, unsigned char *data); | |
43 | ||
44 | static void print_ssid(unsigned char type, unsigned char len, unsigned char *data) | |
45 | { | |
46 | int i; | |
47 | printf("\tSSID: "); | |
48 | for (i=0; i<len; i++) { | |
49 | if (isprint(data[i])) | |
50 | printf("%c", data[i]); | |
51 | else | |
52 | printf("\\x%.2x", data[i]); | |
53 | } | |
54 | printf("\n"); | |
55 | } | |
56 | ||
57 | static void print_supprates(unsigned char type, unsigned char len, unsigned char *data) | |
58 | { | |
59 | int i; | |
60 | ||
61 | if (type == 1) | |
62 | printf("\tSupported rates: "); | |
63 | else | |
64 | printf("\tExtended supported rates: "); | |
65 | ||
66 | for (i=0; i<len; i++) { | |
67 | int r = data[i] & 0x7f; | |
68 | printf("%d.%d%s ", r/2, 5*(r&1), data[i] & 0x80 ? "*":""); | |
69 | } | |
70 | printf("\n"); | |
71 | } | |
72 | ||
73 | static void print_ds(unsigned char type, unsigned char len, unsigned char *data) | |
74 | { | |
75 | printf("\tDS Parameter set: channel %d\n", data[0]); | |
76 | } | |
77 | ||
78 | static void print_ign(unsigned char type, unsigned char len, unsigned char *data) | |
79 | { | |
80 | /* ignore for now, not too useful */ | |
81 | } | |
82 | ||
fc4d1484 MH |
83 | static void print_erp(unsigned char type, unsigned char len, unsigned char *data) |
84 | { | |
85 | if (data[0] == 0x00) | |
86 | return; | |
87 | ||
88 | printf("\tERP:"); | |
89 | if (data[0] & 0x01) | |
90 | printf(" NonERP_Present"); | |
91 | if (data[0] & 0x02) | |
92 | printf(" Use_Protection"); | |
93 | if (data[0] & 0x04) | |
94 | printf(" Barker_Preamble_Mode"); | |
95 | printf("\n"); | |
96 | } | |
97 | ||
764fe753 JB |
98 | static const printfn ieprinters[] = { |
99 | [0] = print_ssid, | |
100 | [1] = print_supprates, | |
101 | [3] = print_ds, | |
102 | [5] = print_ign, | |
fc4d1484 | 103 | [42] = print_erp, |
764fe753 JB |
104 | [50] = print_supprates, |
105 | }; | |
106 | ||
4673a894 JB |
107 | static void tab_on_first(bool *first) |
108 | { | |
109 | if (!*first) | |
110 | printf("\t"); | |
111 | else | |
112 | *first = false; | |
113 | } | |
114 | ||
115 | static void print_wifi_wps(unsigned char type, unsigned char len, unsigned char *data) | |
116 | { | |
117 | bool first = true; | |
118 | __u16 subtype, sublen; | |
119 | ||
120 | printf("\tWPS:"); | |
121 | ||
122 | while (len >= 4) { | |
123 | subtype = (data[0] << 8) + data[1]; | |
124 | sublen = (data[2] << 8) + data[3]; | |
125 | if (sublen > len) | |
126 | break; | |
127 | ||
128 | switch (subtype) { | |
129 | case 0x104a: | |
130 | tab_on_first(&first); | |
131 | printf("\t * Version: %#.2x\n", data[4]); | |
132 | break; | |
133 | case 0x1011: | |
134 | tab_on_first(&first); | |
135 | printf("\t * Device name: %.*s\n", sublen, data + 4); | |
136 | break; | |
137 | case 0x1021: | |
138 | tab_on_first(&first); | |
139 | printf("\t * Manufacturer: %.*s\n", sublen, data + 4); | |
140 | break; | |
141 | case 0x1023: | |
142 | tab_on_first(&first); | |
143 | printf("\t * Model: %.*s\n", sublen, data + 4); | |
144 | break; | |
7ee5a865 JB |
145 | case 0x1057: { |
146 | __u16 val = (data[4] << 8) | data[5]; | |
147 | tab_on_first(&first); | |
148 | printf("\t * AP setup locked: 0x%.4x\n", val); | |
149 | break; | |
150 | } | |
4673a894 JB |
151 | case 0x1008: { |
152 | __u16 meth = (data[4] << 8) + data[5]; | |
153 | bool comma = false; | |
154 | tab_on_first(&first); | |
155 | printf("\t * Config methods:"); | |
156 | #define T(bit, name) do { \ | |
157 | if (meth & (1<<bit)) { \ | |
158 | if (comma) \ | |
159 | printf(","); \ | |
160 | comma = true; \ | |
161 | printf(" " name); \ | |
162 | } } while (0) | |
163 | T(0, "USB"); | |
164 | T(1, "Ethernet"); | |
165 | T(2, "Label"); | |
166 | T(3, "Display"); | |
167 | T(4, "Ext. NFC"); | |
168 | T(5, "Int. NFC"); | |
169 | T(6, "NFC Intf."); | |
170 | T(7, "PBC"); | |
171 | T(8, "Keypad"); | |
172 | printf("\n"); | |
173 | break; | |
174 | #undef T | |
175 | } | |
176 | default: | |
177 | break; | |
178 | } | |
179 | ||
180 | data += sublen + 4; | |
181 | len -= sublen + 4; | |
182 | } | |
183 | ||
184 | if (len != 0) { | |
185 | printf("\t\t * bogus tail data (%d):", len); | |
186 | while (len) { | |
187 | printf(" %.2x", *data); | |
188 | data++; | |
189 | len--; | |
190 | } | |
191 | printf("\n"); | |
192 | } | |
193 | } | |
194 | ||
195 | static const printfn wifiprinters[] = { | |
196 | [4] = print_wifi_wps, | |
197 | }; | |
198 | ||
764fe753 JB |
199 | static void print_vendor(unsigned char len, unsigned char *data, |
200 | struct scan_params *params) | |
3563f4c5 JB |
201 | { |
202 | int i; | |
203 | ||
fbf80af5 | 204 | if (len < 3) { |
4673a894 | 205 | printf("\tVendor specific: <too short> data:"); |
fbf80af5 JB |
206 | for(i = 0; i < len; i++) |
207 | printf(" %.02x", data[i]); | |
208 | printf("\n"); | |
209 | return; | |
210 | } | |
211 | ||
4673a894 JB |
212 | if (len >= 4 && data[0] == 0x00 && data[1] == 0x50 && data[2] == 0xF2) { |
213 | if (data[3] < ARRAY_SIZE(wifiprinters) && wifiprinters[data[3]]) | |
214 | return wifiprinters[data[3]](data[3], len - 4, data + 4); | |
215 | if (!params->unknown) | |
216 | return; | |
217 | printf("\tWiFi OUI %#.2x data:", data[3]); | |
218 | for(i = 0; i < len - 4; i++) | |
219 | printf(" %.02x", data[i + 4]); | |
220 | printf("\n"); | |
221 | return; | |
222 | } | |
223 | ||
764fe753 JB |
224 | if (!params->unknown) |
225 | return; | |
226 | ||
fbf80af5 | 227 | printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:", |
3563f4c5 | 228 | data[0], data[1], data[2]); |
fbf80af5 JB |
229 | for (i = 3; i < len; i++) |
230 | printf(" %.2x", data[i]); | |
3563f4c5 JB |
231 | printf("\n"); |
232 | } | |
233 | ||
764fe753 | 234 | static void print_ies(unsigned char *ie, int ielen, struct scan_params *params) |
3563f4c5 JB |
235 | { |
236 | while (ielen >= 2 && ielen >= ie[1]) { | |
97ebbaf5 | 237 | if (ie[0] < ARRAY_SIZE(ieprinters) && ieprinters[ie[0]]) { |
3563f4c5 | 238 | ieprinters[ie[0]](ie[0], ie[1], ie + 2); |
764fe753 JB |
239 | } else if (ie[0] == 221 /* vendor */) { |
240 | print_vendor(ie[1], ie + 2, params); | |
241 | } else if (params->unknown) { | |
3563f4c5 JB |
242 | int i; |
243 | ||
8086b700 | 244 | printf("\tUnknown IE (%d):", ie[0]); |
3563f4c5 | 245 | for (i=0; i<ie[1]; i++) |
8086b700 | 246 | printf(" %.2x", ie[2+i]); |
3563f4c5 JB |
247 | printf("\n"); |
248 | } | |
249 | ielen -= ie[1] + 2; | |
250 | ie += ie[1] + 2; | |
251 | } | |
252 | } | |
253 | ||
254 | static int print_bss_handler(struct nl_msg *msg, void *arg) | |
255 | { | |
256 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
257 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
258 | struct nlattr *bss[NL80211_BSS_MAX + 1]; | |
259 | char mac_addr[20], dev[20]; | |
260 | static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = { | |
261 | [NL80211_BSS_TSF] = { .type = NLA_U64 }, | |
262 | [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 }, | |
263 | [NL80211_BSS_BSSID] = { }, | |
264 | [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 }, | |
265 | [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 }, | |
266 | [NL80211_BSS_INFORMATION_ELEMENTS] = { }, | |
f2e17e1f JB |
267 | [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 }, |
268 | [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 }, | |
3563f4c5 JB |
269 | }; |
270 | ||
271 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
272 | genlmsg_attrlen(gnlh, 0), NULL); | |
273 | ||
274 | if (!tb[NL80211_ATTR_BSS]) { | |
275 | fprintf(stderr, "bss info missing!"); | |
276 | return NL_SKIP; | |
277 | } | |
278 | if (nla_parse_nested(bss, NL80211_BSS_MAX, | |
279 | tb[NL80211_ATTR_BSS], | |
280 | bss_policy)) { | |
281 | fprintf(stderr, "failed to parse nested attributes!"); | |
282 | return NL_SKIP; | |
283 | } | |
284 | ||
285 | if (!bss[NL80211_BSS_BSSID]) | |
286 | return NL_SKIP; | |
287 | ||
288 | mac_addr_n2a(mac_addr, nla_data(bss[NL80211_BSS_BSSID])); | |
289 | if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev); | |
290 | printf("BSS %s (on %s)\n", mac_addr, dev); | |
291 | ||
e7109a8a JB |
292 | if (bss[NL80211_BSS_TSF]) { |
293 | unsigned long long tsf; | |
294 | tsf = (unsigned long long)nla_get_u64(bss[NL80211_BSS_TSF]); | |
295 | printf("\tTSF: %llu usec (%llud, %.2lld:%.2llu:%.2llu)\n", | |
296 | tsf, tsf/1000/1000/60/60/24, (tsf/1000/1000/60/60) % 24, | |
297 | (tsf/1000/1000/60) % 60, (tsf/1000/1000) % 60); | |
298 | } | |
3563f4c5 JB |
299 | if (bss[NL80211_BSS_FREQUENCY]) |
300 | printf("\tfreq: %d\n", | |
301 | nla_get_u32(bss[NL80211_BSS_FREQUENCY])); | |
302 | if (bss[NL80211_BSS_BEACON_INTERVAL]) | |
303 | printf("\tbeacon interval: %d\n", | |
304 | nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL])); | |
305 | if (bss[NL80211_BSS_CAPABILITY]) | |
306 | printf("\tcapability: 0x%.4x\n", | |
307 | nla_get_u16(bss[NL80211_BSS_CAPABILITY])); | |
f2e17e1f JB |
308 | if (bss[NL80211_BSS_SIGNAL_MBM]) { |
309 | int s = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]); | |
310 | printf("\tsignal: %d.%.2d dBm\n", s/100, s%100); | |
311 | } | |
312 | if (bss[NL80211_BSS_SIGNAL_UNSPEC]) { | |
313 | unsigned char s = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]); | |
314 | printf("\tsignal: %d/100\n", s); | |
315 | } | |
3563f4c5 JB |
316 | if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) |
317 | print_ies(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]), | |
764fe753 JB |
318 | nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]), |
319 | arg); | |
3563f4c5 JB |
320 | |
321 | return NL_SKIP; | |
322 | } | |
323 | ||
764fe753 | 324 | static struct scan_params scan_params; |
3563f4c5 | 325 | |
7c37a24d JB |
326 | static int handle_scan_dump(struct nl80211_state *state, |
327 | struct nl_cb *cb, | |
3563f4c5 JB |
328 | struct nl_msg *msg, |
329 | int argc, char **argv) | |
330 | { | |
764fe753 JB |
331 | if (argc > 1) |
332 | return 1; | |
333 | ||
334 | scan_params.unknown = false; | |
335 | if (argc == 1 && !strcmp(argv[0], "-u")) | |
336 | scan_params.unknown = true; | |
337 | ||
338 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_bss_handler, | |
339 | &scan_params); | |
3563f4c5 JB |
340 | return 0; |
341 | } | |
764fe753 | 342 | COMMAND(scan, dump, "[-u]", |
3563f4c5 | 343 | NL80211_CMD_GET_SCAN, NLM_F_DUMP, CIB_NETDEV, handle_scan_dump); |
a5fe4ef2 JB |
344 | |
345 | static int handle_scan_combined(struct nl80211_state *state, | |
346 | struct nl_cb *cb, | |
347 | struct nl_msg *msg, | |
348 | int argc, char **argv) | |
349 | { | |
350 | static char *trig_argv[] = { | |
351 | NULL, | |
352 | "scan", | |
353 | "trigger", | |
354 | }; | |
355 | static char *dump_argv[] = { | |
356 | NULL, | |
357 | "scan", | |
358 | "dump", | |
92649eab | 359 | NULL, |
a5fe4ef2 JB |
360 | }; |
361 | static const __u32 cmds[] = { | |
362 | NL80211_CMD_NEW_SCAN_RESULTS, | |
363 | NL80211_CMD_SCAN_ABORTED, | |
364 | }; | |
92649eab | 365 | int dump_argc, err; |
a5fe4ef2 JB |
366 | |
367 | trig_argv[0] = argv[0]; | |
368 | err = handle_cmd(state, II_NETDEV, ARRAY_SIZE(trig_argv), trig_argv); | |
369 | if (err) | |
370 | return err; | |
371 | ||
61725dbe JB |
372 | /* |
373 | * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION | |
374 | * | |
375 | * This code has a bug, which requires creating a separate | |
376 | * nl80211 socket to fix: | |
377 | * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or | |
378 | * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel | |
379 | * before (!) we listen to it, because we only start listening | |
380 | * after we send our scan request. | |
381 | * | |
382 | * Doing it the other way around has a race condition as well, | |
383 | * if you first open the events socket you may get a notification | |
384 | * for a previous scan. | |
385 | * | |
386 | * The only proper way to fix this would be to listen to events | |
387 | * before sending the command, and for the kernel to send the | |
388 | * scan request along with the event, so that you can match up | |
389 | * whether the scan you requested was finished or aborted (this | |
390 | * may result in processing a scan that another application | |
391 | * requested, but that doesn't seem to be a problem). | |
392 | * | |
393 | * Alas, the kernel doesn't do that (yet). | |
394 | */ | |
395 | ||
a5fe4ef2 JB |
396 | if (listen_events(state, ARRAY_SIZE(cmds), cmds) == |
397 | NL80211_CMD_SCAN_ABORTED) { | |
398 | printf("scan aborted!\n"); | |
399 | return 0; | |
400 | } | |
401 | ||
92649eab MH |
402 | if (argc == 3 && !strcmp(argv[2], "-u")) { |
403 | dump_argc = 4; | |
404 | dump_argv[3] = "-u"; | |
405 | } else | |
406 | dump_argc = 3; | |
407 | ||
a5fe4ef2 | 408 | dump_argv[0] = argv[0]; |
92649eab | 409 | return handle_cmd(state, II_NETDEV, dump_argc, dump_argv); |
a5fe4ef2 | 410 | } |
92649eab | 411 | TOPLEVEL(scan, "[-u]", 0, 0, CIB_NETDEV, handle_scan_combined); |