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