Added an option to add (or override) Credential attribute(s) in M8
[wpasupplicant] / hostapd / driver_nl80211.c
index 9357ff2..508a379 100644 (file)
 #include <netlink/genl/ctrl.h>
 #include <netlink/msg.h>
 #include <netlink/attr.h>
-#include <linux/nl80211.h>
+#include "nl80211_copy.h"
 #include <net/if.h>
-#include <linux/if_packet.h>
-#include <linux/if_ether.h>   /* The L2 protocols */
+#include <netpacket/packet.h>
 #include "wireless_copy.h"
 #include <net/if_arp.h>
 
 #include "hostapd.h"
 #include "driver.h"
-#include "ieee802_1x.h"
 #include "eloop.h"
-#include "ieee802_11.h"
-#include "sta_info.h"
 #include "hw_features.h"
 #include "mlme.h"
 #include "radiotap.h"
 #include "radiotap_iter.h"
+#include "ieee802_11_defs.h"
+
+#ifdef CONFIG_LIBNL20
+/* libnl 2.0 compatibility code */
+#define nl_handle_alloc_cb nl_socket_alloc_cb
+#define nl_handle_destroy nl_socket_free
+#endif /* CONFIG_LIBNL20 */
+
+static const u8 rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
 
 enum ieee80211_msg_type {
        ieee80211_msg_normal = 0,
@@ -70,9 +75,16 @@ struct i802_driver_data {
        int dtim_period, beacon_int;
        unsigned int beacon_set:1;
        unsigned int ieee802_1x_active:1;
+
+       int last_freq;
+       int last_freq_ht;
 };
 
 
+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 void add_ifidx(struct i802_driver_data *drv, int ifidx)
 {
        int i;
@@ -287,14 +299,10 @@ static int nl_set_encr(int ifindex, struct i802_driver_data *drv,
                    0, NL80211_CMD_SET_KEY, 0);
        NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, idx);
        NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifindex);
-#ifdef NL80211_MFP_PENDING
        if (strcmp(alg, "IGTK") == 0)
                NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT_MGMT);
        else
                NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
-#else /* NL80211_MFP_PENDING */
-       NLA_PUT_FLAG(msg, NL80211_ATTR_KEY_DEFAULT);
-#endif /* NL80211_MFP_PENDING */
 
        ret = send_and_recv_msgs(drv, msg, NULL, NULL);
        if (ret == -ENOENT)
@@ -317,11 +325,6 @@ static int i802_set_encryption(const char *iface, void *priv, const char *alg,
        if (ret < 0)
                return ret;
 
-       if (strcmp(alg, "IGTK") == 0) {
-               ret = nl_set_encr(drv->monitor_ifidx, drv, alg, addr, idx, key,
-                                 key_len, txkey);
-       }
-
        return ret;
 }
 
@@ -384,7 +387,6 @@ static int i802_get_seqnum(const char *iface, void *priv, const u8 *addr,
 static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
                              int mode)
 {
-#ifdef NL80211_ATTR_BSS_BASIC_RATES
        struct i802_driver_data *drv = priv;
        struct nl_msg *msg;
        u8 rates[NL80211_MAX_SUPP_RATES];
@@ -409,9 +411,6 @@ static int i802_set_rate_sets(void *priv, int *supp_rates, int *basic_rates,
        return send_and_recv_msgs(drv, msg, NULL, NULL);
  nla_put_failure:
        return -ENOBUFS;
-#else /* NL80211_ATTR_BSS_BASIC_RATES */
-       return -1;
-#endif /* NL80211_ATTR_BSS_BASIC_RATES */
 }
 
 
@@ -457,26 +456,70 @@ static int i802_send_frame(void *priv, const void *data, size_t len,
 static int i802_send_mgmt_frame(void *priv, const void *data, size_t len,
                                int flags)
 {
-       return i802_send_frame(priv, data, len, 1, 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, int mode, int freq)
+static int i802_set_freq2(void *priv, struct hostapd_freq_params *freq)
 {
        struct i802_driver_data *drv = priv;
-       struct iwreq iwr;
-
-       memset(&iwr, 0, sizeof(iwr));
-       os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
-       iwr.u.freq.m = freq;
-       iwr.u.freq.e = 6;
+       struct nl_msg *msg;
 
-       if (ioctl(drv->ioctl_sock, SIOCSIWFREQ, &iwr) < 0) {
-               perror("ioctl[SIOCSIWFREQ]");
+       msg = nlmsg_alloc();
+       if (!msg)
                return -1;
+
+       drv->last_freq = freq->freq;
+       drv->last_freq_ht = freq->ht_enabled;
+
+       genlmsg_put(msg, 0, 0, genl_family_get_id(drv->nl80211), 0, 0,
+                   NL80211_CMD_SET_WIPHY, 0);
+
+       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
+       NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq->freq);
+       if (freq->ht_enabled) {
+               switch (freq->sec_channel_offset) {
+               case -1:
+                       NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
+                                   NL80211_CHAN_HT40MINUS);
+                       break;
+               case 1:
+                       NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
+                                   NL80211_CHAN_HT40PLUS);
+                       break;
+               default:
+                       NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
+                                   NL80211_CHAN_HT20);
+                       break;
+               }
        }
 
-       return 0;
+       if (send_and_recv_msgs(drv, msg, NULL, NULL) == 0)
+               return 0;
+ nla_put_failure:
+       return -1;
 }
 
 
