Merge hostapd driver init functions into one
[wpasupplicant] / src / drivers / driver_nl80211.c
index db39f70..0bb2caa 100644 (file)
@@ -1,6 +1,10 @@
 /*
  * Driver interaction with Linux nl80211/cfg80211
- * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2003-2004, Instant802 Networks, Inc.
+ * Copyright (c) 2005-2006, Devicescape Software, Inc.
+ * Copyright (c) 2007, Johannes Berg <johannes@sipsolutions.net>
+ * Copyright (c) 2009, Atheros Communications
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
@@ -50,9 +54,6 @@
 #include <net/if_arp.h>
 
 #include "../../hostapd/hostapd.h"
-#include "../../hostapd/config.h"
-#include "../../hostapd/hw_features.h"
-#include "../../hostapd/mlme.h"
 #include "../../hostapd/sta_flags.h"
 #include "ieee802_11_common.h"
 
 #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;
        char ifname[IFNAMSIZ + 1];
@@ -119,7 +126,6 @@ struct wpa_driver_nl80211_data {
 #ifdef HOSTAPD
        struct hostapd_data *hapd;
 
-       int wext_sock; /* socket for wireless events */
        int eapol_sock; /* socket for EAPOL frames */
        int monitor_sock; /* socket for monitor */
        int monitor_ifidx;
@@ -128,7 +134,6 @@ struct wpa_driver_nl80211_data {
        int *if_indices;
        int num_if_indices;
 
-       int we_version;
        int beacon_int;
        struct i802_bss bss;
        unsigned int ht_40mhz_scan:1;
@@ -153,11 +158,16 @@ static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
                                 int ifidx);
 #endif /* CONFIG_AP */
 
-
 #ifdef HOSTAPD
-#define wpa_supplicant_event(c, e, d) do { } while (0)
+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 add_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
+static void del_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx);
 #endif /* HOSTAPD */
 
+
 /* nl80211 code */
 static int ack_handler(struct nl_msg *msg, void *arg)
 {
@@ -181,6 +191,13 @@ static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
        return NL_SKIP;
 }
 
+
+static int no_seq_check(struct nl_msg *msg, void *arg)
+{
+       return NL_OK;
+}
+
+
 static int send_and_recv_msgs(struct wpa_driver_nl80211_data *drv,
                              struct nl_msg *msg,
                              int (*valid_handler)(struct nl_msg *, void *),
@@ -382,6 +399,7 @@ 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)
@@ -621,12 +639,6 @@ try_again:
 }
 
 
-static int no_seq_check(struct nl_msg *msg, void *arg)
-{
-       return NL_OK;
-}
-
-
 static void mlme_event_auth(struct wpa_driver_nl80211_data *drv,
                            const u8 *frame, size_t len)
 {
@@ -729,6 +741,49 @@ static void mlme_event(struct wpa_driver_nl80211_data *drv,
        }
 }
 
+#endif /* HOSTAPD */
+
+
+static void mlme_event_michael_mic_failure(struct wpa_driver_nl80211_data *drv,
+                                          struct nlattr *tb[])
+{
+#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");
+       os_memset(&data, 0, sizeof(data));
+       if (tb[NL80211_ATTR_MAC]) {
+               wpa_hexdump(MSG_DEBUG, "nl80211: Source MAC address",
+                           nla_data(tb[NL80211_ATTR_MAC]),
+                           nla_len(tb[NL80211_ATTR_MAC]));
+       }
+       if (tb[NL80211_ATTR_KEY_SEQ]) {
+               wpa_hexdump(MSG_DEBUG, "nl80211: TSC",
+                           nla_data(tb[NL80211_ATTR_KEY_SEQ]),
+                           nla_len(tb[NL80211_ATTR_KEY_SEQ]));
+       }
+       if (tb[NL80211_ATTR_KEY_TYPE]) {
+               enum nl80211_key_type key_type =
+                       nla_get_u32(tb[NL80211_ATTR_KEY_TYPE]);
+               wpa_printf(MSG_DEBUG, "nl80211: Key Type %d", key_type);
+               if (key_type == NL80211_KEYTYPE_PAIRWISE)
+                       data.michael_mic_failure.unicast = 1;
+       } else
+               data.michael_mic_failure.unicast = 1;
+
+       if (tb[NL80211_ATTR_KEY_IDX]) {
+               u8 key_id = nla_get_u8(tb[NL80211_ATTR_KEY_IDX]);
+               wpa_printf(MSG_DEBUG, "nl80211: Key Id %d", key_id);
+       }
+
+       wpa_supplicant_event(drv->ctx, EVENT_MICHAEL_MIC_FAILURE, &data);
+#endif /* HOSTAPD */
+}
+
 
 static int process_event(struct nl_msg *msg, void *arg)
 {
@@ -750,6 +805,7 @@ static int process_event(struct nl_msg *msg, void *arg)
        }
 
        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;
@@ -773,6 +829,10 @@ static int process_event(struct nl_msg *msg, void *arg)
        case NL80211_CMD_DISASSOCIATE:
                mlme_event(drv, gnlh->cmd, tb[NL80211_ATTR_FRAME]);
                break;
+#endif /* HOSTAPD */
+       case NL80211_CMD_MICHAEL_MIC_FAILURE:
+               mlme_event_michael_mic_failure(drv, tb);
+               break;
        default:
                wpa_printf(MSG_DEBUG, "nl80211: Ignored unknown event "
                           "(cmd=%d)", gnlh->cmd);
@@ -801,62 +861,40 @@ static void wpa_driver_nl80211_event_receive(int sock, void *eloop_ctx,
 }
 
 
-static int wpa_driver_nl80211_get_ifflags_ifname(struct wpa_driver_nl80211_data *drv,
-                                             const char *ifname, int *flags)
+static int hostapd_set_iface_flags(struct wpa_driver_nl80211_data *drv,
+                                  const char *ifname, int dev_up)
 {
        struct ifreq ifr;
 
+       if (drv->ioctl_sock < 0)
+               return -1;
+
        os_memset(&ifr, 0, sizeof(ifr));
        os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
-       if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, (caddr_t) &ifr) < 0) {
+
+       if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
                perror("ioctl[SIOCGIFFLAGS]");
+               wpa_printf(MSG_DEBUG, "Could not read interface flags (%s)",
+                          ifname);
                return -1;
        }
-       *flags = ifr.ifr_flags & 0xffff;
-       return 0;
-}
-
-
-/**
- * wpa_driver_nl80211_get_ifflags - Get interface flags (SIOCGIFFLAGS)
- * @drv: driver_nl80211 private data
- * @flags: Pointer to returned flags value
- * Returns: 0 on success, -1 on failure
- */
-static int wpa_driver_nl80211_get_ifflags(struct wpa_driver_nl80211_data *drv,
-                                         int *flags)
-{
-       return wpa_driver_nl80211_get_ifflags_ifname(drv, drv->ifname, flags);
-}
 
+       if (dev_up) {
+               if (ifr.ifr_flags & IFF_UP)
+                       return 0;
+               ifr.ifr_flags |= IFF_UP;
+       } else {
+               if (!(ifr.ifr_flags & IFF_UP))
+                       return 0;
+               ifr.ifr_flags &= ~IFF_UP;
+       }
 
-static int wpa_driver_nl80211_set_ifflags_ifname(
-       struct wpa_driver_nl80211_data *drv,
-       const char *ifname, int flags)
-{
-       struct ifreq ifr;
-
-       os_memset(&ifr, 0, sizeof(ifr));
-       os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
-       ifr.ifr_flags = flags & 0xffff;
-       if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, (caddr_t) &ifr) < 0) {
-               perror("SIOCSIFFLAGS");
+       if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
+               perror("ioctl[SIOCSIFFLAGS]");
                return -1;
        }
-       return 0;
-}
 
-
-/**
- * wpa_driver_nl80211_set_ifflags - Set interface flags (SIOCSIFFLAGS)
- * @drv: driver_nl80211 private data
- * @flags: New value for flags
- * Returns: 0 on success, -1 on failure
- */
-static int wpa_driver_nl80211_set_ifflags(struct wpa_driver_nl80211_data *drv,
-                                         int flags)
-{
-       return wpa_driver_nl80211_set_ifflags_ifname(drv, drv->ifname, flags);
+       return 0;
 }
 
 
