]> git.ipfire.org Git - thirdparty/hostap.git/blobdiff - src/drivers/driver_nl80211.c
Move struct hostapd_frame_info definition away from driver API
[thirdparty/hostap.git] / src / drivers / driver_nl80211.c
index f606546bebcf24e59b03dc7c0ed588b90e73168e..f4e45e77aee910bdb4c9b5a80bff583e2ead9263 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Driver interaction with Linux nl80211/cfg80211
- * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2002-2010, Jouni Malinen <j@w1.fi>
  * Copyright (c) 2003-2004, Instant802 Networks, Inc.
  * Copyright (c) 2005-2006, Devicescape Software, Inc.
  * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
 #include <netlink/genl/genl.h>
 #include <netlink/genl/family.h>
 #include <netlink/genl/ctrl.h>
+#include <netpacket/packet.h>
+#include <linux/filter.h>
 #include "nl80211_copy.h"
-#ifndef NO_WEXT
-#include "wireless_copy.h"
-#endif /* NO_WEXT */
 
 #include "common.h"
-#include "driver.h"
 #include "eloop.h"
-#include "ieee802_11_defs.h"
-
-#if defined(CONFIG_AP) || defined(HOSTAPD)
-#include <netpacket/packet.h>
-#include <linux/filter.h>
+#include "common/ieee802_11_defs.h"
+#include "netlink.h"
 #include "radiotap.h"
 #include "radiotap_iter.h"
-
-#include "../../hostapd/hostapd_defs.h"
-#include "../../hostapd/sta_flags.h"
-#endif /* CONFIG_AP || HOSTAPD */
-
-#ifdef HOSTAPD
-#include "ieee802_11_common.h"
-#endif /* HOSTAPD */
+#include "driver.h"
 
 #ifdef CONFIG_LIBNL20
 /* libnl 2.0 compatibility code */
@@ -77,7 +65,7 @@ struct i802_bss {
 
 struct wpa_driver_nl80211_data {
        void *ctx;
-       int link_event_sock;
+       struct netlink_data *netlink;
        int ioctl_sock; /* socket for ioctl() use */
        char ifname[IFNAMSIZ + 1];
        int ifindex;
@@ -96,12 +84,14 @@ struct wpa_driver_nl80211_data {
        struct nl_cb *nl_cb;
        struct genl_family *nl80211;
 
+       u8 auth_bssid[ETH_ALEN];
        u8 bssid[ETH_ALEN];
        int associated;
        u8 ssid[32];
        size_t ssid_len;
        int nlmode;
        int ap_scan_as_station;
+       unsigned int assoc_freq;
 
        int monitor_sock;
        int monitor_ifidx;
@@ -128,25 +118,20 @@ static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
 static int wpa_driver_nl80211_set_mode(void *priv, int mode);
 static int
 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv);
-
-#if defined(CONFIG_AP) || defined(HOSTAPD)
+static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
+                                  const u8 *addr, int cmd, u16 reason_code);
 static void nl80211_remove_monitor_interface(
        struct wpa_driver_nl80211_data *drv);
-#endif /* CONFIG_AP || HOSTAPD */
-
-#ifdef CONFIG_AP
-static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
-                                int ifidx);
-#endif /* CONFIG_AP */
 
 #ifdef HOSTAPD
 static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
 static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
 static struct i802_bss * get_bss(struct wpa_driver_nl80211_data *drv,
                                 int ifindex);
-static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
-                                int ifidx);
 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq);
+static int wpa_driver_nl80211_if_remove(void *priv,
+                                       enum wpa_driver_if_type type,
+                                       const char *ifname);
 #endif /* HOSTAPD */
 
 
@@ -323,95 +308,6 @@ static int set_ifhwaddr(struct wpa_driver_nl80211_data *drv,
 #endif /* HOSTAPD */
 
 
-#ifndef HOSTAPD
-
-static int wpa_driver_nl80211_send_oper_ifla(
-       struct wpa_driver_nl80211_data *drv,
-       int linkmode, int operstate)
-{
-       struct {
-               struct nlmsghdr hdr;
-               struct ifinfomsg ifinfo;
-               char opts[16];
-       } req;
-       struct rtattr *rta;
-       static int nl_seq;
-       ssize_t ret;
-
-       os_memset(&req, 0, sizeof(req));
-
-       req.hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
-       req.hdr.nlmsg_type = RTM_SETLINK;
-       req.hdr.nlmsg_flags = NLM_F_REQUEST;
-       req.hdr.nlmsg_seq = ++nl_seq;
-       req.hdr.nlmsg_pid = 0;
-
-       req.ifinfo.ifi_family = AF_UNSPEC;
-       req.ifinfo.ifi_type = 0;
-       req.ifinfo.ifi_index = drv->ifindex;
-       req.ifinfo.ifi_flags = 0;
-       req.ifinfo.ifi_change = 0;
-
-       if (linkmode != -1) {
-               rta = aliasing_hide_typecast(
-                       ((char *) &req + NLMSG_ALIGN(req.hdr.nlmsg_len)),
-                       struct rtattr);
-               rta->rta_type = IFLA_LINKMODE;
-               rta->rta_len = RTA_LENGTH(sizeof(char));
-               *((char *) RTA_DATA(rta)) = linkmode;
-               req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
-                       RTA_LENGTH(sizeof(char));
-       }
-       if (operstate != -1) {
-               rta = (struct rtattr *)
-                       ((char *) &req + NLMSG_ALIGN(req.hdr.nlmsg_len));
-               rta->rta_type = IFLA_OPERSTATE;
-               rta->rta_len = RTA_LENGTH(sizeof(char));
-               *((char *) RTA_DATA(rta)) = operstate;
-               req.hdr.nlmsg_len = NLMSG_ALIGN(req.hdr.nlmsg_len) +
-                       RTA_LENGTH(sizeof(char));
-       }
-
-       wpa_printf(MSG_DEBUG, "nl80211: Operstate: linkmode=%d, operstate=%d",
-                  linkmode, operstate);
-
-       ret = send(drv->link_event_sock, &req, req.hdr.nlmsg_len, 0);
-       if (ret < 0) {
-               wpa_printf(MSG_DEBUG, "nl80211: Sending operstate IFLA failed:"
-                          " %s (assume operstate is not supported)",
-                          strerror(errno));
-       }
-
-       return ret < 0 ? -1 : 0;
-}
-
-
-#ifndef NO_WEXT
-static int wpa_driver_nl80211_set_auth_param(
-       struct wpa_driver_nl80211_data *drv, int idx, u32 value)
-{
-       struct iwreq iwr;
-       int ret = 0;
-
-       os_memset(&iwr, 0, sizeof(iwr));
-       os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
-       iwr.u.param.flags = idx & IW_AUTH_INDEX;
-       iwr.u.param.value = value;
-
-       if (ioctl(drv->ioctl_sock, SIOCSIWAUTH, &iwr) < 0) {
-               if (errno != EOPNOTSUPP) {
-                       wpa_printf(MSG_DEBUG, "WEXT: SIOCSIWAUTH(param %d "
-                                  "value 0x%x) failed: %s)",
-                                  idx, value, strerror(errno));
-               }
-               ret = errno == EOPNOTSUPP ? -2 : -1;
-       }
-
-       return ret;
-}
-#endif /* NO_WEXT */
-
-
 static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
 {
        struct wpa_driver_nl80211_data *drv = priv;
@@ -433,8 +329,7 @@ static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
 
 
 static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
-                                         void *ctx, char *buf, size_t len,
-                                         int del)
+                                         char *buf, size_t len, int del)
 {
        union wpa_event_data event;
 
@@ -457,26 +352,18 @@ static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
                        drv->if_removed = 0;
        }
 
-       wpa_supplicant_event(ctx, EVENT_INTERFACE_STATUS, &event);
+       wpa_supplicant_event(drv->ctx, EVENT_INTERFACE_STATUS, &event);
 }
 
 
 static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
