Remove direct driver calls from accounting.c
[libeap.git] / hostapd / ieee802_11.c
index c82ee2f..f3dc81a 100644 (file)
@@ -1,7 +1,6 @@
 /*
  * hostapd / IEEE 802.11 Management
- * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
- * Copyright (c) 2007-2008, Intel Corporation
+ * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
  *
  * 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
 
 #include <net/if.h>
 
+#include "common.h"
 #include "eloop.h"
+#include "crypto/crypto.h"
+#include "common/wpa_ctrl.h"
+#include "radius/radius.h"
+#include "radius/radius_client.h"
 #include "hostapd.h"
 #include "ieee802_11.h"
 #include "beacon.h"
 #include "hw_features.h"
-#include "radius/radius.h"
-#include "radius/radius_client.h"
 #include "ieee802_11_auth.h"
 #include "sta_info.h"
-#include "rc4.h"
 #include "ieee802_1x.h"
 #include "wpa.h"
 #include "wme.h"
@@ -100,153 +101,6 @@ u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
 }
 
 
-u8 * hostapd_eid_ht_capabilities_info(struct hostapd_data *hapd, u8 *eid)
-{
-#ifdef CONFIG_IEEE80211N
-       struct ieee80211_ht_capability *cap;
-       u8 *pos = eid;
-
-       if (!hapd->iconf->ieee80211n)
-               return eid;
-
-       *pos++ = WLAN_EID_HT_CAP;
-       *pos++ = sizeof(*cap);
-
-       cap = (struct ieee80211_ht_capability *) pos;
-       os_memset(cap, 0, sizeof(*cap));
-       SET_2BIT_U8(&cap->mac_ht_params_info,
-                   MAC_HT_PARAM_INFO_MAX_RX_AMPDU_FACTOR_OFFSET,
-                   MAX_RX_AMPDU_FACTOR_64KB);
-
-       cap->capabilities_info = host_to_le16(hapd->iconf->ht_capab);
-
-       cap->supported_mcs_set[0] = 0xff;
-       cap->supported_mcs_set[1] = 0xff;
-
-       pos += sizeof(*cap);
-
-       return pos;
-#else /* CONFIG_IEEE80211N */
-       return eid;
-#endif /* CONFIG_IEEE80211N */
-}
-
-
-u8 * hostapd_eid_ht_operation(struct hostapd_data *hapd, u8 *eid)
-{
-#ifdef CONFIG_IEEE80211N
-       struct ieee80211_ht_operation *oper;
-       u8 *pos = eid;
-
-       if (!hapd->iconf->ieee80211n)
-               return eid;
-
-       *pos++ = WLAN_EID_HT_OPERATION;
-       *pos++ = sizeof(*oper);
-
-       oper = (struct ieee80211_ht_operation *) pos;
-       os_memset(oper, 0, sizeof(*oper));
-
-       oper->control_chan = hapd->iconf->channel;
-       oper->operation_mode = host_to_le16(hapd->iface->ht_op_mode);
-       if (hapd->iconf->secondary_channel == 1)
-               oper->ht_param |= HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE |
-                       HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH;
-       if (hapd->iconf->secondary_channel == -1)
-               oper->ht_param |= HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW |
-                       HT_INFO_HT_PARAM_REC_TRANS_CHNL_WIDTH;
-
-       pos += sizeof(*oper);
-
-       return pos;
-#else /* CONFIG_IEEE80211N */
-       return eid;
-#endif /* CONFIG_IEEE80211N */
-}
-
-
-#ifdef CONFIG_IEEE80211N
-
-/*
-op_mode
-Set to 0 (HT pure) under the followign conditions
-       - all STAs in the BSS are 20/40 MHz HT in 20/40 MHz BSS or
-       - all STAs in the BSS are 20 MHz HT in 20 MHz BSS
-Set to 1 (HT non-member protection) if there may be non-HT STAs
-       in both the primary and the secondary channel
-Set to 2 if only HT STAs are associated in BSS,
-       however and at least one 20 MHz HT STA is associated
-Set to 3 (HT mixed mode) when one or more non-HT STAs are associated
-       (currently non-GF HT station is considered as non-HT STA also)
-*/
-int hostapd_ht_operation_update(struct hostapd_iface *iface)
-{
-       u16 cur_op_mode, new_op_mode;
-       int op_mode_changes = 0;
-
-       if (!iface->conf->ieee80211n || iface->conf->ht_op_mode_fixed)
-               return 0;
-
-       wpa_printf(MSG_DEBUG, "%s current operation mode=0x%X",
-                  __func__, iface->ht_op_mode);
-
-       if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT)
-           && iface->num_sta_ht_no_gf) {
-               iface->ht_op_mode |=
-                       HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
-               op_mode_changes++;
-       } else if ((iface->ht_op_mode &
-                   HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT) &&
-                  iface->num_sta_ht_no_gf == 0) {
-               iface->ht_op_mode &=
-                       ~HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT;
-               op_mode_changes++;
-       }
-
-       if (!(iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
-           (iface->num_sta_no_ht || iface->olbc_ht)) {
-               iface->ht_op_mode |= HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
-               op_mode_changes++;
-       } else if ((iface->ht_op_mode &
-                   HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT) &&
-                  (iface->num_sta_no_ht == 0 && !iface->olbc_ht)) {
-               iface->ht_op_mode &=
-                       ~HT_INFO_OPERATION_MODE_NON_HT_STA_PRESENT;
-               op_mode_changes++;
-       }
-
-       /* Note: currently we switch to the MIXED op mode if HT non-greenfield
-        * station is associated. Probably it's a theoretical case, since
-        * it looks like all known HT STAs support greenfield.
-        */
-       new_op_mode = 0;
-       if (iface->num_sta_no_ht ||
-           (iface->ht_op_mode & HT_INFO_OPERATION_MODE_NON_GF_DEVS_PRESENT))
-               new_op_mode = OP_MODE_MIXED;
-       else if ((iface->conf->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)
-                && iface->num_sta_ht_20mhz)
-               new_op_mode = OP_MODE_20MHZ_HT_STA_ASSOCED;
-       else if (iface->olbc_ht)
-               new_op_mode = OP_MODE_MAY_BE_LEGACY_STAS;
-       else
-               new_op_mode = OP_MODE_PURE;
-
-       cur_op_mode = iface->ht_op_mode & HT_INFO_OPERATION_MODE_OP_MODE_MASK;
-       if (cur_op_mode != new_op_mode) {
-               iface->ht_op_mode &= ~HT_INFO_OPERATION_MODE_OP_MODE_MASK;
-               iface->ht_op_mode |= new_op_mode;
-               op_mode_changes++;
-       }
-
-       wpa_printf(MSG_DEBUG, "%s new operation mode=0x%X changes=%d",
-                  __func__, iface->ht_op_mode, op_mode_changes);
-
-       return op_mode_changes;
-}
-
-#endif /* CONFIG_IEEE80211N */
-
-
 u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
                           int probe)
 {
@@ -342,7 +196,8 @@ void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
  * @addr: Address of the destination STA
  * @reason: Reason code for Deauthentication
  */
-void ieee802_11_send_deauth(struct hostapd_data *hapd, u8 *addr, u16 reason)
+void ieee802_11_send_deauth(struct hostapd_data *hapd, const u8 *addr,
+                           u16 reason)
 {
        struct ieee80211_mgmt mgmt;
 
@@ -356,14 +211,15 @@ void ieee802_11_send_deauth(struct hostapd_data *hapd, u8 *addr, u16 reason)
        os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
        os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
        mgmt.u.deauth.reason_code = host_to_le16(reason);
-       if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
-                                   sizeof(mgmt.u.deauth)) < 0)
+       if (hapd->drv.send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
+                                      sizeof(mgmt.u.deauth)) < 0)
                perror("ieee802_11_send_deauth: send");
 }
 
 
 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