@@ -486,7 +529,7 @@ static int i802_set_rts(void *priv, int rts)
        struct iwreq iwr;
 
        memset(&iwr, 0, sizeof(iwr));
-       os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
+       os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
        iwr.u.rts.value = rts;
        iwr.u.rts.fixed = 1;
 
@@ -505,7 +548,7 @@ static int i802_get_rts(void *priv, int *rts)
        struct iwreq iwr;
 
        memset(&iwr, 0, sizeof(iwr));
-       os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
+       os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
 
        if (ioctl(drv->ioctl_sock, SIOCGIWRTS, &iwr) < 0) {
                perror("ioctl[SIOCGIWRTS]");
@@ -524,7 +567,7 @@ static int i802_set_frag(void *priv, int frag)
        struct iwreq iwr;
 
        memset(&iwr, 0, sizeof(iwr));
-       os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
+       os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
        iwr.u.frag.value = frag;
        iwr.u.frag.fixed = 1;
 
@@ -543,7 +586,7 @@ static int i802_get_frag(void *priv, int *frag)
        struct iwreq iwr;
 
        memset(&iwr, 0, sizeof(iwr));
-       os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
+       os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
 
        if (ioctl(drv->ioctl_sock, SIOCGIWFRAG, &iwr) < 0) {
                perror("ioctl[SIOCGIWFRAG]");
@@ -562,18 +605,18 @@ static int i802_set_retry(void *priv, int short_retry, int long_retry)
        struct iwreq iwr;
 
        memset(&iwr, 0, sizeof(iwr));
-       os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
+       os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
 
        iwr.u.retry.value = short_retry;
        iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
-       if (ioctl(drv->ioctl_sock, SIOCSIWFRAG, &iwr) < 0) {
+       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, SIOCSIWFRAG, &iwr) < 0) {
+       if (ioctl(drv->ioctl_sock, SIOCSIWRETRY, &iwr) < 0) {
                perror("ioctl[SIOCSIWRETRY(long)]");
                return -1;
        }
@@ -588,7 +631,7 @@ static int i802_get_retry(void *priv, int *short_retry, int *long_retry)
        struct iwreq iwr;
 
        memset(&iwr, 0, sizeof(iwr));
-       os_strlcpy(iwr.ifr_name, drv->hapd->conf->iface, IFNAMSIZ);
+       os_strlcpy(iwr.ifr_name, drv->iface, IFNAMSIZ);
 
        iwr.u.retry.flags = IW_RETRY_LIMIT | IW_RETRY_MIN;
        if (ioctl(drv->ioctl_sock, SIOCGIWRETRY, &iwr) < 0) {
@@ -789,13 +832,11 @@ static int i802_sta_add2(const char *ifname, void *priv,
                    params->listen_interval);
 
 #ifdef CONFIG_IEEE80211N
-#ifdef NL80211_ATTR_HT_CAPABILITY
        if (params->ht_capabilities) {
                NLA_PUT(msg, NL80211_ATTR_HT_CAPABILITY,
                        params->ht_capabilities->length,
                        &params->ht_capabilities->data);
        }
-#endif /* NL80211_ATTR_HT_CAPABILITY */
 #endif /* CONFIG_IEEE80211N */
 
        ret = send_and_recv_msgs(drv, msg, NULL, NULL);
@@ -864,10 +905,8 @@ static int i802_sta_set_flags(void *priv, const u8 *addr,
        if (total_flags & WLAN_STA_SHORT_PREAMBLE)
                NLA_PUT_FLAG(flags, NL80211_STA_FLAG_SHORT_PREAMBLE);
 
-#ifdef NL80211_MFP_PENDING
        if (total_flags & WLAN_STA_MFP)
                NLA_PUT_FLAG(flags, NL80211_STA_FLAG_MFP);
-#endif /* NL80211_MFP_PENDING */
 
        if (nla_put_nested(msg, NL80211_ATTR_STA_FLAGS, flags))
                goto nla_put_failure;
@@ -881,16 +920,9 @@ static int i802_sta_set_flags(void *priv, const u8 *addr,
 }
 
 
-static int i802_set_regulatory_domain(void *priv, unsigned int rd)
-{
-       return -1;
-}
-
-
 static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
                                    int cw_min, int cw_max, int burst_time)
 {
-#ifdef NL80211_ATTR_WIPHY_TXQ_PARAMS
        struct i802_driver_data *drv = priv;
        struct nl_msg *msg;
        struct nlattr *txq, *params;
@@ -929,9 +961,6 @@ static int i802_set_tx_queue_params(void *priv, int queue, int aifs,
                return 0;
  nla_put_failure:
        return -1;
-#else /* NL80211_ATTR_WIPHY_TXQ_PARAMS */
-       return -1;
-#endif /* NL80211_ATTR_WIPHY_TXQ_PARAMS */
 }
 
 
@@ -974,8 +1003,7 @@ static int nl80211_create_iface(struct i802_driver_data *drv,
 
        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->hapd->conf->iface));
+       NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
        NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, ifname);
        NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, iftype);
 
@@ -1210,7 +1238,6 @@ static int i802_set_dtim_period(const char *iface, void *priv, int value)
 
 static int i802_set_bss(void *priv, int cts, int preamble, int slot)
 {
-#ifdef NL80211_CMD_SET_BSS
        struct i802_driver_data *drv = priv;
        struct nl_msg *msg;
 
@@ -1234,9 +1261,6 @@ static int i802_set_bss(void *priv, int cts, int preamble, int slot)
        return send_and_recv_msgs(drv, msg, NULL, NULL);
  nla_put_failure:
        return -ENOBUFS;
-#else /* NL80211_CMD_SET_BSS */
-       return -1;
-#endif /* NL80211_CMD_SET_BSS */
 }
 
 
@@ -1316,6 +1340,7 @@ static int phy_info_handler(struct nl_msg *msg, void *arg)
                [NL80211_FREQUENCY_ATTR_PASSIVE_SCAN] = { .type = NLA_FLAG },
                [NL80211_FREQUENCY_ATTR_NO_IBSS] = { .type = NLA_FLAG },
                [NL80211_FREQUENCY_ATTR_RADAR] = { .type = NLA_FLAG },
+               [NL80211_FREQUENCY_ATTR_MAX_TX_POWER] = { .type = NLA_U32 },
        };
 
        struct nlattr *tb_rate[NL80211_BITRATE_ATTR_MAX + 1];
