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