Do not inform other virtual interfaces of scan results in all cases
[mech_eap.git] / wpa_supplicant / events.c
index d09be6c..17db408 100644 (file)
@@ -23,6 +23,7 @@
 #include "eap_peer/eap.h"
 #include "ap/hostapd.h"
 #include "p2p/p2p.h"
+#include "wnm_sta.h"
 #include "notify.h"
 #include "common/ieee802_11_defs.h"
 #include "common/ieee802_11_common.h"
 #include "gas_query.h"
 #include "p2p_supplicant.h"
 #include "bgscan.h"
+#include "autoscan.h"
 #include "ap.h"
 #include "bss.h"
 #include "scan.h"
 #include "offchannel.h"
+#include "interworking.h"
+
+
+static int wpas_temp_disabled(struct wpa_supplicant *wpa_s,
+                             struct wpa_ssid *ssid)
+{
+       struct os_time now;
+
+       if (ssid == NULL || ssid->disabled_until.sec == 0)
+               return 0;
+
+       os_get_time(&now);
+       if (ssid->disabled_until.sec > now.sec)
+               return ssid->disabled_until.sec - now.sec;
+
+       wpas_clear_temp_disabled(wpa_s, ssid, 0);
+
+       return 0;
+}
 
 
 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
 {
        struct wpa_ssid *ssid, *old_ssid;
+       int res;
 
        if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid)
                return 0;
@@ -62,6 +84,13 @@ static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
                return -1;
        }
 
+       res = wpas_temp_disabled(wpa_s, ssid);
+       if (res > 0) {
+               wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
+                       "disabled for %d second(s)", res);
+               return -1;
+       }
+
        wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
                "current AP");
        if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
@@ -121,6 +150,9 @@ void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
        bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
        os_memset(wpa_s->bssid, 0, ETH_ALEN);
        os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
+#ifdef CONFIG_SME
+       wpa_s->sme.prev_bssid_set = 0;
+#endif /* CONFIG_SME */
 #ifdef CONFIG_P2P
        os_memset(wpa_s->go_dev_addr, 0, ETH_ALEN);
 #endif /* CONFIG_P2P */
@@ -141,6 +173,8 @@ void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
        if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
                eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
        wpa_s->ap_ies_from_associnfo = 0;
+       wpa_s->current_ssid = NULL;
+       wpa_s->key_mgmt = 0;
 }
 
 
@@ -287,7 +321,7 @@ int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
 
 
 #ifndef CONFIG_NO_SCAN_PROCESSING
-static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
+static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
                                        struct wpa_ssid *ssid)
 {
        int i, privacy = 0;
@@ -324,7 +358,7 @@ static int wpa_supplicant_match_privacy(struct wpa_scan_res *bss,
 
 static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
                                         struct wpa_ssid *ssid,
-                                        struct wpa_scan_res *bss)
+                                        struct wpa_bss *bss)
 {
        struct wpa_ie_data ie;
        int proto_match = 0;
@@ -342,7 +376,7 @@ static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
                  ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
                 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
 
-       rsn_ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
+       rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
        while ((ssid->proto & WPA_PROTO_RSN) && rsn_ie) {
                proto_match++;
 
@@ -397,7 +431,7 @@ static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
                return 1;
        }
 
-       wpa_ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
+       wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
        while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
                proto_match++;
 
@@ -443,6 +477,12 @@ static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
                return 1;
        }
 
+       if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
+           !rsn_ie) {
+               wpa_dbg(wpa_s, MSG_DEBUG, "   allow for non-WPA IEEE 802.1X");
+               return 1;
+       }
+
        if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
            wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
                wpa_dbg(wpa_s, MSG_DEBUG, "   skip - no WPA/RSN proto match");
@@ -493,7 +533,7 @@ static int ht_supported(const struct hostapd_hw_modes *mode)
 }
 
 
-static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_scan_res *bss)
+static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
 {
        const struct hostapd_hw_modes *mode = NULL, *modes;
        const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
@@ -531,7 +571,7 @@ static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_scan_res *bss)
                return 0;
 
        for (i = 0; i < (int) sizeof(scan_ie); i++) {
-               rate_ie = wpa_scan_get_ie(bss, scan_ie[i]);
+               rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
                if (rate_ie == NULL)
                        continue;
 
@@ -585,31 +625,26 @@ static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_scan_res *bss)
 
 
 static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