@@ -877,7 +915,7 @@ static int wpa_driver_nl80211_set_country(void *priv, const char *alpha2_arg)
 
        msg = nlmsg_alloc();
        if (!msg)
-               goto nla_put_failure;
+               return -ENOMEM;
 
        alpha2[0] = alpha2_arg[0];
        alpha2[1] = alpha2_arg[1];
@@ -1078,8 +1116,10 @@ static void * wpa_driver_nl80211_init(void *ctx, const char *ifname)
                goto err6;
        }
 
+#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))
@@ -1109,8 +1149,6 @@ err1:
 static int
 wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
 {
-       int flags;
-
        drv->ifindex = if_nametoindex(drv->ifname);
 
        if (wpa_driver_nl80211_set_mode(drv, 0) < 0) {
@@ -1118,18 +1156,11 @@ wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
                           "use managed mode");
        }
 
-       if (wpa_driver_nl80211_get_ifflags(drv, &flags) != 0) {
-               wpa_printf(MSG_ERROR, "Could not get interface '%s' flags",
-                          drv->ifname);
+       if (hostapd_set_iface_flags(drv, drv->ifname, 1)) {
+               wpa_printf(MSG_ERROR, "Could not set interface '%s' "
+                          "UP", drv->ifname);
                return -1;
        }
-       if (!(flags & IFF_UP)) {
-               if (wpa_driver_nl80211_set_ifflags(drv, flags | IFF_UP) != 0) {
-                       wpa_printf(MSG_ERROR, "Could not set interface '%s' "
-                                  "UP", drv->ifname);
-                       return -1;
-               }
-       }
 
        wpa_driver_nl80211_capa(drv);
 
@@ -1149,7 +1180,6 @@ wpa_driver_nl80211_finish_drv_init(struct wpa_driver_nl80211_data *drv)
 static void wpa_driver_nl80211_deinit(void *priv)
 {
        struct wpa_driver_nl80211_data *drv = priv;
-       int flags;
 
 #ifdef CONFIG_AP
        if (drv->monitor_ifidx >= 0)
@@ -1168,8 +1198,7 @@ static void wpa_driver_nl80211_deinit(void *priv)
 
        eloop_unregister_read_sock(drv->link_event_sock);
 
-       if (wpa_driver_nl80211_get_ifflags(drv, &flags) == 0)
-               (void) wpa_driver_nl80211_set_ifflags(drv, flags & ~IFF_UP);
+       hostapd_set_iface_flags(drv, drv->ifname, 0);
        wpa_driver_nl80211_set_mode(drv, 0);
 
        close(drv->link_event_sock);
@@ -1196,7 +1225,9 @@ static void wpa_driver_nl80211_deinit(void *priv)
 static void wpa_driver_nl80211_scan_timeout(void *eloop_ctx, void *timeout_ctx)
 {
        wpa_printf(MSG_DEBUG, "Scan timeout - try to get results");
+#ifndef HOSTAPD
        wpa_supplicant_event(timeout_ctx, EVENT_SCAN_RESULTS, NULL);
+#endif /* HOSTAPD */
 }
 
 
@@ -1397,31 +1428,29 @@ nla_put_failure:
 }
 
 
-static int wpa_driver_nl80211_set_key(void *priv, wpa_alg alg,
-                                     const u8 *addr, int key_idx,
-                                     int set_tx, const u8 *seq,
-                                     size_t seq_len,
-                                     const u8 *key, size_t key_len)
+static int nl_set_encr(int ifindex, struct wpa_driver_nl80211_data *drv,
+                      wpa_alg alg, const u8 *addr, int key_idx, int set_tx,
+                      const u8 *seq, size_t seq_len,
+                      const u8 *key, size_t key_len)
 {
-       struct wpa_driver_nl80211_data *drv = priv;
-       int err;
        struct nl_msg *msg;
+       int ret;
 
-       wpa_printf(MSG_DEBUG, "%s: alg=%d addr=%p key_idx=%d set_tx=%d "
-                  "seq_len=%lu key_len=%lu",
-                  __func__, alg, addr, key_idx, set_tx,
+       wpa_printf(MSG_DEBUG, "%s: ifindex=%d alg=%d addr=%p key_idx=%d "
+                  "set_tx=%d seq_len=%lu key_len=%lu",
+                  __func__, ifindex, alg, addr, key_idx, set_tx,
                   (unsigned long) seq_len, (unsigned long) key_len);
 
        msg = nlmsg_alloc();
-       if (msg == NULL)
-               return -1;
+       if (!msg)
+               return -ENOMEM;
 
        if (alg == WPA_ALG_NONE) {
-               genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
-                           NL80211_CMD_DEL_KEY, 0);
+               genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
+                           0, NL80211_CMD_DEL_KEY, 0);
        } else {
-               genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
-                           NL80211_CMD_NEW_KEY, 0);
+               genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
+                           0, NL80211_CMD_NEW_KEY, 0);
                NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
                switch (alg) {
                case WPA_ALG_WEP:
@@ -1438,57 +1467,84 @@ static int wpa_driver_nl80211_set_key(void *priv, wpa_alg alg,
                case WPA_ALG_CCMP:
                        NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC04);
                        break;
-#ifdef CONFIG_IEEE80211W
                case WPA_ALG_IGTK:
                        NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC06);
                        break;
-#endif /* CONFIG_IEEE80211W */
                default:
+                       wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
+                                  "algorithm %d", __func__, alg);
                        nlmsg_free(msg);
                        return -1;
                }
        }
 
+       if (seq)
+               NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, seq_len, seq);
+
        if (addr && os_memcmp(addr, "\xff\xff\xff\xff\xff\xff", ETH_ALEN) != 0)
        {
                wpa_printf(MSG_DEBUG, "   addr=" MACSTR, MAC2STR(addr));
                NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
        }
        NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
-       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
+       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
 
-       err = send_and_recv_msgs(drv, msg, NULL, NULL);
-       if (err) {
-               wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d", err);
-               return -1;
-       }
+       ret = send_and_recv_msgs(drv, msg, NULL, NULL);
+       if (ret == -ENOENT && alg == WPA_ALG_NONE)
+               ret = 0;
+       if (ret)
+               wpa_printf(MSG_DEBUG, "nl80211: set_key failed; err=%d %s)",
+                          ret, strerror(-ret));
 
-       if (set_tx && alg != WPA_ALG_NONE) {
-               msg = nlmsg_alloc();
-               if (msg == NULL)
-                       return -1;
+       /*
+        * If we failed or don't need to set the default TX key (below),
+        * we're done here.
+        */
+       if (ret || !set_tx || alg == WPA_ALG_NONE)
+               return ret;
+#ifdef HOSTAPD /* FIX: is this needed? */
+       if (addr)
+               return ret;
+#endif /* HOSTAPD */
 
-               genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
-                           0, NL80211_CMD_SET_KEY, 0);
-               NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
-               NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, drv->ifindex);
-               NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
+       msg = nlmsg_alloc();
+       if (!msg)
+               return -ENOMEM;
 
-               err = send_and_recv_msgs(drv, msg, NULL, NULL);
-               if (err) {
-                       wpa_printf(MSG_DEBUG, "nl80211: set default key "
-                                  "failed; err=%d", err);
-                       return -1;
-               }
-       }
+       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
+                   0, NL80211_CMD_SET_KEY, 0);
+       NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
+       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
+       if (alg == WPA_ALG_IGTK)
+               NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
+       else
+               NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
 
-       return 0;
+       ret = send_and_recv_msgs(drv, msg, NULL, NULL);
+       if (ret == -ENOENT)
+               ret = 0;
+       if (ret)
+               wpa_printf(MSG_DEBUG, "nl80211: set_key default failed; "
+                          "err=%d %s)", ret, strerror(-ret));
+       return ret;
 
 nla_put_failure:
        return -ENOBUFS;
 }
 
 
