nl80211: Fix a typo
[hostap-gosc2009.git] / src / drivers / driver_nl80211.c
blob5e768c43f07508d83fa69c7a830f0a970d5bfa5c
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.h>
22 #include <netlink/genl/genl.h>
23 #include <netlink/genl/family.h>
24 #include <netlink/genl/ctrl.h>
25 #include <netpacket/packet.h>
26 #include <linux/filter.h>
27 #include "nl80211_copy.h"
29 #include "common.h"
30 #include "eloop.h"
31 #include "common/ieee802_11_defs.h"
32 #include "netlink.h"
33 #include "linux_ioctl.h"
34 #include "radiotap.h"
35 #include "radiotap_iter.h"
36 #include "driver.h"
38 #ifdef CONFIG_LIBNL20
39 /* libnl 2.0 compatibility code */
40 #define nl_handle nl_sock
41 #define nl_handle_alloc_cb nl_socket_alloc_cb
42 #define nl_handle_destroy nl_socket_free
43 #endif /* CONFIG_LIBNL20 */
46 #ifndef IFF_LOWER_UP
47 #define IFF_LOWER_UP 0x10000 /* driver signals L1 up */
48 #endif
49 #ifndef IFF_DORMANT
50 #define IFF_DORMANT 0x20000 /* driver signals dormant */
51 #endif
53 #ifndef IF_OPER_DORMANT
54 #define IF_OPER_DORMANT 5
55 #endif
56 #ifndef IF_OPER_UP
57 #define IF_OPER_UP 6
58 #endif
60 struct i802_bss {
61 struct i802_bss *next;
62 int ifindex;
63 unsigned int beacon_set:1;
66 struct wpa_driver_nl80211_data {
67 void *ctx;
68 struct netlink_data *netlink;
69 int ioctl_sock; /* socket for ioctl() use */
70 char ifname[IFNAMSIZ + 1];
71 int ifindex;
72 int if_removed;
73 struct wpa_driver_capa capa;
74 int has_capability;
76 int operstate;
78 int scan_complete_events;
80 struct nl_handle *nl_handle;
81 struct nl_handle *nl_handle_event;
82 struct nl_cache *nl_cache;
83 struct nl_cache *nl_cache_event;
84 struct nl_cb *nl_cb;
85 struct genl_family *nl80211;
87 u8 auth_bssid[ETH_ALEN];
88 u8 bssid[ETH_ALEN];
89 int associated;
90 u8 ssid[32];
91 size_t ssid_len;
92 int nlmode;
93 int ap_scan_as_station;
94 unsigned int assoc_freq;
96 int monitor_sock;
97 int monitor_ifidx;
98 int probe_req_report;
100 unsigned int beacon_set:1;
101 unsigned int pending_remain_on_chan:1;
103 u64 remain_on_chan_cookie;
105 #ifdef HOSTAPD
106 int eapol_sock; /* socket for EAPOL frames */
108 int default_if_indices[16];
109 int *if_indices;
110 int num_if_indices;
112 struct i802_bss bss;
114 int last_freq;
115 int last_freq_ht;
116 #endif /* HOSTAPD */
120 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
121 void *timeout_ctx);
122 static int wpa_driver_nl80211_set_mode(void *priv, int mode);
123 static int
124 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
125 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
126 const u8 *addr, int cmd, u16 reason_code);
127 static void nl80211_remove_monitor_interface(
128 struct wpa_driver_nl80211_data *drv);
130 #ifdef HOSTAPD
131 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
132 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
133 static struct i802_bss * get_bss(struct wpa_driver_nl80211_data *drv,
134 int ifindex);
135 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq);
136 static int wpa_driver_nl80211_if_remove(void *priv,
137 enum wpa_driver_if_type type,
138 const char *ifname);
139 #endif /* HOSTAPD */
141 static void wpa_driver_nl80211_probe_req_report_timeout(void *eloop_ctx,
142 void *timeout_ctx);
145 /* nl80211 code */
146 static int ack_handler(struct nl_msg *msg, void *arg)
148 int *err = arg;
149 *err = 0;
150 return NL_STOP;
153 static int finish_handler(struct nl_msg *msg, void *arg)
155 int *ret = arg;
156 *ret = 0;
157 return NL_SKIP;
160 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
161 void *arg)
163 int *ret = arg;
164 *ret = err->error;
165 return NL_SKIP;
169 static int no_seq_check(struct nl_msg *msg, void *arg)
171 return NL_OK;
175 static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
176 struct nl_msg *msg,
177 int (*valid_handler)(struct nl_msg *, void *),
178 void *valid_data)
180 struct nl_cb *cb;
181 int err = -ENOMEM;
183 cb = nl_cb_clone(drv->nl_cb);
184 if (!cb)
185 goto out;
187 err = nl_send_auto_complete(drv->nl_handle, msg);
188 if (err < 0)
189 goto out;
191 err = 1;
193 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
194 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
195 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
197 if (valid_handler)
198 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM,
199 valid_handler, valid_data);
201 while (err > 0)
202 nl_recvmsgs(drv->nl_handle, cb);
203 out:
204 nl_cb_put(cb);
205 nlmsg_free(msg);
206 return err;
210 struct family_data {
211 const char *group;
212 int id;
216 static int family_handler(struct nl_msg *msg, void *arg)
218 struct family_data *res = arg;
219 struct nlattr *tb[CTRL_ATTR_MAX + 1];
220 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
221 struct nlattr *mcgrp;
222 int i;
224 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
225 genlmsg_attrlen(gnlh, 0), NULL);
226 if (!tb[CTRL_ATTR_MCAST_GROUPS])
227 return NL_SKIP;
229 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], i) {
230 struct nlattr *tb2[CTRL_ATTR_MCAST_GRP_MAX + 1];
231 nla_parse(tb2, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp),
232 nla_len(mcgrp), NULL);
233 if (!tb2[CTRL_ATTR_MCAST_GRP_NAME] ||
234 !tb2[CTRL_ATTR_MCAST_GRP_ID] ||
235 os_strncmp(nla_data(tb2[CTRL_ATTR_MCAST_GRP_NAME]),
236 res->group,
237 nla_len(tb2[CTRL_ATTR_MCAST_GRP_NAME])) != 0)
238 continue;
239 res->id = nla_get_u32(tb2[CTRL_ATTR_MCAST_GRP_ID]);
240 break;
243 return NL_SKIP;
247 static int nl_get_multicast_id(struct wpa_driver_nl80211_data *drv,
248 const char *family, const char *group)
250 struct nl_msg *msg;
251 int ret = -1;
252 struct family_data res = { group, -ENOENT };
254 msg = nlmsg_alloc();
255 if (!msg)
256 return -ENOMEM;
257 genlmsg_put(msg, 0, 0, genl_ctrl_resolve(drv->nl_handle, "nlctrl"),
258 0, 0, CTRL_CMD_GETFAMILY, 0);
259 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
261 ret = send_and_recv_msgs(drv, msg, family_handler, &res);
262 msg = NULL;
263 if (ret == 0)
264 ret = res.id;
266 nla_put_failure:
267 nlmsg_free(msg);
268 return ret;
272 static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
274 struct wpa_driver_nl80211_data *drv = priv;
275 if (!drv->associated)
276 return -1;
277 os_memcpy(bssid, drv->bssid, ETH_ALEN);
278 return 0;
282 static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
284 struct wpa_driver_nl80211_data *drv = priv;
285 if (!drv->associated)
286 return -1;
287 os_memcpy(ssid, drv->ssid, drv->ssid_len);
288 return drv->ssid_len;
292 static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
293 char *buf, size_t len, int del)
295 union wpa_event_data event;
297 os_memset(&event, 0, sizeof(event));
298 if (len > sizeof(event.interface_status.ifname))
299 len = sizeof(event.interface_status.ifname) - 1;
300 os_memcpy(event.interface_status.ifname, buf, len);
301 event.interface_status.ievent = del ? EVENT_INTERFACE_REMOVED :
302 EVENT_INTERFACE_ADDED;
304 wpa_printf(MSG_DEBUG, "RTM_%sLINK, IFLA_IFNAME: Interface '%s' %s",
305 del ? "DEL" : "NEW",
306 event.interface_status.ifname,
307 del ? "removed" : "added");
309 if (os_strcmp(drv->ifname, event.interface_status.ifname) == 0) {
310 if (del)
311 drv->if_removed = 1;
312 else
313 drv->if_removed = 0;
316 wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
320 static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
321 u8 *buf, size_t len)
323 int attrlen, rta_len;
324 struct rtattr *attr;
326 attrlen = len;
327 attr = (struct rtattr *) buf;
329 rta_len = RTA_ALIGN(sizeof(struct rtattr));
330 while (RTA_OK(attr, attrlen)) {
331 if (attr->rta_type == IFLA_IFNAME) {
332 if (os_strcmp(((char *) attr) + rta_len, drv->ifname)
333 == 0)
334 return 1;
335 else
336 break;
338 attr = RTA_NEXT(attr, attrlen);
341 return 0;
345 static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
346 int ifindex, u8 *buf, size_t len)
348 if (drv->ifindex == ifindex)
349 return 1;
351 if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
352 drv->ifindex = if_nametoindex(drv->ifname);
353 wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
354 "interface");
355 wpa_driver_nl80211_finish_drv_init(drv);
356 return 1;
359 return 0;
363 static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
364 struct ifinfomsg *ifi,
365 u8 *buf, size_t len)
367 struct wpa_driver_nl80211_data *drv = ctx;
368 int attrlen, rta_len;
369 struct rtattr *attr;
371 if (!wpa_driver_nl80211_own_ifindex(drv, ifi->ifi_index, buf, len)) {
372 wpa_printf(MSG_DEBUG, "Ignore event for foreign ifindex %d",
373 ifi->ifi_index);
374 return;
377 wpa_printf(MSG_DEBUG, "RTM_NEWLINK: operstate=%d ifi_flags=0x%x "
378 "(%s%s%s%s)",
379 drv->operstate, ifi->ifi_flags,
380 (ifi->ifi_flags & IFF_UP) ? "[UP]" : "",
381 (ifi->ifi_flags & IFF_RUNNING) ? "[RUNNING]" : "",
382 (ifi->ifi_flags & IFF_LOWER_UP) ? "[LOWER_UP]" : "",
383 (ifi->ifi_flags & IFF_DORMANT) ? "[DORMANT]" : "");
385 * Some drivers send the association event before the operup event--in
386 * this case, lifting operstate in wpa_driver_nl80211_set_operstate()
387 * fails. This will hit us when wpa_supplicant does not need to do
388 * IEEE 802.1X authentication
390 if (drv->operstate == 1 &&
391 (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
392 !(ifi->ifi_flags & IFF_RUNNING))
393 netlink_send_oper_ifla(drv->netlink, drv->ifindex,
394 -1, IF_OPER_UP);
396 attrlen = len;
397 attr = (struct rtattr *) buf;
398 rta_len = RTA_ALIGN(sizeof(struct rtattr));
399 while (RTA_OK(attr, attrlen)) {
400 if (attr->rta_type == IFLA_IFNAME) {
401 wpa_driver_nl80211_event_link(
402 drv,
403 ((char *) attr) + rta_len,
404 attr->rta_len - rta_len, 0);
406 attr = RTA_NEXT(attr, attrlen);
411 static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
412 struct ifinfomsg *ifi,
413 u8 *buf, size_t len)
415 struct wpa_driver_nl80211_data *drv = ctx;
416 int attrlen, rta_len;
417 struct rtattr *attr;
419 attrlen = len;
420 attr = (struct rtattr *) buf;
422 rta_len = RTA_ALIGN(sizeof(struct rtattr));
423 while (RTA_OK(attr, attrlen)) {
424 if (attr->rta_type == IFLA_IFNAME) {
425 wpa_driver_nl80211_event_link(
426 drv,
427 ((char *) attr) + rta_len,
428 attr->rta_len - rta_len, 1);
430 attr = RTA_NEXT(attr, attrlen);
435 static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
436 const u8 *frame, size_t len)
438 const struct ieee80211_mgmt *mgmt;
439 union wpa_event_data event;
441 mgmt = (const struct ieee80211_mgmt *) frame;
442 if (len < 24 + sizeof(mgmt->u.auth)) {
443 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
444 "frame");
445 return;
448 os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
449 os_memset(&event, 0, sizeof(event));
450 os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
451 event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
452 event.auth.status_code = le_to_host16(mgmt->u.auth.status_code);
453 if (len > 24 + sizeof(mgmt->u.auth)) {
454 event.auth.ies = mgmt->u.auth.variable;
455 event.auth.ies_len = len - 24 - sizeof(mgmt->u.auth);
458 wpa_supplicant_event(drv->ctx, EVENT_AUTH, &event);
462 static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
463 const u8 *frame, size_t len)
465 const struct ieee80211_mgmt *mgmt;
466 union wpa_event_data event;
467 u16 status;
469 mgmt = (const struct ieee80211_mgmt *) frame;
470 if (len < 24 + sizeof(mgmt->u.assoc_resp)) {
471 wpa_printf(MSG_DEBUG, "nl80211: Too short association event "
472 "frame");
473 return;
476 status = le_to_host16(mgmt->u.assoc_resp.status_code);
477 if (status != WLAN_STATUS_SUCCESS) {
478 os_memset(&event, 0, sizeof(event));
479 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
480 event.assoc_reject.resp_ies =
481 (u8 *) mgmt->u.assoc_resp.variable;
482 event.assoc_reject.resp_ies_len =
483 len - 24 - sizeof(mgmt->u.assoc_resp);
485 event.assoc_reject.status_code = status;
487 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
488 return;
491 drv->associated = 1;
492 os_memcpy(drv->bssid, mgmt->sa, ETH_ALEN);
494 os_memset(&event, 0, sizeof(event));
495 if (len > 24 + sizeof(mgmt->u.assoc_resp)) {
496 event.assoc_info.resp_ies = (u8 *) mgmt->u.assoc_resp.variable;
497 event.assoc_info.resp_ies_len =
498 len - 24 - sizeof(mgmt->u.assoc_resp);
501 event.assoc_info.freq = drv->assoc_freq;
503 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
507 static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
508 enum nl80211_commands cmd, struct nlattr *status,
509 struct nlattr *addr, struct nlattr *req_ie,
510 struct nlattr *resp_ie)
512 union wpa_event_data event;
514 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
516 * Avoid reporting two association events that would confuse
517 * the core code.
519 wpa_printf(MSG_DEBUG, "nl80211: Ignore connect event (cmd=%d) "
520 "when using userspace SME", cmd);
521 return;
524 os_memset(&event, 0, sizeof(event));
525 if (cmd == NL80211_CMD_CONNECT &&
526 nla_get_u16(status) != WLAN_STATUS_SUCCESS) {
527 if (resp_ie) {
528 event.assoc_reject.resp_ies = nla_data(resp_ie);
529 event.assoc_reject.resp_ies_len = nla_len(resp_ie);
531 event.assoc_reject.status_code = nla_get_u16(status);
532 wpa_supplicant_event(drv->ctx, EVENT_ASSOC_REJECT, &event);
533 return;
536 drv->associated = 1;
537 if (addr)
538 os_memcpy(drv->bssid, nla_data(addr), ETH_ALEN);
540 if (req_ie) {
541 event.assoc_info.req_ies = nla_data(req_ie);
542 event.assoc_info.req_ies_len = nla_len(req_ie);
544 if (resp_ie) {
545 event.assoc_info.resp_ies = nla_data(resp_ie);
546 event.assoc_info.resp_ies_len = nla_len(resp_ie);
549 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
553 static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
554 enum nl80211_commands cmd, struct nlattr *addr)
556 union wpa_event_data event;
557 enum wpa_event_type ev;
559 if (nla_len(addr) != ETH_ALEN)
560 return;
562 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d; timeout with " MACSTR,
563 cmd, MAC2STR((u8 *) nla_data(addr)));
565 if (cmd == NL80211_CMD_AUTHENTICATE)
566 ev = EVENT_AUTH_TIMED_OUT;
567 else if (cmd == NL80211_CMD_ASSOCIATE)
568 ev = EVENT_ASSOC_TIMED_OUT;
569 else
570 return;
572 os_memset(&event, 0, sizeof(event));
573 os_memcpy(event.timeout_event.addr, nla_data(addr), ETH_ALEN);
574 wpa_supplicant_event(drv->ctx, ev, &event);
578 static void mlme_event(struct wpa_driver_nl80211_data *drv,
579 enum nl80211_commands cmd, struct nlattr *frame,
580 struct nlattr *addr, struct nlattr *timed_out)
582 if (timed_out && addr) {
583 mlme_timeout_event(drv, cmd, addr);
584 return;
587 if (frame == NULL) {
588 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d without frame "
589 "data", cmd);
590 return;
593 wpa_printf(MSG_DEBUG, "nl80211: MLME event %d", cmd);
594 wpa_hexdump(MSG_MSGDUMP, "nl80211: MLME event frame",
595 nla_data(frame), nla_len(frame));
597 switch (cmd) {
598 case NL80211_CMD_AUTHENTICATE:
599 mlme_event_auth(drv, nla_data(frame), nla_len(frame));
600 break;
601 case NL80211_CMD_ASSOCIATE:
602 mlme_event_assoc(drv, nla_data(frame), nla_len(frame));
603 break;
604 case NL80211_CMD_DEAUTHENTICATE:
605 drv->associated = 0;
606 wpa_supplicant_event(drv->ctx, EVENT_DEAUTH, NULL);
607 break;
608 case NL80211_CMD_DISASSOCIATE:
609 drv->associated = 0;
610 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
611 break;
612 default:
613 break;
618 static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv,
619 struct nlattr *tb[])
621 union wpa_event_data data;
623 wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
624 os_memset(&data, 0, sizeof(data));
625 if (tb[NL80211_ATTR_MAC]) {
626 wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
627 nla_data(tb[NL80211_ATTR_MAC]),
628 nla_len(tb[NL80211_ATTR_MAC]));
629 data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
631 if (tb[NL80211_ATTR_KEY_SEQ]) {
632 wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
633 nla_data(tb[NL80211_ATTR_KEY_SEQ]),
634 nla_len(tb[NL80211_ATTR_KEY_SEQ]));
636 if (tb[NL80211_ATTR_KEY_TYPE]) {
637 enum nl80211_key_type key_type =
638 nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
639 wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
640 if (key_type == NL80211_KEYTYPE_PAIRWISE)
641 data.michael_mic_failure.unicast = 1;
642 } else
643 data.michael_mic_failure.unicast = 1;
645 if (tb[NL80211_ATTR_KEY_IDX]) {
646 u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
647 wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
650 wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
654 static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
655 struct nlattr *tb[])
657 if (tb[NL80211_ATTR_MAC] == NULL) {
658 wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
659 "event");
660 return;
662 os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
663 drv->associated = 1;
664 wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
665 MAC2STR(drv->bssid));
667 wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
671 static void mlme_event_remain_on_channel(struct wpa_driver_nl80211_data *drv,
672 int cancel_event, struct nlattr *tb[])
674 unsigned int freq, chan_type, duration;
675 union wpa_event_data data;
676 u64 cookie;
678 if (tb[NL80211_ATTR_WIPHY_FREQ])
679 freq = nla_get_u32(tb[NL80211_ATTR_WIPHY_FREQ]);
680 else
681 freq = 0;
683 if (tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
684 chan_type = nla_get_u32(tb[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
685 else
686 chan_type = 0;
688 if (tb[NL80211_ATTR_DURATION])
689 duration = nla_get_u32(tb[NL80211_ATTR_DURATION]);
690 else
691 duration = 0;
693 if (tb[NL80211_ATTR_COOKIE])
694 cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
695 else
696 cookie = 0;
698 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel event (cancel=%d "
699 "freq=%u channel_type=%u duration=%u cookie=0x%llx (%s))",
700 cancel_event, freq, chan_type, duration,
701 (long long unsigned int) cookie,
702 cookie == drv->remain_on_chan_cookie ? "match" : "unknown");
704 if (cookie != drv->remain_on_chan_cookie)
705 return; /* not for us */
707 drv->pending_remain_on_chan = !cancel_event;
709 os_memset(&data, 0, sizeof(data));
710 data.remain_on_channel.freq = freq;
711 data.remain_on_channel.duration = duration;
712 wpa_supplicant_event(drv->ctx, cancel_event ?
713 EVENT_CANCEL_REMAIN_ON_CHANNEL :
714 EVENT_REMAIN_ON_CHANNEL, &data);
718 static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
719 struct nlattr *tb[])
721 union wpa_event_data event;
722 struct nlattr *nl;
723 int rem;
724 struct scan_info *info;
725 #define MAX_REPORT_FREQS 50
726 int freqs[MAX_REPORT_FREQS];
727 int num_freqs = 0;
729 os_memset(&event, 0, sizeof(event));
730 info = &event.scan_info;
731 info->aborted = aborted;
733 if (tb[NL80211_ATTR_SCAN_SSIDS]) {
734 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
735 struct wpa_driver_scan_ssid *s =
736 &info->ssids[info->num_ssids];
737 s->ssid = nla_data(nl);
738 s->ssid_len = nla_len(nl);
739 info->num_ssids++;
740 if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
741 break;
744 if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
745 nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
747 freqs[num_freqs] = nla_get_u32(nl);
748 num_freqs++;
749 if (num_freqs == MAX_REPORT_FREQS - 1)
750 break;
752 info->freqs = freqs;
753 info->num_freqs = num_freqs;
755 wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
759 static int process_event(struct nl_msg *msg, void *arg)
761 struct wpa_driver_nl80211_data *drv = arg;
762 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
763 struct nlattr *tb[NL80211_ATTR_MAX + 1];
765 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
766 genlmsg_attrlen(gnlh, 0), NULL);
768 if (tb[NL80211_ATTR_IFINDEX]) {
769 int ifindex = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
770 if (ifindex != drv->ifindex) {
771 wpa_printf(MSG_DEBUG, "nl80211: Ignored event (cmd=%d)"
772 " for foreign interface (ifindex %d)",
773 gnlh->cmd, ifindex);
774 return NL_SKIP;
778 if (drv->ap_scan_as_station &&
779 (gnlh->cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
780 gnlh->cmd == NL80211_CMD_SCAN_ABORTED)) {
781 wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_AP);
782 drv->ap_scan_as_station = 0;
785 switch (gnlh->cmd) {
786 case NL80211_CMD_TRIGGER_SCAN:
787 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger");
788 break;
789 case NL80211_CMD_NEW_SCAN_RESULTS:
790 wpa_printf(MSG_DEBUG, "nl80211: New scan results available");
791 drv->scan_complete_events = 1;
792 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
793 drv->ctx);
794 send_scan_event(drv, 0, tb);
795 break;
796 case NL80211_CMD_SCAN_ABORTED:
797 wpa_printf(MSG_DEBUG, "nl80211: Scan aborted");
799 * Need to indicate that scan results are available in order
800 * not to make wpa_supplicant stop its scanning.
802 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
803 drv->ctx);
804 send_scan_event(drv, 1, tb);
805 break;
806 case NL80211_CMD_AUTHENTICATE:
807 case NL80211_CMD_ASSOCIATE:
808 case NL80211_CMD_DEAUTHENTICATE:
809 case NL80211_CMD_DISASSOCIATE:
810 mlme_event(drv, gnlh->cmd, tb[NL80211_ATTR_FRAME],
811 tb[NL80211_ATTR_MAC], tb[NL80211_ATTR_TIMED_OUT]);
812 break;
813 case NL80211_CMD_CONNECT:
814 case NL80211_CMD_ROAM:
815 mlme_event_connect(drv, gnlh->cmd,
816 tb[NL80211_ATTR_STATUS_CODE],
817 tb[NL80211_ATTR_MAC],
818 tb[NL80211_ATTR_REQ_IE],
819 tb[NL80211_ATTR_RESP_IE]);
820 break;
821 case NL80211_CMD_DISCONNECT:
822 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
824 * Avoid reporting two disassociation events that could
825 * confuse the core code.
827 wpa_printf(MSG_DEBUG, "nl80211: Ignore disconnect "
828 "event when using userspace SME");
829 break;
831 drv->associated = 0;
832 wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
833 break;
834 case NL80211_CMD_MICHAEL_MIC_FAILURE:
835 mlme_event_michael_mic_failure(drv, tb);
836 break;
837 case NL80211_CMD_JOIN_IBSS:
838 mlme_event_join_ibss(drv, tb);
839 break;
840 case NL80211_CMD_REMAIN_ON_CHANNEL:
841 mlme_event_remain_on_channel(drv, 0, tb);
842 break;
843 case NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL:
844 mlme_event_remain_on_channel(drv, 1, tb);
845 break;
846 default:
847 wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
848 "(cmd=%d)", gnlh->cmd);
849 break;
852 return NL_SKIP;
856 static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
857 void *sock_ctx)
859 struct nl_cb *cb;
860 struct wpa_driver_nl80211_data *drv = eloop_ctx;
862 wpa_printf(MSG_DEBUG, "nl80211: Event message available");
864 cb = nl_cb_clone(drv->nl_cb);
865 if (!cb)
866 return;
867 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
868 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, process_event, drv);
869 nl_recvmsgs(drv->nl_handle_event, cb);
870 nl_cb_put(cb);
875 * wpa_driver_nl80211_set_country - ask nl80211 to set the regulatory domain
876 * @priv: driver_nl80211 private data
877 * @alpha2_arg: country to which to switch to
878 * Returns: 0 on success, -1 on failure
880 * This asks nl80211 to set the regulatory domain for given
881 * country ISO / IEC alpha2.
883 static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
885 struct wpa_driver_nl80211_data *drv = priv;
886 char alpha2[3];
887 struct nl_msg *msg;
889 msg = nlmsg_alloc();
890 if (!msg)
891 return -ENOMEM;
893 alpha2[0] = alpha2_arg[0];
894 alpha2[1] = alpha2_arg[1];
895 alpha2[2] = '\0';
897 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
898 0, NL80211_CMD_REQ_SET_REG, 0);
900 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
901 if (send_and_recv_msgs(drv, msg, NULL, NULL))
902 return -EINVAL;
903 return 0;
904 nla_put_failure:
905 return -EINVAL;
909 #ifndef HOSTAPD
910 struct wiphy_info_data {
911 int max_scan_ssids;
912 int ap_supported;
913 int auth_supported;
914 int connect_supported;
918 static int wiphy_info_handler(struct nl_msg *msg, void *arg)
920 struct nlattr *tb[NL80211_ATTR_MAX + 1];
921 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
922 struct wiphy_info_data *info = arg;
924 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
925 genlmsg_attrlen(gnlh, 0), NULL);
927 if (tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS])
928 info->max_scan_ssids =
929 nla_get_u8(tb[NL80211_ATTR_MAX_NUM_SCAN_SSIDS]);
931 if (tb[NL80211_ATTR_SUPPORTED_IFTYPES]) {
932 struct nlattr *nl_mode;
933 int i;
934 nla_for_each_nested(nl_mode,
935 tb[NL80211_ATTR_SUPPORTED_IFTYPES], i) {
936 if (nl_mode->nla_type == NL80211_IFTYPE_AP) {
937 info->ap_supported = 1;
938 break;
943 if (tb[NL80211_ATTR_SUPPORTED_COMMANDS]) {
944 struct nlattr *nl_cmd;
945 int i;
947 nla_for_each_nested(nl_cmd,
948 tb[NL80211_ATTR_SUPPORTED_COMMANDS], i) {
949 u32 cmd = nla_get_u32(nl_cmd);
950 if (cmd == NL80211_CMD_AUTHENTICATE)
951 info->auth_supported = 1;
952 else if (cmd == NL80211_CMD_CONNECT)
953 info->connect_supported = 1;
957 return NL_SKIP;
961 static int wpa_driver_nl80211_get_info(struct wpa_driver_nl80211_data *drv,
962 struct wiphy_info_data *info)
964 struct nl_msg *msg;
966 os_memset(info, 0, sizeof(*info));
967 msg = nlmsg_alloc();
968 if (!msg)
969 return -1;
971 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
972 0, NL80211_CMD_GET_WIPHY, 0);
974 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
976 if (send_and_recv_msgs(drv, msg, wiphy_info_handler, info) == 0)
977 return 0;
978 msg = NULL;
979 nla_put_failure:
980 nlmsg_free(msg);
981 return -1;
985 static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
987 struct wiphy_info_data info;
988 if (wpa_driver_nl80211_get_info(drv, &info))
989 return -1;
990 drv->has_capability = 1;
991 /* For now, assume TKIP, CCMP, WPA, WPA2 are supported */
992 drv->capa.key_mgmt = WPA_DRIVER_CAPA_KEY_MGMT_WPA |
993 WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
994 WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
995 WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK;
996 drv->capa.enc = WPA_DRIVER_CAPA_ENC_WEP40 |
997 WPA_DRIVER_CAPA_ENC_WEP104 |
998 WPA_DRIVER_CAPA_ENC_TKIP |
999 WPA_DRIVER_CAPA_ENC_CCMP;
1000 drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
1001 WPA_DRIVER_AUTH_SHARED |
1002 WPA_DRIVER_AUTH_LEAP;
1004 drv->capa.max_scan_ssids = info.max_scan_ssids;
1005 if (info.ap_supported)
1006 drv->capa.flags |= WPA_DRIVER_FLAGS_AP;
1008 if (info.auth_supported)
1009 drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
1010 else if (!info.connect_supported) {
1011 wpa_printf(MSG_INFO, "nl80211: Driver does not support "
1012 "authentication/association or connect commands");
1013 return -1;
1016 drv->capa.flags |= WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE;
1018 return 0;
1020 #endif /* HOSTAPD */
1023 static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv,
1024 void *ctx)
1026 int ret;
1028 /* Initialize generic netlink and nl80211 */
1030 drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
1031 if (drv->nl_cb == NULL) {
1032 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1033 "callbacks");
1034 goto err1;
1037 drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
1038 if (drv->nl_handle == NULL) {
1039 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1040 "callbacks");
1041 goto err2;
1044 drv->nl_handle_event = nl_handle_alloc_cb(drv->nl_cb);
1045 if (drv->nl_handle_event == NULL) {
1046 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate netlink "
1047 "callbacks (event)");
1048 goto err2b;
1051 if (genl_connect(drv->nl_handle)) {
1052 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
1053 "netlink");
1054 goto err3;
1057 if (genl_connect(drv->nl_handle_event)) {
1058 wpa_printf(MSG_ERROR, "nl80211: Failed to connect to generic "
1059 "netlink (event)");
1060 goto err3;
1063 #ifdef CONFIG_LIBNL20
1064 if (genl_ctrl_alloc_cache(drv->nl_handle, &drv->nl_cache) < 0) {
1065 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
1066 "netlink cache");
1067 goto err3;
1069 if (genl_ctrl_alloc_cache(drv->nl_handle_event, &drv->nl_cache_event) <
1070 0) {
1071 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
1072 "netlink cache (event)");
1073 goto err3b;
1075 #else /* CONFIG_LIBNL20 */
1076 drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
1077 if (drv->nl_cache == NULL) {
1078 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
1079 "netlink cache");
1080 goto err3;
1082 drv->nl_cache_event = genl_ctrl_alloc_cache(drv->nl_handle_event);
1083 if (drv->nl_cache_event == NULL) {
1084 wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
1085 "netlink cache (event)");
1086 goto err3b;
1088 #endif /* CONFIG_LIBNL20 */
1090 drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
1091 if (drv->nl80211 == NULL) {
1092 wpa_printf(MSG_ERROR, "nl80211: 'nl80211' generic netlink not "
1093 "found");
1094 goto err4;
1097 ret = nl_get_multicast_id(drv, "nl80211", "scan");
1098 if (ret >= 0)
1099 ret = nl_socket_add_membership(drv->nl_handle_event, ret);
1100 if (ret < 0) {
1101 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1102 "membership for scan events: %d (%s)",
1103 ret, strerror(-ret));
1104 goto err4;
1107 ret = nl_get_multicast_id(drv, "nl80211", "mlme");
1108 if (ret >= 0)
1109 ret = nl_socket_add_membership(drv->nl_handle_event, ret);
1110 if (ret < 0) {
1111 wpa_printf(MSG_ERROR, "nl80211: Could not add multicast "
1112 "membership for mlme events: %d (%s)",
1113 ret, strerror(-ret));
1114 goto err4;
1117 eloop_register_read_sock(nl_socket_get_fd(drv->nl_handle_event),
1118 wpa_driver_nl80211_event_receive, drv, ctx);
1120 return 0;
1122 err4:
1123 nl_cache_free(drv->nl_cache_event);
1124 err3b:
1125 nl_cache_free(drv->nl_cache);
1126 err3:
1127 nl_handle_destroy(drv->nl_handle_event);
1128 err2b:
1129 nl_handle_destroy(drv->nl_handle);
1130 err2:
1131 nl_cb_put(drv->nl_cb);
1132 err1:
1133 return -1;
1138 * wpa_driver_nl80211_init - Initialize nl80211 driver interface
1139 * @ctx: context to be used when calling wpa_supplicant functions,
1140 * e.g., wpa_supplicant_event()
1141 * @ifname: interface name, e.g., wlan0
1142 * Returns: Pointer to private data, %NULL on failure
1144 static void * wpa_driver_nl80211_init(void *ctx, const char *ifname)
1146 struct wpa_driver_nl80211_data *drv;
1147 struct netlink_config *cfg;
1149 drv = os_zalloc(sizeof(*drv));
1150 if (drv == NULL)
1151 return NULL;
1152 drv->ctx = ctx;
1153 os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
1154 drv->monitor_ifidx = -1;
1155 drv->monitor_sock = -1;
1156 drv->ioctl_sock = -1;
1158 if (wpa_driver_nl80211_init_nl(drv, ctx)) {
1159 os_free(drv);
1160 return NULL;
1163 drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
1164 if (drv->ioctl_sock < 0) {
1165 perror("socket(PF_INET,SOCK_DGRAM)");
1166 goto failed;
1169 cfg = os_zalloc(sizeof(*cfg));
1170 if (cfg == NULL)
1171 goto failed;
1172 cfg->ctx = drv;
1173 cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
1174 cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
1175 drv->netlink = netlink_init(cfg);
1176 if (drv->netlink == NULL) {
1177 os_free(cfg);
1178 goto failed;
1180 if (wpa_driver_nl80211_finish_drv_init(drv))
1181 goto failed;
1183 return drv;
1185 failed:
1186 netlink_deinit(drv->netlink);
1187 if (drv->ioctl_sock >= 0)
1188 close(drv->ioctl_sock);
1190 genl_family_put(drv->nl80211);
1191 nl_cache_free(drv->nl_cache);
1192 nl_handle_destroy(drv->nl_handle);
1193 nl_cb_put(drv->nl_cb);
1195 os_free(drv);
1196 return NULL;
1200 static int
1201 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
1203 drv->ifindex = if_nametoindex(drv->ifname);
1205 #ifndef HOSTAPD
1206 if (wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_INFRA) < 0) {
1207 wpa_printf(MSG_DEBUG, "nl80211: Could not configure driver to "
1208 "use managed mode");
1211 if (linux_set_iface_flags(drv->ioctl_sock, drv->ifname, 1)) {
1212 wpa_printf(MSG_ERROR, "Could not set interface '%s' UP",
1213 drv->ifname);
1214 return -1;
1217 if (wpa_driver_nl80211_capa(drv))
1218 return -1;
1220 netlink_send_oper_ifla(drv->netlink, drv->ifindex,
1221 1, IF_OPER_DORMANT);
1222 #endif /* HOSTAPD */
1224 return 0;
1228 #ifdef HOSTAPD
1229 static void wpa_driver_nl80211_free_bss(struct wpa_driver_nl80211_data *drv)
1231 struct i802_bss *bss, *prev;
1232 bss = drv->bss.next;
1233 while (bss) {
1234 prev = bss;
1235 bss = bss->next;
1236 os_free(bss);
1239 #endif /* HOSTAPD */
1242 static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
1244 struct nl_msg *msg;
1246 msg = nlmsg_alloc();
1247 if (!msg)
1248 return -ENOMEM;
1250 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1251 0, NL80211_CMD_DEL_BEACON, 0);
1252 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1254 return send_and_recv_msgs(drv, msg, NULL, NULL);
1255 nla_put_failure:
1256 return -ENOBUFS;
1261 * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
1262 * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
1264 * Shut down driver interface and processing of driver events. Free
1265 * private data buffer if one was allocated in wpa_driver_nl80211_init().
1267 static void wpa_driver_nl80211_deinit(void *priv)
1269 struct wpa_driver_nl80211_data *drv = priv;
1271 nl80211_remove_monitor_interface(drv);
1273 if (drv->nlmode == NL80211_IFTYPE_AP)
1274 wpa_driver_nl80211_del_beacon(drv);
1276 #ifdef HOSTAPD
1277 if (drv->last_freq_ht) {
1278 /* Clear HT flags from the driver */
1279 struct hostapd_freq_params freq;
1280 os_memset(&freq, 0, sizeof(freq));
1281 freq.freq = drv->last_freq;
1282 i802_set_freq(priv, &freq);
1285 if (drv->eapol_sock >= 0) {
1286 eloop_unregister_read_sock(drv->eapol_sock);
1287 close(drv->eapol_sock);
1290 if (drv->if_indices != drv->default_if_indices)
1291 os_free(drv->if_indices);
1293 wpa_driver_nl80211_free_bss(drv);
1294 #endif /* HOSTAPD */
1296 netlink_send_oper_ifla(drv->netlink, drv->ifindex, 0, IF_OPER_UP);
1297 netlink_deinit(drv->netlink);
1299 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
1301 (void) linux_set_iface_flags(drv->ioctl_sock, drv->ifname, 0);
1302 wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_INFRA);
1304 if (drv->ioctl_sock >= 0)
1305 close(drv->ioctl_sock);
1307 eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_handle_event));
1308 genl_family_put(drv->nl80211);
1309 nl_cache_free(drv->nl_cache);
1310 nl_cache_free(drv->nl_cache_event);
1311 nl_handle_destroy(drv->nl_handle);
1312 nl_handle_destroy(drv->nl_handle_event);
1313 nl_cb_put(drv->nl_cb);
1315 eloop_cancel_timeout(wpa_driver_nl80211_probe_req_report_timeout,
1316 drv, NULL);
1318 os_free(drv);
1323 * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
1324 * @eloop_ctx: Driver private data
1325 * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
1327 * This function can be used as registered timeout when starting a scan to
1328 * generate a scan completed event if the driver does not report this.
1330 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1332 struct wpa_driver_nl80211_data *drv = eloop_ctx;
1333 if (drv->ap_scan_as_station) {
1334 wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_AP);
1335 drv->ap_scan_as_station = 0;
1337 wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
1338 wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
1343 * wpa_driver_nl80211_scan - Request the driver to initiate scan
1344 * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
1345 * @params: Scan parameters
1346 * Returns: 0 on success, -1 on failure
1348 static int wpa_driver_nl80211_scan(void *priv,
1349 struct wpa_driver_scan_params *params)
1351 struct wpa_driver_nl80211_data *drv = priv;
1352 int ret = 0, timeout;
1353 struct nl_msg *msg, *ssids, *freqs;
1354 size_t i;
1356 msg = nlmsg_alloc();
1357 ssids = nlmsg_alloc();
1358 freqs = nlmsg_alloc();
1359 if (!msg || !ssids || !freqs) {
1360 nlmsg_free(msg);
1361 nlmsg_free(ssids);
1362 nlmsg_free(freqs);
1363 return -1;
1366 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
1367 NL80211_CMD_TRIGGER_SCAN, 0);
1369 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1371 for (i = 0; i < params->num_ssids; i++) {
1372 NLA_PUT(ssids, i + 1, params->ssids[i].ssid_len,
1373 params->ssids[i].ssid);
1375 if (params->num_ssids)
1376 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
1378 if (params->extra_ies) {
1379 NLA_PUT(msg, NL80211_ATTR_IE, params->extra_ies_len,
1380 params->extra_ies);
1383 if (params->freqs) {
1384 for (i = 0; params->freqs[i]; i++)
1385 NLA_PUT_U32(freqs, i + 1, params->freqs[i]);
1386 nla_put_nested(msg, NL80211_ATTR_SCAN_FREQUENCIES, freqs);
1389 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1390 msg = NULL;
1391 if (ret) {
1392 wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
1393 "(%s)", ret, strerror(-ret));
1394 #ifdef HOSTAPD
1395 if (drv->nlmode == NL80211_IFTYPE_AP) {
1397 * mac80211 does not allow scan requests in AP mode, so
1398 * try to do this in station mode.
1400 if (wpa_driver_nl80211_set_mode(drv,
1401 IEEE80211_MODE_INFRA))
1402 goto nla_put_failure;
1404 if (wpa_driver_nl80211_scan(drv, params)) {
1405 wpa_driver_nl80211_set_mode(drv,
1406 IEEE80211_MODE_AP);
1407 goto nla_put_failure;
1410 /* Restore AP mode when processing scan results */
1411 drv->ap_scan_as_station = 1;
1412 ret = 0;
1413 } else
1414 goto nla_put_failure;
1415 #else /* HOSTAPD */
1416 goto nla_put_failure;
1417 #endif /* HOSTAPD */
1420 /* Not all drivers generate "scan completed" wireless event, so try to
1421 * read results after a timeout. */
1422 timeout = 10;
1423 if (drv->scan_complete_events) {
1425 * The driver seems to deliver events to notify when scan is
1426 * complete, so use longer timeout to avoid race conditions
1427 * with scanning and following association request.
1429 timeout = 30;
1431 wpa_printf(MSG_DEBUG, "Scan requested (ret=%d) - scan timeout %d "
1432 "seconds", ret, timeout);
1433 eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
1434 eloop_register_timeout(timeout, 0, wpa_driver_nl80211_scan_timeout,
1435 drv, drv->ctx);
1437 nla_put_failure:
1438 nlmsg_free(ssids);
1439 nlmsg_free(msg);
1440 nlmsg_free(freqs);
1441 return ret;
1445 static int bss_info_handler(struct nl_msg *msg, void *arg)
1447 struct nlattr *tb[NL80211_ATTR_MAX + 1];
1448 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
1449 struct nlattr *bss[NL80211_BSS_MAX + 1];
1450 static struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
1451 [NL80211_BSS_BSSID] = { .type = NLA_UNSPEC },
1452 [NL80211_BSS_FREQUENCY] = { .type = NLA_U32 },
1453 [NL80211_BSS_TSF] = { .type = NLA_U64 },
1454 [NL80211_BSS_BEACON_INTERVAL] = { .type = NLA_U16 },
1455 [NL80211_BSS_CAPABILITY] = { .type = NLA_U16 },
1456 [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
1457 [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
1458 [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
1459 [NL80211_BSS_STATUS] = { .type = NLA_U32 },
1460 [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
1462 struct wpa_scan_results *res = arg;
1463 struct wpa_scan_res **tmp;
1464 struct wpa_scan_res *r;
1465 const u8 *ie;
1466 size_t ie_len;
1468 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
1469 genlmsg_attrlen(gnlh, 0), NULL);
1470 if (!tb[NL80211_ATTR_BSS])
1471 return NL_SKIP;
1472 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS],
1473 bss_policy))
1474 return NL_SKIP;
1475 if (bss[NL80211_BSS_INFORMATION_ELEMENTS]) {
1476 ie = nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1477 ie_len = nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]);
1478 } else {
1479 ie = NULL;
1480 ie_len = 0;
1483 r = os_zalloc(sizeof(*r) + ie_len);
1484 if (r == NULL)
1485 return NL_SKIP;
1486 if (bss[NL80211_BSS_BSSID])
1487 os_memcpy(r->bssid, nla_data(bss[NL80211_BSS_BSSID]),
1488 ETH_ALEN);
1489 if (bss[NL80211_BSS_FREQUENCY])
1490 r->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
1491 if (bss[NL80211_BSS_BEACON_INTERVAL])
1492 r->beacon_int = nla_get_u16(bss[NL80211_BSS_BEACON_INTERVAL]);
1493 if (bss[NL80211_BSS_CAPABILITY])
1494 r->caps = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
1495 r->flags |= WPA_SCAN_NOISE_INVALID;
1496 if (bss[NL80211_BSS_SIGNAL_MBM]) {
1497 r->level = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
1498 r->level /= 100; /* mBm to dBm */
1499 r->flags |= WPA_SCAN_LEVEL_DBM | WPA_SCAN_QUAL_INVALID;
1500 } else if (bss[NL80211_BSS_SIGNAL_UNSPEC]) {
1501 r->level = nla_get_u8(bss[NL80211_BSS_SIGNAL_UNSPEC]);
1502 r->flags |= WPA_SCAN_LEVEL_INVALID;
1503 } else
1504 r->flags |= WPA_SCAN_LEVEL_INVALID | WPA_SCAN_QUAL_INVALID;
1505 if (bss[NL80211_BSS_TSF])
1506 r->tsf = nla_get_u64(bss[NL80211_BSS_TSF]);
1507 if (bss[NL80211_BSS_SEEN_MS_AGO])
1508 r->age = nla_get_u32(bss[NL80211_BSS_SEEN_MS_AGO]);
1509 r->ie_len = ie_len;
1510 if (ie)
1511 os_memcpy(r + 1, ie, ie_len);
1513 if (bss[NL80211_BSS_STATUS]) {
1514 enum nl80211_bss_status status;
1515 status = nla_get_u32(bss[NL80211_BSS_STATUS]);
1516 switch (status) {
1517 case NL80211_BSS_STATUS_AUTHENTICATED:
1518 r->flags |= WPA_SCAN_AUTHENTICATED;
1519 break;
1520 case NL80211_BSS_STATUS_ASSOCIATED:
1521 r->flags |= WPA_SCAN_ASSOCIATED;
1522 break;
1523 default:
1524 break;
1528 tmp = os_realloc(res->res,
1529 (res->num + 1) * sizeof(struct wpa_scan_res *));
1530 if (tmp == NULL) {
1531 os_free(r);
1532 return NL_SKIP;
1534 tmp[res->num++] = r;
1535 res->res = tmp;
1537 return NL_SKIP;
1541 static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
1542 const u8 *addr)
1544 if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
1545 wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
1546 "mismatch");
1547 wpa_driver_nl80211_mlme(drv, addr,
1548 NL80211_CMD_DEAUTHENTICATE,
1549 WLAN_REASON_PREV_AUTH_NOT_VALID);
1554 static void wpa_driver_nl80211_check_bss_status(
1555 struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
1557 size_t i;
1559 for (i = 0; i < res->num; i++) {
1560 struct wpa_scan_res *r = res->res[i];
1561 if (r->flags & WPA_SCAN_AUTHENTICATED) {
1562 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
1563 "indicates BSS status with " MACSTR
1564 " as authenticated",
1565 MAC2STR(r->bssid));
1566 if (drv->nlmode == NL80211_IFTYPE_STATION &&
1567 os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
1568 os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
1569 0) {
1570 wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
1571 " in local state (auth=" MACSTR
1572 " assoc=" MACSTR ")",
1573 MAC2STR(drv->auth_bssid),
1574 MAC2STR(drv->bssid));
1578 if (r->flags & WPA_SCAN_ASSOCIATED) {
1579 wpa_printf(MSG_DEBUG, "nl80211: Scan results "
1580 "indicate BSS status with " MACSTR
1581 " as associated",
1582 MAC2STR(r->bssid));
1583 if (drv->nlmode == NL80211_IFTYPE_STATION &&
1584 !drv->associated) {
1585 wpa_printf(MSG_DEBUG, "nl80211: Local state "
1586 "(not associated) does not match "
1587 "with BSS state");
1588 clear_state_mismatch(drv, r->bssid);
1589 } else if (drv->nlmode == NL80211_IFTYPE_STATION &&
1590 os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
1591 0) {
1592 wpa_printf(MSG_DEBUG, "nl80211: Local state "
1593 "(associated with " MACSTR ") does "
1594 "not match with BSS state",
1595 MAC2STR(drv->bssid));
1596 clear_state_mismatch(drv, r->bssid);
1597 clear_state_mismatch(drv, drv->bssid);
1604 static void wpa_scan_results_free(struct wpa_scan_results *res)
1606 size_t i;
1608 if (res == NULL)
1609 return;
1611 for (i = 0; i < res->num; i++)
1612 os_free(res->res[i]);
1613 os_free(res->res);
1614 os_free(res);
1619 * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
1620 * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
1621 * Returns: Scan results on success, -1 on failure
1623 static struct wpa_scan_results *
1624 wpa_driver_nl80211_get_scan_results(void *priv)
1626 struct wpa_driver_nl80211_data *drv = priv;
1627 struct nl_msg *msg;
1628 struct wpa_scan_results *res;
1629 int ret;
1631 res = os_zalloc(sizeof(*res));
1632 if (res == NULL)
1633 return NULL;
1634 msg = nlmsg_alloc();
1635 if (!msg)
1636 goto nla_put_failure;
1638 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, NLM_F_DUMP,
1639 NL80211_CMD_GET_SCAN, 0);
1640 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1642 ret = send_and_recv_msgs(drv, msg, bss_info_handler, res);
1643 msg = NULL;
1644 if (ret == 0) {
1645 wpa_printf(MSG_DEBUG, "Received scan results (%lu BSSes)",
1646 (unsigned long) res->num);
1647 wpa_driver_nl80211_check_bss_status(drv, res);
1648 return res;
1650 wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
1651 "(%s)", ret, strerror(-ret));
1652 nla_put_failure:
1653 nlmsg_free(msg);
1654 wpa_scan_results_free(res);
1655 return NULL;
1659 static int wpa_driver_nl80211_set_key(const char *ifname, void *priv,
1660 enum wpa_alg alg, const u8 *addr,
1661 int key_idx, int set_tx,
1662 const u8 *seq, size_t seq_len,
1663 const u8 *key, size_t key_len)
1665 struct wpa_driver_nl80211_data *drv = priv;
1666 int ifindex = if_nametoindex(ifname);
1667 struct nl_msg *msg;
1668 int ret;
1670 wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
1671 "set_tx=%d seq_len=%lu key_len=%lu",
1672 __func__, ifindex, alg, addr, key_idx, set_tx,
1673 (unsigned long) seq_len, (unsigned long) key_len);
1675 msg = nlmsg_alloc();
1676 if (!msg)
1677 return -ENOMEM;
1679 if (alg == WPA_ALG_NONE) {
1680 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1681 0, NL80211_CMD_DEL_KEY, 0);
1682 } else {
1683 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1684 0, NL80211_CMD_NEW_KEY, 0);
1685 NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
1686 switch (alg) {
1687 case WPA_ALG_WEP:
1688 if (key_len == 5)
1689 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
1690 WLAN_CIPHER_SUITE_WEP40);
1691 else
1692 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
1693 WLAN_CIPHER_SUITE_WEP104);
1694 break;
1695 case WPA_ALG_TKIP:
1696 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
1697 WLAN_CIPHER_SUITE_TKIP);
1698 break;
1699 case WPA_ALG_CCMP:
1700 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
1701 WLAN_CIPHER_SUITE_CCMP);
1702 break;
1703 case WPA_ALG_IGTK:
1704 NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
1705 WLAN_CIPHER_SUITE_AES_CMAC);
1706 break;
1707 default:
1708 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
1709 "algorithm %d", __func__, alg);
1710 nlmsg_free(msg);
1711 return -1;
1715 if (seq && seq_len)
1716 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
1718 if (addr && os_memcmp(addr, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
1720 wpa_printf(MSG_DEBUG, " addr=" MACSTR, MAC2STR(addr));
1721 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1723 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1724 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
1726 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1727 if (ret == -ENOENT && alg == WPA_ALG_NONE)
1728 ret = 0;
1729 if (ret)
1730 wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
1731 ret, strerror(-ret));
1734 * If we failed or don't need to set the default TX key (below),
1735 * we're done here.
1737 if (ret || !set_tx || alg == WPA_ALG_NONE)
1738 return ret;
1739 #ifdef HOSTAPD /* FIX: is this needed? */
1740 if (addr)
1741 return ret;
1742 #endif /* HOSTAPD */
1744 msg = nlmsg_alloc();
1745 if (!msg)
1746 return -ENOMEM;
1748 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
1749 0, NL80211_CMD_SET_KEY, 0);
1750 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1751 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
1752 if (alg == WPA_ALG_IGTK)
1753 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
1754 else
1755 NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
1757 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1758 if (ret == -ENOENT)
1759 ret = 0;
1760 if (ret)
1761 wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
1762 "err=%d %s)", ret, strerror(-ret));
1763 return ret;
1765 nla_put_failure:
1766 return -ENOBUFS;
1770 static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
1771 int key_idx, int defkey,
1772 const u8 *seq, size_t seq_len,
1773 const u8 *key, size_t key_len)
1775 struct nlattr *key_attr = nla_nest_start(msg, NL80211_ATTR_KEY);
1776 if (!key_attr)
1777 return -1;
1779 if (defkey && alg == WPA_ALG_IGTK)
1780 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT_MGMT);
1781 else if (defkey)
1782 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
1784 NLA_PUT_U8(msg, NL80211_KEY_IDX, key_idx);
1786 switch (alg) {
1787 case WPA_ALG_WEP:
1788 if (key_len == 5)
1789 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
1790 WLAN_CIPHER_SUITE_WEP40);
1791 else
1792 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
1793 WLAN_CIPHER_SUITE_WEP104);
1794 break;
1795 case WPA_ALG_TKIP:
1796 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_TKIP);
1797 break;
1798 case WPA_ALG_CCMP:
1799 NLA_PUT_U32(msg, NL80211_KEY_CIPHER, WLAN_CIPHER_SUITE_CCMP);
1800 break;
1801 case WPA_ALG_IGTK:
1802 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
1803 WLAN_CIPHER_SUITE_AES_CMAC);
1804 break;
1805 default:
1806 wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
1807 "algorithm %d", __func__, alg);
1808 return -1;
1811 if (seq && seq_len)
1812 NLA_PUT(msg, NL80211_KEY_SEQ, seq_len, seq);
1814 NLA_PUT(msg, NL80211_KEY_DATA, key_len, key);
1816 nla_nest_end(msg, key_attr);
1818 return 0;
1819 nla_put_failure:
1820 return -1;
1824 static int nl80211_set_conn_keys(struct wpa_driver_associate_params *params,
1825 struct nl_msg *msg)
1827 int i, privacy = 0;
1828 struct nlattr *nl_keys, *nl_key;
1830 for (i = 0; i < 4; i++) {
1831 if (!params->wep_key[i])
1832 continue;
1833 privacy = 1;
1834 break;
1836 if (!privacy)
1837 return 0;
1839 NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
1841 nl_keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
1842 if (!nl_keys)
1843 goto nla_put_failure;
1845 for (i = 0; i < 4; i++) {
1846 if (!params->wep_key[i])
1847 continue;
1849 nl_key = nla_nest_start(msg, i);
1850 if (!nl_key)
1851 goto nla_put_failure;
1853 NLA_PUT(msg, NL80211_KEY_DATA, params->wep_key_len[i],
1854 params->wep_key[i]);
1855 if (params->wep_key_len[i] == 5)
1856 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
1857 WLAN_CIPHER_SUITE_WEP40);
1858 else
1859 NLA_PUT_U32(msg, NL80211_KEY_CIPHER,
1860 WLAN_CIPHER_SUITE_WEP104);
1862 NLA_PUT_U8(msg, NL80211_KEY_IDX, i);
1864 if (i == params->wep_tx_keyidx)
1865 NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
1867 nla_nest_end(msg, nl_key);
1869 nla_nest_end(msg, nl_keys);
1871 return 0;
1873 nla_put_failure:
1874 return -ENOBUFS;
1878 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
1879 const u8 *addr, int cmd, u16 reason_code)
1881 int ret = -1;
1882 struct nl_msg *msg;
1884 msg = nlmsg_alloc();
1885 if (!msg)
1886 return -1;
1888 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0, cmd, 0);
1890 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1891 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason_code);
1892 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
1894 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
1895 msg = NULL;
1896 if (ret) {
1897 wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
1898 "(%s)", ret, strerror(-ret));
1899 goto nla_put_failure;
1901 ret = 0;
1903 nla_put_failure:
1904 nlmsg_free(msg);
1905 return ret;
1909 static int wpa_driver_nl80211_disconnect(struct wpa_driver_nl80211_data *drv,
1910 const u8 *addr, int reason_code)
1912 wpa_printf(MSG_DEBUG, "%s", __func__);
1913 drv->associated = 0;
1914 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISCONNECT,
1915 reason_code);
1919 static int wpa_driver_nl80211_deauthenticate(void *priv, const u8 *addr,
1920 int reason_code)
1922 struct wpa_driver_nl80211_data *drv = priv;
1923 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
1924 return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
1925 wpa_printf(MSG_DEBUG, "%s", __func__);
1926 drv->associated = 0;
1927 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
1928 reason_code);
1932 static int wpa_driver_nl80211_disassociate(void *priv, const u8 *addr,
1933 int reason_code)
1935 struct wpa_driver_nl80211_data *drv = priv;
1936 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
1937 return wpa_driver_nl80211_disconnect(drv, addr, reason_code);
1938 wpa_printf(MSG_DEBUG, "%s", __func__);
1939 drv->associated = 0;
1940 return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISASSOCIATE,
1941 reason_code);
1945 static int wpa_driver_nl80211_authenticate(
1946 void *priv, struct wpa_driver_auth_params *params)
1948 struct wpa_driver_nl80211_data *drv = priv;
1949 int ret = -1, i;
1950 struct nl_msg *msg;
1951 enum nl80211_auth_type type;
1952 int count = 0;
1954 drv->associated = 0;
1955 os_memset(drv->auth_bssid, 0, ETH_ALEN);
1957 if (wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_INFRA) < 0)
1958 return -1;
1960 retry:
1961 msg = nlmsg_alloc();
1962 if (!msg)
1963 return -1;
1965 wpa_printf(MSG_DEBUG, "nl80211: Authenticate (ifindex=%d)",
1966 drv->ifindex);
1968 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
1969 NL80211_CMD_AUTHENTICATE, 0);
1971 for (i = 0; i < 4; i++) {
1972 if (!params->wep_key[i])
1973 continue;
1974 wpa_driver_nl80211_set_key(drv->ifname, drv, WPA_ALG_WEP, NULL,
1976 i == params->wep_tx_keyidx, NULL, 0,
1977 params->wep_key[i],
1978 params->wep_key_len[i]);
1979 if (params->wep_tx_keyidx != i)
1980 continue;
1981 if (nl_add_key(msg, WPA_ALG_WEP, i, 1, NULL, 0,
1982 params->wep_key[i], params->wep_key_len[i])) {
1983 nlmsg_free(msg);
1984 return -1;
1988 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
1989 if (params->bssid) {
1990 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
1991 MAC2STR(params->bssid));
1992 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
1994 if (params->freq) {
1995 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
1996 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
1998 if (params->ssid) {
1999 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
2000 params->ssid, params->ssid_len);
2001 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
2002 params->ssid);
2004 wpa_hexdump(MSG_DEBUG, " * IEs", params->ie, params->ie_len);
2005 if (params->ie)
2006 NLA_PUT(msg, NL80211_ATTR_IE, params->ie_len, params->ie);
2008 * TODO: if multiple auth_alg options enabled, try them one by one if
2009 * the AP rejects authentication due to unknown auth alg
2011 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
2012 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
2013 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
2014 type = NL80211_AUTHTYPE_SHARED_KEY;
2015 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
2016 type = NL80211_AUTHTYPE_NETWORK_EAP;
2017 else if (params->auth_alg & WPA_AUTH_ALG_FT)
2018 type = NL80211_AUTHTYPE_FT;
2019 else
2020 goto nla_put_failure;
2021 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
2022 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
2024 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2025 msg = NULL;
2026 if (ret) {
2027 wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
2028 "(%s)", ret, strerror(-ret));
2029 count++;
2030 if (ret == -EALREADY && count == 1 && params->bssid) {
2032 * mac80211 does not currently accept new
2033 * authentication if we are already authenticated. As a
2034 * workaround, force deauthentication and try again.
2036 wpa_printf(MSG_DEBUG, "nl80211: Retry authentication "
2037 "after forced deauthentication");
2038 wpa_driver_nl80211_deauthenticate(
2039 drv, params->bssid,
2040 WLAN_REASON_PREV_AUTH_NOT_VALID);
2041 nlmsg_free(msg);
2042 goto retry;
2044 goto nla_put_failure;
2046 ret = 0;
2047 wpa_printf(MSG_DEBUG, "nl80211: Authentication request send "
2048 "successfully");
2050 nla_put_failure:
2051 nlmsg_free(msg);
2052 return ret;
2056 struct phy_info_arg {
2057 u16 *num_modes;
2058 struct hostapd_hw_modes *modes;
2061 static int phy_info_handler(struct nl_msg *msg, void *arg)
2063 struct nlattr *tb_msg[NL80211_ATTR_MAX + 1];
2064 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
2065 struct phy_info_arg *phy_info = arg;
2067 struct nlattr *tb_band[NL80211_BAND_ATTR_MAX + 1];
2069 struct nlattr *tb_freq[NL80211_FREQUENCY_ATTR_MAX + 1];
2070 static struct nla_policy freq_policy[NL80211_FREQUENCY_ATTR_MAX + 1] = {
2071 [NL80211_FREQUENCY_ATTR_FREQ] = { .type = NLA_U32 },
2072 [NL80211_FREQUENCY_ATTR_DISABLED] = { .type = NLA_FLAG },
2073 [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
2074 [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
2075 [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
2076 [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
2079 struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
2080 static struct nla_policy rate_policy[NL80211_BITRATE_ATTR_MAX + 1] = {
2081 [NL80211_BITRATE_ATTR_RATE] = { .type = NLA_U32 },
2082 [NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE] = { .type = NLA_FLAG },
2085 struct nlattr *nl_band;
2086 struct nlattr *nl_freq;
2087 struct nlattr *nl_rate;
2088 int rem_band, rem_freq, rem_rate;
2089 struct hostapd_hw_modes *mode;
2090 int idx, mode_is_set;
2092 nla_parse(tb_msg, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
2093 genlmsg_attrlen(gnlh, 0), NULL);
2095 if (!tb_msg[NL80211_ATTR_WIPHY_BANDS])
2096 return NL_SKIP;
2098 nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
2099 mode = os_realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
2100 if (!mode)
2101 return NL_SKIP;
2102 phy_info->modes = mode;
2104 mode_is_set = 0;
2106 mode = &phy_info->modes[*(phy_info->num_modes)];
2107 memset(mode, 0, sizeof(*mode));
2108 *(phy_info->num_modes) += 1;
2110 nla_parse(tb_band, NL80211_BAND_ATTR_MAX, nla_data(nl_band),
2111 nla_len(nl_band), NULL);
2113 if (tb_band[NL80211_BAND_ATTR_HT_CAPA]) {
2114 mode->ht_capab = nla_get_u16(
2115 tb_band[NL80211_BAND_ATTR_HT_CAPA]);
2118 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) {
2119 mode->a_mpdu_params |= nla_get_u8(
2120 tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) &
2121 0x03;
2124 if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) {
2125 mode->a_mpdu_params |= nla_get_u8(
2126 tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) <<
2130 if (tb_band[NL80211_BAND_ATTR_HT_MCS_SET] &&
2131 nla_len(tb_band[NL80211_BAND_ATTR_HT_MCS_SET])) {
2132 u8 *mcs;
2133 mcs = nla_data(tb_band[NL80211_BAND_ATTR_HT_MCS_SET]);
2134 os_memcpy(mode->mcs_set, mcs, 16);
2137 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
2138 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
2139 nla_len(nl_freq), freq_policy);
2140 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
2141 continue;
2142 mode->num_channels++;
2145 mode->channels = os_zalloc(mode->num_channels * sizeof(struct hostapd_channel_data));
2146 if (!mode->channels)
2147 return NL_SKIP;
2149 idx = 0;
2151 nla_for_each_nested(nl_freq, tb_band[NL80211_BAND_ATTR_FREQS], rem_freq) {
2152 nla_parse(tb_freq, NL80211_FREQUENCY_ATTR_MAX, nla_data(nl_freq),
2153 nla_len(nl_freq), freq_policy);
2154 if (!tb_freq[NL80211_FREQUENCY_ATTR_FREQ])
2155 continue;
2157 mode->channels[idx].freq = nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_FREQ]);
2158 mode->channels[idx].flag = 0;
2160 if (!mode_is_set) {
2161 /* crude heuristic */
2162 if (mode->channels[idx].freq < 4000)
2163 mode->mode = HOSTAPD_MODE_IEEE80211B;
2164 else
2165 mode->mode = HOSTAPD_MODE_IEEE80211A;
2166 mode_is_set = 1;
2169 /* crude heuristic */
2170 if (mode->channels[idx].freq < 4000)
2171 if (mode->channels[idx].freq == 2484)
2172 mode->channels[idx].chan = 14;
2173 else
2174 mode->channels[idx].chan = (mode->channels[idx].freq - 2407) / 5;
2175 else
2176 mode->channels[idx].chan = mode->channels[idx].freq/5 - 1000;
2178 if (tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
2179 mode->channels[idx].flag |=
2180 HOSTAPD_CHAN_DISABLED;
2181 if (tb_freq[NL80211_FREQUENCY_ATTR_PASSIVE_SCAN])
2182 mode->channels[idx].flag |=
2183 HOSTAPD_CHAN_PASSIVE_SCAN;
2184 if (tb_freq[NL80211_FREQUENCY_ATTR_NO_IBSS])
2185 mode->channels[idx].flag |=
2186 HOSTAPD_CHAN_NO_IBSS;
2187 if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
2188 mode->channels[idx].flag |=
2189 HOSTAPD_CHAN_RADAR;
2191 if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
2192 !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
2193 mode->channels[idx].max_tx_power =
2194 nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
2196 idx++;
2199 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
2200 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
2201 nla_len(nl_rate), rate_policy);
2202 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
2203 continue;
2204 mode->num_rates++;
2207 mode->rates = os_zalloc(mode->num_rates * sizeof(int));
2208 if (!mode->rates)
2209 return NL_SKIP;
2211 idx = 0;
2213 nla_for_each_nested(nl_rate, tb_band[NL80211_BAND_ATTR_RATES], rem_rate) {
2214 nla_parse(tb_rate, NL80211_BITRATE_ATTR_MAX, nla_data(nl_rate),
2215 nla_len(nl_rate), rate_policy);
2216 if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
2217 continue;
2218 mode->rates[idx] = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
2220 /* crude heuristic */
2221 if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
2222 mode->rates[idx] > 200)
2223 mode->mode = HOSTAPD_MODE_IEEE80211G;
2225 idx++;
2229 return NL_SKIP;
2232 static struct hostapd_hw_modes *
2233 wpa_driver_nl80211_add_11b(struct hostapd_hw_modes *modes, u16 *num_modes)
2235 u16 m;
2236 struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
2237 int i, mode11g_idx = -1;
2239 /* If only 802.11g mode is included, use it to construct matching
2240 * 802.11b mode data. */
2242 for (m = 0; m < *num_modes; m++) {
2243 if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
2244 return modes; /* 802.11b already included */
2245 if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
2246 mode11g_idx = m;
2249 if (mode11g_idx < 0)
2250 return modes; /* 2.4 GHz band not supported at all */
2252 nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes));
2253 if (nmodes == NULL)
2254 return modes; /* Could not add 802.11b mode */
2256 mode = &nmodes[*num_modes];
2257 os_memset(mode, 0, sizeof(*mode));
2258 (*num_modes)++;
2259 modes = nmodes;
2261 mode->mode = HOSTAPD_MODE_IEEE80211B;
2263 mode11g = &modes[mode11g_idx];
2264 mode->num_channels = mode11g->num_channels;
2265 mode->channels = os_malloc(mode11g->num_channels *
2266 sizeof(struct hostapd_channel_data));
2267 if (mode->channels == NULL) {
2268 (*num_modes)--;
2269 return modes; /* Could not add 802.11b mode */
2271 os_memcpy(mode->channels, mode11g->channels,
2272 mode11g->num_channels * sizeof(struct hostapd_channel_data));
2274 mode->num_rates = 0;
2275 mode->rates = os_malloc(4 * sizeof(int));
2276 if (mode->rates == NULL) {
2277 os_free(mode->channels);
2278 (*num_modes)--;
2279 return modes; /* Could not add 802.11b mode */
2282 for (i = 0; i < mode11g->num_rates; i++) {
2283 if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
2284 mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
2285 continue;
2286 mode->rates[mode->num_rates] = mode11g->rates[i];
2287 mode->num_rates++;
2288 if (mode->num_rates == 4)
2289 break;
2292 if (mode->num_rates == 0) {
2293 os_free(mode->channels);
2294 os_free(mode->rates);
2295 (*num_modes)--;
2296 return modes; /* No 802.11b rates */
2299 wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
2300 "information");
2302 return modes;
2306 static struct hostapd_hw_modes *
2307 wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
2309 struct wpa_driver_nl80211_data *drv = priv;
2310 struct nl_msg *msg;
2311 struct phy_info_arg result = {
2312 .num_modes = num_modes,
2313 .modes = NULL,
2316 *num_modes = 0;
2317 *flags = 0;
2319 msg = nlmsg_alloc();
2320 if (!msg)
2321 return NULL;
2323 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2324 0, NL80211_CMD_GET_WIPHY, 0);
2326 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2328 if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0)
2329 return wpa_driver_nl80211_add_11b(result.modes, num_modes);
2330 nla_put_failure:
2331 return NULL;
2335 static int wpa_driver_nl80211_send_frame(struct wpa_driver_nl80211_data *drv,
2336 const void *data, size_t len,
2337 int encrypt)
2339 __u8 rtap_hdr[] = {
2340 0x00, 0x00, /* radiotap version */
2341 0x0e, 0x00, /* radiotap length */
2342 0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
2343 IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
2344 0x00, /* padding */
2345 0x00, 0x00, /* RX and TX flags to indicate that */
2346 0x00, 0x00, /* this is the injected frame directly */
2348 struct iovec iov[2] = {
2350 .iov_base = &rtap_hdr,
2351 .iov_len = sizeof(rtap_hdr),
2354 .iov_base = (void *) data,
2355 .iov_len = len,
2358 struct msghdr msg = {
2359 .msg_name = NULL,
2360 .msg_namelen = 0,
2361 .msg_iov = iov,
2362 .msg_iovlen = 2,
2363 .msg_control = NULL,
2364 .msg_controllen = 0,
2365 .msg_flags = 0,
2368 if (encrypt)
2369 rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
2371 return sendmsg(drv->monitor_sock, &msg, 0);
2375 static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data,
2376 size_t data_len)
2378 struct wpa_driver_nl80211_data *drv = priv;
2379 struct ieee80211_mgmt *mgmt;
2380 int encrypt = 1;
2381 u16 fc;
2383 mgmt = (struct ieee80211_mgmt *) data;
2384 fc = le_to_host16(mgmt->frame_control);
2386 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
2387 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
2389 * Only one of the authentication frame types is encrypted.
2390 * In order for static WEP encryption to work properly (i.e.,
2391 * to not encrypt the frame), we need to tell mac80211 about
2392 * the frames that must not be encrypted.
2394 u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
2395 u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
2396 if (auth_alg != WLAN_AUTH_SHARED_KEY || auth_trans != 3)
2397 encrypt = 0;
2400 return wpa_driver_nl80211_send_frame(drv, data, data_len, encrypt);
2404 static int wpa_driver_nl80211_set_beacon(const char *ifname, void *priv,
2405 const u8 *head, size_t head_len,
2406 const u8 *tail, size_t tail_len,
2407 int dtim_period, int beacon_int)
2409 struct wpa_driver_nl80211_data *drv = priv;
2410 struct nl_msg *msg;
2411 u8 cmd = NL80211_CMD_NEW_BEACON;
2412 int ret;
2413 int beacon_set;
2414 int ifindex = if_nametoindex(ifname);
2415 #ifdef HOSTAPD
2416 struct i802_bss *bss;
2418 bss = get_bss(drv, ifindex);
2419 if (bss == NULL)
2420 return -ENOENT;
2421 beacon_set = bss->beacon_set;
2422 #else /* HOSTAPD */
2423 beacon_set = drv->beacon_set;
2424 #endif /* HOSTAPD */
2426 msg = nlmsg_alloc();
2427 if (!msg)
2428 return -ENOMEM;
2430 wpa_printf(MSG_DEBUG, "nl80211: Set beacon (beacon_set=%d)",
2431 beacon_set);
2432 if (beacon_set)
2433 cmd = NL80211_CMD_SET_BEACON;
2435 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2436 0, cmd, 0);
2437 NLA_PUT(msg, NL80211_ATTR_BEACON_HEAD, head_len, head);
2438 NLA_PUT(msg, NL80211_ATTR_BEACON_TAIL, tail_len, tail);
2439 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
2440 NLA_PUT_U32(msg, NL80211_ATTR_BEACON_INTERVAL, beacon_int);
2441 NLA_PUT_U32(msg, NL80211_ATTR_DTIM_PERIOD, dtim_period);
2443 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2444 if (ret) {
2445 wpa_printf(MSG_DEBUG, "nl80211: Beacon set failed: %d (%s)",
2446 ret, strerror(-ret));
2447 } else {
2448 #ifdef HOSTAPD
2449 bss->beacon_set = 1;
2450 #else /* HOSTAPD */
2451 drv->beacon_set = 1;
2452 #endif /* HOSTAPD */
2454 return ret;
2455 nla_put_failure:
2456 return -ENOBUFS;
2460 static int wpa_driver_nl80211_set_freq(struct wpa_driver_nl80211_data *drv,
2461 int freq, int ht_enabled,
2462 int sec_channel_offset)
2464 struct nl_msg *msg;
2465 int ret;
2467 msg = nlmsg_alloc();
2468 if (!msg)
2469 return -1;
2471 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
2472 NL80211_CMD_SET_WIPHY, 0);
2474 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2475 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
2476 if (ht_enabled) {
2477 switch (sec_channel_offset) {
2478 case -1:
2479 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2480 NL80211_CHAN_HT40MINUS);
2481 break;
2482 case 1:
2483 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2484 NL80211_CHAN_HT40PLUS);
2485 break;
2486 default:
2487 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2488 NL80211_CHAN_HT20);
2489 break;
2493 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2494 if (ret == 0)
2495 return 0;
2496 wpa_printf(MSG_DEBUG, "nl80211: Failed to set channel (freq=%d): "
2497 "%d (%s)", freq, ret, strerror(-ret));
2498 nla_put_failure:
2499 return -1;
2503 static int wpa_driver_nl80211_sta_add(const char *ifname, void *priv,
2504 struct hostapd_sta_add_params *params)
2506 struct wpa_driver_nl80211_data *drv = priv;
2507 struct nl_msg *msg;
2508 int ret = -ENOBUFS;
2510 msg = nlmsg_alloc();
2511 if (!msg)
2512 return -ENOMEM;
2514 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2515 0, NL80211_CMD_NEW_STATION, 0);
2517 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(ifname));
2518 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
2519 NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
2520 NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
2521 params->supp_rates);
2522 NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
2523 params->listen_interval);
2524 if (params->ht_capabilities) {
2525 NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
2526 sizeof(*params->ht_capabilities),
2527 params->ht_capabilities);
2530 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2531 if (ret)
2532 wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_NEW_STATION "
2533 "result: %d (%s)", ret, strerror(-ret));
2534 if (ret == -EEXIST)
2535 ret = 0;
2536 nla_put_failure:
2537 return ret;
2541 static int wpa_driver_nl80211_sta_remove(void *priv, const u8 *addr)
2543 struct wpa_driver_nl80211_data *drv = priv;
2544 struct nl_msg *msg;
2545 int ret;
2547 msg = nlmsg_alloc();
2548 if (!msg)
2549 return -ENOMEM;
2551 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2552 0, NL80211_CMD_DEL_STATION, 0);
2554 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
2555 if_nametoindex(drv->ifname));
2556 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
2558 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2559 if (ret == -ENOENT)
2560 return 0;
2561 return ret;
2562 nla_put_failure:
2563 return -ENOBUFS;
2567 static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
2568 int ifidx)
2570 struct nl_msg *msg;
2572 wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
2574 #ifdef HOSTAPD
2575 /* stop listening for EAPOL on this interface */
2576 del_ifidx(drv, ifidx);
2577 #endif /* HOSTAPD */
2579 msg = nlmsg_alloc();
2580 if (!msg)
2581 goto nla_put_failure;
2583 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2584 0, NL80211_CMD_DEL_INTERFACE, 0);
2585 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
2587 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
2588 return;
2589 nla_put_failure:
2590 wpa_printf(MSG_ERROR, "Failed to remove interface (ifidx=%d).\n",
2591 ifidx);
2595 static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
2596 const char *ifname,
2597 enum nl80211_iftype iftype,
2598 const u8 *addr, int wds)
2600 struct nl_msg *msg, *flags = NULL;
2601 int ifidx;
2602 int ret = -ENOBUFS;
2604 msg = nlmsg_alloc();
2605 if (!msg)
2606 return -1;
2608 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
2609 0, NL80211_CMD_NEW_INTERFACE, 0);
2610 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
2611 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
2612 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
2614 if (iftype == NL80211_IFTYPE_MONITOR) {
2615 int err;
2617 flags = nlmsg_alloc();
2618 if (!flags)
2619 goto nla_put_failure;
2621 NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
2623 err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
2625 nlmsg_free(flags);
2627 if (err)
2628 goto nla_put_failure;
2629 } else if (wds) {
2630 NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
2633 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
2634 if (ret) {
2635 nla_put_failure:
2636 wpa_printf(MSG_ERROR, "Failed to create interface %s: %d (%s)",
2637 ifname, ret, strerror(-ret));
2638 return ret;
2641 ifidx = if_nametoindex(ifname);
2642 wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
2643 ifname, ifidx);
2645 if (ifidx <= 0)
2646 return -1;
2648 #ifdef HOSTAPD
2649 /* start listening for EAPOL on this interface */
2650 add_ifidx(drv, ifidx);
2651 #endif /* HOSTAPD */
2653 if (addr && iftype != NL80211_IFTYPE_MONITOR &&
2654 linux_set_ifhwaddr(drv->ioctl_sock, ifname, addr)) {
2655 nl80211_remove_iface(drv, ifidx);
2656 return -1;
2659 return ifidx;
2663 static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
2664 const char *ifname, enum nl80211_iftype iftype,
2665 const u8 *addr, int wds)
2667 int ret;
2669 ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds);
2671 /* if error occured and interface exists already */
2672 if (ret == -ENFILE && if_nametoindex(ifname)) {
2673 wpa_printf(MSG_INFO, "Try to remove and re-create %s", ifname);
2675 /* Try to remove the interface that was already there. */
2676 nl80211_remove_iface(drv, if_nametoindex(ifname));
2678 /* Try to create the interface again */
2679 ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
2680 wds);
2683 return ret;
2687 static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
2689 struct ieee80211_hdr *hdr;
2690 u16 fc;
2691 union wpa_event_data event;
2693 hdr = (struct ieee80211_hdr *) buf;
2694 fc = le_to_host16(hdr->frame_control);
2696 os_memset(&event, 0, sizeof(event));
2697 event.tx_status.type = WLAN_FC_GET_TYPE(fc);
2698 event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
2699 event.tx_status.dst = hdr->addr1;
2700 event.tx_status.data = buf;
2701 event.tx_status.data_len = len;
2702 event.tx_status.ack = ok;
2703 wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
2707 static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
2708 u8 *buf, size_t len)
2710 union wpa_event_data event;
2711 os_memset(&event, 0, sizeof(event));
2712 event.rx_from_unknown.frame = buf;
2713 event.rx_from_unknown.len = len;
2714 wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
2718 static void handle_frame(struct wpa_driver_nl80211_data *drv,
2719 u8 *buf, size_t len, int datarate, int ssi_signal)
2721 struct ieee80211_hdr *hdr;
2722 u16 fc;
2723 union wpa_event_data event;
2725 hdr = (struct ieee80211_hdr *) buf;
2726 fc = le_to_host16(hdr->frame_control);
2728 switch (WLAN_FC_GET_TYPE(fc)) {
2729 case WLAN_FC_TYPE_MGMT:
2730 os_memset(&event, 0, sizeof(event));
2731 event.rx_mgmt.frame = buf;
2732 event.rx_mgmt.frame_len = len;
2733 event.rx_mgmt.datarate = datarate;
2734 event.rx_mgmt.ssi_signal = ssi_signal;
2735 wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
2736 break;
2737 case WLAN_FC_TYPE_CTRL:
2738 /* can only get here with PS-Poll frames */
2739 wpa_printf(MSG_DEBUG, "CTRL");
2740 from_unknown_sta(drv, buf, len);
2741 break;
2742 case WLAN_FC_TYPE_DATA:
2743 from_unknown_sta(drv, buf, len);
2744 break;
2749 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
2751 struct wpa_driver_nl80211_data *drv = eloop_ctx;
2752 int len;
2753 unsigned char buf[3000];
2754 struct ieee80211_radiotap_iterator iter;
2755 int ret;
2756 int datarate = 0, ssi_signal = 0;
2757 int injected = 0, failed = 0, rxflags = 0;
2759 len = recv(sock, buf, sizeof(buf), 0);
2760 if (len < 0) {
2761 perror("recv");
2762 return;
2765 if (drv->nlmode == NL80211_IFTYPE_STATION && !drv->probe_req_report) {
2766 wpa_printf(MSG_DEBUG, "nl80211: Ignore monitor interface "
2767 "frame since Probe Request reporting is disabled");
2768 return;
2771 if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
2772 printf("received invalid radiotap frame\n");
2773 return;
2776 while (1) {
2777 ret = ieee80211_radiotap_iterator_next(&iter);
2778 if (ret == -ENOENT)
2779 break;
2780 if (ret) {
2781 printf("received invalid radiotap frame (%d)\n", ret);
2782 return;
2784 switch (iter.this_arg_index) {
2785 case IEEE80211_RADIOTAP_FLAGS:
2786 if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
2787 len -= 4;
2788 break;
2789 case IEEE80211_RADIOTAP_RX_FLAGS:
2790 rxflags = 1;
2791 break;
2792 case IEEE80211_RADIOTAP_TX_FLAGS:
2793 injected = 1;
2794 failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
2795 IEEE80211_RADIOTAP_F_TX_FAIL;
2796 break;
2797 case IEEE80211_RADIOTAP_DATA_RETRIES:
2798 break;
2799 case IEEE80211_RADIOTAP_CHANNEL:
2800 /* TODO: convert from freq/flags to channel number */
2801 break;
2802 case IEEE80211_RADIOTAP_RATE:
2803 datarate = *iter.this_arg * 5;
2804 break;
2805 case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
2806 ssi_signal = *iter.this_arg;
2807 break;
2811 if (rxflags && injected)
2812 return;
2814 if (!injected)
2815 handle_frame(drv, buf + iter.max_length,
2816 len - iter.max_length, datarate, ssi_signal);
2817 else
2818 handle_tx_callback(drv->ctx, buf + iter.max_length,
2819 len - iter.max_length, !failed);
2824 * we post-process the filter code later and rewrite
2825 * this to the offset to the last instruction
2827 #define PASS 0xFF
2828 #define FAIL 0xFE
2830 static struct sock_filter msock_filter_insns[] = {
2832 * do a little-endian load of the radiotap length field
2834 /* load lower byte into A */
2835 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 2),
2836 /* put it into X (== index register) */
2837 BPF_STMT(BPF_MISC| BPF_TAX, 0),
2838 /* load upper byte into A */
2839 BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 3),
2840 /* left-shift it by 8 */
2841 BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
2842 /* or with X */
2843 BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
2844 /* put result into X */
2845 BPF_STMT(BPF_MISC| BPF_TAX, 0),
2848 * Allow management frames through, this also gives us those
2849 * management frames that we sent ourselves with status
2851 /* load the lower byte of the IEEE 802.11 frame control field */
2852 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
2853 /* mask off frame type and version */
2854 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
2855 /* accept frame if it's both 0, fall through otherwise */
2856 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
2859 * TODO: add a bit to radiotap RX flags that indicates
2860 * that the sending station is not associated, then
2861 * add a filter here that filters on our DA and that flag
2862 * to allow us to deauth frames to that bad station.
2864 * Not a regression -- we didn't do it before either.
2867 #if 0
2869 * drop non-data frames
2871 /* load the lower byte of the frame control field */
2872 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
2873 /* mask off QoS bit */
2874 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x0c),
2875 /* drop non-data frames */
2876 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 8, 0, FAIL),
2877 #endif
2878 /* load the upper byte of the frame control field */
2879 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 1),
2880 /* mask off toDS/fromDS */
2881 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x03),
2882 /* accept WDS frames */
2883 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 3, PASS, 0),
2886 * add header length to index
2888 /* load the lower byte of the frame control field */
2889 BPF_STMT(BPF_LD | BPF_B | BPF_IND, 0),
2890 /* mask off QoS bit */
2891 BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0x80),
2892 /* right shift it by 6 to give 0 or 2 */
2893 BPF_STMT(BPF_ALU | BPF_RSH | BPF_K, 6),
2894 /* add data frame header length */
2895 BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 24),
2896 /* add index, was start of 802.11 header */
2897 BPF_STMT(BPF_ALU | BPF_ADD | BPF_X, 0),
2898 /* move to index, now start of LL header */
2899 BPF_STMT(BPF_MISC | BPF_TAX, 0),
2902 * Accept empty data frames, we use those for
2903 * polling activity.
2905 BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
2906 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
2909 * Accept EAPOL frames
2911 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 0),
2912 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
2913 BPF_STMT(BPF_LD | BPF_W | BPF_IND, 4),
2914 BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
2916 /* keep these last two statements or change the code below */
2917 /* return 0 == "DROP" */
2918 BPF_STMT(BPF_RET | BPF_K, 0),
2919 /* return ~0 == "keep all" */
2920 BPF_STMT(BPF_RET | BPF_K, ~0),
2923 static struct sock_fprog msock_filter = {
2924 .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
2925 .filter = msock_filter_insns,
2929 static int add_monitor_filter(int s)
2931 int idx;
2933 /* rewrite all PASS/FAIL jump offsets */
2934 for (idx = 0; idx < msock_filter.len; idx++) {
2935 struct sock_filter *insn = &msock_filter_insns[idx];
2937 if (BPF_CLASS(insn->code) == BPF_JMP) {
2938 if (insn->code == (BPF_JMP|BPF_JA)) {
2939 if (insn->k == PASS)
2940 insn->k = msock_filter.len - idx - 2;
2941 else if (insn->k == FAIL)
2942 insn->k = msock_filter.len - idx - 3;
2945 if (insn->jt == PASS)
2946 insn->jt = msock_filter.len - idx - 2;
2947 else if (insn->jt == FAIL)
2948 insn->jt = msock_filter.len - idx - 3;
2950 if (insn->jf == PASS)
2951 insn->jf = msock_filter.len - idx - 2;
2952 else if (insn->jf == FAIL)
2953 insn->jf = msock_filter.len - idx - 3;
2957 if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
2958 &msock_filter, sizeof(msock_filter))) {
2959 perror("SO_ATTACH_FILTER");
2960 return -1;
2963 return 0;
2967 static void nl80211_remove_monitor_interface(
2968 struct wpa_driver_nl80211_data *drv)
2970 if (drv->monitor_ifidx >= 0) {
2971 nl80211_remove_iface(drv, drv->monitor_ifidx);
2972 drv->monitor_ifidx = -1;
2974 if (drv->monitor_sock >= 0) {
2975 eloop_unregister_read_sock(drv->monitor_sock);
2976 close(drv->monitor_sock);
2977 drv->monitor_sock = -1;
2982 static int
2983 nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
2985 char buf[IFNAMSIZ];
2986 struct sockaddr_ll ll;
2987 int optval;
2988 socklen_t optlen;
2990 snprintf(buf, IFNAMSIZ, "mon.%s", drv->ifname);
2991 buf[IFNAMSIZ - 1] = '\0';
2993 drv->monitor_ifidx =
2994 nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
2997 if (drv->monitor_ifidx < 0)
2998 return -1;
3000 if (linux_set_iface_flags(drv->ioctl_sock, buf, 1))
3001 goto error;
3003 memset(&ll, 0, sizeof(ll));
3004 ll.sll_family = AF_PACKET;
3005 ll.sll_ifindex = drv->monitor_ifidx;
3006 drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
3007 if (drv->monitor_sock < 0) {
3008 perror("socket[PF_PACKET,SOCK_RAW]");
3009 goto error;
3012 if (add_monitor_filter(drv->monitor_sock)) {
3013 wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
3014 "interface; do filtering in user space");
3015 /* This works, but will cost in performance. */
3018 if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
3019 perror("monitor socket bind");
3020 goto error;
3023 optlen = sizeof(optval);
3024 optval = 20;
3025 if (setsockopt
3026 (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
3027 perror("Failed to set socket priority");
3028 goto error;
3031 if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
3032 drv, NULL)) {
3033 printf("Could not register monitor read socket\n");
3034 goto error;
3037 return 0;
3038 error:
3039 nl80211_remove_monitor_interface(drv);
3040 return -1;
3044 static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
3046 static int wpa_driver_nl80211_hapd_send_eapol(
3047 void *priv, const u8 *addr, const u8 *data,
3048 size_t data_len, int encrypt, const u8 *own_addr)
3050 struct wpa_driver_nl80211_data *drv = priv;
3051 struct ieee80211_hdr *hdr;
3052 size_t len;
3053 u8 *pos;
3054 int res;
3055 #if 0 /* FIX */
3056 int qos = sta->flags & WPA_STA_WMM;
3057 #else
3058 int qos = 0;
3059 #endif
3061 len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
3062 data_len;
3063 hdr = os_zalloc(len);
3064 if (hdr == NULL) {
3065 printf("malloc() failed for i802_send_data(len=%lu)\n",
3066 (unsigned long) len);
3067 return -1;
3070 hdr->frame_control =
3071 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
3072 hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
3073 if (encrypt)
3074 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
3075 #if 0 /* To be enabled if qos determination is added above */
3076 if (qos) {
3077 hdr->frame_control |=
3078 host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
3080 #endif
3082 memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
3083 memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
3084 memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
3085 pos = (u8 *) (hdr + 1);
3087 #if 0 /* To be enabled if qos determination is added above */
3088 if (qos) {
3089 /* add an empty QoS header if needed */
3090 pos[0] = 0;
3091 pos[1] = 0;
3092 pos += 2;
3094 #endif
3096 memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
3097 pos += sizeof(rfc1042_header);
3098 WPA_PUT_BE16(pos, ETH_P_PAE);
3099 pos += 2;
3100 memcpy(pos, data, data_len);
3102 res = wpa_driver_nl80211_send_frame(drv, (u8 *) hdr, len, encrypt);
3103 if (res < 0) {
3104 wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
3105 "failed: %d (%s)",
3106 (unsigned long) len, errno, strerror(errno));
3108 os_free(hdr);
3110 return res;
3114 static u32 sta_flags_nl80211(int flags)
3116 u32 f = 0;
3118 if (flags & WPA_STA_AUTHORIZED)
3119 f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
3120 if (flags & WPA_STA_WMM)
3121 f |= BIT(NL80211_STA_FLAG_WME);
3122 if (flags & WPA_STA_SHORT_PREAMBLE)
3123 f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
3124 if (flags & WPA_STA_MFP)
3125 f |= BIT(NL80211_STA_FLAG_MFP);
3127 return f;
3131 static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
3132 int total_flags, int flags_or,
3133 int flags_and)
3135 struct wpa_driver_nl80211_data *drv = priv;
3136 struct nl_msg *msg, *flags = NULL;
3137 struct nl80211_sta_flag_update upd;
3139 msg = nlmsg_alloc();
3140 if (!msg)
3141 return -ENOMEM;
3143 flags = nlmsg_alloc();
3144 if (!flags) {
3145 nlmsg_free(msg);
3146 return -ENOMEM;
3149 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3150 0, NL80211_CMD_SET_STATION, 0);
3152 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
3153 if_nametoindex(drv->ifname));
3154 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
3157 * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
3158 * can be removed eventually.
3160 if (total_flags & WPA_STA_AUTHORIZED)
3161 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
3163 if (total_flags & WPA_STA_WMM)
3164 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
3166 if (total_flags & WPA_STA_SHORT_PREAMBLE)
3167 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
3169 if (total_flags & WPA_STA_MFP)
3170 NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
3172 if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
3173 goto nla_put_failure;
3175 os_memset(&upd, 0, sizeof(upd));
3176 upd.mask = sta_flags_nl80211(flags_or | ~flags_and);
3177 upd.set = sta_flags_nl80211(flags_or);
3178 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
3180 nlmsg_free(flags);
3182 return send_and_recv_msgs(drv, msg, NULL, NULL);
3183 nla_put_failure:
3184 nlmsg_free(flags);
3185 return -ENOBUFS;
3189 static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
3190 struct wpa_driver_associate_params *params)
3192 if (wpa_driver_nl80211_set_mode(drv, params->mode) ||
3193 wpa_driver_nl80211_set_freq(drv, params->freq, 0, 0)) {
3194 nl80211_remove_monitor_interface(drv);
3195 return -1;
3198 /* TODO: setup monitor interface (and add code somewhere to remove this
3199 * when AP mode is stopped; associate with mode != 2 or drv_deinit) */
3201 return 0;
3205 static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
3207 struct nl_msg *msg;
3208 int ret = -1;
3210 msg = nlmsg_alloc();
3211 if (!msg)
3212 return -1;
3214 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
3215 NL80211_CMD_LEAVE_IBSS, 0);
3216 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3217 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3218 msg = NULL;
3219 if (ret) {
3220 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
3221 "(%s)", ret, strerror(-ret));
3222 goto nla_put_failure;
3225 ret = 0;
3226 wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
3228 nla_put_failure:
3229 nlmsg_free(msg);
3230 return ret;
3234 static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
3235 struct wpa_driver_associate_params *params)
3237 struct nl_msg *msg;
3238 int ret = -1;
3239 int count = 0;
3241 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
3243 if (wpa_driver_nl80211_set_mode(drv, params->mode)) {
3244 wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
3245 "IBSS mode");
3246 return -1;
3249 retry:
3250 msg = nlmsg_alloc();
3251 if (!msg)
3252 return -1;
3254 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
3255 NL80211_CMD_JOIN_IBSS, 0);
3256 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3258 if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
3259 goto nla_put_failure;
3261 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
3262 params->ssid, params->ssid_len);
3263 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
3264 params->ssid);
3265 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
3266 drv->ssid_len = params->ssid_len;
3268 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
3269 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
3271 ret = nl80211_set_conn_keys(params, msg);
3272 if (ret)
3273 goto nla_put_failure;
3275 if (params->wpa_ie) {
3276 wpa_hexdump(MSG_DEBUG,
3277 " * Extra IEs for Beacon/Probe Response frames",
3278 params->wpa_ie, params->wpa_ie_len);
3279 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
3280 params->wpa_ie);
3283 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3284 msg = NULL;
3285 if (ret) {
3286 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
3287 ret, strerror(-ret));
3288 count++;
3289 if (ret == -EALREADY && count == 1) {
3290 wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
3291 "forced leave");
3292 nl80211_leave_ibss(drv);
3293 nlmsg_free(msg);
3294 goto retry;
3297 goto nla_put_failure;
3299 ret = 0;
3300 wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
3302 nla_put_failure:
3303 nlmsg_free(msg);
3304 return ret;
3308 static int wpa_driver_nl80211_connect(
3309 struct wpa_driver_nl80211_data *drv,
3310 struct wpa_driver_associate_params *params)
3312 struct nl_msg *msg;
3313 enum nl80211_auth_type type;
3314 int ret = 0;
3316 msg = nlmsg_alloc();
3317 if (!msg)
3318 return -1;
3320 wpa_printf(MSG_DEBUG, "nl80211: Connect (ifindex=%d)", drv->ifindex);
3321 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
3322 NL80211_CMD_CONNECT, 0);
3324 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3325 if (params->bssid) {
3326 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
3327 MAC2STR(params->bssid));
3328 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
3330 if (params->freq) {
3331 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
3332 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
3334 if (params->ssid) {
3335 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
3336 params->ssid, params->ssid_len);
3337 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
3338 params->ssid);
3339 if (params->ssid_len > sizeof(drv->ssid))
3340 goto nla_put_failure;
3341 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
3342 drv->ssid_len = params->ssid_len;
3344 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
3345 if (params->wpa_ie)
3346 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
3347 params->wpa_ie);
3349 if (params->auth_alg & WPA_AUTH_ALG_OPEN)
3350 type = NL80211_AUTHTYPE_OPEN_SYSTEM;
3351 else if (params->auth_alg & WPA_AUTH_ALG_SHARED)
3352 type = NL80211_AUTHTYPE_SHARED_KEY;
3353 else if (params->auth_alg & WPA_AUTH_ALG_LEAP)
3354 type = NL80211_AUTHTYPE_NETWORK_EAP;
3355 else if (params->auth_alg & WPA_AUTH_ALG_FT)
3356 type = NL80211_AUTHTYPE_FT;
3357 else
3358 goto nla_put_failure;
3360 wpa_printf(MSG_DEBUG, " * Auth Type %d", type);
3361 NLA_PUT_U32(msg, NL80211_ATTR_AUTH_TYPE, type);
3363 if (params->wpa_ie && params->wpa_ie_len) {
3364 enum nl80211_wpa_versions ver;
3366 if (params->wpa_ie[0] == WLAN_EID_RSN)
3367 ver = NL80211_WPA_VERSION_2;
3368 else
3369 ver = NL80211_WPA_VERSION_1;
3371 wpa_printf(MSG_DEBUG, " * WPA Version %d", ver);
3372 NLA_PUT_U32(msg, NL80211_ATTR_WPA_VERSIONS, ver);
3375 if (params->pairwise_suite != CIPHER_NONE) {
3376 int cipher;
3378 switch (params->pairwise_suite) {
3379 case CIPHER_WEP40:
3380 cipher = WLAN_CIPHER_SUITE_WEP40;
3381 break;
3382 case CIPHER_WEP104:
3383 cipher = WLAN_CIPHER_SUITE_WEP104;
3384 break;
3385 case CIPHER_CCMP:
3386 cipher = WLAN_CIPHER_SUITE_CCMP;
3387 break;
3388 case CIPHER_TKIP:
3389 default:
3390 cipher = WLAN_CIPHER_SUITE_TKIP;
3391 break;
3393 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITES_PAIRWISE, cipher);
3396 if (params->group_suite != CIPHER_NONE) {
3397 int cipher;
3399 switch (params->group_suite) {
3400 case CIPHER_WEP40:
3401 cipher = WLAN_CIPHER_SUITE_WEP40;
3402 break;
3403 case CIPHER_WEP104:
3404 cipher = WLAN_CIPHER_SUITE_WEP104;
3405 break;
3406 case CIPHER_CCMP:
3407 cipher = WLAN_CIPHER_SUITE_CCMP;
3408 break;
3409 case CIPHER_TKIP:
3410 default:
3411 cipher = WLAN_CIPHER_SUITE_TKIP;
3412 break;
3414 NLA_PUT_U32(msg, NL80211_ATTR_CIPHER_SUITE_GROUP, cipher);
3417 if (params->key_mgmt_suite == KEY_MGMT_802_1X ||
3418 params->key_mgmt_suite == KEY_MGMT_PSK) {
3419 int mgmt = WLAN_AKM_SUITE_PSK;
3421 switch (params->key_mgmt_suite) {
3422 case KEY_MGMT_802_1X:
3423 mgmt = WLAN_AKM_SUITE_8021X;
3424 break;
3425 case KEY_MGMT_PSK:
3426 default:
3427 mgmt = WLAN_AKM_SUITE_PSK;
3428 break;
3430 NLA_PUT_U32(msg, NL80211_ATTR_AKM_SUITES, mgmt);
3433 ret = nl80211_set_conn_keys(params, msg);
3434 if (ret)
3435 goto nla_put_failure;
3437 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3438 msg = NULL;
3439 if (ret) {
3440 wpa_printf(MSG_DEBUG, "nl80211: MLME connect failed: ret=%d "
3441 "(%s)", ret, strerror(-ret));
3442 goto nla_put_failure;
3444 ret = 0;
3445 wpa_printf(MSG_DEBUG, "nl80211: Connect request send successfully");
3447 nla_put_failure:
3448 nlmsg_free(msg);
3449 return ret;
3454 static int wpa_driver_nl80211_associate(
3455 void *priv, struct wpa_driver_associate_params *params)
3457 struct wpa_driver_nl80211_data *drv = priv;
3458 int ret = -1;
3459 struct nl_msg *msg;
3461 if (params->mode == IEEE80211_MODE_AP)
3462 return wpa_driver_nl80211_ap(drv, params);
3464 if (params->mode == IEEE80211_MODE_IBSS)
3465 return wpa_driver_nl80211_ibss(drv, params);
3467 if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
3468 if (wpa_driver_nl80211_set_mode(drv, params->mode) < 0)
3469 return -1;
3470 return wpa_driver_nl80211_connect(drv, params);
3473 drv->associated = 0;
3475 msg = nlmsg_alloc();
3476 if (!msg)
3477 return -1;
3479 wpa_printf(MSG_DEBUG, "nl80211: Associate (ifindex=%d)",
3480 drv->ifindex);
3481 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
3482 NL80211_CMD_ASSOCIATE, 0);
3484 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3485 if (params->bssid) {
3486 wpa_printf(MSG_DEBUG, " * bssid=" MACSTR,
3487 MAC2STR(params->bssid));
3488 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->bssid);
3490 if (params->freq) {
3491 wpa_printf(MSG_DEBUG, " * freq=%d", params->freq);
3492 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
3493 drv->assoc_freq = params->freq;
3494 } else
3495 drv->assoc_freq = 0;
3496 if (params->ssid) {
3497 wpa_hexdump_ascii(MSG_DEBUG, " * SSID",
3498 params->ssid, params->ssid_len);
3499 NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
3500 params->ssid);
3501 if (params->ssid_len > sizeof(drv->ssid))
3502 goto nla_put_failure;
3503 os_memcpy(drv->ssid, params->ssid, params->ssid_len);
3504 drv->ssid_len = params->ssid_len;
3506 wpa_hexdump(MSG_DEBUG, " * IEs", params->wpa_ie, params->wpa_ie_len);
3507 if (params->wpa_ie)
3508 NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
3509 params->wpa_ie);
3511 #ifdef CONFIG_IEEE80211W
3512 if (params->mgmt_frame_protection == MGMT_FRAME_PROTECTION_REQUIRED)
3513 NLA_PUT_U32(msg, NL80211_ATTR_USE_MFP, NL80211_MFP_REQUIRED);
3514 #endif /* CONFIG_IEEE80211W */
3516 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT);
3518 if (params->prev_bssid) {
3519 wpa_printf(MSG_DEBUG, " * prev_bssid=" MACSTR,
3520 MAC2STR(params->prev_bssid));
3521 NLA_PUT(msg, NL80211_ATTR_PREV_BSSID, ETH_ALEN,
3522 params->prev_bssid);
3525 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3526 msg = NULL;
3527 if (ret) {
3528 wpa_printf(MSG_DEBUG, "nl80211: MLME command failed: ret=%d "
3529 "(%s)", ret, strerror(-ret));
3530 goto nla_put_failure;
3532 ret = 0;
3533 wpa_printf(MSG_DEBUG, "nl80211: Association request send "
3534 "successfully");
3536 nla_put_failure:
3537 nlmsg_free(msg);
3538 return ret;
3542 static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
3543 int ifindex, int mode)
3545 struct nl_msg *msg;
3546 int ret = -ENOBUFS;
3548 msg = nlmsg_alloc();
3549 if (!msg)
3550 return -ENOMEM;
3552 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3553 0, NL80211_CMD_SET_INTERFACE, 0);
3554 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
3555 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
3557 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3558 if (!ret)
3559 return 0;
3560 nla_put_failure:
3561 wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
3562 " %d (%s)", ifindex, mode, ret, strerror(-ret));
3563 return ret;
3567 static int wpa_driver_nl80211_set_mode(void *priv, int mode)
3569 struct wpa_driver_nl80211_data *drv = priv;
3570 int ret = -1;
3571 int nlmode;
3573 switch (mode) {
3574 case 0:
3575 nlmode = NL80211_IFTYPE_STATION;
3576 break;
3577 case 1:
3578 nlmode = NL80211_IFTYPE_ADHOC;
3579 break;
3580 case 2:
3581 nlmode = NL80211_IFTYPE_AP;
3582 break;
3583 default:
3584 return -1;
3587 if (nl80211_set_mode(drv, drv->ifindex, nlmode) == 0) {
3588 drv->nlmode = nlmode;
3589 ret = 0;
3590 goto done;
3593 if (nlmode == drv->nlmode) {
3594 wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
3595 "requested mode - ignore error");
3596 ret = 0;
3597 goto done; /* Already in the requested mode */
3600 /* mac80211 doesn't allow mode changes while the device is up, so
3601 * take the device down, try to set the mode again, and bring the
3602 * device back up.
3604 if (linux_set_iface_flags(drv->ioctl_sock, drv->ifname, 0) == 0) {
3605 /* Try to set the mode again while the interface is down */
3606 ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
3607 if (linux_set_iface_flags(drv->ioctl_sock, drv->ifname, 1))
3608 ret = -1;
3611 if (!ret) {
3612 wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
3613 "interface is down");
3614 drv->nlmode = nlmode;
3617 done:
3618 if (!ret && nlmode == NL80211_IFTYPE_AP) {
3619 /* Setup additional AP mode functionality if needed */
3620 if (drv->monitor_ifidx < 0 &&
3621 nl80211_create_monitor_interface(drv))
3622 return -1;
3623 } else if (!ret && nlmode != NL80211_IFTYPE_AP) {
3624 /* Remove additional AP mode functionality */
3625 nl80211_remove_monitor_interface(drv);
3628 if (ret)
3629 wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
3630 "from %d failed", nlmode, drv->nlmode);
3632 return ret;
3636 static int wpa_driver_nl80211_get_capa(void *priv,
3637 struct wpa_driver_capa *capa)
3639 struct wpa_driver_nl80211_data *drv = priv;
3640 if (!drv->has_capability)
3641 return -1;
3642 os_memcpy(capa, &drv->capa, sizeof(*capa));
3643 return 0;
3647 static int wpa_driver_nl80211_set_operstate(void *priv, int state)
3649 struct wpa_driver_nl80211_data *drv = priv;
3651 wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
3652 __func__, drv->operstate, state, state ? "UP" : "DORMANT");
3653 drv->operstate = state;
3654 return netlink_send_oper_ifla(drv->netlink, drv->ifindex, -1,
3655 state ? IF_OPER_UP : IF_OPER_DORMANT);
3659 static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
3661 struct wpa_driver_nl80211_data *drv = priv;
3662 struct nl_msg *msg;
3663 struct nl80211_sta_flag_update upd;
3665 msg = nlmsg_alloc();
3666 if (!msg)
3667 return -ENOMEM;
3669 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3670 0, NL80211_CMD_SET_STATION, 0);
3672 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
3673 if_nametoindex(drv->ifname));
3674 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, drv->bssid);
3676 os_memset(&upd, 0, sizeof(upd));
3677 upd.mask = BIT(NL80211_STA_FLAG_AUTHORIZED);
3678 if (authorized)
3679 upd.set = BIT(NL80211_STA_FLAG_AUTHORIZED);
3680 NLA_PUT(msg, NL80211_ATTR_STA_FLAGS2, sizeof(upd), &upd);
3682 return send_and_recv_msgs(drv, msg, NULL, NULL);
3683 nla_put_failure:
3684 return -ENOBUFS;
3688 #ifdef HOSTAPD
3690 static struct i802_bss * get_bss(struct wpa_driver_nl80211_data *drv,
3691 int ifindex)
3693 struct i802_bss *bss = &drv->bss;
3694 while (bss) {
3695 if (ifindex == bss->ifindex)
3696 return bss;
3697 bss = bss->next;
3699 wpa_printf(MSG_DEBUG, "nl80211: get_bss(%d) failed", ifindex);
3700 return NULL;
3704 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
3706 int i;
3707 int *old;
3709 wpa_printf(MSG_DEBUG, "nl80211: Add own interface ifindex %d",
3710 ifidx);
3711 for (i = 0; i < drv->num_if_indices; i++) {
3712 if (drv->if_indices[i] == 0) {
3713 drv->if_indices[i] = ifidx;
3714 return;
3718 if (drv->if_indices != drv->default_if_indices)
3719 old = drv->if_indices;
3720 else
3721 old = NULL;
3723 drv->if_indices = os_realloc(old,
3724 sizeof(int) * (drv->num_if_indices + 1));
3725 if (!drv->if_indices) {
3726 if (!old)
3727 drv->if_indices = drv->default_if_indices;
3728 else
3729 drv->if_indices = old;
3730 wpa_printf(MSG_ERROR, "Failed to reallocate memory for "
3731 "interfaces");
3732 wpa_printf(MSG_ERROR, "Ignoring EAPOL on interface %d", ifidx);
3733 return;
3735 drv->if_indices[drv->num_if_indices] = ifidx;
3736 drv->num_if_indices++;
3740 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
3742 int i;
3744 for (i = 0; i < drv->num_if_indices; i++) {
3745 if (drv->if_indices[i] == ifidx) {
3746 drv->if_indices[i] = 0;
3747 break;
3753 static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
3755 int i;
3757 for (i = 0; i < drv->num_if_indices; i++)
3758 if (drv->if_indices[i] == ifidx)
3759 return 1;
3761 return 0;
3765 static inline int min_int(int a, int b)
3767 if (a < b)
3768 return a;
3769 return b;
3773 static int get_key_handler(struct nl_msg *msg, void *arg)
3775 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3776 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3778 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3779 genlmsg_attrlen(gnlh, 0), NULL);
3782 * TODO: validate the key index and mac address!
3783 * Otherwise, there's a race condition as soon as
3784 * the kernel starts sending key notifications.
3787 if (tb[NL80211_ATTR_KEY_SEQ])
3788 memcpy(arg, nla_data(tb[NL80211_ATTR_KEY_SEQ]),
3789 min_int(nla_len(tb[NL80211_ATTR_KEY_SEQ]), 6));
3790 return NL_SKIP;
3794 static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
3795 int idx, u8 *seq)
3797 struct wpa_driver_nl80211_data *drv = priv;
3798 struct nl_msg *msg;
3800 msg = nlmsg_alloc();
3801 if (!msg)
3802 return -ENOMEM;
3804 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3805 0, NL80211_CMD_GET_KEY, 0);
3807 if (addr)
3808 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
3809 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
3810 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(iface));
3812 memset(seq, 0, 6);
3814 return send_and_recv_msgs(drv, msg, get_key_handler, seq);
3815 nla_put_failure:
3816 return -ENOBUFS;
3820 static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
3821 int mode)
3823 struct wpa_driver_nl80211_data *drv = priv;
3824 struct nl_msg *msg;
3825 u8 rates[NL80211_MAX_SUPP_RATES];
3826 u8 rates_len = 0;
3827 int i;
3829 msg = nlmsg_alloc();
3830 if (!msg)
3831 return -ENOMEM;
3833 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
3834 NL80211_CMD_SET_BSS, 0);
3836 for (i = 0; i < NL80211_MAX_SUPP_RATES && basic_rates[i] >= 0; i++)
3837 rates[rates_len++] = basic_rates[i] / 5;
3839 NLA_PUT(msg, NL80211_ATTR_BSS_BASIC_RATES, rates_len, rates);
3841 /* TODO: multi-BSS support */
3842 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
3844 return send_and_recv_msgs(drv, msg, NULL, NULL);
3845 nla_put_failure:
3846 return -ENOBUFS;
3850 /* Set kernel driver on given frequency (MHz) */
3851 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
3853 struct wpa_driver_nl80211_data *drv = priv;
3854 return wpa_driver_nl80211_set_freq(drv, freq->freq, freq->ht_enabled,
3855 freq->sec_channel_offset);
3859 static int i802_set_rts(void *priv, int rts)
3861 struct wpa_driver_nl80211_data *drv = priv;
3862 struct nl_msg *msg;
3863 int ret = -ENOBUFS;
3864 u32 val;
3866 msg = nlmsg_alloc();
3867 if (!msg)
3868 return -ENOMEM;
3870 if (rts >= 2347)
3871 val = (u32) -1;
3872 else
3873 val = rts;
3875 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3876 0, NL80211_CMD_SET_WIPHY, 0);
3877 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3878 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD, val);
3880 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3881 if (!ret)
3882 return 0;
3883 nla_put_failure:
3884 wpa_printf(MSG_DEBUG, "nl80211: Failed to set RTS threshold %d: "
3885 "%d (%s)", rts, ret, strerror(-ret));
3886 return ret;
3890 static int i802_set_frag(void *priv, int frag)
3892 struct wpa_driver_nl80211_data *drv = priv;
3893 struct nl_msg *msg;
3894 int ret = -ENOBUFS;
3895 u32 val;
3897 msg = nlmsg_alloc();
3898 if (!msg)
3899 return -ENOMEM;
3901 if (frag >= 2346)
3902 val = (u32) -1;
3903 else
3904 val = frag;
3906 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3907 0, NL80211_CMD_SET_WIPHY, 0);
3908 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
3909 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD, val);
3911 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
3912 if (!ret)
3913 return 0;
3914 nla_put_failure:
3915 wpa_printf(MSG_DEBUG, "nl80211: Failed to set fragmentation threshold "
3916 "%d: %d (%s)", frag, ret, strerror(-ret));
3917 return ret;
3921 static int i802_flush(void *priv)
3923 struct wpa_driver_nl80211_data *drv = priv;
3924 struct nl_msg *msg;
3926 msg = nlmsg_alloc();
3927 if (!msg)
3928 return -1;
3930 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
3931 0, NL80211_CMD_DEL_STATION, 0);
3934 * XXX: FIX! this needs to flush all VLANs too
3936 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
3937 if_nametoindex(drv->ifname));
3939 return send_and_recv_msgs(drv, msg, NULL, NULL);
3940 nla_put_failure:
3941 return -ENOBUFS;
3945 static int get_sta_handler(struct nl_msg *msg, void *arg)
3947 struct nlattr *tb[NL80211_ATTR_MAX + 1];
3948 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
3949 struct hostap_sta_driver_data *data = arg;
3950 struct nlattr *stats[NL80211_STA_INFO_MAX + 1];
3951 static struct nla_policy stats_policy[NL80211_STA_INFO_MAX + 1] = {
3952 [NL80211_STA_INFO_INACTIVE_TIME] = { .type = NLA_U32 },
3953 [NL80211_STA_INFO_RX_BYTES] = { .type = NLA_U32 },
3954 [NL80211_STA_INFO_TX_BYTES] = { .type = NLA_U32 },
3955 [NL80211_STA_INFO_RX_PACKETS] = { .type = NLA_U32 },
3956 [NL80211_STA_INFO_TX_PACKETS] = { .type = NLA_U32 },
3959 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
3960 genlmsg_attrlen(gnlh, 0), NULL);
3963 * TODO: validate the interface and mac address!
3964 * Otherwise, there's a race condition as soon as
3965 * the kernel starts sending station notifications.
3968 if (!tb[NL80211_ATTR_STA_INFO]) {
3969 wpa_printf(MSG_DEBUG, "sta stats missing!");
3970 return NL_SKIP;
3972 if (nla_parse_nested(stats, NL80211_STA_INFO_MAX,
3973 tb[NL80211_ATTR_STA_INFO],
3974 stats_policy)) {
3975 wpa_printf(MSG_DEBUG, "failed to parse nested attributes!");
3976 return NL_SKIP;
3979 if (stats[NL80211_STA_INFO_INACTIVE_TIME])
3980 data->inactive_msec =
3981 nla_get_u32(stats[NL80211_STA_INFO_INACTIVE_TIME]);
3982 if (stats[NL80211_STA_INFO_RX_BYTES])
3983 data->rx_bytes = nla_get_u32(stats[NL80211_STA_INFO_RX_BYTES]);
3984 if (stats[NL80211_STA_INFO_TX_BYTES])
3985 data->tx_bytes = nla_get_u32(stats[NL80211_STA_INFO_TX_BYTES]);
3986 if (stats[NL80211_STA_INFO_RX_PACKETS])
3987 data->rx_packets =
3988 nla_get_u32(stats[NL80211_STA_INFO_RX_PACKETS]);
3989 if (stats[NL80211_STA_INFO_TX_PACKETS])
3990 data->tx_packets =
3991 nla_get_u32(stats[NL80211_STA_INFO_TX_PACKETS]);
3993 return NL_SKIP;
3996 static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
3997 const u8 *addr)
3999 struct wpa_driver_nl80211_data *drv = priv;
4000 struct nl_msg *msg;
4002 os_memset(data, 0, sizeof(*data));
4003 msg = nlmsg_alloc();
4004 if (!msg)
4005 return -ENOMEM;
4007 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
4008 0, NL80211_CMD_GET_STATION, 0);
4010 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4011 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
4013 return send_and_recv_msgs(drv, msg, get_sta_handler, data);
4014 nla_put_failure:
4015 return -ENOBUFS;
4019 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
4020 int cw_min, int cw_max, int burst_time)
4022 struct wpa_driver_nl80211_data *drv = priv;
4023 struct nl_msg *msg;
4024 struct nlattr *txq, *params;
4026 msg = nlmsg_alloc();
4027 if (!msg)
4028 return -1;
4030 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
4031 0, NL80211_CMD_SET_WIPHY, 0);
4033 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
4035 txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
4036 if (!txq)
4037 goto nla_put_failure;
4039 /* We are only sending parameters for a single TXQ at a time */
4040 params = nla_nest_start(msg, 1);
4041 if (!params)
4042 goto nla_put_failure;
4044 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, queue);
4045 /* Burst time is configured in units of 0.1 msec and TXOP parameter in
4046 * 32 usec, so need to convert the value here. */
4047 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
4048 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
4049 NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
4050 NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
4052 nla_nest_end(msg, params);
4054 nla_nest_end(msg, txq);
4056 if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
4057 return 0;
4058 nla_put_failure:
4059 return -1;
4063 static int i802_set_bss(void *priv, int cts, int preamble, int slot)
4065 struct wpa_driver_nl80211_data *drv = priv;
4066 struct nl_msg *msg;
4068 msg = nlmsg_alloc();
4069 if (!msg)
4070 return -ENOMEM;
4072 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
4073 NL80211_CMD_SET_BSS, 0);
4075 if (cts >= 0)
4076 NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
4077 if (preamble >= 0)
4078 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
4079 if (slot >= 0)
4080 NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
4082 /* TODO: multi-BSS support */
4083 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
4085 return send_and_recv_msgs(drv, msg, NULL, NULL);
4086 nla_put_failure:
4087 return -ENOBUFS;
4091 static int i802_set_cts_protect(void *priv, int value)
4093 return i802_set_bss(priv, value, -1, -1);
4097 static int i802_set_preamble(void *priv, int value)
4099 return i802_set_bss(priv, -1, value, -1);
4103 static int i802_set_short_slot_time(void *priv, int value)
4105 return i802_set_bss(priv, -1, -1, value);
4109 static int i802_set_sta_vlan(void *priv, const u8 *addr,
4110 const char *ifname, int vlan_id)
4112 struct wpa_driver_nl80211_data *drv = priv;
4113 struct nl_msg *msg;
4115 msg = nlmsg_alloc();
4116 if (!msg)
4117 return -ENOMEM;
4119 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
4120 0, NL80211_CMD_SET_STATION, 0);
4122 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
4123 if_nametoindex(drv->ifname));
4124 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
4125 NLA_PUT_U32(msg, NL80211_ATTR_STA_VLAN,
4126 if_nametoindex(ifname));
4128 return send_and_recv_msgs(drv, msg, NULL, NULL);
4129 nla_put_failure:
4130 return -ENOBUFS;
4134 static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val)
4136 struct wpa_driver_nl80211_data *drv = priv;
4137 char name[16];
4139 os_snprintf(name, sizeof(name), "%s.sta%d", drv->ifname, aid);
4140 if (val) {
4141 if (nl80211_create_iface(priv, name, NL80211_IFTYPE_AP_VLAN,
4142 NULL, 1) < 0)
4143 return -1;
4144 linux_set_iface_flags(drv->ioctl_sock, name, 1);
4145 return i802_set_sta_vlan(priv, addr, name, 0);
4146 } else {
4147 i802_set_sta_vlan(priv, addr, drv->ifname, 0);
4148 return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
4149 name);
4154 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
4156 struct wpa_driver_nl80211_data *drv = eloop_ctx;
4157 struct sockaddr_ll lladdr;
4158 unsigned char buf[3000];
4159 int len;
4160 socklen_t fromlen = sizeof(lladdr);
4162 len = recvfrom(sock, buf, sizeof(buf), 0,
4163 (struct sockaddr *)&lladdr, &fromlen);
4164 if (len < 0) {
4165 perror("recv");
4166 return;
4169 if (have_ifidx(drv, lladdr.sll_ifindex))
4170 drv_event_eapol_rx(drv->ctx, lladdr.sll_addr, buf, len);
4174 static int i802_get_inact_sec(void *priv, const u8 *addr)
4176 struct hostap_sta_driver_data data;
4177 int ret;
4179 data.inactive_msec = (unsigned long) -1;
4180 ret = i802_read_sta_data(priv, &data, addr);
4181 if (ret || data.inactive_msec == (unsigned long) -1)
4182 return -1;
4183 return data.inactive_msec / 1000;
4187 static int i802_sta_clear_stats(void *priv, const u8 *addr)
4189 #if 0
4190 /* TODO */
4191 #endif
4192 return 0;
4196 static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
4197 int reason)
4199 struct wpa_driver_nl80211_data *drv = priv;
4200 struct ieee80211_mgmt mgmt;
4202 memset(&mgmt, 0, sizeof(mgmt));
4203 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
4204 WLAN_FC_STYPE_DEAUTH);
4205 memcpy(mgmt.da, addr, ETH_ALEN);
4206 memcpy(mgmt.sa, own_addr, ETH_ALEN);
4207 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
4208 mgmt.u.deauth.reason_code = host_to_le16(reason);
4209 return wpa_driver_nl80211_send_mlme(drv, (u8 *) &mgmt,
4210 IEEE80211_HDRLEN +
4211 sizeof(mgmt.u.deauth));
4215 static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
4216 int reason)
4218 struct wpa_driver_nl80211_data *drv = priv;
4219 struct ieee80211_mgmt mgmt;
4221 memset(&mgmt, 0, sizeof(mgmt));
4222 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
4223 WLAN_FC_STYPE_DISASSOC);
4224 memcpy(mgmt.da, addr, ETH_ALEN);
4225 memcpy(mgmt.sa, own_addr, ETH_ALEN);
4226 memcpy(mgmt.bssid, own_addr, ETH_ALEN);
4227 mgmt.u.disassoc.reason_code = host_to_le16(reason);
4228 return wpa_driver_nl80211_send_mlme(drv, (u8 *) &mgmt,
4229 IEEE80211_HDRLEN +
4230 sizeof(mgmt.u.disassoc));
4234 static void *i802_init(struct hostapd_data *hapd,
4235 struct wpa_init_params *params)
4237 struct wpa_driver_nl80211_data *drv;
4238 size_t i;
4240 drv = wpa_driver_nl80211_init(hapd, params->ifname);
4241 if (drv == NULL)
4242 return NULL;
4244 drv->bss.ifindex = drv->ifindex;
4246 drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
4247 drv->if_indices = drv->default_if_indices;
4248 for (i = 0; i < params->num_bridge; i++) {
4249 if (params->bridge[i])
4250 add_ifidx(drv, if_nametoindex(params->bridge[i]));
4253 /* start listening for EAPOL on the default AP interface */
4254 add_ifidx(drv, drv->ifindex);
4256 if (linux_set_iface_flags(drv->ioctl_sock, drv->ifname, 0))
4257 goto failed;
4259 if (params->bssid) {
4260 if (linux_set_ifhwaddr(drv->ioctl_sock, drv->ifname,
4261 params->bssid))
4262 goto failed;
4265 if (wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_AP)) {
4266 wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
4267 "into AP mode", drv->ifname);
4268 goto failed;
4271 if (linux_set_iface_flags(drv->ioctl_sock, drv->ifname, 1))
4272 goto failed;
4274 drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
4275 if (drv->eapol_sock < 0) {
4276 perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
4277 goto failed;
4280 if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
4282 printf("Could not register read socket for eapol\n");
4283 goto failed;
4286 if (linux_get_ifhwaddr(drv->ioctl_sock, drv->ifname, params->own_addr))
4287 goto failed;
4289 return drv;
4291 failed:
4292 nl80211_remove_monitor_interface(drv);
4293 if (drv->ioctl_sock >= 0)
4294 close(drv->ioctl_sock);
4296 genl_family_put(drv->nl80211);
4297 nl_cache_free(drv->nl_cache);
4298 nl_handle_destroy(drv->nl_handle);
4299 nl_cb_put(drv->nl_cb);
4301 os_free(drv);
4302 return NULL;
4306 static void i802_deinit(void *priv)
4308 wpa_driver_nl80211_deinit(priv);
4311 #endif /* HOSTAPD */
4314 static enum nl80211_iftype wpa_driver_nl80211_if_type(
4315 enum wpa_driver_if_type type)
4317 switch (type) {
4318 case WPA_IF_STATION:
4319 return NL80211_IFTYPE_STATION;
4320 case WPA_IF_AP_VLAN:
4321 return NL80211_IFTYPE_AP_VLAN;
4322 case WPA_IF_AP_BSS:
4323 return NL80211_IFTYPE_AP;
4325 return -1;
4329 static int wpa_driver_nl80211_if_add(const char *iface, void *priv,
4330 enum wpa_driver_if_type type,
4331 const char *ifname, const u8 *addr,
4332 void *bss_ctx)
4334 struct wpa_driver_nl80211_data *drv = priv;
4335 int ifidx;
4336 #ifdef HOSTAPD
4337 struct i802_bss *bss = NULL;
4339 if (type == WPA_IF_AP_BSS) {
4340 bss = os_zalloc(sizeof(*bss));
4341 if (bss == NULL)
4342 return -1;
4344 #endif /* HOSTAPD */
4346 ifidx = nl80211_create_iface(drv, ifname,
4347 wpa_driver_nl80211_if_type(type), addr,
4349 if (ifidx < 0) {
4350 #ifdef HOSTAPD
4351 os_free(bss);
4352 #endif /* HOSTAPD */
4353 return -1;
4356 #ifdef HOSTAPD
4357 if (type == WPA_IF_AP_BSS) {
4358 if (linux_set_iface_flags(drv->ioctl_sock, ifname, 1)) {
4359 nl80211_remove_iface(drv, ifidx);
4360 os_free(bss);
4361 return -1;
4363 bss->ifindex = ifidx;
4364 bss->next = drv->bss.next;
4365 drv->bss.next = bss;
4367 #endif /* HOSTAPD */
4369 return 0;
4373 static int wpa_driver_nl80211_if_remove(void *priv,
4374 enum wpa_driver_if_type type,
4375 const char *ifname)
4377 struct wpa_driver_nl80211_data *drv = priv;
4378 int ifindex = if_nametoindex(ifname);
4380 nl80211_remove_iface(drv, ifindex);
4382 #ifdef HOSTAPD
4383 if (type == WPA_IF_AP_BSS) {
4384 struct i802_bss *bss, *prev;
4385 prev = &drv->bss;
4386 bss = drv->bss.next;
4387 while (bss) {
4388 if (ifindex == bss->ifindex) {
4389 prev->next = bss->next;
4390 os_free(bss);
4391 break;
4393 prev = bss;
4394 bss = bss->next;
4397 #endif /* HOSTAPD */
4399 return 0;
4403 static int cookie_handler(struct nl_msg *msg, void *arg)
4405 struct nlattr *tb[NL80211_ATTR_MAX + 1];
4406 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
4407 u64 *cookie = arg;
4408 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0),
4409 genlmsg_attrlen(gnlh, 0), NULL);
4410 if (tb[NL80211_ATTR_COOKIE])
4411 *cookie = nla_get_u64(tb[NL80211_ATTR_COOKIE]);
4412 return NL_SKIP;
4416 static int wpa_driver_nl80211_remain_on_channel(void *priv, unsigned int freq,
4417 unsigned int duration)
4419 struct wpa_driver_nl80211_data *drv = priv;
4420 struct nl_msg *msg;
4421 int ret;
4422 u64 cookie;
4424 msg = nlmsg_alloc();
4425 if (!msg)
4426 return -1;
4428 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
4429 NL80211_CMD_REMAIN_ON_CHANNEL, 0);
4431 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4432 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
4433 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
4435 cookie = 0;
4436 ret = send_and_recv_msgs(drv, msg, cookie_handler, &cookie);
4437 if (ret == 0) {
4438 wpa_printf(MSG_DEBUG, "nl80211: Remain-on-channel cookie "
4439 "0x%llx for freq=%u MHz duration=%u",
4440 (long long unsigned int) cookie, freq, duration);
4441 drv->remain_on_chan_cookie = cookie;
4442 return 0;
4444 wpa_printf(MSG_DEBUG, "nl80211: Failed to request remain-on-channel "
4445 "(freq=%d): %d (%s)", freq, ret, strerror(-ret));
4446 nla_put_failure:
4447 return -1;
4451 static int wpa_driver_nl80211_cancel_remain_on_channel(void *priv)
4453 struct wpa_driver_nl80211_data *drv = priv;
4454 struct nl_msg *msg;
4455 int ret;
4457 if (!drv->pending_remain_on_chan) {
4458 wpa_printf(MSG_DEBUG, "nl80211: No pending remain-on-channel "
4459 "to cancel");
4460 return -1;
4463 wpa_printf(MSG_DEBUG, "nl80211: Cancel remain-on-channel with cookie "
4464 "0x%llx",
4465 (long long unsigned int) drv->remain_on_chan_cookie);
4467 msg = nlmsg_alloc();
4468 if (!msg)
4469 return -1;
4471 genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
4472 NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL, 0);
4474 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
4475 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, drv->remain_on_chan_cookie);
4477 ret = send_and_recv_msgs(drv, msg, NULL, NULL);
4478 if (ret == 0)
4479 return 0;
4480 wpa_printf(MSG_DEBUG, "nl80211: Failed to cancel remain-on-channel: "
4481 "%d (%s)", ret, strerror(-ret));
4482 nla_put_failure:
4483 return -1;
4487 static void wpa_driver_nl80211_probe_req_report_timeout(void *eloop_ctx,
4488 void *timeout_ctx)
4490 struct wpa_driver_nl80211_data *drv = eloop_ctx;
4491 if (drv->monitor_ifidx < 0)
4492 return; /* monitor interface already removed */
4494 if (drv->nlmode != NL80211_IFTYPE_STATION)
4495 return; /* not in station mode anymore */
4497 if (drv->probe_req_report)
4498 return; /* reporting enabled */
4500 wpa_printf(MSG_DEBUG, "nl80211: Remove monitor interface due to no "
4501 "Probe Request reporting needed anymore");
4502 nl80211_remove_monitor_interface(drv);
4506 static int wpa_driver_nl80211_probe_req_report(void *priv, int report)
4508 struct wpa_driver_nl80211_data *drv = priv;
4510 if (drv->nlmode != NL80211_IFTYPE_STATION) {
4511 wpa_printf(MSG_DEBUG, "nl80211: probe_req_report control only "
4512 "allowed in station mode (iftype=%d)",
4513 drv->nlmode);
4514 return -1;
4516 drv->probe_req_report = report;
4518 if (report) {
4519 eloop_cancel_timeout(
4520 wpa_driver_nl80211_probe_req_report_timeout,
4521 drv, NULL);
4522 if (drv->monitor_ifidx < 0 &&
4523 nl80211_create_monitor_interface(drv))
4524 return -1;
4525 } else {
4527 * It takes a while to remove the monitor interface, so try to
4528 * avoid doing this if it is needed again shortly. Instead,
4529 * schedule the interface to be removed later if no need for it
4530 * is seen.
4532 wpa_printf(MSG_DEBUG, "nl80211: Scheduling monitor interface "
4533 "to be removed after 10 seconds of no use");
4534 eloop_register_timeout(
4535 10, 0, wpa_driver_nl80211_probe_req_report_timeout,
4536 drv, NULL);
4539 return 0;
4543 static int wpa_driver_nl80211_alloc_interface_addr(void *priv, u8 *addr)
4545 struct wpa_driver_nl80211_data *drv = priv;
4547 if (linux_get_ifhwaddr(drv->ioctl_sock, drv->ifname, addr) < 0)
4548 return -1;
4550 if (addr[0] & 0x02) {
4551 /* TODO: add support for generating multiple addresses */
4552 addr[0] ^= 0x80;
4553 } else
4554 addr[0] = 0x02; /* locally administered */
4556 return 0;
4560 static void wpa_driver_nl80211_release_interface_addr(void *priv,
4561 const u8 *addr)
4563 /* TODO: keep list of allocated address and release them here */
4567 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
4568 .name = "nl80211",
4569 .desc = "Linux nl80211/cfg80211",
4570 .get_bssid = wpa_driver_nl80211_get_bssid,
4571 .get_ssid = wpa_driver_nl80211_get_ssid,
4572 .set_key = wpa_driver_nl80211_set_key,
4573 .scan2 = wpa_driver_nl80211_scan,
4574 .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
4575 .deauthenticate = wpa_driver_nl80211_deauthenticate,
4576 .disassociate = wpa_driver_nl80211_disassociate,
4577 .authenticate = wpa_driver_nl80211_authenticate,
4578 .associate = wpa_driver_nl80211_associate,
4579 .init = wpa_driver_nl80211_init,
4580 .deinit = wpa_driver_nl80211_deinit,
4581 .get_capa = wpa_driver_nl80211_get_capa,
4582 .set_operstate = wpa_driver_nl80211_set_operstate,
4583 .set_supp_port = wpa_driver_nl80211_set_supp_port,
4584 .set_country = wpa_driver_nl80211_set_country,
4585 .set_beacon = wpa_driver_nl80211_set_beacon,
4586 .if_add = wpa_driver_nl80211_if_add,
4587 .if_remove = wpa_driver_nl80211_if_remove,
4588 .send_mlme = wpa_driver_nl80211_send_mlme,
4589 .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
4590 .sta_add = wpa_driver_nl80211_sta_add,
4591 .sta_remove = wpa_driver_nl80211_sta_remove,
4592 .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
4593 .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
4594 #ifdef HOSTAPD
4595 .hapd_init = i802_init,
4596 .hapd_deinit = i802_deinit,
4597 .get_seqnum = i802_get_seqnum,
4598 .flush = i802_flush,
4599 .read_sta_data = i802_read_sta_data,
4600 .sta_deauth = i802_sta_deauth,
4601 .sta_disassoc = i802_sta_disassoc,
4602 .get_inact_sec = i802_get_inact_sec,
4603 .sta_clear_stats = i802_sta_clear_stats,
4604 .set_freq = i802_set_freq,
4605 .set_rts = i802_set_rts,
4606 .set_frag = i802_set_frag,
4607 .set_rate_sets = i802_set_rate_sets,
4608 .set_cts_protect = i802_set_cts_protect,
4609 .set_preamble = i802_set_preamble,
4610 .set_short_slot_time = i802_set_short_slot_time,
4611 .set_tx_queue_params = i802_set_tx_queue_params,
4612 .set_sta_vlan = i802_set_sta_vlan,
4613 .set_wds_sta = i802_set_wds_sta,
4614 #endif /* HOSTAPD */
4615 .remain_on_channel = wpa_driver_nl80211_remain_on_channel,
4616 .cancel_remain_on_channel =
4617 wpa_driver_nl80211_cancel_remain_on_channel,
4618 .probe_req_report = wpa_driver_nl80211_probe_req_report,
4619 .alloc_interface_addr = wpa_driver_nl80211_alloc_interface_addr,
4620 .release_interface_addr = wpa_driver_nl80211_release_interface_addr,