-                                        struct nlmsghdr *h)
+                                        u8 *buf, size_t len)
 {
-       struct ifinfomsg *ifi;
-       int attrlen, _nlmsg_len, rta_len;
+       int attrlen, rta_len;
        struct rtattr *attr;
 
-       ifi = NLMSG_DATA(h);
-
-       _nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
-
-       attrlen = h->nlmsg_len - _nlmsg_len;
-       if (attrlen < 0)
-               return 0;
-
-       attr = (struct rtattr *) (((char *) ifi) + _nlmsg_len);
+       attrlen = len;
+       attr = (struct rtattr *) buf;
 
        rta_len = RTA_ALIGN(sizeof(struct rtattr));
        while (RTA_OK(attr, attrlen)) {
@@ -495,12 +382,12 @@ static int wpa_driver_nl80211_own_ifname(struct wpa_driver_nl80211_data *drv,
 
 
 static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
-                                         int ifindex, struct nlmsghdr *h)
+                                         int ifindex, u8 *buf, size_t len)
 {
        if (drv->ifindex == ifindex)
                return 1;
 
-       if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, h)) {
+       if (drv->if_removed && wpa_driver_nl80211_own_ifname(drv, buf, len)) {
                drv->ifindex = if_nametoindex(drv->ifname);
                wpa_printf(MSG_DEBUG, "nl80211: Update ifindex for a removed "
                           "interface");
@@ -512,20 +399,15 @@ static int wpa_driver_nl80211_own_ifindex(struct wpa_driver_nl80211_data *drv,
 }
 
 
-static void wpa_driver_nl80211_event_rtm_newlink(struct wpa_driver_nl80211_data *drv,
-                                             void *ctx, struct nlmsghdr *h,
-                                             size_t len)
+static void wpa_driver_nl80211_event_rtm_newlink(void *ctx,
+                                                struct ifinfomsg *ifi,
+                                                u8 *buf, size_t len)
 {
-       struct ifinfomsg *ifi;
-       int attrlen, _nlmsg_len, rta_len;
-       struct rtattr * attr;
-
-       if (len < sizeof(*ifi))
-               return;
-
-       ifi = NLMSG_DATA(h);
+       struct wpa_driver_nl80211_data *drv = ctx;
+       int attrlen, rta_len;
+       struct rtattr *attr;
 
-       if (!wpa_driver_nl80211_own_ifindex(drv, ifi->ifi_index, h)) {
+       if (!wpa_driver_nl80211_own_ifindex(drv, ifi->ifi_index, buf, len)) {
                wpa_printf(MSG_DEBUG, "Ignore event for foreign ifindex %d",
                           ifi->ifi_index);
                return;
@@ -547,21 +429,16 @@ static void wpa_driver_nl80211_event_rtm_newlink(struct wpa_driver_nl80211_data
        if (drv->operstate == 1 &&
            (ifi->ifi_flags & (IFF_LOWER_UP | IFF_DORMANT)) == IFF_LOWER_UP &&
            !(ifi->ifi_flags & IFF_RUNNING))
-               wpa_driver_nl80211_send_oper_ifla(drv, -1, IF_OPER_UP);
-
-       _nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
-
-       attrlen = h->nlmsg_len - _nlmsg_len;
-       if (attrlen < 0)
-               return;
-
-       attr = (struct rtattr *) (((char *) ifi) + _nlmsg_len);
+               netlink_send_oper_ifla(drv->netlink, drv->ifindex,
+                                      -1, IF_OPER_UP);
 
+       attrlen = len;
+       attr = (struct rtattr *) buf;
        rta_len = RTA_ALIGN(sizeof(struct rtattr));
        while (RTA_OK(attr, attrlen)) {
                if (attr->rta_type == IFLA_IFNAME) {
                        wpa_driver_nl80211_event_link(
-                               drv, ctx,
+                               drv,
                                ((char *) attr) + rta_len,
                                attr->rta_len - rta_len, 0);
                }
@@ -570,32 +447,22 @@ static void wpa_driver_nl80211_event_rtm_newlink(struct wpa_driver_nl80211_data
 }
 
 
-static void wpa_driver_nl80211_event_rtm_dellink(struct wpa_driver_nl80211_data *drv,
-                                             void *ctx, struct nlmsghdr *h,
-                                             size_t len)
+static void wpa_driver_nl80211_event_rtm_dellink(void *ctx,
+                                                struct ifinfomsg *ifi,
+                                                u8 *buf, size_t len)
 {
-       struct ifinfomsg *ifi;
-       int attrlen, _nlmsg_len, rta_len;
-       struct rtattr * attr;
-
-       if (len < sizeof(*ifi))
-               return;
-
-       ifi = NLMSG_DATA(h);
-
-       _nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
-
-       attrlen = h->nlmsg_len - _nlmsg_len;
-       if (attrlen < 0)
-               return;
+       struct wpa_driver_nl80211_data *drv = ctx;
+       int attrlen, rta_len;
+       struct rtattr *attr;
 
-       attr = (struct rtattr *) (((char *) ifi) + _nlmsg_len);
+       attrlen = len;
+       attr = (struct rtattr *) buf;
 
        rta_len = RTA_ALIGN(sizeof(struct rtattr));
        while (RTA_OK(attr, attrlen)) {
                if (attr->rta_type == IFLA_IFNAME) {
                        wpa_driver_nl80211_event_link(
-                               drv, ctx,
+                               drv,
                                ((char *) attr) + rta_len,
                                attr->rta_len - rta_len, 1);
                }
@@ -604,73 +471,6 @@ static void wpa_driver_nl80211_event_rtm_dellink(struct wpa_driver_nl80211_data
 }
 
 
-static void wpa_driver_nl80211_event_receive_link(int sock, void *eloop_ctx,
-                                                 void *sock_ctx)
-{
-       char buf[8192];
-       int left;
-       struct sockaddr_nl from;
-       socklen_t fromlen;
-       struct nlmsghdr *h;
-       int max_events = 10;
-
-try_again:
-       fromlen = sizeof(from);
-       left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
-                       (struct sockaddr *) &from, &fromlen);
-       if (left < 0) {
-               if (errno != EINTR && errno != EAGAIN)
-                       perror("recvfrom(netlink)");
-               return;
-       }
-
-       h = (struct nlmsghdr *) buf;
-       while (left >= (int) sizeof(*h)) {
-               int len, plen;
-
-               len = h->nlmsg_len;
-               plen = len - sizeof(*h);
-               if (len > left || plen < 0) {
-                       wpa_printf(MSG_DEBUG, "Malformed netlink message: "
-                                  "len=%d left=%d plen=%d",
-                                  len, left, plen);
-                       break;
-               }
-
-               switch (h->nlmsg_type) {
-               case RTM_NEWLINK:
-                       wpa_driver_nl80211_event_rtm_newlink(eloop_ctx, sock_ctx,
-                                                         h, plen);
-                       break;
-               case RTM_DELLINK:
-                       wpa_driver_nl80211_event_rtm_dellink(eloop_ctx, sock_ctx,
-                                                         h, plen);
-                       break;
-               }
-
-               len = NLMSG_ALIGN(len);
-               left -= len;
-               h = (struct nlmsghdr *) ((char *) h + len);
-       }
-
-       if (left > 0) {
-               wpa_printf(MSG_DEBUG, "%d extra bytes in the end of netlink "
-                          "message", left);
-       }
-
-       if (--max_events > 0) {
-               /*
-                * Try to receive all events in one eloop call in order to
-                * limit race condition on cases where AssocInfo event, Assoc
-                * event, and EAPOL frames are received more or less at the
-                * same time. We want to process the event messages first
-                * before starting EAPOL processing.
-                */
-               goto try_again;
-       }
-}
-
-
 static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
                            const u8 *frame, size_t len)
 {
@@ -684,6 +484,7 @@ static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
                return;
        }
 
+       os_memcpy(drv->auth_bssid, mgmt->sa, ETH_ALEN);
        os_memset(&event, 0, sizeof(event));
        os_memcpy(event.auth.peer, mgmt->sa, ETH_ALEN);
        event.auth.auth_type = le_to_host16(mgmt->u.auth.auth_alg);
@@ -736,9 +537,12 @@ static void mlme_event_assoc(struct wpa_driver_nl80211_data *drv,
                        len - 24 - sizeof(mgmt->u.assoc_resp);
        }
 
+       event.assoc_info.freq = drv->assoc_freq;
+
        wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
 }
 
+
 static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
                               enum nl80211_commands cmd, struct nlattr *status,
                               struct nlattr *addr, struct nlattr *req_ie,
@@ -784,6 +588,7 @@ static void mlme_event_connect(struct wpa_driver_nl80211_data *drv,
        wpa_supplicant_event(drv->ctx, EVENT_ASSOC, &event);
 }
 
+
 static void mlme_timeout_event(struct wpa_driver_nl80211_data *drv,
                               enum nl80211_commands cmd, struct nlattr *addr)
 {
@@ -848,8 +653,6 @@ static void mlme_event(struct wpa_driver_nl80211_data *drv,
        }
 }
 
-#endif /* HOSTAPD */
-
 
 static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv,
                                           struct nlattr *tb[])
@@ -887,6 +690,64 @@ static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv,
 }
 
 
+static void mlme_event_join_ibss(struct wpa_driver_nl80211_data *drv,
+                                struct nlattr *tb[])
+{
+       if (tb[NL80211_ATTR_MAC] == NULL) {
+               wpa_printf(MSG_DEBUG, "nl80211: No address in IBSS joined "
+                          "event");
+               return;
+       }
+       os_memcpy(drv->bssid, nla_data(tb[NL80211_ATTR_MAC]), ETH_ALEN);
+       drv->associated = 1;
+       wpa_printf(MSG_DEBUG, "nl80211: IBSS " MACSTR " joined",
+                  MAC2STR(drv->bssid));
+
+       wpa_supplicant_event(drv->ctx, EVENT_ASSOC, NULL);
+}
+
+
+static void send_scan_event(struct wpa_driver_nl80211_data *drv, int aborted,
+                           struct nlattr *tb[])
+{
+       union wpa_event_data event;
+       struct nlattr *nl;
+       int rem;
+       struct scan_info *info;
+#define MAX_REPORT_FREQS 50
+       int freqs[MAX_REPORT_FREQS];
+       int num_freqs = 0;
+
+       os_memset(&event, 0, sizeof(event));
+       info = &event.scan_info;
+       info->aborted = aborted;
+
+       if (tb[NL80211_ATTR_SCAN_SSIDS]) {
+               nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_SSIDS], rem) {
+                       struct wpa_driver_scan_ssid *s =
+                               &info->ssids[info->num_ssids];
+                       s->ssid = nla_data(nl);
+                       s->ssid_len = nla_len(nl);
+                       info->num_ssids++;
+                       if (info->num_ssids == WPAS_MAX_SCAN_SSIDS)
+                               break;
+               }
+       }
+       if (tb[NL80211_ATTR_SCAN_FREQUENCIES]) {
+               nla_for_each_nested(nl, tb[NL80211_ATTR_SCAN_FREQUENCIES], rem)
+               {
+                       freqs[num_freqs] = nla_get_u32(nl);
+                       num_freqs++;
+                       if (num_freqs == MAX_REPORT_FREQS - 1)
+                               break;
+               }
+               info->freqs = freqs;
+               info->num_freqs = num_freqs;
+       }
+       wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, &event);
+}
+
+
 static int process_event(struct nl_msg *msg, void *arg)
 {
        struct wpa_driver_nl80211_data *drv = arg;
@@ -922,7 +783,7 @@ static int process_event(struct nl_msg *msg, void *arg)
                drv->scan_complete_events = 1;
                eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
                                     drv->ctx);
-               wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, NULL);
+               send_scan_event(drv, 0, tb);
                break;
        case NL80211_CMD_SCAN_ABORTED:
                wpa_printf(MSG_DEBUG, "nl80211: Scan aborted");