-                          u16 auth_transaction, u8 *challenge, int iswep)
+                          u16 auth_transaction, const u8 *challenge,
+                          int iswep)
 {
        hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
                       HOSTAPD_LEVEL_DEBUG,
@@ -384,8 +240,8 @@ static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
                        r = random();
                        os_memcpy(key, &now, 4);
                        os_memcpy(key + 4, &r, 4);
-                       rc4(sta->challenge, WLAN_AUTH_CHALLENGE_LEN,
-                           key, sizeof(key));
+                       rc4_skip(key, sizeof(key), 0,
+                                sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
                }
                return 0;
        }
@@ -452,7 +308,7 @@ static void send_auth_reply(struct hostapd_data *hapd,
                   " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
                   MAC2STR(dst), auth_alg, auth_transaction,
                   resp, (unsigned long) ies_len);
-       if (hostapd_send_mgmt_frame(hapd, reply, rlen) < 0)
+       if (hapd->drv.send_mgmt_frame(hapd, reply, rlen) < 0)
                perror("send_auth_reply: send");
 
        os_free(buf);
@@ -485,15 +341,15 @@ static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
 #endif /* CONFIG_IEEE80211R */
 
 
-static void handle_auth(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
-                       size_t len)
+static void handle_auth(struct hostapd_data *hapd,
+                       const struct ieee80211_mgmt *mgmt, size_t len)
 {
        u16 auth_alg, auth_transaction, status_code;
        u16 resp = WLAN_STATUS_SUCCESS;
        struct sta_info *sta = NULL;
        int res;
        u16 fc;
-       u8 *challenge = NULL;
+       const u8 *challenge = NULL;
        u32 session_timeout, acct_interim_interval;
        int vlan_id = 0;
        u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
@@ -585,7 +441,7 @@ static void handle_auth(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
 
        if (vlan_id > 0) {
                if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
-                                              sta->vlan_id) == NULL) {
+                                              vlan_id) == NULL) {
                        hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
                                       HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
                                       "%d received from RADIUS server",
@@ -601,8 +457,7 @@ static void handle_auth(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
        sta->flags &= ~WLAN_STA_PREAUTH;
        ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
 
-       if (hapd->conf->radius->acct_interim_interval == 0 &&
-           acct_interim_interval)
+       if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
                sta->acct_interim_interval = acct_interim_interval;
        if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
                ap_sta_session_timeout(hapd, sta, session_timeout);
@@ -698,110 +553,33 @@ static int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
 }
 
 
-static void handle_assoc(struct hostapd_data *hapd,
-                        struct ieee80211_mgmt *mgmt, size_t len, int reassoc)
+static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
+                     const u8 *ssid_ie, size_t ssid_ie_len)
 {
-       u16 capab_info, listen_interval;
-       u16 resp = WLAN_STATUS_SUCCESS;
-       u8 *pos, *wpa_ie;
-       size_t wpa_ie_len;
-       int send_deauth = 0, send_len, left, i;
-       struct sta_info *sta;
-       struct ieee802_11_elems elems;
-       u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
-       struct ieee80211_mgmt *reply;
-
-       if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
-                                     sizeof(mgmt->u.assoc_req))) {
-               printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
-                      "\n", reassoc, (unsigned long) len);
-               return;
-       }
-
-       if (reassoc) {
-               capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
-               listen_interval = le_to_host16(
-                       mgmt->u.reassoc_req.listen_interval);
-               wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
-                          " capab_info=0x%02x listen_interval=%d current_ap="
-                          MACSTR,
-                          MAC2STR(mgmt->sa), capab_info, listen_interval,
-                          MAC2STR(mgmt->u.reassoc_req.current_ap));
-               left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
-               pos = mgmt->u.reassoc_req.variable;
-       } else {
-               capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
-               listen_interval = le_to_host16(
-                       mgmt->u.assoc_req.listen_interval);
-               wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
-                          " capab_info=0x%02x listen_interval=%d",
-                          MAC2STR(mgmt->sa), capab_info, listen_interval);
-               left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
-               pos = mgmt->u.assoc_req.variable;
-       }
-
-       sta = ap_get_sta(hapd, mgmt->sa);
-#ifdef CONFIG_IEEE80211R
-       if (sta && sta->auth_alg == WLAN_AUTH_FT &&
-           (sta->flags & WLAN_STA_AUTH) == 0) {
-               wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
-                          "prior to authentication since it is using "
-                          "over-the-DS FT", MAC2STR(mgmt->sa));
-       } else
-#endif /* CONFIG_IEEE80211R */
-       if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
-               printf("STA " MACSTR " trying to associate before "
-                      "authentication\n", MAC2STR(mgmt->sa));
-               if (sta) {
-                       printf("  sta: addr=" MACSTR " aid=%d flags=0x%04x\n",
-                              MAC2STR(sta->addr), sta->aid, sta->flags);
-               }
-               send_deauth = 1;
-               resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
-               goto fail;
-       }
-
-       if (hapd->tkip_countermeasures) {
-               resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
-               goto fail;
-       }
+       if (ssid_ie == NULL)
+               return WLAN_STATUS_UNSPECIFIED_FAILURE;
 
