]>
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; | |
f0c48e7b | 14 | bool no_ir; |
0168589d LR |
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 }, | |
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 |
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 | 112 | |
e5497f12 | 113 | printf("CQM event: "); |
7988b229 | 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; | |
e5497f12 BG |
123 | bool found_one = false; |
124 | ||
7988b229 | 125 | rssi_event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]); |
e5497f12 BG |
126 | |
127 | switch (rssi_event) { | |
128 | case NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH: | |
f19444fe | 129 | printf("RSSI went above threshold\n"); |
e5497f12 BG |
130 | found_one = true; |
131 | break; | |
132 | case NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW: | |
f19444fe | 133 | printf("RSSI went below threshold\n"); |
e5497f12 BG |
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); | |
f19444fe JB |
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"); | |
7988b229 JO |
154 | } |
155 | ||
8612433d JM |
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 | } | |
7988b229 | 201 | |
819b78cc JB |
202 | static void parse_wowlan_wake_event(struct nlattr **attrs) |
203 | { | |
204 | struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG]; | |
205 | ||
206 | printf("WoWLAN wakeup\n"); | |
207 | if (!attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) { | |
208 | printf("\twakeup not due to WoWLAN\n"); | |
209 | return; | |
210 | } | |
211 | ||
212 | nla_parse(tb, MAX_NL80211_WOWLAN_TRIG, | |
213 | nla_data(attrs[NL80211_ATTR_WOWLAN_TRIGGERS]), | |
214 | nla_len(attrs[NL80211_ATTR_WOWLAN_TRIGGERS]), NULL); | |
215 | ||
216 | if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) | |
217 | printf("\t* was disconnected\n"); | |
218 | if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) | |
219 | printf("\t* magic packet received\n"); | |
220 | if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) | |
221 | printf("\t* pattern index: %u\n", | |
222 | nla_get_u32(tb[NL80211_WOWLAN_TRIG_PKT_PATTERN])); | |
223 | if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) | |
224 | printf("\t* GTK rekey failure\n"); | |
225 | if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) | |
226 | printf("\t* EAP identity request\n"); | |
227 | if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) | |
228 | printf("\t* 4-way handshake\n"); | |
229 | if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) | |
230 | printf("\t* RF-kill released\n"); | |
231 | if (tb[NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211]) { | |
232 | uint8_t *d = nla_data(tb[NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211]); | |
233 | int l = nla_len(tb[NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211]); | |
234 | int i; | |
235 | printf("\t* packet (might be truncated): "); | |
236 | for (i = 0; i < l; i++) { | |
237 | if (i > 0) | |
238 | printf(":"); | |
239 | printf("%.2x", d[i]); | |
240 | } | |
241 | printf("\n"); | |
242 | } | |
243 | if (tb[NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023]) { | |
244 | uint8_t *d = nla_data(tb[NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023]); | |
245 | int l = nla_len(tb[NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023]); | |
246 | int i; | |
247 | printf("\t* packet (might be truncated): "); | |
248 | for (i = 0; i < l; i++) { | |
249 | if (i > 0) | |
250 | printf(":"); | |
251 | printf("%.2x", d[i]); | |
252 | } | |
253 | printf("\n"); | |
254 | } | |
255 | if (tb[NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH]) | |
256 | printf("\t* TCP connection wakeup received\n"); | |
257 | if (tb[NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST]) | |
258 | printf("\t* TCP connection lost\n"); | |
259 | if (tb[NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS]) | |
260 | printf("\t* TCP connection ran out of tokens\n"); | |
261 | } | |
262 | ||
957a0f07 JB |
263 | static int print_event(struct nl_msg *msg, void *arg) |
264 | { | |
265 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
748f8489 | 266 | struct nlattr *tb[NL80211_ATTR_MAX + 1], *nst; |
957a0f07 JB |
267 | struct print_event_args *args = arg; |
268 | char ifname[100]; | |
269 | char macbuf[6*3]; | |
270 | __u8 reg_type; | |
0168589d LR |
271 | struct ieee80211_beacon_channel chan_before_beacon, chan_after_beacon; |
272 | __u32 wiphy_idx = 0; | |
748f8489 | 273 | int rem_nst; |
f1a666a6 | 274 | __u16 status; |
957a0f07 | 275 | |
981b21ad JB |
276 | if (args->time || args->reltime) { |
277 | unsigned long long usecs, previous; | |
278 | ||
279 | previous = 1000000ULL * args->ts.tv_sec + args->ts.tv_usec; | |
280 | gettimeofday(&args->ts, NULL); | |
281 | usecs = 1000000ULL * args->ts.tv_sec + args->ts.tv_usec; | |
282 | if (args->reltime) { | |
283 | if (!args->have_ts) { | |
284 | usecs = 0; | |
285 | args->have_ts = true; | |
286 | } else | |
287 | usecs -= previous; | |
288 | } | |
289 | printf("%llu.%06llu: ", usecs/1000000, usecs % 1000000); | |
957a0f07 JB |
290 | } |
291 | ||
292 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
293 | genlmsg_attrlen(gnlh, 0), NULL); | |
294 | ||
295 | if (tb[NL80211_ATTR_IFINDEX] && tb[NL80211_ATTR_WIPHY]) { | |
296 | if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname); | |
297 | printf("%s (phy #%d): ", ifname, nla_get_u32(tb[NL80211_ATTR_WIPHY])); | |
d8880179 JB |
298 | } else if (tb[NL80211_ATTR_WDEV] && tb[NL80211_ATTR_WIPHY]) { |
299 | printf("wdev 0x%llx (phy #%d): ", | |
300 | (unsigned long long)nla_get_u64(tb[NL80211_ATTR_WDEV]), | |
301 | nla_get_u32(tb[NL80211_ATTR_WIPHY])); | |
957a0f07 JB |
302 | } else if (tb[NL80211_ATTR_IFINDEX]) { |
303 | if_indextoname(nla_get_u32(tb[NL80211_ATTR_IFINDEX]), ifname); | |
304 | printf("%s: ", ifname); | |
d8880179 JB |
305 | } else if (tb[NL80211_ATTR_WDEV]) { |
306 | printf("wdev 0x%llx: ", (unsigned long long)nla_get_u64(tb[NL80211_ATTR_WDEV])); | |
957a0f07 JB |
307 | } else if (tb[NL80211_ATTR_WIPHY]) { |
308 | printf("phy #%d: ", nla_get_u32(tb[NL80211_ATTR_WIPHY])); | |
309 | } | |
310 | ||
311 | switch (gnlh->cmd) { | |
312 | case NL80211_CMD_NEW_WIPHY: | |
313 | printf("renamed to %s\n", nla_get_string(tb[NL80211_ATTR_WIPHY_NAME])); | |
314 | break; | |
e67be815 JB |
315 | case NL80211_CMD_TRIGGER_SCAN: |
316 | printf("scan started\n"); | |
317 | break; | |
957a0f07 | 318 | case NL80211_CMD_NEW_SCAN_RESULTS: |
748f8489 | 319 | printf("scan finished:"); |
957a0f07 | 320 | case NL80211_CMD_SCAN_ABORTED: |
748f8489 JB |
321 | if (gnlh->cmd == NL80211_CMD_SCAN_ABORTED) |
322 | printf("scan aborted:"); | |
323 | if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) { | |
324 | nla_for_each_nested(nst, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem_nst) | |
325 | printf(" %d", nla_get_u32(nst)); | |
326 | printf(","); | |
327 | } | |
328 | if (tb[NL80211_ATTR_SCAN_SSIDS]) { | |
329 | nla_for_each_nested(nst, tb[NL80211_ATTR_SCAN_SSIDS], rem_nst) { | |
330 | printf(" \""); | |
331 | print_ssid_escaped(nla_len(nst), nla_data(nst)); | |
332 | printf("\""); | |
333 | } | |
334 | } | |
335 | printf("\n"); | |
957a0f07 JB |
336 | break; |
337 | case NL80211_CMD_REG_CHANGE: | |
338 | printf("regulatory domain change: "); | |
339 | ||
340 | reg_type = nla_get_u8(tb[NL80211_ATTR_REG_TYPE]); | |
341 | ||
342 | switch (reg_type) { | |
343 | case NL80211_REGDOM_TYPE_COUNTRY: | |
344 | printf("set to %s by %s request", | |
345 | nla_get_string(tb[NL80211_ATTR_REG_ALPHA2]), | |
346 | reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]))); | |
347 | if (tb[NL80211_ATTR_WIPHY]) | |
348 | printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY])); | |
349 | break; | |
350 | case NL80211_REGDOM_TYPE_WORLD: | |
351 | printf("set to world roaming by %s request", | |
352 | reg_initiator_to_string(nla_get_u8(tb[NL80211_ATTR_REG_INITIATOR]))); | |
353 | break; | |
354 | case NL80211_REGDOM_TYPE_CUSTOM_WORLD: | |
355 | printf("custom world roaming rules in place on phy%d by %s request", | |
356 | nla_get_u32(tb[NL80211_ATTR_WIPHY]), | |
357 | reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR]))); | |
358 | break; | |
359 | case NL80211_REGDOM_TYPE_INTERSECTION: | |
360 | printf("intersection used due to a request made by %s", | |
361 | reg_initiator_to_string(nla_get_u32(tb[NL80211_ATTR_REG_INITIATOR]))); | |
362 | if (tb[NL80211_ATTR_WIPHY]) | |
363 | printf(" on phy%d", nla_get_u32(tb[NL80211_ATTR_WIPHY])); | |
364 | break; | |
365 | default: | |
366 | printf("unknown source (upgrade this utility)"); | |
367 | break; | |
368 | } | |
369 | ||
370 | printf("\n"); | |
0168589d LR |
371 | break; |
372 | case NL80211_CMD_REG_BEACON_HINT: | |
373 | ||
374 | wiphy_idx = nla_get_u32(tb[NL80211_ATTR_WIPHY]); | |
375 | ||
376 | memset(&chan_before_beacon, 0, sizeof(chan_before_beacon)); | |
377 | memset(&chan_after_beacon, 0, sizeof(chan_after_beacon)); | |
378 | ||
ce3b2ed9 JB |
379 | if (parse_beacon_hint_chan(tb[NL80211_ATTR_FREQ_BEFORE], |
380 | &chan_before_beacon)) | |
381 | break; | |
382 | if (parse_beacon_hint_chan(tb[NL80211_ATTR_FREQ_AFTER], | |
383 | &chan_after_beacon)) | |
384 | break; | |
0168589d LR |
385 | |
386 | if (chan_before_beacon.center_freq != chan_after_beacon.center_freq) | |
387 | break; | |
388 | ||
389 | /* A beacon hint is sent _only_ if something _did_ change */ | |
390 | printf("beacon hint:\n"); | |
391 | ||
392 | printf("phy%d %d MHz [%d]:\n", | |
393 | wiphy_idx, | |
394 | chan_before_beacon.center_freq, | |
395 | ieee80211_frequency_to_channel(chan_before_beacon.center_freq)); | |
396 | ||
f0c48e7b IP |
397 | if (chan_before_beacon.no_ir && !chan_after_beacon.no_ir) { |
398 | if (chan_before_beacon.no_ibss && !chan_after_beacon.no_ibss) | |
399 | printf("\to Initiating radiation enabled\n"); | |
400 | else | |
401 | printf("\to active scan enabled\n"); | |
402 | } else if (chan_before_beacon.no_ibss && !chan_after_beacon.no_ibss) { | |
403 | printf("\to ibss enabled\n"); | |
404 | } | |
0168589d | 405 | |
cec6d6a7 JB |
406 | break; |
407 | case NL80211_CMD_NEW_STATION: | |
408 | mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC])); | |
409 | printf("new station %s\n", macbuf); | |
957a0f07 | 410 | break; |
27bf109b AQ |
411 | case NL80211_CMD_DEL_STATION: |
412 | mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC])); | |
413 | printf("del station %s\n", macbuf); | |
414 | break; | |
957a0f07 JB |
415 | case NL80211_CMD_JOIN_IBSS: |
416 | mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC])); | |
417 | printf("IBSS %s joined\n", macbuf); | |
418 | break; | |
419 | case NL80211_CMD_AUTHENTICATE: | |
420 | printf("auth"); | |
aea5dbd2 JB |
421 | if (tb[NL80211_ATTR_FRAME]) |
422 | print_frame(args, tb[NL80211_ATTR_FRAME]); | |
423 | else if (tb[NL80211_ATTR_TIMED_OUT]) | |
424 | printf(": timed out"); | |
425 | else | |
426 | printf(": unknown event"); | |
957a0f07 JB |
427 | printf("\n"); |
428 | break; | |
429 | case NL80211_CMD_ASSOCIATE: | |
430 | printf("assoc"); | |
aea5dbd2 JB |
431 | if (tb[NL80211_ATTR_FRAME]) |
432 | print_frame(args, tb[NL80211_ATTR_FRAME]); | |
433 | else if (tb[NL80211_ATTR_TIMED_OUT]) | |
434 | printf(": timed out"); | |
435 | else | |
436 | printf(": unknown event"); | |
957a0f07 JB |
437 | printf("\n"); |
438 | break; | |
439 | case NL80211_CMD_DEAUTHENTICATE: | |
440 | printf("deauth"); | |
441 | print_frame(args, tb[NL80211_ATTR_FRAME]); | |
442 | printf("\n"); | |
443 | break; | |
444 | case NL80211_CMD_DISASSOCIATE: | |
445 | printf("disassoc"); | |
446 | print_frame(args, tb[NL80211_ATTR_FRAME]); | |
447 | printf("\n"); | |
448 | break; | |
99802e56 JB |
449 | case NL80211_CMD_UNPROT_DEAUTHENTICATE: |
450 | printf("unprotected deauth"); | |
451 | print_frame(args, tb[NL80211_ATTR_FRAME]); | |
452 | printf("\n"); | |
453 | break; | |
454 | case NL80211_CMD_UNPROT_DISASSOCIATE: | |
455 | printf("unprotected disassoc"); | |
456 | print_frame(args, tb[NL80211_ATTR_FRAME]); | |
457 | printf("\n"); | |
458 | break; | |
f1a666a6 JB |
459 | case NL80211_CMD_CONNECT: |
460 | status = 0; | |
461 | if (!tb[NL80211_ATTR_STATUS_CODE]) | |
462 | printf("unknown connect status"); | |
463 | else if (nla_get_u16(tb[NL80211_ATTR_STATUS_CODE]) == 0) | |
464 | printf("connected"); | |
465 | else { | |
466 | status = nla_get_u16(tb[NL80211_ATTR_STATUS_CODE]); | |
467 | printf("failed to connect"); | |
468 | } | |
469 | if (tb[NL80211_ATTR_MAC]) { | |
470 | mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC])); | |
471 | printf(" to %s", macbuf); | |
472 | } | |
473 | if (status) | |
474 | printf(", status: %d: %s", status, get_status_str(status)); | |
475 | printf("\n"); | |
476 | break; | |
477 | case NL80211_CMD_ROAM: | |
478 | printf("roamed"); | |
479 | if (tb[NL80211_ATTR_MAC]) { | |
480 | mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC])); | |
481 | printf(" to %s", macbuf); | |
482 | } | |
483 | printf("\n"); | |
484 | break; | |
485 | case NL80211_CMD_DISCONNECT: | |
486 | printf("disconnected"); | |
487 | if (tb[NL80211_ATTR_DISCONNECTED_BY_AP]) | |
488 | printf(" (by AP)"); | |
489 | else | |
490 | printf(" (local request)"); | |
491 | if (tb[NL80211_ATTR_REASON_CODE]) | |
492 | printf(" reason: %d: %s", nla_get_u16(tb[NL80211_ATTR_REASON_CODE]), | |
493 | get_reason_str(nla_get_u16(tb[NL80211_ATTR_REASON_CODE]))); | |
494 | printf("\n"); | |
495 | break; | |
6829308d JB |
496 | case NL80211_CMD_REMAIN_ON_CHANNEL: |
497 | printf("remain on freq %d (%dms, cookie %llx)\n", | |
498 | nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]), | |
499 | nla_get_u32(tb[NL80211_ATTR_DURATION]), | |
500 | (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE])); | |
501 | break; | |
502 | case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL: | |
503 | printf("done with remain on freq %d (cookie %llx)\n", | |
504 | nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]), | |
505 | (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE])); | |
506 | break; | |
7988b229 | 507 | case NL80211_CMD_NOTIFY_CQM: |
f19444fe | 508 | parse_cqm_event(tb); |
7988b229 | 509 | break; |
8612433d JM |
510 | case NL80211_CMD_MICHAEL_MIC_FAILURE: |
511 | parse_mic_failure(tb); | |
512 | break; | |
cbff7089 JB |
513 | case NL80211_CMD_FRAME_TX_STATUS: |
514 | printf("mgmt TX status (cookie %llx): %s\n", | |
515 | (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]), | |
516 | tb[NL80211_ATTR_ACK] ? "acked" : "no ack"); | |
517 | break; | |
0bf2c6a0 | 518 | case NL80211_CMD_PMKSA_CANDIDATE: |
b6688cb8 JB |
519 | printf("PMKSA candidate found\n"); |
520 | break; | |
819b78cc JB |
521 | case NL80211_CMD_SET_WOWLAN: |
522 | parse_wowlan_wake_event(tb); | |
523 | break; | |
4310994d BG |
524 | case NL80211_CMD_PROBE_CLIENT: |
525 | if (tb[NL80211_ATTR_MAC]) | |
526 | mac_addr_n2a(macbuf, nla_data(tb[NL80211_ATTR_MAC])); | |
527 | else | |
528 | strcpy(macbuf, "??"); | |
529 | printf("probe client %s (cookie %llx): %s\n", | |
530 | macbuf, | |
531 | (unsigned long long)nla_get_u64(tb[NL80211_ATTR_COOKIE]), | |
532 | tb[NL80211_ATTR_ACK] ? "acked" : "no ack"); | |
533 | break; | |
e8b3b312 JB |
534 | case NL80211_CMD_VENDOR: |
535 | printf("vendor event %.6x:%d\n", | |
536 | nla_get_u32(tb[NL80211_ATTR_VENDOR_ID]), | |
537 | nla_get_u32(tb[NL80211_ATTR_VENDOR_SUBCMD])); | |
492354de JD |
538 | if (args->frame && tb[NL80211_ATTR_VENDOR_DATA]) |
539 | iw_hexdump("vendor event", | |
540 | nla_data(tb[NL80211_ATTR_VENDOR_DATA]), | |
541 | nla_len(tb[NL80211_ATTR_VENDOR_DATA])); | |
e8b3b312 | 542 | break; |
957a0f07 | 543 | default: |
7c712c89 JB |
544 | printf("unknown event %d (%s)\n", |
545 | gnlh->cmd, command_name(gnlh->cmd)); | |
957a0f07 JB |
546 | break; |
547 | } | |
548 | ||
374e8a26 | 549 | fflush(stdout); |
957a0f07 JB |
550 | return NL_SKIP; |
551 | } | |
552 | ||
553 | struct wait_event { | |
554 | int n_cmds; | |
555 | const __u32 *cmds; | |
556 | __u32 cmd; | |
7fabd346 | 557 | struct print_event_args *pargs; |
957a0f07 JB |
558 | }; |
559 | ||
560 | static int wait_event(struct nl_msg *msg, void *arg) | |
561 | { | |
562 | struct wait_event *wait = arg; | |
563 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
564 | int i; | |
565 | ||
566 | for (i = 0; i < wait->n_cmds; i++) { | |
567 | if (gnlh->cmd == wait->cmds[i]) { | |
568 | wait->cmd = gnlh->cmd; | |
7e7c544f JB |
569 | if (wait->pargs) |
570 | print_event(msg, wait->pargs); | |
957a0f07 JB |
571 | } |
572 | } | |
573 | ||
574 | return NL_SKIP; | |
575 | } | |
576 | ||
ee374e4d | 577 | int __prepare_listen_events(struct nl80211_state *state) |
957a0f07 JB |
578 | { |
579 | int mcid, ret; | |
957a0f07 JB |
580 | |
581 | /* Configuration multicast group */ | |
582 | mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "config"); | |
583 | if (mcid < 0) | |
584 | return mcid; | |
585 | ||
586 | ret = nl_socket_add_membership(state->nl_sock, mcid); | |
587 | if (ret) | |
588 | return ret; | |
589 | ||
590 | /* Scan multicast group */ | |
591 | mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "scan"); | |
592 | if (mcid >= 0) { | |
593 | ret = nl_socket_add_membership(state->nl_sock, mcid); | |
594 | if (ret) | |
595 | return ret; | |
596 | } | |
597 | ||
598 | /* Regulatory multicast group */ | |
599 | mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "regulatory"); | |
600 | if (mcid >= 0) { | |
601 | ret = nl_socket_add_membership(state->nl_sock, mcid); | |
602 | if (ret) | |
603 | return ret; | |
604 | } | |
605 | ||
606 | /* MLME multicast group */ | |
607 | mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "mlme"); | |
608 | if (mcid >= 0) { | |
609 | ret = nl_socket_add_membership(state->nl_sock, mcid); | |
610 | if (ret) | |
611 | return ret; | |
612 | } | |
613 | ||
e8b3b312 JB |
614 | mcid = nl_get_multicast_id(state->nl_sock, "nl80211", "vendor"); |
615 | if (mcid >= 0) { | |
616 | ret = nl_socket_add_membership(state->nl_sock, mcid); | |
617 | if (ret) | |
618 | return ret; | |
619 | } | |
620 | ||
105aed57 JB |
621 | return 0; |
622 | } | |
623 | ||
ee374e4d JB |
624 | __u32 __do_listen_events(struct nl80211_state *state, |
625 | const int n_waits, const __u32 *waits, | |
626 | struct print_event_args *args) | |
105aed57 JB |
627 | { |
628 | struct nl_cb *cb = nl_cb_alloc(iw_debug ? NL_CB_DEBUG : NL_CB_DEFAULT); | |
629 | struct wait_event wait_ev; | |
105aed57 JB |
630 | |
631 | if (!cb) { | |
632 | fprintf(stderr, "failed to allocate netlink callbacks\n"); | |
633 | return -ENOMEM; | |
634 | } | |
635 | ||
957a0f07 JB |
636 | /* no sequence checking for multicast messages */ |
637 | nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL); | |
638 | ||
639 | if (n_waits && waits) { | |
640 | wait_ev.cmds = waits; | |
641 | wait_ev.n_cmds = n_waits; | |
7fabd346 | 642 | wait_ev.pargs = args; |
957a0f07 | 643 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, wait_event, &wait_ev); |
7fabd346 | 644 | } else |
957a0f07 | 645 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, print_event, args); |
957a0f07 JB |
646 | |
647 | wait_ev.cmd = 0; | |
648 | ||
649 | while (!wait_ev.cmd) | |
650 | nl_recvmsgs(state->nl_sock, cb); | |
651 | ||
652 | nl_cb_put(cb); | |
653 | ||
654 | return wait_ev.cmd; | |
655 | } | |
656 | ||
657 | __u32 listen_events(struct nl80211_state *state, | |
658 | const int n_waits, const __u32 *waits) | |
659 | { | |
ee374e4d JB |
660 | int ret; |
661 | ||
662 | ret = __prepare_listen_events(state); | |
663 | if (ret) | |
664 | return ret; | |
665 | ||
666 | return __do_listen_events(state, n_waits, waits, NULL); | |
957a0f07 JB |
667 | } |
668 | ||
669 | static int print_events(struct nl80211_state *state, | |
670 | struct nl_cb *cb, | |
671 | struct nl_msg *msg, | |
05514f95 JB |
672 | int argc, char **argv, |
673 | enum id_input id) | |
957a0f07 JB |
674 | { |
675 | struct print_event_args args; | |
ee374e4d | 676 | int ret; |
957a0f07 JB |
677 | |
678 | memset(&args, 0, sizeof(args)); | |
679 | ||
680 | argc--; | |
681 | argv++; | |
682 | ||
683 | while (argc > 0) { | |
684 | if (strcmp(argv[0], "-f") == 0) | |
685 | args.frame = true; | |
686 | else if (strcmp(argv[0], "-t") == 0) | |
687 | args.time = true; | |
981b21ad JB |
688 | else if (strcmp(argv[0], "-r") == 0) |
689 | args.reltime = true; | |
957a0f07 JB |
690 | else |
691 | return 1; | |
692 | argc--; | |
693 | argv++; | |
694 | } | |
695 | ||
981b21ad JB |
696 | if (args.time && args.reltime) |
697 | return 1; | |
698 | ||
957a0f07 JB |
699 | if (argc) |
700 | return 1; | |
701 | ||
ee374e4d JB |
702 | ret = __prepare_listen_events(state); |
703 | if (ret) | |
704 | return ret; | |
705 | ||
706 | return __do_listen_events(state, 0, NULL, &args); | |
957a0f07 | 707 | } |
981b21ad | 708 | TOPLEVEL(event, "[-t] [-r] [-f]", 0, 0, CIB_NONE, print_events, |
01ae06f9 JB |
709 | "Monitor events from the kernel.\n" |
710 | "-t - print timestamp\n" | |
981b21ad | 711 | "-r - print relative timstamp\n" |
01ae06f9 | 712 | "-f - print full frame for auth/assoc etc."); |