]> git.ipfire.org Git - thirdparty/iw.git/blob - event.c
iw: separate wait/print when waiting for an event
[thirdparty/iw.git] / event.c
1 #include <stdint.h>
2 #include <stdbool.h>
3 #include <net/if.h>
4 #include <errno.h>
5 #include <inttypes.h>
6 #include "iw.h"
7
8 static int no_seq_check(struct nl_msg *msg, void *arg)
9 {
10 return NL_OK;
11 }
12
13 struct ieee80211_beacon_channel {
14 __u16 center_freq;
15 bool no_ir;
16 bool no_ibss;
17 };
18
19 static int parse_beacon_hint_chan(struct nlattr *tb,
20 struct ieee80211_beacon_channel *chan)
21 {
22 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
23 static struct nla_policy beacon_freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
24 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
25 [NL80211_FREQUENCY_ATTR_NO_IR] = { .type = NLA_FLAG },
26 [__NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
27 };
28
29 if (nla_parse_nested(tb_freq,
30 NL80211_FREQUENCY_ATTR_MAX,
31 tb,
32 beacon_freq_policy))
33 return -EINVAL;
34
35 chan->center_freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
36
37 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IR])
38 chan->no_ir = true;
39 if (tb_freq[__NL80211_FREQUENCY_ATTR_NO_IBSS])
40 chan->no_ibss = true;
41
42 return 0;
43 }
44
45 static void print_frame(struct print_event_args *args, struct nlattr *attr)
46 {
47 uint8_t *frame;
48 size_t len;
49 unsigned int i;
50 char macbuf[6*3];
51 uint16_t tmp;
52
53 if (!attr) {
54 printf(" [no frame]");
55 return;
56 }
57
58 frame = nla_data(attr);
59 len = nla_len(attr);
60
61 if (len < 26) {
62 printf(" [invalid frame: ");
63 goto print_frame;
64 }
65
66 mac_addr_n2a(macbuf, frame + 10);
67 printf(" %s -> ", macbuf);
68 mac_addr_n2a(macbuf, frame + 4);
69 printf("%s", macbuf);
70
71 switch (frame[0] & 0xfc) {
72 case 0x10: /* assoc resp */
73 case 0x30: /* reassoc resp */
74 /* status */
75 tmp = (frame[27] << 8) + frame[26];
76 printf(" status: %d: %s", tmp, get_status_str(tmp));
77 break;
78 case 0x00: /* assoc req */
79 case 0x20: /* reassoc req */
80 break;
81 case 0xb0: /* auth */
82 /* status */
83 tmp = (frame[29] << 8) + frame[28];
84 printf(" status: %d: %s", tmp, get_status_str(tmp));
85 break;
86 case 0xa0: /* disassoc */
87 case 0xc0: /* deauth */
88 /* reason */
89 tmp = (frame[25] << 8) + frame[24];
90 printf(" reason %d: %s", tmp, get_reason_str(tmp));
91 break;
92 }
93
94 if (!args->frame)
95 return;
96
97 printf(" [frame:");
98
99 print_frame:
100 for (i = 0; i < len; i++)
101 printf(" %.02x", frame[i]);
102 printf("]");
103 }
104
105 static void parse_cqm_event(struct nlattr **attrs)
106 {
107 static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = {
108 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
109 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
110 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
111 };
112 struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1];
113 struct nlattr *cqm_attr = attrs[NL80211_ATTR_CQM];
114
115 printf("CQM event: ");
116
117 if (!cqm_attr ||
118 nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, cqm_attr, cqm_policy)) {
119 printf("missing data!\n");
120 return;
121 }
122
123 if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]) {
124 enum nl80211_cqm_rssi_threshold_event rssi_event;
125 bool found_one = false;
126
127 rssi_event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]);
128
129 switch (rssi_event) {
130 case NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH:
131 printf("RSSI went above threshold\n");
132 found_one = true;
133 break;
134 case NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW:
135 printf("RSSI went below threshold\n");
136 found_one = true;
137 break;
138 case NL80211_CQM_RSSI_BEACON_LOSS_EVENT:
139 printf("Beacon loss detected\n");
140 found_one = true;
141 break;
142 }
143
144 if (!found_one)
145 printf("Unknown event type: %i\n", rssi_event);
146 } else if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]) {
147 if (attrs[NL80211_ATTR_MAC]) {
148 uint32_t frames;
149 char buf[3*6];
150
151 frames = nla_get_u32(cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]);
152 mac_addr_n2a(buf, nla_data(attrs[NL80211_ATTR_MAC]));
153 printf("peer %s didn't ACK %d packets\n", buf, frames);
154 } else {
155 printf("PKT-LOSS-EVENT did not have MAC attribute!\n");
156 }
157 } else if (cqm[NL80211_ATTR_CQM_BEACON_LOSS_EVENT]) {
158 printf("beacon loss\n");
159 } else {
160 printf("unknown event\n");
161 }
162 }
163
164 static const char * key_type_str(enum nl80211_key_type key_type)
165 {
166 static char buf[30];
167 switch (key_type) {
168 case NL80211_KEYTYPE_GROUP:
169 return "Group";
170 case NL80211_KEYTYPE_PAIRWISE:
171 return "Pairwise";
172 case NL80211_KEYTYPE_PEERKEY:
173 return "PeerKey";
174 default:
175 snprintf(buf, sizeof(buf), "unknown(%d)", key_type);
176 return buf;
177 }
178 }
179
180 static void parse_mic_failure(struct nlattr **attrs)
181 {
182 printf("Michael MIC failure event:");
183
184 if (attrs[NL80211_ATTR_MAC]) {
185 char addr[3 * ETH_ALEN];
186 mac_addr_n2a(addr, nla_data(attrs[NL80211_ATTR_MAC]));
187 printf(" source MAC address %s", addr);
188 }
189
190 if (attrs[NL80211_ATTR_KEY_SEQ] &&
191 nla_len(attrs[NL80211_ATTR_KEY_SEQ]) == 6) {
192 unsigned char *seq = nla_data(attrs[NL80211_ATTR_KEY_SEQ]);
193 printf(" seq=%02x%02x%02x%02x%02x%02x",
194 seq[0], seq[1], seq[2], seq[3], seq[4], seq[5]);
195 }
196 if (attrs[NL80211_ATTR_KEY_TYPE]) {
197 enum nl80211_key_type key_type =
198 nla_get_u32(attrs[NL80211_ATTR_KEY_TYPE]);
199 printf(" Key Type %s", key_type_str(key_type));
200 }
201
202 if (attrs[NL80211_ATTR_KEY_IDX]) {
203 __u8 key_id = nla_get_u8(attrs[NL80211_ATTR_KEY_IDX]);
204 printf(" Key Id %d", key_id);
205 }
206
207 printf("\n");
208 }
209
210 static void parse_wowlan_wake_event(struct nlattr **attrs)
211 {
212 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG],
213 *tb_match[NUM_NL80211_ATTR];
214
215 printf("WoWLAN wakeup\n");
216 if (!attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
217 printf("\twakeup not due to WoWLAN\n");
218 return;
219 }
220
221 nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
222 nla_data(attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
223 nla_len(attrs[NL80211_ATTR_WOWLAN_TRIGGERS]), NULL);
224
225 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT])
226 printf("\t* was disconnected\n");
227 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT])
228 printf("\t* magic packet received\n");
229 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN])
230 printf("\t* pattern index: %u\n",
231 nla_get_u32(tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]));
232 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE])
233 printf("\t* GTK rekey failure\n");
234 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST])
235 printf("\t* EAP identity request\n");
236 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE])
237 printf("\t* 4-way handshake\n");
238 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE])
239 printf("\t* RF-kill released\n");
240 if (tb[NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS]) {
241 struct nlattr *match, *freq;
242 int rem_nst, rem_nst2;
243
244 printf("\t* network detected\n");
245 nla_for_each_nested(match,
246 tb[NL80211_WOWLAN_TRIG_NET_DETECT_RESULTS],
247 rem_nst) {
248 nla_parse_nested(tb_match, NL80211_ATTR_MAX, 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 }
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
296 static void parse_nan_term(struct nlattr **attrs)
297 {
298 struct nlattr *func[NL80211_NAN_FUNC_ATTR_MAX + 1];
299
300 static struct nla_policy
301 nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
302 [NL80211_NAN_FUNC_TYPE] = { .type = NLA_U8 },
303 [NL80211_NAN_FUNC_SERVICE_ID] = { },
304 [NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
305 [NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
306 [NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
307 [NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
308 [NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
309 [NL80211_NAN_FUNC_FOLLOW_UP_DEST] = { },
310 [NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
311 [NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
312 [NL80211_NAN_FUNC_SERVICE_INFO] = { },
313 [NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
314 [NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
315 [NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
316 [NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8},
317 };
318
319 if (!attrs[NL80211_ATTR_COOKIE]) {
320 printf("Bad NAN func termination format - cookie is missing\n");
321 return;
322 }
323
324 if (nla_parse_nested(func, NL80211_NAN_FUNC_ATTR_MAX,
325 attrs[NL80211_ATTR_NAN_FUNC],
326 nan_func_policy)) {
327 printf("NAN: failed to parse nan func\n");
328 return;
329 }
330
331 if (!func[NL80211_NAN_FUNC_INSTANCE_ID]) {
332 printf("Bad NAN func termination format-instance id missing\n");
333 return;
334 }
335
336 if (!func[NL80211_NAN_FUNC_TERM_REASON]) {
337 printf("Bad NAN func termination format - reason is missing\n");
338 return;
339 }
340 printf("NAN(cookie=0x%llx): Termination event: id = %d, reason = ",
341 (long long int)nla_get_u64(attrs[NL80211_ATTR_COOKIE]),
342 nla_get_u8(func[NL80211_NAN_FUNC_INSTANCE_ID]));
343 switch (nla_get_u8(func[NL80211_NAN_FUNC_TERM_REASON])) {
344 case NL80211_NAN_FUNC_TERM_REASON_USER_REQUEST:
345 printf("user request\n");
346 break;
347 case NL80211_NAN_FUNC_TERM_REASON_TTL_EXPIRED:
348 printf("expired\n");
349 break;
350 case NL80211_NAN_FUNC_TERM_REASON_ERROR:
351 printf("error\n");
352 break;
353 default:
354 printf("unknown\n");
355 }
356 }
357
358 static void parse_nan_match(struct nlattr **attrs)
359 {
360 char macbuf[6*3];
361 __u64 cookie;
362 struct nlattr *match[NL80211_NAN_MATCH_ATTR_MAX + 1];
363 struct nlattr *local_func[NL80211_NAN_FUNC_ATTR_MAX + 1];
364 struct nlattr *peer_func[NL80211_NAN_FUNC_ATTR_MAX + 1];
365
366 static struct nla_policy
367 nan_match_policy[NL80211_NAN_MATCH_ATTR_MAX + 1] = {
368 [NL80211_NAN_MATCH_FUNC_LOCAL] = { .type = NLA_NESTED },
369 [NL80211_NAN_MATCH_FUNC_PEER] = { .type = NLA_NESTED },
370 };
371
372 static struct nla_policy
373 nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
374 [NL80211_NAN_FUNC_TYPE] = { .type = NLA_U8 },
375 [NL80211_NAN_FUNC_SERVICE_ID] = { },
376 [NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
377 [NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
378 [NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
379 [NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
380 [NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
381 [NL80211_NAN_FUNC_FOLLOW_UP_DEST] = { },
382 [NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
383 [NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
384 [NL80211_NAN_FUNC_SERVICE_INFO] = { },
385 [NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
386 [NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
387 [NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
388 [NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8},
389 };
390
391 cookie = nla_get_u64(attrs[NL80211_ATTR_COOKIE]);
392 mac_addr_n2a(macbuf, nla_data(attrs[NL80211_ATTR_MAC]));
393
394 if (nla_parse_nested(match, NL80211_NAN_MATCH_ATTR_MAX,
395 attrs[NL80211_ATTR_NAN_MATCH],
396 nan_match_policy)) {
397 printf("NAN: failed to parse nan match event\n");
398 return;
399 }
400
401 if (nla_parse_nested(local_func, NL80211_NAN_FUNC_ATTR_MAX,
402 match[NL80211_NAN_MATCH_FUNC_LOCAL],
403 nan_func_policy)) {
404 printf("NAN: failed to parse nan local func\n");
405 return;
406 }
407
408 if (nla_parse_nested(peer_func, NL80211_NAN_FUNC_ATTR_MAX,
409 match[NL80211_NAN_MATCH_FUNC_PEER],
410 nan_func_policy)) {
411 printf("NAN: failed to parse nan local func\n");
412 return;
413 }
414
415 if (nla_get_u8(peer_func[NL80211_NAN_FUNC_TYPE]) ==
416 NL80211_NAN_FUNC_PUBLISH) {
417 printf(
418 "NAN(cookie=0x%llx): DiscoveryResult, peer_id=%d, local_id=%d, peer_mac=%s",
419 cookie,
420 nla_get_u8(peer_func[NL80211_NAN_FUNC_INSTANCE_ID]),
421 nla_get_u8(local_func[NL80211_NAN_FUNC_INSTANCE_ID]),
422 macbuf);
423 if (peer_func[NL80211_NAN_FUNC_SERVICE_INFO])
424 printf(", info=%.*s",
425 nla_len(peer_func[NL80211_NAN_FUNC_SERVICE_INFO]),
426 (char *)nla_data(peer_func[NL80211_NAN_FUNC_SERVICE_INFO]));
427 } else if (nla_get_u8(peer_func[NL80211_NAN_FUNC_TYPE]) ==
428 NL80211_NAN_FUNC_SUBSCRIBE) {
429 printf(
430 "NAN(cookie=0x%llx): Replied, peer_id=%d, local_id=%d, peer_mac=%s",
431 cookie,
432 nla_get_u8(peer_func[NL80211_NAN_FUNC_INSTANCE_ID]),
433 nla_get_u8(local_func[NL80211_NAN_FUNC_INSTANCE_ID]),
434 macbuf);
435 } else if (nla_get_u8(peer_func[NL80211_NAN_FUNC_TYPE]) ==
436 NL80211_NAN_FUNC_FOLLOW_UP) {
437 printf(
438 "NAN(cookie=0x%llx): FollowUpReceive, peer_id=%d, local_id=%d, peer_mac=%s",
439 cookie,
440 nla_get_u8(peer_func[NL80211_NAN_FUNC_INSTANCE_ID]),
441 nla_get_u8(local_func[NL80211_NAN_FUNC_INSTANCE_ID]),
442 macbuf);
443 if (peer_func[NL80211_NAN_FUNC_SERVICE_INFO])
444 printf(", info=%.*s",
445 nla_len(peer_func[NL80211_NAN_FUNC_SERVICE_INFO]),
446 (char *)nla_data(peer_func[NL80211_NAN_FUNC_SERVICE_INFO]));
447 } else {
448 printf("NaN: Malformed event");
449 }
450
451 printf("\n");
452 }
453
454 static int print_event(struct nl_msg *msg, void *arg)
455 {
456 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
457 struct nlattr *tb[NL80211_ATTR_MAX + 1], *nst;
458 struct print_event_args *args = arg;
459 char ifname[100];
460 char macbuf[6*3];
461 __u8 reg_type;
462 struct ieee80211_beacon_channel chan_before_beacon, chan_after_beacon;
463 __u32 wiphy_idx = 0;
464 int rem_nst;
465 __u16 status;
466
467 if (args->time || args->reltime) {
468 unsigned long long usecs, previous;
469
470 previous = 1000000ULL * args->ts.tv_sec + args->ts.tv_usec;
471 gettimeofday(&args->ts, NULL);
472 usecs = 1000000ULL * args->ts.tv_sec + args->ts.tv_usec;
473 if (args->reltime) {
474 if (!args->have_ts) {
475 usecs = 0;
476 args->have_ts = true;
477 } else
478 usecs -= previous;
479 }
480 printf("%llu.%06llu: ", usecs/1000000, usecs % 1000000);
481 }
482
483 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
484 genlmsg_attrlen(gnlh, 0), NULL);
485
486 if (tb[NL80211_ATTR_IFINDEX] && tb[NL80211_ATTR_WIPHY]) {
487 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname);
488 printf("%s (phy #%d): ", ifname, nla_get_u32(tb[NL80211_ATTR_WIPHY]));
489 } else if (tb[NL80211_ATTR_WDEV] && tb[NL80211_ATTR_WIPHY]) {
490 printf("wdev 0x%llx (phy #%d): ",
491 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_WDEV]),
492 nla_get_u32(tb[NL80211_ATTR_WIPHY]));
493 } else if (tb[NL80211_ATTR_IFINDEX]) {
494 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname);
495 printf("%s: ", ifname);
496 } else if (tb[NL80211_ATTR_WDEV]) {
497 printf("wdev 0x%llx: ", (unsigned long long)nla_get_u64(tb[NL80211_ATTR_WDEV]));
498 } else if (tb[NL80211_ATTR_WIPHY]) {
499 printf("phy #%d: ", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
500 }
501
502 switch (gnlh->cmd) {
503 case NL80211_CMD_NEW_WIPHY:
504 printf("renamed to %s\n", nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]));
505 break;
506 case NL80211_CMD_TRIGGER_SCAN:
507 printf("scan started\n");
508 break;
509 case NL80211_CMD_NEW_SCAN_RESULTS:
510 printf("scan finished:");
511 /* fall through */
512 case NL80211_CMD_SCAN_ABORTED:
513 if (gnlh->cmd == NL80211_CMD_SCAN_ABORTED)
514 printf("scan aborted:");
515 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
516 nla_for_each_nested(nst, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem_nst)
517 printf(" %d", nla_get_u32(nst));
518 printf(",");
519 }
520 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
521 nla_for_each_nested(nst, tb[NL80211_ATTR_SCAN_SSIDS], rem_nst) {
522 printf(" \"");
523 print_ssid_escaped(nla_len(nst), nla_data(nst));
524 printf("\"");
525 }
526 }
527 printf("\n");
528 break;
529 case NL80211_CMD_START_SCHED_SCAN:
530 printf("scheduled scan started\n");
531 break;
532 case NL80211_CMD_SCHED_SCAN_STOPPED:
533 printf("sched scan stopped\n");
534 break;
535 case NL80211_CMD_SCHED_SCAN_RESULTS:
536 printf("got scheduled scan results\n");
537 break;
538 case NL80211_CMD_REG_CHANGE:
539 printf("regulatory domain change: ");
540
541 reg_type = nla_get_u8(tb[NL80211_ATTR_REG_TYPE]);
542
543 switch (reg_type) {
544 case NL80211_REGDOM_TYPE_COUNTRY:
545 printf("set to %s by %s request",
546 nla_get_string(tb[NL80211_ATTR_REG_ALPHA2]),
547 reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])));
548 if (tb[NL80211_ATTR_WIPHY])
549 printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
550 break;
551 case NL80211_REGDOM_TYPE_WORLD:
552 printf("set to world roaming by %s request",
553 reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])));
554 break;
555 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
556 printf("custom world roaming rules in place on phy%d by %s request",
557 nla_get_u32(tb[NL80211_ATTR_WIPHY]),
558 reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR])));
559 break;
560 case NL80211_REGDOM_TYPE_INTERSECTION:
561 printf("intersection used due to a request made by %s",
562 reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR])));
563 if (tb[NL80211_ATTR_WIPHY])
564 printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
565 break;
566 default:
567 printf("unknown source (upgrade this utility)");
568 break;
569 }
570
571 printf("\n");
572 break;
573 case NL80211_CMD_REG_BEACON_HINT:
574
575 wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
576
577 memset(&chan_before_beacon, 0, sizeof(chan_before_beacon));
578 memset(&chan_after_beacon, 0, sizeof(chan_after_beacon));
579
580 if (parse_beacon_hint_chan(tb[NL80211_ATTR_FREQ_BEFORE],
581 &chan_before_beacon))
582 break;
583 if (parse_beacon_hint_chan(tb[NL80211_ATTR_FREQ_AFTER],
584 &chan_after_beacon))
585 break;
586
587 if (chan_before_beacon.center_freq != chan_after_beacon.center_freq)
588 break;
589
590 /* A beacon hint is sent _only_ if something _did_ change */
591 printf("beacon hint:\n");
592
593 printf("phy%d %d MHz [%d]:\n",
594 wiphy_idx,
595 chan_before_beacon.center_freq,
596 ieee80211_frequency_to_channel(chan_before_beacon.center_freq));
597
598 if (chan_before_beacon.no_ir && !chan_after_beacon.no_ir) {
599 if (chan_before_beacon.no_ibss && !chan_after_beacon.no_ibss)
600 printf("\to Initiating radiation enabled\n");
601 else
602 printf("\to active scan enabled\n");
603 } else if (chan_before_beacon.no_ibss && !chan_after_beacon.no_ibss) {
604 printf("\to ibss enabled\n");
605 }
606
607 break;
608 case NL80211_CMD_NEW_STATION:
609 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
610 printf("new station %s\n", macbuf);
611 break;
612 case NL80211_CMD_DEL_STATION:
613 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
614 printf("del station %s\n", macbuf);
615 break;
616 case NL80211_CMD_JOIN_IBSS:
617 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
618 printf("IBSS %s joined\n", macbuf);
619 break;
620 case NL80211_CMD_AUTHENTICATE:
621 printf("auth");
622 if (tb[NL80211_ATTR_FRAME])
623 print_frame(args, tb[NL80211_ATTR_FRAME]);
624 else if (tb[NL80211_ATTR_TIMED_OUT])
625 printf(": timed out");
626 else
627 printf(": unknown event");
628 printf("\n");
629 break;
630 case NL80211_CMD_ASSOCIATE:
631 printf("assoc");
632 if (tb[NL80211_ATTR_FRAME])
633 print_frame(args, tb[NL80211_ATTR_FRAME]);
634 else if (tb[NL80211_ATTR_TIMED_OUT])
635 printf(": timed out");
636 else
637 printf(": unknown event");
638 printf("\n");
639 break;
640 case NL80211_CMD_DEAUTHENTICATE:
641 printf("deauth");
642 print_frame(args, tb[NL80211_ATTR_FRAME]);
643 printf("\n");
644 break;
645 case NL80211_CMD_DISASSOCIATE:
646 printf("disassoc");
647 print_frame(args, tb[NL80211_ATTR_FRAME]);
648 printf("\n");
649 break;
650 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
651 printf("unprotected deauth");
652 print_frame(args, tb[NL80211_ATTR_FRAME]);
653 printf("\n");
654 break;
655 case NL80211_CMD_UNPROT_DISASSOCIATE:
656 printf("unprotected disassoc");
657 print_frame(args, tb[NL80211_ATTR_FRAME]);
658 printf("\n");
659 break;
660 case NL80211_CMD_CONNECT:
661 status = 0;
662 if (tb[NL80211_ATTR_TIMED_OUT])
663 printf("timed out");
664 else if (!tb[NL80211_ATTR_STATUS_CODE])
665 printf("unknown connect status");
666 else if (nla_get_u16(tb[NL80211_ATTR_STATUS_CODE]) == 0)
667 printf("connected");
668 else {
669 status = nla_get_u16(tb[NL80211_ATTR_STATUS_CODE]);
670 printf("failed to connect");
671 }
672 if (tb[NL80211_ATTR_MAC]) {
673 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
674 printf(" to %s", macbuf);
675 }
676 if (status)
677 printf(", status: %d: %s", status, get_status_str(status));
678 printf("\n");
679 break;
680 case NL80211_CMD_ROAM:
681 printf("roamed");
682 if (tb[NL80211_ATTR_MAC]) {
683 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
684 printf(" to %s", macbuf);
685 }
686 printf("\n");
687 break;
688 case NL80211_CMD_DISCONNECT:
689 printf("disconnected");
690 if (tb[NL80211_ATTR_DISCONNECTED_BY_AP])
691 printf(" (by AP)");
692 else
693 printf(" (local request)");
694 if (tb[NL80211_ATTR_REASON_CODE])
695 printf(" reason: %d: %s", nla_get_u16(tb[NL80211_ATTR_REASON_CODE]),
696 get_reason_str(nla_get_u16(tb[NL80211_ATTR_REASON_CODE])));
697 printf("\n");
698 break;
699 case NL80211_CMD_REMAIN_ON_CHANNEL:
700 printf("remain on freq %d (%dms, cookie %llx)\n",
701 nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]),
702 nla_get_u32(tb[NL80211_ATTR_DURATION]),
703 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]));
704 break;
705 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
706 printf("done with remain on freq %d (cookie %llx)\n",
707 nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]),
708 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]));
709 break;
710 case NL80211_CMD_NOTIFY_CQM:
711 parse_cqm_event(tb);
712 break;
713 case NL80211_CMD_MICHAEL_MIC_FAILURE:
714 parse_mic_failure(tb);
715 break;
716 case NL80211_CMD_FRAME_TX_STATUS:
717 printf("mgmt TX status (cookie %llx): %s\n",
718 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]),
719 tb[NL80211_ATTR_ACK] ? "acked" : "no ack");
720 break;
721 case NL80211_CMD_PMKSA_CANDIDATE:
722 printf("PMKSA candidate found\n");
723 break;
724 case NL80211_CMD_SET_WOWLAN:
725 parse_wowlan_wake_event(tb);
726 break;
727 case NL80211_CMD_PROBE_CLIENT:
728 if (tb[NL80211_ATTR_MAC])
729 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
730 else
731 strcpy(macbuf, "??");
732 printf("probe client %s (cookie %llx): %s\n",
733 macbuf,
734 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]),
735 tb[NL80211_ATTR_ACK] ? "acked" : "no ack");
736 break;
737 case NL80211_CMD_VENDOR:
738 printf("vendor event %.6x:%d\n",
739 nla_get_u32(tb[NL80211_ATTR_VENDOR_ID]),
740 nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD]));
741 if (args->frame && tb[NL80211_ATTR_VENDOR_DATA])
742 iw_hexdump("vendor event",
743 nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
744 nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
745 break;
746 case NL80211_CMD_RADAR_DETECT: {
747 enum nl80211_radar_event event_type;
748 uint32_t freq;
749
750 if (!tb[NL80211_ATTR_RADAR_EVENT] ||
751 !tb[NL80211_ATTR_WIPHY_FREQ]) {
752 printf("BAD radar event\n");
753 break;
754 }
755
756 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
757 event_type = nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT]);
758
759 switch (event_type) {
760 case NL80211_RADAR_DETECTED:
761 printf("%d MHz: radar detected\n", freq);
762 break;
763 case NL80211_RADAR_CAC_FINISHED:
764 printf("%d MHz: CAC finished\n", freq);
765 break;
766 case NL80211_RADAR_CAC_ABORTED:
767 printf("%d MHz: CAC was aborted\n", freq);
768 break;
769 case NL80211_RADAR_NOP_FINISHED:
770 printf("%d MHz: NOP finished\n", freq);
771 break;
772 default:
773 printf("%d MHz: unknown radar event\n", freq);
774 }
775 }
776 break;
777 case NL80211_CMD_DEL_WIPHY:
778 printf("delete wiphy\n");
779 break;
780 case NL80211_CMD_DEL_NAN_FUNCTION:
781 parse_nan_term(tb);
782 break;
783 case NL80211_CMD_NAN_MATCH: {
784 parse_nan_match(tb);
785 break;
786 }
787 default:
788 printf("unknown event %d (%s)\n",
789 gnlh->cmd, command_name(gnlh->cmd));
790 break;
791 }
792
793 fflush(stdout);
794 return NL_SKIP;
795 }
796
797 struct wait_event {
798 int n_cmds, n_prints;
799 const __u32 *cmds;
800 const __u32 *prints;
801 __u32 cmd;
802 struct print_event_args *pargs;
803 };
804
805 static int wait_event(struct nl_msg *msg, void *arg)
806 {
807 struct wait_event *wait = arg;
808 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
809 int i;
810
811 if (wait->pargs) {
812 for (i = 0; i < wait->n_prints; i++) {
813 if (gnlh->cmd == wait->prints[i])
814 print_event(msg, wait->pargs);
815 }
816 }
817
818 for (i = 0; i < wait->n_cmds; i++) {
819 if (gnlh->cmd == wait->cmds[i])
820 wait->cmd = gnlh->cmd;
821 }
822
823 return NL_SKIP;
824 }
825
826 int __prepare_listen_events(struct nl80211_state *state)
827 {
828 int mcid, ret;
829
830 /* Configuration multicast group */
831 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "config");
832 if (mcid < 0)
833 return mcid;
834
835 ret = nl_socket_add_membership(state->nl_sock, mcid);
836 if (ret)
837 return ret;
838
839 /* Scan multicast group */
840 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "scan");
841 if (mcid >= 0) {
842 ret = nl_socket_add_membership(state->nl_sock, mcid);
843 if (ret)
844 return ret;
845 }
846
847 /* Regulatory multicast group */
848 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "regulatory");
849 if (mcid >= 0) {
850 ret = nl_socket_add_membership(state->nl_sock, mcid);
851 if (ret)
852 return ret;
853 }
854
855 /* MLME multicast group */
856 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "mlme");
857 if (mcid >= 0) {
858 ret = nl_socket_add_membership(state->nl_sock, mcid);
859 if (ret)
860 return ret;
861 }
862
863 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "vendor");
864 if (mcid >= 0) {
865 ret = nl_socket_add_membership(state->nl_sock, mcid);
866 if (ret)
867 return ret;
868 }
869
870 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "nan");
871 if (mcid >= 0) {
872 ret = nl_socket_add_membership(state->nl_sock, mcid);
873 if (ret)
874 return ret;
875 }
876
877 return 0;
878 }
879
880 __u32 __do_listen_events(struct nl80211_state *state,
881 const int n_waits, const __u32 *waits,
882 const int n_prints, const __u32 *prints,
883 struct print_event_args *args)
884 {
885 struct nl_cb *cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
886 struct wait_event wait_ev;
887
888 if (!cb) {
889 fprintf(stderr, "failed to allocate netlink callbacks\n");
890 return -ENOMEM;
891 }
892
893 /* no sequence checking for multicast messages */
894 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
895 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, valid_handler, NULL);
896
897 if (n_waits && waits) {
898 wait_ev.cmds = waits;
899 wait_ev.n_cmds = n_waits;
900 wait_ev.prints = prints;
901 wait_ev.n_prints = n_prints;
902 wait_ev.pargs = args;
903 register_handler(wait_event, &wait_ev);
904 } else
905 register_handler(print_event, args);
906
907 wait_ev.cmd = 0;
908
909 while (!wait_ev.cmd)
910 nl_recvmsgs(state->nl_sock, cb);
911
912 nl_cb_put(cb);
913
914 return wait_ev.cmd;
915 }
916
917 __u32 listen_events(struct nl80211_state *state,
918 const int n_waits, const __u32 *waits)
919 {
920 int ret;
921
922 ret = __prepare_listen_events(state);
923 if (ret)
924 return ret;
925
926 return __do_listen_events(state, n_waits, waits, 0, NULL, NULL);
927 }
928
929 static int print_events(struct nl80211_state *state,
930 struct nl_msg *msg,
931 int argc, char **argv,
932 enum id_input id)
933 {
934 struct print_event_args args;
935 int ret;
936
937 memset(&args, 0, sizeof(args));
938
939 argc--;
940 argv++;
941
942 while (argc > 0) {
943 if (strcmp(argv[0], "-f") == 0)
944 args.frame = true;
945 else if (strcmp(argv[0], "-t") == 0)
946 args.time = true;
947 else if (strcmp(argv[0], "-r") == 0)
948 args.reltime = true;
949 else
950 return 1;
951 argc--;
952 argv++;
953 }
954
955 if (args.time && args.reltime)
956 return 1;
957
958 if (argc)
959 return 1;
960
961 ret = __prepare_listen_events(state);
962 if (ret)
963 return ret;
964
965 return __do_listen_events(state, 0, NULL, 0, NULL, &args);
966 }
967 TOPLEVEL(event, "[-t|-r] [-f]", 0, 0, CIB_NONE, print_events,
968 "Monitor events from the kernel.\n"
969 "-t - print timestamp\n"
970 "-r - print relative timstamp\n"
971 "-f - print full frame for auth/assoc etc.");