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