Remove unused set_retry() driver op
[wpasupplicant] / src / drivers / driver_nl80211.c
index 073494a..2f87d43 100644 (file)
 #include "includes.h"
 #include <sys/ioctl.h>
 #include <net/if_arp.h>
+#include <net/if.h>
 #include <netlink/genl/genl.h>
 #include <netlink/genl/family.h>
 #include <netlink/genl/ctrl.h>
 #include "nl80211_copy.h"
+#ifndef NO_WEXT
 #include "wireless_copy.h"
+#endif /* NO_WEXT */
 
 #include "common.h"
 #include "driver.h"
 #include <linux/filter.h>
 #include "radiotap.h"
 #include "radiotap_iter.h"
-#endif /* CONFIG_AP || HOSTAPD */
-
-#ifdef CONFIG_AP
-
-#include "../hostapd/hostapd_defs.h"
 
-#ifndef ETH_P_ALL
-#define ETH_P_ALL 0x0003
-#endif
-
-#endif /* CONFIG_AP */
+#include "../../hostapd/hostapd_defs.h"
+#include "../../hostapd/sta_flags.h"
+#endif /* CONFIG_AP || HOSTAPD */
 
 #ifdef HOSTAPD
-#include "../../hostapd/hostapd.h"
-#include "../../hostapd/sta_flags.h"
 #include "ieee802_11_common.h"
 
 #ifdef CONFIG_LIBNL20
 #define IF_OPER_UP 6
 #endif
 
-enum ieee80211_msg_type {
-       ieee80211_msg_normal = 0,
-       ieee80211_msg_tx_callback_ack = 1,
-       ieee80211_msg_tx_callback_fail = 2,
-};
-
 struct i802_bss {
        struct i802_bss *next;
        int ifindex;
@@ -110,20 +98,18 @@ struct wpa_driver_nl80211_data {
        int associated;
        u8 ssid[32];
        size_t ssid_len;
+       int nlmode;
+       int ap_scan_as_station;
 
-#if defined(CONFIG_AP) || defined(HOSTAPD)
        int beacon_int;
        int monitor_sock;
        int monitor_ifidx;
-#endif /* CONFIG_AP || HOSTAPD */
 
 #ifdef CONFIG_AP
        unsigned int beacon_set:1;
 #endif /* CONFIG_AP */
 
 #ifdef HOSTAPD
-       struct hostapd_data *hapd;
-
        int eapol_sock; /* socket for EAPOL frames */
 
        int default_if_indices[16];
@@ -131,23 +117,24 @@ struct wpa_driver_nl80211_data {
        int num_if_indices;
 
        struct i802_bss bss;
-       unsigned int ht_40mhz_scan:1;
 
        int last_freq;
        int last_freq_ht;
-       struct hostapd_neighbor_bss *neighbors;
-       size_t num_neighbors;
 #endif /* HOSTAPD */
 };
 
 
 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx,
                                            void *timeout_ctx);
-static int wpa_driver_nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
-                                      int mode);
+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 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);
@@ -158,6 +145,9 @@ 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);
 #endif /* HOSTAPD */
 
 
@@ -288,6 +278,54 @@ nla_put_failure:
 }
 
 
+#ifdef HOSTAPD
+static int get_ifhwaddr(struct wpa_driver_nl80211_data *drv,
+                       const char *ifname, u8 *addr)
+{
+       struct ifreq ifr;
+
+       os_memset(&ifr, 0, sizeof(ifr));
+       os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
+       if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr)) {
+               wpa_printf(MSG_ERROR, "%s: ioctl(SIOCGIFHWADDR): %d (%s)",
+                          ifname, errno, strerror(errno));
+               return -1;
+       }
+
+       if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
+               wpa_printf(MSG_ERROR, "%s: Invalid HW-addr family 0x%04x",
+                          ifname, ifr.ifr_hwaddr.sa_family);
+               return -1;
+       }
+       os_memcpy(addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
+
+       return 0;
+}
+
+
+static int set_ifhwaddr(struct wpa_driver_nl80211_data *drv,
+                       const char *ifname, const u8 *addr)
+{
+       struct ifreq ifr;
+
+       os_memset(&ifr, 0, sizeof(ifr));
+       os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
+       os_memcpy(ifr.ifr_hwaddr.sa_data, addr, ETH_ALEN);
+       ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
+
+       if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
+               wpa_printf(MSG_DEBUG, "%s: ioctl(SIOCSIFHWADDR): %d (%s)",
+                          ifname, errno, strerror(errno));
+               return -1;
+       }
+
+       return 0;
+}
+#endif /* HOSTAPD */
+
+
+#ifndef HOSTAPD
+
 static int wpa_driver_nl80211_send_oper_ifla(
        struct wpa_driver_nl80211_data *drv,
        int linkmode, int operstate)
@@ -348,6 +386,7 @@ static int wpa_driver_nl80211_send_oper_ifla(
 }
 
 
+#ifndef NO_WEXT
 static int wpa_driver_nl80211_set_auth_param(
        struct wpa_driver_nl80211_data *drv, int idx, u32 value)
 {
@@ -370,6 +409,7 @@ static int wpa_driver_nl80211_set_auth_param(
 
        return ret;
 }
+#endif /* NO_WEXT */
 
 
 static int wpa_driver_nl80211_get_bssid(void *priv, u8 *bssid)
@@ -392,7 +432,6 @@ static int wpa_driver_nl80211_get_ssid(void *priv, u8 *ssid)
 }
 
 
-#ifndef HOSTAPD
 static void wpa_driver_nl80211_event_link(struct wpa_driver_nl80211_data *drv,
                                          void *ctx, char *buf, size_t len,
                                          int del)
@@ -740,11 +779,6 @@ static void mlme_event(struct wpa_driver_nl80211_data *drv,
 static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv,
                                           struct nlattr *tb[])
 {
-#ifdef HOSTAPD
-       if (tb[NL80211_ATTR_MAC])
-               hostapd_michael_mic_failure(drv->hapd,
-                                           nla_data(tb[NL80211_ATTR_MAC]));
-#else /* HOSTAPD */
        union wpa_event_data data;
 
        wpa_printf(MSG_DEBUG, "nl80211: MLME event Michael MIC failure");
@@ -753,6 +787,7 @@ static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv,
                wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
                            nla_data(tb[NL80211_ATTR_MAC]),
                            nla_len(tb[NL80211_ATTR_MAC]));
+               data.michael_mic_failure.src = nla_data(tb[NL80211_ATTR_MAC]);
        }
        if (tb[NL80211_ATTR_KEY_SEQ]) {
                wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
@@ -774,7 +809,6 @@ static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv,
        }
 
        wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
-#endif /* HOSTAPD */
 }
 
 
@@ -797,8 +831,14 @@ static int process_event(struct nl_msg *msg, void *arg)
                }
        }
 
+       if (drv->ap_scan_as_station &&
+           (gnlh->cmd == NL80211_CMD_NEW_SCAN_RESULTS ||
+            gnlh->cmd == NL80211_CMD_SCAN_ABORTED)) {
+               wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_AP);
+               drv->ap_scan_as_station = 0;
+       }
+
        switch (gnlh->cmd) {
-#ifndef HOSTAPD
        case NL80211_CMD_NEW_SCAN_RESULTS:
                wpa_printf(MSG_DEBUG, "nl80211: New scan results available");
                drv->scan_complete_events = 1;
@@ -816,6 +856,7 @@ static int process_event(struct nl_msg *msg, void *arg)
                                     drv->ctx);
                wpa_supplicant_event(drv->ctx, EVENT_SCAN_RESULTS, NULL);
                break;
+#ifndef HOSTAPD
        case NL80211_CMD_AUTHENTICATE:
        case NL80211_CMD_ASSOCIATE:
        case NL80211_CMD_DEAUTHENTICATE:
@@ -926,6 +967,7 @@ nla_put_failure:
 }
 
 
+#ifndef HOSTAPD
 struct wiphy_info_data {
        int max_scan_ssids;
        int ap_supported;
@@ -1005,30 +1047,15 @@ static void wpa_driver_nl80211_capa(struct wpa_driver_nl80211_data *drv)
        if (info.ap_supported)
                drv->capa.flags |= WPA_DRIVER_FLAGS_AP;
 }
+#endif /* HOSTAPD */
 
 
-/**
- * wpa_driver_nl80211_init - Initialize nl80211 driver interface
- * @ctx: context to be used when calling wpa_supplicant functions,
- * e.g., wpa_supplicant_event()
- * @ifname: interface name, e.g., wlan0
- * Returns: Pointer to private data, %NULL on failure
- */
-static void * wpa_driver_nl80211_init(void *ctx, const char *ifname)
+static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv,
+                                     void *ctx)
 {
-       int s, ret;
-       struct sockaddr_nl local;
-       struct wpa_driver_nl80211_data *drv;
+       int ret;
 
-       drv = os_zalloc(sizeof(*drv));
-       if (drv == NULL)
-               return NULL;
-       drv->ctx = ctx;
-       os_strlcpy(drv->ifname, ifname, sizeof(drv->ifname));
-#ifdef CONFIG_AP
-       drv->monitor_ifidx = -1;
-       drv->monitor_sock = -1;
-#endif /* CONFIG_AP */
+       /* Initialize generic netlink and nl80211 */
 
        drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
        if (drv->nl_cb == NULL) {
@@ -1050,12 +1077,21 @@ static void * wpa_driver_nl80211_init(void *ctx, const char *ifname)
                goto err3;
        }
 
+#ifdef CONFIG_LIBNL20
+       if (genl_ctrl_alloc_cache(drv->nl_handle, &drv->nl_cache) < 0) {
+               wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
+                          "netlink cache");
+               goto err3;
+       }
+#else /* CONFIG_LIBNL20 */
        drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
        if (drv->nl_cache == NULL) {
                wpa_printf(MSG_ERROR, "nl80211: Failed to allocate generic "
                           "netlink cache");
                goto err3;
        }
+#endif /* CONFIG_LIBNL20 */
+
 
        drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
        if (drv->nl80211 == NULL) {
@@ -1083,21 +1119,36 @@ static void * wpa_driver_nl80211_init(void *ctx, const char *ifname)
                           ret, strerror(-ret));
                goto err4;
        }
-       drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
 
        eloop_register_read_sock(nl_socket_get_fd(drv->nl_handle),
                                 wpa_driver_nl80211_event_receive, drv, ctx);
 