@@ -932,9 +793,8 @@ static int process_event(struct nl_msg *msg, void *arg)
                 */
                eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv,
                                     drv->ctx);
-               wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, NULL);
+               send_scan_event(drv, 1, tb);
                break;
-#ifndef HOSTAPD
        case NL80211_CMD_AUTHENTICATE:
        case NL80211_CMD_ASSOCIATE:
        case NL80211_CMD_DEAUTHENTICATE:
@@ -963,10 +823,12 @@ static int process_event(struct nl_msg *msg, void *arg)
                drv->associated = 0;
                wpa_supplicant_event(drv->ctx, EVENT_DISASSOC, NULL);
                break;
-#endif /* HOSTAPD */
        case NL80211_CMD_MICHAEL_MIC_FAILURE:
                mlme_event_michael_mic_failure(drv, tb);
                break;
+       case NL80211_CMD_JOIN_IBSS:
+               mlme_event_join_ibss(drv, tb);
+               break;
        default:
                wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
                           "(cmd=%d)", gnlh->cmd);
@@ -1158,6 +1020,9 @@ static int wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
                WPA_DRIVER_CAPA_ENC_WEP104 |
                WPA_DRIVER_CAPA_ENC_TKIP |
                WPA_DRIVER_CAPA_ENC_CCMP;
+       drv->capa.auth = WPA_DRIVER_AUTH_OPEN |
+               WPA_DRIVER_AUTH_SHARED |
+               WPA_DRIVER_AUTH_LEAP;
 
        drv->capa.max_scan_ssids = info.max_scan_ssids;
        if (info.ap_supported)
@@ -1292,39 +1157,6 @@ err1:
 }
 
 
-static int wpa_driver_nl80211_init_link_events(
-       struct wpa_driver_nl80211_data *drv)
-{
-#ifdef HOSTAPD
-       return 0;
-#else /* HOSTAPD */
-       int s;
-       struct sockaddr_nl local;
-
-       s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
-       if (s < 0) {
-               perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
-               return -1;
-       }
-
-       os_memset(&local, 0, sizeof(local));
-       local.nl_family = AF_NETLINK;
-       local.nl_groups = RTMGRP_LINK;
-       if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
-               perror("bind(netlink)");
-               close(s);
-               return -1;
-       }
-
-       eloop_register_read_sock(s, wpa_driver_nl80211_event_receive_link, drv,
-                                drv->ctx);
-       drv->link_event_sock = s;
-
-       return 0;
-#endif /* HOSTAPD */
-}
-
-
 /**
  * wpa_driver_nl80211_init - Initialize nl80211 driver interface
  * @ctx: context to be used when calling wpa_supplicant functions,
@@ -1335,6 +1167,7 @@ static int wpa_driver_nl80211_init_link_events(
 static void * wpa_driver_nl80211_init(void *ctx, const char *ifname)
 {
        struct wpa_driver_nl80211_data *drv;
+       struct netlink_config *cfg;
 
        drv = os_zalloc(sizeof(*drv));
        if (drv == NULL)
@@ -1343,7 +1176,6 @@ static void * wpa_driver_nl80211_init(void *ctx, const char *ifname)
        os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
        drv->monitor_ifidx = -1;
        drv->monitor_sock = -1;
-       drv->link_event_sock = -1;
        drv->ioctl_sock = -1;
 
        if (wpa_driver_nl80211_init_nl(drv, ctx)) {
@@ -1357,17 +1189,24 @@ static void * wpa_driver_nl80211_init(void *ctx, const char *ifname)
                goto failed;
        }
 
-       if (wpa_driver_nl80211_init_link_events(drv) ||
-           wpa_driver_nl80211_finish_drv_init(drv))
+       cfg = os_zalloc(sizeof(*cfg));
+       if (cfg == NULL)
+               goto failed;
+       cfg->ctx = drv;
+       cfg->newlink_cb = wpa_driver_nl80211_event_rtm_newlink;
+       cfg->dellink_cb = wpa_driver_nl80211_event_rtm_dellink;
+       drv->netlink = netlink_init(cfg);
+       if (drv->netlink == NULL) {
+               os_free(cfg);
+               goto failed;
+       }
+       if (wpa_driver_nl80211_finish_drv_init(drv))
                goto failed;
 
        return drv;
 
 failed:
-       if (drv->link_event_sock >= 0) {
-               eloop_unregister_read_sock(drv->link_event_sock);
-               close(drv->link_event_sock);
-       }
+       netlink_deinit(drv->netlink);
        if (drv->ioctl_sock >= 0)
                close(drv->ioctl_sock);
 
@@ -1401,7 +1240,8 @@ wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
        if (wpa_driver_nl80211_capa(drv))
                return -1;
 
-       wpa_driver_nl80211_send_oper_ifla(drv, 1, IF_OPER_DORMANT);
+       netlink_send_oper_ifla(drv->netlink, drv->ifindex,
+                              1, IF_OPER_DORMANT);
 #endif /* HOSTAPD */
 
        return 0;
@@ -1422,7 +1262,6 @@ static void wpa_driver_nl80211_free_bss(struct wpa_driver_nl80211_data *drv)
 #endif /* HOSTAPD */
 
 
-#if defined(CONFIG_AP) || defined(HOSTAPD)
 static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
 {
        struct nl_msg *msg;
@@ -1439,7 +1278,6 @@ static int wpa_driver_nl80211_del_beacon(struct wpa_driver_nl80211_data *drv)
  nla_put_failure:
        return -ENOBUFS;
 }
-#endif /* CONFIG_AP || HOSTAPD */
 
 
 /**
@@ -1453,7 +1291,6 @@ static void wpa_driver_nl80211_deinit(void *priv)
 {
        struct wpa_driver_nl80211_data *drv = priv;
 
-#if defined(CONFIG_AP) || defined(HOSTAPD)
        nl80211_remove_monitor_interface(drv);
        if (drv->monitor_sock >= 0) {
                eloop_unregister_read_sock(drv->monitor_sock);
@@ -1462,7 +1299,6 @@ static void wpa_driver_nl80211_deinit(void *priv)
 
        if (drv->nlmode == NL80211_IFTYPE_AP)
                wpa_driver_nl80211_del_beacon(drv);
-#endif /* CONFIG_AP || HOSTAPD */
 
 #ifdef HOSTAPD
        if (drv->last_freq_ht) {
@@ -1482,19 +1318,11 @@ static void wpa_driver_nl80211_deinit(void *priv)
                os_free(drv->if_indices);
 
        wpa_driver_nl80211_free_bss(drv);
-#else /* HOSTAPD */
-#ifndef NO_WEXT
-       wpa_driver_nl80211_set_auth_param(drv, IW_AUTH_DROP_UNENCRYPTED, 0);
-#endif /* NO_WEXT */
-
-       wpa_driver_nl80211_send_oper_ifla(priv, 0, IF_OPER_UP);
-
-       if (drv->link_event_sock >= 0) {
-               eloop_unregister_read_sock(drv->link_event_sock);
-               close(drv->link_event_sock);
-       }
 #endif /* HOSTAPD */
 
+       netlink_send_oper_ifla(drv->netlink, drv->ifindex, 0, IF_OPER_UP);
+       netlink_deinit(drv->netlink);
+
        eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
 
        (void) hostapd_set_iface_flags(drv, drv->ifname, 0);
@@ -1652,6 +1480,7 @@ static int bss_info_handler(struct nl_msg *msg, void *arg)
                [NL80211_BSS_INFORMATION_ELEMENTS] = { .type = NLA_UNSPEC },
                [NL80211_BSS_SIGNAL_MBM] = { .type = NLA_U32 },
                [NL80211_BSS_SIGNAL_UNSPEC] = { .type = NLA_U8 },
+               [NL80211_BSS_STATUS] = { .type = NLA_U32 },
                [NL80211_BSS_SEEN_MS_AGO] = { .type = NLA_U32 },
        };
        struct wpa_scan_results *res = arg;
