Add station tracking based on other management frame subtypes
[mech_eap.git] / src / ap / ieee802_11.c
index 3ecac91..19c6a12 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * hostapd / IEEE 802.11 Management
- * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
+ * Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi>
  *
  * This software may be distributed under the terms of the BSD license.
  * See README for more details.
@@ -13,7 +13,8 @@
 #include "utils/common.h"
 #include "utils/eloop.h"
 #include "crypto/crypto.h"
-#include "drivers/driver.h"
+#include "crypto/sha256.h"
+#include "crypto/random.h"
 #include "common/ieee802_11_defs.h"
 #include "common/ieee802_11_common.h"
 #include "common/wpa_ctrl.h"
 #include "radius/radius_client.h"
 #include "p2p/p2p.h"
 #include "wps/wps.h"
+#include "fst/fst.h"
 #include "hostapd.h"
 #include "beacon.h"
 #include "ieee802_11_auth.h"
 #include "sta_info.h"
 #include "ieee802_1x.h"
 #include "wpa_auth.h"
+#include "pmksa_cache_auth.h"
 #include "wmm.h"
 #include "ap_list.h"
 #include "accounting.h"
@@ -36,7 +39,9 @@
 #include "p2p_hostapd.h"
 #include "ap_drv_ops.h"
 #include "wnm_ap.h"
+#include "hw_features.h"
 #include "ieee802_11.h"
+#include "dfs.h"
 
 
 u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
@@ -60,7 +65,6 @@ u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
        }
 
        *pos++ = num;
-       count = 0;
        for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
             i++) {
                count++;
@@ -103,7 +107,6 @@ u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
 
        *pos++ = WLAN_EID_EXT_SUPP_RATES;
        *pos++ = num;
-       count = 0;
        for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
             i++) {
                count++;
@@ -131,11 +134,19 @@ u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
 }
 
 
-u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
-                          int probe)
+u16 hostapd_own_capab_info(struct hostapd_data *hapd)
 {
        int capab = WLAN_CAPABILITY_ESS;
        int privacy;
+       int dfs;
+
+       /* Check if any of configured channels require DFS */
+       dfs = hostapd_is_dfs_required(hapd->iface);
+       if (dfs < 0) {
+               wpa_printf(MSG_WARNING, "Failed to check if DFS is required; ret=%d",
+                          dfs);
+               dfs = 0;
+       }
 
        if (hapd->iface->num_sta_no_short_preamble == 0 &&
            hapd->iconf->preamble == SHORT_PREAMBLE)
@@ -151,19 +162,10 @@ u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
        if (hapd->conf->wpa)
                privacy = 1;
 
-       if (sta) {
-               int policy, def_klen;
-               if (probe && sta->ssid_probe) {
-                       policy = sta->ssid_probe->security_policy;
-                       def_klen = sta->ssid_probe->wep.default_len;
-               } else {
-                       policy = sta->ssid->security_policy;
-                       def_klen = sta->ssid->wep.default_len;
-               }
-               privacy = policy != SECURITY_PLAINTEXT;
-               if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
-                       privacy = 0;
-       }
+#ifdef CONFIG_HS20
+       if (hapd->conf->osen)
+               privacy = 1;
+#endif /* CONFIG_HS20 */
 
        if (privacy)
                capab |= WLAN_CAPABILITY_PRIVACY;
@@ -173,25 +175,25 @@ u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
            hapd->iface->num_sta_no_short_slot_time == 0)
                capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
 
-       return capab;
-}
+       /*
+        * Currently, Spectrum Management capability bit is set when directly
+        * requested in configuration by spectrum_mgmt_required or when AP is
+        * running on DFS channel.
+        * TODO: Also consider driver support for TPC to set Spectrum Mgmt bit
+        */
+       if (hapd->iface->current_mode &&
+           hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A &&
+           (hapd->iconf->spectrum_mgmt_required || dfs))
+               capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
 
+       if (hapd->conf->radio_measurements)
+               capab |= IEEE80211_CAP_RRM;
 
-void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
-{
-       int i;
-       if (len > HOSTAPD_MAX_SSID_LEN)
-               len = HOSTAPD_MAX_SSID_LEN;
-       for (i = 0; i < len; i++) {
-               if (ssid[i] >= 32 && ssid[i] < 127)
-                       buf[i] = ssid[i];
-               else
-                       buf[i] = '.';
-       }
-       buf[len] = '\0';
+       return capab;
 }
 
 
+#ifndef CONFIG_NO_RC4
 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
                           u16 auth_transaction, const u8 *challenge,
                           int iswep)
@@ -226,7 +228,8 @@ static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
 
        /* Transaction 3 */
        if (!iswep || !sta->challenge || !challenge ||
-           os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
+           os_memcmp_const(sta->challenge, challenge,
+                           WLAN_AUTH_CHALLENGE_LEN)) {
                hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
                               HOSTAPD_LEVEL_INFO,
                               "shared key authentication - invalid "
@@ -237,18 +240,14 @@ static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
        hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
                       HOSTAPD_LEVEL_DEBUG,
                       "authentication OK (shared key)");
-#ifdef IEEE80211_REQUIRE_AUTH_ACK
-       /* Station will be marked authenticated if it ACKs the
-        * authentication reply. */
-#else
        sta->flags |= WLAN_STA_AUTH;
        wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
-#endif
        os_free(sta->challenge);
        sta->challenge = NULL;
 
        return 0;
 }
+#endif /* CONFIG_NO_RC4 */
 
 
 static void send_auth_reply(struct hostapd_data *hapd,
@@ -284,7 +283,7 @@ static void send_auth_reply(struct hostapd_data *hapd,
                   MAC2STR(dst), auth_alg, auth_transaction,
                   resp, (unsigned long) ies_len);
        if (hostapd_drv_send_mlme(hapd, reply, rlen, 0) < 0)
-               perror("send_auth_reply: send");
+               wpa_printf(MSG_INFO, "send_auth_reply: send");
 
        os_free(buf);
 }
@@ -318,8 +317,11 @@ static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
 
 #ifdef CONFIG_SAE
 