-       if (listen_interval > hapd->conf->max_listen_interval) {
-               hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
-                              HOSTAPD_LEVEL_DEBUG,
-                              "Too large Listen Interval (%d)",
-                              listen_interval);
-               resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
-               goto fail;
+       if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
+           os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
+               char ssid_txt[33];
+               ieee802_11_print_ssid(ssid_txt, ssid_ie, ssid_ie_len);
+               hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
+                              HOSTAPD_LEVEL_INFO,
+                              "Station tried to associate with unknown SSID "
+                              "'%s'", ssid_txt);
+               return WLAN_STATUS_UNSPECIFIED_FAILURE;
        }
 
-       sta->capability = capab_info;
-       sta->listen_interval = listen_interval;
-
-       /* followed by SSID and Supported rates; and HT capabilities if 802.11n
-        * is used */
-       if (ieee802_11_parse_elems(pos, left, &elems, 1) == ParseFailed ||
-           !elems.ssid) {
-               printf("STA " MACSTR " sent invalid association request\n",
-                      MAC2STR(sta->addr));
-               resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
-               goto fail;
-       }
+       return WLAN_STATUS_SUCCESS;
+}
 
-       if (elems.ssid_len != hapd->conf->ssid.ssid_len ||
-           os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != 0)
-       {
-               char ssid_txt[33];
-               ieee802_11_print_ssid(ssid_txt, elems.ssid, elems.ssid_len);
-               printf("Station " MACSTR " tried to associate with "
-                      "unknown SSID '%s'\n", MAC2STR(sta->addr), ssid_txt);
-               resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
-               goto fail;
-       }
 
+static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
+                    const u8 *wmm_ie, size_t wmm_ie_len)
+{
        sta->flags &= ~WLAN_STA_WMM;
-       if (elems.wmm && hapd->conf->wmm_enabled) {
-               if (hostapd_eid_wmm_valid(hapd, elems.wmm, elems.wmm_len))
+       if (wmm_ie && hapd->conf->wmm_enabled) {
+               if (hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len))
                        hostapd_logger(hapd, sta->addr,
                                       HOSTAPD_MODULE_WPA,
                                       HOSTAPD_LEVEL_DEBUG,
@@ -810,62 +588,83 @@ static void handle_assoc(struct hostapd_data *hapd,
                else
                        sta->flags |= WLAN_STA_WMM;
        }
+       return WLAN_STATUS_SUCCESS;
+}
 
-       if (!elems.supp_rates) {
-               hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
+
+static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
+                          struct ieee802_11_elems *elems)
+{
+       if (!elems->supp_rates) {
+               hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
                               HOSTAPD_LEVEL_DEBUG,
                               "No supported rates element in AssocReq");
-               resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
-               goto fail;
+               return WLAN_STATUS_UNSPECIFIED_FAILURE;
        }
 
-       if (elems.supp_rates_len > sizeof(sta->supported_rates)) {
-               hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
+       if (elems->supp_rates_len > sizeof(sta->supported_rates)) {
+               hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
                               HOSTAPD_LEVEL_DEBUG,
                               "Invalid supported rates element length %d",
-                              elems.supp_rates_len);
-               resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
-               goto fail;
+                              elems->supp_rates_len);
+               return WLAN_STATUS_UNSPECIFIED_FAILURE;
        }
 
        os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
-       os_memcpy(sta->supported_rates, elems.supp_rates,
-                 elems.supp_rates_len);
-       sta->supported_rates_len = elems.supp_rates_len;
+       os_memcpy(sta->supported_rates, elems->supp_rates,
+                 elems->supp_rates_len);
+       sta->supported_rates_len = elems->supp_rates_len;
 
-       if (elems.ext_supp_rates) {
-               if (elems.supp_rates_len + elems.ext_supp_rates_len >
+       if (elems->ext_supp_rates) {
+               if (elems->supp_rates_len + elems->ext_supp_rates_len >
                    sizeof(sta->supported_rates)) {
-                       hostapd_logger(hapd, mgmt->sa,
+                       hostapd_logger(hapd, sta->addr,
                                       HOSTAPD_MODULE_IEEE80211,
                                       HOSTAPD_LEVEL_DEBUG,
                                       "Invalid supported rates element length"
-                                      " %d+%d", elems.supp_rates_len,
-                                      elems.ext_supp_rates_len);
-                       resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
-                       goto fail;
+                                      " %d+%d", elems->supp_rates_len,
+                                      elems->ext_supp_rates_len);
+                       return WLAN_STATUS_UNSPECIFIED_FAILURE;
                }
 
-               os_memcpy(sta->supported_rates + elems.supp_rates_len,
-                         elems.ext_supp_rates, elems.ext_supp_rates_len);
-               sta->supported_rates_len += elems.ext_supp_rates_len;
+               os_memcpy(sta->supported_rates + elems->supp_rates_len,
+                         elems->ext_supp_rates, elems->ext_supp_rates_len);
+               sta->supported_rates_len += elems->ext_supp_rates_len;
+       }
+
+       return WLAN_STATUS_SUCCESS;
+}
+
+
+static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
+                          const u8 *ies, size_t ies_len, int reassoc)
+{
+       struct ieee802_11_elems elems;
+       u16 resp;
+       const u8 *wpa_ie;
+       size_t wpa_ie_len;
+
+       if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
+               hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
+                              HOSTAPD_LEVEL_INFO, "Station sent an invalid "
+                              "association request");
+               return WLAN_STATUS_UNSPECIFIED_FAILURE;
        }
 
