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