Fix STA flag setting for auto-authorization if 802.1X/WPA is not used
[wpasupplicant] / hostapd / ieee802_11.c
index 7358c3d..584cd31 100644 (file)
@@ -34,8 +34,7 @@
 #include "wme.h"
 #include "ap_list.h"
 #include "accounting.h"
-#include "driver.h"
-#include "ieee802_11h.h"
+#include "driver_i.h"
 #include "mlme.h"
 
 
@@ -148,7 +147,14 @@ u8 * hostapd_eid_ht_operation(struct hostapd_data *hapd, u8 *eid)
        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);
 
@@ -283,105 +289,30 @@ 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;
 
-       if (hapd->iface->dfs_enable) 
-               capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
-
        return capab;
 }
 
 
-#define OUI_MICROSOFT 0x0050f2 /* Microsoft (also used in Wi-Fi specs)
-                               * 00:50:F2 */
-
-static int ieee802_11_parse_vendor_specific(struct hostapd_data *hapd,
-                                           u8 *pos, size_t elen,
-                                           struct ieee802_11_elems *elems,
-                                           int show_errors)
-{
-       unsigned int oui;
-
-       /* first 3 bytes in vendor specific information element are the IEEE
-        * OUI of the vendor. The following byte is used a vendor specific
-        * sub-type. */
-       if (elen < 4) {
-               if (show_errors) {
-                       wpa_printf(MSG_MSGDUMP, "short vendor specific "
-                                  "information element ignored (len=%lu)",
-                                  (unsigned long) elen);
-               }
-               return -1;
-       }
-
-       oui = WPA_GET_BE24(pos);
-       switch (oui) {
-       case OUI_MICROSOFT:
-               /* Microsoft/Wi-Fi information elements are further typed and
-                * subtyped */
-               switch (pos[3]) {
-               case 1:
-                       /* Microsoft OUI (00:50:F2) with OUI Type 1:
-                        * real WPA information element */
-                       elems->wpa_ie = pos;
-                       elems->wpa_ie_len = elen;
-                       break;
-               case WME_OUI_TYPE: /* this is a Wi-Fi WME info. element */
-                       if (elen < 5) {
-                               wpa_printf(MSG_MSGDUMP, "short WME "
-                                          "information element ignored "
-                                          "(len=%lu)",
-                                          (unsigned long) elen);
-                               return -1;
-                       }
-                       switch (pos[4]) {
-                       case WME_OUI_SUBTYPE_INFORMATION_ELEMENT:
-                       case WME_OUI_SUBTYPE_PARAMETER_ELEMENT:
-                               elems->wme = pos;
-                               elems->wme_len = elen;
-                               break;
-                       case WME_OUI_SUBTYPE_TSPEC_ELEMENT:
-                               elems->wme_tspec = pos;
-                               elems->wme_tspec_len = elen;
-                               break;
-                       default:
-                               wpa_printf(MSG_MSGDUMP, "unknown WME "
-                                          "information element ignored "
-                                          "(subtype=%d len=%lu)",
-                                          pos[4], (unsigned long) elen);
-                               return -1;
-                       }
-                       break;
-               default:
-                       wpa_printf(MSG_MSGDUMP, "Unknown Microsoft "
-                                  "information element ignored "
-                                  "(type=%d len=%lu)\n",
-                                  pos[3], (unsigned long) elen);
-                       return -1;
-               }
-               break;
-
-       default:
-               wpa_printf(MSG_MSGDUMP, "unknown vendor specific information "
-                          "element ignored (vendor OUI %02x:%02x:%02x "
-                          "len=%lu)",
-                          pos[0], pos[1], pos[2], (unsigned long) elen);
-               return -1;
-       }
-
-       return 0;
-}
-
-
 #ifdef CONFIG_IEEE80211W
 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;
 
@@ -390,131 +321,6 @@ static u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
 #endif /* CONFIG_IEEE80211W */
 
 