-static struct wpabuf * auth_process_sae_commit(struct hostapd_data *hapd,
-                                              struct sta_info *sta)
+#define dot11RSNASAESync 5             /* attempts */
+
+
+static struct wpabuf * auth_build_sae_commit(struct hostapd_data *hapd,
+                                            struct sta_info *sta, int update)
 {
        struct wpabuf *buf;
 
@@ -328,7 +330,8 @@ static struct wpabuf * auth_process_sae_commit(struct hostapd_data *hapd,
                return NULL;
        }
 
-       if (sae_prepare_commit(hapd->own_addr, sta->addr,
+       if (update &&
+           sae_prepare_commit(hapd->own_addr, sta->addr,
                               (u8 *) hapd->conf->ssid.wpa_passphrase,
                               os_strlen(hapd->conf->ssid.wpa_passphrase),
                               sta->sae) < 0) {
@@ -336,15 +339,11 @@ static struct wpabuf * auth_process_sae_commit(struct hostapd_data *hapd,
                return NULL;
        }
 
-       if (sae_process_commit(sta->sae) < 0) {
-               wpa_printf(MSG_DEBUG, "SAE: Failed to process peer commit");
-               return NULL;
-       }
-
        buf = wpabuf_alloc(SAE_COMMIT_MAX_LEN);
        if (buf == NULL)
                return NULL;
-       sae_write_commit(sta->sae, buf);
+       sae_write_commit(sta->sae, buf, sta->sae->tmp ?
+                        sta->sae->tmp->anti_clogging_token : NULL);
 
        return buf;
 }
@@ -365,74 +364,515 @@ static struct wpabuf * auth_build_sae_confirm(struct hostapd_data *hapd,
 }
 
 
+static int auth_sae_send_commit(struct hostapd_data *hapd,
+                               struct sta_info *sta,
+                               const u8 *bssid, int update)
+{
+       struct wpabuf *data;
+
+       data = auth_build_sae_commit(hapd, sta, update);
+       if (data == NULL)
+               return WLAN_STATUS_UNSPECIFIED_FAILURE;
+
+       send_auth_reply(hapd, sta->addr, bssid,
+                       WLAN_AUTH_SAE, 1, WLAN_STATUS_SUCCESS,
+                       wpabuf_head(data), wpabuf_len(data));
+
+       wpabuf_free(data);
+
+       return WLAN_STATUS_SUCCESS;
+}
+
+
+static int auth_sae_send_confirm(struct hostapd_data *hapd,
+                                struct sta_info *sta,
+                                const u8 *bssid)
+{
+       struct wpabuf *data;
+
+       data = auth_build_sae_confirm(hapd, sta);
+       if (data == NULL)
+               return WLAN_STATUS_UNSPECIFIED_FAILURE;
+
+       send_auth_reply(hapd, sta->addr, bssid,
+                       WLAN_AUTH_SAE, 2, WLAN_STATUS_SUCCESS,
+                       wpabuf_head(data), wpabuf_len(data));
+
+       wpabuf_free(data);
+
+       return WLAN_STATUS_SUCCESS;
+}
+
+
+static int use_sae_anti_clogging(struct hostapd_data *hapd)
+{
+       struct sta_info *sta;
+       unsigned int open = 0;
+
+       if (hapd->conf->sae_anti_clogging_threshold == 0)
+               return 1;
+
+       for (sta = hapd->sta_list; sta; sta = sta->next) {
+               if (!sta->sae)
+                       continue;
+               if (sta->sae->state != SAE_COMMITTED &&
+                   sta->sae->state != SAE_CONFIRMED)
+                       continue;
+               open++;
+               if (open >= hapd->conf->sae_anti_clogging_threshold)
+                       return 1;
+       }
+
+       return 0;
+}
+
+
+static int check_sae_token(struct hostapd_data *hapd, const u8 *addr,
+                          const u8 *token, size_t token_len)
+{
+       u8 mac[SHA256_MAC_LEN];
+
+       if (token_len != SHA256_MAC_LEN)
+               return -1;
+       if (hmac_sha256(hapd->sae_token_key, sizeof(hapd->sae_token_key),
+                       addr, ETH_ALEN, mac) < 0 ||
+           os_memcmp_const(token, mac, SHA256_MAC_LEN) != 0)
+               return -1;
+
+       return 0;
+}
+
+
+static struct wpabuf * auth_build_token_req(struct hostapd_data *hapd,
+                                           int group, const u8 *addr)
+{
+       struct wpabuf *buf;
+       u8 *token;
+       struct os_reltime now;
+
+       os_get_reltime(&now);
+       if (!os_reltime_initialized(&hapd->last_sae_token_key_update) ||
+           os_reltime_expired(&now, &hapd->last_sae_token_key_update, 60)) {
+               if (random_get_bytes(hapd->sae_token_key,
+                                    sizeof(hapd->sae_token_key)) < 0)
+                       return NULL;
+               wpa_hexdump(MSG_DEBUG, "SAE: Updated token key",
+                           hapd->sae_token_key, sizeof(hapd->sae_token_key));
+               hapd->last_sae_token_key_update = now;
+       }
+
+       buf = wpabuf_alloc(sizeof(le16) + SHA256_MAC_LEN);
+       if (buf == NULL)
+               return NULL;
+
+       wpabuf_put_le16(buf, group); /* Finite Cyclic Group */
+
+       token = wpabuf_put(buf, SHA256_MAC_LEN);
+       hmac_sha256(hapd->sae_token_key, sizeof(hapd->sae_token_key),
+                   addr, ETH_ALEN, token);
+
+       return buf;
+}
+
+
+static int sae_check_big_sync(struct sta_info *sta)
+{
+       if (sta->sae->sync > dot11RSNASAESync) {
+               sta->sae->state = SAE_NOTHING;
+               sta->sae->sync = 0;
+               return -1;
+       }
+       return 0;
+}
+
+
+static void auth_sae_retransmit_timer(void *eloop_ctx, void *eloop_data)
+{
+       struct hostapd_data *hapd = eloop_ctx;
+       struct sta_info *sta = eloop_data;
+       int ret;
+
+       if (sae_check_big_sync(sta))
+               return;
+       sta->sae->sync++;
+
+       switch (sta->sae->state) {
+       case SAE_COMMITTED:
+               ret = auth_sae_send_commit(hapd, sta, hapd->own_addr, 0);
+               eloop_register_timeout(0,
+                                      hapd->dot11RSNASAERetransPeriod * 1000,
+                                      auth_sae_retransmit_timer, hapd, sta);
+               break;
+       case SAE_CONFIRMED:
+               ret = auth_sae_send_confirm(hapd, sta, hapd->own_addr);
+               eloop_register_timeout(0,
+                                      hapd->dot11RSNASAERetransPeriod * 1000,
+                                      auth_sae_retransmit_timer, hapd, sta);
+               break;
+       default:
+               ret = -1;
+               break;
+       }
+
+       if (ret != WLAN_STATUS_SUCCESS)
+               wpa_printf(MSG_INFO, "SAE: Failed to retransmit: ret=%d", ret);
+}
+
+
+void sae_clear_retransmit_timer(struct hostapd_data *hapd, struct sta_info *sta)
+{
+       eloop_cancel_timeout(auth_sae_retransmit_timer, hapd, sta);
+}
+
+
+static void sae_set_retransmit_timer(struct hostapd_data *hapd,
+                                    struct sta_info *sta)
+{
+       if (!(hapd->conf->mesh & MESH_ENABLED))
+               return;
+
+       eloop_cancel_timeout(auth_sae_retransmit_timer, hapd, sta);
+       eloop_register_timeout(0, hapd->dot11RSNASAERetransPeriod * 1000,
+                              auth_sae_retransmit_timer, hapd, sta);
+}
+
+
+static int sae_sm_step(struct hostapd_data *hapd, struct sta_info *sta,
+                      const u8 *bssid, u8 auth_transaction)
+{
+       int ret;
+
+       if (auth_transaction != 1 && auth_transaction != 2)
+               return WLAN_STATUS_UNSPECIFIED_FAILURE;
+
+       switch (sta->sae->state) {
+       case SAE_NOTHING:
+               if (auth_transaction == 1) {
+                       ret = auth_sae_send_commit(hapd, sta, bssid, 1);
+                       if (ret)
+                               return ret;
+                       sta->sae->state = SAE_COMMITTED;
+
+                       if (sae_process_commit(sta->sae) < 0)
+                               return WLAN_STATUS_UNSPECIFIED_FAILURE;
+
+                       /*
+                        * In mesh case, both Commit and Confirm can be sent
+                        * immediately. In infrastructure BSS, only a single
+                        * Authentication frame (Commit) is expected from the AP
+                        * here and the second one (Confirm) will be sent once
+                        * the STA has sent its second Authentication frame
+                        * (Confirm).
+                        */
+                       if (hapd->conf->mesh & MESH_ENABLED) {
+                               /*
+                                * Send both Commit and Confirm immediately
+                                * based on SAE finite state machine
+                                * Nothing -> Confirm transition.
+                                */
+                               ret = auth_sae_send_confirm(hapd, sta, bssid);
+                               if (ret)
+                                       return ret;
+                               sta->sae->state = SAE_CONFIRMED;
+                       } else {
+                               /*
+                                * For infrastructure BSS, send only the Commit
+                                * message now to get alternating sequence of
+                                * Authentication frames between the AP and STA.
+                                * Confirm will be sent in
+                                * Commited -> Confirmed/Accepted transition
+                                * when receiving Confirm from STA.
+                                */
+                       }
+                       sta->sae->sync = 0;
+                       sae_set_retransmit_timer(hapd, sta);
+               } else {
+                       hostapd_logger(hapd, sta->addr,
+                                      HOSTAPD_MODULE_IEEE80211,
+                                      HOSTAPD_LEVEL_DEBUG,
+                                      "SAE confirm before commit");
+               }
+               break;
+       case SAE_COMMITTED:
+               sae_clear_retransmit_timer(hapd, sta);
+               if (auth_transaction == 1) {
+                       if (sae_process_commit(sta->sae) < 0)
+                               return WLAN_STATUS_UNSPECIFIED_FAILURE;
+
+                       ret = auth_sae_send_confirm(hapd, sta, bssid);
+                       if (ret)
+                               return ret;
+                       sta->sae->state = SAE_CONFIRMED;
+                       sta->sae->sync = 0;
+                       sae_set_retransmit_timer(hapd, sta);
+               } else if (hapd->conf->mesh & MESH_ENABLED) {
+                       /*
+                        * In mesh case, follow SAE finite state machine and
+                        * send Commit now, if sync count allows.
+                        */
+                       if (sae_check_big_sync(sta))
+                               return WLAN_STATUS_SUCCESS;
+                       sta->sae->sync++;
+
+                       ret = auth_sae_send_commit(hapd, sta, bssid, 0);
+                       if (ret)
+                               return ret;
+
+                       sae_set_retransmit_timer(hapd, sta);
+               } else {
+                       /*
+                        * For instructure BSS, send the postponed Confirm from
+                        * Nothing -> Confirmed transition that was reduced to
+                        * Nothing -> Committed above.
+                        */
+                       ret = auth_sae_send_confirm(hapd, sta, bssid);
+                       if (ret)
+                               return ret;
+
+                       sta->sae->state = SAE_CONFIRMED;
+
+                       /*
+                        * Since this was triggered on Confirm RX, run another
+                        * step to get to Accepted without waiting for
+                        * additional events.
+                        */
+                       return sae_sm_step(hapd, sta, bssid, auth_transaction);
+               }
+               break;
+       case SAE_CONFIRMED:
+               sae_clear_retransmit_timer(hapd, sta);
+               if (auth_transaction == 1) {
+                       if (sae_check_big_sync(sta))
+                               return WLAN_STATUS_SUCCESS;
+                       sta->sae->sync++;
+
+                       ret = auth_sae_send_commit(hapd, sta, bssid, 1);
+                       if (ret)
+                               return ret;
+
+                       if (sae_process_commit(sta->sae) < 0)
+                               return WLAN_STATUS_UNSPECIFIED_FAILURE;
+
+                       ret = auth_sae_send_confirm(hapd, sta, bssid);
+                       if (ret)
+                               return ret;
+
+                       sae_set_retransmit_timer(hapd, sta);
+               } else {
+                       sta->flags |= WLAN_STA_AUTH;
+                       sta->auth_alg = WLAN_AUTH_SAE;
+                       mlme_authenticate_indication(hapd, sta);
+                       wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
+                       sta->sae->state = SAE_ACCEPTED;
+                       wpa_auth_pmksa_add_sae(hapd->wpa_auth, sta->addr,
+                                              sta->sae->pmk);
+               }
+               break;
+       case SAE_ACCEPTED:
+               if (auth_transaction == 1) {
+                       wpa_printf(MSG_DEBUG, "SAE: remove the STA (" MACSTR
+                                  ") doing reauthentication",
+                                  MAC2STR(sta->addr));
+                       ap_free_sta(hapd, sta);
+               } else {
+                       if (sae_check_big_sync(sta))
+                               return WLAN_STATUS_SUCCESS;
+                       sta->sae->sync++;
+
+                       ret = auth_sae_send_confirm(hapd, sta, bssid);
+                       sae_clear_temp_data(sta->sae);
+                       if (ret)
+                               return ret;
+               }
+               break;
+       default:
+               wpa_printf(MSG_ERROR, "SAE: invalid state %d",
+                          sta->sae->state);
+               return WLAN_STATUS_UNSPECIFIED_FAILURE;
+       }
+       return WLAN_STATUS_SUCCESS;
+}
+
+
 static void handle_auth_sae(struct hostapd_data *hapd, struct sta_info *sta,
                            const struct ieee80211_mgmt *mgmt, size_t len,
-                           u8 auth_transaction)
+                           u16 auth_transaction, u16 status_code)
 {
        u16 resp = WLAN_STATUS_SUCCESS;
        struct wpabuf *data = NULL;
 
        if (!sta->sae) {
+               if (auth_transaction != 1 || status_code != WLAN_STATUS_SUCCESS)
+                       return;
                sta->sae = os_zalloc(sizeof(*sta->sae));
                if (sta->sae == NULL)
                        return;
+               sta->sae->state = SAE_NOTHING;
+               sta->sae->sync = 0;
        }
 
        if (auth_transaction == 1) {
+               const u8 *token = NULL, *pos, *end;
+               size_t token_len = 0;
                hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
                               HOSTAPD_LEVEL_DEBUG,
-                              "start SAE authentication (RX commit)");
+                              "start SAE authentication (RX commit, status=%u)",
+                              status_code);
+
+               if ((hapd->conf->mesh & MESH_ENABLED) &&
+                   status_code == WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ &&
+                   sta->sae->tmp) {
+                       pos = mgmt->u.auth.variable;
+                       end = ((const u8 *) mgmt) + len;
+                       if (pos + sizeof(le16) > end) {
+                               wpa_printf(MSG_ERROR,
+                                          "SAE: Too short anti-clogging token request");
+                               resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
+                               goto reply;
+                       }
+                       resp = sae_group_allowed(sta->sae,
+                                                hapd->conf->sae_groups,
+                                                WPA_GET_LE16(pos));
+                       if (resp != WLAN_STATUS_SUCCESS) {
+                               wpa_printf(MSG_ERROR,
+                                          "SAE: Invalid group in anti-clogging token request");
+                               goto reply;
+                       }
+                       pos += sizeof(le16);
+
+                       wpabuf_free(sta->sae->tmp->anti_clogging_token);
+                       sta->sae->tmp->anti_clogging_token =
+                               wpabuf_alloc_copy(pos, end - pos);
+                       if (sta->sae->tmp->anti_clogging_token == NULL) {
+                               wpa_printf(MSG_ERROR,
+                                          "SAE: Failed to alloc for anti-clogging token");
+                               return;
+                       }
+
+                       /*
+                        * IEEE Std 802.11-2012, 11.3.8.6.4: If the Status code
+                        * is 76, a new Commit Message shall be constructed
+                        * with the Anti-Clogging Token from the received
+                        * Authentication frame, and the commit-scalar and
+                        * COMMIT-ELEMENT previously sent.
+                        */
+                       if (auth_sae_send_commit(hapd, sta, mgmt->bssid, 0)) {
+                               wpa_printf(MSG_ERROR,
+                                          "SAE: Failed to send commit message");
+                               return;
+                       }
+                       sta->sae->state = SAE_COMMITTED;
+                       sta->sae->sync = 0;
+                       sae_set_retransmit_timer(hapd, sta);
+                       return;
+               }
+
+               if (status_code != WLAN_STATUS_SUCCESS)
+                       return;
+
                resp = sae_parse_commit(sta->sae, mgmt->u.auth.variable,
                                        ((const u8 *) mgmt) + len -
-                                       mgmt->u.auth.variable);
-               if (resp == WLAN_STATUS_SUCCESS) {
-                       sta->sae->state = SAE_COMMIT;
-                       data = auth_process_sae_commit(hapd, sta);
-                       if (data == NULL)
-                               resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
+                                       mgmt->u.auth.variable, &token,
+                                       &token_len, hapd->conf->sae_groups);
+               if (resp == SAE_SILENTLY_DISCARD) {
+                       wpa_printf(MSG_DEBUG,
+                                  "SAE: Drop commit message from " MACSTR " due to reflection attack",
+                                  MAC2STR(sta->addr));
+                       return;
                }
-       } else if (auth_transaction == 2) {
-               if (sta->sae->state != SAE_COMMIT) {
-                       hostapd_logger(hapd, sta->addr,
-                                      HOSTAPD_MODULE_IEEE80211,
-                                      HOSTAPD_LEVEL_DEBUG,
-                                      "SAE confirm before commit");
-                       resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
+               if (token && check_sae_token(hapd, sta->addr, token, token_len)
+                   < 0) {
+                       wpa_printf(MSG_DEBUG, "SAE: Drop commit message with "
+                                  "incorrect token from " MACSTR,
+                                  MAC2STR(sta->addr));
+                       return;
+               }
+
+               if (resp != WLAN_STATUS_SUCCESS)
+                       goto reply;
+
+               if (!token && use_sae_anti_clogging(hapd)) {
+                       wpa_printf(MSG_DEBUG,
+                                  "SAE: Request anti-clogging token from "
+                                  MACSTR, MAC2STR(sta->addr));
+                       data = auth_build_token_req(hapd, sta->sae->group,
+                                                   sta->addr);
+                       resp = WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ;
+                       if (hapd->conf->mesh & MESH_ENABLED)
+                               sta->sae->state = SAE_NOTHING;
+                       goto reply;
                }
+
+               resp = sae_sm_step(hapd, sta, mgmt->bssid, auth_transaction);
+       } else if (auth_transaction == 2) {
                hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
                               HOSTAPD_LEVEL_DEBUG,
-                              "SAE authentication (RX confirm)");
-               if (sae_check_confirm(sta->sae, mgmt->u.auth.variable,
-                                      ((u8 *) mgmt) + len -
-                                      mgmt->u.auth.variable) < 0) {
-                       resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
-               } else {
-                       resp = WLAN_STATUS_SUCCESS;
-                       sta->flags |= WLAN_STA_AUTH;
-                       wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
-                       sta->auth_alg = WLAN_AUTH_SAE;
-                       mlme_authenticate_indication(hapd, sta);
-
-                       data = auth_build_sae_confirm(hapd, sta);
-                       if (data == NULL)
+                              "SAE authentication (RX confirm, status=%u)",
+                              status_code);
+               if (status_code != WLAN_STATUS_SUCCESS)
+                       return;
+               if (sta->sae->state >= SAE_CONFIRMED ||
+                   !(hapd->conf->mesh & MESH_ENABLED)) {
+                       if (sae_check_confirm(sta->sae, mgmt->u.auth.variable,
+                                             ((u8 *) mgmt) + len -
+                                             mgmt->u.auth.variable) < 0) {
                                resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
+                               goto reply;
+                       }
                }
+               resp = sae_sm_step(hapd, sta, mgmt->bssid, auth_transaction);
        } else {
                hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
                               HOSTAPD_LEVEL_DEBUG,
-                              "unexpected SAE authentication transaction %u",
-                              auth_transaction);
+                              "unexpected SAE authentication transaction %u (status=%u)",
+                              auth_transaction, status_code);
+               if (status_code != WLAN_STATUS_SUCCESS)
+                       return;
                resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
        }
 