+       resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
+       if (resp != WLAN_STATUS_SUCCESS)
+               return resp;
+       resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
+       if (resp != WLAN_STATUS_SUCCESS)
+               return resp;
+       resp = copy_supp_rates(hapd, sta, &elems);
+       if (resp != WLAN_STATUS_SUCCESS)
+               return resp;
 #ifdef CONFIG_IEEE80211N
-       /* save HT capabilities in the sta object */
-       os_memset(&sta->ht_capabilities, 0, sizeof(sta->ht_capabilities));
-       if (elems.ht_capabilities &&
-           elems.ht_capabilities_len >=
-           sizeof(struct ieee80211_ht_capability)) {
-               sta->flags |= WLAN_STA_HT;
-               sta->ht_capabilities.id = WLAN_EID_HT_CAP;
-               sta->ht_capabilities.length =
-                       sizeof(struct ieee80211_ht_capability);
-               os_memcpy(&sta->ht_capabilities.data,
-                         elems.ht_capabilities,
-                         sizeof(struct ieee80211_ht_capability));
-       } else
-               sta->flags &= ~WLAN_STA_HT;
+       resp = copy_sta_ht_capab(sta, elems.ht_capabilities,
+                                elems.ht_capabilities_len);
+       if (resp != WLAN_STATUS_SUCCESS)
+               return resp;
 #endif /* CONFIG_IEEE80211N */
 
        if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
@@ -879,6 +678,7 @@ static void handle_assoc(struct hostapd_data *hapd,
                wpa_ie = NULL;
                wpa_ie_len = 0;
        }
+
 #ifdef CONFIG_WPS
        sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
        if (hapd->conf->wps_state && wpa_ie == NULL) {
@@ -899,10 +699,10 @@ static void handle_assoc(struct hostapd_data *hapd,
        } else
 #endif /* CONFIG_WPS */
        if (hapd->conf->wpa && wpa_ie == NULL) {
-               printf("STA " MACSTR ": No WPA/RSN IE in association "
-                      "request\n", MAC2STR(sta->addr));
-               resp = WLAN_STATUS_INVALID_IE;
-               goto fail;
+               hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
+                              HOSTAPD_LEVEL_INFO,
+                              "No WPA/RSN IE in association request");
+               return WLAN_STATUS_INVALID_IE;
        }
 
        if (hapd->conf->wpa && wpa_ie) {
@@ -913,9 +713,9 @@ static void handle_assoc(struct hostapd_data *hapd,
                        sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
                                                        sta->addr);
                if (sta->wpa_sm == NULL) {
-                       printf("Failed to initialize WPA state machine\n");
-                       resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
-                       goto fail;
+                       wpa_printf(MSG_WARNING, "Failed to initialize WPA "
+                                  "state machine");
+                       return WLAN_STATUS_UNSPECIFIED_FAILURE;
                }
                res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
                                          wpa_ie, wpa_ie_len,
@@ -939,7 +739,7 @@ static void handle_assoc(struct hostapd_data *hapd,
                else if (res != WPA_IE_OK)
                        resp = WLAN_STATUS_INVALID_IE;
                if (resp != WLAN_STATUS_SUCCESS)
-                       goto fail;
+                       return resp;
 #ifdef CONFIG_IEEE80211W
                if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
                    sta->sa_query_count > 0)
@@ -956,8 +756,7 @@ static void handle_assoc(struct hostapd_data *hapd,
                        if (sta->sa_query_count == 0)
                                ap_sta_start_sa_query(hapd, sta);
 
-                       resp = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
-                       goto fail;
+                       return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
                }
 
                if (wpa_auth_uses_mfp(sta->wpa_sm))
@@ -973,28 +772,207 @@ static void handle_assoc(struct hostapd_data *hapd,
                                           "to use association (not "
                                           "re-association) with FT auth_alg",
                                           MAC2STR(sta->addr));
-                               resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
-                               goto fail;
+                               return WLAN_STATUS_UNSPECIFIED_FAILURE;
                        }
 
-                       resp = wpa_ft_validate_reassoc(sta->wpa_sm, pos, left);
+                       resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
+                                                      ies_len);
                        if (resp != WLAN_STATUS_SUCCESS)
-                               goto fail;
+                               return resp;
                }
 #endif /* CONFIG_IEEE80211R */
+
 #ifdef CONFIG_IEEE80211N
                if ((sta->flags & WLAN_STA_HT) &&
                    wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
-                       wpa_printf(MSG_DEBUG, "HT: " MACSTR " tried to "
-                                  "use TKIP with HT association",
-                                  MAC2STR(sta->addr));
-                       resp = WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
-                       goto fail;
+                       hostapd_logger(hapd, sta->addr,
+                                      HOSTAPD_MODULE_IEEE80211,
+                                      HOSTAPD_LEVEL_INFO,
+                                      "Station tried to use TKIP with HT "
+                                      "association");
+                       return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
                }
 #endif /* CONFIG_IEEE80211N */
        } else
                wpa_auth_sta_no_wpa(sta->wpa_sm);
 
