]> git.ipfire.org Git - thirdparty/iw.git/blob - scan.c
add "dev" command to list all wireless interfaces
[thirdparty/iw.git] / scan.c
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 print_vendor(unsigned char len, unsigned char *data,
92 struct scan_params *params)
93 {
94 int i;
95
96 /* currently _all_ vendor IEs are unknown (not parsed) */
97 if (!params->unknown)
98 return;
99
100 printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data: ",
101 data[0], data[1], data[2]);
102 for (i=3; i<len; i++)
103 printf("\\x%.2x", data[i]);
104 printf("\n");
105 }
106
107 static void print_ies(unsigned char *ie, int ielen, struct scan_params *params)
108 {
109 while (ielen >= 2 && ielen >= ie[1]) {
110 if (ie[0] < ARRAY_SIZE(ieprinters) && ieprinters[ie[0]]) {
111 ieprinters[ie[0]](ie[0], ie[1], ie + 2);
112 } else if (ie[0] == 221 /* vendor */) {
113 print_vendor(ie[1], ie + 2, params);
114 } else if (params->unknown) {
115 int i;
116
117 printf("\tUnknown IE (%d): ", ie[0]);
118 for (i=0; i<ie[1]; i++)
119 printf("\\x%.2x", ie[2+i]);
120 printf("\n");
121 }
122 ielen -= ie[1] + 2;
123 ie += ie[1] + 2;
124 }
125 }
126
127 static int print_bss_handler(struct nl_msg *msg, void *arg)
128 {
129 struct nlattr *tb[NL80211_ATTR_MAX + 1];
130 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
131 struct nlattr *bss[NL80211_BSS_MAX + 1];
132 char mac_addr[20], dev[20];
133 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
134 [NL80211_BSS_TSF] = { .type = NLA_U64 },
135 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
136 [NL80211_BSS_BSSID] = { },
137 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
138 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
139 [NL80211_BSS_INFORMATION_ELEMENTS] = { },
140 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
141 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
142 };
143
144 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
145 genlmsg_attrlen(gnlh, 0), NULL);
146
147 if (!tb[NL80211_ATTR_BSS]) {
148 fprintf(stderr, "bss info missing!");
149 return NL_SKIP;
150 }
151 if (nla_parse_nested(bss, NL80211_BSS_MAX,
152 tb[NL80211_ATTR_BSS],
153 bss_policy)) {
154 fprintf(stderr, "failed to parse nested attributes!");
155 return NL_SKIP;
156 }
157
158 if (!bss[NL80211_BSS_BSSID])
159 return NL_SKIP;
160
161 mac_addr_n2a(mac_addr, nla_data(bss[NL80211_BSS_BSSID]));
162 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev);
163 printf("BSS %s (on %s)\n", mac_addr, dev);
164
165 if (bss[NL80211_BSS_TSF]) {
166 unsigned long long tsf;
167 tsf = (unsigned long long)nla_get_u64(bss[NL80211_BSS_TSF]);
168 printf("\tTSF: %llu usec (%llud, %.2lld:%.2llu:%.2llu)\n",
169 tsf, tsf/1000/1000/60/60/24, (tsf/1000/1000/60/60) % 24,
170 (tsf/1000/1000/60) % 60, (tsf/1000/1000) % 60);
171 }
172 if (bss[NL80211_BSS_FREQUENCY])
173 printf("\tfreq: %d\n",
174 nla_get_u32(bss[NL80211_BSS_FREQUENCY]));
175 if (bss[NL80211_BSS_BEACON_INTERVAL])
176 printf("\tbeacon interval: %d\n",
177 nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]));
178 if (bss[NL80211_BSS_CAPABILITY])
179 printf("\tcapability: 0x%.4x\n",
180 nla_get_u16(bss[NL80211_BSS_CAPABILITY]));
181 if (bss[NL80211_BSS_SIGNAL_MBM]) {
182 int s = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
183 printf("\tsignal: %d.%.2d dBm\n", s/100, s%100);
184 }
185 if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
186 unsigned char s = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
187 printf("\tsignal: %d/100\n", s);
188 }
189 if (bss[NL80211_BSS_INFORMATION_ELEMENTS])
190 print_ies(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
191 nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
192 arg);
193
194 return NL_SKIP;
195 }
196
197 static struct scan_params scan_params;
198
199 static int handle_scan_dump(struct nl80211_state *state,
200 struct nl_cb *cb,
201 struct nl_msg *msg,
202 int argc, char **argv)
203 {
204 if (argc > 1)
205 return 1;
206
207 scan_params.unknown = false;
208 if (argc == 1 && !strcmp(argv[0], "-u"))
209 scan_params.unknown = true;
210
211 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_bss_handler,
212 &scan_params);
213 return 0;
214 }
215 COMMAND(scan, dump, "[-u]",
216 NL80211_CMD_GET_SCAN, NLM_F_DUMP, CIB_NETDEV, handle_scan_dump);
217
218 static int handle_scan_combined(struct nl80211_state *state,
219 struct nl_cb *cb,
220 struct nl_msg *msg,
221 int argc, char **argv)
222 {
223 static char *trig_argv[] = {
224 NULL,
225 "scan",
226 "trigger",
227 };
228 static char *dump_argv[] = {
229 NULL,
230 "scan",
231 "dump",
232 };
233 static const __u32 cmds[] = {
234 NL80211_CMD_NEW_SCAN_RESULTS,
235 NL80211_CMD_SCAN_ABORTED,
236 };
237 int err;
238
239 trig_argv[0] = argv[0];
240 err = handle_cmd(state, II_NETDEV, ARRAY_SIZE(trig_argv), trig_argv);
241 if (err)
242 return err;
243
244 if (listen_events(state, ARRAY_SIZE(cmds), cmds) ==
245 NL80211_CMD_SCAN_ABORTED) {
246 printf("scan aborted!\n");
247 return 0;
248 }
249
250 dump_argv[0] = argv[0];
251 return handle_cmd(state, II_NETDEV, ARRAY_SIZE(dump_argv), dump_argv);
252 }
253 TOPLEVEL(scan, NULL, 0, 0, CIB_NETDEV, handle_scan_combined);