-ParseRes ieee802_11_parse_elems(struct hostapd_data *hapd, u8 *start,
-                               size_t len,
-                               struct ieee802_11_elems *elems,
-                               int show_errors)
-{
-       size_t left = len;
-       u8 *pos = start;
-       int unknown = 0;
-
-       os_memset(elems, 0, sizeof(*elems));
-
-       while (left >= 2) {
-               u8 id, elen;
-
-               id = *pos++;
-               elen = *pos++;
-               left -= 2;
-
-               if (elen > left) {
-                       if (show_errors) {
-                               wpa_printf(MSG_DEBUG, "IEEE 802.11 element "
-                                          "parse failed (id=%d elen=%d "
-                                          "left=%lu)",
-                                          id, elen, (unsigned long) left);
-                               wpa_hexdump(MSG_MSGDUMP, "IEs", start, len);
-                       }
-                       return ParseFailed;
-               }
-
-               switch (id) {
-               case WLAN_EID_SSID:
-                       elems->ssid = pos;
-                       elems->ssid_len = elen;
-                       break;
-               case WLAN_EID_SUPP_RATES:
-                       elems->supp_rates = pos;
-                       elems->supp_rates_len = elen;
-                       break;
-               case WLAN_EID_FH_PARAMS:
-                       elems->fh_params = pos;
-                       elems->fh_params_len = elen;
-                       break;
-               case WLAN_EID_DS_PARAMS:
-                       elems->ds_params = pos;
-                       elems->ds_params_len = elen;
-                       break;
-               case WLAN_EID_CF_PARAMS:
-                       elems->cf_params = pos;
-                       elems->cf_params_len = elen;
-                       break;
-               case WLAN_EID_TIM:
-                       elems->tim = pos;
-                       elems->tim_len = elen;
-                       break;
-               case WLAN_EID_IBSS_PARAMS:
-                       elems->ibss_params = pos;
-                       elems->ibss_params_len = elen;
-                       break;
-               case WLAN_EID_CHALLENGE:
-                       elems->challenge = pos;
-                       elems->challenge_len = elen;
-                       break;
-               case WLAN_EID_ERP_INFO:
-                       elems->erp_info = pos;
-                       elems->erp_info_len = elen;
-                       break;
-               case WLAN_EID_EXT_SUPP_RATES:
-                       elems->ext_supp_rates = pos;
-                       elems->ext_supp_rates_len = elen;
-                       break;
-               case WLAN_EID_VENDOR_SPECIFIC:
-                       if (ieee802_11_parse_vendor_specific(hapd, pos, elen,
-                                                            elems,
-                                                            show_errors))
-                               unknown++;
-                       break;
-               case WLAN_EID_RSN:
-                       elems->rsn_ie = pos;
-                       elems->rsn_ie_len = elen;
-                       break;
-               case WLAN_EID_PWR_CAPABILITY:
-                       elems->power_cap = pos;
-                       elems->power_cap_len = elen;
-                       break;
-               case WLAN_EID_SUPPORTED_CHANNELS:
-                       elems->supp_channels = pos;
-                       elems->supp_channels_len = elen;
-                       break;
-               case WLAN_EID_MOBILITY_DOMAIN:
-                       elems->mdie = pos;
-                       elems->mdie_len = elen;
-                       break;
-               case WLAN_EID_FAST_BSS_TRANSITION:
-                       elems->ftie = pos;
-                       elems->ftie_len = elen;
-                       break;
-               case WLAN_EID_HT_CAP:
-                       elems->ht_capabilities = pos;
-                       elems->ht_capabilities_len = elen;
-                       break;
-               case WLAN_EID_HT_OPERATION:
-                       elems->ht_operation = pos;
-                       elems->ht_operation_len = elen;
-                       break;
-               default:
-                       unknown++;
-                       if (!show_errors)
-                               break;
-                       wpa_printf(MSG_MSGDUMP, "IEEE 802.11 element parse "
-                                  "ignored unknown element (id=%d elen=%d)",
-                                  id, elen);
-                       break;
-               }
-
-               left -= elen;
-               pos += elen;
-       }
-
-       if (left)
-               return ParseFailed;
-
-       return unknown ? ParseUnknown : ParseOK;
-}
-
-
 void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
 {
        int i;
@@ -530,6 +336,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;
@@ -545,93 +357,11 @@ 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");
 }
 
 