+       return WLAN_STATUS_SUCCESS;
+}
+
+
+static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
+                       u16 reason_code)
+{
+       int send_len;
+       struct ieee80211_mgmt reply;
+
+       os_memset(&reply, 0, sizeof(reply));
+       reply.frame_control =
+               IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
+       os_memcpy(reply.da, addr, ETH_ALEN);
+       os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
+       os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
+
+       send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
+       reply.u.deauth.reason_code = host_to_le16(reason_code);
+
+       if (hapd->drv.send_mgmt_frame(hapd, &reply, send_len) < 0)
+               wpa_printf(MSG_INFO, "Failed to send deauth: %s",
+                          strerror(errno));
+}
+
+
+static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
+                           u16 status_code, int reassoc, const u8 *ies,
+                           size_t ies_len)
+{
+       int send_len;
+       u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
+       struct ieee80211_mgmt *reply;
+       u8 *p;
+
+       os_memset(buf, 0, sizeof(buf));
+       reply = (struct ieee80211_mgmt *) buf;
+       reply->frame_control =
+               IEEE80211_FC(WLAN_FC_TYPE_MGMT,
+                            (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
+                             WLAN_FC_STYPE_ASSOC_RESP));
+       os_memcpy(reply->da, sta->addr, ETH_ALEN);
+       os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
+       os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
+
+       send_len = IEEE80211_HDRLEN;
+       send_len += sizeof(reply->u.assoc_resp);
+       reply->u.assoc_resp.capab_info =
+               host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
+       reply->u.assoc_resp.status_code = host_to_le16(status_code);
+       reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
+                                              | BIT(14) | BIT(15));
+       /* Supported rates */
+       p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
+       /* Extended supported rates */
+       p = hostapd_eid_ext_supp_rates(hapd, p);
+       if (sta->flags & WLAN_STA_WMM)
+               p = hostapd_eid_wmm(hapd, p);
+
+#ifdef CONFIG_IEEE80211N
+       p = hostapd_eid_ht_capabilities(hapd, p);
+       p = hostapd_eid_ht_operation(hapd, p);
+#endif /* CONFIG_IEEE80211N */
+
+#ifdef CONFIG_IEEE80211R
+       if (status_code == WLAN_STATUS_SUCCESS) {
+               /* IEEE 802.11r: Mobility Domain Information, Fast BSS
+                * Transition Information, RSN, [RIC Response] */
+               p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
+                                               buf + sizeof(buf) - p,
+                                               sta->auth_alg, ies, ies_len);
+       }
+#endif /* CONFIG_IEEE80211R */
+
+#ifdef CONFIG_IEEE80211W
+       if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
+               p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
+#endif /* CONFIG_IEEE80211W */
+
+       send_len += p - reply->u.assoc_resp.variable;
+
+       if (hapd->drv.send_mgmt_frame(hapd, reply, send_len) < 0)
+               wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
+                          strerror(errno));
+}
+
+
+static void handle_assoc(struct hostapd_data *hapd,
+                        const struct ieee80211_mgmt *mgmt, size_t len,
+                        int reassoc)
+{
+       u16 capab_info, listen_interval;
+       u16 resp = WLAN_STATUS_SUCCESS;
+       const u8 *pos;
+       int left, i;
+       struct sta_info *sta;
+
+       if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
+                                     sizeof(mgmt->u.assoc_req))) {
+               printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
+                      "\n", reassoc, (unsigned long) len);
+               return;
+       }
+
+       if (reassoc) {
+               capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
+               listen_interval = le_to_host16(
+                       mgmt->u.reassoc_req.listen_interval);
+               wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
+                          " capab_info=0x%02x listen_interval=%d current_ap="
+                          MACSTR,
+                          MAC2STR(mgmt->sa), capab_info, listen_interval,
+                          MAC2STR(mgmt->u.reassoc_req.current_ap));
+               left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
+               pos = mgmt->u.reassoc_req.variable;
+       } else {
+               capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
+               listen_interval = le_to_host16(
+                       mgmt->u.assoc_req.listen_interval);
+               wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
+                          " capab_info=0x%02x listen_interval=%d",
+                          MAC2STR(mgmt->sa), capab_info, listen_interval);
+               left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
+               pos = mgmt->u.assoc_req.variable;
+       }
+
+       sta = ap_get_sta(hapd, mgmt->sa);
+#ifdef CONFIG_IEEE80211R
+       if (sta && sta->auth_alg == WLAN_AUTH_FT &&
+           (sta->flags & WLAN_STA_AUTH) == 0) {
+               wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
+                          "prior to authentication since it is using "
+                          "over-the-DS FT", MAC2STR(mgmt->sa));
+       } else
+#endif /* CONFIG_IEEE80211R */
+       if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
+               hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
+                              HOSTAPD_LEVEL_INFO, "Station tried to "
+                              "associate before authentication "
+                              "(aid=%d flags=0x%x)",
+                              sta ? sta->aid : -1,
+                              sta ? sta->flags : 0);
+               send_deauth(hapd, mgmt->sa,
+                           WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
+               return;
+       }
+
+       if (hapd->tkip_countermeasures) {
+               resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
+               goto fail;
+       }
+
+       if (listen_interval > hapd->conf->max_listen_interval) {
+               hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
+                              HOSTAPD_LEVEL_DEBUG,
+                              "Too large Listen Interval (%d)",
+                              listen_interval);
+               resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
+               goto fail;
+       }
+
+       /* followed by SSID and Supported rates; and HT capabilities if 802.11n
+        * is used */
+       resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
+       if (resp != WLAN_STATUS_SUCCESS)
+               goto fail;
+
+       if (hostapd_get_aid(hapd, sta) < 0) {
+               hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
+                              HOSTAPD_LEVEL_INFO, "No room for more AIDs");
+               resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
+               goto fail;
+       }
+
+       sta->capability = capab_info;
+       sta->listen_interval = listen_interval;
+
        if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
                sta->flags |= WLAN_STA_NONERP;
        for (i = 0; i < sta->supported_rates_len; i++) {
@@ -1035,54 +1013,9 @@ static void handle_assoc(struct hostapd_data *hapd,
        }
 
 #ifdef CONFIG_IEEE80211N
-       if (sta->flags & WLAN_STA_HT) {
-               u16 ht_capab = le_to_host16(
-                       sta->ht_capabilities.data.capabilities_info);
-               wpa_printf(MSG_DEBUG, "HT: STA " MACSTR " HT Capabilities "
-                          "Info: 0x%04x", MAC2STR(sta->addr), ht_capab);
-               if ((ht_capab & HT_CAP_INFO_GREEN_FIELD) == 0) {
-                       if (!sta->no_ht_gf_set) {
-                               sta->no_ht_gf_set = 1;
-                               hapd->iface->num_sta_ht_no_gf++;
-                       }
-                       wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no "
-                                  "greenfield, num of non-gf stations %d",
-                                  __func__, MAC2STR(sta->addr),
-                                  hapd->iface->num_sta_ht_no_gf);
-               }
-               if ((ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
-                       if (!sta->ht_20mhz_set) {
-                               sta->ht_20mhz_set = 1;
-                               hapd->iface->num_sta_ht_20mhz++;
-                       }
-                       wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - 20 MHz HT, "
-                                  "num of 20MHz HT STAs %d",
-                                  __func__, MAC2STR(sta->addr),
-                                  hapd->iface->num_sta_ht_20mhz);
-               }
-       } else {
-               if (!sta->no_ht_set) {
-                       sta->no_ht_set = 1;
-                       hapd->iface->num_sta_no_ht++;
-               }
-               if (hapd->iconf->ieee80211n) {
-                       wpa_printf(MSG_DEBUG, "%s STA " MACSTR
-                                  " - no HT, num of non-HT stations %d",
-                                  __func__, MAC2STR(sta->addr),
-                                  hapd->iface->num_sta_no_ht);
-               }
-       }
-
-       if (hostapd_ht_operation_update(hapd->iface) > 0)
-               ieee802_11_set_beacons(hapd->iface);
+       update_ht_state(hapd, sta);
 #endif /* CONFIG_IEEE80211N */
 
