Fix EVENT_FT_RRB_RX processing
[libeap.git] / hostapd / ieee802_11.c
index fe2d88f..63f1429 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"
 #include "ap_list.h"
 #include "accounting.h"
-#include "driver.h"
+#include "driver_i.h"
 #include "mlme.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)
 {
@@ -298,12 +152,21 @@ static u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
                                            struct sta_info *sta, u8 *eid)
 {
        u8 *pos = eid;
-       u32 timeout;
-
-       *pos++ = WLAN_EID_ASSOC_COMEBACK_TIME;
-       *pos++ = 4;
-       timeout = (hapd->conf->assoc_ping_attempts - sta->ping_count + 1) *
-               hapd->conf->assoc_ping_timeout;
+       u32 timeout, tu;
+       struct os_time now, passed;
+
+       *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
+       *pos++ = 5;
+       *pos++ = WLAN_TIMEOUT_ASSOC_COMEBACK;
+       os_get_time(&now);
+       os_time_sub(&now, &sta->sa_query_start, &passed);
+       tu = (passed.sec * 1000000 + passed.usec) / 1024;
+       if (hapd->conf->assoc_sa_query_max_timeout > tu)
+               timeout = hapd->conf->assoc_sa_query_max_timeout - tu;
+       else
+               timeout = 0;
+       if (timeout < hapd->conf->assoc_sa_query_max_timeout)
+               timeout++; /* add some extra time for local timers */
        WPA_PUT_LE32(pos, timeout);
        pos += 4;
 
@@ -327,6 +190,12 @@ void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
 }
 
 
+/**
+ * ieee802_11_send_deauth - Send Deauthentication frame
+ * @hapd: hostapd BSS data
+ * @addr: Address of the destination STA
+ * @reason: Reason code for Deauthentication
+ */
 void ieee802_11_send_deauth(struct hostapd_data *hapd, u8 *addr, u16 reason)
 {
        struct ieee80211_mgmt mgmt;
@@ -342,7 +211,7 @@ void ieee802_11_send_deauth(struct hostapd_data *hapd, u8 *addr, u16 reason)
        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) < 0)
+                                   sizeof(mgmt.u.deauth)) < 0)
                perror("ieee802_11_send_deauth: send");
 }
 
@@ -369,8 +238,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;
        }
@@ -437,7 +306,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) < 0)
+       if (hostapd_send_mgmt_frame(hapd, reply, rlen) < 0)
                perror("send_auth_reply: send");
 
        os_free(buf);
@@ -570,7 +439,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",
@@ -586,8 +455,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);
@@ -650,174 +518,151 @@ static void handle_auth(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
 }
 
 
-static void handle_assoc(struct hostapd_data *hapd,
-                        struct ieee80211_mgmt *mgmt, size_t len, int reassoc)
+static int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
 {
-       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) + 512];
-       struct ieee80211_mgmt *reply;
+       int i, j = 32, aid;
 
-       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;
+       /* get a unique AID */
+       if (sta->aid > 0) {
+               wpa_printf(MSG_DEBUG, "  old AID %d", sta->aid);
+               return 0;
        }
 
-       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);
+       for (i = 0; i < AID_WORDS; i++) {
+               if (hapd->sta_aid[i] == (u32) -1)
+                       continue;
+               for (j = 0; j < 32; j++) {
+                       if (!(hapd->sta_aid[i] & BIT(j)))
+                               break;
                }
-               send_deauth = 1;
-               resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
-               goto fail;
-       }
-
-       if (hapd->tkip_countermeasures) {
-               resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
-               goto fail;
+               if (j < 32)
+                       break;
        }
+       if (j == 32)
+               return -1;
+       aid = i * 32 + j + 1;
+       if (aid > 2007)
+               return -1;
 
-       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;
-       }
+       sta->aid = aid;
+       hapd->sta_aid[i] |= BIT(j);
+       wpa_printf(MSG_DEBUG, "  new AID %d", sta->aid);
+       return 0;
+}
 
