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