+static int wpa_driver_nl80211_set_key(void *priv, wpa_alg alg,
+                                     const u8 *addr, int key_idx,
+                                     int set_tx, const u8 *seq,
+                                     size_t seq_len,
+                                     const u8 *key, size_t key_len)
+{
+       struct wpa_driver_nl80211_data *drv = priv;
+       return nl_set_encr(drv->ifindex, drv, alg, addr, key_idx, set_tx, seq,
+                          seq_len, key, key_len);
+}
+
+
 static int wpa_driver_nl80211_mlme(struct wpa_driver_nl80211_data *drv,
                                   const u8 *addr, int cmd, u16 reason_code)
 {
@@ -1887,10 +1943,6 @@ wpa_driver_nl80211_get_hw_feature_data(void *priv, u16 *num_modes, u16 *flags)
        return NULL;
 }
 
-#endif /* CONFIG_AP || HOSTAPD */
-
-
-#ifdef CONFIG_AP
 
 static int wpa_driver_nl80211_send_frame(struct wpa_driver_nl80211_data *drv,
                                         const void *data, size_t len,
@@ -1962,6 +2014,10 @@ static int wpa_driver_nl80211_send_mlme(void *priv, const u8 *data,
                                             !do_not_encrypt);
 }
 
+#endif /* CONFIG_AP || HOSTAPD */
+
+
+#ifdef CONFIG_AP
 
 static int wpa_driver_nl80211_set_beacon(void *priv,
                                         const u8 *head, size_t head_len,
@@ -2059,12 +2115,20 @@ nla_put_failure:
        return -1;
 }
 
+#endif /* CONFIG_AP */
+
+#if defined(CONFIG_AP) || defined(HOSTAPD)
 
 static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
                                 int ifidx)
 {
        struct nl_msg *msg;
 
+#ifdef HOSTAPD
+       /* stop listening for EAPOL on this interface */
+       del_ifidx(drv, ifidx);
+#endif /* HOSTAPD */
+
        msg = nlmsg_alloc();
        if (!msg)
                goto nla_put_failure;
@@ -2082,11 +2146,16 @@ static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv,
 
 
 static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
-                               const char *ifname, enum nl80211_iftype iftype)
+                               const char *ifname, enum nl80211_iftype iftype,
+                               const u8 *addr)
 {
        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)
@@ -2128,16 +2197,43 @@ static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
        if (ifidx <= 0)
                return -1;
 
+#ifdef HOSTAPD
+       /* 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;
+               }
+       }
+#endif /* HOSTAPD */
+
        return ifidx;
 }
 
+#endif /* CONFIG_AP || HOSTAPD */
 
-enum ieee80211_msg_type {
-       ieee80211_msg_normal = 0,
-       ieee80211_msg_tx_callback_ack = 1,
-       ieee80211_msg_tx_callback_fail = 2,
-};
-
+#ifdef CONFIG_AP
 
 void ap_tx_status(void *ctx, const u8 *addr,
                  const u8 *buf, size_t len, int ack);
@@ -2146,6 +2242,9 @@ void ap_mgmt_rx(void *ctx, u8 *buf, size_t len, u16 stype,
                struct hostapd_frame_info *fi);
 void ap_mgmt_tx_cb(void *ctx, u8 *buf, size_t len, u16 stype, int ok);
 
+#endif /* CONFIG_AP */
+
+#if defined(CONFIG_AP) || defined(HOSTAPD)
 
 static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
 {
@@ -2162,14 +2261,22 @@ static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
        case WLAN_FC_TYPE_MGMT:
                wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
                           ok ? "ACK" : "fail");
+#ifdef HOSTAPD
+               hostapd_mgmt_tx_cb(ctx, buf, len, stype, ok);
+#else /* HOSTAPD */
                ap_mgmt_tx_cb(ctx, buf, len, stype, ok);
+#endif /* HOSTAPD */
                break;
        case WLAN_FC_TYPE_CTRL:
                wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
                           ok ? "ACK" : "fail");
                break;
        case WLAN_FC_TYPE_DATA:
+#ifdef HOSTAPD
+               hostapd_tx_status(ctx, hdr->addr1, buf, len, ok);
+#else /* HOSTAPD */
                ap_tx_status(ctx, hdr->addr1, buf, len, ok);
+#endif /* HOSTAPD */
                break;
        default:
                wpa_printf(MSG_DEBUG, "unknown TX callback frame type %d",
@@ -2178,6 +2285,9 @@ static void handle_tx_callback(void *ctx, u8 *buf, size_t len, int ok)
        }
 }
 
+#endif /* CONFIG_AP || HOSTAPD */
+
+#ifdef CONFIG_AP
 
 static void handle_frame(struct wpa_driver_nl80211_data *drv,
                         u8 *buf, size_t len,
@@ -2257,6 +2367,9 @@ static void handle_frame(struct wpa_driver_nl80211_data *drv,
        }
 }
 
+#endif /* CONFIG_AP */
+
+#if defined(CONFIG_AP) || defined(HOSTAPD)
 
 static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
 {
@@ -2485,31 +2598,18 @@ nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
        struct sockaddr_ll ll;
        int optval;
        socklen_t optlen;
-       int flags;
 
        snprintf(buf, IFNAMSIZ, "mon.%s", drv->ifname);
        buf[IFNAMSIZ - 1] = '\0';
 
        drv->monitor_ifidx =
-               nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR);
+               nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL);
 
        if (drv->monitor_ifidx < 0)
                return -1;
 
-       if (wpa_driver_nl80211_get_ifflags_ifname(drv, buf, &flags) != 0) {
-               wpa_printf(MSG_ERROR, "Could not get interface '%s' flags",
-                          buf);
+       if (hostapd_set_iface_flags(drv, buf, 1))
                goto error;
-       }
-       if (!(flags & IFF_UP)) {
-               if (wpa_driver_nl80211_set_ifflags_ifname(drv, buf,
-                                                         flags | IFF_UP) != 0)
-               {
-                       wpa_printf(MSG_ERROR, "Could not set interface '%s' "
-                                  "UP", buf);
-                       goto error;
-               }
-       }
 
        memset(&ll, 0, sizeof(ll));
        ll.sll_family = AF_PACKET;
@@ -2526,8 +2626,7 @@ nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
                /* This works, but will cost in performance. */
        }
 
-       if (bind(drv->monitor_sock, (struct sockaddr *) &ll,
-                sizeof(ll)) < 0) {
+       if (bind(drv->monitor_sock, (struct sockaddr *) &ll, sizeof(ll)) < 0) {
                perror("monitor socket bind");
                goto error;
        }
@@ -2552,6 +2651,9 @@ nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
        return -1;
 }
 
+#endif /* CONFIG_AP || HOSTAPD */
+
+#ifdef CONFIG_AP
 
 static int wpa_driver_nl80211_ap(struct wpa_driver_nl80211_data *drv,
                                 struct wpa_driver_associate_params *params)
@@ -2652,7 +2754,7 @@ nla_put_failure:
 static int wpa_driver_nl80211_set_mode(struct wpa_driver_nl80211_data *drv,
                                       int mode)
 {
-       int ret = -1, flags;
+       int ret = -1;
        struct nl_msg *msg;
        int nlmode;
 
@@ -2695,9 +2797,7 @@ try_again:
         * take the device down, try to set the mode again, and bring the
         * device back up.
         */
-       if (wpa_driver_nl80211_get_ifflags(drv, &flags) == 0) {
-               (void) wpa_driver_nl80211_set_ifflags(drv, flags & ~IFF_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)
@@ -2714,11 +2814,8 @@ try_again:
                                   drv->ifname, ret, strerror(-ret));
                }
 
-               /* Ignore return value of get_ifflags to ensure that the device
-                * is always up like it was before this function was called.
-                */
-               (void) wpa_driver_nl80211_get_ifflags(drv, &flags);
-               (void) wpa_driver_nl80211_set_ifflags(drv, flags | IFF_UP);
+               if (hostapd_set_iface_flags(drv, drv->ifname, 1))
+                       ret = -1;
        }
 
        return ret;
@@ -2752,13 +2849,6 @@ static int wpa_driver_nl80211_set_operstate(void *priv, int state)
 
 static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
 
-enum ieee80211_msg_type {
-       ieee80211_msg_normal = 0,
-       ieee80211_msg_tx_callback_ack = 1,
-       ieee80211_msg_tx_callback_fail = 2,
-};
-
-
 static int i802_sta_deauth(void *priv, const u8 *addr, int reason);
 static int i802_sta_disassoc(void *priv, const u8 *addr, int reason);
 
