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