@@ -1705,6 +1534,21 @@ static int bss_info_handler(struct nl_msg *msg, void *arg)
        if (ie)
                os_memcpy(r + 1, ie, ie_len);
 
+       if (bss[NL80211_BSS_STATUS]) {
+               enum nl80211_bss_status status;
+               status = nla_get_u32(bss[NL80211_BSS_STATUS]);
+               switch (status) {
+               case NL80211_BSS_STATUS_AUTHENTICATED:
+                       r->flags |= WPA_SCAN_AUTHENTICATED;
+                       break;
+               case NL80211_BSS_STATUS_ASSOCIATED:
+                       r->flags |= WPA_SCAN_ASSOCIATED;
+                       break;
+               default:
+                       break;
+               }
+       }
+
        tmp = os_realloc(res->res,
                         (res->num + 1) * sizeof(struct wpa_scan_res *));
        if (tmp == NULL) {
@@ -1718,6 +1562,69 @@ static int bss_info_handler(struct nl_msg *msg, void *arg)
 }
 
 
+static void clear_state_mismatch(struct wpa_driver_nl80211_data *drv,
+                                const u8 *addr)
+{
+       if (drv->capa.flags & WPA_DRIVER_FLAGS_SME) {
+               wpa_printf(MSG_DEBUG, "nl80211: Clear possible state "
+                          "mismatch");
+               wpa_driver_nl80211_mlme(drv, addr,
+                                       NL80211_CMD_DEAUTHENTICATE,
+                                       WLAN_REASON_PREV_AUTH_NOT_VALID);
+       }
+}
+
+
+static void wpa_driver_nl80211_check_bss_status(
+       struct wpa_driver_nl80211_data *drv, struct wpa_scan_results *res)
+{
+       size_t i;
+
+       for (i = 0; i < res->num; i++) {
+               struct wpa_scan_res *r = res->res[i];
+               if (r->flags & WPA_SCAN_AUTHENTICATED) {
+                       wpa_printf(MSG_DEBUG, "nl80211: Scan results "
+                                  "indicates BSS status with " MACSTR
+                                  " as authenticated",
+                                  MAC2STR(r->bssid));
+                       if (drv->nlmode == NL80211_IFTYPE_STATION &&
+                           os_memcmp(r->bssid, drv->bssid, ETH_ALEN) != 0 &&
+                           os_memcmp(r->bssid, drv->auth_bssid, ETH_ALEN) !=
+                           0) {
+                               wpa_printf(MSG_DEBUG, "nl80211: Unknown BSSID"
+                                          " in local state (auth=" MACSTR
+                                          " assoc=" MACSTR ")",
+                                          MAC2STR(drv->auth_bssid),
+                                          MAC2STR(drv->bssid));
+                       }
+               }
+
+               if (r->flags & WPA_SCAN_ASSOCIATED) {
+                       wpa_printf(MSG_DEBUG, "nl80211: Scan results "
+                                  "indicate BSS status with " MACSTR
+                                  " as associated",
+                                  MAC2STR(r->bssid));
+                       if (drv->nlmode == NL80211_IFTYPE_STATION &&
+                           !drv->associated) {
+                               wpa_printf(MSG_DEBUG, "nl80211: Local state "
+                                          "(not associated) does not match "
+                                          "with BSS state");
+                               clear_state_mismatch(drv, r->bssid);
+                       } else if (drv->nlmode == NL80211_IFTYPE_STATION &&
+                                  os_memcmp(drv->bssid, r->bssid, ETH_ALEN) !=
+                                  0) {
+                               wpa_printf(MSG_DEBUG, "nl80211: Local state "
+                                          "(associated with " MACSTR ") does "
+                                          "not match with BSS state",
+                                          MAC2STR(drv->bssid));
+                               clear_state_mismatch(drv, r->bssid);
+                               clear_state_mismatch(drv, drv->bssid);
+                       }
+               }
+       }
+}
+
+
 /**
  * wpa_driver_nl80211_get_scan_results - Fetch the latest scan results
  * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
@@ -1733,7 +1640,7 @@ wpa_driver_nl80211_get_scan_results(void *priv)
 
        res = os_zalloc(sizeof(*res));
        if (res == NULL)
-               return 0;
+               return NULL;
        msg = nlmsg_alloc();
        if (!msg)
                goto nla_put_failure;
@@ -1747,6 +1654,7 @@ wpa_driver_nl80211_get_scan_results(void *priv)
        if (ret == 0) {
                wpa_printf(MSG_DEBUG, "Received scan results (%lu BSSes)",
                           (unsigned long) res->num);
+               wpa_driver_nl80211_check_bss_status(drv, res);
                return res;
        }
        wpa_printf(MSG_DEBUG, "nl80211: Scan result fetch failed: ret=%d "
@@ -1758,11 +1666,14 @@ nla_put_failure:
 }
 
 
-static int nl_set_encr(int ifindex, struct wpa_driver_nl80211_data *drv,
-                      wpa_alg alg, const u8 *addr, int key_idx, int set_tx,
-                      const u8 *seq, size_t seq_len,
-                      const u8 *key, size_t key_len)
+static int wpa_driver_nl80211_set_key(const char *ifname, void *priv,
+                                     enum wpa_alg alg, const u8 *addr,
+                                     int key_idx, int set_tx,
+                                     const u8 *seq, size_t seq_len,
+                                     const u8 *key, size_t key_len)
 {
+       struct wpa_driver_nl80211_data *drv = priv;
+       int ifindex = if_nametoindex(ifname);
        struct nl_msg *msg;
        int ret;
 
@@ -1866,8 +1777,7 @@ nla_put_failure:
 }
 
 
-#ifndef HOSTAPD
-static int nl_add_key(struct nl_msg *msg, wpa_alg alg,
+static int nl_add_key(struct nl_msg *msg, enum wpa_alg alg,
                      int key_idx, int defkey,
                      const u8 *seq, size_t seq_len,
                      const u8 *key, size_t key_len)
@@ -1975,18 +1885,6 @@ nla_put_failure:
 }
 
 
-static int wpa_driver_nl80211_set_key(void *priv, wpa_alg alg,
-                                     const u8 *addr, int key_idx,
-                                     int set_tx, const u8 *seq,
-                                     size_t seq_len,
-                                     const u8 *key, size_t key_len)
-{
-       struct wpa_driver_nl80211_data *drv = priv;
-       return nl_set_encr(drv->ifindex, drv, alg, addr, key_idx, set_tx, seq,
-                          seq_len, key, key_len);
-}
-
-
 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
                                   const u8 *addr, int cmd, u16 reason_code)
 {
@@ -2064,6 +1962,10 @@ static int wpa_driver_nl80211_authenticate(
        int count = 0;
 
        drv->associated = 0;
+       os_memset(drv->auth_bssid, 0, ETH_ALEN);
+
+       if (wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_INFRA) < 0)
+               return -1;
 
 retry:
        msg = nlmsg_alloc();
@@ -2079,7 +1981,8 @@ retry:
        for (i = 0; i < 4; i++) {
                if (!params->wep_key[i])
                        continue;
-               wpa_driver_nl80211_set_key(drv, WPA_ALG_WEP, NULL, i,
+               wpa_driver_nl80211_set_key(drv->ifname, drv, WPA_ALG_WEP, NULL,
+                                          i,
                                           i == params->wep_tx_keyidx, NULL, 0,
                                           params->wep_key[i],
                                           params->wep_key_len[i]);
@@ -2159,10 +2062,6 @@ nla_put_failure:
        return ret;
 }
 
-#endif /* HOSTAPD */
-
-
-#if defined(CONFIG_AP) || defined(HOSTAPD)
 
 struct phy_info_arg {
        u16 *num_modes;
@@ -2207,7 +2106,7 @@ static int phy_info_handler(struct nl_msg *msg, void *arg)
                return NL_SKIP;
 
        nla_for_each_nested(nl_band, tb_msg[NL80211_ATTR_WIPHY_BANDS], rem_band) {
-               mode = realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
+               mode = os_realloc(phy_info->modes, (*phy_info->num_modes + 1) * sizeof(*mode));
                if (!mode)
                        return NL_SKIP;
                phy_info->modes = mode;
@@ -2226,6 +2125,18 @@ static int phy_info_handler(struct nl_msg *msg, void *arg)
                                tb_band[NL80211_BAND_ATTR_HT_CAPA]);
                }
 
+               if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) {
+                       mode->a_mpdu_params |= nla_get_u8(
+                               tb_band[NL80211_BAND_ATTR_HT_AMPDU_FACTOR]) &
+                               0x03;
+               }
+
+               if (tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) {
+                       mode->a_mpdu_params |= nla_get_u8(
+                               tb_band[NL80211_BAND_ATTR_HT_AMPDU_DENSITY]) <<
+                               2;
+               }
+
                if (tb_band[NL80211_BAND_ATTR_HT_MCS_SET] &&
                    nla_len(tb_band[NL80211_BAND_ATTR_HT_MCS_SET])) {
                        u8 *mcs;
@@ -2241,7 +2152,7 @@ static int phy_info_handler(struct nl_msg *msg, void *arg)
                        mode->num_channels++;
                }
 
