Home | History | Annotate | Download | only in wpa_supplicant
      1 /*
      2  * WPA Supplicant - Driver event processing
      3  * Copyright (c) 2003-2019, Jouni Malinen <j (at) w1.fi>
      4  *
      5  * This software may be distributed under the terms of the BSD license.
      6  * See README for more details.
      7  */
      8 
      9 #include "includes.h"
     10 
     11 #include "common.h"
     12 #include "eapol_supp/eapol_supp_sm.h"
     13 #include "rsn_supp/wpa.h"
     14 #include "eloop.h"
     15 #include "config.h"
     16 #include "l2_packet/l2_packet.h"
     17 #include "wpa_supplicant_i.h"
     18 #include "driver_i.h"
     19 #include "pcsc_funcs.h"
     20 #include "rsn_supp/preauth.h"
     21 #include "rsn_supp/pmksa_cache.h"
     22 #include "common/wpa_ctrl.h"
     23 #include "eap_peer/eap.h"
     24 #include "ap/hostapd.h"
     25 #include "p2p/p2p.h"
     26 #include "fst/fst.h"
     27 #include "wnm_sta.h"
     28 #include "notify.h"
     29 #include "common/ieee802_11_defs.h"
     30 #include "common/ieee802_11_common.h"
     31 #include "common/gas_server.h"
     32 #include "common/dpp.h"
     33 #include "crypto/random.h"
     34 #include "blacklist.h"
     35 #include "wpas_glue.h"
     36 #include "wps_supplicant.h"
     37 #include "ibss_rsn.h"
     38 #include "sme.h"
     39 #include "gas_query.h"
     40 #include "p2p_supplicant.h"
     41 #include "bgscan.h"
     42 #include "autoscan.h"
     43 #include "ap.h"
     44 #include "bss.h"
     45 #include "scan.h"
     46 #include "offchannel.h"
     47 #include "interworking.h"
     48 #include "mesh.h"
     49 #include "mesh_mpm.h"
     50 #include "wmm_ac.h"
     51 #include "dpp_supplicant.h"
     52 
     53 
     54 #define MAX_OWE_TRANSITION_BSS_SELECT_COUNT 5
     55 
     56 
     57 #ifndef CONFIG_NO_SCAN_PROCESSING
     58 static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
     59 					      int new_scan, int own_request);
     60 #endif /* CONFIG_NO_SCAN_PROCESSING */
     61 
     62 
     63 int wpas_temp_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
     64 {
     65 	struct os_reltime now;
     66 
     67 	if (ssid == NULL || ssid->disabled_until.sec == 0)
     68 		return 0;
     69 
     70 	os_get_reltime(&now);
     71 	if (ssid->disabled_until.sec > now.sec)
     72 		return ssid->disabled_until.sec - now.sec;
     73 
     74 	wpas_clear_temp_disabled(wpa_s, ssid, 0);
     75 
     76 	return 0;
     77 }
     78 
     79 
     80 #ifndef CONFIG_NO_SCAN_PROCESSING
     81 /**
     82  * wpas_reenabled_network_time - Time until first network is re-enabled
     83  * @wpa_s: Pointer to wpa_supplicant data
     84  * Returns: If all enabled networks are temporarily disabled, returns the time
     85  *	(in sec) until the first network is re-enabled. Otherwise returns 0.
     86  *
     87  * This function is used in case all enabled networks are temporarily disabled,
     88  * in which case it returns the time (in sec) that the first network will be
     89  * re-enabled. The function assumes that at least one network is enabled.
     90  */
     91 static int wpas_reenabled_network_time(struct wpa_supplicant *wpa_s)
     92 {
     93 	struct wpa_ssid *ssid;
     94 	int disabled_for, res = 0;
     95 
     96 #ifdef CONFIG_INTERWORKING
     97 	if (wpa_s->conf->auto_interworking && wpa_s->conf->interworking &&
     98 	    wpa_s->conf->cred)
     99 		return 0;
    100 #endif /* CONFIG_INTERWORKING */
    101 
    102 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
    103 		if (ssid->disabled)
    104 			continue;
    105 
    106 		disabled_for = wpas_temp_disabled(wpa_s, ssid);
    107 		if (!disabled_for)
    108 			return 0;
    109 
    110 		if (!res || disabled_for < res)
    111 			res = disabled_for;
    112 	}
    113 
    114 	return res;
    115 }
    116 #endif /* CONFIG_NO_SCAN_PROCESSING */
    117 
    118 
    119 void wpas_network_reenabled(void *eloop_ctx, void *timeout_ctx)
    120 {
    121 	struct wpa_supplicant *wpa_s = eloop_ctx;
    122 
    123 	if (wpa_s->disconnected || wpa_s->wpa_state != WPA_SCANNING)
    124 		return;
    125 
    126 	wpa_dbg(wpa_s, MSG_DEBUG,
    127 		"Try to associate due to network getting re-enabled");
    128 	if (wpa_supplicant_fast_associate(wpa_s) != 1) {
    129 		wpa_supplicant_cancel_sched_scan(wpa_s);
    130 		wpa_supplicant_req_scan(wpa_s, 0, 0);
    131 	}
    132 }
    133 
    134 
    135 static struct wpa_bss * wpa_supplicant_get_new_bss(
    136 	struct wpa_supplicant *wpa_s, const u8 *bssid)
    137 {
    138 	struct wpa_bss *bss = NULL;
    139 	struct wpa_ssid *ssid = wpa_s->current_ssid;
    140 
    141 	if (ssid->ssid_len > 0)
    142 		bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
    143 	if (!bss)
    144 		bss = wpa_bss_get_bssid(wpa_s, bssid);
    145 
    146 	return bss;
    147 }
    148 
    149 
    150 static void wpa_supplicant_update_current_bss(struct wpa_supplicant *wpa_s)
    151 {
    152 	struct wpa_bss *bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
    153 
    154 	if (!bss) {
    155 		wpa_supplicant_update_scan_results(wpa_s);
    156 
    157 		/* Get the BSS from the new scan results */
    158 		bss = wpa_supplicant_get_new_bss(wpa_s, wpa_s->bssid);
    159 	}
    160 
    161 	if (bss)
    162 		wpa_s->current_bss = bss;
    163 }
    164 
    165 
    166 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s)
    167 {
    168 	struct wpa_ssid *ssid, *old_ssid;
    169 	u8 drv_ssid[SSID_MAX_LEN];
    170 	size_t drv_ssid_len;
    171 	int res;
    172 
    173 	if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid) {
    174 		wpa_supplicant_update_current_bss(wpa_s);
    175 
    176 		if (wpa_s->current_ssid->ssid_len == 0)
    177 			return 0; /* current profile still in use */
    178 		res = wpa_drv_get_ssid(wpa_s, drv_ssid);
    179 		if (res < 0) {
    180 			wpa_msg(wpa_s, MSG_INFO,
    181 				"Failed to read SSID from driver");
    182 			return 0; /* try to use current profile */
    183 		}
    184 		drv_ssid_len = res;
    185 
    186 		if (drv_ssid_len == wpa_s->current_ssid->ssid_len &&
    187 		    os_memcmp(drv_ssid, wpa_s->current_ssid->ssid,
    188 			      drv_ssid_len) == 0)
    189 			return 0; /* current profile still in use */
    190 
    191 		wpa_msg(wpa_s, MSG_DEBUG,
    192 			"Driver-initiated BSS selection changed the SSID to %s",
    193 			wpa_ssid_txt(drv_ssid, drv_ssid_len));
    194 		/* continue selecting a new network profile */
    195 	}
    196 
    197 	wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
    198 		"information");
    199 	ssid = wpa_supplicant_get_ssid(wpa_s);
    200 	if (ssid == NULL) {
    201 		wpa_msg(wpa_s, MSG_INFO,
    202 			"No network configuration found for the current AP");
    203 		return -1;
    204 	}
    205 
    206 	if (wpas_network_disabled(wpa_s, ssid)) {
    207 		wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
    208 		return -1;
    209 	}
    210 
    211 	if (disallowed_bssid(wpa_s, wpa_s->bssid) ||
    212 	    disallowed_ssid(wpa_s, ssid->ssid, ssid->ssid_len)) {
    213 		wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS is disallowed");
    214 		return -1;
    215 	}
    216 
    217 	res = wpas_temp_disabled(wpa_s, ssid);
    218 	if (res > 0) {
    219 		wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
    220 			"disabled for %d second(s)", res);
    221 		return -1;
    222 	}
    223 
    224 	wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
    225 		"current AP");
    226 	if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
    227 		u8 wpa_ie[80];
    228 		size_t wpa_ie_len = sizeof(wpa_ie);
    229 		if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
    230 					      wpa_ie, &wpa_ie_len) < 0)
    231 			wpa_dbg(wpa_s, MSG_DEBUG, "Could not set WPA suites");
    232 	} else {
    233 		wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
    234 	}
    235 
    236 	if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
    237 		eapol_sm_invalidate_cached_session(wpa_s->eapol);
    238 	old_ssid = wpa_s->current_ssid;
    239 	wpa_s->current_ssid = ssid;
    240 
    241 	wpa_supplicant_update_current_bss(wpa_s);
    242 
    243 	wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
    244 	wpa_supplicant_initiate_eapol(wpa_s);
    245 	if (old_ssid != wpa_s->current_ssid)
    246 		wpas_notify_network_changed(wpa_s);
    247 
    248 	return 0;
    249 }
    250 
    251 
    252 void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
    253 {
    254 	struct wpa_supplicant *wpa_s = eloop_ctx;
    255 
    256 	if (wpa_s->countermeasures) {
    257 		wpa_s->countermeasures = 0;
    258 		wpa_drv_set_countermeasures(wpa_s, 0);
    259 		wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
    260 
    261 		/*
    262 		 * It is possible that the device is sched scanning, which means
    263 		 * that a connection attempt will be done only when we receive
    264 		 * scan results. However, in this case, it would be preferable
    265 		 * to scan and connect immediately, so cancel the sched_scan and
    266 		 * issue a regular scan flow.
    267 		 */
    268 		wpa_supplicant_cancel_sched_scan(wpa_s);
    269 		wpa_supplicant_req_scan(wpa_s, 0, 0);
    270 	}
    271 }
    272 
    273 
    274 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
    275 {
    276 	int bssid_changed;
    277 
    278 	wnm_bss_keep_alive_deinit(wpa_s);
    279 
    280 #ifdef CONFIG_IBSS_RSN
    281 	ibss_rsn_deinit(wpa_s->ibss_rsn);
    282 	wpa_s->ibss_rsn = NULL;
    283 #endif /* CONFIG_IBSS_RSN */
    284 
    285 #ifdef CONFIG_AP
    286 	wpa_supplicant_ap_deinit(wpa_s);
    287 #endif /* CONFIG_AP */
    288 
    289 #ifdef CONFIG_HS20
    290 	/* Clear possibly configured frame filters */
    291 	wpa_drv_configure_frame_filters(wpa_s, 0);
    292 #endif /* CONFIG_HS20 */
    293 
    294 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
    295 		return;
    296 
    297 	if (os_reltime_initialized(&wpa_s->session_start)) {
    298 		os_reltime_age(&wpa_s->session_start, &wpa_s->session_length);
    299 		wpa_s->session_start.sec = 0;
    300 		wpa_s->session_start.usec = 0;
    301 		wpas_notify_session_length(wpa_s);
    302 	}
    303 
    304 	wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
    305 	bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
    306 	os_memset(wpa_s->bssid, 0, ETH_ALEN);
    307 	os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
    308 	sme_clear_on_disassoc(wpa_s);
    309 	wpa_s->current_bss = NULL;
    310 	wpa_s->assoc_freq = 0;
    311 
    312 	if (bssid_changed)
    313 		wpas_notify_bssid_changed(wpa_s);
    314 
    315 	eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
    316 	eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
    317 	if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
    318 	    wpa_s->key_mgmt == WPA_KEY_MGMT_OWE ||
    319 	    wpa_s->key_mgmt == WPA_KEY_MGMT_DPP)
    320 		eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
    321 	wpa_s->ap_ies_from_associnfo = 0;
    322 	wpa_s->current_ssid = NULL;
    323 	eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
    324 	wpa_s->key_mgmt = 0;
    325 
    326 	wpas_rrm_reset(wpa_s);
    327 	wpa_s->wnmsleep_used = 0;
    328 	wnm_clear_coloc_intf_reporting(wpa_s);
    329 
    330 #ifdef CONFIG_TESTING_OPTIONS
    331 	wpa_s->last_tk_alg = WPA_ALG_NONE;
    332 	os_memset(wpa_s->last_tk, 0, sizeof(wpa_s->last_tk));
    333 #endif /* CONFIG_TESTING_OPTIONS */
    334 	wpa_s->ieee80211ac = 0;
    335 
    336 	if (wpa_s->enabled_4addr_mode && wpa_drv_set_4addr_mode(wpa_s, 0) == 0)
    337 		wpa_s->enabled_4addr_mode = 0;
    338 }
    339 
    340 
    341 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s)
    342 {
    343 	struct wpa_ie_data ie;
    344 	int pmksa_set = -1;
    345 	size_t i;
    346 
    347 	if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
    348 	    ie.pmkid == NULL)
    349 		return;
    350 
    351 	for (i = 0; i < ie.num_pmkid; i++) {
    352 		pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
    353 						    ie.pmkid + i * PMKID_LEN,
    354 						    NULL, NULL, 0, NULL, 0);
    355 		if (pmksa_set == 0) {
    356 			eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
    357 			break;
    358 		}
    359 	}
    360 
    361 	wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
    362 		"PMKSA cache", pmksa_set == 0 ? "" : "not ");
    363 }
    364 
    365 
    366 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
    367 						 union wpa_event_data *data)
    368 {
    369 	if (data == NULL) {
    370 		wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
    371 			"event");
    372 		return;
    373 	}
    374 	wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
    375 		" index=%d preauth=%d",
    376 		MAC2STR(data->pmkid_candidate.bssid),
    377 		data->pmkid_candidate.index,
    378 		data->pmkid_candidate.preauth);
    379 
    380 	pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
    381 			    data->pmkid_candidate.index,
    382 			    data->pmkid_candidate.preauth);
    383 }
    384 
    385 
    386 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
    387 {
    388 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
    389 	    wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
    390 		return 0;
    391 
    392 #ifdef IEEE8021X_EAPOL
    393 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
    394 	    wpa_s->current_ssid &&
    395 	    !(wpa_s->current_ssid->eapol_flags &
    396 	      (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
    397 	       EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
    398 		/* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
    399 		 * plaintext or static WEP keys). */
    400 		return 0;
    401 	}
    402 #endif /* IEEE8021X_EAPOL */
    403 
    404 	return 1;
    405 }
    406 
    407 
    408 /**
    409  * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
    410  * @wpa_s: pointer to wpa_supplicant data
    411  * @ssid: Configuration data for the network
    412  * Returns: 0 on success, -1 on failure
    413  *
    414  * This function is called when starting authentication with a network that is
    415  * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
    416  */
    417 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
    418 			      struct wpa_ssid *ssid)
    419 {
    420 #ifdef IEEE8021X_EAPOL
    421 #ifdef PCSC_FUNCS
    422 	int aka = 0, sim = 0;
    423 
    424 	if ((ssid != NULL && ssid->eap.pcsc == NULL) ||
    425 	    wpa_s->scard != NULL || wpa_s->conf->external_sim)
    426 		return 0;
    427 
    428 	if (ssid == NULL || ssid->eap.eap_methods == NULL) {
    429 		sim = 1;
    430 		aka = 1;
    431 	} else {
    432 		struct eap_method_type *eap = ssid->eap.eap_methods;
    433 		while (eap->vendor != EAP_VENDOR_IETF ||
    434 		       eap->method != EAP_TYPE_NONE) {
    435 			if (eap->vendor == EAP_VENDOR_IETF) {
    436 				if (eap->method == EAP_TYPE_SIM)
    437 					sim = 1;
    438 				else if (eap->method == EAP_TYPE_AKA ||
    439 					 eap->method == EAP_TYPE_AKA_PRIME)
    440 					aka = 1;
    441 			}
    442 			eap++;
    443 		}
    444 	}
    445 
    446 	if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
    447 		sim = 0;
    448 	if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
    449 	    eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
    450 	    NULL)
    451 		aka = 0;
    452 
    453 	if (!sim && !aka) {
    454 		wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
    455 			"use SIM, but neither EAP-SIM nor EAP-AKA are "
    456 			"enabled");
    457 		return 0;
    458 	}
    459 
    460 	wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
    461 		"(sim=%d aka=%d) - initialize PCSC", sim, aka);
    462 
    463 	wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
    464 	if (wpa_s->scard == NULL) {
    465 		wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
    466 			"(pcsc-lite)");
    467 		return -1;
    468 	}
    469 	wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
    470 	eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
    471 #endif /* PCSC_FUNCS */
    472 #endif /* IEEE8021X_EAPOL */
    473 
    474 	return 0;
    475 }
    476 
    477 
    478 #ifndef CONFIG_NO_SCAN_PROCESSING
    479 
    480 static int has_wep_key(struct wpa_ssid *ssid)
    481 {
    482 	int i;
    483 
    484 	for (i = 0; i < NUM_WEP_KEYS; i++) {
    485 		if (ssid->wep_key_len[i])
    486 			return 1;
    487 	}
    488 
    489 	return 0;
    490 }
    491 
    492 
    493 static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
    494 					struct wpa_ssid *ssid)
    495 {
    496 	int privacy = 0;
    497 
    498 	if (ssid->mixed_cell)
    499 		return 1;
    500 
    501 #ifdef CONFIG_WPS
    502 	if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
    503 		return 1;
    504 #endif /* CONFIG_WPS */
    505 
    506 #ifdef CONFIG_OWE
    507 	if ((ssid->key_mgmt & WPA_KEY_MGMT_OWE) && !ssid->owe_only)
    508 		return 1;
    509 #endif /* CONFIG_OWE */
    510 
    511 	if (has_wep_key(ssid))
    512 		privacy = 1;
    513 
    514 #ifdef IEEE8021X_EAPOL
    515 	if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
    516 	    ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
    517 				 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
    518 		privacy = 1;
    519 #endif /* IEEE8021X_EAPOL */
    520 
    521 	if (wpa_key_mgmt_wpa(ssid->key_mgmt))
    522 		privacy = 1;
    523 
    524 	if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN)
    525 		privacy = 1;
    526 
    527 	if (bss->caps & IEEE80211_CAP_PRIVACY)
    528 		return privacy;
    529 	return !privacy;
    530 }
    531 
    532 
    533 static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
    534 					 struct wpa_ssid *ssid,
    535 					 struct wpa_bss *bss, int debug_print)
    536 {
    537 	struct wpa_ie_data ie;
    538 	int proto_match = 0;
    539 	const u8 *rsn_ie, *wpa_ie;
    540 	int ret;
    541 	int wep_ok;
    542 
    543 	ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
    544 	if (ret >= 0)
    545 		return ret;
    546 
    547 	/* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
    548 	wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
    549 		(((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
    550 		  ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
    551 		 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
    552 
    553 	rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
    554 	while ((ssid->proto & (WPA_PROTO_RSN | WPA_PROTO_OSEN)) && rsn_ie) {
    555 		proto_match++;
    556 
    557 		if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
    558 			if (debug_print)
    559 				wpa_dbg(wpa_s, MSG_DEBUG,
    560 					"   skip RSN IE - parse failed");
    561 			break;
    562 		}
    563 		if (!ie.has_pairwise)
    564 			ie.pairwise_cipher = wpa_default_rsn_cipher(bss->freq);
    565 		if (!ie.has_group)
    566 			ie.group_cipher = wpa_default_rsn_cipher(bss->freq);
    567 
    568 		if (wep_ok &&
    569 		    (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
    570 		{
    571 			if (debug_print)
    572 				wpa_dbg(wpa_s, MSG_DEBUG,
    573 					"   selected based on TSN in RSN IE");
    574 			return 1;
    575 		}
    576 
    577 		if (!(ie.proto & ssid->proto) &&
    578 		    !(ssid->proto & WPA_PROTO_OSEN)) {
    579 			if (debug_print)
    580 				wpa_dbg(wpa_s, MSG_DEBUG,
    581 					"   skip RSN IE - proto mismatch");
    582 			break;
    583 		}
    584 
    585 		if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
    586 			if (debug_print)
    587 				wpa_dbg(wpa_s, MSG_DEBUG,
    588 					"   skip RSN IE - PTK cipher mismatch");
    589 			break;
    590 		}
    591 
    592 		if (!(ie.group_cipher & ssid->group_cipher)) {
    593 			if (debug_print)
    594 				wpa_dbg(wpa_s, MSG_DEBUG,
    595 					"   skip RSN IE - GTK cipher mismatch");
    596 			break;
    597 		}
    598 
    599 		if (ssid->group_mgmt_cipher &&
    600 		    !(ie.mgmt_group_cipher & ssid->group_mgmt_cipher)) {
    601 			if (debug_print)
    602 				wpa_dbg(wpa_s, MSG_DEBUG,
    603 					"   skip RSN IE - group mgmt cipher mismatch");
    604 			break;
    605 		}
    606 
    607 		if (!(ie.key_mgmt & ssid->key_mgmt)) {
    608 			if (debug_print)
    609 				wpa_dbg(wpa_s, MSG_DEBUG,
    610 					"   skip RSN IE - key mgmt mismatch");
    611 			break;
    612 		}
    613 
    614 #ifdef CONFIG_IEEE80211W
    615 		if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
    616 		    wpas_get_ssid_pmf(wpa_s, ssid) ==
    617 		    MGMT_FRAME_PROTECTION_REQUIRED) {
    618 			if (debug_print)
    619 				wpa_dbg(wpa_s, MSG_DEBUG,
    620 					"   skip RSN IE - no mgmt frame protection");
    621 			break;
    622 		}
    623 #endif /* CONFIG_IEEE80211W */
    624 		if ((ie.capabilities & WPA_CAPABILITY_MFPR) &&
    625 		    wpas_get_ssid_pmf(wpa_s, ssid) ==
    626 		    NO_MGMT_FRAME_PROTECTION) {
    627 			if (debug_print)
    628 				wpa_dbg(wpa_s, MSG_DEBUG,
    629 					"   skip RSN IE - no mgmt frame protection enabled but AP requires it");
    630 			break;
    631 		}
    632 #ifdef CONFIG_MBO
    633 		if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
    634 		    wpas_mbo_get_bss_attr(bss, MBO_ATTR_ID_AP_CAPA_IND) &&
    635 		    wpas_get_ssid_pmf(wpa_s, ssid) !=
    636 		    NO_MGMT_FRAME_PROTECTION) {
    637 			if (debug_print)
    638 				wpa_dbg(wpa_s, MSG_DEBUG,
    639 					"   skip RSN IE - no mgmt frame protection enabled on MBO AP");
    640 			break;
    641 		}
    642 #endif /* CONFIG_MBO */
    643 
    644 		if (debug_print)
    645 			wpa_dbg(wpa_s, MSG_DEBUG,
    646 				"   selected based on RSN IE");
    647 		return 1;
    648 	}
    649 
    650 #ifdef CONFIG_IEEE80211W
    651 	if (wpas_get_ssid_pmf(wpa_s, ssid) == MGMT_FRAME_PROTECTION_REQUIRED &&
    652 	    (!(ssid->key_mgmt & WPA_KEY_MGMT_OWE) || ssid->owe_only)) {
    653 		if (debug_print)
    654 			wpa_dbg(wpa_s, MSG_DEBUG,
    655 				"   skip - MFP Required but network not MFP Capable");
    656 		return 0;
    657 	}
    658 #endif /* CONFIG_IEEE80211W */
    659 
    660 	wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
    661 	while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
    662 		proto_match++;
    663 
    664 		if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
    665 			if (debug_print)
    666 				wpa_dbg(wpa_s, MSG_DEBUG,
    667 					"   skip WPA IE - parse failed");
    668 			break;
    669 		}
    670 
    671 		if (wep_ok &&
    672 		    (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
    673 		{
    674 			if (debug_print)
    675 				wpa_dbg(wpa_s, MSG_DEBUG,
    676 					"   selected based on TSN in WPA IE");
    677 			return 1;
    678 		}
    679 
    680 		if (!(ie.proto & ssid->proto)) {
    681 			if (debug_print)
    682 				wpa_dbg(wpa_s, MSG_DEBUG,
    683 					"   skip WPA IE - proto mismatch");
    684 			break;
    685 		}
    686 
    687 		if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
    688 			if (debug_print)
    689 				wpa_dbg(wpa_s, MSG_DEBUG,
    690 					"   skip WPA IE - PTK cipher mismatch");
    691 			break;
    692 		}
    693 
    694 		if (!(ie.group_cipher & ssid->group_cipher)) {
    695 			if (debug_print)
    696 				wpa_dbg(wpa_s, MSG_DEBUG,
    697 					"   skip WPA IE - GTK cipher mismatch");
    698 			break;
    699 		}
    700 
    701 		if (!(ie.key_mgmt & ssid->key_mgmt)) {
    702 			if (debug_print)
    703 				wpa_dbg(wpa_s, MSG_DEBUG,
    704 					"   skip WPA IE - key mgmt mismatch");
    705 			break;
    706 		}
    707 
    708 		if (debug_print)
    709 			wpa_dbg(wpa_s, MSG_DEBUG,
    710 				"   selected based on WPA IE");
    711 		return 1;
    712 	}
    713 
    714 	if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
    715 	    !rsn_ie) {
    716 		if (debug_print)
    717 			wpa_dbg(wpa_s, MSG_DEBUG,
    718 				"   allow for non-WPA IEEE 802.1X");
    719 		return 1;
    720 	}
    721 
    722 #ifdef CONFIG_OWE
    723 	if ((ssid->key_mgmt & WPA_KEY_MGMT_OWE) && !ssid->owe_only &&
    724 	    !wpa_ie && !rsn_ie) {
    725 		if (wpa_s->owe_transition_select &&
    726 		    wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE) &&
    727 		    ssid->owe_transition_bss_select_count + 1 <=
    728 		    MAX_OWE_TRANSITION_BSS_SELECT_COUNT) {
    729 			ssid->owe_transition_bss_select_count++;
    730 			if (debug_print)
    731 				wpa_dbg(wpa_s, MSG_DEBUG,
    732 					"   skip OWE transition BSS (selection count %d does not exceed %d)",
    733 					ssid->owe_transition_bss_select_count,
    734 					MAX_OWE_TRANSITION_BSS_SELECT_COUNT);
    735 			wpa_s->owe_transition_search = 1;
    736 			return 0;
    737 		}
    738 		if (debug_print)
    739 			wpa_dbg(wpa_s, MSG_DEBUG,
    740 				"   allow in OWE transition mode");
    741 		return 1;
    742 	}
    743 #endif /* CONFIG_OWE */
    744 
    745 	if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
    746 	    wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
    747 		if (debug_print)
    748 			wpa_dbg(wpa_s, MSG_DEBUG,
    749 				"   skip - no WPA/RSN proto match");
    750 		return 0;
    751 	}
    752 
    753 	if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) &&
    754 	    wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE)) {
    755 		if (debug_print)
    756 			wpa_dbg(wpa_s, MSG_DEBUG, "   allow in OSEN");
    757 		return 1;
    758 	}
    759 
    760 	if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
    761 		if (debug_print)
    762 			wpa_dbg(wpa_s, MSG_DEBUG, "   allow in non-WPA/WPA2");
    763 		return 1;
    764 	}
    765 
    766 	if (debug_print)
    767 		wpa_dbg(wpa_s, MSG_DEBUG,
    768 			"   reject due to mismatch with WPA/WPA2");
    769 
    770 	return 0;
    771 }
    772 
    773 
    774 static int freq_allowed(int *freqs, int freq)
    775 {
    776 	int i;
    777 
    778 	if (freqs == NULL)
    779 		return 1;
    780 
    781 	for (i = 0; freqs[i]; i++)
    782 		if (freqs[i] == freq)
    783 			return 1;
    784 	return 0;
    785 }
    786 
    787 
    788 static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
    789 		      int debug_print)
    790 {
    791 	const struct hostapd_hw_modes *mode = NULL, *modes;
    792 	const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
    793 	const u8 *rate_ie;
    794 	int i, j, k;
    795 
    796 	if (bss->freq == 0)
    797 		return 1; /* Cannot do matching without knowing band */
    798 
    799 	modes = wpa_s->hw.modes;
    800 	if (modes == NULL) {
    801 		/*
    802 		 * The driver does not provide any additional information
    803 		 * about the utilized hardware, so allow the connection attempt
    804 		 * to continue.
    805 		 */
    806 		return 1;
    807 	}
    808 
    809 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
    810 		for (j = 0; j < modes[i].num_channels; j++) {
    811 			int freq = modes[i].channels[j].freq;
    812 			if (freq == bss->freq) {
    813 				if (mode &&
    814 				    mode->mode == HOSTAPD_MODE_IEEE80211G)
    815 					break; /* do not allow 802.11b replace
    816 						* 802.11g */
    817 				mode = &modes[i];
    818 				break;
    819 			}
    820 		}
    821 	}
    822 
    823 	if (mode == NULL)
    824 		return 0;
    825 
    826 	for (i = 0; i < (int) sizeof(scan_ie); i++) {
    827 		rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
    828 		if (rate_ie == NULL)
    829 			continue;
    830 
    831 		for (j = 2; j < rate_ie[1] + 2; j++) {
    832 			int flagged = !!(rate_ie[j] & 0x80);
    833 			int r = (rate_ie[j] & 0x7f) * 5;
    834 
    835 			/*
    836 			 * IEEE Std 802.11n-2009 7.3.2.2:
    837 			 * The new BSS Membership selector value is encoded
    838 			 * like a legacy basic rate, but it is not a rate and
    839 			 * only indicates if the BSS members are required to
    840 			 * support the mandatory features of Clause 20 [HT PHY]
    841 			 * in order to join the BSS.
    842 			 */
    843 			if (flagged && ((rate_ie[j] & 0x7f) ==
    844 					BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
    845 				if (!ht_supported(mode)) {
    846 					if (debug_print)
    847 						wpa_dbg(wpa_s, MSG_DEBUG,
    848 							"   hardware does not support HT PHY");
    849 					return 0;
    850 				}
    851 				continue;
    852 			}
    853 
    854 			/* There's also a VHT selector for 802.11ac */
    855 			if (flagged && ((rate_ie[j] & 0x7f) ==
    856 					BSS_MEMBERSHIP_SELECTOR_VHT_PHY)) {
    857 				if (!vht_supported(mode)) {
    858 					if (debug_print)
    859 						wpa_dbg(wpa_s, MSG_DEBUG,
    860 							"   hardware does not support VHT PHY");
    861 					return 0;
    862 				}
    863 				continue;
    864 			}
    865 
    866 			if (!flagged)
    867 				continue;
    868 
    869 			/* check for legacy basic rates */
    870 			for (k = 0; k < mode->num_rates; k++) {
    871 				if (mode->rates[k] == r)
    872 					break;
    873 			}
    874 			if (k == mode->num_rates) {
    875 				/*
    876 				 * IEEE Std 802.11-2007 7.3.2.2 demands that in
    877 				 * order to join a BSS all required rates
    878 				 * have to be supported by the hardware.
    879 				 */
    880 				if (debug_print)
    881 					wpa_dbg(wpa_s, MSG_DEBUG,
    882 						"   hardware does not support required rate %d.%d Mbps (freq=%d mode==%d num_rates=%d)",
    883 						r / 10, r % 10,
    884 						bss->freq, mode->mode, mode->num_rates);
    885 				return 0;
    886 			}
    887 		}
    888 	}
    889 
    890 	return 1;
    891 }
    892 
    893 
    894 /*
    895  * Test whether BSS is in an ESS.
    896  * This is done differently in DMG (60 GHz) and non-DMG bands
    897  */
    898 static int bss_is_ess(struct wpa_bss *bss)
    899 {
    900 	if (bss_is_dmg(bss)) {
    901 		return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
    902 			IEEE80211_CAP_DMG_AP;
    903 	}
    904 
    905 	return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
    906 		IEEE80211_CAP_ESS);
    907 }
    908 
    909 
    910 static int match_mac_mask(const u8 *addr_a, const u8 *addr_b, const u8 *mask)
    911 {
    912 	size_t i;
    913 
    914 	for (i = 0; i < ETH_ALEN; i++) {
    915 		if ((addr_a[i] & mask[i]) != (addr_b[i] & mask[i]))
    916 			return 0;
    917 	}
    918 	return 1;
    919 }
    920 
    921 
    922 static int addr_in_list(const u8 *addr, const u8 *list, size_t num)
    923 {
    924 	size_t i;
    925 
    926 	for (i = 0; i < num; i++) {
    927 		const u8 *a = list + i * ETH_ALEN * 2;
    928 		const u8 *m = a + ETH_ALEN;
    929 
    930 		if (match_mac_mask(a, addr, m))
    931 			return 1;
    932 	}
    933 	return 0;
    934 }
    935 
    936 
    937 static void owe_trans_ssid(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
    938 			   const u8 **ret_ssid, size_t *ret_ssid_len)
    939 {
    940 #ifdef CONFIG_OWE
    941 	const u8 *owe, *pos, *end, *bssid;
    942 	u8 ssid_len;
    943 	struct wpa_bss *open_bss;
    944 
    945 	owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
    946 	if (!owe || !wpa_bss_get_ie(bss, WLAN_EID_RSN))
    947 		return;
    948 
    949 	pos = owe + 6;
    950 	end = owe + 2 + owe[1];
    951 
    952 	if (end - pos < ETH_ALEN + 1)
    953 		return;
    954 	bssid = pos;
    955 	pos += ETH_ALEN;
    956 	ssid_len = *pos++;
    957 	if (end - pos < ssid_len || ssid_len > SSID_MAX_LEN)
    958 		return;
    959 
    960 	/* Match the profile SSID against the OWE transition mode SSID on the
    961 	 * open network. */
    962 	wpa_dbg(wpa_s, MSG_DEBUG, "OWE: transition mode BSSID: " MACSTR
    963 		" SSID: %s", MAC2STR(bssid), wpa_ssid_txt(pos, ssid_len));
    964 	*ret_ssid = pos;
    965 	*ret_ssid_len = ssid_len;
    966 
    967 	if (bss->ssid_len > 0)
    968 		return;
    969 
    970 	open_bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
    971 	if (!open_bss)
    972 		return;
    973 	if (ssid_len != open_bss->ssid_len ||
    974 	    os_memcmp(pos, open_bss->ssid, ssid_len) != 0) {
    975 		wpa_dbg(wpa_s, MSG_DEBUG,
    976 			"OWE: transition mode SSID mismatch: %s",
    977 			wpa_ssid_txt(open_bss->ssid, open_bss->ssid_len));
    978 		return;
    979 	}
    980 
    981 	owe = wpa_bss_get_vendor_ie(open_bss, OWE_IE_VENDOR_TYPE);
    982 	if (!owe || wpa_bss_get_ie(open_bss, WLAN_EID_RSN)) {
    983 		wpa_dbg(wpa_s, MSG_DEBUG,
    984 			"OWE: transition mode open BSS unexpected info");
    985 		return;
    986 	}
    987 
    988 	pos = owe + 6;
    989 	end = owe + 2 + owe[1];
    990 
    991 	if (end - pos < ETH_ALEN + 1)
    992 		return;
    993 	if (os_memcmp(pos, bss->bssid, ETH_ALEN) != 0) {
    994 		wpa_dbg(wpa_s, MSG_DEBUG,
    995 			"OWE: transition mode BSSID mismatch: " MACSTR,
    996 			MAC2STR(pos));
    997 		return;
    998 	}
    999 	pos += ETH_ALEN;
   1000 	ssid_len = *pos++;
   1001 	if (end - pos < ssid_len || ssid_len > SSID_MAX_LEN)
   1002 		return;
   1003 	wpa_dbg(wpa_s, MSG_DEBUG, "OWE: learned transition mode OWE SSID: %s",
   1004 		wpa_ssid_txt(pos, ssid_len));
   1005 	os_memcpy(bss->ssid, pos, ssid_len);
   1006 	bss->ssid_len = ssid_len;
   1007 #endif /* CONFIG_OWE */
   1008 }
   1009 
   1010 
   1011 struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
   1012 				     int i, struct wpa_bss *bss,
   1013 				     struct wpa_ssid *group,
   1014 				     int only_first_ssid, int debug_print)
   1015 {
   1016 	u8 wpa_ie_len, rsn_ie_len;
   1017 	int wpa;
   1018 	struct wpa_blacklist *e;
   1019 	const u8 *ie;
   1020 	struct wpa_ssid *ssid;
   1021 	int osen, rsn_osen = 0;
   1022 #ifdef CONFIG_MBO
   1023 	const u8 *assoc_disallow;
   1024 #endif /* CONFIG_MBO */
   1025 	const u8 *match_ssid;
   1026 	size_t match_ssid_len;
   1027 	struct wpa_ie_data data;
   1028 
   1029 	ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
   1030 	wpa_ie_len = ie ? ie[1] : 0;
   1031 
   1032 	ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
   1033 	rsn_ie_len = ie ? ie[1] : 0;
   1034 	if (ie && wpa_parse_wpa_ie_rsn(ie, 2 + ie[1], &data) == 0 &&
   1035 	    (data.key_mgmt & WPA_KEY_MGMT_OSEN))
   1036 		rsn_osen = 1;
   1037 
   1038 	ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
   1039 	osen = ie != NULL;
   1040 
   1041 	if (debug_print) {
   1042 		wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR
   1043 			" ssid='%s' wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d freq=%d %s%s%s",
   1044 			i, MAC2STR(bss->bssid),
   1045 			wpa_ssid_txt(bss->ssid, bss->ssid_len),
   1046 			wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
   1047 			bss->freq,
   1048 			wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ?
   1049 			" wps" : "",
   1050 			(wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
   1051 			 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE))
   1052 			? " p2p" : "",
   1053 			osen ? " osen=1" : "");
   1054 	}
   1055 
   1056 	e = wpa_blacklist_get(wpa_s, bss->bssid);
   1057 	if (e) {
   1058 		int limit = 1;
   1059 		if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
   1060 			/*
   1061 			 * When only a single network is enabled, we can
   1062 			 * trigger blacklisting on the first failure. This
   1063 			 * should not be done with multiple enabled networks to
   1064 			 * avoid getting forced to move into a worse ESS on
   1065 			 * single error if there are no other BSSes of the
   1066 			 * current ESS.
   1067 			 */
   1068 			limit = 0;
   1069 		}
   1070 		if (e->count > limit) {
   1071 			if (debug_print) {
   1072 				wpa_dbg(wpa_s, MSG_DEBUG,
   1073 					"   skip - blacklisted (count=%d limit=%d)",
   1074 					e->count, limit);
   1075 			}
   1076 			return NULL;
   1077 		}
   1078 	}
   1079 
   1080 	match_ssid = bss->ssid;
   1081 	match_ssid_len = bss->ssid_len;
   1082 	owe_trans_ssid(wpa_s, bss, &match_ssid, &match_ssid_len);
   1083 
   1084 	if (match_ssid_len == 0) {
   1085 		if (debug_print)
   1086 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID not known");
   1087 		return NULL;
   1088 	}
   1089 
   1090 	if (disallowed_bssid(wpa_s, bss->bssid)) {
   1091 		if (debug_print)
   1092 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - BSSID disallowed");
   1093 		return NULL;
   1094 	}
   1095 
   1096 	if (disallowed_ssid(wpa_s, match_ssid, match_ssid_len)) {
   1097 		if (debug_print)
   1098 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID disallowed");
   1099 		return NULL;
   1100 	}
   1101 
   1102 	wpa = wpa_ie_len > 0 || rsn_ie_len > 0;
   1103 
   1104 	for (ssid = group; ssid; ssid = only_first_ssid ? NULL : ssid->pnext) {
   1105 		int check_ssid = wpa ? 1 : (ssid->ssid_len != 0);
   1106 		int res;
   1107 
   1108 		if (wpas_network_disabled(wpa_s, ssid)) {
   1109 			if (debug_print)
   1110 				wpa_dbg(wpa_s, MSG_DEBUG, "   skip - disabled");
   1111 			continue;
   1112 		}
   1113 
   1114 		res = wpas_temp_disabled(wpa_s, ssid);
   1115 		if (res > 0) {
   1116 			if (debug_print)
   1117 				wpa_dbg(wpa_s, MSG_DEBUG,
   1118 					"   skip - disabled temporarily for %d second(s)",
   1119 					res);
   1120 			continue;
   1121 		}
   1122 
   1123 #ifdef CONFIG_WPS
   1124 		if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && e && e->count > 0) {
   1125 			if (debug_print)
   1126 				wpa_dbg(wpa_s, MSG_DEBUG,
   1127 					"   skip - blacklisted (WPS)");
   1128 			continue;
   1129 		}
   1130 
   1131 		if (wpa && ssid->ssid_len == 0 &&
   1132 		    wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
   1133 			check_ssid = 0;
   1134 
   1135 		if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
   1136 			/* Only allow wildcard SSID match if an AP
   1137 			 * advertises active WPS operation that matches
   1138 			 * with our mode. */
   1139 			check_ssid = 1;
   1140 			if (ssid->ssid_len == 0 &&
   1141 			    wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
   1142 				check_ssid = 0;
   1143 		}
   1144 #endif /* CONFIG_WPS */
   1145 
   1146 		if (ssid->bssid_set && ssid->ssid_len == 0 &&
   1147 		    os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) == 0)
   1148 			check_ssid = 0;
   1149 
   1150 		if (check_ssid &&
   1151 		    (match_ssid_len != ssid->ssid_len ||
   1152 		     os_memcmp(match_ssid, ssid->ssid, match_ssid_len) != 0)) {
   1153 			if (debug_print)
   1154 				wpa_dbg(wpa_s, MSG_DEBUG,
   1155 					"   skip - SSID mismatch");
   1156 			continue;
   1157 		}
   1158 
   1159 		if (ssid->bssid_set &&
   1160 		    os_memcmp(bss->bssid, ssid->bssid, ETH_ALEN) != 0) {
   1161 			if (debug_print)
   1162 				wpa_dbg(wpa_s, MSG_DEBUG,
   1163 					"   skip - BSSID mismatch");
   1164 			continue;
   1165 		}
   1166 
   1167 		/* check blacklist */
   1168 		if (ssid->num_bssid_blacklist &&
   1169 		    addr_in_list(bss->bssid, ssid->bssid_blacklist,
   1170 				 ssid->num_bssid_blacklist)) {
   1171 			if (debug_print)
   1172 				wpa_dbg(wpa_s, MSG_DEBUG,
   1173 					"   skip - BSSID blacklisted");
   1174 			continue;
   1175 		}
   1176 
   1177 		/* if there is a whitelist, only accept those APs */
   1178 		if (ssid->num_bssid_whitelist &&
   1179 		    !addr_in_list(bss->bssid, ssid->bssid_whitelist,
   1180 				  ssid->num_bssid_whitelist)) {
   1181 			if (debug_print)
   1182 				wpa_dbg(wpa_s, MSG_DEBUG,
   1183 					"   skip - BSSID not in whitelist");
   1184 			continue;
   1185 		}
   1186 
   1187 		if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss,
   1188 						   debug_print))
   1189 			continue;
   1190 
   1191 		if (!osen && !wpa &&
   1192 		    !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
   1193 		    !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
   1194 		    !(ssid->key_mgmt & WPA_KEY_MGMT_OWE) &&
   1195 		    !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
   1196 			if (debug_print)
   1197 				wpa_dbg(wpa_s, MSG_DEBUG,
   1198 					"   skip - non-WPA network not allowed");
   1199 			continue;
   1200 		}
   1201 
   1202 		if (wpa && !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
   1203 		    has_wep_key(ssid)) {
   1204 			if (debug_print)
   1205 				wpa_dbg(wpa_s, MSG_DEBUG,
   1206 					"   skip - ignore WPA/WPA2 AP for WEP network block");
   1207 			continue;
   1208 		}
   1209 
   1210 		if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) && !osen &&
   1211 		    !rsn_osen) {
   1212 			if (debug_print)
   1213 				wpa_dbg(wpa_s, MSG_DEBUG,
   1214 					"   skip - non-OSEN network not allowed");
   1215 			continue;
   1216 		}
   1217 
   1218 		if (!wpa_supplicant_match_privacy(bss, ssid)) {
   1219 			if (debug_print)
   1220 				wpa_dbg(wpa_s, MSG_DEBUG,
   1221 					"   skip - privacy mismatch");
   1222 			continue;
   1223 		}
   1224 
   1225 		if (ssid->mode != IEEE80211_MODE_MESH && !bss_is_ess(bss) &&
   1226 		    !bss_is_pbss(bss)) {
   1227 			if (debug_print)
   1228 				wpa_dbg(wpa_s, MSG_DEBUG,
   1229 					"   skip - not ESS, PBSS, or MBSS");
   1230 			continue;
   1231 		}
   1232 
   1233 		if (ssid->pbss != 2 && ssid->pbss != bss_is_pbss(bss)) {
   1234 			if (debug_print)
   1235 				wpa_dbg(wpa_s, MSG_DEBUG,
   1236 					"   skip - PBSS mismatch (ssid %d bss %d)",
   1237 					ssid->pbss, bss_is_pbss(bss));
   1238 			continue;
   1239 		}
   1240 
   1241 		if (!freq_allowed(ssid->freq_list, bss->freq)) {
   1242 			if (debug_print)
   1243 				wpa_dbg(wpa_s, MSG_DEBUG,
   1244 					"   skip - frequency not allowed");
   1245 			continue;
   1246 		}
   1247 
   1248 #ifdef CONFIG_MESH
   1249 		if (ssid->mode == IEEE80211_MODE_MESH && ssid->frequency > 0 &&
   1250 		    ssid->frequency != bss->freq) {
   1251 			if (debug_print)
   1252 				wpa_dbg(wpa_s, MSG_DEBUG,
   1253 					"   skip - frequency not allowed (mesh)");
   1254 			continue;
   1255 		}
   1256 #endif /* CONFIG_MESH */
   1257 
   1258 		if (!rate_match(wpa_s, bss, debug_print)) {
   1259 			if (debug_print)
   1260 				wpa_dbg(wpa_s, MSG_DEBUG,
   1261 					"   skip - rate sets do not match");
   1262 			continue;
   1263 		}
   1264 
   1265 #ifndef CONFIG_IBSS_RSN
   1266 		if (ssid->mode == WPAS_MODE_IBSS &&
   1267 		    !(ssid->key_mgmt & (WPA_KEY_MGMT_NONE |
   1268 					WPA_KEY_MGMT_WPA_NONE))) {
   1269 			if (debug_print)
   1270 				wpa_dbg(wpa_s, MSG_DEBUG,
   1271 					"   skip - IBSS RSN not supported in the build");
   1272 			continue;
   1273 		}
   1274 #endif /* !CONFIG_IBSS_RSN */
   1275 
   1276 #ifdef CONFIG_P2P
   1277 		if (ssid->p2p_group &&
   1278 		    !wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
   1279 		    !wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
   1280 			if (debug_print)
   1281 				wpa_dbg(wpa_s, MSG_DEBUG,
   1282 					"   skip - no P2P IE seen");
   1283 			continue;
   1284 		}
   1285 
   1286 		if (!is_zero_ether_addr(ssid->go_p2p_dev_addr)) {
   1287 			struct wpabuf *p2p_ie;
   1288 			u8 dev_addr[ETH_ALEN];
   1289 
   1290 			ie = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
   1291 			if (ie == NULL) {
   1292 				if (debug_print)
   1293 					wpa_dbg(wpa_s, MSG_DEBUG,
   1294 						"   skip - no P2P element");
   1295 				continue;
   1296 			}
   1297 			p2p_ie = wpa_bss_get_vendor_ie_multi(
   1298 				bss, P2P_IE_VENDOR_TYPE);
   1299 			if (p2p_ie == NULL) {
   1300 				if (debug_print)
   1301 					wpa_dbg(wpa_s, MSG_DEBUG,
   1302 						"   skip - could not fetch P2P element");
   1303 				continue;
   1304 			}
   1305 
   1306 			if (p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr) < 0
   1307 			    || os_memcmp(dev_addr, ssid->go_p2p_dev_addr,
   1308 					 ETH_ALEN) != 0) {
   1309 				if (debug_print)
   1310 					wpa_dbg(wpa_s, MSG_DEBUG,
   1311 						"   skip - no matching GO P2P Device Address in P2P element");
   1312 				wpabuf_free(p2p_ie);
   1313 				continue;
   1314 			}
   1315 			wpabuf_free(p2p_ie);
   1316 		}
   1317 
   1318 		/*
   1319 		 * TODO: skip the AP if its P2P IE has Group Formation
   1320 		 * bit set in the P2P Group Capability Bitmap and we
   1321 		 * are not in Group Formation with that device.
   1322 		 */
   1323 #endif /* CONFIG_P2P */
   1324 
   1325 		if (os_reltime_before(&bss->last_update, &wpa_s->scan_min_time))
   1326 		{
   1327 			struct os_reltime diff;
   1328 
   1329 			os_reltime_sub(&wpa_s->scan_min_time,
   1330 				       &bss->last_update, &diff);
   1331 			if (debug_print)
   1332 				wpa_dbg(wpa_s, MSG_DEBUG,
   1333 					"   skip - scan result not recent enough (%u.%06u seconds too old)",
   1334 				(unsigned int) diff.sec,
   1335 				(unsigned int) diff.usec);
   1336 			continue;
   1337 		}
   1338 #ifdef CONFIG_MBO
   1339 #ifdef CONFIG_TESTING_OPTIONS
   1340 		if (wpa_s->ignore_assoc_disallow)
   1341 			goto skip_assoc_disallow;
   1342 #endif /* CONFIG_TESTING_OPTIONS */
   1343 		assoc_disallow = wpas_mbo_get_bss_attr(
   1344 			bss, MBO_ATTR_ID_ASSOC_DISALLOW);
   1345 		if (assoc_disallow && assoc_disallow[1] >= 1) {
   1346 			if (debug_print)
   1347 				wpa_dbg(wpa_s, MSG_DEBUG,
   1348 					"   skip - MBO association disallowed (reason %u)",
   1349 				assoc_disallow[2]);
   1350 			continue;
   1351 		}
   1352 
   1353 		if (wpa_is_bss_tmp_disallowed(wpa_s, bss)) {
   1354 			if (debug_print)
   1355 				wpa_dbg(wpa_s, MSG_DEBUG,
   1356 					"   skip - AP temporarily disallowed");
   1357 			continue;
   1358 		}
   1359 #ifdef CONFIG_TESTING_OPTIONS
   1360 	skip_assoc_disallow:
   1361 #endif /* CONFIG_TESTING_OPTIONS */
   1362 #endif /* CONFIG_MBO */
   1363 
   1364 #ifdef CONFIG_DPP
   1365 		if ((ssid->key_mgmt & WPA_KEY_MGMT_DPP) &&
   1366 		    !wpa_sm_pmksa_exists(wpa_s->wpa, bss->bssid, ssid) &&
   1367 		    (!ssid->dpp_connector ||
   1368 		     !ssid->dpp_netaccesskey ||
   1369 		     !ssid->dpp_csign)) {
   1370 			if (debug_print)
   1371 				wpa_dbg(wpa_s, MSG_DEBUG,
   1372 					"   skip - no PMKSA entry for DPP");
   1373 			continue;
   1374 		}
   1375 #endif /* CONFIG_DPP */
   1376 
   1377 		/* Matching configuration found */
   1378 		return ssid;
   1379 	}
   1380 
   1381 	/* No matching configuration found */
   1382 	return NULL;
   1383 }
   1384 
   1385 
   1386 static struct wpa_bss *
   1387 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
   1388 			  struct wpa_ssid *group,
   1389 			  struct wpa_ssid **selected_ssid,
   1390 			  int only_first_ssid)
   1391 {
   1392 	unsigned int i;
   1393 
   1394 	if (wpa_s->current_ssid) {
   1395 		struct wpa_ssid *ssid;
   1396 
   1397 		wpa_dbg(wpa_s, MSG_DEBUG,
   1398 			"Scan results matching the currently selected network");
   1399 		for (i = 0; i < wpa_s->last_scan_res_used; i++) {
   1400 			struct wpa_bss *bss = wpa_s->last_scan_res[i];
   1401 
   1402 			ssid = wpa_scan_res_match(wpa_s, i, bss, group,
   1403 						  only_first_ssid, 0);
   1404 			if (ssid != wpa_s->current_ssid)
   1405 				continue;
   1406 			wpa_dbg(wpa_s, MSG_DEBUG, "%u: " MACSTR
   1407 				" freq=%d level=%d snr=%d est_throughput=%u",
   1408 				i, MAC2STR(bss->bssid), bss->freq, bss->level,
   1409 				bss->snr, bss->est_throughput);
   1410 		}
   1411 	}
   1412 
   1413 	if (only_first_ssid)
   1414 		wpa_dbg(wpa_s, MSG_DEBUG, "Try to find BSS matching pre-selected network id=%d",
   1415 			group->id);
   1416 	else
   1417 		wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
   1418 			group->priority);
   1419 
   1420 	for (i = 0; i < wpa_s->last_scan_res_used; i++) {
   1421 		struct wpa_bss *bss = wpa_s->last_scan_res[i];
   1422 
   1423 		wpa_s->owe_transition_select = 1;
   1424 		*selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group,
   1425 						    only_first_ssid, 1);
   1426 		wpa_s->owe_transition_select = 0;
   1427 		if (!*selected_ssid)
   1428 			continue;
   1429 		wpa_dbg(wpa_s, MSG_DEBUG, "   selected BSS " MACSTR
   1430 			" ssid='%s'",
   1431 			MAC2STR(bss->bssid),
   1432 			wpa_ssid_txt(bss->ssid, bss->ssid_len));
   1433 		return bss;
   1434 	}
   1435 
   1436 	return NULL;
   1437 }
   1438 
   1439 
   1440 struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
   1441 					     struct wpa_ssid **selected_ssid)
   1442 {
   1443 	struct wpa_bss *selected = NULL;
   1444 	int prio;
   1445 	struct wpa_ssid *next_ssid = NULL;
   1446 	struct wpa_ssid *ssid;
   1447 
   1448 	if (wpa_s->last_scan_res == NULL ||
   1449 	    wpa_s->last_scan_res_used == 0)
   1450 		return NULL; /* no scan results from last update */
   1451 
   1452 	if (wpa_s->next_ssid) {
   1453 		/* check that next_ssid is still valid */
   1454 		for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
   1455 			if (ssid == wpa_s->next_ssid)
   1456 				break;
   1457 		}
   1458 		next_ssid = ssid;
   1459 		wpa_s->next_ssid = NULL;
   1460 	}
   1461 
   1462 	while (selected == NULL) {
   1463 		for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
   1464 			if (next_ssid && next_ssid->priority ==
   1465 			    wpa_s->conf->pssid[prio]->priority) {
   1466 				selected = wpa_supplicant_select_bss(
   1467 					wpa_s, next_ssid, selected_ssid, 1);
   1468 				if (selected)
   1469 					break;
   1470 			}
   1471 			selected = wpa_supplicant_select_bss(
   1472 				wpa_s, wpa_s->conf->pssid[prio],
   1473 				selected_ssid, 0);
   1474 			if (selected)
   1475 				break;
   1476 		}
   1477 
   1478 		if (selected == NULL && wpa_s->blacklist &&
   1479 		    !wpa_s->countermeasures) {
   1480 			wpa_dbg(wpa_s, MSG_DEBUG, "No APs found - clear "
   1481 				"blacklist and try again");
   1482 			wpa_blacklist_clear(wpa_s);
   1483 			wpa_s->blacklist_cleared++;
   1484 		} else if (selected == NULL)
   1485 			break;
   1486 	}
   1487 
   1488 	ssid = *selected_ssid;
   1489 	if (selected && ssid && ssid->mem_only_psk && !ssid->psk_set &&
   1490 	    !ssid->passphrase && !ssid->ext_psk) {
   1491 		const char *field_name, *txt = NULL;
   1492 
   1493 		wpa_dbg(wpa_s, MSG_DEBUG,
   1494 			"PSK/passphrase not yet available for the selected network");
   1495 
   1496 		wpas_notify_network_request(wpa_s, ssid,
   1497 					    WPA_CTRL_REQ_PSK_PASSPHRASE, NULL);
   1498 
   1499 		field_name = wpa_supplicant_ctrl_req_to_string(
   1500 			WPA_CTRL_REQ_PSK_PASSPHRASE, NULL, &txt);
   1501 		if (field_name == NULL)
   1502 			return NULL;
   1503 
   1504 		wpas_send_ctrl_req(wpa_s, ssid, field_name, txt);
   1505 
   1506 		selected = NULL;
   1507 	}
   1508 
   1509 	return selected;
   1510 }
   1511 
   1512 
   1513 static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
   1514 					int timeout_sec, int timeout_usec)
   1515 {
   1516 	if (!wpa_supplicant_enabled_networks(wpa_s)) {
   1517 		/*
   1518 		 * No networks are enabled; short-circuit request so
   1519 		 * we don't wait timeout seconds before transitioning
   1520 		 * to INACTIVE state.
   1521 		 */
   1522 		wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
   1523 			"since there are no enabled networks");
   1524 		wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
   1525 		return;
   1526 	}
   1527 
   1528 	wpa_s->scan_for_connection = 1;
   1529 	wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
   1530 }
   1531 
   1532 
   1533 int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
   1534 			   struct wpa_bss *selected,
   1535 			   struct wpa_ssid *ssid)
   1536 {
   1537 	if (wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
   1538 		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
   1539 			"PBC session overlap");
   1540 		wpas_notify_wps_event_pbc_overlap(wpa_s);
   1541 #ifdef CONFIG_P2P
   1542 		if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
   1543 		    wpa_s->p2p_in_provisioning) {
   1544 			eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb,
   1545 					       wpa_s, NULL);
   1546 			return -1;
   1547 		}
   1548 #endif /* CONFIG_P2P */
   1549 
   1550 #ifdef CONFIG_WPS
   1551 		wpas_wps_pbc_overlap(wpa_s);
   1552 		wpas_wps_cancel(wpa_s);
   1553 #endif /* CONFIG_WPS */
   1554 		return -1;
   1555 	}
   1556 
   1557 	wpa_msg(wpa_s, MSG_DEBUG,
   1558 		"Considering connect request: reassociate: %d  selected: "
   1559 		MACSTR "  bssid: " MACSTR "  pending: " MACSTR
   1560 		"  wpa_state: %s  ssid=%p  current_ssid=%p",
   1561 		wpa_s->reassociate, MAC2STR(selected->bssid),
   1562 		MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
   1563 		wpa_supplicant_state_txt(wpa_s->wpa_state),
   1564 		ssid, wpa_s->current_ssid);
   1565 
   1566 	/*
   1567 	 * Do not trigger new association unless the BSSID has changed or if
   1568 	 * reassociation is requested. If we are in process of associating with
   1569 	 * the selected BSSID, do not trigger new attempt.
   1570 	 */
   1571 	if (wpa_s->reassociate ||
   1572 	    (os_memcmp(selected->bssid, wpa_s->bssid, ETH_ALEN) != 0 &&
   1573 	     ((wpa_s->wpa_state != WPA_ASSOCIATING &&
   1574 	       wpa_s->wpa_state != WPA_AUTHENTICATING) ||
   1575 	      (!is_zero_ether_addr(wpa_s->pending_bssid) &&
   1576 	       os_memcmp(selected->bssid, wpa_s->pending_bssid, ETH_ALEN) !=
   1577 	       0) ||
   1578 	      (is_zero_ether_addr(wpa_s->pending_bssid) &&
   1579 	       ssid != wpa_s->current_ssid)))) {
   1580 		if (wpa_supplicant_scard_init(wpa_s, ssid)) {
   1581 			wpa_supplicant_req_new_scan(wpa_s, 10, 0);
   1582 			return 0;
   1583 		}
   1584 		wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
   1585 			MAC2STR(selected->bssid));
   1586 		wpa_supplicant_associate(wpa_s, selected, ssid);
   1587 	} else {
   1588 		wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
   1589 			"connect with the selected AP");
   1590 	}
   1591 
   1592 	return 0;
   1593 }
   1594 
   1595 
   1596 static struct wpa_ssid *
   1597 wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
   1598 {
   1599 	int prio;
   1600 	struct wpa_ssid *ssid;
   1601 
   1602 	for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
   1603 		for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
   1604 		{
   1605 			if (wpas_network_disabled(wpa_s, ssid))
   1606 				continue;
   1607 #ifndef CONFIG_IBSS_RSN
   1608 			if (ssid->mode == WPAS_MODE_IBSS &&
   1609 			    !(ssid->key_mgmt & (WPA_KEY_MGMT_NONE |
   1610 						WPA_KEY_MGMT_WPA_NONE))) {
   1611 				wpa_msg(wpa_s, MSG_INFO,
   1612 					"IBSS RSN not supported in the build - cannot use the profile for SSID '%s'",
   1613 					wpa_ssid_txt(ssid->ssid,
   1614 						     ssid->ssid_len));
   1615 				continue;
   1616 			}
   1617 #endif /* !CONFIG_IBSS_RSN */
   1618 			if (ssid->mode == IEEE80211_MODE_IBSS ||
   1619 			    ssid->mode == IEEE80211_MODE_AP ||
   1620 			    ssid->mode == IEEE80211_MODE_MESH)
   1621 				return ssid;
   1622 		}
   1623 	}
   1624 	return NULL;
   1625 }
   1626 
   1627 
   1628 /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
   1629  * on BSS added and BSS changed events */
   1630 static void wpa_supplicant_rsn_preauth_scan_results(
   1631 	struct wpa_supplicant *wpa_s)
   1632 {
   1633 	struct wpa_bss *bss;
   1634 
   1635 	if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
   1636 		return;
   1637 
   1638 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
   1639 		const u8 *ssid, *rsn;
   1640 
   1641 		ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
   1642 		if (ssid == NULL)
   1643 			continue;
   1644 
   1645 		rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
   1646 		if (rsn == NULL)
   1647 			continue;
   1648 
   1649 		rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
   1650 	}
   1651 
   1652 }
   1653 
   1654 
   1655 static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
   1656 				       struct wpa_bss *selected,
   1657 				       struct wpa_ssid *ssid)
   1658 {
   1659 	struct wpa_bss *current_bss = NULL;
   1660 #ifndef CONFIG_NO_ROAMING
   1661 	int min_diff, diff;
   1662 	int to_5ghz;
   1663 	int cur_est, sel_est;
   1664 #endif /* CONFIG_NO_ROAMING */
   1665 
   1666 	if (wpa_s->reassociate)
   1667 		return 1; /* explicit request to reassociate */
   1668 	if (wpa_s->wpa_state < WPA_ASSOCIATED)
   1669 		return 1; /* we are not associated; continue */
   1670 	if (wpa_s->current_ssid == NULL)
   1671 		return 1; /* unknown current SSID */
   1672 	if (wpa_s->current_ssid != ssid)
   1673 		return 1; /* different network block */
   1674 
   1675 	if (wpas_driver_bss_selection(wpa_s))
   1676 		return 0; /* Driver-based roaming */
   1677 
   1678 	if (wpa_s->current_ssid->ssid)
   1679 		current_bss = wpa_bss_get(wpa_s, wpa_s->bssid,
   1680 					  wpa_s->current_ssid->ssid,
   1681 					  wpa_s->current_ssid->ssid_len);
   1682 	if (!current_bss)
   1683 		current_bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
   1684 
   1685 	if (!current_bss)
   1686 		return 1; /* current BSS not seen in scan results */
   1687 
   1688 	if (current_bss == selected)
   1689 		return 0;
   1690 
   1691 	if (selected->last_update_idx > current_bss->last_update_idx)
   1692 		return 1; /* current BSS not seen in the last scan */
   1693 
   1694 #ifndef CONFIG_NO_ROAMING
   1695 	wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
   1696 	wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR
   1697 		" freq=%d level=%d snr=%d est_throughput=%u",
   1698 		MAC2STR(current_bss->bssid),
   1699 		current_bss->freq, current_bss->level,
   1700 		current_bss->snr, current_bss->est_throughput);
   1701 	wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR
   1702 		" freq=%d level=%d snr=%d est_throughput=%u",
   1703 		MAC2STR(selected->bssid), selected->freq, selected->level,
   1704 		selected->snr, selected->est_throughput);
   1705 
   1706 	if (wpa_s->current_ssid->bssid_set &&
   1707 	    os_memcmp(selected->bssid, wpa_s->current_ssid->bssid, ETH_ALEN) ==
   1708 	    0) {
   1709 		wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
   1710 			"has preferred BSSID");
   1711 		return 1;
   1712 	}
   1713 
   1714 	if (selected->est_throughput > current_bss->est_throughput + 5000) {
   1715 		wpa_dbg(wpa_s, MSG_DEBUG,
   1716 			"Allow reassociation - selected BSS has better estimated throughput");
   1717 		return 1;
   1718 	}
   1719 
   1720 	to_5ghz = selected->freq > 4000 && current_bss->freq < 4000;
   1721 
   1722 	if (current_bss->level < 0 &&
   1723 	    current_bss->level > selected->level + to_5ghz * 2) {
   1724 		wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
   1725 			"signal level");
   1726 		return 0;
   1727 	}
   1728 
   1729 	if (current_bss->est_throughput > selected->est_throughput + 5000) {
   1730 		wpa_dbg(wpa_s, MSG_DEBUG,
   1731 			"Skip roam - Current BSS has better estimated throughput");
   1732 		return 0;
   1733 	}
   1734 
   1735 	cur_est = current_bss->est_throughput;
   1736 	sel_est = selected->est_throughput;
   1737 	min_diff = 2;
   1738 	if (current_bss->level < 0) {
   1739 		if (current_bss->level < -85)
   1740 			min_diff = 1;
   1741 		else if (current_bss->level < -80)
   1742 			min_diff = 2;
   1743 		else if (current_bss->level < -75)
   1744 			min_diff = 3;
   1745 		else if (current_bss->level < -70)
   1746 			min_diff = 4;
   1747 		else
   1748 			min_diff = 5;
   1749 		if (cur_est > sel_est * 1.5)
   1750 			min_diff += 10;
   1751 		else if (cur_est > sel_est * 1.2)
   1752 			min_diff += 5;
   1753 		else if (cur_est > sel_est * 1.1)
   1754 			min_diff += 2;
   1755 		else if (cur_est > sel_est)
   1756 			min_diff++;
   1757 	}
   1758 	if (to_5ghz) {
   1759 		int reduce = 2;
   1760 
   1761 		/* Make it easier to move to 5 GHz band */
   1762 		if (sel_est > cur_est * 1.5)
   1763 			reduce = 5;
   1764 		else if (sel_est > cur_est * 1.2)
   1765 			reduce = 4;
   1766 		else if (sel_est > cur_est * 1.1)
   1767 			reduce = 3;
   1768 
   1769 		if (min_diff > reduce)
   1770 			min_diff -= reduce;
   1771 		else
   1772 			min_diff = 0;
   1773 	}
   1774 	diff = abs(current_bss->level - selected->level);
   1775 	if (diff < min_diff) {
   1776 		wpa_dbg(wpa_s, MSG_DEBUG,
   1777 			"Skip roam - too small difference in signal level (%d < %d)",
   1778 			diff, min_diff);
   1779 		return 0;
   1780 	}
   1781 
   1782 	wpa_dbg(wpa_s, MSG_DEBUG,
   1783 		"Allow reassociation due to difference in signal level (%d >= %d)",
   1784 		diff, min_diff);
   1785 	return 1;
   1786 #else /* CONFIG_NO_ROAMING */
   1787 	return 0;
   1788 #endif /* CONFIG_NO_ROAMING */
   1789 }
   1790 
   1791 
   1792 /*
   1793  * Return a negative value if no scan results could be fetched or if scan
   1794  * results should not be shared with other virtual interfaces.
   1795  * Return 0 if scan results were fetched and may be shared with other
   1796  * interfaces.
   1797  * Return 1 if scan results may be shared with other virtual interfaces but may
   1798  * not trigger any operations.
   1799  * Return 2 if the interface was removed and cannot be used.
   1800  */
   1801 static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
   1802 					      union wpa_event_data *data,
   1803 					      int own_request, int update_only)
   1804 {
   1805 	struct wpa_scan_results *scan_res = NULL;
   1806 	int ret = 0;
   1807 	int ap = 0;
   1808 #ifndef CONFIG_NO_RANDOM_POOL
   1809 	size_t i, num;
   1810 #endif /* CONFIG_NO_RANDOM_POOL */
   1811 
   1812 #ifdef CONFIG_AP
   1813 	if (wpa_s->ap_iface)
   1814 		ap = 1;
   1815 #endif /* CONFIG_AP */
   1816 
   1817 	wpa_supplicant_notify_scanning(wpa_s, 0);
   1818 
   1819 	scan_res = wpa_supplicant_get_scan_results(wpa_s,
   1820 						   data ? &data->scan_info :
   1821 						   NULL, 1);
   1822 	if (scan_res == NULL) {
   1823 		if (wpa_s->conf->ap_scan == 2 || ap ||
   1824 		    wpa_s->scan_res_handler == scan_only_handler)
   1825 			return -1;
   1826 		if (!own_request)
   1827 			return -1;
   1828 		if (data && data->scan_info.external_scan)
   1829 			return -1;
   1830 		wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results - try "
   1831 			"scanning again");
   1832 		wpa_supplicant_req_new_scan(wpa_s, 1, 0);
   1833 		ret = -1;
   1834 		goto scan_work_done;
   1835 	}
   1836 
   1837 #ifndef CONFIG_NO_RANDOM_POOL
   1838 	num = scan_res->num;
   1839 	if (num > 10)
   1840 		num = 10;
   1841 	for (i = 0; i < num; i++) {
   1842 		u8 buf[5];
   1843 		struct wpa_scan_res *res = scan_res->res[i];
   1844 		buf[0] = res->bssid[5];
   1845 		buf[1] = res->qual & 0xff;
   1846 		buf[2] = res->noise & 0xff;
   1847 		buf[3] = res->level & 0xff;
   1848 		buf[4] = res->tsf & 0xff;
   1849 		random_add_randomness(buf, sizeof(buf));
   1850 	}
   1851 #endif /* CONFIG_NO_RANDOM_POOL */
   1852 
   1853 	if (update_only) {
   1854 		ret = 1;
   1855 		goto scan_work_done;
   1856 	}
   1857 
   1858 	if (own_request && wpa_s->scan_res_handler &&
   1859 	    !(data && data->scan_info.external_scan)) {
   1860 		void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
   1861 					 struct wpa_scan_results *scan_res);
   1862 
   1863 		scan_res_handler = wpa_s->scan_res_handler;
   1864 		wpa_s->scan_res_handler = NULL;
   1865 		scan_res_handler(wpa_s, scan_res);
   1866 		ret = 1;
   1867 		goto scan_work_done;
   1868 	}
   1869 
   1870 	if (ap) {
   1871 		wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
   1872 #ifdef CONFIG_AP
   1873 		if (wpa_s->ap_iface->scan_cb)
   1874 			wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
   1875 #endif /* CONFIG_AP */
   1876 		goto scan_work_done;
   1877 	}
   1878 
   1879 	wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available (own=%u ext=%u)",
   1880 		wpa_s->own_scan_running,
   1881 		data ? data->scan_info.external_scan : 0);
   1882 	if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
   1883 	    wpa_s->manual_scan_use_id && wpa_s->own_scan_running &&
   1884 	    own_request && !(data && data->scan_info.external_scan)) {
   1885 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
   1886 			     wpa_s->manual_scan_id);
   1887 		wpa_s->manual_scan_use_id = 0;
   1888 	} else {
   1889 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
   1890 	}
   1891 	wpas_notify_scan_results(wpa_s);
   1892 
   1893 	wpas_notify_scan_done(wpa_s, 1);
   1894 
   1895 	if (data && data->scan_info.external_scan) {
   1896 		wpa_dbg(wpa_s, MSG_DEBUG, "Do not use results from externally requested scan operation for network selection");
   1897 		wpa_scan_results_free(scan_res);
   1898 		return 0;
   1899 	}
   1900 
   1901 	if (wnm_scan_process(wpa_s, 1) > 0)
   1902 		goto scan_work_done;
   1903 
   1904 	if (sme_proc_obss_scan(wpa_s) > 0)
   1905 		goto scan_work_done;
   1906 
   1907 	if (own_request && data &&
   1908 	    wpas_beacon_rep_scan_process(wpa_s, scan_res, &data->scan_info) > 0)
   1909 		goto scan_work_done;
   1910 
   1911 	if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
   1912 		goto scan_work_done;
   1913 
   1914 	if (autoscan_notify_scan(wpa_s, scan_res))
   1915 		goto scan_work_done;
   1916 
   1917 	if (wpa_s->disconnected) {
   1918 		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
   1919 		goto scan_work_done;
   1920 	}
   1921 
   1922 	if (!wpas_driver_bss_selection(wpa_s) &&
   1923 	    bgscan_notify_scan(wpa_s, scan_res) == 1)
   1924 		goto scan_work_done;
   1925 
   1926 	wpas_wps_update_ap_info(wpa_s, scan_res);
   1927 
   1928 	if (wpa_s->wpa_state >= WPA_AUTHENTICATING &&
   1929 	    wpa_s->wpa_state < WPA_COMPLETED)
   1930 		goto scan_work_done;
   1931 
   1932 	wpa_scan_results_free(scan_res);
   1933 
   1934 	if (own_request && wpa_s->scan_work) {
   1935 		struct wpa_radio_work *work = wpa_s->scan_work;
   1936 		wpa_s->scan_work = NULL;
   1937 		radio_work_done(work);
   1938 	}
   1939 
   1940 	return wpas_select_network_from_last_scan(wpa_s, 1, own_request);
   1941 
   1942 scan_work_done:
   1943 	wpa_scan_results_free(scan_res);
   1944 	if (own_request && wpa_s->scan_work) {
   1945 		struct wpa_radio_work *work = wpa_s->scan_work;
   1946 		wpa_s->scan_work = NULL;
   1947 		radio_work_done(work);
   1948 	}
   1949 	return ret;
   1950 }
   1951 
   1952 
   1953 static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
   1954 					      int new_scan, int own_request)
   1955 {
   1956 	struct wpa_bss *selected;
   1957 	struct wpa_ssid *ssid = NULL;
   1958 	int time_to_reenable = wpas_reenabled_network_time(wpa_s);
   1959 
   1960 	if (time_to_reenable > 0) {
   1961 		wpa_dbg(wpa_s, MSG_DEBUG,
   1962 			"Postpone network selection by %d seconds since all networks are disabled",
   1963 			time_to_reenable);
   1964 		eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
   1965 		eloop_register_timeout(time_to_reenable, 0,
   1966 				       wpas_network_reenabled, wpa_s, NULL);
   1967 		return 0;
   1968 	}
   1969 
   1970 	if (wpa_s->p2p_mgmt)
   1971 		return 0; /* no normal connection on p2p_mgmt interface */
   1972 
   1973 	wpa_s->owe_transition_search = 0;
   1974 	selected = wpa_supplicant_pick_network(wpa_s, &ssid);
   1975 
   1976 #ifdef CONFIG_MESH
   1977 	if (wpa_s->ifmsh) {
   1978 		wpa_msg(wpa_s, MSG_INFO,
   1979 			"Avoiding join because we already joined a mesh group");
   1980 		return 0;
   1981 	}
   1982 #endif /* CONFIG_MESH */
   1983 
   1984 	if (selected) {
   1985 		int skip;
   1986 		skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
   1987 		if (skip) {
   1988 			if (new_scan)
   1989 				wpa_supplicant_rsn_preauth_scan_results(wpa_s);
   1990 			return 0;
   1991 		}
   1992 
   1993 		if (ssid != wpa_s->current_ssid &&
   1994 		    wpa_s->wpa_state >= WPA_AUTHENTICATING) {
   1995 			wpa_s->own_disconnect_req = 1;
   1996 			wpa_supplicant_deauthenticate(
   1997 				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
   1998 		}
   1999 
   2000 		if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
   2001 			wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
   2002 			return -1;
   2003 		}
   2004 		if (new_scan)
   2005 			wpa_supplicant_rsn_preauth_scan_results(wpa_s);
   2006 		/*
   2007 		 * Do not allow other virtual radios to trigger operations based
   2008 		 * on these scan results since we do not want them to start
   2009 		 * other associations at the same time.
   2010 		 */
   2011 		return 1;
   2012 	} else {
   2013 		wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
   2014 		ssid = wpa_supplicant_pick_new_network(wpa_s);
   2015 		if (ssid) {
   2016 			wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
   2017 			wpa_supplicant_associate(wpa_s, NULL, ssid);
   2018 			if (new_scan)
   2019 				wpa_supplicant_rsn_preauth_scan_results(wpa_s);
   2020 		} else if (own_request) {
   2021 			/*
   2022 			 * No SSID found. If SCAN results are as a result of
   2023 			 * own scan request and not due to a scan request on
   2024 			 * another shared interface, try another scan.
   2025 			 */
   2026 			int timeout_sec = wpa_s->scan_interval;
   2027 			int timeout_usec = 0;
   2028 #ifdef CONFIG_P2P
   2029 			int res;
   2030 
   2031 			res = wpas_p2p_scan_no_go_seen(wpa_s);
   2032 			if (res == 2)
   2033 				return 2;
   2034 			if (res == 1)
   2035 				return 0;
   2036 
   2037 			if (wpa_s->p2p_in_provisioning ||
   2038 			    wpa_s->show_group_started ||
   2039 			    wpa_s->p2p_in_invitation) {
   2040 				/*
   2041 				 * Use shorter wait during P2P Provisioning
   2042 				 * state and during P2P join-a-group operation
   2043 				 * to speed up group formation.
   2044 				 */
   2045 				timeout_sec = 0;
   2046 				timeout_usec = 250000;
   2047 				wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
   2048 							    timeout_usec);
   2049 				return 0;
   2050 			}
   2051 #endif /* CONFIG_P2P */
   2052 #ifdef CONFIG_INTERWORKING
   2053 			if (wpa_s->conf->auto_interworking &&
   2054 			    wpa_s->conf->interworking &&
   2055 			    wpa_s->conf->cred) {
   2056 				wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
   2057 					"start ANQP fetch since no matching "
   2058 					"networks found");
   2059 				wpa_s->network_select = 1;
   2060 				wpa_s->auto_network_select = 1;
   2061 				interworking_start_fetch_anqp(wpa_s);
   2062 				return 1;
   2063 			}
   2064 #endif /* CONFIG_INTERWORKING */
   2065 #ifdef CONFIG_WPS
   2066 			if (wpa_s->after_wps > 0 || wpas_wps_searching(wpa_s)) {
   2067 				wpa_dbg(wpa_s, MSG_DEBUG, "Use shorter wait during WPS processing");
   2068 				timeout_sec = 0;
   2069 				timeout_usec = 500000;
   2070 				wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
   2071 							    timeout_usec);
   2072 				return 0;
   2073 			}
   2074 #endif /* CONFIG_WPS */
   2075 #ifdef CONFIG_OWE
   2076 			if (wpa_s->owe_transition_search) {
   2077 				wpa_dbg(wpa_s, MSG_DEBUG,
   2078 					"OWE: Use shorter wait during transition mode search");
   2079 				timeout_sec = 0;
   2080 				timeout_usec = 500000;
   2081 				wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
   2082 							    timeout_usec);
   2083 				return 0;
   2084 			}
   2085 #endif /* CONFIG_OWE */
   2086 			if (wpa_supplicant_req_sched_scan(wpa_s))
   2087 				wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
   2088 							    timeout_usec);
   2089 
   2090 			wpa_msg_ctrl(wpa_s, MSG_INFO,
   2091 				     WPA_EVENT_NETWORK_NOT_FOUND);
   2092 		}
   2093 	}
   2094 	return 0;
   2095 }
   2096 
   2097 
   2098 static int wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
   2099 					     union wpa_event_data *data)
   2100 {
   2101 	struct wpa_supplicant *ifs;
   2102 	int res;
   2103 
   2104 	res = _wpa_supplicant_event_scan_results(wpa_s, data, 1, 0);
   2105 	if (res == 2) {
   2106 		/*
   2107 		 * Interface may have been removed, so must not dereference
   2108 		 * wpa_s after this.
   2109 		 */
   2110 		return 1;
   2111 	}
   2112 
   2113 	if (res < 0) {
   2114 		/*
   2115 		 * If no scan results could be fetched, then no need to
   2116 		 * notify those interfaces that did not actually request
   2117 		 * this scan. Similarly, if scan results started a new operation on this
   2118 		 * interface, do not notify other interfaces to avoid concurrent
   2119 		 * operations during a connection attempt.
   2120 		 */
   2121 		return 0;
   2122 	}
   2123 
   2124 	/*
   2125 	 * Check other interfaces to see if they share the same radio. If
   2126 	 * so, they get updated with this same scan info.
   2127 	 */
   2128 	dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
   2129 			 radio_list) {
   2130 		if (ifs != wpa_s) {
   2131 			wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
   2132 				   "sibling", ifs->ifname);
   2133 			res = _wpa_supplicant_event_scan_results(ifs, data, 0,
   2134 								 res > 0);
   2135 			if (res < 0)
   2136 				return 0;
   2137 		}
   2138 	}
   2139 
   2140 	return 0;
   2141 }
   2142 
   2143 #endif /* CONFIG_NO_SCAN_PROCESSING */
   2144 
   2145 
   2146 int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
   2147 {
   2148 #ifdef CONFIG_NO_SCAN_PROCESSING
   2149 	return -1;
   2150 #else /* CONFIG_NO_SCAN_PROCESSING */
   2151 	struct os_reltime now;
   2152 
   2153 	wpa_s->ignore_post_flush_scan_res = 0;
   2154 
   2155 	if (wpa_s->last_scan_res_used == 0)
   2156 		return -1;
   2157 
   2158 	os_get_reltime(&now);
   2159 	if (os_reltime_expired(&now, &wpa_s->last_scan, 5)) {
   2160 		wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
   2161 		return -1;
   2162 	}
   2163 
   2164 	return wpas_select_network_from_last_scan(wpa_s, 0, 1);
   2165 #endif /* CONFIG_NO_SCAN_PROCESSING */
   2166 }
   2167 
   2168 #ifdef CONFIG_WNM
   2169 
   2170 static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
   2171 {
   2172 	struct wpa_supplicant *wpa_s = eloop_ctx;
   2173 
   2174 	if (wpa_s->wpa_state < WPA_ASSOCIATED)
   2175 		return;
   2176 
   2177 	if (!wpa_s->no_keep_alive) {
   2178 		wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
   2179 			   MAC2STR(wpa_s->bssid));
   2180 		/* TODO: could skip this if normal data traffic has been sent */
   2181 		/* TODO: Consider using some more appropriate data frame for
   2182 		 * this */
   2183 		if (wpa_s->l2)
   2184 			l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
   2185 				       (u8 *) "", 0);
   2186 	}
   2187 
   2188 #ifdef CONFIG_SME
   2189 	if (wpa_s->sme.bss_max_idle_period) {
   2190 		unsigned int msec;
   2191 		msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
   2192 		if (msec > 100)
   2193 			msec -= 100;
   2194 		eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
   2195 				       wnm_bss_keep_alive, wpa_s, NULL);
   2196 	}
   2197 #endif /* CONFIG_SME */
   2198 }
   2199 
   2200 
   2201 static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
   2202 				   const u8 *ies, size_t ies_len)
   2203 {
   2204 	struct ieee802_11_elems elems;
   2205 
   2206 	if (ies == NULL)
   2207 		return;
   2208 
   2209 	if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
   2210 		return;
   2211 
   2212 #ifdef CONFIG_SME
   2213 	if (elems.bss_max_idle_period) {
   2214 		unsigned int msec;
   2215 		wpa_s->sme.bss_max_idle_period =
   2216 			WPA_GET_LE16(elems.bss_max_idle_period);
   2217 		wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
   2218 			   "TU)%s", wpa_s->sme.bss_max_idle_period,
   2219 			   (elems.bss_max_idle_period[2] & 0x01) ?
   2220 			   " (protected keep-live required)" : "");
   2221 		if (wpa_s->sme.bss_max_idle_period == 0)
   2222 			wpa_s->sme.bss_max_idle_period = 1;
   2223 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
   2224 			eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
   2225 			 /* msec times 1000 */
   2226 			msec = wpa_s->sme.bss_max_idle_period * 1024;
   2227 			if (msec > 100)
   2228 				msec -= 100;
   2229 			eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
   2230 					       wnm_bss_keep_alive, wpa_s,
   2231 					       NULL);
   2232 		}
   2233 	}
   2234 #endif /* CONFIG_SME */
   2235 }
   2236 
   2237 #endif /* CONFIG_WNM */
   2238 
   2239 
   2240 void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
   2241 {
   2242 #ifdef CONFIG_WNM
   2243 	eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
   2244 #endif /* CONFIG_WNM */
   2245 }
   2246 
   2247 
   2248 #ifdef CONFIG_INTERWORKING
   2249 
   2250 static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
   2251 			    size_t len)
   2252 {
   2253 	int res;
   2254 
   2255 	wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
   2256 	res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
   2257 	if (res) {
   2258 		wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
   2259 	}
   2260 
   2261 	return res;
   2262 }
   2263 
   2264 
   2265 static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
   2266 					    const u8 *ies, size_t ies_len)
   2267 {
   2268 	struct ieee802_11_elems elems;
   2269 
   2270 	if (ies == NULL)
   2271 		return;
   2272 
   2273 	if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
   2274 		return;
   2275 
   2276 	if (elems.qos_map_set) {
   2277 		wpas_qos_map_set(wpa_s, elems.qos_map_set,
   2278 				 elems.qos_map_set_len);
   2279 	}
   2280 }
   2281 
   2282 #endif /* CONFIG_INTERWORKING */
   2283 
   2284 
   2285 static void multi_ap_process_assoc_resp(struct wpa_supplicant *wpa_s,
   2286 					const u8 *ies, size_t ies_len)
   2287 {
   2288 	struct ieee802_11_elems elems;
   2289 	const u8 *map_sub_elem, *pos;
   2290 	size_t len;
   2291 
   2292 	if (!wpa_s->current_ssid ||
   2293 	    !wpa_s->current_ssid->multi_ap_backhaul_sta ||
   2294 	    !ies ||
   2295 	    ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
   2296 		return;
   2297 
   2298 	if (!elems.multi_ap || elems.multi_ap_len < 7) {
   2299 		wpa_printf(MSG_INFO, "AP doesn't support Multi-AP protocol");
   2300 		goto fail;
   2301 	}
   2302 
   2303 	pos = elems.multi_ap + 4;
   2304 	len = elems.multi_ap_len - 4;
   2305 
   2306 	map_sub_elem = get_ie(pos, len, MULTI_AP_SUB_ELEM_TYPE);
   2307 	if (!map_sub_elem || map_sub_elem[1] < 1) {
   2308 		wpa_printf(MSG_INFO, "invalid Multi-AP sub elem type");
   2309 		goto fail;
   2310 	}
   2311 
   2312 	if (!(map_sub_elem[2] & MULTI_AP_BACKHAUL_BSS)) {
   2313 		if ((map_sub_elem[2] & MULTI_AP_FRONTHAUL_BSS) &&
   2314 		    wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
   2315 			wpa_printf(MSG_INFO,
   2316 				   "WPS active, accepting fronthaul-only BSS");
   2317 			/* Don't set 4addr mode in this case, so just return */
   2318 			return;
   2319 		}
   2320 		wpa_printf(MSG_INFO, "AP doesn't support backhaul BSS");
   2321 		goto fail;
   2322 	}
   2323 
   2324 	if (wpa_drv_set_4addr_mode(wpa_s, 1) < 0) {
   2325 		wpa_printf(MSG_ERROR, "Failed to set 4addr mode");
   2326 		goto fail;
   2327 	}
   2328 	wpa_s->enabled_4addr_mode = 1;
   2329 	return;
   2330 
   2331 fail:
   2332 	wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
   2333 }
   2334 
   2335 
   2336 #ifdef CONFIG_FST
   2337 static int wpas_fst_update_mbie(struct wpa_supplicant *wpa_s,
   2338 				const u8 *ie, size_t ie_len)
   2339 {
   2340 	struct mb_ies_info mb_ies;
   2341 
   2342 	if (!ie || !ie_len || !wpa_s->fst)
   2343 	    return -ENOENT;
   2344 
   2345 	os_memset(&mb_ies, 0, sizeof(mb_ies));
   2346 
   2347 	while (ie_len >= 2 && mb_ies.nof_ies < MAX_NOF_MB_IES_SUPPORTED) {
   2348 		size_t len;
   2349 
   2350 		len = 2 + ie[1];
   2351 		if (len > ie_len) {
   2352 			wpa_hexdump(MSG_DEBUG, "FST: Truncated IE found",
   2353 				    ie, ie_len);
   2354 			break;
   2355 		}
   2356 
   2357 		if (ie[0] == WLAN_EID_MULTI_BAND) {
   2358 			wpa_printf(MSG_DEBUG, "MB IE of %u bytes found",
   2359 				   (unsigned int) len);
   2360 			mb_ies.ies[mb_ies.nof_ies].ie = ie + 2;
   2361 			mb_ies.ies[mb_ies.nof_ies].ie_len = len - 2;
   2362 			mb_ies.nof_ies++;
   2363 		}
   2364 
   2365 		ie_len -= len;
   2366 		ie += len;
   2367 	}
   2368 
   2369 	if (mb_ies.nof_ies > 0) {
   2370 		wpabuf_free(wpa_s->received_mb_ies);
   2371 		wpa_s->received_mb_ies = mb_ies_by_info(&mb_ies);
   2372 		return 0;
   2373 	}
   2374 
   2375 	return -ENOENT;
   2376 }
   2377 #endif /* CONFIG_FST */
   2378 
   2379 
   2380 static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
   2381 					  union wpa_event_data *data)
   2382 {
   2383 	int l, len, found = 0, wpa_found, rsn_found;
   2384 	const u8 *p;
   2385 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_OWE)
   2386 	u8 bssid[ETH_ALEN];
   2387 #endif /* CONFIG_IEEE80211R || CONFIG_OWE */
   2388 
   2389 	wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
   2390 	if (data->assoc_info.req_ies)
   2391 		wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
   2392 			    data->assoc_info.req_ies_len);
   2393 	if (data->assoc_info.resp_ies) {
   2394 		wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
   2395 			    data->assoc_info.resp_ies_len);
   2396 #ifdef CONFIG_TDLS
   2397 		wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
   2398 					data->assoc_info.resp_ies_len);
   2399 #endif /* CONFIG_TDLS */
   2400 #ifdef CONFIG_WNM
   2401 		wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
   2402 				       data->assoc_info.resp_ies_len);
   2403 #endif /* CONFIG_WNM */
   2404 #ifdef CONFIG_INTERWORKING
   2405 		interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
   2406 						data->assoc_info.resp_ies_len);
   2407 #endif /* CONFIG_INTERWORKING */
   2408 		if (wpa_s->hw_capab == CAPAB_VHT &&
   2409 		    get_ie(data->assoc_info.resp_ies,
   2410 			   data->assoc_info.resp_ies_len, WLAN_EID_VHT_CAP))
   2411 			wpa_s->ieee80211ac = 1;
   2412 
   2413 		multi_ap_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
   2414 					    data->assoc_info.resp_ies_len);
   2415 	}
   2416 	if (data->assoc_info.beacon_ies)
   2417 		wpa_hexdump(MSG_DEBUG, "beacon_ies",
   2418 			    data->assoc_info.beacon_ies,
   2419 			    data->assoc_info.beacon_ies_len);
   2420 	if (data->assoc_info.freq)
   2421 		wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
   2422 			data->assoc_info.freq);
   2423 
   2424 	wpa_s->connection_set = 0;
   2425 	if (data->assoc_info.req_ies && data->assoc_info.resp_ies) {
   2426 		struct ieee802_11_elems req_elems, resp_elems;
   2427 
   2428 		if (ieee802_11_parse_elems(data->assoc_info.req_ies,
   2429 					   data->assoc_info.req_ies_len,
   2430 					   &req_elems, 0) != ParseFailed &&
   2431 		    ieee802_11_parse_elems(data->assoc_info.resp_ies,
   2432 					   data->assoc_info.resp_ies_len,
   2433 					   &resp_elems, 0) != ParseFailed) {
   2434 			wpa_s->connection_set = 1;
   2435 			wpa_s->connection_ht = req_elems.ht_capabilities &&
   2436 				resp_elems.ht_capabilities;
   2437 			wpa_s->connection_vht = req_elems.vht_capabilities &&
   2438 				resp_elems.vht_capabilities;
   2439 			wpa_s->connection_he = req_elems.he_capabilities &&
   2440 				resp_elems.he_capabilities;
   2441 		}
   2442 	}
   2443 
   2444 	p = data->assoc_info.req_ies;
   2445 	l = data->assoc_info.req_ies_len;
   2446 
   2447 	/* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
   2448 	while (p && l >= 2) {
   2449 		len = p[1] + 2;
   2450 		if (len > l) {
   2451 			wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
   2452 				    p, l);
   2453 			break;
   2454 		}
   2455 		if ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
   2456 		     (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
   2457 		    (p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 4 &&
   2458 		     (os_memcmp(&p[2], "\x50\x6F\x9A\x12", 4) == 0)) ||
   2459 		    (p[0] == WLAN_EID_RSN && p[1] >= 2)) {
   2460 			if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
   2461 				break;
   2462 			found = 1;
   2463 			wpa_find_assoc_pmkid(wpa_s);
   2464 			break;
   2465 		}
   2466 		l -= len;
   2467 		p += len;
   2468 	}
   2469 	if (!found && data->assoc_info.req_ies)
   2470 		wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
   2471 
   2472 #ifdef CONFIG_FILS
   2473 #ifdef CONFIG_SME
   2474 	if ((wpa_s->sme.auth_alg == WPA_AUTH_ALG_FILS ||
   2475 	     wpa_s->sme.auth_alg == WPA_AUTH_ALG_FILS_SK_PFS) &&
   2476 	    (!data->assoc_info.resp_frame ||
   2477 	     fils_process_assoc_resp(wpa_s->wpa,
   2478 				     data->assoc_info.resp_frame,
   2479 				     data->assoc_info.resp_frame_len) < 0)) {
   2480 		wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_UNSPECIFIED);
   2481 		return -1;
   2482 	}
   2483 #endif /* CONFIG_SME */
   2484 
   2485 	/* Additional processing for FILS when SME is in driver */
   2486 	if (wpa_s->auth_alg == WPA_AUTH_ALG_FILS &&
   2487 	    !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
   2488 		wpa_sm_set_reset_fils_completed(wpa_s->wpa, 1);
   2489 #endif /* CONFIG_FILS */
   2490 
   2491 #ifdef CONFIG_OWE
   2492 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_OWE &&
   2493 	    (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
   2494 	     owe_process_assoc_resp(wpa_s->wpa, bssid,
   2495 				    data->assoc_info.resp_ies,
   2496 				    data->assoc_info.resp_ies_len) < 0)) {
   2497 		wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_UNSPECIFIED);
   2498 		return -1;
   2499 	}
   2500 #endif /* CONFIG_OWE */
   2501 
   2502 #ifdef CONFIG_DPP2
   2503 	wpa_sm_set_dpp_z(wpa_s->wpa, NULL);
   2504 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_DPP && wpa_s->dpp_pfs) {
   2505 		struct ieee802_11_elems elems;
   2506 
   2507 		if (ieee802_11_parse_elems(data->assoc_info.resp_ies,
   2508 					   data->assoc_info.resp_ies_len,
   2509 					   &elems, 0) == ParseFailed ||
   2510 		    !elems.owe_dh)
   2511 			goto no_pfs;
   2512 		if (dpp_pfs_process(wpa_s->dpp_pfs, elems.owe_dh,
   2513 				    elems.owe_dh_len) < 0) {
   2514 			wpa_supplicant_deauthenticate(wpa_s,
   2515 						      WLAN_REASON_UNSPECIFIED);
   2516 			return -1;
   2517 		}
   2518 
   2519 		wpa_sm_set_dpp_z(wpa_s->wpa, wpa_s->dpp_pfs->secret);
   2520 	}
   2521 no_pfs:
   2522 #endif /* CONFIG_DPP2 */
   2523 
   2524 #ifdef CONFIG_IEEE80211R
   2525 #ifdef CONFIG_SME
   2526 	if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
   2527 		if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
   2528 		    wpa_ft_validate_reassoc_resp(wpa_s->wpa,
   2529 						 data->assoc_info.resp_ies,
   2530 						 data->assoc_info.resp_ies_len,
   2531 						 bssid) < 0) {
   2532 			wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
   2533 				"Reassociation Response failed");
   2534 			wpa_supplicant_deauthenticate(
   2535 				wpa_s, WLAN_REASON_INVALID_IE);
   2536 			return -1;
   2537 		}
   2538 	}
   2539 
   2540 	p = data->assoc_info.resp_ies;
   2541 	l = data->assoc_info.resp_ies_len;
   2542 
   2543 #ifdef CONFIG_WPS_STRICT
   2544 	if (p && wpa_s->current_ssid &&
   2545 	    wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
   2546 		struct wpabuf *wps;
   2547 		wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
   2548 		if (wps == NULL) {
   2549 			wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
   2550 				"include WPS IE in (Re)Association Response");
   2551 			return -1;
   2552 		}
   2553 
   2554 		if (wps_validate_assoc_resp(wps) < 0) {
   2555 			wpabuf_free(wps);
   2556 			wpa_supplicant_deauthenticate(
   2557 				wpa_s, WLAN_REASON_INVALID_IE);
   2558 			return -1;
   2559 		}
   2560 		wpabuf_free(wps);
   2561 	}
   2562 #endif /* CONFIG_WPS_STRICT */
   2563 
   2564 	/* Go through the IEs and make a copy of the MDIE, if present. */
   2565 	while (p && l >= 2) {
   2566 		len = p[1] + 2;
   2567 		if (len > l) {
   2568 			wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
   2569 				    p, l);
   2570 			break;
   2571 		}
   2572 		if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
   2573 		    p[1] >= MOBILITY_DOMAIN_ID_LEN) {
   2574 			wpa_s->sme.ft_used = 1;
   2575 			os_memcpy(wpa_s->sme.mobility_domain, p + 2,
   2576 				  MOBILITY_DOMAIN_ID_LEN);
   2577 			break;
   2578 		}
   2579 		l -= len;
   2580 		p += len;
   2581 	}
   2582 #endif /* CONFIG_SME */
   2583 
   2584 	/* Process FT when SME is in the driver */
   2585 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
   2586 	    wpa_ft_is_completed(wpa_s->wpa)) {
   2587 		if (wpa_drv_get_bssid(wpa_s, bssid) < 0 ||
   2588 		    wpa_ft_validate_reassoc_resp(wpa_s->wpa,
   2589 						 data->assoc_info.resp_ies,
   2590 						 data->assoc_info.resp_ies_len,
   2591 						 bssid) < 0) {
   2592 			wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
   2593 				"Reassociation Response failed");
   2594 			wpa_supplicant_deauthenticate(
   2595 				wpa_s, WLAN_REASON_INVALID_IE);
   2596 			return -1;
   2597 		}
   2598 		wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
   2599 	}
   2600 
   2601 	wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
   2602 			     data->assoc_info.resp_ies_len);
   2603 #endif /* CONFIG_IEEE80211R */
   2604 
   2605 	/* WPA/RSN IE from Beacon/ProbeResp */
   2606 	p = data->assoc_info.beacon_ies;
   2607 	l = data->assoc_info.beacon_ies_len;
   2608 
   2609 	/* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
   2610 	 */
   2611 	wpa_found = rsn_found = 0;
   2612 	while (p && l >= 2) {
   2613 		len = p[1] + 2;
   2614 		if (len > l) {
   2615 			wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
   2616 				    p, l);
   2617 			break;
   2618 		}
   2619 		if (!wpa_found &&
   2620 		    p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
   2621 		    os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
   2622 			wpa_found = 1;
   2623 			wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
   2624 		}
   2625 
   2626 		if (!rsn_found &&
   2627 		    p[0] == WLAN_EID_RSN && p[1] >= 2) {
   2628 			rsn_found = 1;
   2629 			wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
   2630 		}
   2631 
   2632 		l -= len;
   2633 		p += len;
   2634 	}
   2635 
   2636 	if (!wpa_found && data->assoc_info.beacon_ies)
   2637 		wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
   2638 	if (!rsn_found && data->assoc_info.beacon_ies)
   2639 		wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
   2640 	if (wpa_found || rsn_found)
   2641 		wpa_s->ap_ies_from_associnfo = 1;
   2642 
   2643 	if (wpa_s->assoc_freq && data->assoc_info.freq &&
   2644 	    wpa_s->assoc_freq != data->assoc_info.freq) {
   2645 		wpa_printf(MSG_DEBUG, "Operating frequency changed from "
   2646 			   "%u to %u MHz",
   2647 			   wpa_s->assoc_freq, data->assoc_info.freq);
   2648 		wpa_supplicant_update_scan_results(wpa_s);
   2649 	}
   2650 
   2651 	wpa_s->assoc_freq = data->assoc_info.freq;
   2652 
   2653 	return 0;
   2654 }
   2655 
   2656 
   2657 static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
   2658 {
   2659 	const u8 *bss_wpa = NULL, *bss_rsn = NULL;
   2660 
   2661 	if (!wpa_s->current_bss || !wpa_s->current_ssid)
   2662 		return -1;
   2663 
   2664 	if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
   2665 		return 0;
   2666 
   2667 	bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
   2668 					WPA_IE_VENDOR_TYPE);
   2669 	bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
   2670 
   2671 	if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
   2672 				 bss_wpa ? 2 + bss_wpa[1] : 0) ||
   2673 	    wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
   2674 				 bss_rsn ? 2 + bss_rsn[1] : 0))
   2675 		return -1;
   2676 
   2677 	return 0;
   2678 }
   2679 
   2680 
   2681 static void wpas_fst_update_mb_assoc(struct wpa_supplicant *wpa_s,
   2682 				     union wpa_event_data *data)
   2683 {
   2684 #ifdef CONFIG_FST
   2685 	struct assoc_info *ai = data ? &data->assoc_info : NULL;
   2686 	struct wpa_bss *bss = wpa_s->current_bss;
   2687 	const u8 *ieprb, *iebcn;
   2688 
   2689 	wpabuf_free(wpa_s->received_mb_ies);
   2690 	wpa_s->received_mb_ies = NULL;
   2691 
   2692 	if (ai &&
   2693 	    !wpas_fst_update_mbie(wpa_s, ai->resp_ies, ai->resp_ies_len)) {
   2694 		wpa_printf(MSG_DEBUG,
   2695 			   "FST: MB IEs updated from Association Response frame");
   2696 		return;
   2697 	}
   2698 
   2699 	if (ai &&
   2700 	    !wpas_fst_update_mbie(wpa_s, ai->beacon_ies, ai->beacon_ies_len)) {
   2701 		wpa_printf(MSG_DEBUG,
   2702 			   "FST: MB IEs updated from association event Beacon IEs");
   2703 		return;
   2704 	}
   2705 
   2706 	if (!bss)
   2707 		return;
   2708 
   2709 	ieprb = (const u8 *) (bss + 1);
   2710 	iebcn = ieprb + bss->ie_len;
   2711 
   2712 	if (!wpas_fst_update_mbie(wpa_s, ieprb, bss->ie_len))
   2713 		wpa_printf(MSG_DEBUG, "FST: MB IEs updated from bss IE");
   2714 	else if (!wpas_fst_update_mbie(wpa_s, iebcn, bss->beacon_ie_len))
   2715 		wpa_printf(MSG_DEBUG, "FST: MB IEs updated from bss beacon IE");
   2716 #endif /* CONFIG_FST */
   2717 }
   2718 
   2719 
   2720 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
   2721 				       union wpa_event_data *data)
   2722 {
   2723 	u8 bssid[ETH_ALEN];
   2724 	int ft_completed, already_authorized;
   2725 	int new_bss = 0;
   2726 
   2727 #ifdef CONFIG_AP
   2728 	if (wpa_s->ap_iface) {
   2729 		if (!data)
   2730 			return;
   2731 		hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
   2732 				    data->assoc_info.addr,
   2733 				    data->assoc_info.req_ies,
   2734 				    data->assoc_info.req_ies_len,
   2735 				    data->assoc_info.reassoc);
   2736 		return;
   2737 	}
   2738 #endif /* CONFIG_AP */
   2739 
   2740 	eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
   2741 
   2742 	ft_completed = wpa_ft_is_completed(wpa_s->wpa);
   2743 	if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
   2744 		return;
   2745 	/*
   2746 	 * FILS authentication can share the same mechanism to mark the
   2747 	 * connection fully authenticated, so set ft_completed also based on
   2748 	 * FILS result.
   2749 	 */
   2750 	if (!ft_completed)
   2751 		ft_completed = wpa_fils_is_completed(wpa_s->wpa);
   2752 
   2753 	if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
   2754 		wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
   2755 		wpa_supplicant_deauthenticate(
   2756 			wpa_s, WLAN_REASON_DEAUTH_LEAVING);
   2757 		return;
   2758 	}
   2759 
   2760 	wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
   2761 	if (os_memcmp(bssid, wpa_s->bssid, ETH_ALEN) != 0) {
   2762 		if (os_reltime_initialized(&wpa_s->session_start)) {
   2763 			os_reltime_age(&wpa_s->session_start,
   2764 				       &wpa_s->session_length);
   2765 			wpa_s->session_start.sec = 0;
   2766 			wpa_s->session_start.usec = 0;
   2767 			wpas_notify_session_length(wpa_s);
   2768 		} else {
   2769 			wpas_notify_auth_changed(wpa_s);
   2770 			os_get_reltime(&wpa_s->session_start);
   2771 		}
   2772 		wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
   2773 			MACSTR, MAC2STR(bssid));
   2774 		new_bss = 1;
   2775 		random_add_randomness(bssid, ETH_ALEN);
   2776 		os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
   2777 		os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
   2778 		wpas_notify_bssid_changed(wpa_s);
   2779 
   2780 		if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
   2781 			wpa_clear_keys(wpa_s, bssid);
   2782 		}
   2783 		if (wpa_supplicant_select_config(wpa_s) < 0) {
   2784 			wpa_supplicant_deauthenticate(
   2785 				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
   2786 			return;
   2787 		}
   2788 	}
   2789 
   2790 	if (wpa_s->conf->ap_scan == 1 &&
   2791 	    wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
   2792 		if (wpa_supplicant_assoc_update_ie(wpa_s) < 0 && new_bss)
   2793 			wpa_msg(wpa_s, MSG_WARNING,
   2794 				"WPA/RSN IEs not updated");
   2795 	}
   2796 
   2797 	wpas_fst_update_mb_assoc(wpa_s, data);
   2798 
   2799 #ifdef CONFIG_SME
   2800 	os_memcpy(wpa_s->sme.prev_bssid, bssid, ETH_ALEN);
   2801 	wpa_s->sme.prev_bssid_set = 1;
   2802 	wpa_s->sme.last_unprot_disconnect.sec = 0;
   2803 #endif /* CONFIG_SME */
   2804 
   2805 	wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
   2806 	if (wpa_s->current_ssid) {
   2807 		/* When using scanning (ap_scan=1), SIM PC/SC interface can be
   2808 		 * initialized before association, but for other modes,
   2809 		 * initialize PC/SC here, if the current configuration needs
   2810 		 * smartcard or SIM/USIM. */
   2811 		wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
   2812 	}
   2813 	wpa_sm_notify_assoc(wpa_s->wpa, bssid);
   2814 	if (wpa_s->l2)
   2815 		l2_packet_notify_auth_start(wpa_s->l2);
   2816 
   2817 	already_authorized = data && data->assoc_info.authorized;
   2818 
   2819 	/*
   2820 	 * Set portEnabled first to FALSE in order to get EAP state machine out
   2821 	 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
   2822 	 * state machine may transit to AUTHENTICATING state based on obsolete
   2823 	 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
   2824 	 * AUTHENTICATED without ever giving chance to EAP state machine to
   2825 	 * reset the state.
   2826 	 */
   2827 	if (!ft_completed && !already_authorized) {
   2828 		eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
   2829 		eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
   2830 	}
   2831 	if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
   2832 	    wpa_s->key_mgmt == WPA_KEY_MGMT_DPP ||
   2833 	    wpa_s->key_mgmt == WPA_KEY_MGMT_OWE || ft_completed ||
   2834 	    already_authorized)
   2835 		eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
   2836 	/* 802.1X::portControl = Auto */
   2837 	eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
   2838 	wpa_s->eapol_received = 0;
   2839 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
   2840 	    wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
   2841 	    (wpa_s->current_ssid &&
   2842 	     wpa_s->current_ssid->mode == IEEE80211_MODE_IBSS)) {
   2843 		if (wpa_s->current_ssid &&
   2844 		    wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
   2845 		    (wpa_s->drv_flags &
   2846 		     WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
   2847 			/*
   2848 			 * Set the key after having received joined-IBSS event
   2849 			 * from the driver.
   2850 			 */
   2851 			wpa_supplicant_set_wpa_none_key(wpa_s,
   2852 							wpa_s->current_ssid);
   2853 		}
   2854 		wpa_supplicant_cancel_auth_timeout(wpa_s);
   2855 		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
   2856 	} else if (!ft_completed) {
   2857 		/* Timeout for receiving the first EAPOL packet */
   2858 		wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
   2859 	}
   2860 	wpa_supplicant_cancel_scan(wpa_s);
   2861 
   2862 	if (ft_completed) {
   2863 		/*
   2864 		 * FT protocol completed - make sure EAPOL state machine ends
   2865 		 * up in authenticated.
   2866 		 */
   2867 		wpa_supplicant_cancel_auth_timeout(wpa_s);
   2868 		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
   2869 		eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
   2870 		eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
   2871 	} else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK) &&
   2872 		   wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
   2873 		/*
   2874 		 * We are done; the driver will take care of RSN 4-way
   2875 		 * handshake.
   2876 		 */
   2877 		wpa_supplicant_cancel_auth_timeout(wpa_s);
   2878 		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
   2879 		eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
   2880 		eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
   2881 	} else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X) &&
   2882 		   wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
   2883 		/*
   2884 		 * The driver will take care of RSN 4-way handshake, so we need
   2885 		 * to allow EAPOL supplicant to complete its work without
   2886 		 * waiting for WPA supplicant.
   2887 		 */
   2888 		eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
   2889 	}
   2890 
   2891 	wpa_s->last_eapol_matches_bssid = 0;
   2892 
   2893 	if (wpa_s->pending_eapol_rx) {
   2894 		struct os_reltime now, age;
   2895 		os_get_reltime(&now);
   2896 		os_reltime_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
   2897 		if (age.sec == 0 && age.usec < 200000 &&
   2898 		    os_memcmp(wpa_s->pending_eapol_rx_src, bssid, ETH_ALEN) ==
   2899 		    0) {
   2900 			wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
   2901 				"frame that was received just before "
   2902 				"association notification");
   2903 			wpa_supplicant_rx_eapol(
   2904 				wpa_s, wpa_s->pending_eapol_rx_src,
   2905 				wpabuf_head(wpa_s->pending_eapol_rx),
   2906 				wpabuf_len(wpa_s->pending_eapol_rx));
   2907 		}
   2908 		wpabuf_free(wpa_s->pending_eapol_rx);
   2909 		wpa_s->pending_eapol_rx = NULL;
   2910 	}
   2911 
   2912 	if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
   2913 	     wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
   2914 	    wpa_s->current_ssid &&
   2915 	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
   2916 		/* Set static WEP keys again */
   2917 		wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
   2918 	}
   2919 
   2920 #ifdef CONFIG_IBSS_RSN
   2921 	if (wpa_s->current_ssid &&
   2922 	    wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
   2923 	    wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
   2924 	    wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
   2925 	    wpa_s->ibss_rsn == NULL) {
   2926 		wpa_s->ibss_rsn = ibss_rsn_init(wpa_s, wpa_s->current_ssid);
   2927 		if (!wpa_s->ibss_rsn) {
   2928 			wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
   2929 			wpa_supplicant_deauthenticate(
   2930 				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
   2931 			return;
   2932 		}
   2933 
   2934 		ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
   2935 	}
   2936 #endif /* CONFIG_IBSS_RSN */
   2937 
   2938 	wpas_wps_notify_assoc(wpa_s, bssid);
   2939 
   2940 	if (data) {
   2941 		wmm_ac_notify_assoc(wpa_s, data->assoc_info.resp_ies,
   2942 				    data->assoc_info.resp_ies_len,
   2943 				    &data->assoc_info.wmm_params);
   2944 
   2945 		if (wpa_s->reassoc_same_bss)
   2946 			wmm_ac_restore_tspecs(wpa_s);
   2947 	}
   2948 
   2949 #ifdef CONFIG_FILS
   2950 	if (wpa_key_mgmt_fils(wpa_s->key_mgmt)) {
   2951 		struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, bssid);
   2952 		const u8 *fils_cache_id = wpa_bss_get_fils_cache_id(bss);
   2953 
   2954 		if (fils_cache_id)
   2955 			wpa_sm_set_fils_cache_id(wpa_s->wpa, fils_cache_id);
   2956 	}
   2957 #endif /* CONFIG_FILS */
   2958 }
   2959 
   2960 
   2961 static int disconnect_reason_recoverable(u16 reason_code)
   2962 {
   2963 	return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
   2964 		reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
   2965 		reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
   2966 }
   2967 
   2968 
   2969 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
   2970 					  u16 reason_code,
   2971 					  int locally_generated)
   2972 {
   2973 	const u8 *bssid;
   2974 
   2975 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
   2976 		/*
   2977 		 * At least Host AP driver and a Prism3 card seemed to be
   2978 		 * generating streams of disconnected events when configuring
   2979 		 * IBSS for WPA-None. Ignore them for now.
   2980 		 */
   2981 		return;
   2982 	}
   2983 
   2984 	bssid = wpa_s->bssid;
   2985 	if (is_zero_ether_addr(bssid))
   2986 		bssid = wpa_s->pending_bssid;
   2987 
   2988 	if (!is_zero_ether_addr(bssid) ||
   2989 	    wpa_s->wpa_state >= WPA_AUTHENTICATING) {
   2990 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
   2991 			" reason=%d%s",
   2992 			MAC2STR(bssid), reason_code,
   2993 			locally_generated ? " locally_generated=1" : "");
   2994 	}
   2995 }
   2996 
   2997 
   2998 static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
   2999 				 int locally_generated)
   3000 {
   3001 	if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
   3002 	    !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt))
   3003 		return 0; /* Not in 4-way handshake with PSK */
   3004 
   3005 	/*
   3006 	 * It looks like connection was lost while trying to go through PSK
   3007 	 * 4-way handshake. Filter out known disconnection cases that are caused
   3008 	 * by something else than PSK mismatch to avoid confusing reports.
   3009 	 */
   3010 
   3011 	if (locally_generated) {
   3012 		if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
   3013 			return 0;
   3014 	}
   3015 
   3016 	return 1;
   3017 }
   3018 
   3019 
   3020 static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
   3021 						 u16 reason_code,
   3022 						 int locally_generated)
   3023 {
   3024 	const u8 *bssid;
   3025 	int authenticating;
   3026 	u8 prev_pending_bssid[ETH_ALEN];
   3027 	struct wpa_bss *fast_reconnect = NULL;
   3028 	struct wpa_ssid *fast_reconnect_ssid = NULL;
   3029 	struct wpa_ssid *last_ssid;
   3030 	struct wpa_bss *curr = NULL;
   3031 
   3032 	authenticating = wpa_s->wpa_state == WPA_AUTHENTICATING;
   3033 	os_memcpy(prev_pending_bssid, wpa_s->pending_bssid, ETH_ALEN);
   3034 
   3035 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
   3036 		/*
   3037 		 * At least Host AP driver and a Prism3 card seemed to be
   3038 		 * generating streams of disconnected events when configuring
   3039 		 * IBSS for WPA-None. Ignore them for now.
   3040 		 */
   3041 		wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
   3042 			"IBSS/WPA-None mode");
   3043 		return;
   3044 	}
   3045 
   3046 	if (!wpa_s->disconnected && wpa_s->wpa_state >= WPA_AUTHENTICATING &&
   3047 	    reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY &&
   3048 	    locally_generated)
   3049 		/*
   3050 		 * Remove the inactive AP (which is probably out of range) from
   3051 		 * the BSS list after marking disassociation. In particular
   3052 		 * mac80211-based drivers use the
   3053 		 * WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY reason code in
   3054 		 * locally generated disconnection events for cases where the
   3055 		 * AP does not reply anymore.
   3056 		 */
   3057 		curr = wpa_s->current_bss;
   3058 
   3059 	if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
   3060 		wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
   3061 			"pre-shared key may be incorrect");
   3062 		if (wpas_p2p_4way_hs_failed(wpa_s) > 0)
   3063 			return; /* P2P group removed */
   3064 		wpas_auth_failed(wpa_s, "WRONG_KEY");
   3065 	}
   3066 	if (!wpa_s->disconnected &&
   3067 	    (!wpa_s->auto_reconnect_disabled ||
   3068 	     wpa_s->key_mgmt == WPA_KEY_MGMT_WPS ||
   3069 	     wpas_wps_searching(wpa_s) ||
   3070 	     wpas_wps_reenable_networks_pending(wpa_s))) {
   3071 		wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
   3072 			"reconnect (wps=%d/%d wpa_state=%d)",
   3073 			wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
   3074 			wpas_wps_searching(wpa_s),
   3075 			wpa_s->wpa_state);
   3076 		if (wpa_s->wpa_state == WPA_COMPLETED &&
   3077 		    wpa_s->current_ssid &&
   3078 		    wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
   3079 		    !locally_generated &&
   3080 		    disconnect_reason_recoverable(reason_code)) {
   3081 			/*
   3082 			 * It looks like the AP has dropped association with
   3083 			 * us, but could allow us to get back in. Try to
   3084 			 * reconnect to the same BSS without full scan to save
   3085 			 * time for some common cases.
   3086 			 */
   3087 			fast_reconnect = wpa_s->current_bss;
   3088 			fast_reconnect_ssid = wpa_s->current_ssid;
   3089 		} else if (wpa_s->wpa_state >= WPA_ASSOCIATING)
   3090 			wpa_supplicant_req_scan(wpa_s, 0, 100000);
   3091 		else
   3092 			wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
   3093 				"immediate scan");
   3094 	} else {
   3095 		wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
   3096 			"try to re-connect");
   3097 		wpa_s->reassociate = 0;
   3098 		wpa_s->disconnected = 1;
   3099 		if (!wpa_s->pno)
   3100 			wpa_supplicant_cancel_sched_scan(wpa_s);
   3101 	}
   3102 	bssid = wpa_s->bssid;
   3103 	if (is_zero_ether_addr(bssid))
   3104 		bssid = wpa_s->pending_bssid;
   3105 	if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
   3106 		wpas_connection_failed(wpa_s, bssid);
   3107 	wpa_sm_notify_disassoc(wpa_s->wpa);
   3108 	if (locally_generated)
   3109 		wpa_s->disconnect_reason = -reason_code;
   3110 	else
   3111 		wpa_s->disconnect_reason = reason_code;
   3112 	wpas_notify_disconnect_reason(wpa_s);
   3113 	if (wpa_supplicant_dynamic_keys(wpa_s)) {
   3114 		wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
   3115 		wpa_clear_keys(wpa_s, wpa_s->bssid);
   3116 	}
   3117 	last_ssid = wpa_s->current_ssid;
   3118 	wpa_supplicant_mark_disassoc(wpa_s);
   3119 
   3120 	if (curr)
   3121 		wpa_bss_remove(wpa_s, curr, "Connection to AP lost");
   3122 
   3123 	if (authenticating && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)) {
   3124 		sme_disassoc_while_authenticating(wpa_s, prev_pending_bssid);
   3125 		wpa_s->current_ssid = last_ssid;
   3126 	}
   3127 
   3128 	if (fast_reconnect &&
   3129 	    !wpas_network_disabled(wpa_s, fast_reconnect_ssid) &&
   3130 	    !disallowed_bssid(wpa_s, fast_reconnect->bssid) &&
   3131 	    !disallowed_ssid(wpa_s, fast_reconnect->ssid,
   3132 			     fast_reconnect->ssid_len) &&
   3133 	    !wpas_temp_disabled(wpa_s, fast_reconnect_ssid) &&
   3134 	    !wpa_is_bss_tmp_disallowed(wpa_s, fast_reconnect)) {
   3135 #ifndef CONFIG_NO_SCAN_PROCESSING
   3136 		wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
   3137 		if (wpa_supplicant_connect(wpa_s, fast_reconnect,
   3138 					   fast_reconnect_ssid) < 0) {
   3139 			/* Recover through full scan */
   3140 			wpa_supplicant_req_scan(wpa_s, 0, 100000);
   3141 		}
   3142 #endif /* CONFIG_NO_SCAN_PROCESSING */
   3143 	} else if (fast_reconnect) {
   3144 		/*
   3145 		 * Could not reconnect to the same BSS due to network being
   3146 		 * disabled. Use a new scan to match the alternative behavior
   3147 		 * above, i.e., to continue automatic reconnection attempt in a
   3148 		 * way that enforces disabled network rules.
   3149 		 */
   3150 		wpa_supplicant_req_scan(wpa_s, 0, 100000);
   3151 	}
   3152 }
   3153 
   3154 
   3155 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
   3156 void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
   3157 {
   3158 	struct wpa_supplicant *wpa_s = eloop_ctx;
   3159 
   3160 	if (!wpa_s->pending_mic_error_report)
   3161 		return;
   3162 
   3163 	wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
   3164 	wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
   3165 	wpa_s->pending_mic_error_report = 0;
   3166 }
   3167 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
   3168 
   3169 
   3170 static void
   3171 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
   3172 					 union wpa_event_data *data)
   3173 {
   3174 	int pairwise;
   3175 	struct os_reltime t;
   3176 
   3177 	wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
   3178 	pairwise = (data && data->michael_mic_failure.unicast);
   3179 	os_get_reltime(&t);
   3180 	if ((wpa_s->last_michael_mic_error.sec &&
   3181 	     !os_reltime_expired(&t, &wpa_s->last_michael_mic_error, 60)) ||
   3182 	    wpa_s->pending_mic_error_report) {
   3183 		if (wpa_s->pending_mic_error_report) {
   3184 			/*
   3185 			 * Send the pending MIC error report immediately since
   3186 			 * we are going to start countermeasures and AP better
   3187 			 * do the same.
   3188 			 */
   3189 			wpa_sm_key_request(wpa_s->wpa, 1,
   3190 					   wpa_s->pending_mic_error_pairwise);
   3191 		}
   3192 
   3193 		/* Send the new MIC error report immediately since we are going
   3194 		 * to start countermeasures and AP better do the same.
   3195 		 */
   3196 		wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
   3197 
   3198 		/* initialize countermeasures */
   3199 		wpa_s->countermeasures = 1;
   3200 
   3201 		wpa_blacklist_add(wpa_s, wpa_s->bssid);
   3202 
   3203 		wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
   3204 
   3205 		/*
   3206 		 * Need to wait for completion of request frame. We do not get
   3207 		 * any callback for the message completion, so just wait a
   3208 		 * short while and hope for the best. */
   3209 		os_sleep(0, 10000);
   3210 
   3211 		wpa_drv_set_countermeasures(wpa_s, 1);
   3212 		wpa_supplicant_deauthenticate(wpa_s,
   3213 					      WLAN_REASON_MICHAEL_MIC_FAILURE);
   3214 		eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
   3215 				     wpa_s, NULL);
   3216 		eloop_register_timeout(60, 0,
   3217 				       wpa_supplicant_stop_countermeasures,
   3218 				       wpa_s, NULL);
   3219 		/* TODO: mark the AP rejected for 60 second. STA is
   3220 		 * allowed to associate with another AP.. */
   3221 	} else {
   3222 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
   3223 		if (wpa_s->mic_errors_seen) {
   3224 			/*
   3225 			 * Reduce the effectiveness of Michael MIC error
   3226 			 * reports as a means for attacking against TKIP if
   3227 			 * more than one MIC failure is noticed with the same
   3228 			 * PTK. We delay the transmission of the reports by a
   3229 			 * random time between 0 and 60 seconds in order to
   3230 			 * force the attacker wait 60 seconds before getting
   3231 			 * the information on whether a frame resulted in a MIC
   3232 			 * failure.
   3233 			 */
   3234 			u8 rval[4];
   3235 			int sec;
   3236 
   3237 			if (os_get_random(rval, sizeof(rval)) < 0)
   3238 				sec = os_random() % 60;
   3239 			else
   3240 				sec = WPA_GET_BE32(rval) % 60;
   3241 			wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
   3242 				"report %d seconds", sec);
   3243 			wpa_s->pending_mic_error_report = 1;
   3244 			wpa_s->pending_mic_error_pairwise = pairwise;
   3245 			eloop_cancel_timeout(
   3246 				wpa_supplicant_delayed_mic_error_report,
   3247 				wpa_s, NULL);
   3248 			eloop_register_timeout(
   3249 				sec, os_random() % 1000000,
   3250 				wpa_supplicant_delayed_mic_error_report,
   3251 				wpa_s, NULL);
   3252 		} else {
   3253 			wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
   3254 		}
   3255 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
   3256 		wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
   3257 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
   3258 	}
   3259 	wpa_s->last_michael_mic_error = t;
   3260 	wpa_s->mic_errors_seen++;
   3261 }
   3262 
   3263 
   3264 #ifdef CONFIG_TERMINATE_ONLASTIF
   3265 static int any_interfaces(struct wpa_supplicant *head)
   3266 {
   3267 	struct wpa_supplicant *wpa_s;
   3268 
   3269 	for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
   3270 		if (!wpa_s->interface_removed)
   3271 			return 1;
   3272 	return 0;
   3273 }
   3274 #endif /* CONFIG_TERMINATE_ONLASTIF */
   3275 
   3276 
   3277 static void
   3278 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
   3279 				      union wpa_event_data *data)
   3280 {
   3281 	if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
   3282 		return;
   3283 
   3284 	switch (data->interface_status.ievent) {
   3285 	case EVENT_INTERFACE_ADDED:
   3286 		if (!wpa_s->interface_removed)
   3287 			break;
   3288 		wpa_s->interface_removed = 0;
   3289 		wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
   3290 		if (wpa_supplicant_driver_init(wpa_s) < 0) {
   3291 			wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
   3292 				"driver after interface was added");
   3293 		}
   3294 
   3295 #ifdef CONFIG_P2P
   3296 		if (!wpa_s->global->p2p &&
   3297 		    !wpa_s->global->p2p_disabled &&
   3298 		    !wpa_s->conf->p2p_disabled &&
   3299 		    (wpa_s->drv_flags &
   3300 		     WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
   3301 		    wpas_p2p_add_p2pdev_interface(
   3302 			    wpa_s, wpa_s->global->params.conf_p2p_dev) < 0) {
   3303 			wpa_printf(MSG_INFO,
   3304 				   "P2P: Failed to enable P2P Device interface");
   3305 			/* Try to continue without. P2P will be disabled. */
   3306 		}
   3307 #endif /* CONFIG_P2P */
   3308 
   3309 		break;
   3310 	case EVENT_INTERFACE_REMOVED:
   3311 		wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
   3312 		wpa_s->interface_removed = 1;
   3313 		wpa_supplicant_mark_disassoc(wpa_s);
   3314 		wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
   3315 		l2_packet_deinit(wpa_s->l2);
   3316 		wpa_s->l2 = NULL;
   3317 
   3318 #ifdef CONFIG_P2P
   3319 		if (wpa_s->global->p2p &&
   3320 		    wpa_s->global->p2p_init_wpa_s->parent == wpa_s &&
   3321 		    (wpa_s->drv_flags &
   3322 		     WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) {
   3323 			wpa_dbg(wpa_s, MSG_DEBUG,
   3324 				"Removing P2P Device interface");
   3325 			wpa_supplicant_remove_iface(
   3326 				wpa_s->global, wpa_s->global->p2p_init_wpa_s,
   3327 				0);
   3328 			wpa_s->global->p2p_init_wpa_s = NULL;
   3329 		}
   3330 #endif /* CONFIG_P2P */
   3331 
   3332 #ifdef CONFIG_MATCH_IFACE
   3333 		if (wpa_s->matched) {
   3334 			wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
   3335 			break;
   3336 		}
   3337 #endif /* CONFIG_MATCH_IFACE */
   3338 
   3339 #ifdef CONFIG_TERMINATE_ONLASTIF
   3340 		/* check if last interface */
   3341 		if (!any_interfaces(wpa_s->global->ifaces))
   3342 			eloop_terminate();
   3343 #endif /* CONFIG_TERMINATE_ONLASTIF */
   3344 		break;
   3345 	}
   3346 }
   3347 
   3348 
   3349 #ifdef CONFIG_TDLS
   3350 static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
   3351 				      union wpa_event_data *data)
   3352 {
   3353 	if (data == NULL)
   3354 		return;
   3355 	switch (data->tdls.oper) {
   3356 	case TDLS_REQUEST_SETUP:
   3357 		wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
   3358 		if (wpa_tdls_is_external_setup(wpa_s->wpa))
   3359 			wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
   3360 		else
   3361 			wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
   3362 		break;
   3363 	case TDLS_REQUEST_TEARDOWN:
   3364 		if (wpa_tdls_is_external_setup(wpa_s->wpa))
   3365 			wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
   3366 					       data->tdls.reason_code);
   3367 		else
   3368 			wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
   3369 					  data->tdls.peer);
   3370 		break;
   3371 	case TDLS_REQUEST_DISCOVER:
   3372 			wpa_tdls_send_discovery_request(wpa_s->wpa,
   3373 							data->tdls.peer);
   3374 		break;
   3375 	}
   3376 }
   3377 #endif /* CONFIG_TDLS */
   3378 
   3379 
   3380 #ifdef CONFIG_WNM
   3381 static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
   3382 				     union wpa_event_data *data)
   3383 {
   3384 	if (data == NULL)
   3385 		return;
   3386 	switch (data->wnm.oper) {
   3387 	case WNM_OPER_SLEEP:
   3388 		wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
   3389 			   "(action=%d, intval=%d)",
   3390 			   data->wnm.sleep_action, data->wnm.sleep_intval);
   3391 		ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
   3392 					     data->wnm.sleep_intval, NULL);
   3393 		break;
   3394 	}
   3395 }
   3396 #endif /* CONFIG_WNM */
   3397 
   3398 
   3399 #ifdef CONFIG_IEEE80211R
   3400 static void
   3401 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
   3402 				 union wpa_event_data *data)
   3403 {
   3404 	if (data == NULL)
   3405 		return;
   3406 
   3407 	if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
   3408 				    data->ft_ies.ies_len,
   3409 				    data->ft_ies.ft_action,
   3410 				    data->ft_ies.target_ap,
   3411 				    data->ft_ies.ric_ies,
   3412 				    data->ft_ies.ric_ies_len) < 0) {
   3413 		/* TODO: prevent MLME/driver from trying to associate? */
   3414 	}
   3415 }
   3416 #endif /* CONFIG_IEEE80211R */
   3417 
   3418 
   3419 #ifdef CONFIG_IBSS_RSN
   3420 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
   3421 						union wpa_event_data *data)
   3422 {
   3423 	struct wpa_ssid *ssid;
   3424 	if (wpa_s->wpa_state < WPA_ASSOCIATED)
   3425 		return;
   3426 	if (data == NULL)
   3427 		return;
   3428 	ssid = wpa_s->current_ssid;
   3429 	if (ssid == NULL)
   3430 		return;
   3431 	if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
   3432 		return;
   3433 
   3434 	ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
   3435 }
   3436 
   3437 
   3438 static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
   3439 					   union wpa_event_data *data)
   3440 {
   3441 	struct wpa_ssid *ssid = wpa_s->current_ssid;
   3442 
   3443 	if (ssid == NULL)
   3444 		return;
   3445 
   3446 	/* check if the ssid is correctly configured as IBSS/RSN */
   3447 	if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
   3448 		return;
   3449 
   3450 	ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
   3451 			     data->rx_mgmt.frame_len);
   3452 }
   3453 #endif /* CONFIG_IBSS_RSN */
   3454 
   3455 
   3456 #ifdef CONFIG_IEEE80211R
   3457 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
   3458 			 size_t len)
   3459 {
   3460 	const u8 *sta_addr, *target_ap_addr;
   3461 	u16 status;
   3462 
   3463 	wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
   3464 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
   3465 		return; /* only SME case supported for now */
   3466 	if (len < 1 + 2 * ETH_ALEN + 2)
   3467 		return;
   3468 	if (data[0] != 2)
   3469 		return; /* Only FT Action Response is supported for now */
   3470 	sta_addr = data + 1;
   3471 	target_ap_addr = data + 1 + ETH_ALEN;
   3472 	status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
   3473 	wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
   3474 		MACSTR " TargetAP " MACSTR " status %u",
   3475 		MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
   3476 
   3477 	if (os_memcmp(sta_addr, wpa_s->own_addr, ETH_ALEN) != 0) {
   3478 		wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
   3479 			" in FT Action Response", MAC2STR(sta_addr));
   3480 		return;
   3481 	}
   3482 
   3483 	if (status) {
   3484 		wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
   3485 			"failure (status code %d)", status);
   3486 		/* TODO: report error to FT code(?) */
   3487 		return;
   3488 	}
   3489 
   3490 	if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
   3491 				    len - (1 + 2 * ETH_ALEN + 2), 1,
   3492 				    target_ap_addr, NULL, 0) < 0)
   3493 		return;
   3494 
   3495 #ifdef CONFIG_SME
   3496 	{
   3497 		struct wpa_bss *bss;
   3498 		bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
   3499 		if (bss)
   3500 			wpa_s->sme.freq = bss->freq;
   3501 		wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
   3502 		sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
   3503 			      WLAN_AUTH_FT);
   3504 	}
   3505 #endif /* CONFIG_SME */
   3506 }
   3507 #endif /* CONFIG_IEEE80211R */
   3508 
   3509 
   3510 static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
   3511 					       struct unprot_deauth *e)
   3512 {
   3513 #ifdef CONFIG_IEEE80211W
   3514 	wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
   3515 		   "dropped: " MACSTR " -> " MACSTR
   3516 		   " (reason code %u)",
   3517 		   MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
   3518 	sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
   3519 #endif /* CONFIG_IEEE80211W */
   3520 }
   3521 
   3522 
   3523 static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
   3524 						 struct unprot_disassoc *e)
   3525 {
   3526 #ifdef CONFIG_IEEE80211W
   3527 	wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
   3528 		   "dropped: " MACSTR " -> " MACSTR
   3529 		   " (reason code %u)",
   3530 		   MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
   3531 	sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
   3532 #endif /* CONFIG_IEEE80211W */
   3533 }
   3534 
   3535 
   3536 static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
   3537 				  u16 reason_code, int locally_generated,
   3538 				  const u8 *ie, size_t ie_len, int deauth)
   3539 {
   3540 #ifdef CONFIG_AP
   3541 	if (wpa_s->ap_iface && addr) {
   3542 		hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
   3543 		return;
   3544 	}
   3545 
   3546 	if (wpa_s->ap_iface) {
   3547 		wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
   3548 		return;
   3549 	}
   3550 #endif /* CONFIG_AP */
   3551 
   3552 	if (!locally_generated)
   3553 		wpa_s->own_disconnect_req = 0;
   3554 
   3555 	wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
   3556 
   3557 	if (((reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
   3558 	      ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
   3559 		(wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
   3560 	       eapol_sm_failed(wpa_s->eapol))) &&
   3561 	     !wpa_s->eap_expected_failure))
   3562 		wpas_auth_failed(wpa_s, "AUTH_FAILED");
   3563 
   3564 #ifdef CONFIG_P2P
   3565 	if (deauth && reason_code > 0) {
   3566 		if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
   3567 					  locally_generated) > 0) {
   3568 			/*
   3569 			 * The interface was removed, so cannot continue
   3570 			 * processing any additional operations after this.
   3571 			 */
   3572 			return;
   3573 		}
   3574 	}
   3575 #endif /* CONFIG_P2P */
   3576 
   3577 	wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
   3578 					     locally_generated);
   3579 }
   3580 
   3581 
   3582 static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
   3583 				struct disassoc_info *info)
   3584 {
   3585 	u16 reason_code = 0;
   3586 	int locally_generated = 0;
   3587 	const u8 *addr = NULL;
   3588 	const u8 *ie = NULL;
   3589 	size_t ie_len = 0;
   3590 
   3591 	wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
   3592 
   3593 	if (info) {
   3594 		addr = info->addr;
   3595 		ie = info->ie;
   3596 		ie_len = info->ie_len;
   3597 		reason_code = info->reason_code;
   3598 		locally_generated = info->locally_generated;
   3599 		wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s", reason_code,
   3600 			locally_generated ? " (locally generated)" : "");
   3601 		if (addr)
   3602 			wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
   3603 				MAC2STR(addr));
   3604 		wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
   3605 			    ie, ie_len);
   3606 	}
   3607 
   3608 #ifdef CONFIG_AP
   3609 	if (wpa_s->ap_iface && info && info->addr) {
   3610 		hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
   3611 		return;
   3612 	}
   3613 
   3614 	if (wpa_s->ap_iface) {
   3615 		wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
   3616 		return;
   3617 	}
   3618 #endif /* CONFIG_AP */
   3619 
   3620 #ifdef CONFIG_P2P
   3621 	if (info) {
   3622 		wpas_p2p_disassoc_notif(
   3623 			wpa_s, info->addr, reason_code, info->ie, info->ie_len,
   3624 			locally_generated);
   3625 	}
   3626 #endif /* CONFIG_P2P */
   3627 
   3628 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
   3629 		sme_event_disassoc(wpa_s, info);
   3630 
   3631 	wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
   3632 			      ie, ie_len, 0);
   3633 }
   3634 
   3635 
   3636 static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
   3637 			      struct deauth_info *info)
   3638 {
   3639 	u16 reason_code = 0;
   3640 	int locally_generated = 0;
   3641 	const u8 *addr = NULL;
   3642 	const u8 *ie = NULL;
   3643 	size_t ie_len = 0;
   3644 
   3645 	wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
   3646 
   3647 	if (info) {
   3648 		addr = info->addr;
   3649 		ie = info->ie;
   3650 		ie_len = info->ie_len;
   3651 		reason_code = info->reason_code;
   3652 		locally_generated = info->locally_generated;
   3653 		wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u%s",
   3654 			reason_code,
   3655 			locally_generated ? " (locally generated)" : "");
   3656 		if (addr) {
   3657 			wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
   3658 				MAC2STR(addr));
   3659 		}
   3660 		wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
   3661 			    ie, ie_len);
   3662 	}
   3663 
   3664 	wpa_reset_ft_completed(wpa_s->wpa);
   3665 
   3666 	wpas_event_disconnect(wpa_s, addr, reason_code,
   3667 			      locally_generated, ie, ie_len, 1);
   3668 }
   3669 
   3670 
   3671 static const char * reg_init_str(enum reg_change_initiator init)
   3672 {
   3673 	switch (init) {
   3674 	case REGDOM_SET_BY_CORE:
   3675 		return "CORE";
   3676 	case REGDOM_SET_BY_USER:
   3677 		return "USER";
   3678 	case REGDOM_SET_BY_DRIVER:
   3679 		return "DRIVER";
   3680 	case REGDOM_SET_BY_COUNTRY_IE:
   3681 		return "COUNTRY_IE";
   3682 	case REGDOM_BEACON_HINT:
   3683 		return "BEACON_HINT";
   3684 	}
   3685 	return "?";
   3686 }
   3687 
   3688 
   3689 static const char * reg_type_str(enum reg_type type)
   3690 {
   3691 	switch (type) {
   3692 	case REGDOM_TYPE_UNKNOWN:
   3693 		return "UNKNOWN";
   3694 	case REGDOM_TYPE_COUNTRY:
   3695 		return "COUNTRY";
   3696 	case REGDOM_TYPE_WORLD:
   3697 		return "WORLD";
   3698 	case REGDOM_TYPE_CUSTOM_WORLD:
   3699 		return "CUSTOM_WORLD";
   3700 	case REGDOM_TYPE_INTERSECTION:
   3701 		return "INTERSECTION";
   3702 	}
   3703 	return "?";
   3704 }
   3705 
   3706 
   3707 void wpa_supplicant_update_channel_list(struct wpa_supplicant *wpa_s,
   3708 					struct channel_list_changed *info)
   3709 {
   3710 	struct wpa_supplicant *ifs;
   3711 	u8 dfs_domain;
   3712 
   3713 	/*
   3714 	 * To allow backwards compatibility with higher level layers that
   3715 	 * assumed the REGDOM_CHANGE event is sent over the initially added
   3716 	 * interface. Find the highest parent of this interface and use it to
   3717 	 * send the event.
   3718 	 */
   3719 	for (ifs = wpa_s; ifs->parent && ifs != ifs->parent; ifs = ifs->parent)
   3720 		;
   3721 
   3722 	if (info) {
   3723 		wpa_msg(ifs, MSG_INFO,
   3724 			WPA_EVENT_REGDOM_CHANGE "init=%s type=%s%s%s",
   3725 			reg_init_str(info->initiator), reg_type_str(info->type),
   3726 			info->alpha2[0] ? " alpha2=" : "",
   3727 			info->alpha2[0] ? info->alpha2 : "");
   3728 	}
   3729 
   3730 	if (wpa_s->drv_priv == NULL)
   3731 		return; /* Ignore event during drv initialization */
   3732 
   3733 	dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
   3734 			 radio_list) {
   3735 		wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
   3736 			   ifs->ifname);
   3737 		free_hw_features(ifs);
   3738 		ifs->hw.modes = wpa_drv_get_hw_feature_data(
   3739 			ifs, &ifs->hw.num_modes, &ifs->hw.flags, &dfs_domain);
   3740 
   3741 		/* Restart PNO/sched_scan with updated channel list */
   3742 		if (ifs->pno) {
   3743 			wpas_stop_pno(ifs);
   3744 			wpas_start_pno(ifs);
   3745 		} else if (ifs->sched_scanning && !ifs->pno_sched_pending) {
   3746 			wpa_dbg(ifs, MSG_DEBUG,
   3747 				"Channel list changed - restart sched_scan");
   3748 			wpas_scan_restart_sched_scan(ifs);
   3749 		}
   3750 	}
   3751 
   3752 	wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DRIVER);
   3753 }
   3754 
   3755 
   3756 static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
   3757 				      const u8 *frame, size_t len, int freq,
   3758 				      int rssi)
   3759 {
   3760 	const struct ieee80211_mgmt *mgmt;
   3761 	const u8 *payload;
   3762 	size_t plen;
   3763 	u8 category;
   3764 
   3765 	if (len < IEEE80211_HDRLEN + 2)
   3766 		return;
   3767 
   3768 	mgmt = (const struct ieee80211_mgmt *) frame;
   3769 	payload = frame + IEEE80211_HDRLEN;
   3770 	category = *payload++;
   3771 	plen = len - IEEE80211_HDRLEN - 1;
   3772 
   3773 	wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
   3774 		" Category=%u DataLen=%d freq=%d MHz",
   3775 		MAC2STR(mgmt->sa), category, (int) plen, freq);
   3776 
   3777 	if (category == WLAN_ACTION_WMM) {
   3778 		wmm_ac_rx_action(wpa_s, mgmt->da, mgmt->sa, payload, plen);
   3779 		return;
   3780 	}
   3781 
   3782 #ifdef CONFIG_IEEE80211R
   3783 	if (category == WLAN_ACTION_FT) {
   3784 		ft_rx_action(wpa_s, payload, plen);
   3785 		return;
   3786 	}
   3787 #endif /* CONFIG_IEEE80211R */
   3788 
   3789 #ifdef CONFIG_IEEE80211W
   3790 #ifdef CONFIG_SME
   3791 	if (category == WLAN_ACTION_SA_QUERY) {
   3792 		sme_sa_query_rx(wpa_s, mgmt->sa, payload, plen);
   3793 		return;
   3794 	}
   3795 #endif /* CONFIG_SME */
   3796 #endif /* CONFIG_IEEE80211W */
   3797 
   3798 #ifdef CONFIG_WNM
   3799 	if (mgmt->u.action.category == WLAN_ACTION_WNM) {
   3800 		ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
   3801 		return;
   3802 	}
   3803 #endif /* CONFIG_WNM */
   3804 
   3805 #ifdef CONFIG_GAS
   3806 	if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
   3807 	     mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
   3808 	    gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
   3809 			 mgmt->u.action.category,
   3810 			 payload, plen, freq) == 0)
   3811 		return;
   3812 #endif /* CONFIG_GAS */
   3813 
   3814 #ifdef CONFIG_GAS_SERVER
   3815 	if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
   3816 	     mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
   3817 	    gas_server_rx(wpa_s->gas_server, mgmt->da, mgmt->sa, mgmt->bssid,
   3818 			  mgmt->u.action.category,
   3819 			  payload, plen, freq) == 0)
   3820 		return;
   3821 #endif /* CONFIG_GAS_SERVER */
   3822 
   3823 #ifdef CONFIG_TDLS
   3824 	if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
   3825 	    payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
   3826 		wpa_dbg(wpa_s, MSG_DEBUG,
   3827 			"TDLS: Received Discovery Response from " MACSTR,
   3828 			MAC2STR(mgmt->sa));
   3829 		return;
   3830 	}
   3831 #endif /* CONFIG_TDLS */
   3832 
   3833 #ifdef CONFIG_INTERWORKING
   3834 	if (category == WLAN_ACTION_QOS && plen >= 1 &&
   3835 	    payload[0] == QOS_QOS_MAP_CONFIG) {
   3836 		const u8 *pos = payload + 1;
   3837 		size_t qlen = plen - 1;
   3838 		wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
   3839 			MACSTR, MAC2STR(mgmt->sa));
   3840 		if (os_memcmp(mgmt->sa, wpa_s->bssid, ETH_ALEN) == 0 &&
   3841 		    qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
   3842 		    pos[1] <= qlen - 2 && pos[1] >= 16)
   3843 			wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
   3844 		return;
   3845 	}
   3846 #endif /* CONFIG_INTERWORKING */
   3847 
   3848 	if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
   3849 	    payload[0] == WLAN_RRM_RADIO_MEASUREMENT_REQUEST) {
   3850 		wpas_rrm_handle_radio_measurement_request(wpa_s, mgmt->sa,
   3851 							  mgmt->da,
   3852 							  payload + 1,
   3853 							  plen - 1);
   3854 		return;
   3855 	}
   3856 
   3857 	if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
   3858 	    payload[0] == WLAN_RRM_NEIGHBOR_REPORT_RESPONSE) {
   3859 		wpas_rrm_process_neighbor_rep(wpa_s, payload + 1, plen - 1);
   3860 		return;
   3861 	}
   3862 
   3863 	if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
   3864 	    payload[0] == WLAN_RRM_LINK_MEASUREMENT_REQUEST) {
   3865 		wpas_rrm_handle_link_measurement_request(wpa_s, mgmt->sa,
   3866 							 payload + 1, plen - 1,
   3867 							 rssi);
   3868 		return;
   3869 	}
   3870 
   3871 #ifdef CONFIG_FST
   3872 	if (mgmt->u.action.category == WLAN_ACTION_FST && wpa_s->fst) {
   3873 		fst_rx_action(wpa_s->fst, mgmt, len);
   3874 		return;
   3875 	}
   3876 #endif /* CONFIG_FST */
   3877 
   3878 #ifdef CONFIG_DPP
   3879 	if (category == WLAN_ACTION_PUBLIC && plen >= 5 &&
   3880 	    payload[0] == WLAN_PA_VENDOR_SPECIFIC &&
   3881 	    WPA_GET_BE24(&payload[1]) == OUI_WFA &&
   3882 	    payload[4] == DPP_OUI_TYPE) {
   3883 		payload++;
   3884 		plen--;
   3885 		wpas_dpp_rx_action(wpa_s, mgmt->sa, payload, plen, freq);
   3886 		return;
   3887 	}
   3888 #endif /* CONFIG_DPP */
   3889 
   3890 	wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
   3891 			   category, payload, plen, freq);
   3892 	if (wpa_s->ifmsh)
   3893 		mesh_mpm_action_rx(wpa_s, mgmt, len);
   3894 }
   3895 
   3896 
   3897 static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
   3898 					     union wpa_event_data *event)
   3899 {
   3900 	struct wpa_freq_range_list *list;
   3901 	char *str = NULL;
   3902 
   3903 	list = &event->freq_range;
   3904 
   3905 	if (list->num)
   3906 		str = freq_range_list_str(list);
   3907 	wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
   3908 		str ? str : "");
   3909 
   3910 #ifdef CONFIG_P2P
   3911 	if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
   3912 		wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
   3913 			__func__);
   3914 	} else {
   3915 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
   3916 
   3917 		/*
   3918 		 * The update channel flow will also take care of moving a GO
   3919 		 * from the unsafe frequency if needed.
   3920 		 */
   3921 		wpas_p2p_update_channel_list(wpa_s,
   3922 					     WPAS_P2P_CHANNEL_UPDATE_AVOID);
   3923 	}
   3924 #endif /* CONFIG_P2P */
   3925 
   3926 	os_free(str);
   3927 }
   3928 
   3929 
   3930 static void wpa_supplicant_event_port_authorized(struct wpa_supplicant *wpa_s)
   3931 {
   3932 	if (wpa_s->wpa_state == WPA_ASSOCIATED) {
   3933 		wpa_supplicant_cancel_auth_timeout(wpa_s);
   3934 		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
   3935 		eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
   3936 		eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
   3937 	}
   3938 }
   3939 
   3940 
   3941 static unsigned int wpas_event_cac_ms(const struct wpa_supplicant *wpa_s,
   3942 				      int freq)
   3943 {
   3944 	size_t i;
   3945 	int j;
   3946 
   3947 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
   3948 		const struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
   3949 
   3950 		for (j = 0; j < mode->num_channels; j++) {
   3951 			const struct hostapd_channel_data *chan;
   3952 
   3953 			chan = &mode->channels[j];
   3954 			if (chan->freq == freq)
   3955 				return chan->dfs_cac_ms;
   3956 		}
   3957 	}
   3958 
   3959 	return 0;
   3960 }
   3961 
   3962 
   3963 static void wpas_event_dfs_cac_started(struct wpa_supplicant *wpa_s,
   3964 				       struct dfs_event *radar)
   3965 {
   3966 #if defined(NEED_AP_MLME) && defined(CONFIG_AP)
   3967 	if (wpa_s->ap_iface || wpa_s->ifmsh) {
   3968 		wpas_ap_event_dfs_cac_started(wpa_s, radar);
   3969 	} else
   3970 #endif /* NEED_AP_MLME && CONFIG_AP */
   3971 	{
   3972 		unsigned int cac_time = wpas_event_cac_ms(wpa_s, radar->freq);
   3973 
   3974 		cac_time /= 1000; /* convert from ms to sec */
   3975 		if (!cac_time)
   3976 			cac_time = 10 * 60; /* max timeout: 10 minutes */
   3977 
   3978 		/* Restart auth timeout: CAC time added to initial timeout */
   3979 		wpas_auth_timeout_restart(wpa_s, cac_time);
   3980 	}
   3981 }
   3982 
   3983 
   3984 static void wpas_event_dfs_cac_finished(struct wpa_supplicant *wpa_s,
   3985 					struct dfs_event *radar)
   3986 {
   3987 #if defined(NEED_AP_MLME) && defined(CONFIG_AP)
   3988 	if (wpa_s->ap_iface || wpa_s->ifmsh) {
   3989 		wpas_ap_event_dfs_cac_finished(wpa_s, radar);
   3990 	} else
   3991 #endif /* NEED_AP_MLME && CONFIG_AP */
   3992 	{
   3993 		/* Restart auth timeout with original value after CAC is
   3994 		 * finished */
   3995 		wpas_auth_timeout_restart(wpa_s, 0);
   3996 	}
   3997 }
   3998 
   3999 
   4000 static void wpas_event_dfs_cac_aborted(struct wpa_supplicant *wpa_s,
   4001 				       struct dfs_event *radar)
   4002 {
   4003 #if defined(NEED_AP_MLME) && defined(CONFIG_AP)
   4004 	if (wpa_s->ap_iface || wpa_s->ifmsh) {
   4005 		wpas_ap_event_dfs_cac_aborted(wpa_s, radar);
   4006 	} else
   4007 #endif /* NEED_AP_MLME && CONFIG_AP */
   4008 	{
   4009 		/* Restart auth timeout with original value after CAC is
   4010 		 * aborted */
   4011 		wpas_auth_timeout_restart(wpa_s, 0);
   4012 	}
   4013 }
   4014 
   4015 
   4016 static void wpa_supplicant_event_assoc_auth(struct wpa_supplicant *wpa_s,
   4017 					    union wpa_event_data *data)
   4018 {
   4019 	wpa_dbg(wpa_s, MSG_DEBUG,
   4020 		"Connection authorized by device, previous state %d",
   4021 		wpa_s->wpa_state);
   4022 
   4023 	wpa_supplicant_event_port_authorized(wpa_s);
   4024 
   4025 	wpa_sm_set_rx_replay_ctr(wpa_s->wpa, data->assoc_info.key_replay_ctr);
   4026 	wpa_sm_set_ptk_kck_kek(wpa_s->wpa, data->assoc_info.ptk_kck,
   4027 			       data->assoc_info.ptk_kck_len,
   4028 			       data->assoc_info.ptk_kek,
   4029 			       data->assoc_info.ptk_kek_len);
   4030 #ifdef CONFIG_FILS
   4031 	if (wpa_s->auth_alg == WPA_AUTH_ALG_FILS) {
   4032 		struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
   4033 		const u8 *fils_cache_id = wpa_bss_get_fils_cache_id(bss);
   4034 
   4035 		/* Update ERP next sequence number */
   4036 		eapol_sm_update_erp_next_seq_num(
   4037 			wpa_s->eapol, data->assoc_info.fils_erp_next_seq_num);
   4038 
   4039 		if (data->assoc_info.fils_pmk && data->assoc_info.fils_pmkid) {
   4040 			/* Add the new PMK and PMKID to the PMKSA cache */
   4041 			wpa_sm_pmksa_cache_add(wpa_s->wpa,
   4042 					       data->assoc_info.fils_pmk,
   4043 					       data->assoc_info.fils_pmk_len,
   4044 					       data->assoc_info.fils_pmkid,
   4045 					       wpa_s->bssid, fils_cache_id);
   4046 		} else if (data->assoc_info.fils_pmkid) {
   4047 			/* Update the current PMKSA used for this connection */
   4048 			pmksa_cache_set_current(wpa_s->wpa,
   4049 						data->assoc_info.fils_pmkid,
   4050 						NULL, NULL, 0, NULL, 0);
   4051 		}
   4052 	}
   4053 #endif /* CONFIG_FILS */
   4054 }
   4055 
   4056 
   4057 static void wpas_event_assoc_reject(struct wpa_supplicant *wpa_s,
   4058 				    union wpa_event_data *data)
   4059 {
   4060 	const u8 *bssid = data->assoc_reject.bssid;
   4061 
   4062 	if (!bssid || is_zero_ether_addr(bssid))
   4063 		bssid = wpa_s->pending_bssid;
   4064 
   4065 	if (data->assoc_reject.bssid)
   4066 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
   4067 			"bssid=" MACSTR	" status_code=%u%s%s%s",
   4068 			MAC2STR(data->assoc_reject.bssid),
   4069 			data->assoc_reject.status_code,
   4070 			data->assoc_reject.timed_out ? " timeout" : "",
   4071 			data->assoc_reject.timeout_reason ? "=" : "",
   4072 			data->assoc_reject.timeout_reason ?
   4073 			data->assoc_reject.timeout_reason : "");
   4074 	else
   4075 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
   4076 			"status_code=%u%s%s%s",
   4077 			data->assoc_reject.status_code,
   4078 			data->assoc_reject.timed_out ? " timeout" : "",
   4079 			data->assoc_reject.timeout_reason ? "=" : "",
   4080 			data->assoc_reject.timeout_reason ?
   4081 			data->assoc_reject.timeout_reason : "");
   4082 	wpa_s->assoc_status_code = data->assoc_reject.status_code;
   4083 	wpa_s->assoc_timed_out = data->assoc_reject.timed_out;
   4084 	wpas_notify_assoc_status_code(wpa_s);
   4085 
   4086 #ifdef CONFIG_OWE
   4087 	if (data->assoc_reject.status_code ==
   4088 	    WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
   4089 	    wpa_s->key_mgmt == WPA_KEY_MGMT_OWE &&
   4090 	    wpa_s->current_ssid &&
   4091 	    wpa_s->current_ssid->owe_group == 0 &&
   4092 	    wpa_s->last_owe_group != 21) {
   4093 		struct wpa_ssid *ssid = wpa_s->current_ssid;
   4094 		struct wpa_bss *bss = wpa_s->current_bss;
   4095 
   4096 		if (!bss) {
   4097 			bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
   4098 			if (!bss) {
   4099 				wpas_connection_failed(wpa_s, bssid);
   4100 				wpa_supplicant_mark_disassoc(wpa_s);
   4101 				return;
   4102 			}
   4103 		}
   4104 		wpa_printf(MSG_DEBUG, "OWE: Try next supported DH group");
   4105 		wpas_connect_work_done(wpa_s);
   4106 		wpa_supplicant_mark_disassoc(wpa_s);
   4107 		wpa_supplicant_connect(wpa_s, bss, ssid);
   4108 		return;
   4109 	}
   4110 #endif /* CONFIG_OWE */
   4111 
   4112 #ifdef CONFIG_MBO
   4113 	if (data->assoc_reject.status_code ==
   4114 	    WLAN_STATUS_DENIED_POOR_CHANNEL_CONDITIONS &&
   4115 	    wpa_s->current_bss && data->assoc_reject.bssid &&
   4116 	    data->assoc_reject.resp_ies) {
   4117 		const u8 *rssi_rej;
   4118 
   4119 		rssi_rej = mbo_get_attr_from_ies(
   4120 			data->assoc_reject.resp_ies,
   4121 			data->assoc_reject.resp_ies_len,
   4122 			OCE_ATTR_ID_RSSI_BASED_ASSOC_REJECT);
   4123 		if (rssi_rej && rssi_rej[1] == 2) {
   4124 			wpa_printf(MSG_DEBUG,
   4125 				   "OCE: RSSI-based association rejection from "
   4126 				   MACSTR " (Delta RSSI: %u, Retry Delay: %u)",
   4127 				   MAC2STR(data->assoc_reject.bssid),
   4128 				   rssi_rej[2], rssi_rej[3]);
   4129 			wpa_bss_tmp_disallow(wpa_s,
   4130 					     data->assoc_reject.bssid,
   4131 					     rssi_rej[3],
   4132 					     rssi_rej[2] +
   4133 					     wpa_s->current_bss->level);
   4134 		}
   4135 	}
   4136 #endif /* CONFIG_MBO */
   4137 
   4138 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
   4139 		sme_event_assoc_reject(wpa_s, data);
   4140 		return;
   4141 	}
   4142 
   4143 	/* Driver-based SME cases */
   4144 
   4145 #ifdef CONFIG_SAE
   4146 	if (wpa_s->current_ssid &&
   4147 	    wpa_key_mgmt_sae(wpa_s->current_ssid->key_mgmt) &&
   4148 	    !data->assoc_reject.timed_out) {
   4149 		wpa_dbg(wpa_s, MSG_DEBUG, "SAE: Drop PMKSA cache entry");
   4150 		wpa_sm_aborted_cached(wpa_s->wpa);
   4151 		wpa_sm_pmksa_cache_flush(wpa_s->wpa, wpa_s->current_ssid);
   4152 	}
   4153 #endif /* CONFIG_SAE */
   4154 
   4155 #ifdef CONFIG_DPP
   4156 	if (wpa_s->current_ssid &&
   4157 	    wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_DPP &&
   4158 	    !data->assoc_reject.timed_out) {
   4159 		wpa_dbg(wpa_s, MSG_DEBUG, "DPP: Drop PMKSA cache entry");
   4160 		wpa_sm_aborted_cached(wpa_s->wpa);
   4161 		wpa_sm_pmksa_cache_flush(wpa_s->wpa, wpa_s->current_ssid);
   4162 	}
   4163 #endif /* CONFIG_DPP */
   4164 
   4165 #ifdef CONFIG_FILS
   4166 	/* Update ERP next sequence number */
   4167 	if (wpa_s->auth_alg == WPA_AUTH_ALG_FILS) {
   4168 		eapol_sm_update_erp_next_seq_num(
   4169 			wpa_s->eapol,
   4170 			data->assoc_reject.fils_erp_next_seq_num);
   4171 		fils_connection_failure(wpa_s);
   4172 	}
   4173 #endif /* CONFIG_FILS */
   4174 
   4175 	wpas_connection_failed(wpa_s, bssid);
   4176 	wpa_supplicant_mark_disassoc(wpa_s);
   4177 }
   4178 
   4179 
   4180 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
   4181 			  union wpa_event_data *data)
   4182 {
   4183 	struct wpa_supplicant *wpa_s = ctx;
   4184 	int resched;
   4185 #ifndef CONFIG_NO_STDOUT_DEBUG
   4186 	int level = MSG_DEBUG;
   4187 #endif /* CONFIG_NO_STDOUT_DEBUG */
   4188 
   4189 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
   4190 	    event != EVENT_INTERFACE_ENABLED &&
   4191 	    event != EVENT_INTERFACE_STATUS &&
   4192 	    event != EVENT_SCAN_RESULTS &&
   4193 	    event != EVENT_SCHED_SCAN_STOPPED) {
   4194 		wpa_dbg(wpa_s, MSG_DEBUG,
   4195 			"Ignore event %s (%d) while interface is disabled",
   4196 			event_to_string(event), event);
   4197 		return;
   4198 	}
   4199 
   4200 #ifndef CONFIG_NO_STDOUT_DEBUG
   4201 	if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
   4202 		const struct ieee80211_hdr *hdr;
   4203 		u16 fc;
   4204 		hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
   4205 		fc = le_to_host16(hdr->frame_control);
   4206 		if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
   4207 		    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
   4208 			level = MSG_EXCESSIVE;
   4209 	}
   4210 
   4211 	wpa_dbg(wpa_s, level, "Event %s (%d) received",
   4212 		event_to_string(event), event);
   4213 #endif /* CONFIG_NO_STDOUT_DEBUG */
   4214 
   4215 	switch (event) {
   4216 	case EVENT_AUTH:
   4217 #ifdef CONFIG_FST
   4218 		if (!wpas_fst_update_mbie(wpa_s, data->auth.ies,
   4219 					  data->auth.ies_len))
   4220 			wpa_printf(MSG_DEBUG,
   4221 				   "FST: MB IEs updated from auth IE");
   4222 #endif /* CONFIG_FST */
   4223 		sme_event_auth(wpa_s, data);
   4224 		wpa_s->auth_status_code = data->auth.status_code;
   4225 		wpas_notify_auth_status_code(wpa_s);
   4226 		break;
   4227 	case EVENT_ASSOC:
   4228 #ifdef CONFIG_TESTING_OPTIONS
   4229 		if (wpa_s->ignore_auth_resp) {
   4230 			wpa_printf(MSG_INFO,
   4231 				   "EVENT_ASSOC - ignore_auth_resp active!");
   4232 			break;
   4233 		}
   4234 		if (wpa_s->testing_resend_assoc) {
   4235 			wpa_printf(MSG_INFO,
   4236 				   "EVENT_DEAUTH - testing_resend_assoc");
   4237 			break;
   4238 		}
   4239 #endif /* CONFIG_TESTING_OPTIONS */
   4240 		wpa_supplicant_event_assoc(wpa_s, data);
   4241 		wpa_s->assoc_status_code = WLAN_STATUS_SUCCESS;
   4242 		if (data &&
   4243 		    (data->assoc_info.authorized ||
   4244 		     (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
   4245 		      wpa_fils_is_completed(wpa_s->wpa))))
   4246 			wpa_supplicant_event_assoc_auth(wpa_s, data);
   4247 		if (data) {
   4248 			wpa_msg(wpa_s, MSG_INFO,
   4249 				WPA_EVENT_SUBNET_STATUS_UPDATE "status=%u",
   4250 				data->assoc_info.subnet_status);
   4251 		}
   4252 		break;
   4253 	case EVENT_DISASSOC:
   4254 		wpas_event_disassoc(wpa_s,
   4255 				    data ? &data->disassoc_info : NULL);
   4256 		break;
   4257 	case EVENT_DEAUTH:
   4258 #ifdef CONFIG_TESTING_OPTIONS
   4259 		if (wpa_s->ignore_auth_resp) {
   4260 			wpa_printf(MSG_INFO,
   4261 				   "EVENT_DEAUTH - ignore_auth_resp active!");
   4262 			break;
   4263 		}
   4264 		if (wpa_s->testing_resend_assoc) {
   4265 			wpa_printf(MSG_INFO,
   4266 				   "EVENT_DEAUTH - testing_resend_assoc");
   4267 			break;
   4268 		}
   4269 #endif /* CONFIG_TESTING_OPTIONS */
   4270 		wpas_event_deauth(wpa_s,
   4271 				  data ? &data->deauth_info : NULL);
   4272 		break;
   4273 	case EVENT_MICHAEL_MIC_FAILURE:
   4274 		wpa_supplicant_event_michael_mic_failure(wpa_s, data);
   4275 		break;
   4276 #ifndef CONFIG_NO_SCAN_PROCESSING
   4277 	case EVENT_SCAN_STARTED:
   4278 		if (wpa_s->own_scan_requested ||
   4279 		    (data && !data->scan_info.external_scan)) {
   4280 			struct os_reltime diff;
   4281 
   4282 			os_get_reltime(&wpa_s->scan_start_time);
   4283 			os_reltime_sub(&wpa_s->scan_start_time,
   4284 				       &wpa_s->scan_trigger_time, &diff);
   4285 			wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %ld.%06ld seconds",
   4286 				diff.sec, diff.usec);
   4287 			wpa_s->own_scan_requested = 0;
   4288 			wpa_s->own_scan_running = 1;
   4289 			if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
   4290 			    wpa_s->manual_scan_use_id) {
   4291 				wpa_msg_ctrl(wpa_s, MSG_INFO,
   4292 					     WPA_EVENT_SCAN_STARTED "id=%u",
   4293 					     wpa_s->manual_scan_id);
   4294 			} else {
   4295 				wpa_msg_ctrl(wpa_s, MSG_INFO,
   4296 					     WPA_EVENT_SCAN_STARTED);
   4297 			}
   4298 		} else {
   4299 			wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
   4300 			wpa_s->radio->external_scan_running = 1;
   4301 			wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
   4302 		}
   4303 		break;
   4304 	case EVENT_SCAN_RESULTS:
   4305 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
   4306 			wpa_s->scan_res_handler = NULL;
   4307 			wpa_s->own_scan_running = 0;
   4308 			wpa_s->radio->external_scan_running = 0;
   4309 			wpa_s->last_scan_req = NORMAL_SCAN_REQ;
   4310 			break;
   4311 		}
   4312 
   4313 		if (!(data && data->scan_info.external_scan) &&
   4314 		    os_reltime_initialized(&wpa_s->scan_start_time)) {
   4315 			struct os_reltime now, diff;
   4316 			os_get_reltime(&now);
   4317 			os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
   4318 			wpa_s->scan_start_time.sec = 0;
   4319 			wpa_s->scan_start_time.usec = 0;
   4320 			wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %ld.%06ld seconds",
   4321 				diff.sec, diff.usec);
   4322 		}
   4323 		if (wpa_supplicant_event_scan_results(wpa_s, data))
   4324 			break; /* interface may have been removed */
   4325 		if (!(data && data->scan_info.external_scan))
   4326 			wpa_s->own_scan_running = 0;
   4327 		if (data && data->scan_info.nl_scan_event)
   4328 			wpa_s->radio->external_scan_running = 0;
   4329 		radio_work_check_next(wpa_s);
   4330 		break;
   4331 #endif /* CONFIG_NO_SCAN_PROCESSING */
   4332 	case EVENT_ASSOCINFO:
   4333 		wpa_supplicant_event_associnfo(wpa_s, data);
   4334 		break;
   4335 	case EVENT_INTERFACE_STATUS:
   4336 		wpa_supplicant_event_interface_status(wpa_s, data);
   4337 		break;
   4338 	case EVENT_PMKID_CANDIDATE:
   4339 		wpa_supplicant_event_pmkid_candidate(wpa_s, data);
   4340 		break;
   4341 #ifdef CONFIG_TDLS
   4342 	case EVENT_TDLS:
   4343 		wpa_supplicant_event_tdls(wpa_s, data);
   4344 		break;
   4345 #endif /* CONFIG_TDLS */
   4346 #ifdef CONFIG_WNM
   4347 	case EVENT_WNM:
   4348 		wpa_supplicant_event_wnm(wpa_s, data);
   4349 		break;
   4350 #endif /* CONFIG_WNM */
   4351 #ifdef CONFIG_IEEE80211R
   4352 	case EVENT_FT_RESPONSE:
   4353 		wpa_supplicant_event_ft_response(wpa_s, data);
   4354 		break;
   4355 #endif /* CONFIG_IEEE80211R */
   4356 #ifdef CONFIG_IBSS_RSN
   4357 	case EVENT_IBSS_RSN_START:
   4358 		wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
   4359 		break;
   4360 #endif /* CONFIG_IBSS_RSN */
   4361 	case EVENT_ASSOC_REJECT:
   4362 		wpas_event_assoc_reject(wpa_s, data);
   4363 		break;
   4364 	case EVENT_AUTH_TIMED_OUT:
   4365 		/* It is possible to get this event from earlier connection */
   4366 		if (wpa_s->current_ssid &&
   4367 		    wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
   4368 			wpa_dbg(wpa_s, MSG_DEBUG,
   4369 				"Ignore AUTH_TIMED_OUT in mesh configuration");
   4370 			break;
   4371 		}
   4372 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
   4373 			sme_event_auth_timed_out(wpa_s, data);
   4374 		break;
   4375 	case EVENT_ASSOC_TIMED_OUT:
   4376 		/* It is possible to get this event from earlier connection */
   4377 		if (wpa_s->current_ssid &&
   4378 		    wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
   4379 			wpa_dbg(wpa_s, MSG_DEBUG,
   4380 				"Ignore ASSOC_TIMED_OUT in mesh configuration");
   4381 			break;
   4382 		}
   4383 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
   4384 			sme_event_assoc_timed_out(wpa_s, data);
   4385 		break;
   4386 	case EVENT_TX_STATUS:
   4387 		wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
   4388 			" type=%d stype=%d",
   4389 			MAC2STR(data->tx_status.dst),
   4390 			data->tx_status.type, data->tx_status.stype);
   4391 #ifdef CONFIG_AP
   4392 		if (wpa_s->ap_iface == NULL) {
   4393 #ifdef CONFIG_OFFCHANNEL
   4394 			if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
   4395 			    data->tx_status.stype == WLAN_FC_STYPE_ACTION)
   4396 				offchannel_send_action_tx_status(
   4397 					wpa_s, data->tx_status.dst,
   4398 					data->tx_status.data,
   4399 					data->tx_status.data_len,
   4400 					data->tx_status.ack ?
   4401 					OFFCHANNEL_SEND_ACTION_SUCCESS :
   4402 					OFFCHANNEL_SEND_ACTION_NO_ACK);
   4403 #endif /* CONFIG_OFFCHANNEL */
   4404 			break;
   4405 		}
   4406 #endif /* CONFIG_AP */
   4407 #ifdef CONFIG_OFFCHANNEL
   4408 		wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
   4409 			MACSTR, MAC2STR(wpa_s->p2pdev->pending_action_dst));
   4410 		/*
   4411 		 * Catch TX status events for Action frames we sent via group
   4412 		 * interface in GO mode, or via standalone AP interface.
   4413 		 * Note, wpa_s->p2pdev will be the same as wpa_s->parent,
   4414 		 * except when the primary interface is used as a GO interface
   4415 		 * (for drivers which do not have group interface concurrency)
   4416 		 */
   4417 		if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
   4418 		    data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
   4419 		    os_memcmp(wpa_s->p2pdev->pending_action_dst,
   4420 			      data->tx_status.dst, ETH_ALEN) == 0) {
   4421 			offchannel_send_action_tx_status(
   4422 				wpa_s->p2pdev, data->tx_status.dst,
   4423 				data->tx_status.data,
   4424 				data->tx_status.data_len,
   4425 				data->tx_status.ack ?
   4426 				OFFCHANNEL_SEND_ACTION_SUCCESS :
   4427 				OFFCHANNEL_SEND_ACTION_NO_ACK);
   4428 			break;
   4429 		}
   4430 #endif /* CONFIG_OFFCHANNEL */
   4431 #ifdef CONFIG_AP
   4432 		switch (data->tx_status.type) {
   4433 		case WLAN_FC_TYPE_MGMT:
   4434 			ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
   4435 				      data->tx_status.data_len,
   4436 				      data->tx_status.stype,
   4437 				      data->tx_status.ack);
   4438 			break;
   4439 		case WLAN_FC_TYPE_DATA:
   4440 			ap_tx_status(wpa_s, data->tx_status.dst,
   4441 				     data->tx_status.data,
   4442 				     data->tx_status.data_len,
   4443 				     data->tx_status.ack);
   4444 			break;
   4445 		}
   4446 #endif /* CONFIG_AP */
   4447 		break;
   4448 #ifdef CONFIG_AP
   4449 	case EVENT_EAPOL_TX_STATUS:
   4450 		ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
   4451 				   data->eapol_tx_status.data,
   4452 				   data->eapol_tx_status.data_len,
   4453 				   data->eapol_tx_status.ack);
   4454 		break;
   4455 	case EVENT_DRIVER_CLIENT_POLL_OK:
   4456 		ap_client_poll_ok(wpa_s, data->client_poll.addr);
   4457 		break;
   4458 	case EVENT_RX_FROM_UNKNOWN:
   4459 		if (wpa_s->ap_iface == NULL)
   4460 			break;
   4461 		ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
   4462 				       data->rx_from_unknown.wds);
   4463 		break;
   4464 #endif /* CONFIG_AP */
   4465 	case EVENT_CH_SWITCH:
   4466 		if (!data || !wpa_s->current_ssid)
   4467 			break;
   4468 
   4469 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CHANNEL_SWITCH
   4470 			"freq=%d ht_enabled=%d ch_offset=%d ch_width=%s cf1=%d cf2=%d",
   4471 			data->ch_switch.freq,
   4472 			data->ch_switch.ht_enabled,
   4473 			data->ch_switch.ch_offset,
   4474 			channel_width_to_string(data->ch_switch.ch_width),
   4475 			data->ch_switch.cf1,
   4476 			data->ch_switch.cf2);
   4477 
   4478 		wpa_s->assoc_freq = data->ch_switch.freq;
   4479 		wpa_s->current_ssid->frequency = data->ch_switch.freq;
   4480 
   4481 #ifdef CONFIG_AP
   4482 		if (wpa_s->current_ssid->mode == WPAS_MODE_AP ||
   4483 		    wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO ||
   4484 		    wpa_s->current_ssid->mode == WPAS_MODE_MESH ||
   4485 		    wpa_s->current_ssid->mode ==
   4486 		    WPAS_MODE_P2P_GROUP_FORMATION) {
   4487 			wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
   4488 					  data->ch_switch.ht_enabled,
   4489 					  data->ch_switch.ch_offset,
   4490 					  data->ch_switch.ch_width,
   4491 					  data->ch_switch.cf1,
   4492 					  data->ch_switch.cf2);
   4493 		}
   4494 #endif /* CONFIG_AP */
   4495 
   4496 #ifdef CONFIG_IEEE80211W
   4497 		sme_event_ch_switch(wpa_s);
   4498 #endif /* CONFIG_IEEE80211W */
   4499 		wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_CS);
   4500 		wnm_clear_coloc_intf_reporting(wpa_s);
   4501 		break;
   4502 #ifdef CONFIG_AP
   4503 #ifdef NEED_AP_MLME
   4504 	case EVENT_DFS_RADAR_DETECTED:
   4505 		if (data)
   4506 			wpas_ap_event_dfs_radar_detected(wpa_s,
   4507 							 &data->dfs_event);
   4508 		break;
   4509 	case EVENT_DFS_NOP_FINISHED:
   4510 		if (data)
   4511 			wpas_ap_event_dfs_cac_nop_finished(wpa_s,
   4512 							   &data->dfs_event);
   4513 		break;
   4514 #endif /* NEED_AP_MLME */
   4515 #endif /* CONFIG_AP */
   4516 	case EVENT_DFS_CAC_STARTED:
   4517 		if (data)
   4518 			wpas_event_dfs_cac_started(wpa_s, &data->dfs_event);
   4519 		break;
   4520 	case EVENT_DFS_CAC_FINISHED:
   4521 		if (data)
   4522 			wpas_event_dfs_cac_finished(wpa_s, &data->dfs_event);
   4523 		break;
   4524 	case EVENT_DFS_CAC_ABORTED:
   4525 		if (data)
   4526 			wpas_event_dfs_cac_aborted(wpa_s, &data->dfs_event);
   4527 		break;
   4528 	case EVENT_RX_MGMT: {
   4529 		u16 fc, stype;
   4530 		const struct ieee80211_mgmt *mgmt;
   4531 
   4532 #ifdef CONFIG_TESTING_OPTIONS
   4533 		if (wpa_s->ext_mgmt_frame_handling) {
   4534 			struct rx_mgmt *rx = &data->rx_mgmt;
   4535 			size_t hex_len = 2 * rx->frame_len + 1;
   4536 			char *hex = os_malloc(hex_len);
   4537 			if (hex) {
   4538 				wpa_snprintf_hex(hex, hex_len,
   4539 						 rx->frame, rx->frame_len);
   4540 				wpa_msg(wpa_s, MSG_INFO, "MGMT-RX freq=%d datarate=%u ssi_signal=%d %s",
   4541 					rx->freq, rx->datarate, rx->ssi_signal,
   4542 					hex);
   4543 				os_free(hex);
   4544 			}
   4545 			break;
   4546 		}
   4547 #endif /* CONFIG_TESTING_OPTIONS */
   4548 
   4549 		mgmt = (const struct ieee80211_mgmt *)
   4550 			data->rx_mgmt.frame;
   4551 		fc = le_to_host16(mgmt->frame_control);
   4552 		stype = WLAN_FC_GET_STYPE(fc);
   4553 
   4554 #ifdef CONFIG_AP
   4555 		if (wpa_s->ap_iface == NULL) {
   4556 #endif /* CONFIG_AP */
   4557 #ifdef CONFIG_P2P
   4558 			if (stype == WLAN_FC_STYPE_PROBE_REQ &&
   4559 			    data->rx_mgmt.frame_len > IEEE80211_HDRLEN) {
   4560 				const u8 *src = mgmt->sa;
   4561 				const u8 *ie;
   4562 				size_t ie_len;
   4563 
   4564 				ie = data->rx_mgmt.frame + IEEE80211_HDRLEN;
   4565 				ie_len = data->rx_mgmt.frame_len -
   4566 					IEEE80211_HDRLEN;
   4567 				wpas_p2p_probe_req_rx(
   4568 					wpa_s, src, mgmt->da,
   4569 					mgmt->bssid, ie, ie_len,
   4570 					data->rx_mgmt.freq,
   4571 					data->rx_mgmt.ssi_signal);
   4572 				break;
   4573 			}
   4574 #endif /* CONFIG_P2P */
   4575 #ifdef CONFIG_IBSS_RSN
   4576 			if (wpa_s->current_ssid &&
   4577 			    wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
   4578 			    stype == WLAN_FC_STYPE_AUTH &&
   4579 			    data->rx_mgmt.frame_len >= 30) {
   4580 				wpa_supplicant_event_ibss_auth(wpa_s, data);
   4581 				break;
   4582 			}
   4583 #endif /* CONFIG_IBSS_RSN */
   4584 
   4585 			if (stype == WLAN_FC_STYPE_ACTION) {
   4586 				wpas_event_rx_mgmt_action(
   4587 					wpa_s, data->rx_mgmt.frame,
   4588 					data->rx_mgmt.frame_len,
   4589 					data->rx_mgmt.freq,
   4590 					data->rx_mgmt.ssi_signal);
   4591 				break;
   4592 			}
   4593 
   4594 			if (wpa_s->ifmsh) {
   4595 				mesh_mpm_mgmt_rx(wpa_s, &data->rx_mgmt);
   4596 				break;
   4597 			}
   4598 
   4599 #ifdef CONFIG_SAE
   4600 			if (stype == WLAN_FC_STYPE_AUTH &&
   4601 			    !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
   4602 			    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE)) {
   4603 				sme_external_auth_mgmt_rx(
   4604 					wpa_s, data->rx_mgmt.frame,
   4605 					data->rx_mgmt.frame_len);
   4606 				break;
   4607 			}
   4608 #endif /* CONFIG_SAE */
   4609 			wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
   4610 				"management frame in non-AP mode");
   4611 			break;
   4612 #ifdef CONFIG_AP
   4613 		}
   4614 
   4615 		if (stype == WLAN_FC_STYPE_PROBE_REQ &&
   4616 		    data->rx_mgmt.frame_len > IEEE80211_HDRLEN) {
   4617 			const u8 *ie;
   4618 			size_t ie_len;
   4619 
   4620 			ie = data->rx_mgmt.frame + IEEE80211_HDRLEN;
   4621 			ie_len = data->rx_mgmt.frame_len - IEEE80211_HDRLEN;
   4622 
   4623 			wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
   4624 					 mgmt->bssid, ie, ie_len,
   4625 					 data->rx_mgmt.ssi_signal);
   4626 		}
   4627 
   4628 		ap_mgmt_rx(wpa_s, &data->rx_mgmt);
   4629 #endif /* CONFIG_AP */
   4630 		break;
   4631 		}
   4632 	case EVENT_RX_PROBE_REQ:
   4633 		if (data->rx_probe_req.sa == NULL ||
   4634 		    data->rx_probe_req.ie == NULL)
   4635 			break;
   4636 #ifdef CONFIG_AP
   4637 		if (wpa_s->ap_iface) {
   4638 			hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
   4639 					     data->rx_probe_req.sa,
   4640 					     data->rx_probe_req.da,
   4641 					     data->rx_probe_req.bssid,
   4642 					     data->rx_probe_req.ie,
   4643 					     data->rx_probe_req.ie_len,
   4644 					     data->rx_probe_req.ssi_signal);
   4645 			break;
   4646 		}
   4647 #endif /* CONFIG_AP */
   4648 		wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
   4649 				      data->rx_probe_req.da,
   4650 				      data->rx_probe_req.bssid,
   4651 				      data->rx_probe_req.ie,
   4652 				      data->rx_probe_req.ie_len,
   4653 				      0,
   4654 				      data->rx_probe_req.ssi_signal);
   4655 		break;
   4656 	case EVENT_REMAIN_ON_CHANNEL:
   4657 #ifdef CONFIG_OFFCHANNEL
   4658 		offchannel_remain_on_channel_cb(
   4659 			wpa_s, data->remain_on_channel.freq,
   4660 			data->remain_on_channel.duration);
   4661 #endif /* CONFIG_OFFCHANNEL */
   4662 		wpas_p2p_remain_on_channel_cb(
   4663 			wpa_s, data->remain_on_channel.freq,
   4664 			data->remain_on_channel.duration);
   4665 		break;
   4666 	case EVENT_CANCEL_REMAIN_ON_CHANNEL:
   4667 #ifdef CONFIG_OFFCHANNEL
   4668 		offchannel_cancel_remain_on_channel_cb(
   4669 			wpa_s, data->remain_on_channel.freq);
   4670 #endif /* CONFIG_OFFCHANNEL */
   4671 		wpas_p2p_cancel_remain_on_channel_cb(
   4672 			wpa_s, data->remain_on_channel.freq);
   4673 #ifdef CONFIG_DPP
   4674 		wpas_dpp_cancel_remain_on_channel_cb(
   4675 			wpa_s, data->remain_on_channel.freq);
   4676 #endif /* CONFIG_DPP */
   4677 		break;
   4678 	case EVENT_EAPOL_RX:
   4679 		wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
   4680 					data->eapol_rx.data,
   4681 					data->eapol_rx.data_len);
   4682 		break;
   4683 	case EVENT_SIGNAL_CHANGE:
   4684 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SIGNAL_CHANGE
   4685 			"above=%d signal=%d noise=%d txrate=%d",
   4686 			data->signal_change.above_threshold,
   4687 			data->signal_change.current_signal,
   4688 			data->signal_change.current_noise,
   4689 			data->signal_change.current_txrate);
   4690 		wpa_bss_update_level(wpa_s->current_bss,
   4691 				     data->signal_change.current_signal);
   4692 		bgscan_notify_signal_change(
   4693 			wpa_s, data->signal_change.above_threshold,
   4694 			data->signal_change.current_signal,
   4695 			data->signal_change.current_noise,
   4696 			data->signal_change.current_txrate);
   4697 		break;
   4698 	case EVENT_INTERFACE_MAC_CHANGED:
   4699 		wpa_supplicant_update_mac_addr(wpa_s);
   4700 		break;
   4701 	case EVENT_INTERFACE_ENABLED:
   4702 		wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
   4703 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
   4704 			wpa_supplicant_update_mac_addr(wpa_s);
   4705 			if (wpa_s->p2p_mgmt) {
   4706 				wpa_supplicant_set_state(wpa_s,
   4707 							 WPA_DISCONNECTED);
   4708 				break;
   4709 			}
   4710 
   4711 #ifdef CONFIG_AP
   4712 			if (!wpa_s->ap_iface) {
   4713 				wpa_supplicant_set_state(wpa_s,
   4714 							 WPA_DISCONNECTED);
   4715 				wpa_s->scan_req = NORMAL_SCAN_REQ;
   4716 				wpa_supplicant_req_scan(wpa_s, 0, 0);
   4717 			} else
   4718 				wpa_supplicant_set_state(wpa_s,
   4719 							 WPA_COMPLETED);
   4720 #else /* CONFIG_AP */
   4721 			wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
   4722 			wpa_supplicant_req_scan(wpa_s, 0, 0);
   4723 #endif /* CONFIG_AP */
   4724 		}
   4725 		break;
   4726 	case EVENT_INTERFACE_DISABLED:
   4727 		wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
   4728 #ifdef CONFIG_P2P
   4729 		if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
   4730 		    (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
   4731 		     wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
   4732 			/*
   4733 			 * Mark interface disabled if this happens to end up not
   4734 			 * being removed as a separate P2P group interface.
   4735 			 */
   4736 			wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
   4737 			/*
   4738 			 * The interface was externally disabled. Remove
   4739 			 * it assuming an external entity will start a
   4740 			 * new session if needed.
   4741 			 */
   4742 			if (wpa_s->current_ssid &&
   4743 			    wpa_s->current_ssid->p2p_group)
   4744 				wpas_p2p_interface_unavailable(wpa_s);
   4745 			else
   4746 				wpas_p2p_disconnect(wpa_s);
   4747 			/*
   4748 			 * wpa_s instance may have been freed, so must not use
   4749 			 * it here anymore.
   4750 			 */
   4751 			break;
   4752 		}
   4753 		if (wpa_s->p2p_scan_work && wpa_s->global->p2p &&
   4754 		    p2p_in_progress(wpa_s->global->p2p) > 1) {
   4755 			/* This radio work will be cancelled, so clear P2P
   4756 			 * state as well.
   4757 			 */
   4758 			p2p_stop_find(wpa_s->global->p2p);
   4759 		}
   4760 #endif /* CONFIG_P2P */
   4761 
   4762 		if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
   4763 			/*
   4764 			 * Indicate disconnection to keep ctrl_iface events
   4765 			 * consistent.
   4766 			 */
   4767 			wpa_supplicant_event_disassoc(
   4768 				wpa_s, WLAN_REASON_DEAUTH_LEAVING, 1);
   4769 		}
   4770 		wpa_supplicant_mark_disassoc(wpa_s);
   4771 		if (!wpa_s->conf->bss_no_flush_when_down)
   4772 			wpa_bss_flush(wpa_s);
   4773 		radio_remove_works(wpa_s, NULL, 0);
   4774 
   4775 		wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
   4776 		break;
   4777 	case EVENT_CHANNEL_LIST_CHANGED:
   4778 		wpa_supplicant_update_channel_list(
   4779 			wpa_s, &data->channel_list_changed);
   4780 		break;
   4781 	case EVENT_INTERFACE_UNAVAILABLE:
   4782 		wpas_p2p_interface_unavailable(wpa_s);
   4783 		break;
   4784 	case EVENT_BEST_CHANNEL:
   4785 		wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
   4786 			"(%d %d %d)",
   4787 			data->best_chan.freq_24, data->best_chan.freq_5,
   4788 			data->best_chan.freq_overall);
   4789 		wpa_s->best_24_freq = data->best_chan.freq_24;
   4790 		wpa_s->best_5_freq = data->best_chan.freq_5;
   4791 		wpa_s->best_overall_freq = data->best_chan.freq_overall;
   4792 		wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
   4793 					      data->best_chan.freq_5,
   4794 					      data->best_chan.freq_overall);
   4795 		break;
   4796 	case EVENT_UNPROT_DEAUTH:
   4797 		wpa_supplicant_event_unprot_deauth(wpa_s,
   4798 						   &data->unprot_deauth);
   4799 		break;
   4800 	case EVENT_UNPROT_DISASSOC:
   4801 		wpa_supplicant_event_unprot_disassoc(wpa_s,
   4802 						     &data->unprot_disassoc);
   4803 		break;
   4804 	case EVENT_STATION_LOW_ACK:
   4805 #ifdef CONFIG_AP
   4806 		if (wpa_s->ap_iface && data)
   4807 			hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
   4808 						  data->low_ack.addr);
   4809 #endif /* CONFIG_AP */
   4810 #ifdef CONFIG_TDLS
   4811 		if (data)
   4812 			wpa_tdls_disable_unreachable_link(wpa_s->wpa,
   4813 							  data->low_ack.addr);
   4814 #endif /* CONFIG_TDLS */
   4815 		break;
   4816 	case EVENT_IBSS_PEER_LOST:
   4817 #ifdef CONFIG_IBSS_RSN
   4818 		ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
   4819 #endif /* CONFIG_IBSS_RSN */
   4820 		break;
   4821 	case EVENT_DRIVER_GTK_REKEY:
   4822 		if (os_memcmp(data->driver_gtk_rekey.bssid,
   4823 			      wpa_s->bssid, ETH_ALEN))
   4824 			break;
   4825 		if (!wpa_s->wpa)
   4826 			break;
   4827 		wpa_sm_update_replay_ctr(wpa_s->wpa,
   4828 					 data->driver_gtk_rekey.replay_ctr);
   4829 		break;
   4830 	case EVENT_SCHED_SCAN_STOPPED:
   4831 		wpa_s->sched_scanning = 0;
   4832 		resched = wpa_s->scanning && wpas_scan_scheduled(wpa_s);
   4833 		wpa_supplicant_notify_scanning(wpa_s, 0);
   4834 
   4835 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
   4836 			break;
   4837 
   4838 		/*
   4839 		 * If the driver stopped scanning without being requested to,
   4840 		 * request a new scan to continue scanning for networks.
   4841 		 */
   4842 		if (!wpa_s->sched_scan_stop_req &&
   4843 		    wpa_s->wpa_state == WPA_SCANNING) {
   4844 			wpa_dbg(wpa_s, MSG_DEBUG,
   4845 				"Restart scanning after unexpected sched_scan stop event");
   4846 			wpa_supplicant_req_scan(wpa_s, 1, 0);
   4847 			break;
   4848 		}
   4849 
   4850 		wpa_s->sched_scan_stop_req = 0;
   4851 
   4852 		/*
   4853 		 * Start a new sched scan to continue searching for more SSIDs
   4854 		 * either if timed out or PNO schedule scan is pending.
   4855 		 */
   4856 		if (wpa_s->sched_scan_timed_out) {
   4857 			wpa_supplicant_req_sched_scan(wpa_s);
   4858 		} else if (wpa_s->pno_sched_pending) {
   4859 			wpa_s->pno_sched_pending = 0;
   4860 			wpas_start_pno(wpa_s);
   4861 		} else if (resched) {
   4862 			wpa_supplicant_req_scan(wpa_s, 0, 0);
   4863 		}
   4864 
   4865 		break;
   4866 	case EVENT_WPS_BUTTON_PUSHED:
   4867 #ifdef CONFIG_WPS
   4868 		wpas_wps_start_pbc(wpa_s, NULL, 0, 0);
   4869 #endif /* CONFIG_WPS */
   4870 		break;
   4871 	case EVENT_AVOID_FREQUENCIES:
   4872 		wpa_supplicant_notify_avoid_freq(wpa_s, data);
   4873 		break;
   4874 	case EVENT_CONNECT_FAILED_REASON:
   4875 #ifdef CONFIG_AP
   4876 		if (!wpa_s->ap_iface || !data)
   4877 			break;
   4878 		hostapd_event_connect_failed_reason(
   4879 			wpa_s->ap_iface->bss[0],
   4880 			data->connect_failed_reason.addr,
   4881 			data->connect_failed_reason.code);
   4882 #endif /* CONFIG_AP */
   4883 		break;
   4884 	case EVENT_NEW_PEER_CANDIDATE:
   4885 #ifdef CONFIG_MESH
   4886 		if (!wpa_s->ifmsh || !data)
   4887 			break;
   4888 		wpa_mesh_notify_peer(wpa_s, data->mesh_peer.peer,
   4889 				     data->mesh_peer.ies,
   4890 				     data->mesh_peer.ie_len);
   4891 #endif /* CONFIG_MESH */
   4892 		break;
   4893 	case EVENT_SURVEY:
   4894 #ifdef CONFIG_AP
   4895 		if (!wpa_s->ap_iface)
   4896 			break;
   4897 		hostapd_event_get_survey(wpa_s->ap_iface,
   4898 					 &data->survey_results);
   4899 #endif /* CONFIG_AP */
   4900 		break;
   4901 	case EVENT_ACS_CHANNEL_SELECTED:
   4902 #ifdef CONFIG_AP
   4903 #ifdef CONFIG_ACS
   4904 		if (!wpa_s->ap_iface)
   4905 			break;
   4906 		hostapd_acs_channel_selected(wpa_s->ap_iface->bss[0],
   4907 					     &data->acs_selected_channels);
   4908 #endif /* CONFIG_ACS */
   4909 #endif /* CONFIG_AP */
   4910 		break;
   4911 	case EVENT_P2P_LO_STOP:
   4912 #ifdef CONFIG_P2P
   4913 		wpa_s->p2p_lo_started = 0;
   4914 		wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_LISTEN_OFFLOAD_STOP
   4915 			P2P_LISTEN_OFFLOAD_STOP_REASON "reason=%d",
   4916 			data->p2p_lo_stop.reason_code);
   4917 #endif /* CONFIG_P2P */
   4918 		break;
   4919 	case EVENT_BEACON_LOSS:
   4920 		if (!wpa_s->current_bss || !wpa_s->current_ssid)
   4921 			break;
   4922 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_BEACON_LOSS);
   4923 		bgscan_notify_beacon_loss(wpa_s);
   4924 		break;
   4925 	case EVENT_EXTERNAL_AUTH:
   4926 #ifdef CONFIG_SAE
   4927 		if (!wpa_s->current_ssid) {
   4928 			wpa_printf(MSG_DEBUG, "SAE: current_ssid is NULL");
   4929 			break;
   4930 		}
   4931 		sme_external_auth_trigger(wpa_s, data);
   4932 #endif /* CONFIG_SAE */
   4933 		break;
   4934 	case EVENT_PORT_AUTHORIZED:
   4935 		wpa_supplicant_event_port_authorized(wpa_s);
   4936 		break;
   4937 	case EVENT_STATION_OPMODE_CHANGED:
   4938 #ifdef CONFIG_AP
   4939 		if (!wpa_s->ap_iface || !data)
   4940 			break;
   4941 
   4942 		hostapd_event_sta_opmode_changed(wpa_s->ap_iface->bss[0],
   4943 						 data->sta_opmode.addr,
   4944 						 data->sta_opmode.smps_mode,
   4945 						 data->sta_opmode.chan_width,
   4946 						 data->sta_opmode.rx_nss);
   4947 #endif /* CONFIG_AP */
   4948 		break;
   4949 	default:
   4950 		wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
   4951 		break;
   4952 	}
   4953 }
   4954 
   4955 
   4956 void wpa_supplicant_event_global(void *ctx, enum wpa_event_type event,
   4957 				 union wpa_event_data *data)
   4958 {
   4959 	struct wpa_supplicant *wpa_s;
   4960 
   4961 	if (event != EVENT_INTERFACE_STATUS)
   4962 		return;
   4963 
   4964 	wpa_s = wpa_supplicant_get_iface(ctx, data->interface_status.ifname);
   4965 	if (wpa_s && wpa_s->driver->get_ifindex) {
   4966 		unsigned int ifindex;
   4967 
   4968 		ifindex = wpa_s->driver->get_ifindex(wpa_s->drv_priv);
   4969 		if (ifindex != data->interface_status.ifindex) {
   4970 			wpa_dbg(wpa_s, MSG_DEBUG,
   4971 				"interface status ifindex %d mismatch (%d)",
   4972 				ifindex, data->interface_status.ifindex);
   4973 			return;
   4974 		}
   4975 	}
   4976 #ifdef CONFIG_MATCH_IFACE
   4977 	else if (data->interface_status.ievent == EVENT_INTERFACE_ADDED) {
   4978 		struct wpa_interface *wpa_i;
   4979 
   4980 		wpa_i = wpa_supplicant_match_iface(
   4981 			ctx, data->interface_status.ifname);
   4982 		if (!wpa_i)
   4983 			return;
   4984 		wpa_s = wpa_supplicant_add_iface(ctx, wpa_i, NULL);
   4985 		os_free(wpa_i);
   4986 		if (wpa_s)
   4987 			wpa_s->matched = 1;
   4988 	}
   4989 #endif /* CONFIG_MATCH_IFACE */
   4990 
   4991 	if (wpa_s)
   4992 		wpa_supplicant_event(wpa_s, event, data);
   4993 }
   4994