@@ -2838,134 +2928,14 @@ static int have_ifidx(struct wpa_driver_nl80211_data *drv, int ifidx)
 }
 
 
-static int hostapd_set_iface_flags(struct wpa_driver_nl80211_data *drv,
-                                  const char *ifname, int dev_up)
+static int i802_set_key(const char *iface, void *priv, wpa_alg alg,
+                       const u8 *addr, int key_idx, int set_tx, const u8 *seq,
+                       size_t seq_len, const u8 *key, size_t key_len)
 {
-       struct ifreq ifr;
-
-       if (drv->ioctl_sock < 0)
-               return -1;
-
-       memset(&ifr, 0, sizeof(ifr));
-       os_strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
-
-       if (ioctl(drv->ioctl_sock, SIOCGIFFLAGS, &ifr) != 0) {
-               perror("ioctl[SIOCGIFFLAGS]");
-               wpa_printf(MSG_DEBUG, "Could not read interface flags (%s)",
-                          drv->ifname);
-               return -1;
-       }
-
-       if (dev_up)
-               ifr.ifr_flags |= IFF_UP;
-       else
-               ifr.ifr_flags &= ~IFF_UP;
-
-       if (ioctl(drv->ioctl_sock, SIOCSIFFLAGS, &ifr) != 0) {
-               perror("ioctl[SIOCSIFFLAGS]");
-               return -1;
-       }
-
-       return 0;
-}
-
-
-static int nl_set_encr(int ifindex, struct wpa_driver_nl80211_data *drv,
-                      wpa_alg alg, const u8 *addr, int idx, const u8 *key,
-                      size_t key_len, int txkey)
-{
-       struct nl_msg *msg;
-       int ret;
-
-       msg = nlmsg_alloc();
-       if (!msg)
-               return -ENOMEM;
-
-       if (alg == WPA_ALG_NONE) {
-               genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
-                           0, NL80211_CMD_DEL_KEY, 0);
-       } else {
-               genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
-                           0, NL80211_CMD_NEW_KEY, 0);
-               NLA_PUT(msg, NL80211_ATTR_KEY_DATA, key_len, key);
-               switch (alg) {
-               case WPA_ALG_WEP:
-                       if (key_len == 5)
-                               NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
-                                           0x000FAC01);
-                       else
-                               NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER,
-                                           0x000FAC05);
-                       break;
-               case WPA_ALG_TKIP:
-                       NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC02);
-                       break;
-               case WPA_ALG_CCMP:
-                       NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC04);
-                       break;
-               case WPA_ALG_IGTK:
-                       NLA_PUT_U32(msg, NL80211_ATTR_KEY_CIPHER, 0x000FAC06);
-                       break;
-               default:
-                       wpa_printf(MSG_ERROR, "%s: Unsupported encryption "
-                                  "algorithm %d", __func__, alg);
-                       nlmsg_free(msg);
-                       return -1;
-               }
-       }
-
-       if (addr)
-               NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
-       NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
-       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
-
-       ret = send_and_recv_msgs(drv, msg, NULL, NULL);
-       if (ret == -ENOENT)
-               ret = 0;
-
-       /*
-        * If we failed or don't need to set the default TX key (below),
-        * we're done here.
-        */
-       if (ret || !txkey || addr)
-               return ret;
-
-       msg = nlmsg_alloc();
-       if (!msg)
-               return -ENOMEM;
-
-       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
-                   0, NL80211_CMD_SET_KEY, 0);
-       NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
-       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
-       if (alg == WPA_ALG_IGTK)
-               NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
-       else
-               NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
-
-       ret = send_and_recv_msgs(drv, msg, NULL, NULL);
-       if (ret == -ENOENT)
-               ret = 0;
-       return ret;
- nla_put_failure:
-       return -ENOBUFS;
-}
-
-
-static int i802_set_key(const char *iface, void *priv, wpa_alg alg,
-                       const u8 *addr, int key_idx, int set_tx, const u8 *seq,
-                       size_t seq_len, const u8 *key, size_t key_len)
-{
-       struct wpa_driver_nl80211_data *drv = priv;
-       int ret;
-
-       ret = nl_set_encr(if_nametoindex(iface), drv, alg, addr, key_idx, key,
-                         key_len, set_tx);
-       if (ret < 0)
-               return ret;
-
-       return ret;
-}
+       struct wpa_driver_nl80211_data *drv = priv;
+       return nl_set_encr(if_nametoindex(iface), drv, alg, addr, key_idx,
+                          set_tx, seq, seq_len, key, key_len);
+}
 
 
 static inline int min_int(int a, int b)
@@ -3053,73 +3023,6 @@ static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
 }
 
 
-static int i802_send_frame(void *priv, const void *data, size_t len,
-                          int encrypt, int flags)
-{
-       __u8 rtap_hdr[] = {
-               0x00, 0x00, /* radiotap version */
-               0x0e, 0x00, /* radiotap length */
-               0x02, 0xc0, 0x00, 0x00, /* bmap: flags, tx and rx flags */
-               IEEE80211_RADIOTAP_F_FRAG, /* F_FRAG (fragment if required) */
-               0x00,       /* padding */
-               0x00, 0x00, /* RX and TX flags to indicate that */
-               0x00, 0x00, /* this is the injected frame directly */
-       };
-       struct wpa_driver_nl80211_data *drv = priv;
-       struct iovec iov[2] = {
-               {
-                       .iov_base = &rtap_hdr,
-                       .iov_len = sizeof(rtap_hdr),
-               },
-               {
-                       .iov_base = (void*)data,
-                       .iov_len = len,
-               }
-       };
-       struct msghdr msg = {
-               .msg_name = NULL,
-               .msg_namelen = 0,
-               .msg_iov = iov,
-               .msg_iovlen = 2,
-               .msg_control = NULL,
-               .msg_controllen = 0,
-               .msg_flags = 0,
-       };
-
-       if (encrypt)
-               rtap_hdr[8] |= IEEE80211_RADIOTAP_F_WEP;
-
-       return sendmsg(drv->monitor_sock, &msg, flags);
-}
-
-static int i802_send_mgmt_frame(void *priv, const void *data, size_t len,
-                               int flags)
-{
-       struct ieee80211_mgmt *mgmt;
-       int do_not_encrypt = 0;
-       u16 fc;
-
-       mgmt = (struct ieee80211_mgmt *) data;
-       fc = le_to_host16(mgmt->frame_control);
-
-       if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
-           WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_AUTH) {
-               /*
-                * Only one of the authentication frame types is encrypted.
-                * In order for static WEP encryption to work properly (i.e.,
-                * to not encrypt the frame), we need to tell mac80211 about
-                * the frames that must not be encrypted.
-                */
-               u16 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
-               u16 auth_trans = le_to_host16(mgmt->u.auth.auth_transaction);
-               if (auth_alg == WLAN_AUTH_OPEN ||
-                   (auth_alg == WLAN_AUTH_SHARED_KEY && auth_trans != 3))
-                       do_not_encrypt = 1;
-       }
-
-       return i802_send_frame(priv, data, len, !do_not_encrypt, flags);
-}
-
 /* Set kernel driver on given frequency (MHz) */
 static int i802_set_freq(void *priv, struct hostapd_freq_params *freq)
 {
@@ -3379,14 +3282,13 @@ static int i802_send_eapol(void *priv, const u8 *addr, const u8 *data,
        pos += 2;
        memcpy(pos, data, data_len);
 
-       res = i802_send_frame(drv, (u8 *) hdr, len, encrypt, 0);
-       free(hdr);
-
+       res = wpa_driver_nl80211_send_frame(drv, (u8 *) hdr, len, encrypt);
        if (res < 0) {
-               perror("i802_send_eapol: send");
-               printf("i802_send_eapol - packet len: %lu - failed\n",
-                      (unsigned long) len);
+               wpa_printf(MSG_ERROR, "i802_send_eapol - packet len: %lu - "
+                          "failed: %d (%s)",
+                          (unsigned long) len, errno, strerror(errno));
        }
+       free(hdr);
 
        return res;
 }
@@ -3551,180 +3453,75 @@ static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
 }
 
 