-       drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
-       if (drv->ioctl_sock < 0) {
-               perror("socket(PF_INET,SOCK_DGRAM)");
-               goto err5;
-       }
+       return 0;
+
+err4:
+       nl_cache_free(drv->nl_cache);
+err3:
+       nl_handle_destroy(drv->nl_handle);
+err2:
+       nl_cb_put(drv->nl_cb);
+err1:
+       return -1;
+}
+
+
+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)");
-               goto err6;
+               return -1;
        }
 
        os_memset(&local, 0, sizeof(local));
@@ -1106,34 +1157,71 @@ static void * wpa_driver_nl80211_init(void *ctx, const char *ifname)
        if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
                perror("bind(netlink)");
                close(s);
-               goto err6;
+               return -1;
        }
 
-#ifndef HOSTAPD
        eloop_register_read_sock(s, wpa_driver_nl80211_event_receive_link, drv,
-                                ctx);
-#endif /* HOSTAPD */
+                                drv->ctx);
        drv->link_event_sock = s;
 
-       if (wpa_driver_nl80211_finish_drv_init(drv))
-               goto err7;
+       return 0;
+#endif /* HOSTAPD */
+}
+
+
+/**
+ * wpa_driver_nl80211_init - Initialize nl80211 driver interface
+ * @ctx: context to be used when calling wpa_supplicant functions,
+ * e.g., wpa_supplicant_event()
+ * @ifname: interface name, e.g., wlan0
+ * Returns: Pointer to private data, %NULL on failure
+ */
+static void * wpa_driver_nl80211_init(void *ctx, const char *ifname)
+{
+       struct wpa_driver_nl80211_data *drv;
+
+       drv = os_zalloc(sizeof(*drv));
+       if (drv == NULL)
+               return NULL;
+       drv->ctx = ctx;
+       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)) {
+               os_free(drv);
+               return NULL;
+       }
+
+       drv->capa.flags |= WPA_DRIVER_FLAGS_SME;
+
+       drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
+       if (drv->ioctl_sock < 0) {
+               perror("socket(PF_INET,SOCK_DGRAM)");
+               goto failed;
+       }
+
+       if (wpa_driver_nl80211_init_link_events(drv) ||
+           wpa_driver_nl80211_finish_drv_init(drv))
+               goto failed;
 
        return drv;
 
-err7:
-       eloop_unregister_read_sock(drv->link_event_sock);
-       close(drv->link_event_sock);
-err6:
-       close(drv->ioctl_sock);
-err5:
+failed:
+       if (drv->link_event_sock >= 0) {
+               eloop_unregister_read_sock(drv->link_event_sock);
+               close(drv->link_event_sock);
+       }
+       if (drv->ioctl_sock >= 0)
+               close(drv->ioctl_sock);
+
        genl_family_put(drv->nl80211);
-err4:
        nl_cache_free(drv->nl_cache);
-err3:
        nl_handle_destroy(drv->nl_handle);
-err2:
        nl_cb_put(drv->nl_cb);
-err1:
+
        os_free(drv);
        return NULL;
 }
@@ -1144,7 +1232,8 @@ wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
 {
        drv->ifindex = if_nametoindex(drv->ifname);
 
-       if (wpa_driver_nl80211_set_mode(drv, 0) < 0) {
+#ifndef HOSTAPD
+       if (wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_INFRA) < 0) {
                wpa_printf(MSG_DEBUG, "nl80211: Could not configure driver to "
                           "use managed mode");
        }
@@ -1158,11 +1247,46 @@ wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
        wpa_driver_nl80211_capa(drv);
 
        wpa_driver_nl80211_send_oper_ifla(drv, 1, IF_OPER_DORMANT);
+#endif /* HOSTAPD */
 
        return 0;
 }
 
 
+#ifdef HOSTAPD
+static void wpa_driver_nl80211_free_bss(struct wpa_driver_nl80211_data *drv)
+{
+       struct i802_bss *bss, *prev;
+       bss = drv->bss.next;
+       while (bss) {
+               prev = bss;
+               bss = bss->next;
+               os_free(bss);
+       }
+}
+#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;
+
+       msg = nlmsg_alloc();
+       if (!msg)
+               return -ENOMEM;
+
+       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
+                   0, NL80211_CMD_DEL_BEACON, 0);
+       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
+
+       return send_and_recv_msgs(drv, msg, NULL, NULL);
+ nla_put_failure:
+       return -ENOBUFS;
+}
+#endif /* CONFIG_AP || HOSTAPD */
+
+
 /**
  * wpa_driver_nl80211_deinit - Deinitialize nl80211 driver interface
  * @priv: Pointer to private nl80211 data from wpa_driver_nl80211_init()
@@ -1174,28 +1298,55 @@ static void wpa_driver_nl80211_deinit(void *priv)
 {
        struct wpa_driver_nl80211_data *drv = priv;
 
-#ifdef CONFIG_AP
-       if (drv->monitor_ifidx >= 0)
-               nl80211_remove_iface(drv, drv->monitor_ifidx);
+#if defined(CONFIG_AP) || defined(HOSTAPD)
+       nl80211_remove_monitor_interface(drv);
        if (drv->monitor_sock >= 0) {
                eloop_unregister_read_sock(drv->monitor_sock);
                close(drv->monitor_sock);
        }
-#endif /* CONFIG_AP */
 
-       eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
+       if (drv->nlmode == NL80211_IFTYPE_AP)
+               wpa_driver_nl80211_del_beacon(drv);
+#endif /* CONFIG_AP || HOSTAPD */
+
+#ifdef HOSTAPD
+       if (drv->last_freq_ht) {
+               /* Clear HT flags from the driver */
+               struct hostapd_freq_params freq;
+               os_memset(&freq, 0, sizeof(freq));
+               freq.freq = drv->last_freq;
+               i802_set_freq(priv, &freq);
+       }
+
+       if (drv->eapol_sock >= 0) {
+               eloop_unregister_read_sock(drv->eapol_sock);
+               close(drv->eapol_sock);
+       }
 
+       if (drv->if_indices != drv->default_if_indices)
+               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);
 
-       eloop_unregister_read_sock(drv->link_event_sock);
+       if (drv->link_event_sock >= 0) {
+               eloop_unregister_read_sock(drv->link_event_sock);
+               close(drv->link_event_sock);
+       }
+#endif /* HOSTAPD */
+
+       eloop_cancel_timeout(wpa_driver_nl80211_scan_timeout, drv, drv->ctx);
 
-       hostapd_set_iface_flags(drv, drv->ifname, 0);
-       wpa_driver_nl80211_set_mode(drv, 0);
+       (void) hostapd_set_iface_flags(drv, drv->ifname, 0);
+       wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_INFRA);
 
-       close(drv->link_event_sock);
-       close(drv->ioctl_sock);
+       if (drv->ioctl_sock >= 0)
+               close(drv->ioctl_sock);
 
        eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_handle));
        genl_family_put(drv->nl80211);
@@ -1209,7 +1360,7 @@ static void wpa_driver_nl80211_deinit(void *priv)
 
 /**
  * wpa_driver_nl80211_scan_timeout - Scan timeout to report scan completion
- * @eloop_ctx: Unused
+ * @eloop_ctx: Driver private data
  * @timeout_ctx: ctx argument given to wpa_driver_nl80211_init()
  *
  * This function can be used as registered timeout when starting a scan to
@@ -1217,16 +1368,19 @@ static void wpa_driver_nl80211_deinit(void *priv)
  */
 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
 {
+       struct wpa_driver_nl80211_data *drv = eloop_ctx;
+       if (drv->ap_scan_as_station) {
+               wpa_driver_nl80211_set_mode(drv, IEEE80211_MODE_AP);
+               drv->ap_scan_as_station = 0;
+       }
        wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
-#ifndef HOSTAPD
        wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
-#endif /* HOSTAPD */
 }
 
 
 /**
  * wpa_driver_nl80211_scan - Request the driver to initiate scan
- * @priv: Pointer to private wext data from wpa_driver_nl80211_init()
+ * @priv: Pointer to private driver data from wpa_driver_nl80211_init()
  * @params: Scan parameters
  * Returns: 0 on success, -1 on failure
  */
@@ -1276,7 +1430,30 @@ static int wpa_driver_nl80211_scan(void *priv,
        if (ret) {
                wpa_printf(MSG_DEBUG, "nl80211: Scan trigger failed: ret=%d "
                           "(%s)", ret, strerror(-ret));
+#ifdef HOSTAPD
+               if (drv->nlmode == NL80211_IFTYPE_AP) {
+                       /*
+                        * mac80211 does not allow scan requests in AP mode, so
+                        * try to do this in station mode.
+                        */
+                       if (wpa_driver_nl80211_set_mode(drv,
+                                                       IEEE80211_MODE_INFRA))
+                               goto nla_put_failure;
+
+                       if (wpa_driver_nl80211_scan(drv, params)) {
+                               wpa_driver_nl80211_set_mode(drv,
+                                                           IEEE80211_MODE_AP);
+                               goto nla_put_failure;
+                       }
+
+                       /* Restore AP mode when processing scan results */
+                       drv->ap_scan_as_station = 1;
+                       ret = 0;
+               } else
+                       goto nla_put_failure;
+#else /* HOSTAPD */
                goto nla_put_failure;
+#endif /* HOSTAPD */
        }
 
        /* Not all drivers generate "scan completed" wireless event, so try to
@@ -1526,6 +1703,8 @@ nla_put_failure:
 }
 
 
+#ifndef HOSTAPD
+
 static int wpa_driver_nl80211_set_key(void *priv, wpa_alg alg,
                                      const u8 *addr, int key_idx,
                                      int set_tx, const u8 *seq,
@@ -1574,6 +1753,7 @@ static int wpa_driver_nl80211_deauthenticate(void *priv, const u8 *addr,
 {
        struct wpa_driver_nl80211_data *drv = priv;
        wpa_printf(MSG_DEBUG, "%s", __func__);
+       drv->associated = 0;
        return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DEAUTHENTICATE,
                                       reason_code);
 }
@@ -1584,6 +1764,7 @@ static int wpa_driver_nl80211_disassociate(void *priv, const u8 *addr,
 {
        struct wpa_driver_nl80211_data *drv = priv;
        wpa_printf(MSG_DEBUG, "%s", __func__);
+       drv->associated = 0;
        return wpa_driver_nl80211_mlme(drv, addr, NL80211_CMD_DISASSOCIATE,
                                       reason_code);
 }
@@ -1670,6 +1851,8 @@ nla_put_failure:
        return ret;
 }
 
+#endif /* HOSTAPD */
+
 
 #if defined(CONFIG_AP) || defined(HOSTAPD)
 
@@ -2139,10 +2322,77 @@ nla_put_failure:
        return -1;
 }
 