-       sta->auth_alg = WLAN_AUTH_SAE;
-
-       send_auth_reply(hapd, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
-                       auth_transaction, resp,
-                       data ? wpabuf_head(data) : (u8 *) "",
-                       data ? wpabuf_len(data) : 0);
+reply:
+       if (resp != WLAN_STATUS_SUCCESS) {
+               send_auth_reply(hapd, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
+                               auth_transaction, resp,
+                               data ? wpabuf_head(data) : (u8 *) "",
+                               data ? wpabuf_len(data) : 0);
+       }
        wpabuf_free(data);
 }
+
+
+/**
+ * auth_sae_init_committed - Send COMMIT and start SAE in committed state
+ * @hapd: BSS data for the device initiating the authentication
+ * @sta: the peer to which commit authentication frame is sent
+ *
+ * This function implements Init event handling (IEEE Std 802.11-2012,
+ * 11.3.8.6.3) in which initial COMMIT message is sent. Prior to calling, the
+ * sta->sae structure should be initialized appropriately via a call to
+ * sae_prepare_commit().
+ */
+int auth_sae_init_committed(struct hostapd_data *hapd, struct sta_info *sta)
+{
+       int ret;
+
+       if (!sta->sae || !sta->sae->tmp)
+               return -1;
+
+       if (sta->sae->state != SAE_NOTHING)
+               return -1;
+
+       ret = auth_sae_send_commit(hapd, sta, hapd->own_addr, 0);
+       if (ret)
+               return -1;
+
+       sta->sae->state = SAE_COMMITTED;
+       sta->sae->sync = 0;
+       sae_set_retransmit_timer(hapd, sta);
+
+       return 0;
+}
+
 #endif /* CONFIG_SAE */
 
 