-static void nl80211_remove_iface(struct wpa_driver_nl80211_data *drv, int ifidx)
+static int i802_bss_add(void *priv, const char *ifname, const u8 *bssid)
 {
-       struct nl_msg *msg;
+       struct wpa_driver_nl80211_data *drv = priv;
+       int ifidx;
+       struct i802_bss *bss;
 
-       /* stop listening for EAPOL on this interface */
-       del_ifidx(drv, ifidx);
+       bss = os_zalloc(sizeof(*bss));
+       if (bss == NULL)
+               return -1;
+       os_strlcpy(bss->ifname, ifname, IFNAMSIZ);
 
-       msg = nlmsg_alloc();
-       if (!msg)
-               goto nla_put_failure;
+       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;
+}
 
-       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
-                   0, NL80211_CMD_DEL_INTERFACE, 0);
-       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifidx);
 
-       if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
-               return;
- nla_put_failure:
-       printf("Failed to remove interface.\n");
+static int i802_bss_remove(void *priv, const char *ifname)
+{
+       struct wpa_driver_nl80211_data *drv = priv;
+       struct i802_bss *bss, *prev;
+       nl80211_remove_iface(priv, if_nametoindex(ifname));
+       prev = &drv->bss;
+       bss = drv->bss.next;
+       while (bss) {
+               if (os_strncmp(ifname, bss->ifname, IFNAMSIZ) == 0) {
+                       prev->next = bss->next;
+                       os_free(bss);
+                       break;
+               }
+               prev = bss;
+               bss = bss->next;
+       }
+       return 0;
 }
 
 