@@ -1405,6 +1430,12 @@ static int phy_info_handler(struct nl_msg *msg, void *arg)
                        if (tb_freq[NL80211_FREQUENCY_ATTR_RADAR])
                                mode->channels[idx].flag |=
                                        HOSTAPD_CHAN_RADAR;
+
+                       if (tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER] &&
+                           !tb_freq[NL80211_FREQUENCY_ATTR_DISABLED])
+                               mode->channels[idx].max_tx_power =
+                                       nla_get_u32(tb_freq[NL80211_FREQUENCY_ATTR_MAX_TX_POWER]) / 100;
+
                        idx++;
                }
 
@@ -1444,6 +1475,80 @@ static int phy_info_handler(struct nl_msg *msg, void *arg)
        return NL_SKIP;
 }
 
+static struct hostapd_hw_modes *i802_add_11b(struct hostapd_hw_modes *modes,
+                                            u16 *num_modes)
+{
+       u16 m;
+       struct hostapd_hw_modes *mode11g = NULL, *nmodes, *mode;
+       int i, mode11g_idx = -1;
+
+       /* If only 802.11g mode is included, use it to construct matching
+        * 802.11b mode data. */
+
+       for (m = 0; m < *num_modes; m++) {
+               if (modes[m].mode == HOSTAPD_MODE_IEEE80211B)
+                       return modes; /* 802.11b already included */
+               if (modes[m].mode == HOSTAPD_MODE_IEEE80211G)
+                       mode11g_idx = m;
+       }
+
+       if (mode11g_idx < 0)
+               return modes; /* 2.4 GHz band not supported at all */
+
+       nmodes = os_realloc(modes, (*num_modes + 1) * sizeof(*nmodes));
+       if (nmodes == NULL)
+               return modes; /* Could not add 802.11b mode */
+
+       mode = &nmodes[*num_modes];
+       os_memset(mode, 0, sizeof(*mode));
+       (*num_modes)++;
+       modes = nmodes;
+
+       mode->mode = HOSTAPD_MODE_IEEE80211B;
+
+       mode11g = &modes[mode11g_idx];
+       mode->num_channels = mode11g->num_channels;
+       mode->channels = os_malloc(mode11g->num_channels *
+                                  sizeof(struct hostapd_channel_data));
+       if (mode->channels == NULL) {
+               (*num_modes)--;
+               return modes; /* Could not add 802.11b mode */
+       }
+       os_memcpy(mode->channels, mode11g->channels,
+                 mode11g->num_channels * sizeof(struct hostapd_channel_data));
+
+       mode->num_rates = 0;
+       mode->rates = os_malloc(4 * sizeof(struct hostapd_rate_data));
+       if (mode->rates == NULL) {
+               os_free(mode->channels);
+               (*num_modes)--;
+               return modes; /* Could not add 802.11b mode */
+       }
+
+       for (i = 0; i < mode11g->num_rates; i++) {
+               if (mode11g->rates[i].rate > 110 ||
+                   mode11g->rates[i].flags &
+                   (HOSTAPD_RATE_ERP | HOSTAPD_RATE_OFDM))
+                       continue;
+               mode->rates[mode->num_rates] = mode11g->rates[i];
+               mode->num_rates++;
+               if (mode->num_rates == 4)
+                       break;
+       }
+
+       if (mode->num_rates == 0) {
+               os_free(mode->channels);
+               os_free(mode->rates);
+               (*num_modes)--;
+               return modes; /* No 802.11b rates */
+       }
+
+       wpa_printf(MSG_DEBUG, "nl80211: Added 802.11b mode based on 802.11g "
+                  "information");
+
+       return modes;
+}
+
 static struct hostapd_hw_modes *i802_get_hw_feature_data(void *priv,
                                                         u16 *num_modes,
                                                         u16 *flags)
