]>
Commit | Line | Data |
---|---|---|
3f5285e8 | 1 | /* |
c5121837 | 2 | * Driver interaction with Linux nl80211/cfg80211 |
8d923a4a | 3 | * Copyright (c) 2002-2010, Jouni Malinen <j@w1.fi> |
072ad14c JM |
4 | * Copyright (c) 2003-2004, Instant802 Networks, Inc. |
5 | * Copyright (c) 2005-2006, Devicescape Software, Inc. | |
6 | * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net> | |
58f6fbe0 | 7 | * Copyright (c) 2009-2010, Atheros Communications |
3f5285e8 JM |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License version 2 as | |
11 | * published by the Free Software Foundation. | |
12 | * | |
13 | * Alternatively, this software may be distributed under the terms of BSD | |
14 | * license. | |
15 | * | |
16 | * See README and COPYING for more details. | |
17 | */ | |
18 | ||
19 | #include "includes.h" | |
20 | #include <sys/ioctl.h> | |
37b7d082 | 21 | #include <net/if.h> |
3f5285e8 JM |
22 | #include <netlink/genl/genl.h> |
23 | #include <netlink/genl/family.h> | |
24 | #include <netlink/genl/ctrl.h> | |
1b648c7e JM |
25 | #include <netpacket/packet.h> |
26 | #include <linux/filter.h> | |
7e45830a | 27 | #include "nl80211_copy.h" |
625f587b | 28 | |
3f5285e8 | 29 | #include "common.h" |
1b648c7e JM |
30 | #include "eloop.h" |
31 | #include "common/ieee802_11_defs.h" | |
e2d02c29 | 32 | #include "netlink.h" |
34f2f814 | 33 | #include "linux_ioctl.h" |
e2d02c29 JM |
34 | #include "radiotap.h" |
35 | #include "radiotap_iter.h" | |
1b648c7e | 36 | #include "driver.h" |
0915d02c | 37 | |
c5121837 JM |
38 | #ifdef CONFIG_LIBNL20 |
39 | /* libnl 2.0 compatibility code */ | |
2e8eac2d | 40 | #define nl_handle nl_sock |
c5121837 JM |
41 | #define nl_handle_alloc_cb nl_socket_alloc_cb |
42 | #define nl_handle_destroy nl_socket_free | |
43 | #endif /* CONFIG_LIBNL20 */ | |
44 | ||
c5121837 | 45 | |
3f5285e8 JM |
46 | #ifndef IFF_LOWER_UP |
47 | #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */ | |
48 | #endif | |
49 | #ifndef IFF_DORMANT | |
50 | #define IFF_DORMANT 0x20000 /* driver signals dormant */ | |
51 | #endif | |
52 | ||
53 | #ifndef IF_OPER_DORMANT | |
54 | #define IF_OPER_DORMANT 5 | |
55 | #endif | |
56 | #ifndef IF_OPER_UP | |
57 | #define IF_OPER_UP 6 | |
58 | #endif | |
59 | ||
c5121837 | 60 | struct i802_bss { |
a2e40bb6 | 61 | struct wpa_driver_nl80211_data *drv; |
c5121837 | 62 | struct i802_bss *next; |
b4fd6fab | 63 | int ifindex; |
a2e40bb6 | 64 | char ifname[IFNAMSIZ + 1]; |
c5121837 JM |
65 | unsigned int beacon_set:1; |
66 | }; | |
3f5285e8 JM |
67 | |
68 | struct wpa_driver_nl80211_data { | |
69 | void *ctx; | |
08063178 | 70 | struct netlink_data *netlink; |
c5121837 | 71 | int ioctl_sock; /* socket for ioctl() use */ |
94627f6c | 72 | char brname[IFNAMSIZ]; |
3f5285e8 | 73 | int ifindex; |
7524cfb1 | 74 | int if_removed; |
c2a04078 JM |
75 | struct wpa_driver_capa capa; |
76 | int has_capability; | |
c2a04078 | 77 | |
3f5285e8 JM |
78 | int operstate; |
79 | ||
3f5285e8 JM |
80 | int scan_complete_events; |
81 | ||
82 | struct nl_handle *nl_handle; | |
335ce76b | 83 | struct nl_handle *nl_handle_event; |
3f5285e8 | 84 | struct nl_cache *nl_cache; |
335ce76b | 85 | struct nl_cache *nl_cache_event; |
3f5285e8 JM |
86 | struct nl_cb *nl_cb; |
87 | struct genl_family *nl80211; | |
1c873584 | 88 | |
e6b8efeb | 89 | u8 auth_bssid[ETH_ALEN]; |
c2a04078 JM |
90 | u8 bssid[ETH_ALEN]; |
91 | int associated; | |
fd05d64e JM |
92 | u8 ssid[32]; |
93 | size_t ssid_len; | |
ad1e68e6 JM |
94 | int nlmode; |
95 | int ap_scan_as_station; | |
4832ecd7 | 96 | unsigned int assoc_freq; |
d2440ba0 | 97 | |
0915d02c JM |
98 | int monitor_sock; |
99 | int monitor_ifidx; | |
504e905c | 100 | int probe_req_report; |
4e5cb1a3 | 101 | int disable_11b_rates; |
7da3abe7 | 102 | |
55777702 | 103 | unsigned int pending_remain_on_chan:1; |
58f6fbe0 | 104 | unsigned int pending_send_action:1; |
94627f6c JM |
105 | unsigned int added_bridge:1; |
106 | unsigned int added_if_into_bridge:1; | |
55777702 JM |
107 | |
108 | u64 remain_on_chan_cookie; | |
58f6fbe0 | 109 | u64 send_action_cookie; |
c5121837 | 110 | |
3812464c JM |
111 | struct wpa_driver_scan_filter *filter_ssids; |
112 | size_t num_filter_ssids; | |
113 | ||
a2e40bb6 FF |
114 | struct i802_bss first_bss; |
115 | ||
c5121837 | 116 | #ifdef HOSTAPD |
c5121837 | 117 | int eapol_sock; /* socket for EAPOL frames */ |
c5121837 JM |
118 | |
119 | int default_if_indices[16]; | |
120 | int *if_indices; | |
121 | int num_if_indices; | |
122 | ||
c5121837 JM |
123 | int last_freq; |
124 | int last_freq_ht; | |
c5121837 | 125 | #endif /* HOSTAPD */ |
3f5285e8 JM |
126 | }; |
127 | ||
128 | ||
129 | static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, | |
130 | void *timeout_ctx); | |
ad1e68e6 | 131 | static int wpa_driver_nl80211_set_mode(void *priv, int mode); |
362f781e | 132 | static int |
7524cfb1 | 133 | wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv); |
d72aad94 | 134 | static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv, |
77339912 JM |
135 | const u8 *addr, int cmd, u16 reason_code, |
136 | int local_state_change); | |
460456f8 JM |
137 | static void nl80211_remove_monitor_interface( |
138 | struct wpa_driver_nl80211_data *drv); | |
0915d02c | 139 | |
072ad14c | 140 | #ifdef HOSTAPD |
2135f224 JM |
141 | static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx); |
142 | static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx); | |
fbbfcbac FF |
143 | static int wpa_driver_nl80211_if_remove(void *priv, |
144 | enum wpa_driver_if_type type, | |
145 | const char *ifname); | |
072ad14c JM |
146 | #endif /* HOSTAPD */ |
147 | ||
e3802622 | 148 | static int i802_set_freq(void *priv, struct hostapd_freq_params *freq); |
504e905c JM |
149 | static void wpa_driver_nl80211_probe_req_report_timeout(void *eloop_ctx, |
150 | void *timeout_ctx); | |
4e5cb1a3 JM |
151 | static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv, |
152 | int ifindex, int disabled); | |
504e905c | 153 | |
3f5285e8 | 154 | |
6241fcb1 JM |
155 | /* nl80211 code */ |
156 | static int ack_handler(struct nl_msg *msg, void *arg) | |
157 | { | |
158 | int *err = arg; | |
159 | *err = 0; | |
160 | return NL_STOP; | |
161 | } | |
162 | ||
163 | static int finish_handler(struct nl_msg *msg, void *arg) | |
164 | { | |
8e8df255 JM |
165 | int *ret = arg; |
166 | *ret = 0; | |
6241fcb1 JM |
167 | return NL_SKIP; |
168 | } | |
169 | ||
170 | static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err, | |
171 | void *arg) | |
172 | { | |
173 | int *ret = arg; | |
174 | *ret = err->error; | |
175 | return NL_SKIP; | |
176 | } | |
177 | ||
5b7b85f6 JM |
178 | |
179 | static int no_seq_check(struct nl_msg *msg, void *arg) | |
180 | { | |
181 | return NL_OK; | |
182 | } | |
183 | ||
184 | ||
58f6fbe0 JM |
185 | static int send_and_recv(struct wpa_driver_nl80211_data *drv, |
186 | struct nl_handle *nl_handle, struct nl_msg *msg, | |
187 | int (*valid_handler)(struct nl_msg *, void *), | |
188 | void *valid_data) | |
6241fcb1 JM |
189 | { |
190 | struct nl_cb *cb; | |
191 | int err = -ENOMEM; | |
192 | ||
193 | cb = nl_cb_clone(drv->nl_cb); | |
194 | if (!cb) | |
195 | goto out; | |
196 | ||
58f6fbe0 | 197 | err = nl_send_auto_complete(nl_handle, msg); |
6241fcb1 JM |
198 | if (err < 0) |
199 | goto out; | |
200 | ||
201 | err = 1; | |
202 | ||
203 | nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err); | |
8e8df255 | 204 | nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err); |
6241fcb1 JM |
205 | nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err); |
206 | ||
207 | if (valid_handler) | |
208 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, | |
209 | valid_handler, valid_data); | |
210 | ||
211 | while (err > 0) | |
58f6fbe0 | 212 | nl_recvmsgs(nl_handle, cb); |
6241fcb1 JM |
213 | out: |
214 | nl_cb_put(cb); | |
215 | nlmsg_free(msg); | |
216 | return err; | |
217 | } | |
218 | ||
219 | ||
58f6fbe0 JM |
220 | static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv, |
221 | struct nl_msg *msg, | |
222 | int (*valid_handler)(struct nl_msg *, void *), | |
223 | void *valid_data) | |
224 | { | |
225 | return send_and_recv(drv, drv->nl_handle, msg, valid_handler, | |
226 | valid_data); | |
227 | } | |
228 | ||
229 | ||
97865538 JM |
230 | struct family_data { |
231 | const char *group; | |
232 | int id; | |
233 | }; | |
234 | ||
235 | ||
236 | static int family_handler(struct nl_msg *msg, void *arg) | |
237 | { | |
238 | struct family_data *res = arg; | |
239 | struct nlattr *tb[CTRL_ATTR_MAX + 1]; | |
240 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
241 | struct nlattr *mcgrp; | |
242 | int i; | |
243 | ||
244 | nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
245 | genlmsg_attrlen(gnlh, 0), NULL); | |
246 | if (!tb[CTRL_ATTR_MCAST_GROUPS]) | |
247 | return NL_SKIP; | |
248 | ||
249 | nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) { | |
250 | struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1]; | |
251 | nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp), | |
252 | nla_len(mcgrp), NULL); | |
253 | if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] || | |
254 | !tb2[CTRL_ATTR_MCAST_GRP_ID] || | |
255 | os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]), | |
256 | res->group, | |
257 | nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0) | |
258 | continue; | |
259 | res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]); | |
260 | break; | |
261 | }; | |
262 | ||
263 | return NL_SKIP; | |
264 | } | |
265 | ||
266 | ||
267 | static int nl_get_multicast_id(struct wpa_driver_nl80211_data *drv, | |
268 | const char *family, const char *group) | |
269 | { | |
270 | struct nl_msg *msg; | |
271 | int ret = -1; | |
272 | struct family_data res = { group, -ENOENT }; | |
273 | ||
274 | msg = nlmsg_alloc(); | |
275 | if (!msg) | |
276 | return -ENOMEM; | |
277 | genlmsg_put(msg, 0, 0, genl_ctrl_resolve(drv->nl_handle, "nlctrl"), | |
278 | 0, 0, CTRL_CMD_GETFAMILY, 0); | |
279 | NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family); | |
280 | ||
281 | ret = send_and_recv_msgs(drv, msg, family_handler, &res); | |
282 | msg = NULL; | |
283 | if (ret == 0) | |
284 | ret = res.id; | |
285 | ||
286 | nla_put_failure: | |
287 | nlmsg_free(msg); | |
288 | return ret; | |
289 | } | |
290 | ||
291 | ||
3f5285e8 JM |
292 | static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid) |
293 | { | |
a2e40bb6 FF |
294 | struct i802_bss *bss = priv; |
295 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
c2a04078 JM |
296 | if (!drv->associated) |
297 | return -1; | |
298 | os_memcpy(bssid, drv->bssid, ETH_ALEN); | |
299 | return 0; | |
3f5285e8 JM |
300 | } |
301 | ||
302 | ||
3f5285e8 JM |
303 | static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid) |
304 | { | |
a2e40bb6 FF |
305 | struct i802_bss *bss = priv; |
306 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
fd05d64e JM |
307 | if (!drv->associated) |
308 | return -1; | |
309 | os_memcpy(ssid, drv->ssid, drv->ssid_len); | |
310 | return drv->ssid_len; | |
3f5285e8 JM |
311 | } |
312 | ||
313 | ||
7524cfb1 | 314 | static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv, |
08063178 | 315 | char *buf, size_t len, int del) |
3f5285e8 JM |
316 | { |
317 | union wpa_event_data event; | |
318 | ||
319 | os_memset(&event, 0, sizeof(event)); | |
320 | if (len > sizeof(event.interface_status.ifname)) | |
321 | len = sizeof(event.interface_status.ifname) - 1; | |
322 | os_memcpy(event.interface_status.ifname, buf, len); | |
323 | event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED : | |
324 | EVENT_INTERFACE_ADDED; | |
325 | ||
326 | wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s", | |
327 | del ? "DEL" : "NEW", | |
328 | event.interface_status.ifname, | |
329 | del ? "removed" : "added"); | |
330 | ||
a2e40bb6 | 331 | if (os_strcmp(drv->first_bss.ifname, event.interface_status.ifname) == 0) { |
7524cfb1 JM |
332 | if (del) |
333 | drv->if_removed = 1; | |
334 | else | |
335 | drv->if_removed = 0; | |
336 | } | |
337 | ||
08063178 | 338 | wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event); |
3f5285e8 JM |
339 | } |
340 | ||
341 | ||
7524cfb1 | 342 | static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv, |
62d680c3 | 343 | u8 *buf, size_t len) |
7524cfb1 | 344 | { |
62d680c3 | 345 | int attrlen, rta_len; |
7524cfb1 JM |
346 | struct rtattr *attr; |
347 | ||
62d680c3 JM |
348 | attrlen = len; |
349 | attr = (struct rtattr *) buf; | |
7524cfb1 JM |
350 | |
351 | rta_len = RTA_ALIGN(sizeof(struct rtattr)); | |
352 | while (RTA_OK(attr, attrlen)) { | |
353 | if (attr->rta_type == IFLA_IFNAME) { | |
a2e40bb6 | 354 | if (os_strcmp(((char *) attr) + rta_len, drv->first_bss.ifname) |
7524cfb1 JM |
355 | == 0) |
356 | return 1; | |
357 | else | |
358 | break; | |
359 | } | |
360 | attr = RTA_NEXT(attr, attrlen); | |
361 | } | |
362 | ||
363 | return 0; | |
364 | } | |
365 | ||
366 | ||
367 | static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv, | |
62d680c3 | 368 | int ifindex, u8 *buf, size_t len) |
7524cfb1 JM |
369 | { |
370 | if (drv->ifindex == ifindex) | |
371 | return 1; | |
372 | ||
62d680c3 | 373 | if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) { |
a2e40bb6 | 374 | drv->first_bss.ifindex = if_nametoindex(drv->first_bss.ifname); |
7524cfb1 JM |
375 | wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed " |
376 | "interface"); | |
377 | wpa_driver_nl80211_finish_drv_init(drv); | |
378 | return 1; | |
379 | } | |
380 | ||
381 | return 0; | |
382 | } | |
383 | ||
384 | ||
62d680c3 JM |
385 | static void wpa_driver_nl80211_event_rtm_newlink(void *ctx, |
386 | struct ifinfomsg *ifi, | |
387 | u8 *buf, size_t len) | |
3f5285e8 | 388 | { |
08063178 | 389 | struct wpa_driver_nl80211_data *drv = ctx; |
62d680c3 JM |
390 | int attrlen, rta_len; |
391 | struct rtattr *attr; | |
3f5285e8 | 392 | |
62d680c3 | 393 | if (!wpa_driver_nl80211_own_ifindex(drv, ifi->ifi_index, buf, len)) { |
3f5285e8 JM |
394 | wpa_printf(MSG_DEBUG, "Ignore event for foreign ifindex %d", |
395 | ifi->ifi_index); | |
396 | return; | |
397 | } | |
398 | ||
399 | wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x " | |
400 | "(%s%s%s%s)", | |
401 | drv->operstate, ifi->ifi_flags, | |
402 | (ifi->ifi_flags & IFF_UP) ? "[UP]" : "", | |
403 | (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "", | |
404 | (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "", | |
405 | (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : ""); | |
406 | /* | |
407 | * Some drivers send the association event before the operup event--in | |
408 | * this case, lifting operstate in wpa_driver_nl80211_set_operstate() | |
409 | * fails. This will hit us when wpa_supplicant does not need to do | |
410 | * IEEE 802.1X authentication | |
411 | */ | |
412 | if (drv->operstate == 1 && | |
413 | (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP && | |
414 | !(ifi->ifi_flags & IFF_RUNNING)) | |
08063178 | 415 | netlink_send_oper_ifla(drv->netlink, drv->ifindex, |
e2d02c29 | 416 | -1, IF_OPER_UP); |
3f5285e8 | 417 | |
62d680c3 JM |
418 | attrlen = len; |
419 | attr = (struct rtattr *) buf; | |
3f5285e8 JM |
420 | rta_len = RTA_ALIGN(sizeof(struct rtattr)); |
421 | while (RTA_OK(attr, attrlen)) { | |
d8816397 | 422 | if (attr->rta_type == IFLA_IFNAME) { |
7524cfb1 | 423 | wpa_driver_nl80211_event_link( |
08063178 | 424 | drv, |
7524cfb1 JM |
425 | ((char *) attr) + rta_len, |
426 | attr->rta_len - rta_len, 0); | |
3f5285e8 JM |
427 | } |
428 | attr = RTA_NEXT(attr, attrlen); | |
429 | } | |
430 | } | |
431 | ||
432 | ||
62d680c3 JM |
433 | static void wpa_driver_nl80211_event_rtm_dellink(void *ctx, |
434 | struct ifinfomsg *ifi, | |
435 | u8 *buf, size_t len) | |
3f5285e8 | 436 | { |
08063178 | 437 | struct wpa_driver_nl80211_data *drv = ctx; |
62d680c3 JM |
438 | int attrlen, rta_len; |
439 | struct rtattr *attr; | |
3f5285e8 | 440 | |
62d680c3 JM |
441 | attrlen = len; |
442 | attr = (struct rtattr *) buf; | |
3f5285e8 JM |
443 | |
444 | rta_len = RTA_ALIGN(sizeof(struct rtattr)); | |
445 | while (RTA_OK(attr, attrlen)) { | |
446 | if (attr->rta_type == IFLA_IFNAME) { | |
7524cfb1 | 447 | wpa_driver_nl80211_event_link( |
08063178 | 448 | drv, |
7524cfb1 JM |
449 | ((char *) attr) + rta_len, |
450 | attr->rta_len - rta_len, 1); | |
3f5285e8 JM |
451 | } |
452 | attr = RTA_NEXT(attr, attrlen); | |
453 | } | |
454 | } | |
455 | ||
456 | ||
c2a04078 JM |
457 | static void mlme_event_auth(struct wpa_driver_nl80211_data *drv, |
458 | const u8 *frame, size_t len) | |
459 | { | |
460 | const struct ieee80211_mgmt *mgmt; | |
461 | union wpa_event_data event; | |
462 | ||
463 | mgmt = (const struct ieee80211_mgmt *) frame; | |
464 | if (len < 24 + sizeof(mgmt->u.auth)) { | |
465 | wpa_printf(MSG_DEBUG, "nl80211: Too short association event " | |
466 | "frame"); | |
467 | return; | |
468 | } | |
469 | ||
e6b8efeb | 470 | os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN); |
c2a04078 JM |
471 | os_memset(&event, 0, sizeof(event)); |
472 | os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN); | |
473 | event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg); | |
474 | event.auth.status_code = le_to_host16(mgmt->u.auth.status_code); | |
475 | if (len > 24 + sizeof(mgmt->u.auth)) { | |
476 | event.auth.ies = mgmt->u.auth.variable; | |
477 | event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth); | |
478 | } | |
479 | ||
480 | wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event); | |
481 | } | |
482 | ||
483 | ||
484 | static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv, | |
485 | const u8 *frame, size_t len) | |
486 | { | |
487 | const struct ieee80211_mgmt *mgmt; | |
488 | union wpa_event_data event; | |
489 | u16 status; | |
490 | ||
491 | mgmt = (const struct ieee80211_mgmt *) frame; | |
492 | if (len < 24 + sizeof(mgmt->u.assoc_resp)) { | |
493 | wpa_printf(MSG_DEBUG, "nl80211: Too short association event " | |
494 | "frame"); | |
495 | return; | |
496 | } | |
497 | ||
498 | status = le_to_host16(mgmt->u.assoc_resp.status_code); | |
499 | if (status != WLAN_STATUS_SUCCESS) { | |
efa46078 JM |
500 | os_memset(&event, 0, sizeof(event)); |
501 | if (len > 24 + sizeof(mgmt->u.assoc_resp)) { | |
502 | event.assoc_reject.resp_ies = | |
503 | (u8 *) mgmt->u.assoc_resp.variable; | |
504 | event.assoc_reject.resp_ies_len = | |
505 | len - 24 - sizeof(mgmt->u.assoc_resp); | |
506 | } | |
507 | event.assoc_reject.status_code = status; | |
508 | ||
509 | wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event); | |
c2a04078 JM |
510 | return; |
511 | } | |
512 | ||
513 | drv->associated = 1; | |
514 | os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN); | |
515 | ||
516 | os_memset(&event, 0, sizeof(event)); | |
517 | if (len > 24 + sizeof(mgmt->u.assoc_resp)) { | |
518 | event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable; | |
519 | event.assoc_info.resp_ies_len = | |
efa46078 | 520 | len - 24 - sizeof(mgmt->u.assoc_resp); |
c2a04078 JM |
521 | } |
522 | ||
4832ecd7 JM |
523 | event.assoc_info.freq = drv->assoc_freq; |
524 | ||
c2a04078 JM |
525 | wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event); |
526 | } | |
527 | ||
c1bb3e0a | 528 | |
da72a1c1 ZY |
529 | static void mlme_event_connect(struct wpa_driver_nl80211_data *drv, |
530 | enum nl80211_commands cmd, struct nlattr *status, | |
531 | struct nlattr *addr, struct nlattr *req_ie, | |
532 | struct nlattr *resp_ie) | |
533 | { | |
534 | union wpa_event_data event; | |
535 | ||
7da2c527 JM |
536 | if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) { |
537 | /* | |
538 | * Avoid reporting two association events that would confuse | |
539 | * the core code. | |
540 | */ | |
541 | wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) " | |
542 | "when using userspace SME", cmd); | |
543 | return; | |
544 | } | |
545 | ||
da72a1c1 ZY |
546 | os_memset(&event, 0, sizeof(event)); |
547 | if (cmd == NL80211_CMD_CONNECT && | |
548 | nla_get_u16(status) != WLAN_STATUS_SUCCESS) { | |
549 | if (resp_ie) { | |
550 | event.assoc_reject.resp_ies = nla_data(resp_ie); | |
551 | event.assoc_reject.resp_ies_len = nla_len(resp_ie); | |
552 | } | |
553 | event.assoc_reject.status_code = nla_get_u16(status); | |
554 | wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event); | |
555 | return; | |
556 | } | |
557 | ||
558 | drv->associated = 1; | |
559 | if (addr) | |
560 | os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN); | |
561 | ||
562 | if (req_ie) { | |
563 | event.assoc_info.req_ies = nla_data(req_ie); | |
564 | event.assoc_info.req_ies_len = nla_len(req_ie); | |
565 | } | |
566 | if (resp_ie) { | |
567 | event.assoc_info.resp_ies = nla_data(resp_ie); | |
568 | event.assoc_info.resp_ies_len = nla_len(resp_ie); | |
569 | } | |
570 | ||
571 | wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event); | |
572 | } | |
c2a04078 | 573 | |
c1bb3e0a | 574 | |
da1fb17c JM |
575 | static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv, |
576 | enum nl80211_commands cmd, struct nlattr *addr) | |
577 | { | |
578 | union wpa_event_data event; | |
579 | enum wpa_event_type ev; | |
580 | ||
581 | if (nla_len(addr) != ETH_ALEN) | |
582 | return; | |
583 | ||
584 | wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR, | |
585 | cmd, MAC2STR((u8 *) nla_data(addr))); | |
586 | ||
587 | if (cmd == NL80211_CMD_AUTHENTICATE) | |
588 | ev = EVENT_AUTH_TIMED_OUT; | |
589 | else if (cmd == NL80211_CMD_ASSOCIATE) | |
590 | ev = EVENT_ASSOC_TIMED_OUT; | |
591 | else | |
592 | return; | |
593 | ||
594 | os_memset(&event, 0, sizeof(event)); | |
595 | os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN); | |
596 | wpa_supplicant_event(drv->ctx, ev, &event); | |
597 | } | |
598 | ||
599 | ||
58f6fbe0 JM |
600 | static void mlme_event_action(struct wpa_driver_nl80211_data *drv, |
601 | struct nlattr *freq, const u8 *frame, size_t len) | |
602 | { | |
603 | const struct ieee80211_mgmt *mgmt; | |
604 | union wpa_event_data event; | |
605 | u16 fc, stype; | |
606 | ||
607 | mgmt = (const struct ieee80211_mgmt *) frame; | |
608 | if (len < 24) { | |
609 | wpa_printf(MSG_DEBUG, "nl80211: Too short action frame"); | |
610 | return; | |
611 | } | |
612 | ||
613 | fc = le_to_host16(mgmt->frame_control); | |
614 | stype = WLAN_FC_GET_STYPE(fc); | |
615 | ||
616 | os_memset(&event, 0, sizeof(event)); | |
617 | event.rx_action.da = mgmt->da; | |
618 | event.rx_action.sa = mgmt->sa; | |
619 | event.rx_action.bssid = mgmt->bssid; | |
620 | event.rx_action.category = mgmt->u.action.category; | |
621 | event.rx_action.data = &mgmt->u.action.category + 1; | |
622 | event.rx_action.len = frame + len - event.rx_action.data; | |
623 | if (freq) | |
624 | event.rx_action.freq = nla_get_u32(freq); | |
625 | wpa_supplicant_event(drv->ctx, EVENT_RX_ACTION, &event); | |
626 | } | |
627 | ||
628 | ||
629 | static void mlme_event_action_tx_status(struct wpa_driver_nl80211_data *drv, | |
630 | struct nlattr *cookie, const u8 *frame, | |
631 | size_t len, struct nlattr *ack) | |
632 | { | |
633 | union wpa_event_data event; | |
634 | const struct ieee80211_hdr *hdr; | |
635 | u16 fc; | |
636 | u64 cookie_val; | |
637 | ||
638 | if (!cookie) | |
639 | return; | |
640 | ||
641 | cookie_val = nla_get_u64(cookie); | |
642 | wpa_printf(MSG_DEBUG, "nl80211: Action TX status: cookie=0%llx%s", | |
643 | (long long unsigned int) cookie_val, | |
644 | cookie_val == drv->send_action_cookie ? | |
645 | " (match)" : " (unknown)"); | |
646 | if (cookie_val != drv->send_action_cookie) | |
647 | return; | |
648 | ||
649 | hdr = (const struct ieee80211_hdr *) frame; | |
650 | fc = le_to_host16(hdr->frame_control); | |
651 | ||
652 | os_memset(&event, 0, sizeof(event)); | |
653 | event.tx_status.type = WLAN_FC_GET_TYPE(fc); | |
654 | event.tx_status.stype = WLAN_FC_GET_STYPE(fc); | |
655 | event.tx_status.dst = hdr->addr1; | |
656 | event.tx_status.data = frame; | |
657 | event.tx_status.data_len = len; | |
658 | event.tx_status.ack = ack != NULL; | |
659 | wpa_supplicant_event(drv->ctx, EVENT_TX_STATUS, &event); | |
660 | } | |
661 | ||
662 | ||
0544b242 JM |
663 | static void mlme_event_deauth_disassoc(struct wpa_driver_nl80211_data *drv, |
664 | enum wpa_event_type type, | |
665 | const u8 *frame, size_t len) | |
666 | { | |
667 | const struct ieee80211_mgmt *mgmt; | |
668 | union wpa_event_data event; | |
669 | const u8 *bssid = NULL; | |
670 | u16 reason_code = 0; | |
671 | ||
672 | drv->associated = 0; | |
673 | os_memset(&event, 0, sizeof(event)); | |
674 | ||
675 | mgmt = (const struct ieee80211_mgmt *) frame; | |
676 | if (len >= 24) | |
677 | bssid = mgmt->bssid; | |
678 | /* Note: Same offset for Reason Code in both frame subtypes */ | |
679 | if (len >= 24 + sizeof(mgmt->u.deauth)) | |
680 | reason_code = le_to_host16(mgmt->u.deauth.reason_code); | |
681 | ||
682 | if (type == EVENT_DISASSOC) { | |
683 | event.disassoc_info.addr = bssid; | |
684 | event.disassoc_info.reason_code = reason_code; | |
685 | } else { | |
686 | event.deauth_info.addr = bssid; | |
687 | event.deauth_info.reason_code = reason_code; | |
688 | } | |
689 | ||
690 | wpa_supplicant_event(drv->ctx, type, &event); | |
691 | } | |
692 | ||
693 | ||
c2a04078 | 694 | static void mlme_event(struct wpa_driver_nl80211_data *drv, |
da1fb17c | 695 | enum nl80211_commands cmd, struct nlattr *frame, |
58f6fbe0 JM |
696 | struct nlattr *addr, struct nlattr *timed_out, |
697 | struct nlattr *freq, struct nlattr *ack, | |
698 | struct nlattr *cookie) | |
c2a04078 | 699 | { |
da1fb17c JM |
700 | if (timed_out && addr) { |
701 | mlme_timeout_event(drv, cmd, addr); | |
702 | return; | |
703 | } | |
704 | ||
c2a04078 JM |
705 | if (frame == NULL) { |
706 | wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame " | |
707 | "data", cmd); | |
708 | return; | |
709 | } | |
710 | ||
711 | wpa_printf(MSG_DEBUG, "nl80211: MLME event %d", cmd); | |
712 | wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame", | |
713 | nla_data(frame), nla_len(frame)); | |
714 | ||
715 | switch (cmd) { | |
716 | case NL80211_CMD_AUTHENTICATE: | |
717 | mlme_event_auth(drv, nla_data(frame), nla_len(frame)); | |
718 | break; | |
719 | case NL80211_CMD_ASSOCIATE: | |
720 | mlme_event_assoc(drv, nla_data(frame), nla_len(frame)); | |
721 | break; | |
722 | case NL80211_CMD_DEAUTHENTICATE: | |
0544b242 JM |
723 | mlme_event_deauth_disassoc(drv, EVENT_DEAUTH, |
724 | nla_data(frame), nla_len(frame)); | |
c2a04078 JM |
725 | break; |
726 | case NL80211_CMD_DISASSOCIATE: | |
0544b242 JM |
727 | mlme_event_deauth_disassoc(drv, EVENT_DISASSOC, |
728 | nla_data(frame), nla_len(frame)); | |
c2a04078 | 729 | break; |
58f6fbe0 JM |
730 | case NL80211_CMD_ACTION: |
731 | mlme_event_action(drv, freq, nla_data(frame), nla_len(frame)); | |
732 | break; | |
733 | case NL80211_CMD_ACTION_TX_STATUS: | |
734 | mlme_event_action_tx_status(drv, cookie, nla_data(frame), | |
735 | nla_len(frame), ack); | |
736 | break; | |
c2a04078 JM |
737 | default: |
738 | break; | |
739 | } | |
740 | } | |
741 | ||
742 | ||
35583f3f JM |
743 | static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv, |
744 | struct nlattr *tb[]) | |
745 | { | |
746 | union wpa_event_data data; | |
747 | ||
748 | wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure"); | |
749 | os_memset(&data, 0, sizeof(data)); | |
750 | if (tb[NL80211_ATTR_MAC]) { | |
751 | wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address", | |
752 | nla_data(tb[NL80211_ATTR_MAC]), | |
753 | nla_len(tb[NL80211_ATTR_MAC])); | |
ad1e68e6 | 754 | data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]); |
35583f3f JM |
755 | } |
756 | if (tb[NL80211_ATTR_KEY_SEQ]) { | |
757 | wpa_hexdump(MSG_DEBUG, "nl80211: TSC", | |
758 | nla_data(tb[NL80211_ATTR_KEY_SEQ]), | |
759 | nla_len(tb[NL80211_ATTR_KEY_SEQ])); | |
760 | } | |
761 | if (tb[NL80211_ATTR_KEY_TYPE]) { | |
762 | enum nl80211_key_type key_type = | |
763 | nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]); | |
764 | wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type); | |
765 | if (key_type == NL80211_KEYTYPE_PAIRWISE) | |
766 | data.michael_mic_failure.unicast = 1; | |
767 | } else | |
768 | data.michael_mic_failure.unicast = 1; | |
769 | ||
770 | if (tb[NL80211_ATTR_KEY_IDX]) { | |
771 | u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]); | |
772 | wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id); | |
773 | } | |
774 | ||
775 | wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE, &data); | |
776 | } | |
777 | ||
778 | ||
5cc4d64b JM |
779 | static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv, |
780 | struct nlattr *tb[]) | |
781 | { | |
782 | if (tb[NL80211_ATTR_MAC] == NULL) { | |
783 | wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined " | |
784 | "event"); | |
785 | return; | |
786 | } | |
787 | os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN); | |
788 | drv->associated = 1; | |
789 | wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined", | |
790 | MAC2STR(drv->bssid)); | |
791 | ||
792 | wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL); | |
793 | } | |
794 | ||
795 | ||
55777702 JM |
796 | static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv, |
797 | int cancel_event, struct nlattr *tb[]) | |
798 | { | |
799 | unsigned int freq, chan_type, duration; | |
800 | union wpa_event_data data; | |
801 | u64 cookie; | |
802 | ||
803 | if (tb[NL80211_ATTR_WIPHY_FREQ]) | |
804 | freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]); | |
805 | else | |
806 | freq = 0; | |
807 | ||
808 | if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) | |
809 | chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]); | |
810 | else | |
811 | chan_type = 0; | |
812 | ||
813 | if (tb[NL80211_ATTR_DURATION]) | |
814 | duration = nla_get_u32(tb[NL80211_ATTR_DURATION]); | |
815 | else | |
816 | duration = 0; | |
817 | ||
818 | if (tb[NL80211_ATTR_COOKIE]) | |
819 | cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]); | |
820 | else | |
821 | cookie = 0; | |
822 | ||
823 | wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d " | |
824 | "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))", | |
825 | cancel_event, freq, chan_type, duration, | |
826 | (long long unsigned int) cookie, | |
827 | cookie == drv->remain_on_chan_cookie ? "match" : "unknown"); | |
828 | ||
829 | if (cookie != drv->remain_on_chan_cookie) | |
830 | return; /* not for us */ | |
831 | ||
832 | drv->pending_remain_on_chan = !cancel_event; | |
833 | ||
834 | os_memset(&data, 0, sizeof(data)); | |
835 | data.remain_on_channel.freq = freq; | |
836 | data.remain_on_channel.duration = duration; | |
837 | wpa_supplicant_event(drv->ctx, cancel_event ? | |
838 | EVENT_CANCEL_REMAIN_ON_CHANNEL : | |
839 | EVENT_REMAIN_ON_CHANNEL, &data); | |
840 | } | |
841 | ||
842 | ||
8d923a4a JM |
843 | static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted, |
844 | struct nlattr *tb[]) | |
845 | { | |
846 | union wpa_event_data event; | |
847 | struct nlattr *nl; | |
848 | int rem; | |
849 | struct scan_info *info; | |
850 | #define MAX_REPORT_FREQS 50 | |
851 | int freqs[MAX_REPORT_FREQS]; | |
852 | int num_freqs = 0; | |
853 | ||
854 | os_memset(&event, 0, sizeof(event)); | |
855 | info = &event.scan_info; | |
856 | info->aborted = aborted; | |
857 | ||
858 | if (tb[NL80211_ATTR_SCAN_SSIDS]) { | |
859 | nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) { | |
860 | struct wpa_driver_scan_ssid *s = | |
861 | &info->ssids[info->num_ssids]; | |
862 | s->ssid = nla_data(nl); | |
863 | s->ssid_len = nla_len(nl); | |
864 | info->num_ssids++; | |
865 | if (info->num_ssids == WPAS_MAX_SCAN_SSIDS) | |
866 | break; | |
867 | } | |
868 | } | |
869 | if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) { | |
870 | nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem) | |
871 | { | |
872 | freqs[num_freqs] = nla_get_u32(nl); | |
873 | num_freqs++; | |
874 | if (num_freqs == MAX_REPORT_FREQS - 1) | |
875 | break; | |
876 | } | |
877 | info->freqs = freqs; | |
878 | info->num_freqs = num_freqs; | |
879 | } | |
880 | wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event); | |
881 | } | |
882 | ||
883 | ||
93910401 JM |
884 | static void nl80211_cqm_event(struct wpa_driver_nl80211_data *drv, |
885 | struct nlattr *tb[]) | |
886 | { | |
887 | static struct nla_policy cqm_policy[NL80211_ATTR_CQM_MAX + 1] = { | |
888 | [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 }, | |
889 | [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U8 }, | |
890 | [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 }, | |
891 | }; | |
892 | struct nlattr *cqm[NL80211_ATTR_CQM_MAX + 1]; | |
893 | enum nl80211_cqm_rssi_threshold_event event; | |
b625473c | 894 | union wpa_event_data ed; |
93910401 JM |
895 | |
896 | if (tb[NL80211_ATTR_CQM] == NULL || | |
897 | nla_parse_nested(cqm, NL80211_ATTR_CQM_MAX, tb[NL80211_ATTR_CQM], | |
898 | cqm_policy)) { | |
899 | wpa_printf(MSG_DEBUG, "nl80211: Ignore invalid CQM event"); | |
900 | return; | |
901 | } | |
902 | ||
903 | if (cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] == NULL) | |
904 | return; | |
905 | event = nla_get_u32(cqm[NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT]); | |
b625473c JM |
906 | |
907 | os_memset(&ed, 0, sizeof(ed)); | |
908 | ||
93910401 JM |
909 | if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH) { |
910 | wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor " | |
911 | "event: RSSI high"); | |
b625473c | 912 | ed.signal_change.above_threshold = 1; |
93910401 JM |
913 | } else if (event == NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW) { |
914 | wpa_printf(MSG_DEBUG, "nl80211: Connection quality monitor " | |
915 | "event: RSSI low"); | |
b625473c JM |
916 | ed.signal_change.above_threshold = 0; |
917 | } else | |
918 | return; | |
919 | ||
920 | wpa_supplicant_event(drv->ctx, EVENT_SIGNAL_CHANGE, &ed); | |
93910401 JM |
921 | } |
922 | ||
923 | ||
97865538 JM |
924 | static int process_event(struct nl_msg *msg, void *arg) |
925 | { | |
926 | struct wpa_driver_nl80211_data *drv = arg; | |
927 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
928 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
0544b242 | 929 | union wpa_event_data data; |
97865538 JM |
930 | |
931 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
932 | genlmsg_attrlen(gnlh, 0), NULL); | |
933 | ||
934 | if (tb[NL80211_ATTR_IFINDEX]) { | |
935 | int ifindex = nla_get_u32(tb[NL80211_ATTR_IFINDEX]); | |
936 | if (ifindex != drv->ifindex) { | |
937 | wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d)" | |
938 | " for foreign interface (ifindex %d)", | |
939 | gnlh->cmd, ifindex); | |
940 | return NL_SKIP; | |
941 | } | |
942 | } | |
943 | ||
ad1e68e6 JM |
944 | if (drv->ap_scan_as_station && |
945 | (gnlh->cmd == NL80211_CMD_NEW_SCAN_RESULTS || | |
946 | gnlh->cmd == NL80211_CMD_SCAN_ABORTED)) { | |
a2e40bb6 FF |
947 | wpa_driver_nl80211_set_mode(&drv->first_bss, |
948 | IEEE80211_MODE_AP); | |
ad1e68e6 JM |
949 | drv->ap_scan_as_station = 0; |
950 | } | |
951 | ||
97865538 | 952 | switch (gnlh->cmd) { |
d942a79e JM |
953 | case NL80211_CMD_TRIGGER_SCAN: |
954 | wpa_printf(MSG_DEBUG, "nl80211: Scan trigger"); | |
955 | break; | |
97865538 JM |
956 | case NL80211_CMD_NEW_SCAN_RESULTS: |
957 | wpa_printf(MSG_DEBUG, "nl80211: New scan results available"); | |
958 | drv->scan_complete_events = 1; | |
959 | eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, | |
960 | drv->ctx); | |
8d923a4a | 961 | send_scan_event(drv, 0, tb); |
97865538 JM |
962 | break; |
963 | case NL80211_CMD_SCAN_ABORTED: | |
964 | wpa_printf(MSG_DEBUG, "nl80211: Scan aborted"); | |
965 | /* | |
966 | * Need to indicate that scan results are available in order | |
967 | * not to make wpa_supplicant stop its scanning. | |
968 | */ | |
969 | eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, | |
970 | drv->ctx); | |
8d923a4a | 971 | send_scan_event(drv, 1, tb); |
97865538 | 972 | break; |
c2a04078 JM |
973 | case NL80211_CMD_AUTHENTICATE: |
974 | case NL80211_CMD_ASSOCIATE: | |
975 | case NL80211_CMD_DEAUTHENTICATE: | |
976 | case NL80211_CMD_DISASSOCIATE: | |
58f6fbe0 JM |
977 | case NL80211_CMD_ACTION: |
978 | case NL80211_CMD_ACTION_TX_STATUS: | |
da1fb17c | 979 | mlme_event(drv, gnlh->cmd, tb[NL80211_ATTR_FRAME], |
58f6fbe0 JM |
980 | tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT], |
981 | tb[NL80211_ATTR_WIPHY_FREQ], tb[NL80211_ATTR_ACK], | |
982 | tb[NL80211_ATTR_COOKIE]); | |
c2a04078 | 983 | break; |
da72a1c1 ZY |
984 | case NL80211_CMD_CONNECT: |
985 | case NL80211_CMD_ROAM: | |
986 | mlme_event_connect(drv, gnlh->cmd, | |
987 | tb[NL80211_ATTR_STATUS_CODE], | |
988 | tb[NL80211_ATTR_MAC], | |
989 | tb[NL80211_ATTR_REQ_IE], | |
990 | tb[NL80211_ATTR_RESP_IE]); | |
991 | break; | |
992 | case NL80211_CMD_DISCONNECT: | |
7da2c527 JM |
993 | if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) { |
994 | /* | |
995 | * Avoid reporting two disassociation events that could | |
996 | * confuse the core code. | |
997 | */ | |
998 | wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect " | |
999 | "event when using userspace SME"); | |
1000 | break; | |
1001 | } | |
da72a1c1 | 1002 | drv->associated = 0; |
0544b242 JM |
1003 | os_memset(&data, 0, sizeof(data)); |
1004 | if (tb[NL80211_ATTR_REASON_CODE]) | |
1005 | data.disassoc_info.reason_code = | |
1006 | nla_get_u16(tb[NL80211_ATTR_REASON_CODE]); | |
1007 | wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, &data); | |
da72a1c1 | 1008 | break; |
35583f3f JM |
1009 | case NL80211_CMD_MICHAEL_MIC_FAILURE: |
1010 | mlme_event_michael_mic_failure(drv, tb); | |
1011 | break; | |
5cc4d64b JM |
1012 | case NL80211_CMD_JOIN_IBSS: |
1013 | mlme_event_join_ibss(drv, tb); | |
1014 | break; | |
55777702 JM |
1015 | case NL80211_CMD_REMAIN_ON_CHANNEL: |
1016 | mlme_event_remain_on_channel(drv, 0, tb); | |
1017 | break; | |
1018 | case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL: | |
1019 | mlme_event_remain_on_channel(drv, 1, tb); | |
1020 | break; | |
93910401 JM |
1021 | case NL80211_CMD_NOTIFY_CQM: |
1022 | nl80211_cqm_event(drv, tb); | |
1023 | break; | |
97865538 | 1024 | default: |
c2a04078 JM |
1025 | wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event " |
1026 | "(cmd=%d)", gnlh->cmd); | |
97865538 JM |
1027 | break; |
1028 | } | |
1029 | ||
1030 | return NL_SKIP; | |
1031 | } | |
1032 | ||
1033 | ||
1034 | static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx, | |
1035 | void *sock_ctx) | |
1036 | { | |
1037 | struct nl_cb *cb; | |
1038 | struct wpa_driver_nl80211_data *drv = eloop_ctx; | |
1039 | ||
1040 | wpa_printf(MSG_DEBUG, "nl80211: Event message available"); | |
1041 | ||
1042 | cb = nl_cb_clone(drv->nl_cb); | |
1043 | if (!cb) | |
1044 | return; | |
1045 | nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL); | |
1046 | nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, process_event, drv); | |
335ce76b | 1047 | nl_recvmsgs(drv->nl_handle_event, cb); |
97865538 JM |
1048 | nl_cb_put(cb); |
1049 | } | |
1050 | ||
1051 | ||
6d158490 LR |
1052 | /** |
1053 | * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain | |
1054 | * @priv: driver_nl80211 private data | |
1055 | * @alpha2_arg: country to which to switch to | |
1056 | * Returns: 0 on success, -1 on failure | |
1057 | * | |
1058 | * This asks nl80211 to set the regulatory domain for given | |
1059 | * country ISO / IEC alpha2. | |
1060 | */ | |
1061 | static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg) | |
1062 | { | |
a2e40bb6 FF |
1063 | struct i802_bss *bss = priv; |
1064 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
6d158490 LR |
1065 | char alpha2[3]; |
1066 | struct nl_msg *msg; | |
1067 | ||
1068 | msg = nlmsg_alloc(); | |
1069 | if (!msg) | |
e785c2ba | 1070 | return -ENOMEM; |
6d158490 LR |
1071 | |
1072 | alpha2[0] = alpha2_arg[0]; | |
1073 | alpha2[1] = alpha2_arg[1]; | |
1074 | alpha2[2] = '\0'; | |
1075 | ||
1076 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, | |
1077 | 0, NL80211_CMD_REQ_SET_REG, 0); | |
1078 | ||
1079 | NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2); | |
1080 | if (send_and_recv_msgs(drv, msg, NULL, NULL)) | |
1081 | return -EINVAL; | |
1082 | return 0; | |
1083 | nla_put_failure: | |
1084 | return -EINVAL; | |
1085 | } | |
1086 | ||
1087 | ||
bbaf0837 | 1088 | #ifndef HOSTAPD |
80bc75f1 JM |
1089 | struct wiphy_info_data { |
1090 | int max_scan_ssids; | |
1581b38b | 1091 | int ap_supported; |
93d11400 ZY |
1092 | int auth_supported; |
1093 | int connect_supported; | |
80bc75f1 JM |
1094 | }; |
1095 | ||
1096 | ||
1097 | static int wiphy_info_handler(struct nl_msg *msg, void *arg) | |
1098 | { | |
1099 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
1100 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
1101 | struct wiphy_info_data *info = arg; | |
1102 | ||
1103 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
1104 | genlmsg_attrlen(gnlh, 0), NULL); | |
1105 | ||
1106 | if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]) | |
1107 | info->max_scan_ssids = | |
1108 | nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]); | |
1109 | ||
1581b38b JM |
1110 | if (tb[NL80211_ATTR_SUPPORTED_IFTYPES]) { |
1111 | struct nlattr *nl_mode; | |
1112 | int i; | |
1113 | nla_for_each_nested(nl_mode, | |
1114 | tb[NL80211_ATTR_SUPPORTED_IFTYPES], i) { | |
1115 | if (nl_mode->nla_type == NL80211_IFTYPE_AP) { | |
1116 | info->ap_supported = 1; | |
1117 | break; | |
1118 | } | |
1119 | } | |
1120 | } | |
1121 | ||
93d11400 ZY |
1122 | if (tb[NL80211_ATTR_SUPPORTED_COMMANDS]) { |
1123 | struct nlattr *nl_cmd; | |
1124 | int i; | |
1125 | ||
1126 | nla_for_each_nested(nl_cmd, | |
1127 | tb[NL80211_ATTR_SUPPORTED_COMMANDS], i) { | |
1128 | u32 cmd = nla_get_u32(nl_cmd); | |
1129 | if (cmd == NL80211_CMD_AUTHENTICATE) | |
1130 | info->auth_supported = 1; | |
1131 | else if (cmd == NL80211_CMD_CONNECT) | |
1132 | info->connect_supported = 1; | |
1133 | } | |
1134 | } | |
1135 | ||
80bc75f1 JM |
1136 | return NL_SKIP; |
1137 | } | |
1138 | ||
1139 | ||
1140 | static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv, | |
1141 | struct wiphy_info_data *info) | |
1142 | { | |
1143 | struct nl_msg *msg; | |
1144 | ||
1145 | os_memset(info, 0, sizeof(*info)); | |
1146 | msg = nlmsg_alloc(); | |
1147 | if (!msg) | |
1148 | return -1; | |
1149 | ||
1150 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, | |
1151 | 0, NL80211_CMD_GET_WIPHY, 0); | |
1152 | ||
a2e40bb6 | 1153 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->first_bss.ifindex); |
80bc75f1 JM |
1154 | |
1155 | if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info) == 0) | |
1156 | return 0; | |
1157 | msg = NULL; | |
1158 | nla_put_failure: | |
1159 | nlmsg_free(msg); | |
1160 | return -1; | |
1161 | } | |
1162 | ||
1163 | ||
93d11400 | 1164 | static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv) |
80bc75f1 JM |
1165 | { |
1166 | struct wiphy_info_data info; | |
1167 | if (wpa_driver_nl80211_get_info(drv, &info)) | |
93d11400 | 1168 | return -1; |
80bc75f1 | 1169 | drv->has_capability = 1; |
1b2a72e8 JM |
1170 | /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */ |
1171 | drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA | | |
1172 | WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK | | |
1173 | WPA_DRIVER_CAPA_KEY_MGMT_WPA2 | | |
1174 | WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK; | |
1175 | drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 | | |
1176 | WPA_DRIVER_CAPA_ENC_WEP104 | | |
1177 | WPA_DRIVER_CAPA_ENC_TKIP | | |
1178 | WPA_DRIVER_CAPA_ENC_CCMP; | |
291b6068 JM |
1179 | drv->capa.auth = WPA_DRIVER_AUTH_OPEN | |
1180 | WPA_DRIVER_AUTH_SHARED | | |
1181 | WPA_DRIVER_AUTH_LEAP; | |
1b2a72e8 | 1182 | |
80bc75f1 | 1183 | drv->capa.max_scan_ssids = info.max_scan_ssids; |
1581b38b JM |
1184 | if (info.ap_supported) |
1185 | drv->capa.flags |= WPA_DRIVER_FLAGS_AP; | |
93d11400 ZY |
1186 | |
1187 | if (info.auth_supported) | |
1188 | drv->capa.flags |= WPA_DRIVER_FLAGS_SME; | |
1189 | else if (!info.connect_supported) { | |
1190 | wpa_printf(MSG_INFO, "nl80211: Driver does not support " | |
1191 | "authentication/association or connect commands"); | |
1192 | return -1; | |
1193 | } | |
1194 | ||
0194fedb | 1195 | drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE; |
814782b9 | 1196 | drv->capa.max_remain_on_chan = 5000; |
0194fedb | 1197 | |
93d11400 | 1198 | return 0; |
80bc75f1 | 1199 | } |
bbaf0837 | 1200 | #endif /* HOSTAPD */ |
80bc75f1 JM |
1201 | |
1202 | ||
9fff9fdc JM |
1203 | static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv, |
1204 | void *ctx) | |
3f5285e8 | 1205 | { |
9fff9fdc | 1206 | int ret; |
3f5285e8 | 1207 | |
9fff9fdc | 1208 | /* Initialize generic netlink and nl80211 */ |
3f5285e8 JM |
1209 | |
1210 | drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT); | |
1211 | if (drv->nl_cb == NULL) { | |
1212 | wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink " | |
1213 | "callbacks"); | |
1214 | goto err1; | |
1215 | } | |
1216 | ||
1217 | drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb); | |
1218 | if (drv->nl_handle == NULL) { | |
1219 | wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink " | |
1220 | "callbacks"); | |
1221 | goto err2; | |
1222 | } | |
1223 | ||
335ce76b JM |
1224 | drv->nl_handle_event = nl_handle_alloc_cb(drv->nl_cb); |
1225 | if (drv->nl_handle_event == NULL) { | |
1226 | wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink " | |
1227 | "callbacks (event)"); | |
1228 | goto err2b; | |
1229 | } | |
1230 | ||
3f5285e8 JM |
1231 | if (genl_connect(drv->nl_handle)) { |
1232 | wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic " | |
1233 | "netlink"); | |
1234 | goto err3; | |
1235 | } | |
1236 | ||
335ce76b JM |
1237 | if (genl_connect(drv->nl_handle_event)) { |
1238 | wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic " | |
1239 | "netlink (event)"); | |
1240 | goto err3; | |
1241 | } | |
1242 | ||
9fff9fdc JM |
1243 | #ifdef CONFIG_LIBNL20 |
1244 | if (genl_ctrl_alloc_cache(drv->nl_handle, &drv->nl_cache) < 0) { | |
1245 | wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic " | |
1246 | "netlink cache"); | |
1247 | goto err3; | |
1248 | } | |
335ce76b JM |
1249 | if (genl_ctrl_alloc_cache(drv->nl_handle_event, &drv->nl_cache_event) < |
1250 | 0) { | |
1251 | wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic " | |
1252 | "netlink cache (event)"); | |
1253 | goto err3b; | |
1254 | } | |
9fff9fdc | 1255 | #else /* CONFIG_LIBNL20 */ |
3f5285e8 JM |
1256 | drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle); |
1257 | if (drv->nl_cache == NULL) { | |
1258 | wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic " | |
1259 | "netlink cache"); | |
1260 | goto err3; | |
1261 | } | |
335ce76b JM |
1262 | drv->nl_cache_event = genl_ctrl_alloc_cache(drv->nl_handle_event); |
1263 | if (drv->nl_cache_event == NULL) { | |
1264 | wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic " | |
1265 | "netlink cache (event)"); | |
1266 | goto err3b; | |
1267 | } | |
9fff9fdc JM |
1268 | #endif /* CONFIG_LIBNL20 */ |
1269 | ||
3f5285e8 JM |
1270 | drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211"); |
1271 | if (drv->nl80211 == NULL) { | |
1272 | wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not " | |
1273 | "found"); | |
1274 | goto err4; | |
1275 | } | |
1276 | ||
97865538 JM |
1277 | ret = nl_get_multicast_id(drv, "nl80211", "scan"); |
1278 | if (ret >= 0) | |
335ce76b | 1279 | ret = nl_socket_add_membership(drv->nl_handle_event, ret); |
97865538 JM |
1280 | if (ret < 0) { |
1281 | wpa_printf(MSG_ERROR, "nl80211: Could not add multicast " | |
1282 | "membership for scan events: %d (%s)", | |
1283 | ret, strerror(-ret)); | |
1284 | goto err4; | |
1285 | } | |
c2a04078 JM |
1286 | |
1287 | ret = nl_get_multicast_id(drv, "nl80211", "mlme"); | |
1288 | if (ret >= 0) | |
335ce76b | 1289 | ret = nl_socket_add_membership(drv->nl_handle_event, ret); |
c2a04078 JM |
1290 | if (ret < 0) { |
1291 | wpa_printf(MSG_ERROR, "nl80211: Could not add multicast " | |
1292 | "membership for mlme events: %d (%s)", | |
1293 | ret, strerror(-ret)); | |
1294 | goto err4; | |
1295 | } | |
c2a04078 | 1296 | |
335ce76b | 1297 | eloop_register_read_sock(nl_socket_get_fd(drv->nl_handle_event), |
97865538 JM |
1298 | wpa_driver_nl80211_event_receive, drv, ctx); |
1299 | ||
9fff9fdc JM |
1300 | return 0; |
1301 | ||
1302 | err4: | |
335ce76b JM |
1303 | nl_cache_free(drv->nl_cache_event); |
1304 | err3b: | |
9fff9fdc JM |
1305 | nl_cache_free(drv->nl_cache); |
1306 | err3: | |
335ce76b JM |
1307 | nl_handle_destroy(drv->nl_handle_event); |
1308 | err2b: | |
9fff9fdc JM |
1309 | nl_handle_destroy(drv->nl_handle); |
1310 | err2: | |
1311 | nl_cb_put(drv->nl_cb); | |
1312 | err1: | |
1313 | return -1; | |
1314 | } | |
1315 | ||
1316 | ||
1317 | /** | |
1318 | * wpa_driver_nl80211_init - Initialize nl80211 driver interface | |
1319 | * @ctx: context to be used when calling wpa_supplicant functions, | |
1320 | * e.g., wpa_supplicant_event() | |
1321 | * @ifname: interface name, e.g., wlan0 | |
1322 | * Returns: Pointer to private data, %NULL on failure | |
1323 | */ | |
1324 | static void * wpa_driver_nl80211_init(void *ctx, const char *ifname) | |
1325 | { | |
9fff9fdc | 1326 | struct wpa_driver_nl80211_data *drv; |
08063178 | 1327 | struct netlink_config *cfg; |
a2e40bb6 | 1328 | struct i802_bss *bss; |
9fff9fdc JM |
1329 | |
1330 | drv = os_zalloc(sizeof(*drv)); | |
1331 | if (drv == NULL) | |
1332 | return NULL; | |
1333 | drv->ctx = ctx; | |
a2e40bb6 FF |
1334 | bss = &drv->first_bss; |
1335 | bss->drv = drv; | |
1336 | os_strlcpy(bss->ifname, ifname, sizeof(bss->ifname)); | |
9fff9fdc JM |
1337 | drv->monitor_ifidx = -1; |
1338 | drv->monitor_sock = -1; | |
bbaf0837 | 1339 | drv->ioctl_sock = -1; |
9fff9fdc | 1340 | |
bbaf0837 JM |
1341 | if (wpa_driver_nl80211_init_nl(drv, ctx)) { |
1342 | os_free(drv); | |
1343 | return NULL; | |
1344 | } | |
9fff9fdc | 1345 | |
3f5285e8 JM |
1346 | drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0); |
1347 | if (drv->ioctl_sock < 0) { | |
1348 | perror("socket(PF_INET,SOCK_DGRAM)"); | |
bbaf0837 | 1349 | goto failed; |
3f5285e8 JM |
1350 | } |
1351 | ||
08063178 JM |
1352 | cfg = os_zalloc(sizeof(*cfg)); |
1353 | if (cfg == NULL) | |
1354 | goto failed; | |
1355 | cfg->ctx = drv; | |
1356 | cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink; | |
1357 | cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink; | |
1358 | drv->netlink = netlink_init(cfg); | |
1359 | if (drv->netlink == NULL) { | |
1360 | os_free(cfg); | |
1361 | goto failed; | |
1362 | } | |
1363 | if (wpa_driver_nl80211_finish_drv_init(drv)) | |
bbaf0837 | 1364 | goto failed; |
7524cfb1 | 1365 | |
a2e40bb6 | 1366 | return bss; |
7524cfb1 | 1367 | |
bbaf0837 | 1368 | failed: |
08063178 | 1369 | netlink_deinit(drv->netlink); |
bbaf0837 JM |
1370 | if (drv->ioctl_sock >= 0) |
1371 | close(drv->ioctl_sock); | |
1372 | ||
7524cfb1 | 1373 | genl_family_put(drv->nl80211); |
7524cfb1 | 1374 | nl_cache_free(drv->nl_cache); |
7524cfb1 | 1375 | nl_handle_destroy(drv->nl_handle); |
7524cfb1 | 1376 | nl_cb_put(drv->nl_cb); |
05ba8690 | 1377 | eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_handle_event)); |
bbaf0837 | 1378 | |
7524cfb1 JM |
1379 | os_free(drv); |
1380 | return NULL; | |
1381 | } | |
1382 | ||
1383 | ||
58f6fbe0 JM |
1384 | static int nl80211_register_action_frame(struct wpa_driver_nl80211_data *drv, |
1385 | const u8 *match, size_t match_len) | |
1386 | { | |
1387 | struct nl_msg *msg; | |
1388 | int ret = -1; | |
1389 | ||
1390 | msg = nlmsg_alloc(); | |
1391 | if (!msg) | |
1392 | return -1; | |
1393 | ||
1394 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, | |
1395 | NL80211_CMD_REGISTER_ACTION, 0); | |
1396 | ||
1397 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); | |
1398 | NLA_PUT(msg, NL80211_ATTR_FRAME_MATCH, match_len, match); | |
1399 | ||
1400 | ret = send_and_recv(drv, drv->nl_handle_event, msg, NULL, NULL); | |
1401 | msg = NULL; | |
1402 | if (ret) { | |
1403 | wpa_printf(MSG_DEBUG, "nl80211: Register Action command " | |
1404 | "failed: ret=%d (%s)", ret, strerror(-ret)); | |
1405 | wpa_hexdump(MSG_DEBUG, "nl80211: Register Action match", | |
1406 | match, match_len); | |
1407 | goto nla_put_failure; | |
1408 | } | |
1409 | ret = 0; | |
1410 | nla_put_failure: | |
1411 | nlmsg_free(msg); | |
1412 | return ret; | |
1413 | } | |
1414 | ||
1415 | ||
1416 | static int nl80211_register_action_frames(struct wpa_driver_nl80211_data *drv) | |
1417 | { | |
7b90c16a JM |
1418 | /* FT Action frames */ |
1419 | if (nl80211_register_action_frame(drv, (u8 *) "\x06", 1) < 0) | |
1420 | return -1; | |
1421 | else | |
1422 | drv->capa.key_mgmt |= WPA_DRIVER_CAPA_KEY_MGMT_FT | | |
1423 | WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK; | |
1424 | ||
58f6fbe0 JM |
1425 | return 0; |
1426 | } | |
1427 | ||
1428 | ||
362f781e | 1429 | static int |
7524cfb1 JM |
1430 | wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv) |
1431 | { | |
a2e40bb6 FF |
1432 | struct i802_bss *bss = &drv->first_bss; |
1433 | ||
1434 | drv->ifindex = if_nametoindex(bss->ifname); | |
1435 | drv->first_bss.ifindex = drv->ifindex; | |
a87c9d96 | 1436 | |
bbaf0837 | 1437 | #ifndef HOSTAPD |
a2e40bb6 | 1438 | if (wpa_driver_nl80211_set_mode(bss, IEEE80211_MODE_INFRA) < 0) { |
a87c9d96 JM |
1439 | wpa_printf(MSG_DEBUG, "nl80211: Could not configure driver to " |
1440 | "use managed mode"); | |
1441 | } | |
1442 | ||
a2e40bb6 | 1443 | if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 1)) { |
34f2f814 | 1444 | wpa_printf(MSG_ERROR, "Could not set interface '%s' UP", |
a2e40bb6 | 1445 | bss->ifname); |
362f781e JM |
1446 | return -1; |
1447 | } | |
3f5285e8 | 1448 | |
93d11400 ZY |
1449 | if (wpa_driver_nl80211_capa(drv)) |
1450 | return -1; | |
80bc75f1 | 1451 | |
08063178 | 1452 | netlink_send_oper_ifla(drv->netlink, drv->ifindex, |
e2d02c29 | 1453 | 1, IF_OPER_DORMANT); |
bbaf0837 | 1454 | #endif /* HOSTAPD */ |
362f781e | 1455 | |
7b90c16a JM |
1456 | if (nl80211_register_action_frames(drv) < 0) { |
1457 | wpa_printf(MSG_DEBUG, "nl80211: Failed to register Action " | |
1458 | "frame processing - ignore for now"); | |
1459 | /* | |
1460 | * Older kernel versions did not support this, so ignore the | |
1461 | * error for now. Some functionality may not be available | |
1462 | * because of this. | |
1463 | */ | |
1464 | } | |
58f6fbe0 | 1465 | |
362f781e | 1466 | return 0; |
3f5285e8 JM |
1467 | } |
1468 | ||
1469 | ||
8a27af5c JM |
1470 | static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv) |
1471 | { | |
1472 | struct nl_msg *msg; | |
1473 | ||
1474 | msg = nlmsg_alloc(); | |
1475 | if (!msg) | |
1476 | return -ENOMEM; | |
1477 | ||
1478 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, | |
1479 | 0, NL80211_CMD_DEL_BEACON, 0); | |
1480 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); | |
1481 | ||
1482 | return send_and_recv_msgs(drv, msg, NULL, NULL); | |
1483 | nla_put_failure: | |
1484 | return -ENOBUFS; | |
1485 | } | |
8a27af5c JM |
1486 | |
1487 | ||
3f5285e8 | 1488 | /** |
7e5ba1b9 JM |
1489 | * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface |
1490 | * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init() | |
3f5285e8 JM |
1491 | * |
1492 | * Shut down driver interface and processing of driver events. Free | |
1493 | * private data buffer if one was allocated in wpa_driver_nl80211_init(). | |
1494 | */ | |
7e5ba1b9 | 1495 | static void wpa_driver_nl80211_deinit(void *priv) |
3f5285e8 | 1496 | { |
a2e40bb6 FF |
1497 | struct i802_bss *bss = priv; |
1498 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
3f5285e8 | 1499 | |
94627f6c | 1500 | if (drv->added_if_into_bridge) { |
a2e40bb6 | 1501 | if (linux_br_del_if(drv->ioctl_sock, drv->brname, bss->ifname) |
94627f6c JM |
1502 | < 0) |
1503 | wpa_printf(MSG_INFO, "nl80211: Failed to remove " | |
1504 | "interface %s from bridge %s: %s", | |
a2e40bb6 | 1505 | bss->ifname, drv->brname, strerror(errno)); |
94627f6c JM |
1506 | } |
1507 | if (drv->added_bridge) { | |
1508 | if (linux_br_del(drv->ioctl_sock, drv->brname) < 0) | |
1509 | wpa_printf(MSG_INFO, "nl80211: Failed to remove " | |
1510 | "bridge %s: %s", | |
1511 | drv->brname, strerror(errno)); | |
1512 | } | |
1513 | ||
460456f8 | 1514 | nl80211_remove_monitor_interface(drv); |
8a27af5c JM |
1515 | |
1516 | if (drv->nlmode == NL80211_IFTYPE_AP) | |
1517 | wpa_driver_nl80211_del_beacon(drv); | |
0915d02c | 1518 | |
bbaf0837 JM |
1519 | #ifdef HOSTAPD |
1520 | if (drv->last_freq_ht) { | |
1521 | /* Clear HT flags from the driver */ | |
1522 | struct hostapd_freq_params freq; | |
1523 | os_memset(&freq, 0, sizeof(freq)); | |
1524 | freq.freq = drv->last_freq; | |
1525 | i802_set_freq(priv, &freq); | |
1526 | } | |
1527 | ||
bbaf0837 JM |
1528 | if (drv->eapol_sock >= 0) { |
1529 | eloop_unregister_read_sock(drv->eapol_sock); | |
1530 | close(drv->eapol_sock); | |
1531 | } | |
1532 | ||
1533 | if (drv->if_indices != drv->default_if_indices) | |
1534 | os_free(drv->if_indices); | |
1b648c7e | 1535 | #endif /* HOSTAPD */ |
3f5285e8 | 1536 | |
4e5cb1a3 JM |
1537 | if (drv->disable_11b_rates) |
1538 | nl80211_disable_11b_rates(drv, drv->ifindex, 0); | |
1539 | ||
08063178 JM |
1540 | netlink_send_oper_ifla(drv->netlink, drv->ifindex, 0, IF_OPER_UP); |
1541 | netlink_deinit(drv->netlink); | |
3f5285e8 | 1542 | |
bbaf0837 JM |
1543 | eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx); |
1544 | ||
a2e40bb6 FF |
1545 | (void) linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 0); |
1546 | wpa_driver_nl80211_set_mode(bss, IEEE80211_MODE_INFRA); | |
3f5285e8 | 1547 | |
bbaf0837 JM |
1548 | if (drv->ioctl_sock >= 0) |
1549 | close(drv->ioctl_sock); | |
3f5285e8 | 1550 | |
335ce76b | 1551 | eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_handle_event)); |
3f5285e8 JM |
1552 | genl_family_put(drv->nl80211); |
1553 | nl_cache_free(drv->nl_cache); | |
335ce76b | 1554 | nl_cache_free(drv->nl_cache_event); |
3f5285e8 | 1555 | nl_handle_destroy(drv->nl_handle); |
335ce76b | 1556 | nl_handle_destroy(drv->nl_handle_event); |
3f5285e8 JM |
1557 | nl_cb_put(drv->nl_cb); |
1558 | ||
504e905c JM |
1559 | eloop_cancel_timeout(wpa_driver_nl80211_probe_req_report_timeout, |
1560 | drv, NULL); | |
1561 | ||
3812464c JM |
1562 | os_free(drv->filter_ssids); |
1563 | ||
3f5285e8 JM |
1564 | os_free(drv); |
1565 | } | |
1566 | ||
1567 | ||
1568 | /** | |
1569 | * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion | |
ad1e68e6 | 1570 | * @eloop_ctx: Driver private data |
3f5285e8 JM |
1571 | * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init() |
1572 | * | |
1573 | * This function can be used as registered timeout when starting a scan to | |
1574 | * generate a scan completed event if the driver does not report this. | |
1575 | */ | |
1576 | static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx) | |
1577 | { | |
ad1e68e6 JM |
1578 | struct wpa_driver_nl80211_data *drv = eloop_ctx; |
1579 | if (drv->ap_scan_as_station) { | |
a2e40bb6 FF |
1580 | wpa_driver_nl80211_set_mode(&drv->first_bss, |
1581 | IEEE80211_MODE_AP); | |
ad1e68e6 JM |
1582 | drv->ap_scan_as_station = 0; |
1583 | } | |
3f5285e8 JM |
1584 | wpa_printf(MSG_DEBUG, "Scan timeout - try to get results"); |
1585 | wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL); | |
1586 | } | |
1587 | ||
1588 | ||
1589 | /** | |
1590 | * wpa_driver_nl80211_scan - Request the driver to initiate scan | |
ad1e68e6 | 1591 | * @priv: Pointer to private driver data from wpa_driver_nl80211_init() |
6a1063e0 | 1592 | * @params: Scan parameters |
3f5285e8 JM |
1593 | * Returns: 0 on success, -1 on failure |
1594 | */ | |
6a1063e0 JM |
1595 | static int wpa_driver_nl80211_scan(void *priv, |
1596 | struct wpa_driver_scan_params *params) | |
3f5285e8 | 1597 | { |
a2e40bb6 FF |
1598 | struct i802_bss *bss = priv; |
1599 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
3f5285e8 | 1600 | int ret = 0, timeout; |
d3a98225 | 1601 | struct nl_msg *msg, *ssids, *freqs; |
6a1063e0 | 1602 | size_t i; |
3f5285e8 | 1603 | |
0e75527f JM |
1604 | msg = nlmsg_alloc(); |
1605 | ssids = nlmsg_alloc(); | |
d3a98225 JM |
1606 | freqs = nlmsg_alloc(); |
1607 | if (!msg || !ssids || !freqs) { | |
0e75527f JM |
1608 | nlmsg_free(msg); |
1609 | nlmsg_free(ssids); | |
d3a98225 | 1610 | nlmsg_free(freqs); |
3f5285e8 JM |
1611 | return -1; |
1612 | } | |
1613 | ||
3812464c JM |
1614 | os_free(drv->filter_ssids); |
1615 | drv->filter_ssids = params->filter_ssids; | |
1616 | params->filter_ssids = NULL; | |
1617 | drv->num_filter_ssids = params->num_filter_ssids; | |
1618 | ||
0e75527f JM |
1619 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, |
1620 | NL80211_CMD_TRIGGER_SCAN, 0); | |
1621 | ||
1622 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); | |
3f5285e8 | 1623 | |
6a1063e0 | 1624 | for (i = 0; i < params->num_ssids; i++) { |
9fad706c JM |
1625 | wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan SSID", |
1626 | params->ssids[i].ssid, | |
1627 | params->ssids[i].ssid_len); | |
6a1063e0 JM |
1628 | NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len, |
1629 | params->ssids[i].ssid); | |
3f5285e8 | 1630 | } |
6a1063e0 JM |
1631 | if (params->num_ssids) |
1632 | nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids); | |
3f5285e8 | 1633 | |
d173df52 | 1634 | if (params->extra_ies) { |
9fad706c JM |
1635 | wpa_hexdump_ascii(MSG_MSGDUMP, "nl80211: Scan extra IEs", |
1636 | params->extra_ies, params->extra_ies_len); | |
d173df52 JM |
1637 | NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len, |
1638 | params->extra_ies); | |
1639 | } | |
1640 | ||
d3a98225 | 1641 | if (params->freqs) { |
9fad706c JM |
1642 | for (i = 0; params->freqs[i]; i++) { |
1643 | wpa_printf(MSG_MSGDUMP, "nl80211: Scan frequency %u " | |
1644 | "MHz", params->freqs[i]); | |
d3a98225 | 1645 | NLA_PUT_U32(freqs, i + 1, params->freqs[i]); |
9fad706c | 1646 | } |
d3a98225 JM |
1647 | nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs); |
1648 | } | |
1649 | ||
0e75527f JM |
1650 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); |
1651 | msg = NULL; | |
1652 | if (ret) { | |
1653 | wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d " | |
1654 | "(%s)", ret, strerror(-ret)); | |
ad1e68e6 JM |
1655 | #ifdef HOSTAPD |
1656 | if (drv->nlmode == NL80211_IFTYPE_AP) { | |
1657 | /* | |
1658 | * mac80211 does not allow scan requests in AP mode, so | |
1659 | * try to do this in station mode. | |
1660 | */ | |
a2e40bb6 | 1661 | if (wpa_driver_nl80211_set_mode(bss, |
ad1e68e6 JM |
1662 | IEEE80211_MODE_INFRA)) |
1663 | goto nla_put_failure; | |
1664 | ||
1665 | if (wpa_driver_nl80211_scan(drv, params)) { | |
a2e40bb6 | 1666 | wpa_driver_nl80211_set_mode(bss, |
ad1e68e6 JM |
1667 | IEEE80211_MODE_AP); |
1668 | goto nla_put_failure; | |
1669 | } | |
1670 | ||
1671 | /* Restore AP mode when processing scan results */ | |
1672 | drv->ap_scan_as_station = 1; | |
1673 | ret = 0; | |
1674 | } else | |
1675 | goto nla_put_failure; | |
1676 | #else /* HOSTAPD */ | |
0e75527f | 1677 | goto nla_put_failure; |
ad1e68e6 | 1678 | #endif /* HOSTAPD */ |
3f5285e8 JM |
1679 | } |
1680 | ||
1681 | /* Not all drivers generate "scan completed" wireless event, so try to | |
1682 | * read results after a timeout. */ | |
0e75527f | 1683 | timeout = 10; |
3f5285e8 JM |
1684 | if (drv->scan_complete_events) { |
1685 | /* | |
d173df52 JM |
1686 | * The driver seems to deliver events to notify when scan is |
1687 | * complete, so use longer timeout to avoid race conditions | |
1688 | * with scanning and following association request. | |
3f5285e8 JM |
1689 | */ |
1690 | timeout = 30; | |
1691 | } | |
1692 | wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d " | |
1693 | "seconds", ret, timeout); | |
1694 | eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx); | |
0e75527f JM |
1695 | eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout, |
1696 | drv, drv->ctx); | |
3f5285e8 | 1697 | |
0e75527f JM |
1698 | nla_put_failure: |
1699 | nlmsg_free(ssids); | |
1700 | nlmsg_free(msg); | |
d3a98225 | 1701 | nlmsg_free(freqs); |
3f5285e8 JM |
1702 | return ret; |
1703 | } | |
1704 | ||
1705 | ||
3812464c JM |
1706 | static const u8 * nl80211_get_ie(const u8 *ies, size_t ies_len, u8 ie) |
1707 | { | |
1708 | const u8 *end, *pos; | |
1709 | ||
1710 | if (ies == NULL) | |
1711 | return NULL; | |
1712 | ||
1713 | pos = ies; | |
1714 | end = ies + ies_len; | |
1715 | ||
1716 | while (pos + 1 < end) { | |
1717 | if (pos + 2 + pos[1] > end) | |
1718 | break; | |
1719 | if (pos[0] == ie) | |
1720 | return pos; | |
1721 | pos += 2 + pos[1]; | |
1722 | } | |
1723 | ||
1724 | return NULL; | |
1725 | } | |
1726 | ||
1727 | ||
1728 | static int nl80211_scan_filtered(struct wpa_driver_nl80211_data *drv, | |
1729 | const u8 *ie, size_t ie_len) | |
1730 | { | |
1731 | const u8 *ssid; | |
1732 | size_t i; | |
1733 | ||
1734 | if (drv->filter_ssids == NULL) | |
1735 | return 0; | |
1736 | ||
1737 | ssid = nl80211_get_ie(ie, ie_len, WLAN_EID_SSID); | |
1738 | if (ssid == NULL) | |
1739 | return 1; | |
1740 | ||
1741 | for (i = 0; i < drv->num_filter_ssids; i++) { | |
1742 | if (ssid[1] == drv->filter_ssids[i].ssid_len && | |
1743 | os_memcmp(ssid + 2, drv->filter_ssids[i].ssid, ssid[1]) == | |
1744 | 0) | |
1745 | return 0; | |
1746 | } | |
1747 | ||
1748 | return 1; | |
1749 | } | |
1750 | ||
1751 | ||
1752 | struct nl80211_bss_info_arg { | |
1753 | struct wpa_driver_nl80211_data *drv; | |
1754 | struct wpa_scan_results *res; | |
1755 | }; | |
1756 | ||
b3db1e1c | 1757 | static int bss_info_handler(struct nl_msg *msg, void *arg) |
3f5285e8 | 1758 | { |
b3db1e1c JM |
1759 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; |
1760 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
1761 | struct nlattr *bss[NL80211_BSS_MAX + 1]; | |
1762 | static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = { | |
1763 | [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC }, | |
1764 | [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 }, | |
1765 | [NL80211_BSS_TSF] = { .type = NLA_U64 }, | |
1766 | [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 }, | |
1767 | [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 }, | |
1768 | [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC }, | |
1769 | [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 }, | |
1770 | [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 }, | |
e6b8efeb | 1771 | [NL80211_BSS_STATUS] = { .type = NLA_U32 }, |
b3ad11bb | 1772 | [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 }, |
8c090654 | 1773 | [NL80211_BSS_BEACON_IES] = { .type = NLA_UNSPEC }, |
b3db1e1c | 1774 | }; |
3812464c JM |
1775 | struct nl80211_bss_info_arg *_arg = arg; |
1776 | struct wpa_scan_results *res = _arg->res; | |
3f5285e8 JM |
1777 | struct wpa_scan_res **tmp; |
1778 | struct wpa_scan_res *r; | |
8c090654 JM |
1779 | const u8 *ie, *beacon_ie; |
1780 | size_t ie_len, beacon_ie_len; | |
1781 | u8 *pos; | |
3f5285e8 | 1782 | |
b3db1e1c JM |
1783 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), |
1784 | genlmsg_attrlen(gnlh, 0), NULL); | |
1785 | if (!tb[NL80211_ATTR_BSS]) | |
1786 | return NL_SKIP; | |
1787 | if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS], | |
1788 | bss_policy)) | |
1789 | return NL_SKIP; | |
1790 | if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) { | |
1791 | ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]); | |
1792 | ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]); | |
1793 | } else { | |
1794 | ie = NULL; | |
1795 | ie_len = 0; | |
1796 | } | |
8c090654 JM |
1797 | if (bss[NL80211_BSS_BEACON_IES]) { |
1798 | beacon_ie = nla_data(bss[NL80211_BSS_BEACON_IES]); | |
1799 | beacon_ie_len = nla_len(bss[NL80211_BSS_BEACON_IES]); | |
1800 | } else { | |
1801 | beacon_ie = NULL; | |
1802 | beacon_ie_len = 0; | |
1803 | } | |
3f5285e8 | 1804 | |
3812464c JM |
1805 | if (nl80211_scan_filtered(_arg->drv, ie ? ie : beacon_ie, |
1806 | ie ? ie_len : beacon_ie_len)) | |
1807 | return NL_SKIP; | |
1808 | ||
8c090654 | 1809 | r = os_zalloc(sizeof(*r) + ie_len + beacon_ie_len); |
3f5285e8 | 1810 | if (r == NULL) |
b3db1e1c JM |
1811 | return NL_SKIP; |
1812 | if (bss[NL80211_BSS_BSSID]) | |
1813 | os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]), | |
1814 | ETH_ALEN); | |
1815 | if (bss[NL80211_BSS_FREQUENCY]) | |
1816 | r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]); | |
1817 | if (bss[NL80211_BSS_BEACON_INTERVAL]) | |
1818 | r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]); | |
1819 | if (bss[NL80211_BSS_CAPABILITY]) | |
1820 | r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]); | |
7c2849d2 JM |
1821 | r->flags |= WPA_SCAN_NOISE_INVALID; |
1822 | if (bss[NL80211_BSS_SIGNAL_MBM]) { | |
b3db1e1c | 1823 | r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]); |
7c2849d2 JM |
1824 | r->level /= 100; /* mBm to dBm */ |
1825 | r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID; | |
1826 | } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) { | |
1827 | r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]); | |
1828 | r->flags |= WPA_SCAN_LEVEL_INVALID; | |
1829 | } else | |
1830 | r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID; | |
b3db1e1c JM |
1831 | if (bss[NL80211_BSS_TSF]) |
1832 | r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]); | |
b3ad11bb JM |
1833 | if (bss[NL80211_BSS_SEEN_MS_AGO]) |
1834 | r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]); | |
b3db1e1c | 1835 | r->ie_len = ie_len; |
8c090654 JM |
1836 | pos = (u8 *) (r + 1); |
1837 | if (ie) { | |
1838 | os_memcpy(pos, ie, ie_len); | |
1839 | pos += ie_len; | |
1840 | } | |
1841 | r->beacon_ie_len = beacon_ie_len; | |
1842 | if (beacon_ie) | |
1843 | os_memcpy(pos, beacon_ie, beacon_ie_len); | |
3f5285e8 | 1844 | |
e6b8efeb JM |
1845 | if (bss[NL80211_BSS_STATUS]) { |
1846 | enum nl80211_bss_status status; | |
1847 | status = nla_get_u32(bss[NL80211_BSS_STATUS]); | |
1848 | switch (status) { | |
1849 | case NL80211_BSS_STATUS_AUTHENTICATED: | |
1850 | r->flags |= WPA_SCAN_AUTHENTICATED; | |
1851 | break; | |
1852 | case NL80211_BSS_STATUS_ASSOCIATED: | |
1853 | r->flags |= WPA_SCAN_ASSOCIATED; | |
1854 | break; | |
1855 | default: | |
1856 | break; | |
1857 | } | |
1858 | } | |
1859 | ||
3f5285e8 JM |
1860 | tmp = os_realloc(res->res, |
1861 | (res->num + 1) * sizeof(struct wpa_scan_res *)); | |
1862 | if (tmp == NULL) { | |
1863 | os_free(r); | |
b3db1e1c | 1864 | return NL_SKIP; |
3f5285e8 JM |
1865 | } |
1866 | tmp[res->num++] = r; | |
1867 | res->res = tmp; | |
b3db1e1c JM |
1868 | |
1869 | return NL_SKIP; | |
3f5285e8 | 1870 | } |
b3db1e1c | 1871 | |
3f5285e8 | 1872 | |
d72aad94 JM |
1873 | static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv, |
1874 | const u8 *addr) | |
1875 | { | |
1876 | if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) { | |
1877 | wpa_printf(MSG_DEBUG, "nl80211: Clear possible state " | |
582507be | 1878 | "mismatch (" MACSTR ")", MAC2STR(addr)); |
d72aad94 JM |
1879 | wpa_driver_nl80211_mlme(drv, addr, |
1880 | NL80211_CMD_DEAUTHENTICATE, | |
77339912 | 1881 | WLAN_REASON_PREV_AUTH_NOT_VALID, 1); |
d72aad94 JM |
1882 | } |
1883 | } | |
1884 | ||
1885 | ||
e6b8efeb JM |
1886 | static void wpa_driver_nl80211_check_bss_status( |
1887 | struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res) | |
1888 | { | |
1889 | size_t i; | |
1890 | ||
1891 | for (i = 0; i < res->num; i++) { | |
1892 | struct wpa_scan_res *r = res->res[i]; | |
1893 | if (r->flags & WPA_SCAN_AUTHENTICATED) { | |
1894 | wpa_printf(MSG_DEBUG, "nl80211: Scan results " | |
1895 | "indicates BSS status with " MACSTR | |
1896 | " as authenticated", | |
1897 | MAC2STR(r->bssid)); | |
1898 | if (drv->nlmode == NL80211_IFTYPE_STATION && | |
1899 | os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 && | |
1900 | os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) != | |
1901 | 0) { | |
1902 | wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID" | |
1903 | " in local state (auth=" MACSTR | |
1904 | " assoc=" MACSTR ")", | |
1905 | MAC2STR(drv->auth_bssid), | |
1906 | MAC2STR(drv->bssid)); | |
582507be | 1907 | clear_state_mismatch(drv, r->bssid); |
e6b8efeb JM |
1908 | } |
1909 | } | |
1910 | ||
1911 | if (r->flags & WPA_SCAN_ASSOCIATED) { | |
1912 | wpa_printf(MSG_DEBUG, "nl80211: Scan results " | |
1913 | "indicate BSS status with " MACSTR | |
1914 | " as associated", | |
1915 | MAC2STR(r->bssid)); | |
1916 | if (drv->nlmode == NL80211_IFTYPE_STATION && | |
1917 | !drv->associated) { | |
1918 | wpa_printf(MSG_DEBUG, "nl80211: Local state " | |
1919 | "(not associated) does not match " | |
1920 | "with BSS state"); | |
d72aad94 | 1921 | clear_state_mismatch(drv, r->bssid); |
e6b8efeb JM |
1922 | } else if (drv->nlmode == NL80211_IFTYPE_STATION && |
1923 | os_memcmp(drv->bssid, r->bssid, ETH_ALEN) != | |
1924 | 0) { | |
1925 | wpa_printf(MSG_DEBUG, "nl80211: Local state " | |
1926 | "(associated with " MACSTR ") does " | |
1927 | "not match with BSS state", | |
d72aad94 JM |
1928 | MAC2STR(drv->bssid)); |
1929 | clear_state_mismatch(drv, r->bssid); | |
1930 | clear_state_mismatch(drv, drv->bssid); | |
e6b8efeb JM |
1931 | } |
1932 | } | |
1933 | } | |
1934 | } | |
1935 | ||
1936 | ||
d1f9c410 JM |
1937 | static void wpa_scan_results_free(struct wpa_scan_results *res) |
1938 | { | |
1939 | size_t i; | |
1940 | ||
1941 | if (res == NULL) | |
1942 | return; | |
1943 | ||
1944 | for (i = 0; i < res->num; i++) | |
1945 | os_free(res->res[i]); | |
1946 | os_free(res->res); | |
1947 | os_free(res); | |
1948 | } | |
1949 | ||
1950 | ||
7e5ba1b9 | 1951 | static struct wpa_scan_results * |
8856462d | 1952 | nl80211_get_scan_results(struct wpa_driver_nl80211_data *drv) |
3f5285e8 | 1953 | { |
b3db1e1c | 1954 | struct nl_msg *msg; |
3f5285e8 | 1955 | struct wpa_scan_results *res; |
b3db1e1c | 1956 | int ret; |
3812464c | 1957 | struct nl80211_bss_info_arg arg; |
3f5285e8 JM |
1958 | |
1959 | res = os_zalloc(sizeof(*res)); | |
b3db1e1c | 1960 | if (res == NULL) |
8e2c104f | 1961 | return NULL; |
b3db1e1c JM |
1962 | msg = nlmsg_alloc(); |
1963 | if (!msg) | |
1964 | goto nla_put_failure; | |
3f5285e8 | 1965 | |
b3db1e1c JM |
1966 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, NLM_F_DUMP, |
1967 | NL80211_CMD_GET_SCAN, 0); | |
1968 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); | |
3f5285e8 | 1969 | |
3812464c JM |
1970 | arg.drv = drv; |
1971 | arg.res = res; | |
1972 | ret = send_and_recv_msgs(drv, msg, bss_info_handler, &arg); | |
b3db1e1c JM |
1973 | msg = NULL; |
1974 | if (ret == 0) { | |
1975 | wpa_printf(MSG_DEBUG, "Received scan results (%lu BSSes)", | |
1976 | (unsigned long) res->num); | |
1977 | return res; | |
3f5285e8 | 1978 | } |
b3db1e1c JM |
1979 | wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d " |
1980 | "(%s)", ret, strerror(-ret)); | |
1981 | nla_put_failure: | |
1982 | nlmsg_free(msg); | |
1983 | wpa_scan_results_free(res); | |
1984 | return NULL; | |
3f5285e8 JM |
1985 | } |
1986 | ||
1987 | ||
8856462d JM |
1988 | /** |
1989 | * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results | |
1990 | * @priv: Pointer to private wext data from wpa_driver_nl80211_init() | |
1991 | * Returns: Scan results on success, -1 on failure | |
1992 | */ | |
1993 | static struct wpa_scan_results * | |
1994 | wpa_driver_nl80211_get_scan_results(void *priv) | |
1995 | { | |
a2e40bb6 FF |
1996 | struct i802_bss *bss = priv; |
1997 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
8856462d JM |
1998 | struct wpa_scan_results *res; |
1999 | ||
2000 | res = nl80211_get_scan_results(drv); | |
2001 | if (res) | |
2002 | wpa_driver_nl80211_check_bss_status(drv, res); | |
2003 | return res; | |
2004 | } | |
2005 | ||
2006 | ||
2007 | static void nl80211_dump_scan(struct wpa_driver_nl80211_data *drv) | |
2008 | { | |
2009 | struct wpa_scan_results *res; | |
2010 | size_t i; | |
2011 | ||
2012 | res = nl80211_get_scan_results(drv); | |
2013 | if (res == NULL) { | |
2014 | wpa_printf(MSG_DEBUG, "nl80211: Failed to get scan results"); | |
2015 | return; | |
2016 | } | |
2017 | ||
2018 | wpa_printf(MSG_DEBUG, "nl80211: Scan result dump"); | |
2019 | for (i = 0; i < res->num; i++) { | |
2020 | struct wpa_scan_res *r = res->res[i]; | |
2021 | wpa_printf(MSG_DEBUG, "nl80211: %d/%d " MACSTR "%s%s", | |
2022 | (int) i, (int) res->num, MAC2STR(r->bssid), | |
2023 | r->flags & WPA_SCAN_AUTHENTICATED ? " [auth]" : "", | |
2024 | r->flags & WPA_SCAN_ASSOCIATED ? " [assoc]" : ""); | |
2025 | } | |
2026 | ||
2027 | wpa_scan_results_free(res); | |
2028 | } | |
2029 | ||
2030 | ||
642187d6 | 2031 | static int wpa_driver_nl80211_set_key(const char *ifname, void *priv, |
71934751 JM |
2032 | enum wpa_alg alg, const u8 *addr, |
2033 | int key_idx, int set_tx, | |
642187d6 JM |
2034 | const u8 *seq, size_t seq_len, |
2035 | const u8 *key, size_t key_len) | |
3f5285e8 | 2036 | { |
a2e40bb6 FF |
2037 | struct i802_bss *bss = priv; |
2038 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
642187d6 | 2039 | int ifindex = if_nametoindex(ifname); |
3f5285e8 | 2040 | struct nl_msg *msg; |
1ad1cdc2 | 2041 | int ret; |
3f5285e8 | 2042 | |
1ad1cdc2 JM |
2043 | wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d " |
2044 | "set_tx=%d seq_len=%lu key_len=%lu", | |
2045 | __func__, ifindex, alg, addr, key_idx, set_tx, | |
3f5285e8 JM |
2046 | (unsigned long) seq_len, (unsigned long) key_len); |
2047 | ||
2048 | msg = nlmsg_alloc(); | |
1ad1cdc2 JM |
2049 | if (!msg) |
2050 | return -ENOMEM; | |
3f5285e8 JM |
2051 | |
2052 | if (alg == WPA_ALG_NONE) { | |
1ad1cdc2 JM |
2053 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, |
2054 | 0, NL80211_CMD_DEL_KEY, 0); | |
3f5285e8 | 2055 | } else { |
1ad1cdc2 JM |
2056 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, |
2057 | 0, NL80211_CMD_NEW_KEY, 0); | |
3f5285e8 | 2058 | NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key); |
d723bab4 JM |
2059 | switch (alg) { |
2060 | case WPA_ALG_WEP: | |
2061 | if (key_len == 5) | |
2062 | NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, | |
2063 | WLAN_CIPHER_SUITE_WEP40); | |
2064 | else | |
2065 | NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, | |
2066 | WLAN_CIPHER_SUITE_WEP104); | |
2067 | break; | |
2068 | case WPA_ALG_TKIP: | |
2069 | NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, | |
2070 | WLAN_CIPHER_SUITE_TKIP); | |
2071 | break; | |
2072 | case WPA_ALG_CCMP: | |
2073 | NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, | |
2074 | WLAN_CIPHER_SUITE_CCMP); | |
2075 | break; | |
2076 | case WPA_ALG_IGTK: | |
2077 | NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, | |
2078 | WLAN_CIPHER_SUITE_AES_CMAC); | |
2079 | break; | |
2080 | default: | |
2081 | wpa_printf(MSG_ERROR, "%s: Unsupported encryption " | |
2082 | "algorithm %d", __func__, alg); | |
3f5285e8 JM |
2083 | nlmsg_free(msg); |
2084 | return -1; | |
2085 | } | |
2086 | } | |
2087 | ||
849ef835 | 2088 | if (seq && seq_len) |
1ad1cdc2 JM |
2089 | NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq); |
2090 | ||
3f5285e8 JM |
2091 | if (addr && os_memcmp(addr, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0) |
2092 | { | |
2093 | wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr)); | |
2094 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); | |
2095 | } | |
2096 | NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx); | |
1ad1cdc2 | 2097 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); |
3f5285e8 | 2098 | |
1ad1cdc2 | 2099 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); |
15664ad0 | 2100 | if ((ret == -ENOENT || ret == -ENOLINK) && alg == WPA_ALG_NONE) |
1ad1cdc2 JM |
2101 | ret = 0; |
2102 | if (ret) | |
2103 | wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)", | |
2104 | ret, strerror(-ret)); | |
3f5285e8 | 2105 | |
1ad1cdc2 JM |
2106 | /* |
2107 | * If we failed or don't need to set the default TX key (below), | |
2108 | * we're done here. | |
2109 | */ | |
2110 | if (ret || !set_tx || alg == WPA_ALG_NONE) | |
2111 | return ret; | |
de12717a | 2112 | #ifdef HOSTAPD |
1ad1cdc2 JM |
2113 | if (addr) |
2114 | return ret; | |
de12717a JM |
2115 | #else /* HOSTAPD */ |
2116 | if (drv->nlmode == NL80211_IFTYPE_AP && addr) | |
2117 | return ret; | |
1ad1cdc2 | 2118 | #endif /* HOSTAPD */ |
3f5285e8 | 2119 | |
1ad1cdc2 JM |
2120 | msg = nlmsg_alloc(); |
2121 | if (!msg) | |
2122 | return -ENOMEM; | |
3f5285e8 | 2123 | |
1ad1cdc2 JM |
2124 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, |
2125 | 0, NL80211_CMD_SET_KEY, 0); | |
2126 | NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx); | |
2127 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); | |
2128 | if (alg == WPA_ALG_IGTK) | |
2129 | NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT); | |
2130 | else | |
2131 | NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT); | |
3f5285e8 | 2132 | |
1ad1cdc2 JM |
2133 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); |
2134 | if (ret == -ENOENT) | |
2135 | ret = 0; | |
2136 | if (ret) | |
2137 | wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; " | |
2138 | "err=%d %s)", ret, strerror(-ret)); | |
2139 | return ret; | |
3f5285e8 JM |
2140 | |
2141 | nla_put_failure: | |
6241fcb1 | 2142 | return -ENOBUFS; |
3f5285e8 JM |
2143 | } |
2144 | ||
2145 | ||
71934751 | 2146 | static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg, |
0194fedb JB |
2147 | int key_idx, int defkey, |
2148 | const u8 *seq, size_t seq_len, | |
2149 | const u8 *key, size_t key_len) | |
2150 | { | |
2151 | struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY); | |
2152 | if (!key_attr) | |
2153 | return -1; | |
2154 | ||
2155 | if (defkey && alg == WPA_ALG_IGTK) | |
2156 | NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT); | |
2157 | else if (defkey) | |
2158 | NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT); | |
2159 | ||
2160 | NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx); | |
2161 | ||
d723bab4 JM |
2162 | switch (alg) { |
2163 | case WPA_ALG_WEP: | |
2164 | if (key_len == 5) | |
2aa5f847 JM |
2165 | NLA_PUT_U32(msg, NL80211_KEY_CIPHER, |
2166 | WLAN_CIPHER_SUITE_WEP40); | |
d723bab4 | 2167 | else |
2aa5f847 JM |
2168 | NLA_PUT_U32(msg, NL80211_KEY_CIPHER, |
2169 | WLAN_CIPHER_SUITE_WEP104); | |
d723bab4 JM |
2170 | break; |
2171 | case WPA_ALG_TKIP: | |
2aa5f847 | 2172 | NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP); |
d723bab4 JM |
2173 | break; |
2174 | case WPA_ALG_CCMP: | |
2aa5f847 | 2175 | NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP); |
d723bab4 JM |
2176 | break; |
2177 | case WPA_ALG_IGTK: | |
2aa5f847 JM |
2178 | NLA_PUT_U32(msg, NL80211_KEY_CIPHER, |
2179 | WLAN_CIPHER_SUITE_AES_CMAC); | |
d723bab4 JM |
2180 | break; |
2181 | default: | |
2182 | wpa_printf(MSG_ERROR, "%s: Unsupported encryption " | |
2183 | "algorithm %d", __func__, alg); | |
0194fedb | 2184 | return -1; |
d723bab4 | 2185 | } |
0194fedb JB |
2186 | |
2187 | if (seq && seq_len) | |
2188 | NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq); | |
2189 | ||
2190 | NLA_PUT(msg, NL80211_KEY_DATA, key_len, key); | |
2191 | ||
2192 | nla_nest_end(msg, key_attr); | |
2193 | ||
2194 | return 0; | |
2195 | nla_put_failure: | |
2196 | return -1; | |
2197 | } | |
2198 | ||
c811d5bc | 2199 | |
cfaab580 ZY |
2200 | static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params, |
2201 | struct nl_msg *msg) | |
2202 | { | |
2203 | int i, privacy = 0; | |
2204 | struct nlattr *nl_keys, *nl_key; | |
2205 | ||
2206 | for (i = 0; i < 4; i++) { | |
2207 | if (!params->wep_key[i]) | |
2208 | continue; | |
2209 | privacy = 1; | |
2210 | break; | |
2211 | } | |
2212 | if (!privacy) | |
2213 | return 0; | |
2214 | ||
2215 | NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY); | |
2216 | ||
2217 | nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS); | |
2218 | if (!nl_keys) | |
2219 | goto nla_put_failure; | |
2220 | ||
2221 | for (i = 0; i < 4; i++) { | |
2222 | if (!params->wep_key[i]) | |
2223 | continue; | |
2224 | ||
2225 | nl_key = nla_nest_start(msg, i); | |
2226 | if (!nl_key) | |
2227 | goto nla_put_failure; | |
2228 | ||
2229 | NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i], | |
2230 | params->wep_key[i]); | |
2231 | if (params->wep_key_len[i] == 5) | |
2232 | NLA_PUT_U32(msg, NL80211_KEY_CIPHER, | |
2233 | WLAN_CIPHER_SUITE_WEP40); | |
2234 | else | |
2235 | NLA_PUT_U32(msg, NL80211_KEY_CIPHER, | |
2236 | WLAN_CIPHER_SUITE_WEP104); | |
2237 | ||
2238 | NLA_PUT_U8(msg, NL80211_KEY_IDX, i); | |
2239 | ||
2240 | if (i == params->wep_tx_keyidx) | |
2241 | NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT); | |
2242 | ||
2243 | nla_nest_end(msg, nl_key); | |
2244 | } | |
2245 | nla_nest_end(msg, nl_keys); | |
2246 | ||
2247 | return 0; | |
2248 | ||
2249 | nla_put_failure: | |
2250 | return -ENOBUFS; | |
2251 | } | |
2252 | ||
2253 | ||
c2a04078 | 2254 | static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv, |
77339912 JM |
2255 | const u8 *addr, int cmd, u16 reason_code, |
2256 | int local_state_change) | |
c2a04078 JM |
2257 | { |
2258 | int ret = -1; | |
2259 | struct nl_msg *msg; | |
2260 | ||
2261 | msg = nlmsg_alloc(); | |
2262 | if (!msg) | |
2263 | return -1; | |
2264 | ||
2265 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, cmd, 0); | |
2266 | ||
2267 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); | |
2268 | NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code); | |
2269 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); | |
77339912 JM |
2270 | if (local_state_change) |
2271 | NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE); | |
c2a04078 JM |
2272 | |
2273 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
2274 | msg = NULL; | |
2275 | if (ret) { | |
2276 | wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d " | |
2277 | "(%s)", ret, strerror(-ret)); | |
2278 | goto nla_put_failure; | |
2279 | } | |
2280 | ret = 0; | |
2281 | ||
2282 | nla_put_failure: | |
2283 | nlmsg_free(msg); | |
2284 | return ret; | |
2285 | } | |
3f5285e8 JM |
2286 | |
2287 | ||
cfaab580 ZY |
2288 | static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv, |
2289 | const u8 *addr, int reason_code) | |
2290 | { | |
2291 | wpa_printf(MSG_DEBUG, "%s", __func__); | |
2292 | drv->associated = 0; | |
2293 | return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISCONNECT, | |
77339912 | 2294 | reason_code, 0); |
cfaab580 ZY |
2295 | } |
2296 | ||
2297 | ||
3f5285e8 | 2298 | static int wpa_driver_nl80211_deauthenticate(void *priv, const u8 *addr, |
c2a04078 | 2299 | int reason_code) |
3f5285e8 | 2300 | { |
a2e40bb6 FF |
2301 | struct i802_bss *bss = priv; |
2302 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
cfaab580 ZY |
2303 | if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) |
2304 | return wpa_driver_nl80211_disconnect(drv, addr, reason_code); | |
c2a04078 | 2305 | wpa_printf(MSG_DEBUG, "%s", __func__); |
13405f35 | 2306 | drv->associated = 0; |
c2a04078 | 2307 | return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE, |
77339912 | 2308 | reason_code, 0); |
3f5285e8 JM |
2309 | } |
2310 | ||
2311 | ||
2312 | static int wpa_driver_nl80211_disassociate(void *priv, const u8 *addr, | |
c2a04078 | 2313 | int reason_code) |
3f5285e8 | 2314 | { |
a2e40bb6 FF |
2315 | struct i802_bss *bss = priv; |
2316 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
cfaab580 ZY |
2317 | if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) |
2318 | return wpa_driver_nl80211_disconnect(drv, addr, reason_code); | |
c2a04078 | 2319 | wpa_printf(MSG_DEBUG, "%s", __func__); |
13405f35 | 2320 | drv->associated = 0; |
c2a04078 | 2321 | return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISASSOCIATE, |
77339912 | 2322 | reason_code, 0); |
3f5285e8 JM |
2323 | } |
2324 | ||
2325 | ||
c2a04078 JM |
2326 | static int wpa_driver_nl80211_authenticate( |
2327 | void *priv, struct wpa_driver_auth_params *params) | |
2328 | { | |
a2e40bb6 FF |
2329 | struct i802_bss *bss = priv; |
2330 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
a0b2f99b | 2331 | int ret = -1, i; |
c2a04078 JM |
2332 | struct nl_msg *msg; |
2333 | enum nl80211_auth_type type; | |
6d6f4bb8 | 2334 | int count = 0; |
c2a04078 JM |
2335 | |
2336 | drv->associated = 0; | |
e6b8efeb | 2337 | os_memset(drv->auth_bssid, 0, ETH_ALEN); |
af473088 JM |
2338 | /* FIX: IBSS mode */ |
2339 | if (drv->nlmode != NL80211_IFTYPE_STATION) | |
2340 | wpa_driver_nl80211_set_mode(priv, IEEE80211_MODE_INFRA); | |
c2a04078 | 2341 | |
a2e40bb6 | 2342 | if (wpa_driver_nl80211_set_mode(priv, IEEE80211_MODE_INFRA) < 0) |
4a867032 JM |
2343 | return -1; |
2344 | ||
6d6f4bb8 | 2345 | retry: |
c2a04078 JM |
2346 | msg = nlmsg_alloc(); |
2347 | if (!msg) | |
2348 | return -1; | |
2349 | ||
2350 | wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)", | |
2351 | drv->ifindex); | |
a0b2f99b | 2352 | |
0194fedb JB |
2353 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, |
2354 | NL80211_CMD_AUTHENTICATE, 0); | |
2355 | ||
a0b2f99b JM |
2356 | for (i = 0; i < 4; i++) { |
2357 | if (!params->wep_key[i]) | |
2358 | continue; | |
2ea2fcc7 HS |
2359 | wpa_driver_nl80211_set_key(bss->ifname, priv, WPA_ALG_WEP, |
2360 | NULL, i, | |
a0b2f99b JM |
2361 | i == params->wep_tx_keyidx, NULL, 0, |
2362 | params->wep_key[i], | |
2363 | params->wep_key_len[i]); | |
0194fedb JB |
2364 | if (params->wep_tx_keyidx != i) |
2365 | continue; | |
2366 | if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0, | |
2367 | params->wep_key[i], params->wep_key_len[i])) { | |
2368 | nlmsg_free(msg); | |
2369 | return -1; | |
2370 | } | |
a0b2f99b JM |
2371 | } |
2372 | ||
c2a04078 JM |
2373 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); |
2374 | if (params->bssid) { | |
2375 | wpa_printf(MSG_DEBUG, " * bssid=" MACSTR, | |
2376 | MAC2STR(params->bssid)); | |
2377 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid); | |
2378 | } | |
2379 | if (params->freq) { | |
2380 | wpa_printf(MSG_DEBUG, " * freq=%d", params->freq); | |
2381 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq); | |
2382 | } | |
2383 | if (params->ssid) { | |
2384 | wpa_hexdump_ascii(MSG_DEBUG, " * SSID", | |
2385 | params->ssid, params->ssid_len); | |
2386 | NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len, | |
2387 | params->ssid); | |
2388 | } | |
2389 | wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len); | |
2390 | if (params->ie) | |
2391 | NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie); | |
2392 | /* | |
2393 | * TODO: if multiple auth_alg options enabled, try them one by one if | |
2394 | * the AP rejects authentication due to unknown auth alg | |
2395 | */ | |
abd9fafa | 2396 | if (params->auth_alg & WPA_AUTH_ALG_OPEN) |
c2a04078 | 2397 | type = NL80211_AUTHTYPE_OPEN_SYSTEM; |
abd9fafa | 2398 | else if (params->auth_alg & WPA_AUTH_ALG_SHARED) |
c2a04078 | 2399 | type = NL80211_AUTHTYPE_SHARED_KEY; |
abd9fafa | 2400 | else if (params->auth_alg & WPA_AUTH_ALG_LEAP) |
c2a04078 | 2401 | type = NL80211_AUTHTYPE_NETWORK_EAP; |
abd9fafa | 2402 | else if (params->auth_alg & WPA_AUTH_ALG_FT) |
c2a04078 JM |
2403 | type = NL80211_AUTHTYPE_FT; |
2404 | else | |
2405 | goto nla_put_failure; | |
2406 | wpa_printf(MSG_DEBUG, " * Auth Type %d", type); | |
2407 | NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type); | |
77339912 JM |
2408 | if (params->local_state_change) { |
2409 | wpa_printf(MSG_DEBUG, " * Local state change only"); | |
2410 | NLA_PUT_FLAG(msg, NL80211_ATTR_LOCAL_STATE_CHANGE); | |
2411 | } | |
c2a04078 JM |
2412 | |
2413 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
2414 | msg = NULL; | |
2415 | if (ret) { | |
2416 | wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d " | |
2417 | "(%s)", ret, strerror(-ret)); | |
6d6f4bb8 | 2418 | count++; |
77339912 JM |
2419 | if (ret == -EALREADY && count == 1 && params->bssid && |
2420 | !params->local_state_change) { | |
6d6f4bb8 JM |
2421 | /* |
2422 | * mac80211 does not currently accept new | |
2423 | * authentication if we are already authenticated. As a | |
2424 | * workaround, force deauthentication and try again. | |
2425 | */ | |
2426 | wpa_printf(MSG_DEBUG, "nl80211: Retry authentication " | |
2427 | "after forced deauthentication"); | |
2428 | wpa_driver_nl80211_deauthenticate( | |
5205c4f9 | 2429 | bss, params->bssid, |
6d6f4bb8 JM |
2430 | WLAN_REASON_PREV_AUTH_NOT_VALID); |
2431 | nlmsg_free(msg); | |
2432 | goto retry; | |
2433 | } | |
c2a04078 JM |
2434 | goto nla_put_failure; |
2435 | } | |
2436 | ret = 0; | |
2437 | wpa_printf(MSG_DEBUG, "nl80211: Authentication request send " | |
2438 | "successfully"); | |
2439 | ||
2440 | nla_put_failure: | |
2441 | nlmsg_free(msg); | |
2442 | return ret; | |
2443 | } | |
2444 | ||
2445 | ||
282d5590 JM |
2446 | struct phy_info_arg { |
2447 | u16 *num_modes; | |
2448 | struct hostapd_hw_modes *modes; | |
2449 | }; | |
2450 | ||
2451 | static int phy_info_handler(struct nl_msg *msg, void *arg) | |
2452 | { | |
2453 | struct nlattr *tb_msg[NL80211_ATTR_MAX + 1]; | |
2454 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
2455 | struct phy_info_arg *phy_info = arg; | |
2456 | ||
2457 | struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1]; | |
2458 | ||
2459 | struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1]; | |
2460 | static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = { | |
2461 | [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 }, | |
2462 | [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG }, | |
2463 | [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG }, | |
2464 | [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG }, | |
2465 | [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG }, | |
2466 | [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 }, | |
2467 | }; | |
2468 | ||
2469 | struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1]; | |
2470 | static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = { | |
2471 | [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 }, | |
2472 | [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG }, | |
2473 | }; | |
2474 | ||
2475 | struct nlattr *nl_band; | |
2476 | struct nlattr *nl_freq; | |
2477 | struct nlattr *nl_rate; | |
2478 | int rem_band, rem_freq, rem_rate; | |
2479 | struct hostapd_hw_modes *mode; | |
2480 | int idx, mode_is_set; | |
2481 | ||
2482 | nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
2483 | genlmsg_attrlen(gnlh, 0), NULL); | |
2484 | ||
2485 | if (!tb_msg[NL80211_ATTR_WIPHY_BANDS]) | |
2486 | return NL_SKIP; | |
2487 | ||
2488 | nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) { | |
e62fb0a0 | 2489 | mode = os_realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode)); |
282d5590 JM |
2490 | if (!mode) |
2491 | return NL_SKIP; | |
2492 | phy_info->modes = mode; | |
2493 | ||
2494 | mode_is_set = 0; | |
2495 | ||
2496 | mode = &phy_info->modes[*(phy_info->num_modes)]; | |
2497 | memset(mode, 0, sizeof(*mode)); | |
2498 | *(phy_info->num_modes) += 1; | |
2499 | ||
2500 | nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band), | |
2501 | nla_len(nl_band), NULL); | |
2502 | ||
2503 | if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) { | |
2504 | mode->ht_capab = nla_get_u16( | |
2505 | tb_band[NL80211_BAND_ATTR_HT_CAPA]); | |
2506 | } | |
2507 | ||
be8eb8ab JM |
2508 | if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) { |
2509 | mode->a_mpdu_params |= nla_get_u8( | |
2510 | tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) & | |
2511 | 0x03; | |
2512 | } | |
2513 | ||
2514 | if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) { | |
2515 | mode->a_mpdu_params |= nla_get_u8( | |
2516 | tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) << | |
2517 | 2; | |
2518 | } | |
2519 | ||
08eb154d JM |
2520 | if (tb_band[NL80211_BAND_ATTR_HT_MCS_SET] && |
2521 | nla_len(tb_band[NL80211_BAND_ATTR_HT_MCS_SET])) { | |
2522 | u8 *mcs; | |
2523 | mcs = nla_data(tb_band[NL80211_BAND_ATTR_HT_MCS_SET]); | |
2524 | os_memcpy(mode->mcs_set, mcs, 16); | |
2525 | } | |
2526 | ||
282d5590 JM |
2527 | nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) { |
2528 | nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq), | |
2529 | nla_len(nl_freq), freq_policy); | |
2530 | if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ]) | |
2531 | continue; | |
2532 | mode->num_channels++; | |
2533 | } | |
2534 | ||
e62fb0a0 | 2535 | mode->channels = os_zalloc(mode->num_channels * sizeof(struct hostapd_channel_data)); |
282d5590 JM |
2536 | if (!mode->channels) |
2537 | return NL_SKIP; | |
2538 | ||
2539 | idx = 0; | |
2540 | ||
2541 | nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) { | |
2542 | nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq), | |
2543 | nla_len(nl_freq), freq_policy); | |
2544 | if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ]) | |
2545 | continue; | |
2546 | ||
2547 | mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]); | |
2548 | mode->channels[idx].flag = 0; | |
2549 | ||
2550 | if (!mode_is_set) { | |
2551 | /* crude heuristic */ | |
2552 | if (mode->channels[idx].freq < 4000) | |
2553 | mode->mode = HOSTAPD_MODE_IEEE80211B; | |
2554 | else | |
2555 | mode->mode = HOSTAPD_MODE_IEEE80211A; | |
2556 | mode_is_set = 1; | |
2557 | } | |
2558 | ||
2559 | /* crude heuristic */ | |
2560 | if (mode->channels[idx].freq < 4000) | |
5a0ffb5f | 2561 | if (mode->channels[idx].freq == 2484) |
282d5590 JM |
2562 | mode->channels[idx].chan = 14; |
2563 | else | |
2564 | mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5; | |
2565 | else | |
2566 | mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000; | |
2567 | ||
2568 | if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED]) | |
2569 | mode->channels[idx].flag |= | |
2570 | HOSTAPD_CHAN_DISABLED; | |
2571 | if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN]) | |
2572 | mode->channels[idx].flag |= | |
2573 | HOSTAPD_CHAN_PASSIVE_SCAN; | |
2574 | if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS]) | |
2575 | mode->channels[idx].flag |= | |
2576 | HOSTAPD_CHAN_NO_IBSS; | |
2577 | if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR]) | |
2578 | mode->channels[idx].flag |= | |
2579 | HOSTAPD_CHAN_RADAR; | |
2580 | ||
2581 | if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] && | |
2582 | !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED]) | |
2583 | mode->channels[idx].max_tx_power = | |
2584 | nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100; | |
2585 | ||
2586 | idx++; | |
2587 | } | |
2588 | ||
2589 | nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) { | |
2590 | nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate), | |
2591 | nla_len(nl_rate), rate_policy); | |
2592 | if (!tb_rate[NL80211_BITRATE_ATTR_RATE]) | |
2593 | continue; | |
2594 | mode->num_rates++; | |
2595 | } | |
2596 | ||
e62fb0a0 | 2597 | mode->rates = os_zalloc(mode->num_rates * sizeof(int)); |
282d5590 JM |
2598 | if (!mode->rates) |
2599 | return NL_SKIP; | |
2600 | ||
2601 | idx = 0; | |
2602 | ||
2603 | nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) { | |
2604 | nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate), | |
2605 | nla_len(nl_rate), rate_policy); | |
2606 | if (!tb_rate[NL80211_BITRATE_ATTR_RATE]) | |
2607 | continue; | |
fb7842aa | 2608 | mode->rates[idx] = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]); |
282d5590 JM |
2609 | |
2610 | /* crude heuristic */ | |
2611 | if (mode->mode == HOSTAPD_MODE_IEEE80211B && | |
fb7842aa | 2612 | mode->rates[idx] > 200) |
282d5590 JM |
2613 | mode->mode = HOSTAPD_MODE_IEEE80211G; |
2614 | ||
282d5590 JM |
2615 | idx++; |
2616 | } | |
2617 | } | |
2618 | ||
2619 | return NL_SKIP; | |
2620 | } | |
2621 | ||
2622 | static struct hostapd_hw_modes * | |
2623 | wpa_driver_nl80211_add_11b(struct hostapd_hw_modes *modes, u16 *num_modes) | |
2624 | { | |
2625 | u16 m; | |
2626 | struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode; | |
2627 | int i, mode11g_idx = -1; | |
2628 | ||
2629 | /* If only 802.11g mode is included, use it to construct matching | |
2630 | * 802.11b mode data. */ | |
2631 | ||
2632 | for (m = 0; m < *num_modes; m++) { | |
2633 | if (modes[m].mode == HOSTAPD_MODE_IEEE80211B) | |
2634 | return modes; /* 802.11b already included */ | |
2635 | if (modes[m].mode == HOSTAPD_MODE_IEEE80211G) | |
2636 | mode11g_idx = m; | |
2637 | } | |
2638 | ||
2639 | if (mode11g_idx < 0) | |
2640 | return modes; /* 2.4 GHz band not supported at all */ | |
2641 | ||
2642 | nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes)); | |
2643 | if (nmodes == NULL) | |
2644 | return modes; /* Could not add 802.11b mode */ | |
2645 | ||
2646 | mode = &nmodes[*num_modes]; | |
2647 | os_memset(mode, 0, sizeof(*mode)); | |
2648 | (*num_modes)++; | |
2649 | modes = nmodes; | |
2650 | ||
2651 | mode->mode = HOSTAPD_MODE_IEEE80211B; | |
2652 | ||
2653 | mode11g = &modes[mode11g_idx]; | |
2654 | mode->num_channels = mode11g->num_channels; | |
2655 | mode->channels = os_malloc(mode11g->num_channels * | |
2656 | sizeof(struct hostapd_channel_data)); | |
2657 | if (mode->channels == NULL) { | |
2658 | (*num_modes)--; | |
2659 | return modes; /* Could not add 802.11b mode */ | |
2660 | } | |
2661 | os_memcpy(mode->channels, mode11g->channels, | |
2662 | mode11g->num_channels * sizeof(struct hostapd_channel_data)); | |
2663 | ||
2664 | mode->num_rates = 0; | |
fb7842aa | 2665 | mode->rates = os_malloc(4 * sizeof(int)); |
282d5590 JM |
2666 | if (mode->rates == NULL) { |
2667 | os_free(mode->channels); | |
2668 | (*num_modes)--; | |
2669 | return modes; /* Could not add 802.11b mode */ | |
2670 | } | |
2671 | ||
2672 | for (i = 0; i < mode11g->num_rates; i++) { | |
fb7842aa JM |
2673 | if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 && |
2674 | mode11g->rates[i] != 55 && mode11g->rates[i] != 110) | |
282d5590 JM |
2675 | continue; |
2676 | mode->rates[mode->num_rates] = mode11g->rates[i]; | |
2677 | mode->num_rates++; | |
2678 | if (mode->num_rates == 4) | |
2679 | break; | |
2680 | } | |
2681 | ||
2682 | if (mode->num_rates == 0) { | |
2683 | os_free(mode->channels); | |
2684 | os_free(mode->rates); | |
2685 | (*num_modes)--; | |
2686 | return modes; /* No 802.11b rates */ | |
2687 | } | |
2688 | ||
2689 | wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g " | |
2690 | "information"); | |
2691 | ||
2692 | return modes; | |
2693 | } | |
2694 | ||
2695 | ||
2696 | static struct hostapd_hw_modes * | |
2697 | wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags) | |
2698 | { | |
a2e40bb6 FF |
2699 | struct i802_bss *bss = priv; |
2700 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
282d5590 JM |
2701 | struct nl_msg *msg; |
2702 | struct phy_info_arg result = { | |
2703 | .num_modes = num_modes, | |
2704 | .modes = NULL, | |
2705 | }; | |
2706 | ||
2707 | *num_modes = 0; | |
2708 | *flags = 0; | |
2709 | ||
2710 | msg = nlmsg_alloc(); | |
2711 | if (!msg) | |
2712 | return NULL; | |
2713 | ||
2714 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, | |
2715 | 0, NL80211_CMD_GET_WIPHY, 0); | |
2716 | ||
2717 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); | |
2718 | ||
2719 | if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0) | |
2720 | return wpa_driver_nl80211_add_11b(result.modes, num_modes); | |
2721 | nla_put_failure: | |
2722 | return NULL; | |
2723 | } | |
2724 | ||
2725 | ||
2c2010ac JM |
2726 | static int wpa_driver_nl80211_send_frame(struct wpa_driver_nl80211_data *drv, |
2727 | const void *data, size_t len, | |
2728 | int encrypt) | |
2729 | { | |
2730 | __u8 rtap_hdr[] = { | |
2731 | 0x00, 0x00, /* radiotap version */ | |
2732 | 0x0e, 0x00, /* radiotap length */ | |
2733 | 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */ | |
2734 | IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */ | |
2735 | 0x00, /* padding */ | |
2736 | 0x00, 0x00, /* RX and TX flags to indicate that */ | |
2737 | 0x00, 0x00, /* this is the injected frame directly */ | |
2738 | }; | |
2739 | struct iovec iov[2] = { | |
2740 | { | |
2741 | .iov_base = &rtap_hdr, | |
2742 | .iov_len = sizeof(rtap_hdr), | |
2743 | }, | |
2744 | { | |
2745 | .iov_base = (void *) data, | |
2746 | .iov_len = len, | |
2747 | } | |
2748 | }; | |
2749 | struct msghdr msg = { | |
2750 | .msg_name = NULL, | |
2751 | .msg_namelen = 0, | |
2752 | .msg_iov = iov, | |
2753 | .msg_iovlen = 2, | |
2754 | .msg_control = NULL, | |
2755 | .msg_controllen = 0, | |
2756 | .msg_flags = 0, | |
2757 | }; | |
2758 | ||
2759 | if (encrypt) | |
2760 | rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP; | |
2761 | ||
2762 | return sendmsg(drv->monitor_sock, &msg, 0); | |
2763 | } | |
2764 | ||
2765 | ||
2766 | static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data, | |
2767 | size_t data_len) | |
2768 | { | |
a2e40bb6 FF |
2769 | struct i802_bss *bss = priv; |
2770 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
2c2010ac | 2771 | struct ieee80211_mgmt *mgmt; |
7a47d567 | 2772 | int encrypt = 1; |
2c2010ac JM |
2773 | u16 fc; |
2774 | ||
2775 | mgmt = (struct ieee80211_mgmt *) data; | |
2776 | fc = le_to_host16(mgmt->frame_control); | |
2777 | ||
2778 | if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT && | |
2779 | WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) { | |
2780 | /* | |
2781 | * Only one of the authentication frame types is encrypted. | |
2782 | * In order for static WEP encryption to work properly (i.e., | |
2783 | * to not encrypt the frame), we need to tell mac80211 about | |
2784 | * the frames that must not be encrypted. | |
2785 | */ | |
2786 | u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg); | |
2787 | u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction); | |
7a47d567 JB |
2788 | if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3) |
2789 | encrypt = 0; | |
2c2010ac JM |
2790 | } |
2791 | ||
7a47d567 | 2792 | return wpa_driver_nl80211_send_frame(drv, data, data_len, encrypt); |
2c2010ac JM |
2793 | } |
2794 | ||
2795 | ||
8b897f5a | 2796 | static int wpa_driver_nl80211_set_beacon(void *priv, |
5d674872 JM |
2797 | const u8 *head, size_t head_len, |
2798 | const u8 *tail, size_t tail_len, | |
2799 | int dtim_period, int beacon_int) | |
d2440ba0 | 2800 | { |
a2e40bb6 FF |
2801 | struct i802_bss *bss = priv; |
2802 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
d2440ba0 JM |
2803 | struct nl_msg *msg; |
2804 | u8 cmd = NL80211_CMD_NEW_BEACON; | |
2805 | int ret; | |
b4fd6fab | 2806 | int beacon_set; |
8b897f5a | 2807 | int ifindex = if_nametoindex(bss->ifname); |
b4fd6fab | 2808 | |
b4fd6fab | 2809 | beacon_set = bss->beacon_set; |
d2440ba0 JM |
2810 | |
2811 | msg = nlmsg_alloc(); | |
2812 | if (!msg) | |
2813 | return -ENOMEM; | |
2814 | ||
2815 | wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)", | |
b4fd6fab JM |
2816 | beacon_set); |
2817 | if (beacon_set) | |
d2440ba0 JM |
2818 | cmd = NL80211_CMD_SET_BEACON; |
2819 | ||
2820 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, | |
2821 | 0, cmd, 0); | |
2822 | NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head); | |
2823 | NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail); | |
b4fd6fab | 2824 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); |
5d674872 | 2825 | NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, beacon_int); |
d2440ba0 JM |
2826 | NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period); |
2827 | ||
2828 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
2829 | if (ret) { | |
2830 | wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)", | |
2831 | ret, strerror(-ret)); | |
b4fd6fab | 2832 | } else { |
b4fd6fab | 2833 | bss->beacon_set = 1; |
b4fd6fab | 2834 | } |
d2440ba0 JM |
2835 | return ret; |
2836 | nla_put_failure: | |
2837 | return -ENOBUFS; | |
2838 | } | |
2839 | ||
2840 | ||
f019981a JM |
2841 | static int wpa_driver_nl80211_set_freq(struct wpa_driver_nl80211_data *drv, |
2842 | int freq, int ht_enabled, | |
2843 | int sec_channel_offset) | |
1581b38b JM |
2844 | { |
2845 | struct nl_msg *msg; | |
d2440ba0 | 2846 | int ret; |
1581b38b JM |
2847 | |
2848 | msg = nlmsg_alloc(); | |
2849 | if (!msg) | |
2850 | return -1; | |
2851 | ||
2852 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, | |
2853 | NL80211_CMD_SET_WIPHY, 0); | |
2854 | ||
2855 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); | |
f019981a JM |
2856 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq); |
2857 | if (ht_enabled) { | |
2858 | switch (sec_channel_offset) { | |
2859 | case -1: | |
2860 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, | |
2861 | NL80211_CHAN_HT40MINUS); | |
2862 | break; | |
2863 | case 1: | |
2864 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, | |
2865 | NL80211_CHAN_HT40PLUS); | |
2866 | break; | |
2867 | default: | |
2868 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, | |
2869 | NL80211_CHAN_HT20); | |
2870 | break; | |
2871 | } | |
2872 | } | |
1581b38b | 2873 | |
d2440ba0 JM |
2874 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); |
2875 | if (ret == 0) | |
1581b38b | 2876 | return 0; |
f019981a JM |
2877 | wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): " |
2878 | "%d (%s)", freq, ret, strerror(-ret)); | |
1581b38b JM |
2879 | nla_put_failure: |
2880 | return -1; | |
2881 | } | |
2882 | ||
0f4e8b4f | 2883 | |
62847751 | 2884 | static int wpa_driver_nl80211_sta_add(void *priv, |
0f4e8b4f JM |
2885 | struct hostapd_sta_add_params *params) |
2886 | { | |
a2e40bb6 FF |
2887 | struct i802_bss *bss = priv; |
2888 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
0f4e8b4f JM |
2889 | struct nl_msg *msg; |
2890 | int ret = -ENOBUFS; | |
2891 | ||
2892 | msg = nlmsg_alloc(); | |
2893 | if (!msg) | |
2894 | return -ENOMEM; | |
2895 | ||
2896 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, | |
2897 | 0, NL80211_CMD_NEW_STATION, 0); | |
2898 | ||
62847751 | 2899 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname)); |
0f4e8b4f JM |
2900 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr); |
2901 | NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid); | |
2902 | NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len, | |
2903 | params->supp_rates); | |
2904 | NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL, | |
2905 | params->listen_interval); | |
0f4e8b4f JM |
2906 | if (params->ht_capabilities) { |
2907 | NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY, | |
fc4e2d95 JM |
2908 | sizeof(*params->ht_capabilities), |
2909 | params->ht_capabilities); | |
0f4e8b4f | 2910 | } |
0f4e8b4f JM |
2911 | |
2912 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
2913 | if (ret) | |
2914 | wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_NEW_STATION " | |
2915 | "result: %d (%s)", ret, strerror(-ret)); | |
2916 | if (ret == -EEXIST) | |
2917 | ret = 0; | |
2918 | nla_put_failure: | |
2919 | return ret; | |
2920 | } | |
2921 | ||
2922 | ||
2923 | static int wpa_driver_nl80211_sta_remove(void *priv, const u8 *addr) | |
2924 | { | |
a2e40bb6 FF |
2925 | struct i802_bss *bss = priv; |
2926 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
0f4e8b4f JM |
2927 | struct nl_msg *msg; |
2928 | int ret; | |
2929 | ||
2930 | msg = nlmsg_alloc(); | |
2931 | if (!msg) | |
2932 | return -ENOMEM; | |
2933 | ||
2934 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, | |
2935 | 0, NL80211_CMD_DEL_STATION, 0); | |
2936 | ||
2937 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, | |
a2e40bb6 | 2938 | if_nametoindex(bss->ifname)); |
0f4e8b4f JM |
2939 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); |
2940 | ||
2941 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
2942 | if (ret == -ENOENT) | |
2943 | return 0; | |
2944 | return ret; | |
2945 | nla_put_failure: | |
2946 | return -ENOBUFS; | |
2947 | } | |
2948 | ||
1581b38b | 2949 | |
0915d02c JM |
2950 | static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, |
2951 | int ifidx) | |
2952 | { | |
2953 | struct nl_msg *msg; | |
2954 | ||
c6e8e8e4 JM |
2955 | wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx); |
2956 | ||
2135f224 JM |
2957 | #ifdef HOSTAPD |
2958 | /* stop listening for EAPOL on this interface */ | |
2959 | del_ifidx(drv, ifidx); | |
2960 | #endif /* HOSTAPD */ | |
2961 | ||
0915d02c JM |
2962 | msg = nlmsg_alloc(); |
2963 | if (!msg) | |
2964 | goto nla_put_failure; | |
2965 | ||
2966 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, | |
2967 | 0, NL80211_CMD_DEL_INTERFACE, 0); | |
2968 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx); | |
2969 | ||
2970 | if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0) | |
2971 | return; | |
2972 | nla_put_failure: | |
e748062b | 2973 | wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d)", ifidx); |
0915d02c JM |
2974 | } |
2975 | ||
2976 | ||
a35187e7 KH |
2977 | static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv, |
2978 | const char *ifname, | |
2979 | enum nl80211_iftype iftype, | |
fbbfcbac | 2980 | const u8 *addr, int wds) |
0915d02c JM |
2981 | { |
2982 | struct nl_msg *msg, *flags = NULL; | |
2983 | int ifidx; | |
2984 | int ret = -ENOBUFS; | |
2985 | ||
2986 | msg = nlmsg_alloc(); | |
2987 | if (!msg) | |
2988 | return -1; | |
2989 | ||
2990 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, | |
2991 | 0, NL80211_CMD_NEW_INTERFACE, 0); | |
2992 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); | |
2993 | NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname); | |
2994 | NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype); | |
2995 | ||
2996 | if (iftype == NL80211_IFTYPE_MONITOR) { | |
2997 | int err; | |
2998 | ||
2999 | flags = nlmsg_alloc(); | |
3000 | if (!flags) | |
3001 | goto nla_put_failure; | |
3002 | ||
3003 | NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES); | |
3004 | ||
3005 | err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags); | |
3006 | ||
3007 | nlmsg_free(flags); | |
3008 | ||
3009 | if (err) | |
3010 | goto nla_put_failure; | |
fbbfcbac FF |
3011 | } else if (wds) { |
3012 | NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds); | |
0915d02c JM |
3013 | } |
3014 | ||
3015 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
3016 | if (ret) { | |
3017 | nla_put_failure: | |
a35187e7 KH |
3018 | wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)", |
3019 | ifname, ret, strerror(-ret)); | |
0915d02c JM |
3020 | return ret; |
3021 | } | |
3022 | ||
3023 | ifidx = if_nametoindex(ifname); | |
c6e8e8e4 JM |
3024 | wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d", |
3025 | ifname, ifidx); | |
0915d02c JM |
3026 | |
3027 | if (ifidx <= 0) | |
3028 | return -1; | |
3029 | ||
2135f224 JM |
3030 | #ifdef HOSTAPD |
3031 | /* start listening for EAPOL on this interface */ | |
3032 | add_ifidx(drv, ifidx); | |
7bfc47c3 | 3033 | #endif /* HOSTAPD */ |
2135f224 | 3034 | |
7bfc47c3 | 3035 | if (addr && iftype != NL80211_IFTYPE_MONITOR && |
2ac9688e | 3036 | linux_set_ifhwaddr(drv->ioctl_sock, ifname, addr)) { |
41d931ee JM |
3037 | nl80211_remove_iface(drv, ifidx); |
3038 | return -1; | |
2135f224 | 3039 | } |
2135f224 | 3040 | |
0915d02c JM |
3041 | return ifidx; |
3042 | } | |
22a7c9d7 JM |
3043 | |
3044 | ||
a35187e7 KH |
3045 | static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv, |
3046 | const char *ifname, enum nl80211_iftype iftype, | |
fbbfcbac | 3047 | const u8 *addr, int wds) |
a35187e7 KH |
3048 | { |
3049 | int ret; | |
3050 | ||
fbbfcbac | 3051 | ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds); |
a35187e7 KH |
3052 | |
3053 | /* if error occured and interface exists already */ | |
3054 | if (ret == -ENFILE && if_nametoindex(ifname)) { | |
3055 | wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname); | |
3056 | ||
3057 | /* Try to remove the interface that was already there. */ | |
3058 | nl80211_remove_iface(drv, if_nametoindex(ifname)); | |
3059 | ||
3060 | /* Try to create the interface again */ | |
fbbfcbac FF |
3061 | ret = nl80211_create_iface_once(drv, ifname, iftype, addr, |
3062 | wds); | |
a35187e7 KH |
3063 | } |
3064 | ||
4e5cb1a3 JM |
3065 | if (ret >= 0 && drv->disable_11b_rates) |
3066 | nl80211_disable_11b_rates(drv, ret, 1); | |
3067 | ||
a35187e7 KH |
3068 | return ret; |
3069 | } | |
0915d02c | 3070 | |
2135f224 | 3071 | |
0915d02c JM |
3072 | static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok) |
3073 | { | |
3074 | struct ieee80211_hdr *hdr; | |
f8b1f695 JM |
3075 | u16 fc; |
3076 | union wpa_event_data event; | |
0915d02c JM |
3077 | |
3078 | hdr = (struct ieee80211_hdr *) buf; | |
3079 | fc = le_to_host16(hdr->frame_control); | |
3080 | ||
f8b1f695 JM |
3081 | os_memset(&event, 0, sizeof(event)); |
3082 | event.tx_status.type = WLAN_FC_GET_TYPE(fc); | |
3083 | event.tx_status.stype = WLAN_FC_GET_STYPE(fc); | |
3084 | event.tx_status.dst = hdr->addr1; | |
3085 | event.tx_status.data = buf; | |
3086 | event.tx_status.data_len = len; | |
3087 | event.tx_status.ack = ok; | |
3088 | wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event); | |
0915d02c JM |
3089 | } |
3090 | ||
3091 | ||
4b9841d3 | 3092 | static void from_unknown_sta(struct wpa_driver_nl80211_data *drv, |
0d9fc3d8 | 3093 | u8 *buf, size_t len) |
0915d02c | 3094 | { |
f8b1f695 JM |
3095 | union wpa_event_data event; |
3096 | os_memset(&event, 0, sizeof(event)); | |
0d9fc3d8 | 3097 | event.rx_from_unknown.frame = buf; |
f8b1f695 JM |
3098 | event.rx_from_unknown.len = len; |
3099 | wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event); | |
4b9841d3 | 3100 | } |
0915d02c | 3101 | |
4b9841d3 JM |
3102 | |
3103 | static void handle_frame(struct wpa_driver_nl80211_data *drv, | |
2a8b7416 | 3104 | u8 *buf, size_t len, int datarate, int ssi_signal) |
4b9841d3 JM |
3105 | { |
3106 | struct ieee80211_hdr *hdr; | |
f8b1f695 JM |
3107 | u16 fc; |
3108 | union wpa_event_data event; | |
0915d02c JM |
3109 | |
3110 | hdr = (struct ieee80211_hdr *) buf; | |
3111 | fc = le_to_host16(hdr->frame_control); | |
0915d02c | 3112 | |
4b9841d3 | 3113 | switch (WLAN_FC_GET_TYPE(fc)) { |
0915d02c | 3114 | case WLAN_FC_TYPE_MGMT: |
f8b1f695 JM |
3115 | os_memset(&event, 0, sizeof(event)); |
3116 | event.rx_mgmt.frame = buf; | |
3117 | event.rx_mgmt.frame_len = len; | |
2a8b7416 JM |
3118 | event.rx_mgmt.datarate = datarate; |
3119 | event.rx_mgmt.ssi_signal = ssi_signal; | |
f8b1f695 | 3120 | wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event); |
0915d02c JM |
3121 | break; |
3122 | case WLAN_FC_TYPE_CTRL: | |
3123 | /* can only get here with PS-Poll frames */ | |
3124 | wpa_printf(MSG_DEBUG, "CTRL"); | |
0d9fc3d8 | 3125 | from_unknown_sta(drv, buf, len); |
0915d02c JM |
3126 | break; |
3127 | case WLAN_FC_TYPE_DATA: | |
0d9fc3d8 | 3128 | from_unknown_sta(drv, buf, len); |
0915d02c JM |
3129 | break; |
3130 | } | |
3131 | } | |
3132 | ||
3133 | ||
3134 | static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx) | |
3135 | { | |
3136 | struct wpa_driver_nl80211_data *drv = eloop_ctx; | |
3137 | int len; | |
3138 | unsigned char buf[3000]; | |
3139 | struct ieee80211_radiotap_iterator iter; | |
3140 | int ret; | |
2a8b7416 | 3141 | int datarate = 0, ssi_signal = 0; |
4b9841d3 | 3142 | int injected = 0, failed = 0, rxflags = 0; |
0915d02c JM |
3143 | |
3144 | len = recv(sock, buf, sizeof(buf), 0); | |
3145 | if (len < 0) { | |
3146 | perror("recv"); | |
3147 | return; | |
3148 | } | |
3149 | ||
504e905c JM |
3150 | if (drv->nlmode == NL80211_IFTYPE_STATION && !drv->probe_req_report) { |
3151 | wpa_printf(MSG_DEBUG, "nl80211: Ignore monitor interface " | |
3152 | "frame since Probe Request reporting is disabled"); | |
3153 | return; | |
3154 | } | |
3155 | ||
0915d02c JM |
3156 | if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) { |
3157 | printf("received invalid radiotap frame\n"); | |
3158 | return; | |
3159 | } | |
3160 | ||
0915d02c JM |
3161 | while (1) { |
3162 | ret = ieee80211_radiotap_iterator_next(&iter); | |
3163 | if (ret == -ENOENT) | |
3164 | break; | |
3165 | if (ret) { | |
3166 | printf("received invalid radiotap frame (%d)\n", ret); | |
3167 | return; | |
3168 | } | |
3169 | switch (iter.this_arg_index) { | |
3170 | case IEEE80211_RADIOTAP_FLAGS: | |
3171 | if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS) | |
3172 | len -= 4; | |
3173 | break; | |
3174 | case IEEE80211_RADIOTAP_RX_FLAGS: | |
3175 | rxflags = 1; | |
3176 | break; | |
3177 | case IEEE80211_RADIOTAP_TX_FLAGS: | |
3178 | injected = 1; | |
3179 | failed = le_to_host16((*(uint16_t *) iter.this_arg)) & | |
3180 | IEEE80211_RADIOTAP_F_TX_FAIL; | |
3181 | break; | |
3182 | case IEEE80211_RADIOTAP_DATA_RETRIES: | |
3183 | break; | |
3184 | case IEEE80211_RADIOTAP_CHANNEL: | |
2a8b7416 | 3185 | /* TODO: convert from freq/flags to channel number */ |
0915d02c JM |
3186 | break; |
3187 | case IEEE80211_RADIOTAP_RATE: | |
2a8b7416 | 3188 | datarate = *iter.this_arg * 5; |
0915d02c JM |
3189 | break; |
3190 | case IEEE80211_RADIOTAP_DB_ANTSIGNAL: | |
2a8b7416 | 3191 | ssi_signal = *iter.this_arg; |
0915d02c JM |
3192 | break; |
3193 | } | |
3194 | } | |
3195 | ||
3196 | if (rxflags && injected) | |
3197 | return; | |
3198 | ||
3199 | if (!injected) | |
4b9841d3 | 3200 | handle_frame(drv, buf + iter.max_length, |
2a8b7416 | 3201 | len - iter.max_length, datarate, ssi_signal); |
0915d02c | 3202 | else |
4b9841d3 JM |
3203 | handle_tx_callback(drv->ctx, buf + iter.max_length, |
3204 | len - iter.max_length, !failed); | |
0915d02c JM |
3205 | } |
3206 | ||
3207 | ||
3208 | /* | |
3209 | * we post-process the filter code later and rewrite | |
3210 | * this to the offset to the last instruction | |
3211 | */ | |
3212 | #define PASS 0xFF | |
3213 | #define FAIL 0xFE | |
3214 | ||
3215 | static struct sock_filter msock_filter_insns[] = { | |
3216 | /* | |
3217 | * do a little-endian load of the radiotap length field | |
3218 | */ | |
3219 | /* load lower byte into A */ | |
3220 | BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2), | |
3221 | /* put it into X (== index register) */ | |
3222 | BPF_STMT(BPF_MISC| BPF_TAX, 0), | |
3223 | /* load upper byte into A */ | |
3224 | BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3), | |
3225 | /* left-shift it by 8 */ | |
3226 | BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8), | |
3227 | /* or with X */ | |
3228 | BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0), | |
3229 | /* put result into X */ | |
3230 | BPF_STMT(BPF_MISC| BPF_TAX, 0), | |
3231 | ||
3232 | /* | |
3233 | * Allow management frames through, this also gives us those | |
3234 | * management frames that we sent ourselves with status | |
3235 | */ | |
3236 | /* load the lower byte of the IEEE 802.11 frame control field */ | |
3237 | BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0), | |
3238 | /* mask off frame type and version */ | |
3239 | BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF), | |
3240 | /* accept frame if it's both 0, fall through otherwise */ | |
3241 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0), | |
3242 | ||
3243 | /* | |
3244 | * TODO: add a bit to radiotap RX flags that indicates | |
3245 | * that the sending station is not associated, then | |
3246 | * add a filter here that filters on our DA and that flag | |
3247 | * to allow us to deauth frames to that bad station. | |
3248 | * | |
3249 | * Not a regression -- we didn't do it before either. | |
3250 | */ | |
3251 | ||
3252 | #if 0 | |
3253 | /* | |
fbbfcbac | 3254 | * drop non-data frames |
0915d02c JM |
3255 | */ |
3256 | /* load the lower byte of the frame control field */ | |
3257 | BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0), | |
3258 | /* mask off QoS bit */ | |
3259 | BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c), | |
3260 | /* drop non-data frames */ | |
3261 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL), | |
fbbfcbac | 3262 | #endif |
0915d02c | 3263 | /* load the upper byte of the frame control field */ |
fbbfcbac | 3264 | BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1), |
0915d02c JM |
3265 | /* mask off toDS/fromDS */ |
3266 | BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03), | |
fbbfcbac FF |
3267 | /* accept WDS frames */ |
3268 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0), | |
0915d02c JM |
3269 | |
3270 | /* | |
3271 | * add header length to index | |
3272 | */ | |
3273 | /* load the lower byte of the frame control field */ | |
3274 | BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0), | |
3275 | /* mask off QoS bit */ | |
3276 | BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80), | |
3277 | /* right shift it by 6 to give 0 or 2 */ | |
3278 | BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6), | |
3279 | /* add data frame header length */ | |
3280 | BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24), | |
3281 | /* add index, was start of 802.11 header */ | |
3282 | BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0), | |
3283 | /* move to index, now start of LL header */ | |
3284 | BPF_STMT(BPF_MISC | BPF_TAX, 0), | |
3285 | ||
3286 | /* | |
3287 | * Accept empty data frames, we use those for | |
3288 | * polling activity. | |
3289 | */ | |
3290 | BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0), | |
3291 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0), | |
3292 | ||
3293 | /* | |
3294 | * Accept EAPOL frames | |
3295 | */ | |
3296 | BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0), | |
3297 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL), | |
3298 | BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4), | |
3299 | BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL), | |
3300 | ||
3301 | /* keep these last two statements or change the code below */ | |
3302 | /* return 0 == "DROP" */ | |
3303 | BPF_STMT(BPF_RET | BPF_K, 0), | |
3304 | /* return ~0 == "keep all" */ | |
3305 | BPF_STMT(BPF_RET | BPF_K, ~0), | |
3306 | }; | |
3307 | ||
3308 | static struct sock_fprog msock_filter = { | |
3309 | .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]), | |
3310 | .filter = msock_filter_insns, | |
3311 | }; | |
3312 | ||
3313 | ||
3314 | static int add_monitor_filter(int s) | |
3315 | { | |
3316 | int idx; | |
3317 | ||
3318 | /* rewrite all PASS/FAIL jump offsets */ | |
3319 | for (idx = 0; idx < msock_filter.len; idx++) { | |
3320 | struct sock_filter *insn = &msock_filter_insns[idx]; | |
3321 | ||
3322 | if (BPF_CLASS(insn->code) == BPF_JMP) { | |
3323 | if (insn->code == (BPF_JMP|BPF_JA)) { | |
3324 | if (insn->k == PASS) | |
3325 | insn->k = msock_filter.len - idx - 2; | |
3326 | else if (insn->k == FAIL) | |
3327 | insn->k = msock_filter.len - idx - 3; | |
3328 | } | |
3329 | ||
3330 | if (insn->jt == PASS) | |
3331 | insn->jt = msock_filter.len - idx - 2; | |
3332 | else if (insn->jt == FAIL) | |
3333 | insn->jt = msock_filter.len - idx - 3; | |
3334 | ||
3335 | if (insn->jf == PASS) | |
3336 | insn->jf = msock_filter.len - idx - 2; | |
3337 | else if (insn->jf == FAIL) | |
3338 | insn->jf = msock_filter.len - idx - 3; | |
3339 | } | |
3340 | } | |
3341 | ||
3342 | if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, | |
3343 | &msock_filter, sizeof(msock_filter))) { | |
3344 | perror("SO_ATTACH_FILTER"); | |
3345 | return -1; | |
3346 | } | |
3347 | ||
3348 | return 0; | |
3349 | } | |
3350 | ||
3351 | ||
460456f8 JM |
3352 | static void nl80211_remove_monitor_interface( |
3353 | struct wpa_driver_nl80211_data *drv) | |
3354 | { | |
3355 | if (drv->monitor_ifidx >= 0) { | |
3356 | nl80211_remove_iface(drv, drv->monitor_ifidx); | |
3357 | drv->monitor_ifidx = -1; | |
3358 | } | |
504e905c JM |
3359 | if (drv->monitor_sock >= 0) { |
3360 | eloop_unregister_read_sock(drv->monitor_sock); | |
3361 | close(drv->monitor_sock); | |
3362 | drv->monitor_sock = -1; | |
3363 | } | |
460456f8 JM |
3364 | } |
3365 | ||
3366 | ||
0915d02c JM |
3367 | static int |
3368 | nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv) | |
3369 | { | |
3370 | char buf[IFNAMSIZ]; | |
3371 | struct sockaddr_ll ll; | |
3372 | int optval; | |
3373 | socklen_t optlen; | |
0915d02c | 3374 | |
a2e40bb6 | 3375 | snprintf(buf, IFNAMSIZ, "mon.%s", drv->first_bss.ifname); |
0915d02c JM |
3376 | buf[IFNAMSIZ - 1] = '\0'; |
3377 | ||
3378 | drv->monitor_ifidx = | |
fbbfcbac FF |
3379 | nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL, |
3380 | 0); | |
0915d02c JM |
3381 | |
3382 | if (drv->monitor_ifidx < 0) | |
3383 | return -1; | |
3384 | ||
34f2f814 | 3385 | if (linux_set_iface_flags(drv->ioctl_sock, buf, 1)) |
0915d02c | 3386 | goto error; |
0915d02c JM |
3387 | |
3388 | memset(&ll, 0, sizeof(ll)); | |
3389 | ll.sll_family = AF_PACKET; | |
3390 | ll.sll_ifindex = drv->monitor_ifidx; | |
3391 | drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); | |
3392 | if (drv->monitor_sock < 0) { | |
3393 | perror("socket[PF_PACKET,SOCK_RAW]"); | |
3394 | goto error; | |
3395 | } | |
3396 | ||
3397 | if (add_monitor_filter(drv->monitor_sock)) { | |
3398 | wpa_printf(MSG_INFO, "Failed to set socket filter for monitor " | |
3399 | "interface; do filtering in user space"); | |
3400 | /* This works, but will cost in performance. */ | |
3401 | } | |
3402 | ||
2135f224 | 3403 | if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) { |
0915d02c JM |
3404 | perror("monitor socket bind"); |
3405 | goto error; | |
3406 | } | |
3407 | ||
3408 | optlen = sizeof(optval); | |
3409 | optval = 20; | |
3410 | if (setsockopt | |
3411 | (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) { | |
3412 | perror("Failed to set socket priority"); | |
3413 | goto error; | |
3414 | } | |
3415 | ||
3416 | if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read, | |
3417 | drv, NULL)) { | |
3418 | printf("Could not register monitor read socket\n"); | |
3419 | goto error; | |
3420 | } | |
3421 | ||
3422 | return 0; | |
3423 | error: | |
460456f8 | 3424 | nl80211_remove_monitor_interface(drv); |
0915d02c JM |
3425 | return -1; |
3426 | } | |
3427 | ||
db149ac9 JM |
3428 | |
3429 | static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 }; | |
3430 | ||
3431 | static int wpa_driver_nl80211_hapd_send_eapol( | |
3432 | void *priv, const u8 *addr, const u8 *data, | |
3433 | size_t data_len, int encrypt, const u8 *own_addr) | |
3434 | { | |
a2e40bb6 FF |
3435 | struct i802_bss *bss = priv; |
3436 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
db149ac9 JM |
3437 | struct ieee80211_hdr *hdr; |
3438 | size_t len; | |
3439 | u8 *pos; | |
3440 | int res; | |
3441 | #if 0 /* FIX */ | |
0de39516 | 3442 | int qos = sta->flags & WPA_STA_WMM; |
db149ac9 JM |
3443 | #else |
3444 | int qos = 0; | |
3445 | #endif | |
3446 | ||
3447 | len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 + | |
3448 | data_len; | |
3449 | hdr = os_zalloc(len); | |
3450 | if (hdr == NULL) { | |
3451 | printf("malloc() failed for i802_send_data(len=%lu)\n", | |
3452 | (unsigned long) len); | |
3453 | return -1; | |
3454 | } | |
3455 | ||
3456 | hdr->frame_control = | |
3457 | IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA); | |
3458 | hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS); | |
3459 | if (encrypt) | |
3460 | hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP); | |
3461 | #if 0 /* To be enabled if qos determination is added above */ | |
3462 | if (qos) { | |
3463 | hdr->frame_control |= | |
3464 | host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4); | |
3465 | } | |
3466 | #endif | |
3467 | ||
3468 | memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN); | |
3469 | memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN); | |
3470 | memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN); | |
3471 | pos = (u8 *) (hdr + 1); | |
3472 | ||
3473 | #if 0 /* To be enabled if qos determination is added above */ | |
3474 | if (qos) { | |
3475 | /* add an empty QoS header if needed */ | |
3476 | pos[0] = 0; | |
3477 | pos[1] = 0; | |
3478 | pos += 2; | |
3479 | } | |
3480 | #endif | |
3481 | ||
3482 | memcpy(pos, rfc1042_header, sizeof(rfc1042_header)); | |
3483 | pos += sizeof(rfc1042_header); | |
3484 | WPA_PUT_BE16(pos, ETH_P_PAE); | |
3485 | pos += 2; | |
3486 | memcpy(pos, data, data_len); | |
3487 | ||
3488 | res = wpa_driver_nl80211_send_frame(drv, (u8 *) hdr, len, encrypt); | |
3489 | if (res < 0) { | |
3490 | wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - " | |
3491 | "failed: %d (%s)", | |
3492 | (unsigned long) len, errno, strerror(errno)); | |
3493 | } | |
7bf12757 | 3494 | os_free(hdr); |
db149ac9 JM |
3495 | |
3496 | return res; | |
3497 | } | |
3498 | ||
a8d6ffa4 | 3499 | |
7e76ee9c JM |
3500 | static u32 sta_flags_nl80211(int flags) |
3501 | { | |
3502 | u32 f = 0; | |
3503 | ||
0de39516 | 3504 | if (flags & WPA_STA_AUTHORIZED) |
7e76ee9c | 3505 | f |= BIT(NL80211_STA_FLAG_AUTHORIZED); |
0de39516 | 3506 | if (flags & WPA_STA_WMM) |
7e76ee9c | 3507 | f |= BIT(NL80211_STA_FLAG_WME); |
0de39516 | 3508 | if (flags & WPA_STA_SHORT_PREAMBLE) |
7e76ee9c | 3509 | f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE); |
0de39516 | 3510 | if (flags & WPA_STA_MFP) |
7e76ee9c JM |
3511 | f |= BIT(NL80211_STA_FLAG_MFP); |
3512 | ||
3513 | return f; | |
3514 | } | |
3515 | ||
3516 | ||
3234cba4 JM |
3517 | static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr, |
3518 | int total_flags, | |
4c32757d | 3519 | int flags_or, int flags_and) |
a8d6ffa4 | 3520 | { |
a2e40bb6 FF |
3521 | struct i802_bss *bss = priv; |
3522 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
a8d6ffa4 | 3523 | struct nl_msg *msg, *flags = NULL; |
7e76ee9c | 3524 | struct nl80211_sta_flag_update upd; |
a8d6ffa4 JM |
3525 | |
3526 | msg = nlmsg_alloc(); | |
3527 | if (!msg) | |
3528 | return -ENOMEM; | |
3529 | ||
3530 | flags = nlmsg_alloc(); | |
3531 | if (!flags) { | |
3532 | nlmsg_free(msg); | |
3533 | return -ENOMEM; | |
3534 | } | |
3535 | ||
3536 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, | |
3537 | 0, NL80211_CMD_SET_STATION, 0); | |
3538 | ||
3539 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, | |
3234cba4 | 3540 | if_nametoindex(bss->ifname)); |
a8d6ffa4 JM |
3541 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); |
3542 | ||
7e76ee9c JM |
3543 | /* |
3544 | * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This | |
3545 | * can be removed eventually. | |
3546 | */ | |
0de39516 | 3547 | if (total_flags & WPA_STA_AUTHORIZED) |
a8d6ffa4 JM |
3548 | NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED); |
3549 | ||
0de39516 | 3550 | if (total_flags & WPA_STA_WMM) |
a8d6ffa4 JM |
3551 | NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME); |
3552 | ||
0de39516 | 3553 | if (total_flags & WPA_STA_SHORT_PREAMBLE) |
a8d6ffa4 JM |
3554 | NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE); |
3555 | ||
0de39516 | 3556 | if (total_flags & WPA_STA_MFP) |
a8d6ffa4 JM |
3557 | NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP); |
3558 | ||
3559 | if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags)) | |
3560 | goto nla_put_failure; | |
3561 | ||
7e76ee9c JM |
3562 | os_memset(&upd, 0, sizeof(upd)); |
3563 | upd.mask = sta_flags_nl80211(flags_or | ~flags_and); | |
3564 | upd.set = sta_flags_nl80211(flags_or); | |
3565 | NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd); | |
3566 | ||
a8d6ffa4 JM |
3567 | nlmsg_free(flags); |
3568 | ||
3569 | return send_and_recv_msgs(drv, msg, NULL, NULL); | |
3570 | nla_put_failure: | |
3571 | nlmsg_free(flags); | |
3572 | return -ENOBUFS; | |
3573 | } | |
3574 | ||
0915d02c | 3575 | |
1581b38b JM |
3576 | static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv, |
3577 | struct wpa_driver_associate_params *params) | |
3578 | { | |
a2e40bb6 | 3579 | if (wpa_driver_nl80211_set_mode(&drv->first_bss, params->mode) || |
f019981a | 3580 | wpa_driver_nl80211_set_freq(drv, params->freq, 0, 0)) { |
460456f8 | 3581 | nl80211_remove_monitor_interface(drv); |
1581b38b | 3582 | return -1; |
0915d02c | 3583 | } |
1581b38b JM |
3584 | |
3585 | /* TODO: setup monitor interface (and add code somewhere to remove this | |
3586 | * when AP mode is stopped; associate with mode != 2 or drv_deinit) */ | |
1581b38b JM |
3587 | |
3588 | return 0; | |
3589 | } | |
1581b38b JM |
3590 | |
3591 | ||
5cc4d64b JM |
3592 | static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv) |
3593 | { | |
3594 | struct nl_msg *msg; | |
3595 | int ret = -1; | |
3596 | ||
3597 | msg = nlmsg_alloc(); | |
3598 | if (!msg) | |
3599 | return -1; | |
3600 | ||
3601 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, | |
3602 | NL80211_CMD_LEAVE_IBSS, 0); | |
3603 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); | |
3604 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
3605 | msg = NULL; | |
3606 | if (ret) { | |
3607 | wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d " | |
3608 | "(%s)", ret, strerror(-ret)); | |
3609 | goto nla_put_failure; | |
3610 | } | |
3611 | ||
3612 | ret = 0; | |
3613 | wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully"); | |
3614 | ||
3615 | nla_put_failure: | |
3616 | nlmsg_free(msg); | |
3617 | return ret; | |
3618 | } | |
3619 | ||
3620 | ||
3621 | static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv, | |
3622 | struct wpa_driver_associate_params *params) | |
3623 | { | |
3624 | struct nl_msg *msg; | |
3625 | int ret = -1; | |
3626 | int count = 0; | |
3627 | ||
3628 | wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex); | |
3629 | ||
a2e40bb6 | 3630 | if (wpa_driver_nl80211_set_mode(&drv->first_bss, params->mode)) { |
5cc4d64b JM |
3631 | wpa_printf(MSG_INFO, "nl80211: Failed to set interface into " |
3632 | "IBSS mode"); | |
3633 | return -1; | |
3634 | } | |
3635 | ||
3636 | retry: | |
3637 | msg = nlmsg_alloc(); | |
3638 | if (!msg) | |
3639 | return -1; | |
3640 | ||
3641 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, | |
3642 | NL80211_CMD_JOIN_IBSS, 0); | |
3643 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); | |
3644 | ||
3645 | if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid)) | |
3646 | goto nla_put_failure; | |
3647 | ||
3648 | wpa_hexdump_ascii(MSG_DEBUG, " * SSID", | |
3649 | params->ssid, params->ssid_len); | |
3650 | NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len, | |
3651 | params->ssid); | |
3652 | os_memcpy(drv->ssid, params->ssid, params->ssid_len); | |
3653 | drv->ssid_len = params->ssid_len; | |
3654 | ||
3655 | wpa_printf(MSG_DEBUG, " * freq=%d", params->freq); | |
3656 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq); | |
3657 | ||
3658 | ret = nl80211_set_conn_keys(params, msg); | |
3659 | if (ret) | |
3660 | goto nla_put_failure; | |
3661 | ||
a95795ad JM |
3662 | if (params->wpa_ie) { |
3663 | wpa_hexdump(MSG_DEBUG, | |
3664 | " * Extra IEs for Beacon/Probe Response frames", | |
3665 | params->wpa_ie, params->wpa_ie_len); | |
3666 | NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len, | |
3667 | params->wpa_ie); | |
3668 | } | |
3669 | ||
5cc4d64b JM |
3670 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); |
3671 | msg = NULL; | |
3672 | if (ret) { | |
3673 | wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)", | |
3674 | ret, strerror(-ret)); | |
3675 | count++; | |
3676 | if (ret == -EALREADY && count == 1) { | |
3677 | wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after " | |
3678 | "forced leave"); | |
3679 | nl80211_leave_ibss(drv); | |
3680 | nlmsg_free(msg); | |
3681 | goto retry; | |
3682 | } | |
3683 | ||
3684 | goto nla_put_failure; | |
3685 | } | |
3686 | ret = 0; | |
3687 | wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully"); | |
3688 | ||
3689 | nla_put_failure: | |
3690 | nlmsg_free(msg); | |
3691 | return ret; | |
3692 | } | |
3693 | ||
3694 | ||
cfaab580 ZY |
3695 | static int wpa_driver_nl80211_connect( |
3696 | struct wpa_driver_nl80211_data *drv, | |
3697 | struct wpa_driver_associate_params *params) | |
3698 | { | |
3699 | struct nl_msg *msg; | |
3700 | enum nl80211_auth_type type; | |
3701 | int ret = 0; | |
3702 | ||
3703 | msg = nlmsg_alloc(); | |
3704 | if (!msg) | |
3705 | return -1; | |
3706 | ||
3707 | wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex); | |
3708 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, | |
3709 | NL80211_CMD_CONNECT, 0); | |
3710 | ||
3711 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); | |
3712 | if (params->bssid) { | |
3713 | wpa_printf(MSG_DEBUG, " * bssid=" MACSTR, | |
3714 | MAC2STR(params->bssid)); | |
3715 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid); | |
3716 | } | |
3717 | if (params->freq) { | |
3718 | wpa_printf(MSG_DEBUG, " * freq=%d", params->freq); | |
3719 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq); | |
3720 | } | |
3721 | if (params->ssid) { | |
3722 | wpa_hexdump_ascii(MSG_DEBUG, " * SSID", | |
3723 | params->ssid, params->ssid_len); | |
3724 | NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len, | |
3725 | params->ssid); | |
3726 | if (params->ssid_len > sizeof(drv->ssid)) | |
3727 | goto nla_put_failure; | |
3728 | os_memcpy(drv->ssid, params->ssid, params->ssid_len); | |
3729 | drv->ssid_len = params->ssid_len; | |
3730 | } | |
3731 | wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len); | |
3732 | if (params->wpa_ie) | |
3733 | NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len, | |
3734 | params->wpa_ie); | |
3735 | ||
abd9fafa | 3736 | if (params->auth_alg & WPA_AUTH_ALG_OPEN) |
cfaab580 | 3737 | type = NL80211_AUTHTYPE_OPEN_SYSTEM; |
abd9fafa | 3738 | else if (params->auth_alg & WPA_AUTH_ALG_SHARED) |
cfaab580 | 3739 | type = NL80211_AUTHTYPE_SHARED_KEY; |
abd9fafa | 3740 | else if (params->auth_alg & WPA_AUTH_ALG_LEAP) |
cfaab580 | 3741 | type = NL80211_AUTHTYPE_NETWORK_EAP; |
abd9fafa | 3742 | else if (params->auth_alg & WPA_AUTH_ALG_FT) |
cfaab580 ZY |
3743 | type = NL80211_AUTHTYPE_FT; |
3744 | else | |
3745 | goto nla_put_failure; | |
3746 | ||
3747 | wpa_printf(MSG_DEBUG, " * Auth Type %d", type); | |
3748 | NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type); | |
3749 | ||
3750 | if (params->wpa_ie && params->wpa_ie_len) { | |
3751 | enum nl80211_wpa_versions ver; | |
3752 | ||
3753 | if (params->wpa_ie[0] == WLAN_EID_RSN) | |
3754 | ver = NL80211_WPA_VERSION_2; | |
3755 | else | |
3756 | ver = NL80211_WPA_VERSION_1; | |
3757 | ||
3758 | wpa_printf(MSG_DEBUG, " * WPA Version %d", ver); | |
3759 | NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver); | |
3760 | } | |
3761 | ||
3762 | if (params->pairwise_suite != CIPHER_NONE) { | |
c1bb3e0a | 3763 | int cipher; |
cfaab580 ZY |
3764 | |
3765 | switch (params->pairwise_suite) { | |
3766 | case CIPHER_WEP40: | |
3767 | cipher = WLAN_CIPHER_SUITE_WEP40; | |
3768 | break; | |
3769 | case CIPHER_WEP104: | |
3770 | cipher = WLAN_CIPHER_SUITE_WEP104; | |
3771 | break; | |
3772 | case CIPHER_CCMP: | |
3773 | cipher = WLAN_CIPHER_SUITE_CCMP; | |
3774 | break; | |
3775 | case CIPHER_TKIP: | |
3776 | default: | |
3777 | cipher = WLAN_CIPHER_SUITE_TKIP; | |
3778 | break; | |
3779 | } | |
3780 | NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher); | |
3781 | } | |
3782 | ||
3783 | if (params->group_suite != CIPHER_NONE) { | |
c1bb3e0a | 3784 | int cipher; |
cfaab580 ZY |
3785 | |
3786 | switch (params->group_suite) { | |
3787 | case CIPHER_WEP40: | |
3788 | cipher = WLAN_CIPHER_SUITE_WEP40; | |
3789 | break; | |
3790 | case CIPHER_WEP104: | |
3791 | cipher = WLAN_CIPHER_SUITE_WEP104; | |
3792 | break; | |
3793 | case CIPHER_CCMP: | |
3794 | cipher = WLAN_CIPHER_SUITE_CCMP; | |
3795 | break; | |
3796 | case CIPHER_TKIP: | |
3797 | default: | |
3798 | cipher = WLAN_CIPHER_SUITE_TKIP; | |
3799 | break; | |
3800 | } | |
3801 | NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher); | |
3802 | } | |
3803 | ||
3804 | if (params->key_mgmt_suite == KEY_MGMT_802_1X || | |
3805 | params->key_mgmt_suite == KEY_MGMT_PSK) { | |
3806 | int mgmt = WLAN_AKM_SUITE_PSK; | |
3807 | ||
3808 | switch (params->key_mgmt_suite) { | |
3809 | case KEY_MGMT_802_1X: | |
3810 | mgmt = WLAN_AKM_SUITE_8021X; | |
3811 | break; | |
3812 | case KEY_MGMT_PSK: | |
3813 | default: | |
3814 | mgmt = WLAN_AKM_SUITE_PSK; | |
3815 | break; | |
3816 | } | |
3817 | NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt); | |
3818 | } | |
3819 | ||
3820 | ret = nl80211_set_conn_keys(params, msg); | |
3821 | if (ret) | |
3822 | goto nla_put_failure; | |
3823 | ||
3824 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
3825 | msg = NULL; | |
3826 | if (ret) { | |
3827 | wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d " | |
3828 | "(%s)", ret, strerror(-ret)); | |
3829 | goto nla_put_failure; | |
3830 | } | |
3831 | ret = 0; | |
3832 | wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully"); | |
3833 | ||
3834 | nla_put_failure: | |
3835 | nlmsg_free(msg); | |
3836 | return ret; | |
3837 | ||
3838 | } | |
3839 | ||
3840 | ||
c2a04078 JM |
3841 | static int wpa_driver_nl80211_associate( |
3842 | void *priv, struct wpa_driver_associate_params *params) | |
3843 | { | |
a2e40bb6 FF |
3844 | struct i802_bss *bss = priv; |
3845 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
c2a04078 JM |
3846 | int ret = -1; |
3847 | struct nl_msg *msg; | |
3848 | ||
5cc4d64b | 3849 | if (params->mode == IEEE80211_MODE_AP) |
1581b38b | 3850 | return wpa_driver_nl80211_ap(drv, params); |
1581b38b | 3851 | |
5cc4d64b JM |
3852 | if (params->mode == IEEE80211_MODE_IBSS) |
3853 | return wpa_driver_nl80211_ibss(drv, params); | |
3854 | ||
4a867032 | 3855 | if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) { |
a2e40bb6 | 3856 | if (wpa_driver_nl80211_set_mode(priv, params->mode) < 0) |
4a867032 | 3857 | return -1; |
cfaab580 | 3858 | return wpa_driver_nl80211_connect(drv, params); |
4a867032 | 3859 | } |
cfaab580 | 3860 | |
c2a04078 JM |
3861 | drv->associated = 0; |
3862 | ||
3863 | msg = nlmsg_alloc(); | |
3864 | if (!msg) | |
3865 | return -1; | |
3866 | ||
3867 | wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)", | |
3868 | drv->ifindex); | |
3869 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, | |
3870 | NL80211_CMD_ASSOCIATE, 0); | |
3871 | ||
3872 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); | |
3873 | if (params->bssid) { | |
3874 | wpa_printf(MSG_DEBUG, " * bssid=" MACSTR, | |
3875 | MAC2STR(params->bssid)); | |
3876 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid); | |
3877 | } | |
3878 | if (params->freq) { | |
3879 | wpa_printf(MSG_DEBUG, " * freq=%d", params->freq); | |
3880 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq); | |
4832ecd7 JM |
3881 | drv->assoc_freq = params->freq; |
3882 | } else | |
3883 | drv->assoc_freq = 0; | |
c2a04078 JM |
3884 | if (params->ssid) { |
3885 | wpa_hexdump_ascii(MSG_DEBUG, " * SSID", | |
3886 | params->ssid, params->ssid_len); | |
3887 | NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len, | |
3888 | params->ssid); | |
fd05d64e JM |
3889 | if (params->ssid_len > sizeof(drv->ssid)) |
3890 | goto nla_put_failure; | |
3891 | os_memcpy(drv->ssid, params->ssid, params->ssid_len); | |
3892 | drv->ssid_len = params->ssid_len; | |
c2a04078 JM |
3893 | } |
3894 | wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len); | |
3895 | if (params->wpa_ie) | |
3896 | NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len, | |
3897 | params->wpa_ie); | |
3898 | ||
e572fa33 JM |
3899 | #ifdef CONFIG_IEEE80211W |
3900 | if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED) | |
3901 | NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED); | |
3902 | #endif /* CONFIG_IEEE80211W */ | |
3903 | ||
01652550 JM |
3904 | NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT); |
3905 | ||
62fa124c JM |
3906 | if (params->prev_bssid) { |
3907 | wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR, | |
3908 | MAC2STR(params->prev_bssid)); | |
3909 | NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN, | |
3910 | params->prev_bssid); | |
3911 | } | |
3912 | ||
c2a04078 JM |
3913 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); |
3914 | msg = NULL; | |
3915 | if (ret) { | |
3916 | wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d " | |
3917 | "(%s)", ret, strerror(-ret)); | |
8856462d | 3918 | nl80211_dump_scan(drv); |
c2a04078 JM |
3919 | goto nla_put_failure; |
3920 | } | |
3921 | ret = 0; | |
3922 | wpa_printf(MSG_DEBUG, "nl80211: Association request send " | |
3923 | "successfully"); | |
3924 | ||
3925 | nla_put_failure: | |
3926 | nlmsg_free(msg); | |
3927 | return ret; | |
3928 | } | |
3f5285e8 JM |
3929 | |
3930 | ||
ad1e68e6 JM |
3931 | static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv, |
3932 | int ifindex, int mode) | |
3f5285e8 | 3933 | { |
3f5285e8 | 3934 | struct nl_msg *msg; |
ad1e68e6 JM |
3935 | int ret = -ENOBUFS; |
3936 | ||
3937 | msg = nlmsg_alloc(); | |
3938 | if (!msg) | |
3939 | return -ENOMEM; | |
3940 | ||
3941 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, | |
3942 | 0, NL80211_CMD_SET_INTERFACE, 0); | |
3943 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); | |
3944 | NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode); | |
3945 | ||
3946 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
3947 | if (!ret) | |
3948 | return 0; | |
3949 | nla_put_failure: | |
3950 | wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:" | |
3951 | " %d (%s)", ifindex, mode, ret, strerror(-ret)); | |
3952 | return ret; | |
3953 | } | |
3954 | ||
3955 | ||
3956 | static int wpa_driver_nl80211_set_mode(void *priv, int mode) | |
3957 | { | |
a2e40bb6 FF |
3958 | struct i802_bss *bss = priv; |
3959 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
ad1e68e6 | 3960 | int ret = -1; |
1581b38b JM |
3961 | int nlmode; |
3962 | ||
3963 | switch (mode) { | |
3964 | case 0: | |
3965 | nlmode = NL80211_IFTYPE_STATION; | |
3966 | break; | |
3967 | case 1: | |
3968 | nlmode = NL80211_IFTYPE_ADHOC; | |
3969 | break; | |
3970 | case 2: | |
3971 | nlmode = NL80211_IFTYPE_AP; | |
3972 | break; | |
3973 | default: | |
3974 | return -1; | |
3975 | } | |
3f5285e8 | 3976 | |
ad1e68e6 JM |
3977 | if (nl80211_set_mode(drv, drv->ifindex, nlmode) == 0) { |
3978 | drv->nlmode = nlmode; | |
460456f8 JM |
3979 | ret = 0; |
3980 | goto done; | |
ad1e68e6 | 3981 | } |
3f5285e8 | 3982 | |
460456f8 | 3983 | if (nlmode == drv->nlmode) { |
c6e8e8e4 JM |
3984 | wpa_printf(MSG_DEBUG, "nl80211: Interface already in " |
3985 | "requested mode - ignore error"); | |
460456f8 JM |
3986 | ret = 0; |
3987 | goto done; /* Already in the requested mode */ | |
3988 | } | |
3f5285e8 | 3989 | |
3f5285e8 JM |
3990 | /* mac80211 doesn't allow mode changes while the device is up, so |
3991 | * take the device down, try to set the mode again, and bring the | |
3992 | * device back up. | |
3993 | */ | |
a2e40bb6 | 3994 | if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 0) == 0) { |
3f5285e8 | 3995 | /* Try to set the mode again while the interface is down */ |
ad1e68e6 | 3996 | ret = nl80211_set_mode(drv, drv->ifindex, nlmode); |
a2e40bb6 | 3997 | if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 1)) |
2135f224 | 3998 | ret = -1; |
3f5285e8 JM |
3999 | } |
4000 | ||
c6e8e8e4 JM |
4001 | if (!ret) { |
4002 | wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while " | |
4003 | "interface is down"); | |
ad1e68e6 | 4004 | drv->nlmode = nlmode; |
c6e8e8e4 | 4005 | } |
ad1e68e6 | 4006 | |
460456f8 | 4007 | done: |
460456f8 JM |
4008 | if (!ret && nlmode == NL80211_IFTYPE_AP) { |
4009 | /* Setup additional AP mode functionality if needed */ | |
4010 | if (drv->monitor_ifidx < 0 && | |
4011 | nl80211_create_monitor_interface(drv)) | |
4012 | return -1; | |
4013 | } else if (!ret && nlmode != NL80211_IFTYPE_AP) { | |
4014 | /* Remove additional AP mode functionality */ | |
4015 | nl80211_remove_monitor_interface(drv); | |
a2e40bb6 | 4016 | bss->beacon_set = 0; |
460456f8 | 4017 | } |
460456f8 | 4018 | |
c6e8e8e4 JM |
4019 | if (ret) |
4020 | wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d " | |
4021 | "from %d failed", nlmode, drv->nlmode); | |
4022 | ||
3f5285e8 JM |
4023 | return ret; |
4024 | } | |
4025 | ||
4026 | ||
3f5285e8 JM |
4027 | static int wpa_driver_nl80211_get_capa(void *priv, |
4028 | struct wpa_driver_capa *capa) | |
4029 | { | |
a2e40bb6 FF |
4030 | struct i802_bss *bss = priv; |
4031 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
3f5285e8 JM |
4032 | if (!drv->has_capability) |
4033 | return -1; | |
4034 | os_memcpy(capa, &drv->capa, sizeof(*capa)); | |
4035 | return 0; | |
4036 | } | |
4037 | ||
4038 | ||
4039 | static int wpa_driver_nl80211_set_operstate(void *priv, int state) | |
4040 | { | |
a2e40bb6 FF |
4041 | struct i802_bss *bss = priv; |
4042 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
3f5285e8 JM |
4043 | |
4044 | wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)", | |
4045 | __func__, drv->operstate, state, state ? "UP" : "DORMANT"); | |
4046 | drv->operstate = state; | |
08063178 | 4047 | return netlink_send_oper_ifla(drv->netlink, drv->ifindex, -1, |
e2d02c29 | 4048 | state ? IF_OPER_UP : IF_OPER_DORMANT); |
3f5285e8 JM |
4049 | } |
4050 | ||
01652550 JM |
4051 | |
4052 | static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized) | |
4053 | { | |
a2e40bb6 FF |
4054 | struct i802_bss *bss = priv; |
4055 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
01652550 JM |
4056 | struct nl_msg *msg; |
4057 | struct nl80211_sta_flag_update upd; | |
4058 | ||
4059 | msg = nlmsg_alloc(); | |
4060 | if (!msg) | |
4061 | return -ENOMEM; | |
4062 | ||
4063 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, | |
4064 | 0, NL80211_CMD_SET_STATION, 0); | |
4065 | ||
4066 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, | |
a2e40bb6 | 4067 | if_nametoindex(bss->ifname)); |
01652550 JM |
4068 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid); |
4069 | ||
4070 | os_memset(&upd, 0, sizeof(upd)); | |
4071 | upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED); | |
4072 | if (authorized) | |
4073 | upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED); | |
4074 | NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd); | |
4075 | ||
4076 | return send_and_recv_msgs(drv, msg, NULL, NULL); | |
4077 | nla_put_failure: | |
4078 | return -ENOBUFS; | |
4079 | } | |
4080 | ||
3f5285e8 | 4081 | |
c5121837 JM |
4082 | #ifdef HOSTAPD |
4083 | ||
c5121837 JM |
4084 | static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx) |
4085 | { | |
4086 | int i; | |
4087 | int *old; | |
4088 | ||
4089 | wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d", | |
4090 | ifidx); | |
4091 | for (i = 0; i < drv->num_if_indices; i++) { | |
4092 | if (drv->if_indices[i] == 0) { | |
4093 | drv->if_indices[i] = ifidx; | |
4094 | return; | |
4095 | } | |
4096 | } | |
4097 | ||
4098 | if (drv->if_indices != drv->default_if_indices) | |
4099 | old = drv->if_indices; | |
4100 | else | |
4101 | old = NULL; | |
4102 | ||
7bf12757 JM |
4103 | drv->if_indices = os_realloc(old, |
4104 | sizeof(int) * (drv->num_if_indices + 1)); | |
c5121837 JM |
4105 | if (!drv->if_indices) { |
4106 | if (!old) | |
4107 | drv->if_indices = drv->default_if_indices; | |
4108 | else | |
4109 | drv->if_indices = old; | |
4110 | wpa_printf(MSG_ERROR, "Failed to reallocate memory for " | |
4111 | "interfaces"); | |
4112 | wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx); | |
4113 | return; | |
4114 | } | |
4115 | drv->if_indices[drv->num_if_indices] = ifidx; | |
4116 | drv->num_if_indices++; | |
4117 | } | |
4118 | ||
4119 | ||
4120 | static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx) | |
4121 | { | |
4122 | int i; | |
4123 | ||
4124 | for (i = 0; i < drv->num_if_indices; i++) { | |
4125 | if (drv->if_indices[i] == ifidx) { | |
4126 | drv->if_indices[i] = 0; | |
4127 | break; | |
4128 | } | |
4129 | } | |
4130 | } | |
4131 | ||
4132 | ||
4133 | static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx) | |
4134 | { | |
4135 | int i; | |
4136 | ||
4137 | for (i = 0; i < drv->num_if_indices; i++) | |
4138 | if (drv->if_indices[i] == ifidx) | |
4139 | return 1; | |
4140 | ||
4141 | return 0; | |
4142 | } | |
4143 | ||
4144 | ||
c5121837 JM |
4145 | static inline int min_int(int a, int b) |
4146 | { | |
4147 | if (a < b) | |
4148 | return a; | |
4149 | return b; | |
4150 | } | |
4151 | ||
4152 | ||
4153 | static int get_key_handler(struct nl_msg *msg, void *arg) | |
4154 | { | |
4155 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
4156 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
4157 | ||
4158 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
4159 | genlmsg_attrlen(gnlh, 0), NULL); | |
4160 | ||
4161 | /* | |
4162 | * TODO: validate the key index and mac address! | |
4163 | * Otherwise, there's a race condition as soon as | |
4164 | * the kernel starts sending key notifications. | |
4165 | */ | |
4166 | ||
4167 | if (tb[NL80211_ATTR_KEY_SEQ]) | |
4168 | memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]), | |
4169 | min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6)); | |
4170 | return NL_SKIP; | |
4171 | } | |
4172 | ||
4173 | ||
4174 | static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr, | |
4175 | int idx, u8 *seq) | |
4176 | { | |
a2e40bb6 FF |
4177 | struct i802_bss *bss = priv; |
4178 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
c5121837 JM |
4179 | struct nl_msg *msg; |
4180 | ||
4181 | msg = nlmsg_alloc(); | |
4182 | if (!msg) | |
4183 | return -ENOMEM; | |
4184 | ||
4185 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, | |
4186 | 0, NL80211_CMD_GET_KEY, 0); | |
4187 | ||
4188 | if (addr) | |
4189 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); | |
4190 | NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx); | |
4191 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface)); | |
4192 | ||
4193 | memset(seq, 0, 6); | |
4194 | ||
4195 | return send_and_recv_msgs(drv, msg, get_key_handler, seq); | |
4196 | nla_put_failure: | |
4197 | return -ENOBUFS; | |
4198 | } | |
4199 | ||
4200 | ||
4201 | static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates, | |
4202 | int mode) | |
4203 | { | |
a2e40bb6 FF |
4204 | struct i802_bss *bss = priv; |
4205 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
c5121837 JM |
4206 | struct nl_msg *msg; |
4207 | u8 rates[NL80211_MAX_SUPP_RATES]; | |
4208 | u8 rates_len = 0; | |
4209 | int i; | |
4210 | ||
4211 | msg = nlmsg_alloc(); | |
4212 | if (!msg) | |
4213 | return -ENOMEM; | |
4214 | ||
4215 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, | |
4216 | NL80211_CMD_SET_BSS, 0); | |
4217 | ||
4218 | for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++) | |
4219 | rates[rates_len++] = basic_rates[i] / 5; | |
4220 | ||
4221 | NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates); | |
4222 | ||
a2e40bb6 | 4223 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname)); |
c5121837 JM |
4224 | |
4225 | return send_and_recv_msgs(drv, msg, NULL, NULL); | |
4226 | nla_put_failure: | |
4227 | return -ENOBUFS; | |
4228 | } | |
4229 | ||
e3802622 JM |
4230 | #endif /* HOSTAPD */ |
4231 | ||
c5121837 | 4232 | |
c5121837 JM |
4233 | /* Set kernel driver on given frequency (MHz) */ |
4234 | static int i802_set_freq(void *priv, struct hostapd_freq_params *freq) | |
4235 | { | |
a2e40bb6 FF |
4236 | struct i802_bss *bss = priv; |
4237 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
f019981a JM |
4238 | return wpa_driver_nl80211_set_freq(drv, freq->freq, freq->ht_enabled, |
4239 | freq->sec_channel_offset); | |
c5121837 JM |
4240 | } |
4241 | ||
4242 | ||
e3802622 JM |
4243 | #ifdef HOSTAPD |
4244 | ||
c5121837 JM |
4245 | static int i802_set_rts(void *priv, int rts) |
4246 | { | |
a2e40bb6 FF |
4247 | struct i802_bss *bss = priv; |
4248 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
ad649451 JM |
4249 | struct nl_msg *msg; |
4250 | int ret = -ENOBUFS; | |
4251 | u32 val; | |
c5121837 | 4252 | |
ad649451 JM |
4253 | msg = nlmsg_alloc(); |
4254 | if (!msg) | |
4255 | return -ENOMEM; | |
c5121837 | 4256 | |
ad649451 JM |
4257 | if (rts >= 2347) |
4258 | val = (u32) -1; | |
4259 | else | |
4260 | val = rts; | |
c5121837 | 4261 | |
ad649451 JM |
4262 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, |
4263 | 0, NL80211_CMD_SET_WIPHY, 0); | |
4264 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); | |
4265 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val); | |
4266 | ||
4267 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
4268 | if (!ret) | |
4269 | return 0; | |
4270 | nla_put_failure: | |
4271 | wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: " | |
4272 | "%d (%s)", rts, ret, strerror(-ret)); | |
4273 | return ret; | |
c5121837 JM |
4274 | } |
4275 | ||
4276 | ||
4277 | static int i802_set_frag(void *priv, int frag) | |
4278 | { | |
a2e40bb6 FF |
4279 | struct i802_bss *bss = priv; |
4280 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
ad649451 JM |
4281 | struct nl_msg *msg; |
4282 | int ret = -ENOBUFS; | |
4283 | u32 val; | |
c5121837 | 4284 | |
ad649451 JM |
4285 | msg = nlmsg_alloc(); |
4286 | if (!msg) | |
4287 | return -ENOMEM; | |
c5121837 | 4288 | |
ad649451 JM |
4289 | if (frag >= 2346) |
4290 | val = (u32) -1; | |
4291 | else | |
4292 | val = frag; | |
c5121837 | 4293 | |
ad649451 JM |
4294 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, |
4295 | 0, NL80211_CMD_SET_WIPHY, 0); | |
4296 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); | |
4297 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val); | |
4298 | ||
4299 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
4300 | if (!ret) | |
4301 | return 0; | |
4302 | nla_put_failure: | |
4303 | wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold " | |
4304 | "%d: %d (%s)", frag, ret, strerror(-ret)); | |
4305 | return ret; | |
c5121837 JM |
4306 | } |
4307 | ||
4308 | ||
c5121837 JM |
4309 | static int i802_flush(void *priv) |
4310 | { | |
a2e40bb6 FF |
4311 | struct i802_bss *bss = priv; |
4312 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
c5121837 JM |
4313 | struct nl_msg *msg; |
4314 | ||
4315 | msg = nlmsg_alloc(); | |
4316 | if (!msg) | |
4317 | return -1; | |
4318 | ||
4319 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, | |
4320 | 0, NL80211_CMD_DEL_STATION, 0); | |
4321 | ||
4322 | /* | |
4323 | * XXX: FIX! this needs to flush all VLANs too | |
4324 | */ | |
4325 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, | |
a2e40bb6 | 4326 | if_nametoindex(bss->ifname)); |
c5121837 JM |
4327 | |
4328 | return send_and_recv_msgs(drv, msg, NULL, NULL); | |
4329 | nla_put_failure: | |
4330 | return -ENOBUFS; | |
4331 | } | |
4332 | ||
4333 | ||
4334 | static int get_sta_handler(struct nl_msg *msg, void *arg) | |
4335 | { | |
4336 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
4337 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
4338 | struct hostap_sta_driver_data *data = arg; | |
4339 | struct nlattr *stats[NL80211_STA_INFO_MAX + 1]; | |
4340 | static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = { | |
4341 | [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 }, | |
4342 | [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 }, | |
4343 | [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 }, | |
4344 | [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 }, | |
4345 | [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 }, | |
4346 | }; | |
4347 | ||
4348 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
4349 | genlmsg_attrlen(gnlh, 0), NULL); | |
4350 | ||
4351 | /* | |
4352 | * TODO: validate the interface and mac address! | |
4353 | * Otherwise, there's a race condition as soon as | |
4354 | * the kernel starts sending station notifications. | |
4355 | */ | |
4356 | ||
4357 | if (!tb[NL80211_ATTR_STA_INFO]) { | |
4358 | wpa_printf(MSG_DEBUG, "sta stats missing!"); | |
4359 | return NL_SKIP; | |
4360 | } | |
4361 | if (nla_parse_nested(stats, NL80211_STA_INFO_MAX, | |
4362 | tb[NL80211_ATTR_STA_INFO], | |
4363 | stats_policy)) { | |
4364 | wpa_printf(MSG_DEBUG, "failed to parse nested attributes!"); | |
4365 | return NL_SKIP; | |
4366 | } | |
4367 | ||
4368 | if (stats[NL80211_STA_INFO_INACTIVE_TIME]) | |
4369 | data->inactive_msec = | |
4370 | nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]); | |
4371 | if (stats[NL80211_STA_INFO_RX_BYTES]) | |
4372 | data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]); | |
4373 | if (stats[NL80211_STA_INFO_TX_BYTES]) | |
4374 | data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]); | |
4375 | if (stats[NL80211_STA_INFO_RX_PACKETS]) | |
4376 | data->rx_packets = | |
4377 | nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]); | |
4378 | if (stats[NL80211_STA_INFO_TX_PACKETS]) | |
4379 | data->tx_packets = | |
4380 | nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]); | |
4381 | ||
4382 | return NL_SKIP; | |
4383 | } | |
4384 | ||
4385 | static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data, | |
4386 | const u8 *addr) | |
4387 | { | |
a2e40bb6 FF |
4388 | struct i802_bss *bss = priv; |
4389 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
c5121837 JM |
4390 | struct nl_msg *msg; |
4391 | ||
4392 | os_memset(data, 0, sizeof(*data)); | |
4393 | msg = nlmsg_alloc(); | |
4394 | if (!msg) | |
4395 | return -ENOMEM; | |
4396 | ||
4397 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, | |
4398 | 0, NL80211_CMD_GET_STATION, 0); | |
4399 | ||
4400 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); | |
a2e40bb6 | 4401 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname)); |
c5121837 JM |
4402 | |
4403 | return send_and_recv_msgs(drv, msg, get_sta_handler, data); | |
4404 | nla_put_failure: | |
4405 | return -ENOBUFS; | |
4406 | } | |
4407 | ||
4408 | ||
c5121837 JM |
4409 | static int i802_set_tx_queue_params(void *priv, int queue, int aifs, |
4410 | int cw_min, int cw_max, int burst_time) | |
4411 | { | |
a2e40bb6 FF |
4412 | struct i802_bss *bss = priv; |
4413 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
c5121837 JM |
4414 | struct nl_msg *msg; |
4415 | struct nlattr *txq, *params; | |
4416 | ||
4417 | msg = nlmsg_alloc(); | |
4418 | if (!msg) | |
4419 | return -1; | |
4420 | ||
4421 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, | |
4422 | 0, NL80211_CMD_SET_WIPHY, 0); | |
4423 | ||
a2e40bb6 | 4424 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname)); |
c5121837 JM |
4425 | |
4426 | txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS); | |
4427 | if (!txq) | |
4428 | goto nla_put_failure; | |
4429 | ||
4430 | /* We are only sending parameters for a single TXQ at a time */ | |
4431 | params = nla_nest_start(msg, 1); | |
4432 | if (!params) | |
4433 | goto nla_put_failure; | |
4434 | ||
4435 | NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, queue); | |
4436 | /* Burst time is configured in units of 0.1 msec and TXOP parameter in | |
4437 | * 32 usec, so need to convert the value here. */ | |
4438 | NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32); | |
4439 | NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min); | |
4440 | NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max); | |
4441 | NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs); | |
4442 | ||
4443 | nla_nest_end(msg, params); | |
4444 | ||
4445 | nla_nest_end(msg, txq); | |
4446 | ||
4447 | if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0) | |
4448 | return 0; | |
4449 | nla_put_failure: | |
4450 | return -1; | |
4451 | } | |
4452 | ||
4453 | ||
c5121837 JM |
4454 | static int i802_set_bss(void *priv, int cts, int preamble, int slot) |
4455 | { | |
a2e40bb6 FF |
4456 | struct i802_bss *bss = priv; |
4457 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
c5121837 JM |
4458 | struct nl_msg *msg; |
4459 | ||
4460 | msg = nlmsg_alloc(); | |
4461 | if (!msg) | |
4462 | return -ENOMEM; | |
4463 | ||
4464 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, | |
4465 | NL80211_CMD_SET_BSS, 0); | |
4466 | ||
4467 | if (cts >= 0) | |
4468 | NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts); | |
4469 | if (preamble >= 0) | |
4470 | NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble); | |
4471 | if (slot >= 0) | |
4472 | NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot); | |
4473 | ||
a2e40bb6 | 4474 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(bss->ifname)); |
c5121837 JM |
4475 | |
4476 | return send_and_recv_msgs(drv, msg, NULL, NULL); | |
4477 | nla_put_failure: | |
4478 | return -ENOBUFS; | |
4479 | } | |
4480 | ||
4481 | ||
4482 | static int i802_set_cts_protect(void *priv, int value) | |
4483 | { | |
4484 | return i802_set_bss(priv, value, -1, -1); | |
4485 | } | |
4486 | ||
4487 | ||
4488 | static int i802_set_preamble(void *priv, int value) | |
4489 | { | |
4490 | return i802_set_bss(priv, -1, value, -1); | |
4491 | } | |
4492 | ||
4493 | ||
4494 | static int i802_set_short_slot_time(void *priv, int value) | |
4495 | { | |
4496 | return i802_set_bss(priv, -1, -1, value); | |
4497 | } | |
4498 | ||
4499 | ||
c5121837 JM |
4500 | static int i802_set_sta_vlan(void *priv, const u8 *addr, |
4501 | const char *ifname, int vlan_id) | |
4502 | { | |
a2e40bb6 FF |
4503 | struct i802_bss *bss = priv; |
4504 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
c5121837 | 4505 | struct nl_msg *msg; |
cd1d72c1 | 4506 | int ret = -ENOBUFS; |
c5121837 JM |
4507 | |
4508 | msg = nlmsg_alloc(); | |
4509 | if (!msg) | |
4510 | return -ENOMEM; | |
4511 | ||
4512 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, | |
4513 | 0, NL80211_CMD_SET_STATION, 0); | |
4514 | ||
4515 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, | |
a2e40bb6 | 4516 | if_nametoindex(bss->ifname)); |
c5121837 | 4517 | NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr); |
1c766b09 | 4518 | NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN, |
c5121837 JM |
4519 | if_nametoindex(ifname)); |
4520 | ||
cd1d72c1 JM |
4521 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); |
4522 | if (ret < 0) { | |
4523 | wpa_printf(MSG_ERROR, "nl80211: NL80211_ATTR_STA_VLAN (addr=" | |
4524 | MACSTR " ifname=%s vlan_id=%d) failed: %d (%s)", | |
4525 | MAC2STR(addr), ifname, vlan_id, ret, | |
4526 | strerror(-ret)); | |
4527 | } | |
c5121837 | 4528 | nla_put_failure: |
cd1d72c1 | 4529 | return ret; |
c5121837 JM |
4530 | } |
4531 | ||
4532 | ||
fbbfcbac FF |
4533 | static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val) |
4534 | { | |
a2e40bb6 FF |
4535 | struct i802_bss *bss = priv; |
4536 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
e748062b | 4537 | char name[IFNAMSIZ + 1]; |
fbbfcbac | 4538 | |
a2e40bb6 | 4539 | os_snprintf(name, sizeof(name), "%s.sta%d", bss->ifname, aid); |
e748062b JM |
4540 | wpa_printf(MSG_DEBUG, "nl80211: Set WDS STA addr=" MACSTR |
4541 | " aid=%d val=%d name=%s", MAC2STR(addr), aid, val, name); | |
fbbfcbac | 4542 | if (val) { |
a2e40bb6 | 4543 | if (nl80211_create_iface(drv, name, NL80211_IFTYPE_AP_VLAN, |
fbbfcbac FF |
4544 | NULL, 1) < 0) |
4545 | return -1; | |
34f2f814 | 4546 | linux_set_iface_flags(drv->ioctl_sock, name, 1); |
fbbfcbac FF |
4547 | return i802_set_sta_vlan(priv, addr, name, 0); |
4548 | } else { | |
a2e40bb6 | 4549 | i802_set_sta_vlan(priv, addr, bss->ifname, 0); |
fbbfcbac FF |
4550 | return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN, |
4551 | name); | |
4552 | } | |
4553 | } | |
4554 | ||
4555 | ||
c5121837 JM |
4556 | static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx) |
4557 | { | |
4558 | struct wpa_driver_nl80211_data *drv = eloop_ctx; | |
4559 | struct sockaddr_ll lladdr; | |
4560 | unsigned char buf[3000]; | |
4561 | int len; | |
4562 | socklen_t fromlen = sizeof(lladdr); | |
4563 | ||
4564 | len = recvfrom(sock, buf, sizeof(buf), 0, | |
4565 | (struct sockaddr *)&lladdr, &fromlen); | |
4566 | if (len < 0) { | |
4567 | perror("recv"); | |
4568 | return; | |
4569 | } | |
4570 | ||
baac6490 JM |
4571 | if (have_ifidx(drv, lladdr.sll_ifindex)) |
4572 | drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len); | |
c5121837 JM |
4573 | } |
4574 | ||
4575 | ||
c5121837 JM |
4576 | static int i802_get_inact_sec(void *priv, const u8 *addr) |
4577 | { | |
4578 | struct hostap_sta_driver_data data; | |
4579 | int ret; | |
4580 | ||
4581 | data.inactive_msec = (unsigned long) -1; | |
4582 | ret = i802_read_sta_data(priv, &data, addr); | |
4583 | if (ret || data.inactive_msec == (unsigned long) -1) | |
4584 | return -1; | |
4585 | return data.inactive_msec / 1000; | |
4586 | } | |
4587 | ||
4588 | ||
4589 | static int i802_sta_clear_stats(void *priv, const u8 *addr) | |
4590 | { | |
4591 | #if 0 | |
4592 | /* TODO */ | |
4593 | #endif | |
4594 | return 0; | |
4595 | } | |
4596 | ||
4597 | ||
731723a5 JM |
4598 | static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr, |
4599 | int reason) | |
c5121837 | 4600 | { |
a2e40bb6 | 4601 | struct i802_bss *bss = priv; |
c5121837 JM |
4602 | struct ieee80211_mgmt mgmt; |
4603 | ||
4604 | memset(&mgmt, 0, sizeof(mgmt)); | |
4605 | mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, | |
4606 | WLAN_FC_STYPE_DEAUTH); | |
4607 | memcpy(mgmt.da, addr, ETH_ALEN); | |
731723a5 JM |
4608 | memcpy(mgmt.sa, own_addr, ETH_ALEN); |
4609 | memcpy(mgmt.bssid, own_addr, ETH_ALEN); | |
c5121837 | 4610 | mgmt.u.deauth.reason_code = host_to_le16(reason); |
a2e40bb6 | 4611 | return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt, |
9f324b61 JM |
4612 | IEEE80211_HDRLEN + |
4613 | sizeof(mgmt.u.deauth)); | |
c5121837 JM |
4614 | } |
4615 | ||
4616 | ||
731723a5 JM |
4617 | static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr, |
4618 | int reason) | |
c5121837 | 4619 | { |
a2e40bb6 | 4620 | struct i802_bss *bss = priv; |
c5121837 JM |
4621 | struct ieee80211_mgmt mgmt; |
4622 | ||
4623 | memset(&mgmt, 0, sizeof(mgmt)); | |
4624 | mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT, | |
4625 | WLAN_FC_STYPE_DISASSOC); | |
4626 | memcpy(mgmt.da, addr, ETH_ALEN); | |
731723a5 JM |
4627 | memcpy(mgmt.sa, own_addr, ETH_ALEN); |
4628 | memcpy(mgmt.bssid, own_addr, ETH_ALEN); | |
c5121837 | 4629 | mgmt.u.disassoc.reason_code = host_to_le16(reason); |
a2e40bb6 | 4630 | return wpa_driver_nl80211_send_mlme(bss, (u8 *) &mgmt, |
9f324b61 JM |
4631 | IEEE80211_HDRLEN + |
4632 | sizeof(mgmt.u.disassoc)); | |
c5121837 JM |
4633 | } |
4634 | ||
4635 | ||
94627f6c JM |
4636 | static int i802_check_bridge(struct wpa_driver_nl80211_data *drv, |
4637 | const char *brname, const char *ifname) | |
4638 | { | |
4639 | int ifindex; | |
4640 | char in_br[IFNAMSIZ]; | |
4641 | ||
4642 | os_strlcpy(drv->brname, brname, IFNAMSIZ); | |
4643 | ifindex = if_nametoindex(brname); | |
4644 | if (ifindex == 0) { | |
4645 | /* | |
4646 | * Bridge was configured, but the bridge device does | |
4647 | * not exist. Try to add it now. | |
4648 | */ | |
4649 | if (linux_br_add(drv->ioctl_sock, brname) < 0) { | |
4650 | wpa_printf(MSG_ERROR, "nl80211: Failed to add the " | |
4651 | "bridge interface %s: %s", | |
4652 | brname, strerror(errno)); | |
4653 | return -1; | |
4654 | } | |
4655 | drv->added_bridge = 1; | |
4656 | add_ifidx(drv, if_nametoindex(brname)); | |
4657 | } | |
4658 | ||
4659 | if (linux_br_get(in_br, ifname) == 0) { | |
4660 | if (os_strcmp(in_br, brname) == 0) | |
4661 | return 0; /* already in the bridge */ | |
4662 | ||
4663 | wpa_printf(MSG_DEBUG, "nl80211: Removing interface %s from " | |
4664 | "bridge %s", ifname, in_br); | |
4665 | if (linux_br_del_if(drv->ioctl_sock, in_br, ifname) < 0) { | |
4666 | wpa_printf(MSG_ERROR, "nl80211: Failed to " | |
4667 | "remove interface %s from bridge " | |
4668 | "%s: %s", | |
4669 | ifname, brname, strerror(errno)); | |
4670 | return -1; | |
4671 | } | |
4672 | } | |
4673 | ||
4674 | wpa_printf(MSG_DEBUG, "nl80211: Adding interface %s into bridge %s", | |
4675 | ifname, brname); | |
4676 | if (linux_br_add_if(drv->ioctl_sock, brname, ifname) < 0) { | |
4677 | wpa_printf(MSG_ERROR, "nl80211: Failed to add interface %s " | |
4678 | "into bridge %s: %s", | |
4679 | ifname, brname, strerror(errno)); | |
4680 | return -1; | |
4681 | } | |
4682 | drv->added_if_into_bridge = 1; | |
4683 | ||
4684 | return 0; | |
4685 | } | |
4686 | ||
4687 | ||
92f475b4 JM |
4688 | static void *i802_init(struct hostapd_data *hapd, |
4689 | struct wpa_init_params *params) | |
c5121837 JM |
4690 | { |
4691 | struct wpa_driver_nl80211_data *drv; | |
a2e40bb6 | 4692 | struct i802_bss *bss; |
c5121837 | 4693 | size_t i; |
94627f6c JM |
4694 | char brname[IFNAMSIZ]; |
4695 | int ifindex, br_ifindex; | |
4696 | int br_added = 0; | |
c5121837 | 4697 | |
a2e40bb6 FF |
4698 | bss = wpa_driver_nl80211_init(hapd, params->ifname); |
4699 | if (bss == NULL) | |
c5121837 | 4700 | return NULL; |
c5121837 | 4701 | |
a2e40bb6 | 4702 | drv = bss->drv; |
94627f6c JM |
4703 | if (linux_br_get(brname, params->ifname) == 0) { |
4704 | wpa_printf(MSG_DEBUG, "nl80211: Interface %s is in bridge %s", | |
4705 | params->ifname, brname); | |
4706 | br_ifindex = if_nametoindex(brname); | |
4707 | } else { | |
4708 | brname[0] = '\0'; | |
4709 | br_ifindex = 0; | |
4710 | } | |
4711 | ||
c5121837 JM |
4712 | drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int); |
4713 | drv->if_indices = drv->default_if_indices; | |
92f475b4 | 4714 | for (i = 0; i < params->num_bridge; i++) { |
94627f6c JM |
4715 | if (params->bridge[i]) { |
4716 | ifindex = if_nametoindex(params->bridge[i]); | |
4717 | if (ifindex) | |
4718 | add_ifidx(drv, ifindex); | |
4719 | if (ifindex == br_ifindex) | |
4720 | br_added = 1; | |
4721 | } | |
c5121837 | 4722 | } |
94627f6c JM |
4723 | if (!br_added && br_ifindex && |
4724 | (params->num_bridge == 0 || !params->bridge[0])) | |
4725 | add_ifidx(drv, br_ifindex); | |
c5121837 | 4726 | |
ad1e68e6 JM |
4727 | /* start listening for EAPOL on the default AP interface */ |
4728 | add_ifidx(drv, drv->ifindex); | |
4729 | ||
a2e40bb6 | 4730 | if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 0)) |
6980c191 | 4731 | goto failed; |
ad1e68e6 | 4732 | |
6980c191 | 4733 | if (params->bssid) { |
a2e40bb6 | 4734 | if (linux_set_ifhwaddr(drv->ioctl_sock, bss->ifname, |
2ac9688e | 4735 | params->bssid)) |
460456f8 JM |
4736 | goto failed; |
4737 | } | |
ad1e68e6 | 4738 | |
a2e40bb6 | 4739 | if (wpa_driver_nl80211_set_mode(bss, IEEE80211_MODE_AP)) { |
ad1e68e6 | 4740 | wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s " |
a2e40bb6 | 4741 | "into AP mode", bss->ifname); |
bbaf0837 | 4742 | goto failed; |
ad1e68e6 JM |
4743 | } |
4744 | ||
94627f6c JM |
4745 | if (params->num_bridge && params->bridge[0] && |
4746 | i802_check_bridge(drv, params->bridge[0], params->ifname) < 0) | |
4747 | goto failed; | |
4748 | ||
a2e40bb6 | 4749 | if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 1)) |
bbaf0837 | 4750 | goto failed; |
ad1e68e6 JM |
4751 | |
4752 | drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE)); | |
4753 | if (drv->eapol_sock < 0) { | |
4754 | perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)"); | |
bbaf0837 | 4755 | goto failed; |
ad1e68e6 JM |
4756 | } |
4757 | ||
4758 | if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL)) | |
4759 | { | |
4760 | printf("Could not register read socket for eapol\n"); | |
c5121837 | 4761 | goto failed; |
ad1e68e6 JM |
4762 | } |
4763 | ||
a2e40bb6 | 4764 | if (linux_get_ifhwaddr(drv->ioctl_sock, bss->ifname, params->own_addr)) |
bbaf0837 | 4765 | goto failed; |
c5121837 | 4766 | |
a2e40bb6 | 4767 | return bss; |
c5121837 JM |
4768 | |
4769 | failed: | |
460456f8 | 4770 | nl80211_remove_monitor_interface(drv); |
c5121837 JM |
4771 | if (drv->ioctl_sock >= 0) |
4772 | close(drv->ioctl_sock); | |
c5121837 JM |
4773 | |
4774 | genl_family_put(drv->nl80211); | |
4775 | nl_cache_free(drv->nl_cache); | |
4776 | nl_handle_destroy(drv->nl_handle); | |
4777 | nl_cb_put(drv->nl_cb); | |
4778 | ||
bbaf0837 JM |
4779 | os_free(drv); |
4780 | return NULL; | |
4781 | } | |
c5121837 | 4782 | |
c5121837 | 4783 | |
bbaf0837 JM |
4784 | static void i802_deinit(void *priv) |
4785 | { | |
4786 | wpa_driver_nl80211_deinit(priv); | |
c5121837 JM |
4787 | } |
4788 | ||
4789 | #endif /* HOSTAPD */ | |
4790 | ||
4791 | ||
22a7c9d7 JM |
4792 | static enum nl80211_iftype wpa_driver_nl80211_if_type( |
4793 | enum wpa_driver_if_type type) | |
4794 | { | |
4795 | switch (type) { | |
4796 | case WPA_IF_STATION: | |
4797 | return NL80211_IFTYPE_STATION; | |
4798 | case WPA_IF_AP_VLAN: | |
4799 | return NL80211_IFTYPE_AP_VLAN; | |
4800 | case WPA_IF_AP_BSS: | |
4801 | return NL80211_IFTYPE_AP; | |
4802 | } | |
4803 | return -1; | |
4804 | } | |
4805 | ||
4806 | ||
7ab68865 | 4807 | static int wpa_driver_nl80211_if_add(void *priv, enum wpa_driver_if_type type, |
8043e725 | 4808 | const char *ifname, const u8 *addr, |
f3585c8a JM |
4809 | void *bss_ctx, void **drv_priv, |
4810 | char *force_ifname, u8 *if_addr) | |
22a7c9d7 | 4811 | { |
a2e40bb6 FF |
4812 | struct i802_bss *bss = priv; |
4813 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
22a7c9d7 JM |
4814 | int ifidx; |
4815 | #ifdef HOSTAPD | |
a2e40bb6 | 4816 | struct i802_bss *new_bss = NULL; |
22a7c9d7 JM |
4817 | |
4818 | if (type == WPA_IF_AP_BSS) { | |
a2e40bb6 FF |
4819 | new_bss = os_zalloc(sizeof(*new_bss)); |
4820 | if (new_bss == NULL) | |
22a7c9d7 JM |
4821 | return -1; |
4822 | } | |
4823 | #endif /* HOSTAPD */ | |
4824 | ||
f3585c8a JM |
4825 | if (addr) |
4826 | os_memcpy(if_addr, addr, ETH_ALEN); | |
22a7c9d7 | 4827 | ifidx = nl80211_create_iface(drv, ifname, |
fbbfcbac FF |
4828 | wpa_driver_nl80211_if_type(type), addr, |
4829 | 0); | |
22a7c9d7 JM |
4830 | if (ifidx < 0) { |
4831 | #ifdef HOSTAPD | |
a2e40bb6 | 4832 | os_free(new_bss); |
22a7c9d7 JM |
4833 | #endif /* HOSTAPD */ |
4834 | return -1; | |
4835 | } | |
4836 | ||
f3585c8a JM |
4837 | if (!addr && |
4838 | linux_get_ifhwaddr(drv->ioctl_sock, bss->ifname, if_addr) < 0) | |
4839 | return -1; | |
4840 | ||
22a7c9d7 JM |
4841 | #ifdef HOSTAPD |
4842 | if (type == WPA_IF_AP_BSS) { | |
34f2f814 JM |
4843 | if (linux_set_iface_flags(drv->ioctl_sock, ifname, 1)) { |
4844 | nl80211_remove_iface(drv, ifidx); | |
07179987 | 4845 | os_free(new_bss); |
22a7c9d7 JM |
4846 | return -1; |
4847 | } | |
a2e40bb6 FF |
4848 | os_strlcpy(new_bss->ifname, ifname, IFNAMSIZ); |
4849 | new_bss->ifindex = ifidx; | |
4850 | new_bss->drv = drv; | |
4851 | new_bss->next = drv->first_bss.next; | |
4852 | drv->first_bss.next = new_bss; | |
4853 | if (drv_priv) | |
4854 | *drv_priv = new_bss; | |
22a7c9d7 JM |
4855 | } |
4856 | #endif /* HOSTAPD */ | |
4857 | ||
4858 | return 0; | |
4859 | } | |
4860 | ||
4861 | ||
4862 | static int wpa_driver_nl80211_if_remove(void *priv, | |
4863 | enum wpa_driver_if_type type, | |
4864 | const char *ifname) | |
4865 | { | |
a2e40bb6 FF |
4866 | struct i802_bss *bss = priv; |
4867 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
22a7c9d7 JM |
4868 | int ifindex = if_nametoindex(ifname); |
4869 | ||
e748062b JM |
4870 | wpa_printf(MSG_DEBUG, "nl80211: %s(type=%d ifname=%s) ifindex=%d", |
4871 | __func__, type, ifname, ifindex); | |
4872 | if (ifindex <= 0) | |
4873 | return -1; | |
22a7c9d7 JM |
4874 | nl80211_remove_iface(drv, ifindex); |
4875 | ||
4876 | #ifdef HOSTAPD | |
a2e40bb6 FF |
4877 | if (type != WPA_IF_AP_BSS) |
4878 | return 0; | |
4879 | ||
4880 | if (bss != &drv->first_bss) { | |
4881 | struct i802_bss *tbss = &drv->first_bss; | |
4882 | ||
4883 | while (tbss) { | |
4884 | if (tbss->next != bss) | |
4885 | continue; | |
4886 | ||
4887 | tbss->next = bss->next; | |
4888 | os_free(bss); | |
4889 | break; | |
22a7c9d7 JM |
4890 | } |
4891 | } | |
4892 | #endif /* HOSTAPD */ | |
4893 | ||
4894 | return 0; | |
4895 | } | |
4896 | ||
4897 | ||
55777702 JM |
4898 | static int cookie_handler(struct nl_msg *msg, void *arg) |
4899 | { | |
4900 | struct nlattr *tb[NL80211_ATTR_MAX + 1]; | |
4901 | struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg)); | |
4902 | u64 *cookie = arg; | |
4903 | nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), | |
4904 | genlmsg_attrlen(gnlh, 0), NULL); | |
4905 | if (tb[NL80211_ATTR_COOKIE]) | |
4906 | *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]); | |
4907 | return NL_SKIP; | |
4908 | } | |
4909 | ||
4910 | ||
58f6fbe0 JM |
4911 | static int wpa_driver_nl80211_send_action(void *priv, unsigned int freq, |
4912 | const u8 *dst, const u8 *src, | |
4913 | const u8 *bssid, | |
4914 | const u8 *data, size_t data_len) | |
4915 | { | |
a2e40bb6 FF |
4916 | struct i802_bss *bss = priv; |
4917 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
58f6fbe0 JM |
4918 | int ret = -1; |
4919 | struct nl_msg *msg; | |
4920 | u8 *buf; | |
4921 | struct ieee80211_hdr *hdr; | |
4922 | u64 cookie; | |
4923 | ||
4924 | wpa_printf(MSG_DEBUG, "nl80211: Send Action frame (ifindex=%d)", | |
4925 | drv->ifindex); | |
4926 | ||
4927 | buf = os_zalloc(24 + data_len); | |
4928 | if (buf == NULL) | |
4929 | return ret; | |
4930 | os_memcpy(buf + 24, data, data_len); | |
4931 | hdr = (struct ieee80211_hdr *) buf; | |
4932 | hdr->frame_control = | |
4933 | IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_ACTION); | |
4934 | os_memcpy(hdr->addr1, dst, ETH_ALEN); | |
4935 | os_memcpy(hdr->addr2, src, ETH_ALEN); | |
4936 | os_memcpy(hdr->addr3, bssid, ETH_ALEN); | |
4937 | ||
4938 | if (drv->nlmode == NL80211_IFTYPE_AP) { | |
4939 | ret = wpa_driver_nl80211_send_mlme(priv, buf, 24 + data_len); | |
4940 | os_free(buf); | |
4941 | return ret; | |
4942 | } | |
4943 | ||
4944 | msg = nlmsg_alloc(); | |
f8bf1421 JM |
4945 | if (!msg) { |
4946 | os_free(buf); | |
58f6fbe0 | 4947 | return -1; |
f8bf1421 | 4948 | } |
58f6fbe0 JM |
4949 | |
4950 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, | |
4951 | NL80211_CMD_ACTION, 0); | |
4952 | ||
4953 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); | |
4954 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq); | |
4955 | NLA_PUT(msg, NL80211_ATTR_FRAME, 24 + data_len, buf); | |
4956 | os_free(buf); | |
f8bf1421 | 4957 | buf = NULL; |
58f6fbe0 JM |
4958 | |
4959 | cookie = 0; | |
4960 | ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie); | |
4961 | msg = NULL; | |
4962 | if (ret) { | |
4963 | wpa_printf(MSG_DEBUG, "nl80211: Action command failed: ret=%d " | |
4964 | "(%s)", ret, strerror(-ret)); | |
4965 | goto nla_put_failure; | |
4966 | } | |
4967 | wpa_printf(MSG_DEBUG, "nl80211: Action TX command accepted; " | |
4968 | "cookie 0x%llx", (long long unsigned int) cookie); | |
4969 | drv->send_action_cookie = cookie; | |
4970 | drv->pending_send_action = 1; | |
4971 | ret = 0; | |
4972 | ||
4973 | nla_put_failure: | |
f8bf1421 | 4974 | os_free(buf); |
58f6fbe0 JM |
4975 | nlmsg_free(msg); |
4976 | return ret; | |
4977 | } | |
4978 | ||
4979 | ||
55777702 JM |
4980 | static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq, |
4981 | unsigned int duration) | |
4982 | { | |
a2e40bb6 FF |
4983 | struct i802_bss *bss = priv; |
4984 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
55777702 JM |
4985 | struct nl_msg *msg; |
4986 | int ret; | |
4987 | u64 cookie; | |
4988 | ||
4989 | msg = nlmsg_alloc(); | |
4990 | if (!msg) | |
4991 | return -1; | |
4992 | ||
4993 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, | |
4994 | NL80211_CMD_REMAIN_ON_CHANNEL, 0); | |
4995 | ||
4996 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); | |
4997 | NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq); | |
4998 | NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration); | |
4999 | ||
5000 | cookie = 0; | |
5001 | ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie); | |
5002 | if (ret == 0) { | |
5003 | wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie " | |
5004 | "0x%llx for freq=%u MHz duration=%u", | |
5005 | (long long unsigned int) cookie, freq, duration); | |
5006 | drv->remain_on_chan_cookie = cookie; | |
5007 | return 0; | |
5008 | } | |
5009 | wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel " | |
5010 | "(freq=%d): %d (%s)", freq, ret, strerror(-ret)); | |
5011 | nla_put_failure: | |
5012 | return -1; | |
5013 | } | |
5014 | ||
5015 | ||
5016 | static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv) | |
5017 | { | |
a2e40bb6 FF |
5018 | struct i802_bss *bss = priv; |
5019 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
55777702 JM |
5020 | struct nl_msg *msg; |
5021 | int ret; | |
5022 | ||
5023 | if (!drv->pending_remain_on_chan) { | |
5024 | wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel " | |
5025 | "to cancel"); | |
5026 | return -1; | |
5027 | } | |
5028 | ||
5029 | wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie " | |
5030 | "0x%llx", | |
5031 | (long long unsigned int) drv->remain_on_chan_cookie); | |
5032 | ||
5033 | msg = nlmsg_alloc(); | |
5034 | if (!msg) | |
5035 | return -1; | |
5036 | ||
5037 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, | |
5038 | NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL, 0); | |
5039 | ||
5040 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex); | |
5041 | NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie); | |
5042 | ||
5043 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
5044 | if (ret == 0) | |
5045 | return 0; | |
5046 | wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: " | |
5047 | "%d (%s)", ret, strerror(-ret)); | |
5048 | nla_put_failure: | |
5049 | return -1; | |
5050 | } | |
5051 | ||
5052 | ||
504e905c JM |
5053 | static void wpa_driver_nl80211_probe_req_report_timeout(void *eloop_ctx, |
5054 | void *timeout_ctx) | |
5055 | { | |
5056 | struct wpa_driver_nl80211_data *drv = eloop_ctx; | |
5057 | if (drv->monitor_ifidx < 0) | |
5058 | return; /* monitor interface already removed */ | |
5059 | ||
5060 | if (drv->nlmode != NL80211_IFTYPE_STATION) | |
5061 | return; /* not in station mode anymore */ | |
5062 | ||
5063 | if (drv->probe_req_report) | |
5064 | return; /* reporting enabled */ | |
5065 | ||
5066 | wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface due to no " | |
5067 | "Probe Request reporting needed anymore"); | |
5068 | nl80211_remove_monitor_interface(drv); | |
5069 | } | |
5070 | ||
5071 | ||
5072 | static int wpa_driver_nl80211_probe_req_report(void *priv, int report) | |
5073 | { | |
a2e40bb6 FF |
5074 | struct i802_bss *bss = priv; |
5075 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
504e905c JM |
5076 | |
5077 | if (drv->nlmode != NL80211_IFTYPE_STATION) { | |
5078 | wpa_printf(MSG_DEBUG, "nl80211: probe_req_report control only " | |
5079 | "allowed in station mode (iftype=%d)", | |
5080 | drv->nlmode); | |
5081 | return -1; | |
5082 | } | |
5083 | drv->probe_req_report = report; | |
5084 | ||
5085 | if (report) { | |
5086 | eloop_cancel_timeout( | |
5087 | wpa_driver_nl80211_probe_req_report_timeout, | |
5088 | drv, NULL); | |
5089 | if (drv->monitor_ifidx < 0 && | |
5090 | nl80211_create_monitor_interface(drv)) | |
5091 | return -1; | |
5092 | } else { | |
5093 | /* | |
5094 | * It takes a while to remove the monitor interface, so try to | |
5095 | * avoid doing this if it is needed again shortly. Instead, | |
5096 | * schedule the interface to be removed later if no need for it | |
5097 | * is seen. | |
5098 | */ | |
5099 | wpa_printf(MSG_DEBUG, "nl80211: Scheduling monitor interface " | |
5100 | "to be removed after 10 seconds of no use"); | |
5101 | eloop_register_timeout( | |
5102 | 10, 0, wpa_driver_nl80211_probe_req_report_timeout, | |
5103 | drv, NULL); | |
5104 | } | |
5105 | ||
5106 | return 0; | |
5107 | } | |
5108 | ||
5109 | ||
4e5cb1a3 JM |
5110 | static int nl80211_disable_11b_rates(struct wpa_driver_nl80211_data *drv, |
5111 | int ifindex, int disabled) | |
5112 | { | |
5113 | struct nl_msg *msg; | |
5114 | struct nlattr *bands, *band; | |
5115 | int ret; | |
5116 | ||
5117 | msg = nlmsg_alloc(); | |
5118 | if (!msg) | |
5119 | return -1; | |
5120 | ||
5121 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, | |
5122 | NL80211_CMD_SET_TX_BITRATE_MASK, 0); | |
5123 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex); | |
5124 | ||
5125 | bands = nla_nest_start(msg, NL80211_ATTR_TX_RATES); | |
5126 | if (!bands) | |
5127 | goto nla_put_failure; | |
5128 | ||
5129 | /* | |
5130 | * Disable 2 GHz rates 1, 2, 5.5, 11 Mbps by masking out everything | |
5131 | * else apart from 6, 9, 12, 18, 24, 36, 48, 54 Mbps from non-MCS | |
5132 | * rates. All 5 GHz rates are left enabled. | |
5133 | */ | |
5134 | band = nla_nest_start(msg, NL80211_BAND_2GHZ); | |
5135 | if (!band) | |
5136 | goto nla_put_failure; | |
5137 | NLA_PUT(msg, NL80211_TXRATE_LEGACY, 8, | |
5138 | "\x0c\x12\x18\x24\x30\x48\x60\x6c"); | |
5139 | nla_nest_end(msg, band); | |
5140 | ||
5141 | nla_nest_end(msg, bands); | |
5142 | ||
5143 | ret = send_and_recv_msgs(drv, msg, NULL, NULL); | |
5144 | msg = NULL; | |
5145 | if (ret) { | |
5146 | wpa_printf(MSG_DEBUG, "nl80211: Set TX rates failed: ret=%d " | |
5147 | "(%s)", ret, strerror(-ret)); | |
5148 | } | |
5149 | ||
5150 | return ret; | |
5151 | ||
5152 | nla_put_failure: | |
5153 | nlmsg_free(msg); | |
5154 | return -1; | |
5155 | } | |
5156 | ||
5157 | ||
5158 | static int wpa_driver_nl80211_disable_11b_rates(void *priv, int disabled) | |
5159 | { | |
a2e40bb6 FF |
5160 | struct i802_bss *bss = priv; |
5161 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
4e5cb1a3 JM |
5162 | drv->disable_11b_rates = disabled; |
5163 | return nl80211_disable_11b_rates(drv, drv->ifindex, disabled); | |
5164 | } | |
5165 | ||
5166 | ||
af473088 JM |
5167 | static int wpa_driver_nl80211_deinit_ap(void *priv) |
5168 | { | |
a2e40bb6 FF |
5169 | struct i802_bss *bss = priv; |
5170 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
af473088 JM |
5171 | if (drv->nlmode != NL80211_IFTYPE_AP) |
5172 | return -1; | |
5173 | wpa_driver_nl80211_del_beacon(drv); | |
a2e40bb6 | 5174 | return wpa_driver_nl80211_set_mode(priv, IEEE80211_MODE_INFRA); |
af473088 JM |
5175 | } |
5176 | ||
5177 | ||
207ef3fb JM |
5178 | static void wpa_driver_nl80211_resume(void *priv) |
5179 | { | |
a2e40bb6 FF |
5180 | struct i802_bss *bss = priv; |
5181 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
5182 | if (linux_set_iface_flags(drv->ioctl_sock, bss->ifname, 1)) { | |
207ef3fb JM |
5183 | wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface up on " |
5184 | "resume event"); | |
5185 | } | |
5186 | } | |
5187 | ||
5188 | ||
7b90c16a JM |
5189 | static int nl80211_send_ft_action(void *priv, u8 action, const u8 *target_ap, |
5190 | const u8 *ies, size_t ies_len) | |
5191 | { | |
5192 | struct i802_bss *bss = priv; | |
5193 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
5194 | int ret; | |
5195 | u8 *data, *pos; | |
5196 | size_t data_len; | |
5197 | u8 own_addr[ETH_ALEN]; | |
5198 | ||
5199 | if (linux_get_ifhwaddr(drv->ioctl_sock, bss->ifname, own_addr) < 0) | |
5200 | return -1; | |
5201 | ||
5202 | if (action != 1) { | |
5203 | wpa_printf(MSG_ERROR, "nl80211: Unsupported send_ft_action " | |
5204 | "action %d", action); | |
5205 | return -1; | |
5206 | } | |
5207 | ||
5208 | /* | |
5209 | * Action frame payload: | |
5210 | * Category[1] = 6 (Fast BSS Transition) | |
5211 | * Action[1] = 1 (Fast BSS Transition Request) | |
5212 | * STA Address | |
5213 | * Target AP Address | |
5214 | * FT IEs | |
5215 | */ | |
5216 | ||
73fc617d JM |
5217 | data_len = 2 + 2 * ETH_ALEN + ies_len; |
5218 | data = os_malloc(data_len); | |
7b90c16a JM |
5219 | if (data == NULL) |
5220 | return -1; | |
5221 | pos = data; | |
5222 | *pos++ = 0x06; /* FT Action category */ | |
5223 | *pos++ = action; | |
5224 | os_memcpy(pos, own_addr, ETH_ALEN); | |
5225 | pos += ETH_ALEN; | |
5226 | os_memcpy(pos, target_ap, ETH_ALEN); | |
5227 | pos += ETH_ALEN; | |
5228 | os_memcpy(pos, ies, ies_len); | |
5229 | ||
5230 | ret = wpa_driver_nl80211_send_action(bss, drv->assoc_freq, drv->bssid, | |
5231 | own_addr, drv->bssid, | |
5232 | data, data_len); | |
5233 | os_free(data); | |
5234 | ||
5235 | return ret; | |
5236 | } | |
5237 | ||
5238 | ||
b625473c JM |
5239 | static int nl80211_signal_monitor(void *priv, int threshold, int hysteresis) |
5240 | { | |
5241 | struct i802_bss *bss = priv; | |
5242 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
5243 | struct nl_msg *msg, *cqm = NULL; | |
5244 | ||
5245 | wpa_printf(MSG_DEBUG, "nl80211: Signal monitor threshold=%d " | |
5246 | "hysteresis=%d", threshold, hysteresis); | |
5247 | ||
5248 | msg = nlmsg_alloc(); | |
5249 | if (!msg) | |
5250 | return -1; | |
5251 | ||
5252 | genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, | |
5253 | 0, NL80211_CMD_SET_CQM, 0); | |
5254 | ||
5255 | NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, bss->ifindex); | |
5256 | ||
5257 | cqm = nlmsg_alloc(); | |
5258 | if (cqm == NULL) | |
5259 | return -1; | |
5260 | ||
5261 | NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_THOLD, threshold); | |
5262 | NLA_PUT_U32(cqm, NL80211_ATTR_CQM_RSSI_HYST, hysteresis); | |
5263 | nla_put_nested(msg, NL80211_ATTR_CQM, cqm); | |
5264 | ||
5265 | if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0) | |
5266 | return 0; | |
5267 | msg = NULL; | |
5268 | ||
5269 | nla_put_failure: | |
5270 | if (cqm) | |
5271 | nlmsg_free(cqm); | |
5272 | nlmsg_free(msg); | |
5273 | return -1; | |
5274 | } | |
5275 | ||
5276 | ||
b91ab76e JM |
5277 | static int nl80211_send_frame(void *priv, const u8 *data, size_t data_len, |
5278 | int encrypt) | |
5279 | { | |
5280 | struct i802_bss *bss = priv; | |
5281 | struct wpa_driver_nl80211_data *drv = bss->drv; | |
5282 | return wpa_driver_nl80211_send_frame(drv, data, data_len, encrypt); | |
5283 | } | |
5284 | ||
5285 | ||
3f5285e8 JM |
5286 | const struct wpa_driver_ops wpa_driver_nl80211_ops = { |
5287 | .name = "nl80211", | |
5288 | .desc = "Linux nl80211/cfg80211", | |
5289 | .get_bssid = wpa_driver_nl80211_get_bssid, | |
5290 | .get_ssid = wpa_driver_nl80211_get_ssid, | |
3f5285e8 | 5291 | .set_key = wpa_driver_nl80211_set_key, |
6a1063e0 | 5292 | .scan2 = wpa_driver_nl80211_scan, |
3f5285e8 JM |
5293 | .get_scan_results2 = wpa_driver_nl80211_get_scan_results, |
5294 | .deauthenticate = wpa_driver_nl80211_deauthenticate, | |
5295 | .disassociate = wpa_driver_nl80211_disassociate, | |
c2a04078 | 5296 | .authenticate = wpa_driver_nl80211_authenticate, |
3f5285e8 | 5297 | .associate = wpa_driver_nl80211_associate, |
3f5285e8 JM |
5298 | .init = wpa_driver_nl80211_init, |
5299 | .deinit = wpa_driver_nl80211_deinit, | |
3f5285e8 JM |
5300 | .get_capa = wpa_driver_nl80211_get_capa, |
5301 | .set_operstate = wpa_driver_nl80211_set_operstate, | |
01652550 | 5302 | .set_supp_port = wpa_driver_nl80211_set_supp_port, |
6d158490 | 5303 | .set_country = wpa_driver_nl80211_set_country, |
d2440ba0 | 5304 | .set_beacon = wpa_driver_nl80211_set_beacon, |
22a7c9d7 JM |
5305 | .if_add = wpa_driver_nl80211_if_add, |
5306 | .if_remove = wpa_driver_nl80211_if_remove, | |
071f8ac4 | 5307 | .send_mlme = wpa_driver_nl80211_send_mlme, |
c3965310 | 5308 | .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data, |
0f4e8b4f JM |
5309 | .sta_add = wpa_driver_nl80211_sta_add, |
5310 | .sta_remove = wpa_driver_nl80211_sta_remove, | |
db149ac9 | 5311 | .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol, |
a8d6ffa4 | 5312 | .sta_set_flags = wpa_driver_nl80211_sta_set_flags, |
c5121837 JM |
5313 | #ifdef HOSTAPD |
5314 | .hapd_init = i802_init, | |
c5121837 | 5315 | .hapd_deinit = i802_deinit, |
c5121837 JM |
5316 | .get_seqnum = i802_get_seqnum, |
5317 | .flush = i802_flush, | |
5318 | .read_sta_data = i802_read_sta_data, | |
c5121837 JM |
5319 | .sta_deauth = i802_sta_deauth, |
5320 | .sta_disassoc = i802_sta_disassoc, | |
c5121837 JM |
5321 | .get_inact_sec = i802_get_inact_sec, |
5322 | .sta_clear_stats = i802_sta_clear_stats, | |
c5121837 JM |
5323 | .set_rts = i802_set_rts, |
5324 | .set_frag = i802_set_frag, | |
c5121837 | 5325 | .set_rate_sets = i802_set_rate_sets, |
c5121837 JM |
5326 | .set_cts_protect = i802_set_cts_protect, |
5327 | .set_preamble = i802_set_preamble, | |
5328 | .set_short_slot_time = i802_set_short_slot_time, | |
5329 | .set_tx_queue_params = i802_set_tx_queue_params, | |
c5121837 | 5330 | .set_sta_vlan = i802_set_sta_vlan, |
fbbfcbac | 5331 | .set_wds_sta = i802_set_wds_sta, |
c5121837 | 5332 | #endif /* HOSTAPD */ |
e3802622 | 5333 | .set_freq = i802_set_freq, |
58f6fbe0 | 5334 | .send_action = wpa_driver_nl80211_send_action, |
55777702 JM |
5335 | .remain_on_channel = wpa_driver_nl80211_remain_on_channel, |
5336 | .cancel_remain_on_channel = | |
5337 | wpa_driver_nl80211_cancel_remain_on_channel, | |
504e905c | 5338 | .probe_req_report = wpa_driver_nl80211_probe_req_report, |
4e5cb1a3 | 5339 | .disable_11b_rates = wpa_driver_nl80211_disable_11b_rates, |
af473088 | 5340 | .deinit_ap = wpa_driver_nl80211_deinit_ap, |
207ef3fb | 5341 | .resume = wpa_driver_nl80211_resume, |
7b90c16a | 5342 | .send_ft_action = nl80211_send_ft_action, |
b625473c | 5343 | .signal_monitor = nl80211_signal_monitor, |
b91ab76e | 5344 | .send_frame = nl80211_send_frame, |
3f5285e8 | 5345 | }; |