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