7 static int no_seq_check(struct nl_msg
*msg
, void *arg
)
12 struct ieee80211_beacon_channel
{
18 static int parse_beacon_hint_chan(struct nlattr
*tb
,
19 struct ieee80211_beacon_channel
*chan
)
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
},
28 if (nla_parse_nested(tb_freq
,
29 NL80211_FREQUENCY_ATTR_MAX
,
34 chan
->center_freq
= nla_get_u32(tb_freq
[NL80211_FREQUENCY_ATTR_FREQ
]);
36 if (tb_freq
[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN
])
37 chan
->passive_scan
= true;
38 if (tb_freq
[NL80211_FREQUENCY_ATTR_NO_IBSS
])
44 static void print_frame(struct print_event_args
*args
, struct nlattr
*attr
)
53 printf(" [no frame]");
55 frame
= nla_data(attr
);
59 printf(" [invalid frame: ");
63 mac_addr_n2a(macbuf
, frame
+ 10);
64 printf(" %s -> ", macbuf
);
65 mac_addr_n2a(macbuf
, frame
+ 4);
68 switch (frame
[0] & 0xfc) {
69 case 0x10: /* assoc resp */
70 case 0x30: /* reassoc resp */
72 tmp
= (frame
[27] << 8) + frame
[26];
73 printf(" status: %d: %s", tmp
, get_status_str(tmp
));
75 case 0x00: /* assoc req */
76 case 0x20: /* reassoc req */
80 tmp
= (frame
[29] << 8) + frame
[28];
81 printf(" status: %d: %s", tmp
, get_status_str(tmp
));
84 case 0xa0: /* disassoc */
85 case 0xc0: /* deauth */
87 tmp
= (frame
[25] << 8) + frame
[24];
88 printf(" reason %d: %s", tmp
, get_reason_str(tmp
));
98 for (i
= 0; i
< len
; i
++)
99 printf(" %.02x", frame
[i
]);
103 static int print_event(struct nl_msg
*msg
, void *arg
)
105 #define PARSE_BEACON_CHAN(_attr, _chan) do { \
106 r = parse_beacon_hint_chan(tb[_attr], \
111 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
112 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1], *nst
;
113 struct print_event_args
*args
= arg
;
117 struct ieee80211_beacon_channel chan_before_beacon
, chan_after_beacon
;
125 gettimeofday(&tv
, NULL
);
126 printf("%ld.%06u: ", (long) tv
.tv_sec
, (unsigned int) tv
.tv_usec
);
129 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
130 genlmsg_attrlen(gnlh
, 0), NULL
);
132 if (tb
[NL80211_ATTR_IFINDEX
] && tb
[NL80211_ATTR_WIPHY
]) {
133 if_indextoname(nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]), ifname
);
134 printf("%s (phy #%d): ", ifname
, nla_get_u32(tb
[NL80211_ATTR_WIPHY
]));
135 } else if (tb
[NL80211_ATTR_IFINDEX
]) {
136 if_indextoname(nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]), ifname
);
137 printf("%s: ", ifname
);
138 } else if (tb
[NL80211_ATTR_WIPHY
]) {
139 printf("phy #%d: ", nla_get_u32(tb
[NL80211_ATTR_WIPHY
]));
143 case NL80211_CMD_NEW_WIPHY
:
144 printf("renamed to %s\n", nla_get_string(tb
[NL80211_ATTR_WIPHY_NAME
]));
146 case NL80211_CMD_TRIGGER_SCAN
:
147 printf("scan started\n");
149 case NL80211_CMD_NEW_SCAN_RESULTS
:
150 printf("scan finished:");
151 case NL80211_CMD_SCAN_ABORTED
:
152 if (gnlh
->cmd
== NL80211_CMD_SCAN_ABORTED
)
153 printf("scan aborted:");
154 if (tb
[NL80211_ATTR_SCAN_FREQUENCIES
]) {
155 nla_for_each_nested(nst
, tb
[NL80211_ATTR_SCAN_FREQUENCIES
], rem_nst
)
156 printf(" %d", nla_get_u32(nst
));
159 if (tb
[NL80211_ATTR_SCAN_SSIDS
]) {
160 nla_for_each_nested(nst
, tb
[NL80211_ATTR_SCAN_SSIDS
], rem_nst
) {
162 print_ssid_escaped(nla_len(nst
), nla_data(nst
));
168 case NL80211_CMD_REG_CHANGE
:
169 printf("regulatory domain change: ");
171 reg_type
= nla_get_u8(tb
[NL80211_ATTR_REG_TYPE
]);
174 case NL80211_REGDOM_TYPE_COUNTRY
:
175 printf("set to %s by %s request",
176 nla_get_string(tb
[NL80211_ATTR_REG_ALPHA2
]),
177 reg_initiator_to_string(nla_get_u8(tb
[NL80211_ATTR_REG_INITIATOR
])));
178 if (tb
[NL80211_ATTR_WIPHY
])
179 printf(" on phy%d", nla_get_u32(tb
[NL80211_ATTR_WIPHY
]));
181 case NL80211_REGDOM_TYPE_WORLD
:
182 printf("set to world roaming by %s request",
183 reg_initiator_to_string(nla_get_u8(tb
[NL80211_ATTR_REG_INITIATOR
])));
185 case NL80211_REGDOM_TYPE_CUSTOM_WORLD
:
186 printf("custom world roaming rules in place on phy%d by %s request",
187 nla_get_u32(tb
[NL80211_ATTR_WIPHY
]),
188 reg_initiator_to_string(nla_get_u32(tb
[NL80211_ATTR_REG_INITIATOR
])));
190 case NL80211_REGDOM_TYPE_INTERSECTION
:
191 printf("intersection used due to a request made by %s",
192 reg_initiator_to_string(nla_get_u32(tb
[NL80211_ATTR_REG_INITIATOR
])));
193 if (tb
[NL80211_ATTR_WIPHY
])
194 printf(" on phy%d", nla_get_u32(tb
[NL80211_ATTR_WIPHY
]));
197 printf("unknown source (upgrade this utility)");
203 case NL80211_CMD_REG_BEACON_HINT
:
205 wiphy_idx
= nla_get_u32(tb
[NL80211_ATTR_WIPHY
]);
207 memset(&chan_before_beacon
, 0, sizeof(chan_before_beacon
));
208 memset(&chan_after_beacon
, 0, sizeof(chan_after_beacon
));
210 PARSE_BEACON_CHAN(NL80211_ATTR_FREQ_BEFORE
, chan_before_beacon
);
211 PARSE_BEACON_CHAN(NL80211_ATTR_FREQ_AFTER
, chan_after_beacon
);
213 if (chan_before_beacon
.center_freq
!= chan_after_beacon
.center_freq
)
216 /* A beacon hint is sent _only_ if something _did_ change */
217 printf("beacon hint:\n");
219 printf("phy%d %d MHz [%d]:\n",
221 chan_before_beacon
.center_freq
,
222 ieee80211_frequency_to_channel(chan_before_beacon
.center_freq
));
224 if (chan_before_beacon
.passive_scan
&& !chan_after_beacon
.passive_scan
)
225 printf("\to active scanning enabled\n");
226 if (chan_before_beacon
.no_ibss
&& !chan_after_beacon
.no_ibss
)
227 printf("\to beaconing enabled\n");
230 case NL80211_CMD_JOIN_IBSS
:
231 mac_addr_n2a(macbuf
, nla_data(tb
[NL80211_ATTR_MAC
]));
232 printf("IBSS %s joined\n", macbuf
);
234 case NL80211_CMD_AUTHENTICATE
:
236 if (tb
[NL80211_ATTR_FRAME
])
237 print_frame(args
, tb
[NL80211_ATTR_FRAME
]);
238 else if (tb
[NL80211_ATTR_TIMED_OUT
])
239 printf(": timed out");
241 printf(": unknown event");
244 case NL80211_CMD_ASSOCIATE
:
246 if (tb
[NL80211_ATTR_FRAME
])
247 print_frame(args
, tb
[NL80211_ATTR_FRAME
]);
248 else if (tb
[NL80211_ATTR_TIMED_OUT
])
249 printf(": timed out");
251 printf(": unknown event");
254 case NL80211_CMD_DEAUTHENTICATE
:
256 print_frame(args
, tb
[NL80211_ATTR_FRAME
]);
259 case NL80211_CMD_DISASSOCIATE
:
261 print_frame(args
, tb
[NL80211_ATTR_FRAME
]);
264 case NL80211_CMD_CONNECT
:
266 if (!tb
[NL80211_ATTR_STATUS_CODE
])
267 printf("unknown connect status");
268 else if (nla_get_u16(tb
[NL80211_ATTR_STATUS_CODE
]) == 0)
271 status
= nla_get_u16(tb
[NL80211_ATTR_STATUS_CODE
]);
272 printf("failed to connect");
274 if (tb
[NL80211_ATTR_MAC
]) {
275 mac_addr_n2a(macbuf
, nla_data(tb
[NL80211_ATTR_MAC
]));
276 printf(" to %s", macbuf
);
279 printf(", status: %d: %s", status
, get_status_str(status
));
282 case NL80211_CMD_ROAM
:
284 if (tb
[NL80211_ATTR_MAC
]) {
285 mac_addr_n2a(macbuf
, nla_data(tb
[NL80211_ATTR_MAC
]));
286 printf(" to %s", macbuf
);
290 case NL80211_CMD_DISCONNECT
:
291 printf("disconnected");
292 if (tb
[NL80211_ATTR_DISCONNECTED_BY_AP
])
295 printf(" (local request)");
296 if (tb
[NL80211_ATTR_REASON_CODE
])
297 printf(" reason: %d: %s", nla_get_u16(tb
[NL80211_ATTR_REASON_CODE
]),
298 get_reason_str(nla_get_u16(tb
[NL80211_ATTR_REASON_CODE
])));
302 printf("unknown event %d\n", gnlh
->cmd
);
307 #undef PARSE_BEACON_CHAN
314 struct print_event_args
*pargs
;
317 static int wait_event(struct nl_msg
*msg
, void *arg
)
319 struct wait_event
*wait
= arg
;
320 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
323 for (i
= 0; i
< wait
->n_cmds
; i
++) {
324 if (gnlh
->cmd
== wait
->cmds
[i
]) {
325 wait
->cmd
= gnlh
->cmd
;
327 print_event(msg
, wait
->pargs
);
334 __u32
__listen_events(struct nl80211_state
*state
,
335 const int n_waits
, const __u32
*waits
,
336 struct print_event_args
*args
)
339 struct nl_cb
*cb
= nl_cb_alloc(iw_debug
? NL_CB_DEBUG
: NL_CB_DEFAULT
);
340 struct wait_event wait_ev
;
343 fprintf(stderr
, "failed to allocate netlink callbacks\n");
347 /* Configuration multicast group */
348 mcid
= nl_get_multicast_id(state
->nl_sock
, "nl80211", "config");
352 ret
= nl_socket_add_membership(state
->nl_sock
, mcid
);
356 /* Scan multicast group */
357 mcid
= nl_get_multicast_id(state
->nl_sock
, "nl80211", "scan");
359 ret
= nl_socket_add_membership(state
->nl_sock
, mcid
);
364 /* Regulatory multicast group */
365 mcid
= nl_get_multicast_id(state
->nl_sock
, "nl80211", "regulatory");
367 ret
= nl_socket_add_membership(state
->nl_sock
, mcid
);
372 /* MLME multicast group */
373 mcid
= nl_get_multicast_id(state
->nl_sock
, "nl80211", "mlme");
375 ret
= nl_socket_add_membership(state
->nl_sock
, mcid
);
380 /* no sequence checking for multicast messages */
381 nl_cb_set(cb
, NL_CB_SEQ_CHECK
, NL_CB_CUSTOM
, no_seq_check
, NULL
);
383 if (n_waits
&& waits
) {
384 wait_ev
.cmds
= waits
;
385 wait_ev
.n_cmds
= n_waits
;
386 wait_ev
.pargs
= args
;
387 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, wait_event
, &wait_ev
);
389 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_event
, args
);
394 nl_recvmsgs(state
->nl_sock
, cb
);
401 __u32
listen_events(struct nl80211_state
*state
,
402 const int n_waits
, const __u32
*waits
)
404 return __listen_events(state
, n_waits
, waits
, NULL
);
407 static int print_events(struct nl80211_state
*state
,
410 int argc
, char **argv
)
412 struct print_event_args args
;
414 memset(&args
, 0, sizeof(args
));
420 if (strcmp(argv
[0], "-f") == 0)
422 else if (strcmp(argv
[0], "-t") == 0)
433 return __listen_events(state
, 0, NULL
, &args
);
435 TOPLEVEL(event
, "[-t] [-f]", 0, 0, CIB_NONE
, print_events
,
436 "Monitor events from the kernel.\n"
437 "-t - print timestamp\n"
438 "-f - print full frame for auth/assoc etc.");