-       if (hostapd_get_aid(hapd, sta) < 0) {
-               resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
-               wpa_printf(MSG_ERROR, "  no room for more AIDs");
-               goto fail;
-       }
-
        hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
                       HOSTAPD_LEVEL_DEBUG,
                       "association OK (aid %d)", sta->aid);
@@ -1118,65 +1051,12 @@ static void handle_assoc(struct hostapd_data *hapd,
        sta->timeout_next = STA_NULLFUNC;
 
  fail:
-       os_memset(buf, 0, sizeof(buf));
-       reply = (struct ieee80211_mgmt *) buf;
-       reply->frame_control =
-               IEEE80211_FC(WLAN_FC_TYPE_MGMT,
-                            (send_deauth ? WLAN_FC_STYPE_DEAUTH :
-                             (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
-                              WLAN_FC_STYPE_ASSOC_RESP)));
-       os_memcpy(reply->da, mgmt->sa, ETH_ALEN);
-       os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
-       os_memcpy(reply->bssid, mgmt->bssid, ETH_ALEN);
-
-       send_len = IEEE80211_HDRLEN;
-       if (send_deauth) {
-               send_len += sizeof(reply->u.deauth);
-               reply->u.deauth.reason_code = host_to_le16(resp);
-       } else {
-               u8 *p;
-               send_len += sizeof(reply->u.assoc_resp);
-               reply->u.assoc_resp.capab_info =
-                       host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
-               reply->u.assoc_resp.status_code = host_to_le16(resp);
-               reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
-                                                      | BIT(14) | BIT(15));
-               /* Supported rates */
-               p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
-               /* Extended supported rates */
-               p = hostapd_eid_ext_supp_rates(hapd, p);
-               if (sta->flags & WLAN_STA_WMM)
-                       p = hostapd_eid_wmm(hapd, p);
-
-               p = hostapd_eid_ht_capabilities_info(hapd, p);
-               p = hostapd_eid_ht_operation(hapd, p);
-
-#ifdef CONFIG_IEEE80211R
-               if (resp == WLAN_STATUS_SUCCESS) {
-                       /* IEEE 802.11r: Mobility Domain Information, Fast BSS
-                        * Transition Information, RSN, [RIC Response] */
-                       p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
-                                                       buf + sizeof(buf) - p,
-                                                       sta->auth_alg,
-                                                       pos, left);
-               }
-#endif /* CONFIG_IEEE80211R */
-
-#ifdef CONFIG_IEEE80211W
-               if (resp == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
-                       p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
-#endif /* CONFIG_IEEE80211W */
-
-               send_len += p - reply->u.assoc_resp.variable;
-       }
-
-       if (hostapd_send_mgmt_frame(hapd, reply, send_len) < 0)
-               perror("handle_assoc: send");
+       send_assoc_resp(hapd, sta, resp, reassoc, pos, left);
 }
 
 
 static void handle_disassoc(struct hostapd_data *hapd,
-                           struct ieee80211_mgmt *mgmt, size_t len)
+                           const struct ieee80211_mgmt *mgmt, size_t len)
 {
        struct sta_info *sta;
 
@@ -1198,6 +1078,8 @@ static void handle_disassoc(struct hostapd_data *hapd,
        }
 
        sta->flags &= ~WLAN_STA_ASSOC;
+       wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
+               MAC2STR(sta->addr));
        wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
        hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
                       HOSTAPD_LEVEL_INFO, "disassociated");
@@ -1223,7 +1105,7 @@ static void handle_disassoc(struct hostapd_data *hapd,
 
 
 static void handle_deauth(struct hostapd_data *hapd,
-                         struct ieee80211_mgmt *mgmt, size_t len)
+                         const struct ieee80211_mgmt *mgmt, size_t len)
 {
        struct sta_info *sta;
 
@@ -1246,6 +1128,8 @@ static void handle_deauth(struct hostapd_data *hapd,
        }
 
        sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
+       wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
+               MAC2STR(sta->addr));
        wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
        hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
                       HOSTAPD_LEVEL_DEBUG, "deauthenticated");
