]>
Commit | Line | Data |
---|---|---|
957a0f07 JB |
1 | #include <stdint.h> |
2 | #include <stdbool.h> | |
3 | #include <net/if.h> | |
4 | #include <errno.h> | |
5 | #include "iw.h" | |
6 | ||
7 | static int no_seq_check(struct nl_msg *msg, void *arg) | |
8 | { | |
9 | return NL_OK; | |
10 | } | |
11 | ||
12 | struct print_event_args { | |
13 | bool frame, time; | |
14 | }; | |
15 | ||
16 | static void print_frame(struct print_event_args *args, struct nlattr *attr) | |
17 | { | |
18 | uint8_t *frame; | |
19 | size_t len; | |
20 | int i; | |
21 | char macbuf[6*3]; | |
22 | uint16_t tmp; | |
23 | ||
24 | if (!attr) | |
25 | printf(" [no frame]"); | |
26 | ||
27 | frame = nla_data(attr); | |
28 | len = nla_len(attr); | |
29 | ||
30 | if (len < 26) { | |
31 | printf(" [invalid frame: "); | |
32 | goto print_frame; | |
33 | } | |
34 | ||
35 | mac_addr_n2a(macbuf, frame + 10); | |
36 | printf(" %s -> ", macbuf); | |
37 | mac_addr_n2a(macbuf, frame + 4); | |
38 | printf("%s", macbuf); | |
39 | ||
40 | switch (frame[0] & 0xfc) { | |
41 | case 0x10: /* assoc resp */ | |
42 | case 0x30: /* reassoc resp */ | |
43 | /* status */ | |
44 | tmp = (frame[27] << 8) + frame[26]; | |
45 | printf(" status: %d: %s", tmp, get_status_str(tmp)); | |
46 | break; | |
47 | case 0x00: /* assoc req */ | |
48 | case 0x20: /* reassoc req */ | |
49 | break; | |
50 | case 0xb0: /* auth */ | |
51 | /* status */ | |
52 | tmp = (frame[29] << 8) + frame[28]; | |
53 | printf(" status: %d: %s", tmp, get_status_str(tmp)); | |
54 | break; | |
55 | break; | |
56 | case 0xa0: /* disassoc */ | |
57 | case 0xc0: /* deauth */ | |
58 | /* reason */ | |
59 | tmp = (frame[25] << 8) + frame[24]; | |
60 | printf(" reason %d: %s", tmp, get_reason_str(tmp)); | |
61 | break; | |
62 | } | |
63 | ||
64 | if (!args->frame) | |
65 | return; | |
66 | ||
67 | printf(" [frame:"); | |
68 | ||
69 | print_frame: | |
70 | for (i = 0; i < len; i++) | |
71 | printf(" %.02x", frame[i]); | |
72 | printf("]"); | |
73 | } | |
74 | ||
75 | static int print_event(struct nl_msg *msg, void *arg) | |
76 | { | |
77 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
748f8489 | 78 | struct nlattr *tb[NL80211_ATTR_MAX + 1], *nst; |
957a0f07 JB |
79 | struct print_event_args *args = arg; |
80 | char ifname[100]; | |
81 | char macbuf[6*3]; | |
82 | __u8 reg_type; | |
748f8489 | 83 | int rem_nst; |
f1a666a6 | 84 | __u16 status; |
957a0f07 JB |
85 | |
86 | if (args->time) { | |
87 | struct timeval tv; | |
88 | gettimeofday(&tv, NULL); | |
89 | printf("%ld.%06u: ", (long) tv.tv_sec, (unsigned int) tv.tv_usec); | |
90 | } | |
91 | ||
92 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
93 | genlmsg_attrlen(gnlh, 0), NULL); | |
94 | ||
95 | if (tb[NL80211_ATTR_IFINDEX] && tb[NL80211_ATTR_WIPHY]) { | |
96 | if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname); | |
97 | printf("%s (phy #%d): ", ifname, nla_get_u32(tb[NL80211_ATTR_WIPHY])); | |
98 | } else if (tb[NL80211_ATTR_IFINDEX]) { | |
99 | if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname); | |
100 | printf("%s: ", ifname); | |
101 | } else if (tb[NL80211_ATTR_WIPHY]) { | |
102 | printf("phy #%d: ", nla_get_u32(tb[NL80211_ATTR_WIPHY])); | |
103 | } | |
104 | ||
105 | switch (gnlh->cmd) { | |
106 | case NL80211_CMD_NEW_WIPHY: | |
107 | printf("renamed to %s\n", nla_get_string(tb[NL80211_ATTR_WIPHY_NAME])); | |
108 | break; | |
e67be815 JB |
109 | case NL80211_CMD_TRIGGER_SCAN: |
110 | printf("scan started\n"); | |
111 | break; | |
957a0f07 | 112 | case NL80211_CMD_NEW_SCAN_RESULTS: |
748f8489 | 113 | printf("scan finished:"); |
957a0f07 | 114 | case NL80211_CMD_SCAN_ABORTED: |
748f8489 JB |
115 | if (gnlh->cmd == NL80211_CMD_SCAN_ABORTED) |
116 | printf("scan aborted:"); | |
117 | if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) { | |
118 | nla_for_each_nested(nst, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem_nst) | |
119 | printf(" %d", nla_get_u32(nst)); | |
120 | printf(","); | |
121 | } | |
122 | if (tb[NL80211_ATTR_SCAN_SSIDS]) { | |
123 | nla_for_each_nested(nst, tb[NL80211_ATTR_SCAN_SSIDS], rem_nst) { | |
124 | printf(" \""); | |
125 | print_ssid_escaped(nla_len(nst), nla_data(nst)); | |
126 | printf("\""); | |
127 | } | |
128 | } | |
129 | printf("\n"); | |
957a0f07 JB |
130 | break; |
131 | case NL80211_CMD_REG_CHANGE: | |
132 | printf("regulatory domain change: "); | |
133 | ||
134 | reg_type = nla_get_u8(tb[NL80211_ATTR_REG_TYPE]); | |
135 | ||
136 | switch (reg_type) { | |
137 | case NL80211_REGDOM_TYPE_COUNTRY: | |
138 | printf("set to %s by %s request", | |
139 | nla_get_string(tb[NL80211_ATTR_REG_ALPHA2]), | |
140 | reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]))); | |
141 | if (tb[NL80211_ATTR_WIPHY]) | |
142 | printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY])); | |
143 | break; | |
144 | case NL80211_REGDOM_TYPE_WORLD: | |
145 | printf("set to world roaming by %s request", | |
146 | reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]))); | |
147 | break; | |
148 | case NL80211_REGDOM_TYPE_CUSTOM_WORLD: | |
149 | printf("custom world roaming rules in place on phy%d by %s request", | |
150 | nla_get_u32(tb[NL80211_ATTR_WIPHY]), | |
151 | reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR]))); | |
152 | break; | |
153 | case NL80211_REGDOM_TYPE_INTERSECTION: | |
154 | printf("intersection used due to a request made by %s", | |
155 | reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR]))); | |
156 | if (tb[NL80211_ATTR_WIPHY]) | |
157 | printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY])); | |
158 | break; | |
159 | default: | |
160 | printf("unknown source (upgrade this utility)"); | |
161 | break; | |
162 | } | |
163 | ||
164 | printf("\n"); | |
165 | break; | |
166 | case NL80211_CMD_JOIN_IBSS: | |
167 | mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC])); | |
168 | printf("IBSS %s joined\n", macbuf); | |
169 | break; | |
170 | case NL80211_CMD_AUTHENTICATE: | |
171 | printf("auth"); | |
aea5dbd2 JB |
172 | if (tb[NL80211_ATTR_FRAME]) |
173 | print_frame(args, tb[NL80211_ATTR_FRAME]); | |
174 | else if (tb[NL80211_ATTR_TIMED_OUT]) | |
175 | printf(": timed out"); | |
176 | else | |
177 | printf(": unknown event"); | |
957a0f07 JB |
178 | printf("\n"); |
179 | break; | |
180 | case NL80211_CMD_ASSOCIATE: | |
181 | printf("assoc"); | |
aea5dbd2 JB |
182 | if (tb[NL80211_ATTR_FRAME]) |
183 | print_frame(args, tb[NL80211_ATTR_FRAME]); | |
184 | else if (tb[NL80211_ATTR_TIMED_OUT]) | |
185 | printf(": timed out"); | |
186 | else | |
187 | printf(": unknown event"); | |
957a0f07 JB |
188 | printf("\n"); |
189 | break; | |
190 | case NL80211_CMD_DEAUTHENTICATE: | |
191 | printf("deauth"); | |
192 | print_frame(args, tb[NL80211_ATTR_FRAME]); | |
193 | printf("\n"); | |
194 | break; | |
195 | case NL80211_CMD_DISASSOCIATE: | |
196 | printf("disassoc"); | |
197 | print_frame(args, tb[NL80211_ATTR_FRAME]); | |
198 | printf("\n"); | |
199 | break; | |
f1a666a6 JB |
200 | case NL80211_CMD_CONNECT: |
201 | status = 0; | |
202 | if (!tb[NL80211_ATTR_STATUS_CODE]) | |
203 | printf("unknown connect status"); | |
204 | else if (nla_get_u16(tb[NL80211_ATTR_STATUS_CODE]) == 0) | |
205 | printf("connected"); | |
206 | else { | |
207 | status = nla_get_u16(tb[NL80211_ATTR_STATUS_CODE]); | |
208 | printf("failed to connect"); | |
209 | } | |
210 | if (tb[NL80211_ATTR_MAC]) { | |
211 | mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC])); | |
212 | printf(" to %s", macbuf); | |
213 | } | |
214 | if (status) | |
215 | printf(", status: %d: %s", status, get_status_str(status)); | |
216 | printf("\n"); | |
217 | break; | |
218 | case NL80211_CMD_ROAM: | |
219 | printf("roamed"); | |
220 | if (tb[NL80211_ATTR_MAC]) { | |
221 | mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC])); | |
222 | printf(" to %s", macbuf); | |
223 | } | |
224 | printf("\n"); | |
225 | break; | |
226 | case NL80211_CMD_DISCONNECT: | |
227 | printf("disconnected"); | |
228 | if (tb[NL80211_ATTR_DISCONNECTED_BY_AP]) | |
229 | printf(" (by AP)"); | |
230 | else | |
231 | printf(" (local request)"); | |
232 | if (tb[NL80211_ATTR_REASON_CODE]) | |
233 | printf(" reason: %d: %s", nla_get_u16(tb[NL80211_ATTR_REASON_CODE]), | |
234 | get_reason_str(nla_get_u16(tb[NL80211_ATTR_REASON_CODE]))); | |
235 | printf("\n"); | |
236 | break; | |
957a0f07 JB |
237 | default: |
238 | printf("unknown event %d\n", gnlh->cmd); | |
239 | break; | |
240 | } | |
241 | ||
242 | return NL_SKIP; | |
243 | } | |
244 | ||
245 | struct wait_event { | |
246 | int n_cmds; | |
247 | const __u32 *cmds; | |
248 | __u32 cmd; | |
249 | }; | |
250 | ||
251 | static int wait_event(struct nl_msg *msg, void *arg) | |
252 | { | |
253 | struct wait_event *wait = arg; | |
254 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
255 | int i; | |
256 | ||
257 | for (i = 0; i < wait->n_cmds; i++) { | |
258 | if (gnlh->cmd == wait->cmds[i]) { | |
259 | wait->cmd = gnlh->cmd; | |
260 | } | |
261 | } | |
262 | ||
263 | return NL_SKIP; | |
264 | } | |
265 | ||
266 | static __u32 __listen_events(struct nl80211_state *state, | |
267 | const int n_waits, const __u32 *waits, | |
268 | struct print_event_args *args) | |
269 | { | |
270 | int mcid, ret; | |
271 | struct nl_cb *cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT); | |
272 | struct wait_event wait_ev; | |
273 | ||
274 | if (!cb) { | |
275 | fprintf(stderr, "failed to allocate netlink callbacks\n"); | |
276 | return -ENOMEM; | |
277 | } | |
278 | ||
279 | /* Configuration multicast group */ | |
280 | mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "config"); | |
281 | if (mcid < 0) | |
282 | return mcid; | |
283 | ||
284 | ret = nl_socket_add_membership(state->nl_sock, mcid); | |
285 | if (ret) | |
286 | return ret; | |
287 | ||
288 | /* Scan multicast group */ | |
289 | mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "scan"); | |
290 | if (mcid >= 0) { | |
291 | ret = nl_socket_add_membership(state->nl_sock, mcid); | |
292 | if (ret) | |
293 | return ret; | |
294 | } | |
295 | ||
296 | /* Regulatory multicast group */ | |
297 | mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "regulatory"); | |
298 | if (mcid >= 0) { | |
299 | ret = nl_socket_add_membership(state->nl_sock, mcid); | |
300 | if (ret) | |
301 | return ret; | |
302 | } | |
303 | ||
304 | /* MLME multicast group */ | |
305 | mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "mlme"); | |
306 | if (mcid >= 0) { | |
307 | ret = nl_socket_add_membership(state->nl_sock, mcid); | |
308 | if (ret) | |
309 | return ret; | |
310 | } | |
311 | ||
312 | /* no sequence checking for multicast messages */ | |
313 | nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL); | |
314 | ||
315 | if (n_waits && waits) { | |
316 | wait_ev.cmds = waits; | |
317 | wait_ev.n_cmds = n_waits; | |
318 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, wait_event, &wait_ev); | |
319 | } else { | |
320 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_event, args); | |
321 | } | |
322 | ||
323 | wait_ev.cmd = 0; | |
324 | ||
325 | while (!wait_ev.cmd) | |
326 | nl_recvmsgs(state->nl_sock, cb); | |
327 | ||
328 | nl_cb_put(cb); | |
329 | ||
330 | return wait_ev.cmd; | |
331 | } | |
332 | ||
333 | __u32 listen_events(struct nl80211_state *state, | |
334 | const int n_waits, const __u32 *waits) | |
335 | { | |
336 | return __listen_events(state, n_waits, waits, NULL); | |
337 | } | |
338 | ||
339 | static int print_events(struct nl80211_state *state, | |
340 | struct nl_cb *cb, | |
341 | struct nl_msg *msg, | |
342 | int argc, char **argv) | |
343 | { | |
344 | struct print_event_args args; | |
345 | ||
346 | memset(&args, 0, sizeof(args)); | |
347 | ||
348 | argc--; | |
349 | argv++; | |
350 | ||
351 | while (argc > 0) { | |
352 | if (strcmp(argv[0], "-f") == 0) | |
353 | args.frame = true; | |
354 | else if (strcmp(argv[0], "-t") == 0) | |
355 | args.time = true; | |
356 | else | |
357 | return 1; | |
358 | argc--; | |
359 | argv++; | |
360 | } | |
361 | ||
362 | if (argc) | |
363 | return 1; | |
364 | ||
365 | return __listen_events(state, 0, NULL, &args); | |
366 | } | |
01ae06f9 JB |
367 | TOPLEVEL(event, "[-t] [-f]", 0, 0, CIB_NONE, print_events, |
368 | "Monitor events from the kernel.\n" | |
369 | "-t - print timestamp\n" | |
370 | "-f - print full frame for auth/assoc etc."); |