@@ -452,17 +892,29 @@ static void handle_auth(struct hostapd_data *hapd,
        size_t resp_ies_len = 0;
        char *identity = NULL;
        char *radius_cui = NULL;
+       u16 seq_ctrl;
 
        if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
-               printf("handle_auth - too short payload (len=%lu)\n",
-                      (unsigned long) len);
+               wpa_printf(MSG_INFO, "handle_auth - too short payload (len=%lu)",
+                          (unsigned long) len);
+               return;
+       }
+
+#ifdef CONFIG_TESTING_OPTIONS
+       if (hapd->iconf->ignore_auth_probability > 0.0 &&
+           drand48() < hapd->iconf->ignore_auth_probability) {
+               wpa_printf(MSG_INFO,
+                          "TESTING: ignoring auth frame from " MACSTR,
+                          MAC2STR(mgmt->sa));
                return;
        }
+#endif /* CONFIG_TESTING_OPTIONS */
 
        auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
        auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
        status_code = le_to_host16(mgmt->u.auth.status_code);
        fc = le_to_host16(mgmt->frame_control);
+       seq_ctrl = le_to_host16(mgmt->seq_ctrl);
 
        if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
            2 + WLAN_AUTH_CHALLENGE_LEN &&
@@ -471,10 +923,22 @@ static void handle_auth(struct hostapd_data *hapd,
                challenge = &mgmt->u.auth.variable[2];
 
        wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
-                  "auth_transaction=%d status_code=%d wep=%d%s",
+                  "auth_transaction=%d status_code=%d wep=%d%s "
+                  "seq_ctrl=0x%x%s",
                   MAC2STR(mgmt->sa), auth_alg, auth_transaction,
                   status_code, !!(fc & WLAN_FC_ISWEP),
-                  challenge ? " challenge" : "");
+                  challenge ? " challenge" : "",
+                  seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
+
+#ifdef CONFIG_NO_RC4
+       if (auth_alg == WLAN_AUTH_SHARED_KEY) {
+               wpa_printf(MSG_INFO,
+                          "Unsupported authentication algorithm (%d)",
+                          auth_alg);
+               resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
+               goto fail;
+       }
+#endif /* CONFIG_NO_RC4 */
 
        if (hapd->tkip_countermeasures) {
                resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
@@ -493,35 +957,90 @@ static void handle_auth(struct hostapd_data *hapd,
 #endif /* CONFIG_SAE */
              ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
               auth_alg == WLAN_AUTH_SHARED_KEY))) {
-               printf("Unsupported authentication algorithm (%d)\n",
-                      auth_alg);
+               wpa_printf(MSG_INFO, "Unsupported authentication algorithm (%d)",
+                          auth_alg);
                resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
                goto fail;
        }
 
        if (!(auth_transaction == 1 || auth_alg == WLAN_AUTH_SAE ||
              (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
-               printf("Unknown authentication transaction number (%d)\n",
-                      auth_transaction);
+               wpa_printf(MSG_INFO, "Unknown authentication transaction number (%d)",
+                          auth_transaction);
                resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
                goto fail;
        }
 
        if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
-               printf("Station " MACSTR " not allowed to authenticate.\n",
-                      MAC2STR(mgmt->sa));
+               wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
+                          MAC2STR(mgmt->sa));
                resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
                goto fail;
        }
 
+       if (hapd->conf->no_auth_if_seen_on) {
+               struct hostapd_data *other;
+
+               other = sta_track_seen_on(hapd->iface, mgmt->sa,
+                                         hapd->conf->no_auth_if_seen_on);
+               if (other) {
+                       u8 *pos;
+                       u32 info;
+                       u8 op_class, channel, phytype;
+
+                       wpa_printf(MSG_DEBUG, "%s: Reject authentication from "
+                                  MACSTR " since STA has been seen on %s",
+                                  hapd->conf->iface, MAC2STR(mgmt->sa),
+                                  hapd->conf->no_auth_if_seen_on);
+
+                       resp = WLAN_STATUS_REJECTED_WITH_SUGGESTED_BSS_TRANSITION;
+                       pos = &resp_ies[0];
+                       *pos++ = WLAN_EID_NEIGHBOR_REPORT;
+                       *pos++ = 13;
+                       os_memcpy(pos, other->own_addr, ETH_ALEN);
+                       pos += ETH_ALEN;
+                       info = 0; /* TODO: BSSID Information */
+                       WPA_PUT_LE32(pos, info);
+                       pos += 4;
+                       if (other->iconf->hw_mode == HOSTAPD_MODE_IEEE80211AD)
+                               phytype = 8; /* dmg */
+                       else if (other->iconf->ieee80211ac)
+                               phytype = 9; /* vht */
+                       else if (other->iconf->ieee80211n)
+                               phytype = 7; /* ht */
+                       else if (other->iconf->hw_mode ==
+                                HOSTAPD_MODE_IEEE80211A)
+                               phytype = 4; /* ofdm */
+                       else if (other->iconf->hw_mode ==
+                                HOSTAPD_MODE_IEEE80211G)
+                               phytype = 6; /* erp */
+                       else
+                               phytype = 5; /* hrdsss */
+                       if (ieee80211_freq_to_channel_ext(
+                                   hostapd_hw_get_freq(other,
+                                                       other->iconf->channel),
+                                   other->iconf->secondary_channel,
+                                   other->iconf->ieee80211ac,
+                                   &op_class, &channel) == NUM_HOSTAPD_MODES) {
+                               op_class = 0;
+                               channel = other->iconf->channel;
+                       }
+                       *pos++ = op_class;
+                       *pos++ = channel;
+                       *pos++ = phytype;
+                       resp_ies_len = pos - &resp_ies[0];
+                       goto fail;
+               }
+       }
+
        res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
                                      &session_timeout,
                                      &acct_interim_interval, &vlan_id,
                                      &psk, &identity, &radius_cui);
 
        if (res == HOSTAPD_ACL_REJECT) {
-               printf("Station " MACSTR " not allowed to authenticate.\n",
-                      MAC2STR(mgmt->sa));
+               wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
+                          MAC2STR(mgmt->sa));
                resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
                goto fail;
        }
@@ -535,15 +1054,49 @@ static void handle_auth(struct hostapd_data *hapd,
                return;
        }
 