@@ -1468,7 +1573,7 @@ static struct hostapd_hw_modes *i802_get_hw_feature_data(void *priv,
        NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, if_nametoindex(drv->iface));
 
        if (send_and_recv_msgs(drv, msg, phy_info_handler, &result) == 0)
-               return result.modes;
+               return i802_add_11b(result.modes, num_modes);
  nla_put_failure:
        return NULL;
 }
@@ -1499,23 +1604,27 @@ static int i802_set_sta_vlan(void *priv, const u8 *addr,
 }
 
 
-static void handle_unknown_sta(struct hostapd_data *hapd, u8 *ta)
+static int i802_set_country(void *priv, const char *country)
 {
-       struct sta_info *sta;
+       struct i802_driver_data *drv = priv;
+       struct nl_msg *msg;
+       char alpha2[3];
 
-       sta = ap_get_sta(hapd, ta);
-       if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
-               printf("Data/PS-poll frame from not associated STA "
-                      MACSTR "\n", MAC2STR(ta));
-               if (sta && (sta->flags & WLAN_STA_AUTH))
-                       hostapd_sta_disassoc(
-                               hapd, ta,
-                               WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
-               else
-                       hostapd_sta_deauth(
-                               hapd, ta,
-                               WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
-       }
+       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;
 }
 
 
@@ -1524,7 +1633,6 @@ static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
 {
        struct ieee80211_hdr *hdr;
        u16 fc, type, stype;
-       struct sta_info *sta;
 
        hdr = (struct ieee80211_hdr *) buf;
        fc = le_to_host16(hdr->frame_control);
@@ -1536,7 +1644,7 @@ static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
        case WLAN_FC_TYPE_MGMT:
                wpa_printf(MSG_DEBUG, "MGMT (TX callback) %s",
                           ok ? "ACK" : "fail");
-               ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
+               hostapd_mgmt_tx_cb(hapd, buf, len, stype, ok);
                break;
        case WLAN_FC_TYPE_CTRL:
                wpa_printf(MSG_DEBUG, "CTRL (TX callback) %s",
@@ -1545,16 +1653,7 @@ static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
        case WLAN_FC_TYPE_DATA:
                wpa_printf(MSG_DEBUG, "DATA (TX callback) %s",
                           ok ? "ACK" : "fail");
-               sta = ap_get_sta(hapd, hdr->addr1);
-               if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
-                       wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
-                                  "activity poll", MAC2STR(sta->addr),
-                                  ok ? "ACKed" : "did not ACK");
-                       if (ok)
-                               sta->flags &= ~WLAN_STA_PENDING_POLL;
-               }
-               if (sta)
-                       ieee802_1x_tx_status(hapd, sta, buf, len, ok);
+               hostapd_tx_status(hapd, hdr->addr1, buf, len, ok);
                break;
        default:
                printf("unknown TX callback frame type %d\n", type);
@@ -1563,7 +1662,8 @@ static void handle_tx_callback(struct hostapd_data *hapd, u8 *buf, size_t len,
 }
 
 
-static void handle_frame(struct hostapd_iface *iface, u8 *buf, size_t len,
+static void handle_frame(struct i802_driver_data *drv,
+                        struct hostapd_iface *iface, u8 *buf, size_t len,
                         struct hostapd_frame_info *hfi,
                         enum ieee80211_msg_type msg_type)
 {
@@ -1661,19 +1761,18 @@ static void handle_frame(struct hostapd_iface *iface, u8 *buf, size_t len,
                        wpa_printf(MSG_MSGDUMP, "MGMT");
                if (broadcast_bssid) {
                        for (i = 0; i < iface->num_bss; i++)
-                               ieee802_11_mgmt(iface->bss[i], buf, data_len,
+                               hostapd_mgmt_rx(iface->bss[i], buf, data_len,
                                                stype, hfi);
                } else
-                       ieee802_11_mgmt(hapd, buf, data_len, stype, hfi);
+                       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");
-               handle_unknown_sta(hapd, hdr->addr2);
+               hostapd_rx_from_unknown_sta(drv->hapd, hdr->addr2);
                break;
        case WLAN_FC_TYPE_DATA:
-               wpa_printf(MSG_DEBUG, "DATA");
-               handle_unknown_sta(hapd, hdr->addr2);
+               hostapd_rx_from_unknown_sta(drv->hapd, hdr->addr2);
                break;
        }
 }
@@ -1696,7 +1795,7 @@ static void handle_eapol(int sock, void *eloop_ctx, void *sock_ctx)
        }
 
        if (have_ifidx(drv, lladdr.sll_ifindex))
-               ieee802_1x_receive(hapd, lladdr.sll_addr, buf, len);
+               hostapd_eapol_receive(hapd, lladdr.sll_addr, buf, len);
 }
 
 
@@ -1772,7 +1871,7 @@ static void handle_monitor_read(int sock, void *eloop_ctx, void *sock_ctx)
        else
                msg_type = ieee80211_msg_tx_callback_ack;
 
-       handle_frame(hapd->iface, buf + iter.max_length,
+       handle_frame(drv, hapd->iface, buf + iter.max_length,
                     len - iter.max_length, &hfi, msg_type);
 }
 
@@ -1863,8 +1962,6 @@ static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
        struct ifreq ifr;
        struct sockaddr_ll addr;
 
-       drv->ioctl_sock = -1;
-
        drv->ioctl_sock = socket(PF_INET, SOCK_DGRAM, 0);
        if (drv->ioctl_sock < 0) {
                perror("socket[PF_INET,SOCK_DGRAM]");
@@ -1908,11 +2005,18 @@ static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
                return -1;
        }
 
+#ifdef CONFIG_LIBNL20
+       if (genl_ctrl_alloc_cache(drv->nl_handle, &drv->nl_cache) < 0) {
+               printf("Failed to allocate generic netlink cache.\n");
+               return -1;
+       }
+#else /* CONFIG_LIBNL20 */
        drv->nl_cache = genl_ctrl_alloc_cache(drv->nl_handle);
        if (!drv->nl_cache) {
                printf("Failed to allocate generic netlink cache.\n");
                return -1;
        }
+#endif /* CONFIG_LIBNL20 */
 
        drv->nl80211 = genl_ctrl_search_by_name(drv->nl_cache, "nl80211");
        if (!drv->nl80211) {
@@ -1925,10 +2029,10 @@ static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
                return -1;
 
        if (nl80211_set_master_mode(drv, drv->iface))
-               return -1;
+               goto fail1;
 
        if (hostapd_set_iface_flags(drv, drv->iface, 1))
-               return -1;
+               goto fail1;
 
        memset(&addr, 0, sizeof(addr));
        addr.sll_family = AF_PACKET;
@@ -1939,7 +2043,7 @@ static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
        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)");
-               return -1;
+               goto fail1;
        }
 
        if (eloop_register_read_sock(drv->eapol_sock, handle_eapol, drv, NULL))