-#endif /* CONFIG_AP || HOSTAPD */
 
+static int wpa_driver_nl80211_sta_add(const char *ifname, void *priv,
+                                     struct hostapd_sta_add_params *params)
+{
+       struct wpa_driver_nl80211_data *drv = priv;
+       struct nl_msg *msg;
+       int ret = -ENOBUFS;
+
+       msg = nlmsg_alloc();
+       if (!msg)
+               return -ENOMEM;
+
+       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
+                   0, NL80211_CMD_NEW_STATION, 0);
+
+       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(ifname));
+       NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
+       NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
+       NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
+               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);
+       }
+#endif /* CONFIG_IEEE80211N */
+
+       ret = send_and_recv_msgs(drv, msg, NULL, NULL);
+       if (ret)
+               wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_NEW_STATION "
+                          "result: %d (%s)", ret, strerror(-ret));
+       if (ret == -EEXIST)
+               ret = 0;
+ nla_put_failure:
+       return ret;
+}
 
-#ifdef CONFIG_AP
+
+static int wpa_driver_nl80211_sta_remove(void *priv, const u8 *addr)
+{
+       struct wpa_driver_nl80211_data *drv = priv;
+       struct nl_msg *msg;
+       int ret;
+
+       msg = nlmsg_alloc();
+       if (!msg)
+               return -ENOMEM;
+
+       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
+                   0, NL80211_CMD_DEL_STATION, 0);
+
+       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
+                   if_nametoindex(drv->ifname));
+       NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
+
+       ret = send_and_recv_msgs(drv, msg, NULL, NULL);
+       if (ret == -ENOENT)
+               return 0;
+       return ret;
+ nla_put_failure:
+       return -ENOBUFS;
+}
+
+#endif /* CONFIG_AP || HOSTAPD */
+
+
+#ifdef CONFIG_AP
 
 static int wpa_driver_nl80211_set_beacon(void *priv,
                                         const u8 *head, size_t head_len,
@@ -2193,10 +2443,6 @@ static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
        struct nl_msg *msg, *flags = NULL;
        int ifidx;
        int ret = -ENOBUFS;
-#ifdef HOSTAPD
-       struct ifreq ifreq;
-       struct iwreq iwr;
-#endif /* HOSTAPD */
 
        msg = nlmsg_alloc();
        if (!msg)
@@ -2242,30 +2488,10 @@ static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
        /* start listening for EAPOL on this interface */
        add_ifidx(drv, ifidx);
 
-       if (addr) {
-               switch (iftype) {
-               case NL80211_IFTYPE_AP:
-                       os_strlcpy(ifreq.ifr_name, ifname, IFNAMSIZ);
-                       memcpy(ifreq.ifr_hwaddr.sa_data, addr, ETH_ALEN);
-                       ifreq.ifr_hwaddr.sa_family = ARPHRD_ETHER;
-
-                       if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifreq)) {
-                               nl80211_remove_iface(drv, ifidx);
-                               return -1;
-                       }
-                       break;
-               case NL80211_IFTYPE_WDS:
-                       memset(&iwr, 0, sizeof(iwr));
-                       os_strlcpy(iwr.ifr_name, ifname, IFNAMSIZ);
-                       iwr.u.addr.sa_family = ARPHRD_ETHER;
-                       memcpy(iwr.u.addr.sa_data, addr, ETH_ALEN);
-                       if (ioctl(drv->ioctl_sock, SIOCSIWAP, &iwr))
-                               return -1;
-                       break;
-               default:
-                       /* nothing */
-                       break;
-               }
+       if (addr && iftype == NL80211_IFTYPE_AP &&
+           set_ifhwaddr(drv, ifname, addr)) {
+               nl80211_remove_iface(drv, ifidx);
+               return -1;
        }
 #endif /* HOSTAPD */
 
@@ -2327,139 +2553,46 @@ static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
 }
 
 
-static void handle_frame(struct wpa_driver_nl80211_data *drv,
-                        u8 *buf, size_t len,
-                        struct hostapd_frame_info *hfi,
-                        enum ieee80211_msg_type msg_type)
+static void from_unknown_sta(struct wpa_driver_nl80211_data *drv,
+                            struct ieee80211_hdr *hdr, size_t len)
 {
-       struct ieee80211_hdr *hdr;
-       u16 fc, type, stype;
-       size_t data_len = len;
-       u8 *bssid;
-       void *ctx = drv->ctx;
 #ifdef HOSTAPD
-       struct hostapd_iface *iface = drv->hapd->iface;
-       struct hostapd_data *hapd = NULL;
-       int broadcast_bssid = 0;
-       size_t i;
+       hostapd_rx_from_unknown_sta(drv->ctx, hdr, len);
+#else /* HOSTAPD */
+       ap_rx_from_unknown_sta(drv->ctx, hdr->addr2);
 #endif /* HOSTAPD */
+}
 
-       /*
-        * PS-Poll frames are 16 bytes. All other frames are
-        * 24 bytes or longer.
-        */
-       if (len < 16)
-               return;
+
+static void handle_frame(struct wpa_driver_nl80211_data *drv,
+                        u8 *buf, size_t len,
+                        struct hostapd_frame_info *hfi)
+{
+       struct ieee80211_hdr *hdr;
+       u16 fc, stype;
 
        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_DATA:
-               if (len < 24)
-                       return;
-               switch (fc & (WLAN_FC_FROMDS | WLAN_FC_TODS)) {
-               case WLAN_FC_TODS:
-                       bssid = hdr->addr1;
-                       break;
-               case WLAN_FC_FROMDS:
-                       bssid = hdr->addr2;
-                       break;
-               default:
-                       /* discard */
-                       return;
-               }
-               break;
-       case WLAN_FC_TYPE_CTRL:
-               /* discard non-ps-poll frames */
-               if (stype != WLAN_FC_STYPE_PSPOLL)
-                       return;
-               bssid = hdr->addr1;
-               break;
-       case WLAN_FC_TYPE_MGMT:
-               bssid = hdr->addr3;
-               break;
-       default:
-               /* discard */
-               return;
-       }
-
-#ifdef HOSTAPD
-       /* find interface frame belongs to */
-       for (i = 0; i < iface->num_bss; i++) {
-               if (memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0) {
-                       hapd = iface->bss[i];
-                       break;
-               }
-       }
-
-       if (hapd == NULL) {
-               hapd = iface->bss[0];
-
-               if (bssid[0] != 0xff || bssid[1] != 0xff ||
-                   bssid[2] != 0xff || bssid[3] != 0xff ||
-                   bssid[4] != 0xff || bssid[5] != 0xff) {
-                       /*
-                        * Unknown BSSID - drop frame if this is not from
-                        * passive scanning or a beacon (at least ProbeReq
-                        * frames to other APs may be allowed through RX
-                        * filtering in the wlan hw/driver)
-                        */
-                       if ((type != WLAN_FC_TYPE_MGMT ||
-                            stype != WLAN_FC_STYPE_BEACON))
-                               return;
-               } else
-                       broadcast_bssid = 1;
-       }
-       ctx = hapd;
-#endif /* HOSTAPD */
-
-       switch (msg_type) {
-       case ieee80211_msg_normal:
-               /* continue processing */
-               break;
-       case ieee80211_msg_tx_callback_ack:
-               handle_tx_callback(ctx, buf, data_len, 1);
-               return;
-       case ieee80211_msg_tx_callback_fail:
-               handle_tx_callback(ctx, buf, data_len, 0);
-               return;
-       }
-
-       switch (type) {
+       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
-               if (broadcast_bssid) {
-                       for (i = 0; i < iface->num_bss; i++)
-                               hostapd_mgmt_rx(iface->bss[i], buf, data_len,
-                                               stype, hfi);
-               } else
-                       hostapd_mgmt_rx(hapd, buf, data_len, stype, hfi);
+               hostapd_mgmt_rx(drv->ctx, buf, len, stype, hfi);
 #else /* HOSTAPD */
-               ap_mgmt_rx(drv->ctx, buf, data_len, stype, hfi);
+               ap_mgmt_rx(drv->ctx, buf, len, stype, hfi);
 #endif /* HOSTAPD */
                break;
        case WLAN_FC_TYPE_CTRL:
                /* can only get here with PS-Poll frames */
                wpa_printf(MSG_DEBUG, "CTRL");
-#ifdef HOSTAPD
-               hostapd_rx_from_unknown_sta(drv->hapd, hdr->addr2);
-#else /* HOSTAPD */
-               ap_rx_from_unknown_sta(drv->ctx, hdr->addr2);
-#endif /* HOSTAPD */
+               from_unknown_sta(drv, hdr, len);
                break;
        case WLAN_FC_TYPE_DATA:
-#ifdef HOSTAPD
-               hostapd_rx_from_unknown_sta(drv->hapd, hdr->addr2);
-#else /* HOSTAPD */
-               ap_rx_from_unknown_sta(drv->ctx, hdr->addr2);
-#endif /* HOSTAPD */
+               from_unknown_sta(drv, hdr, len);
                break;
        }
 }
@@ -2473,7 +2606,7 @@ static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
        struct ieee80211_radiotap_iterator iter;
        int ret;
        struct hostapd_frame_info hfi;
-       int injected = 0, failed = 0, msg_type, rxflags = 0;
+       int injected = 0, failed = 0, rxflags = 0;
 
        len = recv(sock, buf, sizeof(buf), 0);
        if (len < 0) {
@@ -2530,14 +2663,11 @@ static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
                return;
 
        if (!injected)
-               msg_type = ieee80211_msg_normal;
-       else if (failed)
-               msg_type = ieee80211_msg_tx_callback_fail;
+               handle_frame(drv, buf + iter.max_length,
+                            len - iter.max_length, &hfi);
        else
-               msg_type = ieee80211_msg_tx_callback_ack;
-
-       handle_frame(drv, buf + iter.max_length,
-                    len - iter.max_length, &hfi, msg_type);
+               handle_tx_callback(drv->ctx, buf + iter.max_length,
+                                  len - iter.max_length, !failed);
 }
 
 