-       sta = ap_sta_add(hapd, mgmt->sa);
-       if (!sta) {
-               resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
-               goto fail;
+       sta = ap_get_sta(hapd, mgmt->sa);
+       if (sta) {
+               if ((fc & WLAN_FC_RETRY) &&
+                   sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
+                   sta->last_seq_ctrl == seq_ctrl &&
+                   sta->last_subtype == WLAN_FC_STYPE_AUTH) {
+                       hostapd_logger(hapd, sta->addr,
+                                      HOSTAPD_MODULE_IEEE80211,
+                                      HOSTAPD_LEVEL_DEBUG,
+                                      "Drop repeated authentication frame seq_ctrl=0x%x",
+                                      seq_ctrl);
+                       return;
+               }
+       } else {
+#ifdef CONFIG_MESH
+               if (hapd->conf->mesh & MESH_ENABLED) {
+                       /* if the mesh peer is not available, we don't do auth.
+                        */
+                       wpa_printf(MSG_DEBUG, "Mesh peer " MACSTR
+                                  " not yet known - drop Authentiation frame",
+                                  MAC2STR(mgmt->sa));
+                       /*
+                        * Save a copy of the frame so that it can be processed
+                        * if a new peer entry is added shortly after this.
+                        */
+                       wpabuf_free(hapd->mesh_pending_auth);
+                       hapd->mesh_pending_auth = wpabuf_alloc_copy(mgmt, len);
+                       os_get_reltime(&hapd->mesh_pending_auth_time);
+                       return;
+               }
+#endif /* CONFIG_MESH */
+
+               sta = ap_sta_add(hapd, mgmt->sa);
+               if (!sta) {
+                       resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
+                       goto fail;
+               }
        }
+       sta->last_seq_ctrl = seq_ctrl;
+       sta->last_subtype = WLAN_FC_STYPE_AUTH;
 
        if (vlan_id > 0) {
-               if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
-                                              vlan_id) == NULL) {
+               if (!hostapd_vlan_id_valid(hapd->conf->vlan, vlan_id)) {
                        hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
                                       HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
                                       "%d received from RADIUS server",
@@ -584,16 +1137,12 @@ static void handle_auth(struct hostapd_data *hapd,
                hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
                               HOSTAPD_LEVEL_DEBUG,
                               "authentication OK (open system)");
-#ifdef IEEE80211_REQUIRE_AUTH_ACK
-               /* Station will be marked authenticated if it ACKs the
-                * authentication reply. */
-#else
                sta->flags |= WLAN_STA_AUTH;
                wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
                sta->auth_alg = WLAN_AUTH_OPEN;
                mlme_authenticate_indication(hapd, sta);
-#endif
                break;
+#ifndef CONFIG_NO_RC4
        case WLAN_AUTH_SHARED_KEY:
                resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
                                       fc & WLAN_FC_ISWEP);
@@ -607,12 +1156,13 @@ static void handle_auth(struct hostapd_data *hapd,
                        resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
                }
                break;
+#endif /* CONFIG_NO_RC4 */
 #ifdef CONFIG_IEEE80211R
        case WLAN_AUTH_FT:
                sta->auth_alg = WLAN_AUTH_FT;
                if (sta->wpa_sm == NULL)
                        sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
-                                                       sta->addr);
+                                                       sta->addr, NULL);
                if (sta->wpa_sm == NULL) {
                        wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
                                   "state machine");
@@ -629,7 +1179,23 @@ static void handle_auth(struct hostapd_data *hapd,
 #endif /* CONFIG_IEEE80211R */
 #ifdef CONFIG_SAE
        case WLAN_AUTH_SAE:
-               handle_auth_sae(hapd, sta, mgmt, len, auth_transaction);
+#ifdef CONFIG_MESH
+               if (status_code == WLAN_STATUS_SUCCESS &&
+                   hapd->conf->mesh & MESH_ENABLED) {
+                       if (sta->wpa_sm == NULL)
+                               sta->wpa_sm =
+                                       wpa_auth_sta_init(hapd->wpa_auth,
+                                                         sta->addr, NULL);
+                       if (sta->wpa_sm == NULL) {
+                               wpa_printf(MSG_DEBUG,
+                                          "SAE: Failed to initialize WPA state machine");
+                               resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
+                               goto fail;
+                       }
+               }
+#endif /* CONFIG_MESH */
+               handle_auth_sae(hapd, sta, mgmt, len, auth_transaction,
+                               status_code);
                return;
 #endif /* CONFIG_SAE */
        }
@@ -685,12 +1251,10 @@ static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
 
        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);
+                              "'%s'", wpa_ssid_txt(ssid_ie, ssid_ie_len));
                return WLAN_STATUS_UNSPECIFIED_FAILURE;
        }
 
@@ -752,6 +1316,21 @@ static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
 }
 
 
+static u16 check_ext_capab(struct hostapd_data *hapd, struct sta_info *sta,
+                          const u8 *ext_capab_ie, size_t ext_capab_ie_len)
+{
+#ifdef CONFIG_INTERWORKING
+       /* check for QoS Map support */
+       if (ext_capab_ie_len >= 5) {
+               if (ext_capab_ie[4] & 0x01)
+                       sta->qos_map_enabled = 1;
+       }
+#endif /* CONFIG_INTERWORKING */
+
+       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)
 {
@@ -759,6 +1338,7 @@ static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
        u16 resp;
        const u8 *wpa_ie;
        size_t wpa_ie_len;
+       const u8 *p2p_dev_addr = NULL;
 
        if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
                hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
@@ -773,12 +1353,14 @@ static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
        resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
        if (resp != WLAN_STATUS_SUCCESS)
                return resp;
+       resp = check_ext_capab(hapd, sta, elems.ext_capab, elems.ext_capab_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
-       resp = copy_sta_ht_capab(hapd, sta, elems.ht_capabilities,
-                                elems.ht_capabilities_len);
+       resp = copy_sta_ht_capab(hapd, sta, elems.ht_capabilities);
        if (resp != WLAN_STATUS_SUCCESS)
                return resp;
        if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
@@ -791,19 +1373,43 @@ static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
 #endif /* CONFIG_IEEE80211N */
 
 #ifdef CONFIG_IEEE80211AC
-       resp = copy_sta_vht_capab(hapd, sta, elems.vht_capabilities,
-                                 elems.vht_capabilities_len);
+       resp = copy_sta_vht_capab(hapd, sta, elems.vht_capabilities);
        if (resp != WLAN_STATUS_SUCCESS)
                return resp;
+
+       resp = set_sta_vht_opmode(hapd, sta, elems.vht_opmode_notif);
+       if (resp != WLAN_STATUS_SUCCESS)
+               return resp;
+
        if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht &&
            !(sta->flags & WLAN_STA_VHT)) {
                hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
                               HOSTAPD_LEVEL_INFO, "Station does not support "
                               "mandatory VHT PHY - reject association");
-               return WLAN_STATUS_UNSPECIFIED_FAILURE;
+               return WLAN_STATUS_ASSOC_DENIED_NO_VHT;
+       }
+
+       if (hapd->conf->vendor_vht && !elems.vht_capabilities) {
+               resp = copy_sta_vendor_vht(hapd, sta, elems.vendor_vht,
+                                          elems.vendor_vht_len);
+               if (resp != WLAN_STATUS_SUCCESS)
+                       return resp;
        }
 #endif /* CONFIG_IEEE80211AC */
 
+#ifdef CONFIG_P2P
+       if (elems.p2p) {
+               wpabuf_free(sta->p2p_ie);
+               sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
+                                                         P2P_IE_VENDOR_TYPE);
+               if (sta->p2p_ie)
+                       p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
+       } else {
+               wpabuf_free(sta->p2p_ie);
+               sta->p2p_ie = NULL;
+       }
+#endif /* CONFIG_P2P */
+
        if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
                wpa_ie = elems.rsn_ie;
                wpa_ie_len = elems.rsn_ie_len;
@@ -855,7 +1461,8 @@ static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
                wpa_ie_len += 2;
                if (sta->wpa_sm == NULL)
                        sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
-                                                       sta->addr);
+                                                       sta->addr,
+                                                       p2p_dev_addr);
                if (sta->wpa_sm == NULL) {
                        wpa_printf(MSG_WARNING, "Failed to initialize WPA "
                                   "state machine");
@@ -928,7 +1535,21 @@ static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
 
 #ifdef CONFIG_SAE
                if (wpa_auth_uses_sae(sta->wpa_sm) &&
-                   sta->auth_alg != WLAN_AUTH_SAE) {
+                   sta->auth_alg == WLAN_AUTH_OPEN) {
+                       struct rsn_pmksa_cache_entry *sa;
+                       sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
+                       if (!sa || sa->akmp != WPA_KEY_MGMT_SAE) {
+                               wpa_printf(MSG_DEBUG,
+                                          "SAE: No PMKSA cache entry found for "
+                                          MACSTR, MAC2STR(sta->addr));
+                               return WLAN_STATUS_INVALID_PMKID;
+                       }
+                       wpa_printf(MSG_DEBUG, "SAE: " MACSTR
+                                  " using PMKSA caching", MAC2STR(sta->addr));
+               } else if (wpa_auth_uses_sae(sta->wpa_sm) &&
+                          sta->auth_alg != WLAN_AUTH_SAE &&
+                          !(sta->auth_alg == WLAN_AUTH_FT &&
+                            wpa_auth_uses_ft_sae(sta->wpa_sm))) {
                        wpa_printf(MSG_DEBUG, "SAE: " MACSTR " tried to use "
                                   "SAE AKM after non-SAE auth_alg %u",
                                   MAC2STR(sta->addr), sta->auth_alg);
@@ -947,20 +1568,33 @@ static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
                        return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
                }
 #endif /* CONFIG_IEEE80211N */
+#ifdef CONFIG_HS20
+       } else if (hapd->conf->osen) {
+               if (elems.osen == NULL) {
+                       hostapd_logger(
+                               hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
+                               HOSTAPD_LEVEL_INFO,
+                               "No HS 2.0 OSEN element in association request");
+                       return WLAN_STATUS_INVALID_IE;
+               }
+
+               wpa_printf(MSG_DEBUG, "HS 2.0: OSEN association");
+               if (sta->wpa_sm == NULL)
+                       sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
+                                                       sta->addr, NULL);
+               if (sta->wpa_sm == NULL) {
+                       wpa_printf(MSG_WARNING, "Failed to initialize WPA "
+                                  "state machine");
+                       return WLAN_STATUS_UNSPECIFIED_FAILURE;
+               }
+               if (wpa_validate_osen(hapd->wpa_auth, sta->wpa_sm,
+                                     elems.osen - 2, elems.osen_len + 2) < 0)
+                       return WLAN_STATUS_INVALID_IE;
+#endif /* CONFIG_HS20 */
        } else
                wpa_auth_sta_no_wpa(sta->wpa_sm);
 
 #ifdef CONFIG_P2P
