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