Remove unused set_retry() driver op
[wpasupplicant] / src / drivers / driver_nl80211.c
index 127f38a..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;
@@ -113,19 +101,15 @@ struct wpa_driver_nl80211_data {
        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];
@@ -146,6 +130,11 @@ 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);
@@ -156,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 */
 
 
@@ -286,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)
@@ -346,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)
 {
@@ -368,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)
@@ -390,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)
@@ -926,6 +967,7 @@ nla_put_failure:
 }
 
 
+#ifndef HOSTAPD
 struct wiphy_info_data {
        int max_scan_ssids;
        int ap_supported;
@@ -1005,6 +1047,7 @@ 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 */
 
 
 static int wpa_driver_nl80211_init_nl(struct wpa_driver_nl80211_data *drv,
@@ -1093,6 +1136,39 @@ err1:
 }
 
 
+static int wpa_driver_nl80211_init_link_events(
+       struct wpa_driver_nl80211_data *drv)
+{
+#ifdef HOSTAPD
+       return 0;
+#else /* HOSTAPD */
+       int s;
+       struct sockaddr_nl local;
+
+       s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
+       if (s < 0) {
+               perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
+               return -1;
+       }
+
+       os_memset(&local, 0, sizeof(local));
+       local.nl_family = AF_NETLINK;
+       local.nl_groups = RTMGRP_LINK;
+       if (bind(s, (struct sockaddr *) &local, sizeof(local)) < 0) {
+               perror("bind(netlink)");
+               close(s);
+               return -1;
+       }
+
+       eloop_register_read_sock(s, wpa_driver_nl80211_event_receive_link, drv,
+                                drv->ctx);
+       drv->link_event_sock = s;
+
+       return 0;
+#endif /* HOSTAPD */
+}
+
+
 /**
  * wpa_driver_nl80211_init - Initialize nl80211 driver interface
  * @ctx: context to be used when calling wpa_supplicant functions,
@@ -1102,8 +1178,6 @@ err1:
  */
 static void * wpa_driver_nl80211_init(void *ctx, const char *ifname)
 {
-       int s;
-       struct sockaddr_nl local;
        struct wpa_driver_nl80211_data *drv;
 
        drv = os_zalloc(sizeof(*drv));
@@ -1111,59 +1185,43 @@ static void * wpa_driver_nl80211_init(void *ctx, const char *ifname)
                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 */
+       drv->link_event_sock = -1;
+       drv->ioctl_sock = -1;
 
-       if (wpa_driver_nl80211_init_nl(drv, ctx))
-               goto err1;
+       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 err5;
-       }
-
-       s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
-       if (s < 0) {
-               perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
-               goto err6;
-       }
-
-       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);
-               goto err6;
+               goto failed;
        }
 
-#ifndef HOSTAPD
-       eloop_register_read_sock(s, wpa_driver_nl80211_event_receive_link, drv,
-                                ctx);
-#endif /* HOSTAPD */
-       drv->link_event_sock = s;
-
-       if (wpa_driver_nl80211_finish_drv_init(drv))
-               goto err7;
+       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);
        nl_cache_free(drv->nl_cache);
        nl_handle_destroy(drv->nl_handle);
        nl_cb_put(drv->nl_cb);
-err1:
+
        os_free(drv);
        return NULL;
 }
@@ -1174,6 +1232,7 @@ wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
 {
        drv->ifindex = if_nametoindex(drv->ifname);
 
+#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");
@@ -1188,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()
@@ -1204,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);
+       (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);
@@ -1582,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,
@@ -1630,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);
 }
@@ -1640,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);
 }
@@ -1726,6 +1851,8 @@ nla_put_failure:
        return ret;
 }
 
+#endif /* HOSTAPD */
+
 
 #if defined(CONFIG_AP) || defined(HOSTAPD)
 
@@ -2195,6 +2322,73 @@ nla_put_failure:
        return -1;
 }
 
+
+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;
+}
+
+
+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 */
 
 
@@ -2249,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)
@@ -2298,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 */
 
@@ -2383,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;
        }
 }
@@ -2529,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) {
@@ -2586,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);
 }
 
 
@@ -2741,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)
 {
@@ -2797,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
@@ -2808,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;
        }
 
@@ -2827,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)
 {
@@ -2839,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;
 
@@ -2893,6 +3093,7 @@ nla_put_failure:
        nlmsg_free(msg);
        return ret;
 }
+#endif /* HOSTAPD */
 
 
 static int nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
@@ -2942,11 +3143,14 @@ static int wpa_driver_nl80211_set_mode(void *priv, int mode)
 
        if (nl80211_set_mode(drv, drv->ifindex, nlmode) == 0) {
                drv->nlmode = nlmode;
-               return 0;
+               ret = 0;
+               goto done;
        }
 