-       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;
-       }
+static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
+                     const u8 *ssid_ie, size_t ssid_ie_len)
+{
+       if (ssid_ie == NULL)
+               return WLAN_STATUS_UNSPECIFIED_FAILURE;
 
-       if (elems.ssid_len != hapd->conf->ssid.ssid_len ||
-           os_memcmp(elems.ssid, hapd->conf->ssid.ssid, elems.ssid_len) != 0)
-       {
+       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, 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;
+               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->flags &= ~WLAN_STA_WME;
-       if (elems.wme && hapd->conf->wme_enabled) {
-               if (hostapd_eid_wme_valid(hapd, elems.wme, elems.wme_len))
+       return WLAN_STATUS_SUCCESS;
+}
+
+
+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 (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,
-                                      "invalid WME element in association "
+                                      "invalid WMM element in association "
                                       "request");
                else
-                       sta->flags |= WLAN_STA_WME;
+                       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,
+                          u8 *ies, size_t ies_len, int reassoc)
+{
+       struct ieee802_11_elems elems;
+       u16 resp;
+       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) {
@@ -831,6 +676,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) {
@@ -851,10 +697,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) {
@@ -865,9 +711,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,
@@ -882,30 +728,33 @@ static void handle_assoc(struct hostapd_data *hapd,
                        resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
 #ifdef CONFIG_IEEE80211W
                else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
-                       resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
+                       resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
                else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
-                       resp = WLAN_STATUS_UNSPECIFIED_FAILURE; /* FIX */
+                       resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
 #endif /* CONFIG_IEEE80211W */
                else if (res == WPA_INVALID_MDIE)
                        resp = WLAN_STATUS_INVALID_MDIE;
                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->ping_timed_out) {
+               if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
+                   sta->sa_query_count > 0)
+                       ap_check_sa_query_timeout(hapd, sta);
+               if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
+                   (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
                        /*
-                        * STA has already been associated with MFP and ping
-                        * timeout has not been reached. Reject the
-                        * association attempt temporarily and start ping, if
-                        * one is not pending.
+                        * STA has already been associated with MFP and SA
+                        * Query timeout has not been reached. Reject the
+                        * association attempt temporarily and start SA Query,
+                        * if one is not pending.
                         */
 
-                       if (sta->ping_count == 0)
-                               ap_sta_start_ping(hapd, sta);
+                       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))
@@ -921,28 +770,206 @@ 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 (hostapd_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, 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 (hostapd_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,
+                        struct ieee80211_mgmt *mgmt, size_t len, int reassoc)
+{
+       u16 capab_info, listen_interval;
+       u16 resp = WLAN_STATUS_SUCCESS;
+       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++) {
@@ -983,72 +1010,28 @@ 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 */
 
-       /* get a unique AID */
-       if (sta->aid > 0) {
-               wpa_printf(MSG_DEBUG, "  old AID %d", sta->aid);
-       } else {
-               for (sta->aid = 1; sta->aid <= MAX_AID_TABLE_SIZE; sta->aid++)
-                       if (hapd->sta_aid[sta->aid - 1] == NULL)
-                               break;
-               if (sta->aid > MAX_AID_TABLE_SIZE) {
-                       sta->aid = 0;
-                       resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
-                       wpa_printf(MSG_ERROR, "  no room for more AIDs");
-                       goto fail;
-               } else {
-                       hapd->sta_aid[sta->aid - 1] = sta;
-                       wpa_printf(MSG_DEBUG, "  new AID %d", sta->aid);
-               }
-       }
-
        hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
                       HOSTAPD_LEVEL_DEBUG,
                       "association OK (aid %d)", sta->aid);
        /* Station will be marked associated, after it acknowledges AssocResp
         */
 
+#ifdef CONFIG_IEEE80211W
+       if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
+               wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
+                          "SA Query procedure", reassoc ? "re" : "");
+               /* TODO: Send a protected Disassociate frame to the STA using
+                * the old key and Reason Code "Previous Authentication no
+                * longer valid". Make sure this is only sent protected since
+                * unprotected frame would be received by the STA that is now
+                * trying to associate.
+                */
+       }
+#endif /* CONFIG_IEEE80211W */
+
        if (reassoc) {
                os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
                          ETH_ALEN);
@@ -1065,59 +1048,7 @@ 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_WME)
-                       p = hostapd_eid_wme(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 */
-                       p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
-                                                       buf + sizeof(buf) - p,
-                                                       sta->auth_alg);
-               }
-#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) < 0)
-               perror("handle_assoc: send");
+       send_assoc_resp(hapd, sta, resp, reassoc, pos, left);
 }
 
 