@@ -2685,6 +2815,16 @@ static int add_monitor_filter(int s)
 }
 
 
+static void nl80211_remove_monitor_interface(
+       struct wpa_driver_nl80211_data *drv)
+{
+       if (drv->monitor_ifidx >= 0) {
+               nl80211_remove_iface(drv, drv->monitor_ifidx);
+               drv->monitor_ifidx = -1;
+       }
+}
+
+
 static int
 nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
 {
@@ -2741,10 +2881,128 @@ nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
 
        return 0;
  error:
-       nl80211_remove_iface(drv, drv->monitor_ifidx);
+       nl80211_remove_monitor_interface(drv);
        return -1;
 }
 
+
+static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
+
+static int wpa_driver_nl80211_hapd_send_eapol(
+       void *priv, const u8 *addr, const u8 *data,
+       size_t data_len, int encrypt, const u8 *own_addr)
+{
+       struct wpa_driver_nl80211_data *drv = priv;
+       struct ieee80211_hdr *hdr;
+       size_t len;
+       u8 *pos;
+       int res;
+#if 0 /* FIX */
+       int qos = sta->flags & WLAN_STA_WME;
+#else
+       int qos = 0;
+#endif
+
+       len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
+               data_len;
+       hdr = os_zalloc(len);
+       if (hdr == NULL) {
+               printf("malloc() failed for i802_send_data(len=%lu)\n",
+                      (unsigned long) len);
+               return -1;
+       }
+
+       hdr->frame_control =
+               IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
+       hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
+       if (encrypt)
+               hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
+#if 0 /* To be enabled if qos determination is added above */
+       if (qos) {
+               hdr->frame_control |=
+                       host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
+       }
+#endif
+
+       memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
+       memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
+       memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
+       pos = (u8 *) (hdr + 1);
+
+#if 0 /* To be enabled if qos determination is added above */
+       if (qos) {
+               /* add an empty QoS header if needed */
+               pos[0] = 0;
+               pos[1] = 0;
+               pos += 2;
+       }
+#endif
+
+       memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
+       pos += sizeof(rfc1042_header);
+       WPA_PUT_BE16(pos, ETH_P_PAE);
+       pos += 2;
+       memcpy(pos, data, data_len);
+
+       res = wpa_driver_nl80211_send_frame(drv, (u8 *) hdr, len, encrypt);
+       if (res < 0) {
+               wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
+                          "failed: %d (%s)",
+                          (unsigned long) len, errno, strerror(errno));
+       }
+       free(hdr);
+
+       return res;
+}
+
+
+static int wpa_driver_nl80211_sta_set_flags(void *priv, const u8 *addr,
+                                           int total_flags, int flags_or,
+                                           int flags_and)
+{
+       struct wpa_driver_nl80211_data *drv = priv;
+       struct nl_msg *msg, *flags = NULL;
+
+       msg = nlmsg_alloc();
+       if (!msg)
+               return -ENOMEM;
+
+       flags = nlmsg_alloc();
+       if (!flags) {
+               nlmsg_free(msg);
+               return -ENOMEM;
+       }
+
+       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
+                   0, NL80211_CMD_SET_STATION, 0);
+
+       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
+                   if_nametoindex(drv->ifname));
+       NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
+
+       if (total_flags & WLAN_STA_AUTHORIZED)
+               NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
+
+       if (total_flags & WLAN_STA_WMM)
+               NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
+
+       if (total_flags & WLAN_STA_SHORT_PREAMBLE)
+               NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
+
+       if (total_flags & WLAN_STA_MFP)
+               NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
+
+       if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
+               goto nla_put_failure;
+
+       nlmsg_free(flags);
+
+       return send_and_recv_msgs(drv, msg, NULL, NULL);
+ nla_put_failure:
+       nlmsg_free(flags);
+       return -ENOBUFS;
+}
+
 #endif /* CONFIG_AP || HOSTAPD */
 
 #ifdef CONFIG_AP
@@ -2752,14 +3010,9 @@ nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
 static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
                                 struct wpa_driver_associate_params *params)
 {
-       if (drv->monitor_ifidx < 0 &&
-           nl80211_create_monitor_interface(drv))
-               return -1;
-
        if (wpa_driver_nl80211_set_mode(drv, params->mode) ||
            wpa_driver_nl80211_set_freq(drv, params->freq, 0, 0)) {
-               nl80211_remove_iface(drv, drv->monitor_ifidx);
-               drv->monitor_ifidx = -1;
+               nl80211_remove_monitor_interface(drv);
                return -1;
        }
 
@@ -2771,6 +3024,7 @@ static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
 #endif /* CONFIG_AP */
 
 
+#ifndef HOSTAPD
 static int wpa_driver_nl80211_associate(
        void *priv, struct wpa_driver_associate_params *params)
 {
@@ -2783,8 +3037,10 @@ static int wpa_driver_nl80211_associate(
                return wpa_driver_nl80211_ap(drv, params);
 #endif /* CONFIG_AP */
 
+#ifndef NO_WEXT
        wpa_driver_nl80211_set_auth_param(drv, IW_AUTH_DROP_UNENCRYPTED,
                                          params->drop_unencrypted);
+#endif /* NO_WEXT */
 
        drv->associated = 0;
 
@@ -2837,19 +3093,38 @@ nla_put_failure:
        nlmsg_free(msg);
        return ret;
 }
+#endif /* HOSTAPD */
 
 
-/**
- * wpa_driver_nl80211_set_mode - Set wireless mode (infra/adhoc)
- * @drv: Pointer to private driver data from wpa_driver_nl80211_init()
- * @mode: 0 = infra/BSS (associate with an AP), 1 = adhoc/IBSS
- * Returns: 0 on success, -1 on failure
- */
-static int wpa_driver_nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
-                                      int mode)
+static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
+                           int ifindex, int mode)
 {
-       int ret = -1;
        struct nl_msg *msg;
+       int ret = -ENOBUFS;
+
+       msg = nlmsg_alloc();
+       if (!msg)
+               return -ENOMEM;
+
+       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
+                   0, NL80211_CMD_SET_INTERFACE, 0);
+       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
+       NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
+
+       ret = send_and_recv_msgs(drv, msg, NULL, NULL);
+       if (!ret)
+               return 0;
+nla_put_failure:
+       wpa_printf(MSG_DEBUG, "nl80211: Failed to set interface %d to mode %d:"
+                  " %d (%s)", ifindex, mode, ret, strerror(-ret));
+       return ret;
+}
+
+
+static int wpa_driver_nl80211_set_mode(void *priv, int mode)
+{
+       struct wpa_driver_nl80211_data *drv = priv;
+       int ret = -1;
        int nlmode;
 
        switch (mode) {
@@ -2866,56 +3141,50 @@ static int wpa_driver_nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
                return -1;
        }
 
-       msg = nlmsg_alloc();
-       if (!msg)
-               return -1;
-
-       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
-                   0, NL80211_CMD_SET_INTERFACE, 0);
-       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
-       NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, nlmode);
-
-       ret = send_and_recv_msgs(drv, msg, NULL, NULL);
-       if (!ret)
-               return 0;
-       else
-               goto try_again;
+       if (nl80211_set_mode(drv, drv->ifindex, nlmode) == 0) {
+               drv->nlmode = nlmode;
+               ret = 0;
+               goto done;
+       }
 
-nla_put_failure:
-       wpa_printf(MSG_ERROR, "nl80211: Failed to set interface mode: %d (%s)",
-                  ret, strerror(-ret));
-       return -1;
+       if (nlmode == drv->nlmode) {
+               ret = 0;
+               goto done; /* Already in the requested mode */
+       }
 
-try_again:
        /* mac80211 doesn't allow mode changes while the device is up, so
         * take the device down, try to set the mode again, and bring the
         * device back up.
         */
        if (hostapd_set_iface_flags(drv, drv->ifname, 0) == 0) {
                /* Try to set the mode again while the interface is down */
-               msg = nlmsg_alloc();
-               if (!msg)
-                       return -1;
-
-               genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
-                           0, NL80211_CMD_SET_INTERFACE, 0);
-               NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
-               NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, nlmode);
-               ret = send_and_recv_msgs(drv, msg, NULL, NULL);
-               if (ret) {
-                       wpa_printf(MSG_ERROR, "Failed to set interface %s "
-                                  "mode(try_again): %d (%s)",
-                                  drv->ifname, ret, strerror(-ret));
-               }
-
+               ret = nl80211_set_mode(drv, drv->ifindex, nlmode);
                if (hostapd_set_iface_flags(drv, drv->ifname, 1))
                        ret = -1;
        }
 
+       if (!ret)
+               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 &&
+                   nl80211_create_monitor_interface(drv))
+                       return -1;
+       } else if (!ret && nlmode != NL80211_IFTYPE_AP) {
+               /* Remove additional AP mode functionality */
+               nl80211_remove_monitor_interface(drv);
+       }
+#endif /* CONFIG_AP || HOSTAPD */
+
        return ret;
 }
 
 
+#ifndef HOSTAPD
+
 static int wpa_driver_nl80211_get_capa(void *priv,
                                       struct wpa_driver_capa *capa)
 {
@@ -2938,14 +3207,10 @@ static int wpa_driver_nl80211_set_operstate(void *priv, int state)
                drv, -1, state ? IF_OPER_UP : IF_OPER_DORMANT);
 }
 
+#endif /* HOSTAPD */
 
-#ifdef HOSTAPD
-
-static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
-
-static int i802_sta_deauth(void *priv, const u8 *addr, int reason);
-static int i802_sta_disassoc(void *priv, const u8 *addr, int reason);
 
+#ifdef HOSTAPD
 
 static struct i802_bss * get_bss(struct wpa_driver_nl80211_data *drv,
                                 int ifindex)
@@ -3128,6 +3393,9 @@ static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
 
 static int i802_set_rts(void *priv, int rts)
 {
+#ifdef NO_WEXT
+       return -1;
+#else /* NO_WEXT */
        struct wpa_driver_nl80211_data *drv = priv;
        struct iwreq iwr;
 
@@ -3142,11 +3410,15 @@ static int i802_set_rts(void *priv, int rts)
        }
 
        return 0;
+#endif /* NO_WEXT */
 }
 
 
 static int i802_set_frag(void *priv, int frag)
 {
+#ifdef NO_WEXT
+       return -1;
+#else /* NO_WEXT */
        struct wpa_driver_nl80211_data *drv = priv;
        struct iwreq iwr;
 
@@ -3161,32 +3433,7 @@ static int i802_set_frag(void *priv, int frag)
        }
 
        return 0;