-                                           int i, struct wpa_scan_res *bss,
+                                           int i, struct wpa_bss *bss,
                                            struct wpa_ssid *group)
 {
-       const u8 *ssid_;
-       u8 wpa_ie_len, rsn_ie_len, ssid_len;
+       u8 wpa_ie_len, rsn_ie_len;
        int wpa;
        struct wpa_blacklist *e;
        const u8 *ie;
        struct wpa_ssid *ssid;
 
-       ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
-       ssid_ = ie ? ie + 2 : (u8 *) "";
-       ssid_len = ie ? ie[1] : 0;
-
-       ie = wpa_scan_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
+       ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
        wpa_ie_len = ie ? ie[1] : 0;
 
-       ie = wpa_scan_get_ie(bss, WLAN_EID_RSN);
+       ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
        rsn_ie_len = ie ? ie[1] : 0;
 
        wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR " ssid='%s' "
                "wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d%s",
-               i, MAC2STR(bss->bssid), wpa_ssid_txt(ssid_, ssid_len),
+               i, MAC2STR(bss->bssid), wpa_ssid_txt(bss->ssid, bss->ssid_len),
                wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
-               wpa_scan_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "");
+               wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ? " wps" : "");
 
        e = wpa_blacklist_get(wpa_s, bss->bssid);
        if (e) {
@@ -632,7 +667,7 @@ static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
                }
        }
 