-static void ieee802_11_sta_authenticate(void *eloop_ctx, void *timeout_ctx)
-{
-       struct hostapd_data *hapd = eloop_ctx;
-       struct ieee80211_mgmt mgmt;
-       char ssid_txt[33];
-
-       if (hapd->assoc_ap_state == WAIT_BEACON)
-               hapd->assoc_ap_state = AUTHENTICATE;
-       if (hapd->assoc_ap_state != AUTHENTICATE)
-               return;
-
-       ieee802_11_print_ssid(ssid_txt, (u8 *) hapd->assoc_ap_ssid,
-                             hapd->assoc_ap_ssid_len);
-       printf("Authenticate with AP " MACSTR " SSID=%s (as station)\n",
-              MAC2STR(hapd->conf->assoc_ap_addr), ssid_txt);
-
-       os_memset(&mgmt, 0, sizeof(mgmt));
-       mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
-                                         WLAN_FC_STYPE_AUTH);
-       /* Request TX callback */
-       mgmt.frame_control |= host_to_le16(BIT(1));
-       os_memcpy(mgmt.da, hapd->conf->assoc_ap_addr, ETH_ALEN);
-       os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
-       os_memcpy(mgmt.bssid, hapd->conf->assoc_ap_addr, ETH_ALEN);
-       mgmt.u.auth.auth_alg = host_to_le16(WLAN_AUTH_OPEN);
-       mgmt.u.auth.auth_transaction = host_to_le16(1);
-       mgmt.u.auth.status_code = host_to_le16(0);
-       if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
-                                   sizeof(mgmt.u.auth), 0) < 0)
-               perror("ieee802_11_sta_authenticate: send");
-
-       /* Try to authenticate again, if this attempt fails or times out. */
-       eloop_register_timeout(5, 0, ieee802_11_sta_authenticate, hapd, NULL);
-}
-
-
-static void ieee802_11_sta_associate(void *eloop_ctx, void *timeout_ctx)
-{
-       struct hostapd_data *hapd = eloop_ctx;
-       u8 buf[256];
-       struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *) buf;
-       u8 *p;
-       char ssid_txt[33];
-
-       if (hapd->assoc_ap_state == AUTHENTICATE)
-               hapd->assoc_ap_state = ASSOCIATE;
-       if (hapd->assoc_ap_state != ASSOCIATE)
-               return;
-
-       ieee802_11_print_ssid(ssid_txt, (u8 *) hapd->assoc_ap_ssid,
-                             hapd->assoc_ap_ssid_len);
-       printf("Associate with AP " MACSTR " SSID=%s (as station)\n",
-              MAC2STR(hapd->conf->assoc_ap_addr), ssid_txt);
-
-       os_memset(mgmt, 0, sizeof(*mgmt));
-       mgmt->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
-                                         WLAN_FC_STYPE_ASSOC_REQ);
-       /* Request TX callback */
-       mgmt->frame_control |= host_to_le16(BIT(1));
-       os_memcpy(mgmt->da, hapd->conf->assoc_ap_addr, ETH_ALEN);
-       os_memcpy(mgmt->sa, hapd->own_addr, ETH_ALEN);
-       os_memcpy(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN);
-       mgmt->u.assoc_req.capab_info = host_to_le16(0);
-       mgmt->u.assoc_req.listen_interval = host_to_le16(1);
-       p = &mgmt->u.assoc_req.variable[0];
-
-       *p++ = WLAN_EID_SSID;
-       *p++ = hapd->assoc_ap_ssid_len;
-       os_memcpy(p, hapd->assoc_ap_ssid, hapd->assoc_ap_ssid_len);
-       p += hapd->assoc_ap_ssid_len;
-
-       p = hostapd_eid_supp_rates(hapd, p);
-       p = hostapd_eid_ext_supp_rates(hapd, p);
-
-       if (hostapd_send_mgmt_frame(hapd, mgmt, p - (u8 *) mgmt, 0) < 0)
-               perror("ieee802_11_sta_associate: send");
-
-       /* Try to authenticate again, if this attempt fails or times out. */
-       eloop_register_timeout(5, 0, ieee802_11_sta_associate, hapd, NULL);
-}
-
-
 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
                           u16 auth_transaction, u8 *challenge, int iswep)
 {
@@ -707,8 +437,6 @@ static void send_auth_reply(struct hostapd_data *hapd,
        reply = (struct ieee80211_mgmt *) buf;
        reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
                                            WLAN_FC_STYPE_AUTH);
-       /* Request TX callback */
-       reply->frame_control |= host_to_le16(BIT(1));
        os_memcpy(reply->da, dst, ETH_ALEN);
        os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
        os_memcpy(reply->bssid, bssid, ETH_ALEN);
@@ -724,7 +452,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);
@@ -794,22 +522,6 @@ static void handle_auth(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
                   status_code, !!(fc & WLAN_FC_ISWEP),
                   challenge ? " challenge" : "");
 