-}
-
-
-static int i802_set_retry(void *priv, int short_retry, int long_retry)
-{
-       struct wpa_driver_nl80211_data *drv = priv;
-       struct iwreq iwr;
-
-       memset(&iwr, 0, sizeof(iwr));
-       os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
-
-       iwr.u.retry.value = short_retry;
-       iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
-       if (ioctl(drv->ioctl_sock, SIOCSIWRETRY, &iwr) < 0) {
-               perror("ioctl[SIOCSIWRETRY(short)]");
-               return -1;
-       }
-
-       iwr.u.retry.value = long_retry;
-       iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
-       if (ioctl(drv->ioctl_sock, SIOCSIWRETRY, &iwr) < 0) {
-               perror("ioctl[SIOCSIWRETRY(long)]");
-               return -1;
-       }
-
-       return 0;
+#endif /* NO_WEXT */
 }
 
 
@@ -3288,985 +3535,238 @@ static int i802_read_sta_data(void *priv, struct hostap_sta_driver_data *data,
 }
 
 
-static int i802_send_eapol(void *priv, const u8 *addr, const u8 *data,
-                          size_t data_len, int encrypt, const u8 *own_addr)
+static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
+                                   int cw_min, int cw_max, int burst_time)
 {
        struct wpa_driver_nl80211_data *drv = priv;
-       struct ieee80211_hdr *hdr;
-       size_t len;
-       u8 *pos;
-       int res;
-#if 0 /* FIX */
-       int qos = sta->flags & WLAN_STA_WME;
-#else
-       int qos = 0;
-#endif
+       struct nl_msg *msg;
+       struct nlattr *txq, *params;
 
-       len = sizeof(*hdr) + (qos ? 2 : 0) + sizeof(rfc1042_header) + 2 +
-               data_len;
-       hdr = os_zalloc(len);
-       if (hdr == NULL) {
-               printf("malloc() failed for i802_send_data(len=%lu)\n",
-                      (unsigned long) len);
+       msg = nlmsg_alloc();
+       if (!msg)
                return -1;
-       }
 
-       hdr->frame_control =
-               IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
-       hdr->frame_control |= host_to_le16(WLAN_FC_FROMDS);
-       if (encrypt)
-               hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
-#if 0 /* To be enabled if qos determination is added above */
-       if (qos) {
-               hdr->frame_control |=
-                       host_to_le16(WLAN_FC_STYPE_QOS_DATA << 4);
-       }
-#endif
+       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
+                   0, NL80211_CMD_SET_WIPHY, 0);
 
-       memcpy(hdr->IEEE80211_DA_FROMDS, addr, ETH_ALEN);
-       memcpy(hdr->IEEE80211_BSSID_FROMDS, own_addr, ETH_ALEN);
-       memcpy(hdr->IEEE80211_SA_FROMDS, own_addr, ETH_ALEN);
-       pos = (u8 *) (hdr + 1);
+       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
 
-#if 0 /* To be enabled if qos determination is added above */
-       if (qos) {
-               /* add an empty QoS header if needed */
-               pos[0] = 0;
-               pos[1] = 0;
-               pos += 2;
-       }
-#endif
+       txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
+       if (!txq)
+               goto nla_put_failure;
 
-       memcpy(pos, rfc1042_header, sizeof(rfc1042_header));
-       pos += sizeof(rfc1042_header);
-       WPA_PUT_BE16(pos, ETH_P_PAE);
-       pos += 2;
-       memcpy(pos, data, data_len);
+       /* We are only sending parameters for a single TXQ at a time */
+       params = nla_nest_start(msg, 1);
+       if (!params)
+               goto nla_put_failure;
 
-       res = wpa_driver_nl80211_send_frame(drv, (u8 *) hdr, len, encrypt);
-       if (res < 0) {
-               wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
-                          "failed: %d (%s)",
-                          (unsigned long) len, errno, strerror(errno));
-       }
-       free(hdr);
+       NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, queue);
+       /* Burst time is configured in units of 0.1 msec and TXOP parameter in
+        * 32 usec, so need to convert the value here. */
+       NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
+       NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
+       NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
+       NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
 
-       return res;
+       nla_nest_end(msg, params);
+
+       nla_nest_end(msg, txq);
+
+       if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
+               return 0;
+ nla_put_failure:
+       return -1;
 }
 
 
-static int i802_sta_add(const char *ifname, void *priv,
-                       struct hostapd_sta_add_params *params)
+static int i802_bss_add(void *priv, const char *ifname, const u8 *bssid)
 {
        struct wpa_driver_nl80211_data *drv = priv;
-       struct nl_msg *msg;
-       int ret = -ENOBUFS;
+       int ifidx;
+       struct i802_bss *bss;
 
-       msg = nlmsg_alloc();
-       if (!msg)
-               return -ENOMEM;
+       bss = os_zalloc(sizeof(*bss));
+       if (bss == NULL)
+               return -1;
 
-       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
-                   0, NL80211_CMD_NEW_STATION, 0);
+       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;
+}
 
-       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
-                   if_nametoindex(drv->ifname));
-       NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, params->addr);
-       NLA_PUT_U16(msg, NL80211_ATTR_STA_AID, params->aid);
-       NLA_PUT(msg, NL80211_ATTR_STA_SUPPORTED_RATES, params->supp_rates_len,
-               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);
+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;
        }
-#endif /* CONFIG_IEEE80211N */
+       return 0;
+}
 
-       ret = send_and_recv_msgs(drv, msg, NULL, NULL);
-       if (ret)
-               wpa_printf(MSG_DEBUG, "nl80211: NL80211_CMD_NEW_STATION "
-                          "result: %d (%s)", ret, strerror(-ret));
-       if (ret == -EEXIST)
-               ret = 0;
- nla_put_failure:
-       return ret;
+
+static int i802_set_beacon(const char *iface, void *priv,
+                          const u8 *head, size_t head_len,
+                          const u8 *tail, size_t tail_len, int dtim_period)
+{
+       return wpa_driver_nl80211_set_beacon_iface(if_nametoindex(iface), priv,
+                                                  head, head_len,
+                                                  tail, tail_len,
+                                                  dtim_period);
 }
 
 
-static int i802_sta_remove(void *priv, const u8 *addr)
+static int i802_set_bss(void *priv, int cts, int preamble, int slot)
 {
        struct wpa_driver_nl80211_data *drv = priv;
        struct nl_msg *msg;
-       int ret;
 
        msg = nlmsg_alloc();
        if (!msg)
                return -ENOMEM;
 
-       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
-                   0, NL80211_CMD_DEL_STATION, 0);
+       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
+                   NL80211_CMD_SET_BSS, 0);
 