-       if (elems.p2p) {
-               wpabuf_free(sta->p2p_ie);
-               sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
-                                                         P2P_IE_VENDOR_TYPE);
-
-       } else {
-               wpabuf_free(sta->p2p_ie);
-               sta->p2p_ie = NULL;
-       }
-
        p2p_group_notif_assoc(hapd->p2p_group, sta->addr, ies, ies_len);
 #endif /* CONFIG_P2P */
 
@@ -973,6 +1607,14 @@ static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
                sta->hs20_ie = NULL;
 #endif /* CONFIG_HS20 */
 
+#ifdef CONFIG_FST
+       wpabuf_free(sta->mb_ies);
+       if (hapd->iface->fst)
+               sta->mb_ies = mb_ies_by_info(&elems.mb_ies);
+       else
+               sta->mb_ies = NULL;
+#endif /* CONFIG_FST */
+
        return WLAN_STATUS_SUCCESS;
 }
 
@@ -1021,10 +1663,9 @@ static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
        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));
+               host_to_le16(hostapd_own_capab_info(hapd));
        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));
+       reply->u.assoc_resp.aid = host_to_le16(sta->aid | BIT(14) | BIT(15));
        /* Supported rates */
        p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
        /* Extended supported rates */
@@ -1051,12 +1692,29 @@ static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
 #endif /* CONFIG_IEEE80211N */
 
 #ifdef CONFIG_IEEE80211AC
-       p = hostapd_eid_vht_capabilities(hapd, p);
-       p = hostapd_eid_vht_operation(hapd, p);
+       if (hapd->iconf->ieee80211ac && !hapd->conf->disable_11ac) {
+               p = hostapd_eid_vht_capabilities(hapd, p);
+               p = hostapd_eid_vht_operation(hapd, p);
+       }
 #endif /* CONFIG_IEEE80211AC */
 
        p = hostapd_eid_ext_capab(hapd, p);
        p = hostapd_eid_bss_max_idle_period(hapd, p);
+       if (sta->qos_map_enabled)
+               p = hostapd_eid_qos_map_set(hapd, p);
+
+#ifdef CONFIG_FST
+       if (hapd->iface->fst_ies) {
+               os_memcpy(p, wpabuf_head(hapd->iface->fst_ies),
+                         wpabuf_len(hapd->iface->fst_ies));
+               p += wpabuf_len(hapd->iface->fst_ies);
+       }
+#endif /* CONFIG_FST */
+
+#ifdef CONFIG_IEEE80211AC
+       if (hapd->conf->vendor_vht && (sta->flags & WLAN_STA_VENDOR_VHT))
+               p = hostapd_eid_vendor_vht(hapd, p);
+#endif /* CONFIG_IEEE80211AC */
 
        if (sta->flags & WLAN_STA_WMM)
                p = hostapd_eid_wmm(hapd, p);
@@ -1115,7 +1773,7 @@ static void handle_assoc(struct hostapd_data *hapd,
                         const struct ieee80211_mgmt *mgmt, size_t len,
                         int reassoc)
 {
-       u16 capab_info, listen_interval;
+       u16 capab_info, listen_interval, seq_ctrl, fc;
        u16 resp = WLAN_STATUS_SUCCESS;
        const u8 *pos;
        int left, i;
@@ -1123,20 +1781,44 @@ static void handle_assoc(struct hostapd_data *hapd,
 
        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);
+               wpa_printf(MSG_INFO, "handle_assoc(reassoc=%d) - too short payload (len=%lu)",
+                          reassoc, (unsigned long) len);
                return;
        }
 
+#ifdef CONFIG_TESTING_OPTIONS
+       if (reassoc) {
+               if (hapd->iconf->ignore_reassoc_probability > 0.0 &&
+                   drand48() < hapd->iconf->ignore_reassoc_probability) {
+                       wpa_printf(MSG_INFO,
+                                  "TESTING: ignoring reassoc request from "
+                                  MACSTR, MAC2STR(mgmt->sa));
+                       return;
+               }
+       } else {
+               if (hapd->iconf->ignore_assoc_probability > 0.0 &&
+                   drand48() < hapd->iconf->ignore_assoc_probability) {
+                       wpa_printf(MSG_INFO,
+                                  "TESTING: ignoring assoc request from "
+                                  MACSTR, MAC2STR(mgmt->sa));
+                       return;
+               }
+       }
+#endif /* CONFIG_TESTING_OPTIONS */
+
+       fc = le_to_host16(mgmt->frame_control);
+       seq_ctrl = le_to_host16(mgmt->seq_ctrl);
+
        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,
+                          MACSTR " seq_ctrl=0x%x%s",
                           MAC2STR(mgmt->sa), capab_info, listen_interval,
-                          MAC2STR(mgmt->u.reassoc_req.current_ap));
+                          MAC2STR(mgmt->u.reassoc_req.current_ap),
+                          seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
                left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
                pos = mgmt->u.reassoc_req.variable;
        } else {
@@ -1144,8 +1826,10 @@ static void handle_assoc(struct hostapd_data *hapd,
                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);
+                          " capab_info=0x%02x listen_interval=%d "
+                          "seq_ctrl=0x%x%s",
+                          MAC2STR(mgmt->sa), capab_info, listen_interval,
+                          seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
                left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
                pos = mgmt->u.assoc_req.variable;
        }
@@ -1171,6 +1855,21 @@ static void handle_assoc(struct hostapd_data *hapd,
                return;
        }
 
+       if ((fc & WLAN_FC_RETRY) &&
+           sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
+           sta->last_seq_ctrl == seq_ctrl &&
+           sta->last_subtype == reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
+           WLAN_FC_STYPE_ASSOC_REQ) {
+               hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
+                              HOSTAPD_LEVEL_DEBUG,
+                              "Drop repeated association frame seq_ctrl=0x%x",
+                              seq_ctrl);
+               return;
+       }
+       sta->last_seq_ctrl = seq_ctrl;
+       sta->last_subtype = reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
+               WLAN_FC_STYPE_ASSOC_REQ;
+
        if (hapd->tkip_countermeasures) {
                resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
                goto fail;
@@ -1264,17 +1963,6 @@ static void handle_assoc(struct hostapd_data *hapd,
        }
 #endif /* CONFIG_IEEE80211W */
 
-       if (reassoc) {
-               os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
-                         ETH_ALEN);
-       }
-
-       if (sta->last_assoc_req)
-               os_free(sta->last_assoc_req);
-       sta->last_assoc_req = os_malloc(len);
-       if (sta->last_assoc_req)
-               os_memcpy(sta->last_assoc_req, mgmt, len);
-
        /* Make sure that the previously registered inactivity timer will not
         * remove the STA immediately. */
        sta->timeout_next = STA_NULLFUNC;
@@ -1290,8 +1978,8 @@ static void handle_disassoc(struct hostapd_data *hapd,
        struct sta_info *sta;
 
        if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
-               printf("handle_disassoc - too short payload (len=%lu)\n",
-                      (unsigned long) len);
+               wpa_printf(MSG_INFO, "handle_disassoc - too short payload (len=%lu)",
+                          (unsigned long) len);
                return;
        }
 
