]> git.ipfire.org Git - thirdparty/iw.git/blame - event.c
iw: move generic sched scan parsing code out of net detect
[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
JB
361 break;
362 case NL80211_CMD_REG_CHANGE:
363 printf("regulatory domain change: ");
364
365 reg_type = nla_get_u8(tb[NL80211_ATTR_REG_TYPE]);
366
367 switch (reg_type) {
368 case NL80211_REGDOM_TYPE_COUNTRY:
369 printf("set to %s by %s request",
370 nla_get_string(tb[NL80211_ATTR_REG_ALPHA2]),
371 reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])));
372 if (tb[NL80211_ATTR_WIPHY])
373 printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
374 break;
375 case NL80211_REGDOM_TYPE_WORLD:
376 printf("set to world roaming by %s request",
377 reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])));
378 break;
379 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
380 printf("custom world roaming rules in place on phy%d by %s request",
381 nla_get_u32(tb[NL80211_ATTR_WIPHY]),
382 reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR])));
383 break;
384 case NL80211_REGDOM_TYPE_INTERSECTION:
385 printf("intersection used due to a request made by %s",
386 reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR])));
387 if (tb[NL80211_ATTR_WIPHY])
388 printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
389 break;
390 default:
391 printf("unknown source (upgrade this utility)");
392 break;
393 }
394
395 printf("\n");
0168589d
LR
396 break;
397 case NL80211_CMD_REG_BEACON_HINT:
398
399 wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
400
401 memset(&chan_before_beacon, 0, sizeof(chan_before_beacon));
402 memset(&chan_after_beacon, 0, sizeof(chan_after_beacon));
403
ce3b2ed9
JB
404 if (parse_beacon_hint_chan(tb[NL80211_ATTR_FREQ_BEFORE],
405 &chan_before_beacon))
406 break;
407 if (parse_beacon_hint_chan(tb[NL80211_ATTR_FREQ_AFTER],
408 &chan_after_beacon))
409 break;
0168589d
LR
410
411 if (chan_before_beacon.center_freq != chan_after_beacon.center_freq)
412 break;
413
414 /* A beacon hint is sent _only_ if something _did_ change */
415 printf("beacon hint:\n");
416
417 printf("phy%d %d MHz [%d]:\n",
418 wiphy_idx,
419 chan_before_beacon.center_freq,
420 ieee80211_frequency_to_channel(chan_before_beacon.center_freq));
421
f0c48e7b
IP
422 if (chan_before_beacon.no_ir && !chan_after_beacon.no_ir) {
423 if (chan_before_beacon.no_ibss && !chan_after_beacon.no_ibss)
424 printf("\to Initiating radiation enabled\n");
425 else
426 printf("\to active scan enabled\n");
427 } else if (chan_before_beacon.no_ibss && !chan_after_beacon.no_ibss) {
428 printf("\to ibss enabled\n");
429 }
0168589d 430
cec6d6a7
JB
431 break;
432 case NL80211_CMD_NEW_STATION:
433 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
434 printf("new station %s\n", macbuf);
957a0f07 435 break;
27bf109b
AQ
436 case NL80211_CMD_DEL_STATION:
437 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
438 printf("del station %s\n", macbuf);
439 break;
957a0f07
JB
440 case NL80211_CMD_JOIN_IBSS:
441 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
442 printf("IBSS %s joined\n", macbuf);
443 break;
444 case NL80211_CMD_AUTHENTICATE:
445 printf("auth");
aea5dbd2
JB
446 if (tb[NL80211_ATTR_FRAME])
447 print_frame(args, tb[NL80211_ATTR_FRAME]);
448 else if (tb[NL80211_ATTR_TIMED_OUT])
449 printf(": timed out");
450 else
451 printf(": unknown event");
957a0f07
JB
452 printf("\n");
453 break;
454 case NL80211_CMD_ASSOCIATE:
455 printf("assoc");
aea5dbd2
JB
456 if (tb[NL80211_ATTR_FRAME])
457 print_frame(args, tb[NL80211_ATTR_FRAME]);
458 else if (tb[NL80211_ATTR_TIMED_OUT])
459 printf(": timed out");
460 else
461 printf(": unknown event");
957a0f07
JB
462 printf("\n");
463 break;
464 case NL80211_CMD_DEAUTHENTICATE:
465 printf("deauth");
466 print_frame(args, tb[NL80211_ATTR_FRAME]);
467 printf("\n");
468 break;
469 case NL80211_CMD_DISASSOCIATE:
470 printf("disassoc");
471 print_frame(args, tb[NL80211_ATTR_FRAME]);
472 printf("\n");
473 break;
99802e56
JB
474 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
475 printf("unprotected deauth");
476 print_frame(args, tb[NL80211_ATTR_FRAME]);
477 printf("\n");
478 break;
479 case NL80211_CMD_UNPROT_DISASSOCIATE:
480 printf("unprotected disassoc");
481 print_frame(args, tb[NL80211_ATTR_FRAME]);
482 printf("\n");
483 break;
f1a666a6
JB
484 case NL80211_CMD_CONNECT:
485 status = 0;
486 if (!tb[NL80211_ATTR_STATUS_CODE])
487 printf("unknown connect status");
488 else if (nla_get_u16(tb[NL80211_ATTR_STATUS_CODE]) == 0)
489 printf("connected");
490 else {
491 status = nla_get_u16(tb[NL80211_ATTR_STATUS_CODE]);
492 printf("failed to connect");
493 }
494 if (tb[NL80211_ATTR_MAC]) {
495 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
496 printf(" to %s", macbuf);
497 }
498 if (status)
499 printf(", status: %d: %s", status, get_status_str(status));
500 printf("\n");
501 break;
502 case NL80211_CMD_ROAM:
503 printf("roamed");
504 if (tb[NL80211_ATTR_MAC]) {
505 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
506 printf(" to %s", macbuf);
507 }
508 printf("\n");
509 break;
510 case NL80211_CMD_DISCONNECT:
511 printf("disconnected");
512 if (tb[NL80211_ATTR_DISCONNECTED_BY_AP])
513 printf(" (by AP)");
514 else
515 printf(" (local request)");
516 if (tb[NL80211_ATTR_REASON_CODE])
517 printf(" reason: %d: %s", nla_get_u16(tb[NL80211_ATTR_REASON_CODE]),
518 get_reason_str(nla_get_u16(tb[NL80211_ATTR_REASON_CODE])));
519 printf("\n");
520 break;
6829308d
JB
521 case NL80211_CMD_REMAIN_ON_CHANNEL:
522 printf("remain on freq %d (%dms, cookie %llx)\n",
523 nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]),
524 nla_get_u32(tb[NL80211_ATTR_DURATION]),
525 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]));
526 break;
527 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
528 printf("done with remain on freq %d (cookie %llx)\n",
529 nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]),
530 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]));
531 break;
7988b229 532 case NL80211_CMD_NOTIFY_CQM:
f19444fe 533 parse_cqm_event(tb);
7988b229 534 break;
8612433d
JM
535 case NL80211_CMD_MICHAEL_MIC_FAILURE:
536 parse_mic_failure(tb);
537 break;
cbff7089
JB
538 case NL80211_CMD_FRAME_TX_STATUS:
539 printf("mgmt TX status (cookie %llx): %s\n",
540 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]),
541 tb[NL80211_ATTR_ACK] ? "acked" : "no ack");
542 break;
0bf2c6a0 543 case NL80211_CMD_PMKSA_CANDIDATE:
b6688cb8
JB
544 printf("PMKSA candidate found\n");
545 break;
819b78cc
JB
546 case NL80211_CMD_SET_WOWLAN:
547 parse_wowlan_wake_event(tb);
548 break;
4310994d
BG
549 case NL80211_CMD_PROBE_CLIENT:
550 if (tb[NL80211_ATTR_MAC])
551 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
552 else
553 strcpy(macbuf, "??");
554 printf("probe client %s (cookie %llx): %s\n",
555 macbuf,
556 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]),
557 tb[NL80211_ATTR_ACK] ? "acked" : "no ack");
558 break;
e8b3b312
JB
559 case NL80211_CMD_VENDOR:
560 printf("vendor event %.6x:%d\n",
561 nla_get_u32(tb[NL80211_ATTR_VENDOR_ID]),
562 nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD]));
492354de
JD
563 if (args->frame && tb[NL80211_ATTR_VENDOR_DATA])
564 iw_hexdump("vendor event",
565 nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
566 nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
e8b3b312 567 break;
085f4f08
HS
568 case NL80211_CMD_RADAR_DETECT:
569 printf("radar event ");
570 if (tb[NL80211_ATTR_RADAR_EVENT]) {
571 switch (nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT])) {
572 case NL80211_RADAR_DETECTED:
573 printf("(radar detected)");
574 break;
575 case NL80211_RADAR_CAC_FINISHED:
576 printf("(cac finished)");
577 break;
578 case NL80211_RADAR_CAC_ABORTED:
579 printf("(cac aborted)");
580 break;
581 case NL80211_RADAR_NOP_FINISHED:
582 printf("(nop finished)");
583 break;
584 default:
585 printf("(unknown)");
586 break;
587 };
588 } else {
589 printf("(unknown)");
590 }
591 printf("\n");
592 break;
957a0f07 593 default:
7c712c89
JB
594 printf("unknown event %d (%s)\n",
595 gnlh->cmd, command_name(gnlh->cmd));
957a0f07
JB
596 break;
597 }
598
374e8a26 599 fflush(stdout);
957a0f07
JB
600 return NL_SKIP;
601}
602
603struct wait_event {
604 int n_cmds;
605 const __u32 *cmds;
606 __u32 cmd;
7fabd346 607 struct print_event_args *pargs;
957a0f07
JB
608};
609
610static int wait_event(struct nl_msg *msg, void *arg)
611{
612 struct wait_event *wait = arg;
613 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
614 int i;
615
616 for (i = 0; i < wait->n_cmds; i++) {
617 if (gnlh->cmd == wait->cmds[i]) {
618 wait->cmd = gnlh->cmd;
7e7c544f
JB
619 if (wait->pargs)
620 print_event(msg, wait->pargs);
957a0f07
JB
621 }
622 }
623
624 return NL_SKIP;
625}
626
ee374e4d 627int __prepare_listen_events(struct nl80211_state *state)
957a0f07
JB
628{
629 int mcid, ret;
957a0f07
JB
630
631 /* Configuration multicast group */
632 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "config");
633 if (mcid < 0)
634 return mcid;
635
636 ret = nl_socket_add_membership(state->nl_sock, mcid);
637 if (ret)
638 return ret;
639
640 /* Scan multicast group */
641 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "scan");
642 if (mcid >= 0) {
643 ret = nl_socket_add_membership(state->nl_sock, mcid);
644 if (ret)
645 return ret;
646 }
647
648 /* Regulatory multicast group */
649 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "regulatory");
650 if (mcid >= 0) {
651 ret = nl_socket_add_membership(state->nl_sock, mcid);
652 if (ret)
653 return ret;
654 }
655
656 /* MLME multicast group */
657 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "mlme");
658 if (mcid >= 0) {
659 ret = nl_socket_add_membership(state->nl_sock, mcid);
660 if (ret)
661 return ret;
662 }
663
e8b3b312
JB
664 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "vendor");
665 if (mcid >= 0) {
666 ret = nl_socket_add_membership(state->nl_sock, mcid);
667 if (ret)
668 return ret;
669 }
670
105aed57
JB
671 return 0;
672}
673
ee374e4d
JB
674__u32 __do_listen_events(struct nl80211_state *state,
675 const int n_waits, const __u32 *waits,
676 struct print_event_args *args)
105aed57
JB
677{
678 struct nl_cb *cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
679 struct wait_event wait_ev;
105aed57
JB
680
681 if (!cb) {
682 fprintf(stderr, "failed to allocate netlink callbacks\n");
683 return -ENOMEM;
684 }
685
957a0f07
JB
686 /* no sequence checking for multicast messages */
687 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
688
689 if (n_waits && waits) {
690 wait_ev.cmds = waits;
691 wait_ev.n_cmds = n_waits;
7fabd346 692 wait_ev.pargs = args;
957a0f07 693 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, wait_event, &wait_ev);
7fabd346 694 } else
957a0f07 695 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_event, args);
957a0f07
JB
696
697 wait_ev.cmd = 0;
698
699 while (!wait_ev.cmd)
700 nl_recvmsgs(state->nl_sock, cb);
701
702 nl_cb_put(cb);
703
704 return wait_ev.cmd;
705}
706
707__u32 listen_events(struct nl80211_state *state,
708 const int n_waits, const __u32 *waits)
709{
ee374e4d
JB
710 int ret;
711
712 ret = __prepare_listen_events(state);
713 if (ret)
714 return ret;
715
716 return __do_listen_events(state, n_waits, waits, NULL);
957a0f07
JB
717}
718
719static int print_events(struct nl80211_state *state,
720 struct nl_cb *cb,
721 struct nl_msg *msg,
05514f95
JB
722 int argc, char **argv,
723 enum id_input id)
957a0f07
JB
724{
725 struct print_event_args args;
ee374e4d 726 int ret;
957a0f07
JB
727
728 memset(&args, 0, sizeof(args));
729
730 argc--;
731 argv++;
732
733 while (argc > 0) {
734 if (strcmp(argv[0], "-f") == 0)
735 args.frame = true;
736 else if (strcmp(argv[0], "-t") == 0)
737 args.time = true;
981b21ad
JB
738 else if (strcmp(argv[0], "-r") == 0)
739 args.reltime = true;
957a0f07
JB
740 else
741 return 1;
742 argc--;
743 argv++;
744 }
745
981b21ad
JB
746 if (args.time && args.reltime)
747 return 1;
748
957a0f07
JB
749 if (argc)
750 return 1;
751
ee374e4d
JB
752 ret = __prepare_listen_events(state);
753 if (ret)
754 return ret;
755
756 return __do_listen_events(state, 0, NULL, &args);
957a0f07 757}
981b21ad 758TOPLEVEL(event, "[-t] [-r] [-f]", 0, 0, CIB_NONE, print_events,
01ae06f9
JB
759 "Monitor events from the kernel.\n"
760 "-t - print timestamp\n"
981b21ad 761 "-r - print relative timstamp\n"
01ae06f9 762 "-f - print full frame for auth/assoc etc.");