]> git.ipfire.org Git - thirdparty/iw.git/blame - event.c
iw: only use tags matching "v*"
[thirdparty/iw.git] / event.c
CommitLineData
957a0f07
JB
1#include <stdint.h>
2#include <stdbool.h>
3#include <net/if.h>
4#include <errno.h>
5#include "iw.h"
6
7static int no_seq_check(struct nl_msg *msg, void *arg)
8{
9 return NL_OK;
10}
11
0168589d
LR
12struct ieee80211_beacon_channel {
13 __u16 center_freq;
f0c48e7b 14 bool no_ir;
0168589d
LR
15 bool no_ibss;
16};
17
18static 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 },
f0c48e7b
IP
24 [NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
25 [__NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
0168589d
LR
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
f0c48e7b
IP
36 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
37 chan->no_ir = true;
38 if (tb_freq[__NL80211_FREQUENCY_ATTR_NO_IBSS])
0168589d
LR
39 chan->no_ibss = true;
40
41 return 0;
42}
43
957a0f07
JB
44static 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;
957a0f07
JB
83 case 0xa0: /* disassoc */
84 case 0xc0: /* deauth */
85 /* reason */
86 tmp = (frame[25] << 8) + frame[24];
87 printf(" reason %d: %s", tmp, get_reason_str(tmp));
88 break;
89 }
90
91 if (!args->frame)
92 return;
93
94 printf(" [frame:");
95
96 print_frame:
97 for (i = 0; i < len; i++)
98 printf(" %.02x", frame[i]);
99 printf("]");
100}
101
f19444fe 102static void parse_cqm_event(struct nlattr **attrs)
7988b229
JO
103{
104 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
105 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
106 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
107 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
108 };
109 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
f19444fe 110 struct nlattr *cqm_attr = attrs[NL80211_ATTR_CQM];
7988b229 111
e5497f12 112 printf("CQM event: ");
7988b229 113
f19444fe
JB
114 if (!cqm_attr ||
115 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, cqm_attr, cqm_policy)) {
7988b229
JO
116 printf("missing data!\n");
117 return;
118 }
119
120 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]) {
121 enum nl80211_cqm_rssi_threshold_event rssi_event;
e5497f12
BG
122 bool found_one = false;
123
7988b229 124 rssi_event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
e5497f12
BG
125
126 switch (rssi_event) {
127 case NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH:
f19444fe 128 printf("RSSI went above threshold\n");
e5497f12
BG
129 found_one = true;
130 break;
131 case NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW:
f19444fe 132 printf("RSSI went below threshold\n");
e5497f12
BG
133 found_one = true;
134 break;
135 case NL80211_CQM_RSSI_BEACON_LOSS_EVENT:
136 printf("Beacon loss detected\n");
137 found_one = true;
138 break;
139 }
140
141 if (!found_one)
142 printf("Unknown event type: %i\n", rssi_event);
f19444fe
JB
143 } else if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT] &&
144 attrs[NL80211_ATTR_MAC]) {
145 uint32_t frames;
146 char buf[3*6];
147
148 frames = nla_get_u32(cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]);
149 mac_addr_n2a(buf, nla_data(attrs[NL80211_ATTR_MAC]));
150 printf("peer %s didn't ACK %d packets\n", buf, frames);
151 } else
152 printf("unknown event\n");
7988b229
JO
153}
154
8612433d
JM
155static const char * key_type_str(enum nl80211_key_type key_type)
156{
157 static char buf[30];
158 switch (key_type) {
159 case NL80211_KEYTYPE_GROUP:
160 return "Group";
161 case NL80211_KEYTYPE_PAIRWISE:
162 return "Pairwise";
163 case NL80211_KEYTYPE_PEERKEY:
164 return "PeerKey";
165 default:
166 snprintf(buf, sizeof(buf), "unknown(%d)", key_type);
167 return buf;
168 }
169}
170
171static void parse_mic_failure(struct nlattr **attrs)
172{
173 printf("Michael MIC failure event:");
174
175 if (attrs[NL80211_ATTR_MAC]) {
176 char addr[3 * ETH_ALEN];
177 mac_addr_n2a(addr, nla_data(attrs[NL80211_ATTR_MAC]));
178 printf(" source MAC address %s", addr);
179 }
180
181 if (attrs[NL80211_ATTR_KEY_SEQ] &&
182 nla_len(attrs[NL80211_ATTR_KEY_SEQ]) == 6) {
183 unsigned char *seq = nla_data(attrs[NL80211_ATTR_KEY_SEQ]);
184 printf(" seq=%02x%02x%02x%02x%02x%02x",
185 seq[0], seq[1], seq[2], seq[3], seq[4], seq[5]);
186 }
187 if (attrs[NL80211_ATTR_KEY_TYPE]) {
188 enum nl80211_key_type key_type =
189 nla_get_u32(attrs[NL80211_ATTR_KEY_TYPE]);
190 printf(" Key Type %s", key_type_str(key_type));
191 }
192
193 if (attrs[NL80211_ATTR_KEY_IDX]) {
194 __u8 key_id = nla_get_u8(attrs[NL80211_ATTR_KEY_IDX]);
195 printf(" Key Id %d", key_id);
196 }
197
198 printf("\n");
199}
7988b229 200
819b78cc
JB
201static void parse_wowlan_wake_event(struct nlattr **attrs)
202{
d516c5bc
LC
203 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG],
204 *tb_match[NUM_NL80211_ATTR];
819b78cc
JB
205
206 printf("WoWLAN wakeup\n");
207 if (!attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
208 printf("\twakeup not due to WoWLAN\n");
209 return;
210 }
211
212 nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
213 nla_data(attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
214 nla_len(attrs[NL80211_ATTR_WOWLAN_TRIGGERS]), NULL);
215
216 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT])
217 printf("\t* was disconnected\n");
218 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT])
219 printf("\t* magic packet received\n");
220 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN])
221 printf("\t* pattern index: %u\n",
222 nla_get_u32(tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]));
223 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE])
224 printf("\t* GTK rekey failure\n");
225 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST])
226 printf("\t* EAP identity request\n");
227 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE])
228 printf("\t* 4-way handshake\n");
229 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE])
230 printf("\t* RF-kill released\n");
d516c5bc
LC
231 if (tb[NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS]) {
232 struct nlattr *match, *freq;
233 int rem_nst, rem_nst2;
234
235 printf("\t* network detected\n");
236 nla_for_each_nested(match,
237 tb[NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS],
238 rem_nst) {
239 nla_parse(tb_match, NUM_NL80211_ATTR, nla_data(match),
240 nla_len(match),
241 NULL);
242 printf("\t\tSSID: \"");
243 print_ssid_escaped(nla_len(tb_match[NL80211_ATTR_SSID]),
244 nla_data(tb_match[NL80211_ATTR_SSID]));
245 printf("\"");
246 if (tb_match[NL80211_ATTR_SCAN_FREQUENCIES]) {
247 printf(" freq(s):");
248 nla_for_each_nested(freq,
249 tb_match[NL80211_ATTR_SCAN_FREQUENCIES],
250 rem_nst2)
251 printf(" %d", nla_get_u32(freq));
252 }
253 printf("\n");
254 }
255 }
819b78cc
JB
256 if (tb[NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211]) {
257 uint8_t *d = nla_data(tb[NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211]);
258 int l = nla_len(tb[NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211]);
259 int i;
260 printf("\t* packet (might be truncated): ");
261 for (i = 0; i < l; i++) {
262 if (i > 0)
263 printf(":");
264 printf("%.2x", d[i]);
265 }
266 printf("\n");
267 }
268 if (tb[NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023]) {
269 uint8_t *d = nla_data(tb[NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023]);
270 int l = nla_len(tb[NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023]);
271 int i;
272 printf("\t* packet (might be truncated): ");
273 for (i = 0; i < l; i++) {
274 if (i > 0)
275 printf(":");
276 printf("%.2x", d[i]);
277 }
278 printf("\n");
279 }
280 if (tb[NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH])
281 printf("\t* TCP connection wakeup received\n");
282 if (tb[NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST])
283 printf("\t* TCP connection lost\n");
284 if (tb[NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS])
285 printf("\t* TCP connection ran out of tokens\n");
286}
287
957a0f07
JB
288static int print_event(struct nl_msg *msg, void *arg)
289{
290 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
748f8489 291 struct nlattr *tb[NL80211_ATTR_MAX + 1], *nst;
957a0f07
JB
292 struct print_event_args *args = arg;
293 char ifname[100];
294 char macbuf[6*3];
295 __u8 reg_type;
0168589d
LR
296 struct ieee80211_beacon_channel chan_before_beacon, chan_after_beacon;
297 __u32 wiphy_idx = 0;
748f8489 298 int rem_nst;
f1a666a6 299 __u16 status;
957a0f07 300
981b21ad
JB
301 if (args->time || args->reltime) {
302 unsigned long long usecs, previous;
303
304 previous = 1000000ULL * args->ts.tv_sec + args->ts.tv_usec;
305 gettimeofday(&args->ts, NULL);
306 usecs = 1000000ULL * args->ts.tv_sec + args->ts.tv_usec;
307 if (args->reltime) {
308 if (!args->have_ts) {
309 usecs = 0;
310 args->have_ts = true;
311 } else
312 usecs -= previous;
313 }
314 printf("%llu.%06llu: ", usecs/1000000, usecs % 1000000);
957a0f07
JB
315 }
316
317 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
318 genlmsg_attrlen(gnlh, 0), NULL);
319
320 if (tb[NL80211_ATTR_IFINDEX] && tb[NL80211_ATTR_WIPHY]) {
321 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname);
322 printf("%s (phy #%d): ", ifname, nla_get_u32(tb[NL80211_ATTR_WIPHY]));
d8880179
JB
323 } else if (tb[NL80211_ATTR_WDEV] && tb[NL80211_ATTR_WIPHY]) {
324 printf("wdev 0x%llx (phy #%d): ",
325 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_WDEV]),
326 nla_get_u32(tb[NL80211_ATTR_WIPHY]));
957a0f07
JB
327 } else if (tb[NL80211_ATTR_IFINDEX]) {
328 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname);
329 printf("%s: ", ifname);
d8880179
JB
330 } else if (tb[NL80211_ATTR_WDEV]) {
331 printf("wdev 0x%llx: ", (unsigned long long)nla_get_u64(tb[NL80211_ATTR_WDEV]));
957a0f07
JB
332 } else if (tb[NL80211_ATTR_WIPHY]) {
333 printf("phy #%d: ", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
334 }
335
336 switch (gnlh->cmd) {
337 case NL80211_CMD_NEW_WIPHY:
338 printf("renamed to %s\n", nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]));
339 break;
e67be815
JB
340 case NL80211_CMD_TRIGGER_SCAN:
341 printf("scan started\n");
342 break;
957a0f07 343 case NL80211_CMD_NEW_SCAN_RESULTS:
748f8489 344 printf("scan finished:");
957a0f07 345 case NL80211_CMD_SCAN_ABORTED:
748f8489
JB
346 if (gnlh->cmd == NL80211_CMD_SCAN_ABORTED)
347 printf("scan aborted:");
348 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
349 nla_for_each_nested(nst, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem_nst)
350 printf(" %d", nla_get_u32(nst));
351 printf(",");
352 }
353 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
354 nla_for_each_nested(nst, tb[NL80211_ATTR_SCAN_SSIDS], rem_nst) {
355 printf(" \"");
356 print_ssid_escaped(nla_len(nst), nla_data(nst));
357 printf("\"");
358 }
359 }
360 printf("\n");
957a0f07 361 break;
3fce58aa
LC
362 case NL80211_CMD_START_SCHED_SCAN:
363 printf("scheduled scan started\n");
364 break;
365 case NL80211_CMD_SCHED_SCAN_STOPPED:
366 printf("sched scan stopped\n");
367 break;
368 case NL80211_CMD_SCHED_SCAN_RESULTS:
369 printf("got scheduled scan results\n");
370 break;
957a0f07
JB
371 case NL80211_CMD_REG_CHANGE:
372 printf("regulatory domain change: ");
373
374 reg_type = nla_get_u8(tb[NL80211_ATTR_REG_TYPE]);
375
376 switch (reg_type) {
377 case NL80211_REGDOM_TYPE_COUNTRY:
378 printf("set to %s by %s request",
379 nla_get_string(tb[NL80211_ATTR_REG_ALPHA2]),
380 reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])));
381 if (tb[NL80211_ATTR_WIPHY])
382 printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
383 break;
384 case NL80211_REGDOM_TYPE_WORLD:
385 printf("set to world roaming by %s request",
386 reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])));
387 break;
388 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
389 printf("custom world roaming rules in place on phy%d by %s request",
390 nla_get_u32(tb[NL80211_ATTR_WIPHY]),
391 reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR])));
392 break;
393 case NL80211_REGDOM_TYPE_INTERSECTION:
394 printf("intersection used due to a request made by %s",
395 reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR])));
396 if (tb[NL80211_ATTR_WIPHY])
397 printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
398 break;
399 default:
400 printf("unknown source (upgrade this utility)");
401 break;
402 }
403
404 printf("\n");
0168589d
LR
405 break;
406 case NL80211_CMD_REG_BEACON_HINT:
407
408 wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
409
410 memset(&chan_before_beacon, 0, sizeof(chan_before_beacon));
411 memset(&chan_after_beacon, 0, sizeof(chan_after_beacon));
412
ce3b2ed9
JB
413 if (parse_beacon_hint_chan(tb[NL80211_ATTR_FREQ_BEFORE],
414 &chan_before_beacon))
415 break;
416 if (parse_beacon_hint_chan(tb[NL80211_ATTR_FREQ_AFTER],
417 &chan_after_beacon))
418 break;
0168589d
LR
419
420 if (chan_before_beacon.center_freq != chan_after_beacon.center_freq)
421 break;
422
423 /* A beacon hint is sent _only_ if something _did_ change */
424 printf("beacon hint:\n");
425
426 printf("phy%d %d MHz [%d]:\n",
427 wiphy_idx,
428 chan_before_beacon.center_freq,
429 ieee80211_frequency_to_channel(chan_before_beacon.center_freq));
430
f0c48e7b
IP
431 if (chan_before_beacon.no_ir && !chan_after_beacon.no_ir) {
432 if (chan_before_beacon.no_ibss && !chan_after_beacon.no_ibss)
433 printf("\to Initiating radiation enabled\n");
434 else
435 printf("\to active scan enabled\n");
436 } else if (chan_before_beacon.no_ibss && !chan_after_beacon.no_ibss) {
437 printf("\to ibss enabled\n");
438 }
0168589d 439
cec6d6a7
JB
440 break;
441 case NL80211_CMD_NEW_STATION:
442 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
443 printf("new station %s\n", macbuf);
957a0f07 444 break;
27bf109b
AQ
445 case NL80211_CMD_DEL_STATION:
446 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
447 printf("del station %s\n", macbuf);
448 break;
957a0f07
JB
449 case NL80211_CMD_JOIN_IBSS:
450 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
451 printf("IBSS %s joined\n", macbuf);
452 break;
453 case NL80211_CMD_AUTHENTICATE:
454 printf("auth");
aea5dbd2
JB
455 if (tb[NL80211_ATTR_FRAME])
456 print_frame(args, tb[NL80211_ATTR_FRAME]);
457 else if (tb[NL80211_ATTR_TIMED_OUT])
458 printf(": timed out");
459 else
460 printf(": unknown event");
957a0f07
JB
461 printf("\n");
462 break;
463 case NL80211_CMD_ASSOCIATE:
464 printf("assoc");
aea5dbd2
JB
465 if (tb[NL80211_ATTR_FRAME])
466 print_frame(args, tb[NL80211_ATTR_FRAME]);
467 else if (tb[NL80211_ATTR_TIMED_OUT])
468 printf(": timed out");
469 else
470 printf(": unknown event");
957a0f07
JB
471 printf("\n");
472 break;
473 case NL80211_CMD_DEAUTHENTICATE:
474 printf("deauth");
475 print_frame(args, tb[NL80211_ATTR_FRAME]);
476 printf("\n");
477 break;
478 case NL80211_CMD_DISASSOCIATE:
479 printf("disassoc");
480 print_frame(args, tb[NL80211_ATTR_FRAME]);
481 printf("\n");
482 break;
99802e56
JB
483 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
484 printf("unprotected deauth");
485 print_frame(args, tb[NL80211_ATTR_FRAME]);
486 printf("\n");
487 break;
488 case NL80211_CMD_UNPROT_DISASSOCIATE:
489 printf("unprotected disassoc");
490 print_frame(args, tb[NL80211_ATTR_FRAME]);
491 printf("\n");
492 break;
f1a666a6
JB
493 case NL80211_CMD_CONNECT:
494 status = 0;
495 if (!tb[NL80211_ATTR_STATUS_CODE])
496 printf("unknown connect status");
497 else if (nla_get_u16(tb[NL80211_ATTR_STATUS_CODE]) == 0)
498 printf("connected");
499 else {
500 status = nla_get_u16(tb[NL80211_ATTR_STATUS_CODE]);
501 printf("failed to connect");
502 }
503 if (tb[NL80211_ATTR_MAC]) {
504 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
505 printf(" to %s", macbuf);
506 }
507 if (status)
508 printf(", status: %d: %s", status, get_status_str(status));
509 printf("\n");
510 break;
511 case NL80211_CMD_ROAM:
512 printf("roamed");
513 if (tb[NL80211_ATTR_MAC]) {
514 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
515 printf(" to %s", macbuf);
516 }
517 printf("\n");
518 break;
519 case NL80211_CMD_DISCONNECT:
520 printf("disconnected");
521 if (tb[NL80211_ATTR_DISCONNECTED_BY_AP])
522 printf(" (by AP)");
523 else
524 printf(" (local request)");
525 if (tb[NL80211_ATTR_REASON_CODE])
526 printf(" reason: %d: %s", nla_get_u16(tb[NL80211_ATTR_REASON_CODE]),
527 get_reason_str(nla_get_u16(tb[NL80211_ATTR_REASON_CODE])));
528 printf("\n");
529 break;
6829308d
JB
530 case NL80211_CMD_REMAIN_ON_CHANNEL:
531 printf("remain on freq %d (%dms, cookie %llx)\n",
532 nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]),
533 nla_get_u32(tb[NL80211_ATTR_DURATION]),
534 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]));
535 break;
536 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
537 printf("done with remain on freq %d (cookie %llx)\n",
538 nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]),
539 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]));
540 break;
7988b229 541 case NL80211_CMD_NOTIFY_CQM:
f19444fe 542 parse_cqm_event(tb);
7988b229 543 break;
8612433d
JM
544 case NL80211_CMD_MICHAEL_MIC_FAILURE:
545 parse_mic_failure(tb);
546 break;
cbff7089
JB
547 case NL80211_CMD_FRAME_TX_STATUS:
548 printf("mgmt TX status (cookie %llx): %s\n",
549 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]),
550 tb[NL80211_ATTR_ACK] ? "acked" : "no ack");
551 break;
0bf2c6a0 552 case NL80211_CMD_PMKSA_CANDIDATE:
b6688cb8
JB
553 printf("PMKSA candidate found\n");
554 break;
819b78cc
JB
555 case NL80211_CMD_SET_WOWLAN:
556 parse_wowlan_wake_event(tb);
557 break;
4310994d
BG
558 case NL80211_CMD_PROBE_CLIENT:
559 if (tb[NL80211_ATTR_MAC])
560 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
561 else
562 strcpy(macbuf, "??");
563 printf("probe client %s (cookie %llx): %s\n",
564 macbuf,
565 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]),
566 tb[NL80211_ATTR_ACK] ? "acked" : "no ack");
567 break;
e8b3b312
JB
568 case NL80211_CMD_VENDOR:
569 printf("vendor event %.6x:%d\n",
570 nla_get_u32(tb[NL80211_ATTR_VENDOR_ID]),
571 nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD]));
492354de
JD
572 if (args->frame && tb[NL80211_ATTR_VENDOR_DATA])
573 iw_hexdump("vendor event",
574 nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
575 nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
e8b3b312 576 break;
085f4f08
HS
577 case NL80211_CMD_RADAR_DETECT:
578 printf("radar event ");
579 if (tb[NL80211_ATTR_RADAR_EVENT]) {
580 switch (nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT])) {
581 case NL80211_RADAR_DETECTED:
582 printf("(radar detected)");
583 break;
584 case NL80211_RADAR_CAC_FINISHED:
585 printf("(cac finished)");
586 break;
587 case NL80211_RADAR_CAC_ABORTED:
588 printf("(cac aborted)");
589 break;
590 case NL80211_RADAR_NOP_FINISHED:
591 printf("(nop finished)");
592 break;
593 default:
594 printf("(unknown)");
595 break;
596 };
597 } else {
598 printf("(unknown)");
599 }
600 printf("\n");
601 break;
957a0f07 602 default:
7c712c89
JB
603 printf("unknown event %d (%s)\n",
604 gnlh->cmd, command_name(gnlh->cmd));
957a0f07
JB
605 break;
606 }
607
374e8a26 608 fflush(stdout);
957a0f07
JB
609 return NL_SKIP;
610}
611
612struct wait_event {
613 int n_cmds;
614 const __u32 *cmds;
615 __u32 cmd;
7fabd346 616 struct print_event_args *pargs;
957a0f07
JB
617};
618
619static int wait_event(struct nl_msg *msg, void *arg)
620{
621 struct wait_event *wait = arg;
622 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
623 int i;
624
625 for (i = 0; i < wait->n_cmds; i++) {
626 if (gnlh->cmd == wait->cmds[i]) {
627 wait->cmd = gnlh->cmd;
7e7c544f
JB
628 if (wait->pargs)
629 print_event(msg, wait->pargs);
957a0f07
JB
630 }
631 }
632
633 return NL_SKIP;
634}
635
ee374e4d 636int __prepare_listen_events(struct nl80211_state *state)
957a0f07
JB
637{
638 int mcid, ret;
957a0f07
JB
639
640 /* Configuration multicast group */
641 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "config");
642 if (mcid < 0)
643 return mcid;
644
645 ret = nl_socket_add_membership(state->nl_sock, mcid);
646 if (ret)
647 return ret;
648
649 /* Scan multicast group */
650 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "scan");
651 if (mcid >= 0) {
652 ret = nl_socket_add_membership(state->nl_sock, mcid);
653 if (ret)
654 return ret;
655 }
656
657 /* Regulatory multicast group */
658 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "regulatory");
659 if (mcid >= 0) {
660 ret = nl_socket_add_membership(state->nl_sock, mcid);
661 if (ret)
662 return ret;
663 }
664
665 /* MLME multicast group */
666 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "mlme");
667 if (mcid >= 0) {
668 ret = nl_socket_add_membership(state->nl_sock, mcid);
669 if (ret)
670 return ret;
671 }
672
e8b3b312
JB
673 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "vendor");
674 if (mcid >= 0) {
675 ret = nl_socket_add_membership(state->nl_sock, mcid);
676 if (ret)
677 return ret;
678 }
679
105aed57
JB
680 return 0;
681}
682
ee374e4d
JB
683__u32 __do_listen_events(struct nl80211_state *state,
684 const int n_waits, const __u32 *waits,
685 struct print_event_args *args)
105aed57
JB
686{
687 struct nl_cb *cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
688 struct wait_event wait_ev;
105aed57
JB
689
690 if (!cb) {
691 fprintf(stderr, "failed to allocate netlink callbacks\n");
692 return -ENOMEM;
693 }
694
957a0f07
JB
695 /* no sequence checking for multicast messages */
696 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
697
698 if (n_waits && waits) {
699 wait_ev.cmds = waits;
700 wait_ev.n_cmds = n_waits;
7fabd346 701 wait_ev.pargs = args;
957a0f07 702 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, wait_event, &wait_ev);
7fabd346 703 } else
957a0f07 704 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_event, args);
957a0f07
JB
705
706 wait_ev.cmd = 0;
707
708 while (!wait_ev.cmd)
709 nl_recvmsgs(state->nl_sock, cb);
710
711 nl_cb_put(cb);
712
713 return wait_ev.cmd;
714}
715
716__u32 listen_events(struct nl80211_state *state,
717 const int n_waits, const __u32 *waits)
718{
ee374e4d
JB
719 int ret;
720
721 ret = __prepare_listen_events(state);
722 if (ret)
723 return ret;
724
725 return __do_listen_events(state, n_waits, waits, NULL);
957a0f07
JB
726}
727
728static int print_events(struct nl80211_state *state,
729 struct nl_cb *cb,
730 struct nl_msg *msg,
05514f95
JB
731 int argc, char **argv,
732 enum id_input id)
957a0f07
JB
733{
734 struct print_event_args args;
ee374e4d 735 int ret;
957a0f07
JB
736
737 memset(&args, 0, sizeof(args));
738
739 argc--;
740 argv++;
741
742 while (argc > 0) {
743 if (strcmp(argv[0], "-f") == 0)
744 args.frame = true;
745 else if (strcmp(argv[0], "-t") == 0)
746 args.time = true;
981b21ad
JB
747 else if (strcmp(argv[0], "-r") == 0)
748 args.reltime = true;
957a0f07
JB
749 else
750 return 1;
751 argc--;
752 argv++;
753 }
754
981b21ad
JB
755 if (args.time && args.reltime)
756 return 1;
757
957a0f07
JB
758 if (argc)
759 return 1;
760
ee374e4d
JB
761 ret = __prepare_listen_events(state);
762 if (ret)
763 return ret;
764
765 return __do_listen_events(state, 0, NULL, &args);
957a0f07 766}
981b21ad 767TOPLEVEL(event, "[-t] [-r] [-f]", 0, 0, CIB_NONE, print_events,
01ae06f9
JB
768 "Monitor events from the kernel.\n"
769 "-t - print timestamp\n"
981b21ad 770 "-r - print relative timstamp\n"
01ae06f9 771 "-f - print full frame for auth/assoc etc.");