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