@@ -1258,7 +1142,7 @@ static void handle_deauth(struct hostapd_data *hapd,
 
 
 static void handle_beacon(struct hostapd_data *hapd,
-                         struct ieee80211_mgmt *mgmt, size_t len,
+                         const struct ieee80211_mgmt *mgmt, size_t len,
                          struct hostapd_frame_info *fi)
 {
        struct ieee802_11_elems elems;
@@ -1303,16 +1187,17 @@ void ieee802_11_send_sa_query_req(struct hostapd_data *hapd,
        os_memcpy(mgmt.u.action.u.sa_query_req.trans_id, trans_id,
                  WLAN_SA_QUERY_TR_ID_LEN);
        end = mgmt.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
-       if (hostapd_send_mgmt_frame(hapd, &mgmt, end - (u8 *) &mgmt) < 0)
+       if (hapd->drv.send_mgmt_frame(hapd, &mgmt, end - (u8 *) &mgmt) < 0)
                perror("ieee802_11_send_sa_query_req: send");
 }
 
 
 static void hostapd_sa_query_action(struct hostapd_data *hapd,
-                                   struct ieee80211_mgmt *mgmt, size_t len)
+                                   const struct ieee80211_mgmt *mgmt,
+                                   size_t len)
 {
        struct sta_info *sta;
-       u8 *end;
+       const u8 *end;
        int i;
 
        end = mgmt->u.action.u.sa_query_resp.trans_id +
@@ -1363,12 +1248,21 @@ static void hostapd_sa_query_action(struct hostapd_data *hapd,
                       "Reply to pending SA Query received");
        ap_sta_stop_sa_query(hapd, sta);
 }
+
+
+static int robust_action_frame(u8 category)
+{
+       return category != WLAN_ACTION_PUBLIC &&
+               category != WLAN_ACTION_HT;
+}
 #endif /* CONFIG_IEEE80211W */
 
 
 static void handle_action(struct hostapd_data *hapd,
-                         struct ieee80211_mgmt *mgmt, size_t len)
+                         const struct ieee80211_mgmt *mgmt, size_t len)
 {
+       struct sta_info *sta;
+
        if (len < IEEE80211_HDRLEN + 1) {
                hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
                               HOSTAPD_LEVEL_DEBUG,
@@ -1377,13 +1271,23 @@ static void handle_action(struct hostapd_data *hapd,
                return;
        }
 
+       sta = ap_get_sta(hapd, mgmt->sa);
+#ifdef CONFIG_IEEE80211W
+       if (sta && (sta->flags & WLAN_STA_MFP) &&
+           !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP) &&
+             robust_action_frame(mgmt->u.action.category))) {
+               hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
+                              HOSTAPD_LEVEL_DEBUG,
+                              "Dropped unprotected Robust Action frame from "
+                              "an MFP STA");
+               return;
+       }
+#endif /* CONFIG_IEEE80211W */
+
        switch (mgmt->u.action.category) {
 #ifdef CONFIG_IEEE80211R
        case WLAN_ACTION_FT:
        {
-               struct sta_info *sta;
-
-               sta = ap_get_sta(hapd, mgmt->sa);
                if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
                        wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
                                   "frame from unassociated STA " MACSTR,
@@ -1415,6 +1319,8 @@ static void handle_action(struct hostapd_data *hapd,
                       mgmt->u.action.category);
        if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
            !(mgmt->sa[0] & 0x01)) {
+               struct ieee80211_mgmt *resp;
+
                /*
                 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
                 * Return the Action frame to the source without change
@@ -1422,12 +1328,17 @@ static void handle_action(struct hostapd_data *hapd,
                 */
                wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
                           "frame back to sender");
-               os_memcpy(mgmt->da, mgmt->sa, ETH_ALEN);
-               os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
-               os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
-               mgmt->u.action.category |= 0x80;
+               resp = os_malloc(len);
+               if (resp == NULL)
+                       return;
+               os_memcpy(resp, mgmt, len);
+               os_memcpy(resp->da, resp->sa, ETH_ALEN);
+               os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
+               os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
+               resp->u.action.category |= 0x80;
 
-               hostapd_send_mgmt_frame(hapd, mgmt, len);
+               hapd->drv.send_mgmt_frame(hapd, resp, len);
+               os_free(resp);
        }
 }
 
@@ -1438,7 +1349,6 @@ static void handle_action(struct hostapd_data *hapd,
  * sent to)
  * @buf: management frame data (starting from IEEE 802.11 header)
  * @len: length of frame data in octets
- * @stype: management frame subtype from frame control field
  * @fi: meta data about received frame (signal level, etc.)
  *
  * Process all incoming IEEE 802.11 management frames. This will be called for