@@ -1144,6 +1075,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");
@@ -1192,6 +1125,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");
@@ -1225,51 +1160,96 @@ static void handle_beacon(struct hostapd_data *hapd,
 
 
 #ifdef CONFIG_IEEE80211W
-static void hostapd_ping_action(struct hostapd_data *hapd,
-                               struct ieee80211_mgmt *mgmt, size_t len)
+
+/* MLME-SAQuery.request */
+void ieee802_11_send_sa_query_req(struct hostapd_data *hapd,
+                                 const u8 *addr, const u8 *trans_id)
+{
+       struct ieee80211_mgmt mgmt;
+       u8 *end;
+
+       wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Request to "
+                  MACSTR, MAC2STR(addr));
+       wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
+                   trans_id, WLAN_SA_QUERY_TR_ID_LEN);
+
+       os_memset(&mgmt, 0, sizeof(mgmt));
+       mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
+                                         WLAN_FC_STYPE_ACTION);
+       os_memcpy(mgmt.da, addr, ETH_ALEN);
+       os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
+       os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
+       mgmt.u.action.category = WLAN_ACTION_SA_QUERY;
+       mgmt.u.action.u.sa_query_req.action = WLAN_SA_QUERY_REQUEST;
+       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)
+               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)
 {
        struct sta_info *sta;
        u8 *end;
        int i;
 
-       end = mgmt->u.action.u.ping_resp.trans_id + WLAN_PING_TRANS_ID_LEN;
+       end = mgmt->u.action.u.sa_query_resp.trans_id +
+               WLAN_SA_QUERY_TR_ID_LEN;
        if (((u8 *) mgmt) + len < end) {
-               wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short Ping Action "
+               wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
                           "frame (len=%lu)", (unsigned long) len);
                return;
        }
 
-       if (mgmt->u.action.u.ping_resp.action != WLAN_PING_RESPONSE) {
-               wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected Ping Action %d",
-                          mgmt->u.action.u.ping_resp.action);
+       if (mgmt->u.action.u.sa_query_resp.action != WLAN_SA_QUERY_RESPONSE) {
+               wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected SA Query "
+                          "Action %d", mgmt->u.action.u.sa_query_resp.action);
                return;
        }
 
-       /* MLME-PING.confirm */
+       wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Response from "
+                  MACSTR, MAC2STR(mgmt->sa));
+       wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
+                   mgmt->u.action.u.sa_query_resp.trans_id,
+                   WLAN_SA_QUERY_TR_ID_LEN);
+
+       /* MLME-SAQuery.confirm */
 
        sta = ap_get_sta(hapd, mgmt->sa);
-       if (sta == NULL || sta->ping_trans_id == NULL) {
+       if (sta == NULL || sta->sa_query_trans_id == NULL) {
                wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
-                          "pending ping request found");
+                          "pending SA Query request found");
                return;
        }
 
-       for (i = 0; i < sta->ping_count; i++) {
-               if (os_memcmp(sta->ping_trans_id + i * WLAN_PING_TRANS_ID_LEN,
-                             mgmt->u.action.u.ping_resp.trans_id,
-                             WLAN_PING_TRANS_ID_LEN) == 0)
+       for (i = 0; i < sta->sa_query_count; i++) {
+               if (os_memcmp(sta->sa_query_trans_id +
+                             i * WLAN_SA_QUERY_TR_ID_LEN,
+                             mgmt->u.action.u.sa_query_resp.trans_id,
+                             WLAN_SA_QUERY_TR_ID_LEN) == 0)
                        break;
        }
 
-       if (i >= sta->ping_count) {
-               wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching ping "
+       if (i >= sta->sa_query_count) {
+               wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching SA Query "
                           "transaction identifier found");
                return;
        }
 
        hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
-                      HOSTAPD_LEVEL_DEBUG, "Reply to pending ping received");
-       ap_sta_stop_ping(hapd, sta);
+                      HOSTAPD_LEVEL_DEBUG,
+                      "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 */
 
@@ -1277,6 +1257,8 @@ static void hostapd_ping_action(struct hostapd_data *hapd,
 static void handle_action(struct hostapd_data *hapd,
                          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,
@@ -1285,13 +1267,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,
@@ -1307,11 +1299,11 @@ static void handle_action(struct hostapd_data *hapd,
        }
 #endif /* CONFIG_IEEE80211R */
        case WLAN_ACTION_WMM:
-               hostapd_wme_action(hapd, mgmt, len);
+               hostapd_wmm_action(hapd, mgmt, len);
                return;
 #ifdef CONFIG_IEEE80211W
-       case WLAN_ACTION_PING:
-               hostapd_ping_action(hapd, mgmt, len);
+       case WLAN_ACTION_SA_QUERY:
+               hostapd_sa_query_action(hapd, mgmt, len);
                return;
 #endif /* CONFIG_IEEE80211W */
        }
@@ -1335,7 +1327,7 @@ static void handle_action(struct hostapd_data *hapd,
                os_memcpy(mgmt->bssid, hapd->own_addr, ETH_ALEN);
                mgmt->u.action.category |= 0x80;
 
-               hostapd_send_mgmt_frame(hapd, mgmt, len, 0);
+               hostapd_send_mgmt_frame(hapd, mgmt, len);
        }
 }
 