-               mode->channels = calloc(mode->num_channels, sizeof(struct hostapd_channel_data));
+               mode->channels = os_zalloc(mode->num_channels * sizeof(struct hostapd_channel_data));
                if (!mode->channels)
                        return NL_SKIP;
 
@@ -2303,7 +2214,7 @@ static int phy_info_handler(struct nl_msg *msg, void *arg)
                        mode->num_rates++;
                }
 
-               mode->rates = calloc(mode->num_rates, sizeof(struct hostapd_rate_data));
+               mode->rates = os_zalloc(mode->num_rates * sizeof(int));
                if (!mode->rates)
                        return NL_SKIP;
 
@@ -2314,16 +2225,13 @@ static int phy_info_handler(struct nl_msg *msg, void *arg)
                                  nla_len(nl_rate), rate_policy);
                        if (!tb_rate[NL80211_BITRATE_ATTR_RATE])
                                continue;
-                       mode->rates[idx].rate = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
+                       mode->rates[idx] = nla_get_u32(tb_rate[NL80211_BITRATE_ATTR_RATE]);
 
                        /* crude heuristic */
                        if (mode->mode == HOSTAPD_MODE_IEEE80211B &&
-                           mode->rates[idx].rate > 200)
+                           mode->rates[idx] > 200)
                                mode->mode = HOSTAPD_MODE_IEEE80211G;
 
-                       if (tb_rate[NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE])
-                               mode->rates[idx].flags |= HOSTAPD_RATE_PREAMBLE2;
-
                        idx++;
                }
        }
@@ -2374,7 +2282,7 @@ wpa_driver_nl80211_add_11b(struct hostapd_hw_modes *modes, u16 *num_modes)
                  mode11g->num_channels * sizeof(struct hostapd_channel_data));
 
        mode->num_rates = 0;
-       mode->rates = os_malloc(4 * sizeof(struct hostapd_rate_data));
+       mode->rates = os_malloc(4 * sizeof(int));
        if (mode->rates == NULL) {
                os_free(mode->channels);
                (*num_modes)--;
@@ -2382,9 +2290,8 @@ wpa_driver_nl80211_add_11b(struct hostapd_hw_modes *modes, u16 *num_modes)
        }
 
        for (i = 0; i < mode11g->num_rates; i++) {
-               if (mode11g->rates[i].rate > 110 ||
-                   mode11g->rates[i].flags &
-                   (HOSTAPD_RATE_ERP | HOSTAPD_RATE_OFDM))
+               if (mode11g->rates[i] != 10 && mode11g->rates[i] != 20 &&
+                   mode11g->rates[i] != 55 && mode11g->rates[i] != 110)
                        continue;
                mode->rates[mode->num_rates] = mode11g->rates[i];
                mode->num_rates++;
@@ -2503,8 +2410,6 @@ static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data,
        return wpa_driver_nl80211_send_frame(drv, data, data_len, encrypt);
 }
 
-#endif /* CONFIG_AP || HOSTAPD */
-
 
 static int wpa_driver_nl80211_set_beacon(const char *ifname, void *priv,
                                         const u8 *head, size_t head_len,
@@ -2562,8 +2467,6 @@ static int wpa_driver_nl80211_set_beacon(const char *ifname, void *priv,
 }
 
 
-#if defined(CONFIG_AP) || defined(HOSTAPD)
-
 static int wpa_driver_nl80211_set_freq(struct wpa_driver_nl80211_data *drv,
                                       int freq, int ht_enabled,
                                       int sec_channel_offset)
@@ -2628,14 +2531,11 @@ static int wpa_driver_nl80211_sta_add(const char *ifname, void *priv,
                params->supp_rates);
        NLA_PUT_U16(msg, NL80211_ATTR_STA_LISTEN_INTERVAL,
                    params->listen_interval);
-
-#ifdef CONFIG_IEEE80211N
        if (params->ht_capabilities) {
                NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
-                       params->ht_capabilities->length,
-                       &params->ht_capabilities->data);
+                       sizeof(*params->ht_capabilities),
+                       params->ht_capabilities);
        }
-#endif /* CONFIG_IEEE80211N */
 
        ret = send_and_recv_msgs(drv, msg, NULL, NULL);
        if (ret)
@@ -2679,6 +2579,8 @@ static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
 {
        struct nl_msg *msg;
 
+       wpa_printf(MSG_DEBUG, "nl80211: Remove interface ifindex=%d", ifidx);
+
 #ifdef HOSTAPD
        /* stop listening for EAPOL on this interface */
        del_ifidx(drv, ifidx);
@@ -2703,7 +2605,7 @@ static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
 static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
                                     const char *ifname,
                                     enum nl80211_iftype iftype,
-                                    const u8 *addr)
+                                    const u8 *addr, int wds)
 {
        struct nl_msg *msg, *flags = NULL;
        int ifidx;
@@ -2734,6 +2636,8 @@ static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
 
                if (err)
                        goto nla_put_failure;
+       } else if (wds) {
+               NLA_PUT_U8(msg, NL80211_ATTR_4ADDR, wds);
        }
 
        ret = send_and_recv_msgs(drv, msg, NULL, NULL);
@@ -2745,6 +2649,8 @@ static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
        }
 
        ifidx = if_nametoindex(ifname);
+       wpa_printf(MSG_DEBUG, "nl80211: New interface %s created: ifindex=%d",
+                  ifname, ifidx);
 
        if (ifidx <= 0)
                return -1;
@@ -2762,13 +2668,15 @@ static int nl80211_create_iface_once(struct wpa_driver_nl80211_data *drv,
 
        return ifidx;
 }
+
+
 static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
                                const char *ifname, enum nl80211_iftype iftype,
-                               const u8 *addr)
+                               const u8 *addr, int wds)
 {
        int ret;
 
-       ret = nl80211_create_iface_once(drv, ifname, iftype, addr);
+       ret = nl80211_create_iface_once(drv, ifname, iftype, addr, wds);
 
        /* if error occured and interface exists already */
        if (ret == -ENFILE && if_nametoindex(ifname)) {
@@ -2778,107 +2686,71 @@ static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
                nl80211_remove_iface(drv, if_nametoindex(ifname));
 
                /* Try to create the interface again */
-               ret = nl80211_create_iface_once(drv, ifname, iftype, addr);
+               ret = nl80211_create_iface_once(drv, ifname, iftype, addr,
+                                               wds);
        }
 
        return ret;
 }
 
-#endif /* CONFIG_AP || HOSTAPD */
-
-#ifdef CONFIG_AP
-
-void ap_tx_status(void *ctx, const u8 *addr,
-                 const u8 *buf, size_t len, int ack);
-void ap_rx_from_unknown_sta(void *ctx, struct ieee80211_hdr *hdr, size_t len);
-void ap_mgmt_rx(void *ctx, u8 *buf, size_t len, u16 stype,
-               struct hostapd_frame_info *fi);
-void ap_mgmt_tx_cb(void *ctx, u8 *buf, size_t len, u16 stype, int ok);
-
-#endif /* CONFIG_AP */
-
-#if defined(CONFIG_AP) || defined(HOSTAPD)
 
 static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
 {
        struct ieee80211_hdr *hdr;
-       u16 fc, type, stype;
+       u16 fc;
+       union wpa_event_data event;
 
        hdr = (struct ieee80211_hdr *) buf;
        fc = le_to_host16(hdr->frame_control);
 
-       type = WLAN_FC_GET_TYPE(fc);
-       stype = WLAN_FC_GET_STYPE(fc);
-
-       switch (type) {
-       case WLAN_FC_TYPE_MGMT:
-               wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
-                          ok ? "ACK" : "fail");
-#ifdef HOSTAPD
-               hostapd_mgmt_tx_cb(ctx, buf, len, stype, ok);
-#else /* HOSTAPD */
-               ap_mgmt_tx_cb(ctx, buf, len, stype, ok);
-#endif /* HOSTAPD */
-               break;
-       case WLAN_FC_TYPE_CTRL:
-               wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
-                          ok ? "ACK" : "fail");
-               break;
-       case WLAN_FC_TYPE_DATA:
-#ifdef HOSTAPD
-               hostapd_tx_status(ctx, hdr->addr1, buf, len, ok);
-#else /* HOSTAPD */
-               ap_tx_status(ctx, hdr->addr1, buf, len, ok);
-#endif /* HOSTAPD */
-               break;
-       default:
-               wpa_printf(MSG_DEBUG, "unknown TX callback frame type %d",
-                          type);
-               break;
-       }
+       os_memset(&event, 0, sizeof(event));
+       event.tx_status.type = WLAN_FC_GET_TYPE(fc);
+       event.tx_status.stype = WLAN_FC_GET_STYPE(fc);
+       event.tx_status.dst = hdr->addr1;
+       event.tx_status.data = buf;
+       event.tx_status.data_len = len;
+       event.tx_status.ack = ok;
+       wpa_supplicant_event(ctx, EVENT_TX_STATUS, &event);
 }
 
 
 static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
