]> git.ipfire.org Git - thirdparty/iw.git/blob - event.c
iw: event: also handle reg change on wiphy
[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 const char *ftm_fail_reason(unsigned int reason)
359 {
360 #define FTM_FAIL_REASON(x) case NL80211_PMSR_FTM_FAILURE_##x: return #x
361 switch (reason) {
362 FTM_FAIL_REASON(UNSPECIFIED);
363 FTM_FAIL_REASON(NO_RESPONSE);
364 FTM_FAIL_REASON(REJECTED);
365 FTM_FAIL_REASON(WRONG_CHANNEL);
366 FTM_FAIL_REASON(PEER_NOT_CAPABLE);
367 FTM_FAIL_REASON(INVALID_TIMESTAMP);
368 FTM_FAIL_REASON(PEER_BUSY);
369 FTM_FAIL_REASON(BAD_CHANGED_PARAMS);
370 default:
371 return "unknown";
372 }
373 }
374
375 static void parse_pmsr_ftm_data(struct nlattr *data)
376 {
377 struct nlattr *ftm[NL80211_PMSR_FTM_RESP_ATTR_MAX + 1];
378
379 printf(" FTM");
380 nla_parse_nested(ftm, NL80211_PMSR_FTM_RESP_ATTR_MAX, data, NULL);
381
382 if (ftm[NL80211_PMSR_FTM_RESP_ATTR_FAIL_REASON]) {
383 printf(" failed: %s (%d)",
384 ftm_fail_reason(nla_get_u32(ftm[NL80211_PMSR_FTM_RESP_ATTR_FAIL_REASON])),
385 nla_get_u32(ftm[NL80211_PMSR_FTM_RESP_ATTR_FAIL_REASON]));
386 if (ftm[NL80211_PMSR_FTM_RESP_ATTR_BUSY_RETRY_TIME])
387 printf(" retry after %us",
388 nla_get_u32(ftm[NL80211_PMSR_FTM_RESP_ATTR_BUSY_RETRY_TIME]));
389 printf("\n");
390 return;
391 }
392
393 printf("\n");
394
395 #define PFTM(tp, attr, sign) \
396 do { \
397 if (ftm[NL80211_PMSR_FTM_RESP_ATTR_##attr]) \
398 printf(" " #attr ": %lld\n", \
399 (sign long long)nla_get_##tp( \
400 ftm[NL80211_PMSR_FTM_RESP_ATTR_##attr])); \
401 } while (0)
402
403 PFTM(u32, BURST_INDEX, unsigned);
404 PFTM(u32, NUM_FTMR_ATTEMPTS, unsigned);
405 PFTM(u32, NUM_FTMR_SUCCESSES, unsigned);
406 PFTM(u8, NUM_BURSTS_EXP, unsigned);
407 PFTM(u8, BURST_DURATION, unsigned);
408 PFTM(u8, FTMS_PER_BURST, unsigned);
409 PFTM(u32, RSSI_AVG, signed);
410 PFTM(u32, RSSI_SPREAD, unsigned);
411 PFTM(u64, RTT_AVG, signed);
412 PFTM(u64, RTT_VARIANCE, unsigned);
413 PFTM(u64, RTT_SPREAD, unsigned);
414 PFTM(u64, DIST_AVG, signed);
415 PFTM(u64, DIST_VARIANCE, unsigned);
416 PFTM(u64, DIST_SPREAD, unsigned);
417
418 if (ftm[NL80211_PMSR_FTM_RESP_ATTR_TX_RATE]) {
419 char buf[100];
420
421 parse_bitrate(ftm[NL80211_PMSR_FTM_RESP_ATTR_TX_RATE],
422 buf, sizeof(buf));
423 printf(" TX bitrate: %s\n", buf);
424 }
425
426 if (ftm[NL80211_PMSR_FTM_RESP_ATTR_RX_RATE]) {
427 char buf[100];
428
429 parse_bitrate(ftm[NL80211_PMSR_FTM_RESP_ATTR_RX_RATE],
430 buf, sizeof(buf));
431 printf(" RX bitrate: %s\n", buf);
432 }
433
434 if (ftm[NL80211_PMSR_FTM_RESP_ATTR_LCI])
435 iw_hexdump(" LCI",
436 nla_data(ftm[NL80211_PMSR_FTM_RESP_ATTR_LCI]),
437 nla_len(ftm[NL80211_PMSR_FTM_RESP_ATTR_LCI]));
438
439 if (ftm[NL80211_PMSR_FTM_RESP_ATTR_CIVICLOC])
440 iw_hexdump(" civic location",
441 nla_data(ftm[NL80211_PMSR_FTM_RESP_ATTR_CIVICLOC]),
442 nla_len(ftm[NL80211_PMSR_FTM_RESP_ATTR_CIVICLOC]));
443 }
444
445 static const char *pmsr_status(unsigned int status)
446 {
447 #define PMSR_STATUS(x) case NL80211_PMSR_STATUS_##x: return #x
448 switch (status) {
449 PMSR_STATUS(SUCCESS);
450 PMSR_STATUS(REFUSED);
451 PMSR_STATUS(TIMEOUT);
452 PMSR_STATUS(FAILURE);
453 default:
454 return "unknown";
455 }
456 #undef PMSR_STATUS
457 }
458
459 static void parse_pmsr_peer(struct nlattr *peer)
460 {
461 struct nlattr *tb[NL80211_PMSR_PEER_ATTR_MAX + 1];
462 struct nlattr *resp[NL80211_PMSR_RESP_ATTR_MAX + 1];
463 struct nlattr *data[NL80211_PMSR_TYPE_MAX + 1];
464 char macbuf[6*3];
465 int err;
466
467 err = nla_parse_nested(tb, NL80211_PMSR_PEER_ATTR_MAX, peer, NULL);
468 if (err) {
469 printf(" Peer: failed to parse!\n");
470 return;
471 }
472
473 if (!tb[NL80211_PMSR_PEER_ATTR_ADDR]) {
474 printf(" Peer: no MAC address\n");
475 return;
476 }
477
478 mac_addr_n2a(macbuf, nla_data(tb[NL80211_PMSR_PEER_ATTR_ADDR]));
479 printf(" Peer %s:", macbuf);
480
481 if (!tb[NL80211_PMSR_PEER_ATTR_RESP]) {
482 printf(" no response!\n");
483 return;
484 }
485
486 err = nla_parse_nested(resp, NL80211_PMSR_RESP_ATTR_MAX,
487 tb[NL80211_PMSR_PEER_ATTR_RESP], NULL);
488 if (err) {
489 printf(" failed to parse response!\n");
490 return;
491 }
492
493 if (resp[NL80211_PMSR_RESP_ATTR_STATUS])
494 printf(" status=%d (%s)",
495 nla_get_u32(resp[NL80211_PMSR_RESP_ATTR_STATUS]),
496 pmsr_status(nla_get_u32(resp[NL80211_PMSR_RESP_ATTR_STATUS])));
497 if (resp[NL80211_PMSR_RESP_ATTR_HOST_TIME])
498 printf(" @%llu",
499 (unsigned long long)nla_get_u64(resp[NL80211_PMSR_RESP_ATTR_HOST_TIME]));
500 if (resp[NL80211_PMSR_RESP_ATTR_AP_TSF])
501 printf(" tsf=%llu",
502 (unsigned long long)nla_get_u64(resp[NL80211_PMSR_RESP_ATTR_AP_TSF]));
503 if (resp[NL80211_PMSR_RESP_ATTR_FINAL])
504 printf(" (final)");
505
506 if (!resp[NL80211_PMSR_RESP_ATTR_DATA]) {
507 printf(" - no data!\n");
508 return;
509 }
510
511 printf("\n");
512
513 nla_parse_nested(data, NL80211_PMSR_TYPE_MAX,
514 resp[NL80211_PMSR_RESP_ATTR_DATA], NULL);
515
516 if (data[NL80211_PMSR_TYPE_FTM])
517 parse_pmsr_ftm_data(data[NL80211_PMSR_TYPE_FTM]);
518 }
519
520 static void parse_pmsr_result(struct nlattr **tb,
521 struct print_event_args *pargs)
522 {
523 struct nlattr *pmsr[NL80211_PMSR_ATTR_MAX + 1];
524 struct nlattr *peer;
525 unsigned long long cookie;
526 int err, i;
527
528 if (!tb[NL80211_ATTR_COOKIE]) {
529 printf("Peer measurements: no cookie!\n");
530 return;
531 }
532 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
533
534 if (!tb[NL80211_ATTR_PEER_MEASUREMENTS]) {
535 printf("Peer measurements: no measurement data!\n");
536 return;
537 }
538
539 err = nla_parse_nested(pmsr, NL80211_PMSR_ATTR_MAX,
540 tb[NL80211_ATTR_PEER_MEASUREMENTS], NULL);
541 if (err) {
542 printf("Peer measurements: failed to parse measurement data!\n");
543 return;
544 }
545
546 if (!pmsr[NL80211_PMSR_ATTR_PEERS]) {
547 printf("Peer measurements: no peer data!\n");
548 return;
549 }
550
551 printf("Peer measurements (cookie %llu):\n", cookie);
552
553 nla_for_each_nested(peer, pmsr[NL80211_PMSR_ATTR_PEERS], i)
554 parse_pmsr_peer(peer);
555 }
556
557 static void parse_nan_match(struct nlattr **attrs)
558 {
559 char macbuf[6*3];
560 __u64 cookie;
561 struct nlattr *match[NL80211_NAN_MATCH_ATTR_MAX + 1];
562 struct nlattr *local_func[NL80211_NAN_FUNC_ATTR_MAX + 1];
563 struct nlattr *peer_func[NL80211_NAN_FUNC_ATTR_MAX + 1];
564
565 static struct nla_policy
566 nan_match_policy[NL80211_NAN_MATCH_ATTR_MAX + 1] = {
567 [NL80211_NAN_MATCH_FUNC_LOCAL] = { .type = NLA_NESTED },
568 [NL80211_NAN_MATCH_FUNC_PEER] = { .type = NLA_NESTED },
569 };
570
571 static struct nla_policy
572 nan_func_policy[NL80211_NAN_FUNC_ATTR_MAX + 1] = {
573 [NL80211_NAN_FUNC_TYPE] = { .type = NLA_U8 },
574 [NL80211_NAN_FUNC_SERVICE_ID] = { },
575 [NL80211_NAN_FUNC_PUBLISH_TYPE] = { .type = NLA_U8 },
576 [NL80211_NAN_FUNC_PUBLISH_BCAST] = { .type = NLA_FLAG },
577 [NL80211_NAN_FUNC_SUBSCRIBE_ACTIVE] = { .type = NLA_FLAG },
578 [NL80211_NAN_FUNC_FOLLOW_UP_ID] = { .type = NLA_U8 },
579 [NL80211_NAN_FUNC_FOLLOW_UP_REQ_ID] = { .type = NLA_U8 },
580 [NL80211_NAN_FUNC_FOLLOW_UP_DEST] = { },
581 [NL80211_NAN_FUNC_CLOSE_RANGE] = { .type = NLA_FLAG },
582 [NL80211_NAN_FUNC_TTL] = { .type = NLA_U32 },
583 [NL80211_NAN_FUNC_SERVICE_INFO] = { },
584 [NL80211_NAN_FUNC_SRF] = { .type = NLA_NESTED },
585 [NL80211_NAN_FUNC_RX_MATCH_FILTER] = { .type = NLA_NESTED },
586 [NL80211_NAN_FUNC_TX_MATCH_FILTER] = { .type = NLA_NESTED },
587 [NL80211_NAN_FUNC_INSTANCE_ID] = { .type = NLA_U8},
588 };
589
590 cookie = nla_get_u64(attrs[NL80211_ATTR_COOKIE]);
591 mac_addr_n2a(macbuf, nla_data(attrs[NL80211_ATTR_MAC]));
592
593 if (nla_parse_nested(match, NL80211_NAN_MATCH_ATTR_MAX,
594 attrs[NL80211_ATTR_NAN_MATCH],
595 nan_match_policy)) {
596 printf("NAN: failed to parse nan match event\n");
597 return;
598 }
599
600 if (nla_parse_nested(local_func, NL80211_NAN_FUNC_ATTR_MAX,
601 match[NL80211_NAN_MATCH_FUNC_LOCAL],
602 nan_func_policy)) {
603 printf("NAN: failed to parse nan local func\n");
604 return;
605 }
606
607 if (nla_parse_nested(peer_func, NL80211_NAN_FUNC_ATTR_MAX,
608 match[NL80211_NAN_MATCH_FUNC_PEER],
609 nan_func_policy)) {
610 printf("NAN: failed to parse nan local func\n");
611 return;
612 }
613
614 if (nla_get_u8(peer_func[NL80211_NAN_FUNC_TYPE]) ==
615 NL80211_NAN_FUNC_PUBLISH) {
616 printf(
617 "NAN(cookie=0x%llx): DiscoveryResult, peer_id=%d, local_id=%d, peer_mac=%s",
618 cookie,
619 nla_get_u8(peer_func[NL80211_NAN_FUNC_INSTANCE_ID]),
620 nla_get_u8(local_func[NL80211_NAN_FUNC_INSTANCE_ID]),
621 macbuf);
622 if (peer_func[NL80211_NAN_FUNC_SERVICE_INFO])
623 printf(", info=%.*s",
624 nla_len(peer_func[NL80211_NAN_FUNC_SERVICE_INFO]),
625 (char *)nla_data(peer_func[NL80211_NAN_FUNC_SERVICE_INFO]));
626 } else if (nla_get_u8(peer_func[NL80211_NAN_FUNC_TYPE]) ==
627 NL80211_NAN_FUNC_SUBSCRIBE) {
628 printf(
629 "NAN(cookie=0x%llx): Replied, peer_id=%d, local_id=%d, peer_mac=%s",
630 cookie,
631 nla_get_u8(peer_func[NL80211_NAN_FUNC_INSTANCE_ID]),
632 nla_get_u8(local_func[NL80211_NAN_FUNC_INSTANCE_ID]),
633 macbuf);
634 } else if (nla_get_u8(peer_func[NL80211_NAN_FUNC_TYPE]) ==
635 NL80211_NAN_FUNC_FOLLOW_UP) {
636 printf(
637 "NAN(cookie=0x%llx): FollowUpReceive, peer_id=%d, local_id=%d, peer_mac=%s",
638 cookie,
639 nla_get_u8(peer_func[NL80211_NAN_FUNC_INSTANCE_ID]),
640 nla_get_u8(local_func[NL80211_NAN_FUNC_INSTANCE_ID]),
641 macbuf);
642 if (peer_func[NL80211_NAN_FUNC_SERVICE_INFO])
643 printf(", info=%.*s",
644 nla_len(peer_func[NL80211_NAN_FUNC_SERVICE_INFO]),
645 (char *)nla_data(peer_func[NL80211_NAN_FUNC_SERVICE_INFO]));
646 } else {
647 printf("NaN: Malformed event");
648 }
649
650 printf("\n");
651 }
652
653 static void parse_new_peer_candidate(struct nlattr **attrs)
654 {
655 char macbuf[ETH_ALEN * 3];
656 int32_t sig_dbm;
657
658 printf("new peer candidate");
659 if (attrs[NL80211_ATTR_MAC]) {
660 mac_addr_n2a(macbuf, nla_data(attrs[NL80211_ATTR_MAC]));
661 printf(" %s", macbuf);
662 }
663 if (attrs[NL80211_ATTR_RX_SIGNAL_DBM]) {
664 sig_dbm = nla_get_u32(attrs[NL80211_ATTR_RX_SIGNAL_DBM]);
665 printf(" %d dBm", sig_dbm);
666 }
667
668 printf("\n");
669 }
670
671 static void parse_recv_interface(struct nlattr **attrs, int command)
672 {
673 switch (command) {
674 case NL80211_CMD_NEW_INTERFACE:
675 printf("new interface");
676 break;
677 case NL80211_CMD_DEL_INTERFACE:
678 printf("del interface");
679 break;
680 case NL80211_CMD_SET_INTERFACE:
681 printf("set interface");
682 break;
683 default:
684 printf("unknown interface command (%i) received\n", command);
685 return;
686 }
687
688 if (attrs[NL80211_ATTR_IFTYPE]) {
689 printf(" type ");
690 switch (nla_get_u32(attrs[NL80211_ATTR_IFTYPE])) {
691 case NL80211_IFTYPE_STATION:
692 printf("station");
693 break;
694 case NL80211_IFTYPE_AP:
695 printf("access point");
696 break;
697 case NL80211_IFTYPE_MESH_POINT:
698 printf("mesh point");
699 break;
700 case NL80211_IFTYPE_ADHOC:
701 printf("IBSS");
702 break;
703 case NL80211_IFTYPE_MONITOR:
704 printf("monitor");
705 break;
706 case NL80211_IFTYPE_AP_VLAN:
707 printf("AP-VLAN");
708 break;
709 case NL80211_IFTYPE_WDS:
710 printf("WDS");
711 break;
712 case NL80211_IFTYPE_P2P_CLIENT:
713 printf("P2P-client");
714 break;
715 case NL80211_IFTYPE_P2P_GO:
716 printf("P2P-GO");
717 break;
718 case NL80211_IFTYPE_P2P_DEVICE:
719 printf("P2P-Device");
720 break;
721 case NL80211_IFTYPE_OCB:
722 printf("OCB");
723 break;
724 case NL80211_IFTYPE_NAN:
725 printf("NAN");
726 break;
727 default:
728 printf("unknown (%d)",
729 nla_get_u32(attrs[NL80211_ATTR_IFTYPE]));
730 break;
731 }
732 }
733
734 if (attrs[NL80211_ATTR_MESH_ID]) {
735 printf(" meshid ");
736 print_ssid_escaped(nla_len(attrs[NL80211_ATTR_MESH_ID]),
737 nla_data(attrs[NL80211_ATTR_MESH_ID]));
738 }
739
740 if (attrs[NL80211_ATTR_4ADDR]) {
741 printf(" use 4addr %d", nla_get_u8(attrs[NL80211_ATTR_4ADDR]));
742 }
743
744 printf("\n");
745 }
746
747 static void parse_sta_opmode_changed(struct nlattr **attrs)
748 {
749 char macbuf[ETH_ALEN*3];
750
751 printf("sta opmode changed");
752
753 if (attrs[NL80211_ATTR_MAC]) {
754 mac_addr_n2a(macbuf, nla_data(attrs[NL80211_ATTR_MAC]));
755 printf(" %s", macbuf);
756 }
757
758 if (attrs[NL80211_ATTR_SMPS_MODE])
759 printf(" smps mode %d", nla_get_u8(attrs[NL80211_ATTR_SMPS_MODE]));
760
761 if (attrs[NL80211_ATTR_CHANNEL_WIDTH])
762 printf(" chan width %d", nla_get_u8(attrs[NL80211_ATTR_CHANNEL_WIDTH]));
763
764 if (attrs[NL80211_ATTR_NSS])
765 printf(" nss %d", nla_get_u8(attrs[NL80211_ATTR_NSS]));
766
767 printf("\n");
768 }
769
770 static void parse_ch_switch_notify(struct nlattr **attrs, int command)
771 {
772 switch (command) {
773 case NL80211_CMD_CH_SWITCH_STARTED_NOTIFY:
774 printf("channel switch started");
775 break;
776 case NL80211_CMD_CH_SWITCH_NOTIFY:
777 printf("channel switch");
778 break;
779 default:
780 printf("unknown channel switch command (%i) received\n", command);
781 return;
782 }
783
784 if (attrs[NL80211_ATTR_CH_SWITCH_COUNT])
785 printf(" (count=%d)", nla_get_u32(attrs[NL80211_ATTR_CH_SWITCH_COUNT]));
786
787 if (attrs[NL80211_ATTR_WIPHY_FREQ])
788 printf(" freq=%d", nla_get_u32(attrs[NL80211_ATTR_WIPHY_FREQ]));
789
790 if (attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
791 printf(" width=");
792 switch(nla_get_u32(attrs[NL80211_ATTR_CHANNEL_WIDTH])) {
793 case NL80211_CHAN_WIDTH_20_NOHT:
794 case NL80211_CHAN_WIDTH_20:
795 printf("\"20 MHz\"");
796 break;
797 case NL80211_CHAN_WIDTH_40:
798 printf("\"40 MHz\"");
799 break;
800 case NL80211_CHAN_WIDTH_80:
801 printf("\"80 MHz\"");
802 break;
803 case NL80211_CHAN_WIDTH_80P80:
804 printf("\"80+80 MHz\"");
805 break;
806 case NL80211_CHAN_WIDTH_160:
807 printf("\"160 MHz\"");
808 break;
809 case NL80211_CHAN_WIDTH_5:
810 printf("\"5 MHz\"");
811 break;
812 case NL80211_CHAN_WIDTH_10:
813 printf("\"10 MHz\"");
814 break;
815 default:
816 printf("\"unknown\"");
817 }
818 }
819
820 if (attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
821 printf(" type=");
822 switch(nla_get_u32(attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE])) {
823 case NL80211_CHAN_NO_HT:
824 printf("\"No HT\"");
825 break;
826 case NL80211_CHAN_HT20:
827 printf("\"HT20\"");
828 break;
829 case NL80211_CHAN_HT40MINUS:
830 printf("\"HT40-\"");
831 break;
832 case NL80211_CHAN_HT40PLUS:
833 printf("\"HT40+\"");
834 break;
835 }
836 }
837
838 if (attrs[NL80211_ATTR_CENTER_FREQ1])
839 printf(" freq1=%d", nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ1]));
840
841 if (attrs[NL80211_ATTR_CENTER_FREQ2])
842 printf(" freq2=%d", nla_get_u32(attrs[NL80211_ATTR_CENTER_FREQ2]));
843
844 printf("\n");
845 }
846
847 static int print_event(struct nl_msg *msg, void *arg)
848 {
849 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
850 struct nlattr *tb[NL80211_ATTR_MAX + 1], *nst;
851 struct print_event_args *args = arg;
852 char ifname[100];
853 char macbuf[6*3];
854 __u8 reg_type;
855 struct ieee80211_beacon_channel chan_before_beacon, chan_after_beacon;
856 __u32 wiphy_idx = 0;
857 int rem_nst;
858 __u16 status;
859
860 if (args->time || args->reltime) {
861 unsigned long long usecs, previous;
862
863 previous = 1000000ULL * args->ts.tv_sec + args->ts.tv_usec;
864 gettimeofday(&args->ts, NULL);
865 usecs = 1000000ULL * args->ts.tv_sec + args->ts.tv_usec;
866 if (args->reltime) {
867 if (!args->have_ts) {
868 usecs = 0;
869 args->have_ts = true;
870 } else
871 usecs -= previous;
872 }
873 printf("%llu.%06llu: ", usecs/1000000, usecs % 1000000);
874 }
875
876 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
877 genlmsg_attrlen(gnlh, 0), NULL);
878
879 if (tb[NL80211_ATTR_IFINDEX] && tb[NL80211_ATTR_WIPHY]) {
880 /* if_indextoname may fails on delete interface/wiphy event */
881 if(if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname))
882 printf("%s (phy #%d): ", ifname, nla_get_u32(tb[NL80211_ATTR_WIPHY]));
883 else
884 printf("phy #%d: ", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
885 } else if (tb[NL80211_ATTR_WDEV] && tb[NL80211_ATTR_WIPHY]) {
886 printf("wdev 0x%llx (phy #%d): ",
887 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_WDEV]),
888 nla_get_u32(tb[NL80211_ATTR_WIPHY]));
889 } else if (tb[NL80211_ATTR_IFINDEX]) {
890 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname);
891 printf("%s: ", ifname);
892 } else if (tb[NL80211_ATTR_WDEV]) {
893 printf("wdev 0x%llx: ", (unsigned long long)nla_get_u64(tb[NL80211_ATTR_WDEV]));
894 } else if (tb[NL80211_ATTR_WIPHY]) {
895 printf("phy #%d: ", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
896 }
897
898 switch (gnlh->cmd) {
899 case NL80211_CMD_NEW_WIPHY:
900 printf("renamed to %s\n", nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]));
901 break;
902 case NL80211_CMD_TRIGGER_SCAN:
903 printf("scan started\n");
904 break;
905 case NL80211_CMD_NEW_SCAN_RESULTS:
906 printf("scan finished:");
907 /* fall through */
908 case NL80211_CMD_SCAN_ABORTED:
909 if (gnlh->cmd == NL80211_CMD_SCAN_ABORTED)
910 printf("scan aborted:");
911 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
912 nla_for_each_nested(nst, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem_nst)
913 printf(" %d", nla_get_u32(nst));
914 printf(",");
915 }
916 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
917 nla_for_each_nested(nst, tb[NL80211_ATTR_SCAN_SSIDS], rem_nst) {
918 printf(" \"");
919 print_ssid_escaped(nla_len(nst), nla_data(nst));
920 printf("\"");
921 }
922 }
923 printf("\n");
924 break;
925 case NL80211_CMD_START_SCHED_SCAN:
926 printf("scheduled scan started\n");
927 break;
928 case NL80211_CMD_SCHED_SCAN_STOPPED:
929 printf("sched scan stopped\n");
930 break;
931 case NL80211_CMD_SCHED_SCAN_RESULTS:
932 printf("got scheduled scan results\n");
933 break;
934 case NL80211_CMD_WIPHY_REG_CHANGE:
935 case NL80211_CMD_REG_CHANGE:
936 if(gnlh->cmd == NL80211_CMD_WIPHY_REG_CHANGE)
937 printf("regulatory domain change (phy): ");
938 else
939 printf("regulatory domain change: ");
940
941 reg_type = nla_get_u8(tb[NL80211_ATTR_REG_TYPE]);
942
943 switch (reg_type) {
944 case NL80211_REGDOM_TYPE_COUNTRY:
945 printf("set to %s by %s request",
946 nla_get_string(tb[NL80211_ATTR_REG_ALPHA2]),
947 reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])));
948 if (tb[NL80211_ATTR_WIPHY])
949 printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
950 break;
951 case NL80211_REGDOM_TYPE_WORLD:
952 printf("set to world roaming by %s request",
953 reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])));
954 break;
955 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
956 printf("custom world roaming rules in place on phy%d by %s request",
957 nla_get_u32(tb[NL80211_ATTR_WIPHY]),
958 reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR])));
959 break;
960 case NL80211_REGDOM_TYPE_INTERSECTION:
961 printf("intersection used due to a request made by %s",
962 reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR])));
963 if (tb[NL80211_ATTR_WIPHY])
964 printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
965 break;
966 default:
967 printf("unknown source (upgrade this utility)");
968 break;
969 }
970
971 printf("\n");
972 break;
973 case NL80211_CMD_REG_BEACON_HINT:
974
975 wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
976
977 memset(&chan_before_beacon, 0, sizeof(chan_before_beacon));
978 memset(&chan_after_beacon, 0, sizeof(chan_after_beacon));
979
980 if (parse_beacon_hint_chan(tb[NL80211_ATTR_FREQ_BEFORE],
981 &chan_before_beacon))
982 break;
983 if (parse_beacon_hint_chan(tb[NL80211_ATTR_FREQ_AFTER],
984 &chan_after_beacon))
985 break;
986
987 if (chan_before_beacon.center_freq != chan_after_beacon.center_freq)
988 break;
989
990 /* A beacon hint is sent _only_ if something _did_ change */
991 printf("beacon hint:\n");
992
993 printf("phy%d %d MHz [%d]:\n",
994 wiphy_idx,
995 chan_before_beacon.center_freq,
996 ieee80211_frequency_to_channel(chan_before_beacon.center_freq));
997
998 if (chan_before_beacon.no_ir && !chan_after_beacon.no_ir) {
999 if (chan_before_beacon.no_ibss && !chan_after_beacon.no_ibss)
1000 printf("\to Initiating radiation enabled\n");
1001 else
1002 printf("\to active scan enabled\n");
1003 } else if (chan_before_beacon.no_ibss && !chan_after_beacon.no_ibss) {
1004 printf("\to ibss enabled\n");
1005 }
1006
1007 break;
1008 case NL80211_CMD_NEW_STATION:
1009 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
1010 printf("new station %s\n", macbuf);
1011 break;
1012 case NL80211_CMD_DEL_STATION:
1013 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
1014 printf("del station %s\n", macbuf);
1015 break;
1016 case NL80211_CMD_JOIN_IBSS:
1017 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
1018 printf("IBSS %s joined\n", macbuf);
1019 break;
1020 case NL80211_CMD_AUTHENTICATE:
1021 printf("auth");
1022 if (tb[NL80211_ATTR_FRAME])
1023 print_frame(args, tb[NL80211_ATTR_FRAME]);
1024 else if (tb[NL80211_ATTR_TIMED_OUT])
1025 printf(": timed out");
1026 else
1027 printf(": unknown event");
1028 printf("\n");
1029 break;
1030 case NL80211_CMD_ASSOCIATE:
1031 printf("assoc");
1032 if (tb[NL80211_ATTR_FRAME])
1033 print_frame(args, tb[NL80211_ATTR_FRAME]);
1034 else if (tb[NL80211_ATTR_TIMED_OUT])
1035 printf(": timed out");
1036 else
1037 printf(": unknown event");
1038 printf("\n");
1039 break;
1040 case NL80211_CMD_DEAUTHENTICATE:
1041 printf("deauth");
1042 print_frame(args, tb[NL80211_ATTR_FRAME]);
1043 printf("\n");
1044 break;
1045 case NL80211_CMD_DISASSOCIATE:
1046 printf("disassoc");
1047 print_frame(args, tb[NL80211_ATTR_FRAME]);
1048 printf("\n");
1049 break;
1050 case NL80211_CMD_UNPROT_DEAUTHENTICATE:
1051 printf("unprotected deauth");
1052 print_frame(args, tb[NL80211_ATTR_FRAME]);
1053 printf("\n");
1054 break;
1055 case NL80211_CMD_UNPROT_DISASSOCIATE:
1056 printf("unprotected disassoc");
1057 print_frame(args, tb[NL80211_ATTR_FRAME]);
1058 printf("\n");
1059 break;
1060 case NL80211_CMD_CONNECT:
1061 status = 0;
1062 if (tb[NL80211_ATTR_TIMED_OUT])
1063 printf("timed out");
1064 else if (!tb[NL80211_ATTR_STATUS_CODE])
1065 printf("unknown connect status");
1066 else if (nla_get_u16(tb[NL80211_ATTR_STATUS_CODE]) == 0)
1067 printf("connected");
1068 else {
1069 status = nla_get_u16(tb[NL80211_ATTR_STATUS_CODE]);
1070 printf("failed to connect");
1071 }
1072 if (tb[NL80211_ATTR_MAC]) {
1073 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
1074 printf(" to %s", macbuf);
1075 }
1076 if (status)
1077 printf(", status: %d: %s", status, get_status_str(status));
1078 printf("\n");
1079 break;
1080 case NL80211_CMD_ROAM:
1081 printf("roamed");
1082 if (tb[NL80211_ATTR_MAC]) {
1083 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
1084 printf(" to %s", macbuf);
1085 }
1086 printf("\n");
1087 break;
1088 case NL80211_CMD_DISCONNECT:
1089 printf("disconnected");
1090 if (tb[NL80211_ATTR_DISCONNECTED_BY_AP])
1091 printf(" (by AP)");
1092 else
1093 printf(" (local request)");
1094 if (tb[NL80211_ATTR_REASON_CODE])
1095 printf(" reason: %d: %s", nla_get_u16(tb[NL80211_ATTR_REASON_CODE]),
1096 get_reason_str(nla_get_u16(tb[NL80211_ATTR_REASON_CODE])));
1097 printf("\n");
1098 break;
1099 case NL80211_CMD_REMAIN_ON_CHANNEL:
1100 printf("remain on freq %d (%dms, cookie %llx)\n",
1101 nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]),
1102 nla_get_u32(tb[NL80211_ATTR_DURATION]),
1103 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]));
1104 break;
1105 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
1106 printf("done with remain on freq %d (cookie %llx)\n",
1107 nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]),
1108 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]));
1109 break;
1110 case NL80211_CMD_FRAME_WAIT_CANCEL:
1111 printf("frame wait cancel on freq %d (cookie %llx)\n",
1112 nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]),
1113 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]));
1114 break;
1115 case NL80211_CMD_NOTIFY_CQM:
1116 parse_cqm_event(tb);
1117 break;
1118 case NL80211_CMD_MICHAEL_MIC_FAILURE:
1119 parse_mic_failure(tb);
1120 break;
1121 case NL80211_CMD_FRAME_TX_STATUS:
1122 printf("mgmt TX status (cookie %llx): %s\n",
1123 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]),
1124 tb[NL80211_ATTR_ACK] ? "acked" : "no ack");
1125 break;
1126 case NL80211_CMD_PMKSA_CANDIDATE:
1127 printf("PMKSA candidate found\n");
1128 break;
1129 case NL80211_CMD_SET_WOWLAN:
1130 parse_wowlan_wake_event(tb);
1131 break;
1132 case NL80211_CMD_PROBE_CLIENT:
1133 if (tb[NL80211_ATTR_MAC])
1134 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
1135 else
1136 strcpy(macbuf, "??");
1137 printf("probe client %s (cookie %llx): %s\n",
1138 macbuf,
1139 (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]),
1140 tb[NL80211_ATTR_ACK] ? "acked" : "no ack");
1141 break;
1142 case NL80211_CMD_VENDOR:
1143 printf("vendor event %.6x:%d\n",
1144 nla_get_u32(tb[NL80211_ATTR_VENDOR_ID]),
1145 nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD]));
1146 if (args->frame && tb[NL80211_ATTR_VENDOR_DATA])
1147 iw_hexdump("vendor event",
1148 nla_data(tb[NL80211_ATTR_VENDOR_DATA]),
1149 nla_len(tb[NL80211_ATTR_VENDOR_DATA]));
1150 break;
1151 case NL80211_CMD_RADAR_DETECT: {
1152 enum nl80211_radar_event event_type;
1153 uint32_t freq;
1154
1155 if (!tb[NL80211_ATTR_RADAR_EVENT] ||
1156 !tb[NL80211_ATTR_WIPHY_FREQ]) {
1157 printf("BAD radar event\n");
1158 break;
1159 }
1160
1161 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
1162 event_type = nla_get_u32(tb[NL80211_ATTR_RADAR_EVENT]);
1163
1164 switch (event_type) {
1165 case NL80211_RADAR_DETECTED:
1166 printf("%d MHz: radar detected\n", freq);
1167 break;
1168 case NL80211_RADAR_CAC_FINISHED:
1169 printf("%d MHz: CAC finished\n", freq);
1170 break;
1171 case NL80211_RADAR_CAC_ABORTED:
1172 printf("%d MHz: CAC was aborted\n", freq);
1173 break;
1174 case NL80211_RADAR_NOP_FINISHED:
1175 printf("%d MHz: NOP finished\n", freq);
1176 break;
1177 default:
1178 printf("%d MHz: unknown radar event\n", freq);
1179 }
1180 }
1181 break;
1182 case NL80211_CMD_DEL_WIPHY:
1183 printf("delete wiphy\n");
1184 break;
1185 case NL80211_CMD_PEER_MEASUREMENT_RESULT:
1186 parse_pmsr_result(tb, args);
1187 break;
1188 case NL80211_CMD_PEER_MEASUREMENT_COMPLETE:
1189 printf("peer measurement complete\n");
1190 break;
1191 case NL80211_CMD_DEL_NAN_FUNCTION:
1192 parse_nan_term(tb);
1193 break;
1194 case NL80211_CMD_NAN_MATCH:
1195 parse_nan_match(tb);
1196 break;
1197 case NL80211_CMD_NEW_PEER_CANDIDATE:
1198 parse_new_peer_candidate(tb);
1199 break;
1200 case NL80211_CMD_NEW_INTERFACE:
1201 case NL80211_CMD_SET_INTERFACE:
1202 case NL80211_CMD_DEL_INTERFACE:
1203 parse_recv_interface(tb, gnlh->cmd);
1204 break;
1205 case NL80211_CMD_STA_OPMODE_CHANGED:
1206 parse_sta_opmode_changed(tb);
1207 break;
1208 case NL80211_CMD_STOP_AP:
1209 printf("stop ap\n");
1210 break;
1211 case NL80211_CMD_CH_SWITCH_STARTED_NOTIFY:
1212 case NL80211_CMD_CH_SWITCH_NOTIFY:
1213 parse_ch_switch_notify(tb, gnlh->cmd);
1214 break;
1215 default:
1216 printf("unknown event %d (%s)\n",
1217 gnlh->cmd, command_name(gnlh->cmd));
1218 break;
1219 }
1220
1221 fflush(stdout);
1222 return NL_SKIP;
1223 }
1224
1225 struct wait_event {
1226 int n_cmds, n_prints;
1227 const __u32 *cmds;
1228 const __u32 *prints;
1229 __u32 cmd;
1230 struct print_event_args *pargs;
1231 };
1232
1233 static int wait_event(struct nl_msg *msg, void *arg)
1234 {
1235 struct wait_event *wait = arg;
1236 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1237 int i;
1238
1239 if (wait->pargs) {
1240 for (i = 0; i < wait->n_prints; i++) {
1241 if (gnlh->cmd == wait->prints[i])
1242 print_event(msg, wait->pargs);
1243 }
1244 }
1245
1246 for (i = 0; i < wait->n_cmds; i++) {
1247 if (gnlh->cmd == wait->cmds[i])
1248 wait->cmd = gnlh->cmd;
1249 }
1250
1251 return NL_SKIP;
1252 }
1253
1254 int __prepare_listen_events(struct nl80211_state *state)
1255 {
1256 int mcid, ret;
1257
1258 /* Configuration multicast group */
1259 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "config");
1260 if (mcid < 0)
1261 return mcid;
1262
1263 ret = nl_socket_add_membership(state->nl_sock, mcid);
1264 if (ret)
1265 return ret;
1266
1267 /* Scan multicast group */
1268 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "scan");
1269 if (mcid >= 0) {
1270 ret = nl_socket_add_membership(state->nl_sock, mcid);
1271 if (ret)
1272 return ret;
1273 }
1274
1275 /* Regulatory multicast group */
1276 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "regulatory");
1277 if (mcid >= 0) {
1278 ret = nl_socket_add_membership(state->nl_sock, mcid);
1279 if (ret)
1280 return ret;
1281 }
1282
1283 /* MLME multicast group */
1284 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "mlme");
1285 if (mcid >= 0) {
1286 ret = nl_socket_add_membership(state->nl_sock, mcid);
1287 if (ret)
1288 return ret;
1289 }
1290
1291 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "vendor");
1292 if (mcid >= 0) {
1293 ret = nl_socket_add_membership(state->nl_sock, mcid);
1294 if (ret)
1295 return ret;
1296 }
1297
1298 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "nan");
1299 if (mcid >= 0) {
1300 ret = nl_socket_add_membership(state->nl_sock, mcid);
1301 if (ret)
1302 return ret;
1303 }
1304
1305 return 0;
1306 }
1307
1308 __u32 __do_listen_events(struct nl80211_state *state,
1309 const int n_waits, const __u32 *waits,
1310 const int n_prints, const __u32 *prints,
1311 struct print_event_args *args)
1312 {
1313 struct nl_cb *cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
1314 struct wait_event wait_ev;
1315
1316 if (!cb) {
1317 fprintf(stderr, "failed to allocate netlink callbacks\n");
1318 return -ENOMEM;
1319 }
1320
1321 /* no sequence checking for multicast messages */
1322 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
1323 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, valid_handler, NULL);
1324
1325 if (n_waits && waits) {
1326 wait_ev.cmds = waits;
1327 wait_ev.n_cmds = n_waits;
1328 wait_ev.prints = prints;
1329 wait_ev.n_prints = n_prints;
1330 wait_ev.pargs = args;
1331 register_handler(wait_event, &wait_ev);
1332 } else
1333 register_handler(print_event, args);
1334
1335 wait_ev.cmd = 0;
1336
1337 while (!wait_ev.cmd)
1338 nl_recvmsgs(state->nl_sock, cb);
1339
1340 nl_cb_put(cb);
1341
1342 return wait_ev.cmd;
1343 }
1344
1345 __u32 listen_events(struct nl80211_state *state,
1346 const int n_waits, const __u32 *waits)
1347 {
1348 int ret;
1349
1350 ret = __prepare_listen_events(state);
1351 if (ret)
1352 return ret;
1353
1354 return __do_listen_events(state, n_waits, waits, 0, NULL, NULL);
1355 }
1356
1357 static int print_events(struct nl80211_state *state,
1358 struct nl_msg *msg,
1359 int argc, char **argv,
1360 enum id_input id)
1361 {
1362 struct print_event_args args;
1363 int ret;
1364
1365 memset(&args, 0, sizeof(args));
1366
1367 argc--;
1368 argv++;
1369
1370 while (argc > 0) {
1371 if (strcmp(argv[0], "-f") == 0)
1372 args.frame = true;
1373 else if (strcmp(argv[0], "-t") == 0)
1374 args.time = true;
1375 else if (strcmp(argv[0], "-r") == 0)
1376 args.reltime = true;
1377 else
1378 return 1;
1379 argc--;
1380 argv++;
1381 }
1382
1383 if (args.time && args.reltime)
1384 return 1;
1385
1386 if (argc)
1387 return 1;
1388
1389 ret = __prepare_listen_events(state);
1390 if (ret)
1391 return ret;
1392
1393 return __do_listen_events(state, 0, NULL, 0, NULL, &args);
1394 }
1395 TOPLEVEL(event, "[-t|-r] [-f]", 0, 0, CIB_NONE, print_events,
1396 "Monitor events from the kernel.\n"
1397 "-t - print timestamp\n"
1398 "-r - print relative timestamp\n"
1399 "-f - print full frame for auth/assoc etc.");