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