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