-       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
-                   if_nametoindex(drv->ifname));
-       NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
-
-       ret = send_and_recv_msgs(drv, msg, NULL, NULL);
-       if (ret == -ENOENT)
-               return 0;
-       return ret;
- nla_put_failure:
-       return -ENOBUFS;
-}
-
-
-static int i802_sta_set_flags(void *priv, const u8 *addr,
-                             int total_flags, int flags_or, int flags_and)
-{
-       struct wpa_driver_nl80211_data *drv = priv;
-       struct nl_msg *msg, *flags = NULL;
-
-       msg = nlmsg_alloc();
-       if (!msg)
-               return -ENOMEM;
-
-       flags = nlmsg_alloc();
-       if (!flags) {
-               nlmsg_free(msg);
-               return -ENOMEM;
-       }
-
-       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
-                   0, NL80211_CMD_SET_STATION, 0);
-
-       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
-                   if_nametoindex(drv->ifname));
-       NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
-
-       if (total_flags & WLAN_STA_AUTHORIZED)
-               NLA_PUT_FLAG(flags, NL80211_STA_FLAG_AUTHORIZED);
-
-       if (total_flags & WLAN_STA_WMM)
-               NLA_PUT_FLAG(flags, NL80211_STA_FLAG_WME);
-
-       if (total_flags & WLAN_STA_SHORT_PREAMBLE)
-               NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
-
-       if (total_flags & WLAN_STA_MFP)
-               NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
-
-       if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
-               goto nla_put_failure;
-
-       nlmsg_free(flags);
-
-       return send_and_recv_msgs(drv, msg, NULL, NULL);
- nla_put_failure:
-       nlmsg_free(flags);
-       return -ENOBUFS;
-}
-
-
-static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
-                                   int cw_min, int cw_max, int burst_time)
-{
-       struct wpa_driver_nl80211_data *drv = priv;
-       struct nl_msg *msg;
-       struct nlattr *txq, *params;
-
-       msg = nlmsg_alloc();
-       if (!msg)
-               return -1;
-
-       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
-                   0, NL80211_CMD_SET_WIPHY, 0);
-
-       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
-
-       txq = nla_nest_start(msg, NL80211_ATTR_WIPHY_TXQ_PARAMS);
-       if (!txq)
-               goto nla_put_failure;
-
-       /* We are only sending parameters for a single TXQ at a time */
-       params = nla_nest_start(msg, 1);
-       if (!params)
-               goto nla_put_failure;
-
-       NLA_PUT_U8(msg, NL80211_TXQ_ATTR_QUEUE, queue);
-       /* Burst time is configured in units of 0.1 msec and TXOP parameter in
-        * 32 usec, so need to convert the value here. */
-       NLA_PUT_U16(msg, NL80211_TXQ_ATTR_TXOP, (burst_time * 100 + 16) / 32);
-       NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMIN, cw_min);
-       NLA_PUT_U16(msg, NL80211_TXQ_ATTR_CWMAX, cw_max);
-       NLA_PUT_U8(msg, NL80211_TXQ_ATTR_AIFS, aifs);
-
-       nla_nest_end(msg, params);
-
-       nla_nest_end(msg, txq);
-
-       if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
-               return 0;
- nla_put_failure:
-       return -1;
-}
-
-
-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;
-       bss->ifindex = if_nametoindex(ifname);
-
-       ifidx = nl80211_create_iface(priv, ifname, NL80211_IFTYPE_AP, bssid);
-       if (ifidx < 0) {
-               os_free(bss);
-               return -1;
-       }
-       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_beacon(const char *iface, void *priv,
-                          const u8 *head, size_t head_len,
-                          const u8 *tail, size_t tail_len, int dtim_period)
-{
-       return wpa_driver_nl80211_set_beacon_iface(if_nametoindex(iface), priv,
-                                                  head, head_len,
-                                                  tail, tail_len,
-                                                  dtim_period);
-}
-
-
-static int i802_del_beacon(struct wpa_driver_nl80211_data *drv)
-{
-       struct nl_msg *msg;
-
-       msg = nlmsg_alloc();
-       if (!msg)
-               return -ENOMEM;
-
-       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
-                   0, NL80211_CMD_DEL_BEACON, 0);
-       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
-
-       return send_and_recv_msgs(drv, msg, NULL, NULL);
- nla_put_failure:
-       return -ENOBUFS;
-}
-
-
-static int i802_set_bss(void *priv, int cts, int preamble, int slot)
-{
-       struct wpa_driver_nl80211_data *drv = priv;
-       struct nl_msg *msg;
-
-       msg = nlmsg_alloc();
-       if (!msg)
-               return -ENOMEM;
-
-       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
-                   NL80211_CMD_SET_BSS, 0);
-
-       if (cts >= 0)
-               NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
-       if (preamble >= 0)
-               NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
-       if (slot >= 0)
-               NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
-
-       /* TODO: multi-BSS support */
-       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
-
-       return send_and_recv_msgs(drv, msg, NULL, NULL);
- nla_put_failure:
-       return -ENOBUFS;
-}
-
-
-static int i802_set_cts_protect(void *priv, int value)
-{
-       return i802_set_bss(priv, value, -1, -1);
-}
-
-
-static int i802_set_preamble(void *priv, int value)
-{
-       return i802_set_bss(priv, -1, value, -1);
-}
-
-
-static int i802_set_short_slot_time(void *priv, int value)
-{
-       return i802_set_bss(priv, -1, -1, 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;
-       case HOSTAPD_IF_WDS:
-               return NL80211_IFTYPE_WDS;
-       }
-       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)
-{
-       struct wpa_driver_nl80211_data *drv = priv;
-       struct nl_msg *msg;
-
-       msg = nlmsg_alloc();
-       if (!msg)
-               return -ENOMEM;
-
-       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
-                   0, NL80211_CMD_SET_STATION, 0);
-
-       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
-                   if_nametoindex(drv->ifname));
-       NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
-       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
-                   if_nametoindex(ifname));
-
-       return send_and_recv_msgs(drv, msg, NULL, NULL);
- nla_put_failure:
-       return -ENOBUFS;
-}
-
-
-static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
-{
-       struct wpa_driver_nl80211_data *drv = eloop_ctx;
-       struct sockaddr_ll lladdr;
-       unsigned char buf[3000];
-       int len;
-       socklen_t fromlen = sizeof(lladdr);
-
-       len = recvfrom(sock, buf, sizeof(buf), 0,
-                      (struct sockaddr *)&lladdr, &fromlen);
-       if (len < 0) {
-               perror("recv");
-               return;
-       }
-
-       if (have_ifidx(drv, lladdr.sll_ifindex)) {
-               struct hostapd_data *hapd;
-               hapd = hostapd_sta_get_bss(drv->hapd, lladdr.sll_addr);
-               if (!hapd)
-                       return;
-               hostapd_eapol_receive(hapd, lladdr.sll_addr, buf, len);
-       }
-}
-
-
-static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv, const char *ifname,
-                           int mode)
-{
-       struct nl_msg *msg;
-       int ret = -ENOBUFS;
-
-       msg = nlmsg_alloc();
-       if (!msg)
-               return -ENOMEM;
-
-       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
-                   0, NL80211_CMD_SET_INTERFACE, 0);
-       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
-                   if_nametoindex(ifname));
-       NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, mode);
-
-       ret = send_and_recv_msgs(drv, msg, NULL, NULL);
-       if (!ret)
-               return 0;
- nla_put_failure:
-       wpa_printf(MSG_ERROR, "Failed to set interface %s to master "
-                  "mode.", ifname);
-       return ret;
-}
-
-
-#ifdef CONFIG_IEEE80211N
-static void i802_add_neighbor(struct wpa_driver_nl80211_data *drv, u8 *bssid,
-                             int freq, u8 *ie, size_t ie_len)
-{
-       struct ieee802_11_elems elems;
-       int ht, pri_chan = 0, sec_chan = 0;
-       struct ieee80211_ht_operation *oper;
-       struct hostapd_neighbor_bss *nnei;
-
-       ieee802_11_parse_elems(ie, ie_len, &elems, 0);
-       ht = elems.ht_capabilities || elems.ht_operation;
-       if (elems.ht_operation && elems.ht_operation_len >= sizeof(*oper)) {
-               oper = (struct ieee80211_ht_operation *) elems.ht_operation;
-               pri_chan = oper->control_chan;
-               if (oper->ht_param & HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH) {
-                       if (oper->ht_param &
-                           HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
-                               sec_chan = pri_chan + 4;
-                       else if (oper->ht_param &
-                           HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
-                               sec_chan = pri_chan - 4;
-               }
-       }
-
-       wpa_printf(MSG_DEBUG, "nl80211: Neighboring BSS - bssid=" MACSTR
-                  " freq=%d MHz HT=%d pri_chan=%d sec_chan=%d",
-                  MAC2STR(bssid), freq, ht, pri_chan, sec_chan);
-
-       nnei = os_realloc(drv->neighbors, (drv->num_neighbors + 1) *
-                         sizeof(struct hostapd_neighbor_bss));
-       if (nnei == NULL)
-               return;
-       drv->neighbors = nnei;
-       nnei = &nnei[drv->num_neighbors];
-       os_memcpy(nnei->bssid, bssid, ETH_ALEN);
-       nnei->freq = freq;
-       nnei->ht = !!ht;
-       nnei->pri_chan = pri_chan;
-       nnei->sec_chan = sec_chan;
-       drv->num_neighbors++;
-}
-
-
-static int i802_get_scan_freq(struct iw_event *iwe, int *freq)
-{
-       int divi = 1000000, i;
-
-       if (iwe->u.freq.e == 0) {
-               /*
-                * Some drivers do not report frequency, but a channel.
-                * Try to map this to frequency by assuming they are using
-                * IEEE 802.11b/g.  But don't overwrite a previously parsed
-                * frequency if the driver sends both frequency and channel,
-                * since the driver may be sending an A-band channel that we
-                * don't handle here.
-                */
-
-               if (*freq)
-                       return 0;
-
-               if (iwe->u.freq.m >= 1 && iwe->u.freq.m <= 13) {
-                       *freq = 2407 + 5 * iwe->u.freq.m;
-                       return 0;
-               } else if (iwe->u.freq.m == 14) {
-                       *freq = 2484;
-                       return 0;
-               }
-       }
-
-       if (iwe->u.freq.e > 6) {
-               wpa_printf(MSG_DEBUG, "Invalid freq in scan results: "
-                          "m=%d e=%d", iwe->u.freq.m, iwe->u.freq.e);
-               return -1;
-       }
-
-       for (i = 0; i < iwe->u.freq.e; i++)
-               divi /= 10;
-       *freq = iwe->u.freq.m / divi;
-       return 0;
-}
-
-
-static int i802_parse_scan(struct wpa_driver_nl80211_data *drv, u8 *res_buf,
-                          size_t len)
-{
-       size_t ap_num = 0;
-       int first;
-       struct iw_event iwe_buf, *iwe = &iwe_buf;
-       char *pos, *end, *custom;
-       u8 bssid[ETH_ALEN];
-       int freq = 0;
-       u8 *ie = NULL;
-       size_t ie_len = 0;
-
-       ap_num = 0;
-       first = 1;
-
-       pos = (char *) res_buf;
-       end = (char *) res_buf + len;
-
-       while (pos + IW_EV_LCP_LEN <= end) {
-               /* Event data may be unaligned, so make a local, aligned copy
-                * before processing. */
-               os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
-               if (iwe->len <= IW_EV_LCP_LEN)
-                       break;
-
-               custom = pos + IW_EV_POINT_LEN;
-               if (iwe->cmd == IWEVGENIE) {
-                       /* WE-19 removed the pointer from struct iw_point */
-                       char *dpos = (char *) &iwe_buf.u.data.length;
-                       int dlen = dpos - (char *) &iwe_buf;
-                       os_memcpy(dpos, pos + IW_EV_LCP_LEN,
-                                 sizeof(struct iw_event) - dlen);
-               } else {
-                       os_memcpy(&iwe_buf, pos, sizeof(struct iw_event));
-                       custom += IW_EV_POINT_OFF;
-               }
-
-               switch (iwe->cmd) {
-               case SIOCGIWAP:
-                       if (!first)
-                               i802_add_neighbor(drv, bssid, freq, ie,
-                                                 ie_len);
-                       first = 0;
-                       os_memcpy(bssid, iwe->u.ap_addr.sa_data, ETH_ALEN);
-                       freq = 0;
-                       ie = NULL;
-                       ie_len = 0;
-                       break;
-               case SIOCGIWFREQ:
-                       i802_get_scan_freq(iwe, &freq);
-                       break;
-               case IWEVGENIE:
-                       if (custom + iwe->u.data.length > end) {
-                               wpa_printf(MSG_ERROR, "IWEVGENIE overflow");
-                               return -1;
-                       }
-                       ie = (u8 *) custom;
-                       ie_len = iwe->u.data.length;
-                       break;
-               }
-
-               pos += iwe->len;
-       }
-
-       if (!first)
-               i802_add_neighbor(drv, bssid, freq, ie, ie_len);
-
-       return 0;
-}
-
-
-static int i802_get_ht_scan_res(struct wpa_driver_nl80211_data *drv)
-{
-       struct iwreq iwr;
-       u8 *res_buf;
-       size_t res_buf_len;
-       int res;
-
-       res_buf_len = IW_SCAN_MAX_DATA;
-       for (;;) {
-               res_buf = os_malloc(res_buf_len);
-               if (res_buf == NULL)
-                       return -1;
-               os_memset(&iwr, 0, sizeof(iwr));
-               os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
-               iwr.u.data.pointer = res_buf;
-               iwr.u.data.length = res_buf_len;
-
-               if (ioctl(drv->ioctl_sock, SIOCGIWSCAN, &iwr) == 0)
-                       break;
-
-               if (errno == E2BIG && res_buf_len < 65535) {
-                       os_free(res_buf);
-                       res_buf = NULL;
-                       res_buf_len *= 2;
-                       if (res_buf_len > 65535)
-                               res_buf_len = 65535; /* 16-bit length field */
-                       wpa_printf(MSG_DEBUG, "Scan results did not fit - "
-                                  "trying larger buffer (%lu bytes)",
-                                  (unsigned long) res_buf_len);
-               } else {
-                       perror("ioctl[SIOCGIWSCAN]");
-                       os_free(res_buf);
-                       return -1;
-               }
-       }
-
-       if (iwr.u.data.length > res_buf_len) {
-               os_free(res_buf);
-               return -1;
-       }
-
-       res = i802_parse_scan(drv, res_buf, iwr.u.data.length);
-       os_free(res_buf);
-
-       return res;
-}
-
-
-static int i802_is_event_wireless_scan_complete(char *data, int len)
-{
-       struct iw_event iwe_buf, *iwe = &iwe_buf;
-       char *pos, *end;
-
-       pos = data;
-       end = data + len;
-
-       while (pos + IW_EV_LCP_LEN <= end) {
-               /* Event data may be unaligned, so make a local, aligned copy
-                * before processing. */
-               os_memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
-               if (iwe->cmd == SIOCGIWSCAN)
-                       return 1;
-
-               pos += iwe->len;
-       }
-
-       return 0;
-}
-
-
-static int i802_is_rtm_scan_complete(int ifindex, struct nlmsghdr *h, int len)
-{
-       struct ifinfomsg *ifi;
-       int attrlen, _nlmsg_len, rta_len;
-       struct rtattr *attr;
-
-       if (len < (int) sizeof(*ifi))
-               return 0;
-
-       ifi = NLMSG_DATA(h);
-
-       if (ifindex != ifi->ifi_index)
-               return 0; /* event for foreign ifindex */
-
-       _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);
-
-       rta_len = RTA_ALIGN(sizeof(struct rtattr));
-       while (RTA_OK(attr, attrlen)) {
-               if (attr->rta_type == IFLA_WIRELESS &&
-                   i802_is_event_wireless_scan_complete(
-                           ((char *) attr) + rta_len,
-                           attr->rta_len - rta_len))
-                       return 1;
-               attr = RTA_NEXT(attr, attrlen);
-       }
-
-       return 0;
-}
-
-
-static int i802_is_scan_complete(int s, int ifindex)
-{
-       char buf[1024];
-       int left;
-       struct nlmsghdr *h;
-
-       left = recv(s, buf, sizeof(buf), MSG_DONTWAIT);
-       if (left < 0) {
-               perror("recv(netlink)");
-               return 0;
-       }
-
-       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:
-                       if (i802_is_rtm_scan_complete(ifindex, h, plen))
-                               return 1;
-                       break;
-               }
+       if (cts >= 0)
+               NLA_PUT_U8(msg, NL80211_ATTR_BSS_CTS_PROT, cts);
+       if (preamble >= 0)
+               NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_PREAMBLE, preamble);
+       if (slot >= 0)
+               NLA_PUT_U8(msg, NL80211_ATTR_BSS_SHORT_SLOT_TIME, slot);
 