-                            struct ieee80211_hdr *hdr, size_t len)
+                            u8 *buf, size_t len)
 {
-#ifdef HOSTAPD
-       hostapd_rx_from_unknown_sta(drv->ctx, hdr, len);
-#else /* HOSTAPD */
-       ap_rx_from_unknown_sta(drv->ctx, hdr, len);
-#endif /* HOSTAPD */
+       union wpa_event_data event;
+       os_memset(&event, 0, sizeof(event));
+       event.rx_from_unknown.frame = buf;
+       event.rx_from_unknown.len = len;
+       wpa_supplicant_event(drv->ctx, EVENT_RX_FROM_UNKNOWN, &event);
 }
 
 
 static void handle_frame(struct wpa_driver_nl80211_data *drv,
-                        u8 *buf, size_t len,
-                        struct hostapd_frame_info *hfi)
+                        u8 *buf, size_t len, int datarate, int ssi_signal)
 {
        struct ieee80211_hdr *hdr;
-       u16 fc, stype;
+       u16 fc;
+       union wpa_event_data event;
 
        hdr = (struct ieee80211_hdr *) buf;
        fc = le_to_host16(hdr->frame_control);
-       stype = WLAN_FC_GET_STYPE(fc);
 
        switch (WLAN_FC_GET_TYPE(fc)) {
        case WLAN_FC_TYPE_MGMT:
-               if (stype != WLAN_FC_STYPE_BEACON &&
-                   stype != WLAN_FC_STYPE_PROBE_REQ)
-                       wpa_printf(MSG_MSGDUMP, "MGMT");
-#ifdef HOSTAPD
-               hostapd_mgmt_rx(drv->ctx, buf, len, stype, hfi);
-#else /* HOSTAPD */
-               ap_mgmt_rx(drv->ctx, buf, len, stype, hfi);
-#endif /* HOSTAPD */
+               os_memset(&event, 0, sizeof(event));
+               event.rx_mgmt.frame = buf;
+               event.rx_mgmt.frame_len = len;
+               event.rx_mgmt.datarate = datarate;
+               event.rx_mgmt.ssi_signal = ssi_signal;
+               wpa_supplicant_event(drv->ctx, EVENT_RX_MGMT, &event);
                break;
        case WLAN_FC_TYPE_CTRL:
                /* can only get here with PS-Poll frames */
                wpa_printf(MSG_DEBUG, "CTRL");
-               from_unknown_sta(drv, hdr, len);
+               from_unknown_sta(drv, buf, len);
                break;
        case WLAN_FC_TYPE_DATA:
-               from_unknown_sta(drv, hdr, len);
+               from_unknown_sta(drv, buf, len);
                break;
        }
 }
@@ -2891,7 +2763,7 @@ static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
        unsigned char buf[3000];
        struct ieee80211_radiotap_iterator iter;
        int ret;
-       struct hostapd_frame_info hfi;
+       int datarate = 0, ssi_signal = 0;
        int injected = 0, failed = 0, rxflags = 0;
 
        len = recv(sock, buf, sizeof(buf), 0);
@@ -2905,8 +2777,6 @@ static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
                return;
        }
 
-       memset(&hfi, 0, sizeof(hfi));
-
        while (1) {
                ret = ieee80211_radiotap_iterator_next(&iter);
                if (ret == -ENOENT)
@@ -2931,16 +2801,13 @@ static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
                case IEEE80211_RADIOTAP_DATA_RETRIES:
                        break;
                case IEEE80211_RADIOTAP_CHANNEL:
-                       /* TODO convert from freq/flags to channel number
-                       hfi.channel = XXX;
-                       hfi.phytype = XXX;
-                        */
+                       /* TODO: convert from freq/flags to channel number */
                        break;
                case IEEE80211_RADIOTAP_RATE:
-                       hfi.datarate = *iter.this_arg * 5;
+                       datarate = *iter.this_arg * 5;
                        break;
                case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
-                       hfi.ssi_signal = *iter.this_arg;
+                       ssi_signal = *iter.this_arg;
                        break;
                }
        }
@@ -2950,7 +2817,7 @@ static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
 
        if (!injected)
                handle_frame(drv, buf + iter.max_length,
-                            len - iter.max_length, &hfi);
+                            len - iter.max_length, datarate, ssi_signal);
        else
                handle_tx_callback(drv->ctx, buf + iter.max_length,
                                   len - iter.max_length, !failed);
@@ -3003,7 +2870,7 @@ static struct sock_filter msock_filter_insns[] = {
 
 #if 0
        /*
-        * drop non-data frames, WDS frames
+        * drop non-data frames
         */
        /* load the lower byte of the frame control field */
        BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
@@ -3011,13 +2878,13 @@ static struct sock_filter msock_filter_insns[] = {
        BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x0c),
        /* drop non-data frames */
        BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 8, 0, FAIL),
+#endif
        /* load the upper byte of the frame control field */
-       BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
+       BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 1),
        /* mask off toDS/fromDS */
        BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x03),
-       /* drop WDS frames */
-       BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 3, FAIL, 0),
-#endif
+       /* accept WDS frames */
+       BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 3, PASS, 0),
 
        /*
         * add header length to index
@@ -3123,7 +2990,8 @@ nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
        buf[IFNAMSIZ - 1] = '\0';
 
        drv->monitor_ifidx =
-               nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL);
+               nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL,
+                                    0);
 
        if (drv->monitor_ifidx < 0)
                return -1;
@@ -3184,7 +3052,7 @@ static int wpa_driver_nl80211_hapd_send_eapol(
        u8 *pos;
        int res;
 #if 0 /* FIX */
-       int qos = sta->flags & WLAN_STA_WME;
+       int qos = sta->flags & WPA_STA_WMM;
 #else
        int qos = 0;
 #endif
@@ -3236,7 +3104,7 @@ static int wpa_driver_nl80211_hapd_send_eapol(
                           "failed: %d (%s)",
                           (unsigned long) len, errno, strerror(errno));
        }
-       free(hdr);
+       os_free(hdr);
 
        return res;
 }
@@ -3246,13 +3114,13 @@ static u32 sta_flags_nl80211(int flags)
 {
        u32 f = 0;
 
-       if (flags & WLAN_STA_AUTHORIZED)
+       if (flags & WPA_STA_AUTHORIZED)
                f |= BIT(NL80211_STA_FLAG_AUTHORIZED);
-       if (flags & WLAN_STA_WMM)
+       if (flags & WPA_STA_WMM)
                f |= BIT(NL80211_STA_FLAG_WME);
-       if (flags & WLAN_STA_SHORT_PREAMBLE)
+       if (flags & WPA_STA_SHORT_PREAMBLE)
                f |= BIT(NL80211_STA_FLAG_SHORT_PREAMBLE);
-       if (flags & WLAN_STA_MFP)
+       if (flags & WPA_STA_MFP)
                f |= BIT(NL80211_STA_FLAG_MFP);
 
        return f;
@@ -3288,16 +3156,16 @@ static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
         * Backwards compatibility version using NL80211_ATTR_STA_FLAGS. This
         * can be removed eventually.
         */
-       if (total_flags & WLAN_STA_AUTHORIZED)
+       if (total_flags & WPA_STA_AUTHORIZED)
                NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
 
-       if (total_flags & WLAN_STA_WMM)
+       if (total_flags & WPA_STA_WMM)
                NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
 
-       if (total_flags & WLAN_STA_SHORT_PREAMBLE)
+       if (total_flags & WPA_STA_SHORT_PREAMBLE)
                NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
 
-       if (total_flags & WLAN_STA_MFP)
+       if (total_flags & WPA_STA_MFP)
                NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
 
        if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
@@ -3316,9 +3184,6 @@ static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
        return -ENOBUFS;
 }
 
-#endif /* CONFIG_AP || HOSTAPD */
-
-#ifdef CONFIG_AP
 
 static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
                                 struct wpa_driver_associate_params *params)
@@ -3334,10 +3199,111 @@ static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
 
        return 0;
 }