-       if (nlmode == drv->nlmode)
-               return 0; /* Already in the requested mode */
+       if (nlmode == drv->nlmode) {
+               ret = 0;
+               goto done; /* Already in the requested mode */
+       }
 
        /* 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
@@ -2962,10 +3166,25 @@ static int wpa_driver_nl80211_set_mode(void *priv, int mode)
        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)
 {
@@ -2988,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)
@@ -3178,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;
 
@@ -3192,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;
 
@@ -3211,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 */
 }
 
 
@@ -3338,188 +3535,6 @@ 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)
-{
-       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 i802_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(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);
-       }
-#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;
-}
-
-
-static int i802_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;
-}
-
-
-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)
 {
@@ -3573,13 +3588,13 @@ static int i802_bss_add(void *priv, const char *ifname, const u8 *bssid)
        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;
        }
+       bss->ifindex = ifidx;
        if (hostapd_set_iface_flags(priv, ifname, 1)) {
                nl80211_remove_iface(priv, ifidx);
                os_free(bss);
@@ -3623,24 +3638,6 @@ static int i802_set_beacon(const char *iface, void *priv,
 }
 
 
-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;
@@ -3692,8 +3689,6 @@ 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;
 }
@@ -3766,11 +3761,11 @@ static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
        }
 
        if (have_ifidx(drv, lladdr.sll_ifindex)) {
-               struct hostapd_data *hapd;
-               hapd = hostapd_sta_get_bss(drv->hapd, lladdr.sll_addr);
-               if (!hapd)
+               void *ctx;
+               ctx = hostapd_sta_get_bss(drv->ctx, lladdr.sll_addr);
+               if (!ctx)
                        return;
-               hostapd_eapol_receive(hapd, lladdr.sll_addr, buf, len);
+               hostapd_eapol_receive(ctx, lladdr.sll_addr, buf, len);
        }
 }
 
@@ -3797,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;
@@ -3806,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 +
@@ -3815,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;
@@ -3824,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 +
@@ -3838,18 +3835,11 @@ static void *i802_init(struct hostapd_data *hapd,
 {
        struct wpa_driver_nl80211_data *drv;
        size_t i;
-       struct ifreq ifr;
 
-       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;
-       drv->ctx = 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);
@@ -3859,49 +3849,30 @@ static void *i802_init(struct hostapd_data *hapd,
                        add_ifidx(drv, if_nametoindex(params->bridge[i]));
        }
 
-       drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
-       if (drv->ioctl_sock < 0) {
-               perror("socket[PF_INET,SOCK_DGRAM]");
-               goto failed;
-       }
-
        /* start listening for EAPOL on the default AP interface */
        add_ifidx(drv, drv->ifindex);
 
-       if (hostapd_set_iface_flags(drv, drv->ifname, 0))
-               goto failed;
-
        if (params->bssid) {
-               os_strlcpy(ifr.ifr_name, drv->ifname, IFNAMSIZ);
-               os_memcpy(ifr.ifr_hwaddr.sa_data, params->bssid, ETH_ALEN);
-               ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER;
+               if (hostapd_set_iface_flags(drv, drv->ifname, 0))
+                       goto failed;
 
-               if (ioctl(drv->ioctl_sock, SIOCSIFHWADDR, &ifr)) {
-                       perror("ioctl(SIOCSIFHWADDR)");
+               if (set_ifhwaddr(drv, drv->ifname, params->bssid))
                        goto failed;
-               }
        }
 
-       if (wpa_driver_nl80211_init_nl(drv, drv->hapd))
-               goto failed;
-
-       /* Initialise a monitor interface */
-       if (nl80211_create_monitor_interface(drv))
-               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 fail1;
+               goto failed;
        }
 
        if (hostapd_set_iface_flags(drv, drv->ifname, 1))
-               goto fail1;
+               goto failed;
 
        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;
+               goto failed;
        }
 
        if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
@@ -3910,78 +3881,29 @@ static void *i802_init(struct hostapd_data *hapd,
                goto failed;
        }
 
-       os_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;
-       }
-
-       if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
-               printf("Invalid HW-addr family 0x%04x\n",
-                      ifr.ifr_hwaddr.sa_family);
-               goto fail1;
-       }
-       os_memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
+       if (get_ifhwaddr(drv, drv->ifname, params->own_addr))
+               goto failed;
 
        return drv;
 
-fail1:
-       nl80211_remove_iface(drv, drv->monitor_ifidx);
 failed:
-       free(drv);
-       return NULL;
-}
-
-
-static void i802_deinit(void *priv)
-{
-       struct wpa_driver_nl80211_data *drv = priv;
-       struct i802_bss *bss, *prev;
-
-       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);
-       }
-
-       i802_del_beacon(drv);
-
-       /* remove monitor interface */
-       nl80211_remove_iface(drv, drv->monitor_ifidx);
-
-       (void) hostapd_set_iface_flags(drv, drv->ifname, 0);
-
-       if (drv->monitor_sock >= 0) {
-               eloop_unregister_read_sock(drv->monitor_sock);
-               close(drv->monitor_sock);
-       }
+       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);
+       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 */
@@ -3990,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,
@@ -4003,6 +3928,7 @@ 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
@@ -4012,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,
@@ -4020,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,