-               len = NLMSG_ALIGN(len);
-               left -= len;
-               h = (struct nlmsghdr *) ((char *) h + len);
-       }
+       /* TODO: multi-BSS support */
+       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
 
-       return 0;
+       return send_and_recv_msgs(drv, msg, NULL, NULL);
+ nla_put_failure:
+       return -ENOBUFS;
 }
 
 
-static int i802_ht_scan(struct wpa_driver_nl80211_data *drv)
+static int i802_set_cts_protect(void *priv, int value)
 {
-       struct iwreq iwr;
-       int s, res, ifindex;
-       struct sockaddr_nl local;
-       time_t now, end;
-       fd_set rfds;
-       struct timeval tv;
-
-       wpa_printf(MSG_DEBUG, "nl80211: Scanning overlapping BSSes before "
-                  "starting HT 20/40 MHz BSS");
-
-       /* Request a new scan */
-       /* TODO: would be enough to scan the selected band */
-       os_memset(&iwr, 0, sizeof(iwr));
-       os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
-       if (ioctl(drv->ioctl_sock, SIOCSIWSCAN, &iwr) < 0) {
-               perror("ioctl[SIOCSIWSCAN]");
-               return -1;
-       }
-
-       ifindex = if_nametoindex(drv->ifname);
-
-       /* Wait for scan completion event or timeout */
-       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;
-       }
-
-       time(&end);
-       end += 30; /* Wait at most 30 seconds for scan results */
-       for (;;) {
-               time(&now);
-               tv.tv_sec = end > now ? end - now : 0;
-               tv.tv_usec = 0;
-               FD_ZERO(&rfds);
-               FD_SET(s, &rfds);
-               res = select(s + 1, &rfds, NULL, NULL, &tv);
-               if (res < 0) {
-                       perror("select");
-                       /* Assume results are ready after 10 seconds wait */
-                       os_sleep(10, 0);
-                       break;
-               } else if (res) {
-                       if (i802_is_scan_complete(s, ifindex)) {
-                               wpa_printf(MSG_DEBUG, "nl80211: Scan "
-                                          "completed");
-                               break;
-                       }
-               } else {
-                       wpa_printf(MSG_DEBUG, "nl80211: Scan timeout");
-                       /* Assume results are ready to be read now */
-                       break;
-               }
-       }
-
-       close(s);
-
-       return i802_get_ht_scan_res(drv);
+       return i802_set_bss(priv, value, -1, -1);
 }
-#endif /* CONFIG_IEEE80211N */
 
 
-static int i802_init_sockets(struct wpa_driver_nl80211_data *drv, const u8 *bssid)
+static int i802_set_preamble(void *priv, int value)
 {
-       struct ifreq ifr;
-       int ret;
-
-       drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
-       if (drv->ioctl_sock < 0) {
-               perror("socket[PF_INET,SOCK_DGRAM]");
-               return -1;
-       }
+       return i802_set_bss(priv, -1, value, -1);
+}
 
-       /* start listening for EAPOL on the default AP interface */
-       add_ifidx(drv, if_nametoindex(drv->ifname));
 
-       if (hostapd_set_iface_flags(drv, drv->ifname, 0))
-               return -1;
+static int i802_set_short_slot_time(void *priv, int value)
+{
+       return i802_set_bss(priv, -1, -1, value);
+}
 
-       if (bssid) {
-               os_strlcpy(ifr.ifr_name, drv->ifname, IFNAMSIZ);
-               memcpy(ifr.ifr_hwaddr.sa_data, bssid, ETH_ALEN);
-               ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
 
-               if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
-                       perror("ioctl(SIOCSIFHWADDR)");
-                       return -1;
-               }
+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;
+}
 
-       /*
-        * initialise generic netlink and nl80211
-        */
-       drv->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
-       if (!drv->nl_cb) {
-               printf("Failed to allocate netlink callbacks.\n");
-               return -1;
-       }
 
-       drv->nl_handle = nl_handle_alloc_cb(drv->nl_cb);
-       if (!drv->nl_handle) {
-               printf("Failed to allocate netlink handle.\n");
+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;
+}
 
-       if (genl_connect(drv->nl_handle)) {
-               printf("Failed to connect to generic netlink.\n");
-               return -1;
-       }
 
-#ifdef CONFIG_LIBNL20
-       if (genl_ctrl_alloc_cache(drv->nl_handle, &drv->nl_cache) < 0) {
-               printf("Failed to allocate generic netlink cache.\n");
-               return -1;
-       }
-#else /* CONFIG_LIBNL20 */
-       drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
-       if (!drv->nl_cache) {
-               printf("Failed to allocate generic netlink cache.\n");
-               return -1;
-       }
-#endif /* CONFIG_LIBNL20 */
+static int i802_if_update(void *priv, enum hostapd_driver_if_type type,
+                         char *ifname, const u8 *addr)
+{
+       /* unused at the moment */
+       return -1;
+}
 
-       drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
-       if (!drv->nl80211) {
-               printf("nl80211 not found.\n");
-               return -1;
-       }
 
-       ret = nl_get_multicast_id(drv, "nl80211", "scan");
-       if (ret >= 0)
-               ret = nl_socket_add_membership(drv->nl_handle, ret);
-       if (ret < 0) {
-               wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
-                          "membership for scan events: %d (%s)",
-                          ret, strerror(-ret));
-       }
+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;
+}
 
-       ret = nl_get_multicast_id(drv, "nl80211", "mlme");
-       if (ret >= 0)
-               ret = nl_socket_add_membership(drv->nl_handle, ret);
-       if (ret < 0) {
-               wpa_printf(MSG_DEBUG, "nl80211: Could not add multicast "
-                          "membership for mlme events: %d (%s)",
-                          ret, strerror(-ret));
-       }
 
-       eloop_register_read_sock(nl_socket_get_fd(drv->nl_handle),
-                                wpa_driver_nl80211_event_receive, drv,
-                                drv->hapd);
+static int i802_set_sta_vlan(void *priv, const u8 *addr,
+                            const char *ifname, int vlan_id)
+{
+       struct wpa_driver_nl80211_data *drv = priv;
+       struct nl_msg *msg;
 
-#ifdef CONFIG_IEEE80211N
-       if (drv->ht_40mhz_scan) {
-               if (nl80211_set_mode(drv, drv->ifname, NL80211_IFTYPE_STATION)
-                   || hostapd_set_iface_flags(drv, drv->ifname, 1) ||
-                   i802_ht_scan(drv) ||
-                   hostapd_set_iface_flags(drv, drv->ifname, 0)) {
-                       wpa_printf(MSG_ERROR, "Failed to scan channels for "
-                                  "HT 40 MHz operations");
-                       return -1;
-               }
-       }
-#endif /* CONFIG_IEEE80211N */
+       msg = nlmsg_alloc();
+       if (!msg)
+               return -ENOMEM;
 
-       /* Initialise a monitor interface */
-       if (nl80211_create_monitor_interface(drv))
-               return -1;
+       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
+                   0, NL80211_CMD_SET_STATION, 0);
 
-       if (nl80211_set_mode(drv, drv->ifname, NL80211_IFTYPE_AP))
-               goto fail1;
+       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
+                   if_nametoindex(drv->ifname));
+       NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
+       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX,
+                   if_nametoindex(ifname));
 
-       if (hostapd_set_iface_flags(drv, drv->ifname, 1))
-               goto fail1;
+       return send_and_recv_msgs(drv, msg, NULL, NULL);
+ nla_put_failure:
+       return -ENOBUFS;
+}
 