@@ -1347,6 +1339,7 @@ static void handle_action(struct hostapd_data *hapd,
  * @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
  * each frame received from the kernel driver through wlan#ap interface. In
@@ -1364,9 +1357,6 @@ void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
                return;
        }
 
-       if (fi && fi->passive_scan)
-               return;
-
        broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
                mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
                mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
@@ -1474,7 +1464,8 @@ static void handle_assoc_cb(struct hostapd_data *hapd,
        u16 status;
        struct sta_info *sta;
        int new_assoc = 1;
-       struct ht_cap_ie *ht_cap = 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,
@@ -1517,25 +1508,38 @@ static void handle_assoc_cb(struct hostapd_data *hapd,
        if (sta->flags & WLAN_STA_ASSOC)
                new_assoc = 0;
        sta->flags |= WLAN_STA_ASSOC;
+       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)
                mlme_reassociate_indication(hapd, sta);
        else
                mlme_associate_indication(hapd, sta);
 
+#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)
-               ht_cap = &sta->ht_capabilities;
+               hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
 #endif /* CONFIG_IEEE80211N */
 
-#ifdef CONFIG_IEEE80211W
-       sta->ping_timed_out = 0;
-#endif /* CONFIG_IEEE80211W */
-
        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))
+                           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,
@@ -1553,13 +1557,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);
@@ -1578,6 +1585,15 @@ static void handle_assoc_cb(struct hostapd_data *hapd,
 }
 
 
+/**
+ * ieee802_11_mgmt_cb - Process management frame TX status callback
+ * @hapd: hostapd BSS data structure (the BSS from which the management frame
+ * was sent from)
+ * @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
+ * @ok: Whether the frame was ACK'ed
+ */
 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
                        u16 stype, int ok)
 {
@@ -1612,76 +1628,6 @@ void ieee802_11_mgmt_cb(struct hostapd_data *hapd, u8 *buf, size_t len,
 }
 
 
-static void ieee80211_tkip_countermeasures_stop(void *eloop_ctx,
-                                               void *timeout_ctx)
-{
-       struct hostapd_data *hapd = eloop_ctx;
-       hapd->tkip_countermeasures = 0;
-       hostapd_set_countermeasures(hapd, 0);
-       hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
-                      HOSTAPD_LEVEL_INFO, "TKIP countermeasures ended");
-}
-
-
-static void ieee80211_tkip_countermeasures_start(struct hostapd_data *hapd)
-{
-       struct sta_info *sta;
-
-       hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
-                      HOSTAPD_LEVEL_INFO, "TKIP countermeasures initiated");
-
-       wpa_auth_countermeasures_start(hapd->wpa_auth);
-       hapd->tkip_countermeasures = 1;
-       hostapd_set_countermeasures(hapd, 1);
-       wpa_gtk_rekey(hapd->wpa_auth);
-       eloop_cancel_timeout(ieee80211_tkip_countermeasures_stop, hapd, NULL);
-       eloop_register_timeout(60, 0, ieee80211_tkip_countermeasures_stop,
-                              hapd, NULL);
-       for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
-               hostapd_sta_deauth(hapd, sta->addr,
-                                  WLAN_REASON_MICHAEL_MIC_FAILURE);
-               sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
-                               WLAN_STA_AUTHORIZED);
-               hostapd_sta_remove(hapd, sta->addr);
-       }
-}
-
-
-void ieee80211_michael_mic_failure(struct hostapd_data *hapd, const u8 *addr,
-                                  int local)
-{
-       time_t now;
-
-       if (addr && local) {
-               struct sta_info *sta = ap_get_sta(hapd, addr);
-               if (sta != NULL) {
-                       wpa_auth_sta_local_mic_failure_report(sta->wpa_sm);
-                       hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
-                                      HOSTAPD_LEVEL_INFO,
-                                      "Michael MIC failure detected in "
-                                      "received frame");
-                       mlme_michaelmicfailure_indication(hapd, addr);
-               } else {
-                       wpa_printf(MSG_DEBUG,
-                                  "MLME-MICHAELMICFAILURE.indication "
-                                  "for not associated STA (" MACSTR
-                                  ") ignored", MAC2STR(addr));
-                       return;
-               }
-       }
-
-       time(&now);
-       if (now > hapd->michael_mic_failure + 60) {
-               hapd->michael_mic_failures = 1;
-       } else {
-               hapd->michael_mic_failures++;
-               if (hapd->michael_mic_failures > 1)
-                       ieee80211_tkip_countermeasures_start(hapd);
-       }
-       hapd->michael_mic_failure = now;
-}
-
-
 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
 {
        /* TODO */