-#endif /* CONFIG_AP */
 
 
-#ifndef HOSTAPD
+static int nl80211_leave_ibss(struct wpa_driver_nl80211_data *drv)
+{
+       struct nl_msg *msg;
+       int ret = -1;
+
+       msg = nlmsg_alloc();
+       if (!msg)
+               return -1;
+
+       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
+                   NL80211_CMD_LEAVE_IBSS, 0);
+       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
+       ret = send_and_recv_msgs(drv, msg, NULL, NULL);
+       msg = NULL;
+       if (ret) {
+               wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS failed: ret=%d "
+                          "(%s)", ret, strerror(-ret));
+               goto nla_put_failure;
+       }
+
+       ret = 0;
+       wpa_printf(MSG_DEBUG, "nl80211: Leave IBSS request sent successfully");
+
+nla_put_failure:
+       nlmsg_free(msg);
+       return ret;
+}
+
+
+static int wpa_driver_nl80211_ibss(struct wpa_driver_nl80211_data *drv,
+                                  struct wpa_driver_associate_params *params)
+{
+       struct nl_msg *msg;
+       int ret = -1;
+       int count = 0;
+
+       wpa_printf(MSG_DEBUG, "nl80211: Join IBSS (ifindex=%d)", drv->ifindex);
+
+       if (wpa_driver_nl80211_set_mode(drv, params->mode)) {
+               wpa_printf(MSG_INFO, "nl80211: Failed to set interface into "
+                          "IBSS mode");
+               return -1;
+       }
+
+retry:
+       msg = nlmsg_alloc();
+       if (!msg)
+               return -1;
+
+       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
+                   NL80211_CMD_JOIN_IBSS, 0);
+       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
+
+       if (params->ssid == NULL || params->ssid_len > sizeof(drv->ssid))
+               goto nla_put_failure;
+
+       wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
+                         params->ssid, params->ssid_len);
+       NLA_PUT(msg, NL80211_ATTR_SSID, params->ssid_len,
+               params->ssid);
+       os_memcpy(drv->ssid, params->ssid, params->ssid_len);
+       drv->ssid_len = params->ssid_len;
+
+       wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
+       NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
+
+       ret = nl80211_set_conn_keys(params, msg);
+       if (ret)
+               goto nla_put_failure;
+
+       if (params->wpa_ie) {
+               wpa_hexdump(MSG_DEBUG,
+                           "  * Extra IEs for Beacon/Probe Response frames",
+                           params->wpa_ie, params->wpa_ie_len);
+               NLA_PUT(msg, NL80211_ATTR_IE, params->wpa_ie_len,
+                       params->wpa_ie);
+       }
+
+       ret = send_and_recv_msgs(drv, msg, NULL, NULL);
+       msg = NULL;
+       if (ret) {
+               wpa_printf(MSG_DEBUG, "nl80211: Join IBSS failed: ret=%d (%s)",
+                          ret, strerror(-ret));
+               count++;
+               if (ret == -EALREADY && count == 1) {
+                       wpa_printf(MSG_DEBUG, "nl80211: Retry IBSS join after "
+                                  "forced leave");
+                       nl80211_leave_ibss(drv);
+                       nlmsg_free(msg);
+                       goto retry;
+               }
+
+               goto nla_put_failure;
+       }
+       ret = 0;
+       wpa_printf(MSG_DEBUG, "nl80211: Join IBSS request sent successfully");
+
+nla_put_failure:
+       nlmsg_free(msg);
+       return ret;
+}
+
+
 static int wpa_driver_nl80211_connect(
        struct wpa_driver_nl80211_data *drv,
        struct wpa_driver_associate_params *params)
@@ -3406,7 +3372,7 @@ static int wpa_driver_nl80211_connect(
        }
 
        if (params->pairwise_suite != CIPHER_NONE) {
-               int cipher = IW_AUTH_CIPHER_NONE;
+               int cipher;
 
                switch (params->pairwise_suite) {
                case CIPHER_WEP40:
@@ -3427,7 +3393,7 @@ static int wpa_driver_nl80211_connect(
        }
 
        if (params->group_suite != CIPHER_NONE) {
-               int cipher = IW_AUTH_CIPHER_NONE;
+               int cipher;
 
                switch (params->group_suite) {
                case CIPHER_WEP40:
@@ -3491,18 +3457,17 @@ static int wpa_driver_nl80211_associate(
        int ret = -1;
        struct nl_msg *msg;
 
-#ifdef CONFIG_AP
-       if (params->mode == 2)
+       if (params->mode == IEEE80211_MODE_AP)
                return wpa_driver_nl80211_ap(drv, params);
-#endif /* CONFIG_AP */
 
-       if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME))
-               return wpa_driver_nl80211_connect(drv, params);
+       if (params->mode == IEEE80211_MODE_IBSS)
+               return wpa_driver_nl80211_ibss(drv, params);
 
-#ifndef NO_WEXT
-       wpa_driver_nl80211_set_auth_param(drv, IW_AUTH_DROP_UNENCRYPTED,
-                                         params->drop_unencrypted);
-#endif /* NO_WEXT */
+       if (!(drv->capa.flags & WPA_DRIVER_FLAGS_SME)) {
+               if (wpa_driver_nl80211_set_mode(drv, params->mode) < 0)
+                       return -1;
+               return wpa_driver_nl80211_connect(drv, params);
+       }
 
        drv->associated = 0;
 
@@ -3524,7 +3489,9 @@ static int wpa_driver_nl80211_associate(
        if (params->freq) {
                wpa_printf(MSG_DEBUG, "  * freq=%d", params->freq);
                NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, params->freq);
-       }
+               drv->assoc_freq = params->freq;
+       } else
+               drv->assoc_freq = 0;
        if (params->ssid) {
                wpa_hexdump_ascii(MSG_DEBUG, "  * SSID",
                                  params->ssid, params->ssid_len);
@@ -3569,7 +3536,6 @@ nla_put_failure:
        nlmsg_free(msg);
        return ret;
 }
-#endif /* HOSTAPD */
 
 
 static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
@@ -3624,6 +3590,8 @@ static int wpa_driver_nl80211_set_mode(void *priv, int mode)
        }
 
        if (nlmode == drv->nlmode) {
+               wpa_printf(MSG_DEBUG, "nl80211: Interface already in "
+                          "requested mode - ignore error");
                ret = 0;
                goto done; /* Already in the requested mode */
        }
@@ -3639,11 +3607,13 @@ static int wpa_driver_nl80211_set_mode(void *priv, int mode)
                        ret = -1;
        }
 
-       if (!ret)
+       if (!ret) {
+               wpa_printf(MSG_DEBUG, "nl80211: Mode change succeeded while "
+                          "interface is down");
                drv->nlmode = nlmode;
+       }
 
 done:
-#if defined(CONFIG_AP) || defined(HOSTAPD)
        if (!ret && nlmode == NL80211_IFTYPE_AP) {
                /* Setup additional AP mode functionality if needed */
                if (drv->monitor_ifidx < 0 &&
@@ -3653,14 +3623,15 @@ done:
                /* Remove additional AP mode functionality */
                nl80211_remove_monitor_interface(drv);
        }
-#endif /* CONFIG_AP || HOSTAPD */
+
+       if (ret)
+               wpa_printf(MSG_DEBUG, "nl80211: Interface mode change to %d "
+                          "from %d failed", nlmode, drv->nlmode);
 
        return ret;
 }
 
 
-#ifndef HOSTAPD
-
 static int wpa_driver_nl80211_get_capa(void *priv,
                                       struct wpa_driver_capa *capa)
 {
@@ -3679,8 +3650,8 @@ static int wpa_driver_nl80211_set_operstate(void *priv, int state)
        wpa_printf(MSG_DEBUG, "%s: operstate %d->%d (%s)",
                   __func__, drv->operstate, state, state ? "UP" : "DORMANT");
        drv->operstate = state;
-       return wpa_driver_nl80211_send_oper_ifla(
-               drv, -1, state ? IF_OPER_UP : IF_OPER_DORMANT);
+       return netlink_send_oper_ifla(drv->netlink, drv->ifindex, -1,
+                                     state ? IF_OPER_UP : IF_OPER_DORMANT);
 }
 
 
@@ -3712,8 +3683,6 @@ static int wpa_driver_nl80211_set_supp_port(void *priv, int authorized)
        return -ENOBUFS;
 }
 
-#endif /* HOSTAPD */
-
 
 #ifdef HOSTAPD
 
@@ -3750,8 +3719,8 @@ static void add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
        else
                old = NULL;
 
-       drv->if_indices = realloc(old,
-                                 sizeof(int) * (drv->num_if_indices + 1));
+       drv->if_indices = os_realloc(old,
+                                    sizeof(int) * (drv->num_if_indices + 1));
        if (!drv->if_indices) {
                if (!old)
                        drv->if_indices = drv->default_if_indices;
@@ -3792,16 +3761,6 @@ static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
 }
 
 
-static int i802_set_key(const char *iface, void *priv, wpa_alg alg,
-                       const u8 *addr, int key_idx, int set_tx, const u8 *seq,
-                       size_t seq_len, const u8 *key, size_t key_len)
-{
-       struct wpa_driver_nl80211_data *drv = priv;
-       return nl_set_encr(if_nametoindex(iface), drv, alg, addr, key_idx,
-                          set_tx, seq, seq_len, key, key_len);
-}
-
-
 static inline int min_int(int a, int b)
 {
        if (a < b)
@@ -4100,54 +4059,6 @@ static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
 }
 
 
