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