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