-static int i802_bss_add(void *priv, const char *ifname, const u8 *bssid)
-{
-       struct wpa_driver_nl80211_data *drv = priv;
-       int ifidx;
-       struct i802_bss *bss;
-
-       bss = os_zalloc(sizeof(*bss));
-       if (bss == NULL)
-               return -1;
-
-       ifidx = nl80211_create_iface(priv, ifname, NL80211_IFTYPE_AP, bssid);
-       if (ifidx < 0) {
-               os_free(bss);
-               return -1;
-       }
-       bss->ifindex = ifidx;
-       if (hostapd_set_iface_flags(priv, ifname, 1)) {
-               nl80211_remove_iface(priv, ifidx);
-               os_free(bss);
-               return -1;
-       }
-       bss->next = drv->bss.next;
-       drv->bss.next = bss;
-       return 0;
-}
-
-
-static int i802_bss_remove(void *priv, const char *ifname)
-{
-       struct wpa_driver_nl80211_data *drv = priv;
-       struct i802_bss *bss, *prev;
-       int ifindex = if_nametoindex(ifname);
-       nl80211_remove_iface(priv, ifindex);
-       prev = &drv->bss;
-       bss = drv->bss.next;
-       while (bss) {
-               if (ifindex == bss->ifindex) {
-                       prev->next = bss->next;
-                       os_free(bss);
-                       break;
-               }
-               prev = bss;
-               bss = bss->next;
-       }
-       return 0;
-}
-
-
 static int i802_set_bss(void *priv, int cts, int preamble, int slot)
 {
        struct wpa_driver_nl80211_data *drv = priv;
@@ -4194,42 +4105,6 @@ static int i802_set_short_slot_time(void *priv, int value)
 }
 
 
-static enum nl80211_iftype i802_if_type(enum hostapd_driver_if_type type)
-{
-       switch (type) {
-       case HOSTAPD_IF_VLAN:
-               return NL80211_IFTYPE_AP_VLAN;
-       }
-       return -1;
-}
-
-
-static int i802_if_add(const char *iface, void *priv,
-                      enum hostapd_driver_if_type type, char *ifname,
-                      const u8 *addr)
-{
-       if (nl80211_create_iface(priv, ifname, i802_if_type(type), addr) < 0)
-               return -1;
-       return 0;
-}
-
-
-static int i802_if_update(void *priv, enum hostapd_driver_if_type type,
-                         char *ifname, const u8 *addr)
-{
-       /* unused at the moment */
-       return -1;
-}
-
-
-static int i802_if_remove(void *priv, enum hostapd_driver_if_type type,
-                         const char *ifname, const u8 *addr)
-{
-       nl80211_remove_iface(priv, if_nametoindex(ifname));
-       return 0;
-}
-
-
 static int i802_set_sta_vlan(void *priv, const u8 *addr,
                             const char *ifname, int vlan_id)
 {
@@ -4255,6 +4130,26 @@ static int i802_set_sta_vlan(void *priv, const u8 *addr,
 }
 
 
+static int i802_set_wds_sta(void *priv, const u8 *addr, int aid, int val)
+{
+       struct wpa_driver_nl80211_data *drv = priv;
+       char name[16];
+
+       os_snprintf(name, sizeof(name), "%s.sta%d", drv->ifname, aid);
+       if (val) {
+               if (nl80211_create_iface(priv, name, NL80211_IFTYPE_AP_VLAN,
+                                        NULL, 1) < 0)
+                       return -1;
+               hostapd_set_iface_flags(drv, name, 1);
+               return i802_set_sta_vlan(priv, addr, name, 0);
+       } else {
+               i802_set_sta_vlan(priv, addr, drv->ifname, 0);
+               return wpa_driver_nl80211_if_remove(priv, WPA_IF_AP_VLAN,
+                                                   name);
+       }
+}
+
+
 static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
 {
        struct wpa_driver_nl80211_data *drv = eloop_ctx;
@@ -4370,7 +4265,7 @@ static void *i802_init(struct hostapd_data *hapd,
                        goto failed;
        }
 
-       if (nl80211_set_mode(drv, drv->ifindex, NL80211_IFTYPE_AP)) {
+       if (wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_AP)) {
                wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
                           "into AP mode", drv->ifname);
                goto failed;
@@ -4419,17 +4314,103 @@ static void i802_deinit(void *priv)
 #endif /* HOSTAPD */
 
 
+static enum nl80211_iftype wpa_driver_nl80211_if_type(
+       enum wpa_driver_if_type type)
+{
+       switch (type) {
+       case WPA_IF_STATION:
+               return NL80211_IFTYPE_STATION;
+       case WPA_IF_AP_VLAN:
+               return NL80211_IFTYPE_AP_VLAN;
+       case WPA_IF_AP_BSS:
+               return NL80211_IFTYPE_AP;
+       }
+       return -1;
+}
+
+
+static int wpa_driver_nl80211_if_add(const char *iface, void *priv,
+                                    enum wpa_driver_if_type type,
+                                    const char *ifname, const u8 *addr,
+                                    void *bss_ctx)
+{
+       struct wpa_driver_nl80211_data *drv = priv;
+       int ifidx;
+#ifdef HOSTAPD
+       struct i802_bss *bss = NULL;
+
+       if (type == WPA_IF_AP_BSS) {
+               bss = os_zalloc(sizeof(*bss));
+               if (bss == NULL)
+                       return -1;
+       }
+#endif /* HOSTAPD */
+
+       ifidx = nl80211_create_iface(drv, ifname,
+                                    wpa_driver_nl80211_if_type(type), addr,
+                                    0);
+       if (ifidx < 0) {
+#ifdef HOSTAPD
+               os_free(bss);
+#endif /* HOSTAPD */
+               return -1;
+       }
+
+#ifdef HOSTAPD
+       if (type == WPA_IF_AP_BSS) {
+               if (hostapd_set_iface_flags(priv, ifname, 1)) {
+                       nl80211_remove_iface(priv, ifidx);
+                       os_free(bss);
+                       return -1;
+               }
+               bss->ifindex = ifidx;
+               bss->next = drv->bss.next;
+               drv->bss.next = bss;
+       }
+#endif /* HOSTAPD */
+
+       return 0;
+}
+
+
+static int wpa_driver_nl80211_if_remove(void *priv,
+                                       enum wpa_driver_if_type type,
+                                       const char *ifname)
+{
+       struct wpa_driver_nl80211_data *drv = priv;
+       int ifindex = if_nametoindex(ifname);
+
+       nl80211_remove_iface(drv, ifindex);
+
+#ifdef HOSTAPD
+       if (type == WPA_IF_AP_BSS) {
+               struct i802_bss *bss, *prev;
+               prev = &drv->bss;
+               bss = drv->bss.next;
+               while (bss) {
+                       if (ifindex == bss->ifindex) {
+                               prev->next = bss->next;
+                               os_free(bss);
+                               break;
+                       }
+                       prev = bss;
+                       bss = bss->next;
+               }
+       }
+#endif /* HOSTAPD */
+
+       return 0;
+}
+
+
 const struct wpa_driver_ops wpa_driver_nl80211_ops = {
        .name = "nl80211",
        .desc = "Linux nl80211/cfg80211",
-#ifndef HOSTAPD
        .get_bssid = wpa_driver_nl80211_get_bssid,
        .get_ssid = wpa_driver_nl80211_get_ssid,
        .set_key = wpa_driver_nl80211_set_key,
-#endif /* HOSTAPD */
        .scan2 = wpa_driver_nl80211_scan,
        .get_scan_results2 = wpa_driver_nl80211_get_scan_results,
-#ifndef HOSTAPD
        .deauthenticate = wpa_driver_nl80211_deauthenticate,
        .disassociate = wpa_driver_nl80211_disassociate,
        .authenticate = wpa_driver_nl80211_authenticate,
@@ -4439,22 +4420,19 @@ const struct wpa_driver_ops wpa_driver_nl80211_ops = {
        .get_capa = wpa_driver_nl80211_get_capa,
        .set_operstate = wpa_driver_nl80211_set_operstate,
        .set_supp_port = wpa_driver_nl80211_set_supp_port,
-#endif /* HOSTAPD */
        .set_country = wpa_driver_nl80211_set_country,
-       .set_mode = wpa_driver_nl80211_set_mode,
        .set_beacon = wpa_driver_nl80211_set_beacon,
-#if defined(CONFIG_AP) || defined(HOSTAPD)
+       .if_add = wpa_driver_nl80211_if_add,
+       .if_remove = wpa_driver_nl80211_if_remove,
        .send_mlme = wpa_driver_nl80211_send_mlme,
        .get_hw_feature_data = wpa_driver_nl80211_get_hw_feature_data,
        .sta_add = wpa_driver_nl80211_sta_add,
        .sta_remove = wpa_driver_nl80211_sta_remove,
        .hapd_send_eapol = wpa_driver_nl80211_hapd_send_eapol,
        .sta_set_flags = wpa_driver_nl80211_sta_set_flags,
-#endif /* CONFIG_AP || HOSTAPD */
 #ifdef HOSTAPD
        .hapd_init = i802_init,
        .hapd_deinit = i802_deinit,
-       .hapd_set_key = i802_set_key,
        .get_seqnum = i802_get_seqnum,
        .flush = i802_flush,
        .read_sta_data = i802_read_sta_data,
@@ -4470,11 +4448,7 @@ const struct wpa_driver_ops wpa_driver_nl80211_ops = {
        .set_preamble = i802_set_preamble,
        .set_short_slot_time = i802_set_short_slot_time,
        .set_tx_queue_params = i802_set_tx_queue_params,
-       .bss_add = i802_bss_add,
-       .bss_remove = i802_bss_remove,
-       .if_add = i802_if_add,
-       .if_update = i802_if_update,
-       .if_remove = i802_if_remove,
        .set_sta_vlan = i802_set_sta_vlan,
+       .set_wds_sta = i802_set_wds_sta,
 #endif /* HOSTAPD */
 };