@@ -1952,17 +2056,21 @@ static int i802_init_sockets(struct i802_driver_data *drv, const u8 *bssid)
        os_strlcpy(ifr.ifr_name, drv->iface, sizeof(ifr.ifr_name));
        if (ioctl(drv->ioctl_sock, SIOCGIFHWADDR, &ifr) != 0) {
                perror("ioctl(SIOCGIFHWADDR)");
-               return -1;
+               goto fail1;
        }
 
        if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
                printf("Invalid HW-addr family 0x%04x\n",
                       ifr.ifr_hwaddr.sa_family);
-               return -1;
+               goto fail1;
        }
        memcpy(drv->hapd->own_addr, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
 
        return 0;
+
+fail1:
+       nl80211_remove_iface(drv, drv->monitor_ifidx);
+       return -1;
 }
 
 
@@ -2006,7 +2114,7 @@ hostapd_wireless_event_wireless_custom(struct i802_driver_data *drv,
                }
                pos += 5;
                if (hwaddr_aton(pos, addr) == 0) {
-                       ieee80211_michael_mic_failure(drv->hapd, addr, 1);
+                       hostapd_michael_mic_failure(drv->hapd, addr);
                } else {
                        wpa_printf(MSG_DEBUG,
                                   "MLME-MICHAELMICFAILURE.indication "
@@ -2311,6 +2419,14 @@ static void i802_deinit(void *priv)
 {
        struct i802_driver_data *drv = priv;
 
+       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_freq2(priv, &freq);
+       }
+
        i802_del_beacon(drv);
 
        /* remove monitor interface */
@@ -2363,7 +2479,7 @@ const struct wpa_driver_ops wpa_driver_nl80211_ops = {
        .sta_add2 = i802_sta_add2,
        .get_inact_sec = i802_get_inact_sec,
        .sta_clear_stats = i802_sta_clear_stats,
-       .set_freq = i802_set_freq,
+       .set_freq2 = i802_set_freq2,
        .set_rts = i802_set_rts,
        .get_rts = i802_get_rts,
        .set_frag = i802_set_frag,
@@ -2371,7 +2487,6 @@ const struct wpa_driver_ops wpa_driver_nl80211_ops = {
        .set_retry = i802_set_retry,
        .get_retry = i802_get_retry,
        .set_rate_sets = i802_set_rate_sets,
-       .set_regulatory_domain = i802_set_regulatory_domain,
        .set_beacon = i802_set_beacon,
        .set_internal_bridge = i802_set_internal_bridge,
        .set_beacon_int = i802_set_beacon_int,
@@ -2387,4 +2502,5 @@ const struct wpa_driver_ops wpa_driver_nl80211_ops = {
        .if_remove = i802_if_remove,
        .get_hw_feature_data = i802_get_hw_feature_data,
        .set_sta_vlan = i802_set_sta_vlan,
+       .set_country = i802_set_country,
 };