@@ -1301,12 +1989,13 @@ static void handle_disassoc(struct hostapd_data *hapd,
 
        sta = ap_get_sta(hapd, mgmt->sa);
        if (sta == NULL) {
-               printf("Station " MACSTR " trying to disassociate, but it "
-                      "is not associated.\n", MAC2STR(mgmt->sa));
+               wpa_printf(MSG_INFO, "Station " MACSTR " trying to disassociate, but it is not associated",
+                          MAC2STR(mgmt->sa));
                return;
        }
 
        ap_sta_set_authorized(hapd, sta, 0);
+       sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
        sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
        wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
        hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
@@ -1317,6 +2006,9 @@ static void handle_disassoc(struct hostapd_data *hapd,
         * authenticated. */
        accounting_sta_stop(hapd, sta);
        ieee802_1x_free_station(sta);
+       if (sta->ipaddr)
+               hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
+       ap_sta_ip6addr_del(hapd, sta);
        hostapd_drv_sta_remove(hapd, sta->addr);
 
        if (sta->timeout_next == STA_NULLFUNC ||
@@ -1356,6 +2048,7 @@ static void handle_deauth(struct hostapd_data *hapd,
        }
 
        ap_sta_set_authorized(hapd, sta, 0);
+       sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
        sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
                        WLAN_STA_ASSOC_REQ_OK);
        wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
@@ -1376,8 +2069,8 @@ static void handle_beacon(struct hostapd_data *hapd,
        struct ieee802_11_elems elems;
 
        if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
-               printf("handle_beacon - too short payload (len=%lu)\n",
-                      (unsigned long) len);
+               wpa_printf(MSG_INFO, "handle_beacon - too short payload (len=%lu)",
+                          (unsigned long) len);
                return;
        }
 
@@ -1392,9 +2085,9 @@ static void handle_beacon(struct hostapd_data *hapd,
 
 #ifdef CONFIG_IEEE80211W
 
-static void hostapd_sa_query_action(struct hostapd_data *hapd,
-                                   const struct ieee80211_mgmt *mgmt,
-                                   size_t len)
+static int hostapd_sa_query_action(struct hostapd_data *hapd,
+                                  const struct ieee80211_mgmt *mgmt,
+                                  size_t len)
 {
        const u8 *end;
 
@@ -1403,12 +2096,13 @@ static void hostapd_sa_query_action(struct hostapd_data *hapd,
        if (((u8 *) mgmt) + len < end) {
                wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
                           "frame (len=%lu)", (unsigned long) len);
-               return;
+               return 0;
        }
 
        ieee802_11_sa_query_action(hapd, mgmt->sa,
                                   mgmt->u.action.u.sa_query_resp.action,
                                   mgmt->u.action.u.sa_query_resp.trans_id);
+       return 1;
 }
 
 
@@ -1420,29 +2114,8 @@ static int robust_action_frame(u8 category)
 #endif /* CONFIG_IEEE80211W */
 
 
-#ifdef CONFIG_WNM
-static void hostapd_wnm_action(struct hostapd_data *hapd, struct sta_info *sta,
-                              const struct ieee80211_mgmt *mgmt,
-                              size_t len)
-{
-       struct rx_action action;
-       if (len < IEEE80211_HDRLEN + 2)
-               return;
-       os_memset(&action, 0, sizeof(action));
-       action.da = mgmt->da;
-       action.sa = mgmt->sa;
-       action.bssid = mgmt->bssid;
-       action.category = mgmt->u.action.category;
-       action.data = (const u8 *) &mgmt->u.action.u.wnm_sleep_req.action;
-       action.len = len - IEEE80211_HDRLEN - 1;
-       action.freq = hapd->iface->freq;
-       ieee802_11_rx_wnm_action_ap(hapd, &action);
-}
-#endif /* CONFIG_WNM */
-
-
-static void handle_action(struct hostapd_data *hapd,
-                         const struct ieee80211_mgmt *mgmt, size_t len)
+static int handle_action(struct hostapd_data *hapd,
+                        const struct ieee80211_mgmt *mgmt, size_t len)
 {
        struct sta_info *sta;
        sta = ap_get_sta(hapd, mgmt->sa);
@@ -1452,7 +2125,7 @@ static void handle_action(struct hostapd_data *hapd,
                               HOSTAPD_LEVEL_DEBUG,
                               "handle_action - too short payload (len=%lu)",
                               (unsigned long) len);
-               return;
+               return 0;
        }
 
        if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
@@ -1460,56 +2133,102 @@ static void handle_action(struct hostapd_data *hapd,
                wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored Action "
                           "frame (category=%u) from unassociated STA " MACSTR,
                           MAC2STR(mgmt->sa), mgmt->u.action.category);
-               return;
+               return 0;
        }
 
 #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))) {
+           !(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;
+               return 0;
        }
 #endif /* CONFIG_IEEE80211W */
 
+       if (sta) {
+               u16 fc = le_to_host16(mgmt->frame_control);
+               u16 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
+
+               if ((fc & WLAN_FC_RETRY) &&
+                   sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
+                   sta->last_seq_ctrl == seq_ctrl &&
+                   sta->last_subtype == WLAN_FC_STYPE_ACTION) {
+                       hostapd_logger(hapd, sta->addr,
+                                      HOSTAPD_MODULE_IEEE80211,
+                                      HOSTAPD_LEVEL_DEBUG,
+                                      "Drop repeated action frame seq_ctrl=0x%x",
+                                      seq_ctrl);
+                       return 1;
+               }
+
+               sta->last_seq_ctrl = seq_ctrl;
+               sta->last_subtype = WLAN_FC_STYPE_ACTION;
+       }
+
        switch (mgmt->u.action.category) {
 #ifdef CONFIG_IEEE80211R
        case WLAN_ACTION_FT:
-               if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
+               if (!sta ||
+                   wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
                                     len - IEEE80211_HDRLEN))
                        break;
-               return;
+               return 1;
 #endif /* CONFIG_IEEE80211R */
        case WLAN_ACTION_WMM:
                hostapd_wmm_action(hapd, mgmt, len);
-               return;
+               return 1;
 #ifdef CONFIG_IEEE80211W
        case WLAN_ACTION_SA_QUERY:
-               hostapd_sa_query_action(hapd, mgmt, len);
-               return;
+               return hostapd_sa_query_action(hapd, mgmt, len);
 #endif /* CONFIG_IEEE80211W */
 #ifdef CONFIG_WNM
        case WLAN_ACTION_WNM:
-               hostapd_wnm_action(hapd, sta, mgmt, len);
-               return;
+               ieee802_11_rx_wnm_action_ap(hapd, mgmt, len);
+               return 1;
 #endif /* CONFIG_WNM */
+#ifdef CONFIG_FST
+       case WLAN_ACTION_FST:
+               if (hapd->iface->fst)
+                       fst_rx_action(hapd->iface->fst, mgmt, len);
+               else
+                       wpa_printf(MSG_DEBUG,
+                                  "FST: Ignore FST Action frame - no FST attached");
+               return 1;
+#endif /* CONFIG_FST */
        case WLAN_ACTION_PUBLIC:
+       case WLAN_ACTION_PROTECTED_DUAL:
+#ifdef CONFIG_IEEE80211N
+               if (len >= IEEE80211_HDRLEN + 2 &&
+                   mgmt->u.action.u.public_action.action ==
+                   WLAN_PA_20_40_BSS_COEX) {
+                       wpa_printf(MSG_DEBUG,
+                                  "HT20/40 coex mgmt frame received from STA "
+                                  MACSTR, MAC2STR(mgmt->sa));
+                       hostapd_2040_coex_action(hapd, mgmt, len);
+               }
+#endif /* CONFIG_IEEE80211N */
                if (hapd->public_action_cb) {
                        hapd->public_action_cb(hapd->public_action_cb_ctx,
                                               (u8 *) mgmt, len,
                                               hapd->iface->freq);
-                       return;
                }
+               if (hapd->public_action_cb2) {
+                       hapd->public_action_cb2(hapd->public_action_cb2_ctx,
+                                               (u8 *) mgmt, len,
+                                               hapd->iface->freq);
+               }
+               if (hapd->public_action_cb || hapd->public_action_cb2)
+                       return 1;
                break;
        case WLAN_ACTION_VENDOR_SPECIFIC:
                if (hapd->vendor_action_cb) {
                        if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
                                                   (u8 *) mgmt, len,
                                                   hapd->iface->freq) == 0)
-                               return;
+                               return 1;
                }
                break;
        }
@@ -1532,7 +2251,7 @@ static void handle_action(struct hostapd_data *hapd,
                           "frame back to sender");
                resp = os_malloc(len);
                if (resp == NULL)
-                       return;
+                       return 0;
                os_memcpy(resp, mgmt, len);
                os_memcpy(resp->da, resp->sa, ETH_ALEN);
                os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
@@ -1545,6 +2264,8 @@ static void handle_action(struct hostapd_data *hapd,
                }
                os_free(resp);
        }
+
+       return 1;
 }
 
 
@@ -1561,15 +2282,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, const u8 *buf, size_t len,
-                    struct hostapd_frame_info *fi)
+int ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
+                   struct hostapd_frame_info *fi)
 {
        struct ieee80211_mgmt *mgmt;
        int broadcast;
        u16 fc, stype;
+       int ret = 0;
 
        if (len < 24)
-               return;
+               return 0;
 
        mgmt = (struct ieee80211_mgmt *) buf;
        fc = le_to_host16(mgmt->frame_control);
@@ -1577,7 +2299,7 @@ void ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
 
        if (stype == WLAN_FC_STYPE_BEACON) {
                handle_beacon(hapd, mgmt, len, fi);
-               return;
+               return 1;
        }
 
        broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
@@ -1590,16 +2312,19 @@ void ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
            !((hapd->conf->p2p & P2P_GROUP_OWNER) &&
              stype == WLAN_FC_STYPE_ACTION) &&
 #endif /* CONFIG_P2P */
+#ifdef CONFIG_MESH
+           !(hapd->conf->mesh & MESH_ENABLED) &&
+#endif /* CONFIG_MESH */
            os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
-               printf("MGMT: BSSID=" MACSTR " not our address\n",
-                      MAC2STR(mgmt->bssid));
-               return;
+               wpa_printf(MSG_INFO, "MGMT: BSSID=" MACSTR " not our address",
+                          MAC2STR(mgmt->bssid));
+               return 0;
        }
 
 
        if (stype == WLAN_FC_STYPE_PROBE_REQ) {
                handle_probe_req(hapd, mgmt, len, fi->ssi_signal);
-               return;
+               return 1;
        }
 
        if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
