]> git.ipfire.org Git - thirdparty/iw.git/blob - event.c
add P2P Device handling primitives
[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 void parse_cqm_event(struct nlattr **attrs)
104 {
105 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
106 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
107 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
108 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
109 };
110 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
111 struct nlattr *cqm_attr = attrs[NL80211_ATTR_CQM];
112
113 printf("connection quality monitor event: ");
114
115 if (!cqm_attr ||
116 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, cqm_attr, cqm_policy)) {
117 printf("missing data!\n");
118 return;
119 }
120
121 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]) {
122 enum nl80211_cqm_rssi_threshold_event rssi_event;
123 rssi_event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
124 if (rssi_event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH)
125 printf("RSSI went above threshold\n");
126 else
127 printf("RSSI went below threshold\n");
128 } else if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT] &&
129 attrs[NL80211_ATTR_MAC]) {
130 uint32_t frames;
131 char buf[3*6];
132
133 frames = nla_get_u32(cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]);
134 mac_addr_n2a(buf, nla_data(attrs[NL80211_ATTR_MAC]));
135 printf("peer %s didn't ACK %d packets\n", buf, frames);
136 } else
137 printf("unknown event\n");
138 }
139
140 static const char * key_type_str(enum nl80211_key_type key_type)
141 {
142 static char buf[30];
143 switch (key_type) {
144 case NL80211_KEYTYPE_GROUP:
145 return "Group";
146 case NL80211_KEYTYPE_PAIRWISE:
147 return "Pairwise";
148 case NL80211_KEYTYPE_PEERKEY:
149 return "PeerKey";
150 default:
151 snprintf(buf, sizeof(buf), "unknown(%d)", key_type);
152 return buf;
153 }
154 }
155
156 static void parse_mic_failure(struct nlattr **attrs)
157 {
158 printf("Michael MIC failure event:");
159
160 if (attrs[NL80211_ATTR_MAC]) {
161 char addr[3 * ETH_ALEN];
162 mac_addr_n2a(addr, nla_data(attrs[NL80211_ATTR_MAC]));
163 printf(" source MAC address %s", addr);
164 }
165
166 if (attrs[NL80211_ATTR_KEY_SEQ] &&
167 nla_len(attrs[NL80211_ATTR_KEY_SEQ]) == 6) {
168 unsigned char *seq = nla_data(attrs[NL80211_ATTR_KEY_SEQ]);
169 printf(" seq=%02x%02x%02x%02x%02x%02x",
170 seq[0], seq[1], seq[2], seq[3], seq[4], seq[5]);
171 }
172 if (attrs[NL80211_ATTR_KEY_TYPE]) {
173 enum nl80211_key_type key_type =
174 nla_get_u32(attrs[NL80211_ATTR_KEY_TYPE]);
175 printf(" Key Type %s", key_type_str(key_type));
176 }
177
178 if (attrs[NL80211_ATTR_KEY_IDX]) {
179 __u8 key_id = nla_get_u8(attrs[NL80211_ATTR_KEY_IDX]);
180 printf(" Key Id %d", key_id);
181 }
182
183 printf("\n");
184 }
185
186 static int print_event(struct nl_msg *msg, void *arg)
187 {
188 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
189 struct nlattr *tb[NL80211_ATTR_MAX + 1], *nst;
190 struct print_event_args *args = arg;
191 char ifname[100];
192 char macbuf[6*3];
193 __u8 reg_type;
194 struct ieee80211_beacon_channel chan_before_beacon, chan_after_beacon;
195 __u32 wiphy_idx = 0;
196 int rem_nst;
197 __u16 status;
198
199 if (args->time || args->reltime) {
200 unsigned long long usecs, previous;
201
202 previous = 1000000ULL * args->ts.tv_sec + args->ts.tv_usec;
203 gettimeofday(&args->ts, NULL);
204 usecs = 1000000ULL * args->ts.tv_sec + args->ts.tv_usec;
205 if (args->reltime) {
206 if (!args->have_ts) {
207 usecs = 0;
208 args->have_ts = true;
209 } else
210 usecs -= previous;
211 }
212 printf("%llu.%06llu: ", usecs/1000000, usecs % 1000000);
213 }
214
215 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
216 genlmsg_attrlen(gnlh, 0), NULL);
217
218 if (tb[NL80211_ATTR_IFINDEX] && tb[NL80211_ATTR_WIPHY]) {
219 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname);
220 printf("%s (phy #%d): ", ifname, nla_get_u32(tb[NL80211_ATTR_WIPHY]));
221 } else if (tb[NL80211_ATTR_WDEV] && tb[NL80211_ATTR_WIPHY]) {
222 printf("wdev 0x%llx (phy #%d): ",
223 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_WDEV]),
224 nla_get_u32(tb[NL80211_ATTR_WIPHY]));
225 } else if (tb[NL80211_ATTR_IFINDEX]) {
226 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname);
227 printf("%s: ", ifname);
228 } else if (tb[NL80211_ATTR_WDEV]) {
229 printf("wdev 0x%llx: ", (unsigned long long)nla_get_u64(tb[NL80211_ATTR_WDEV]));
230 } else if (tb[NL80211_ATTR_WIPHY]) {
231 printf("phy #%d: ", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
232 }
233
234 switch (gnlh->cmd) {
235 case NL80211_CMD_NEW_WIPHY:
236 printf("renamed to %s\n", nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]));
237 break;
238 case NL80211_CMD_TRIGGER_SCAN:
239 printf("scan started\n");
240 break;
241 case NL80211_CMD_NEW_SCAN_RESULTS:
242 printf("scan finished:");
243 case NL80211_CMD_SCAN_ABORTED:
244 if (gnlh->cmd == NL80211_CMD_SCAN_ABORTED)
245 printf("scan aborted:");
246 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
247 nla_for_each_nested(nst, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem_nst)
248 printf(" %d", nla_get_u32(nst));
249 printf(",");
250 }
251 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
252 nla_for_each_nested(nst, tb[NL80211_ATTR_SCAN_SSIDS], rem_nst) {
253 printf(" \"");
254 print_ssid_escaped(nla_len(nst), nla_data(nst));
255 printf("\"");
256 }
257 }
258 printf("\n");
259 break;
260 case NL80211_CMD_REG_CHANGE:
261 printf("regulatory domain change: ");
262
263 reg_type = nla_get_u8(tb[NL80211_ATTR_REG_TYPE]);
264
265 switch (reg_type) {
266 case NL80211_REGDOM_TYPE_COUNTRY:
267 printf("set to %s by %s request",
268 nla_get_string(tb[NL80211_ATTR_REG_ALPHA2]),
269 reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])));
270 if (tb[NL80211_ATTR_WIPHY])
271 printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
272 break;
273 case NL80211_REGDOM_TYPE_WORLD:
274 printf("set to world roaming by %s request",
275 reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])));
276 break;
277 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
278 printf("custom world roaming rules in place on phy%d by %s request",
279 nla_get_u32(tb[NL80211_ATTR_WIPHY]),
280 reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR])));
281 break;
282 case NL80211_REGDOM_TYPE_INTERSECTION:
283 printf("intersection used due to a request made by %s",
284 reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR])));
285 if (tb[NL80211_ATTR_WIPHY])
286 printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
287 break;
288 default:
289 printf("unknown source (upgrade this utility)");
290 break;
291 }
292
293 printf("\n");
294 break;
295 case NL80211_CMD_REG_BEACON_HINT:
296
297 wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
298
299 memset(&chan_before_beacon, 0, sizeof(chan_before_beacon));
300 memset(&chan_after_beacon, 0, sizeof(chan_after_beacon));
301
302 if (parse_beacon_hint_chan(tb[NL80211_ATTR_FREQ_BEFORE],
303 &chan_before_beacon))
304 break;
305 if (parse_beacon_hint_chan(tb[NL80211_ATTR_FREQ_AFTER],
306 &chan_after_beacon))
307 break;
308
309 if (chan_before_beacon.center_freq != chan_after_beacon.center_freq)
310 break;
311
312 /* A beacon hint is sent _only_ if something _did_ change */
313 printf("beacon hint:\n");
314
315 printf("phy%d %d MHz [%d]:\n",
316 wiphy_idx,
317 chan_before_beacon.center_freq,
318 ieee80211_frequency_to_channel(chan_before_beacon.center_freq));
319
320 if (chan_before_beacon.passive_scan && !chan_after_beacon.passive_scan)
321 printf("\to active scanning enabled\n");
322 if (chan_before_beacon.no_ibss && !chan_after_beacon.no_ibss)
323 printf("\to beaconing enabled\n");
324
325 break;
326 case NL80211_CMD_NEW_STATION:
327 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
328 printf("new station %s\n", macbuf);
329 break;
330 case NL80211_CMD_DEL_STATION:
331 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
332 printf("del station %s\n", macbuf);
333 break;
334 case NL80211_CMD_JOIN_IBSS:
335 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
336 printf("IBSS %s joined\n", macbuf);
337 break;
338 case NL80211_CMD_AUTHENTICATE:
339 printf("auth");
340 if (tb[NL80211_ATTR_FRAME])
341 print_frame(args, tb[NL80211_ATTR_FRAME]);
342 else if (tb[NL80211_ATTR_TIMED_OUT])
343 printf(": timed out");
344 else
345 printf(": unknown event");
346 printf("\n");
347 break;
348 case NL80211_CMD_ASSOCIATE:
349 printf("assoc");
350 if (tb[NL80211_ATTR_FRAME])
351 print_frame(args, tb[NL80211_ATTR_FRAME]);
352 else if (tb[NL80211_ATTR_TIMED_OUT])
353 printf(": timed out");
354 else
355 printf(": unknown event");
356 printf("\n");
357 break;
358 case NL80211_CMD_DEAUTHENTICATE:
359 printf("deauth");
360 print_frame(args, tb[NL80211_ATTR_FRAME]);
361 printf("\n");
362 break;
363 case NL80211_CMD_DISASSOCIATE:
364 printf("disassoc");
365 print_frame(args, tb[NL80211_ATTR_FRAME]);
366 printf("\n");
367 break;
368 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
369 printf("unprotected deauth");
370 print_frame(args, tb[NL80211_ATTR_FRAME]);
371 printf("\n");
372 break;
373 case NL80211_CMD_UNPROT_DISASSOCIATE:
374 printf("unprotected disassoc");
375 print_frame(args, tb[NL80211_ATTR_FRAME]);
376 printf("\n");
377 break;
378 case NL80211_CMD_CONNECT:
379 status = 0;
380 if (!tb[NL80211_ATTR_STATUS_CODE])
381 printf("unknown connect status");
382 else if (nla_get_u16(tb[NL80211_ATTR_STATUS_CODE]) == 0)
383 printf("connected");
384 else {
385 status = nla_get_u16(tb[NL80211_ATTR_STATUS_CODE]);
386 printf("failed to connect");
387 }
388 if (tb[NL80211_ATTR_MAC]) {
389 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
390 printf(" to %s", macbuf);
391 }
392 if (status)
393 printf(", status: %d: %s", status, get_status_str(status));
394 printf("\n");
395 break;
396 case NL80211_CMD_ROAM:
397 printf("roamed");
398 if (tb[NL80211_ATTR_MAC]) {
399 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
400 printf(" to %s", macbuf);
401 }
402 printf("\n");
403 break;
404 case NL80211_CMD_DISCONNECT:
405 printf("disconnected");
406 if (tb[NL80211_ATTR_DISCONNECTED_BY_AP])
407 printf(" (by AP)");
408 else
409 printf(" (local request)");
410 if (tb[NL80211_ATTR_REASON_CODE])
411 printf(" reason: %d: %s", nla_get_u16(tb[NL80211_ATTR_REASON_CODE]),
412 get_reason_str(nla_get_u16(tb[NL80211_ATTR_REASON_CODE])));
413 printf("\n");
414 break;
415 case NL80211_CMD_REMAIN_ON_CHANNEL:
416 printf("remain on freq %d (%dms, cookie %llx)\n",
417 nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]),
418 nla_get_u32(tb[NL80211_ATTR_DURATION]),
419 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]));
420 break;
421 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
422 printf("done with remain on freq %d (cookie %llx)\n",
423 nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]),
424 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]));
425 break;
426 case NL80211_CMD_NOTIFY_CQM:
427 parse_cqm_event(tb);
428 break;
429 case NL80211_CMD_MICHAEL_MIC_FAILURE:
430 parse_mic_failure(tb);
431 break;
432 case NL80211_CMD_FRAME_TX_STATUS:
433 printf("mgmt TX status (cookie %llx): %s\n",
434 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]),
435 tb[NL80211_ATTR_ACK] ? "acked" : "no ack");
436 break;
437 case NL80211_ATTR_PMKSA_CANDIDATE:
438 printf("PMKSA candidate found\n");
439 break;
440 default:
441 printf("unknown event %d\n", gnlh->cmd);
442 break;
443 }
444
445 fflush(stdout);
446 return NL_SKIP;
447 }
448
449 struct wait_event {
450 int n_cmds;
451 const __u32 *cmds;
452 __u32 cmd;
453 struct print_event_args *pargs;
454 };
455
456 static int wait_event(struct nl_msg *msg, void *arg)
457 {
458 struct wait_event *wait = arg;
459 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
460 int i;
461
462 for (i = 0; i < wait->n_cmds; i++) {
463 if (gnlh->cmd == wait->cmds[i]) {
464 wait->cmd = gnlh->cmd;
465 if (wait->pargs)
466 print_event(msg, wait->pargs);
467 }
468 }
469
470 return NL_SKIP;
471 }
472
473 int __prepare_listen_events(struct nl80211_state *state)
474 {
475 int mcid, ret;
476
477 /* Configuration multicast group */
478 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "config");
479 if (mcid < 0)
480 return mcid;
481
482 ret = nl_socket_add_membership(state->nl_sock, mcid);
483 if (ret)
484 return ret;
485
486 /* Scan multicast group */
487 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "scan");
488 if (mcid >= 0) {
489 ret = nl_socket_add_membership(state->nl_sock, mcid);
490 if (ret)
491 return ret;
492 }
493
494 /* Regulatory multicast group */
495 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "regulatory");
496 if (mcid >= 0) {
497 ret = nl_socket_add_membership(state->nl_sock, mcid);
498 if (ret)
499 return ret;
500 }
501
502 /* MLME multicast group */
503 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "mlme");
504 if (mcid >= 0) {
505 ret = nl_socket_add_membership(state->nl_sock, mcid);
506 if (ret)
507 return ret;
508 }
509
510 return 0;
511 }
512
513 __u32 __do_listen_events(struct nl80211_state *state,
514 const int n_waits, const __u32 *waits,
515 struct print_event_args *args)
516 {
517 struct nl_cb *cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
518 struct wait_event wait_ev;
519
520 if (!cb) {
521 fprintf(stderr, "failed to allocate netlink callbacks\n");
522 return -ENOMEM;
523 }
524
525 /* no sequence checking for multicast messages */
526 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
527
528 if (n_waits && waits) {
529 wait_ev.cmds = waits;
530 wait_ev.n_cmds = n_waits;
531 wait_ev.pargs = args;
532 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, wait_event, &wait_ev);
533 } else
534 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_event, args);
535
536 wait_ev.cmd = 0;
537
538 while (!wait_ev.cmd)
539 nl_recvmsgs(state->nl_sock, cb);
540
541 nl_cb_put(cb);
542
543 return wait_ev.cmd;
544 }
545
546 __u32 listen_events(struct nl80211_state *state,
547 const int n_waits, const __u32 *waits)
548 {
549 int ret;
550
551 ret = __prepare_listen_events(state);
552 if (ret)
553 return ret;
554
555 return __do_listen_events(state, n_waits, waits, NULL);
556 }
557
558 static int print_events(struct nl80211_state *state,
559 struct nl_cb *cb,
560 struct nl_msg *msg,
561 int argc, char **argv,
562 enum id_input id)
563 {
564 struct print_event_args args;
565 int ret;
566
567 memset(&args, 0, sizeof(args));
568
569 argc--;
570 argv++;
571
572 while (argc > 0) {
573 if (strcmp(argv[0], "-f") == 0)
574 args.frame = true;
575 else if (strcmp(argv[0], "-t") == 0)
576 args.time = true;
577 else if (strcmp(argv[0], "-r") == 0)
578 args.reltime = true;
579 else
580 return 1;
581 argc--;
582 argv++;
583 }
584
585 if (args.time && args.reltime)
586 return 1;
587
588 if (argc)
589 return 1;
590
591 ret = __prepare_listen_events(state);
592 if (ret)
593 return ret;
594
595 return __do_listen_events(state, 0, NULL, &args);
596 }
597 TOPLEVEL(event, "[-t] [-r] [-f]", 0, 0, CIB_NONE, print_events,
598 "Monitor events from the kernel.\n"
599 "-t - print timestamp\n"
600 "-r - print relative timstamp\n"
601 "-f - print full frame for auth/assoc etc.");