Share a single Linux ioctl helper fo setting interface up/down
[hostap-gosc2009.git] / src / drivers / driver_nl80211.c
bloba6e6188224cac9861a39f07fd28195ce931d5108
1 /*
2 * Driver interaction with Linux nl80211/cfg80211
3 * Copyright (c) 2002-2010, 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
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.
13 * Alternatively, this software may be distributed under the terms of BSD
14 * license.
16 * See README and COPYING for more details.
19 #include "includes.h"
20 #include <sys/ioctl.h>
21 #include <net/if_arp.h>
22 #include <net/if.h>
23 #include <netlink/genl/genl.h>
24 #include <netlink/genl/family.h>
25 #include <netlink/genl/ctrl.h>
26 #include <netpacket/packet.h>
27 #include <linux/filter.h>
28 #include "nl80211_copy.h"
30 #include "common.h"
31 #include "eloop.h"
32 #include "common/ieee802_11_defs.h"
33 #include "netlink.h"
34 #include "linux_ioctl.h"
35 #include "radiotap.h"
36 #include "radiotap_iter.h"
37 #include "driver.h"
39 #ifdef CONFIG_LIBNL20
40 /* libnl 2.0 compatibility code */
41 #define nl_handle nl_sock
42 #define nl_handle_alloc_cb nl_socket_alloc_cb
43 #define nl_handle_destroy nl_socket_free
44 #endif /* CONFIG_LIBNL20 */
47 #ifndef IFF_LOWER_UP
48 #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
49 #endif
50 #ifndef IFF_DORMANT
51 #define IFF_DORMANT 0x20000 /* driver signals dormant */
52 #endif
54 #ifndef IF_OPER_DORMANT
55 #define IF_OPER_DORMANT 5
56 #endif
57 #ifndef IF_OPER_UP
58 #define IF_OPER_UP 6
59 #endif
61 struct i802_bss {
62 struct i802_bss *next;
63 int ifindex;
64 unsigned int beacon_set:1;
67 struct wpa_driver_nl80211_data {
68 void *ctx;
69 struct netlink_data *netlink;
70 int ioctl_sock; /* socket for ioctl() use */
71 char ifname[IFNAMSIZ + 1];
72 int ifindex;
73 int if_removed;
74 struct wpa_driver_capa capa;
75 int has_capability;
77 int operstate;
79 int scan_complete_events;
81 struct nl_handle *nl_handle;
82 struct nl_handle *nl_handle_event;
83 struct nl_cache *nl_cache;
84 struct nl_cache *nl_cache_event;
85 struct nl_cb *nl_cb;
86 struct genl_family *nl80211;
88 u8 auth_bssid[ETH_ALEN];
89 u8 bssid[ETH_ALEN];
90 int associated;
91 u8 ssid[32];
92 size_t ssid_len;
93 int nlmode;
94 int ap_scan_as_station;
95 unsigned int assoc_freq;
97 int monitor_sock;
98 int monitor_ifidx;
99 int probe_req_report;
101 unsigned int beacon_set:1;
102 unsigned int pending_remain_on_chan:1;
104 u64 remain_on_chan_cookie;
106 #ifdef HOSTAPD
107 int eapol_sock; /* socket for EAPOL frames */
109 int default_if_indices[16];
110 int *if_indices;
111 int num_if_indices;
113 struct i802_bss bss;
115 int last_freq;
116 int last_freq_ht;
117 #endif /* HOSTAPD */
121 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
122 void *timeout_ctx);
123 static int wpa_driver_nl80211_set_mode(void *priv, int mode);
124 static int
125 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
126 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
127 const u8 *addr, int cmd, u16 reason_code);
128 static void nl80211_remove_monitor_interface(
129 struct wpa_driver_nl80211_data *drv);
131 #ifdef HOSTAPD
132 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
133 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
134 static struct i802_bss * get_bss(struct wpa_driver_nl80211_data *drv,
135 int ifindex);
136 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq);
137 static int wpa_driver_nl80211_if_remove(void *priv,
138 enum wpa_driver_if_type type,
139 const char *ifname);
140 #endif /* HOSTAPD */
142 static void wpa_driver_nl80211_probe_req_report_timeout(void *eloop_ctx,
143 void *timeout_ctx);
146 /* nl80211 code */
147 static int ack_handler(struct nl_msg *msg, void *arg)
149 int *err = arg;
150 *err = 0;
151 return NL_STOP;
154 static int finish_handler(struct nl_msg *msg, void *arg)
156 int *ret = arg;
157 *ret = 0;
158 return NL_SKIP;
161 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
162 void *arg)
164 int *ret = arg;
165 *ret = err->error;
166 return NL_SKIP;
170 static int no_seq_check(struct nl_msg *msg, void *arg)
172 return NL_OK;
176 static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
177 struct nl_msg *msg,
178 int (*valid_handler)(struct nl_msg *, void *),
179 void *valid_data)
181 struct nl_cb *cb;
182 int err = -ENOMEM;
184 cb = nl_cb_clone(drv->nl_cb);
185 if (!cb)
186 goto out;
188 err = nl_send_auto_complete(drv->nl_handle, msg);
189 if (err < 0)
190 goto out;
192 err = 1;
194 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
195 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
196 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
198 if (valid_handler)
199 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
200 valid_handler, valid_data);
202 while (err > 0)
203 nl_recvmsgs(drv->nl_handle, cb);
204 out:
205 nl_cb_put(cb);
206 nlmsg_free(msg);
207 return err;
211 struct family_data {
212 const char *group;
213 int id;
217 static int family_handler(struct nl_msg *msg, void *arg)
219 struct family_data *res = arg;
220 struct nlattr *tb[CTRL_ATTR_MAX + 1];
221 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
222 struct nlattr *mcgrp;
223 int i;
225 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
226 genlmsg_attrlen(gnlh, 0), NULL);
227 if (!tb[CTRL_ATTR_MCAST_GROUPS])
228 return NL_SKIP;
230 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
231 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
232 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
233 nla_len(mcgrp), NULL);
234 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
235 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
236 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
237 res->group,
238 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
239 continue;
240 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
241 break;
244 return NL_SKIP;
248 static int nl_get_multicast_id(struct wpa_driver_nl80211_data *drv,
249 const char *family, const char *group)
251 struct nl_msg *msg;
252 int ret = -1;
253 struct family_data res = { group, -ENOENT };
255 msg = nlmsg_alloc();
256 if (!msg)
257 return -ENOMEM;
258 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(drv->nl_handle, "nlctrl"),
259 0, 0, CTRL_CMD_GETFAMILY, 0);
260 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
262 ret = send_and_recv_msgs(drv, msg, family_handler, &res);
263 msg = NULL;
264 if (ret == 0)
265 ret = res.id;
267 nla_put_failure:
268 nlmsg_free(msg);
269 return ret;
273 static int get_ifhwaddr(struct wpa_driver_nl80211_data *drv,
274 const char *ifname, u8 *addr)
276 struct ifreq ifr;
278 os_memset(&ifr, 0, sizeof(ifr));
279 os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
280 if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr)) {
281 wpa_printf(MSG_ERROR, "%s: ioctl(SIOCGIFHWADDR): %d (%s)",
282 ifname, errno, strerror(errno));
283 return -1;
286 if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
287 wpa_printf(MSG_ERROR, "%s: Invalid HW-addr family 0x%04x",
288 ifname, ifr.ifr_hwaddr.sa_family);
289 return -1;
291 os_memcpy(addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
293 return 0;
297 static int set_ifhwaddr(struct wpa_driver_nl80211_data *drv,
298 const char *ifname, const u8 *addr)
300 struct ifreq ifr;
302 os_memset(&ifr, 0, sizeof(ifr));
303 os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
304 os_memcpy(ifr.ifr_hwaddr.sa_data, addr, ETH_ALEN);
305 ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
307 if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
308 wpa_printf(MSG_DEBUG, "%s: ioctl(SIOCSIFHWADDR): %d (%s)",
309 ifname, errno, strerror(errno));
310 return -1;
313 return 0;
317 static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
319 struct wpa_driver_nl80211_data *drv = priv;
320 if (!drv->associated)
321 return -1;
322 os_memcpy(bssid, drv->bssid, ETH_ALEN);
323 return 0;
327 static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
329 struct wpa_driver_nl80211_data *drv = priv;
330 if (!drv->associated)
331 return -1;
332 os_memcpy(ssid, drv->ssid, drv->ssid_len);
333 return drv->ssid_len;
337 static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
338 char *buf, size_t len, int del)
340 union wpa_event_data event;
342 os_memset(&event, 0, sizeof(event));
343 if (len > sizeof(event.interface_status.ifname))
344 len = sizeof(event.interface_status.ifname) - 1;
345 os_memcpy(event.interface_status.ifname, buf, len);
346 event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
347 EVENT_INTERFACE_ADDED;
349 wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
350 del ? "DEL" : "NEW",
351 event.interface_status.ifname,
352 del ? "removed" : "added");
354 if (os_strcmp(drv->ifname, event.interface_status.ifname) == 0) {
355 if (del)
356 drv->if_removed = 1;
357 else
358 drv->if_removed = 0;
361 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
365 static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
366 u8 *buf, size_t len)
368 int attrlen, rta_len;
369 struct rtattr *attr;
371 attrlen = len;
372 attr = (struct rtattr *) buf;
374 rta_len = RTA_ALIGN(sizeof(struct rtattr));
375 while (RTA_OK(attr, attrlen)) {
376 if (attr->rta_type == IFLA_IFNAME) {
377 if (os_strcmp(((char *) attr) + rta_len, drv->ifname)
378 == 0)
379 return 1;
380 else
381 break;
383 attr = RTA_NEXT(attr, attrlen);
386 return 0;
390 static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
391 int ifindex, u8 *buf, size_t len)
393 if (drv->ifindex == ifindex)
394 return 1;
396 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
397 drv->ifindex = if_nametoindex(drv->ifname);
398 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
399 "interface");
400 wpa_driver_nl80211_finish_drv_init(drv);
401 return 1;
404 return 0;
408 static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
409 struct ifinfomsg *ifi,
410 u8 *buf, size_t len)
412 struct wpa_driver_nl80211_data *drv = ctx;
413 int attrlen, rta_len;
414 struct rtattr *attr;
416 if (!wpa_driver_nl80211_own_ifindex(drv, ifi->ifi_index, buf, len)) {
417 wpa_printf(MSG_DEBUG, "Ignore event for foreign ifindex %d",
418 ifi->ifi_index);
419 return;
422 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
423 "(%s%s%s%s)",
424 drv->operstate, ifi->ifi_flags,
425 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
426 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
427 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
428 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
430 * Some drivers send the association event before the operup event--in
431 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
432 * fails. This will hit us when wpa_supplicant does not need to do
433 * IEEE 802.1X authentication
435 if (drv->operstate == 1 &&
436 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
437 !(ifi->ifi_flags & IFF_RUNNING))
438 netlink_send_oper_ifla(drv->netlink, drv->ifindex,
439 -1, IF_OPER_UP);
441 attrlen = len;
442 attr = (struct rtattr *) buf;
443 rta_len = RTA_ALIGN(sizeof(struct rtattr));
444 while (RTA_OK(attr, attrlen)) {
445 if (attr->rta_type == IFLA_IFNAME) {
446 wpa_driver_nl80211_event_link(
447 drv,
448 ((char *) attr) + rta_len,
449 attr->rta_len - rta_len, 0);
451 attr = RTA_NEXT(attr, attrlen);
456 static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
457 struct ifinfomsg *ifi,
458 u8 *buf, size_t len)
460 struct wpa_driver_nl80211_data *drv = ctx;
461 int attrlen, rta_len;
462 struct rtattr *attr;
464 attrlen = len;
465 attr = (struct rtattr *) buf;
467 rta_len = RTA_ALIGN(sizeof(struct rtattr));
468 while (RTA_OK(attr, attrlen)) {
469 if (attr->rta_type == IFLA_IFNAME) {
470 wpa_driver_nl80211_event_link(
471 drv,
472 ((char *) attr) + rta_len,
473 attr->rta_len - rta_len, 1);
475 attr = RTA_NEXT(attr, attrlen);
480 static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
481 const u8 *frame, size_t len)
483 const struct ieee80211_mgmt *mgmt;
484 union wpa_event_data event;
486 mgmt = (const struct ieee80211_mgmt *) frame;
487 if (len < 24 + sizeof(mgmt->u.auth)) {
488 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
489 "frame");
490 return;
493 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
494 os_memset(&event, 0, sizeof(event));
495 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
496 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
497 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
498 if (len > 24 + sizeof(mgmt->u.auth)) {
499 event.auth.ies = mgmt->u.auth.variable;
500 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
503 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
507 static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
508 const u8 *frame, size_t len)
510 const struct ieee80211_mgmt *mgmt;
511 union wpa_event_data event;
512 u16 status;
514 mgmt = (const struct ieee80211_mgmt *) frame;
515 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
516 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
517 "frame");
518 return;
521 status = le_to_host16(mgmt->u.assoc_resp.status_code);
522 if (status != WLAN_STATUS_SUCCESS) {
523 os_memset(&event, 0, sizeof(event));
524 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
525 event.assoc_reject.resp_ies =
526 (u8 *) mgmt->u.assoc_resp.variable;
527 event.assoc_reject.resp_ies_len =
528 len - 24 - sizeof(mgmt->u.assoc_resp);
530 event.assoc_reject.status_code = status;
532 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
533 return;
536 drv->associated = 1;
537 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
539 os_memset(&event, 0, sizeof(event));
540 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
541 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
542 event.assoc_info.resp_ies_len =
543 len - 24 - sizeof(mgmt->u.assoc_resp);
546 event.assoc_info.freq = drv->assoc_freq;
548 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
552 static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
553 enum nl80211_commands cmd, struct nlattr *status,
554 struct nlattr *addr, struct nlattr *req_ie,
555 struct nlattr *resp_ie)
557 union wpa_event_data event;
559 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
561 * Avoid reporting two association events that would confuse
562 * the core code.
564 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
565 "when using userspace SME", cmd);
566 return;
569 os_memset(&event, 0, sizeof(event));
570 if (cmd == NL80211_CMD_CONNECT &&
571 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
572 if (resp_ie) {
573 event.assoc_reject.resp_ies = nla_data(resp_ie);
574 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
576 event.assoc_reject.status_code = nla_get_u16(status);
577 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
578 return;
581 drv->associated = 1;
582 if (addr)
583 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
585 if (req_ie) {
586 event.assoc_info.req_ies = nla_data(req_ie);
587 event.assoc_info.req_ies_len = nla_len(req_ie);
589 if (resp_ie) {
590 event.assoc_info.resp_ies = nla_data(resp_ie);
591 event.assoc_info.resp_ies_len = nla_len(resp_ie);
594 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
598 static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
599 enum nl80211_commands cmd, struct nlattr *addr)
601 union wpa_event_data event;
602 enum wpa_event_type ev;
604 if (nla_len(addr) != ETH_ALEN)
605 return;
607 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
608 cmd, MAC2STR((u8 *) nla_data(addr)));
610 if (cmd == NL80211_CMD_AUTHENTICATE)
611 ev = EVENT_AUTH_TIMED_OUT;
612 else if (cmd == NL80211_CMD_ASSOCIATE)
613 ev = EVENT_ASSOC_TIMED_OUT;
614 else
615 return;
617 os_memset(&event, 0, sizeof(event));
618 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
619 wpa_supplicant_event(drv->ctx, ev, &event);
623 static void mlme_event(struct wpa_driver_nl80211_data *drv,
624 enum nl80211_commands cmd, struct nlattr *frame,
625 struct nlattr *addr, struct nlattr *timed_out)
627 if (timed_out && addr) {
628 mlme_timeout_event(drv, cmd, addr);
629 return;
632 if (frame == NULL) {
633 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame "
634 "data", cmd);
635 return;
638 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d", cmd);
639 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
640 nla_data(frame), nla_len(frame));
642 switch (cmd) {
643 case NL80211_CMD_AUTHENTICATE:
644 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
645 break;
646 case NL80211_CMD_ASSOCIATE:
647 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
648 break;
649 case NL80211_CMD_DEAUTHENTICATE:
650 drv->associated = 0;
651 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, NULL);
652 break;
653 case NL80211_CMD_DISASSOCIATE:
654 drv->associated = 0;
655 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
656 break;
657 default:
658 break;
663 static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv,
664 struct nlattr *tb[])
666 union wpa_event_data data;
668 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
669 os_memset(&data, 0, sizeof(data));
670 if (tb[NL80211_ATTR_MAC]) {
671 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
672 nla_data(tb[NL80211_ATTR_MAC]),
673 nla_len(tb[NL80211_ATTR_MAC]));
674 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
676 if (tb[NL80211_ATTR_KEY_SEQ]) {
677 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
678 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
679 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
681 if (tb[NL80211_ATTR_KEY_TYPE]) {
682 enum nl80211_key_type key_type =
683 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
684 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
685 if (key_type == NL80211_KEYTYPE_PAIRWISE)
686 data.michael_mic_failure.unicast = 1;
687 } else
688 data.michael_mic_failure.unicast = 1;
690 if (tb[NL80211_ATTR_KEY_IDX]) {
691 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
692 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
695 wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
699 static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
700 struct nlattr *tb[])
702 if (tb[NL80211_ATTR_MAC] == NULL) {
703 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
704 "event");
705 return;
707 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
708 drv->associated = 1;
709 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
710 MAC2STR(drv->bssid));
712 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
716 static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
717 int cancel_event, struct nlattr *tb[])
719 unsigned int freq, chan_type, duration;
720 union wpa_event_data data;
721 u64 cookie;
723 if (tb[NL80211_ATTR_WIPHY_FREQ])
724 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
725 else
726 freq = 0;
728 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
729 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
730 else
731 chan_type = 0;
733 if (tb[NL80211_ATTR_DURATION])
734 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
735 else
736 duration = 0;
738 if (tb[NL80211_ATTR_COOKIE])
739 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
740 else
741 cookie = 0;
743 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
744 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
745 cancel_event, freq, chan_type, duration,
746 (long long unsigned int) cookie,
747 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
749 if (cookie != drv->remain_on_chan_cookie)
750 return; /* not for us */
752 drv->pending_remain_on_chan = !cancel_event;
754 os_memset(&data, 0, sizeof(data));
755 data.remain_on_channel.freq = freq;
756 data.remain_on_channel.duration = duration;
757 wpa_supplicant_event(drv->ctx, cancel_event ?
758 EVENT_CANCEL_REMAIN_ON_CHANNEL :
759 EVENT_REMAIN_ON_CHANNEL, &data);
763 static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
764 struct nlattr *tb[])
766 union wpa_event_data event;
767 struct nlattr *nl;
768 int rem;
769 struct scan_info *info;
770 #define MAX_REPORT_FREQS 50
771 int freqs[MAX_REPORT_FREQS];
772 int num_freqs = 0;
774 os_memset(&event, 0, sizeof(event));
775 info = &event.scan_info;
776 info->aborted = aborted;
778 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
779 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
780 struct wpa_driver_scan_ssid *s =
781 &info->ssids[info->num_ssids];
782 s->ssid = nla_data(nl);
783 s->ssid_len = nla_len(nl);
784 info->num_ssids++;
785 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
786 break;
789 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
790 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
792 freqs[num_freqs] = nla_get_u32(nl);
793 num_freqs++;
794 if (num_freqs == MAX_REPORT_FREQS - 1)
795 break;
797 info->freqs = freqs;
798 info->num_freqs = num_freqs;
800 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
804 static int process_event(struct nl_msg *msg, void *arg)
806 struct wpa_driver_nl80211_data *drv = arg;
807 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
808 struct nlattr *tb[NL80211_ATTR_MAX + 1];
810 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
811 genlmsg_attrlen(gnlh, 0), NULL);
813 if (tb[NL80211_ATTR_IFINDEX]) {
814 int ifindex = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
815 if (ifindex != drv->ifindex) {
816 wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d)"
817 " for foreign interface (ifindex %d)",
818 gnlh->cmd, ifindex);
819 return NL_SKIP;
823 if (drv->ap_scan_as_station &&
824 (gnlh->cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
825 gnlh->cmd == NL80211_CMD_SCAN_ABORTED)) {
826 wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_AP);
827 drv->ap_scan_as_station = 0;
830 switch (gnlh->cmd) {
831 case NL80211_CMD_TRIGGER_SCAN:
832 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger");
833 break;
834 case NL80211_CMD_NEW_SCAN_RESULTS:
835 wpa_printf(MSG_DEBUG, "nl80211: New scan results available");
836 drv->scan_complete_events = 1;
837 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
838 drv->ctx);
839 send_scan_event(drv, 0, tb);
840 break;
841 case NL80211_CMD_SCAN_ABORTED:
842 wpa_printf(MSG_DEBUG, "nl80211: Scan aborted");
844 * Need to indicate that scan results are available in order
845 * not to make wpa_supplicant stop its scanning.
847 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
848 drv->ctx);
849 send_scan_event(drv, 1, tb);
850 break;
851 case NL80211_CMD_AUTHENTICATE:
852 case NL80211_CMD_ASSOCIATE:
853 case NL80211_CMD_DEAUTHENTICATE:
854 case NL80211_CMD_DISASSOCIATE:
855 mlme_event(drv, gnlh->cmd, tb[NL80211_ATTR_FRAME],
856 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT]);
857 break;
858 case NL80211_CMD_CONNECT:
859 case NL80211_CMD_ROAM:
860 mlme_event_connect(drv, gnlh->cmd,
861 tb[NL80211_ATTR_STATUS_CODE],
862 tb[NL80211_ATTR_MAC],
863 tb[NL80211_ATTR_REQ_IE],
864 tb[NL80211_ATTR_RESP_IE]);
865 break;
866 case NL80211_CMD_DISCONNECT:
867 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
869 * Avoid reporting two disassociation events that could
870 * confuse the core code.
872 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
873 "event when using userspace SME");
874 break;
876 drv->associated = 0;
877 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
878 break;
879 case NL80211_CMD_MICHAEL_MIC_FAILURE:
880 mlme_event_michael_mic_failure(drv, tb);
881 break;
882 case NL80211_CMD_JOIN_IBSS:
883 mlme_event_join_ibss(drv, tb);
884 break;
885 case NL80211_CMD_REMAIN_ON_CHANNEL:
886 mlme_event_remain_on_channel(drv, 0, tb);
887 break;
888 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
889 mlme_event_remain_on_channel(drv, 1, tb);
890 break;
891 default:
892 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
893 "(cmd=%d)", gnlh->cmd);
894 break;
897 return NL_SKIP;
901 static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
902 void *sock_ctx)
904 struct nl_cb *cb;
905 struct wpa_driver_nl80211_data *drv = eloop_ctx;
907 wpa_printf(MSG_DEBUG, "nl80211: Event message available");
909 cb = nl_cb_clone(drv->nl_cb);
910 if (!cb)
911 return;
912 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
913 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, process_event, drv);
914 nl_recvmsgs(drv->nl_handle_event, cb);
915 nl_cb_put(cb);
920 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
921 * @priv: driver_nl80211 private data
922 * @alpha2_arg: country to which to switch to
923 * Returns: 0 on success, -1 on failure
925 * This asks nl80211 to set the regulatory domain for given
926 * country ISO / IEC alpha2.
928 static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
930 struct wpa_driver_nl80211_data *drv = priv;
931 char alpha2[3];
932 struct nl_msg *msg;
934 msg = nlmsg_alloc();
935 if (!msg)
936 return -ENOMEM;
938 alpha2[0] = alpha2_arg[0];
939 alpha2[1] = alpha2_arg[1];
940 alpha2[2] = '\0';
942 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
943 0, NL80211_CMD_REQ_SET_REG, 0);
945 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
946 if (send_and_recv_msgs(drv, msg, NULL, NULL))
947 return -EINVAL;
948 return 0;
949 nla_put_failure:
950 return -EINVAL;
954 #ifndef HOSTAPD
955 struct wiphy_info_data {
956 int max_scan_ssids;
957 int ap_supported;
958 int auth_supported;
959 int connect_supported;
963 static int wiphy_info_handler(struct nl_msg *msg, void *arg)
965 struct nlattr *tb[NL80211_ATTR_MAX + 1];
966 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
967 struct wiphy_info_data *info = arg;
969 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
970 genlmsg_attrlen(gnlh, 0), NULL);
972 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
973 info->max_scan_ssids =
974 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
976 if (tb[NL80211_ATTR_SUPPORTED_IFTYPES]) {
977 struct nlattr *nl_mode;
978 int i;
979 nla_for_each_nested(nl_mode,
980 tb[NL80211_ATTR_SUPPORTED_IFTYPES], i) {
981 if (nl_mode->nla_type == NL80211_IFTYPE_AP) {
982 info->ap_supported = 1;
983 break;
988 if (tb[NL80211_ATTR_SUPPORTED_COMMANDS]) {
989 struct nlattr *nl_cmd;
990 int i;
992 nla_for_each_nested(nl_cmd,
993 tb[NL80211_ATTR_SUPPORTED_COMMANDS], i) {
994 u32 cmd = nla_get_u32(nl_cmd);
995 if (cmd == NL80211_CMD_AUTHENTICATE)
996 info->auth_supported = 1;
997 else if (cmd == NL80211_CMD_CONNECT)
998 info->connect_supported = 1;
1002 return NL_SKIP;
1006 static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
1007 struct wiphy_info_data *info)
1009 struct nl_msg *msg;
1011 os_memset(info, 0, sizeof(*info));
1012 msg = nlmsg_alloc();
1013 if (!msg)
1014 return -1;
1016 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1017 0, NL80211_CMD_GET_WIPHY, 0);
1019 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1021 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info) == 0)
1022 return 0;
1023 msg = NULL;
1024 nla_put_failure:
1025 nlmsg_free(msg);
1026 return -1;
1030 static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
1032 struct wiphy_info_data info;
1033 if (wpa_driver_nl80211_get_info(drv, &info))
1034 return -1;
1035 drv->has_capability = 1;
1036 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
1037 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1038 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
1039 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
1040 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
1041 drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
1042 WPA_DRIVER_CAPA_ENC_WEP104 |
1043 WPA_DRIVER_CAPA_ENC_TKIP |
1044 WPA_DRIVER_CAPA_ENC_CCMP;
1045 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
1046 WPA_DRIVER_AUTH_SHARED |
1047 WPA_DRIVER_AUTH_LEAP;
1049 drv->capa.max_scan_ssids = info.max_scan_ssids;
1050 if (info.ap_supported)
1051 drv->capa.flags |= WPA_DRIVER_FLAGS_AP;
1053 if (info.auth_supported)
1054 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
1055 else if (!info.connect_supported) {
1056 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
1057 "authentication/association or connect commands");
1058 return -1;
1061 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
1063 return 0;
1065 #endif /* HOSTAPD */
1068 static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv,
1069 void *ctx)
1071 int ret;
1073 /* Initialize generic netlink and nl80211 */
1075 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1076 if (drv->nl_cb == NULL) {
1077 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1078 "callbacks");
1079 goto err1;
1082 drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
1083 if (drv->nl_handle == NULL) {
1084 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1085 "callbacks");
1086 goto err2;
1089 drv->nl_handle_event = nl_handle_alloc_cb(drv->nl_cb);
1090 if (drv->nl_handle_event == NULL) {
1091 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1092 "callbacks (event)");
1093 goto err2b;
1096 if (genl_connect(drv->nl_handle)) {
1097 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
1098 "netlink");
1099 goto err3;
1102 if (genl_connect(drv->nl_handle_event)) {
1103 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
1104 "netlink (event)");
1105 goto err3;
1108 #ifdef CONFIG_LIBNL20
1109 if (genl_ctrl_alloc_cache(drv->nl_handle, &drv->nl_cache) < 0) {
1110 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
1111 "netlink cache");
1112 goto err3;
1114 if (genl_ctrl_alloc_cache(drv->nl_handle_event, &drv->nl_cache_event) <
1115 0) {
1116 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
1117 "netlink cache (event)");
1118 goto err3b;
1120 #else /* CONFIG_LIBNL20 */
1121 drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
1122 if (drv->nl_cache == NULL) {
1123 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
1124 "netlink cache");
1125 goto err3;
1127 drv->nl_cache_event = genl_ctrl_alloc_cache(drv->nl_handle_event);
1128 if (drv->nl_cache_event == NULL) {
1129 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
1130 "netlink cache (event)");
1131 goto err3b;
1133 #endif /* CONFIG_LIBNL20 */
1135 drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
1136 if (drv->nl80211 == NULL) {
1137 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
1138 "found");
1139 goto err4;
1142 ret = nl_get_multicast_id(drv, "nl80211", "scan");
1143 if (ret >= 0)
1144 ret = nl_socket_add_membership(drv->nl_handle_event, ret);
1145 if (ret < 0) {
1146 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1147 "membership for scan events: %d (%s)",
1148 ret, strerror(-ret));
1149 goto err4;
1152 ret = nl_get_multicast_id(drv, "nl80211", "mlme");
1153 if (ret >= 0)
1154 ret = nl_socket_add_membership(drv->nl_handle_event, ret);
1155 if (ret < 0) {
1156 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1157 "membership for mlme events: %d (%s)",
1158 ret, strerror(-ret));
1159 goto err4;
1162 eloop_register_read_sock(nl_socket_get_fd(drv->nl_handle_event),
1163 wpa_driver_nl80211_event_receive, drv, ctx);
1165 return 0;
1167 err4:
1168 nl_cache_free(drv->nl_cache_event);
1169 err3b:
1170 nl_cache_free(drv->nl_cache);
1171 err3:
1172 nl_handle_destroy(drv->nl_handle_event);
1173 err2b:
1174 nl_handle_destroy(drv->nl_handle);
1175 err2:
1176 nl_cb_put(drv->nl_cb);
1177 err1:
1178 return -1;
1183 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
1184 * @ctx: context to be used when calling wpa_supplicant functions,
1185 * e.g., wpa_supplicant_event()
1186 * @ifname: interface name, e.g., wlan0
1187 * Returns: Pointer to private data, %NULL on failure
1189 static void * wpa_driver_nl80211_init(void *ctx, const char *ifname)
1191 struct wpa_driver_nl80211_data *drv;
1192 struct netlink_config *cfg;
1194 drv = os_zalloc(sizeof(*drv));
1195 if (drv == NULL)
1196 return NULL;
1197 drv->ctx = ctx;
1198 os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
1199 drv->monitor_ifidx = -1;
1200 drv->monitor_sock = -1;
1201 drv->ioctl_sock = -1;
1203 if (wpa_driver_nl80211_init_nl(drv, ctx)) {
1204 os_free(drv);
1205 return NULL;
1208 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1209 if (drv->ioctl_sock < 0) {
1210 perror("socket(PF_INET,SOCK_DGRAM)");
1211 goto failed;
1214 cfg = os_zalloc(sizeof(*cfg));
1215 if (cfg == NULL)
1216 goto failed;
1217 cfg->ctx = drv;
1218 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
1219 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
1220 drv->netlink = netlink_init(cfg);
1221 if (drv->netlink == NULL) {
1222 os_free(cfg);
1223 goto failed;
1225 if (wpa_driver_nl80211_finish_drv_init(drv))
1226 goto failed;
1228 return drv;
1230 failed:
1231 netlink_deinit(drv->netlink);
1232 if (drv->ioctl_sock >= 0)
1233 close(drv->ioctl_sock);
1235 genl_family_put(drv->nl80211);
1236 nl_cache_free(drv->nl_cache);
1237 nl_handle_destroy(drv->nl_handle);
1238 nl_cb_put(drv->nl_cb);
1240 os_free(drv);
1241 return NULL;
1245 static int
1246 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
1248 drv->ifindex = if_nametoindex(drv->ifname);
1250 #ifndef HOSTAPD
1251 if (wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_INFRA) < 0) {
1252 wpa_printf(MSG_DEBUG, "nl80211: Could not configure driver to "
1253 "use managed mode");
1256 if (linux_set_iface_flags(drv->ioctl_sock, drv->ifname, 1)) {
1257 wpa_printf(MSG_ERROR, "Could not set interface '%s' UP",
1258 drv->ifname);
1259 return -1;
1262 if (wpa_driver_nl80211_capa(drv))
1263 return -1;
1265 netlink_send_oper_ifla(drv->netlink, drv->ifindex,
1266 1, IF_OPER_DORMANT);
1267 #endif /* HOSTAPD */
1269 return 0;
1273 #ifdef HOSTAPD
1274 static void wpa_driver_nl80211_free_bss(struct wpa_driver_nl80211_data *drv)
1276 struct i802_bss *bss, *prev;
1277 bss = drv->bss.next;
1278 while (bss) {
1279 prev = bss;
1280 bss = bss->next;
1281 os_free(bss);
1284 #endif /* HOSTAPD */
1287 static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
1289 struct nl_msg *msg;
1291 msg = nlmsg_alloc();
1292 if (!msg)
1293 return -ENOMEM;
1295 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1296 0, NL80211_CMD_DEL_BEACON, 0);
1297 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1299 return send_and_recv_msgs(drv, msg, NULL, NULL);
1300 nla_put_failure:
1301 return -ENOBUFS;
1306 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
1307 * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
1309 * Shut down driver interface and processing of driver events. Free
1310 * private data buffer if one was allocated in wpa_driver_nl80211_init().
1312 static void wpa_driver_nl80211_deinit(void *priv)
1314 struct wpa_driver_nl80211_data *drv = priv;
1316 nl80211_remove_monitor_interface(drv);
1318 if (drv->nlmode == NL80211_IFTYPE_AP)
1319 wpa_driver_nl80211_del_beacon(drv);
1321 #ifdef HOSTAPD
1322 if (drv->last_freq_ht) {
1323 /* Clear HT flags from the driver */
1324 struct hostapd_freq_params freq;
1325 os_memset(&freq, 0, sizeof(freq));
1326 freq.freq = drv->last_freq;
1327 i802_set_freq(priv, &freq);
1330 if (drv->eapol_sock >= 0) {
1331 eloop_unregister_read_sock(drv->eapol_sock);
1332 close(drv->eapol_sock);
1335 if (drv->if_indices != drv->default_if_indices)
1336 os_free(drv->if_indices);
1338 wpa_driver_nl80211_free_bss(drv);
1339 #endif /* HOSTAPD */
1341 netlink_send_oper_ifla(drv->netlink, drv->ifindex, 0, IF_OPER_UP);
1342 netlink_deinit(drv->netlink);
1344 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
1346 (void) linux_set_iface_flags(drv->ioctl_sock, drv->ifname, 0);
1347 wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_INFRA);
1349 if (drv->ioctl_sock >= 0)
1350 close(drv->ioctl_sock);
1352 eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_handle_event));
1353 genl_family_put(drv->nl80211);
1354 nl_cache_free(drv->nl_cache);
1355 nl_cache_free(drv->nl_cache_event);
1356 nl_handle_destroy(drv->nl_handle);
1357 nl_handle_destroy(drv->nl_handle_event);
1358 nl_cb_put(drv->nl_cb);
1360 eloop_cancel_timeout(wpa_driver_nl80211_probe_req_report_timeout,
1361 drv, NULL);
1363 os_free(drv);
1368 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
1369 * @eloop_ctx: Driver private data
1370 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
1372 * This function can be used as registered timeout when starting a scan to
1373 * generate a scan completed event if the driver does not report this.
1375 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1377 struct wpa_driver_nl80211_data *drv = eloop_ctx;
1378 if (drv->ap_scan_as_station) {
1379 wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_AP);
1380 drv->ap_scan_as_station = 0;
1382 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
1383 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
1388 * wpa_driver_nl80211_scan - Request the driver to initiate scan
1389 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
1390 * @params: Scan parameters
1391 * Returns: 0 on success, -1 on failure
1393 static int wpa_driver_nl80211_scan(void *priv,
1394 struct wpa_driver_scan_params *params)
1396 struct wpa_driver_nl80211_data *drv = priv;
1397 int ret = 0, timeout;
1398 struct nl_msg *msg, *ssids, *freqs;
1399 size_t i;
1401 msg = nlmsg_alloc();
1402 ssids = nlmsg_alloc();
1403 freqs = nlmsg_alloc();
1404 if (!msg || !ssids || !freqs) {
1405 nlmsg_free(msg);
1406 nlmsg_free(ssids);
1407 nlmsg_free(freqs);
1408 return -1;
1411 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
1412 NL80211_CMD_TRIGGER_SCAN, 0);
1414 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1416 for (i = 0; i < params->num_ssids; i++) {
1417 NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len,
1418 params->ssids[i].ssid);
1420 if (params->num_ssids)
1421 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
1423 if (params->extra_ies) {
1424 NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len,
1425 params->extra_ies);
1428 if (params->freqs) {
1429 for (i = 0; params->freqs[i]; i++)
1430 NLA_PUT_U32(freqs, i + 1, params->freqs[i]);
1431 nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
1434 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1435 msg = NULL;
1436 if (ret) {
1437 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
1438 "(%s)", ret, strerror(-ret));
1439 #ifdef HOSTAPD
1440 if (drv->nlmode == NL80211_IFTYPE_AP) {
1442 * mac80211 does not allow scan requests in AP mode, so
1443 * try to do this in station mode.
1445 if (wpa_driver_nl80211_set_mode(drv,
1446 IEEE80211_MODE_INFRA))
1447 goto nla_put_failure;
1449 if (wpa_driver_nl80211_scan(drv, params)) {
1450 wpa_driver_nl80211_set_mode(drv,
1451 IEEE80211_MODE_AP);
1452 goto nla_put_failure;
1455 /* Restore AP mode when processing scan results */
1456 drv->ap_scan_as_station = 1;
1457 ret = 0;
1458 } else
1459 goto nla_put_failure;
1460 #else /* HOSTAPD */
1461 goto nla_put_failure;
1462 #endif /* HOSTAPD */
1465 /* Not all drivers generate "scan completed" wireless event, so try to
1466 * read results after a timeout. */
1467 timeout = 10;
1468 if (drv->scan_complete_events) {
1470 * The driver seems to deliver events to notify when scan is
1471 * complete, so use longer timeout to avoid race conditions
1472 * with scanning and following association request.
1474 timeout = 30;
1476 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
1477 "seconds", ret, timeout);
1478 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
1479 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
1480 drv, drv->ctx);
1482 nla_put_failure:
1483 nlmsg_free(ssids);
1484 nlmsg_free(msg);
1485 nlmsg_free(freqs);
1486 return ret;
1490 static int bss_info_handler(struct nl_msg *msg, void *arg)
1492 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1493 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1494 struct nlattr *bss[NL80211_BSS_MAX + 1];
1495 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1496 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
1497 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
1498 [NL80211_BSS_TSF] = { .type = NLA_U64 },
1499 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
1500 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
1501 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
1502 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
1503 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
1504 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
1505 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
1507 struct wpa_scan_results *res = arg;
1508 struct wpa_scan_res **tmp;
1509 struct wpa_scan_res *r;
1510 const u8 *ie;
1511 size_t ie_len;
1513 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1514 genlmsg_attrlen(gnlh, 0), NULL);
1515 if (!tb[NL80211_ATTR_BSS])
1516 return NL_SKIP;
1517 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
1518 bss_policy))
1519 return NL_SKIP;
1520 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
1521 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1522 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1523 } else {
1524 ie = NULL;
1525 ie_len = 0;
1528 r = os_zalloc(sizeof(*r) + ie_len);
1529 if (r == NULL)
1530 return NL_SKIP;
1531 if (bss[NL80211_BSS_BSSID])
1532 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
1533 ETH_ALEN);
1534 if (bss[NL80211_BSS_FREQUENCY])
1535 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1536 if (bss[NL80211_BSS_BEACON_INTERVAL])
1537 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
1538 if (bss[NL80211_BSS_CAPABILITY])
1539 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
1540 r->flags |= WPA_SCAN_NOISE_INVALID;
1541 if (bss[NL80211_BSS_SIGNAL_MBM]) {
1542 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
1543 r->level /= 100; /* mBm to dBm */
1544 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
1545 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
1546 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
1547 r->flags |= WPA_SCAN_LEVEL_INVALID;
1548 } else
1549 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
1550 if (bss[NL80211_BSS_TSF])
1551 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
1552 if (bss[NL80211_BSS_SEEN_MS_AGO])
1553 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
1554 r->ie_len = ie_len;
1555 if (ie)
1556 os_memcpy(r + 1, ie, ie_len);
1558 if (bss[NL80211_BSS_STATUS]) {
1559 enum nl80211_bss_status status;
1560 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
1561 switch (status) {
1562 case NL80211_BSS_STATUS_AUTHENTICATED:
1563 r->flags |= WPA_SCAN_AUTHENTICATED;
1564 break;
1565 case NL80211_BSS_STATUS_ASSOCIATED:
1566 r->flags |= WPA_SCAN_ASSOCIATED;
1567 break;
1568 default:
1569 break;
1573 tmp = os_realloc(res->res,
1574 (res->num + 1) * sizeof(struct wpa_scan_res *));
1575 if (tmp == NULL) {
1576 os_free(r);
1577 return NL_SKIP;
1579 tmp[res->num++] = r;
1580 res->res = tmp;
1582 return NL_SKIP;
1586 static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
1587 const u8 *addr)
1589 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1590 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
1591 "mismatch");
1592 wpa_driver_nl80211_mlme(drv, addr,
1593 NL80211_CMD_DEAUTHENTICATE,
1594 WLAN_REASON_PREV_AUTH_NOT_VALID);
1599 static void wpa_driver_nl80211_check_bss_status(
1600 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
1602 size_t i;
1604 for (i = 0; i < res->num; i++) {
1605 struct wpa_scan_res *r = res->res[i];
1606 if (r->flags & WPA_SCAN_AUTHENTICATED) {
1607 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
1608 "indicates BSS status with " MACSTR
1609 " as authenticated",
1610 MAC2STR(r->bssid));
1611 if (drv->nlmode == NL80211_IFTYPE_STATION &&
1612 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
1613 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
1614 0) {
1615 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
1616 " in local state (auth=" MACSTR
1617 " assoc=" MACSTR ")",
1618 MAC2STR(drv->auth_bssid),
1619 MAC2STR(drv->bssid));
1623 if (r->flags & WPA_SCAN_ASSOCIATED) {
1624 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
1625 "indicate BSS status with " MACSTR
1626 " as associated",
1627 MAC2STR(r->bssid));
1628 if (drv->nlmode == NL80211_IFTYPE_STATION &&
1629 !drv->associated) {
1630 wpa_printf(MSG_DEBUG, "nl80211: Local state "
1631 "(not associated) does not match "
1632 "with BSS state");
1633 clear_state_mismatch(drv, r->bssid);
1634 } else if (drv->nlmode == NL80211_IFTYPE_STATION &&
1635 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
1636 0) {
1637 wpa_printf(MSG_DEBUG, "nl80211: Local state "
1638 "(associated with " MACSTR ") does "
1639 "not match with BSS state",
1640 MAC2STR(drv->bssid));
1641 clear_state_mismatch(drv, r->bssid);
1642 clear_state_mismatch(drv, drv->bssid);
1649 static void wpa_scan_results_free(struct wpa_scan_results *res)
1651 size_t i;
1653 if (res == NULL)
1654 return;
1656 for (i = 0; i < res->num; i++)
1657 os_free(res->res[i]);
1658 os_free(res->res);
1659 os_free(res);
1664 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
1665 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
1666 * Returns: Scan results on success, -1 on failure
1668 static struct wpa_scan_results *
1669 wpa_driver_nl80211_get_scan_results(void *priv)
1671 struct wpa_driver_nl80211_data *drv = priv;
1672 struct nl_msg *msg;
1673 struct wpa_scan_results *res;
1674 int ret;
1676 res = os_zalloc(sizeof(*res));
1677 if (res == NULL)
1678 return NULL;
1679 msg = nlmsg_alloc();
1680 if (!msg)
1681 goto nla_put_failure;
1683 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, NLM_F_DUMP,
1684 NL80211_CMD_GET_SCAN, 0);
1685 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1687 ret = send_and_recv_msgs(drv, msg, bss_info_handler, res);
1688 msg = NULL;
1689 if (ret == 0) {
1690 wpa_printf(MSG_DEBUG, "Received scan results (%lu BSSes)",
1691 (unsigned long) res->num);
1692 wpa_driver_nl80211_check_bss_status(drv, res);
1693 return res;
1695 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1696 "(%s)", ret, strerror(-ret));
1697 nla_put_failure:
1698 nlmsg_free(msg);
1699 wpa_scan_results_free(res);
1700 return NULL;
1704 static int wpa_driver_nl80211_set_key(const char *ifname, void *priv,
1705 enum wpa_alg alg, const u8 *addr,
1706 int key_idx, int set_tx,
1707 const u8 *seq, size_t seq_len,
1708 const u8 *key, size_t key_len)
1710 struct wpa_driver_nl80211_data *drv = priv;
1711 int ifindex = if_nametoindex(ifname);
1712 struct nl_msg *msg;
1713 int ret;
1715 wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
1716 "set_tx=%d seq_len=%lu key_len=%lu",
1717 __func__, ifindex, alg, addr, key_idx, set_tx,
1718 (unsigned long) seq_len, (unsigned long) key_len);
1720 msg = nlmsg_alloc();
1721 if (!msg)
1722 return -ENOMEM;
1724 if (alg == WPA_ALG_NONE) {
1725 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1726 0, NL80211_CMD_DEL_KEY, 0);
1727 } else {
1728 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1729 0, NL80211_CMD_NEW_KEY, 0);
1730 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
1731 switch (alg) {
1732 case WPA_ALG_WEP:
1733 if (key_len == 5)
1734 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
1735 WLAN_CIPHER_SUITE_WEP40);
1736 else
1737 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
1738 WLAN_CIPHER_SUITE_WEP104);
1739 break;
1740 case WPA_ALG_TKIP:
1741 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
1742 WLAN_CIPHER_SUITE_TKIP);
1743 break;
1744 case WPA_ALG_CCMP:
1745 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
1746 WLAN_CIPHER_SUITE_CCMP);
1747 break;
1748 case WPA_ALG_IGTK:
1749 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
1750 WLAN_CIPHER_SUITE_AES_CMAC);
1751 break;
1752 default:
1753 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
1754 "algorithm %d", __func__, alg);
1755 nlmsg_free(msg);
1756 return -1;
1760 if (seq && seq_len)
1761 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
1763 if (addr && os_memcmp(addr, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1765 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
1766 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1768 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1769 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
1771 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1772 if (ret == -ENOENT && alg == WPA_ALG_NONE)
1773 ret = 0;
1774 if (ret)
1775 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
1776 ret, strerror(-ret));
1779 * If we failed or don't need to set the default TX key (below),
1780 * we're done here.
1782 if (ret || !set_tx || alg == WPA_ALG_NONE)
1783 return ret;
1784 #ifdef HOSTAPD /* FIX: is this needed? */
1785 if (addr)
1786 return ret;
1787 #endif /* HOSTAPD */
1789 msg = nlmsg_alloc();
1790 if (!msg)
1791 return -ENOMEM;
1793 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1794 0, NL80211_CMD_SET_KEY, 0);
1795 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1796 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
1797 if (alg == WPA_ALG_IGTK)
1798 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
1799 else
1800 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
1802 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1803 if (ret == -ENOENT)
1804 ret = 0;
1805 if (ret)
1806 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
1807 "err=%d %s)", ret, strerror(-ret));
1808 return ret;
1810 nla_put_failure:
1811 return -ENOBUFS;
1815 static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
1816 int key_idx, int defkey,
1817 const u8 *seq, size_t seq_len,
1818 const u8 *key, size_t key_len)
1820 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
1821 if (!key_attr)
1822 return -1;
1824 if (defkey && alg == WPA_ALG_IGTK)
1825 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
1826 else if (defkey)
1827 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
1829 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
1831 switch (alg) {
1832 case WPA_ALG_WEP:
1833 if (key_len == 5)
1834 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
1835 WLAN_CIPHER_SUITE_WEP40);
1836 else
1837 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
1838 WLAN_CIPHER_SUITE_WEP104);
1839 break;
1840 case WPA_ALG_TKIP:
1841 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
1842 break;
1843 case WPA_ALG_CCMP:
1844 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
1845 break;
1846 case WPA_ALG_IGTK:
1847 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
1848 WLAN_CIPHER_SUITE_AES_CMAC);
1849 break;
1850 default:
1851 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
1852 "algorithm %d", __func__, alg);
1853 return -1;
1856 if (seq && seq_len)
1857 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
1859 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
1861 nla_nest_end(msg, key_attr);
1863 return 0;
1864 nla_put_failure:
1865 return -1;
1869 static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
1870 struct nl_msg *msg)
1872 int i, privacy = 0;
1873 struct nlattr *nl_keys, *nl_key;
1875 for (i = 0; i < 4; i++) {
1876 if (!params->wep_key[i])
1877 continue;
1878 privacy = 1;
1879 break;
1881 if (!privacy)
1882 return 0;
1884 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
1886 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
1887 if (!nl_keys)
1888 goto nla_put_failure;
1890 for (i = 0; i < 4; i++) {
1891 if (!params->wep_key[i])
1892 continue;
1894 nl_key = nla_nest_start(msg, i);
1895 if (!nl_key)
1896 goto nla_put_failure;
1898 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
1899 params->wep_key[i]);
1900 if (params->wep_key_len[i] == 5)
1901 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
1902 WLAN_CIPHER_SUITE_WEP40);
1903 else
1904 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
1905 WLAN_CIPHER_SUITE_WEP104);
1907 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
1909 if (i == params->wep_tx_keyidx)
1910 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
1912 nla_nest_end(msg, nl_key);
1914 nla_nest_end(msg, nl_keys);
1916 return 0;
1918 nla_put_failure:
1919 return -ENOBUFS;
1923 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
1924 const u8 *addr, int cmd, u16 reason_code)
1926 int ret = -1;
1927 struct nl_msg *msg;
1929 msg = nlmsg_alloc();
1930 if (!msg)
1931 return -1;
1933 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, cmd, 0);
1935 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1936 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
1937 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1939 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1940 msg = NULL;
1941 if (ret) {
1942 wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
1943 "(%s)", ret, strerror(-ret));
1944 goto nla_put_failure;
1946 ret = 0;
1948 nla_put_failure:
1949 nlmsg_free(msg);
1950 return ret;
1954 static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
1955 const u8 *addr, int reason_code)
1957 wpa_printf(MSG_DEBUG, "%s", __func__);
1958 drv->associated = 0;
1959 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISCONNECT,
1960 reason_code);
1964 static int wpa_driver_nl80211_deauthenticate(void *priv, const u8 *addr,
1965 int reason_code)
1967 struct wpa_driver_nl80211_data *drv = priv;
1968 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
1969 return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
1970 wpa_printf(MSG_DEBUG, "%s", __func__);
1971 drv->associated = 0;
1972 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
1973 reason_code);
1977 static int wpa_driver_nl80211_disassociate(void *priv, const u8 *addr,
1978 int reason_code)
1980 struct wpa_driver_nl80211_data *drv = priv;
1981 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
1982 return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
1983 wpa_printf(MSG_DEBUG, "%s", __func__);
1984 drv->associated = 0;
1985 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISASSOCIATE,
1986 reason_code);
1990 static int wpa_driver_nl80211_authenticate(
1991 void *priv, struct wpa_driver_auth_params *params)
1993 struct wpa_driver_nl80211_data *drv = priv;
1994 int ret = -1, i;
1995 struct nl_msg *msg;
1996 enum nl80211_auth_type type;
1997 int count = 0;
1999 drv->associated = 0;
2000 os_memset(drv->auth_bssid, 0, ETH_ALEN);
2002 if (wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_INFRA) < 0)
2003 return -1;
2005 retry:
2006 msg = nlmsg_alloc();
2007 if (!msg)
2008 return -1;
2010 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
2011 drv->ifindex);
2013 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
2014 NL80211_CMD_AUTHENTICATE, 0);
2016 for (i = 0; i < 4; i++) {
2017 if (!params->wep_key[i])
2018 continue;
2019 wpa_driver_nl80211_set_key(drv->ifname, drv, WPA_ALG_WEP, NULL,
2021 i == params->wep_tx_keyidx, NULL, 0,
2022 params->wep_key[i],
2023 params->wep_key_len[i]);
2024 if (params->wep_tx_keyidx != i)
2025 continue;
2026 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
2027 params->wep_key[i], params->wep_key_len[i])) {
2028 nlmsg_free(msg);
2029 return -1;
2033 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2034 if (params->bssid) {
2035 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
2036 MAC2STR(params->bssid));
2037 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
2039 if (params->freq) {
2040 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
2041 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
2043 if (params->ssid) {
2044 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
2045 params->ssid, params->ssid_len);
2046 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
2047 params->ssid);
2049 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
2050 if (params->ie)
2051 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
2053 * TODO: if multiple auth_alg options enabled, try them one by one if
2054 * the AP rejects authentication due to unknown auth alg
2056 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
2057 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
2058 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
2059 type = NL80211_AUTHTYPE_SHARED_KEY;
2060 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
2061 type = NL80211_AUTHTYPE_NETWORK_EAP;
2062 else if (params->auth_alg & WPA_AUTH_ALG_FT)
2063 type = NL80211_AUTHTYPE_FT;
2064 else
2065 goto nla_put_failure;
2066 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
2067 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
2069 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2070 msg = NULL;
2071 if (ret) {
2072 wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
2073 "(%s)", ret, strerror(-ret));
2074 count++;
2075 if (ret == -EALREADY && count == 1 && params->bssid) {
2077 * mac80211 does not currently accept new
2078 * authentication if we are already authenticated. As a
2079 * workaround, force deauthentication and try again.
2081 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
2082 "after forced deauthentication");
2083 wpa_driver_nl80211_deauthenticate(
2084 drv, params->bssid,
2085 WLAN_REASON_PREV_AUTH_NOT_VALID);
2086 nlmsg_free(msg);
2087 goto retry;
2089 goto nla_put_failure;
2091 ret = 0;
2092 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
2093 "successfully");
2095 nla_put_failure:
2096 nlmsg_free(msg);
2097 return ret;
2101 struct phy_info_arg {
2102 u16 *num_modes;
2103 struct hostapd_hw_modes *modes;
2106 static int phy_info_handler(struct nl_msg *msg, void *arg)
2108 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
2109 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2110 struct phy_info_arg *phy_info = arg;
2112 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
2114 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
2115 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
2116 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
2117 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
2118 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
2119 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
2120 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
2121 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
2124 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
2125 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
2126 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
2127 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
2130 struct nlattr *nl_band;
2131 struct nlattr *nl_freq;
2132 struct nlattr *nl_rate;
2133 int rem_band, rem_freq, rem_rate;
2134 struct hostapd_hw_modes *mode;
2135 int idx, mode_is_set;
2137 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2138 genlmsg_attrlen(gnlh, 0), NULL);
2140 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
2141 return NL_SKIP;
2143 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
2144 mode = os_realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
2145 if (!mode)
2146 return NL_SKIP;
2147 phy_info->modes = mode;
2149 mode_is_set = 0;
2151 mode = &phy_info->modes[*(phy_info->num_modes)];
2152 memset(mode, 0, sizeof(*mode));
2153 *(phy_info->num_modes) += 1;
2155 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
2156 nla_len(nl_band), NULL);
2158 if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
2159 mode->ht_capab = nla_get_u16(
2160 tb_band[NL80211_BAND_ATTR_HT_CAPA]);
2163 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) {
2164 mode->a_mpdu_params |= nla_get_u8(
2165 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) &
2166 0x03;
2169 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) {
2170 mode->a_mpdu_params |= nla_get_u8(
2171 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) <<
2175 if (tb_band[NL80211_BAND_ATTR_HT_MCS_SET] &&
2176 nla_len(tb_band[NL80211_BAND_ATTR_HT_MCS_SET])) {
2177 u8 *mcs;
2178 mcs = nla_data(tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
2179 os_memcpy(mode->mcs_set, mcs, 16);
2182 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
2183 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
2184 nla_len(nl_freq), freq_policy);
2185 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
2186 continue;
2187 mode->num_channels++;
2190 mode->channels = os_zalloc(mode->num_channels * sizeof(struct hostapd_channel_data));
2191 if (!mode->channels)
2192 return NL_SKIP;
2194 idx = 0;
2196 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
2197 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
2198 nla_len(nl_freq), freq_policy);
2199 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
2200 continue;
2202 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
2203 mode->channels[idx].flag = 0;
2205 if (!mode_is_set) {
2206 /* crude heuristic */
2207 if (mode->channels[idx].freq < 4000)
2208 mode->mode = HOSTAPD_MODE_IEEE80211B;
2209 else
2210 mode->mode = HOSTAPD_MODE_IEEE80211A;
2211 mode_is_set = 1;
2214 /* crude heuristic */
2215 if (mode->channels[idx].freq < 4000)
2216 if (mode->channels[idx].freq == 2484)
2217 mode->channels[idx].chan = 14;
2218 else
2219 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
2220 else
2221 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
2223 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
2224 mode->channels[idx].flag |=
2225 HOSTAPD_CHAN_DISABLED;
2226 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
2227 mode->channels[idx].flag |=
2228 HOSTAPD_CHAN_PASSIVE_SCAN;
2229 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
2230 mode->channels[idx].flag |=
2231 HOSTAPD_CHAN_NO_IBSS;
2232 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
2233 mode->channels[idx].flag |=
2234 HOSTAPD_CHAN_RADAR;
2236 if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
2237 !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
2238 mode->channels[idx].max_tx_power =
2239 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
2241 idx++;
2244 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
2245 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
2246 nla_len(nl_rate), rate_policy);
2247 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
2248 continue;
2249 mode->num_rates++;
2252 mode->rates = os_zalloc(mode->num_rates * sizeof(int));
2253 if (!mode->rates)
2254 return NL_SKIP;
2256 idx = 0;
2258 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
2259 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
2260 nla_len(nl_rate), rate_policy);
2261 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
2262 continue;
2263 mode->rates[idx] = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
2265 /* crude heuristic */
2266 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
2267 mode->rates[idx] > 200)
2268 mode->mode = HOSTAPD_MODE_IEEE80211G;
2270 idx++;
2274 return NL_SKIP;
2277 static struct hostapd_hw_modes *
2278 wpa_driver_nl80211_add_11b(struct hostapd_hw_modes *modes, u16 *num_modes)
2280 u16 m;
2281 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
2282 int i, mode11g_idx = -1;
2284 /* If only 802.11g mode is included, use it to construct matching
2285 * 802.11b mode data. */
2287 for (m = 0; m < *num_modes; m++) {
2288 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
2289 return modes; /* 802.11b already included */
2290 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
2291 mode11g_idx = m;
2294 if (mode11g_idx < 0)
2295 return modes; /* 2.4 GHz band not supported at all */
2297 nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes));
2298 if (nmodes == NULL)
2299 return modes; /* Could not add 802.11b mode */
2301 mode = &nmodes[*num_modes];
2302 os_memset(mode, 0, sizeof(*mode));
2303 (*num_modes)++;
2304 modes = nmodes;
2306 mode->mode = HOSTAPD_MODE_IEEE80211B;
2308 mode11g = &modes[mode11g_idx];
2309 mode->num_channels = mode11g->num_channels;
2310 mode->channels = os_malloc(mode11g->num_channels *
2311 sizeof(struct hostapd_channel_data));
2312 if (mode->channels == NULL) {
2313 (*num_modes)--;
2314 return modes; /* Could not add 802.11b mode */
2316 os_memcpy(mode->channels, mode11g->channels,
2317 mode11g->num_channels * sizeof(struct hostapd_channel_data));
2319 mode->num_rates = 0;
2320 mode->rates = os_malloc(4 * sizeof(int));
2321 if (mode->rates == NULL) {
2322 os_free(mode->channels);
2323 (*num_modes)--;
2324 return modes; /* Could not add 802.11b mode */
2327 for (i = 0; i < mode11g->num_rates; i++) {
2328 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
2329 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
2330 continue;
2331 mode->rates[mode->num_rates] = mode11g->rates[i];
2332 mode->num_rates++;
2333 if (mode->num_rates == 4)
2334 break;
2337 if (mode->num_rates == 0) {
2338 os_free(mode->channels);
2339 os_free(mode->rates);
2340 (*num_modes)--;
2341 return modes; /* No 802.11b rates */
2344 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
2345 "information");
2347 return modes;
2351 static struct hostapd_hw_modes *
2352 wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
2354 struct wpa_driver_nl80211_data *drv = priv;
2355 struct nl_msg *msg;
2356 struct phy_info_arg result = {
2357 .num_modes = num_modes,
2358 .modes = NULL,
2361 *num_modes = 0;
2362 *flags = 0;
2364 msg = nlmsg_alloc();
2365 if (!msg)
2366 return NULL;
2368 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2369 0, NL80211_CMD_GET_WIPHY, 0);
2371 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2373 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0)
2374 return wpa_driver_nl80211_add_11b(result.modes, num_modes);
2375 nla_put_failure:
2376 return NULL;
2380 static int wpa_driver_nl80211_send_frame(struct wpa_driver_nl80211_data *drv,
2381 const void *data, size_t len,
2382 int encrypt)
2384 __u8 rtap_hdr[] = {
2385 0x00, 0x00, /* radiotap version */
2386 0x0e, 0x00, /* radiotap length */
2387 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
2388 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
2389 0x00, /* padding */
2390 0x00, 0x00, /* RX and TX flags to indicate that */
2391 0x00, 0x00, /* this is the injected frame directly */
2393 struct iovec iov[2] = {
2395 .iov_base = &rtap_hdr,
2396 .iov_len = sizeof(rtap_hdr),
2399 .iov_base = (void *) data,
2400 .iov_len = len,
2403 struct msghdr msg = {
2404 .msg_name = NULL,
2405 .msg_namelen = 0,
2406 .msg_iov = iov,
2407 .msg_iovlen = 2,
2408 .msg_control = NULL,
2409 .msg_controllen = 0,
2410 .msg_flags = 0,
2413 if (encrypt)
2414 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
2416 return sendmsg(drv->monitor_sock, &msg, 0);
2420 static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data,
2421 size_t data_len)
2423 struct wpa_driver_nl80211_data *drv = priv;
2424 struct ieee80211_mgmt *mgmt;
2425 int encrypt = 1;
2426 u16 fc;
2428 mgmt = (struct ieee80211_mgmt *) data;
2429 fc = le_to_host16(mgmt->frame_control);
2431 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2432 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
2434 * Only one of the authentication frame types is encrypted.
2435 * In order for static WEP encryption to work properly (i.e.,
2436 * to not encrypt the frame), we need to tell mac80211 about
2437 * the frames that must not be encrypted.
2439 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
2440 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
2441 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
2442 encrypt = 0;
2445 return wpa_driver_nl80211_send_frame(drv, data, data_len, encrypt);
2449 static int wpa_driver_nl80211_set_beacon(const char *ifname, void *priv,
2450 const u8 *head, size_t head_len,
2451 const u8 *tail, size_t tail_len,
2452 int dtim_period, int beacon_int)
2454 struct wpa_driver_nl80211_data *drv = priv;
2455 struct nl_msg *msg;
2456 u8 cmd = NL80211_CMD_NEW_BEACON;
2457 int ret;
2458 int beacon_set;
2459 int ifindex = if_nametoindex(ifname);
2460 #ifdef HOSTAPD
2461 struct i802_bss *bss;
2463 bss = get_bss(drv, ifindex);
2464 if (bss == NULL)
2465 return -ENOENT;
2466 beacon_set = bss->beacon_set;
2467 #else /* HOSTAPD */
2468 beacon_set = drv->beacon_set;
2469 #endif /* HOSTAPD */
2471 msg = nlmsg_alloc();
2472 if (!msg)
2473 return -ENOMEM;
2475 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
2476 beacon_set);
2477 if (beacon_set)
2478 cmd = NL80211_CMD_SET_BEACON;
2480 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2481 0, cmd, 0);
2482 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
2483 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
2484 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
2485 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, beacon_int);
2486 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
2488 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2489 if (ret) {
2490 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
2491 ret, strerror(-ret));
2492 } else {
2493 #ifdef HOSTAPD
2494 bss->beacon_set = 1;
2495 #else /* HOSTAPD */
2496 drv->beacon_set = 1;
2497 #endif /* HOSTAPD */
2499 return ret;
2500 nla_put_failure:
2501 return -ENOBUFS;
2505 static int wpa_driver_nl80211_set_freq(struct wpa_driver_nl80211_data *drv,
2506 int freq, int ht_enabled,
2507 int sec_channel_offset)
2509 struct nl_msg *msg;
2510 int ret;
2512 msg = nlmsg_alloc();
2513 if (!msg)
2514 return -1;
2516 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
2517 NL80211_CMD_SET_WIPHY, 0);
2519 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2520 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
2521 if (ht_enabled) {
2522 switch (sec_channel_offset) {
2523 case -1:
2524 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2525 NL80211_CHAN_HT40MINUS);
2526 break;
2527 case 1:
2528 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2529 NL80211_CHAN_HT40PLUS);
2530 break;
2531 default:
2532 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2533 NL80211_CHAN_HT20);
2534 break;
2538 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2539 if (ret == 0)
2540 return 0;
2541 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
2542 "%d (%s)", freq, ret, strerror(-ret));
2543 nla_put_failure:
2544 return -1;
2548 static int wpa_driver_nl80211_sta_add(const char *ifname, void *priv,
2549 struct hostapd_sta_add_params *params)
2551 struct wpa_driver_nl80211_data *drv = priv;
2552 struct nl_msg *msg;
2553 int ret = -ENOBUFS;
2555 msg = nlmsg_alloc();
2556 if (!msg)
2557 return -ENOMEM;
2559 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2560 0, NL80211_CMD_NEW_STATION, 0);
2562 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(ifname));
2563 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
2564 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
2565 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
2566 params->supp_rates);
2567 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
2568 params->listen_interval);
2569 if (params->ht_capabilities) {
2570 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
2571 sizeof(*params->ht_capabilities),
2572 params->ht_capabilities);
2575 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2576 if (ret)
2577 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_NEW_STATION "
2578 "result: %d (%s)", ret, strerror(-ret));
2579 if (ret == -EEXIST)
2580 ret = 0;
2581 nla_put_failure:
2582 return ret;
2586 static int wpa_driver_nl80211_sta_remove(void *priv, const u8 *addr)
2588 struct wpa_driver_nl80211_data *drv = priv;
2589 struct nl_msg *msg;
2590 int ret;
2592 msg = nlmsg_alloc();
2593 if (!msg)
2594 return -ENOMEM;
2596 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2597 0, NL80211_CMD_DEL_STATION, 0);
2599 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
2600 if_nametoindex(drv->ifname));
2601 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
2603 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2604 if (ret == -ENOENT)
2605 return 0;
2606 return ret;
2607 nla_put_failure:
2608 return -ENOBUFS;
2612 static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
2613 int ifidx)
2615 struct nl_msg *msg;
2617 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
2619 #ifdef HOSTAPD
2620 /* stop listening for EAPOL on this interface */
2621 del_ifidx(drv, ifidx);
2622 #endif /* HOSTAPD */
2624 msg = nlmsg_alloc();
2625 if (!msg)
2626 goto nla_put_failure;
2628 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2629 0, NL80211_CMD_DEL_INTERFACE, 0);
2630 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
2632 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
2633 return;
2634 nla_put_failure:
2635 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d).\n",
2636 ifidx);
2640 static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
2641 const char *ifname,
2642 enum nl80211_iftype iftype,
2643 const u8 *addr, int wds)
2645 struct nl_msg *msg, *flags = NULL;
2646 int ifidx;
2647 int ret = -ENOBUFS;
2649 msg = nlmsg_alloc();
2650 if (!msg)
2651 return -1;
2653 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2654 0, NL80211_CMD_NEW_INTERFACE, 0);
2655 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2656 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
2657 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
2659 if (iftype == NL80211_IFTYPE_MONITOR) {
2660 int err;
2662 flags = nlmsg_alloc();
2663 if (!flags)
2664 goto nla_put_failure;
2666 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
2668 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
2670 nlmsg_free(flags);
2672 if (err)
2673 goto nla_put_failure;
2674 } else if (wds) {
2675 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
2678 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2679 if (ret) {
2680 nla_put_failure:
2681 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
2682 ifname, ret, strerror(-ret));
2683 return ret;
2686 ifidx = if_nametoindex(ifname);
2687 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
2688 ifname, ifidx);
2690 if (ifidx <= 0)
2691 return -1;
2693 #ifdef HOSTAPD
2694 /* start listening for EAPOL on this interface */
2695 add_ifidx(drv, ifidx);
2696 #endif /* HOSTAPD */
2698 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
2699 set_ifhwaddr(drv, ifname, addr)) {
2700 nl80211_remove_iface(drv, ifidx);
2701 return -1;
2704 return ifidx;
2708 static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
2709 const char *ifname, enum nl80211_iftype iftype,
2710 const u8 *addr, int wds)
2712 int ret;
2714 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds);
2716 /* if error occured and interface exists already */
2717 if (ret == -ENFILE && if_nametoindex(ifname)) {
2718 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
2720 /* Try to remove the interface that was already there. */
2721 nl80211_remove_iface(drv, if_nametoindex(ifname));
2723 /* Try to create the interface again */
2724 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
2725 wds);
2728 return ret;
2732 static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
2734 struct ieee80211_hdr *hdr;
2735 u16 fc;
2736 union wpa_event_data event;
2738 hdr = (struct ieee80211_hdr *) buf;
2739 fc = le_to_host16(hdr->frame_control);
2741 os_memset(&event, 0, sizeof(event));
2742 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
2743 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
2744 event.tx_status.dst = hdr->addr1;
2745 event.tx_status.data = buf;
2746 event.tx_status.data_len = len;
2747 event.tx_status.ack = ok;
2748 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
2752 static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
2753 u8 *buf, size_t len)
2755 union wpa_event_data event;
2756 os_memset(&event, 0, sizeof(event));
2757 event.rx_from_unknown.frame = buf;
2758 event.rx_from_unknown.len = len;
2759 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2763 static void handle_frame(struct wpa_driver_nl80211_data *drv,
2764 u8 *buf, size_t len, int datarate, int ssi_signal)
2766 struct ieee80211_hdr *hdr;
2767 u16 fc;
2768 union wpa_event_data event;
2770 hdr = (struct ieee80211_hdr *) buf;
2771 fc = le_to_host16(hdr->frame_control);
2773 switch (WLAN_FC_GET_TYPE(fc)) {
2774 case WLAN_FC_TYPE_MGMT:
2775 os_memset(&event, 0, sizeof(event));
2776 event.rx_mgmt.frame = buf;
2777 event.rx_mgmt.frame_len = len;
2778 event.rx_mgmt.datarate = datarate;
2779 event.rx_mgmt.ssi_signal = ssi_signal;
2780 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
2781 break;
2782 case WLAN_FC_TYPE_CTRL:
2783 /* can only get here with PS-Poll frames */
2784 wpa_printf(MSG_DEBUG, "CTRL");
2785 from_unknown_sta(drv, buf, len);
2786 break;
2787 case WLAN_FC_TYPE_DATA:
2788 from_unknown_sta(drv, buf, len);
2789 break;
2794 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
2796 struct wpa_driver_nl80211_data *drv = eloop_ctx;
2797 int len;
2798 unsigned char buf[3000];
2799 struct ieee80211_radiotap_iterator iter;
2800 int ret;
2801 int datarate = 0, ssi_signal = 0;
2802 int injected = 0, failed = 0, rxflags = 0;
2804 len = recv(sock, buf, sizeof(buf), 0);
2805 if (len < 0) {
2806 perror("recv");
2807 return;
2810 if (drv->nlmode == NL80211_IFTYPE_STATION && !drv->probe_req_report) {
2811 wpa_printf(MSG_DEBUG, "nl80211: Ignore monitor interface "
2812 "frame since Probe Request reporting is disabled");
2813 return;
2816 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
2817 printf("received invalid radiotap frame\n");
2818 return;
2821 while (1) {
2822 ret = ieee80211_radiotap_iterator_next(&iter);
2823 if (ret == -ENOENT)
2824 break;
2825 if (ret) {
2826 printf("received invalid radiotap frame (%d)\n", ret);
2827 return;
2829 switch (iter.this_arg_index) {
2830 case IEEE80211_RADIOTAP_FLAGS:
2831 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
2832 len -= 4;
2833 break;
2834 case IEEE80211_RADIOTAP_RX_FLAGS:
2835 rxflags = 1;
2836 break;
2837 case IEEE80211_RADIOTAP_TX_FLAGS:
2838 injected = 1;
2839 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
2840 IEEE80211_RADIOTAP_F_TX_FAIL;
2841 break;
2842 case IEEE80211_RADIOTAP_DATA_RETRIES:
2843 break;
2844 case IEEE80211_RADIOTAP_CHANNEL:
2845 /* TODO: convert from freq/flags to channel number */
2846 break;
2847 case IEEE80211_RADIOTAP_RATE:
2848 datarate = *iter.this_arg * 5;
2849 break;
2850 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
2851 ssi_signal = *iter.this_arg;
2852 break;
2856 if (rxflags && injected)
2857 return;
2859 if (!injected)
2860 handle_frame(drv, buf + iter.max_length,
2861 len - iter.max_length, datarate, ssi_signal);
2862 else
2863 handle_tx_callback(drv->ctx, buf + iter.max_length,
2864 len - iter.max_length, !failed);
2869 * we post-process the filter code later and rewrite
2870 * this to the offset to the last instruction
2872 #define PASS 0xFF
2873 #define FAIL 0xFE
2875 static struct sock_filter msock_filter_insns[] = {
2877 * do a little-endian load of the radiotap length field
2879 /* load lower byte into A */
2880 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
2881 /* put it into X (== index register) */
2882 BPF_STMT(BPF_MISC| BPF_TAX, 0),
2883 /* load upper byte into A */
2884 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
2885 /* left-shift it by 8 */
2886 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
2887 /* or with X */
2888 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
2889 /* put result into X */
2890 BPF_STMT(BPF_MISC| BPF_TAX, 0),
2893 * Allow management frames through, this also gives us those
2894 * management frames that we sent ourselves with status
2896 /* load the lower byte of the IEEE 802.11 frame control field */
2897 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
2898 /* mask off frame type and version */
2899 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
2900 /* accept frame if it's both 0, fall through otherwise */
2901 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
2904 * TODO: add a bit to radiotap RX flags that indicates
2905 * that the sending station is not associated, then
2906 * add a filter here that filters on our DA and that flag
2907 * to allow us to deauth frames to that bad station.
2909 * Not a regression -- we didn't do it before either.
2912 #if 0
2914 * drop non-data frames
2916 /* load the lower byte of the frame control field */
2917 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
2918 /* mask off QoS bit */
2919 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
2920 /* drop non-data frames */
2921 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
2922 #endif
2923 /* load the upper byte of the frame control field */
2924 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
2925 /* mask off toDS/fromDS */
2926 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
2927 /* accept WDS frames */
2928 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
2931 * add header length to index
2933 /* load the lower byte of the frame control field */
2934 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
2935 /* mask off QoS bit */
2936 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
2937 /* right shift it by 6 to give 0 or 2 */
2938 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
2939 /* add data frame header length */
2940 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
2941 /* add index, was start of 802.11 header */
2942 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
2943 /* move to index, now start of LL header */
2944 BPF_STMT(BPF_MISC | BPF_TAX, 0),
2947 * Accept empty data frames, we use those for
2948 * polling activity.
2950 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
2951 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
2954 * Accept EAPOL frames
2956 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
2957 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
2958 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
2959 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
2961 /* keep these last two statements or change the code below */
2962 /* return 0 == "DROP" */
2963 BPF_STMT(BPF_RET | BPF_K, 0),
2964 /* return ~0 == "keep all" */
2965 BPF_STMT(BPF_RET | BPF_K, ~0),
2968 static struct sock_fprog msock_filter = {
2969 .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
2970 .filter = msock_filter_insns,
2974 static int add_monitor_filter(int s)
2976 int idx;
2978 /* rewrite all PASS/FAIL jump offsets */
2979 for (idx = 0; idx < msock_filter.len; idx++) {
2980 struct sock_filter *insn = &msock_filter_insns[idx];
2982 if (BPF_CLASS(insn->code) == BPF_JMP) {
2983 if (insn->code == (BPF_JMP|BPF_JA)) {
2984 if (insn->k == PASS)
2985 insn->k = msock_filter.len - idx - 2;
2986 else if (insn->k == FAIL)
2987 insn->k = msock_filter.len - idx - 3;
2990 if (insn->jt == PASS)
2991 insn->jt = msock_filter.len - idx - 2;
2992 else if (insn->jt == FAIL)
2993 insn->jt = msock_filter.len - idx - 3;
2995 if (insn->jf == PASS)
2996 insn->jf = msock_filter.len - idx - 2;
2997 else if (insn->jf == FAIL)
2998 insn->jf = msock_filter.len - idx - 3;
3002 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
3003 &msock_filter, sizeof(msock_filter))) {
3004 perror("SO_ATTACH_FILTER");
3005 return -1;
3008 return 0;
3012 static void nl80211_remove_monitor_interface(
3013 struct wpa_driver_nl80211_data *drv)
3015 if (drv->monitor_ifidx >= 0) {
3016 nl80211_remove_iface(drv, drv->monitor_ifidx);
3017 drv->monitor_ifidx = -1;
3019 if (drv->monitor_sock >= 0) {
3020 eloop_unregister_read_sock(drv->monitor_sock);
3021 close(drv->monitor_sock);
3022 drv->monitor_sock = -1;
3027 static int
3028 nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
3030 char buf[IFNAMSIZ];
3031 struct sockaddr_ll ll;
3032 int optval;
3033 socklen_t optlen;
3035 snprintf(buf, IFNAMSIZ, "mon.%s", drv->ifname);
3036 buf[IFNAMSIZ - 1] = '\0';
3038 drv->monitor_ifidx =
3039 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
3042 if (drv->monitor_ifidx < 0)
3043 return -1;
3045 if (linux_set_iface_flags(drv->ioctl_sock, buf, 1))
3046 goto error;
3048 memset(&ll, 0, sizeof(ll));
3049 ll.sll_family = AF_PACKET;
3050 ll.sll_ifindex = drv->monitor_ifidx;
3051 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
3052 if (drv->monitor_sock < 0) {
3053 perror("socket[PF_PACKET,SOCK_RAW]");
3054 goto error;
3057 if (add_monitor_filter(drv->monitor_sock)) {
3058 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
3059 "interface; do filtering in user space");
3060 /* This works, but will cost in performance. */
3063 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
3064 perror("monitor socket bind");
3065 goto error;
3068 optlen = sizeof(optval);
3069 optval = 20;
3070 if (setsockopt
3071 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
3072 perror("Failed to set socket priority");
3073 goto error;
3076 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
3077 drv, NULL)) {
3078 printf("Could not register monitor read socket\n");
3079 goto error;
3082 return 0;
3083 error:
3084 nl80211_remove_monitor_interface(drv);
3085 return -1;
3089 static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
3091 static int wpa_driver_nl80211_hapd_send_eapol(
3092 void *priv, const u8 *addr, const u8 *data,
3093 size_t data_len, int encrypt, const u8 *own_addr)
3095 struct wpa_driver_nl80211_data *drv = priv;
3096 struct ieee80211_hdr *hdr;
3097 size_t len;
3098 u8 *pos;
3099 int res;
3100 #if 0 /* FIX */
3101 int qos = sta->flags & WPA_STA_WMM;
3102 #else
3103 int qos = 0;
3104 #endif
3106 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
3107 data_len;
3108 hdr = os_zalloc(len);
3109 if (hdr == NULL) {
3110 printf("malloc() failed for i802_send_data(len=%lu)\n",
3111 (unsigned long) len);
3112 return -1;
3115 hdr->frame_control =
3116 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
3117 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
3118 if (encrypt)
3119 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
3120 #if 0 /* To be enabled if qos determination is added above */
3121 if (qos) {
3122 hdr->frame_control |=
3123 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
3125 #endif
3127 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
3128 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
3129 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
3130 pos = (u8 *) (hdr + 1);
3132 #if 0 /* To be enabled if qos determination is added above */
3133 if (qos) {
3134 /* add an empty QoS header if needed */
3135 pos[0] = 0;
3136 pos[1] = 0;
3137 pos += 2;
3139 #endif
3141 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
3142 pos += sizeof(rfc1042_header);
3143 WPA_PUT_BE16(pos, ETH_P_PAE);
3144 pos += 2;
3145 memcpy(pos, data, data_len);
3147 res = wpa_driver_nl80211_send_frame(drv, (u8 *) hdr, len, encrypt);
3148 if (res < 0) {
3149 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
3150 "failed: %d (%s)",
3151 (unsigned long) len, errno, strerror(errno));
3153 os_free(hdr);
3155 return res;
3159 static u32 sta_flags_nl80211(int flags)
3161 u32 f = 0;
3163 if (flags & WPA_STA_AUTHORIZED)
3164 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
3165 if (flags & WPA_STA_WMM)
3166 f |= BIT(NL80211_STA_FLAG_WME);
3167 if (flags & WPA_STA_SHORT_PREAMBLE)
3168 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
3169 if (flags & WPA_STA_MFP)
3170 f |= BIT(NL80211_STA_FLAG_MFP);
3172 return f;
3176 static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
3177 int total_flags, int flags_or,
3178 int flags_and)
3180 struct wpa_driver_nl80211_data *drv = priv;
3181 struct nl_msg *msg, *flags = NULL;
3182 struct nl80211_sta_flag_update upd;
3184 msg = nlmsg_alloc();
3185 if (!msg)
3186 return -ENOMEM;
3188 flags = nlmsg_alloc();
3189 if (!flags) {
3190 nlmsg_free(msg);
3191 return -ENOMEM;
3194 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3195 0, NL80211_CMD_SET_STATION, 0);
3197 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
3198 if_nametoindex(drv->ifname));
3199 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
3202 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
3203 * can be removed eventually.
3205 if (total_flags & WPA_STA_AUTHORIZED)
3206 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
3208 if (total_flags & WPA_STA_WMM)
3209 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
3211 if (total_flags & WPA_STA_SHORT_PREAMBLE)
3212 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
3214 if (total_flags & WPA_STA_MFP)
3215 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
3217 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
3218 goto nla_put_failure;
3220 os_memset(&upd, 0, sizeof(upd));
3221 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
3222 upd.set = sta_flags_nl80211(flags_or);
3223 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
3225 nlmsg_free(flags);
3227 return send_and_recv_msgs(drv, msg, NULL, NULL);
3228 nla_put_failure:
3229 nlmsg_free(flags);
3230 return -ENOBUFS;
3234 static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
3235 struct wpa_driver_associate_params *params)
3237 if (wpa_driver_nl80211_set_mode(drv, params->mode) ||
3238 wpa_driver_nl80211_set_freq(drv, params->freq, 0, 0)) {
3239 nl80211_remove_monitor_interface(drv);
3240 return -1;
3243 /* TODO: setup monitor interface (and add code somewhere to remove this
3244 * when AP mode is stopped; associate with mode != 2 or drv_deinit) */
3246 return 0;
3250 static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
3252 struct nl_msg *msg;
3253 int ret = -1;
3255 msg = nlmsg_alloc();
3256 if (!msg)
3257 return -1;
3259 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
3260 NL80211_CMD_LEAVE_IBSS, 0);
3261 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3262 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3263 msg = NULL;
3264 if (ret) {
3265 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
3266 "(%s)", ret, strerror(-ret));
3267 goto nla_put_failure;
3270 ret = 0;
3271 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
3273 nla_put_failure:
3274 nlmsg_free(msg);
3275 return ret;
3279 static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
3280 struct wpa_driver_associate_params *params)
3282 struct nl_msg *msg;
3283 int ret = -1;
3284 int count = 0;
3286 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
3288 if (wpa_driver_nl80211_set_mode(drv, params->mode)) {
3289 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
3290 "IBSS mode");
3291 return -1;
3294 retry:
3295 msg = nlmsg_alloc();
3296 if (!msg)
3297 return -1;
3299 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
3300 NL80211_CMD_JOIN_IBSS, 0);
3301 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3303 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
3304 goto nla_put_failure;
3306 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
3307 params->ssid, params->ssid_len);
3308 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
3309 params->ssid);
3310 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
3311 drv->ssid_len = params->ssid_len;
3313 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
3314 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
3316 ret = nl80211_set_conn_keys(params, msg);
3317 if (ret)
3318 goto nla_put_failure;
3320 if (params->wpa_ie) {
3321 wpa_hexdump(MSG_DEBUG,
3322 " * Extra IEs for Beacon/Probe Response frames",
3323 params->wpa_ie, params->wpa_ie_len);
3324 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
3325 params->wpa_ie);
3328 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3329 msg = NULL;
3330 if (ret) {
3331 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
3332 ret, strerror(-ret));
3333 count++;
3334 if (ret == -EALREADY && count == 1) {
3335 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
3336 "forced leave");
3337 nl80211_leave_ibss(drv);
3338 nlmsg_free(msg);
3339 goto retry;
3342 goto nla_put_failure;
3344 ret = 0;
3345 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
3347 nla_put_failure:
3348 nlmsg_free(msg);
3349 return ret;
3353 static int wpa_driver_nl80211_connect(
3354 struct wpa_driver_nl80211_data *drv,
3355 struct wpa_driver_associate_params *params)
3357 struct nl_msg *msg;
3358 enum nl80211_auth_type type;
3359 int ret = 0;
3361 msg = nlmsg_alloc();
3362 if (!msg)
3363 return -1;
3365 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
3366 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
3367 NL80211_CMD_CONNECT, 0);
3369 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3370 if (params->bssid) {
3371 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
3372 MAC2STR(params->bssid));
3373 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
3375 if (params->freq) {
3376 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
3377 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
3379 if (params->ssid) {
3380 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
3381 params->ssid, params->ssid_len);
3382 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
3383 params->ssid);
3384 if (params->ssid_len > sizeof(drv->ssid))
3385 goto nla_put_failure;
3386 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
3387 drv->ssid_len = params->ssid_len;
3389 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
3390 if (params->wpa_ie)
3391 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
3392 params->wpa_ie);
3394 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
3395 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
3396 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
3397 type = NL80211_AUTHTYPE_SHARED_KEY;
3398 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
3399 type = NL80211_AUTHTYPE_NETWORK_EAP;
3400 else if (params->auth_alg & WPA_AUTH_ALG_FT)
3401 type = NL80211_AUTHTYPE_FT;
3402 else
3403 goto nla_put_failure;
3405 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
3406 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
3408 if (params->wpa_ie && params->wpa_ie_len) {
3409 enum nl80211_wpa_versions ver;
3411 if (params->wpa_ie[0] == WLAN_EID_RSN)
3412 ver = NL80211_WPA_VERSION_2;
3413 else
3414 ver = NL80211_WPA_VERSION_1;
3416 wpa_printf(MSG_DEBUG, " * WPA Version %d", ver);
3417 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
3420 if (params->pairwise_suite != CIPHER_NONE) {
3421 int cipher;
3423 switch (params->pairwise_suite) {
3424 case CIPHER_WEP40:
3425 cipher = WLAN_CIPHER_SUITE_WEP40;
3426 break;
3427 case CIPHER_WEP104:
3428 cipher = WLAN_CIPHER_SUITE_WEP104;
3429 break;
3430 case CIPHER_CCMP:
3431 cipher = WLAN_CIPHER_SUITE_CCMP;
3432 break;
3433 case CIPHER_TKIP:
3434 default:
3435 cipher = WLAN_CIPHER_SUITE_TKIP;
3436 break;
3438 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
3441 if (params->group_suite != CIPHER_NONE) {
3442 int cipher;
3444 switch (params->group_suite) {
3445 case CIPHER_WEP40:
3446 cipher = WLAN_CIPHER_SUITE_WEP40;
3447 break;
3448 case CIPHER_WEP104:
3449 cipher = WLAN_CIPHER_SUITE_WEP104;
3450 break;
3451 case CIPHER_CCMP:
3452 cipher = WLAN_CIPHER_SUITE_CCMP;
3453 break;
3454 case CIPHER_TKIP:
3455 default:
3456 cipher = WLAN_CIPHER_SUITE_TKIP;
3457 break;
3459 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
3462 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
3463 params->key_mgmt_suite == KEY_MGMT_PSK) {
3464 int mgmt = WLAN_AKM_SUITE_PSK;
3466 switch (params->key_mgmt_suite) {
3467 case KEY_MGMT_802_1X:
3468 mgmt = WLAN_AKM_SUITE_8021X;
3469 break;
3470 case KEY_MGMT_PSK:
3471 default:
3472 mgmt = WLAN_AKM_SUITE_PSK;
3473 break;
3475 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
3478 ret = nl80211_set_conn_keys(params, msg);
3479 if (ret)
3480 goto nla_put_failure;
3482 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3483 msg = NULL;
3484 if (ret) {
3485 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
3486 "(%s)", ret, strerror(-ret));
3487 goto nla_put_failure;
3489 ret = 0;
3490 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
3492 nla_put_failure:
3493 nlmsg_free(msg);
3494 return ret;
3499 static int wpa_driver_nl80211_associate(
3500 void *priv, struct wpa_driver_associate_params *params)
3502 struct wpa_driver_nl80211_data *drv = priv;
3503 int ret = -1;
3504 struct nl_msg *msg;
3506 if (params->mode == IEEE80211_MODE_AP)
3507 return wpa_driver_nl80211_ap(drv, params);
3509 if (params->mode == IEEE80211_MODE_IBSS)
3510 return wpa_driver_nl80211_ibss(drv, params);
3512 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
3513 if (wpa_driver_nl80211_set_mode(drv, params->mode) < 0)
3514 return -1;
3515 return wpa_driver_nl80211_connect(drv, params);
3518 drv->associated = 0;
3520 msg = nlmsg_alloc();
3521 if (!msg)
3522 return -1;
3524 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
3525 drv->ifindex);
3526 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
3527 NL80211_CMD_ASSOCIATE, 0);
3529 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3530 if (params->bssid) {
3531 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
3532 MAC2STR(params->bssid));
3533 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
3535 if (params->freq) {
3536 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
3537 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
3538 drv->assoc_freq = params->freq;
3539 } else
3540 drv->assoc_freq = 0;
3541 if (params->ssid) {
3542 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
3543 params->ssid, params->ssid_len);
3544 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
3545 params->ssid);
3546 if (params->ssid_len > sizeof(drv->ssid))
3547 goto nla_put_failure;
3548 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
3549 drv->ssid_len = params->ssid_len;
3551 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
3552 if (params->wpa_ie)
3553 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
3554 params->wpa_ie);
3556 #ifdef CONFIG_IEEE80211W
3557 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
3558 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
3559 #endif /* CONFIG_IEEE80211W */
3561 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
3563 if (params->prev_bssid) {
3564 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
3565 MAC2STR(params->prev_bssid));
3566 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
3567 params->prev_bssid);
3570 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3571 msg = NULL;
3572 if (ret) {
3573 wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
3574 "(%s)", ret, strerror(-ret));
3575 goto nla_put_failure;
3577 ret = 0;
3578 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
3579 "successfully");
3581 nla_put_failure:
3582 nlmsg_free(msg);
3583 return ret;
3587 static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
3588 int ifindex, int mode)
3590 struct nl_msg *msg;
3591 int ret = -ENOBUFS;
3593 msg = nlmsg_alloc();
3594 if (!msg)
3595 return -ENOMEM;
3597 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3598 0, NL80211_CMD_SET_INTERFACE, 0);
3599 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
3600 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
3602 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3603 if (!ret)
3604 return 0;
3605 nla_put_failure:
3606 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
3607 " %d (%s)", ifindex, mode, ret, strerror(-ret));
3608 return ret;
3612 static int wpa_driver_nl80211_set_mode(void *priv, int mode)
3614 struct wpa_driver_nl80211_data *drv = priv;
3615 int ret = -1;
3616 int nlmode;
3618 switch (mode) {
3619 case 0:
3620 nlmode = NL80211_IFTYPE_STATION;
3621 break;
3622 case 1:
3623 nlmode = NL80211_IFTYPE_ADHOC;
3624 break;
3625 case 2:
3626 nlmode = NL80211_IFTYPE_AP;
3627 break;
3628 default:
3629 return -1;
3632 if (nl80211_set_mode(drv, drv->ifindex, nlmode) == 0) {
3633 drv->nlmode = nlmode;
3634 ret = 0;
3635 goto done;
3638 if (nlmode == drv->nlmode) {
3639 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
3640 "requested mode - ignore error");
3641 ret = 0;
3642 goto done; /* Already in the requested mode */
3645 /* mac80211 doesn't allow mode changes while the device is up, so
3646 * take the device down, try to set the mode again, and bring the
3647 * device back up.
3649 if (linux_set_iface_flags(drv->ioctl_sock, drv->ifname, 0) == 0) {
3650 /* Try to set the mode again while the interface is down */
3651 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
3652 if (linux_set_iface_flags(drv->ioctl_sock, drv->ifname, 1))
3653 ret = -1;
3656 if (!ret) {
3657 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
3658 "interface is down");
3659 drv->nlmode = nlmode;
3662 done:
3663 if (!ret && nlmode == NL80211_IFTYPE_AP) {
3664 /* Setup additional AP mode functionality if needed */
3665 if (drv->monitor_ifidx < 0 &&
3666 nl80211_create_monitor_interface(drv))
3667 return -1;
3668 } else if (!ret && nlmode != NL80211_IFTYPE_AP) {
3669 /* Remove additional AP mode functionality */
3670 nl80211_remove_monitor_interface(drv);
3673 if (ret)
3674 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
3675 "from %d failed", nlmode, drv->nlmode);
3677 return ret;
3681 static int wpa_driver_nl80211_get_capa(void *priv,
3682 struct wpa_driver_capa *capa)
3684 struct wpa_driver_nl80211_data *drv = priv;
3685 if (!drv->has_capability)
3686 return -1;
3687 os_memcpy(capa, &drv->capa, sizeof(*capa));
3688 return 0;
3692 static int wpa_driver_nl80211_set_operstate(void *priv, int state)
3694 struct wpa_driver_nl80211_data *drv = priv;
3696 wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
3697 __func__, drv->operstate, state, state ? "UP" : "DORMANT");
3698 drv->operstate = state;
3699 return netlink_send_oper_ifla(drv->netlink, drv->ifindex, -1,
3700 state ? IF_OPER_UP : IF_OPER_DORMANT);
3704 static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
3706 struct wpa_driver_nl80211_data *drv = priv;
3707 struct nl_msg *msg;
3708 struct nl80211_sta_flag_update upd;
3710 msg = nlmsg_alloc();
3711 if (!msg)
3712 return -ENOMEM;
3714 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3715 0, NL80211_CMD_SET_STATION, 0);
3717 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
3718 if_nametoindex(drv->ifname));
3719 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
3721 os_memset(&upd, 0, sizeof(upd));
3722 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
3723 if (authorized)
3724 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
3725 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
3727 return send_and_recv_msgs(drv, msg, NULL, NULL);
3728 nla_put_failure:
3729 return -ENOBUFS;
3733 #ifdef HOSTAPD
3735 static struct i802_bss * get_bss(struct wpa_driver_nl80211_data *drv,
3736 int ifindex)
3738 struct i802_bss *bss = &drv->bss;
3739 while (bss) {
3740 if (ifindex == bss->ifindex)
3741 return bss;
3742 bss = bss->next;
3744 wpa_printf(MSG_DEBUG, "nl80211: get_bss(%d) failed", ifindex);
3745 return NULL;
3749 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
3751 int i;
3752 int *old;
3754 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
3755 ifidx);
3756 for (i = 0; i < drv->num_if_indices; i++) {
3757 if (drv->if_indices[i] == 0) {
3758 drv->if_indices[i] = ifidx;
3759 return;
3763 if (drv->if_indices != drv->default_if_indices)
3764 old = drv->if_indices;
3765 else
3766 old = NULL;
3768 drv->if_indices = os_realloc(old,
3769 sizeof(int) * (drv->num_if_indices + 1));
3770 if (!drv->if_indices) {
3771 if (!old)
3772 drv->if_indices = drv->default_if_indices;
3773 else
3774 drv->if_indices = old;
3775 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
3776 "interfaces");
3777 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
3778 return;
3780 drv->if_indices[drv->num_if_indices] = ifidx;
3781 drv->num_if_indices++;
3785 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
3787 int i;
3789 for (i = 0; i < drv->num_if_indices; i++) {
3790 if (drv->if_indices[i] == ifidx) {
3791 drv->if_indices[i] = 0;
3792 break;
3798 static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
3800 int i;
3802 for (i = 0; i < drv->num_if_indices; i++)
3803 if (drv->if_indices[i] == ifidx)
3804 return 1;
3806 return 0;
3810 static inline int min_int(int a, int b)
3812 if (a < b)
3813 return a;
3814 return b;
3818 static int get_key_handler(struct nl_msg *msg, void *arg)
3820 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3821 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3823 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3824 genlmsg_attrlen(gnlh, 0), NULL);
3827 * TODO: validate the key index and mac address!
3828 * Otherwise, there's a race condition as soon as
3829 * the kernel starts sending key notifications.
3832 if (tb[NL80211_ATTR_KEY_SEQ])
3833 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
3834 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
3835 return NL_SKIP;
3839 static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
3840 int idx, u8 *seq)
3842 struct wpa_driver_nl80211_data *drv = priv;
3843 struct nl_msg *msg;
3845 msg = nlmsg_alloc();
3846 if (!msg)
3847 return -ENOMEM;
3849 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3850 0, NL80211_CMD_GET_KEY, 0);
3852 if (addr)
3853 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
3854 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
3855 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
3857 memset(seq, 0, 6);
3859 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
3860 nla_put_failure:
3861 return -ENOBUFS;
3865 static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
3866 int mode)
3868 struct wpa_driver_nl80211_data *drv = priv;
3869 struct nl_msg *msg;
3870 u8 rates[NL80211_MAX_SUPP_RATES];
3871 u8 rates_len = 0;
3872 int i;
3874 msg = nlmsg_alloc();
3875 if (!msg)
3876 return -ENOMEM;
3878 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
3879 NL80211_CMD_SET_BSS, 0);
3881 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
3882 rates[rates_len++] = basic_rates[i] / 5;
3884 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
3886 /* TODO: multi-BSS support */
3887 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
3889 return send_and_recv_msgs(drv, msg, NULL, NULL);
3890 nla_put_failure:
3891 return -ENOBUFS;
3895 /* Set kernel driver on given frequency (MHz) */
3896 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
3898 struct wpa_driver_nl80211_data *drv = priv;
3899 return wpa_driver_nl80211_set_freq(drv, freq->freq, freq->ht_enabled,
3900 freq->sec_channel_offset);
3904 static int i802_set_rts(void *priv, int rts)
3906 struct wpa_driver_nl80211_data *drv = priv;
3907 struct nl_msg *msg;
3908 int ret = -ENOBUFS;
3909 u32 val;
3911 msg = nlmsg_alloc();
3912 if (!msg)
3913 return -ENOMEM;
3915 if (rts >= 2347)
3916 val = (u32) -1;
3917 else
3918 val = rts;
3920 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3921 0, NL80211_CMD_SET_WIPHY, 0);
3922 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3923 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
3925 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3926 if (!ret)
3927 return 0;
3928 nla_put_failure:
3929 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
3930 "%d (%s)", rts, ret, strerror(-ret));
3931 return ret;
3935 static int i802_set_frag(void *priv, int frag)
3937 struct wpa_driver_nl80211_data *drv = priv;
3938 struct nl_msg *msg;
3939 int ret = -ENOBUFS;
3940 u32 val;
3942 msg = nlmsg_alloc();
3943 if (!msg)
3944 return -ENOMEM;
3946 if (frag >= 2346)
3947 val = (u32) -1;
3948 else
3949 val = frag;
3951 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3952 0, NL80211_CMD_SET_WIPHY, 0);
3953 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3954 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
3956 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3957 if (!ret)
3958 return 0;
3959 nla_put_failure:
3960 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
3961 "%d: %d (%s)", frag, ret, strerror(-ret));
3962 return ret;
3966 static int i802_flush(void *priv)
3968 struct wpa_driver_nl80211_data *drv = priv;
3969 struct nl_msg *msg;
3971 msg = nlmsg_alloc();
3972 if (!msg)
3973 return -1;
3975 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3976 0, NL80211_CMD_DEL_STATION, 0);
3979 * XXX: FIX! this needs to flush all VLANs too
3981 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
3982 if_nametoindex(drv->ifname));
3984 return send_and_recv_msgs(drv, msg, NULL, NULL);
3985 nla_put_failure:
3986 return -ENOBUFS;
3990 static int get_sta_handler(struct nl_msg *msg, void *arg)
3992 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3993 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3994 struct hostap_sta_driver_data *data = arg;
3995 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
3996 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
3997 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
3998 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
3999 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
4000 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
4001 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
4004 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4005 genlmsg_attrlen(gnlh, 0), NULL);
4008 * TODO: validate the interface and mac address!
4009 * Otherwise, there's a race condition as soon as
4010 * the kernel starts sending station notifications.
4013 if (!tb[NL80211_ATTR_STA_INFO]) {
4014 wpa_printf(MSG_DEBUG, "sta stats missing!");
4015 return NL_SKIP;
4017 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
4018 tb[NL80211_ATTR_STA_INFO],
4019 stats_policy)) {
4020 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
4021 return NL_SKIP;
4024 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
4025 data->inactive_msec =
4026 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
4027 if (stats[NL80211_STA_INFO_RX_BYTES])
4028 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
4029 if (stats[NL80211_STA_INFO_TX_BYTES])
4030 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
4031 if (stats[NL80211_STA_INFO_RX_PACKETS])
4032 data->rx_packets =
4033 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
4034 if (stats[NL80211_STA_INFO_TX_PACKETS])
4035 data->tx_packets =
4036 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
4038 return NL_SKIP;
4041 static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
4042 const u8 *addr)
4044 struct wpa_driver_nl80211_data *drv = priv;
4045 struct nl_msg *msg;
4047 os_memset(data, 0, sizeof(*data));
4048 msg = nlmsg_alloc();
4049 if (!msg)
4050 return -ENOMEM;
4052 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
4053 0, NL80211_CMD_GET_STATION, 0);
4055 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4056 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
4058 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
4059 nla_put_failure:
4060 return -ENOBUFS;
4064 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
4065 int cw_min, int cw_max, int burst_time)
4067 struct wpa_driver_nl80211_data *drv = priv;
4068 struct nl_msg *msg;
4069 struct nlattr *txq, *params;
4071 msg = nlmsg_alloc();
4072 if (!msg)
4073 return -1;
4075 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
4076 0, NL80211_CMD_SET_WIPHY, 0);
4078 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
4080 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
4081 if (!txq)
4082 goto nla_put_failure;
4084 /* We are only sending parameters for a single TXQ at a time */
4085 params = nla_nest_start(msg, 1);
4086 if (!params)
4087 goto nla_put_failure;
4089 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, queue);
4090 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
4091 * 32 usec, so need to convert the value here. */
4092 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
4093 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
4094 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
4095 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
4097 nla_nest_end(msg, params);
4099 nla_nest_end(msg, txq);
4101 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
4102 return 0;
4103 nla_put_failure:
4104 return -1;
4108 static int i802_set_bss(void *priv, int cts, int preamble, int slot)
4110 struct wpa_driver_nl80211_data *drv = priv;
4111 struct nl_msg *msg;
4113 msg = nlmsg_alloc();
4114 if (!msg)
4115 return -ENOMEM;
4117 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
4118 NL80211_CMD_SET_BSS, 0);
4120 if (cts >= 0)
4121 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
4122 if (preamble >= 0)
4123 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
4124 if (slot >= 0)
4125 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
4127 /* TODO: multi-BSS support */
4128 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
4130 return send_and_recv_msgs(drv, msg, NULL, NULL);
4131 nla_put_failure:
4132 return -ENOBUFS;
4136 static int i802_set_cts_protect(void *priv, int value)
4138 return i802_set_bss(priv, value, -1, -1);
4142 static int i802_set_preamble(void *priv, int value)
4144 return i802_set_bss(priv, -1, value, -1);
4148 static int i802_set_short_slot_time(void *priv, int value)
4150 return i802_set_bss(priv, -1, -1, value);
4154 static int i802_set_sta_vlan(void *priv, const u8 *addr,
4155 const char *ifname, int vlan_id)
4157 struct wpa_driver_nl80211_data *drv = priv;
4158 struct nl_msg *msg;
4160 msg = nlmsg_alloc();
4161 if (!msg)
4162 return -ENOMEM;
4164 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
4165 0, NL80211_CMD_SET_STATION, 0);
4167 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
4168 if_nametoindex(drv->ifname));
4169 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4170 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
4171 if_nametoindex(ifname));
4173 return send_and_recv_msgs(drv, msg, NULL, NULL);
4174 nla_put_failure:
4175 return -ENOBUFS;
4179 static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val)
4181 struct wpa_driver_nl80211_data *drv = priv;
4182 char name[16];
4184 os_snprintf(name, sizeof(name), "%s.sta%d", drv->ifname, aid);
4185 if (val) {
4186 if (nl80211_create_iface(priv, name, NL80211_IFTYPE_AP_VLAN,
4187 NULL, 1) < 0)
4188 return -1;
4189 linux_set_iface_flags(drv->ioctl_sock, name, 1);
4190 return i802_set_sta_vlan(priv, addr, name, 0);
4191 } else {
4192 i802_set_sta_vlan(priv, addr, drv->ifname, 0);
4193 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
4194 name);
4199 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
4201 struct wpa_driver_nl80211_data *drv = eloop_ctx;
4202 struct sockaddr_ll lladdr;
4203 unsigned char buf[3000];
4204 int len;
4205 socklen_t fromlen = sizeof(lladdr);
4207 len = recvfrom(sock, buf, sizeof(buf), 0,
4208 (struct sockaddr *)&lladdr, &fromlen);
4209 if (len < 0) {
4210 perror("recv");
4211 return;
4214 if (have_ifidx(drv, lladdr.sll_ifindex))
4215 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
4219 static int i802_get_inact_sec(void *priv, const u8 *addr)
4221 struct hostap_sta_driver_data data;
4222 int ret;
4224 data.inactive_msec = (unsigned long) -1;
4225 ret = i802_read_sta_data(priv, &data, addr);
4226 if (ret || data.inactive_msec == (unsigned long) -1)
4227 return -1;
4228 return data.inactive_msec / 1000;
4232 static int i802_sta_clear_stats(void *priv, const u8 *addr)
4234 #if 0
4235 /* TODO */
4236 #endif
4237 return 0;
4241 static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
4242 int reason)
4244 struct wpa_driver_nl80211_data *drv = priv;
4245 struct ieee80211_mgmt mgmt;
4247 memset(&mgmt, 0, sizeof(mgmt));
4248 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
4249 WLAN_FC_STYPE_DEAUTH);
4250 memcpy(mgmt.da, addr, ETH_ALEN);
4251 memcpy(mgmt.sa, own_addr, ETH_ALEN);
4252 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
4253 mgmt.u.deauth.reason_code = host_to_le16(reason);
4254 return wpa_driver_nl80211_send_mlme(drv, (u8 *) &mgmt,
4255 IEEE80211_HDRLEN +
4256 sizeof(mgmt.u.deauth));
4260 static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
4261 int reason)
4263 struct wpa_driver_nl80211_data *drv = priv;
4264 struct ieee80211_mgmt mgmt;
4266 memset(&mgmt, 0, sizeof(mgmt));
4267 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
4268 WLAN_FC_STYPE_DISASSOC);
4269 memcpy(mgmt.da, addr, ETH_ALEN);
4270 memcpy(mgmt.sa, own_addr, ETH_ALEN);
4271 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
4272 mgmt.u.disassoc.reason_code = host_to_le16(reason);
4273 return wpa_driver_nl80211_send_mlme(drv, (u8 *) &mgmt,
4274 IEEE80211_HDRLEN +
4275 sizeof(mgmt.u.disassoc));
4279 static void *i802_init(struct hostapd_data *hapd,
4280 struct wpa_init_params *params)
4282 struct wpa_driver_nl80211_data *drv;
4283 size_t i;
4285 drv = wpa_driver_nl80211_init(hapd, params->ifname);
4286 if (drv == NULL)
4287 return NULL;
4289 drv->bss.ifindex = drv->ifindex;
4291 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
4292 drv->if_indices = drv->default_if_indices;
4293 for (i = 0; i < params->num_bridge; i++) {
4294 if (params->bridge[i])
4295 add_ifidx(drv, if_nametoindex(params->bridge[i]));
4298 /* start listening for EAPOL on the default AP interface */
4299 add_ifidx(drv, drv->ifindex);
4301 if (linux_set_iface_flags(drv->ioctl_sock, drv->ifname, 0))
4302 goto failed;
4304 if (params->bssid) {
4305 if (set_ifhwaddr(drv, drv->ifname, params->bssid))
4306 goto failed;
4309 if (wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_AP)) {
4310 wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
4311 "into AP mode", drv->ifname);
4312 goto failed;
4315 if (linux_set_iface_flags(drv->ioctl_sock, drv->ifname, 1))
4316 goto failed;
4318 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
4319 if (drv->eapol_sock < 0) {
4320 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
4321 goto failed;
4324 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
4326 printf("Could not register read socket for eapol\n");
4327 goto failed;
4330 if (get_ifhwaddr(drv, drv->ifname, params->own_addr))
4331 goto failed;
4333 return drv;
4335 failed:
4336 nl80211_remove_monitor_interface(drv);
4337 if (drv->ioctl_sock >= 0)
4338 close(drv->ioctl_sock);
4340 genl_family_put(drv->nl80211);
4341 nl_cache_free(drv->nl_cache);
4342 nl_handle_destroy(drv->nl_handle);
4343 nl_cb_put(drv->nl_cb);
4345 os_free(drv);
4346 return NULL;
4350 static void i802_deinit(void *priv)
4352 wpa_driver_nl80211_deinit(priv);
4355 #endif /* HOSTAPD */
4358 static enum nl80211_iftype wpa_driver_nl80211_if_type(
4359 enum wpa_driver_if_type type)
4361 switch (type) {
4362 case WPA_IF_STATION:
4363 return NL80211_IFTYPE_STATION;
4364 case WPA_IF_AP_VLAN:
4365 return NL80211_IFTYPE_AP_VLAN;
4366 case WPA_IF_AP_BSS:
4367 return NL80211_IFTYPE_AP;
4369 return -1;
4373 static int wpa_driver_nl80211_if_add(const char *iface, void *priv,
4374 enum wpa_driver_if_type type,
4375 const char *ifname, const u8 *addr,
4376 void *bss_ctx)
4378 struct wpa_driver_nl80211_data *drv = priv;
4379 int ifidx;
4380 #ifdef HOSTAPD
4381 struct i802_bss *bss = NULL;
4383 if (type == WPA_IF_AP_BSS) {
4384 bss = os_zalloc(sizeof(*bss));
4385 if (bss == NULL)
4386 return -1;
4388 #endif /* HOSTAPD */
4390 ifidx = nl80211_create_iface(drv, ifname,
4391 wpa_driver_nl80211_if_type(type), addr,
4393 if (ifidx < 0) {
4394 #ifdef HOSTAPD
4395 os_free(bss);
4396 #endif /* HOSTAPD */
4397 return -1;
4400 #ifdef HOSTAPD
4401 if (type == WPA_IF_AP_BSS) {
4402 if (linux_set_iface_flags(drv->ioctl_sock, ifname, 1)) {
4403 nl80211_remove_iface(drv, ifidx);
4404 os_free(bss);
4405 return -1;
4407 bss->ifindex = ifidx;
4408 bss->next = drv->bss.next;
4409 drv->bss.next = bss;
4411 #endif /* HOSTAPD */
4413 return 0;
4417 static int wpa_driver_nl80211_if_remove(void *priv,
4418 enum wpa_driver_if_type type,
4419 const char *ifname)
4421 struct wpa_driver_nl80211_data *drv = priv;
4422 int ifindex = if_nametoindex(ifname);
4424 nl80211_remove_iface(drv, ifindex);
4426 #ifdef HOSTAPD
4427 if (type == WPA_IF_AP_BSS) {
4428 struct i802_bss *bss, *prev;
4429 prev = &drv->bss;
4430 bss = drv->bss.next;
4431 while (bss) {
4432 if (ifindex == bss->ifindex) {
4433 prev->next = bss->next;
4434 os_free(bss);
4435 break;
4437 prev = bss;
4438 bss = bss->next;
4441 #endif /* HOSTAPD */
4443 return 0;
4447 static int cookie_handler(struct nl_msg *msg, void *arg)
4449 struct nlattr *tb[NL80211_ATTR_MAX + 1];
4450 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4451 u64 *cookie = arg;
4452 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4453 genlmsg_attrlen(gnlh, 0), NULL);
4454 if (tb[NL80211_ATTR_COOKIE])
4455 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
4456 return NL_SKIP;
4460 static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
4461 unsigned int duration)
4463 struct wpa_driver_nl80211_data *drv = priv;
4464 struct nl_msg *msg;
4465 int ret;
4466 u64 cookie;
4468 msg = nlmsg_alloc();
4469 if (!msg)
4470 return -1;
4472 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
4473 NL80211_CMD_REMAIN_ON_CHANNEL, 0);
4475 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4476 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
4477 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
4479 cookie = 0;
4480 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
4481 if (ret == 0) {
4482 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
4483 "0x%llx for freq=%u MHz duration=%u",
4484 (long long unsigned int) cookie, freq, duration);
4485 drv->remain_on_chan_cookie = cookie;
4486 return 0;
4488 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
4489 "(freq=%d): %d (%s)", freq, ret, strerror(-ret));
4490 nla_put_failure:
4491 return -1;
4495 static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
4497 struct wpa_driver_nl80211_data *drv = priv;
4498 struct nl_msg *msg;
4499 int ret;
4501 if (!drv->pending_remain_on_chan) {
4502 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
4503 "to cancel");
4504 return -1;
4507 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
4508 "0x%llx",
4509 (long long unsigned int) drv->remain_on_chan_cookie);
4511 msg = nlmsg_alloc();
4512 if (!msg)
4513 return -1;
4515 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
4516 NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL, 0);
4518 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4519 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
4521 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4522 if (ret == 0)
4523 return 0;
4524 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
4525 "%d (%s)", ret, strerror(-ret));
4526 nla_put_failure:
4527 return -1;
4531 static void wpa_driver_nl80211_probe_req_report_timeout(void *eloop_ctx,
4532 void *timeout_ctx)
4534 struct wpa_driver_nl80211_data *drv = eloop_ctx;
4535 if (drv->monitor_ifidx < 0)
4536 return; /* monitor interface already removed */
4538 if (drv->nlmode != NL80211_IFTYPE_STATION)
4539 return; /* not in station mode anymore */
4541 if (drv->probe_req_report)
4542 return; /* reporting enabled */
4544 wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface due to no "
4545 "Probe Request reporting needed anymore");
4546 nl80211_remove_monitor_interface(drv);
4550 static int wpa_driver_nl80211_probe_req_report(void *priv, int report)
4552 struct wpa_driver_nl80211_data *drv = priv;
4554 if (drv->nlmode != NL80211_IFTYPE_STATION) {
4555 wpa_printf(MSG_DEBUG, "nl80211: probe_req_report control only "
4556 "allowed in station mode (iftype=%d)",
4557 drv->nlmode);
4558 return -1;
4560 drv->probe_req_report = report;
4562 if (report) {
4563 eloop_cancel_timeout(
4564 wpa_driver_nl80211_probe_req_report_timeout,
4565 drv, NULL);
4566 if (drv->monitor_ifidx < 0 &&
4567 nl80211_create_monitor_interface(drv))
4568 return -1;
4569 } else {
4571 * It takes a while to remove the monitor interface, so try to
4572 * avoid doing this if it is needed again shortly. Instead,
4573 * schedule the interface to be removed later if no need for it
4574 * is seen.
4576 wpa_printf(MSG_DEBUG, "nl80211: Scheduling monitor interface "
4577 "to be removed after 10 seconds of no use");
4578 eloop_register_timeout(
4579 10, 0, wpa_driver_nl80211_probe_req_report_timeout,
4580 drv, NULL);
4583 return 0;
4587 static int wpa_driver_nl80211_alloc_interface_addr(void *priv, u8 *addr)
4589 struct wpa_driver_nl80211_data *drv = priv;
4591 if (get_ifhwaddr(drv, drv->ifname, addr) < 0)
4592 return -1;
4594 if (addr[0] & 0x02) {
4595 /* TODO: add support for generating multiple addresses */
4596 addr[0] ^= 0x80;
4597 } else
4598 addr[0] = 0x02; /* locally administered */
4600 return 0;
4604 static void wpa_driver_nl80211_release_interface_addr(void *priv,
4605 const u8 *addr)
4607 /* TODO: keep list of allocated address and release them here */
4611 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
4612 .name = "nl80211",
4613 .desc = "Linux nl80211/cfg80211",
4614 .get_bssid = wpa_driver_nl80211_get_bssid,
4615 .get_ssid = wpa_driver_nl80211_get_ssid,
4616 .set_key = wpa_driver_nl80211_set_key,
4617 .scan2 = wpa_driver_nl80211_scan,
4618 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
4619 .deauthenticate = wpa_driver_nl80211_deauthenticate,
4620 .disassociate = wpa_driver_nl80211_disassociate,
4621 .authenticate = wpa_driver_nl80211_authenticate,
4622 .associate = wpa_driver_nl80211_associate,
4623 .init = wpa_driver_nl80211_init,
4624 .deinit = wpa_driver_nl80211_deinit,
4625 .get_capa = wpa_driver_nl80211_get_capa,
4626 .set_operstate = wpa_driver_nl80211_set_operstate,
4627 .set_supp_port = wpa_driver_nl80211_set_supp_port,
4628 .set_country = wpa_driver_nl80211_set_country,
4629 .set_beacon = wpa_driver_nl80211_set_beacon,
4630 .if_add = wpa_driver_nl80211_if_add,
4631 .if_remove = wpa_driver_nl80211_if_remove,
4632 .send_mlme = wpa_driver_nl80211_send_mlme,
4633 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
4634 .sta_add = wpa_driver_nl80211_sta_add,
4635 .sta_remove = wpa_driver_nl80211_sta_remove,
4636 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
4637 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
4638 #ifdef HOSTAPD
4639 .hapd_init = i802_init,
4640 .hapd_deinit = i802_deinit,
4641 .get_seqnum = i802_get_seqnum,
4642 .flush = i802_flush,
4643 .read_sta_data = i802_read_sta_data,
4644 .sta_deauth = i802_sta_deauth,
4645 .sta_disassoc = i802_sta_disassoc,
4646 .get_inact_sec = i802_get_inact_sec,
4647 .sta_clear_stats = i802_sta_clear_stats,
4648 .set_freq = i802_set_freq,
4649 .set_rts = i802_set_rts,
4650 .set_frag = i802_set_frag,
4651 .set_rate_sets = i802_set_rate_sets,
4652 .set_cts_protect = i802_set_cts_protect,
4653 .set_preamble = i802_set_preamble,
4654 .set_short_slot_time = i802_set_short_slot_time,
4655 .set_tx_queue_params = i802_set_tx_queue_params,
4656 .set_sta_vlan = i802_set_sta_vlan,
4657 .set_wds_sta = i802_set_wds_sta,
4658 #endif /* HOSTAPD */
4659 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
4660 .cancel_remain_on_channel =
4661 wpa_driver_nl80211_cancel_remain_on_channel,
4662 .probe_req_report = wpa_driver_nl80211_probe_req_report,
4663 .alloc_interface_addr = wpa_driver_nl80211_alloc_interface_addr,
4664 .release_interface_addr = wpa_driver_nl80211_release_interface_addr,