-static int nl80211_create_iface(struct wpa_driver_nl80211_data *drv,
-                               const char *ifname,
-                               enum nl80211_iftype iftype,
-                               const u8 *addr)
+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)
 {
-       struct nl_msg *msg, *flags = NULL;
-       int ifidx;
-       struct ifreq ifreq;
-       struct iwreq iwr;
-       int ret = -ENOBUFS;
+       struct wpa_driver_nl80211_data *drv = priv;
+       struct nl_msg *msg;
+       u8 cmd = NL80211_CMD_NEW_BEACON;
+       int ret;
+       struct i802_bss *bss;
+
+       bss = get_bss(drv, iface);
+       if (bss == NULL)
+               return -ENOENT;
 
        msg = nlmsg_alloc();
        if (!msg)
-               return -1;
-
-       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
-                   0, NL80211_CMD_NEW_INTERFACE, 0);
-       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->ifname));
-       NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
-       NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
-
-       if (iftype == NL80211_IFTYPE_MONITOR) {
-               int err;
-
-               flags = nlmsg_alloc();
-               if (!flags)
-                       goto nla_put_failure;
-
-               NLA_PUT_FLAG(flags, NL80211_MNTR_FLAG_COOK_FRAMES);
-
-               err = nla_put_nested(msg, NL80211_ATTR_MNTR_FLAGS, flags);
-
-               nlmsg_free(flags);
-
-               if (err)
-                       goto nla_put_failure;
-       }
-
-       ret = send_and_recv_msgs(drv, msg, NULL, NULL);
-       if (ret) {
- nla_put_failure:
-               printf("Failed to create interface %s.\n", ifname);
-               return ret;
-       }
-
-       ifidx = if_nametoindex(ifname);
-
-       if (ifidx <= 0)
-               return -1;
-
-       /* 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;
-               }
-       }
-
-       return ifidx;
-}
-
-
-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;
-       os_strlcpy(bss->ifname, ifname, IFNAMSIZ);
-
-       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;
-       nl80211_remove_iface(priv, if_nametoindex(ifname));
-       prev = &drv->bss;
-       bss = drv->bss.next;
-       while (bss) {
-               if (os_strncmp(ifname, bss->ifname, IFNAMSIZ) == 0) {
-                       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)
-{
-       struct wpa_driver_nl80211_data *drv = priv;
-       struct nl_msg *msg;
-       u8 cmd = NL80211_CMD_NEW_BEACON;
-       int ret;
-       struct i802_bss *bss;
-
-       bss = get_bss(drv, iface);
-       if (bss == NULL)
-               return -ENOENT;
-
-       msg = nlmsg_alloc();
-       if (!msg)
-               return -ENOMEM;
-
-       wpa_printf(MSG_DEBUG, "nl80211: Set beacon (iface=%s beacon_set=%d)",
-                  iface, bss->beacon_set);
-       if (bss->beacon_set)
-               cmd = NL80211_CMD_SET_BEACON;
+               return -ENOMEM;
+
+       wpa_printf(MSG_DEBUG, "nl80211: Set beacon (iface=%s beacon_set=%d)",
+                  iface, bss->beacon_set);
+       if (bss->beacon_set)
+               cmd = NL80211_CMD_SET_BEACON;
 
        genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
                    0, cmd, 0);
@@ -3898,67 +3695,12 @@ static int i802_set_sta_vlan(void *priv, const u8 *addr,
 }
 
 
-static int i802_set_country(void *priv, const char *country)
-{
-       struct wpa_driver_nl80211_data *drv = priv;
-       struct nl_msg *msg;
-       char alpha2[3];
-
-       msg = nlmsg_alloc();
-       if (!msg)
-               return -ENOMEM;
-
-       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0,
-                   0, NL80211_CMD_REQ_SET_REG, 0);
-
-       alpha2[0] = country[0];
-       alpha2[1] = country[1];
-       alpha2[2] = '\0';
-       NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, alpha2);
-
-       return send_and_recv_msgs(drv, msg, NULL, NULL);
- nla_put_failure:
-       return -ENOBUFS;
-}
-
-
-static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
-                              int ok)
-{
-       struct ieee80211_hdr *hdr;
-       u16 fc, type, 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_MGMT:
-               wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
-                          ok ? "ACK" : "fail");
-               hostapd_mgmt_tx_cb(hapd, buf, len, stype, ok);
-               break;
-       case WLAN_FC_TYPE_CTRL:
-               wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
-                          ok ? "ACK" : "fail");
-               break;
-       case WLAN_FC_TYPE_DATA:
-               hostapd_tx_status(hapd, hdr->addr1, buf, len, ok);
-               break;
-       default:
-               printf("unknown TX callback frame type %d\n", type);
-               break;
-       }
-}
-
-
 static void handle_frame(struct wpa_driver_nl80211_data *drv,
-                        struct hostapd_iface *iface, u8 *buf, size_t len,
+                        u8 *buf, size_t len,
                         struct hostapd_frame_info *hfi,
                         enum ieee80211_msg_type msg_type)
 {
+       struct hostapd_iface *iface = drv->hapd->iface;
        struct ieee80211_hdr *hdr;
        u16 fc, type, stype;
        size_t data_len = len;
@@ -4034,349 +3776,67 @@ static void handle_frame(struct wpa_driver_nl80211_data *drv,
                             stype != WLAN_FC_STYPE_BEACON))
                                return;
                } else
-                       broadcast_bssid = 1;
-       }
-
-       switch (msg_type) {
-       case ieee80211_msg_normal:
-               /* continue processing */
-               break;
-       case ieee80211_msg_tx_callback_ack:
-               handle_tx_callback(hapd, buf, data_len, 1);
-               return;
-       case ieee80211_msg_tx_callback_fail:
-               handle_tx_callback(hapd, buf, data_len, 0);
-               return;
-       }
-
-       switch (type) {
-       case WLAN_FC_TYPE_MGMT:
-               if (stype != WLAN_FC_STYPE_BEACON &&
-                   stype != WLAN_FC_STYPE_PROBE_REQ)
-                       wpa_printf(MSG_MSGDUMP, "MGMT");
-               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);
-               break;
-       case WLAN_FC_TYPE_CTRL:
-               /* can only get here with PS-Poll frames */
-               wpa_printf(MSG_DEBUG, "CTRL");
-               hostapd_rx_from_unknown_sta(drv->hapd, hdr->addr2);
-               break;
-       case WLAN_FC_TYPE_DATA:
-               hostapd_rx_from_unknown_sta(drv->hapd, hdr->addr2);
-               break;
-       }
-}
-
-
-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 void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
-{
-       struct wpa_driver_nl80211_data *drv = eloop_ctx;
-       int len;
-       unsigned char buf[3000];
-       struct hostapd_data *hapd = drv->hapd;
-       struct ieee80211_radiotap_iterator iter;
-       int ret;
-       struct hostapd_frame_info hfi;
-       int injected = 0, failed = 0, msg_type, rxflags = 0;
-
-       len = recv(sock, buf, sizeof(buf), 0);
-       if (len < 0) {
-               perror("recv");
-               return;
-       }
-
-       if (ieee80211_radiotap_iterator_init(&iter, (void*)buf, len)) {
-               printf("received invalid radiotap frame\n");
-               return;
-       }
-
-       memset(&hfi, 0, sizeof(hfi));
-
-       while (1) {
-               ret = ieee80211_radiotap_iterator_next(&iter);
-               if (ret == -ENOENT)
-                       break;
-               if (ret) {
-                       printf("received invalid radiotap frame (%d)\n", ret);
-                       return;
-               }
-               switch (iter.this_arg_index) {
-               case IEEE80211_RADIOTAP_FLAGS:
-                       if (*iter.this_arg & IEEE80211_RADIOTAP_F_FCS)
-                               len -= 4;
-                       break;
-               case IEEE80211_RADIOTAP_RX_FLAGS:
-                       rxflags = 1;
-                       break;
-               case IEEE80211_RADIOTAP_TX_FLAGS:
-                       injected = 1;
-                       failed = le_to_host16((*(uint16_t *) iter.this_arg)) &
-                                       IEEE80211_RADIOTAP_F_TX_FAIL;
-                       break;
-               case IEEE80211_RADIOTAP_DATA_RETRIES:
-                       break;
-               case IEEE80211_RADIOTAP_CHANNEL:
-                       /* TODO convert from freq/flags to channel number
-                       hfi.channel = XXX;
-                       hfi.phytype = XXX;
-                        */
-                       break;
-               case IEEE80211_RADIOTAP_RATE:
-                       hfi.datarate = *iter.this_arg * 5;
-                       break;
-               case IEEE80211_RADIOTAP_DB_ANTSIGNAL:
-                       hfi.ssi_signal = *iter.this_arg;
-                       break;
-               }
-       }
-
-       if (rxflags && injected)
-               return;
-
-       if (!injected)
-               msg_type = ieee80211_msg_normal;
-       else if (failed)
-               msg_type = ieee80211_msg_tx_callback_fail;
-       else
-               msg_type = ieee80211_msg_tx_callback_ack;
-
-       handle_frame(drv, hapd->iface, buf + iter.max_length,
-                    len - iter.max_length, &hfi, msg_type);
-}
-
-
-/*
- * we post-process the filter code later and rewrite
- * this to the offset to the last instruction
- */
-#define PASS   0xFF
-#define FAIL   0xFE
-
-static struct sock_filter msock_filter_insns[] = {
-       /*
-        * do a little-endian load of the radiotap length field
-        */
-       /* load lower byte into A */
-       BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 2),
-       /* put it into X (== index register) */
-       BPF_STMT(BPF_MISC| BPF_TAX, 0),
-       /* load upper byte into A */
-       BPF_STMT(BPF_LD  | BPF_B | BPF_ABS, 3),
-       /* left-shift it by 8 */
-       BPF_STMT(BPF_ALU | BPF_LSH | BPF_K, 8),
-       /* or with X */
-       BPF_STMT(BPF_ALU | BPF_OR | BPF_X, 0),
-       /* put result into X */
-       BPF_STMT(BPF_MISC| BPF_TAX, 0),
-
-       /*
-        * Allow management frames through, this also gives us those
-        * management frames that we sent ourselves with status
-        */
-       /* load the lower byte of the IEEE 802.11 frame control field */
-       BPF_STMT(BPF_LD  | BPF_B | BPF_IND, 0),
-       /* mask off frame type and version */
-       BPF_STMT(BPF_ALU | BPF_AND | BPF_K, 0xF),
-       /* accept frame if it's both 0, fall through otherwise */
-       BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, PASS, 0),
-
-       /*
-        * TODO: add a bit to radiotap RX flags that indicates
-        * that the sending station is not associated, then
-        * add a filter here that filters on our DA and that flag
-        * to allow us to deauth frames to that bad station.
-        *
-        * Not a regression -- we didn't do it before either.
-        */
-
-#if 0
-       /*
-        * drop non-data frames, WDS frames
-        */
-       /* load the lower byte of the frame control field */
-       BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
-       /* mask off QoS bit */
-       BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x0c),
-       /* drop non-data frames */
-       BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 8, 0, FAIL),
-       /* load the upper byte of the frame control field */
-       BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
-       /* mask off toDS/fromDS */
-       BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x03),
-       /* drop WDS frames */
-       BPF_JUMP(BPF_JMP  | BPF_JEQ | BPF_K, 3, FAIL, 0),
-#endif
-
-       /*
-        * add header length to index
-        */
-       /* load the lower byte of the frame control field */
-       BPF_STMT(BPF_LD   | BPF_B | BPF_IND, 0),
-       /* mask off QoS bit */
-       BPF_STMT(BPF_ALU  | BPF_AND | BPF_K, 0x80),
-       /* right shift it by 6 to give 0 or 2 */
-       BPF_STMT(BPF_ALU  | BPF_RSH | BPF_K, 6),
-       /* add data frame header length */
-       BPF_STMT(BPF_ALU  | BPF_ADD | BPF_K, 24),
-       /* add index, was start of 802.11 header */
-       BPF_STMT(BPF_ALU  | BPF_ADD | BPF_X, 0),
-       /* move to index, now start of LL header */
-       BPF_STMT(BPF_MISC | BPF_TAX, 0),
-
-       /*
-        * Accept empty data frames, we use those for
-        * polling activity.
-        */
-       BPF_STMT(BPF_LD  | BPF_W | BPF_LEN, 0),
-       BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_X, 0, PASS, 0),
-
-       /*
-        * Accept EAPOL frames
-        */
-       BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 0),
-       BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA0300, 0, FAIL),
-       BPF_STMT(BPF_LD  | BPF_W | BPF_IND, 4),
-       BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0x0000888E, PASS, FAIL),
-
-       /* keep these last two statements or change the code below */
-       /* return 0 == "DROP" */
-       BPF_STMT(BPF_RET | BPF_K, 0),
-       /* return ~0 == "keep all" */
-       BPF_STMT(BPF_RET | BPF_K, ~0),
-};
-
-static struct sock_fprog msock_filter = {
-       .len = sizeof(msock_filter_insns)/sizeof(msock_filter_insns[0]),
-       .filter = msock_filter_insns,
-};
-
-
-static int add_monitor_filter(int s)
-{
-       int idx;
-
-       /* rewrite all PASS/FAIL jump offsets */
-       for (idx = 0; idx < msock_filter.len; idx++) {
-               struct sock_filter *insn = &msock_filter_insns[idx];
-
-               if (BPF_CLASS(insn->code) == BPF_JMP) {
-                       if (insn->code == (BPF_JMP|BPF_JA)) {
-                               if (insn->k == PASS)
-                                       insn->k = msock_filter.len - idx - 2;
-                               else if (insn->k == FAIL)
-                                       insn->k = msock_filter.len - idx - 3;
-                       }
-
-                       if (insn->jt == PASS)
-                               insn->jt = msock_filter.len - idx - 2;
-                       else if (insn->jt == FAIL)
-                               insn->jt = msock_filter.len - idx - 3;
-
-                       if (insn->jf == PASS)
-                               insn->jf = msock_filter.len - idx - 2;
-                       else if (insn->jf == FAIL)
-                               insn->jf = msock_filter.len - idx - 3;
-               }
-       }
-
-       if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER,
-                      &msock_filter, sizeof(msock_filter))) {
-               perror("SO_ATTACH_FILTER");
-               return -1;
-       }
-
-       return 0;
-}
-
-
-static int nl80211_create_monitor_interface(struct wpa_driver_nl80211_data *drv)
-{
-       char buf[IFNAMSIZ];
-       struct sockaddr_ll ll;
-       int optval;
-       socklen_t optlen;
-
-       snprintf(buf, IFNAMSIZ, "mon.%s", drv->ifname);
-       buf[IFNAMSIZ - 1] = '\0';
-
-       drv->monitor_ifidx =
-               nl80211_create_iface(drv, buf, NL80211_IFTYPE_MONITOR, NULL);
-
-       if (drv->monitor_ifidx < 0)
-               return -1;
-
-       if (hostapd_set_iface_flags(drv, buf, 1))
-               goto error;
-
-       memset(&ll, 0, sizeof(ll));
-       ll.sll_family = AF_PACKET;
-       ll.sll_ifindex = drv->monitor_ifidx;
-       drv->monitor_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
-       if (drv->monitor_sock < 0) {
-               perror("socket[PF_PACKET,SOCK_RAW]");
-               goto error;
+                       broadcast_bssid = 1;
        }
 
-       if (add_monitor_filter(drv->monitor_sock)) {
-               wpa_printf(MSG_INFO, "Failed to set socket filter for monitor "
-                          "interface; do filtering in user space");
-               /* This works, but will cost in performance. */
+       switch (msg_type) {
+       case ieee80211_msg_normal:
+               /* continue processing */
+               break;
+       case ieee80211_msg_tx_callback_ack:
+               handle_tx_callback(hapd, buf, data_len, 1);
+               return;
+       case ieee80211_msg_tx_callback_fail:
+               handle_tx_callback(hapd, buf, data_len, 0);
+               return;
        }
 
-       if (bind(drv->monitor_sock, (struct sockaddr *) &ll,
-                sizeof(ll)) < 0) {
-               perror("monitor socket bind");
-               goto error;
+       switch (type) {
+       case WLAN_FC_TYPE_MGMT:
+               if (stype != WLAN_FC_STYPE_BEACON &&
+                   stype != WLAN_FC_STYPE_PROBE_REQ)
+                       wpa_printf(MSG_MSGDUMP, "MGMT");
+               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);
+               break;
+       case WLAN_FC_TYPE_CTRL:
+               /* can only get here with PS-Poll frames */
+               wpa_printf(MSG_DEBUG, "CTRL");
+               hostapd_rx_from_unknown_sta(drv->hapd, hdr->addr2);
+               break;
+       case WLAN_FC_TYPE_DATA:
+               hostapd_rx_from_unknown_sta(drv->hapd, hdr->addr2);
+               break;
        }
+}
 
-       optlen = sizeof(optval);
-       optval = 20;
-       if (setsockopt
-           (drv->monitor_sock, SOL_SOCKET, SO_PRIORITY, &optval, optlen)) {
-               perror("Failed to set socket priority");
-               goto error;
-       }
 
-       if (eloop_register_read_sock(drv->monitor_sock, handle_monitor_read,
-                                    drv, NULL)) {
-               printf("Could not register monitor read socket\n");
-               goto error;
+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;
        }
 
-       return 0;
- error:
-       nl80211_remove_iface(drv, drv->monitor_ifidx);
-       return -1;
+       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);
+       }
 }
 
 