-       if (ssid_len == 0) {
+       if (bss->ssid_len == 0) {
                wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID not known");
                return NULL;
        }
@@ -641,12 +676,20 @@ static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
 
        for (ssid = group; ssid; ssid = ssid->pnext) {
                int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
+               int res;
 
                if (wpas_network_disabled(wpa_s, ssid)) {
                        wpa_dbg(wpa_s, MSG_DEBUG, "   skip - disabled");
                        continue;
                }
 
+               res = wpas_temp_disabled(wpa_s, ssid);
+               if (res > 0) {
+                       wpa_dbg(wpa_s, MSG_DEBUG, "   skip - disabled "
+                               "temporarily for %d second(s)", res);
+                       continue;
+               }
+
 #ifdef CONFIG_WPS
                if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
                        wpa_dbg(wpa_s, MSG_DEBUG, "   skip - blacklisted "
@@ -674,8 +717,8 @@ static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
                        check_ssid = 0;
 
                if (check_ssid &&
-                   (ssid_len != ssid->ssid_len ||
-                    os_memcmp(ssid_, ssid->ssid, ssid_len) != 0)) {
+                   (bss->ssid_len != ssid->ssid_len ||
+                    os_memcmp(bss->ssid, ssid->ssid, bss->ssid_len) != 0)) {
                        wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID mismatch");
                        continue;
                }
@@ -741,32 +784,24 @@ static struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
 
 static struct wpa_bss *
 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
-                         struct wpa_scan_results *scan_res,
                          struct wpa_ssid *group,
                          struct wpa_ssid **selected_ssid)
 {
-       size_t i;
+       unsigned int i;
 
        wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
                group->priority);
 
-       for (i = 0; i < scan_res->num; i++) {
-               struct wpa_scan_res *bss = scan_res->res[i];
-               const u8 *ie, *ssid;
-               u8 ssid_len;
-
+       for (i = 0; i < wpa_s->last_scan_res_used; i++) {
+               struct wpa_bss *bss = wpa_s->last_scan_res[i];
                *selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group);
                if (!*selected_ssid)
                        continue;
-
-               ie = wpa_scan_get_ie(bss, WLAN_EID_SSID);
-               ssid = ie ? ie + 2 : (u8 *) "";
-               ssid_len = ie ? ie[1] : 0;
-
                wpa_dbg(wpa_s, MSG_DEBUG, "   selected BSS " MACSTR
                        " ssid='%s'",
-                       MAC2STR(bss->bssid), wpa_ssid_txt(ssid, ssid_len));
-               return wpa_bss_get(wpa_s, bss->bssid, ssid, ssid_len);
+                       MAC2STR(bss->bssid),
+                       wpa_ssid_txt(bss->ssid, bss->ssid_len));
+               return bss;
        }
 
        return NULL;
@@ -775,16 +810,19 @@ wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
 
 static struct wpa_bss *
 wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
-                           struct wpa_scan_results *scan_res,
                            struct wpa_ssid **selected_ssid)
 {
        struct wpa_bss *selected = NULL;
        int prio;
 
+       if (wpa_s->last_scan_res == NULL ||
+           wpa_s->last_scan_res_used == 0)
+               return NULL; /* no scan results from last update */
+
        while (selected == NULL) {
                for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
                        selected = wpa_supplicant_select_bss(
-                               wpa_s, scan_res, wpa_s->conf->pssid[prio],
+                               wpa_s, wpa_s->conf->pssid[prio],
                                selected_ssid);
                        if (selected)
                                break;
@@ -918,11 +956,9 @@ static void wpa_supplicant_rsn_preauth_scan_results(
 
 static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
                                       struct wpa_bss *selected,
-                                      struct wpa_ssid *ssid,
-                                      struct wpa_scan_results *scan_res)
+                                      struct wpa_ssid *ssid)
 {
-       size_t i;
-       struct wpa_scan_res *current_bss = NULL;
+       struct wpa_bss *current_bss = NULL;
        int min_diff;
 
        if (wpa_s->reassociate)
@@ -937,25 +973,22 @@ static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
        if (wpas_driver_bss_selection(wpa_s))
                return 0; /* Driver-based roaming */
 
-       for (i = 0; i < scan_res->num; i++) {
-               struct wpa_scan_res *res = scan_res->res[i];
-               const u8 *ie;
-               if (os_memcmp(res->bssid, wpa_s->bssid, ETH_ALEN) != 0)
-                       continue;
-
-               ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
-               if (ie == NULL)
-                       continue;
-               if (ie[1] != wpa_s->current_ssid->ssid_len ||
-                   os_memcmp(ie + 2, wpa_s->current_ssid->ssid, ie[1]) != 0)
-                       continue;
-               current_bss = res;
-               break;
-       }
+       if (wpa_s->current_ssid->ssid)
+               current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
+                                         wpa_s->current_ssid->ssid,
+                                         wpa_s->current_ssid->ssid_len);
+       if (!current_bss)
+               current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
 
        if (!current_bss)
                return 1; /* current BSS not seen in scan results */
 
+       if (current_bss == selected)
+               return 0;
+
+       if (selected->last_update_idx > current_bss->last_update_idx)
+               return 1; /* current BSS not seen in the last scan */
+
 #ifndef CONFIG_NO_ROAMING
        wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
        wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR " level=%d",
@@ -997,14 +1030,16 @@ static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
 }
 
 
-/* Return < 0 if no scan results could be fetched. */
+/* Return != 0 if no scan results could be fetched or if scan results should not
+ * be shared with other virtual interfaces. */
 static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
                                              union wpa_event_data *data)
 {
-       struct wpa_bss *selected;
-       struct wpa_ssid *ssid = NULL;
        struct wpa_scan_results *scan_res;
        int ap = 0;
+#ifndef CONFIG_NO_RANDOM_POOL
+       size_t i, num;
+#endif /* CONFIG_NO_RANDOM_POOL */
 
 #ifdef CONFIG_AP
        if (wpa_s->ap_iface)
@@ -1014,15 +1049,19 @@ static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
        wpa_supplicant_notify_scanning(wpa_s, 0);
 
 #ifdef CONFIG_P2P
-       if (wpa_s->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
-           wpa_s->global->p2p != NULL) {
-               wpa_s->p2p_cb_on_scan_complete = 0;
+       if (wpa_s->global->p2p_cb_on_scan_complete &&
+           !wpa_s->global->p2p_disabled &&
+           wpa_s->global->p2p != NULL && !wpa_s->sta_scan_pending) {
+               wpa_s->global->p2p_cb_on_scan_complete = 0;
                if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
                        wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
                                "stopped scan processing");
+                       wpa_s->sta_scan_pending = 1;
+                       wpa_supplicant_req_scan(wpa_s, 5, 0);
                        return -1;
                }
        }
