SME: Fix re-try after auth/assoc timeout/failure
[mech_eap.git] / wpa_supplicant / sme.c
index b5927c5..55788de 100644 (file)
 #include "driver_i.h"
 #include "wpas_glue.h"
 #include "wps_supplicant.h"
+#include "p2p_supplicant.h"
 #include "notify.h"
 #include "blacklist.h"
 #include "bss.h"
 #include "scan.h"
 #include "sme.h"
 
+static int sme_another_bss_in_ess(struct wpa_supplicant *wpa_s)
+{
+       struct wpa_bss *bss, *cbss;
+
+       cbss = wpa_s->current_bss;
+
+       dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
+               if (bss == cbss)
+                       continue;
+               if (bss->ssid_len == cbss->ssid_len &&
+                   os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
+                   wpa_blacklist_get(wpa_s, bss->bssid) == NULL)
+                       return 1;
+       }
+
+       return 0;
+}
+
+
+static void sme_connection_failed(struct wpa_supplicant *wpa_s,
+                                 const u8 *bssid)
+{
+       int timeout;
+       int count;
+
+       /*
+        * Add the failed BSSID into the blacklist and speed up next scan
+        * attempt if there could be other APs that could accept association.
+        * The current blacklist count indicates how many times we have tried
+        * connecting to this AP and multiple attempts mean that other APs are
+        * either not available or has already been tried, so that we can start
+        * increasing the delay here to avoid constant scanning.
+        */
+       count = wpa_blacklist_add(wpa_s, bssid);
+       if (count == 1 && wpa_s->current_bss) {
+               /*
+                * This BSS was not in the blacklist before. If there is
+                * another BSS available for the same ESS, we should try that
+                * next. Otherwise, we may as well try this one once more
+                * before allowing other, likely worse, ESSes to be considered.
+                */
+               if (sme_another_bss_in_ess(wpa_s)) {
+                       wpa_printf(MSG_DEBUG, "SME: Another BSS in this ESS "
+                                  "has been seen; try it next");
+                       wpa_blacklist_add(wpa_s, bssid);
+               }
+       }
+
+       switch (count) {
+       case 1:
+               timeout = 100;
+               break;
+       case 2:
+               timeout = 500;
+               break;
+       case 3:
+               timeout = 1000;
+               break;
+       default:
+               timeout = 5000;
+       }
+
+       /*
+        * 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));
+}
+
+
 void sme_authenticate(struct wpa_supplicant *wpa_s,
                      struct wpa_bss *bss, struct wpa_ssid *ssid)
 {
@@ -209,6 +281,22 @@ void sme_authenticate(struct wpa_supplicant *wpa_s,
        }
 #endif /* CONFIG_IEEE80211W */
 
+#ifdef CONFIG_P2P
+       if (wpa_s->global->p2p) {
+               u8 *pos;
+               size_t len;
+               int res;
+               int p2p_group;
+               p2p_group = wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE;
+               pos = wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len;
+               len = sizeof(wpa_s->sme.assoc_req_ie) -
+                       wpa_s->sme.assoc_req_ie_len;
+               res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len, p2p_group);
+               if (res >= 0)
+                       wpa_s->sme.assoc_req_ie_len += res;
+       }
+#endif /* CONFIG_P2P */
+
        wpa_supplicant_cancel_scan(wpa_s);
 
        wpa_msg(wpa_s, MSG_INFO, "Trying to authenticate with " MACSTR
@@ -274,7 +362,35 @@ void sme_event_auth(struct wpa_supplicant *wpa_s, union wpa_event_data *data)
        if (data->auth.status_code != WLAN_STATUS_SUCCESS) {
                wpa_printf(MSG_DEBUG, "SME: Authentication failed (status "
                           "code %d)", data->auth.status_code);
-               return;
+
+               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) {
+                       sme_connection_failed(wpa_s, wpa_s->pending_bssid);
+                       return;
+               }
+
+               switch (data->auth.auth_type) {
+               case WLAN_AUTH_OPEN:
+                       wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_SHARED;
+
+                       wpa_printf(MSG_DEBUG, "SME: Trying SHARED auth");
+                       wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
+                                                wpa_s->current_ssid);
+                       return;
+
+               case WLAN_AUTH_SHARED_KEY:
+                       wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_LEAP;
+
+                       wpa_printf(MSG_DEBUG, "SME: Trying LEAP auth");
+                       wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
+                                                wpa_s->current_ssid);
+                       return;
+
+               default:
+                       return;
+               }
        }
 
 #ifdef CONFIG_IEEE80211R
@@ -339,6 +455,14 @@ void sme_associate(struct wpa_supplicant *wpa_s, enum wpas_mode mode,
                                        elems.wpa_ie_len + 2);
        else
                wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
+       if (elems.p2p &&
+           (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 "
@@ -379,7 +503,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),
@@ -401,29 +524,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;
-               }
-       }
+       sme_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));
 }
 
 
@@ -431,7 +537,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);
+       sme_connection_failed(wpa_s, wpa_s->pending_bssid);
 }
 
 
@@ -439,8 +545,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");
+       sme_connection_failed(wpa_s, wpa_s->pending_bssid);
        wpa_supplicant_mark_disassoc(wpa_s);
-       wpa_supplicant_req_scan(wpa_s, 5, 0);
 }
 
 
@@ -448,7 +554,7 @@ void sme_event_disassoc(struct wpa_supplicant *wpa_s,
                        union wpa_event_data *data)
 {
        wpa_printf(MSG_DEBUG, "SME: Disassociation event received");
-       if (!is_zero_ether_addr(wpa_s->bssid) &&
+       if (wpa_s->sme.prev_bssid_set &&
            !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)) {
                /*
                 * cfg80211/mac80211 can get into somewhat confused state if
@@ -458,7 +564,7 @@ void sme_event_disassoc(struct wpa_supplicant *wpa_s,
                 */
                wpa_printf(MSG_DEBUG, "SME: Deauthenticate to clear driver "
                           "state");
-               wpa_drv_deauthenticate(wpa_s, wpa_s->bssid,
+               wpa_drv_deauthenticate(wpa_s, wpa_s->sme.prev_bssid,
                                       WLAN_REASON_DEAUTH_LEAVING);
        }
 }