@@ -4782,6 +4242,7 @@ static int i802_ht_scan(struct wpa_driver_nl80211_data *drv)
 static int i802_init_sockets(struct wpa_driver_nl80211_data *drv, const u8 *bssid)
 {
        struct ifreq ifr;
+       int ret;
 
        drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
        if (drv->ioctl_sock < 0) {
@@ -4845,6 +4306,28 @@ static int i802_init_sockets(struct wpa_driver_nl80211_data *drv, const u8 *bssi
                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));
+       }
+
+       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);
+
 #ifdef CONFIG_IEEE80211N
        if (drv->ht_40mhz_scan) {
                if (nl80211_set_mode(drv, drv->ifname, NL80211_IFTYPE_STATION)
@@ -4924,255 +4407,6 @@ static int i802_sta_clear_stats(void *priv, const u8 *addr)
 }
 
 
-static void
-hostapd_wireless_event_wireless_custom(struct wpa_driver_nl80211_data *drv,
-                                      char *custom)
-{
-       wpa_printf(MSG_DEBUG, "Custom wireless event: '%s'", custom);
-
-       if (strncmp(custom, "MLME-MICHAELMICFAILURE.indication", 33) == 0) {
-               char *pos;
-               u8 addr[ETH_ALEN];
-               pos = strstr(custom, "addr=");
-               if (pos == NULL) {
-                       wpa_printf(MSG_DEBUG,
-                                  "MLME-MICHAELMICFAILURE.indication "
-                                  "without sender address ignored");
-                       return;
-               }
-               pos += 5;
-               if (hwaddr_aton(pos, addr) == 0) {
-                       hostapd_michael_mic_failure(drv->hapd, addr);
-               } else {
-                       wpa_printf(MSG_DEBUG,
-                                  "MLME-MICHAELMICFAILURE.indication "
-                                  "with invalid MAC address");
-               }
-       }
-}
-
-
-static void hostapd_wireless_event_wireless(struct wpa_driver_nl80211_data *drv,
-                                           char *data, int len)
-{
-       struct iw_event iwe_buf, *iwe = &iwe_buf;
-       char *pos, *end, *custom, *buf;
-
-       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. */
-               memcpy(&iwe_buf, pos, IW_EV_LCP_LEN);
-               wpa_printf(MSG_DEBUG, "Wireless event: cmd=0x%x len=%d",
-                          iwe->cmd, iwe->len);
-               if (iwe->len <= IW_EV_LCP_LEN)
-                       return;
-
-               custom = pos + IW_EV_POINT_LEN;
-               if (drv->we_version > 18 &&
-                   (iwe->cmd == IWEVMICHAELMICFAILURE ||
-                    iwe->cmd == IWEVCUSTOM)) {
-                       /* WE-19 removed the pointer from struct iw_point */
-                       char *dpos = (char *) &iwe_buf.u.data.length;
-                       int dlen = dpos - (char *) &iwe_buf;
-                       memcpy(dpos, pos + IW_EV_LCP_LEN,
-                              sizeof(struct iw_event) - dlen);
-               } else {
-                       memcpy(&iwe_buf, pos, sizeof(struct iw_event));
-                       custom += IW_EV_POINT_OFF;
-               }
-
-               switch (iwe->cmd) {
-               case IWEVCUSTOM:
-                       if (custom + iwe->u.data.length > end)
-                               return;
-                       buf = malloc(iwe->u.data.length + 1);
-                       if (buf == NULL)
-                               return;
-                       memcpy(buf, custom, iwe->u.data.length);
-                       buf[iwe->u.data.length] = '\0';
-                       hostapd_wireless_event_wireless_custom(drv, buf);
-                       free(buf);
-                       break;
-               }
-
-               pos += iwe->len;
-       }
-}
-
-
-static void hostapd_wireless_event_rtm_newlink(struct wpa_driver_nl80211_data *drv,
-                                              struct nlmsghdr *h, int len)
-{
-       struct ifinfomsg *ifi;
-       int attrlen, _nlmsg_len, rta_len;
-       struct rtattr *attr;
-
-       if (len < (int) sizeof(*ifi))
-               return;
-
-       ifi = NLMSG_DATA(h);
-
-       /* TODO: use ifi->ifi_index to filter out wireless events from other
-        * interfaces */
-
-       _nlmsg_len = NLMSG_ALIGN(sizeof(struct ifinfomsg));
-
-       attrlen = h->nlmsg_len - _nlmsg_len;
-       if (attrlen < 0)
-               return;
-
-       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) {
-                       hostapd_wireless_event_wireless(
-                               drv, ((char *) attr) + rta_len,
-                               attr->rta_len - rta_len);
-               }
-               attr = RTA_NEXT(attr, attrlen);
-       }
-}
-
-
-static void hostapd_wireless_event_receive(int sock, void *eloop_ctx,
-                                          void *sock_ctx)
-{
-       char buf[256];
-       int left;
-       struct sockaddr_nl from;
-       socklen_t fromlen;
-       struct nlmsghdr *h;
-       struct wpa_driver_nl80211_data *drv = eloop_ctx;
-
-       fromlen = sizeof(from);
-       left = recvfrom(sock, buf, sizeof(buf), MSG_DONTWAIT,
-                       (struct sockaddr *) &from, &fromlen);
-       if (left < 0) {
-               if (errno != EINTR && errno != EAGAIN)
-                       perror("recvfrom(netlink)");
-               return;
-       }
-
-       h = (struct nlmsghdr *) buf;
-       while (left >= (int) sizeof(*h)) {
-               int len, plen;
-
-               len = h->nlmsg_len;
-               plen = len - sizeof(*h);
-               if (len > left || plen < 0) {
-                       printf("Malformed netlink message: "
-                              "len=%d left=%d plen=%d\n",
-                              len, left, plen);
-                       break;
-               }
-
-               switch (h->nlmsg_type) {
-               case RTM_NEWLINK:
-                       hostapd_wireless_event_rtm_newlink(drv, h, plen);
-                       break;
-               }
-
-               len = NLMSG_ALIGN(len);
-               left -= len;
-               h = (struct nlmsghdr *) ((char *) h + len);
-       }
-
-       if (left > 0) {
-               printf("%d extra bytes in the end of netlink message\n", left);
-       }
-}
-
-
-static int hostap_get_we_version(struct wpa_driver_nl80211_data *drv)
-{
-       struct iw_range *range;
-       struct iwreq iwr;
-       int minlen;
-       size_t buflen;
-
-       drv->we_version = 0;
-
-       /*
-        * Use larger buffer than struct iw_range in order to allow the
-        * structure to grow in the future.
-        */
-       buflen = sizeof(struct iw_range) + 500;
-       range = os_zalloc(buflen);
-       if (range == NULL)
-               return -1;
-
-       memset(&iwr, 0, sizeof(iwr));
-       os_strlcpy(iwr.ifr_name, drv->ifname, IFNAMSIZ);
-       iwr.u.data.pointer = (caddr_t) range;
-       iwr.u.data.length = buflen;
-
-       minlen = ((char *) &range->enc_capa) - (char *) range +
-               sizeof(range->enc_capa);
-
-       if (ioctl(drv->ioctl_sock, SIOCGIWRANGE, &iwr) < 0) {
-               perror("ioctl[SIOCGIWRANGE]");
-               free(range);
-               return -1;
-       } else if (iwr.u.data.length >= minlen &&
-                  range->we_version_compiled >= 18) {
-               wpa_printf(MSG_DEBUG, "SIOCGIWRANGE: WE(compiled)=%d "
-                          "WE(source)=%d enc_capa=0x%x",
-                          range->we_version_compiled,
-                          range->we_version_source,
-                          range->enc_capa);
-               drv->we_version = range->we_version_compiled;
-       }
-
-       free(range);
-       return 0;
-}
-
-
-static int i802_wireless_event_init(struct wpa_driver_nl80211_data *drv)
-{
-       int s;
-       struct sockaddr_nl local;
-
-       hostap_get_we_version(drv);
-
-       drv->wext_sock = -1;
-
-       s = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
-       if (s < 0) {
-               perror("socket(PF_NETLINK,SOCK_RAW,NETLINK_ROUTE)");
-               return -1;
-       }
-
-       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, hostapd_wireless_event_receive, drv,
-                                NULL);
-       drv->wext_sock = s;
-
-       return 0;
-}
-
-
-static void i802_wireless_event_deinit(struct wpa_driver_nl80211_data *drv)
-{
-       if (drv->wext_sock < 0)
-               return;
-       eloop_unregister_read_sock(drv->wext_sock);
-       close(drv->wext_sock);
-}
-
-
 static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
 {
        struct wpa_driver_nl80211_data *drv = priv;
@@ -5185,8 +4419,9 @@ static int i802_sta_deauth(void *priv, const u8 *addr, int reason)
        memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
        memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
        mgmt.u.deauth.reason_code = host_to_le16(reason);
-       return i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
-                                     sizeof(mgmt.u.deauth), 0);
+       return wpa_driver_nl80211_send_mlme(drv, (u8 *) &mgmt,
+                                           IEEE80211_HDRLEN +
+                                           sizeof(mgmt.u.deauth));
 }
 
 