-       drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
-       if (drv->eapol_sock < 0) {
-               perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
-               goto fail1;
-       }
 
-       if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
-       {
-               printf("Could not register read socket for eapol\n");
-               return -1;
-       }
+static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
+{
+       struct wpa_driver_nl80211_data *drv = eloop_ctx;
+       struct sockaddr_ll lladdr;
+       unsigned char buf[3000];
+       int len;
+       socklen_t fromlen = sizeof(lladdr);
 
-       memset(&ifr, 0, sizeof(ifr));
-       os_strlcpy(ifr.ifr_name, drv->ifname, sizeof(ifr.ifr_name));
-       if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) != 0) {
-               perror("ioctl(SIOCGIFHWADDR)");
-               goto fail1;
+       len = recvfrom(sock, buf, sizeof(buf), 0,
+                      (struct sockaddr *)&lladdr, &fromlen);
+       if (len < 0) {
+               perror("recv");
+               return;
        }
 
-       if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
-               printf("Invalid HW-addr family 0x%04x\n",
-                      ifr.ifr_hwaddr.sa_family);
-               goto fail1;
+       if (have_ifidx(drv, lladdr.sll_ifindex)) {
+               void *ctx;
+               ctx = hostapd_sta_get_bss(drv->ctx, lladdr.sll_addr);
+               if (!ctx)
+                       return;
+               hostapd_eapol_receive(ctx, lladdr.sll_addr, buf, len);
        }
-       memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
-
-       return 0;
-
-fail1:
-       nl80211_remove_iface(drv, drv->monitor_ifidx);
-       return -1;
 }
 
 
@@ -4292,7 +3792,8 @@ static int i802_sta_clear_stats(void *priv, const u8 *addr)
 }
 
 
-static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
+static int i802_sta_deauth(void *priv, const u8 *own_addr, const u8 *addr,
+                          int reason)
 {
        struct wpa_driver_nl80211_data *drv = priv;
        struct ieee80211_mgmt mgmt;
@@ -4301,8 +3802,8 @@ static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
        mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
                                          WLAN_FC_STYPE_DEAUTH);
        memcpy(mgmt.da, addr, ETH_ALEN);
-       memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
-       memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
+       memcpy(mgmt.sa, own_addr, ETH_ALEN);
+       memcpy(mgmt.bssid, own_addr, ETH_ALEN);
        mgmt.u.deauth.reason_code = host_to_le16(reason);
        return wpa_driver_nl80211_send_mlme(drv, (u8 *) &mgmt,
                                            IEEE80211_HDRLEN +
@@ -4310,7 +3811,8 @@ static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
 }
 
 
-static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
+static int i802_sta_disassoc(void *priv, const u8 *own_addr, const u8 *addr,
+                            int reason)
 {
        struct wpa_driver_nl80211_data *drv = priv;
        struct ieee80211_mgmt mgmt;
@@ -4319,8 +3821,8 @@ static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
        mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
                                          WLAN_FC_STYPE_DISASSOC);
        memcpy(mgmt.da, addr, ETH_ALEN);
-       memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
-       memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
+       memcpy(mgmt.sa, own_addr, ETH_ALEN);
+       memcpy(mgmt.bssid, own_addr, ETH_ALEN);
        mgmt.u.disassoc.reason_code = host_to_le16(reason);
        return wpa_driver_nl80211_send_mlme(drv, (u8 *) &mgmt,
                                            IEEE80211_HDRLEN +
@@ -4328,30 +3830,16 @@ static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
 }
 
 
-static const struct hostapd_neighbor_bss *
-i802_get_neighbor_bss(void *priv, size_t *num)
-{
-       struct wpa_driver_nl80211_data *drv = priv;
-       *num = drv->num_neighbors;
-       return drv->neighbors;
-}
-
-
 static void *i802_init(struct hostapd_data *hapd,
                       struct wpa_init_params *params)
 {
        struct wpa_driver_nl80211_data *drv;
        size_t i;
 
-       drv = os_zalloc(sizeof(struct wpa_driver_nl80211_data));
-       if (drv == NULL) {
-               printf("Could not allocate memory for i802 driver data\n");
+       drv = wpa_driver_nl80211_init(hapd, params->ifname);
+       if (drv == NULL)
                return NULL;
-       }
 
-       drv->hapd = hapd;
-       memcpy(drv->ifname, params->ifname, sizeof(drv->ifname));
-       drv->ifindex = if_nametoindex(drv->ifname);
        drv->bss.ifindex = drv->ifindex;
 
        drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
@@ -4360,69 +3848,62 @@ static void *i802_init(struct hostapd_data *hapd,
                if (params->bridge[i])
                        add_ifidx(drv, if_nametoindex(params->bridge[i]));
        }
-       drv->ht_40mhz_scan = params->ht_40mhz_scan;
 
-       if (i802_init_sockets(drv, params->bssid))
-               goto failed;
+       /* start listening for EAPOL on the default AP interface */
+       add_ifidx(drv, drv->ifindex);
 
-       return drv;
+       if (params->bssid) {
+               if (hostapd_set_iface_flags(drv, drv->ifname, 0))
+                       goto failed;
 
-failed:
-       free(drv);
-       return NULL;
-}
+               if (set_ifhwaddr(drv, drv->ifname, params->bssid))
+                       goto failed;
+       }
 
+       if (nl80211_set_mode(drv, drv->ifindex, NL80211_IFTYPE_AP)) {
+               wpa_printf(MSG_ERROR, "nl80211: Failed to set interface %s "
+                          "into AP mode", drv->ifname);
+               goto failed;
+       }
 
-static void i802_deinit(void *priv)
-{
-       struct wpa_driver_nl80211_data *drv = priv;
-       struct i802_bss *bss, *prev;
+       if (hostapd_set_iface_flags(drv, drv->ifname, 1))
+               goto failed;
 
-       if (drv->last_freq_ht) {
-               /* Clear HT flags from the driver */
-               struct hostapd_freq_params freq;
-               os_memset(&freq, 0, sizeof(freq));
-               freq.freq = drv->last_freq;
-               i802_set_freq(priv, &freq);
+       drv->eapol_sock = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_PAE));
+       if (drv->eapol_sock < 0) {
+               perror("socket(PF_PACKET, SOCK_DGRAM, ETH_P_PAE)");
+               goto failed;
        }
 
-       i802_del_beacon(drv);
+       if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
+       {
+               printf("Could not register read socket for eapol\n");
+               goto failed;
+       }
 
-       /* remove monitor interface */
-       nl80211_remove_iface(drv, drv->monitor_ifidx);
+       if (get_ifhwaddr(drv, drv->ifname, params->own_addr))
+               goto failed;
 
-       (void) hostapd_set_iface_flags(drv, drv->ifname, 0);
+       return drv;
 
-       if (drv->monitor_sock >= 0) {
-               eloop_unregister_read_sock(drv->monitor_sock);
-               close(drv->monitor_sock);
-       }
+failed:
+       nl80211_remove_monitor_interface(drv);
        if (drv->ioctl_sock >= 0)
                close(drv->ioctl_sock);
-       if (drv->eapol_sock >= 0) {
-               eloop_unregister_read_sock(drv->eapol_sock);
-               close(drv->eapol_sock);
-       }
 
-       eloop_unregister_read_sock(nl_socket_get_fd(drv->nl_handle));
        genl_family_put(drv->nl80211);
        nl_cache_free(drv->nl_cache);
        nl_handle_destroy(drv->nl_handle);
        nl_cb_put(drv->nl_cb);
 
-       if (drv->if_indices != drv->default_if_indices)
-               free(drv->if_indices);
-
-       os_free(drv->neighbors);
+       os_free(drv);
+       return NULL;
+}
 
-       bss = drv->bss.next;
-       while (bss) {
-               prev = bss;
-               bss = bss->next;
-               os_free(bss);
-       }
 
-       free(drv);
+static void i802_deinit(void *priv)
+{
+       wpa_driver_nl80211_deinit(priv);
 }
 
 #endif /* HOSTAPD */
@@ -4431,11 +3912,14 @@ static void i802_deinit(void *priv)
 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,
@@ -4444,7 +3928,9 @@ const struct wpa_driver_ops wpa_driver_nl80211_ops = {
        .deinit = wpa_driver_nl80211_deinit,
        .get_capa = wpa_driver_nl80211_get_capa,
        .set_operstate = wpa_driver_nl80211_set_operstate,
+#endif /* HOSTAPD */
        .set_country = wpa_driver_nl80211_set_country,
+       .set_mode = wpa_driver_nl80211_set_mode,
 #ifdef CONFIG_AP
        .set_beacon = wpa_driver_nl80211_set_beacon,
 #endif /* CONFIG_AP */
@@ -4452,6 +3938,10 @@ const struct wpa_driver_ops wpa_driver_nl80211_ops = {
        .send_mlme = wpa_driver_nl80211_send_mlme,
        .set_beacon_int = wpa_driver_nl80211_set_beacon_int,
        .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,
@@ -4460,18 +3950,13 @@ const struct wpa_driver_ops wpa_driver_nl80211_ops = {
        .get_seqnum = i802_get_seqnum,
        .flush = i802_flush,
        .read_sta_data = i802_read_sta_data,
-       .hapd_send_eapol = i802_send_eapol,
-       .sta_set_flags = i802_sta_set_flags,
        .sta_deauth = i802_sta_deauth,
        .sta_disassoc = i802_sta_disassoc,
-       .sta_remove = i802_sta_remove,
-       .sta_add = i802_sta_add,
        .get_inact_sec = i802_get_inact_sec,
        .sta_clear_stats = i802_sta_clear_stats,
        .set_freq = i802_set_freq,
        .set_rts = i802_set_rts,
        .set_frag = i802_set_frag,
-       .set_retry = i802_set_retry,
        .set_rate_sets = i802_set_rate_sets,
        .hapd_set_beacon = i802_set_beacon,
        .set_cts_protect = i802_set_cts_protect,
@@ -4484,6 +3969,5 @@ const struct wpa_driver_ops wpa_driver_nl80211_ops = {
        .if_update = i802_if_update,
        .if_remove = i802_if_remove,
        .set_sta_vlan = i802_set_sta_vlan,
-       .get_neighbor_bss = i802_get_neighbor_bss,
 #endif /* HOSTAPD */
 };