@@ -1446,11 +1356,16 @@ static void handle_action(struct hostapd_data *hapd,
  * addition, it can be called to re-inserted pending frames (e.g., when using
  * external RADIUS server as an MAC ACL).
  */
-void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
+void ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
                     struct hostapd_frame_info *fi)
 {
-       struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
+       struct ieee80211_mgmt *mgmt;
        int broadcast;
+       u16 fc, stype;
+
+       mgmt = (struct ieee80211_mgmt *) buf;
+       fc = le_to_host16(mgmt->frame_control);
+       stype = WLAN_FC_GET_STYPE(fc);
 
        if (stype == WLAN_FC_STYPE_BEACON) {
                handle_beacon(hapd, mgmt, len, fi);
@@ -1517,7 +1432,7 @@ void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
 
 
 static void handle_auth_cb(struct hostapd_data *hapd,
-                          struct ieee80211_mgmt *mgmt,
+                          const struct ieee80211_mgmt *mgmt,
                           size_t len, int ok)
 {
        u16 auth_alg, auth_transaction, status_code;
@@ -1557,45 +1472,15 @@ static void handle_auth_cb(struct hostapd_data *hapd,
 }
 
 
-#ifdef CONFIG_IEEE80211N
-static void
-hostapd_get_ht_capab(struct hostapd_data *hapd,
-                    struct ht_cap_ie *ht_cap_ie,
-                    struct ht_cap_ie *neg_ht_cap_ie)
-{
-
-       os_memcpy(neg_ht_cap_ie, ht_cap_ie, sizeof(struct ht_cap_ie));
-       neg_ht_cap_ie->data.capabilities_info =
-               ht_cap_ie->data.capabilities_info & hapd->iconf->ht_capab;
-
-       neg_ht_cap_ie->data.capabilities_info &= ~HT_CAP_INFO_SMPS_DISABLED;
-       if ((ht_cap_ie->data.capabilities_info & HT_CAP_INFO_SMPS_DISABLED) ==
-           (hapd->iconf->ht_capab & HT_CAP_INFO_SMPS_DISABLED))
-               neg_ht_cap_ie->data.capabilities_info |=
-                       hapd->iconf->ht_capab & HT_CAP_INFO_SMPS_DISABLED;
-       else
-               neg_ht_cap_ie->data.capabilities_info |=
-                       HT_CAP_INFO_SMPS_DISABLED;
-
-       /* FIXME: Rx STBC needs to be handled specially */
-       neg_ht_cap_ie->data.capabilities_info &= ~HT_CAP_INFO_RX_STBC_MASK;
-       neg_ht_cap_ie->data.capabilities_info |=
-               hapd->iconf->ht_capab & HT_CAP_INFO_RX_STBC_MASK;
-}
-#endif /* CONFIG_IEEE80211N */
-
-
 static void handle_assoc_cb(struct hostapd_data *hapd,
-                           struct ieee80211_mgmt *mgmt,
+                           const struct ieee80211_mgmt *mgmt,
                            size_t len, int reassoc, int ok)
 {
        u16 status;
        struct sta_info *sta;
        int new_assoc = 1;
-#ifdef CONFIG_IEEE80211N
-       struct ht_cap_ie ht_cap;
-#endif /* CONFIG_IEEE80211N */
-       struct ht_cap_ie *ht_cap_ptr = NULL;
+       struct ieee80211_ht_capabilities ht_cap;
+       int set_flags, total_flags, flags_and, flags_or;
 
        if (!ok) {
                hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
@@ -1641,6 +1526,8 @@ static void handle_assoc_cb(struct hostapd_data *hapd,
        if (!hapd->conf->ieee802_1x && !hapd->conf->wpa) {
                /* Open or static WEP; no separate authorization */
                sta->flags |= WLAN_STA_AUTHORIZED;
+               wpa_msg(hapd->msg_ctx, MSG_INFO,
+                       AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
        }
 
        if (reassoc)
@@ -1648,21 +1535,26 @@ static void handle_assoc_cb(struct hostapd_data *hapd,
        else
                mlme_associate_indication(hapd, sta);
 
-#ifdef CONFIG_IEEE80211N
-       if (sta->flags & WLAN_STA_HT) {
-               ht_cap_ptr = &ht_cap;
-               hostapd_get_ht_capab(hapd, &sta->ht_capabilities, ht_cap_ptr);
-       }
-#endif /* CONFIG_IEEE80211N */
-
 #ifdef CONFIG_IEEE80211W
        sta->sa_query_timed_out = 0;
 #endif /* CONFIG_IEEE80211W */
 
+       /*
+        * Remove the STA entry in order to make sure the STA PS state gets
+        * cleared and configuration gets updated in case of reassociation back
+        * to the same AP.
+        */
+       hostapd_sta_remove(hapd, sta->addr);
+
+#ifdef CONFIG_IEEE80211N
+       if (sta->flags & WLAN_STA_HT)
+               hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
+#endif /* CONFIG_IEEE80211N */
+
        if (hostapd_sta_add(hapd->conf->iface, hapd, sta->addr, sta->aid,
                            sta->capability, sta->supported_rates,
-                           sta->supported_rates_len, 0, sta->listen_interval,
-                           ht_cap_ptr))
+                           sta->supported_rates_len, sta->listen_interval,
+                           sta->flags & WLAN_STA_HT ? &ht_cap : NULL))
        {
                hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
                               HOSTAPD_LEVEL_NOTICE,
@@ -1680,13 +1572,16 @@ static void handle_assoc_cb(struct hostapd_data *hapd,
                /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
                ap_sta_bind_vlan(hapd, sta, 0);
        }
-       if (sta->flags & WLAN_STA_SHORT_PREAMBLE) {
-               hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
-                                     WLAN_STA_SHORT_PREAMBLE, ~0);
-       } else {
-               hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
-                                     0, ~WLAN_STA_SHORT_PREAMBLE);
-       }
+
+       total_flags = hostapd_sta_flags_to_drv(sta->flags);
+       set_flags = WPA_STA_SHORT_PREAMBLE | WPA_STA_WMM | WPA_STA_MFP;
+       if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
+           sta->flags & WLAN_STA_AUTHORIZED)
+               set_flags |= WPA_STA_AUTHORIZED;
+       flags_or = total_flags & set_flags;
+       flags_and = total_flags | ~set_flags;
+       hostapd_sta_set_flags(hapd, sta->addr, total_flags,
+                             flags_or, flags_and);
 
        if (sta->auth_alg == WLAN_AUTH_FT)
                wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
@@ -1714,10 +1609,11 @@ static void handle_assoc_cb(struct hostapd_data *hapd,
  * @stype: management frame subtype from frame control field
  * @ok: Whether the frame was ACK'ed
  */
-void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
+void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
                        u16 stype, int ok)
 {
-       struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
+       const struct ieee80211_mgmt *mgmt;
+       mgmt = (const struct ieee80211_mgmt *) buf;
 
        switch (stype) {
        case WLAN_FC_STYPE_AUTH:
@@ -1762,4 +1658,65 @@ int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
        return 0;
 }
 
+
+void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
+                      const u8 *buf, size_t len, int ack)
+{
+       struct sta_info *sta;
+       struct hostapd_iface *iface = hapd->iface;
+
+       sta = ap_get_sta(hapd, addr);
+       if (sta == NULL && iface->num_bss > 1) {
+               size_t j;
+               for (j = 0; j < iface->num_bss; j++) {
+                       hapd = iface->bss[j];
+                       sta = ap_get_sta(hapd, addr);
+                       if (sta)
+                               break;
+               }
+       }
+       if (sta == NULL)
+               return;
+       if (sta->flags & WLAN_STA_PENDING_POLL) {
+               wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
+                          "activity poll", MAC2STR(sta->addr),
+                          ack ? "ACKed" : "did not ACK");
+               if (ack)
+                       sta->flags &= ~WLAN_STA_PENDING_POLL;
+       }
+
+       ieee802_1x_tx_status(hapd, sta, buf, len, ack);
+}
+
+
+void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
+                               int wds)
+{
+       struct sta_info *sta;
+
+       sta = ap_get_sta(hapd, src);
+       if (sta && (sta->flags & WLAN_STA_ASSOC)) {
+               if (wds && !(sta->flags & WLAN_STA_WDS)) {
+                       wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
+                                  "STA " MACSTR " (aid %u)",
+                                  MAC2STR(sta->addr), sta->aid);
+                       sta->flags |= WLAN_STA_WDS;
+                       hostapd_set_wds_sta(hapd, sta->addr, sta->aid, 1);
+               }
+               return;
+       }
+
+       wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
+                  MACSTR, MAC2STR(src));
+       if (sta && (sta->flags & WLAN_STA_AUTH))
+               hostapd_sta_disassoc(
+                       hapd, src,
+                       WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
+       else
+               hostapd_sta_deauth(
+                       hapd, src,
+                       WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
+}
+
+
 #endif /* CONFIG_NATIVE_WINDOWS */