@@ -1607,33 +2332,41 @@ void ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
                               HOSTAPD_LEVEL_DEBUG,
                               "MGMT: DA=" MACSTR " not our address",
                               MAC2STR(mgmt->da));
-               return;
+               return 0;
        }
 
+       if (hapd->iconf->track_sta_max_num)
+               sta_track_add(hapd->iface, mgmt->sa);
+
        switch (stype) {
        case WLAN_FC_STYPE_AUTH:
                wpa_printf(MSG_DEBUG, "mgmt::auth");
                handle_auth(hapd, mgmt, len);
+               ret = 1;
                break;
        case WLAN_FC_STYPE_ASSOC_REQ:
                wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
                handle_assoc(hapd, mgmt, len, 0);
+               ret = 1;
                break;
        case WLAN_FC_STYPE_REASSOC_REQ:
                wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
                handle_assoc(hapd, mgmt, len, 1);
+               ret = 1;
                break;
        case WLAN_FC_STYPE_DISASSOC:
                wpa_printf(MSG_DEBUG, "mgmt::disassoc");
                handle_disassoc(hapd, mgmt, len);
+               ret = 1;
                break;
        case WLAN_FC_STYPE_DEAUTH:
                wpa_msg(hapd->msg_ctx, MSG_DEBUG, "mgmt::deauth");
                handle_deauth(hapd, mgmt, len);
+               ret = 1;
                break;
        case WLAN_FC_STYPE_ACTION:
                wpa_printf(MSG_DEBUG, "mgmt::action");
-               handle_action(hapd, mgmt, len);
+               ret = handle_action(hapd, mgmt, len);
                break;
        default:
                hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
@@ -1641,6 +2374,8 @@ void ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
                               "unknown mgmt frame subtype %d", stype);
                break;
        }
+
+       return ret;
 }
 
 
@@ -1659,8 +2394,8 @@ static void handle_auth_cb(struct hostapd_data *hapd,
        }
 
        if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
-               printf("handle_auth_cb - too short payload (len=%lu)\n",
-                      (unsigned long) len);
+               wpa_printf(MSG_INFO, "handle_auth_cb - too short payload (len=%lu)",
+                          (unsigned long) len);
                return;
        }
 
@@ -1670,8 +2405,8 @@ static void handle_auth_cb(struct hostapd_data *hapd,
 
        sta = ap_get_sta(hapd, mgmt->da);
        if (!sta) {
-               printf("handle_auth_cb: STA " MACSTR " not found\n",
-                      MAC2STR(mgmt->da));
+               wpa_printf(MSG_INFO, "handle_auth_cb: STA " MACSTR " not found",
+                          MAC2STR(mgmt->da));
                return;
        }
 
@@ -1685,6 +2420,30 @@ static void handle_auth_cb(struct hostapd_data *hapd,
 }
 
 
+static void hostapd_set_wds_encryption(struct hostapd_data *hapd,
+                                      struct sta_info *sta,
+                                      char *ifname_wds)
+{
+       int i;
+       struct hostapd_ssid *ssid = &hapd->conf->ssid;
+
+       if (hapd->conf->ieee802_1x || hapd->conf->wpa)
+               return;
+
+       for (i = 0; i < 4; i++) {
+               if (ssid->wep.key[i] &&
+                   hostapd_drv_set_key(ifname_wds, hapd, WPA_ALG_WEP, NULL, i,
+                                       i == ssid->wep.idx, NULL, 0,
+                                       ssid->wep.key[i], ssid->wep.len[i])) {
+                       wpa_printf(MSG_WARNING,
+                                  "Could not set WEP keys for WDS interface; %s",
+                                  ifname_wds);
+                       break;
+               }
+       }
+}
+
+
 static void handle_assoc_cb(struct hostapd_data *hapd,
                            const struct ieee80211_mgmt *mgmt,
                            size_t len, int reassoc, int ok)
@@ -1697,15 +2456,15 @@ static void handle_assoc_cb(struct hostapd_data *hapd,
 
        if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
                                      sizeof(mgmt->u.assoc_resp))) {
-               printf("handle_assoc_cb(reassoc=%d) - too short payload "
-                      "(len=%lu)\n", reassoc, (unsigned long) len);
+               wpa_printf(MSG_INFO, "handle_assoc_cb(reassoc=%d) - too short payload (len=%lu)",
+                          reassoc, (unsigned long) len);
                return;
        }
 
        sta = ap_get_sta(hapd, mgmt->da);
        if (!sta) {
-               printf("handle_assoc_cb: STA " MACSTR " not found\n",
-                      MAC2STR(mgmt->da));
+               wpa_printf(MSG_INFO, "handle_assoc_cb: STA " MACSTR " not found",
+                          MAC2STR(mgmt->da));
                return;
        }
 
@@ -1723,7 +2482,7 @@ static void handle_assoc_cb(struct hostapd_data *hapd,
                status = le_to_host16(mgmt->u.assoc_resp.status_code);
 
        if (status != WLAN_STATUS_SUCCESS)
-               goto fail;
+               return;
 
        /* Stop previous accounting session, if one is started, and allocate
         * new session id for the new session. */
@@ -1737,7 +2496,8 @@ 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) ||
+       sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
+       if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) ||
            sta->auth_alg == WLAN_AUTH_FT) {
                /*
                 * Open, static WEP, or FT protocol; no separate authorization
@@ -1776,7 +2536,7 @@ static void handle_assoc_cb(struct hostapd_data *hapd,
                            sta->listen_interval,
                            sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
                            sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
-                           sta->flags, sta->qosinfo)) {
+                           sta->flags, sta->qosinfo, sta->vht_opmode)) {
                hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
                               HOSTAPD_LEVEL_NOTICE,
                               "Could not add STA to kernel driver");
@@ -1784,11 +2544,18 @@ static void handle_assoc_cb(struct hostapd_data *hapd,
                ap_sta_disconnect(hapd, sta, sta->addr,
                                  WLAN_REASON_DISASSOC_AP_BUSY);
 
-               goto fail;
+               return;
        }
 
-       if (sta->flags & WLAN_STA_WDS)
-               hostapd_set_wds_sta(hapd, sta->addr, sta->aid, 1);
+       if (sta->flags & WLAN_STA_WDS) {
+               int ret;
+               char ifname_wds[IFNAMSIZ + 1];
+
+               ret = hostapd_set_wds_sta(hapd, ifname_wds, sta->addr,
+                                         sta->aid, 1);
+               if (!ret)
+                       hostapd_set_wds_encryption(hapd, sta, ifname_wds);
+       }
 
        if (sta->eapol_sm == NULL) {
                /*
@@ -1796,12 +2563,12 @@ static void handle_assoc_cb(struct hostapd_data *hapd,
                 * so bind it to the selected VLAN interface now, since the
                 * interface selection is not going to change anymore.
                 */
-               if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
-                       goto fail;
+               if (ap_sta_bind_vlan(hapd, sta) < 0)
+                       return;
        } else if (sta->vlan_id) {
                /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
-               if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
-                       goto fail;
+               if (ap_sta_bind_vlan(hapd, sta) < 0)
+                       return;
        }
 
        hostapd_set_sta_flags(hapd, sta);
@@ -1813,13 +2580,6 @@ static void handle_assoc_cb(struct hostapd_data *hapd,
        hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
 
        ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
-
- fail:
-       /* Copy of the association request is not needed anymore */
-       if (sta->last_assoc_req) {
-               os_free(sta->last_assoc_req);
-               sta->last_assoc_req = NULL;
-       }
 }
 
 
@@ -1886,6 +2646,14 @@ void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
        const struct ieee80211_mgmt *mgmt;
        mgmt = (const struct ieee80211_mgmt *) buf;
 
+#ifdef CONFIG_TESTING_OPTIONS
+       if (hapd->ext_mgmt_frame_handling) {
+               wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-TX-STATUS stype=%u ok=%d",
+                       stype, ok);
+               return;
+       }
+#endif /* CONFIG_TESTING_OPTIONS */
+
        switch (stype) {
        case WLAN_FC_STYPE_AUTH:
                wpa_printf(MSG_DEBUG, "mgmt::auth cb");
@@ -1914,7 +2682,7 @@ void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
                wpa_printf(MSG_DEBUG, "mgmt::action cb");
                break;
        default:
-               printf("unknown mgmt cb frame subtype %d\n", stype);
+               wpa_printf(MSG_INFO, "unknown mgmt cb frame subtype %d", stype);
                break;
        }
 }
@@ -2029,11 +2797,18 @@ void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
                        return;
 
                if (wds && !(sta->flags & WLAN_STA_WDS)) {
+                       int ret;
+                       char ifname_wds[IFNAMSIZ + 1];
+
                        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);
+                       ret = hostapd_set_wds_sta(hapd, ifname_wds,
+                                                 sta->addr, sta->aid, 1);
+                       if (!ret)
+                               hostapd_set_wds_encryption(hapd, sta,
+                                                          ifname_wds);
                }
                return;
        }