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