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