-       if (hapd->assoc_ap_state == AUTHENTICATE && auth_transaction == 2 &&
-           os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0 &&
-           os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
-               if (status_code != 0) {
-                       printf("Authentication (as station) with AP "
-                              MACSTR " failed (status_code=%d)\n",
-                              MAC2STR(hapd->conf->assoc_ap_addr),
-                              status_code);
-                       return;
-               }
-               printf("Authenticated (as station) with AP " MACSTR "\n",
-                      MAC2STR(hapd->conf->assoc_ap_addr));
-               ieee802_11_sta_associate(hapd, NULL);
-               return;
-       }
-
        if (hapd->tkip_countermeasures) {
                resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
                goto fail;
@@ -953,6 +665,39 @@ static void handle_auth(struct hostapd_data *hapd, struct ieee80211_mgmt *mgmt,
 }
 
 
+static int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
+{
+       int i, j = 32, aid;
+
+       /* get a unique AID */
+       if (sta->aid > 0) {
+               wpa_printf(MSG_DEBUG, "  old AID %d", sta->aid);
+               return 0;
+       }
+
+       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;
+               }
+               if (j < 32)
+                       break;
+       }
+       if (j == 32)
+               return -1;
+       aid = i * 32 + j + 1;
+       if (aid > 2007)
+               return -1;
+
+       sta->aid = aid;
+       hapd->sta_aid[i] |= BIT(j);
+       wpa_printf(MSG_DEBUG, "  new AID %d", sta->aid);
+       return 0;
+}
+
+
 static void handle_assoc(struct hostapd_data *hapd,
                         struct ieee80211_mgmt *mgmt, size_t len, int reassoc)
 {
@@ -963,7 +708,7 @@ static void handle_assoc(struct hostapd_data *hapd,
        int send_deauth = 0, send_len, left, i;
        struct sta_info *sta;
        struct ieee802_11_elems elems;
-       u8 buf[sizeof(struct ieee80211_mgmt) + 512];
+       u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
        struct ieee80211_mgmt *reply;
 
        if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
@@ -1031,11 +776,12 @@ static void handle_assoc(struct hostapd_data *hapd,
        }
 
        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(hapd, pos, left, &elems, 1) == ParseFailed
-           || !elems.ssid) {
+       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;
@@ -1053,16 +799,16 @@ static void handle_assoc(struct hostapd_data *hapd,
                goto fail;
        }
 
-       sta->flags &= ~WLAN_STA_WME;
-       if (elems.wme && hapd->conf->wme_enabled) {
-               if (hostapd_eid_wme_valid(hapd, elems.wme, elems.wme_len))
+       sta->flags &= ~WLAN_STA_WMM;
+       if (elems.wmm && hapd->conf->wmm_enabled) {
+               if (hostapd_eid_wmm_valid(hapd, elems.wmm, elems.wmm_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;
        }
 
        if (!elems.supp_rates) {
@@ -1109,9 +855,8 @@ static void handle_assoc(struct hostapd_data *hapd,
        /* 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_WME)) {
-               /* note: without WMM capability, treat the sta as non-HT */
+           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 =
@@ -1134,6 +879,25 @@ 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) {
+               if (elems.wps_ie) {
+                       wpa_printf(MSG_DEBUG, "STA included WPS IE in "
+                                  "(Re)Association Request - assume WPS is "
+                                  "used");
+                       sta->flags |= WLAN_STA_WPS;
+                       wpabuf_free(sta->wps_ie);
+                       sta->wps_ie = wpabuf_alloc_copy(elems.wps_ie + 4,
+                                                       elems.wps_ie_len - 4);
+               } else {
+                       wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE "
+                                  "in (Re)Association Request - possible WPS "
+                                  "use");
+                       sta->flags |= WLAN_STA_MAYBE_WPS;
+               }
+       } 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));
@@ -1166,9 +930,9 @@ 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;
@@ -1177,16 +941,20 @@ static void handle_assoc(struct hostapd_data *hapd,
                if (resp != WLAN_STATUS_SUCCESS)
                        goto fail;
 #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;
@@ -1214,21 +982,18 @@ static void handle_assoc(struct hostapd_data *hapd,
                                goto fail;
                }
 #endif /* CONFIG_IEEE80211R */
-       }
-
-       if (hapd->iface->dfs_enable &&
-           hapd->iconf->ieee80211h == SPECT_STRICT_BINDING) {
-               if (hostapd_check_power_cap(hapd, elems.power_cap,
-                                           elems.power_cap_len)) {
-                       resp = WLAN_STATUS_PWR_CAPABILITY_NOT_VALID;
-                       hostapd_logger(hapd, sta->addr,
-                                      HOSTAPD_MODULE_IEEE80211,
-                                      HOSTAPD_LEVEL_DEBUG,
-                                      "Power capabilities of the station not "
-                                      "acceptable");
+#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;
                }
-       }
+#endif /* CONFIG_IEEE80211N */
+       } else
+               wpa_auth_sta_no_wpa(sta->wpa_sm);
 
        if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
                sta->flags |= WLAN_STA_NONERP;
@@ -1271,24 +1036,35 @@ static void handle_assoc(struct hostapd_data *hapd,
 
 #ifdef CONFIG_IEEE80211N
        if (sta->flags & WLAN_STA_HT) {
-               if ((sta->ht_capabilities.data.capabilities_info &
-                    HT_CAP_INFO_GREEN_FIELD) == 0) {
-                       hapd->iface->num_sta_ht_no_gf++;
+               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 ((sta->ht_capabilities.data.capabilities_info &
-                    HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
-                       hapd->iface->num_sta_ht_20mhz++;
+               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 {
-               hapd->iface->num_sta_no_ht++;
+               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",
@@ -1301,22 +1077,10 @@ static void handle_assoc(struct hostapd_data *hapd,
                ieee802_11_set_beacons(hapd->iface);
 #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);
-               }
+       if (hostapd_get_aid(hapd, sta) < 0) {
+               resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
+               wpa_printf(MSG_ERROR, "  no room for more AIDs");
+               goto fail;
        }
 
        hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
@@ -1325,6 +1089,19 @@ static void handle_assoc(struct hostapd_data *hapd,
        /* 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);
@@ -1368,8 +1145,8 @@ static void handle_assoc(struct hostapd_data *hapd,
                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);
+               if (sta->flags & WLAN_STA_WMM)
+                       p = hostapd_eid_wmm(hapd, p);
 
                p = hostapd_eid_ht_capabilities_info(hapd, p);
                p = hostapd_eid_ht_operation(hapd, p);
@@ -1377,10 +1154,11 @@ static void handle_assoc(struct hostapd_data *hapd,
 #ifdef CONFIG_IEEE80211R
                if (resp == WLAN_STATUS_SUCCESS) {
                        /* IEEE 802.11r: Mobility Domain Information, Fast BSS
-                        * Transition Information, RSN */
+                        * Transition Information, RSN, [RIC Response] */
                        p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
                                                        buf + sizeof(buf) - p,
-                                                       sta->auth_alg);
+                                                       sta->auth_alg,
+                                                       pos, left);
                }
 #endif /* CONFIG_IEEE80211R */
 
@@ -1390,67 +1168,13 @@ static void handle_assoc(struct hostapd_data *hapd,
 #endif /* CONFIG_IEEE80211W */
 
                send_len += p - reply->u.assoc_resp.variable;
-
-               /* Request TX callback */
-               reply->frame_control |= host_to_le16(BIT(1));
        }
 
-       if (hostapd_send_mgmt_frame(hapd, reply, send_len, 0) < 0)
+       if (hostapd_send_mgmt_frame(hapd, reply, send_len) < 0)
                perror("handle_assoc: send");
 }
 
 
-static void handle_assoc_resp(struct hostapd_data *hapd,
-                             struct ieee80211_mgmt *mgmt, size_t len)
-{
-       u16 status_code, aid;
-
-       if (hapd->assoc_ap_state != ASSOCIATE) {
-               printf("Unexpected association response received from " MACSTR
-                      "\n", MAC2STR(mgmt->sa));
-               return;
-       }
-
-       if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp)) {
-               printf("handle_assoc_resp - too short payload (len=%lu)\n",
-                      (unsigned long) len);
-               return;
-       }
-
-       if (os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0 ||
-           os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0) {
-               printf("Received association response from unexpected address "
-                      "(SA=" MACSTR " BSSID=" MACSTR "\n",
-                      MAC2STR(mgmt->sa), MAC2STR(mgmt->bssid));
-               return;
-       }
-
-       status_code = le_to_host16(mgmt->u.assoc_resp.status_code);
-       aid = le_to_host16(mgmt->u.assoc_resp.aid);
-       aid &= ~(BIT(14) | BIT(15));
-
-       if (status_code != 0) {
-               printf("Association (as station) with AP " MACSTR " failed "
-                      "(status_code=%d)\n",
-                      MAC2STR(hapd->conf->assoc_ap_addr), status_code);
-               /* Try to authenticate again */
-               hapd->assoc_ap_state = AUTHENTICATE;
-               eloop_register_timeout(5, 0, ieee802_11_sta_authenticate,
-                                      hapd, NULL);
-       }
-
-       printf("Associated (as station) with AP " MACSTR " (aid=%d)\n",
-              MAC2STR(hapd->conf->assoc_ap_addr), aid);
-       hapd->assoc_ap_aid = aid;
-       hapd->assoc_ap_state = ASSOCIATED;
-
-       if (hostapd_set_assoc_ap(hapd, hapd->conf->assoc_ap_addr)) {
-               printf("Could not set associated AP address to kernel "
-                      "driver.\n");
-       }
-}
-
-
 static void handle_disassoc(struct hostapd_data *hapd,
                            struct ieee80211_mgmt *mgmt, size_t len)
 {
@@ -1466,19 +1190,6 @@ static void handle_disassoc(struct hostapd_data *hapd,
                   MAC2STR(mgmt->sa),
                   le_to_host16(mgmt->u.disassoc.reason_code));
 
-       if (hapd->assoc_ap_state != DO_NOT_ASSOC &&
-           os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
-               printf("Assoc AP " MACSTR " sent disassociation "
-                      "(reason_code=%d) - try to authenticate\n",
-                      MAC2STR(hapd->conf->assoc_ap_addr),
-                      le_to_host16(mgmt->u.disassoc.reason_code));
-               hapd->assoc_ap_state = AUTHENTICATE;
-               ieee802_11_sta_authenticate(hapd, NULL);
-               eloop_register_timeout(0, 500000, ieee802_11_sta_authenticate,
-                                      hapd, NULL);
-               return;
-       }
-
        sta = ap_get_sta(hapd, mgmt->sa);
        if (sta == NULL) {
                printf("Station " MACSTR " trying to disassociate, but it "
@@ -1527,18 +1238,6 @@ static void handle_deauth(struct hostapd_data *hapd,
                   MAC2STR(mgmt->sa),
                   le_to_host16(mgmt->u.deauth.reason_code));
 
-       if (hapd->assoc_ap_state != DO_NOT_ASSOC &&
-           os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
-               printf("Assoc AP " MACSTR " sent deauthentication "
-                      "(reason_code=%d) - try to authenticate\n",
-                      MAC2STR(hapd->conf->assoc_ap_addr),
-                      le_to_host16(mgmt->u.deauth.reason_code));
-               hapd->assoc_ap_state = AUTHENTICATE;
-               eloop_register_timeout(0, 500000, ieee802_11_sta_authenticate,
-                                      hapd, NULL);
-               return;
-       }
-
        sta = ap_get_sta(hapd, mgmt->sa);
        if (sta == NULL) {
                printf("Station " MACSTR " trying to deauthenticate, but it "
@@ -1570,72 +1269,106 @@ static void handle_beacon(struct hostapd_data *hapd,
                return;
        }
 
-       (void) ieee802_11_parse_elems(hapd, mgmt->u.beacon.variable,
+       (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
                                      len - (IEEE80211_HDRLEN +
                                             sizeof(mgmt->u.beacon)), &elems,
                                      0);
 
-       if (hapd->assoc_ap_state == WAIT_BEACON &&
-           os_memcmp(mgmt->sa, hapd->conf->assoc_ap_addr, ETH_ALEN) == 0) {
-               if (elems.ssid && elems.ssid_len <= 32) {
-                       os_memcpy(hapd->assoc_ap_ssid, elems.ssid,
-                                 elems.ssid_len);
-                       hapd->assoc_ap_ssid[elems.ssid_len] = '\0';
-                       hapd->assoc_ap_ssid_len = elems.ssid_len;
-               }
-               ieee802_11_sta_authenticate(hapd, NULL);
-       }
-
        ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
 }
 
 
 #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 */
 
@@ -1643,6 +1376,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,
@@ -1651,13 +1386,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,
@@ -1672,12 +1417,12 @@ static void handle_action(struct hostapd_data *hapd,
                return;
        }
 #endif /* CONFIG_IEEE80211R */
-       case WME_ACTION_CATEGORY:
-               hostapd_wme_action(hapd, mgmt, len);
+       case WLAN_ACTION_WMM:
+               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 */
        }
@@ -1701,7 +1446,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);
        }
 }
 
@@ -1713,6 +1458,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
@@ -1730,18 +1476,12 @@ 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;
 
        if (!broadcast &&
-           os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0 &&
-           (hapd->assoc_ap_state == DO_NOT_ASSOC ||
-            os_memcmp(mgmt->bssid, hapd->conf->assoc_ap_addr, ETH_ALEN) != 0))
-       {
+           os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
                printf("MGMT: BSSID=" MACSTR " not our address\n",
                       MAC2STR(mgmt->bssid));
                return;
@@ -1770,10 +1510,6 @@ void ieee802_11_mgmt(struct hostapd_data *hapd, u8 *buf, size_t len, u16 stype,
                wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
                handle_assoc(hapd, mgmt, len, 0);
                break;
-       case WLAN_FC_STYPE_ASSOC_RESP:
-               wpa_printf(MSG_DEBUG, "mgmt::assoc_resp");
-               handle_assoc_resp(hapd, mgmt, len);
-               break;
        case WLAN_FC_STYPE_REASSOC_REQ:
                wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
                handle_assoc(hapd, mgmt, len, 1);
@@ -1840,6 +1576,34 @@ static void handle_auth_cb(struct hostapd_data *hapd,
 }
 
 
+#ifdef CONFIG_IEEE80211N
+static void
+hostapd_get_ht_capab(struct hostapd_data *hapd,
+                    struct ht_cap_ie *ht_cap_ie,
+                    struct ht_cap_ie *neg_ht_cap_ie)
+{
+
+       os_memcpy(neg_ht_cap_ie, ht_cap_ie, sizeof(struct ht_cap_ie));
+       neg_ht_cap_ie->data.capabilities_info =
+               ht_cap_ie->data.capabilities_info & hapd->iconf->ht_capab;
+
+       neg_ht_cap_ie->data.capabilities_info &= ~HT_CAP_INFO_SMPS_DISABLED;
+       if ((ht_cap_ie->data.capabilities_info & HT_CAP_INFO_SMPS_DISABLED) ==
+           (hapd->iconf->ht_capab & HT_CAP_INFO_SMPS_DISABLED))
+               neg_ht_cap_ie->data.capabilities_info |=
+                       hapd->iconf->ht_capab & HT_CAP_INFO_SMPS_DISABLED;
+       else
+               neg_ht_cap_ie->data.capabilities_info |=
+                       HT_CAP_INFO_SMPS_DISABLED;
+
+       /* FIXME: Rx STBC needs to be handled specially */
+       neg_ht_cap_ie->data.capabilities_info &= ~HT_CAP_INFO_RX_STBC_MASK;
+       neg_ht_cap_ie->data.capabilities_info |=
+               hapd->iconf->ht_capab & HT_CAP_INFO_RX_STBC_MASK;
+}
+#endif /* CONFIG_IEEE80211N */
+
+
 static void handle_assoc_cb(struct hostapd_data *hapd,
                            struct ieee80211_mgmt *mgmt,
                            size_t len, int reassoc, int ok)
@@ -1847,7 +1611,11 @@ 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;
+#ifdef CONFIG_IEEE80211N
+       struct ht_cap_ie ht_cap;
+#endif /* CONFIG_IEEE80211N */
+       struct ht_cap_ie *ht_cap_ptr = NULL;
+       int set_flags, flags_and, flags_or;
 
        if (!ok) {
                hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
@@ -1881,17 +1649,19 @@ static void handle_assoc_cb(struct hostapd_data *hapd,
        /* Stop previous accounting session, if one is started, and allocate
         * new session id for the new session. */
        accounting_sta_stop(hapd, sta);
-       accounting_sta_get_id(hapd, sta);
 
        hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
                       HOSTAPD_LEVEL_INFO,
-                      "associated (aid %d, accounting session %08X-%08X)",
-                      sta->aid, sta->acct_session_id_hi,
-                      sta->acct_session_id_lo);
+                      "associated (aid %d)",
+                      sta->aid);
 
        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;
+       }
 
        if (reassoc)
                mlme_reassociate_indication(hapd, sta);
@@ -1899,18 +1669,20 @@ static void handle_assoc_cb(struct hostapd_data *hapd,
                mlme_associate_indication(hapd, sta);
 
 #ifdef CONFIG_IEEE80211N
-       if (sta->flags & WLAN_STA_HT)
-               ht_cap = &sta->ht_capabilities;
+       if (sta->flags & WLAN_STA_HT) {
+               ht_cap_ptr = &ht_cap;
+               hostapd_get_ht_capab(hapd, &sta->ht_capabilities, ht_cap_ptr);
+       }
 #endif /* CONFIG_IEEE80211N */
 
 #ifdef CONFIG_IEEE80211W
-       sta->ping_timed_out = 0;
+       sta->sa_query_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))
+                           ht_cap_ptr))
        {
                hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
                               HOSTAPD_LEVEL_NOTICE,
@@ -1928,13 +1700,15 @@ 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);
-       }
+
+       set_flags = WLAN_STA_SHORT_PREAMBLE | WLAN_STA_WMM | WLAN_STA_MFP;
+       if (!hapd->conf->ieee802_1x && !hapd->conf->wpa &&
+           sta->flags & WLAN_STA_AUTHORIZED)
+               set_flags |= WLAN_STA_AUTHORIZED;
+       flags_or = sta->flags & set_flags;
+       flags_and = sta->flags | ~set_flags;
+       hostapd_sta_set_flags(hapd, sta->addr, sta->flags,
+                             flags_or, flags_and);
 
        if (sta->auth_alg == WLAN_AUTH_FT)
                wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
@@ -1953,6 +1727,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)
 {
@@ -1987,76 +1770,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 */