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