Use SA Query procedure to recovery from AP/STA state mismatch
[mech_eap.git] / wpa_supplicant / sme.c
index 23ffe80..018b372 100644 (file)
@@ -15,6 +15,7 @@
 #include "includes.h"
 
 #include "common.h"
+#include "utils/eloop.h"
 #include "common/ieee802_11_defs.h"
 #include "common/ieee802_11_common.h"
 #include "eapol_supp/eapol_supp_sm.h"
@@ -295,8 +296,10 @@ void sme_event_auth(struct wpa_supplicant *wpa_s, union wpa_event_data *data)
                if (data->auth.status_code !=
                    WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG ||
                    wpa_s->sme.auth_alg == data->auth.auth_type ||
-                   wpa_s->current_ssid->auth_alg == WPA_AUTH_ALG_LEAP)
+                   wpa_s->current_ssid->auth_alg == WPA_AUTH_ALG_LEAP) {
+                       wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
                        return;
+               }
 
                switch (data->auth.auth_type) {
                case WLAN_AUTH_OPEN:
@@ -350,6 +353,8 @@ void sme_associate(struct wpa_supplicant *wpa_s, enum wpas_mode mode,
        params.wpa_ie = wpa_s->sme.assoc_req_ie_len ?
                wpa_s->sme.assoc_req_ie : NULL;
        params.wpa_ie_len = wpa_s->sme.assoc_req_ie_len;
+       params.pairwise_suite = cipher_suite2driver(wpa_s->pairwise_cipher);
+       params.group_suite = cipher_suite2driver(wpa_s->group_cipher);
 #ifdef CONFIG_IEEE80211R
        if (auth_type == WLAN_AUTH_FT && wpa_s->sme.ft_ies) {
                params.wpa_ie = wpa_s->sme.ft_ies;
@@ -386,10 +391,16 @@ void sme_associate(struct wpa_supplicant *wpa_s, enum wpas_mode mode,
            (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
                params.p2p = 1;
 
+       if (wpa_s->parent->set_sta_uapsd)
+               params.uapsd = wpa_s->parent->sta_uapsd;
+       else
+               params.uapsd = -1;
+
        if (wpa_drv_associate(wpa_s, &params) < 0) {
                wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
                        "failed");
-               wpa_supplicant_req_scan(wpa_s, 5, 0);
+               wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
+               os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
                return;
        }
 
@@ -425,7 +436,6 @@ void sme_event_assoc_reject(struct wpa_supplicant *wpa_s,
                            union wpa_event_data *data)
 {
        int bssid_changed;
-       int timeout = 5000;
 
        wpa_printf(MSG_DEBUG, "SME: Association with " MACSTR " failed: "
                   "status code %d", MAC2STR(wpa_s->pending_bssid),
@@ -447,29 +457,12 @@ void sme_event_assoc_reject(struct wpa_supplicant *wpa_s,
        }
        wpa_s->sme.prev_bssid_set = 0;
 
-       if (wpa_blacklist_add(wpa_s, wpa_s->pending_bssid) == 0) {
-               struct wpa_blacklist *b;
-               b = wpa_blacklist_get(wpa_s, wpa_s->pending_bssid);
-               if (b && b->count < 3) {
-                       /*
-                        * Speed up next attempt if there could be other APs
-                        * that could accept association.
-                        */
-                       timeout = 100;
-               }
-       }
+       wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
        wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
        os_memset(wpa_s->bssid, 0, ETH_ALEN);
        os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
        if (bssid_changed)
                wpas_notify_bssid_changed(wpa_s);
-
-       /*
-        * TODO: if more than one possible AP is available in scan results,
-        * could try the other ones before requesting a new scan.
-        */
-       wpa_supplicant_req_scan(wpa_s, timeout / 1000,
-                               1000 * (timeout % 1000));
 }
 
 
@@ -477,7 +470,7 @@ void sme_event_auth_timed_out(struct wpa_supplicant *wpa_s,
                              union wpa_event_data *data)
 {
        wpa_printf(MSG_DEBUG, "SME: Authentication timed out");
-       wpa_supplicant_req_scan(wpa_s, 5, 0);
+       wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
 }
 
 
@@ -485,8 +478,8 @@ void sme_event_assoc_timed_out(struct wpa_supplicant *wpa_s,
                               union wpa_event_data *data)
 {
        wpa_printf(MSG_DEBUG, "SME: Association timed out");
+       wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
        wpa_supplicant_mark_disassoc(wpa_s);
-       wpa_supplicant_req_scan(wpa_s, 5, 0);
 }
 
 
@@ -508,3 +501,160 @@ void sme_event_disassoc(struct wpa_supplicant *wpa_s,
                                       WLAN_REASON_DEAUTH_LEAVING);
        }
 }
+
+
+#ifdef CONFIG_IEEE80211W
+
+static const unsigned int sa_query_max_timeout = 1000;
+static const unsigned int sa_query_retry_timeout = 201;
+
+static int sme_check_sa_query_timeout(struct wpa_supplicant *wpa_s)
+{
+       u32 tu;
+       struct os_time now, passed;
+       os_get_time(&now);
+       os_time_sub(&now, &wpa_s->sme.sa_query_start, &passed);
+       tu = (passed.sec * 1000000 + passed.usec) / 1024;
+       if (sa_query_max_timeout < tu) {
+               wpa_printf(MSG_DEBUG, "SME: SA Query timed out");
+               sme_stop_sa_query(wpa_s);
+               wpa_supplicant_deauthenticate(
+                       wpa_s, WLAN_REASON_PREV_AUTH_NOT_VALID);
+               return 1;
+       }
+
+       return 0;
+}
+
+
+static void sme_send_sa_query_req(struct wpa_supplicant *wpa_s,
+                                 const u8 *trans_id)
+{
+       u8 req[2 + WLAN_SA_QUERY_TR_ID_LEN];
+       wpa_printf(MSG_DEBUG, "SME: Sending SA Query Request to "
+                  MACSTR, MAC2STR(wpa_s->bssid));
+       wpa_hexdump(MSG_DEBUG, "SME: SA Query Transaction ID",
+                   trans_id, WLAN_SA_QUERY_TR_ID_LEN);
+       req[0] = WLAN_ACTION_SA_QUERY;
+       req[1] = WLAN_SA_QUERY_REQUEST;
+       os_memcpy(req + 2, trans_id, WLAN_SA_QUERY_TR_ID_LEN);
+       if (wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, wpa_s->bssid,
+                               wpa_s->own_addr, wpa_s->bssid,
+                               req, sizeof(req)) < 0)
+               wpa_printf(MSG_INFO, "SME: Failed to send SA Query Request");
+}
+
+
+static void sme_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
+{
+       struct wpa_supplicant *wpa_s = eloop_ctx;
+       unsigned int timeout, sec, usec;
+       u8 *trans_id, *nbuf;
+
+       if (wpa_s->sme.sa_query_count > 0 &&
+           sme_check_sa_query_timeout(wpa_s))
+               return;
+
+       nbuf = os_realloc(wpa_s->sme.sa_query_trans_id,
+                         (wpa_s->sme.sa_query_count + 1) *
+                         WLAN_SA_QUERY_TR_ID_LEN);
+       if (nbuf == NULL)
+               return;
+       if (wpa_s->sme.sa_query_count == 0) {
+               /* Starting a new SA Query procedure */
+               os_get_time(&wpa_s->sme.sa_query_start);
+       }
+       trans_id = nbuf + wpa_s->sme.sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
+       wpa_s->sme.sa_query_trans_id = nbuf;
+       wpa_s->sme.sa_query_count++;
+
+       os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
+
+       timeout = sa_query_retry_timeout;
+       sec = ((timeout / 1000) * 1024) / 1000;
+       usec = (timeout % 1000) * 1024;
+       eloop_register_timeout(sec, usec, sme_sa_query_timer, wpa_s, NULL);
+
+       wpa_printf(MSG_DEBUG, "SME: Association SA Query attempt %d",
+                  wpa_s->sme.sa_query_count);
+
+       sme_send_sa_query_req(wpa_s, trans_id);
+}
+
+
+static void sme_start_sa_query(struct wpa_supplicant *wpa_s)
+{
+       sme_sa_query_timer(wpa_s, NULL);
+}
+
+
+void sme_stop_sa_query(struct wpa_supplicant *wpa_s)
+{
+       eloop_cancel_timeout(sme_sa_query_timer, wpa_s, NULL);
+       os_free(wpa_s->sme.sa_query_trans_id);
+       wpa_s->sme.sa_query_trans_id = NULL;
+       wpa_s->sme.sa_query_count = 0;
+}
+
+
+void sme_event_unprot_disconnect(struct wpa_supplicant *wpa_s, const u8 *sa,
+                                const u8 *da, u16 reason_code)
+{
+       struct wpa_ssid *ssid;
+
+       if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
+               return;
+       if (wpa_s->wpa_state != WPA_COMPLETED)
+               return;
+       ssid = wpa_s->current_ssid;
+       if (ssid == NULL || ssid->ieee80211w == 0)
+               return;
+       if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
+               return;
+       if (reason_code != WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA &&
+           reason_code != WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA)
+               return;
+       if (wpa_s->sme.sa_query_count > 0)
+               return;
+
+       wpa_printf(MSG_DEBUG, "SME: Unprotected disconnect dropped - possible "
+                  "AP/STA state mismatch - trigger SA Query");
+       sme_start_sa_query(wpa_s);
+}
+
+
+void sme_sa_query_rx(struct wpa_supplicant *wpa_s, const u8 *sa,
+                    const u8 *data, size_t len)
+{
+       int i;
+
+       if (wpa_s->sme.sa_query_trans_id == NULL ||
+           len < 1 + WLAN_SA_QUERY_TR_ID_LEN ||
+           data[0] != WLAN_SA_QUERY_RESPONSE)
+               return;
+       wpa_printf(MSG_DEBUG, "SME: Received SA Query response from " MACSTR
+                  " (trans_id %02x%02x)",
+                  MAC2STR(sa), data[1], data[2]);
+
+       if (os_memcmp(sa, wpa_s->bssid, ETH_ALEN) != 0)
+               return;
+
+       for (i = 0; i < wpa_s->sme.sa_query_count; i++) {
+               if (os_memcmp(wpa_s->sme.sa_query_trans_id +
+                             i * WLAN_SA_QUERY_TR_ID_LEN,
+                             data + 1, WLAN_SA_QUERY_TR_ID_LEN) == 0)
+                       break;
+       }
+
+       if (i >= wpa_s->sme.sa_query_count) {
+               wpa_printf(MSG_DEBUG, "SME: No matching SA Query "
+                          "transaction identifier found");
+               return;
+       }
+
+       wpa_printf(MSG_DEBUG, "SME: Reply to pending SA Query received from "
+                  MACSTR, MAC2STR(sa));
+       sme_stop_sa_query(wpa_s);
+}
+
+#endif /* CONFIG_IEEE80211W */