+       wpa_s->sta_scan_pending = 0;
 #endif /* CONFIG_P2P */
 
        scan_res = wpa_supplicant_get_scan_results(wpa_s,
@@ -1038,7 +1077,6 @@ static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
        }
 
 #ifndef CONFIG_NO_RANDOM_POOL
-       size_t i, num;
        num = scan_res->num;
        if (num > 10)
                num = 10;
@@ -1063,7 +1101,7 @@ static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
                scan_res_handler(wpa_s, scan_res);
 
                wpa_scan_results_free(scan_res);
-               return 0;
+               return -2;
        }
 
        if (ap) {
@@ -1092,6 +1130,11 @@ static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
                return 0;
        }
 
+       if (autoscan_notify_scan(wpa_s, scan_res)) {
+               wpa_scan_results_free(scan_res);
+               return 0;
+       }
+
        if (wpa_s->disconnected) {
                wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
                wpa_scan_results_free(scan_res);
@@ -1104,13 +1147,24 @@ static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
                return 0;
        }
 
-       selected = wpa_supplicant_pick_network(wpa_s, scan_res, &ssid);
+       wpas_wps_update_ap_info(wpa_s, scan_res);
+
+       wpa_scan_results_free(scan_res);
+
+       return wpas_select_network_from_last_scan(wpa_s);
+}
+
+
+int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s)
+{
+       struct wpa_bss *selected;
+       struct wpa_ssid *ssid = NULL;
+
+       selected = wpa_supplicant_pick_network(wpa_s, &ssid);
 
        if (selected) {
                int skip;
-               skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid,
-                                                   scan_res);
-               wpa_scan_results_free(scan_res);
+               skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
                if (skip) {
                        wpa_supplicant_rsn_preauth_scan_results(wpa_s);
                        return 0;
@@ -1121,8 +1175,12 @@ static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
                        return -1;
                }
                wpa_supplicant_rsn_preauth_scan_results(wpa_s);
+               /*
+                * Do not notify other virtual radios of scan results since we do not
+                * want them to start other associations at the same time.
+                */
+               return 1;
        } else {
-               wpa_scan_results_free(scan_res);
                wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
                ssid = wpa_supplicant_pick_new_network(wpa_s);
                if (ssid) {
@@ -1148,6 +1206,19 @@ static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
                                return 0;
                        }
 #endif /* CONFIG_P2P */
+#ifdef CONFIG_INTERWORKING
+                       if (wpa_s->conf->auto_interworking &&
+                           wpa_s->conf->interworking &&
+                           wpa_s->conf->cred) {
+                               wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
+                                       "start ANQP fetch since no matching "
+                                       "networks found");
+                               wpa_s->network_select = 1;
+                               wpa_s->auto_network_select = 1;
+                               interworking_start_fetch_anqp(wpa_s);
+                               return 1;
+                       }
+#endif /* CONFIG_INTERWORKING */
                        if (wpa_supplicant_req_sched_scan(wpa_s))
                                wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
                                                            timeout_usec);
@@ -1163,11 +1234,13 @@ static void wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
        const char *rn, *rn2;
        struct wpa_supplicant *ifs;
 
-       if (_wpa_supplicant_event_scan_results(wpa_s, data) < 0) {
+       if (_wpa_supplicant_event_scan_results(wpa_s, data) != 0) {
                /*
                 * If no scan results could be fetched, then no need to
                 * notify those interfaces that did not actually request
-                * this scan.
+                * this scan. Similarly, if scan results started a new operation on this
+                * interface, do not notify other interfaces to avoid concurrent
+                * operations during a connection attempt.
                 */
                return;
        }
@@ -1495,7 +1568,6 @@ static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
 {
        u8 bssid[ETH_ALEN];
        int ft_completed;
-       int bssid_changed;
        struct wpa_driver_capa capa;
 
 #ifdef CONFIG_AP
@@ -1513,17 +1585,21 @@ static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
        if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
                return;
 
+       if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
+               wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
+               wpa_supplicant_disassociate(
+                       wpa_s, WLAN_REASON_DEAUTH_LEAVING);
+               return;
+       }
+
        wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
-       if (wpa_drv_get_bssid(wpa_s, bssid) >= 0 &&
-           os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
+       if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
                wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
                        MACSTR, MAC2STR(bssid));
                random_add_randomness(bssid, ETH_ALEN);
-               bssid_changed = os_memcmp(wpa_s->bssid, bssid, ETH_ALEN);
                os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
                os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
-               if (bssid_changed)
-                       wpas_notify_bssid_changed(wpa_s);
+               wpas_notify_bssid_changed(wpa_s);
 
                if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
                        wpa_clear_keys(wpa_s, bssid);
@@ -1675,6 +1751,8 @@ static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
                ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
        }
 #endif /* CONFIG_IBSS_RSN */
