]>
Commit | Line | Data |
---|---|---|
1 | #include <stdint.h> | |
2 | #include <stdbool.h> | |
3 | #include <net/if.h> | |
4 | #include <errno.h> | |
5 | #include "iw.h" | |
6 | ||
7 | static int no_seq_check(struct nl_msg *msg, void *arg) | |
8 | { | |
9 | return NL_OK; | |
10 | } | |
11 | ||
12 | struct ieee80211_beacon_channel { | |
13 | __u16 center_freq; | |
14 | bool passive_scan; | |
15 | bool no_ibss; | |
16 | }; | |
17 | ||
18 | static 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 | ||
44 | static 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 | ||
103 | static void parse_cqm_event(struct nlattr **attrs) | |
104 | { | |
105 | static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = { | |
106 | [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 }, | |
107 | [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 }, | |
108 | [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 }, | |
109 | }; | |
110 | struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1]; | |
111 | struct nlattr *cqm_attr = attrs[NL80211_ATTR_CQM]; | |
112 | ||
113 | printf("CQM event: "); | |
114 | ||
115 | if (!cqm_attr || | |
116 | nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, cqm_attr, cqm_policy)) { | |
117 | printf("missing data!\n"); | |
118 | return; | |
119 | } | |
120 | ||
121 | if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]) { | |
122 | enum nl80211_cqm_rssi_threshold_event rssi_event; | |
123 | bool found_one = false; | |
124 | ||
125 | rssi_event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]); | |
126 | ||
127 | switch (rssi_event) { | |
128 | case NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH: | |
129 | printf("RSSI went above threshold\n"); | |
130 | found_one = true; | |
131 | break; | |
132 | case NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW: | |
133 | printf("RSSI went below threshold\n"); | |
134 | found_one = true; | |
135 | break; | |
136 | case NL80211_CQM_RSSI_BEACON_LOSS_EVENT: | |
137 | printf("Beacon loss detected\n"); | |
138 | found_one = true; | |
139 | break; | |
140 | } | |
141 | ||
142 | if (!found_one) | |
143 | printf("Unknown event type: %i\n", rssi_event); | |
144 | } else if (cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT] && | |
145 | attrs[NL80211_ATTR_MAC]) { | |
146 | uint32_t frames; | |
147 | char buf[3*6]; | |
148 | ||
149 | frames = nla_get_u32(cqm[NL80211_ATTR_CQM_PKT_LOSS_EVENT]); | |
150 | mac_addr_n2a(buf, nla_data(attrs[NL80211_ATTR_MAC])); | |
151 | printf("peer %s didn't ACK %d packets\n", buf, frames); | |
152 | } else | |
153 | printf("unknown event\n"); | |
154 | } | |
155 | ||
156 | static const char * key_type_str(enum nl80211_key_type key_type) | |
157 | { | |
158 | static char buf[30]; | |
159 | switch (key_type) { | |
160 | case NL80211_KEYTYPE_GROUP: | |
161 | return "Group"; | |
162 | case NL80211_KEYTYPE_PAIRWISE: | |
163 | return "Pairwise"; | |
164 | case NL80211_KEYTYPE_PEERKEY: | |
165 | return "PeerKey"; | |
166 | default: | |
167 | snprintf(buf, sizeof(buf), "unknown(%d)", key_type); | |
168 | return buf; | |
169 | } | |
170 | } | |
171 | ||
172 | static void parse_mic_failure(struct nlattr **attrs) | |
173 | { | |
174 | printf("Michael MIC failure event:"); | |
175 | ||
176 | if (attrs[NL80211_ATTR_MAC]) { | |
177 | char addr[3 * ETH_ALEN]; | |
178 | mac_addr_n2a(addr, nla_data(attrs[NL80211_ATTR_MAC])); | |
179 | printf(" source MAC address %s", addr); | |
180 | } | |
181 | ||
182 | if (attrs[NL80211_ATTR_KEY_SEQ] && | |
183 | nla_len(attrs[NL80211_ATTR_KEY_SEQ]) == 6) { | |
184 | unsigned char *seq = nla_data(attrs[NL80211_ATTR_KEY_SEQ]); | |
185 | printf(" seq=%02x%02x%02x%02x%02x%02x", | |
186 | seq[0], seq[1], seq[2], seq[3], seq[4], seq[5]); | |
187 | } | |
188 | if (attrs[NL80211_ATTR_KEY_TYPE]) { | |
189 | enum nl80211_key_type key_type = | |
190 | nla_get_u32(attrs[NL80211_ATTR_KEY_TYPE]); | |
191 | printf(" Key Type %s", key_type_str(key_type)); | |
192 | } | |
193 | ||
194 | if (attrs[NL80211_ATTR_KEY_IDX]) { | |
195 | __u8 key_id = nla_get_u8(attrs[NL80211_ATTR_KEY_IDX]); | |
196 | printf(" Key Id %d", key_id); | |
197 | } | |
198 | ||
199 | printf("\n"); | |
200 | } | |
201 | ||
202 | static int print_event(struct nl_msg *msg, void *arg) | |
203 | { | |
204 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
205 | struct nlattr *tb[NL80211_ATTR_MAX + 1], *nst; | |
206 | struct print_event_args *args = arg; | |
207 | char ifname[100]; | |
208 | char macbuf[6*3]; | |
209 | __u8 reg_type; | |
210 | struct ieee80211_beacon_channel chan_before_beacon, chan_after_beacon; | |
211 | __u32 wiphy_idx = 0; | |
212 | int rem_nst; | |
213 | __u16 status; | |
214 | ||
215 | if (args->time || args->reltime) { | |
216 | unsigned long long usecs, previous; | |
217 | ||
218 | previous = 1000000ULL * args->ts.tv_sec + args->ts.tv_usec; | |
219 | gettimeofday(&args->ts, NULL); | |
220 | usecs = 1000000ULL * args->ts.tv_sec + args->ts.tv_usec; | |
221 | if (args->reltime) { | |
222 | if (!args->have_ts) { | |
223 | usecs = 0; | |
224 | args->have_ts = true; | |
225 | } else | |
226 | usecs -= previous; | |
227 | } | |
228 | printf("%llu.%06llu: ", usecs/1000000, usecs % 1000000); | |
229 | } | |
230 | ||
231 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
232 | genlmsg_attrlen(gnlh, 0), NULL); | |
233 | ||
234 | if (tb[NL80211_ATTR_IFINDEX] && tb[NL80211_ATTR_WIPHY]) { | |
235 | if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname); | |
236 | printf("%s (phy #%d): ", ifname, nla_get_u32(tb[NL80211_ATTR_WIPHY])); | |
237 | } else if (tb[NL80211_ATTR_WDEV] && tb[NL80211_ATTR_WIPHY]) { | |
238 | printf("wdev 0x%llx (phy #%d): ", | |
239 | (unsigned long long)nla_get_u64(tb[NL80211_ATTR_WDEV]), | |
240 | nla_get_u32(tb[NL80211_ATTR_WIPHY])); | |
241 | } else if (tb[NL80211_ATTR_IFINDEX]) { | |
242 | if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname); | |
243 | printf("%s: ", ifname); | |
244 | } else if (tb[NL80211_ATTR_WDEV]) { | |
245 | printf("wdev 0x%llx: ", (unsigned long long)nla_get_u64(tb[NL80211_ATTR_WDEV])); | |
246 | } else if (tb[NL80211_ATTR_WIPHY]) { | |
247 | printf("phy #%d: ", nla_get_u32(tb[NL80211_ATTR_WIPHY])); | |
248 | } | |
249 | ||
250 | switch (gnlh->cmd) { | |
251 | case NL80211_CMD_NEW_WIPHY: | |
252 | printf("renamed to %s\n", nla_get_string(tb[NL80211_ATTR_WIPHY_NAME])); | |
253 | break; | |
254 | case NL80211_CMD_TRIGGER_SCAN: | |
255 | printf("scan started\n"); | |
256 | break; | |
257 | case NL80211_CMD_NEW_SCAN_RESULTS: | |
258 | printf("scan finished:"); | |
259 | case NL80211_CMD_SCAN_ABORTED: | |
260 | if (gnlh->cmd == NL80211_CMD_SCAN_ABORTED) | |
261 | printf("scan aborted:"); | |
262 | if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) { | |
263 | nla_for_each_nested(nst, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem_nst) | |
264 | printf(" %d", nla_get_u32(nst)); | |
265 | printf(","); | |
266 | } | |
267 | if (tb[NL80211_ATTR_SCAN_SSIDS]) { | |
268 | nla_for_each_nested(nst, tb[NL80211_ATTR_SCAN_SSIDS], rem_nst) { | |
269 | printf(" \""); | |
270 | print_ssid_escaped(nla_len(nst), nla_data(nst)); | |
271 | printf("\""); | |
272 | } | |
273 | } | |
274 | printf("\n"); | |
275 | break; | |
276 | case NL80211_CMD_REG_CHANGE: | |
277 | printf("regulatory domain change: "); | |
278 | ||
279 | reg_type = nla_get_u8(tb[NL80211_ATTR_REG_TYPE]); | |
280 | ||
281 | switch (reg_type) { | |
282 | case NL80211_REGDOM_TYPE_COUNTRY: | |
283 | printf("set to %s by %s request", | |
284 | nla_get_string(tb[NL80211_ATTR_REG_ALPHA2]), | |
285 | reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]))); | |
286 | if (tb[NL80211_ATTR_WIPHY]) | |
287 | printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY])); | |
288 | break; | |
289 | case NL80211_REGDOM_TYPE_WORLD: | |
290 | printf("set to world roaming by %s request", | |
291 | reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]))); | |
292 | break; | |
293 | case NL80211_REGDOM_TYPE_CUSTOM_WORLD: | |
294 | printf("custom world roaming rules in place on phy%d by %s request", | |
295 | nla_get_u32(tb[NL80211_ATTR_WIPHY]), | |
296 | reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR]))); | |
297 | break; | |
298 | case NL80211_REGDOM_TYPE_INTERSECTION: | |
299 | printf("intersection used due to a request made by %s", | |
300 | reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR]))); | |
301 | if (tb[NL80211_ATTR_WIPHY]) | |
302 | printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY])); | |
303 | break; | |
304 | default: | |
305 | printf("unknown source (upgrade this utility)"); | |
306 | break; | |
307 | } | |
308 | ||
309 | printf("\n"); | |
310 | break; | |
311 | case NL80211_CMD_REG_BEACON_HINT: | |
312 | ||
313 | wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]); | |
314 | ||
315 | memset(&chan_before_beacon, 0, sizeof(chan_before_beacon)); | |
316 | memset(&chan_after_beacon, 0, sizeof(chan_after_beacon)); | |
317 | ||
318 | if (parse_beacon_hint_chan(tb[NL80211_ATTR_FREQ_BEFORE], | |
319 | &chan_before_beacon)) | |
320 | break; | |
321 | if (parse_beacon_hint_chan(tb[NL80211_ATTR_FREQ_AFTER], | |
322 | &chan_after_beacon)) | |
323 | break; | |
324 | ||
325 | if (chan_before_beacon.center_freq != chan_after_beacon.center_freq) | |
326 | break; | |
327 | ||
328 | /* A beacon hint is sent _only_ if something _did_ change */ | |
329 | printf("beacon hint:\n"); | |
330 | ||
331 | printf("phy%d %d MHz [%d]:\n", | |
332 | wiphy_idx, | |
333 | chan_before_beacon.center_freq, | |
334 | ieee80211_frequency_to_channel(chan_before_beacon.center_freq)); | |
335 | ||
336 | if (chan_before_beacon.passive_scan && !chan_after_beacon.passive_scan) | |
337 | printf("\to active scanning enabled\n"); | |
338 | if (chan_before_beacon.no_ibss && !chan_after_beacon.no_ibss) | |
339 | printf("\to beaconing enabled\n"); | |
340 | ||
341 | break; | |
342 | case NL80211_CMD_NEW_STATION: | |
343 | mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC])); | |
344 | printf("new station %s\n", macbuf); | |
345 | break; | |
346 | case NL80211_CMD_DEL_STATION: | |
347 | mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC])); | |
348 | printf("del station %s\n", macbuf); | |
349 | break; | |
350 | case NL80211_CMD_JOIN_IBSS: | |
351 | mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC])); | |
352 | printf("IBSS %s joined\n", macbuf); | |
353 | break; | |
354 | case NL80211_CMD_AUTHENTICATE: | |
355 | printf("auth"); | |
356 | if (tb[NL80211_ATTR_FRAME]) | |
357 | print_frame(args, tb[NL80211_ATTR_FRAME]); | |
358 | else if (tb[NL80211_ATTR_TIMED_OUT]) | |
359 | printf(": timed out"); | |
360 | else | |
361 | printf(": unknown event"); | |
362 | printf("\n"); | |
363 | break; | |
364 | case NL80211_CMD_ASSOCIATE: | |
365 | printf("assoc"); | |
366 | if (tb[NL80211_ATTR_FRAME]) | |
367 | print_frame(args, tb[NL80211_ATTR_FRAME]); | |
368 | else if (tb[NL80211_ATTR_TIMED_OUT]) | |
369 | printf(": timed out"); | |
370 | else | |
371 | printf(": unknown event"); | |
372 | printf("\n"); | |
373 | break; | |
374 | case NL80211_CMD_DEAUTHENTICATE: | |
375 | printf("deauth"); | |
376 | print_frame(args, tb[NL80211_ATTR_FRAME]); | |
377 | printf("\n"); | |
378 | break; | |
379 | case NL80211_CMD_DISASSOCIATE: | |
380 | printf("disassoc"); | |
381 | print_frame(args, tb[NL80211_ATTR_FRAME]); | |
382 | printf("\n"); | |
383 | break; | |
384 | case NL80211_CMD_UNPROT_DEAUTHENTICATE: | |
385 | printf("unprotected deauth"); | |
386 | print_frame(args, tb[NL80211_ATTR_FRAME]); | |
387 | printf("\n"); | |
388 | break; | |
389 | case NL80211_CMD_UNPROT_DISASSOCIATE: | |
390 | printf("unprotected disassoc"); | |
391 | print_frame(args, tb[NL80211_ATTR_FRAME]); | |
392 | printf("\n"); | |
393 | break; | |
394 | case NL80211_CMD_CONNECT: | |
395 | status = 0; | |
396 | if (!tb[NL80211_ATTR_STATUS_CODE]) | |
397 | printf("unknown connect status"); | |
398 | else if (nla_get_u16(tb[NL80211_ATTR_STATUS_CODE]) == 0) | |
399 | printf("connected"); | |
400 | else { | |
401 | status = nla_get_u16(tb[NL80211_ATTR_STATUS_CODE]); | |
402 | printf("failed to connect"); | |
403 | } | |
404 | if (tb[NL80211_ATTR_MAC]) { | |
405 | mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC])); | |
406 | printf(" to %s", macbuf); | |
407 | } | |
408 | if (status) | |
409 | printf(", status: %d: %s", status, get_status_str(status)); | |
410 | printf("\n"); | |
411 | break; | |
412 | case NL80211_CMD_ROAM: | |
413 | printf("roamed"); | |
414 | if (tb[NL80211_ATTR_MAC]) { | |
415 | mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC])); | |
416 | printf(" to %s", macbuf); | |
417 | } | |
418 | printf("\n"); | |
419 | break; | |
420 | case NL80211_CMD_DISCONNECT: | |
421 | printf("disconnected"); | |
422 | if (tb[NL80211_ATTR_DISCONNECTED_BY_AP]) | |
423 | printf(" (by AP)"); | |
424 | else | |
425 | printf(" (local request)"); | |
426 | if (tb[NL80211_ATTR_REASON_CODE]) | |
427 | printf(" reason: %d: %s", nla_get_u16(tb[NL80211_ATTR_REASON_CODE]), | |
428 | get_reason_str(nla_get_u16(tb[NL80211_ATTR_REASON_CODE]))); | |
429 | printf("\n"); | |
430 | break; | |
431 | case NL80211_CMD_REMAIN_ON_CHANNEL: | |
432 | printf("remain on freq %d (%dms, cookie %llx)\n", | |
433 | nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]), | |
434 | nla_get_u32(tb[NL80211_ATTR_DURATION]), | |
435 | (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE])); | |
436 | break; | |
437 | case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL: | |
438 | printf("done with remain on freq %d (cookie %llx)\n", | |
439 | nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]), | |
440 | (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE])); | |
441 | break; | |
442 | case NL80211_CMD_NOTIFY_CQM: | |
443 | parse_cqm_event(tb); | |
444 | break; | |
445 | case NL80211_CMD_MICHAEL_MIC_FAILURE: | |
446 | parse_mic_failure(tb); | |
447 | break; | |
448 | case NL80211_CMD_FRAME_TX_STATUS: | |
449 | printf("mgmt TX status (cookie %llx): %s\n", | |
450 | (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]), | |
451 | tb[NL80211_ATTR_ACK] ? "acked" : "no ack"); | |
452 | break; | |
453 | case NL80211_CMD_PMKSA_CANDIDATE: | |
454 | printf("PMKSA candidate found\n"); | |
455 | break; | |
456 | default: | |
457 | printf("unknown event %d\n", gnlh->cmd); | |
458 | break; | |
459 | } | |
460 | ||
461 | fflush(stdout); | |
462 | return NL_SKIP; | |
463 | } | |
464 | ||
465 | struct wait_event { | |
466 | int n_cmds; | |
467 | const __u32 *cmds; | |
468 | __u32 cmd; | |
469 | struct print_event_args *pargs; | |
470 | }; | |
471 | ||
472 | static int wait_event(struct nl_msg *msg, void *arg) | |
473 | { | |
474 | struct wait_event *wait = arg; | |
475 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
476 | int i; | |
477 | ||
478 | for (i = 0; i < wait->n_cmds; i++) { | |
479 | if (gnlh->cmd == wait->cmds[i]) { | |
480 | wait->cmd = gnlh->cmd; | |
481 | if (wait->pargs) | |
482 | print_event(msg, wait->pargs); | |
483 | } | |
484 | } | |
485 | ||
486 | return NL_SKIP; | |
487 | } | |
488 | ||
489 | int __prepare_listen_events(struct nl80211_state *state) | |
490 | { | |
491 | int mcid, ret; | |
492 | ||
493 | /* Configuration multicast group */ | |
494 | mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "config"); | |
495 | if (mcid < 0) | |
496 | return mcid; | |
497 | ||
498 | ret = nl_socket_add_membership(state->nl_sock, mcid); | |
499 | if (ret) | |
500 | return ret; | |
501 | ||
502 | /* Scan multicast group */ | |
503 | mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "scan"); | |
504 | if (mcid >= 0) { | |
505 | ret = nl_socket_add_membership(state->nl_sock, mcid); | |
506 | if (ret) | |
507 | return ret; | |
508 | } | |
509 | ||
510 | /* Regulatory multicast group */ | |
511 | mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "regulatory"); | |
512 | if (mcid >= 0) { | |
513 | ret = nl_socket_add_membership(state->nl_sock, mcid); | |
514 | if (ret) | |
515 | return ret; | |
516 | } | |
517 | ||
518 | /* MLME multicast group */ | |
519 | mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "mlme"); | |
520 | if (mcid >= 0) { | |
521 | ret = nl_socket_add_membership(state->nl_sock, mcid); | |
522 | if (ret) | |
523 | return ret; | |
524 | } | |
525 | ||
526 | return 0; | |
527 | } | |
528 | ||
529 | __u32 __do_listen_events(struct nl80211_state *state, | |
530 | const int n_waits, const __u32 *waits, | |
531 | struct print_event_args *args) | |
532 | { | |
533 | struct nl_cb *cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT); | |
534 | struct wait_event wait_ev; | |
535 | ||
536 | if (!cb) { | |
537 | fprintf(stderr, "failed to allocate netlink callbacks\n"); | |
538 | return -ENOMEM; | |
539 | } | |
540 | ||
541 | /* no sequence checking for multicast messages */ | |
542 | nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL); | |
543 | ||
544 | if (n_waits && waits) { | |
545 | wait_ev.cmds = waits; | |
546 | wait_ev.n_cmds = n_waits; | |
547 | wait_ev.pargs = args; | |
548 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, wait_event, &wait_ev); | |
549 | } else | |
550 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_event, args); | |
551 | ||
552 | wait_ev.cmd = 0; | |
553 | ||
554 | while (!wait_ev.cmd) | |
555 | nl_recvmsgs(state->nl_sock, cb); | |
556 | ||
557 | nl_cb_put(cb); | |
558 | ||
559 | return wait_ev.cmd; | |
560 | } | |
561 | ||
562 | __u32 listen_events(struct nl80211_state *state, | |
563 | const int n_waits, const __u32 *waits) | |
564 | { | |
565 | int ret; | |
566 | ||
567 | ret = __prepare_listen_events(state); | |
568 | if (ret) | |
569 | return ret; | |
570 | ||
571 | return __do_listen_events(state, n_waits, waits, NULL); | |
572 | } | |
573 | ||
574 | static int print_events(struct nl80211_state *state, | |
575 | struct nl_cb *cb, | |
576 | struct nl_msg *msg, | |
577 | int argc, char **argv, | |
578 | enum id_input id) | |
579 | { | |
580 | struct print_event_args args; | |
581 | int ret; | |
582 | ||
583 | memset(&args, 0, sizeof(args)); | |
584 | ||
585 | argc--; | |
586 | argv++; | |
587 | ||
588 | while (argc > 0) { | |
589 | if (strcmp(argv[0], "-f") == 0) | |
590 | args.frame = true; | |
591 | else if (strcmp(argv[0], "-t") == 0) | |
592 | args.time = true; | |
593 | else if (strcmp(argv[0], "-r") == 0) | |
594 | args.reltime = true; | |
595 | else | |
596 | return 1; | |
597 | argc--; | |
598 | argv++; | |
599 | } | |
600 | ||
601 | if (args.time && args.reltime) | |
602 | return 1; | |
603 | ||
604 | if (argc) | |
605 | return 1; | |
606 | ||
607 | ret = __prepare_listen_events(state); | |
608 | if (ret) | |
609 | return ret; | |
610 | ||
611 | return __do_listen_events(state, 0, NULL, &args); | |
612 | } | |
613 | TOPLEVEL(event, "[-t] [-r] [-f]", 0, 0, CIB_NONE, print_events, | |
614 | "Monitor events from the kernel.\n" | |
615 | "-t - print timestamp\n" | |
616 | "-r - print relative timstamp\n" | |
617 | "-f - print full frame for auth/assoc etc."); |