]> git.ipfire.org Git - thirdparty/iw.git/blame - event.c
print unknown commands in supported list with number
[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;
14 bool passive_scan;
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 },
24 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
25 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
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
36 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
37 chan->passive_scan = true;
38 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
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;
83 break;
84 case 0xa0: /* disassoc */
85 case 0xc0: /* deauth */
86 /* reason */
87 tmp = (frame[25] << 8) + frame[24];
88 printf(" reason %d: %s", tmp, get_reason_str(tmp));
89 break;
90 }
91
92 if (!args->frame)
93 return;
94
95 printf(" [frame:");
96
97 print_frame:
98 for (i = 0; i < len; i++)
99 printf(" %.02x", frame[i]);
100 printf("]");
101}
102
103static int print_event(struct nl_msg *msg, void *arg)
104{
0168589d
LR
105#define PARSE_BEACON_CHAN(_attr, _chan) do { \
106 r = parse_beacon_hint_chan(tb[_attr], \
107 &_chan); \
108 if (r) \
109 return NL_SKIP; \
110} while (0)
957a0f07 111 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
748f8489 112 struct nlattr *tb[NL80211_ATTR_MAX + 1], *nst;
957a0f07
JB
113 struct print_event_args *args = arg;
114 char ifname[100];
115 char macbuf[6*3];
116 __u8 reg_type;
0168589d
LR
117 struct ieee80211_beacon_channel chan_before_beacon, chan_after_beacon;
118 __u32 wiphy_idx = 0;
119 int r;
748f8489 120 int rem_nst;
f1a666a6 121 __u16 status;
957a0f07
JB
122
123 if (args->time) {
124 struct timeval tv;
125 gettimeofday(&tv, NULL);
126 printf("%ld.%06u: ", (long) tv.tv_sec, (unsigned int) tv.tv_usec);
127 }
128
129 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
130 genlmsg_attrlen(gnlh, 0), NULL);
131
132 if (tb[NL80211_ATTR_IFINDEX] && tb[NL80211_ATTR_WIPHY]) {
133 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname);
134 printf("%s (phy #%d): ", ifname, nla_get_u32(tb[NL80211_ATTR_WIPHY]));
135 } else if (tb[NL80211_ATTR_IFINDEX]) {
136 if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname);
137 printf("%s: ", ifname);
138 } else if (tb[NL80211_ATTR_WIPHY]) {
139 printf("phy #%d: ", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
140 }
141
142 switch (gnlh->cmd) {
143 case NL80211_CMD_NEW_WIPHY:
144 printf("renamed to %s\n", nla_get_string(tb[NL80211_ATTR_WIPHY_NAME]));
145 break;
e67be815
JB
146 case NL80211_CMD_TRIGGER_SCAN:
147 printf("scan started\n");
148 break;
957a0f07 149 case NL80211_CMD_NEW_SCAN_RESULTS:
748f8489 150 printf("scan finished:");
957a0f07 151 case NL80211_CMD_SCAN_ABORTED:
748f8489
JB
152 if (gnlh->cmd == NL80211_CMD_SCAN_ABORTED)
153 printf("scan aborted:");
154 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
155 nla_for_each_nested(nst, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem_nst)
156 printf(" %d", nla_get_u32(nst));
157 printf(",");
158 }
159 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
160 nla_for_each_nested(nst, tb[NL80211_ATTR_SCAN_SSIDS], rem_nst) {
161 printf(" \"");
162 print_ssid_escaped(nla_len(nst), nla_data(nst));
163 printf("\"");
164 }
165 }
166 printf("\n");
957a0f07
JB
167 break;
168 case NL80211_CMD_REG_CHANGE:
169 printf("regulatory domain change: ");
170
171 reg_type = nla_get_u8(tb[NL80211_ATTR_REG_TYPE]);
172
173 switch (reg_type) {
174 case NL80211_REGDOM_TYPE_COUNTRY:
175 printf("set to %s by %s request",
176 nla_get_string(tb[NL80211_ATTR_REG_ALPHA2]),
177 reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])));
178 if (tb[NL80211_ATTR_WIPHY])
179 printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
180 break;
181 case NL80211_REGDOM_TYPE_WORLD:
182 printf("set to world roaming by %s request",
183 reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR])));
184 break;
185 case NL80211_REGDOM_TYPE_CUSTOM_WORLD:
186 printf("custom world roaming rules in place on phy%d by %s request",
187 nla_get_u32(tb[NL80211_ATTR_WIPHY]),
188 reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR])));
189 break;
190 case NL80211_REGDOM_TYPE_INTERSECTION:
191 printf("intersection used due to a request made by %s",
192 reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR])));
193 if (tb[NL80211_ATTR_WIPHY])
194 printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY]));
195 break;
196 default:
197 printf("unknown source (upgrade this utility)");
198 break;
199 }
200
201 printf("\n");
0168589d
LR
202 break;
203 case NL80211_CMD_REG_BEACON_HINT:
204
205 wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
206
207 memset(&chan_before_beacon, 0, sizeof(chan_before_beacon));
208 memset(&chan_after_beacon, 0, sizeof(chan_after_beacon));
209
210 PARSE_BEACON_CHAN(NL80211_ATTR_FREQ_BEFORE, chan_before_beacon);
211 PARSE_BEACON_CHAN(NL80211_ATTR_FREQ_AFTER, chan_after_beacon);
212
213 if (chan_before_beacon.center_freq != chan_after_beacon.center_freq)
214 break;
215
216 /* A beacon hint is sent _only_ if something _did_ change */
217 printf("beacon hint:\n");
218
219 printf("phy%d %d MHz [%d]:\n",
220 wiphy_idx,
221 chan_before_beacon.center_freq,
222 ieee80211_frequency_to_channel(chan_before_beacon.center_freq));
223
224 if (chan_before_beacon.passive_scan && !chan_after_beacon.passive_scan)
225 printf("\to active scanning enabled\n");
226 if (chan_before_beacon.no_ibss && !chan_after_beacon.no_ibss)
227 printf("\to beaconing enabled\n");
228
cec6d6a7
JB
229 break;
230 case NL80211_CMD_NEW_STATION:
231 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
232 printf("new station %s\n", macbuf);
957a0f07
JB
233 break;
234 case NL80211_CMD_JOIN_IBSS:
235 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
236 printf("IBSS %s joined\n", macbuf);
237 break;
238 case NL80211_CMD_AUTHENTICATE:
239 printf("auth");
aea5dbd2
JB
240 if (tb[NL80211_ATTR_FRAME])
241 print_frame(args, tb[NL80211_ATTR_FRAME]);
242 else if (tb[NL80211_ATTR_TIMED_OUT])
243 printf(": timed out");
244 else
245 printf(": unknown event");
957a0f07
JB
246 printf("\n");
247 break;
248 case NL80211_CMD_ASSOCIATE:
249 printf("assoc");
aea5dbd2
JB
250 if (tb[NL80211_ATTR_FRAME])
251 print_frame(args, tb[NL80211_ATTR_FRAME]);
252 else if (tb[NL80211_ATTR_TIMED_OUT])
253 printf(": timed out");
254 else
255 printf(": unknown event");
957a0f07
JB
256 printf("\n");
257 break;
258 case NL80211_CMD_DEAUTHENTICATE:
259 printf("deauth");
260 print_frame(args, tb[NL80211_ATTR_FRAME]);
261 printf("\n");
262 break;
263 case NL80211_CMD_DISASSOCIATE:
264 printf("disassoc");
265 print_frame(args, tb[NL80211_ATTR_FRAME]);
266 printf("\n");
267 break;
f1a666a6
JB
268 case NL80211_CMD_CONNECT:
269 status = 0;
270 if (!tb[NL80211_ATTR_STATUS_CODE])
271 printf("unknown connect status");
272 else if (nla_get_u16(tb[NL80211_ATTR_STATUS_CODE]) == 0)
273 printf("connected");
274 else {
275 status = nla_get_u16(tb[NL80211_ATTR_STATUS_CODE]);
276 printf("failed to connect");
277 }
278 if (tb[NL80211_ATTR_MAC]) {
279 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
280 printf(" to %s", macbuf);
281 }
282 if (status)
283 printf(", status: %d: %s", status, get_status_str(status));
284 printf("\n");
285 break;
286 case NL80211_CMD_ROAM:
287 printf("roamed");
288 if (tb[NL80211_ATTR_MAC]) {
289 mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC]));
290 printf(" to %s", macbuf);
291 }
292 printf("\n");
293 break;
294 case NL80211_CMD_DISCONNECT:
295 printf("disconnected");
296 if (tb[NL80211_ATTR_DISCONNECTED_BY_AP])
297 printf(" (by AP)");
298 else
299 printf(" (local request)");
300 if (tb[NL80211_ATTR_REASON_CODE])
301 printf(" reason: %d: %s", nla_get_u16(tb[NL80211_ATTR_REASON_CODE]),
302 get_reason_str(nla_get_u16(tb[NL80211_ATTR_REASON_CODE])));
303 printf("\n");
304 break;
957a0f07
JB
305 default:
306 printf("unknown event %d\n", gnlh->cmd);
307 break;
308 }
309
310 return NL_SKIP;
0168589d 311#undef PARSE_BEACON_CHAN
957a0f07
JB
312}
313
314struct wait_event {
315 int n_cmds;
316 const __u32 *cmds;
317 __u32 cmd;
7fabd346 318 struct print_event_args *pargs;
957a0f07
JB
319};
320
321static int wait_event(struct nl_msg *msg, void *arg)
322{
323 struct wait_event *wait = arg;
324 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
325 int i;
326
327 for (i = 0; i < wait->n_cmds; i++) {
328 if (gnlh->cmd == wait->cmds[i]) {
329 wait->cmd = gnlh->cmd;
7fabd346
JB
330 if (wait->pargs)
331 print_event(msg, wait->pargs);
957a0f07
JB
332 }
333 }
334
335 return NL_SKIP;
336}
337
7fabd346
JB
338__u32 __listen_events(struct nl80211_state *state,
339 const int n_waits, const __u32 *waits,
340 struct print_event_args *args)
957a0f07
JB
341{
342 int mcid, ret;
343 struct nl_cb *cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT);
344 struct wait_event wait_ev;
345
346 if (!cb) {
347 fprintf(stderr, "failed to allocate netlink callbacks\n");
348 return -ENOMEM;
349 }
350
351 /* Configuration multicast group */
352 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "config");
353 if (mcid < 0)
354 return mcid;
355
356 ret = nl_socket_add_membership(state->nl_sock, mcid);
357 if (ret)
358 return ret;
359
360 /* Scan multicast group */
361 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "scan");
362 if (mcid >= 0) {
363 ret = nl_socket_add_membership(state->nl_sock, mcid);
364 if (ret)
365 return ret;
366 }
367
368 /* Regulatory multicast group */
369 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "regulatory");
370 if (mcid >= 0) {
371 ret = nl_socket_add_membership(state->nl_sock, mcid);
372 if (ret)
373 return ret;
374 }
375
376 /* MLME multicast group */
377 mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "mlme");
378 if (mcid >= 0) {
379 ret = nl_socket_add_membership(state->nl_sock, mcid);
380 if (ret)
381 return ret;
382 }
383
384 /* no sequence checking for multicast messages */
385 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
386
387 if (n_waits && waits) {
388 wait_ev.cmds = waits;
389 wait_ev.n_cmds = n_waits;
7fabd346 390 wait_ev.pargs = args;
957a0f07 391 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, wait_event, &wait_ev);
7fabd346 392 } else
957a0f07 393 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_event, args);
957a0f07
JB
394
395 wait_ev.cmd = 0;
396
397 while (!wait_ev.cmd)
398 nl_recvmsgs(state->nl_sock, cb);
399
400 nl_cb_put(cb);
401
402 return wait_ev.cmd;
403}
404
405__u32 listen_events(struct nl80211_state *state,
406 const int n_waits, const __u32 *waits)
407{
408 return __listen_events(state, n_waits, waits, NULL);
409}
410
411static int print_events(struct nl80211_state *state,
412 struct nl_cb *cb,
413 struct nl_msg *msg,
414 int argc, char **argv)
415{
416 struct print_event_args args;
417
418 memset(&args, 0, sizeof(args));
419
420 argc--;
421 argv++;
422
423 while (argc > 0) {
424 if (strcmp(argv[0], "-f") == 0)
425 args.frame = true;
426 else if (strcmp(argv[0], "-t") == 0)
427 args.time = true;
428 else
429 return 1;
430 argc--;
431 argv++;
432 }
433
434 if (argc)
435 return 1;
436
437 return __listen_events(state, 0, NULL, &args);
438}
01ae06f9
JB
439TOPLEVEL(event, "[-t] [-f]", 0, 0, CIB_NONE, print_events,
440 "Monitor events from the kernel.\n"
441 "-t - print timestamp\n"
442 "-f - print full frame for auth/assoc etc.");