+
+       wpas_wps_notify_assoc(wpa_s, bssid);
 }
 
 
@@ -1691,10 +1769,40 @@ static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
                                          int locally_generated)
 {
        const u8 *bssid;
+
+       if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
+               /*
+                * At least Host AP driver and a Prism3 card seemed to be
+                * generating streams of disconnected events when configuring
+                * IBSS for WPA-None. Ignore them for now.
+                */
+               return;
+       }
+
+       bssid = wpa_s->bssid;
+       if (is_zero_ether_addr(bssid))
+               bssid = wpa_s->pending_bssid;
+
+       if (!is_zero_ether_addr(bssid) ||
+           wpa_s->wpa_state >= WPA_AUTHENTICATING) {
+               wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
+                       " reason=%d%s",
+                       MAC2STR(bssid), reason_code,
+                       locally_generated ? " locally_generated=1" : "");
+       }
+}
+
+
+static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
+                                                u16 reason_code,
+                                                int locally_generated)
+{
+       const u8 *bssid;
        int authenticating;
        u8 prev_pending_bssid[ETH_ALEN];
        struct wpa_bss *fast_reconnect = NULL;
        struct wpa_ssid *fast_reconnect_ssid = NULL;
+       struct wpa_ssid *last_ssid;
 
        authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
        os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
@@ -1714,6 +1822,7 @@ static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
            wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
                wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
                        "pre-shared key may be incorrect");
+               wpas_auth_failed(wpa_s);
        }
        if (!wpa_s->auto_reconnect_disabled ||
            wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
@@ -1752,22 +1861,23 @@ static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
        if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
                wpas_connection_failed(wpa_s, bssid);
        wpa_sm_notify_disassoc(wpa_s->wpa);
-       if (!is_zero_ether_addr(bssid) ||
-           wpa_s->wpa_state >= WPA_AUTHENTICATING) {
-               wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
-                       " reason=%d%s",
-                       MAC2STR(bssid), reason_code,
-                       locally_generated ? " locally_generated=1" : "");
-       }
+       if (locally_generated)
+               wpa_s->disconnect_reason = -reason_code;
+       else
+               wpa_s->disconnect_reason = reason_code;
+       wpas_notify_disconnect_reason(wpa_s);
        if (wpa_supplicant_dynamic_keys(wpa_s)) {
                wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
                wpa_s->keys_cleared = 0;
                wpa_clear_keys(wpa_s, wpa_s->bssid);
        }
+       last_ssid = wpa_s->current_ssid;
        wpa_supplicant_mark_disassoc(wpa_s);
 
-       if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
+       if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
                sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
+               wpa_s->current_ssid = last_ssid;
+       }
 
        if (fast_reconnect) {
 #ifndef CONFIG_NO_SCAN_PROCESSING
@@ -1975,6 +2085,25 @@ static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
 #endif /* CONFIG_TDLS */
 
 
+#ifdef CONFIG_IEEE80211V
+static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
+                                    union wpa_event_data *data)
+{
+       if (data == NULL)
+               return;
+       switch (data->wnm.oper) {
+       case WNM_OPER_SLEEP:
+               wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
+                          "(action=%d, intval=%d)",
+                          data->wnm.sleep_action, data->wnm.sleep_intval);
+               ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
+                                            data->wnm.sleep_intval);
+               break;
+       }
+}
+#endif /* CONFIG_IEEE80211V */
+
+
 #ifdef CONFIG_IEEE80211R
 static void
 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
