]>
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 | ||
764fe753 JB |
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) | |
3563f4c5 JB |
93 | { |
94 | int i; | |
95 | ||
fbf80af5 JB |
96 | if (len < 3) { |
97 | printf("\tVendor specific: <too short> data:\n"); | |
98 | for(i = 0; i < len; i++) | |
99 | printf(" %.02x", data[i]); | |
100 | printf("\n"); | |
101 | return; | |
102 | } | |
103 | ||
764fe753 JB |
104 | /* currently _all_ vendor IEs are unknown (not parsed) */ |
105 | if (!params->unknown) | |
106 | return; | |
107 | ||
fbf80af5 | 108 | printf("\tVendor specific: OUI %.2x:%.2x:%.2x, data:", |
3563f4c5 | 109 | data[0], data[1], data[2]); |
fbf80af5 JB |
110 | for (i = 3; i < len; i++) |
111 | printf(" %.2x", data[i]); | |
3563f4c5 JB |
112 | printf("\n"); |
113 | } | |
114 | ||
764fe753 | 115 | static void print_ies(unsigned char *ie, int ielen, struct scan_params *params) |
3563f4c5 JB |
116 | { |
117 | while (ielen >= 2 && ielen >= ie[1]) { | |
97ebbaf5 | 118 | if (ie[0] < ARRAY_SIZE(ieprinters) && ieprinters[ie[0]]) { |
3563f4c5 | 119 | ieprinters[ie[0]](ie[0], ie[1], ie + 2); |
764fe753 JB |
120 | } else if (ie[0] == 221 /* vendor */) { |
121 | print_vendor(ie[1], ie + 2, params); | |
122 | } else if (params->unknown) { | |
3563f4c5 JB |
123 | int i; |
124 | ||
8086b700 | 125 | printf("\tUnknown IE (%d):", ie[0]); |
3563f4c5 | 126 | for (i=0; i<ie[1]; i++) |
8086b700 | 127 | printf(" %.2x", ie[2+i]); |
3563f4c5 JB |
128 | printf("\n"); |
129 | } | |
130 | ielen -= ie[1] + 2; | |
131 | ie += ie[1] + 2; | |
132 | } | |
133 | } | |
134 | ||
135 | static int print_bss_handler(struct nl_msg *msg, void *arg) | |
136 | { | |
137 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
138 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
139 | struct nlattr *bss[NL80211_BSS_MAX + 1]; | |
140 | char mac_addr[20], dev[20]; | |
141 | static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = { | |
142 | [NL80211_BSS_TSF] = { .type = NLA_U64 }, | |
143 | [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 }, | |
144 | [NL80211_BSS_BSSID] = { }, | |
145 | [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 }, | |
146 | [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 }, | |
147 | [NL80211_BSS_INFORMATION_ELEMENTS] = { }, | |
f2e17e1f JB |
148 | [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 }, |
149 | [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 }, | |
3563f4c5 JB |
150 | }; |
151 | ||
152 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
153 | genlmsg_attrlen(gnlh, 0), NULL); | |
154 | ||
155 | if (!tb[NL80211_ATTR_BSS]) { | |
156 | fprintf(stderr, "bss info missing!"); | |
157 | return NL_SKIP; | |
158 | } | |
159 | if (nla_parse_nested(bss, NL80211_BSS_MAX, | |
160 | tb[NL80211_ATTR_BSS], | |
161 | bss_policy)) { | |
162 | fprintf(stderr, "failed to parse nested attributes!"); | |
163 | return NL_SKIP; | |
164 | } | |
165 | ||
166 | if (!bss[NL80211_BSS_BSSID]) | |
167 | return NL_SKIP; | |
168 | ||
169 | mac_addr_n2a(mac_addr, nla_data(bss[NL80211_BSS_BSSID])); | |
170 | if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), dev); | |
171 | printf("BSS %s (on %s)\n", mac_addr, dev); | |
172 | ||
e7109a8a JB |
173 | if (bss[NL80211_BSS_TSF]) { |
174 | unsigned long long tsf; | |
175 | tsf = (unsigned long long)nla_get_u64(bss[NL80211_BSS_TSF]); | |
176 | printf("\tTSF: %llu usec (%llud, %.2lld:%.2llu:%.2llu)\n", | |
177 | tsf, tsf/1000/1000/60/60/24, (tsf/1000/1000/60/60) % 24, | |
178 | (tsf/1000/1000/60) % 60, (tsf/1000/1000) % 60); | |
179 | } | |
3563f4c5 JB |
180 | if (bss[NL80211_BSS_FREQUENCY]) |
181 | printf("\tfreq: %d\n", | |
182 | nla_get_u32(bss[NL80211_BSS_FREQUENCY])); | |
183 | if (bss[NL80211_BSS_BEACON_INTERVAL]) | |
184 | printf("\tbeacon interval: %d\n", | |
185 | nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL])); | |
186 | if (bss[NL80211_BSS_CAPABILITY]) | |
187 | printf("\tcapability: 0x%.4x\n", | |
188 | nla_get_u16(bss[NL80211_BSS_CAPABILITY])); | |
f2e17e1f JB |
189 | if (bss[NL80211_BSS_SIGNAL_MBM]) { |
190 | int s = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]); | |
191 | printf("\tsignal: %d.%.2d dBm\n", s/100, s%100); | |
192 | } | |
193 | if (bss[NL80211_BSS_SIGNAL_UNSPEC]) { | |
194 | unsigned char s = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]); | |
195 | printf("\tsignal: %d/100\n", s); | |
196 | } | |
3563f4c5 JB |
197 | if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) |
198 | print_ies(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]), | |
764fe753 JB |
199 | nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]), |
200 | arg); | |
3563f4c5 JB |
201 | |
202 | return NL_SKIP; | |
203 | } | |
204 | ||
764fe753 | 205 | static struct scan_params scan_params; |
3563f4c5 | 206 | |
7c37a24d JB |
207 | static int handle_scan_dump(struct nl80211_state *state, |
208 | struct nl_cb *cb, | |
3563f4c5 JB |
209 | struct nl_msg *msg, |
210 | int argc, char **argv) | |
211 | { | |
764fe753 JB |
212 | if (argc > 1) |
213 | return 1; | |
214 | ||
215 | scan_params.unknown = false; | |
216 | if (argc == 1 && !strcmp(argv[0], "-u")) | |
217 | scan_params.unknown = true; | |
218 | ||
219 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_bss_handler, | |
220 | &scan_params); | |
3563f4c5 JB |
221 | return 0; |
222 | } | |
764fe753 | 223 | COMMAND(scan, dump, "[-u]", |
3563f4c5 | 224 | NL80211_CMD_GET_SCAN, NLM_F_DUMP, CIB_NETDEV, handle_scan_dump); |
a5fe4ef2 JB |
225 | |
226 | static int handle_scan_combined(struct nl80211_state *state, | |
227 | struct nl_cb *cb, | |
228 | struct nl_msg *msg, | |
229 | int argc, char **argv) | |
230 | { | |
231 | static char *trig_argv[] = { | |
232 | NULL, | |
233 | "scan", | |
234 | "trigger", | |
235 | }; | |
236 | static char *dump_argv[] = { | |
237 | NULL, | |
238 | "scan", | |
239 | "dump", | |
240 | }; | |
241 | static const __u32 cmds[] = { | |
242 | NL80211_CMD_NEW_SCAN_RESULTS, | |
243 | NL80211_CMD_SCAN_ABORTED, | |
244 | }; | |
245 | int err; | |
246 | ||
247 | trig_argv[0] = argv[0]; | |
248 | err = handle_cmd(state, II_NETDEV, ARRAY_SIZE(trig_argv), trig_argv); | |
249 | if (err) | |
250 | return err; | |
251 | ||
61725dbe JB |
252 | /* |
253 | * WARNING: DO NOT COPY THIS CODE INTO YOUR APPLICATION | |
254 | * | |
255 | * This code has a bug, which requires creating a separate | |
256 | * nl80211 socket to fix: | |
257 | * It is possible for a NL80211_CMD_NEW_SCAN_RESULTS or | |
258 | * NL80211_CMD_SCAN_ABORTED message to be sent by the kernel | |
259 | * before (!) we listen to it, because we only start listening | |
260 | * after we send our scan request. | |
261 | * | |
262 | * Doing it the other way around has a race condition as well, | |
263 | * if you first open the events socket you may get a notification | |
264 | * for a previous scan. | |
265 | * | |
266 | * The only proper way to fix this would be to listen to events | |
267 | * before sending the command, and for the kernel to send the | |
268 | * scan request along with the event, so that you can match up | |
269 | * whether the scan you requested was finished or aborted (this | |
270 | * may result in processing a scan that another application | |
271 | * requested, but that doesn't seem to be a problem). | |
272 | * | |
273 | * Alas, the kernel doesn't do that (yet). | |
274 | */ | |
275 | ||
a5fe4ef2 JB |
276 | if (listen_events(state, ARRAY_SIZE(cmds), cmds) == |
277 | NL80211_CMD_SCAN_ABORTED) { | |
278 | printf("scan aborted!\n"); | |
279 | return 0; | |
280 | } | |
281 | ||
282 | dump_argv[0] = argv[0]; | |
283 | return handle_cmd(state, II_NETDEV, ARRAY_SIZE(dump_argv), dump_argv); | |
284 | } | |
285 | TOPLEVEL(scan, NULL, 0, 0, CIB_NETDEV, handle_scan_combined); |