@@ -5202,8 +4437,9 @@ static int i802_sta_disassoc(void *priv, const u8 *addr, int reason)
        memcpy(mgmt.sa, drv->hapd->own_addr, ETH_ALEN);
        memcpy(mgmt.bssid, drv->hapd->own_addr, ETH_ALEN);
        mgmt.u.disassoc.reason_code = host_to_le16(reason);
-       return  i802_send_mgmt_frame(drv, &mgmt, IEEE80211_HDRLEN +
-                                      sizeof(mgmt.u.disassoc), 0);
+       return wpa_driver_nl80211_send_mlme(drv, (u8 *) &mgmt,
+                                           IEEE80211_HDRLEN +
+                                           sizeof(mgmt.u.disassoc));
 }
 
 
@@ -5216,7 +4452,8 @@ i802_get_neighbor_bss(void *priv, size_t *num)
 }
 
 
-static void *i802_init_bssid(struct hostapd_data *hapd, const u8 *bssid)
+static void *i802_init(struct hostapd_data *hapd,
+                      struct wpa_init_params *params)
 {
        struct wpa_driver_nl80211_data *drv;
        size_t i;
@@ -5228,23 +4465,19 @@ static void *i802_init_bssid(struct hostapd_data *hapd, const u8 *bssid)
        }
 
        drv->hapd = hapd;
-       memcpy(drv->ifname, hapd->conf->iface, sizeof(drv->ifname));
-       memcpy(drv->bss.ifname, hapd->conf->iface, sizeof(drv->bss.ifname));
+       memcpy(drv->ifname, params->ifname, sizeof(drv->ifname));
+       memcpy(drv->bss.ifname, params->ifname, sizeof(drv->bss.ifname));
        drv->ifindex = if_nametoindex(drv->ifname);
 
        drv->num_if_indices = sizeof(drv->default_if_indices) / sizeof(int);
        drv->if_indices = drv->default_if_indices;
-       for (i = 0; i < hapd->iface->num_bss; i++) {
-               struct hostapd_data *bss = hapd->iface->bss[i];
-               if (bss->conf->bridge)
-                       add_ifidx(drv, if_nametoindex(bss->conf->bridge));
+       for (i = 0; i < params->num_bridge; i++) {
+               if (params->bridge[i])
+                       add_ifidx(drv, if_nametoindex(params->bridge[i]));
        }
-       drv->ht_40mhz_scan = hapd->iconf->secondary_channel != 0;
+       drv->ht_40mhz_scan = params->ht_40mhz_scan;
 
-       if (i802_init_sockets(drv, bssid))
-               goto failed;
-
-       if (i802_wireless_event_init(drv))
+       if (i802_init_sockets(drv, params->bssid))
                goto failed;
 
        return drv;
@@ -5255,19 +4488,11 @@ failed:
 }
 
 
-static void *i802_init(struct hostapd_data *hapd)
-{
-       return i802_init_bssid(hapd, NULL);
-}
-
-
 static void i802_deinit(void *priv)
 {
        struct wpa_driver_nl80211_data *drv = priv;
        struct i802_bss *bss, *prev;
 
-       i802_wireless_event_deinit(drv);
-
        if (drv->last_freq_ht) {
                /* Clear HT flags from the driver */
                struct hostapd_freq_params freq;
@@ -5294,6 +4519,7 @@ static void i802_deinit(void *priv)
                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);
@@ -5344,7 +4570,6 @@ const struct wpa_driver_ops wpa_driver_nl80211_ops = {
 #endif /* CONFIG_AP || HOSTAPD */
 #ifdef HOSTAPD
        .hapd_init = i802_init,
-       .init_bssid = i802_init_bssid,
        .hapd_deinit = i802_deinit,
        .hapd_set_key = i802_set_key,
        .get_seqnum = i802_get_seqnum,
@@ -5355,7 +4580,6 @@ const struct wpa_driver_ops wpa_driver_nl80211_ops = {
        .sta_deauth = i802_sta_deauth,
        .sta_disassoc = i802_sta_disassoc,
        .sta_remove = i802_sta_remove,
-       .send_mgmt_frame = i802_send_mgmt_frame,
        .sta_add = i802_sta_add,
        .get_inact_sec = i802_get_inact_sec,
        .sta_clear_stats = i802_sta_clear_stats,
@@ -5376,7 +4600,6 @@ 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,
-       .hapd_set_country = i802_set_country,
        .get_neighbor_bss = i802_get_neighbor_bss,
 #endif /* HOSTAPD */
 };