@@ -2261,15 +2390,30 @@ void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
 #endif /* CONFIG_AP */
                wpa_supplicant_event_disassoc(wpa_s, reason_code,
                                              locally_generated);
+               if (reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
+                   ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
+                     (wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
+                    eapol_sm_failed(wpa_s->eapol)))
+                       wpas_auth_failed(wpa_s);
 #ifdef CONFIG_P2P
                if (event == EVENT_DEAUTH && data) {
-                       wpas_p2p_deauth_notif(wpa_s, data->deauth_info.addr,
-                                             reason_code,
-                                             data->deauth_info.ie,
-                                             data->deauth_info.ie_len,
-                                             locally_generated);
+                       if (wpas_p2p_deauth_notif(wpa_s,
+                                                 data->deauth_info.addr,
+                                                 reason_code,
+                                                 data->deauth_info.ie,
+                                                 data->deauth_info.ie_len,
+                                                 locally_generated) > 0) {
+                               /*
+                                * The interface was removed, so cannot
+                                * continue processing any additional
+                                * operations after this.
+                                */
+                               break;
+                       }
                }
 #endif /* CONFIG_P2P */
+               wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
+                                                    locally_generated);
                break;
        case EVENT_MICHAEL_MIC_FAILURE:
                wpa_supplicant_event_michael_mic_failure(wpa_s, data);
@@ -2277,6 +2421,18 @@ void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
 #ifndef CONFIG_NO_SCAN_PROCESSING
        case EVENT_SCAN_RESULTS:
                wpa_supplicant_event_scan_results(wpa_s, data);
+#ifdef CONFIG_P2P
+       if (wpa_s->global->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
+           wpa_s->global->p2p != NULL &&
+           wpa_s->wpa_state != WPA_AUTHENTICATING &&
+           wpa_s->wpa_state != WPA_ASSOCIATING) {
+               wpa_s->global->p2p_cb_on_scan_complete = 0;
+               if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
+                       wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
+                               "continued after scan result processing");
+               }
+       }
+#endif /* CONFIG_P2P */
                break;
 #endif /* CONFIG_NO_SCAN_PROCESSING */
        case EVENT_ASSOCINFO:
@@ -2298,6 +2454,11 @@ void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
                wpa_supplicant_event_tdls(wpa_s, data);
                break;
 #endif /* CONFIG_TDLS */
+#ifdef CONFIG_IEEE80211V
+       case EVENT_WNM:
+               wpa_supplicant_event_wnm(wpa_s, data);
+               break;
+#endif /* CONFIG_IEEE80211V */
 #ifdef CONFIG_IEEE80211R
        case EVENT_FT_RESPONSE:
                wpa_supplicant_event_ft_response(wpa_s, data);
@@ -2404,6 +2565,21 @@ void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
                ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
                                       data->rx_from_unknown.wds);
                break;
+       case EVENT_CH_SWITCH:
+               if (!data)
+                       break;
+               if (!wpa_s->ap_iface) {
+                       wpa_dbg(wpa_s, MSG_DEBUG, "AP: Ignore channel switch "
+                               "event in non-AP mode");
+                       break;
+               }
+
+#ifdef CONFIG_AP
+               wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
+                                 data->ch_switch.ht_enabled,
+                                 data->ch_switch.ch_offset);
+#endif /* CONFIG_AP */
+               break;
        case EVENT_RX_MGMT: {
                u16 fc, stype;
                const struct ieee80211_mgmt *mgmt;
@@ -2473,6 +2649,12 @@ void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
                }
 #endif /* CONFIG_SME */
 #endif /* CONFIG_IEEE80211W */
+#ifdef CONFIG_IEEE80211V
+               if (data->rx_action.category == WLAN_ACTION_WNM) {
+                       ieee802_11_rx_wnm_action(wpa_s, &data->rx_action);
+                       break;
+               }
+#endif /* CONFIG_IEEE80211V */
 #ifdef CONFIG_GAS
                if (data->rx_action.category == WLAN_ACTION_PUBLIC &&
                    gas_query_rx(wpa_s->gas, data->rx_action.da,