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