Home | History | Annotate | Line # | 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 "ap/sta_info.h"
     26 #include "p2p/p2p.h"
     27 #include "fst/fst.h"
     28 #include "wnm_sta.h"
     29 #include "notify.h"
     30 #include "common/ieee802_11_defs.h"
     31 #include "common/ieee802_11_common.h"
     32 #include "common/gas_server.h"
     33 #include "common/dpp.h"
     34 #include "common/ptksa_cache.h"
     35 #include "crypto/random.h"
     36 #include "bssid_ignore.h"
     37 #include "wpas_glue.h"
     38 #include "wps_supplicant.h"
     39 #include "ibss_rsn.h"
     40 #include "sme.h"
     41 #include "gas_query.h"
     42 #include "p2p_supplicant.h"
     43 #include "bgscan.h"
     44 #include "autoscan.h"
     45 #include "ap.h"
     46 #include "bss.h"
     47 #include "scan.h"
     48 #include "offchannel.h"
     49 #include "interworking.h"
     50 #include "mesh.h"
     51 #include "mesh_mpm.h"
     52 #include "wmm_ac.h"
     53 #include "nan_usd.h"
     54 #include "dpp_supplicant.h"
     55 
     56 
     57 #define MAX_OWE_TRANSITION_BSS_SELECT_COUNT 5
     58 
     59 
     60 #ifndef CONFIG_NO_SCAN_PROCESSING
     61 static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
     62 					      int new_scan, int own_request,
     63 					      bool trigger_6ghz_scan,
     64 					      union wpa_event_data *data);
     65 #endif /* CONFIG_NO_SCAN_PROCESSING */
     66 
     67 
     68 int wpas_temp_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
     69 {
     70 	struct os_reltime now;
     71 
     72 	if (ssid == NULL || ssid->disabled_until.sec == 0)
     73 		return 0;
     74 
     75 	os_get_reltime(&now);
     76 	if (ssid->disabled_until.sec > now.sec)
     77 		return ssid->disabled_until.sec - now.sec;
     78 
     79 	wpas_clear_temp_disabled(wpa_s, ssid, 0);
     80 
     81 	return 0;
     82 }
     83 
     84 
     85 #ifndef CONFIG_NO_SCAN_PROCESSING
     86 /**
     87  * wpas_reenabled_network_time - Time until first network is re-enabled
     88  * @wpa_s: Pointer to wpa_supplicant data
     89  * Returns: If all enabled networks are temporarily disabled, returns the time
     90  *	(in sec) until the first network is re-enabled. Otherwise returns 0.
     91  *
     92  * This function is used in case all enabled networks are temporarily disabled,
     93  * in which case it returns the time (in sec) that the first network will be
     94  * re-enabled. The function assumes that at least one network is enabled.
     95  */
     96 static int wpas_reenabled_network_time(struct wpa_supplicant *wpa_s)
     97 {
     98 	struct wpa_ssid *ssid;
     99 	int disabled_for, res = 0;
    100 
    101 #ifdef CONFIG_INTERWORKING
    102 	if (wpa_s->conf->auto_interworking && wpa_s->conf->interworking &&
    103 	    wpa_s->conf->cred)
    104 		return 0;
    105 #endif /* CONFIG_INTERWORKING */
    106 
    107 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
    108 		if (ssid->disabled)
    109 			continue;
    110 
    111 		disabled_for = wpas_temp_disabled(wpa_s, ssid);
    112 		if (!disabled_for)
    113 			return 0;
    114 
    115 		if (!res || disabled_for < res)
    116 			res = disabled_for;
    117 	}
    118 
    119 	return res;
    120 }
    121 #endif /* CONFIG_NO_SCAN_PROCESSING */
    122 
    123 
    124 void wpas_network_reenabled(void *eloop_ctx, void *timeout_ctx)
    125 {
    126 	struct wpa_supplicant *wpa_s = eloop_ctx;
    127 
    128 	if (wpa_s->disconnected || wpa_s->wpa_state != WPA_SCANNING)
    129 		return;
    130 
    131 	wpa_dbg(wpa_s, MSG_DEBUG,
    132 		"Try to associate due to network getting re-enabled");
    133 	if (wpa_supplicant_fast_associate(wpa_s) != 1) {
    134 		wpa_supplicant_cancel_sched_scan(wpa_s);
    135 		wpa_supplicant_req_scan(wpa_s, 0, 0);
    136 	}
    137 }
    138 
    139 
    140 static struct wpa_bss * __wpa_supplicant_get_new_bss(
    141 	struct wpa_supplicant *wpa_s, const u8 *bssid, const u8 *ssid,
    142 	size_t ssid_len)
    143 {
    144 	if (ssid && ssid_len > 0)
    145 		return wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
    146 	else
    147 		return wpa_bss_get_bssid(wpa_s, bssid);
    148 }
    149 
    150 
    151 static struct wpa_bss * _wpa_supplicant_get_new_bss(
    152 	struct wpa_supplicant *wpa_s, const u8 *bssid, const u8 *ssid,
    153 	size_t ssid_len, bool try_update_scan_results)
    154 {
    155 	struct wpa_bss *bss = __wpa_supplicant_get_new_bss(wpa_s, bssid, ssid,
    156 							   ssid_len);
    157 
    158 	if (bss || !try_update_scan_results)
    159 		return bss;
    160 
    161 	wpa_supplicant_update_scan_results(wpa_s, bssid);
    162 
    163 	return __wpa_supplicant_get_new_bss(wpa_s, bssid, ssid, ssid_len);
    164 }
    165 
    166 
    167 static struct wpa_bss * wpa_supplicant_get_new_bss(
    168 	struct wpa_supplicant *wpa_s, const u8 *bssid)
    169 {
    170 	struct wpa_bss *bss = NULL;
    171 	struct wpa_ssid *ssid = wpa_s->current_ssid;
    172 	u8 drv_ssid[SSID_MAX_LEN];
    173 	int res;
    174 	bool try_update_scan_results = true;
    175 
    176 	res = wpa_drv_get_ssid(wpa_s, drv_ssid);
    177 	if (res > 0) {
    178 		bss = _wpa_supplicant_get_new_bss(wpa_s, bssid, drv_ssid, res,
    179 						  try_update_scan_results);
    180 		try_update_scan_results = false;
    181 	}
    182 	if (!bss && ssid && ssid->ssid_len > 0) {
    183 		bss = _wpa_supplicant_get_new_bss(wpa_s, bssid, ssid->ssid,
    184 						  ssid->ssid_len,
    185 						  try_update_scan_results);
    186 		try_update_scan_results = false;
    187 	}
    188 	if (!bss)
    189 		bss = _wpa_supplicant_get_new_bss(wpa_s, bssid, NULL, 0,
    190 						  try_update_scan_results);
    191 
    192 	return bss;
    193 }
    194 
    195 
    196 static struct wpa_bss *
    197 wpa_supplicant_update_current_bss(struct wpa_supplicant *wpa_s, const u8 *bssid)
    198 {
    199 	struct wpa_bss *bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
    200 
    201 	if (bss)
    202 		wpa_s->current_bss = bss;
    203 
    204 	return bss;
    205 }
    206 
    207 
    208 static void wpa_supplicant_update_link_bss(struct wpa_supplicant *wpa_s,
    209 					   u8 link_id, const u8 *bssid)
    210 {
    211 	struct wpa_bss *bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
    212 
    213 	if (bss)
    214 		wpa_s->links[link_id].bss = bss;
    215 }
    216 
    217 
    218 static int wpa_supplicant_select_config(struct wpa_supplicant *wpa_s,
    219 					union wpa_event_data *data)
    220 {
    221 	struct wpa_ssid *ssid, *old_ssid;
    222 	struct wpa_bss *bss;
    223 	u8 drv_ssid[SSID_MAX_LEN];
    224 	size_t drv_ssid_len;
    225 	int res;
    226 
    227 	if (wpa_s->conf->ap_scan == 1 && wpa_s->current_ssid) {
    228 		wpa_supplicant_update_current_bss(wpa_s, wpa_s->bssid);
    229 
    230 		if (wpa_s->current_ssid->ssid_len == 0)
    231 			return 0; /* current profile still in use */
    232 		res = wpa_drv_get_ssid(wpa_s, drv_ssid);
    233 		if (res < 0) {
    234 			wpa_msg(wpa_s, MSG_INFO,
    235 				"Failed to read SSID from driver");
    236 			return 0; /* try to use current profile */
    237 		}
    238 		drv_ssid_len = res;
    239 
    240 		if (drv_ssid_len == wpa_s->current_ssid->ssid_len &&
    241 		    os_memcmp(drv_ssid, wpa_s->current_ssid->ssid,
    242 			      drv_ssid_len) == 0)
    243 			return 0; /* current profile still in use */
    244 
    245 #ifdef CONFIG_OWE
    246 		if ((wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_OWE) &&
    247 		    wpa_s->current_bss &&
    248 		    (wpa_s->current_bss->flags & WPA_BSS_OWE_TRANSITION) &&
    249 		    drv_ssid_len == wpa_s->current_bss->ssid_len &&
    250 		    os_memcmp(drv_ssid, wpa_s->current_bss->ssid,
    251 			      drv_ssid_len) == 0)
    252 			return 0; /* current profile still in use */
    253 #endif /* CONFIG_OWE */
    254 
    255 		wpa_msg(wpa_s, MSG_DEBUG,
    256 			"Driver-initiated BSS selection changed the SSID to %s",
    257 			wpa_ssid_txt(drv_ssid, drv_ssid_len));
    258 		/* continue selecting a new network profile */
    259 	}
    260 
    261 	wpa_dbg(wpa_s, MSG_DEBUG, "Select network based on association "
    262 		"information");
    263 	ssid = wpa_supplicant_get_ssid(wpa_s);
    264 	if (ssid == NULL) {
    265 		wpa_msg(wpa_s, MSG_INFO,
    266 			"No network configuration found for the current AP");
    267 		return -1;
    268 	}
    269 
    270 	if (wpas_network_disabled(wpa_s, ssid)) {
    271 		wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is disabled");
    272 		return -1;
    273 	}
    274 
    275 	if (disallowed_bssid(wpa_s, wpa_s->bssid) ||
    276 	    disallowed_ssid(wpa_s, ssid->ssid, ssid->ssid_len)) {
    277 		wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS is disallowed");
    278 		return -1;
    279 	}
    280 
    281 	res = wpas_temp_disabled(wpa_s, ssid);
    282 	if (res > 0) {
    283 		wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is temporarily "
    284 			"disabled for %d second(s)", res);
    285 		return -1;
    286 	}
    287 
    288 	wpa_dbg(wpa_s, MSG_DEBUG, "Network configuration found for the "
    289 		"current AP");
    290 	bss = wpa_supplicant_update_current_bss(wpa_s, wpa_s->bssid);
    291 	if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
    292 		u8 wpa_ie[80];
    293 		size_t wpa_ie_len = sizeof(wpa_ie);
    294 		bool skip_default_rsne;
    295 
    296 		/* Do not override RSNE/RSNXE with the default values if the
    297 		 * driver indicated the actual values used in the
    298 		 * (Re)Association Request frame. */
    299 		skip_default_rsne = data && data->assoc_info.req_ies;
    300 		if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
    301 					      wpa_ie, &wpa_ie_len,
    302 					      skip_default_rsne) < 0)
    303 			wpa_dbg(wpa_s, MSG_DEBUG, "Could not set WPA suites");
    304 	} else {
    305 		wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
    306 	}
    307 
    308 	if (wpa_s->current_ssid && wpa_s->current_ssid != ssid)
    309 		eapol_sm_invalidate_cached_session(wpa_s->eapol);
    310 	old_ssid = wpa_s->current_ssid;
    311 	wpa_s->current_ssid = ssid;
    312 
    313 	wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
    314 	wpa_supplicant_initiate_eapol(wpa_s);
    315 	if (old_ssid != wpa_s->current_ssid)
    316 		wpas_notify_network_changed(wpa_s);
    317 
    318 	return 0;
    319 }
    320 
    321 
    322 void wpa_supplicant_stop_countermeasures(void *eloop_ctx, void *sock_ctx)
    323 {
    324 	struct wpa_supplicant *wpa_s = eloop_ctx;
    325 
    326 	if (wpa_s->countermeasures) {
    327 		wpa_s->countermeasures = 0;
    328 		wpa_drv_set_countermeasures(wpa_s, 0);
    329 		wpa_msg(wpa_s, MSG_INFO, "WPA: TKIP countermeasures stopped");
    330 
    331 		/*
    332 		 * It is possible that the device is sched scanning, which means
    333 		 * that a connection attempt will be done only when we receive
    334 		 * scan results. However, in this case, it would be preferable
    335 		 * to scan and connect immediately, so cancel the sched_scan and
    336 		 * issue a regular scan flow.
    337 		 */
    338 		wpa_supplicant_cancel_sched_scan(wpa_s);
    339 		wpa_supplicant_req_scan(wpa_s, 0, 0);
    340 	}
    341 }
    342 
    343 
    344 void wpas_reset_mlo_info(struct wpa_supplicant *wpa_s)
    345 {
    346 	if (!wpa_s->valid_links)
    347 		return;
    348 
    349 	wpa_s->valid_links = 0;
    350 	wpa_s->mlo_assoc_link_id = 0;
    351 	os_memset(wpa_s->ap_mld_addr, 0, ETH_ALEN);
    352 	os_memset(wpa_s->links, 0, sizeof(wpa_s->links));
    353 }
    354 
    355 
    356 void wpa_supplicant_mark_disassoc(struct wpa_supplicant *wpa_s)
    357 {
    358 	int bssid_changed;
    359 
    360 	wnm_bss_keep_alive_deinit(wpa_s);
    361 
    362 #ifdef CONFIG_IBSS_RSN
    363 	ibss_rsn_deinit(wpa_s->ibss_rsn);
    364 	wpa_s->ibss_rsn = NULL;
    365 #endif /* CONFIG_IBSS_RSN */
    366 
    367 #ifdef CONFIG_AP
    368 	wpa_supplicant_ap_deinit(wpa_s);
    369 #endif /* CONFIG_AP */
    370 
    371 #ifdef CONFIG_HS20
    372 	/* Clear possibly configured frame filters */
    373 	wpa_drv_configure_frame_filters(wpa_s, 0);
    374 #endif /* CONFIG_HS20 */
    375 
    376 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
    377 		return;
    378 
    379 	if (os_reltime_initialized(&wpa_s->session_start)) {
    380 		os_reltime_age(&wpa_s->session_start, &wpa_s->session_length);
    381 		wpa_s->session_start.sec = 0;
    382 		wpa_s->session_start.usec = 0;
    383 		wpas_notify_session_length(wpa_s);
    384 	}
    385 
    386 	wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
    387 	bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
    388 	os_memset(wpa_s->bssid, 0, ETH_ALEN);
    389 	os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
    390 	sme_clear_on_disassoc(wpa_s);
    391 	wpa_s->current_bss = NULL;
    392 	wpa_s->assoc_freq = 0;
    393 
    394 	if (bssid_changed)
    395 		wpas_notify_bssid_changed(wpa_s);
    396 
    397 	eapol_sm_notify_portEnabled(wpa_s->eapol, false);
    398 	eapol_sm_notify_portValid(wpa_s->eapol, false);
    399 	if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
    400 	    wpa_s->key_mgmt == WPA_KEY_MGMT_OWE ||
    401 	    wpa_s->key_mgmt == WPA_KEY_MGMT_DPP || wpa_s->drv_authorized_port)
    402 		eapol_sm_notify_eap_success(wpa_s->eapol, false);
    403 	wpa_s->drv_authorized_port = 0;
    404 	wpa_s->ap_ies_from_associnfo = 0;
    405 	wpa_s->current_ssid = NULL;
    406 	eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
    407 	wpa_s->key_mgmt = 0;
    408 	wpa_s->allowed_key_mgmts = 0;
    409 
    410 #ifndef CONFIG_NO_RRM
    411 	wpas_rrm_reset(wpa_s);
    412 #endif /* CONFIG_NO_RRM */
    413 	wpa_s->wnmsleep_used = 0;
    414 #ifdef CONFIG_WNM
    415 	wpa_s->wnm_mode = 0;
    416 #endif /* CONFIG_WNM */
    417 	wnm_clear_coloc_intf_reporting(wpa_s);
    418 	wpa_s->disable_mbo_oce = 0;
    419 
    420 #ifdef CONFIG_TESTING_OPTIONS
    421 	wpa_s->last_tk_alg = WPA_ALG_NONE;
    422 	os_memset(wpa_s->last_tk, 0, sizeof(wpa_s->last_tk));
    423 #endif /* CONFIG_TESTING_OPTIONS */
    424 	wpa_s->ieee80211ac = 0;
    425 
    426 	if (wpa_s->enabled_4addr_mode && wpa_drv_set_4addr_mode(wpa_s, 0) == 0)
    427 		wpa_s->enabled_4addr_mode = 0;
    428 
    429 	wpa_s->wps_scan_done = false;
    430 	wpas_reset_mlo_info(wpa_s);
    431 
    432 #ifdef CONFIG_SME
    433 	wpa_s->sme.bss_max_idle_period = 0;
    434 #endif /* CONFIG_SME */
    435 
    436 	wpa_s->ssid_verified = false;
    437 	wpa_s->bigtk_set = false;
    438 }
    439 
    440 
    441 static void wpa_find_assoc_pmkid(struct wpa_supplicant *wpa_s, bool authorized)
    442 {
    443 	struct wpa_ie_data ie;
    444 	int pmksa_set = -1;
    445 	size_t i;
    446 	struct rsn_pmksa_cache_entry *cur_pmksa;
    447 
    448 	/* Start with assumption of no PMKSA cache entry match for cases other
    449 	 * than SAE. In particular, this is needed to generate the PMKSA cache
    450 	 * entries for Suite B cases with driver-based roaming indication. */
    451 	cur_pmksa = pmksa_cache_get_current(wpa_s->wpa);
    452 	if (cur_pmksa && !wpa_key_mgmt_sae(cur_pmksa->akmp))
    453 		pmksa_cache_clear_current(wpa_s->wpa);
    454 
    455 	if (wpa_sm_parse_own_wpa_ie(wpa_s->wpa, &ie) < 0 ||
    456 	    ie.pmkid == NULL)
    457 		return;
    458 
    459 	for (i = 0; i < ie.num_pmkid; i++) {
    460 		pmksa_set = pmksa_cache_set_current(wpa_s->wpa,
    461 						    ie.pmkid + i * PMKID_LEN,
    462 						    NULL, NULL, 0, NULL, 0,
    463 						    true);
    464 		if (pmksa_set == 0) {
    465 			eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
    466 			if (authorized)
    467 				wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
    468 			break;
    469 		}
    470 	}
    471 
    472 	wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID from assoc IE %sfound from "
    473 		"PMKSA cache", pmksa_set == 0 ? "" : "not ");
    474 }
    475 
    476 
    477 static void wpa_supplicant_event_pmkid_candidate(struct wpa_supplicant *wpa_s,
    478 						 union wpa_event_data *data)
    479 {
    480 	if (data == NULL) {
    481 		wpa_dbg(wpa_s, MSG_DEBUG, "RSN: No data in PMKID candidate "
    482 			"event");
    483 		return;
    484 	}
    485 	wpa_dbg(wpa_s, MSG_DEBUG, "RSN: PMKID candidate event - bssid=" MACSTR
    486 		" index=%d preauth=%d",
    487 		MAC2STR(data->pmkid_candidate.bssid),
    488 		data->pmkid_candidate.index,
    489 		data->pmkid_candidate.preauth);
    490 
    491 	pmksa_candidate_add(wpa_s->wpa, data->pmkid_candidate.bssid,
    492 			    data->pmkid_candidate.index,
    493 			    data->pmkid_candidate.preauth);
    494 }
    495 
    496 
    497 static int wpa_supplicant_dynamic_keys(struct wpa_supplicant *wpa_s)
    498 {
    499 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
    500 	    wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
    501 		return 0;
    502 
    503 #ifdef IEEE8021X_EAPOL
    504 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
    505 	    wpa_s->current_ssid &&
    506 	    !(wpa_s->current_ssid->eapol_flags &
    507 	      (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
    508 	       EAPOL_FLAG_REQUIRE_KEY_BROADCAST))) {
    509 		/* IEEE 802.1X, but not using dynamic WEP keys (i.e., either
    510 		 * plaintext or static WEP keys). */
    511 		return 0;
    512 	}
    513 #endif /* IEEE8021X_EAPOL */
    514 
    515 	return 1;
    516 }
    517 
    518 
    519 /**
    520  * wpa_supplicant_scard_init - Initialize SIM/USIM access with PC/SC
    521  * @wpa_s: pointer to wpa_supplicant data
    522  * @ssid: Configuration data for the network
    523  * Returns: 0 on success, -1 on failure
    524  *
    525  * This function is called when starting authentication with a network that is
    526  * configured to use PC/SC for SIM/USIM access (EAP-SIM or EAP-AKA).
    527  */
    528 int wpa_supplicant_scard_init(struct wpa_supplicant *wpa_s,
    529 			      struct wpa_ssid *ssid)
    530 {
    531 #ifdef IEEE8021X_EAPOL
    532 #ifdef PCSC_FUNCS
    533 	int aka = 0, sim = 0;
    534 
    535 	if ((ssid != NULL && ssid->eap.pcsc == NULL) ||
    536 	    wpa_s->scard != NULL || wpa_s->conf->external_sim)
    537 		return 0;
    538 
    539 	if (ssid == NULL || ssid->eap.eap_methods == NULL) {
    540 		sim = 1;
    541 		aka = 1;
    542 	} else {
    543 		struct eap_method_type *eap = ssid->eap.eap_methods;
    544 		while (eap->vendor != EAP_VENDOR_IETF ||
    545 		       eap->method != EAP_TYPE_NONE) {
    546 			if (eap->vendor == EAP_VENDOR_IETF) {
    547 				if (eap->method == EAP_TYPE_SIM)
    548 					sim = 1;
    549 				else if (eap->method == EAP_TYPE_AKA ||
    550 					 eap->method == EAP_TYPE_AKA_PRIME)
    551 					aka = 1;
    552 			}
    553 			eap++;
    554 		}
    555 	}
    556 
    557 	if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_SIM) == NULL)
    558 		sim = 0;
    559 	if (eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA) == NULL &&
    560 	    eap_peer_get_eap_method(EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME) ==
    561 	    NULL)
    562 		aka = 0;
    563 
    564 	if (!sim && !aka) {
    565 		wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to "
    566 			"use SIM, but neither EAP-SIM nor EAP-AKA are "
    567 			"enabled");
    568 		return 0;
    569 	}
    570 
    571 	wpa_dbg(wpa_s, MSG_DEBUG, "Selected network is configured to use SIM "
    572 		"(sim=%d aka=%d) - initialize PCSC", sim, aka);
    573 
    574 	wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
    575 	if (wpa_s->scard == NULL) {
    576 		wpa_msg(wpa_s, MSG_WARNING, "Failed to initialize SIM "
    577 			"(pcsc-lite)");
    578 		return -1;
    579 	}
    580 	wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
    581 	eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
    582 #endif /* PCSC_FUNCS */
    583 #endif /* IEEE8021X_EAPOL */
    584 
    585 	return 0;
    586 }
    587 
    588 
    589 #ifndef CONFIG_NO_SCAN_PROCESSING
    590 
    591 #ifdef CONFIG_WEP
    592 static int has_wep_key(struct wpa_ssid *ssid)
    593 {
    594 	int i;
    595 
    596 	for (i = 0; i < NUM_WEP_KEYS; i++) {
    597 		if (ssid->wep_key_len[i])
    598 			return 1;
    599 	}
    600 
    601 	return 0;
    602 }
    603 #endif /* CONFIG_WEP */
    604 
    605 
    606 static int wpa_supplicant_match_privacy(struct wpa_bss *bss,
    607 					struct wpa_ssid *ssid)
    608 {
    609 	int privacy = 0;
    610 
    611 	if (ssid->mixed_cell)
    612 		return 1;
    613 
    614 #ifdef CONFIG_WPS
    615 	if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
    616 		return 1;
    617 #endif /* CONFIG_WPS */
    618 
    619 #ifdef CONFIG_OWE
    620 	if ((ssid->key_mgmt & WPA_KEY_MGMT_OWE) && !ssid->owe_only)
    621 		return 1;
    622 #endif /* CONFIG_OWE */
    623 
    624 #ifdef CONFIG_WEP
    625 	if (has_wep_key(ssid))
    626 		privacy = 1;
    627 #endif /* CONFIG_WEP */
    628 
    629 #ifdef IEEE8021X_EAPOL
    630 	if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
    631 	    ssid->eapol_flags & (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
    632 				 EAPOL_FLAG_REQUIRE_KEY_BROADCAST))
    633 		privacy = 1;
    634 #endif /* IEEE8021X_EAPOL */
    635 
    636 	if (wpa_key_mgmt_wpa(ssid->key_mgmt))
    637 		privacy = 1;
    638 
    639 	if (ssid->key_mgmt & WPA_KEY_MGMT_OSEN)
    640 		privacy = 1;
    641 
    642 	if (bss->caps & IEEE80211_CAP_PRIVACY)
    643 		return privacy;
    644 	return !privacy;
    645 }
    646 
    647 
    648 static int wpa_supplicant_ssid_bss_match(struct wpa_supplicant *wpa_s,
    649 					 struct wpa_ssid *ssid,
    650 					 struct wpa_bss *bss, int debug_print)
    651 {
    652 	struct wpa_ie_data ie;
    653 	int proto_match = 0;
    654 	const u8 *rsn_ie, *wpa_ie;
    655 	int ret;
    656 #ifdef CONFIG_WEP
    657 	int wep_ok;
    658 #endif /* CONFIG_WEP */
    659 	bool is_6ghz_bss = is_6ghz_freq(bss->freq);
    660 
    661 	ret = wpas_wps_ssid_bss_match(wpa_s, ssid, bss);
    662 	if (ret >= 0)
    663 		return ret;
    664 
    665 #ifdef CONFIG_WEP
    666 	/* Allow TSN if local configuration accepts WEP use without WPA/WPA2 */
    667 	wep_ok = !wpa_key_mgmt_wpa(ssid->key_mgmt) &&
    668 		(((ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
    669 		  ssid->wep_key_len[ssid->wep_tx_keyidx] > 0) ||
    670 		 (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA));
    671 #endif /* CONFIG_WEP */
    672 
    673 	rsn_ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
    674 	if (is_6ghz_bss && !rsn_ie) {
    675 		if (debug_print)
    676 			wpa_dbg(wpa_s, MSG_DEBUG,
    677 				"   skip - 6 GHz BSS without RSNE");
    678 		return 0;
    679 	}
    680 
    681 	while ((ssid->proto & (WPA_PROTO_RSN | WPA_PROTO_OSEN)) && rsn_ie) {
    682 		proto_match++;
    683 
    684 		if (wpa_parse_wpa_ie(rsn_ie, 2 + rsn_ie[1], &ie)) {
    685 			if (debug_print)
    686 				wpa_dbg(wpa_s, MSG_DEBUG,
    687 					"   skip RSN IE - parse failed");
    688 			break;
    689 		}
    690 		if (!ie.has_pairwise)
    691 			ie.pairwise_cipher = wpa_default_rsn_cipher(bss->freq);
    692 		if (!ie.has_group)
    693 			ie.group_cipher = wpa_default_rsn_cipher(bss->freq);
    694 
    695 		if (is_6ghz_bss || !is_zero_ether_addr(bss->mld_addr)) {
    696 			/* WEP and TKIP are not allowed on 6 GHz/MLD */
    697 			ie.pairwise_cipher &= ~(WPA_CIPHER_WEP40 |
    698 						WPA_CIPHER_WEP104 |
    699 						WPA_CIPHER_TKIP);
    700 			ie.group_cipher &= ~(WPA_CIPHER_WEP40 |
    701 					     WPA_CIPHER_WEP104 |
    702 					     WPA_CIPHER_TKIP);
    703 		}
    704 
    705 #ifdef CONFIG_WEP
    706 		if (wep_ok &&
    707 		    (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
    708 		{
    709 			if (debug_print)
    710 				wpa_dbg(wpa_s, MSG_DEBUG,
    711 					"   selected based on TSN in RSN IE");
    712 			return 1;
    713 		}
    714 #endif /* CONFIG_WEP */
    715 
    716 		if (!(ie.proto & ssid->proto) &&
    717 		    !(ssid->proto & WPA_PROTO_OSEN)) {
    718 			if (debug_print)
    719 				wpa_dbg(wpa_s, MSG_DEBUG,
    720 					"   skip RSN IE - proto mismatch");
    721 			break;
    722 		}
    723 
    724 		if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
    725 			if (debug_print)
    726 				wpa_dbg(wpa_s, MSG_DEBUG,
    727 					"   skip RSN IE - PTK cipher mismatch");
    728 			break;
    729 		}
    730 
    731 		if (!(ie.group_cipher & ssid->group_cipher)) {
    732 			if (debug_print)
    733 				wpa_dbg(wpa_s, MSG_DEBUG,
    734 					"   skip RSN IE - GTK cipher mismatch");
    735 			break;
    736 		}
    737 
    738 		if (ssid->group_mgmt_cipher &&
    739 		    !(ie.mgmt_group_cipher & ssid->group_mgmt_cipher)) {
    740 			if (debug_print)
    741 				wpa_dbg(wpa_s, MSG_DEBUG,
    742 					"   skip RSN IE - group mgmt cipher mismatch");
    743 			break;
    744 		}
    745 
    746 		if (is_6ghz_bss) {
    747 			/* MFPC must be supported on 6 GHz */
    748 			if (!(ie.capabilities & WPA_CAPABILITY_MFPC)) {
    749 				if (debug_print)
    750 					wpa_dbg(wpa_s, MSG_DEBUG,
    751 						"   skip RSNE - 6 GHz without MFPC");
    752 				break;
    753 			}
    754 
    755 			/* WPA PSK is not allowed on the 6 GHz band */
    756 			ie.key_mgmt &= ~(WPA_KEY_MGMT_PSK |
    757 					 WPA_KEY_MGMT_FT_PSK |
    758 					 WPA_KEY_MGMT_PSK_SHA256);
    759 		}
    760 
    761 		if (!(ie.key_mgmt & ssid->key_mgmt)) {
    762 			if (debug_print)
    763 				wpa_dbg(wpa_s, MSG_DEBUG,
    764 					"   skip RSN IE - key mgmt mismatch");
    765 			break;
    766 		}
    767 
    768 		if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
    769 		    wpas_get_ssid_pmf(wpa_s, ssid) ==
    770 		    MGMT_FRAME_PROTECTION_REQUIRED) {
    771 			if (debug_print)
    772 				wpa_dbg(wpa_s, MSG_DEBUG,
    773 					"   skip RSN IE - no mgmt frame protection");
    774 			break;
    775 		}
    776 		if ((ie.capabilities & WPA_CAPABILITY_MFPR) &&
    777 		    wpas_get_ssid_pmf(wpa_s, ssid) ==
    778 		    NO_MGMT_FRAME_PROTECTION) {
    779 			if (debug_print)
    780 				wpa_dbg(wpa_s, MSG_DEBUG,
    781 					"   skip RSN IE - no mgmt frame protection enabled but AP requires it");
    782 			break;
    783 		}
    784 
    785 		if (debug_print)
    786 			wpa_dbg(wpa_s, MSG_DEBUG,
    787 				"   selected based on RSN IE");
    788 		return 1;
    789 	}
    790 
    791 	if (is_6ghz_bss) {
    792 		if (debug_print)
    793 			wpa_dbg(wpa_s, MSG_DEBUG,
    794 				"   skip - 6 GHz BSS without matching RSNE");
    795 		return 0;
    796 	}
    797 
    798 	wpa_ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
    799 
    800 	if (wpas_get_ssid_pmf(wpa_s, ssid) == MGMT_FRAME_PROTECTION_REQUIRED &&
    801 	    (!(ssid->key_mgmt & WPA_KEY_MGMT_OWE) || ssid->owe_only)) {
    802 #ifdef CONFIG_OWE
    803 		if ((ssid->key_mgmt & WPA_KEY_MGMT_OWE) && ssid->owe_only &&
    804 		    !wpa_ie && !rsn_ie &&
    805 		    wpa_s->owe_transition_select &&
    806 		    wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE) &&
    807 		    ssid->owe_transition_bss_select_count + 1 <=
    808 		    MAX_OWE_TRANSITION_BSS_SELECT_COUNT) {
    809 			ssid->owe_transition_bss_select_count++;
    810 			if (debug_print)
    811 				wpa_dbg(wpa_s, MSG_DEBUG,
    812 					"   skip OWE open BSS (selection count %d does not exceed %d)",
    813 					ssid->owe_transition_bss_select_count,
    814 					MAX_OWE_TRANSITION_BSS_SELECT_COUNT);
    815 			wpa_s->owe_transition_search = 1;
    816 			return 0;
    817 		}
    818 #endif /* CONFIG_OWE */
    819 		if (debug_print)
    820 			wpa_dbg(wpa_s, MSG_DEBUG,
    821 				"   skip - MFP Required but network not MFP Capable");
    822 		return 0;
    823 	}
    824 
    825 	while ((ssid->proto & WPA_PROTO_WPA) && wpa_ie) {
    826 		proto_match++;
    827 
    828 		if (wpa_parse_wpa_ie(wpa_ie, 2 + wpa_ie[1], &ie)) {
    829 			if (debug_print)
    830 				wpa_dbg(wpa_s, MSG_DEBUG,
    831 					"   skip WPA IE - parse failed");
    832 			break;
    833 		}
    834 
    835 #ifdef CONFIG_WEP
    836 		if (wep_ok &&
    837 		    (ie.group_cipher & (WPA_CIPHER_WEP40 | WPA_CIPHER_WEP104)))
    838 		{
    839 			if (debug_print)
    840 				wpa_dbg(wpa_s, MSG_DEBUG,
    841 					"   selected based on TSN in WPA IE");
    842 			return 1;
    843 		}
    844 #endif /* CONFIG_WEP */
    845 
    846 		if (!(ie.proto & ssid->proto)) {
    847 			if (debug_print)
    848 				wpa_dbg(wpa_s, MSG_DEBUG,
    849 					"   skip WPA IE - proto mismatch");
    850 			break;
    851 		}
    852 
    853 		if (!(ie.pairwise_cipher & ssid->pairwise_cipher)) {
    854 			if (debug_print)
    855 				wpa_dbg(wpa_s, MSG_DEBUG,
    856 					"   skip WPA IE - PTK cipher mismatch");
    857 			break;
    858 		}
    859 
    860 		if (!(ie.group_cipher & ssid->group_cipher)) {
    861 			if (debug_print)
    862 				wpa_dbg(wpa_s, MSG_DEBUG,
    863 					"   skip WPA IE - GTK cipher mismatch");
    864 			break;
    865 		}
    866 
    867 		if (!(ie.key_mgmt & ssid->key_mgmt)) {
    868 			if (debug_print)
    869 				wpa_dbg(wpa_s, MSG_DEBUG,
    870 					"   skip WPA IE - key mgmt mismatch");
    871 			break;
    872 		}
    873 
    874 		if (debug_print)
    875 			wpa_dbg(wpa_s, MSG_DEBUG,
    876 				"   selected based on WPA IE");
    877 		return 1;
    878 	}
    879 
    880 	if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && !wpa_ie &&
    881 	    !rsn_ie) {
    882 		if (debug_print)
    883 			wpa_dbg(wpa_s, MSG_DEBUG,
    884 				"   allow for non-WPA IEEE 802.1X");
    885 		return 1;
    886 	}
    887 
    888 #ifdef CONFIG_OWE
    889 	if ((ssid->key_mgmt & WPA_KEY_MGMT_OWE) && !ssid->owe_only &&
    890 	    !wpa_ie && !rsn_ie) {
    891 		if (wpa_s->owe_transition_select &&
    892 		    wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE) &&
    893 		    ssid->owe_transition_bss_select_count + 1 <=
    894 		    MAX_OWE_TRANSITION_BSS_SELECT_COUNT) {
    895 			ssid->owe_transition_bss_select_count++;
    896 			if (debug_print)
    897 				wpa_dbg(wpa_s, MSG_DEBUG,
    898 					"   skip OWE transition BSS (selection count %d does not exceed %d)",
    899 					ssid->owe_transition_bss_select_count,
    900 					MAX_OWE_TRANSITION_BSS_SELECT_COUNT);
    901 			wpa_s->owe_transition_search = 1;
    902 			return 0;
    903 		}
    904 		if (debug_print)
    905 			wpa_dbg(wpa_s, MSG_DEBUG,
    906 				"   allow in OWE transition mode");
    907 		return 1;
    908 	}
    909 #endif /* CONFIG_OWE */
    910 
    911 	if ((ssid->proto & (WPA_PROTO_WPA | WPA_PROTO_RSN)) &&
    912 	    wpa_key_mgmt_wpa(ssid->key_mgmt) && proto_match == 0) {
    913 		if (debug_print)
    914 			wpa_dbg(wpa_s, MSG_DEBUG,
    915 				"   skip - no WPA/RSN proto match");
    916 		return 0;
    917 	}
    918 
    919 	if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) &&
    920 	    wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE)) {
    921 		if (debug_print)
    922 			wpa_dbg(wpa_s, MSG_DEBUG, "   allow in OSEN");
    923 		return 1;
    924 	}
    925 
    926 	if (!wpa_key_mgmt_wpa(ssid->key_mgmt)) {
    927 		if (debug_print)
    928 			wpa_dbg(wpa_s, MSG_DEBUG, "   allow in non-WPA/WPA2");
    929 		return 1;
    930 	}
    931 
    932 	if (debug_print)
    933 		wpa_dbg(wpa_s, MSG_DEBUG,
    934 			"   reject due to mismatch with WPA/WPA2");
    935 
    936 	return 0;
    937 }
    938 
    939 
    940 static int freq_allowed(int *freqs, int freq)
    941 {
    942 	int i;
    943 
    944 	if (freqs == NULL)
    945 		return 1;
    946 
    947 	for (i = 0; freqs[i]; i++)
    948 		if (freqs[i] == freq)
    949 			return 1;
    950 	return 0;
    951 }
    952 
    953 
    954 static int rate_match(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
    955 		      struct wpa_bss *bss, int debug_print)
    956 {
    957 	const struct hostapd_hw_modes *mode = NULL, *modes;
    958 	const u8 scan_ie[2] = { WLAN_EID_SUPP_RATES, WLAN_EID_EXT_SUPP_RATES };
    959 	const u8 *rate_ie;
    960 	int i, j, k;
    961 
    962 	if (bss->freq == 0)
    963 		return 1; /* Cannot do matching without knowing band */
    964 
    965 	modes = wpa_s->hw.modes;
    966 	if (modes == NULL) {
    967 		/*
    968 		 * The driver does not provide any additional information
    969 		 * about the utilized hardware, so allow the connection attempt
    970 		 * to continue.
    971 		 */
    972 		return 1;
    973 	}
    974 
    975 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
    976 		for (j = 0; j < modes[i].num_channels; j++) {
    977 			int freq = modes[i].channels[j].freq;
    978 			if (freq == bss->freq) {
    979 				if (mode &&
    980 				    mode->mode == HOSTAPD_MODE_IEEE80211G)
    981 					break; /* do not allow 802.11b replace
    982 						* 802.11g */
    983 				mode = &modes[i];
    984 				break;
    985 			}
    986 		}
    987 	}
    988 
    989 	if (mode == NULL)
    990 		return 0;
    991 
    992 	for (i = 0; i < (int) sizeof(scan_ie); i++) {
    993 		rate_ie = wpa_bss_get_ie(bss, scan_ie[i]);
    994 		if (rate_ie == NULL)
    995 			continue;
    996 
    997 		for (j = 2; j < rate_ie[1] + 2; j++) {
    998 			int flagged = !!(rate_ie[j] & 0x80);
    999 			int r = (rate_ie[j] & 0x7f) * 5;
   1000 
   1001 			/*
   1002 			 * IEEE Std 802.11n-2009 7.3.2.2:
   1003 			 * The new BSS Membership selector value is encoded
   1004 			 * like a legacy basic rate, but it is not a rate and
   1005 			 * only indicates if the BSS members are required to
   1006 			 * support the mandatory features of Clause 20 [HT PHY]
   1007 			 * in order to join the BSS.
   1008 			 */
   1009 			if (flagged && ((rate_ie[j] & 0x7f) ==
   1010 					BSS_MEMBERSHIP_SELECTOR_HT_PHY)) {
   1011 				if (!ht_supported(mode)) {
   1012 					if (debug_print)
   1013 						wpa_dbg(wpa_s, MSG_DEBUG,
   1014 							"   hardware does not support HT PHY");
   1015 					return 0;
   1016 				}
   1017 				continue;
   1018 			}
   1019 
   1020 			/* There's also a VHT selector for 802.11ac */
   1021 			if (flagged && ((rate_ie[j] & 0x7f) ==
   1022 					BSS_MEMBERSHIP_SELECTOR_VHT_PHY)) {
   1023 				if (!vht_supported(mode)) {
   1024 					if (debug_print)
   1025 						wpa_dbg(wpa_s, MSG_DEBUG,
   1026 							"   hardware does not support VHT PHY");
   1027 					return 0;
   1028 				}
   1029 				continue;
   1030 			}
   1031 
   1032 			if (flagged && ((rate_ie[j] & 0x7f) ==
   1033 					BSS_MEMBERSHIP_SELECTOR_HE_PHY)) {
   1034 				if (!he_supported(mode, IEEE80211_MODE_INFRA)) {
   1035 					if (debug_print)
   1036 						wpa_dbg(wpa_s, MSG_DEBUG,
   1037 							"   hardware does not support HE PHY");
   1038 					return 0;
   1039 				}
   1040 				continue;
   1041 			}
   1042 
   1043 #ifdef CONFIG_SAE
   1044 			if (flagged && ((rate_ie[j] & 0x7f) ==
   1045 					BSS_MEMBERSHIP_SELECTOR_SAE_H2E_ONLY)) {
   1046 				if (wpa_s->conf->sae_pwe ==
   1047 				    SAE_PWE_HUNT_AND_PECK &&
   1048 				    !ssid->sae_password_id &&
   1049 				    !is_6ghz_freq(bss->freq) &&
   1050 				    wpa_key_mgmt_sae(ssid->key_mgmt)) {
   1051 					if (debug_print)
   1052 						wpa_dbg(wpa_s, MSG_DEBUG,
   1053 							"   SAE H2E disabled");
   1054 #ifdef CONFIG_TESTING_OPTIONS
   1055 					if (wpa_s->ignore_sae_h2e_only) {
   1056 						wpa_dbg(wpa_s, MSG_DEBUG,
   1057 							"TESTING: Ignore SAE H2E requirement mismatch");
   1058 						continue;
   1059 					}
   1060 #endif /* CONFIG_TESTING_OPTIONS */
   1061 					return 0;
   1062 				}
   1063 				continue;
   1064 			}
   1065 #endif /* CONFIG_SAE */
   1066 
   1067 			if (!flagged)
   1068 				continue;
   1069 
   1070 			/* check for legacy basic rates */
   1071 			for (k = 0; k < mode->num_rates; k++) {
   1072 				if (mode->rates[k] == r)
   1073 					break;
   1074 			}
   1075 			if (k == mode->num_rates) {
   1076 				/*
   1077 				 * IEEE Std 802.11-2007 7.3.2.2 demands that in
   1078 				 * order to join a BSS all required rates
   1079 				 * have to be supported by the hardware.
   1080 				 */
   1081 				if (debug_print)
   1082 					wpa_dbg(wpa_s, MSG_DEBUG,
   1083 						"   hardware does not support required rate %d.%d Mbps (freq=%d mode==%d num_rates=%d)",
   1084 						r / 10, r % 10,
   1085 						bss->freq, mode->mode, mode->num_rates);
   1086 				return 0;
   1087 			}
   1088 		}
   1089 	}
   1090 
   1091 	return 1;
   1092 }
   1093 
   1094 
   1095 /*
   1096  * Test whether BSS is in an ESS.
   1097  * This is done differently in DMG (60 GHz) and non-DMG bands
   1098  */
   1099 static int bss_is_ess(struct wpa_bss *bss)
   1100 {
   1101 	if (bss_is_dmg(bss)) {
   1102 		return (bss->caps & IEEE80211_CAP_DMG_MASK) ==
   1103 			IEEE80211_CAP_DMG_AP;
   1104 	}
   1105 
   1106 	return ((bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
   1107 		IEEE80211_CAP_ESS);
   1108 }
   1109 
   1110 
   1111 static int match_mac_mask(const u8 *addr_a, const u8 *addr_b, const u8 *mask)
   1112 {
   1113 	size_t i;
   1114 
   1115 	for (i = 0; i < ETH_ALEN; i++) {
   1116 		if ((addr_a[i] & mask[i]) != (addr_b[i] & mask[i]))
   1117 			return 0;
   1118 	}
   1119 	return 1;
   1120 }
   1121 
   1122 
   1123 static int addr_in_list(const u8 *addr, const u8 *list, size_t num)
   1124 {
   1125 	size_t i;
   1126 
   1127 	for (i = 0; i < num; i++) {
   1128 		const u8 *a = list + i * ETH_ALEN * 2;
   1129 		const u8 *m = a + ETH_ALEN;
   1130 
   1131 		if (match_mac_mask(a, addr, m))
   1132 			return 1;
   1133 	}
   1134 	return 0;
   1135 }
   1136 
   1137 
   1138 static void owe_trans_ssid(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
   1139 			   const u8 **ret_ssid, size_t *ret_ssid_len)
   1140 {
   1141 #ifdef CONFIG_OWE
   1142 	const u8 *owe, *pos, *end, *bssid;
   1143 	u8 ssid_len;
   1144 
   1145 	owe = wpa_bss_get_vendor_ie(bss, OWE_IE_VENDOR_TYPE);
   1146 	if (!owe || !wpa_bss_get_ie(bss, WLAN_EID_RSN))
   1147 		return;
   1148 
   1149 	pos = owe + 6;
   1150 	end = owe + 2 + owe[1];
   1151 
   1152 	if (end - pos < ETH_ALEN + 1)
   1153 		return;
   1154 	bssid = pos;
   1155 	pos += ETH_ALEN;
   1156 	ssid_len = *pos++;
   1157 	if (end - pos < ssid_len || ssid_len > SSID_MAX_LEN)
   1158 		return;
   1159 
   1160 	/* Match the profile SSID against the OWE transition mode SSID on the
   1161 	 * open network. */
   1162 	wpa_dbg(wpa_s, MSG_DEBUG, "OWE: transition mode BSSID: " MACSTR
   1163 		" SSID: %s", MAC2STR(bssid), wpa_ssid_txt(pos, ssid_len));
   1164 	*ret_ssid = pos;
   1165 	*ret_ssid_len = ssid_len;
   1166 
   1167 	if (!(bss->flags & WPA_BSS_OWE_TRANSITION)) {
   1168 		struct wpa_ssid *ssid;
   1169 
   1170 		for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
   1171 			if (wpas_network_disabled(wpa_s, ssid))
   1172 				continue;
   1173 			if (ssid->ssid_len == ssid_len &&
   1174 			    os_memcmp(ssid->ssid, pos, ssid_len) == 0) {
   1175 				/* OWE BSS in transition mode for a currently
   1176 				 * enabled OWE network. */
   1177 				wpa_dbg(wpa_s, MSG_DEBUG,
   1178 					"OWE: transition mode OWE SSID for active OWE profile");
   1179 				bss->flags |= WPA_BSS_OWE_TRANSITION;
   1180 				break;
   1181 			}
   1182 		}
   1183 	}
   1184 #endif /* CONFIG_OWE */
   1185 }
   1186 
   1187 
   1188 static bool wpas_valid_ml_bss(struct wpa_supplicant *wpa_s, struct wpa_bss *bss)
   1189 {
   1190 	u16 removed_links;
   1191 
   1192 	if (wpa_bss_parse_basic_ml_element(wpa_s, bss, NULL, NULL, NULL, NULL))
   1193 		return true;
   1194 
   1195 	if (!bss->valid_links)
   1196 		return true;
   1197 
   1198 	/* Check if the current BSS is going to be removed */
   1199 	removed_links = wpa_bss_parse_reconf_ml_element(wpa_s, bss);
   1200 	if (BIT(bss->mld_link_id) & removed_links)
   1201 		return false;
   1202 
   1203 	return true;
   1204 }
   1205 
   1206 
   1207 int disabled_freq(struct wpa_supplicant *wpa_s, int freq)
   1208 {
   1209 	int i, j;
   1210 
   1211 	if (!wpa_s->hw.modes || !wpa_s->hw.num_modes)
   1212 		return 0;
   1213 
   1214 	for (j = 0; j < wpa_s->hw.num_modes; j++) {
   1215 		struct hostapd_hw_modes *mode = &wpa_s->hw.modes[j];
   1216 
   1217 		for (i = 0; i < mode->num_channels; i++) {
   1218 			struct hostapd_channel_data *chan = &mode->channels[i];
   1219 
   1220 			if (chan->freq == freq)
   1221 				return !!(chan->flag & HOSTAPD_CHAN_DISABLED);
   1222 		}
   1223 	}
   1224 
   1225 	return 1;
   1226 }
   1227 
   1228 
   1229 static bool wpa_scan_res_ok(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
   1230 			    const u8 *match_ssid, size_t match_ssid_len,
   1231 			    struct wpa_bss *bss, int bssid_ignore_count,
   1232 			    bool debug_print);
   1233 
   1234 
   1235 #ifdef CONFIG_SAE_PK
   1236 static bool sae_pk_acceptable_bss_with_pk(struct wpa_supplicant *wpa_s,
   1237 					  struct wpa_bss *orig_bss,
   1238 					  struct wpa_ssid *ssid,
   1239 					  const u8 *match_ssid,
   1240 					  size_t match_ssid_len)
   1241 {
   1242 	struct wpa_bss *bss;
   1243 
   1244 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
   1245 		int count;
   1246 		const u8 *ie;
   1247 
   1248 		if (bss == orig_bss)
   1249 			continue;
   1250 		ie = wpa_bss_get_ie(bss, WLAN_EID_RSNX);
   1251 		if (!(ieee802_11_rsnx_capab(ie, WLAN_RSNX_CAPAB_SAE_PK)))
   1252 			continue;
   1253 
   1254 		/* TODO: Could be more thorough in checking what kind of
   1255 		 * signal strength or throughput estimate would be acceptable
   1256 		 * compared to the originally selected BSS. */
   1257 		if (bss->est_throughput < 2000)
   1258 			return false;
   1259 
   1260 		count = wpa_bssid_ignore_is_listed(wpa_s, bss->bssid);
   1261 		if (wpa_scan_res_ok(wpa_s, ssid, match_ssid, match_ssid_len,
   1262 				    bss, count, 0))
   1263 			return true;
   1264 	}
   1265 
   1266 	return false;
   1267 }
   1268 #endif /* CONFIG_SAE_PK */
   1269 
   1270 
   1271 static bool wpa_scan_res_ok(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
   1272 			    const u8 *match_ssid, size_t match_ssid_len,
   1273 			    struct wpa_bss *bss, int bssid_ignore_count,
   1274 			    bool debug_print)
   1275 {
   1276 	int res;
   1277 	bool wpa, check_ssid, osen, rsn_osen = false;
   1278 	struct wpa_ie_data data;
   1279 #ifdef CONFIG_MBO
   1280 	const u8 *assoc_disallow;
   1281 #endif /* CONFIG_MBO */
   1282 #ifdef CONFIG_SAE
   1283 	u8 rsnxe_capa = 0;
   1284 #endif /* CONFIG_SAE */
   1285 	const u8 *ie;
   1286 
   1287 	ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
   1288 	wpa = ie && ie[1];
   1289 	ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
   1290 	wpa |= ie && ie[1];
   1291 	if (ie && wpa_parse_wpa_ie_rsn(ie, 2 + ie[1], &data) == 0 &&
   1292 	    (data.key_mgmt & WPA_KEY_MGMT_OSEN))
   1293 		rsn_osen = true;
   1294 	ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
   1295 	osen = ie != NULL;
   1296 
   1297 #ifdef CONFIG_SAE
   1298 	ie = wpa_bss_get_ie(bss, WLAN_EID_RSNX);
   1299 	if (ie && ie[1] >= 1)
   1300 		rsnxe_capa = ie[2];
   1301 #endif /* CONFIG_SAE */
   1302 
   1303 	check_ssid = wpa || ssid->ssid_len > 0;
   1304 
   1305 	if (wpas_network_disabled(wpa_s, ssid)) {
   1306 		if (debug_print)
   1307 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - disabled");
   1308 		return false;
   1309 	}
   1310 
   1311 	res = wpas_temp_disabled(wpa_s, ssid);
   1312 	if (res > 0) {
   1313 		if (debug_print)
   1314 			wpa_dbg(wpa_s, MSG_DEBUG,
   1315 				"   skip - disabled temporarily for %d second(s)",
   1316 				res);
   1317 		return false;
   1318 	}
   1319 
   1320 #ifdef CONFIG_WPS
   1321 	if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && bssid_ignore_count) {
   1322 		if (debug_print)
   1323 			wpa_dbg(wpa_s, MSG_DEBUG,
   1324 				"   skip - BSSID ignored (WPS)");
   1325 		return false;
   1326 	}
   1327 
   1328 	if (wpa && ssid->ssid_len == 0 &&
   1329 	    wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss))
   1330 		check_ssid = false;
   1331 
   1332 	if (!wpa && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
   1333 		/* Only allow wildcard SSID match if an AP advertises active
   1334 		 * WPS operation that matches our mode. */
   1335 		check_ssid = ssid->ssid_len > 0 ||
   1336 			!wpas_wps_ssid_wildcard_ok(wpa_s, ssid, bss);
   1337 	}
   1338 #endif /* CONFIG_WPS */
   1339 
   1340 	if (ssid->bssid_set && ssid->ssid_len == 0 &&
   1341 	    ether_addr_equal(bss->bssid, ssid->bssid))
   1342 		check_ssid = false;
   1343 
   1344 	if (check_ssid &&
   1345 	    (match_ssid_len != ssid->ssid_len ||
   1346 	     os_memcmp(match_ssid, ssid->ssid, match_ssid_len) != 0)) {
   1347 		if (debug_print)
   1348 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID mismatch");
   1349 		return false;
   1350 	}
   1351 
   1352 	if (ssid->bssid_set &&
   1353 	    !ether_addr_equal(bss->bssid, ssid->bssid)) {
   1354 		if (debug_print)
   1355 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - BSSID mismatch");
   1356 		return false;
   1357 	}
   1358 
   1359 	/* check the list of BSSIDs to ignore */
   1360 	if (ssid->num_bssid_ignore &&
   1361 	    addr_in_list(bss->bssid, ssid->bssid_ignore,
   1362 			 ssid->num_bssid_ignore)) {
   1363 		if (debug_print)
   1364 			wpa_dbg(wpa_s, MSG_DEBUG,
   1365 				"   skip - BSSID configured to be ignored");
   1366 		return false;
   1367 	}
   1368 
   1369 	/* if there is a list of accepted BSSIDs, only accept those APs */
   1370 	if (ssid->num_bssid_accept &&
   1371 	    !addr_in_list(bss->bssid, ssid->bssid_accept,
   1372 			  ssid->num_bssid_accept)) {
   1373 		if (debug_print)
   1374 			wpa_dbg(wpa_s, MSG_DEBUG,
   1375 				"   skip - BSSID not in list of accepted values");
   1376 		return false;
   1377 	}
   1378 
   1379 	if (!wpa_supplicant_ssid_bss_match(wpa_s, ssid, bss, debug_print))
   1380 		return false;
   1381 
   1382 	if (!osen && !wpa &&
   1383 	    !(ssid->key_mgmt & WPA_KEY_MGMT_NONE) &&
   1384 	    !(ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
   1385 	    !(ssid->key_mgmt & WPA_KEY_MGMT_OWE) &&
   1386 	    !(ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) {
   1387 		if (debug_print)
   1388 			wpa_dbg(wpa_s, MSG_DEBUG,
   1389 				"   skip - non-WPA network not allowed");
   1390 		return false;
   1391 	}
   1392 
   1393 #ifdef CONFIG_WEP
   1394 	if (wpa && !wpa_key_mgmt_wpa(ssid->key_mgmt) && has_wep_key(ssid)) {
   1395 		if (debug_print)
   1396 			wpa_dbg(wpa_s, MSG_DEBUG,
   1397 				"   skip - ignore WPA/WPA2 AP for WEP network block");
   1398 		return false;
   1399 	}
   1400 #endif /* CONFIG_WEP */
   1401 
   1402 	if ((ssid->key_mgmt & WPA_KEY_MGMT_OSEN) && !osen && !rsn_osen) {
   1403 		if (debug_print)
   1404 			wpa_dbg(wpa_s, MSG_DEBUG,
   1405 				"   skip - non-OSEN network not allowed");
   1406 		return false;
   1407 	}
   1408 
   1409 	if (!wpa_supplicant_match_privacy(bss, ssid)) {
   1410 		if (debug_print)
   1411 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - privacy mismatch");
   1412 		return false;
   1413 	}
   1414 
   1415 	if (ssid->mode != WPAS_MODE_MESH && !bss_is_ess(bss) &&
   1416 	    !bss_is_pbss(bss)) {
   1417 		if (debug_print)
   1418 			wpa_dbg(wpa_s, MSG_DEBUG,
   1419 				"   skip - not ESS, PBSS, or MBSS");
   1420 		return false;
   1421 	}
   1422 
   1423 	if (ssid->pbss != 2 && ssid->pbss != bss_is_pbss(bss)) {
   1424 		if (debug_print)
   1425 			wpa_dbg(wpa_s, MSG_DEBUG,
   1426 				"   skip - PBSS mismatch (ssid %d bss %d)",
   1427 				ssid->pbss, bss_is_pbss(bss));
   1428 		return false;
   1429 	}
   1430 
   1431 	if (!freq_allowed(ssid->freq_list, bss->freq)) {
   1432 		if (debug_print)
   1433 			wpa_dbg(wpa_s, MSG_DEBUG,
   1434 				"   skip - frequency not allowed");
   1435 		return false;
   1436 	}
   1437 
   1438 #ifdef CONFIG_MESH
   1439 	if (ssid->mode == WPAS_MODE_MESH && ssid->frequency > 0 &&
   1440 	    ssid->frequency != bss->freq) {
   1441 		if (debug_print)
   1442 			wpa_dbg(wpa_s, MSG_DEBUG,
   1443 				"   skip - frequency not allowed (mesh)");
   1444 		return false;
   1445 	}
   1446 #endif /* CONFIG_MESH */
   1447 
   1448 	if (!rate_match(wpa_s, ssid, bss, debug_print)) {
   1449 		if (debug_print)
   1450 			wpa_dbg(wpa_s, MSG_DEBUG,
   1451 				"   skip - rate sets do not match");
   1452 		return false;
   1453 	}
   1454 
   1455 #ifdef CONFIG_SAE
   1456 	/* When using SAE Password Identifier and when operationg on the 6 GHz
   1457 	 * band, only H2E is allowed. */
   1458 	if ((wpa_s->conf->sae_pwe == SAE_PWE_HASH_TO_ELEMENT ||
   1459 	     is_6ghz_freq(bss->freq) || ssid->sae_password_id) &&
   1460 	    wpa_s->conf->sae_pwe != SAE_PWE_FORCE_HUNT_AND_PECK &&
   1461 	    wpa_key_mgmt_sae(ssid->key_mgmt) &&
   1462 	    !(rsnxe_capa & BIT(WLAN_RSNX_CAPAB_SAE_H2E))) {
   1463 		if (debug_print)
   1464 			wpa_dbg(wpa_s, MSG_DEBUG,
   1465 				"   skip - SAE H2E required, but not supported by the AP");
   1466 		return false;
   1467 	}
   1468 #endif /* CONFIG_SAE */
   1469 
   1470 #ifdef CONFIG_SAE_PK
   1471 	if (ssid->sae_pk == SAE_PK_MODE_ONLY &&
   1472 	    !(rsnxe_capa & BIT(WLAN_RSNX_CAPAB_SAE_PK))) {
   1473 		if (debug_print)
   1474 			wpa_dbg(wpa_s, MSG_DEBUG,
   1475 				"   skip - SAE-PK required, but not supported by the AP");
   1476 		return false;
   1477 	}
   1478 #endif /* CONFIG_SAE_PK */
   1479 
   1480 #ifndef CONFIG_IBSS_RSN
   1481 	if (ssid->mode == WPAS_MODE_IBSS &&
   1482 	    !(ssid->key_mgmt & (WPA_KEY_MGMT_NONE | WPA_KEY_MGMT_WPA_NONE))) {
   1483 		if (debug_print)
   1484 			wpa_dbg(wpa_s, MSG_DEBUG,
   1485 				"   skip - IBSS RSN not supported in the build");
   1486 		return false;
   1487 	}
   1488 #endif /* !CONFIG_IBSS_RSN */
   1489 
   1490 #ifdef CONFIG_P2P
   1491 	if (ssid->p2p_group &&
   1492 	    !wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
   1493 	    !wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
   1494 		if (debug_print)
   1495 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - no P2P IE seen");
   1496 		return false;
   1497 	}
   1498 
   1499 	if (!is_zero_ether_addr(ssid->go_p2p_dev_addr)) {
   1500 		struct wpabuf *p2p_ie;
   1501 		u8 dev_addr[ETH_ALEN];
   1502 
   1503 		ie = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
   1504 		if (!ie) {
   1505 			if (debug_print)
   1506 				wpa_dbg(wpa_s, MSG_DEBUG,
   1507 					"   skip - no P2P element");
   1508 			return false;
   1509 		}
   1510 		p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
   1511 		if (!p2p_ie) {
   1512 			if (debug_print)
   1513 				wpa_dbg(wpa_s, MSG_DEBUG,
   1514 					"   skip - could not fetch P2P element");
   1515 			return false;
   1516 		}
   1517 
   1518 		if (p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr) < 0 ||
   1519 		    !ether_addr_equal(dev_addr, ssid->go_p2p_dev_addr)) {
   1520 			if (debug_print)
   1521 				wpa_dbg(wpa_s, MSG_DEBUG,
   1522 					"   skip - no matching GO P2P Device Address in P2P element");
   1523 			wpabuf_free(p2p_ie);
   1524 			return false;
   1525 		}
   1526 		wpabuf_free(p2p_ie);
   1527 	}
   1528 
   1529 	/*
   1530 	 * TODO: skip the AP if its P2P IE has Group Formation bit set in the
   1531 	 * P2P Group Capability Bitmap and we are not in Group Formation with
   1532 	 * that device.
   1533 	 */
   1534 #endif /* CONFIG_P2P */
   1535 
   1536 	if (os_reltime_before(&bss->last_update, &wpa_s->scan_min_time)) {
   1537 		struct os_reltime diff;
   1538 
   1539 		os_reltime_sub(&wpa_s->scan_min_time, &bss->last_update, &diff);
   1540 		if (debug_print)
   1541 			wpa_dbg(wpa_s, MSG_DEBUG,
   1542 				"   skip - scan result not recent enough (%u.%06u seconds too old)",
   1543 				(unsigned int) diff.sec,
   1544 				(unsigned int) diff.usec);
   1545 		return false;
   1546 	}
   1547 #ifdef CONFIG_MBO
   1548 #ifdef CONFIG_TESTING_OPTIONS
   1549 	if (wpa_s->ignore_assoc_disallow)
   1550 		goto skip_assoc_disallow;
   1551 #endif /* CONFIG_TESTING_OPTIONS */
   1552 	assoc_disallow = wpas_mbo_check_assoc_disallow(bss);
   1553 	if (assoc_disallow && assoc_disallow[1] >= 1) {
   1554 		if (debug_print)
   1555 			wpa_dbg(wpa_s, MSG_DEBUG,
   1556 				"   skip - MBO association disallowed (reason %u)",
   1557 				assoc_disallow[2]);
   1558 		return false;
   1559 	}
   1560 
   1561 	if (wpa_is_bss_tmp_disallowed(wpa_s, bss)) {
   1562 		if (debug_print)
   1563 			wpa_dbg(wpa_s, MSG_DEBUG,
   1564 				"   skip - AP temporarily disallowed");
   1565 		return false;
   1566 	}
   1567 #ifdef CONFIG_TESTING_OPTIONS
   1568 skip_assoc_disallow:
   1569 #endif /* CONFIG_TESTING_OPTIONS */
   1570 #endif /* CONFIG_MBO */
   1571 
   1572 #ifdef CONFIG_DPP
   1573 	if ((ssid->key_mgmt & WPA_KEY_MGMT_DPP) &&
   1574 	    !wpa_sm_pmksa_exists(wpa_s->wpa, bss->bssid, wpa_s->own_addr,
   1575 				 ssid) &&
   1576 	    (!ssid->dpp_connector || !ssid->dpp_netaccesskey ||
   1577 	     !ssid->dpp_csign)) {
   1578 		if (debug_print)
   1579 			wpa_dbg(wpa_s, MSG_DEBUG,
   1580 				"   skip - no PMKSA entry for DPP");
   1581 		return false;
   1582 	}
   1583 #endif /* CONFIG_DPP */
   1584 
   1585 #ifdef CONFIG_SAE_PK
   1586 	if (ssid->sae_pk == SAE_PK_MODE_AUTOMATIC &&
   1587 	    wpa_key_mgmt_sae(ssid->key_mgmt) &&
   1588 	    ((ssid->sae_password &&
   1589 	      sae_pk_valid_password(ssid->sae_password)) ||
   1590 	     (!ssid->sae_password && ssid->passphrase &&
   1591 	      sae_pk_valid_password(ssid->passphrase))) &&
   1592 	    !(rsnxe_capa & BIT(WLAN_RSNX_CAPAB_SAE_PK)) &&
   1593 	    sae_pk_acceptable_bss_with_pk(wpa_s, bss, ssid, match_ssid,
   1594 					  match_ssid_len)) {
   1595 		if (debug_print)
   1596 			wpa_dbg(wpa_s, MSG_DEBUG,
   1597 				"   skip - another acceptable BSS with SAE-PK in the same ESS");
   1598 		return false;
   1599 	}
   1600 #endif /* CONFIG_SAE_PK */
   1601 
   1602 	if (bss->ssid_len == 0) {
   1603 #ifdef CONFIG_OWE
   1604 		const u8 *owe_ssid = NULL;
   1605 		size_t owe_ssid_len = 0;
   1606 
   1607 		owe_trans_ssid(wpa_s, bss, &owe_ssid, &owe_ssid_len);
   1608 		if (owe_ssid && owe_ssid_len &&
   1609 		    owe_ssid_len == ssid->ssid_len &&
   1610 		    os_memcmp(owe_ssid, ssid->ssid, owe_ssid_len) == 0) {
   1611 			if (debug_print)
   1612 				wpa_dbg(wpa_s, MSG_DEBUG,
   1613 					"   skip - no SSID in BSS entry for a possible OWE transition mode BSS");
   1614 			int_array_add_unique(&wpa_s->owe_trans_scan_freq,
   1615 					     bss->freq);
   1616 			return false;
   1617 		}
   1618 #endif /* CONFIG_OWE */
   1619 		if (debug_print)
   1620 			wpa_dbg(wpa_s, MSG_DEBUG,
   1621 				"   skip - no SSID known for the BSS");
   1622 		return false;
   1623 	}
   1624 
   1625 	if (!wpas_valid_ml_bss(wpa_s, bss)) {
   1626 		if (debug_print)
   1627 			wpa_dbg(wpa_s, MSG_DEBUG,
   1628 				"   skip - ML BSS going to be removed");
   1629 		return false;
   1630 	}
   1631 
   1632 	/* Matching configuration found */
   1633 	return true;
   1634 }
   1635 
   1636 
   1637 struct wpa_ssid * wpa_scan_res_match(struct wpa_supplicant *wpa_s,
   1638 				     int i, struct wpa_bss *bss,
   1639 				     struct wpa_ssid *group,
   1640 				     int only_first_ssid, int debug_print)
   1641 {
   1642 	u8 wpa_ie_len, rsn_ie_len;
   1643 	const u8 *ie;
   1644 	struct wpa_ssid *ssid;
   1645 	int osen;
   1646 	const u8 *match_ssid;
   1647 	size_t match_ssid_len;
   1648 	int bssid_ignore_count;
   1649 
   1650 	ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
   1651 	wpa_ie_len = ie ? ie[1] : 0;
   1652 
   1653 	ie = wpa_bss_get_ie(bss, WLAN_EID_RSN);
   1654 	rsn_ie_len = ie ? ie[1] : 0;
   1655 
   1656 	ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
   1657 	osen = ie != NULL;
   1658 
   1659 	if (debug_print) {
   1660 		wpa_dbg(wpa_s, MSG_DEBUG, "%d: " MACSTR
   1661 			" ssid='%s' wpa_ie_len=%u rsn_ie_len=%u caps=0x%x level=%d freq=%d %s%s%s",
   1662 			i, MAC2STR(bss->bssid),
   1663 			wpa_ssid_txt(bss->ssid, bss->ssid_len),
   1664 			wpa_ie_len, rsn_ie_len, bss->caps, bss->level,
   1665 			bss->freq,
   1666 			wpa_bss_get_vendor_ie(bss, WPS_IE_VENDOR_TYPE) ?
   1667 			" wps" : "",
   1668 			(wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
   1669 			 wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE))
   1670 			? " p2p" : "",
   1671 			osen ? " osen=1" : "");
   1672 	}
   1673 
   1674 	bssid_ignore_count = wpa_bssid_ignore_is_listed(wpa_s, bss->bssid);
   1675 	if (bssid_ignore_count) {
   1676 		int limit = 1;
   1677 		if (wpa_supplicant_enabled_networks(wpa_s) == 1) {
   1678 			/*
   1679 			 * When only a single network is enabled, we can
   1680 			 * trigger BSSID ignoring on the first failure. This
   1681 			 * should not be done with multiple enabled networks to
   1682 			 * avoid getting forced to move into a worse ESS on
   1683 			 * single error if there are no other BSSes of the
   1684 			 * current ESS.
   1685 			 */
   1686 			limit = 0;
   1687 		}
   1688 		if (bssid_ignore_count > limit) {
   1689 			if (debug_print) {
   1690 				wpa_dbg(wpa_s, MSG_DEBUG,
   1691 					"   skip - BSSID ignored (count=%d limit=%d)",
   1692 					bssid_ignore_count, limit);
   1693 			}
   1694 			return NULL;
   1695 		}
   1696 	}
   1697 
   1698 	match_ssid = bss->ssid;
   1699 	match_ssid_len = bss->ssid_len;
   1700 	owe_trans_ssid(wpa_s, bss, &match_ssid, &match_ssid_len);
   1701 
   1702 	if (match_ssid_len == 0) {
   1703 		if (debug_print)
   1704 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID not known");
   1705 		return NULL;
   1706 	}
   1707 
   1708 	if (disallowed_bssid(wpa_s, bss->bssid)) {
   1709 		if (debug_print)
   1710 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - BSSID disallowed");
   1711 		return NULL;
   1712 	}
   1713 
   1714 	if (disallowed_ssid(wpa_s, match_ssid, match_ssid_len)) {
   1715 		if (debug_print)
   1716 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - SSID disallowed");
   1717 		return NULL;
   1718 	}
   1719 
   1720 	if (disabled_freq(wpa_s, bss->freq)) {
   1721 		if (debug_print)
   1722 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - channel disabled");
   1723 		return NULL;
   1724 	}
   1725 
   1726 	if (wnm_is_bss_excluded(wpa_s, bss)) {
   1727 		if (debug_print)
   1728 			wpa_dbg(wpa_s, MSG_DEBUG, "   skip - BSSID excluded");
   1729 		return NULL;
   1730 	}
   1731 
   1732 	for (ssid = group; ssid; ssid = only_first_ssid ? NULL : ssid->pnext) {
   1733 		if (wpa_scan_res_ok(wpa_s, ssid, match_ssid, match_ssid_len,
   1734 				    bss, bssid_ignore_count, debug_print))
   1735 			return ssid;
   1736 	}
   1737 
   1738 	/* No matching configuration found */
   1739 	return NULL;
   1740 }
   1741 
   1742 
   1743 static struct wpa_bss *
   1744 wpa_supplicant_select_bss(struct wpa_supplicant *wpa_s,
   1745 			  struct wpa_ssid *group,
   1746 			  struct wpa_ssid **selected_ssid,
   1747 			  int only_first_ssid)
   1748 {
   1749 	unsigned int i;
   1750 
   1751 	if (wpa_s->current_ssid) {
   1752 		struct wpa_ssid *ssid;
   1753 
   1754 		wpa_dbg(wpa_s, MSG_DEBUG,
   1755 			"Scan results matching the currently selected network");
   1756 		for (i = 0; i < wpa_s->last_scan_res_used; i++) {
   1757 			struct wpa_bss *bss = wpa_s->last_scan_res[i];
   1758 
   1759 			ssid = wpa_scan_res_match(wpa_s, i, bss, group,
   1760 						  only_first_ssid, 0);
   1761 			if (ssid != wpa_s->current_ssid)
   1762 				continue;
   1763 			wpa_dbg(wpa_s, MSG_DEBUG, "%u: " MACSTR
   1764 				" freq=%d level=%d snr=%d est_throughput=%u",
   1765 				i, MAC2STR(bss->bssid), bss->freq, bss->level,
   1766 				bss->snr, bss->est_throughput);
   1767 		}
   1768 	}
   1769 
   1770 	if (only_first_ssid)
   1771 		wpa_dbg(wpa_s, MSG_DEBUG, "Try to find BSS matching pre-selected network id=%d",
   1772 			group->id);
   1773 	else
   1774 		wpa_dbg(wpa_s, MSG_DEBUG, "Selecting BSS from priority group %d",
   1775 			group->priority);
   1776 
   1777 	for (i = 0; i < wpa_s->last_scan_res_used; i++) {
   1778 		struct wpa_bss *bss = wpa_s->last_scan_res[i];
   1779 
   1780 		wpa_s->owe_transition_select = 1;
   1781 		*selected_ssid = wpa_scan_res_match(wpa_s, i, bss, group,
   1782 						    only_first_ssid, 1);
   1783 		wpa_s->owe_transition_select = 0;
   1784 		if (!*selected_ssid)
   1785 			continue;
   1786 		wpa_dbg(wpa_s, MSG_DEBUG, "   selected %sBSS " MACSTR
   1787 			" ssid='%s'",
   1788 			bss == wpa_s->current_bss ? "current ": "",
   1789 			MAC2STR(bss->bssid),
   1790 			wpa_ssid_txt(bss->ssid, bss->ssid_len));
   1791 		return bss;
   1792 	}
   1793 
   1794 	return NULL;
   1795 }
   1796 
   1797 
   1798 struct wpa_bss * wpa_supplicant_pick_network(struct wpa_supplicant *wpa_s,
   1799 					     struct wpa_ssid **selected_ssid)
   1800 {
   1801 	struct wpa_bss *selected = NULL;
   1802 	size_t prio;
   1803 	struct wpa_ssid *next_ssid = NULL;
   1804 	struct wpa_ssid *ssid;
   1805 
   1806 	if (wpa_s->last_scan_res == NULL ||
   1807 	    wpa_s->last_scan_res_used == 0)
   1808 		return NULL; /* no scan results from last update */
   1809 
   1810 	if (wpa_s->next_ssid) {
   1811 		/* check that next_ssid is still valid */
   1812 		for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
   1813 			if (ssid == wpa_s->next_ssid)
   1814 				break;
   1815 		}
   1816 		next_ssid = ssid;
   1817 		wpa_s->next_ssid = NULL;
   1818 	}
   1819 
   1820 	while (selected == NULL) {
   1821 		for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
   1822 			if (next_ssid && next_ssid->priority ==
   1823 			    wpa_s->conf->pssid[prio]->priority) {
   1824 				selected = wpa_supplicant_select_bss(
   1825 					wpa_s, next_ssid, selected_ssid, 1);
   1826 				if (selected)
   1827 					break;
   1828 			}
   1829 			selected = wpa_supplicant_select_bss(
   1830 				wpa_s, wpa_s->conf->pssid[prio],
   1831 				selected_ssid, 0);
   1832 			if (selected)
   1833 				break;
   1834 		}
   1835 
   1836 		if (!selected &&
   1837 		    (wpa_s->bssid_ignore || wnm_active_bss_trans_mgmt(wpa_s)) &&
   1838 		    !wpa_s->countermeasures) {
   1839 			wpa_dbg(wpa_s, MSG_DEBUG,
   1840 				"No APs found - clear BSSID ignore list and try again");
   1841 			wnm_btm_reset(wpa_s);
   1842 			wpa_bssid_ignore_clear(wpa_s);
   1843 			wpa_s->bssid_ignore_cleared = true;
   1844 		} else if (selected == NULL)
   1845 			break;
   1846 	}
   1847 
   1848 	ssid = *selected_ssid;
   1849 	if (selected && ssid && ssid->mem_only_psk && !ssid->psk_set &&
   1850 	    !ssid->passphrase && !ssid->ext_psk) {
   1851 		const char *field_name, *txt = NULL;
   1852 
   1853 		wpa_dbg(wpa_s, MSG_DEBUG,
   1854 			"PSK/passphrase not yet available for the selected network");
   1855 
   1856 		wpas_notify_network_request(wpa_s, ssid,
   1857 					    WPA_CTRL_REQ_PSK_PASSPHRASE, NULL);
   1858 
   1859 		field_name = wpa_supplicant_ctrl_req_to_string(
   1860 			WPA_CTRL_REQ_PSK_PASSPHRASE, NULL, &txt);
   1861 		if (field_name == NULL)
   1862 			return NULL;
   1863 
   1864 		wpas_send_ctrl_req(wpa_s, ssid, field_name, txt);
   1865 
   1866 		selected = NULL;
   1867 	}
   1868 
   1869 	return selected;
   1870 }
   1871 
   1872 
   1873 static void wpa_supplicant_req_new_scan(struct wpa_supplicant *wpa_s,
   1874 					int timeout_sec, int timeout_usec)
   1875 {
   1876 	if (!wpa_supplicant_enabled_networks(wpa_s)) {
   1877 		/*
   1878 		 * No networks are enabled; short-circuit request so
   1879 		 * we don't wait timeout seconds before transitioning
   1880 		 * to INACTIVE state.
   1881 		 */
   1882 		wpa_dbg(wpa_s, MSG_DEBUG, "Short-circuit new scan request "
   1883 			"since there are no enabled networks");
   1884 		wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
   1885 		return;
   1886 	}
   1887 
   1888 	wpa_s->scan_for_connection = 1;
   1889 	wpa_supplicant_req_scan(wpa_s, timeout_sec, timeout_usec);
   1890 }
   1891 
   1892 
   1893 static bool ml_link_probe_scan(struct wpa_supplicant *wpa_s)
   1894 {
   1895 	if (!wpa_s->ml_connect_probe_ssid || !wpa_s->ml_connect_probe_bss)
   1896 		return false;
   1897 
   1898 	wpa_msg(wpa_s, MSG_DEBUG,
   1899 		"Request association with " MACSTR " after ML probe",
   1900 		MAC2STR(wpa_s->ml_connect_probe_bss->bssid));
   1901 
   1902 	wpa_supplicant_associate(wpa_s, wpa_s->ml_connect_probe_bss,
   1903 				 wpa_s->ml_connect_probe_ssid);
   1904 
   1905 	wpa_s->ml_connect_probe_ssid = NULL;
   1906 	wpa_s->ml_connect_probe_bss = NULL;
   1907 
   1908 	return true;
   1909 }
   1910 
   1911 
   1912 static int wpa_supplicant_connect_ml_missing(struct wpa_supplicant *wpa_s,
   1913 					     struct wpa_bss *selected,
   1914 					     struct wpa_ssid *ssid)
   1915 {
   1916 	int *freqs;
   1917 	u16 missing_links = 0, removed_links;
   1918 	u8 ap_mld_id;
   1919 
   1920 	if (!((wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_MLO) &&
   1921 	      (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)))
   1922 		return 0;
   1923 
   1924 	if (wpa_bss_parse_basic_ml_element(wpa_s, selected, NULL,
   1925 					   &missing_links, ssid,
   1926 					   &ap_mld_id) ||
   1927 	    !missing_links)
   1928 		return 0;
   1929 
   1930 	removed_links = wpa_bss_parse_reconf_ml_element(wpa_s, selected);
   1931 	missing_links &= ~removed_links;
   1932 
   1933 	if (!missing_links)
   1934 		return 0;
   1935 
   1936 	wpa_dbg(wpa_s, MSG_DEBUG,
   1937 		"MLD: Doing an ML probe for missing links 0x%04x",
   1938 		missing_links);
   1939 
   1940 	freqs = os_malloc(sizeof(int) * 2);
   1941 	if (!freqs)
   1942 		return 0;
   1943 
   1944 	wpa_s->ml_connect_probe_ssid = ssid;
   1945 	wpa_s->ml_connect_probe_bss = selected;
   1946 
   1947 	freqs[0] = selected->freq;
   1948 	freqs[1] = 0;
   1949 
   1950 	wpa_s->manual_scan_passive = 0;
   1951 	wpa_s->manual_scan_use_id = 0;
   1952 	wpa_s->manual_scan_only_new = 0;
   1953 	wpa_s->scan_id_count = 0;
   1954 	os_free(wpa_s->manual_scan_freqs);
   1955 	wpa_s->manual_scan_freqs = freqs;
   1956 
   1957 	os_memcpy(wpa_s->ml_probe_bssid, selected->bssid, ETH_ALEN);
   1958 
   1959 	/*
   1960 	 * In case the ML probe request is intended to retrieve information from
   1961 	 * the transmitted BSS, the AP MLD ID should be included and should be
   1962 	 * set to zero.
   1963 	 * In case the ML probe requested is intended to retrieve information
   1964 	 * from a non-transmitted BSS, the AP MLD ID should not be included.
   1965 	 */
   1966 	if (ap_mld_id)
   1967 		wpa_s->ml_probe_mld_id = -1;
   1968 	else
   1969 		wpa_s->ml_probe_mld_id = 0;
   1970 
   1971 	if (ssid && ssid->ssid_len) {
   1972 		os_free(wpa_s->ssids_from_scan_req);
   1973 		wpa_s->num_ssids_from_scan_req = 0;
   1974 
   1975 		wpa_s->ssids_from_scan_req =
   1976 			os_zalloc(sizeof(struct wpa_ssid_value));
   1977 		if (wpa_s->ssids_from_scan_req) {
   1978 			wpa_printf(MSG_DEBUG,
   1979 				   "MLD: ML probe: With direct SSID");
   1980 
   1981 			wpa_s->num_ssids_from_scan_req = 1;
   1982 			wpa_s->ssids_from_scan_req[0].ssid_len = ssid->ssid_len;
   1983 			os_memcpy(wpa_s->ssids_from_scan_req[0].ssid,
   1984 				  ssid->ssid, ssid->ssid_len);
   1985 		}
   1986 	}
   1987 
   1988 	wpa_s->ml_probe_links = missing_links;
   1989 
   1990 	wpa_s->normal_scans = 0;
   1991 	wpa_s->scan_req = MANUAL_SCAN_REQ;
   1992 	wpa_s->after_wps = 0;
   1993 	wpa_s->known_wps_freq = 0;
   1994 	wpa_supplicant_req_scan(wpa_s, 0, 0);
   1995 
   1996 	return 1;
   1997 }
   1998 
   1999 
   2000 int wpa_supplicant_connect(struct wpa_supplicant *wpa_s,
   2001 			   struct wpa_bss *selected,
   2002 			   struct wpa_ssid *ssid)
   2003 {
   2004 #ifdef IEEE8021X_EAPOL
   2005 	if ((eap_is_wps_pbc_enrollee(&ssid->eap) &&
   2006 	     wpas_wps_partner_link_overlap_detect(wpa_s)) ||
   2007 	    wpas_wps_scan_pbc_overlap(wpa_s, selected, ssid)) {
   2008 		wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_OVERLAP
   2009 			"PBC session overlap");
   2010 		wpas_notify_wps_event_pbc_overlap(wpa_s);
   2011 		wpa_s->wps_overlap = true;
   2012 #ifdef CONFIG_P2P
   2013 		if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
   2014 		    wpa_s->p2p_in_provisioning) {
   2015 			eloop_register_timeout(0, 0, wpas_p2p_pbc_overlap_cb,
   2016 					       wpa_s, NULL);
   2017 			return -1;
   2018 		}
   2019 #endif /* CONFIG_P2P */
   2020 
   2021 #ifdef CONFIG_WPS
   2022 		wpas_wps_pbc_overlap(wpa_s);
   2023 		wpas_wps_cancel(wpa_s);
   2024 #endif /* CONFIG_WPS */
   2025 		return -1;
   2026 	}
   2027 #endif /* IEEE8021X_EAPOL */
   2028 
   2029 	wpa_msg(wpa_s, MSG_DEBUG,
   2030 		"Considering connect request: reassociate: %d  selected: "
   2031 		MACSTR "  bssid: " MACSTR "  pending: " MACSTR
   2032 		"  wpa_state: %s  ssid=%p  current_ssid=%p",
   2033 		wpa_s->reassociate, MAC2STR(selected->bssid),
   2034 		MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
   2035 		wpa_supplicant_state_txt(wpa_s->wpa_state),
   2036 		ssid, wpa_s->current_ssid);
   2037 
   2038 	/*
   2039 	 * Do not trigger new association unless the BSSID has changed or if
   2040 	 * reassociation is requested. If we are in process of associating with
   2041 	 * the selected BSSID, do not trigger new attempt.
   2042 	 */
   2043 	if (wpa_s->reassociate ||
   2044 	    (!ether_addr_equal(selected->bssid, wpa_s->bssid) &&
   2045 	     ((wpa_s->wpa_state != WPA_ASSOCIATING &&
   2046 	       wpa_s->wpa_state != WPA_AUTHENTICATING) ||
   2047 	      (!is_zero_ether_addr(wpa_s->pending_bssid) &&
   2048 	       !ether_addr_equal(selected->bssid, wpa_s->pending_bssid)) ||
   2049 	      (is_zero_ether_addr(wpa_s->pending_bssid) &&
   2050 	       ssid != wpa_s->current_ssid)))) {
   2051 		if (wpa_supplicant_scard_init(wpa_s, ssid)) {
   2052 			wpa_supplicant_req_new_scan(wpa_s, 10, 0);
   2053 			return 0;
   2054 		}
   2055 
   2056 		if (wpa_supplicant_connect_ml_missing(wpa_s, selected, ssid))
   2057 			return 0;
   2058 
   2059 		wpa_msg(wpa_s, MSG_DEBUG, "Request association with " MACSTR,
   2060 			MAC2STR(selected->bssid));
   2061 		wpa_supplicant_associate(wpa_s, selected, ssid);
   2062 	} else {
   2063 		wpa_dbg(wpa_s, MSG_DEBUG, "Already associated or trying to "
   2064 			"connect with the selected AP");
   2065 	}
   2066 
   2067 	return 0;
   2068 }
   2069 
   2070 
   2071 static struct wpa_ssid *
   2072 wpa_supplicant_pick_new_network(struct wpa_supplicant *wpa_s)
   2073 {
   2074 	size_t prio;
   2075 	struct wpa_ssid *ssid;
   2076 
   2077 	for (prio = 0; prio < wpa_s->conf->num_prio; prio++) {
   2078 		for (ssid = wpa_s->conf->pssid[prio]; ssid; ssid = ssid->pnext)
   2079 		{
   2080 			if (wpas_network_disabled(wpa_s, ssid))
   2081 				continue;
   2082 #ifndef CONFIG_IBSS_RSN
   2083 			if (ssid->mode == WPAS_MODE_IBSS &&
   2084 			    !(ssid->key_mgmt & (WPA_KEY_MGMT_NONE |
   2085 						WPA_KEY_MGMT_WPA_NONE))) {
   2086 				wpa_msg(wpa_s, MSG_INFO,
   2087 					"IBSS RSN not supported in the build - cannot use the profile for SSID '%s'",
   2088 					wpa_ssid_txt(ssid->ssid,
   2089 						     ssid->ssid_len));
   2090 				continue;
   2091 			}
   2092 #endif /* !CONFIG_IBSS_RSN */
   2093 			if (ssid->mode == WPAS_MODE_IBSS ||
   2094 			    ssid->mode == WPAS_MODE_AP ||
   2095 			    ssid->mode == WPAS_MODE_MESH)
   2096 				return ssid;
   2097 		}
   2098 	}
   2099 	return NULL;
   2100 }
   2101 
   2102 
   2103 /* TODO: move the rsn_preauth_scan_result*() to be called from notify.c based
   2104  * on BSS added and BSS changed events */
   2105 static void wpa_supplicant_rsn_preauth_scan_results(
   2106 	struct wpa_supplicant *wpa_s)
   2107 {
   2108 	struct wpa_bss *bss;
   2109 
   2110 	if (rsn_preauth_scan_results(wpa_s->wpa) < 0)
   2111 		return;
   2112 
   2113 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
   2114 		const u8 *ssid, *rsn;
   2115 
   2116 		ssid = wpa_bss_get_ie(bss, WLAN_EID_SSID);
   2117 		if (ssid == NULL)
   2118 			continue;
   2119 
   2120 		rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
   2121 		if (rsn == NULL)
   2122 			continue;
   2123 
   2124 		rsn_preauth_scan_result(wpa_s->wpa, bss->bssid, ssid, rsn);
   2125 	}
   2126 
   2127 }
   2128 
   2129 
   2130 #ifndef CONFIG_NO_ROAMING
   2131 
   2132 static int wpas_get_snr_signal_info(u32 frequency, int avg_signal, int noise)
   2133 {
   2134 	if (noise == WPA_INVALID_NOISE) {
   2135 		if (IS_5GHZ(frequency)) {
   2136 			noise = DEFAULT_NOISE_FLOOR_5GHZ;
   2137 		} else if (is_6ghz_freq(frequency)) {
   2138 			noise = DEFAULT_NOISE_FLOOR_6GHZ;
   2139 		} else {
   2140 			noise = DEFAULT_NOISE_FLOOR_2GHZ;
   2141 		}
   2142 	}
   2143 	return avg_signal - noise;
   2144 }
   2145 
   2146 
   2147 static unsigned int
   2148 wpas_get_est_throughput_from_bss_snr(const struct wpa_supplicant *wpa_s,
   2149 				     const struct wpa_bss *bss, int snr)
   2150 {
   2151 	int rate = wpa_bss_get_max_rate(bss);
   2152 	const u8 *ies = wpa_bss_ie_ptr(bss);
   2153 	size_t ie_len = bss->ie_len ? bss->ie_len : bss->beacon_ie_len;
   2154 	enum chan_width max_cw = CHAN_WIDTH_UNKNOWN;
   2155 
   2156 	return wpas_get_est_tpt(wpa_s, ies, ie_len, rate, snr, bss->freq,
   2157 				&max_cw);
   2158 }
   2159 
   2160 
   2161 static int wpas_evaluate_band_score(int frequency)
   2162 {
   2163 	if (is_6ghz_freq(frequency))
   2164 		return 2;
   2165 	if (IS_5GHZ(frequency))
   2166 		return 1;
   2167 	return 0;
   2168 }
   2169 
   2170 
   2171 int wpa_supplicant_need_to_roam_within_ess(struct wpa_supplicant *wpa_s,
   2172 					   struct wpa_bss *current_bss,
   2173 					   struct wpa_bss *selected)
   2174 {
   2175 	int min_diff, diff;
   2176 	int cur_band_score, sel_band_score;
   2177 	int to_5ghz, to_6ghz;
   2178 	int cur_level, sel_level;
   2179 	unsigned int cur_est, sel_est;
   2180 	struct wpa_signal_info si;
   2181 	int cur_snr = 0;
   2182 	int ret = 0;
   2183 	const u8 *cur_ies = wpa_bss_ie_ptr(current_bss);
   2184 	const u8 *sel_ies = wpa_bss_ie_ptr(selected);
   2185 	size_t cur_ie_len = current_bss->ie_len ? current_bss->ie_len :
   2186 		current_bss->beacon_ie_len;
   2187 	size_t sel_ie_len = selected->ie_len ? selected->ie_len :
   2188 		selected->beacon_ie_len;
   2189 
   2190 	wpa_dbg(wpa_s, MSG_DEBUG, "Considering within-ESS reassociation");
   2191 	wpa_dbg(wpa_s, MSG_DEBUG, "Current BSS: " MACSTR
   2192 		" freq=%d level=%d snr=%d est_throughput=%u",
   2193 		MAC2STR(current_bss->bssid),
   2194 		current_bss->freq, current_bss->level,
   2195 		current_bss->snr, current_bss->est_throughput);
   2196 	wpa_dbg(wpa_s, MSG_DEBUG, "Selected BSS: " MACSTR
   2197 		" freq=%d level=%d snr=%d est_throughput=%u",
   2198 		MAC2STR(selected->bssid), selected->freq, selected->level,
   2199 		selected->snr, selected->est_throughput);
   2200 
   2201 	if (wpas_ap_link_address(wpa_s, selected->bssid)) {
   2202 		wpa_dbg(wpa_s, MSG_DEBUG, "MLD: associated to selected BSS");
   2203 		return 0;
   2204 	}
   2205 
   2206 	if (wpa_s->current_ssid->bssid_set &&
   2207 	    ether_addr_equal(selected->bssid, wpa_s->current_ssid->bssid)) {
   2208 		wpa_dbg(wpa_s, MSG_DEBUG, "Allow reassociation - selected BSS "
   2209 			"has preferred BSSID");
   2210 		return 1;
   2211 	}
   2212 
   2213 	/*
   2214 	 * Try to poll the signal from the driver since this will allow to get
   2215 	 * more accurate values. In some cases, there can be big differences
   2216 	 * between the RSSI of the Probe Response frames of the AP we are
   2217 	 * associated with and the Beacon frames we hear from the same AP after
   2218 	 * association. This can happen, e.g., when there are two antennas that
   2219 	 * hear the AP very differently. If the driver chooses to hear the
   2220 	 * Probe Response frames during the scan on the "bad" antenna because
   2221 	 * it wants to save power, but knows to choose the other antenna after
   2222 	 * association, we will hear our AP with a low RSSI as part of the
   2223 	 * scan even when we can hear it decently on the other antenna. To cope
   2224 	 * with this, ask the driver to teach us how it hears the AP. Also, the
   2225 	 * scan results may be a bit old, since we can very quickly get fresh
   2226 	 * information about our currently associated AP.
   2227 	 */
   2228 	if (wpa_drv_signal_poll(wpa_s, &si) == 0 &&
   2229 	    (si.data.avg_beacon_signal || si.data.avg_signal)) {
   2230 		/*
   2231 		 * Normalize avg_signal to the RSSI over 20 MHz, as the
   2232 		 * throughput is estimated based on the RSSI over 20 MHz
   2233 		 */
   2234 		cur_level = si.data.avg_beacon_signal ?
   2235 			si.data.avg_beacon_signal :
   2236 			(si.data.avg_signal -
   2237 			 wpas_channel_width_rssi_bump(cur_ies, cur_ie_len,
   2238 						      si.chanwidth));
   2239 		cur_snr = wpas_get_snr_signal_info(si.frequency, cur_level,
   2240 						   si.current_noise);
   2241 
   2242 		cur_est = wpas_get_est_throughput_from_bss_snr(wpa_s,
   2243 							       current_bss,
   2244 							       cur_snr);
   2245 		wpa_dbg(wpa_s, MSG_DEBUG,
   2246 			"Using signal poll values for the current BSS: level=%d snr=%d est_throughput=%u",
   2247 			cur_level, cur_snr, cur_est);
   2248 	} else {
   2249 		/* Level and SNR are measured over 20 MHz channel */
   2250 		cur_level = current_bss->level;
   2251 		cur_snr = current_bss->snr;
   2252 		cur_est = current_bss->est_throughput;
   2253 	}
   2254 
   2255 	/* Adjust the SNR of BSSes based on the channel width. */
   2256 	cur_level += wpas_channel_width_rssi_bump(cur_ies, cur_ie_len,
   2257 						  current_bss->max_cw);
   2258 	cur_snr = wpas_adjust_snr_by_chanwidth(cur_ies, cur_ie_len,
   2259 					       current_bss->max_cw, cur_snr);
   2260 
   2261 	sel_est = selected->est_throughput;
   2262 	sel_level = selected->level +
   2263 		wpas_channel_width_rssi_bump(sel_ies, sel_ie_len,
   2264 					     selected->max_cw);
   2265 
   2266 	if (sel_est > cur_est + 5000) {
   2267 		wpa_dbg(wpa_s, MSG_DEBUG,
   2268 			"Allow reassociation - selected BSS has better estimated throughput");
   2269 		return 1;
   2270 	}
   2271 
   2272 	to_5ghz = selected->freq > 4000 && current_bss->freq < 4000;
   2273 	to_6ghz = is_6ghz_freq(selected->freq) &&
   2274 		!is_6ghz_freq(current_bss->freq);
   2275 
   2276 	if (cur_level < 0 &&
   2277 	    cur_level > sel_level + to_5ghz * 2 + to_6ghz * 2 &&
   2278 	    sel_est < cur_est * 1.2) {
   2279 		wpa_dbg(wpa_s, MSG_DEBUG, "Skip roam - Current BSS has better "
   2280 			"signal level");
   2281 		return 0;
   2282 	}
   2283 
   2284 	if (cur_est > sel_est + 5000) {
   2285 		wpa_dbg(wpa_s, MSG_DEBUG,
   2286 			"Skip roam - Current BSS has better estimated throughput");
   2287 		return 0;
   2288 	}
   2289 
   2290 	if (cur_snr > GREAT_SNR) {
   2291 		wpa_dbg(wpa_s, MSG_DEBUG,
   2292 			"Skip roam - Current BSS has good SNR (%u > %u)",
   2293 			cur_snr, GREAT_SNR);
   2294 		return 0;
   2295 	}
   2296 
   2297 	if (cur_level < -85) /* ..-86 dBm */
   2298 		min_diff = 1;
   2299 	else if (cur_level < -80) /* -85..-81 dBm */
   2300 		min_diff = 2;
   2301 	else if (cur_level < -75) /* -80..-76 dBm */
   2302 		min_diff = 3;
   2303 	else if (cur_level < -70) /* -75..-71 dBm */
   2304 		min_diff = 4;
   2305 	else if (cur_level < 0) /* -70..-1 dBm */
   2306 		min_diff = 5;
   2307 	else /* unspecified units (not in dBm) */
   2308 		min_diff = 2;
   2309 
   2310 	if (cur_est > sel_est * 1.5)
   2311 		min_diff += 10;
   2312 	else if (cur_est > sel_est * 1.2)
   2313 		min_diff += 5;
   2314 	else if (cur_est > sel_est * 1.1)
   2315 		min_diff += 2;
   2316 	else if (cur_est > sel_est)
   2317 		min_diff++;
   2318 	else if (sel_est > cur_est * 1.5)
   2319 		min_diff -= 10;
   2320 	else if (sel_est > cur_est * 1.2)
   2321 		min_diff -= 5;
   2322 	else if (sel_est > cur_est * 1.1)
   2323 		min_diff -= 2;
   2324 	else if (sel_est > cur_est)
   2325 		min_diff--;
   2326 
   2327 	cur_band_score = wpas_evaluate_band_score(current_bss->freq);
   2328 	sel_band_score = wpas_evaluate_band_score(selected->freq);
   2329 	min_diff += (cur_band_score - sel_band_score) * 2;
   2330 	if (wpa_s->signal_threshold && cur_level <= wpa_s->signal_threshold &&
   2331 	    sel_level > wpa_s->signal_threshold)
   2332 		min_diff -= 2;
   2333 	diff = sel_level - cur_level;
   2334 	if (diff < min_diff) {
   2335 		wpa_dbg(wpa_s, MSG_DEBUG,
   2336 			"Skip roam - too small difference in signal level (%d < %d)",
   2337 			diff, min_diff);
   2338 		ret = 0;
   2339 	} else {
   2340 		wpa_dbg(wpa_s, MSG_DEBUG,
   2341 			"Allow reassociation due to difference in signal level (%d >= %d)",
   2342 			diff, min_diff);
   2343 		ret = 1;
   2344 	}
   2345 	wpa_msg_ctrl(wpa_s, MSG_INFO, "%scur_bssid=" MACSTR
   2346 		     " cur_freq=%d cur_level=%d cur_est=%d sel_bssid=" MACSTR
   2347 		     " sel_freq=%d sel_level=%d sel_est=%d",
   2348 		     ret ? WPA_EVENT_DO_ROAM : WPA_EVENT_SKIP_ROAM,
   2349 		     MAC2STR(current_bss->bssid),
   2350 		     current_bss->freq, cur_level, cur_est,
   2351 		     MAC2STR(selected->bssid),
   2352 		     selected->freq, sel_level, sel_est);
   2353 	return ret;
   2354 }
   2355 
   2356 #endif /* CONFIG_NO_ROAMING */
   2357 
   2358 
   2359 static int wpa_supplicant_need_to_roam(struct wpa_supplicant *wpa_s,
   2360 				       struct wpa_bss *selected,
   2361 				       struct wpa_ssid *ssid)
   2362 {
   2363 	struct wpa_bss *current_bss = NULL;
   2364 	const u8 *bssid;
   2365 
   2366 	if (wpa_s->reassociate)
   2367 		return 1; /* explicit request to reassociate */
   2368 	if (wpa_s->wpa_state < WPA_ASSOCIATED)
   2369 		return 1; /* we are not associated; continue */
   2370 	if (wpa_s->current_ssid == NULL)
   2371 		return 1; /* unknown current SSID */
   2372 	if (wpa_s->current_ssid != ssid)
   2373 		return 1; /* different network block */
   2374 
   2375 	if (wpas_driver_bss_selection(wpa_s))
   2376 		return 0; /* Driver-based roaming */
   2377 
   2378 	if (wpa_s->valid_links)
   2379 		bssid = wpa_s->links[wpa_s->mlo_assoc_link_id].bssid;
   2380 	else
   2381 		bssid = wpa_s->bssid;
   2382 
   2383 	if (wpa_s->current_ssid->ssid)
   2384 		current_bss = wpa_bss_get(wpa_s, bssid,
   2385 					  wpa_s->current_ssid->ssid,
   2386 					  wpa_s->current_ssid->ssid_len);
   2387 	if (!current_bss)
   2388 		current_bss = wpa_bss_get_bssid(wpa_s, bssid);
   2389 
   2390 	if (!current_bss)
   2391 		return 1; /* current BSS not seen in scan results */
   2392 
   2393 	if (current_bss == selected)
   2394 		return 0;
   2395 
   2396 	if (selected->last_update_idx > current_bss->last_update_idx)
   2397 		return 1; /* current BSS not seen in the last scan */
   2398 
   2399 #ifndef CONFIG_NO_ROAMING
   2400 	return wpa_supplicant_need_to_roam_within_ess(wpa_s, current_bss,
   2401 						      selected);
   2402 #else /* CONFIG_NO_ROAMING */
   2403 	return 0;
   2404 #endif /* CONFIG_NO_ROAMING */
   2405 }
   2406 
   2407 
   2408 /*
   2409  * Return a negative value if no scan results could be fetched or if scan
   2410  * results should not be shared with other virtual interfaces.
   2411  * Return 0 if scan results were fetched and may be shared with other
   2412  * interfaces.
   2413  * Return 1 if scan results may be shared with other virtual interfaces but may
   2414  * not trigger any operations.
   2415  * Return 2 if the interface was removed and cannot be used.
   2416  */
   2417 static int _wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
   2418 					      union wpa_event_data *data,
   2419 					      int own_request, int update_only)
   2420 {
   2421 	struct wpa_scan_results *scan_res = NULL;
   2422 	int ret = 0;
   2423 	int ap = 0;
   2424 	bool trigger_6ghz_scan;
   2425 #ifndef CONFIG_NO_RANDOM_POOL
   2426 	size_t i, num;
   2427 #endif /* CONFIG_NO_RANDOM_POOL */
   2428 
   2429 #ifdef CONFIG_AP
   2430 	if (wpa_s->ap_iface)
   2431 		ap = 1;
   2432 #endif /* CONFIG_AP */
   2433 
   2434 	trigger_6ghz_scan = wpa_s->crossed_6ghz_dom &&
   2435 		wpa_s->last_scan_all_chan;
   2436 	wpa_s->crossed_6ghz_dom = false;
   2437 	wpa_s->last_scan_all_chan = false;
   2438 
   2439 	wpa_supplicant_notify_scanning(wpa_s, 0);
   2440 
   2441 	scan_res = wpa_supplicant_get_scan_results(wpa_s,
   2442 						   data ? &data->scan_info :
   2443 						   NULL, 1, NULL);
   2444 	if (scan_res == NULL) {
   2445 		if (wpa_s->conf->ap_scan == 2 || ap ||
   2446 		    wpa_s->scan_res_handler == scan_only_handler)
   2447 			return -1;
   2448 		if (!own_request)
   2449 			return -1;
   2450 		if (data && data->scan_info.external_scan)
   2451 			return -1;
   2452 		if (wpa_s->scan_res_fail_handler) {
   2453 			void (*handler)(struct wpa_supplicant *wpa_s);
   2454 
   2455 			handler = wpa_s->scan_res_fail_handler;
   2456 			wpa_s->scan_res_fail_handler = NULL;
   2457 			handler(wpa_s);
   2458 		} else {
   2459 			wpa_dbg(wpa_s, MSG_DEBUG,
   2460 				"Failed to get scan results - try scanning again");
   2461 			wpa_supplicant_req_new_scan(wpa_s, 1, 0);
   2462 		}
   2463 
   2464 		ret = -1;
   2465 		goto scan_work_done;
   2466 	}
   2467 
   2468 #ifndef CONFIG_NO_RANDOM_POOL
   2469 	num = scan_res->num;
   2470 	if (num > 10)
   2471 		num = 10;
   2472 	for (i = 0; i < num; i++) {
   2473 		u8 buf[5];
   2474 		struct wpa_scan_res *res = scan_res->res[i];
   2475 		buf[0] = res->bssid[5];
   2476 		buf[1] = res->qual & 0xff;
   2477 		buf[2] = res->noise & 0xff;
   2478 		buf[3] = res->level & 0xff;
   2479 		buf[4] = res->tsf & 0xff;
   2480 		random_add_randomness(buf, sizeof(buf));
   2481 	}
   2482 #endif /* CONFIG_NO_RANDOM_POOL */
   2483 
   2484 	if (update_only) {
   2485 		ret = 1;
   2486 		goto scan_work_done;
   2487 	}
   2488 
   2489 	if (own_request && wpa_s->scan_res_handler &&
   2490 	    !(data && data->scan_info.external_scan)) {
   2491 		void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
   2492 					 struct wpa_scan_results *scan_res);
   2493 
   2494 		scan_res_handler = wpa_s->scan_res_handler;
   2495 		wpa_s->scan_res_handler = NULL;
   2496 		scan_res_handler(wpa_s, scan_res);
   2497 		ret = 1;
   2498 		goto scan_work_done;
   2499 	}
   2500 
   2501 	wpa_dbg(wpa_s, MSG_DEBUG, "New scan results available (own=%u ext=%u)",
   2502 		wpa_s->own_scan_running,
   2503 		data ? data->scan_info.external_scan : 0);
   2504 	if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
   2505 	    wpa_s->manual_scan_use_id && wpa_s->own_scan_running &&
   2506 	    own_request && !(data && data->scan_info.external_scan)) {
   2507 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
   2508 			     wpa_s->manual_scan_id);
   2509 		wpa_s->manual_scan_use_id = 0;
   2510 	} else {
   2511 		wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
   2512 	}
   2513 	wpas_notify_scan_results(wpa_s);
   2514 
   2515 	wpas_notify_scan_done(wpa_s, 1);
   2516 
   2517 	if (ap) {
   2518 		wpa_dbg(wpa_s, MSG_DEBUG, "Ignore scan results in AP mode");
   2519 #ifdef CONFIG_AP
   2520 		if (wpa_s->ap_iface->scan_cb)
   2521 			wpa_s->ap_iface->scan_cb(wpa_s->ap_iface);
   2522 #endif /* CONFIG_AP */
   2523 		goto scan_work_done;
   2524 	}
   2525 
   2526 	if (data && data->scan_info.external_scan) {
   2527 		wpa_dbg(wpa_s, MSG_DEBUG, "Do not use results from externally requested scan operation for network selection");
   2528 		wpa_scan_results_free(scan_res);
   2529 		return 0;
   2530 	}
   2531 
   2532 	if (wnm_scan_process(wpa_s, false) > 0)
   2533 		goto scan_work_done;
   2534 
   2535 	if (sme_proc_obss_scan(wpa_s) > 0)
   2536 		goto scan_work_done;
   2537 
   2538 #ifndef CONFIG_NO_RRM
   2539 	if (own_request && data &&
   2540 	    wpas_beacon_rep_scan_process(wpa_s, scan_res, &data->scan_info) > 0)
   2541 		goto scan_work_done;
   2542 #endif /* CONFIG_NO_RRM */
   2543 
   2544 	if (ml_link_probe_scan(wpa_s))
   2545 		goto scan_work_done;
   2546 
   2547 	if ((wpa_s->conf->ap_scan == 2 && !wpas_wps_searching(wpa_s)))
   2548 		goto scan_work_done;
   2549 
   2550 	if (autoscan_notify_scan(wpa_s, scan_res))
   2551 		goto scan_work_done;
   2552 
   2553 	if (wpa_s->disconnected) {
   2554 		wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
   2555 		goto scan_work_done;
   2556 	}
   2557 
   2558 	if (!wpas_driver_bss_selection(wpa_s) &&
   2559 	    bgscan_notify_scan(wpa_s, scan_res) == 1)
   2560 		goto scan_work_done;
   2561 
   2562 	wpas_wps_update_ap_info(wpa_s, scan_res);
   2563 
   2564 	if (wpa_s->wpa_state >= WPA_AUTHENTICATING &&
   2565 	    wpa_s->wpa_state < WPA_COMPLETED)
   2566 		goto scan_work_done;
   2567 
   2568 	wpa_scan_results_free(scan_res);
   2569 
   2570 	if (own_request && wpa_s->scan_work) {
   2571 		struct wpa_radio_work *work = wpa_s->scan_work;
   2572 		wpa_s->scan_work = NULL;
   2573 		radio_work_done(work);
   2574 	}
   2575 
   2576 	os_free(wpa_s->last_scan_freqs);
   2577 	wpa_s->last_scan_freqs = NULL;
   2578 	wpa_s->num_last_scan_freqs = 0;
   2579 	if (own_request && data &&
   2580 	    data->scan_info.freqs && data->scan_info.num_freqs) {
   2581 		wpa_s->last_scan_freqs = os_malloc(sizeof(int) *
   2582 						   data->scan_info.num_freqs);
   2583 		if (wpa_s->last_scan_freqs) {
   2584 			os_memcpy(wpa_s->last_scan_freqs,
   2585 				  data->scan_info.freqs,
   2586 				  sizeof(int) * data->scan_info.num_freqs);
   2587 			wpa_s->num_last_scan_freqs = data->scan_info.num_freqs;
   2588 		}
   2589 	}
   2590 
   2591 	if (wpa_s->supp_pbc_active && !wpas_wps_partner_link_scan_done(wpa_s))
   2592 		return ret;
   2593 
   2594 	return wpas_select_network_from_last_scan(wpa_s, 1, own_request,
   2595 						  trigger_6ghz_scan, data);
   2596 
   2597 scan_work_done:
   2598 	wpa_scan_results_free(scan_res);
   2599 	if (own_request && wpa_s->scan_work) {
   2600 		struct wpa_radio_work *work = wpa_s->scan_work;
   2601 		wpa_s->scan_work = NULL;
   2602 		radio_work_done(work);
   2603 	}
   2604 	return ret;
   2605 }
   2606 
   2607 
   2608 static int wpas_trigger_6ghz_scan(struct wpa_supplicant *wpa_s,
   2609 				  union wpa_event_data *data)
   2610 {
   2611 	struct wpa_driver_scan_params params;
   2612 	unsigned int j;
   2613 
   2614 	wpa_dbg(wpa_s, MSG_INFO, "Triggering 6GHz-only scan");
   2615 	os_memset(&params, 0, sizeof(params));
   2616 	params.non_coloc_6ghz = wpa_s->last_scan_non_coloc_6ghz;
   2617 	for (j = 0; j < data->scan_info.num_ssids; j++)
   2618 		params.ssids[j] = data->scan_info.ssids[j];
   2619 	params.num_ssids = data->scan_info.num_ssids;
   2620 	wpa_add_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, &params,
   2621 				true, !wpa_s->last_scan_non_coloc_6ghz, false);
   2622 	if (!wpa_supplicant_trigger_scan(wpa_s, &params, true, true)) {
   2623 		os_free(params.freqs);
   2624 		return 1;
   2625 	}
   2626 	wpa_dbg(wpa_s, MSG_INFO, "Failed to trigger 6GHz-only scan");
   2627 	os_free(params.freqs);
   2628 	return 0;
   2629 }
   2630 
   2631 
   2632 /**
   2633  * Select a network from the last scan
   2634  * @wpa_s: Pointer to wpa_supplicant data
   2635  * @new_scan: Whether this function was called right after a scan has finished
   2636  * @own_request: Whether the scan was requested by this interface
   2637  * @trigger_6ghz_scan: Whether to trigger a 6ghz-only scan when applicable
   2638  * @data: Scan data from scan that finished if applicable
   2639  *
   2640  * See _wpa_supplicant_event_scan_results() for return values.
   2641  */
   2642 static int wpas_select_network_from_last_scan(struct wpa_supplicant *wpa_s,
   2643 					      int new_scan, int own_request,
   2644 					      bool trigger_6ghz_scan,
   2645 					      union wpa_event_data *data)
   2646 {
   2647 	struct wpa_bss *selected;
   2648 	struct wpa_ssid *ssid = NULL;
   2649 	int time_to_reenable = wpas_reenabled_network_time(wpa_s);
   2650 
   2651 	if (time_to_reenable > 0) {
   2652 		wpa_dbg(wpa_s, MSG_DEBUG,
   2653 			"Postpone network selection by %d seconds since all networks are disabled",
   2654 			time_to_reenable);
   2655 		eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
   2656 		eloop_register_timeout(time_to_reenable, 0,
   2657 				       wpas_network_reenabled, wpa_s, NULL);
   2658 		return 0;
   2659 	}
   2660 
   2661 	if (wpa_s->p2p_mgmt)
   2662 		return 0; /* no normal connection on p2p_mgmt interface */
   2663 
   2664 	wpa_s->owe_transition_search = 0;
   2665 #ifdef CONFIG_OWE
   2666 	os_free(wpa_s->owe_trans_scan_freq);
   2667 	wpa_s->owe_trans_scan_freq = NULL;
   2668 #endif /* CONFIG_OWE */
   2669 	selected = wpa_supplicant_pick_network(wpa_s, &ssid);
   2670 
   2671 #ifdef CONFIG_MESH
   2672 	if (wpa_s->ifmsh) {
   2673 		wpa_msg(wpa_s, MSG_INFO,
   2674 			"Avoiding join because we already joined a mesh group");
   2675 		return 0;
   2676 	}
   2677 #endif /* CONFIG_MESH */
   2678 
   2679 	if (selected) {
   2680 		int skip;
   2681 		skip = !wpa_supplicant_need_to_roam(wpa_s, selected, ssid);
   2682 		if (skip) {
   2683 			if (new_scan)
   2684 				wpa_supplicant_rsn_preauth_scan_results(wpa_s);
   2685 			return 0;
   2686 		}
   2687 
   2688 		wpa_s->suitable_network++;
   2689 
   2690 		if (ssid != wpa_s->current_ssid &&
   2691 		    wpa_s->wpa_state >= WPA_AUTHENTICATING) {
   2692 			wpa_s->own_disconnect_req = 1;
   2693 			wpa_supplicant_deauthenticate(
   2694 				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
   2695 		}
   2696 
   2697 		if (wpa_supplicant_connect(wpa_s, selected, ssid) < 0) {
   2698 			wpa_dbg(wpa_s, MSG_DEBUG, "Connect failed");
   2699 			return -1;
   2700 		}
   2701 		wpa_s->supp_pbc_active = false;
   2702 
   2703 		if (new_scan)
   2704 			wpa_supplicant_rsn_preauth_scan_results(wpa_s);
   2705 		/*
   2706 		 * Do not allow other virtual radios to trigger operations based
   2707 		 * on these scan results since we do not want them to start
   2708 		 * other associations at the same time.
   2709 		 */
   2710 		return 1;
   2711 	} else {
   2712 		wpa_s->no_suitable_network++;
   2713 		wpa_dbg(wpa_s, MSG_DEBUG, "No suitable network found");
   2714 		ssid = wpa_supplicant_pick_new_network(wpa_s);
   2715 		if (ssid) {
   2716 			wpa_dbg(wpa_s, MSG_DEBUG, "Setup a new network");
   2717 			wpa_supplicant_associate(wpa_s, NULL, ssid);
   2718 			if (new_scan)
   2719 				wpa_supplicant_rsn_preauth_scan_results(wpa_s);
   2720 		} else if (own_request) {
   2721 			if (wpa_s->support_6ghz && trigger_6ghz_scan && data &&
   2722 			    wpas_trigger_6ghz_scan(wpa_s, data) < 0)
   2723 				return 1;
   2724 
   2725 			/*
   2726 			 * No SSID found. If SCAN results are as a result of
   2727 			 * own scan request and not due to a scan request on
   2728 			 * another shared interface, try another scan.
   2729 			 */
   2730 			int timeout_sec = wpa_s->scan_interval;
   2731 			int timeout_usec = 0;
   2732 #ifdef CONFIG_P2P
   2733 			int res;
   2734 
   2735 			res = wpas_p2p_scan_no_go_seen(wpa_s);
   2736 			if (res == 2)
   2737 				return 2;
   2738 			if (res == 1)
   2739 				return 0;
   2740 
   2741 			if (wpas_p2p_retry_limit_exceeded(wpa_s))
   2742 				return 0;
   2743 
   2744 			if (wpa_s->p2p_in_provisioning ||
   2745 			    wpa_s->show_group_started ||
   2746 			    wpa_s->p2p_in_invitation) {
   2747 				/*
   2748 				 * Use shorter wait during P2P Provisioning
   2749 				 * state and during P2P join-a-group operation
   2750 				 * to speed up group formation.
   2751 				 */
   2752 				timeout_sec = 0;
   2753 				timeout_usec = 250000;
   2754 				wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
   2755 							    timeout_usec);
   2756 				return 0;
   2757 			}
   2758 #endif /* CONFIG_P2P */
   2759 #ifdef CONFIG_INTERWORKING
   2760 			if (wpa_s->conf->auto_interworking &&
   2761 			    wpa_s->conf->interworking &&
   2762 			    wpa_s->conf->cred) {
   2763 				wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: "
   2764 					"start ANQP fetch since no matching "
   2765 					"networks found");
   2766 				wpa_s->network_select = 1;
   2767 				wpa_s->auto_network_select = 1;
   2768 				interworking_start_fetch_anqp(wpa_s);
   2769 				return 1;
   2770 			}
   2771 #endif /* CONFIG_INTERWORKING */
   2772 #ifdef CONFIG_WPS
   2773 			if (wpa_s->after_wps > 0 || wpas_wps_searching(wpa_s)) {
   2774 				wpa_dbg(wpa_s, MSG_DEBUG, "Use shorter wait during WPS processing");
   2775 				timeout_sec = 0;
   2776 				timeout_usec = 500000;
   2777 				wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
   2778 							    timeout_usec);
   2779 				return 0;
   2780 			}
   2781 #endif /* CONFIG_WPS */
   2782 #ifdef CONFIG_OWE
   2783 			if (wpa_s->owe_transition_search) {
   2784 				wpa_dbg(wpa_s, MSG_DEBUG,
   2785 					"OWE: Use shorter wait during transition mode search");
   2786 				timeout_sec = 0;
   2787 				timeout_usec = 500000;
   2788 				if (wpa_s->owe_trans_scan_freq) {
   2789 					os_free(wpa_s->next_scan_freqs);
   2790 					wpa_s->next_scan_freqs =
   2791 						wpa_s->owe_trans_scan_freq;
   2792 					wpa_s->owe_trans_scan_freq = NULL;
   2793 					timeout_usec = 100000;
   2794 				}
   2795 				wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
   2796 							    timeout_usec);
   2797 				return 0;
   2798 			}
   2799 #endif /* CONFIG_OWE */
   2800 			if (wpa_supplicant_req_sched_scan(wpa_s))
   2801 				wpa_supplicant_req_new_scan(wpa_s, timeout_sec,
   2802 							    timeout_usec);
   2803 
   2804 			wpa_msg_ctrl(wpa_s, MSG_INFO,
   2805 				     WPA_EVENT_NETWORK_NOT_FOUND);
   2806 		}
   2807 	}
   2808 	return 0;
   2809 }
   2810 
   2811 
   2812 static int wpa_supplicant_event_scan_results(struct wpa_supplicant *wpa_s,
   2813 					     union wpa_event_data *data)
   2814 {
   2815 	struct wpa_supplicant *ifs;
   2816 	int res;
   2817 
   2818 	res = _wpa_supplicant_event_scan_results(wpa_s, data, 1, 0);
   2819 	if (res == 2) {
   2820 		/*
   2821 		 * Interface may have been removed, so must not dereference
   2822 		 * wpa_s after this.
   2823 		 */
   2824 		return 1;
   2825 	}
   2826 
   2827 	if (res < 0) {
   2828 		/*
   2829 		 * If no scan results could be fetched, then no need to
   2830 		 * notify those interfaces that did not actually request
   2831 		 * this scan. Similarly, if scan results started a new operation on this
   2832 		 * interface, do not notify other interfaces to avoid concurrent
   2833 		 * operations during a connection attempt.
   2834 		 */
   2835 		return 0;
   2836 	}
   2837 
   2838 	/*
   2839 	 * Check other interfaces to see if they share the same radio. If
   2840 	 * so, they get updated with this same scan info.
   2841 	 */
   2842 	dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
   2843 			 radio_list) {
   2844 		if (ifs != wpa_s) {
   2845 			wpa_printf(MSG_DEBUG, "%s: Updating scan results from "
   2846 				   "sibling", ifs->ifname);
   2847 			res = _wpa_supplicant_event_scan_results(ifs, data, 0,
   2848 								 res > 0);
   2849 			if (res < 0)
   2850 				return 0;
   2851 		}
   2852 	}
   2853 
   2854 	return 0;
   2855 }
   2856 
   2857 #endif /* CONFIG_NO_SCAN_PROCESSING */
   2858 
   2859 
   2860 int wpa_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
   2861 {
   2862 #ifdef CONFIG_NO_SCAN_PROCESSING
   2863 	return -1;
   2864 #else /* CONFIG_NO_SCAN_PROCESSING */
   2865 	struct os_reltime now;
   2866 
   2867 	wpa_s->ignore_post_flush_scan_res = 0;
   2868 
   2869 	if (wpa_s->last_scan_res_used == 0)
   2870 		return -1;
   2871 
   2872 	os_get_reltime(&now);
   2873 	if (os_reltime_expired(&now, &wpa_s->last_scan,
   2874 			       wpa_s->conf->scan_res_valid_for_connect)) {
   2875 		wpa_printf(MSG_DEBUG, "Fast associate: Old scan results");
   2876 		return -1;
   2877 	} else if (wpa_s->crossed_6ghz_dom) {
   2878 		wpa_printf(MSG_DEBUG, "Fast associate: Crossed 6 GHz domain");
   2879 		return -1;
   2880 	}
   2881 
   2882 	return wpas_select_network_from_last_scan(wpa_s, 0, 1, false, NULL);
   2883 #endif /* CONFIG_NO_SCAN_PROCESSING */
   2884 }
   2885 
   2886 
   2887 int wpa_wps_supplicant_fast_associate(struct wpa_supplicant *wpa_s)
   2888 {
   2889 #ifdef CONFIG_NO_SCAN_PROCESSING
   2890 	return -1;
   2891 #else /* CONFIG_NO_SCAN_PROCESSING */
   2892 	return wpas_select_network_from_last_scan(wpa_s, 1, 1, false, NULL);
   2893 #endif /* CONFIG_NO_SCAN_PROCESSING */
   2894 }
   2895 
   2896 
   2897 #ifdef CONFIG_WNM
   2898 
   2899 static void wnm_bss_keep_alive(void *eloop_ctx, void *sock_ctx)
   2900 {
   2901 	struct wpa_supplicant *wpa_s = eloop_ctx;
   2902 
   2903 	if (wpa_s->wpa_state < WPA_ASSOCIATED)
   2904 		return;
   2905 
   2906 	if (!wpa_s->no_keep_alive) {
   2907 		wpa_printf(MSG_DEBUG, "WNM: Send keep-alive to AP " MACSTR,
   2908 			   MAC2STR(wpa_s->bssid));
   2909 		/* TODO: could skip this if normal data traffic has been sent */
   2910 		/* TODO: Consider using some more appropriate data frame for
   2911 		 * this */
   2912 		if (wpa_s->l2)
   2913 			l2_packet_send(wpa_s->l2, wpa_s->bssid, 0x0800,
   2914 				       (u8 *) "", 0);
   2915 	}
   2916 
   2917 #ifdef CONFIG_SME
   2918 	if (wpa_s->sme.bss_max_idle_period) {
   2919 		unsigned int msec;
   2920 		msec = wpa_s->sme.bss_max_idle_period * 1024; /* times 1000 */
   2921 		if (msec > 100)
   2922 			msec -= 100;
   2923 		eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
   2924 				       wnm_bss_keep_alive, wpa_s, NULL);
   2925 	}
   2926 #endif /* CONFIG_SME */
   2927 }
   2928 
   2929 
   2930 static void wnm_process_assoc_resp(struct wpa_supplicant *wpa_s,
   2931 				   const u8 *ies, size_t ies_len)
   2932 {
   2933 	struct ieee802_11_elems elems;
   2934 
   2935 	if (ies == NULL)
   2936 		return;
   2937 
   2938 	if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
   2939 		return;
   2940 
   2941 #ifdef CONFIG_SME
   2942 	if (elems.bss_max_idle_period) {
   2943 		unsigned int msec;
   2944 		wpa_s->sme.bss_max_idle_period =
   2945 			WPA_GET_LE16(elems.bss_max_idle_period);
   2946 		wpa_printf(MSG_DEBUG, "WNM: BSS Max Idle Period: %u (* 1000 "
   2947 			   "TU)%s", wpa_s->sme.bss_max_idle_period,
   2948 			   (elems.bss_max_idle_period[2] & 0x01) ?
   2949 			   " (protected keep-live required)" : "");
   2950 		if (wpa_s->sme.bss_max_idle_period == 0)
   2951 			wpa_s->sme.bss_max_idle_period = 1;
   2952 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
   2953 			eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
   2954 			 /* msec times 1000 */
   2955 			msec = wpa_s->sme.bss_max_idle_period * 1024;
   2956 			if (msec > 100)
   2957 				msec -= 100;
   2958 			eloop_register_timeout(msec / 1000, msec % 1000 * 1000,
   2959 					       wnm_bss_keep_alive, wpa_s,
   2960 					       NULL);
   2961 		}
   2962 	} else {
   2963 		wpa_s->sme.bss_max_idle_period = 0;
   2964 	}
   2965 #endif /* CONFIG_SME */
   2966 }
   2967 
   2968 #endif /* CONFIG_WNM */
   2969 
   2970 
   2971 void wnm_bss_keep_alive_deinit(struct wpa_supplicant *wpa_s)
   2972 {
   2973 #ifdef CONFIG_WNM
   2974 	eloop_cancel_timeout(wnm_bss_keep_alive, wpa_s, NULL);
   2975 #endif /* CONFIG_WNM */
   2976 }
   2977 
   2978 
   2979 static int wpas_qos_map_set(struct wpa_supplicant *wpa_s, const u8 *qos_map,
   2980 			    size_t len)
   2981 {
   2982 	int res;
   2983 
   2984 	wpa_hexdump(MSG_DEBUG, "Interworking: QoS Map Set", qos_map, len);
   2985 	res = wpa_drv_set_qos_map(wpa_s, qos_map, len);
   2986 	if (res) {
   2987 		wpa_printf(MSG_DEBUG, "Interworking: Failed to configure QoS Map Set to the driver");
   2988 	}
   2989 
   2990 	return res;
   2991 }
   2992 
   2993 
   2994 static void interworking_process_assoc_resp(struct wpa_supplicant *wpa_s,
   2995 					    const u8 *ies, size_t ies_len)
   2996 {
   2997 	struct ieee802_11_elems elems;
   2998 
   2999 	if (ies == NULL)
   3000 		return;
   3001 
   3002 	if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed)
   3003 		return;
   3004 
   3005 	if (elems.qos_map_set) {
   3006 		wpas_qos_map_set(wpa_s, elems.qos_map_set,
   3007 				 elems.qos_map_set_len);
   3008 	}
   3009 }
   3010 
   3011 
   3012 static void wpa_supplicant_set_4addr_mode(struct wpa_supplicant *wpa_s)
   3013 {
   3014 	if (wpa_s->enabled_4addr_mode) {
   3015 		wpa_printf(MSG_DEBUG, "4addr mode already set");
   3016 		return;
   3017 	}
   3018 
   3019 	if (wpa_drv_set_4addr_mode(wpa_s, 1) < 0) {
   3020 		wpa_msg(wpa_s, MSG_ERROR, "Failed to set 4addr mode");
   3021 		goto fail;
   3022 	}
   3023 	wpa_s->enabled_4addr_mode = 1;
   3024 	wpa_msg(wpa_s, MSG_INFO, "Successfully set 4addr mode");
   3025 	return;
   3026 
   3027 fail:
   3028 	wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
   3029 }
   3030 
   3031 
   3032 static void multi_ap_process_assoc_resp(struct wpa_supplicant *wpa_s,
   3033 					const u8 *ies, size_t ies_len)
   3034 {
   3035 	struct ieee802_11_elems elems;
   3036 	struct multi_ap_params multi_ap;
   3037 	u16 status;
   3038 
   3039 	wpa_s->multi_ap_ie = 0;
   3040 
   3041 	if (!ies ||
   3042 	    ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed ||
   3043 	    !elems.multi_ap)
   3044 		return;
   3045 
   3046 	status = check_multi_ap_ie(elems.multi_ap + 4, elems.multi_ap_len - 4,
   3047 				   &multi_ap);
   3048 	if (status != WLAN_STATUS_SUCCESS)
   3049 		return;
   3050 
   3051 	wpa_s->multi_ap_backhaul = !!(multi_ap.capability &
   3052 				      MULTI_AP_BACKHAUL_BSS);
   3053 	wpa_s->multi_ap_fronthaul = !!(multi_ap.capability &
   3054 				       MULTI_AP_FRONTHAUL_BSS);
   3055 	wpa_s->multi_ap_ie = 1;
   3056 }
   3057 
   3058 
   3059 static void multi_ap_set_4addr_mode(struct wpa_supplicant *wpa_s)
   3060 {
   3061 	if (!wpa_s->current_ssid ||
   3062 	    !wpa_s->current_ssid->multi_ap_backhaul_sta)
   3063 		return;
   3064 
   3065 	if (!wpa_s->multi_ap_ie) {
   3066 		wpa_printf(MSG_INFO,
   3067 			   "AP does not include valid Multi-AP element");
   3068 		goto fail;
   3069 	}
   3070 
   3071 	if (!wpa_s->multi_ap_backhaul) {
   3072 		if (wpa_s->multi_ap_fronthaul &&
   3073 		    wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
   3074 			wpa_printf(MSG_INFO,
   3075 				   "WPS active, accepting fronthaul-only BSS");
   3076 			/* Don't set 4addr mode in this case, so just return */
   3077 			return;
   3078 		}
   3079 		wpa_printf(MSG_INFO, "AP doesn't support backhaul BSS");
   3080 		goto fail;
   3081 	}
   3082 
   3083 	wpa_supplicant_set_4addr_mode(wpa_s);
   3084 	return;
   3085 
   3086 fail:
   3087 	wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
   3088 }
   3089 
   3090 
   3091 #ifdef CONFIG_FST
   3092 static int wpas_fst_update_mbie(struct wpa_supplicant *wpa_s,
   3093 				const u8 *ie, size_t ie_len)
   3094 {
   3095 	struct mb_ies_info mb_ies;
   3096 
   3097 	if (!ie || !ie_len || !wpa_s->fst)
   3098 	    return -ENOENT;
   3099 
   3100 	os_memset(&mb_ies, 0, sizeof(mb_ies));
   3101 
   3102 	while (ie_len >= 2 && mb_ies.nof_ies < MAX_NOF_MB_IES_SUPPORTED) {
   3103 		size_t len;
   3104 
   3105 		len = 2 + ie[1];
   3106 		if (len > ie_len) {
   3107 			wpa_hexdump(MSG_DEBUG, "FST: Truncated IE found",
   3108 				    ie, ie_len);
   3109 			break;
   3110 		}
   3111 
   3112 		if (ie[0] == WLAN_EID_MULTI_BAND) {
   3113 			wpa_printf(MSG_DEBUG, "MB IE of %u bytes found",
   3114 				   (unsigned int) len);
   3115 			mb_ies.ies[mb_ies.nof_ies].ie = ie + 2;
   3116 			mb_ies.ies[mb_ies.nof_ies].ie_len = len - 2;
   3117 			mb_ies.nof_ies++;
   3118 		}
   3119 
   3120 		ie_len -= len;
   3121 		ie += len;
   3122 	}
   3123 
   3124 	if (mb_ies.nof_ies > 0) {
   3125 		wpabuf_free(wpa_s->received_mb_ies);
   3126 		wpa_s->received_mb_ies = mb_ies_by_info(&mb_ies);
   3127 		return 0;
   3128 	}
   3129 
   3130 	return -ENOENT;
   3131 }
   3132 #endif /* CONFIG_FST */
   3133 
   3134 
   3135 static int wpa_supplicant_use_own_rsne_params(struct wpa_supplicant *wpa_s,
   3136 					      union wpa_event_data *data)
   3137 {
   3138 	int sel;
   3139 	const u8 *p;
   3140 	int l, len;
   3141 	bool found = false;
   3142 	struct wpa_ie_data ie;
   3143 	struct wpa_ssid *ssid = wpa_s->current_ssid;
   3144 	struct wpa_bss *bss = wpa_s->current_bss;
   3145 	int pmf;
   3146 
   3147 	if (!ssid)
   3148 		return 0;
   3149 
   3150 	p = data->assoc_info.req_ies;
   3151 	l = data->assoc_info.req_ies_len;
   3152 
   3153 	while (p && l >= 2) {
   3154 		len = p[1] + 2;
   3155 		if (len > l) {
   3156 			wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
   3157 				    p, l);
   3158 			break;
   3159 		}
   3160 		if (((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
   3161 		      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
   3162 		     (p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 4 &&
   3163 		      (os_memcmp(&p[2], "\x50\x6F\x9A\x12", 4) == 0)) ||
   3164 		     (p[0] == WLAN_EID_RSN && p[1] >= 2))) {
   3165 			found = true;
   3166 			break;
   3167 		}
   3168 		l -= len;
   3169 		p += len;
   3170 	}
   3171 
   3172 	if (!found || wpa_parse_wpa_ie(p, len, &ie) < 0) {
   3173 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCV, 0);
   3174 		return 0;
   3175 	}
   3176 
   3177 	wpa_hexdump(MSG_DEBUG,
   3178 		    "WPA: Update cipher suite selection based on IEs in driver-generated WPA/RSNE in AssocReq",
   3179 		    p, l);
   3180 
   3181 	/* Update proto from (Re)Association Request frame info */
   3182 	wpa_s->wpa_proto = ie.proto;
   3183 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, wpa_s->wpa_proto);
   3184 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
   3185 			 !!(wpa_s->wpa_proto &
   3186 			    (WPA_PROTO_RSN | WPA_PROTO_OSEN)));
   3187 
   3188 	/* Update AKMP suite from (Re)Association Request frame info */
   3189 	sel = ie.key_mgmt;
   3190 	if (ssid->key_mgmt)
   3191 		sel &= ssid->key_mgmt;
   3192 
   3193 	wpa_dbg(wpa_s, MSG_DEBUG,
   3194 		"WPA: AP key_mgmt 0x%x network key_mgmt 0x%x; available key_mgmt 0x%x",
   3195 		ie.key_mgmt, ssid->key_mgmt, sel);
   3196 	if (ie.key_mgmt && !sel) {
   3197 		wpa_supplicant_deauthenticate(
   3198 			wpa_s, WLAN_REASON_AKMP_NOT_VALID);
   3199 		return -1;
   3200 	}
   3201 
   3202 #ifdef CONFIG_OCV
   3203 	if (((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) ||
   3204 	     (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_OCV)) && ssid->ocv)
   3205 		wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_OCV,
   3206 				 !!(ie.capabilities & WPA_CAPABILITY_OCVC));
   3207 #endif /* CONFIG_OCV */
   3208 
   3209 	/*
   3210 	 * Update PMK in wpa_sm and the driver if roamed to WPA/WPA2 PSK from a
   3211 	 * different AKM.
   3212 	 */
   3213 	if (wpa_s->key_mgmt != ie.key_mgmt &&
   3214 	    wpa_key_mgmt_wpa_psk_no_sae(ie.key_mgmt)) {
   3215 		if (!ssid->psk_set) {
   3216 			wpa_dbg(wpa_s, MSG_INFO,
   3217 				"No PSK available for association");
   3218 			wpas_auth_failed(wpa_s, "NO_PSK_AVAILABLE", NULL);
   3219 			return -1;
   3220 		}
   3221 
   3222 		wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN, NULL, NULL);
   3223 		if (wpa_s->conf->key_mgmt_offload &&
   3224 		    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD) &&
   3225 		    wpa_drv_set_key(wpa_s, -1, 0, NULL, 0, 0, NULL, 0,
   3226 				    ssid->psk, PMK_LEN, KEY_FLAG_PMK))
   3227 			wpa_dbg(wpa_s, MSG_ERROR,
   3228 				"WPA: Cannot set PMK for key management offload");
   3229 	}
   3230 
   3231 	wpa_s->key_mgmt = ie.key_mgmt;
   3232 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
   3233 	wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT %s and proto %d",
   3234 		wpa_key_mgmt_txt(wpa_s->key_mgmt, wpa_s->wpa_proto),
   3235 		wpa_s->wpa_proto);
   3236 
   3237 	/* Update pairwise cipher from (Re)Association Request frame info */
   3238 	sel = ie.pairwise_cipher;
   3239 	if (ssid->pairwise_cipher)
   3240 		sel &= ssid->pairwise_cipher;
   3241 
   3242 	wpa_dbg(wpa_s, MSG_DEBUG,
   3243 		"WPA: AP pairwise cipher 0x%x network pairwise cipher 0x%x; available pairwise cipher 0x%x",
   3244 		ie.pairwise_cipher, ssid->pairwise_cipher, sel);
   3245 	if (ie.pairwise_cipher && !sel) {
   3246 		wpa_supplicant_deauthenticate(
   3247 			wpa_s, WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID);
   3248 		return -1;
   3249 	}
   3250 
   3251 	wpa_s->pairwise_cipher = ie.pairwise_cipher;
   3252 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
   3253 			 wpa_s->pairwise_cipher);
   3254 	wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK %s",
   3255 		wpa_cipher_txt(wpa_s->pairwise_cipher));
   3256 
   3257 	/* Update other parameters based on AP's WPA IE/RSNE, if available */
   3258 	if (!bss) {
   3259 		wpa_dbg(wpa_s, MSG_DEBUG,
   3260 			"WPA: current_bss == NULL - skip AP IE check");
   3261 		return 0;
   3262 	}
   3263 
   3264 	/* Update GTK and IGTK from AP's RSNE */
   3265 	found = false;
   3266 
   3267 	if (wpa_s->wpa_proto & (WPA_PROTO_RSN | WPA_PROTO_OSEN)) {
   3268 		const u8 *bss_rsn;
   3269 
   3270 		bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
   3271 		if (bss_rsn) {
   3272 			p = bss_rsn;
   3273 			len = 2 + bss_rsn[1];
   3274 			found = true;
   3275 		}
   3276 	} else if (wpa_s->wpa_proto & WPA_PROTO_WPA) {
   3277 		const u8 *bss_wpa;
   3278 
   3279 		bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
   3280 		if (bss_wpa) {
   3281 			p = bss_wpa;
   3282 			len = 2 + bss_wpa[1];
   3283 			found = true;
   3284 		}
   3285 	}
   3286 
   3287 	if (!found || wpa_parse_wpa_ie(p, len, &ie) < 0)
   3288 		return 0;
   3289 
   3290 	pmf = wpas_get_ssid_pmf(wpa_s, ssid);
   3291 	if (!(ie.capabilities & WPA_CAPABILITY_MFPC) &&
   3292 	    pmf == MGMT_FRAME_PROTECTION_REQUIRED) {
   3293 		/* AP does not support MFP, local configuration requires it */
   3294 		wpa_supplicant_deauthenticate(
   3295 			wpa_s, WLAN_REASON_INVALID_RSN_IE_CAPAB);
   3296 		return -1;
   3297 	}
   3298 	if ((ie.capabilities & WPA_CAPABILITY_MFPR) &&
   3299 	    pmf == NO_MGMT_FRAME_PROTECTION) {
   3300 		/* AP requires MFP, local configuration disables it */
   3301 		wpa_supplicant_deauthenticate(
   3302 			wpa_s, WLAN_REASON_INVALID_RSN_IE_CAPAB);
   3303 		return -1;
   3304 	}
   3305 
   3306 	/* Update PMF from local configuration now that MFP validation was done
   3307 	 * above */
   3308 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, pmf);
   3309 
   3310 	/* Update GTK from AP's RSNE */
   3311 	sel = ie.group_cipher;
   3312 	if (ssid->group_cipher)
   3313 		sel &= ssid->group_cipher;
   3314 
   3315 	wpa_dbg(wpa_s, MSG_DEBUG,
   3316 		"WPA: AP group cipher 0x%x network group cipher 0x%x; available group cipher 0x%x",
   3317 		ie.group_cipher, ssid->group_cipher, sel);
   3318 	if (ie.group_cipher && !sel) {
   3319 		wpa_supplicant_deauthenticate(
   3320 			wpa_s, WLAN_REASON_GROUP_CIPHER_NOT_VALID);
   3321 		return -1;
   3322 	}
   3323 
   3324 	wpa_s->group_cipher = ie.group_cipher;
   3325 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
   3326 	wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK %s",
   3327 		wpa_cipher_txt(wpa_s->group_cipher));
   3328 
   3329 	/* Update IGTK from AP RSN IE */
   3330 	sel = ie.mgmt_group_cipher;
   3331 	if (ssid->group_mgmt_cipher)
   3332 		sel &= ssid->group_mgmt_cipher;
   3333 
   3334 	wpa_dbg(wpa_s, MSG_DEBUG,
   3335 		"WPA: AP mgmt_group_cipher 0x%x network mgmt_group_cipher 0x%x; available mgmt_group_cipher 0x%x",
   3336 		ie.mgmt_group_cipher, ssid->group_mgmt_cipher, sel);
   3337 
   3338 	if (pmf == NO_MGMT_FRAME_PROTECTION ||
   3339 	    !(ie.capabilities & WPA_CAPABILITY_MFPC)) {
   3340 		wpa_dbg(wpa_s, MSG_DEBUG,
   3341 			"WPA: STA/AP is not MFP capable; AP RSNE caps 0x%x",
   3342 			ie.capabilities);
   3343 		ie.mgmt_group_cipher = 0;
   3344 	}
   3345 
   3346 	if (ie.mgmt_group_cipher && !sel) {
   3347 		wpa_supplicant_deauthenticate(
   3348 			wpa_s, WLAN_REASON_CIPHER_SUITE_REJECTED);
   3349 		return -1;
   3350 	}
   3351 
   3352 	wpa_s->mgmt_group_cipher = ie.mgmt_group_cipher;
   3353 	wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
   3354 			 wpa_s->mgmt_group_cipher);
   3355 	if (wpa_s->mgmt_group_cipher)
   3356 		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher %s",
   3357 			wpa_cipher_txt(wpa_s->mgmt_group_cipher));
   3358 	else
   3359 		wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
   3360 
   3361 	return 0;
   3362 }
   3363 
   3364 
   3365 static int wpa_supplicant_event_associnfo(struct wpa_supplicant *wpa_s,
   3366 					  union wpa_event_data *data)
   3367 {
   3368 	int l, len, found = 0, found_x = 0, wpa_found, rsn_found;
   3369 	const u8 *p;
   3370 	u8 bssid[ETH_ALEN];
   3371 	bool bssid_known;
   3372 
   3373 	wpa_dbg(wpa_s, MSG_DEBUG, "Association info event");
   3374 	wpa_s->ssid_verified = false;
   3375 	wpa_s->bigtk_set = false;
   3376 #ifdef CONFIG_SAE
   3377 #ifdef CONFIG_SME
   3378 	/* SAE H2E binds the SSID into PT and that verifies the SSID
   3379 	 * implicitly. */
   3380 	if (wpa_s->sme.sae.state == SAE_ACCEPTED && wpa_s->sme.sae.h2e)
   3381 		wpa_s->ssid_verified = true;
   3382 #endif /* CONFIG_SME */
   3383 #endif /* CONFIG_SAE */
   3384 	bssid_known = wpa_drv_get_bssid(wpa_s, bssid) == 0;
   3385 	if (data->assoc_info.req_ies)
   3386 		wpa_hexdump(MSG_DEBUG, "req_ies", data->assoc_info.req_ies,
   3387 			    data->assoc_info.req_ies_len);
   3388 	if (data->assoc_info.resp_ies) {
   3389 		wpa_hexdump(MSG_DEBUG, "resp_ies", data->assoc_info.resp_ies,
   3390 			    data->assoc_info.resp_ies_len);
   3391 #ifdef CONFIG_TDLS
   3392 		wpa_tdls_assoc_resp_ies(wpa_s->wpa, data->assoc_info.resp_ies,
   3393 					data->assoc_info.resp_ies_len);
   3394 #endif /* CONFIG_TDLS */
   3395 #ifdef CONFIG_WNM
   3396 		wnm_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
   3397 				       data->assoc_info.resp_ies_len);
   3398 #endif /* CONFIG_WNM */
   3399 		interworking_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
   3400 						data->assoc_info.resp_ies_len);
   3401 		if (wpa_s->hw_capab == CAPAB_VHT &&
   3402 		    get_ie(data->assoc_info.resp_ies,
   3403 			   data->assoc_info.resp_ies_len, WLAN_EID_VHT_CAP))
   3404 			wpa_s->ieee80211ac = 1;
   3405 
   3406 		multi_ap_process_assoc_resp(wpa_s, data->assoc_info.resp_ies,
   3407 					    data->assoc_info.resp_ies_len);
   3408 	}
   3409 	if (data->assoc_info.beacon_ies)
   3410 		wpa_hexdump(MSG_DEBUG, "beacon_ies",
   3411 			    data->assoc_info.beacon_ies,
   3412 			    data->assoc_info.beacon_ies_len);
   3413 	if (data->assoc_info.freq)
   3414 		wpa_dbg(wpa_s, MSG_DEBUG, "freq=%u MHz",
   3415 			data->assoc_info.freq);
   3416 
   3417 	wpa_s->connection_set = 0;
   3418 	if (data->assoc_info.req_ies && data->assoc_info.resp_ies) {
   3419 		struct ieee802_11_elems req_elems, resp_elems;
   3420 
   3421 		if (ieee802_11_parse_elems(data->assoc_info.req_ies,
   3422 					   data->assoc_info.req_ies_len,
   3423 					   &req_elems, 0) != ParseFailed &&
   3424 		    ieee802_11_parse_elems(data->assoc_info.resp_ies,
   3425 					   data->assoc_info.resp_ies_len,
   3426 					   &resp_elems, 0) != ParseFailed) {
   3427 			wpa_s->connection_set = 1;
   3428 			wpa_s->connection_ht = req_elems.ht_capabilities &&
   3429 				resp_elems.ht_capabilities;
   3430 			/* Do not include subset of VHT on 2.4 GHz vendor
   3431 			 * extension in consideration for reporting VHT
   3432 			 * association. */
   3433 			wpa_s->connection_vht = req_elems.vht_capabilities &&
   3434 				resp_elems.vht_capabilities &&
   3435 				(!data->assoc_info.freq ||
   3436 				 wpas_freq_to_band(data->assoc_info.freq) !=
   3437 				 BAND_2_4_GHZ);
   3438 			wpa_s->connection_he = req_elems.he_capabilities &&
   3439 				resp_elems.he_capabilities;
   3440 			wpa_s->connection_eht = req_elems.eht_capabilities &&
   3441 				resp_elems.eht_capabilities;
   3442 			if (req_elems.rrm_enabled)
   3443 				wpa_s->rrm.rrm_used = 1;
   3444 		}
   3445 	}
   3446 
   3447 	p = data->assoc_info.req_ies;
   3448 	l = data->assoc_info.req_ies_len;
   3449 
   3450 	/* Go through the IEs and make a copy of the WPA/RSN IE, if present. */
   3451 	while (p && l >= 2) {
   3452 		len = p[1] + 2;
   3453 		if (len > l) {
   3454 			wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
   3455 				    p, l);
   3456 			break;
   3457 		}
   3458 		if (!found &&
   3459 		    ((p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
   3460 		      (os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0)) ||
   3461 		     (p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 4 &&
   3462 		      (os_memcmp(&p[2], "\x50\x6F\x9A\x12", 4) == 0)) ||
   3463 		     (p[0] == WLAN_EID_RSN && p[1] >= 2))) {
   3464 			if (wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, p, len))
   3465 				break;
   3466 			found = 1;
   3467 			wpa_find_assoc_pmkid(wpa_s,
   3468 					     data->assoc_info.authorized);
   3469 		}
   3470 		if (!found_x && p[0] == WLAN_EID_RSNX) {
   3471 			if (wpa_sm_set_assoc_rsnxe(wpa_s->wpa, p, len))
   3472 				break;
   3473 			found_x = 1;
   3474 		}
   3475 		l -= len;
   3476 		p += len;
   3477 	}
   3478 	if (!found && data->assoc_info.req_ies)
   3479 		wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
   3480 	if (!found_x && data->assoc_info.req_ies)
   3481 		wpa_sm_set_assoc_rsnxe(wpa_s->wpa, NULL, 0);
   3482 
   3483 #ifdef CONFIG_FILS
   3484 #ifdef CONFIG_SME
   3485 	if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FILS ||
   3486 	    wpa_s->sme.auth_alg == WPA_AUTH_ALG_FILS_SK_PFS) {
   3487 		if (!data->assoc_info.resp_frame ||
   3488 		    fils_process_assoc_resp(wpa_s->wpa,
   3489 					    data->assoc_info.resp_frame,
   3490 					    data->assoc_info.resp_frame_len) <
   3491 		    0) {
   3492 			wpa_supplicant_deauthenticate(wpa_s,
   3493 						      WLAN_REASON_UNSPECIFIED);
   3494 			return -1;
   3495 		}
   3496 
   3497 		/* FILS use of an AEAD cipher include the SSID element in
   3498 		 * (Re)Association Request frame in the AAD and since the AP
   3499 		 * accepted that, the SSID was verified. */
   3500 		wpa_s->ssid_verified = true;
   3501 	}
   3502 #endif /* CONFIG_SME */
   3503 
   3504 	/* Additional processing for FILS when SME is in driver */
   3505 	if (wpa_s->auth_alg == WPA_AUTH_ALG_FILS &&
   3506 	    !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
   3507 		wpa_sm_set_reset_fils_completed(wpa_s->wpa, 1);
   3508 #endif /* CONFIG_FILS */
   3509 
   3510 #ifdef CONFIG_OWE
   3511 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_OWE &&
   3512 	    !(wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_OWE_OFFLOAD_STA) &&
   3513 	    (!bssid_known ||
   3514 	     owe_process_assoc_resp(wpa_s->wpa,
   3515 				    wpa_s->valid_links ?
   3516 				    wpa_s->ap_mld_addr : bssid,
   3517 				    data->assoc_info.resp_ies,
   3518 				    data->assoc_info.resp_ies_len) < 0)) {
   3519 		wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_UNSPECIFIED);
   3520 		return -1;
   3521 	}
   3522 #endif /* CONFIG_OWE */
   3523 
   3524 #ifdef CONFIG_DPP2
   3525 	wpa_sm_set_dpp_z(wpa_s->wpa, NULL);
   3526 	if (DPP_VERSION > 1 && wpa_s->key_mgmt == WPA_KEY_MGMT_DPP &&
   3527 	    wpa_s->dpp_pfs) {
   3528 		struct ieee802_11_elems elems;
   3529 
   3530 		if (ieee802_11_parse_elems(data->assoc_info.resp_ies,
   3531 					   data->assoc_info.resp_ies_len,
   3532 					   &elems, 0) == ParseFailed ||
   3533 		    !elems.owe_dh)
   3534 			goto no_pfs;
   3535 		if (dpp_pfs_process(wpa_s->dpp_pfs, elems.owe_dh,
   3536 				    elems.owe_dh_len) < 0) {
   3537 			wpa_supplicant_deauthenticate(wpa_s,
   3538 						      WLAN_REASON_UNSPECIFIED);
   3539 			return -1;
   3540 		}
   3541 
   3542 		wpa_sm_set_dpp_z(wpa_s->wpa, wpa_s->dpp_pfs->secret);
   3543 	}
   3544 no_pfs:
   3545 #endif /* CONFIG_DPP2 */
   3546 
   3547 #ifdef CONFIG_IEEE80211R
   3548 #ifdef CONFIG_SME
   3549 	if (wpa_s->sme.auth_alg == WPA_AUTH_ALG_FT) {
   3550 		if (!bssid_known ||
   3551 		    wpa_ft_validate_reassoc_resp(wpa_s->wpa,
   3552 						 data->assoc_info.resp_ies,
   3553 						 data->assoc_info.resp_ies_len,
   3554 						 bssid) < 0) {
   3555 			wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
   3556 				"Reassociation Response failed");
   3557 			wpa_supplicant_deauthenticate(
   3558 				wpa_s, WLAN_REASON_INVALID_IE);
   3559 			return -1;
   3560 		}
   3561 		/* SSID is included in PMK-R0 derivation, so it is verified
   3562 		 * implicitly. */
   3563 		wpa_s->ssid_verified = true;
   3564 	}
   3565 
   3566 	p = data->assoc_info.resp_ies;
   3567 	l = data->assoc_info.resp_ies_len;
   3568 
   3569 #ifdef CONFIG_WPS_STRICT
   3570 	if (p && wpa_s->current_ssid &&
   3571 	    wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
   3572 		struct wpabuf *wps;
   3573 		wps = ieee802_11_vendor_ie_concat(p, l, WPS_IE_VENDOR_TYPE);
   3574 		if (wps == NULL) {
   3575 			wpa_msg(wpa_s, MSG_INFO, "WPS-STRICT: AP did not "
   3576 				"include WPS IE in (Re)Association Response");
   3577 			return -1;
   3578 		}
   3579 
   3580 		if (wps_validate_assoc_resp(wps) < 0) {
   3581 			wpabuf_free(wps);
   3582 			wpa_supplicant_deauthenticate(
   3583 				wpa_s, WLAN_REASON_INVALID_IE);
   3584 			return -1;
   3585 		}
   3586 		wpabuf_free(wps);
   3587 	}
   3588 #endif /* CONFIG_WPS_STRICT */
   3589 
   3590 	/* Go through the IEs and make a copy of the MDIE, if present. */
   3591 	while (p && l >= 2) {
   3592 		len = p[1] + 2;
   3593 		if (len > l) {
   3594 			wpa_hexdump(MSG_DEBUG, "Truncated IE in assoc_info",
   3595 				    p, l);
   3596 			break;
   3597 		}
   3598 		if (p[0] == WLAN_EID_MOBILITY_DOMAIN &&
   3599 		    p[1] >= MOBILITY_DOMAIN_ID_LEN) {
   3600 			wpa_s->sme.ft_used = 1;
   3601 			os_memcpy(wpa_s->sme.mobility_domain, p + 2,
   3602 				  MOBILITY_DOMAIN_ID_LEN);
   3603 			break;
   3604 		}
   3605 		l -= len;
   3606 		p += len;
   3607 	}
   3608 #endif /* CONFIG_SME */
   3609 
   3610 	/* Process FT when SME is in the driver */
   3611 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
   3612 	    wpa_ft_is_completed(wpa_s->wpa)) {
   3613 		if (!bssid_known ||
   3614 		    wpa_ft_validate_reassoc_resp(wpa_s->wpa,
   3615 						 data->assoc_info.resp_ies,
   3616 						 data->assoc_info.resp_ies_len,
   3617 						 bssid) < 0) {
   3618 			wpa_dbg(wpa_s, MSG_DEBUG, "FT: Validation of "
   3619 				"Reassociation Response failed");
   3620 			wpa_supplicant_deauthenticate(
   3621 				wpa_s, WLAN_REASON_INVALID_IE);
   3622 			return -1;
   3623 		}
   3624 		wpa_dbg(wpa_s, MSG_DEBUG, "FT: Reassociation Response done");
   3625 		/* SSID is included in PMK-R0 derivation, so it is verified
   3626 		 * implicitly. */
   3627 		wpa_s->ssid_verified = true;
   3628 	}
   3629 
   3630 	wpa_sm_set_ft_params(wpa_s->wpa, data->assoc_info.resp_ies,
   3631 			     data->assoc_info.resp_ies_len);
   3632 #endif /* CONFIG_IEEE80211R */
   3633 
   3634 #ifndef CONFIG_NO_ROBUST_AV
   3635 	if (bssid_known)
   3636 		wpas_handle_assoc_resp_mscs(wpa_s, bssid,
   3637 					    data->assoc_info.resp_ies,
   3638 					    data->assoc_info.resp_ies_len);
   3639 #endif /* CONFIG_NO_ROBUST_AV */
   3640 
   3641 	/* WPA/RSN IE from Beacon/ProbeResp */
   3642 	p = data->assoc_info.beacon_ies;
   3643 	l = data->assoc_info.beacon_ies_len;
   3644 
   3645 	/* Go through the IEs and make a copy of the WPA/RSN IEs, if present.
   3646 	 */
   3647 	wpa_found = rsn_found = 0;
   3648 	while (p && l >= 2) {
   3649 		len = p[1] + 2;
   3650 		if (len > l) {
   3651 			wpa_hexdump(MSG_DEBUG, "Truncated IE in beacon_ies",
   3652 				    p, l);
   3653 			break;
   3654 		}
   3655 		if (!wpa_found &&
   3656 		    p[0] == WLAN_EID_VENDOR_SPECIFIC && p[1] >= 6 &&
   3657 		    os_memcmp(&p[2], "\x00\x50\xF2\x01\x01\x00", 6) == 0) {
   3658 			wpa_found = 1;
   3659 			wpa_sm_set_ap_wpa_ie(wpa_s->wpa, p, len);
   3660 		}
   3661 
   3662 		if (!rsn_found &&
   3663 		    p[0] == WLAN_EID_RSN && p[1] >= 2) {
   3664 			rsn_found = 1;
   3665 			wpa_sm_set_ap_rsn_ie(wpa_s->wpa, p, len);
   3666 		}
   3667 
   3668 		if (p[0] == WLAN_EID_RSNX && p[1] >= 1)
   3669 			wpa_sm_set_ap_rsnxe(wpa_s->wpa, p, len);
   3670 
   3671 		l -= len;
   3672 		p += len;
   3673 	}
   3674 
   3675 	if (!wpa_found && data->assoc_info.beacon_ies)
   3676 		wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
   3677 	if (!rsn_found && data->assoc_info.beacon_ies) {
   3678 		wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
   3679 		wpa_sm_set_ap_rsnxe(wpa_s->wpa, NULL, 0);
   3680 	}
   3681 	if (wpa_found || rsn_found)
   3682 		wpa_s->ap_ies_from_associnfo = 1;
   3683 
   3684 	if (wpa_s->assoc_freq && data->assoc_info.freq) {
   3685 		struct wpa_bss *bss;
   3686 		unsigned int freq = 0;
   3687 
   3688 		if (bssid_known) {
   3689 			bss = wpa_bss_get_bssid_latest(wpa_s, bssid);
   3690 			if (bss)
   3691 				freq = bss->freq;
   3692 		}
   3693 		if (freq != data->assoc_info.freq) {
   3694 			wpa_printf(MSG_DEBUG,
   3695 				   "Operating frequency changed from %u to %u MHz",
   3696 				   wpa_s->assoc_freq, data->assoc_info.freq);
   3697 			wpa_supplicant_update_scan_results(wpa_s, bssid);
   3698 		}
   3699 	}
   3700 
   3701 	wpa_s->assoc_freq = data->assoc_info.freq;
   3702 
   3703 #ifndef CONFIG_NO_ROBUST_AV
   3704 	wpas_handle_assoc_resp_qos_mgmt(wpa_s, data->assoc_info.resp_ies,
   3705 					data->assoc_info.resp_ies_len);
   3706 #endif /* CONFIG_NO_ROBUST_AV */
   3707 
   3708 	return 0;
   3709 }
   3710 
   3711 
   3712 static int wpa_supplicant_assoc_update_ie(struct wpa_supplicant *wpa_s)
   3713 {
   3714 	const u8 *bss_wpa = NULL, *bss_rsn = NULL, *bss_rsnx = NULL;
   3715 
   3716 	if (!wpa_s->current_bss || !wpa_s->current_ssid)
   3717 		return -1;
   3718 
   3719 	if (!wpa_key_mgmt_wpa_any(wpa_s->current_ssid->key_mgmt))
   3720 		return 0;
   3721 
   3722 	bss_wpa = wpa_bss_get_vendor_ie(wpa_s->current_bss,
   3723 					WPA_IE_VENDOR_TYPE);
   3724 	bss_rsn = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSN);
   3725 	bss_rsnx = wpa_bss_get_ie(wpa_s->current_bss, WLAN_EID_RSNX);
   3726 
   3727 	if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
   3728 				 bss_wpa ? 2 + bss_wpa[1] : 0) ||
   3729 	    wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
   3730 				 bss_rsn ? 2 + bss_rsn[1] : 0) ||
   3731 	    wpa_sm_set_ap_rsnxe(wpa_s->wpa, bss_rsnx,
   3732 				 bss_rsnx ? 2 + bss_rsnx[1] : 0))
   3733 		return -1;
   3734 
   3735 	return 0;
   3736 }
   3737 
   3738 
   3739 static void wpas_fst_update_mb_assoc(struct wpa_supplicant *wpa_s,
   3740 				     union wpa_event_data *data)
   3741 {
   3742 #ifdef CONFIG_FST
   3743 	struct assoc_info *ai = data ? &data->assoc_info : NULL;
   3744 	struct wpa_bss *bss = wpa_s->current_bss;
   3745 	const u8 *ieprb, *iebcn;
   3746 
   3747 	wpabuf_free(wpa_s->received_mb_ies);
   3748 	wpa_s->received_mb_ies = NULL;
   3749 
   3750 	if (ai &&
   3751 	    !wpas_fst_update_mbie(wpa_s, ai->resp_ies, ai->resp_ies_len)) {
   3752 		wpa_printf(MSG_DEBUG,
   3753 			   "FST: MB IEs updated from Association Response frame");
   3754 		return;
   3755 	}
   3756 
   3757 	if (ai &&
   3758 	    !wpas_fst_update_mbie(wpa_s, ai->beacon_ies, ai->beacon_ies_len)) {
   3759 		wpa_printf(MSG_DEBUG,
   3760 			   "FST: MB IEs updated from association event Beacon IEs");
   3761 		return;
   3762 	}
   3763 
   3764 	if (!bss)
   3765 		return;
   3766 
   3767 	ieprb = wpa_bss_ie_ptr(bss);
   3768 	iebcn = ieprb + bss->ie_len;
   3769 
   3770 	if (!wpas_fst_update_mbie(wpa_s, ieprb, bss->ie_len))
   3771 		wpa_printf(MSG_DEBUG, "FST: MB IEs updated from bss IE");
   3772 	else if (!wpas_fst_update_mbie(wpa_s, iebcn, bss->beacon_ie_len))
   3773 		wpa_printf(MSG_DEBUG, "FST: MB IEs updated from bss beacon IE");
   3774 #endif /* CONFIG_FST */
   3775 }
   3776 
   3777 
   3778 static unsigned int wpas_ml_parse_assoc(struct wpa_supplicant *wpa_s,
   3779 					struct ieee802_11_elems *elems,
   3780 					struct ml_sta_link_info *ml_info)
   3781 {
   3782 	struct wpabuf *mlbuf;
   3783 	struct ieee80211_eht_ml *ml;
   3784 	size_t ml_len;
   3785 	struct eht_ml_basic_common_info *common_info;
   3786 	const u8 *pos;
   3787 	u16 eml_capa = 0, mld_capa = 0;
   3788 	const u16 control =
   3789 		host_to_le16(MULTI_LINK_CONTROL_TYPE_BASIC |
   3790 			     BASIC_MULTI_LINK_CTRL_PRES_LINK_ID |
   3791 			     BASIC_MULTI_LINK_CTRL_PRES_BSS_PARAM_CH_COUNT);
   3792 	u8 expected_common_info_len = 9;
   3793 	unsigned int i = 0;
   3794 	u16 ml_control;
   3795 
   3796 	if (!wpa_s->valid_links || !elems->basic_mle || !elems->basic_mle_len)
   3797 		return 0;
   3798 
   3799 	mlbuf = ieee802_11_defrag(elems->basic_mle, elems->basic_mle_len, true);
   3800 	if (!mlbuf)
   3801 		return 0;
   3802 
   3803 	ml = (struct ieee80211_eht_ml *) wpabuf_head(mlbuf);
   3804 	ml_len = wpabuf_len(mlbuf);
   3805 	if (ml_len < sizeof(*ml))
   3806 		goto out;
   3807 
   3808 	os_memset(ml_info, 0, sizeof(*ml_info) * MAX_NUM_MLD_LINKS);
   3809 
   3810 	ml_control = le_to_host16(ml->ml_control);
   3811 
   3812 	if ((ml_control & control) != control) {
   3813 		wpa_printf(MSG_DEBUG, "MLD: Invalid presence BM=0x%x",
   3814 			   ml_control);
   3815 		goto out;
   3816 	}
   3817 
   3818 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_EML_CAPA) {
   3819 		wpa_printf(MSG_DEBUG, "MLD: EML capabilities included");
   3820 		expected_common_info_len += 2;
   3821 	}
   3822 
   3823 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_MLD_CAPA) {
   3824 		wpa_printf(MSG_DEBUG, "MLD: MLD capabilities included");
   3825 		expected_common_info_len += 2;
   3826 	}
   3827 
   3828 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_MSD_INFO) {
   3829 		wpa_printf(MSG_DEBUG,
   3830 			   "MLD: Unexpected: medium sync delay info present");
   3831 		expected_common_info_len += 2;
   3832 	}
   3833 
   3834 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_AP_MLD_ID) {
   3835 		wpa_printf(MSG_DEBUG,
   3836 			   "MLD: Unexpected: MLD ID present");
   3837 		expected_common_info_len++;
   3838 	}
   3839 
   3840 	if (sizeof(*ml) + expected_common_info_len > ml_len) {
   3841 		wpa_printf(MSG_DEBUG,
   3842 			   "MLD: Not enough bytes for common info. ml_len=%zu",
   3843 			   ml_len);
   3844 		goto out;
   3845 	}
   3846 
   3847 	common_info = (struct eht_ml_basic_common_info *) ml->variable;
   3848 	if (common_info->len != expected_common_info_len) {
   3849 		wpa_printf(MSG_DEBUG,
   3850 			   "MLD: Invalid common info len=%u. expected=%u",
   3851 			   common_info->len, expected_common_info_len);
   3852 		goto out;
   3853 	}
   3854 
   3855 	wpa_printf(MSG_DEBUG, "MLD: address: " MACSTR,
   3856 		   MAC2STR(common_info->mld_addr));
   3857 
   3858 	if (!ether_addr_equal(wpa_s->ap_mld_addr, common_info->mld_addr)) {
   3859 		wpa_printf(MSG_DEBUG, "MLD: Mismatching MLD address (expected "
   3860 			   MACSTR ")", MAC2STR(wpa_s->ap_mld_addr));
   3861 		goto out;
   3862 	}
   3863 
   3864 	pos = common_info->variable;
   3865 
   3866 	/* Store the information for the association link */
   3867 	ml_info[i].link_id = *pos;
   3868 	pos++;
   3869 
   3870 	/* Skip the BSS Parameters Change Count */
   3871 	pos++;
   3872 
   3873 	/* Skip the Medium Synchronization Delay Information if present  */
   3874 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_MSD_INFO)
   3875 		pos += 2;
   3876 
   3877 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_EML_CAPA) {
   3878 		eml_capa = WPA_GET_LE16(pos);
   3879 		pos += 2;
   3880 	}
   3881 
   3882 	if (ml_control & BASIC_MULTI_LINK_CTRL_PRES_MLD_CAPA) {
   3883 		mld_capa = WPA_GET_LE16(pos);
   3884 		pos += 2;
   3885 	}
   3886 
   3887 	wpa_printf(MSG_DEBUG,
   3888 		   "MLD: link_id=%u, eml=0x%x, mld=0x%x",
   3889 		   ml_info[i].link_id, eml_capa, mld_capa);
   3890 
   3891 	i++;
   3892 
   3893 	pos = ((u8 *) common_info) + common_info->len;
   3894 	ml_len -= sizeof(*ml) + common_info->len;
   3895 	while (ml_len > 2 && i < MAX_NUM_MLD_LINKS) {
   3896 		u8 sub_elem_len = pos[1];
   3897 		u8 sta_info_len;
   3898 		u8 nstr_bitmap_len = 0;
   3899 		u16 ctrl;
   3900 		const u8 *end;
   3901 
   3902 		wpa_printf(MSG_DEBUG, "MLD: Subelement len=%u", sub_elem_len);
   3903 
   3904 		if (sub_elem_len > ml_len - 2) {
   3905 			wpa_printf(MSG_DEBUG,
   3906 				   "MLD: Invalid link info len: %u > %zu",
   3907 				   2 + sub_elem_len, ml_len);
   3908 			goto out;
   3909 		}
   3910 
   3911 		switch (*pos) {
   3912 		case EHT_ML_SUB_ELEM_PER_STA_PROFILE:
   3913 			break;
   3914 		case EHT_ML_SUB_ELEM_FRAGMENT:
   3915 		case EHT_ML_SUB_ELEM_VENDOR:
   3916 			wpa_printf(MSG_DEBUG,
   3917 				   "MLD: Skip subelement id=%u, len=%u",
   3918 				   *pos, sub_elem_len);
   3919 			pos += 2 + sub_elem_len;
   3920 			ml_len -= 2 + sub_elem_len;
   3921 			continue;
   3922 		default:
   3923 			wpa_printf(MSG_DEBUG, "MLD: Unknown subelement ID=%u",
   3924 				   *pos);
   3925 			goto out;
   3926 		}
   3927 
   3928 		end = pos + 2 + sub_elem_len;
   3929 
   3930 		/* Skip the subelement ID and the length */
   3931 		pos += 2;
   3932 		ml_len -= 2;
   3933 
   3934 		if (end - pos < 2)
   3935 			goto out;
   3936 
   3937 		/* Get the station control field */
   3938 		ctrl = WPA_GET_LE16(pos);
   3939 
   3940 		pos += 2;
   3941 		ml_len -= 2;
   3942 
   3943 		if (!(ctrl & EHT_PER_STA_CTRL_COMPLETE_PROFILE_MSK)) {
   3944 			wpa_printf(MSG_DEBUG,
   3945 				   "MLD: Per STA complete profile expected");
   3946 			goto out;
   3947 		}
   3948 
   3949 		if (!(ctrl & EHT_PER_STA_CTRL_MAC_ADDR_PRESENT_MSK)) {
   3950 			wpa_printf(MSG_DEBUG,
   3951 				   "MLD: Per STA MAC address not present");
   3952 			goto out;
   3953 		}
   3954 
   3955 		if (!(ctrl & EHT_PER_STA_CTRL_TSF_OFFSET_PRESENT_MSK)) {
   3956 			wpa_printf(MSG_DEBUG,
   3957 				   "MLD: Per STA TSF offset not present");
   3958 			goto out;
   3959 		}
   3960 
   3961 		if (!(ctrl & EHT_PER_STA_CTRL_BEACON_INTERVAL_PRESENT_MSK)) {
   3962 			wpa_printf(MSG_DEBUG,
   3963 				   "MLD: Beacon interval not present");
   3964 			goto out;
   3965 		}
   3966 
   3967 		if (!(ctrl & EHT_PER_STA_CTRL_DTIM_INFO_PRESENT_MSK)) {
   3968 			wpa_printf(MSG_DEBUG,
   3969 				   "MLD:  DTIM information not present");
   3970 			goto out;
   3971 		}
   3972 
   3973 		if (ctrl & EHT_PER_STA_CTRL_NSTR_LINK_PAIR_PRESENT_MSK) {
   3974 			if (ctrl & EHT_PER_STA_CTRL_NSTR_BM_SIZE_MSK)
   3975 				nstr_bitmap_len = 2;
   3976 			else
   3977 				nstr_bitmap_len = 1;
   3978 		}
   3979 
   3980 		if (!(ctrl & EHT_PER_STA_CTRL_BSS_PARAM_CNT_PRESENT_MSK)) {
   3981 			wpa_printf(MSG_DEBUG,
   3982 				   "MLD:  BSS params change count not present");
   3983 			goto out;
   3984 		}
   3985 
   3986 		sta_info_len = 1 + ETH_ALEN + 8 + 2 + 2 + 1 + nstr_bitmap_len;
   3987 		if (sta_info_len > ml_len || sta_info_len > end - pos ||
   3988 		    sta_info_len + 2 > sub_elem_len ||
   3989 		    sta_info_len != *pos) {
   3990 			wpa_printf(MSG_DEBUG,
   3991 				   "MLD: Invalid STA info len=%u, len=%u",
   3992 				   sta_info_len, *pos);
   3993 			goto out;
   3994 		}
   3995 
   3996 		/* Get the link address */
   3997 		wpa_printf(MSG_DEBUG,
   3998 			   "MLD: link addr: " MACSTR " nstr BM len=%u",
   3999 			   MAC2STR(pos + 1), nstr_bitmap_len);
   4000 
   4001 		ml_info[i].link_id = ctrl & EHT_PER_STA_CTRL_LINK_ID_MSK;
   4002 		os_memcpy(ml_info[i].bssid, pos + 1, ETH_ALEN);
   4003 
   4004 		pos += sta_info_len;
   4005 		ml_len -= sta_info_len;
   4006 
   4007 		wpa_printf(MSG_DEBUG, "MLD: sub_elem_len=%u, sta_info_len=%u",
   4008 			   sub_elem_len, sta_info_len);
   4009 
   4010 		sub_elem_len -= sta_info_len + 2;
   4011 		if (sub_elem_len < 4) {
   4012 			wpa_printf(MSG_DEBUG, "MLD: Per STA profile too short");
   4013 			goto out;
   4014 		}
   4015 
   4016 		wpa_hexdump(MSG_MSGDUMP, "MLD: STA profile", pos, sub_elem_len);
   4017 		ml_info[i].status = WPA_GET_LE16(pos + 2);
   4018 
   4019 		pos += sub_elem_len;
   4020 		ml_len -= sub_elem_len;
   4021 
   4022 		i++;
   4023 	}
   4024 
   4025 	wpabuf_free(mlbuf);
   4026 	return i;
   4027 out:
   4028 	wpabuf_free(mlbuf);
   4029 	return 0;
   4030 }
   4031 
   4032 
   4033 static int wpa_drv_get_mlo_info(struct wpa_supplicant *wpa_s)
   4034 {
   4035 	struct driver_sta_mlo_info mlo;
   4036 	int i;
   4037 
   4038 	os_memset(&mlo, 0, sizeof(mlo));
   4039 	if (wpas_drv_get_sta_mlo_info(wpa_s, &mlo)) {
   4040 		wpa_dbg(wpa_s, MSG_ERROR, "Failed to get MLO link info");
   4041 		wpa_supplicant_deauthenticate(wpa_s,
   4042 					      WLAN_REASON_DEAUTH_LEAVING);
   4043 		return -1;
   4044 	}
   4045 
   4046 	if (wpa_s->valid_links == mlo.valid_links) {
   4047 		bool match = true;
   4048 
   4049 		if (!mlo.valid_links)
   4050 			return 0;
   4051 
   4052 		for_each_link(mlo.valid_links, i) {
   4053 			if (!ether_addr_equal(wpa_s->links[i].addr,
   4054 					      mlo.links[i].addr) ||
   4055 			    !ether_addr_equal(wpa_s->links[i].bssid,
   4056 					      mlo.links[i].bssid)) {
   4057 				match = false;
   4058 				break;
   4059 			}
   4060 		}
   4061 
   4062 		if (match && wpa_s->mlo_assoc_link_id == mlo.assoc_link_id &&
   4063 		    ether_addr_equal(wpa_s->ap_mld_addr, mlo.ap_mld_addr))
   4064 			return 0;
   4065 	}
   4066 
   4067 	wpa_s->valid_links = mlo.valid_links;
   4068 	wpa_s->mlo_assoc_link_id = mlo.assoc_link_id;
   4069 	os_memcpy(wpa_s->ap_mld_addr, mlo.ap_mld_addr, ETH_ALEN);
   4070 	for_each_link(wpa_s->valid_links, i) {
   4071 		os_memcpy(wpa_s->links[i].addr, mlo.links[i].addr, ETH_ALEN);
   4072 		os_memcpy(wpa_s->links[i].bssid, mlo.links[i].bssid, ETH_ALEN);
   4073 		wpa_s->links[i].freq = mlo.links[i].freq;
   4074 		wpa_supplicant_update_link_bss(wpa_s, i, mlo.links[i].bssid);
   4075 	}
   4076 
   4077 	return 0;
   4078 }
   4079 
   4080 
   4081 static int wpa_sm_set_ml_info(struct wpa_supplicant *wpa_s)
   4082 {
   4083 	struct driver_sta_mlo_info drv_mlo;
   4084 	struct wpa_sm_mlo wpa_mlo;
   4085 	const u8 *bss_rsn = NULL, *bss_rsnx = NULL;
   4086 	int i;
   4087 
   4088 	os_memset(&drv_mlo, 0, sizeof(drv_mlo));
   4089 	if (wpas_drv_get_sta_mlo_info(wpa_s, &drv_mlo)) {
   4090 		wpa_dbg(wpa_s, MSG_INFO, "Failed to get MLO link info");
   4091 		return -1;
   4092 	}
   4093 
   4094 	os_memset(&wpa_mlo, 0, sizeof(wpa_mlo));
   4095 	if (!drv_mlo.valid_links)
   4096 		goto out;
   4097 
   4098 	os_memcpy(wpa_mlo.ap_mld_addr, drv_mlo.ap_mld_addr, ETH_ALEN);
   4099 	wpa_mlo.assoc_link_id = drv_mlo.assoc_link_id;
   4100 	wpa_mlo.valid_links = drv_mlo.valid_links;
   4101 	wpa_mlo.req_links = drv_mlo.req_links;
   4102 
   4103 	for_each_link(drv_mlo.req_links, i) {
   4104 		struct wpa_bss *bss;
   4105 
   4106 		bss = wpa_supplicant_get_new_bss(wpa_s, drv_mlo.links[i].bssid);
   4107 		if (!bss) {
   4108 			wpa_dbg(wpa_s, MSG_INFO,
   4109 				"Failed to get MLO link %d BSS", i);
   4110 			return -1;
   4111 		}
   4112 
   4113 		bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
   4114 		bss_rsnx = wpa_bss_get_ie(bss, WLAN_EID_RSNX);
   4115 
   4116 		wpa_mlo.links[i].ap_rsne = bss_rsn ? (u8 *) bss_rsn : NULL;
   4117 		wpa_mlo.links[i].ap_rsne_len = bss_rsn ? 2 + bss_rsn[1] : 0;
   4118 		wpa_mlo.links[i].ap_rsnxe = bss_rsnx ? (u8 *) bss_rsnx : NULL;
   4119 		wpa_mlo.links[i].ap_rsnxe_len = bss_rsnx ? 2 + bss_rsnx[1] : 0;
   4120 
   4121 		os_memcpy(wpa_mlo.links[i].bssid, drv_mlo.links[i].bssid,
   4122 			  ETH_ALEN);
   4123 		os_memcpy(wpa_mlo.links[i].addr, drv_mlo.links[i].addr,
   4124 			  ETH_ALEN);
   4125 	}
   4126 
   4127 out:
   4128 	return wpa_sm_set_mlo_params(wpa_s->wpa, &wpa_mlo);
   4129 }
   4130 
   4131 
   4132 static void wpa_supplicant_event_assoc(struct wpa_supplicant *wpa_s,
   4133 				       union wpa_event_data *data)
   4134 {
   4135 	u8 bssid[ETH_ALEN];
   4136 	int ft_completed, already_authorized;
   4137 	int new_bss = 0;
   4138 #if defined(CONFIG_FILS) || defined(CONFIG_MBO)
   4139 	struct wpa_bss *bss;
   4140 #endif /* CONFIG_FILS || CONFIG_MBO */
   4141 
   4142 #ifdef CONFIG_AP
   4143 	if (wpa_s->ap_iface) {
   4144 		if (!data)
   4145 			return;
   4146 		hostapd_notif_assoc(wpa_s->ap_iface->bss[0],
   4147 				    data->assoc_info.addr,
   4148 				    data->assoc_info.req_ies,
   4149 				    data->assoc_info.req_ies_len, NULL, 0,
   4150 				    NULL, data->assoc_info.reassoc);
   4151 		return;
   4152 	}
   4153 #endif /* CONFIG_AP */
   4154 
   4155 	eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
   4156 	wpa_s->own_reconnect_req = 0;
   4157 
   4158 	ft_completed = wpa_ft_is_completed(wpa_s->wpa);
   4159 
   4160 	if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
   4161 		wpa_dbg(wpa_s, MSG_ERROR, "Failed to get BSSID");
   4162 		wpa_supplicant_deauthenticate(
   4163 			wpa_s, WLAN_REASON_DEAUTH_LEAVING);
   4164 		return;
   4165 	}
   4166 
   4167 	if (wpa_drv_get_mlo_info(wpa_s) < 0) {
   4168 		wpa_dbg(wpa_s, MSG_ERROR, "Failed to get MLO connection info");
   4169 		wpa_supplicant_deauthenticate(wpa_s,
   4170 					      WLAN_REASON_DEAUTH_LEAVING);
   4171 		return;
   4172 	}
   4173 
   4174 	if (ft_completed &&
   4175 	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION)) {
   4176 		wpa_msg(wpa_s, MSG_INFO, "Attempt to roam to " MACSTR,
   4177 			MAC2STR(bssid));
   4178 		if (!wpa_supplicant_update_current_bss(wpa_s, bssid)) {
   4179 			wpa_printf(MSG_ERROR,
   4180 				   "Can't find target AP's information!");
   4181 			return;
   4182 		}
   4183 		wpa_supplicant_assoc_update_ie(wpa_s);
   4184 	}
   4185 
   4186 	if (data && wpa_supplicant_event_associnfo(wpa_s, data) < 0)
   4187 		return;
   4188 	/*
   4189 	 * FILS authentication can share the same mechanism to mark the
   4190 	 * connection fully authenticated, so set ft_completed also based on
   4191 	 * FILS result.
   4192 	 */
   4193 	if (!ft_completed)
   4194 		ft_completed = wpa_fils_is_completed(wpa_s->wpa);
   4195 
   4196 	wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATED);
   4197 	if (!ether_addr_equal(bssid, wpa_s->bssid)) {
   4198 		if (os_reltime_initialized(&wpa_s->session_start)) {
   4199 			os_reltime_age(&wpa_s->session_start,
   4200 				       &wpa_s->session_length);
   4201 			wpa_s->session_start.sec = 0;
   4202 			wpa_s->session_start.usec = 0;
   4203 			wpas_notify_session_length(wpa_s);
   4204 		} else {
   4205 			wpas_notify_auth_changed(wpa_s);
   4206 			os_get_reltime(&wpa_s->session_start);
   4207 		}
   4208 		wpa_dbg(wpa_s, MSG_DEBUG, "Associated to a new BSS: BSSID="
   4209 			MACSTR, MAC2STR(bssid));
   4210 		new_bss = 1;
   4211 		random_add_randomness(bssid, ETH_ALEN);
   4212 		os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
   4213 		os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
   4214 		wpas_notify_bssid_changed(wpa_s);
   4215 
   4216 		if (wpa_supplicant_dynamic_keys(wpa_s) && !ft_completed) {
   4217 			wpa_clear_keys(wpa_s, bssid);
   4218 		}
   4219 		if (wpa_supplicant_select_config(wpa_s, data) < 0) {
   4220 			wpa_supplicant_deauthenticate(
   4221 				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
   4222 			return;
   4223 		}
   4224 	}
   4225 
   4226 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
   4227 	    data && wpa_supplicant_use_own_rsne_params(wpa_s, data) < 0)
   4228 		return;
   4229 
   4230 	multi_ap_set_4addr_mode(wpa_s);
   4231 
   4232 	if (wpa_s->conf->ap_scan == 1 &&
   4233 	    wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION) {
   4234 		if (wpa_supplicant_assoc_update_ie(wpa_s) < 0 && new_bss)
   4235 			wpa_msg(wpa_s, MSG_WARNING,
   4236 				"WPA/RSN IEs not updated");
   4237 	}
   4238 
   4239 	wpas_fst_update_mb_assoc(wpa_s, data);
   4240 
   4241 #ifdef CONFIG_SME
   4242 	/*
   4243 	 * Cache the current AP's BSSID (for non-MLO connection) or MLD address
   4244 	 * (for MLO connection) as the previous BSSID for subsequent
   4245 	 * reassociation requests handled by SME-in-wpa_supplicant.
   4246 	 */
   4247 	os_memcpy(wpa_s->sme.prev_bssid,
   4248 		  wpa_s->valid_links ? wpa_s->ap_mld_addr : bssid, ETH_ALEN);
   4249 	wpa_s->sme.prev_bssid_set = 1;
   4250 	wpa_s->sme.last_unprot_disconnect.sec = 0;
   4251 #endif /* CONFIG_SME */
   4252 
   4253 	wpa_msg(wpa_s, MSG_INFO, "Associated with " MACSTR, MAC2STR(bssid));
   4254 	if (wpa_s->current_ssid) {
   4255 		/* When using scanning (ap_scan=1), SIM PC/SC interface can be
   4256 		 * initialized before association, but for other modes,
   4257 		 * initialize PC/SC here, if the current configuration needs
   4258 		 * smartcard or SIM/USIM. */
   4259 		wpa_supplicant_scard_init(wpa_s, wpa_s->current_ssid);
   4260 	}
   4261 	wpa_sm_notify_assoc(wpa_s->wpa, bssid);
   4262 
   4263 	if (wpa_sm_set_ml_info(wpa_s)) {
   4264 		wpa_dbg(wpa_s, MSG_INFO,
   4265 			"Failed to set MLO connection info to wpa_sm");
   4266 		wpa_supplicant_deauthenticate(wpa_s,
   4267 					      WLAN_REASON_DEAUTH_LEAVING);
   4268 		return;
   4269 	}
   4270 
   4271 	if (wpa_s->l2)
   4272 		l2_packet_notify_auth_start(wpa_s->l2);
   4273 
   4274 	already_authorized = data && data->assoc_info.authorized;
   4275 
   4276 	/*
   4277 	 * Set portEnabled first to false in order to get EAP state machine out
   4278 	 * of the SUCCESS state and eapSuccess cleared. Without this, EAPOL PAE
   4279 	 * state machine may transit to AUTHENTICATING state based on obsolete
   4280 	 * eapSuccess and then trigger BE_AUTH to SUCCESS and PAE to
   4281 	 * AUTHENTICATED without ever giving chance to EAP state machine to
   4282 	 * reset the state.
   4283 	 */
   4284 	if (!ft_completed && !already_authorized) {
   4285 		eapol_sm_notify_portEnabled(wpa_s->eapol, false);
   4286 		eapol_sm_notify_portValid(wpa_s->eapol, false);
   4287 	}
   4288 	if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
   4289 	    wpa_s->key_mgmt == WPA_KEY_MGMT_DPP ||
   4290 	    wpa_s->key_mgmt == WPA_KEY_MGMT_OWE || ft_completed ||
   4291 	    already_authorized || wpa_s->drv_authorized_port)
   4292 		eapol_sm_notify_eap_success(wpa_s->eapol, false);
   4293 	/* 802.1X::portControl = Auto */
   4294 	eapol_sm_notify_portEnabled(wpa_s->eapol, true);
   4295 	wpa_s->eapol_received = 0;
   4296 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
   4297 	    wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE ||
   4298 	    (wpa_s->current_ssid &&
   4299 	     wpa_s->current_ssid->mode == WPAS_MODE_IBSS)) {
   4300 		if (wpa_s->current_ssid &&
   4301 		    wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE &&
   4302 		    (wpa_s->drv_flags &
   4303 		     WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
   4304 			/*
   4305 			 * Set the key after having received joined-IBSS event
   4306 			 * from the driver.
   4307 			 */
   4308 			wpa_supplicant_set_wpa_none_key(wpa_s,
   4309 							wpa_s->current_ssid);
   4310 		}
   4311 		wpa_supplicant_cancel_auth_timeout(wpa_s);
   4312 		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
   4313 	} else if (!ft_completed) {
   4314 		/* Timeout for receiving the first EAPOL packet */
   4315 		wpa_supplicant_req_auth_timeout(wpa_s, 10, 0);
   4316 	}
   4317 	wpa_supplicant_cancel_scan(wpa_s);
   4318 
   4319 	if (ft_completed) {
   4320 		/*
   4321 		 * FT protocol completed - make sure EAPOL state machine ends
   4322 		 * up in authenticated.
   4323 		 */
   4324 		wpa_supplicant_cancel_auth_timeout(wpa_s);
   4325 		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
   4326 		eapol_sm_notify_portValid(wpa_s->eapol, true);
   4327 		eapol_sm_notify_eap_success(wpa_s->eapol, true);
   4328 	} else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_PSK) &&
   4329 		   wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
   4330 		if (already_authorized) {
   4331 			/*
   4332 			 * We are done; the driver will take care of RSN 4-way
   4333 			 * handshake.
   4334 			 */
   4335 			wpa_supplicant_cancel_auth_timeout(wpa_s);
   4336 			wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
   4337 			eapol_sm_notify_portValid(wpa_s->eapol, true);
   4338 			eapol_sm_notify_eap_success(wpa_s->eapol, true);
   4339 		} else {
   4340 			/* Update port, WPA_COMPLETED state from the
   4341 			 * EVENT_PORT_AUTHORIZED handler when the driver is done
   4342 			 * with the 4-way handshake.
   4343 			 */
   4344 			wpa_msg(wpa_s, MSG_DEBUG,
   4345 				"ASSOC INFO: wait for driver port authorized indication");
   4346 		}
   4347 	} else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X) &&
   4348 		   wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
   4349 		/*
   4350 		 * The driver will take care of RSN 4-way handshake, so we need
   4351 		 * to allow EAPOL supplicant to complete its work without
   4352 		 * waiting for WPA supplicant.
   4353 		 */
   4354 		eapol_sm_notify_portValid(wpa_s->eapol, true);
   4355 	}
   4356 
   4357 	wpa_s->last_eapol_matches_bssid = 0;
   4358 
   4359 #ifdef CONFIG_TESTING_OPTIONS
   4360 	if (wpa_s->rsne_override_eapol) {
   4361 		wpa_printf(MSG_DEBUG,
   4362 			   "TESTING: RSNE EAPOL-Key msg 2/4 override");
   4363 		wpa_sm_set_assoc_wpa_ie(wpa_s->wpa,
   4364 					wpabuf_head(wpa_s->rsne_override_eapol),
   4365 					wpabuf_len(wpa_s->rsne_override_eapol));
   4366 	}
   4367 	if (wpa_s->rsnxe_override_eapol) {
   4368 		wpa_printf(MSG_DEBUG,
   4369 			   "TESTING: RSNXE EAPOL-Key msg 2/4 override");
   4370 		wpa_sm_set_assoc_rsnxe(wpa_s->wpa,
   4371 				       wpabuf_head(wpa_s->rsnxe_override_eapol),
   4372 				       wpabuf_len(wpa_s->rsnxe_override_eapol));
   4373 	}
   4374 #endif /* CONFIG_TESTING_OPTIONS */
   4375 
   4376 	if (wpa_s->pending_eapol_rx) {
   4377 		struct os_reltime now, age;
   4378 		os_get_reltime(&now);
   4379 		os_reltime_sub(&now, &wpa_s->pending_eapol_rx_time, &age);
   4380 		if (age.sec == 0 && age.usec < 200000 &&
   4381 		    ether_addr_equal(wpa_s->pending_eapol_rx_src,
   4382 				     wpa_s->valid_links ? wpa_s->ap_mld_addr :
   4383 				     bssid)) {
   4384 			wpa_dbg(wpa_s, MSG_DEBUG, "Process pending EAPOL "
   4385 				"frame that was received just before "
   4386 				"association notification");
   4387 			wpa_supplicant_rx_eapol(
   4388 				wpa_s, wpa_s->pending_eapol_rx_src,
   4389 				wpabuf_head(wpa_s->pending_eapol_rx),
   4390 				wpabuf_len(wpa_s->pending_eapol_rx),
   4391 				wpa_s->pending_eapol_encrypted);
   4392 		}
   4393 		wpabuf_free(wpa_s->pending_eapol_rx);
   4394 		wpa_s->pending_eapol_rx = NULL;
   4395 	}
   4396 
   4397 #ifdef CONFIG_WEP
   4398 	if ((wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
   4399 	     wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) &&
   4400 	    wpa_s->current_ssid &&
   4401 	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC_DONE)) {
   4402 		/* Set static WEP keys again */
   4403 		wpa_set_wep_keys(wpa_s, wpa_s->current_ssid);
   4404 	}
   4405 #endif /* CONFIG_WEP */
   4406 
   4407 #ifdef CONFIG_IBSS_RSN
   4408 	if (wpa_s->current_ssid &&
   4409 	    wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
   4410 	    wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
   4411 	    wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE &&
   4412 	    wpa_s->ibss_rsn == NULL) {
   4413 		wpa_s->ibss_rsn = ibss_rsn_init(wpa_s, wpa_s->current_ssid);
   4414 		if (!wpa_s->ibss_rsn) {
   4415 			wpa_msg(wpa_s, MSG_INFO, "Failed to init IBSS RSN");
   4416 			wpa_supplicant_deauthenticate(
   4417 				wpa_s, WLAN_REASON_DEAUTH_LEAVING);
   4418 			return;
   4419 		}
   4420 
   4421 		ibss_rsn_set_psk(wpa_s->ibss_rsn, wpa_s->current_ssid->psk);
   4422 	}
   4423 #endif /* CONFIG_IBSS_RSN */
   4424 
   4425 	wpas_wps_notify_assoc(wpa_s, bssid);
   4426 
   4427 #ifndef CONFIG_NO_WMM_AC
   4428 	if (data) {
   4429 		wmm_ac_notify_assoc(wpa_s, data->assoc_info.resp_ies,
   4430 				    data->assoc_info.resp_ies_len,
   4431 				    &data->assoc_info.wmm_params);
   4432 
   4433 		if (wpa_s->reassoc_same_bss)
   4434 			wmm_ac_restore_tspecs(wpa_s);
   4435 	}
   4436 #endif /* CONFIG_NO_WMM_AC */
   4437 
   4438 #if defined(CONFIG_FILS) || defined(CONFIG_MBO)
   4439 	bss = wpa_bss_get_bssid(wpa_s, bssid);
   4440 #endif /* CONFIG_FILS || CONFIG_MBO */
   4441 #ifdef CONFIG_FILS
   4442 	if (wpa_key_mgmt_fils(wpa_s->key_mgmt)) {
   4443 		const u8 *fils_cache_id = wpa_bss_get_fils_cache_id(bss);
   4444 
   4445 		if (fils_cache_id)
   4446 			wpa_sm_set_fils_cache_id(wpa_s->wpa, fils_cache_id);
   4447 	}
   4448 #endif /* CONFIG_FILS */
   4449 
   4450 #ifdef CONFIG_MBO
   4451 	wpas_mbo_check_pmf(wpa_s, bss, wpa_s->current_ssid);
   4452 #endif /* CONFIG_MBO */
   4453 
   4454 #ifdef CONFIG_DPP2
   4455 	wpa_s->dpp_pfs_fallback = 0;
   4456 #endif /* CONFIG_DPP2 */
   4457 
   4458 	if (wpa_s->current_ssid && wpa_s->current_ssid->enable_4addr_mode)
   4459 		wpa_supplicant_set_4addr_mode(wpa_s);
   4460 }
   4461 
   4462 
   4463 static int disconnect_reason_recoverable(u16 reason_code)
   4464 {
   4465 	return reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY ||
   4466 		reason_code == WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA ||
   4467 		reason_code == WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA;
   4468 }
   4469 
   4470 
   4471 static void wpa_supplicant_event_disassoc(struct wpa_supplicant *wpa_s,
   4472 					  u16 reason_code,
   4473 					  int locally_generated)
   4474 {
   4475 	const u8 *bssid;
   4476 
   4477 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
   4478 		/*
   4479 		 * At least Host AP driver and a Prism3 card seemed to be
   4480 		 * generating streams of disconnected events when configuring
   4481 		 * IBSS for WPA-None. Ignore them for now.
   4482 		 */
   4483 		return;
   4484 	}
   4485 
   4486 	bssid = wpa_s->bssid;
   4487 	if (is_zero_ether_addr(bssid))
   4488 		bssid = wpa_s->pending_bssid;
   4489 
   4490 	if (!is_zero_ether_addr(bssid) ||
   4491 	    wpa_s->wpa_state >= WPA_AUTHENTICATING) {
   4492 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_DISCONNECTED "bssid=" MACSTR
   4493 			" reason=%d%s",
   4494 			MAC2STR(bssid), reason_code,
   4495 			locally_generated ? " locally_generated=1" : "");
   4496 	}
   4497 }
   4498 
   4499 
   4500 static int could_be_psk_mismatch(struct wpa_supplicant *wpa_s, u16 reason_code,
   4501 				 int locally_generated)
   4502 {
   4503 	if (wpa_s->wpa_state != WPA_4WAY_HANDSHAKE ||
   4504 	    !wpa_s->new_connection ||
   4505 	    !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
   4506 	    wpa_key_mgmt_sae(wpa_s->key_mgmt))
   4507 		return 0; /* Not in initial 4-way handshake with PSK */
   4508 
   4509 	/*
   4510 	 * It looks like connection was lost while trying to go through PSK
   4511 	 * 4-way handshake. Filter out known disconnection cases that are caused
   4512 	 * by something else than PSK mismatch to avoid confusing reports.
   4513 	 */
   4514 
   4515 	if (locally_generated) {
   4516 		if (reason_code == WLAN_REASON_IE_IN_4WAY_DIFFERS)
   4517 			return 0;
   4518 	}
   4519 
   4520 	return 1;
   4521 }
   4522 
   4523 
   4524 static void wpa_supplicant_event_disassoc_finish(struct wpa_supplicant *wpa_s,
   4525 						 u16 reason_code,
   4526 						 int locally_generated)
   4527 {
   4528 	const u8 *bssid;
   4529 	struct wpa_bss *fast_reconnect = NULL;
   4530 	struct wpa_ssid *fast_reconnect_ssid = NULL;
   4531 	struct wpa_bss *curr = NULL;
   4532 
   4533 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
   4534 		/*
   4535 		 * At least Host AP driver and a Prism3 card seemed to be
   4536 		 * generating streams of disconnected events when configuring
   4537 		 * IBSS for WPA-None. Ignore them for now.
   4538 		 */
   4539 		wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - ignore in "
   4540 			"IBSS/WPA-None mode");
   4541 		return;
   4542 	}
   4543 
   4544 	if (!wpa_s->disconnected && wpa_s->wpa_state >= WPA_AUTHENTICATING &&
   4545 	    reason_code == WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY &&
   4546 	    locally_generated)
   4547 		/*
   4548 		 * Remove the inactive AP (which is probably out of range) from
   4549 		 * the BSS list after marking disassociation. In particular
   4550 		 * mac80211-based drivers use the
   4551 		 * WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY reason code in
   4552 		 * locally generated disconnection events for cases where the
   4553 		 * AP does not reply anymore.
   4554 		 */
   4555 		curr = wpa_s->current_bss;
   4556 
   4557 	if (could_be_psk_mismatch(wpa_s, reason_code, locally_generated)) {
   4558 		wpa_msg(wpa_s, MSG_INFO, "WPA: 4-Way Handshake failed - "
   4559 			"pre-shared key may be incorrect");
   4560 		if (wpas_p2p_4way_hs_failed(wpa_s) > 0)
   4561 			return; /* P2P group removed */
   4562 		wpas_auth_failed(wpa_s, "WRONG_KEY", wpa_s->pending_bssid);
   4563 		wpas_notify_psk_mismatch(wpa_s);
   4564 #ifdef CONFIG_DPP2
   4565 		wpas_dpp_send_conn_status_result(wpa_s,
   4566 						 DPP_STATUS_AUTH_FAILURE);
   4567 #endif /* CONFIG_DPP2 */
   4568 	}
   4569 	if (!wpa_s->disconnected &&
   4570 	    (!wpa_s->auto_reconnect_disabled ||
   4571 	     wpa_s->key_mgmt == WPA_KEY_MGMT_WPS ||
   4572 	     wpas_wps_searching(wpa_s) ||
   4573 	     wpas_wps_reenable_networks_pending(wpa_s))) {
   4574 		wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect enabled: try to "
   4575 			"reconnect (wps=%d/%d wpa_state=%d)",
   4576 			wpa_s->key_mgmt == WPA_KEY_MGMT_WPS,
   4577 			wpas_wps_searching(wpa_s),
   4578 			wpa_s->wpa_state);
   4579 		if (wpa_s->wpa_state == WPA_COMPLETED &&
   4580 		    wpa_s->current_ssid &&
   4581 		    wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
   4582 		    (wpa_s->own_reconnect_req ||
   4583 		     (!locally_generated &&
   4584 		      disconnect_reason_recoverable(reason_code)))) {
   4585 			/*
   4586 			 * It looks like the AP has dropped association with
   4587 			 * us, but could allow us to get back in. This is also
   4588 			 * triggered for cases where local reconnection request
   4589 			 * is used to force reassociation with the same BSS.
   4590 			 * Try to reconnect to the same BSS without a full scan
   4591 			 * to save time for some common cases.
   4592 			 */
   4593 			fast_reconnect = wpa_s->current_bss;
   4594 			fast_reconnect_ssid = wpa_s->current_ssid;
   4595 		} else if (wpa_s->wpa_state >= WPA_ASSOCIATING) {
   4596 			wpa_supplicant_req_scan(wpa_s, 0, 100000);
   4597 		} else {
   4598 			wpa_dbg(wpa_s, MSG_DEBUG, "Do not request new "
   4599 				"immediate scan");
   4600 		}
   4601 	} else {
   4602 		wpa_dbg(wpa_s, MSG_DEBUG, "Auto connect disabled: do not "
   4603 			"try to re-connect");
   4604 		wpa_s->reassociate = 0;
   4605 		wpa_s->disconnected = 1;
   4606 		if (!wpa_s->pno)
   4607 			wpa_supplicant_cancel_sched_scan(wpa_s);
   4608 	}
   4609 	bssid = wpa_s->bssid;
   4610 	if (is_zero_ether_addr(bssid))
   4611 		bssid = wpa_s->pending_bssid;
   4612 	if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
   4613 		wpas_connection_failed(wpa_s, bssid, NULL);
   4614 	wpa_sm_notify_disassoc(wpa_s->wpa);
   4615 	ptksa_cache_flush(wpa_s->ptksa, wpa_s->bssid, WPA_CIPHER_NONE);
   4616 
   4617 	if (locally_generated)
   4618 		wpa_s->disconnect_reason = -reason_code;
   4619 	else
   4620 		wpa_s->disconnect_reason = reason_code;
   4621 	wpas_notify_disconnect_reason(wpa_s);
   4622 	if (wpa_supplicant_dynamic_keys(wpa_s)) {
   4623 		wpa_dbg(wpa_s, MSG_DEBUG, "Disconnect event - remove keys");
   4624 		wpa_clear_keys(wpa_s, wpa_s->bssid);
   4625 	}
   4626 	wpa_supplicant_mark_disassoc(wpa_s);
   4627 
   4628 	if (curr)
   4629 		wpa_bss_remove(wpa_s, curr, "Connection to AP lost");
   4630 
   4631 	if (fast_reconnect &&
   4632 	    !wpas_network_disabled(wpa_s, fast_reconnect_ssid) &&
   4633 	    !disallowed_bssid(wpa_s, fast_reconnect->bssid) &&
   4634 	    !disallowed_ssid(wpa_s, fast_reconnect->ssid,
   4635 			     fast_reconnect->ssid_len) &&
   4636 	    !wpas_temp_disabled(wpa_s, fast_reconnect_ssid) &&
   4637 	    !wpa_is_bss_tmp_disallowed(wpa_s, fast_reconnect)) {
   4638 #ifndef CONFIG_NO_SCAN_PROCESSING
   4639 		wpa_dbg(wpa_s, MSG_DEBUG, "Try to reconnect to the same BSS");
   4640 		if (wpa_supplicant_connect(wpa_s, fast_reconnect,
   4641 					   fast_reconnect_ssid) < 0) {
   4642 			/* Recover through full scan */
   4643 			wpa_supplicant_req_scan(wpa_s, 0, 100000);
   4644 		}
   4645 #endif /* CONFIG_NO_SCAN_PROCESSING */
   4646 	} else if (fast_reconnect) {
   4647 		/*
   4648 		 * Could not reconnect to the same BSS due to network being
   4649 		 * disabled. Use a new scan to match the alternative behavior
   4650 		 * above, i.e., to continue automatic reconnection attempt in a
   4651 		 * way that enforces disabled network rules.
   4652 		 */
   4653 		wpa_supplicant_req_scan(wpa_s, 0, 100000);
   4654 	}
   4655 }
   4656 
   4657 
   4658 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
   4659 void wpa_supplicant_delayed_mic_error_report(void *eloop_ctx, void *sock_ctx)
   4660 {
   4661 	struct wpa_supplicant *wpa_s = eloop_ctx;
   4662 
   4663 	if (!wpa_s->pending_mic_error_report)
   4664 		return;
   4665 
   4666 	wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Sending pending MIC error report");
   4667 	wpa_sm_key_request(wpa_s->wpa, 1, wpa_s->pending_mic_error_pairwise);
   4668 	wpa_s->pending_mic_error_report = 0;
   4669 }
   4670 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
   4671 
   4672 
   4673 static void
   4674 wpa_supplicant_event_michael_mic_failure(struct wpa_supplicant *wpa_s,
   4675 					 union wpa_event_data *data)
   4676 {
   4677 	int pairwise;
   4678 	struct os_reltime t;
   4679 
   4680 	wpa_msg(wpa_s, MSG_WARNING, "Michael MIC failure detected");
   4681 	pairwise = (data && data->michael_mic_failure.unicast);
   4682 	os_get_reltime(&t);
   4683 	if ((os_reltime_initialized(&wpa_s->last_michael_mic_error) &&
   4684 	     !os_reltime_expired(&t, &wpa_s->last_michael_mic_error, 60)) ||
   4685 	    wpa_s->pending_mic_error_report) {
   4686 		if (wpa_s->pending_mic_error_report) {
   4687 			/*
   4688 			 * Send the pending MIC error report immediately since
   4689 			 * we are going to start countermeasures and AP better
   4690 			 * do the same.
   4691 			 */
   4692 			wpa_sm_key_request(wpa_s->wpa, 1,
   4693 					   wpa_s->pending_mic_error_pairwise);
   4694 		}
   4695 
   4696 		/* Send the new MIC error report immediately since we are going
   4697 		 * to start countermeasures and AP better do the same.
   4698 		 */
   4699 		wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
   4700 
   4701 		/* initialize countermeasures */
   4702 		wpa_s->countermeasures = 1;
   4703 
   4704 		wpa_bssid_ignore_add(wpa_s, wpa_s->bssid);
   4705 
   4706 		wpa_msg(wpa_s, MSG_WARNING, "TKIP countermeasures started");
   4707 
   4708 		/*
   4709 		 * Need to wait for completion of request frame. We do not get
   4710 		 * any callback for the message completion, so just wait a
   4711 		 * short while and hope for the best. */
   4712 		os_sleep(0, 10000);
   4713 
   4714 		wpa_drv_set_countermeasures(wpa_s, 1);
   4715 		wpa_supplicant_deauthenticate(wpa_s,
   4716 					      WLAN_REASON_MICHAEL_MIC_FAILURE);
   4717 		eloop_cancel_timeout(wpa_supplicant_stop_countermeasures,
   4718 				     wpa_s, NULL);
   4719 		eloop_register_timeout(60, 0,
   4720 				       wpa_supplicant_stop_countermeasures,
   4721 				       wpa_s, NULL);
   4722 		/* TODO: mark the AP rejected for 60 second. STA is
   4723 		 * allowed to associate with another AP.. */
   4724 	} else {
   4725 #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
   4726 		if (wpa_s->mic_errors_seen) {
   4727 			/*
   4728 			 * Reduce the effectiveness of Michael MIC error
   4729 			 * reports as a means for attacking against TKIP if
   4730 			 * more than one MIC failure is noticed with the same
   4731 			 * PTK. We delay the transmission of the reports by a
   4732 			 * random time between 0 and 60 seconds in order to
   4733 			 * force the attacker wait 60 seconds before getting
   4734 			 * the information on whether a frame resulted in a MIC
   4735 			 * failure.
   4736 			 */
   4737 			u8 rval[4];
   4738 			int sec;
   4739 
   4740 			if (os_get_random(rval, sizeof(rval)) < 0)
   4741 				sec = os_random() % 60;
   4742 			else
   4743 				sec = WPA_GET_BE32(rval) % 60;
   4744 			wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Delay MIC error "
   4745 				"report %d seconds", sec);
   4746 			wpa_s->pending_mic_error_report = 1;
   4747 			wpa_s->pending_mic_error_pairwise = pairwise;
   4748 			eloop_cancel_timeout(
   4749 				wpa_supplicant_delayed_mic_error_report,
   4750 				wpa_s, NULL);
   4751 			eloop_register_timeout(
   4752 				sec, os_random() % 1000000,
   4753 				wpa_supplicant_delayed_mic_error_report,
   4754 				wpa_s, NULL);
   4755 		} else {
   4756 			wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
   4757 		}
   4758 #else /* CONFIG_DELAYED_MIC_ERROR_REPORT */
   4759 		wpa_sm_key_request(wpa_s->wpa, 1, pairwise);
   4760 #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
   4761 	}
   4762 	wpa_s->last_michael_mic_error = t;
   4763 	wpa_s->mic_errors_seen++;
   4764 }
   4765 
   4766 
   4767 #ifdef CONFIG_TERMINATE_ONLASTIF
   4768 static int any_interfaces(struct wpa_supplicant *head)
   4769 {
   4770 	struct wpa_supplicant *wpa_s;
   4771 
   4772 	for (wpa_s = head; wpa_s != NULL; wpa_s = wpa_s->next)
   4773 		if (!wpa_s->interface_removed)
   4774 			return 1;
   4775 	return 0;
   4776 }
   4777 #endif /* CONFIG_TERMINATE_ONLASTIF */
   4778 
   4779 
   4780 static void
   4781 wpa_supplicant_event_interface_status(struct wpa_supplicant *wpa_s,
   4782 				      union wpa_event_data *data)
   4783 {
   4784 	if (os_strcmp(wpa_s->ifname, data->interface_status.ifname) != 0)
   4785 		return;
   4786 
   4787 	switch (data->interface_status.ievent) {
   4788 	case EVENT_INTERFACE_ADDED:
   4789 		if (!wpa_s->interface_removed)
   4790 			break;
   4791 		wpa_s->interface_removed = 0;
   4792 		wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was added");
   4793 		if (wpa_supplicant_driver_init(wpa_s) < 0) {
   4794 			wpa_msg(wpa_s, MSG_INFO, "Failed to initialize the "
   4795 				"driver after interface was added");
   4796 		}
   4797 
   4798 #ifdef CONFIG_P2P
   4799 		if (!wpa_s->global->p2p &&
   4800 		    !wpa_s->global->p2p_disabled &&
   4801 		    !wpa_s->conf->p2p_disabled &&
   4802 		    (wpa_s->drv_flags &
   4803 		     WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
   4804 		    wpas_p2p_add_p2pdev_interface(
   4805 			    wpa_s, wpa_s->global->params.conf_p2p_dev) < 0) {
   4806 			wpa_printf(MSG_INFO,
   4807 				   "P2P: Failed to enable P2P Device interface");
   4808 			/* Try to continue without. P2P will be disabled. */
   4809 		}
   4810 #endif /* CONFIG_P2P */
   4811 
   4812 		break;
   4813 	case EVENT_INTERFACE_REMOVED:
   4814 		wpa_dbg(wpa_s, MSG_DEBUG, "Configured interface was removed");
   4815 		wpa_s->interface_removed = 1;
   4816 		wpa_supplicant_mark_disassoc(wpa_s);
   4817 		wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
   4818 		l2_packet_deinit(wpa_s->l2);
   4819 		wpa_s->l2 = NULL;
   4820 
   4821 #ifdef CONFIG_P2P
   4822 		if (wpa_s->global->p2p &&
   4823 		    wpa_s->global->p2p_init_wpa_s->parent == wpa_s &&
   4824 		    (wpa_s->drv_flags &
   4825 		     WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) {
   4826 			wpa_dbg(wpa_s, MSG_DEBUG,
   4827 				"Removing P2P Device interface");
   4828 			wpa_supplicant_remove_iface(
   4829 				wpa_s->global, wpa_s->global->p2p_init_wpa_s,
   4830 				0);
   4831 			wpa_s->global->p2p_init_wpa_s = NULL;
   4832 		}
   4833 #endif /* CONFIG_P2P */
   4834 
   4835 #ifdef CONFIG_MATCH_IFACE
   4836 		if (wpa_s->matched) {
   4837 			wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
   4838 			break;
   4839 		}
   4840 #endif /* CONFIG_MATCH_IFACE */
   4841 
   4842 #ifdef CONFIG_TERMINATE_ONLASTIF
   4843 		/* check if last interface */
   4844 		if (!any_interfaces(wpa_s->global->ifaces))
   4845 			eloop_terminate();
   4846 #endif /* CONFIG_TERMINATE_ONLASTIF */
   4847 		break;
   4848 	}
   4849 }
   4850 
   4851 
   4852 #ifdef CONFIG_TDLS
   4853 static void wpa_supplicant_event_tdls(struct wpa_supplicant *wpa_s,
   4854 				      union wpa_event_data *data)
   4855 {
   4856 	if (data == NULL)
   4857 		return;
   4858 	switch (data->tdls.oper) {
   4859 	case TDLS_REQUEST_SETUP:
   4860 		wpa_tdls_remove(wpa_s->wpa, data->tdls.peer);
   4861 		if (wpa_tdls_is_external_setup(wpa_s->wpa))
   4862 			wpa_tdls_start(wpa_s->wpa, data->tdls.peer);
   4863 		else
   4864 			wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, data->tdls.peer);
   4865 		break;
   4866 	case TDLS_REQUEST_TEARDOWN:
   4867 		if (wpa_tdls_is_external_setup(wpa_s->wpa))
   4868 			wpa_tdls_teardown_link(wpa_s->wpa, data->tdls.peer,
   4869 					       data->tdls.reason_code);
   4870 		else
   4871 			wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN,
   4872 					  data->tdls.peer);
   4873 		break;
   4874 	case TDLS_REQUEST_DISCOVER:
   4875 			wpa_tdls_send_discovery_request(wpa_s->wpa,
   4876 							data->tdls.peer);
   4877 		break;
   4878 	}
   4879 }
   4880 #endif /* CONFIG_TDLS */
   4881 
   4882 
   4883 #ifdef CONFIG_WNM
   4884 static void wpa_supplicant_event_wnm(struct wpa_supplicant *wpa_s,
   4885 				     union wpa_event_data *data)
   4886 {
   4887 	if (data == NULL)
   4888 		return;
   4889 	switch (data->wnm.oper) {
   4890 	case WNM_OPER_SLEEP:
   4891 		wpa_printf(MSG_DEBUG, "Start sending WNM-Sleep Request "
   4892 			   "(action=%d, intval=%d)",
   4893 			   data->wnm.sleep_action, data->wnm.sleep_intval);
   4894 		ieee802_11_send_wnmsleep_req(wpa_s, data->wnm.sleep_action,
   4895 					     data->wnm.sleep_intval, NULL);
   4896 		break;
   4897 	}
   4898 }
   4899 #endif /* CONFIG_WNM */
   4900 
   4901 
   4902 #ifdef CONFIG_IEEE80211R
   4903 static void
   4904 wpa_supplicant_event_ft_response(struct wpa_supplicant *wpa_s,
   4905 				 union wpa_event_data *data)
   4906 {
   4907 	if (data == NULL)
   4908 		return;
   4909 
   4910 	if (wpa_ft_process_response(wpa_s->wpa, data->ft_ies.ies,
   4911 				    data->ft_ies.ies_len,
   4912 				    data->ft_ies.ft_action,
   4913 				    data->ft_ies.target_ap,
   4914 				    data->ft_ies.ric_ies,
   4915 				    data->ft_ies.ric_ies_len) < 0) {
   4916 		/* TODO: prevent MLME/driver from trying to associate? */
   4917 	}
   4918 }
   4919 #endif /* CONFIG_IEEE80211R */
   4920 
   4921 
   4922 #ifdef CONFIG_IBSS_RSN
   4923 static void wpa_supplicant_event_ibss_rsn_start(struct wpa_supplicant *wpa_s,
   4924 						union wpa_event_data *data)
   4925 {
   4926 	struct wpa_ssid *ssid;
   4927 	if (wpa_s->wpa_state < WPA_ASSOCIATED)
   4928 		return;
   4929 	if (data == NULL)
   4930 		return;
   4931 	ssid = wpa_s->current_ssid;
   4932 	if (ssid == NULL)
   4933 		return;
   4934 	if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
   4935 		return;
   4936 
   4937 	ibss_rsn_start(wpa_s->ibss_rsn, data->ibss_rsn_start.peer);
   4938 }
   4939 
   4940 
   4941 static void wpa_supplicant_event_ibss_auth(struct wpa_supplicant *wpa_s,
   4942 					   union wpa_event_data *data)
   4943 {
   4944 	struct wpa_ssid *ssid = wpa_s->current_ssid;
   4945 
   4946 	if (ssid == NULL)
   4947 		return;
   4948 
   4949 	/* check if the ssid is correctly configured as IBSS/RSN */
   4950 	if (ssid->mode != WPAS_MODE_IBSS || !wpa_key_mgmt_wpa(ssid->key_mgmt))
   4951 		return;
   4952 
   4953 	ibss_rsn_handle_auth(wpa_s->ibss_rsn, data->rx_mgmt.frame,
   4954 			     data->rx_mgmt.frame_len);
   4955 }
   4956 #endif /* CONFIG_IBSS_RSN */
   4957 
   4958 
   4959 #ifdef CONFIG_IEEE80211R
   4960 static void ft_rx_action(struct wpa_supplicant *wpa_s, const u8 *data,
   4961 			 size_t len)
   4962 {
   4963 	const u8 *sta_addr, *target_ap_addr;
   4964 	u16 status;
   4965 
   4966 	wpa_hexdump(MSG_MSGDUMP, "FT: RX Action", data, len);
   4967 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME))
   4968 		return; /* only SME case supported for now */
   4969 	if (len < 1 + 2 * ETH_ALEN + 2)
   4970 		return;
   4971 	if (data[0] != 2)
   4972 		return; /* Only FT Action Response is supported for now */
   4973 	sta_addr = data + 1;
   4974 	target_ap_addr = data + 1 + ETH_ALEN;
   4975 	status = WPA_GET_LE16(data + 1 + 2 * ETH_ALEN);
   4976 	wpa_dbg(wpa_s, MSG_DEBUG, "FT: Received FT Action Response: STA "
   4977 		MACSTR " TargetAP " MACSTR " status %u",
   4978 		MAC2STR(sta_addr), MAC2STR(target_ap_addr), status);
   4979 
   4980 	if (!ether_addr_equal(sta_addr, wpa_s->own_addr)) {
   4981 		wpa_dbg(wpa_s, MSG_DEBUG, "FT: Foreign STA Address " MACSTR
   4982 			" in FT Action Response", MAC2STR(sta_addr));
   4983 		return;
   4984 	}
   4985 
   4986 	if (status) {
   4987 		wpa_dbg(wpa_s, MSG_DEBUG, "FT: FT Action Response indicates "
   4988 			"failure (status code %d)", status);
   4989 		/* TODO: report error to FT code(?) */
   4990 		return;
   4991 	}
   4992 
   4993 	if (wpa_ft_process_response(wpa_s->wpa, data + 1 + 2 * ETH_ALEN + 2,
   4994 				    len - (1 + 2 * ETH_ALEN + 2), 1,
   4995 				    target_ap_addr, NULL, 0) < 0)
   4996 		return;
   4997 
   4998 #ifdef CONFIG_SME
   4999 	{
   5000 		struct wpa_bss *bss;
   5001 		bss = wpa_bss_get_bssid(wpa_s, target_ap_addr);
   5002 		if (bss)
   5003 			wpa_s->sme.freq = bss->freq;
   5004 		wpa_s->sme.auth_alg = WPA_AUTH_ALG_FT;
   5005 		sme_associate(wpa_s, WPAS_MODE_INFRA, target_ap_addr,
   5006 			      WLAN_AUTH_FT);
   5007 	}
   5008 #endif /* CONFIG_SME */
   5009 }
   5010 #endif /* CONFIG_IEEE80211R */
   5011 
   5012 
   5013 static void wpa_supplicant_event_unprot_deauth(struct wpa_supplicant *wpa_s,
   5014 					       struct unprot_deauth *e)
   5015 {
   5016 	wpa_printf(MSG_DEBUG, "Unprotected Deauthentication frame "
   5017 		   "dropped: " MACSTR " -> " MACSTR
   5018 		   " (reason code %u)",
   5019 		   MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
   5020 	sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
   5021 }
   5022 
   5023 
   5024 static void wpa_supplicant_event_unprot_disassoc(struct wpa_supplicant *wpa_s,
   5025 						 struct unprot_disassoc *e)
   5026 {
   5027 	wpa_printf(MSG_DEBUG, "Unprotected Disassociation frame "
   5028 		   "dropped: " MACSTR " -> " MACSTR
   5029 		   " (reason code %u)",
   5030 		   MAC2STR(e->sa), MAC2STR(e->da), e->reason_code);
   5031 	sme_event_unprot_disconnect(wpa_s, e->sa, e->da, e->reason_code);
   5032 }
   5033 
   5034 
   5035 static void wpas_event_disconnect(struct wpa_supplicant *wpa_s, const u8 *addr,
   5036 				  u16 reason_code, int locally_generated,
   5037 				  const u8 *ie, size_t ie_len, int deauth)
   5038 {
   5039 #ifdef CONFIG_AP
   5040 	if (wpa_s->ap_iface && addr) {
   5041 		hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], addr);
   5042 		return;
   5043 	}
   5044 
   5045 	if (wpa_s->ap_iface) {
   5046 		wpa_dbg(wpa_s, MSG_DEBUG, "Ignore deauth event in AP mode");
   5047 		return;
   5048 	}
   5049 #endif /* CONFIG_AP */
   5050 
   5051 	if (!locally_generated)
   5052 		wpa_s->own_disconnect_req = 0;
   5053 
   5054 	wpa_supplicant_event_disassoc(wpa_s, reason_code, locally_generated);
   5055 
   5056 	if (((reason_code == WLAN_REASON_IEEE_802_1X_AUTH_FAILED ||
   5057 	      ((wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
   5058 		(wpa_s->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)) &&
   5059 	       eapol_sm_failed(wpa_s->eapol))) &&
   5060 	     !wpa_s->eap_expected_failure))
   5061 		wpas_auth_failed(wpa_s, "AUTH_FAILED", addr);
   5062 
   5063 #ifdef CONFIG_P2P
   5064 	if (deauth && reason_code > 0) {
   5065 		if (wpas_p2p_deauth_notif(wpa_s, addr, reason_code, ie, ie_len,
   5066 					  locally_generated) > 0) {
   5067 			/*
   5068 			 * The interface was removed, so cannot continue
   5069 			 * processing any additional operations after this.
   5070 			 */
   5071 			return;
   5072 		}
   5073 	}
   5074 #endif /* CONFIG_P2P */
   5075 
   5076 	wpa_supplicant_event_disassoc_finish(wpa_s, reason_code,
   5077 					     locally_generated);
   5078 }
   5079 
   5080 
   5081 static void wpas_event_disassoc(struct wpa_supplicant *wpa_s,
   5082 				struct disassoc_info *info)
   5083 {
   5084 	u16 reason_code = 0;
   5085 	int locally_generated = 0;
   5086 	const u8 *addr = NULL;
   5087 	const u8 *ie = NULL;
   5088 	size_t ie_len = 0;
   5089 
   5090 	wpa_dbg(wpa_s, MSG_DEBUG, "Disassociation notification");
   5091 
   5092 	if (info) {
   5093 		addr = info->addr;
   5094 		ie = info->ie;
   5095 		ie_len = info->ie_len;
   5096 		reason_code = info->reason_code;
   5097 		locally_generated = info->locally_generated;
   5098 		wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u (%s)%s", reason_code,
   5099 			reason2str(reason_code),
   5100 			locally_generated ? " locally_generated=1" : "");
   5101 		if (addr)
   5102 			wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
   5103 				MAC2STR(addr));
   5104 		wpa_hexdump(MSG_DEBUG, "Disassociation frame IE(s)",
   5105 			    ie, ie_len);
   5106 	}
   5107 
   5108 #ifdef CONFIG_AP
   5109 	if (wpa_s->ap_iface && info && info->addr) {
   5110 		hostapd_notif_disassoc(wpa_s->ap_iface->bss[0], info->addr);
   5111 		return;
   5112 	}
   5113 
   5114 	if (wpa_s->ap_iface) {
   5115 		wpa_dbg(wpa_s, MSG_DEBUG, "Ignore disassoc event in AP mode");
   5116 		return;
   5117 	}
   5118 #endif /* CONFIG_AP */
   5119 
   5120 #ifdef CONFIG_P2P
   5121 	if (info) {
   5122 		wpas_p2p_disassoc_notif(
   5123 			wpa_s, info->addr, reason_code, info->ie, info->ie_len,
   5124 			locally_generated);
   5125 	}
   5126 #endif /* CONFIG_P2P */
   5127 
   5128 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
   5129 		sme_event_disassoc(wpa_s, info);
   5130 
   5131 	wpas_event_disconnect(wpa_s, addr, reason_code, locally_generated,
   5132 			      ie, ie_len, 0);
   5133 }
   5134 
   5135 
   5136 static void wpas_event_deauth(struct wpa_supplicant *wpa_s,
   5137 			      struct deauth_info *info)
   5138 {
   5139 	u16 reason_code = 0;
   5140 	int locally_generated = 0;
   5141 	const u8 *addr = NULL;
   5142 	const u8 *ie = NULL;
   5143 	size_t ie_len = 0;
   5144 
   5145 	wpa_dbg(wpa_s, MSG_DEBUG, "Deauthentication notification");
   5146 
   5147 	if (info) {
   5148 		addr = info->addr;
   5149 		ie = info->ie;
   5150 		ie_len = info->ie_len;
   5151 		reason_code = info->reason_code;
   5152 		locally_generated = info->locally_generated;
   5153 		wpa_dbg(wpa_s, MSG_DEBUG, " * reason %u (%s)%s",
   5154 			reason_code, reason2str(reason_code),
   5155 			locally_generated ? " locally_generated=1" : "");
   5156 		if (addr) {
   5157 			wpa_dbg(wpa_s, MSG_DEBUG, " * address " MACSTR,
   5158 				MAC2STR(addr));
   5159 		}
   5160 		wpa_hexdump(MSG_DEBUG, "Deauthentication frame IE(s)",
   5161 			    ie, ie_len);
   5162 	}
   5163 
   5164 	wpa_reset_ft_completed(wpa_s->wpa);
   5165 
   5166 	wpas_event_disconnect(wpa_s, addr, reason_code,
   5167 			      locally_generated, ie, ie_len, 1);
   5168 }
   5169 
   5170 
   5171 static const char * reg_init_str(enum reg_change_initiator init)
   5172 {
   5173 	switch (init) {
   5174 	case REGDOM_SET_BY_CORE:
   5175 		return "CORE";
   5176 	case REGDOM_SET_BY_USER:
   5177 		return "USER";
   5178 	case REGDOM_SET_BY_DRIVER:
   5179 		return "DRIVER";
   5180 	case REGDOM_SET_BY_COUNTRY_IE:
   5181 		return "COUNTRY_IE";
   5182 	case REGDOM_BEACON_HINT:
   5183 		return "BEACON_HINT";
   5184 	}
   5185 	return "?";
   5186 }
   5187 
   5188 
   5189 static const char * reg_type_str(enum reg_type type)
   5190 {
   5191 	switch (type) {
   5192 	case REGDOM_TYPE_UNKNOWN:
   5193 		return "UNKNOWN";
   5194 	case REGDOM_TYPE_COUNTRY:
   5195 		return "COUNTRY";
   5196 	case REGDOM_TYPE_WORLD:
   5197 		return "WORLD";
   5198 	case REGDOM_TYPE_CUSTOM_WORLD:
   5199 		return "CUSTOM_WORLD";
   5200 	case REGDOM_TYPE_INTERSECTION:
   5201 		return "INTERSECTION";
   5202 	}
   5203 	return "?";
   5204 }
   5205 
   5206 
   5207 static void wpas_beacon_hint(struct wpa_supplicant *wpa_s, const char *title,
   5208 			     struct frequency_attrs *attrs)
   5209 {
   5210 	if (!attrs->freq)
   5211 		return;
   5212 	wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REGDOM_BEACON_HINT
   5213 		"%s freq=%u max_tx_power=%u%s%s%s",
   5214 		title, attrs->freq, attrs->max_tx_power,
   5215 		attrs->disabled ? " disabled=1" : "",
   5216 		attrs->no_ir ? " no_ir=1" : "",
   5217 		attrs->radar ? " radar=1" : "");
   5218 }
   5219 
   5220 
   5221 void wpa_supplicant_update_channel_list(struct wpa_supplicant *wpa_s,
   5222 					struct channel_list_changed *info)
   5223 {
   5224 	struct wpa_supplicant *ifs;
   5225 	u8 dfs_domain;
   5226 
   5227 	/*
   5228 	 * To allow backwards compatibility with higher level layers that
   5229 	 * assumed the REGDOM_CHANGE event is sent over the initially added
   5230 	 * interface. Find the highest parent of this interface and use it to
   5231 	 * send the event.
   5232 	 */
   5233 	for (ifs = wpa_s; ifs->parent && ifs != ifs->parent; ifs = ifs->parent)
   5234 		;
   5235 
   5236 	if (info) {
   5237 		wpa_msg(ifs, MSG_INFO,
   5238 			WPA_EVENT_REGDOM_CHANGE "init=%s type=%s%s%s",
   5239 			reg_init_str(info->initiator), reg_type_str(info->type),
   5240 			info->alpha2[0] ? " alpha2=" : "",
   5241 			info->alpha2[0] ? info->alpha2 : "");
   5242 
   5243 		if (info->initiator == REGDOM_BEACON_HINT) {
   5244 			wpas_beacon_hint(ifs, "before",
   5245 					 &info->beacon_hint_before);
   5246 			wpas_beacon_hint(ifs, "after",
   5247 					 &info->beacon_hint_after);
   5248 		}
   5249 	}
   5250 
   5251 	if (wpa_s->drv_priv == NULL)
   5252 		return; /* Ignore event during drv initialization */
   5253 
   5254 	dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
   5255 			 radio_list) {
   5256 		bool was_6ghz_enabled;
   5257 
   5258 		wpa_printf(MSG_DEBUG, "%s: Updating hw mode",
   5259 			   ifs->ifname);
   5260 		free_hw_features(ifs);
   5261 		ifs->hw.modes = wpa_drv_get_hw_feature_data(
   5262 			ifs, &ifs->hw.num_modes, &ifs->hw.flags, &dfs_domain);
   5263 
   5264 		was_6ghz_enabled = ifs->is_6ghz_enabled;
   5265 		ifs->is_6ghz_enabled = wpas_is_6ghz_supported(ifs, true);
   5266 
   5267 		/* Restart PNO/sched_scan with updated channel list */
   5268 		if (ifs->pno) {
   5269 			wpas_stop_pno(ifs);
   5270 			wpas_start_pno(ifs);
   5271 		} else if (ifs->sched_scanning && !ifs->pno_sched_pending) {
   5272 			wpa_dbg(ifs, MSG_DEBUG,
   5273 				"Channel list changed - restart sched_scan");
   5274 			wpas_scan_restart_sched_scan(ifs);
   5275 		} else if (!was_6ghz_enabled && ifs->is_6ghz_enabled) {
   5276 			wpa_dbg(ifs, MSG_INFO,
   5277 				"Channel list changed: 6 GHz was enabled");
   5278 
   5279 			ifs->crossed_6ghz_dom = true;
   5280 		}
   5281 	}
   5282 
   5283 	wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DRIVER);
   5284 }
   5285 
   5286 
   5287 static void wpas_event_rx_mgmt_action(struct wpa_supplicant *wpa_s,
   5288 				      const u8 *frame, size_t len, int freq,
   5289 				      int rssi)
   5290 {
   5291 	const struct ieee80211_mgmt *mgmt;
   5292 	const u8 *payload;
   5293 	size_t plen;
   5294 	u8 category;
   5295 
   5296 	if (len < IEEE80211_HDRLEN + 2)
   5297 		return;
   5298 
   5299 	mgmt = (const struct ieee80211_mgmt *) frame;
   5300 	payload = frame + IEEE80211_HDRLEN;
   5301 	category = *payload++;
   5302 	plen = len - IEEE80211_HDRLEN - 1;
   5303 
   5304 	wpa_dbg(wpa_s, MSG_DEBUG, "Received Action frame: SA=" MACSTR
   5305 		" Category=%u DataLen=%d freq=%d MHz",
   5306 		MAC2STR(mgmt->sa), category, (int) plen, freq);
   5307 
   5308 #ifndef CONFIG_NO_WMM_AC
   5309 	if (category == WLAN_ACTION_WMM) {
   5310 		wmm_ac_rx_action(wpa_s, mgmt->da, mgmt->sa, payload, plen);
   5311 		return;
   5312 	}
   5313 #endif /* CONFIG_NO_WMM_AC */
   5314 
   5315 #ifdef CONFIG_IEEE80211R
   5316 	if (category == WLAN_ACTION_FT) {
   5317 		ft_rx_action(wpa_s, payload, plen);
   5318 		return;
   5319 	}
   5320 #endif /* CONFIG_IEEE80211R */
   5321 
   5322 #ifdef CONFIG_SME
   5323 	if (category == WLAN_ACTION_SA_QUERY) {
   5324 		sme_sa_query_rx(wpa_s, mgmt->da, mgmt->sa, payload, plen);
   5325 		return;
   5326 	}
   5327 #endif /* CONFIG_SME */
   5328 
   5329 #ifdef CONFIG_WNM
   5330 	if (mgmt->u.action.category == WLAN_ACTION_WNM) {
   5331 		ieee802_11_rx_wnm_action(wpa_s, mgmt, len);
   5332 		return;
   5333 	}
   5334 #endif /* CONFIG_WNM */
   5335 
   5336 #ifdef CONFIG_GAS
   5337 	if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
   5338 	     mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
   5339 	    gas_query_rx(wpa_s->gas, mgmt->da, mgmt->sa, mgmt->bssid,
   5340 			 mgmt->u.action.category,
   5341 			 payload, plen, freq) == 0)
   5342 		return;
   5343 #endif /* CONFIG_GAS */
   5344 
   5345 #ifdef CONFIG_GAS_SERVER
   5346 	if ((mgmt->u.action.category == WLAN_ACTION_PUBLIC ||
   5347 	     mgmt->u.action.category == WLAN_ACTION_PROTECTED_DUAL) &&
   5348 	    gas_server_rx(wpa_s->gas_server, mgmt->da, mgmt->sa, mgmt->bssid,
   5349 			  mgmt->u.action.category,
   5350 			  payload, plen, freq) == 0)
   5351 		return;
   5352 #endif /* CONFIG_GAS_SERVER */
   5353 
   5354 #ifdef CONFIG_TDLS
   5355 	if (category == WLAN_ACTION_PUBLIC && plen >= 4 &&
   5356 	    payload[0] == WLAN_TDLS_DISCOVERY_RESPONSE) {
   5357 		wpa_dbg(wpa_s, MSG_DEBUG,
   5358 			"TDLS: Received Discovery Response from " MACSTR,
   5359 			MAC2STR(mgmt->sa));
   5360 		if (wpa_s->valid_links &&
   5361 		    wpa_tdls_process_discovery_response(wpa_s->wpa, mgmt->sa,
   5362 							&payload[1], plen - 1))
   5363 			wpa_dbg(wpa_s, MSG_ERROR,
   5364 				"TDLS: Discovery Response process failed for "
   5365 				MACSTR, MAC2STR(mgmt->sa));
   5366 		return;
   5367 	}
   5368 #endif /* CONFIG_TDLS */
   5369 
   5370 #ifdef CONFIG_INTERWORKING
   5371 	if (category == WLAN_ACTION_QOS && plen >= 1 &&
   5372 	    payload[0] == QOS_QOS_MAP_CONFIG) {
   5373 		const u8 *pos = payload + 1;
   5374 		size_t qlen = plen - 1;
   5375 		wpa_dbg(wpa_s, MSG_DEBUG, "Interworking: Received QoS Map Configure frame from "
   5376 			MACSTR, MAC2STR(mgmt->sa));
   5377 		if (ether_addr_equal(mgmt->sa, wpa_s->bssid) &&
   5378 		    qlen > 2 && pos[0] == WLAN_EID_QOS_MAP_SET &&
   5379 		    pos[1] <= qlen - 2 && pos[1] >= 16)
   5380 			wpas_qos_map_set(wpa_s, pos + 2, pos[1]);
   5381 		return;
   5382 	}
   5383 #endif /* CONFIG_INTERWORKING */
   5384 
   5385 #ifndef CONFIG_NO_RRM
   5386 	if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
   5387 	    payload[0] == WLAN_RRM_RADIO_MEASUREMENT_REQUEST) {
   5388 		wpas_rrm_handle_radio_measurement_request(wpa_s, mgmt->sa,
   5389 							  mgmt->da,
   5390 							  payload + 1,
   5391 							  plen - 1);
   5392 		return;
   5393 	}
   5394 
   5395 	if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
   5396 	    payload[0] == WLAN_RRM_NEIGHBOR_REPORT_RESPONSE) {
   5397 		wpas_rrm_process_neighbor_rep(wpa_s, payload + 1, plen - 1);
   5398 		return;
   5399 	}
   5400 
   5401 	if (category == WLAN_ACTION_RADIO_MEASUREMENT &&
   5402 	    payload[0] == WLAN_RRM_LINK_MEASUREMENT_REQUEST) {
   5403 		wpas_rrm_handle_link_measurement_request(wpa_s, mgmt->sa,
   5404 							 payload + 1, plen - 1,
   5405 							 rssi);
   5406 		return;
   5407 	}
   5408 #endif /* CONFIG_NO_RRM */
   5409 
   5410 #ifdef CONFIG_FST
   5411 	if (mgmt->u.action.category == WLAN_ACTION_FST && wpa_s->fst) {
   5412 		fst_rx_action(wpa_s->fst, mgmt, len);
   5413 		return;
   5414 	}
   5415 #endif /* CONFIG_FST */
   5416 
   5417 #ifdef CONFIG_NAN_USD
   5418 	if (category == WLAN_ACTION_PUBLIC && plen >= 5 &&
   5419 	    payload[0] == WLAN_PA_VENDOR_SPECIFIC &&
   5420 	    WPA_GET_BE32(&payload[1]) == NAN_SDF_VENDOR_TYPE) {
   5421 		payload += 5;
   5422 		plen -= 5;
   5423 		wpas_nan_usd_rx_sdf(wpa_s, mgmt->sa, freq, payload, plen);
   5424 		return;
   5425 	}
   5426 #endif /* CONFIG_NAN_USD */
   5427 
   5428 #ifdef CONFIG_DPP
   5429 	if (category == WLAN_ACTION_PUBLIC && plen >= 5 &&
   5430 	    payload[0] == WLAN_PA_VENDOR_SPECIFIC &&
   5431 	    WPA_GET_BE24(&payload[1]) == OUI_WFA &&
   5432 	    payload[4] == DPP_OUI_TYPE) {
   5433 		payload++;
   5434 		plen--;
   5435 		wpas_dpp_rx_action(wpa_s, mgmt->sa, payload, plen, freq);
   5436 		return;
   5437 	}
   5438 #endif /* CONFIG_DPP */
   5439 
   5440 #ifndef CONFIG_NO_ROBUST_AV
   5441 	if (category == WLAN_ACTION_ROBUST_AV_STREAMING &&
   5442 	    payload[0] == ROBUST_AV_SCS_RESP) {
   5443 		wpas_handle_robust_av_scs_recv_action(wpa_s, mgmt->sa,
   5444 						      payload + 1, plen - 1);
   5445 		return;
   5446 	}
   5447 
   5448 	if (category == WLAN_ACTION_ROBUST_AV_STREAMING &&
   5449 	    payload[0] == ROBUST_AV_MSCS_RESP) {
   5450 		wpas_handle_robust_av_recv_action(wpa_s, mgmt->sa,
   5451 						  payload + 1, plen - 1);
   5452 		return;
   5453 	}
   5454 
   5455 	if (category == WLAN_ACTION_VENDOR_SPECIFIC_PROTECTED && plen > 4 &&
   5456 	    WPA_GET_BE32(payload) == QM_ACTION_VENDOR_TYPE) {
   5457 		wpas_handle_qos_mgmt_recv_action(wpa_s, mgmt->sa,
   5458 						 payload + 4, plen - 4);
   5459 		return;
   5460 	}
   5461 #endif /* CONFIG_NO_ROBUST_AV */
   5462 
   5463 	wpas_p2p_rx_action(wpa_s, mgmt->da, mgmt->sa, mgmt->bssid,
   5464 			   category, payload, plen, freq);
   5465 	if (wpa_s->ifmsh)
   5466 		mesh_mpm_action_rx(wpa_s, mgmt, len);
   5467 }
   5468 
   5469 
   5470 static void wpa_supplicant_notify_avoid_freq(struct wpa_supplicant *wpa_s,
   5471 					     union wpa_event_data *event)
   5472 {
   5473 	struct wpa_freq_range_list *list;
   5474 	char *str = NULL;
   5475 
   5476 	list = &event->freq_range;
   5477 
   5478 	if (list->num)
   5479 		str = freq_range_list_str(list);
   5480 	wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_AVOID_FREQ "ranges=%s",
   5481 		str ? str : "");
   5482 
   5483 #ifdef CONFIG_P2P
   5484 	if (freq_range_list_parse(&wpa_s->global->p2p_go_avoid_freq, str)) {
   5485 		wpa_dbg(wpa_s, MSG_ERROR, "%s: Failed to parse freq range",
   5486 			__func__);
   5487 	} else {
   5488 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Update channel list based on frequency avoid event");
   5489 
   5490 		/*
   5491 		 * The update channel flow will also take care of moving a GO
   5492 		 * from the unsafe frequency if needed.
   5493 		 */
   5494 		wpas_p2p_update_channel_list(wpa_s,
   5495 					     WPAS_P2P_CHANNEL_UPDATE_AVOID);
   5496 	}
   5497 #endif /* CONFIG_P2P */
   5498 
   5499 	os_free(str);
   5500 }
   5501 
   5502 
   5503 static void wpa_supplicant_event_port_authorized(struct wpa_supplicant *wpa_s)
   5504 {
   5505 	if (wpa_s->wpa_state == WPA_ASSOCIATED) {
   5506 		wpa_supplicant_cancel_auth_timeout(wpa_s);
   5507 		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
   5508 		eapol_sm_notify_portValid(wpa_s->eapol, true);
   5509 		eapol_sm_notify_eap_success(wpa_s->eapol, true);
   5510 		wpa_s->drv_authorized_port = 1;
   5511 	}
   5512 }
   5513 
   5514 
   5515 static unsigned int wpas_event_cac_ms(const struct wpa_supplicant *wpa_s,
   5516 				      int freq)
   5517 {
   5518 	size_t i;
   5519 	int j;
   5520 
   5521 	for (i = 0; i < wpa_s->hw.num_modes; i++) {
   5522 		const struct hostapd_hw_modes *mode = &wpa_s->hw.modes[i];
   5523 
   5524 		for (j = 0; j < mode->num_channels; j++) {
   5525 			const struct hostapd_channel_data *chan;
   5526 
   5527 			chan = &mode->channels[j];
   5528 			if (chan->freq == freq)
   5529 				return chan->dfs_cac_ms;
   5530 		}
   5531 	}
   5532 
   5533 	return 0;
   5534 }
   5535 
   5536 
   5537 static void wpas_event_dfs_cac_started(struct wpa_supplicant *wpa_s,
   5538 				       struct dfs_event *radar)
   5539 {
   5540 #if defined(NEED_AP_MLME) && defined(CONFIG_AP)
   5541 	if (wpa_s->ap_iface || wpa_s->ifmsh) {
   5542 		wpas_ap_event_dfs_cac_started(wpa_s, radar);
   5543 	} else
   5544 #endif /* NEED_AP_MLME && CONFIG_AP */
   5545 	{
   5546 		unsigned int cac_time = wpas_event_cac_ms(wpa_s, radar->freq);
   5547 
   5548 		cac_time /= 1000; /* convert from ms to sec */
   5549 		if (!cac_time)
   5550 			cac_time = 10 * 60; /* max timeout: 10 minutes */
   5551 
   5552 		/* Restart auth timeout: CAC time added to initial timeout */
   5553 		wpas_auth_timeout_restart(wpa_s, cac_time);
   5554 	}
   5555 }
   5556 
   5557 
   5558 static void wpas_event_dfs_cac_finished(struct wpa_supplicant *wpa_s,
   5559 					struct dfs_event *radar)
   5560 {
   5561 #if defined(NEED_AP_MLME) && defined(CONFIG_AP)
   5562 	if (wpa_s->ap_iface || wpa_s->ifmsh) {
   5563 		wpas_ap_event_dfs_cac_finished(wpa_s, radar);
   5564 	} else
   5565 #endif /* NEED_AP_MLME && CONFIG_AP */
   5566 	{
   5567 		/* Restart auth timeout with original value after CAC is
   5568 		 * finished */
   5569 		wpas_auth_timeout_restart(wpa_s, 0);
   5570 	}
   5571 }
   5572 
   5573 
   5574 static void wpas_event_dfs_cac_aborted(struct wpa_supplicant *wpa_s,
   5575 				       struct dfs_event *radar)
   5576 {
   5577 #if defined(NEED_AP_MLME) && defined(CONFIG_AP)
   5578 	if (wpa_s->ap_iface || wpa_s->ifmsh) {
   5579 		wpas_ap_event_dfs_cac_aborted(wpa_s, radar);
   5580 	} else
   5581 #endif /* NEED_AP_MLME && CONFIG_AP */
   5582 	{
   5583 		/* Restart auth timeout with original value after CAC is
   5584 		 * aborted */
   5585 		wpas_auth_timeout_restart(wpa_s, 0);
   5586 	}
   5587 }
   5588 
   5589 
   5590 static void wpa_supplicant_event_assoc_auth(struct wpa_supplicant *wpa_s,
   5591 					    union wpa_event_data *data)
   5592 {
   5593 	wpa_dbg(wpa_s, MSG_DEBUG,
   5594 		"Connection authorized by device, previous state %d",
   5595 		wpa_s->wpa_state);
   5596 
   5597 	wpa_supplicant_event_port_authorized(wpa_s);
   5598 
   5599 	wpa_s->last_eapol_matches_bssid = 1;
   5600 
   5601 	wpa_sm_set_rx_replay_ctr(wpa_s->wpa, data->assoc_info.key_replay_ctr);
   5602 	wpa_sm_set_ptk_kck_kek(wpa_s->wpa, data->assoc_info.ptk_kck,
   5603 			       data->assoc_info.ptk_kck_len,
   5604 			       data->assoc_info.ptk_kek,
   5605 			       data->assoc_info.ptk_kek_len);
   5606 #ifdef CONFIG_FILS
   5607 	if (wpa_s->auth_alg == WPA_AUTH_ALG_FILS) {
   5608 		struct wpa_bss *bss = wpa_bss_get_bssid(wpa_s, wpa_s->bssid);
   5609 		const u8 *fils_cache_id = wpa_bss_get_fils_cache_id(bss);
   5610 
   5611 		/* Update ERP next sequence number */
   5612 		eapol_sm_update_erp_next_seq_num(
   5613 			wpa_s->eapol, data->assoc_info.fils_erp_next_seq_num);
   5614 
   5615 		if (data->assoc_info.fils_pmk && data->assoc_info.fils_pmkid) {
   5616 			/* Add the new PMK and PMKID to the PMKSA cache */
   5617 			wpa_sm_pmksa_cache_add(wpa_s->wpa,
   5618 					       data->assoc_info.fils_pmk,
   5619 					       data->assoc_info.fils_pmk_len,
   5620 					       data->assoc_info.fils_pmkid,
   5621 					       wpa_s->valid_links ?
   5622 					       wpa_s->ap_mld_addr :
   5623 					       wpa_s->bssid,
   5624 					       fils_cache_id);
   5625 		} else if (data->assoc_info.fils_pmkid) {
   5626 			/* Update the current PMKSA used for this connection */
   5627 			pmksa_cache_set_current(wpa_s->wpa,
   5628 						data->assoc_info.fils_pmkid,
   5629 						NULL, NULL, 0, NULL, 0, true);
   5630 		}
   5631 	}
   5632 #endif /* CONFIG_FILS */
   5633 }
   5634 
   5635 
   5636 static const char * connect_fail_reason(enum sta_connect_fail_reason_codes code)
   5637 {
   5638 	switch (code) {
   5639 	case STA_CONNECT_FAIL_REASON_UNSPECIFIED:
   5640 		return "";
   5641 	case STA_CONNECT_FAIL_REASON_NO_BSS_FOUND:
   5642 		return "no_bss_found";
   5643 	case STA_CONNECT_FAIL_REASON_AUTH_TX_FAIL:
   5644 		return "auth_tx_fail";
   5645 	case STA_CONNECT_FAIL_REASON_AUTH_NO_ACK_RECEIVED:
   5646 		return "auth_no_ack_received";
   5647 	case STA_CONNECT_FAIL_REASON_AUTH_NO_RESP_RECEIVED:
   5648 		return "auth_no_resp_received";
   5649 	case STA_CONNECT_FAIL_REASON_ASSOC_REQ_TX_FAIL:
   5650 		return "assoc_req_tx_fail";
   5651 	case STA_CONNECT_FAIL_REASON_ASSOC_NO_ACK_RECEIVED:
   5652 		return "assoc_no_ack_received";
   5653 	case STA_CONNECT_FAIL_REASON_ASSOC_NO_RESP_RECEIVED:
   5654 		return "assoc_no_resp_received";
   5655 	default:
   5656 		return "unknown_reason";
   5657 	}
   5658 }
   5659 
   5660 
   5661 static void wpas_event_assoc_reject(struct wpa_supplicant *wpa_s,
   5662 				    union wpa_event_data *data)
   5663 {
   5664 	const u8 *bssid = data->assoc_reject.bssid;
   5665 	struct ieee802_11_elems elems;
   5666 	struct ml_sta_link_info ml_info[MAX_NUM_MLD_LINKS];
   5667 	const u8 *link_bssids[MAX_NUM_MLD_LINKS + 1];
   5668 #ifdef CONFIG_MBO
   5669 	struct wpa_bss *reject_bss;
   5670 #endif /* CONFIG_MBO */
   5671 
   5672 	if (!bssid || is_zero_ether_addr(bssid))
   5673 		bssid = wpa_s->pending_bssid;
   5674 #ifdef CONFIG_MBO
   5675 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
   5676 		reject_bss = wpa_s->current_bss;
   5677 	else
   5678 		reject_bss = wpa_bss_get_bssid(wpa_s, bssid);
   5679 #endif /* CONFIG_MBO */
   5680 
   5681 	if (data->assoc_reject.bssid)
   5682 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
   5683 			"bssid=" MACSTR	" status_code=%u%s%s%s%s%s",
   5684 			MAC2STR(data->assoc_reject.bssid),
   5685 			data->assoc_reject.status_code,
   5686 			data->assoc_reject.timed_out ? " timeout" : "",
   5687 			data->assoc_reject.timeout_reason ? "=" : "",
   5688 			data->assoc_reject.timeout_reason ?
   5689 			data->assoc_reject.timeout_reason : "",
   5690 			data->assoc_reject.reason_code !=
   5691 			STA_CONNECT_FAIL_REASON_UNSPECIFIED ?
   5692 			" qca_driver_reason=" : "",
   5693 			connect_fail_reason(data->assoc_reject.reason_code));
   5694 	else
   5695 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_ASSOC_REJECT
   5696 			"status_code=%u%s%s%s%s%s",
   5697 			data->assoc_reject.status_code,
   5698 			data->assoc_reject.timed_out ? " timeout" : "",
   5699 			data->assoc_reject.timeout_reason ? "=" : "",
   5700 			data->assoc_reject.timeout_reason ?
   5701 			data->assoc_reject.timeout_reason : "",
   5702 			data->assoc_reject.reason_code !=
   5703 			STA_CONNECT_FAIL_REASON_UNSPECIFIED ?
   5704 			" qca_driver_reason=" : "",
   5705 			connect_fail_reason(data->assoc_reject.reason_code));
   5706 	wpa_s->assoc_status_code = data->assoc_reject.status_code;
   5707 	wpas_notify_assoc_status_code(wpa_s);
   5708 
   5709 #ifdef CONFIG_OWE
   5710 	if (data->assoc_reject.status_code ==
   5711 	    WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED &&
   5712 	    wpa_s->key_mgmt == WPA_KEY_MGMT_OWE &&
   5713 	    wpa_s->current_ssid &&
   5714 	    wpa_s->current_ssid->owe_group == 0 &&
   5715 	    wpa_s->last_owe_group != 21) {
   5716 		struct wpa_ssid *ssid = wpa_s->current_ssid;
   5717 		struct wpa_bss *bss = wpa_s->current_bss;
   5718 
   5719 		if (!bss) {
   5720 			bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
   5721 			if (!bss) {
   5722 				wpas_connection_failed(wpa_s, bssid, NULL);
   5723 				wpa_supplicant_mark_disassoc(wpa_s);
   5724 				return;
   5725 			}
   5726 		}
   5727 		wpa_printf(MSG_DEBUG, "OWE: Try next supported DH group");
   5728 		wpas_connect_work_done(wpa_s);
   5729 		wpa_supplicant_mark_disassoc(wpa_s);
   5730 		wpa_supplicant_connect(wpa_s, bss, ssid);
   5731 		return;
   5732 	}
   5733 #endif /* CONFIG_OWE */
   5734 
   5735 #ifdef CONFIG_DPP2
   5736 	/* Try to follow AP's PFS policy. WLAN_STATUS_ASSOC_DENIED_UNSPEC is
   5737 	 * the status code defined in the DPP R2 tech spec.
   5738 	 * WLAN_STATUS_AKMP_NOT_VALID is addressed in the same manner as an
   5739 	 * interoperability workaround with older hostapd implementation. */
   5740 	if (DPP_VERSION > 1 && wpa_s->current_ssid &&
   5741 	    (wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_DPP ||
   5742 	     ((wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_DPP) &&
   5743 	      wpa_s->key_mgmt == WPA_KEY_MGMT_DPP)) &&
   5744 	    wpa_s->current_ssid->dpp_pfs == 0 &&
   5745 	    (data->assoc_reject.status_code ==
   5746 	     WLAN_STATUS_ASSOC_DENIED_UNSPEC ||
   5747 	     data->assoc_reject.status_code == WLAN_STATUS_AKMP_NOT_VALID)) {
   5748 		struct wpa_ssid *ssid = wpa_s->current_ssid;
   5749 		struct wpa_bss *bss = wpa_s->current_bss;
   5750 
   5751 		wpa_s->current_ssid->dpp_pfs_fallback ^= 1;
   5752 		if (!bss)
   5753 			bss = wpa_supplicant_get_new_bss(wpa_s, bssid);
   5754 		if (!bss || wpa_s->dpp_pfs_fallback) {
   5755 			wpa_printf(MSG_DEBUG,
   5756 				   "DPP: Updated PFS policy for next try");
   5757 			wpas_connection_failed(wpa_s, bssid, NULL);
   5758 			wpa_supplicant_mark_disassoc(wpa_s);
   5759 			return;
   5760 		}
   5761 		wpa_printf(MSG_DEBUG, "DPP: Try again with updated PFS policy");
   5762 		wpa_s->dpp_pfs_fallback = 1;
   5763 		wpas_connect_work_done(wpa_s);
   5764 		wpa_supplicant_mark_disassoc(wpa_s);
   5765 		wpa_supplicant_connect(wpa_s, bss, ssid);
   5766 		return;
   5767 	}
   5768 #endif /* CONFIG_DPP2 */
   5769 
   5770 #ifdef CONFIG_MBO
   5771 	if (data->assoc_reject.status_code ==
   5772 	    WLAN_STATUS_DENIED_POOR_CHANNEL_CONDITIONS &&
   5773 	    reject_bss && data->assoc_reject.resp_ies) {
   5774 		const u8 *rssi_rej;
   5775 
   5776 		rssi_rej = mbo_get_attr_from_ies(
   5777 			data->assoc_reject.resp_ies,
   5778 			data->assoc_reject.resp_ies_len,
   5779 			OCE_ATTR_ID_RSSI_BASED_ASSOC_REJECT);
   5780 		if (rssi_rej && rssi_rej[1] == 2) {
   5781 			wpa_printf(MSG_DEBUG,
   5782 				   "OCE: RSSI-based association rejection from "
   5783 				   MACSTR " (Delta RSSI: %u, Retry Delay: %u)",
   5784 				   MAC2STR(reject_bss->bssid),
   5785 				   rssi_rej[2], rssi_rej[3]);
   5786 			wpa_bss_tmp_disallow(wpa_s,
   5787 					     reject_bss->bssid,
   5788 					     rssi_rej[3],
   5789 					     rssi_rej[2] + reject_bss->level);
   5790 		}
   5791 	}
   5792 #endif /* CONFIG_MBO */
   5793 
   5794 	/* Check for other failed links in the response */
   5795 	os_memset(link_bssids, 0, sizeof(link_bssids));
   5796 	if (ieee802_11_parse_elems(data->assoc_reject.resp_ies,
   5797 				   data->assoc_reject.resp_ies_len,
   5798 				   &elems, 1) != ParseFailed) {
   5799 		unsigned int n_links, i, idx;
   5800 
   5801 		idx = 0;
   5802 		n_links = wpas_ml_parse_assoc(wpa_s, &elems, ml_info);
   5803 
   5804 		for (i = 1; i < n_links; i++) {
   5805 			/* The status cannot be success here.
   5806 			 * Add the link to the failed list if it is reporting
   5807 			 * an error. The only valid "non-error" status is
   5808 			 * TX_LINK_NOT_ACCEPTED as that means this link may
   5809 			 * still accept an association from us.
   5810 			 */
   5811 			if (ml_info[i].status !=
   5812 			    WLAN_STATUS_DENIED_TX_LINK_NOT_ACCEPTED) {
   5813 				link_bssids[idx] = ml_info[i].bssid;
   5814 				idx++;
   5815 			}
   5816 		}
   5817 	}
   5818 
   5819 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) {
   5820 		sme_event_assoc_reject(wpa_s, data, link_bssids);
   5821 		return;
   5822 	}
   5823 
   5824 	/* Driver-based SME cases */
   5825 
   5826 #ifdef CONFIG_SAE
   5827 	if (wpa_s->current_ssid &&
   5828 	    wpa_key_mgmt_sae(wpa_s->current_ssid->key_mgmt) &&
   5829 	    !data->assoc_reject.timed_out) {
   5830 		wpa_dbg(wpa_s, MSG_DEBUG, "SAE: Drop PMKSA cache entry");
   5831 		wpa_sm_aborted_cached(wpa_s->wpa);
   5832 		wpa_sm_pmksa_cache_flush(wpa_s->wpa, wpa_s->current_ssid);
   5833 	}
   5834 #endif /* CONFIG_SAE */
   5835 
   5836 #ifdef CONFIG_DPP
   5837 	if (wpa_s->current_ssid &&
   5838 	    wpa_s->current_ssid->key_mgmt == WPA_KEY_MGMT_DPP &&
   5839 	    !data->assoc_reject.timed_out) {
   5840 		wpa_dbg(wpa_s, MSG_DEBUG, "DPP: Drop PMKSA cache entry");
   5841 		wpa_sm_aborted_cached(wpa_s->wpa);
   5842 		wpa_sm_pmksa_cache_flush(wpa_s->wpa, wpa_s->current_ssid);
   5843 	}
   5844 #endif /* CONFIG_DPP */
   5845 
   5846 #ifdef CONFIG_FILS
   5847 	/* Update ERP next sequence number */
   5848 	if (wpa_s->auth_alg == WPA_AUTH_ALG_FILS) {
   5849 		fils_pmksa_cache_flush(wpa_s);
   5850 		eapol_sm_update_erp_next_seq_num(
   5851 			wpa_s->eapol,
   5852 			data->assoc_reject.fils_erp_next_seq_num);
   5853 		fils_connection_failure(wpa_s);
   5854 	}
   5855 #endif /* CONFIG_FILS */
   5856 
   5857 	wpas_connection_failed(wpa_s, bssid, link_bssids);
   5858 	wpa_supplicant_mark_disassoc(wpa_s);
   5859 }
   5860 
   5861 
   5862 static void wpas_event_unprot_beacon(struct wpa_supplicant *wpa_s,
   5863 				     struct unprot_beacon *data)
   5864 {
   5865 	struct wpabuf *buf;
   5866 	int res;
   5867 
   5868 	if (!data || wpa_s->wpa_state != WPA_COMPLETED ||
   5869 	    !ether_addr_equal(data->sa, wpa_s->bssid))
   5870 		return;
   5871 	wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_UNPROT_BEACON MACSTR,
   5872 		MAC2STR(data->sa));
   5873 
   5874 	buf = wpabuf_alloc(4);
   5875 	if (!buf)
   5876 		return;
   5877 
   5878 	wpabuf_put_u8(buf, WLAN_ACTION_WNM);
   5879 	wpabuf_put_u8(buf, WNM_NOTIFICATION_REQ);
   5880 	wpabuf_put_u8(buf, 1); /* Dialog Token */
   5881 	wpabuf_put_u8(buf, WNM_NOTIF_TYPE_BEACON_PROTECTION_FAILURE);
   5882 
   5883 	res = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
   5884 				  wpa_s->own_addr, wpa_s->bssid,
   5885 				  wpabuf_head(buf), wpabuf_len(buf), 0);
   5886 	if (res < 0)
   5887 		wpa_printf(MSG_DEBUG,
   5888 			   "Failed to send WNM-Notification Request frame");
   5889 
   5890 	wpabuf_free(buf);
   5891 }
   5892 
   5893 
   5894 static const char * bitmap_to_str(u8 value, char *buf)
   5895 {
   5896 	char *pos = buf;
   5897 	int i, k = 0;
   5898 
   5899 	for (i = 7; i >= 0; i--)
   5900 		pos[k++] = (value & BIT(i)) ? '1' : '0';
   5901 
   5902 	pos[8] = '\0';
   5903 	return pos;
   5904 }
   5905 
   5906 
   5907 static void wpas_tid_link_map(struct wpa_supplicant *wpa_s,
   5908 			      struct tid_link_map_info *info)
   5909 {
   5910 	char map_info[1000], *pos, *end;
   5911 	int res, i;
   5912 
   5913 	pos = map_info;
   5914 	end = pos + sizeof(map_info);
   5915 	res = os_snprintf(map_info, sizeof(map_info), "default=%d",
   5916 			  info->default_map);
   5917 	if (os_snprintf_error(end - pos, res))
   5918 		return;
   5919 	pos += res;
   5920 
   5921 	if (!info->default_map) {
   5922 		for_each_link(info->valid_links, i) {
   5923 			char uplink_map_str[9];
   5924 			char downlink_map_str[9];
   5925 
   5926 			bitmap_to_str(info->t2lmap[i].uplink, uplink_map_str);
   5927 			bitmap_to_str(info->t2lmap[i].downlink,
   5928 				      downlink_map_str);
   5929 
   5930 			res = os_snprintf(pos, end - pos,
   5931 					  " link_id=%d up_link=%s down_link=%s",
   5932 					  i, uplink_map_str,
   5933 					  downlink_map_str);
   5934 			if (os_snprintf_error(end - pos, res))
   5935 				return;
   5936 			pos += res;
   5937 		}
   5938 	}
   5939 
   5940 	wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_T2LM_UPDATE "%s", map_info);
   5941 }
   5942 
   5943 
   5944 static void wpas_link_reconfig(struct wpa_supplicant *wpa_s)
   5945 {
   5946 	u8 bssid[ETH_ALEN];
   5947 
   5948 	if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
   5949 		wpa_printf(MSG_ERROR, "LINK_RECONFIG: Failed to get BSSID");
   5950 		wpa_supplicant_deauthenticate(wpa_s,
   5951 					      WLAN_REASON_DEAUTH_LEAVING);
   5952 		return;
   5953 	}
   5954 
   5955 	if (!ether_addr_equal(bssid, wpa_s->bssid)) {
   5956 		os_memcpy(wpa_s->bssid, bssid, ETH_ALEN);
   5957 		wpa_supplicant_update_current_bss(wpa_s, wpa_s->bssid);
   5958 		wpas_notify_bssid_changed(wpa_s);
   5959 	}
   5960 
   5961 	if (wpa_drv_get_mlo_info(wpa_s) < 0) {
   5962 		wpa_printf(MSG_ERROR,
   5963 			   "LINK_RECONFIG: Failed to get MLO connection info");
   5964 		wpa_supplicant_deauthenticate(wpa_s,
   5965 					      WLAN_REASON_DEAUTH_LEAVING);
   5966 		return;
   5967 	}
   5968 
   5969 	if (wpa_sm_set_ml_info(wpa_s)) {
   5970 		wpa_printf(MSG_ERROR,
   5971 			   "LINK_RECONFIG: Failed to set MLO connection info to wpa_sm");
   5972 		wpa_supplicant_deauthenticate(wpa_s,
   5973 					      WLAN_REASON_DEAUTH_LEAVING);
   5974 		return;
   5975 	}
   5976 
   5977 	wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_LINK_RECONFIG "valid_links=0x%x",
   5978 		wpa_s->valid_links);
   5979 }
   5980 
   5981 
   5982 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
   5983 			  union wpa_event_data *data)
   5984 {
   5985 	struct wpa_supplicant *wpa_s = ctx;
   5986 	int resched;
   5987 	struct os_reltime age, clear_at;
   5988 #ifndef CONFIG_NO_STDOUT_DEBUG
   5989 	int level = MSG_DEBUG;
   5990 #endif /* CONFIG_NO_STDOUT_DEBUG */
   5991 
   5992 	if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED &&
   5993 	    event != EVENT_INTERFACE_ENABLED &&
   5994 	    event != EVENT_INTERFACE_STATUS &&
   5995 	    event != EVENT_SCAN_RESULTS &&
   5996 	    event != EVENT_SCHED_SCAN_STOPPED) {
   5997 		wpa_dbg(wpa_s, MSG_DEBUG,
   5998 			"Ignore event %s (%d) while interface is disabled",
   5999 			event_to_string(event), event);
   6000 		return;
   6001 	}
   6002 
   6003 #ifndef CONFIG_NO_STDOUT_DEBUG
   6004 	if (event == EVENT_RX_MGMT && data->rx_mgmt.frame_len >= 24) {
   6005 		const struct ieee80211_hdr *hdr;
   6006 		u16 fc;
   6007 		hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
   6008 		fc = le_to_host16(hdr->frame_control);
   6009 		if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
   6010 		    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
   6011 			level = MSG_EXCESSIVE;
   6012 	}
   6013 
   6014 	wpa_dbg(wpa_s, level, "Event %s (%d) received",
   6015 		event_to_string(event), event);
   6016 #endif /* CONFIG_NO_STDOUT_DEBUG */
   6017 
   6018 	switch (event) {
   6019 	case EVENT_AUTH:
   6020 #ifdef CONFIG_FST
   6021 		if (!wpas_fst_update_mbie(wpa_s, data->auth.ies,
   6022 					  data->auth.ies_len))
   6023 			wpa_printf(MSG_DEBUG,
   6024 				   "FST: MB IEs updated from auth IE");
   6025 #endif /* CONFIG_FST */
   6026 		sme_event_auth(wpa_s, data);
   6027 		wpa_s->auth_status_code = data->auth.status_code;
   6028 		wpas_notify_auth_status_code(wpa_s);
   6029 		break;
   6030 	case EVENT_ASSOC:
   6031 #ifdef CONFIG_TESTING_OPTIONS
   6032 		if (wpa_s->ignore_auth_resp) {
   6033 			wpa_printf(MSG_INFO,
   6034 				   "EVENT_ASSOC - ignore_auth_resp active!");
   6035 			break;
   6036 		}
   6037 		if (wpa_s->testing_resend_assoc) {
   6038 			wpa_printf(MSG_INFO,
   6039 				   "EVENT_DEAUTH - testing_resend_assoc");
   6040 			break;
   6041 		}
   6042 #endif /* CONFIG_TESTING_OPTIONS */
   6043 		if (wpa_s->disconnected) {
   6044 			wpa_printf(MSG_INFO,
   6045 				   "Ignore unexpected EVENT_ASSOC in disconnected state");
   6046 			break;
   6047 		}
   6048 		wpa_supplicant_event_assoc(wpa_s, data);
   6049 		wpa_s->assoc_status_code = WLAN_STATUS_SUCCESS;
   6050 		if (data &&
   6051 		    (data->assoc_info.authorized ||
   6052 		     (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
   6053 		      wpa_fils_is_completed(wpa_s->wpa))))
   6054 			wpa_supplicant_event_assoc_auth(wpa_s, data);
   6055 		if (data) {
   6056 			wpa_msg(wpa_s, MSG_INFO,
   6057 				WPA_EVENT_SUBNET_STATUS_UPDATE "status=%u",
   6058 				data->assoc_info.subnet_status);
   6059 		}
   6060 		break;
   6061 	case EVENT_DISASSOC:
   6062 		wpas_event_disassoc(wpa_s,
   6063 				    data ? &data->disassoc_info : NULL);
   6064 		break;
   6065 	case EVENT_DEAUTH:
   6066 #ifdef CONFIG_TESTING_OPTIONS
   6067 		if (wpa_s->ignore_auth_resp) {
   6068 			wpa_printf(MSG_INFO,
   6069 				   "EVENT_DEAUTH - ignore_auth_resp active!");
   6070 			break;
   6071 		}
   6072 		if (wpa_s->testing_resend_assoc) {
   6073 			wpa_printf(MSG_INFO,
   6074 				   "EVENT_DEAUTH - testing_resend_assoc");
   6075 			break;
   6076 		}
   6077 #endif /* CONFIG_TESTING_OPTIONS */
   6078 		wpas_event_deauth(wpa_s,
   6079 				  data ? &data->deauth_info : NULL);
   6080 		break;
   6081 	case EVENT_LINK_RECONFIG:
   6082 		wpas_link_reconfig(wpa_s);
   6083 		break;
   6084 	case EVENT_MICHAEL_MIC_FAILURE:
   6085 		wpa_supplicant_event_michael_mic_failure(wpa_s, data);
   6086 		break;
   6087 #ifndef CONFIG_NO_SCAN_PROCESSING
   6088 	case EVENT_SCAN_STARTED:
   6089 		if (wpa_s->own_scan_requested ||
   6090 		    (data && !data->scan_info.external_scan)) {
   6091 			struct os_reltime diff;
   6092 
   6093 			os_get_reltime(&wpa_s->scan_start_time);
   6094 			os_reltime_sub(&wpa_s->scan_start_time,
   6095 				       &wpa_s->scan_trigger_time, &diff);
   6096 			wpa_dbg(wpa_s, MSG_DEBUG, "Own scan request started a scan in %jd.%06ld seconds",
   6097 				(intmax_t)diff.sec, (long)diff.usec);
   6098 			wpa_s->own_scan_requested = 0;
   6099 			wpa_s->own_scan_running = 1;
   6100 			if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
   6101 			    wpa_s->manual_scan_use_id) {
   6102 				wpa_msg_ctrl(wpa_s, MSG_INFO,
   6103 					     WPA_EVENT_SCAN_STARTED "id=%u",
   6104 					     wpa_s->manual_scan_id);
   6105 			} else {
   6106 				wpa_msg_ctrl(wpa_s, MSG_INFO,
   6107 					     WPA_EVENT_SCAN_STARTED);
   6108 			}
   6109 		} else {
   6110 			wpa_dbg(wpa_s, MSG_DEBUG, "External program started a scan");
   6111 			wpa_s->radio->external_scan_req_interface = wpa_s;
   6112 			wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_STARTED);
   6113 		}
   6114 		break;
   6115 	case EVENT_SCAN_RESULTS:
   6116 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
   6117 			wpa_s->scan_res_handler = NULL;
   6118 			wpa_s->own_scan_running = 0;
   6119 			wpa_s->radio->external_scan_req_interface = NULL;
   6120 			wpa_s->last_scan_req = NORMAL_SCAN_REQ;
   6121 			break;
   6122 		}
   6123 
   6124 		if (!(data && data->scan_info.external_scan) &&
   6125 		    os_reltime_initialized(&wpa_s->scan_start_time)) {
   6126 			struct os_reltime now, diff;
   6127 			os_get_reltime(&now);
   6128 			os_reltime_sub(&now, &wpa_s->scan_start_time, &diff);
   6129 			wpa_s->scan_start_time.sec = 0;
   6130 			wpa_s->scan_start_time.usec = 0;
   6131 			wpa_s->wps_scan_done = true;
   6132 			wpa_dbg(wpa_s, MSG_DEBUG, "Scan completed in %jd.%06ld seconds",
   6133 				(intmax_t)diff.sec, (long)diff.usec);
   6134 		}
   6135 		if (wpa_supplicant_event_scan_results(wpa_s, data))
   6136 			break; /* interface may have been removed */
   6137 		if (!(data && data->scan_info.external_scan))
   6138 			wpa_s->own_scan_running = 0;
   6139 		if (data && data->scan_info.nl_scan_event)
   6140 			wpa_s->radio->external_scan_req_interface = NULL;
   6141 		radio_work_check_next(wpa_s);
   6142 		break;
   6143 #endif /* CONFIG_NO_SCAN_PROCESSING */
   6144 	case EVENT_ASSOCINFO:
   6145 		wpa_supplicant_event_associnfo(wpa_s, data);
   6146 		break;
   6147 	case EVENT_INTERFACE_STATUS:
   6148 		wpa_supplicant_event_interface_status(wpa_s, data);
   6149 		break;
   6150 	case EVENT_PMKID_CANDIDATE:
   6151 		wpa_supplicant_event_pmkid_candidate(wpa_s, data);
   6152 		break;
   6153 #ifdef CONFIG_TDLS
   6154 	case EVENT_TDLS:
   6155 		wpa_supplicant_event_tdls(wpa_s, data);
   6156 		break;
   6157 #endif /* CONFIG_TDLS */
   6158 #ifdef CONFIG_WNM
   6159 	case EVENT_WNM:
   6160 		wpa_supplicant_event_wnm(wpa_s, data);
   6161 		break;
   6162 #endif /* CONFIG_WNM */
   6163 #ifdef CONFIG_IEEE80211R
   6164 	case EVENT_FT_RESPONSE:
   6165 		wpa_supplicant_event_ft_response(wpa_s, data);
   6166 		break;
   6167 #endif /* CONFIG_IEEE80211R */
   6168 #ifdef CONFIG_IBSS_RSN
   6169 	case EVENT_IBSS_RSN_START:
   6170 		wpa_supplicant_event_ibss_rsn_start(wpa_s, data);
   6171 		break;
   6172 #endif /* CONFIG_IBSS_RSN */
   6173 	case EVENT_ASSOC_REJECT:
   6174 		wpas_event_assoc_reject(wpa_s, data);
   6175 		break;
   6176 	case EVENT_AUTH_TIMED_OUT:
   6177 		/* It is possible to get this event from earlier connection */
   6178 		if (wpa_s->current_ssid &&
   6179 		    wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
   6180 			wpa_dbg(wpa_s, MSG_DEBUG,
   6181 				"Ignore AUTH_TIMED_OUT in mesh configuration");
   6182 			break;
   6183 		}
   6184 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
   6185 			sme_event_auth_timed_out(wpa_s, data);
   6186 		break;
   6187 	case EVENT_ASSOC_TIMED_OUT:
   6188 		/* It is possible to get this event from earlier connection */
   6189 		if (wpa_s->current_ssid &&
   6190 		    wpa_s->current_ssid->mode == WPAS_MODE_MESH) {
   6191 			wpa_dbg(wpa_s, MSG_DEBUG,
   6192 				"Ignore ASSOC_TIMED_OUT in mesh configuration");
   6193 			break;
   6194 		}
   6195 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
   6196 			sme_event_assoc_timed_out(wpa_s, data);
   6197 		break;
   6198 	case EVENT_TX_STATUS:
   6199 		wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS dst=" MACSTR
   6200 			" type=%d stype=%d",
   6201 			MAC2STR(data->tx_status.dst),
   6202 			data->tx_status.type, data->tx_status.stype);
   6203 #ifdef CONFIG_WNM
   6204 		if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
   6205 		    data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
   6206 		    wnm_btm_resp_tx_status(wpa_s, data->tx_status.data,
   6207 					   data->tx_status.data_len) == 0)
   6208 			break;
   6209 #endif /* CONFIG_WNM */
   6210 #ifdef CONFIG_PASN
   6211 		if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
   6212 		    data->tx_status.stype == WLAN_FC_STYPE_AUTH &&
   6213 		    wpas_pasn_auth_tx_status(wpa_s, data->tx_status.data,
   6214 					     data->tx_status.data_len,
   6215 					     data->tx_status.ack) == 0)
   6216 			break;
   6217 #endif /* CONFIG_PASN */
   6218 #ifdef CONFIG_AP
   6219 		if (wpa_s->ap_iface == NULL) {
   6220 #ifdef CONFIG_OFFCHANNEL
   6221 			if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
   6222 			    data->tx_status.stype == WLAN_FC_STYPE_ACTION)
   6223 				offchannel_send_action_tx_status(
   6224 					wpa_s, data->tx_status.dst,
   6225 					data->tx_status.data,
   6226 					data->tx_status.data_len,
   6227 					data->tx_status.ack ?
   6228 					OFFCHANNEL_SEND_ACTION_SUCCESS :
   6229 					OFFCHANNEL_SEND_ACTION_NO_ACK);
   6230 #endif /* CONFIG_OFFCHANNEL */
   6231 			break;
   6232 		}
   6233 #endif /* CONFIG_AP */
   6234 #ifdef CONFIG_OFFCHANNEL
   6235 		wpa_dbg(wpa_s, MSG_DEBUG, "EVENT_TX_STATUS pending_dst="
   6236 			MACSTR, MAC2STR(wpa_s->p2pdev->pending_action_dst));
   6237 		/*
   6238 		 * Catch TX status events for Action frames we sent via group
   6239 		 * interface in GO mode, or via standalone AP interface.
   6240 		 * Note, wpa_s->p2pdev will be the same as wpa_s->parent,
   6241 		 * except when the primary interface is used as a GO interface
   6242 		 * (for drivers which do not have group interface concurrency)
   6243 		 */
   6244 		if (data->tx_status.type == WLAN_FC_TYPE_MGMT &&
   6245 		    data->tx_status.stype == WLAN_FC_STYPE_ACTION &&
   6246 		    ether_addr_equal(wpa_s->p2pdev->pending_action_dst,
   6247 				     data->tx_status.dst)) {
   6248 			offchannel_send_action_tx_status(
   6249 				wpa_s->p2pdev, data->tx_status.dst,
   6250 				data->tx_status.data,
   6251 				data->tx_status.data_len,
   6252 				data->tx_status.ack ?
   6253 				OFFCHANNEL_SEND_ACTION_SUCCESS :
   6254 				OFFCHANNEL_SEND_ACTION_NO_ACK);
   6255 			break;
   6256 		}
   6257 #endif /* CONFIG_OFFCHANNEL */
   6258 #ifdef CONFIG_AP
   6259 		switch (data->tx_status.type) {
   6260 		case WLAN_FC_TYPE_MGMT:
   6261 			ap_mgmt_tx_cb(wpa_s, data->tx_status.data,
   6262 				      data->tx_status.data_len,
   6263 				      data->tx_status.stype,
   6264 				      data->tx_status.ack);
   6265 			break;
   6266 		case WLAN_FC_TYPE_DATA:
   6267 			ap_tx_status(wpa_s, data->tx_status.dst,
   6268 				     data->tx_status.data,
   6269 				     data->tx_status.data_len,
   6270 				     data->tx_status.ack);
   6271 			break;
   6272 		}
   6273 #endif /* CONFIG_AP */
   6274 		break;
   6275 #ifdef CONFIG_AP
   6276 	case EVENT_EAPOL_TX_STATUS:
   6277 		ap_eapol_tx_status(wpa_s, data->eapol_tx_status.dst,
   6278 				   data->eapol_tx_status.data,
   6279 				   data->eapol_tx_status.data_len,
   6280 				   data->eapol_tx_status.ack);
   6281 		break;
   6282 	case EVENT_DRIVER_CLIENT_POLL_OK:
   6283 		ap_client_poll_ok(wpa_s, data->client_poll.addr);
   6284 		break;
   6285 	case EVENT_RX_FROM_UNKNOWN:
   6286 		if (wpa_s->ap_iface == NULL)
   6287 			break;
   6288 		ap_rx_from_unknown_sta(wpa_s, data->rx_from_unknown.addr,
   6289 				       data->rx_from_unknown.wds);
   6290 		break;
   6291 #endif /* CONFIG_AP */
   6292 
   6293 	case EVENT_LINK_CH_SWITCH_STARTED:
   6294 	case EVENT_LINK_CH_SWITCH:
   6295 		if (!data || !wpa_s->current_ssid ||
   6296 		    !(wpa_s->valid_links & BIT(data->ch_switch.link_id)))
   6297 			break;
   6298 
   6299 		wpa_msg(wpa_s, MSG_INFO,
   6300 			"%sfreq=%d link_id=%d ht_enabled=%d ch_offset=%d ch_width=%s cf1=%d cf2=%d",
   6301 			event == EVENT_LINK_CH_SWITCH ?
   6302 			WPA_EVENT_LINK_CHANNEL_SWITCH :
   6303 			WPA_EVENT_LINK_CHANNEL_SWITCH_STARTED,
   6304 			data->ch_switch.freq,
   6305 			data->ch_switch.link_id,
   6306 			data->ch_switch.ht_enabled,
   6307 			data->ch_switch.ch_offset,
   6308 			channel_width_to_string(data->ch_switch.ch_width),
   6309 			data->ch_switch.cf1,
   6310 			data->ch_switch.cf2);
   6311 		if (event == EVENT_LINK_CH_SWITCH_STARTED)
   6312 			break;
   6313 
   6314 		wpa_s->links[data->ch_switch.link_id].freq =
   6315 			data->ch_switch.freq;
   6316 		if (wpa_s->links[data->ch_switch.link_id].bss &&
   6317 		    wpa_s->links[data->ch_switch.link_id].bss->freq !=
   6318 		    data->ch_switch.freq) {
   6319 			wpa_s->links[data->ch_switch.link_id].bss->freq =
   6320 				data->ch_switch.freq;
   6321 			notify_bss_changes(
   6322 				wpa_s, WPA_BSS_FREQ_CHANGED_FLAG,
   6323 				wpa_s->links[data->ch_switch.link_id].bss);
   6324 		}
   6325 		break;
   6326 	case EVENT_CH_SWITCH_STARTED:
   6327 	case EVENT_CH_SWITCH:
   6328 		if (!data || !wpa_s->current_ssid)
   6329 			break;
   6330 
   6331 		wpa_msg(wpa_s, MSG_INFO,
   6332 			"%sfreq=%d ht_enabled=%d ch_offset=%d ch_width=%s cf1=%d cf2=%d",
   6333 			event == EVENT_CH_SWITCH ? WPA_EVENT_CHANNEL_SWITCH :
   6334 			WPA_EVENT_CHANNEL_SWITCH_STARTED,
   6335 			data->ch_switch.freq,
   6336 			data->ch_switch.ht_enabled,
   6337 			data->ch_switch.ch_offset,
   6338 			channel_width_to_string(data->ch_switch.ch_width),
   6339 			data->ch_switch.cf1,
   6340 			data->ch_switch.cf2);
   6341 		if (event == EVENT_CH_SWITCH_STARTED)
   6342 			break;
   6343 
   6344 		wpa_s->assoc_freq = data->ch_switch.freq;
   6345 		wpa_s->current_ssid->frequency = data->ch_switch.freq;
   6346 		if (wpa_s->current_bss &&
   6347 		    wpa_s->current_bss->freq != data->ch_switch.freq) {
   6348 			wpa_s->current_bss->freq = data->ch_switch.freq;
   6349 			notify_bss_changes(wpa_s, WPA_BSS_FREQ_CHANGED_FLAG,
   6350 					   wpa_s->current_bss);
   6351 		}
   6352 
   6353 #ifdef CONFIG_SME
   6354 		switch (data->ch_switch.ch_offset) {
   6355 		case 1:
   6356 			wpa_s->sme.ht_sec_chan = HT_SEC_CHAN_ABOVE;
   6357 			break;
   6358 		case -1:
   6359 			wpa_s->sme.ht_sec_chan = HT_SEC_CHAN_BELOW;
   6360 			break;
   6361 		default:
   6362 			wpa_s->sme.ht_sec_chan = HT_SEC_CHAN_UNKNOWN;
   6363 			break;
   6364 		}
   6365 #endif /* CONFIG_SME */
   6366 
   6367 #ifdef CONFIG_AP
   6368 		if (wpa_s->current_ssid->mode == WPAS_MODE_AP ||
   6369 		    wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO ||
   6370 		    wpa_s->current_ssid->mode == WPAS_MODE_MESH ||
   6371 		    wpa_s->current_ssid->mode ==
   6372 		    WPAS_MODE_P2P_GROUP_FORMATION) {
   6373 			wpas_ap_ch_switch(wpa_s, data->ch_switch.freq,
   6374 					  data->ch_switch.ht_enabled,
   6375 					  data->ch_switch.ch_offset,
   6376 					  data->ch_switch.ch_width,
   6377 					  data->ch_switch.cf1,
   6378 					  data->ch_switch.cf2,
   6379 					  data->ch_switch.punct_bitmap,
   6380 					  1);
   6381 		}
   6382 #endif /* CONFIG_AP */
   6383 
   6384 		if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
   6385 			sme_event_ch_switch(wpa_s);
   6386 
   6387 		wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_CS);
   6388 		wnm_clear_coloc_intf_reporting(wpa_s);
   6389 		break;
   6390 #ifdef CONFIG_AP
   6391 #ifdef NEED_AP_MLME
   6392 	case EVENT_DFS_RADAR_DETECTED:
   6393 		if (data)
   6394 			wpas_ap_event_dfs_radar_detected(wpa_s,
   6395 							 &data->dfs_event);
   6396 		break;
   6397 	case EVENT_DFS_NOP_FINISHED:
   6398 		if (data)
   6399 			wpas_ap_event_dfs_cac_nop_finished(wpa_s,
   6400 							   &data->dfs_event);
   6401 		break;
   6402 #endif /* NEED_AP_MLME */
   6403 #endif /* CONFIG_AP */
   6404 	case EVENT_DFS_CAC_STARTED:
   6405 		if (data)
   6406 			wpas_event_dfs_cac_started(wpa_s, &data->dfs_event);
   6407 		break;
   6408 	case EVENT_DFS_CAC_FINISHED:
   6409 		if (data)
   6410 			wpas_event_dfs_cac_finished(wpa_s, &data->dfs_event);
   6411 		break;
   6412 	case EVENT_DFS_CAC_ABORTED:
   6413 		if (data)
   6414 			wpas_event_dfs_cac_aborted(wpa_s, &data->dfs_event);
   6415 		break;
   6416 	case EVENT_RX_MGMT: {
   6417 		u16 fc, stype;
   6418 		const struct ieee80211_mgmt *mgmt;
   6419 
   6420 #ifdef CONFIG_TESTING_OPTIONS
   6421 		if (wpa_s->ext_mgmt_frame_handling) {
   6422 			struct rx_mgmt *rx = &data->rx_mgmt;
   6423 			size_t hex_len = 2 * rx->frame_len + 1;
   6424 			char *hex = os_malloc(hex_len);
   6425 			if (hex) {
   6426 				wpa_snprintf_hex(hex, hex_len,
   6427 						 rx->frame, rx->frame_len);
   6428 				wpa_msg(wpa_s, MSG_INFO, "MGMT-RX freq=%d datarate=%u ssi_signal=%d %s",
   6429 					rx->freq, rx->datarate, rx->ssi_signal,
   6430 					hex);
   6431 				os_free(hex);
   6432 			}
   6433 			break;
   6434 		}
   6435 #endif /* CONFIG_TESTING_OPTIONS */
   6436 
   6437 		mgmt = (const struct ieee80211_mgmt *)
   6438 			data->rx_mgmt.frame;
   6439 		fc = le_to_host16(mgmt->frame_control);
   6440 		stype = WLAN_FC_GET_STYPE(fc);
   6441 
   6442 #ifdef CONFIG_AP
   6443 		if (wpa_s->ap_iface == NULL) {
   6444 #endif /* CONFIG_AP */
   6445 #ifdef CONFIG_P2P
   6446 			if (stype == WLAN_FC_STYPE_PROBE_REQ &&
   6447 			    data->rx_mgmt.frame_len > IEEE80211_HDRLEN) {
   6448 				const u8 *src = mgmt->sa;
   6449 				const u8 *ie;
   6450 				size_t ie_len;
   6451 
   6452 				ie = data->rx_mgmt.frame + IEEE80211_HDRLEN;
   6453 				ie_len = data->rx_mgmt.frame_len -
   6454 					IEEE80211_HDRLEN;
   6455 				wpas_p2p_probe_req_rx(
   6456 					wpa_s, src, mgmt->da,
   6457 					mgmt->bssid, ie, ie_len,
   6458 					data->rx_mgmt.freq,
   6459 					data->rx_mgmt.ssi_signal);
   6460 				break;
   6461 			}
   6462 #endif /* CONFIG_P2P */
   6463 #ifdef CONFIG_IBSS_RSN
   6464 			if (wpa_s->current_ssid &&
   6465 			    wpa_s->current_ssid->mode == WPAS_MODE_IBSS &&
   6466 			    stype == WLAN_FC_STYPE_AUTH &&
   6467 			    data->rx_mgmt.frame_len >= 30) {
   6468 				wpa_supplicant_event_ibss_auth(wpa_s, data);
   6469 				break;
   6470 			}
   6471 #endif /* CONFIG_IBSS_RSN */
   6472 
   6473 			if (stype == WLAN_FC_STYPE_ACTION) {
   6474 				wpas_event_rx_mgmt_action(
   6475 					wpa_s, data->rx_mgmt.frame,
   6476 					data->rx_mgmt.frame_len,
   6477 					data->rx_mgmt.freq,
   6478 					data->rx_mgmt.ssi_signal);
   6479 				break;
   6480 			}
   6481 
   6482 			if (wpa_s->ifmsh) {
   6483 				mesh_mpm_mgmt_rx(wpa_s, &data->rx_mgmt);
   6484 				break;
   6485 			}
   6486 #ifdef CONFIG_PASN
   6487 			if (stype == WLAN_FC_STYPE_AUTH &&
   6488 			    wpas_pasn_auth_rx(wpa_s, mgmt,
   6489 					      data->rx_mgmt.frame_len) != -2)
   6490 				break;
   6491 #endif /* CONFIG_PASN */
   6492 
   6493 #ifdef CONFIG_SAE
   6494 			if (stype == WLAN_FC_STYPE_AUTH &&
   6495 			    !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
   6496 			    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE)) {
   6497 				sme_external_auth_mgmt_rx(
   6498 					wpa_s, data->rx_mgmt.frame,
   6499 					data->rx_mgmt.frame_len);
   6500 				break;
   6501 			}
   6502 #endif /* CONFIG_SAE */
   6503 			wpa_dbg(wpa_s, MSG_DEBUG, "AP: ignore received "
   6504 				"management frame in non-AP mode");
   6505 			break;
   6506 #ifdef CONFIG_AP
   6507 		}
   6508 
   6509 		if (stype == WLAN_FC_STYPE_PROBE_REQ &&
   6510 		    data->rx_mgmt.frame_len > IEEE80211_HDRLEN) {
   6511 			const u8 *ie;
   6512 			size_t ie_len;
   6513 
   6514 			ie = data->rx_mgmt.frame + IEEE80211_HDRLEN;
   6515 			ie_len = data->rx_mgmt.frame_len - IEEE80211_HDRLEN;
   6516 
   6517 			wpas_notify_preq(wpa_s, mgmt->sa, mgmt->da,
   6518 					 mgmt->bssid, ie, ie_len,
   6519 					 data->rx_mgmt.ssi_signal);
   6520 		}
   6521 
   6522 		ap_mgmt_rx(wpa_s, &data->rx_mgmt);
   6523 #endif /* CONFIG_AP */
   6524 		break;
   6525 		}
   6526 	case EVENT_RX_PROBE_REQ:
   6527 		if (data->rx_probe_req.sa == NULL ||
   6528 		    data->rx_probe_req.ie == NULL)
   6529 			break;
   6530 #ifdef CONFIG_AP
   6531 		if (wpa_s->ap_iface) {
   6532 			hostapd_probe_req_rx(wpa_s->ap_iface->bss[0],
   6533 					     data->rx_probe_req.sa,
   6534 					     data->rx_probe_req.da,
   6535 					     data->rx_probe_req.bssid,
   6536 					     data->rx_probe_req.ie,
   6537 					     data->rx_probe_req.ie_len,
   6538 					     data->rx_probe_req.ssi_signal);
   6539 			break;
   6540 		}
   6541 #endif /* CONFIG_AP */
   6542 		wpas_p2p_probe_req_rx(wpa_s, data->rx_probe_req.sa,
   6543 				      data->rx_probe_req.da,
   6544 				      data->rx_probe_req.bssid,
   6545 				      data->rx_probe_req.ie,
   6546 				      data->rx_probe_req.ie_len,
   6547 				      0,
   6548 				      data->rx_probe_req.ssi_signal);
   6549 		break;
   6550 	case EVENT_REMAIN_ON_CHANNEL:
   6551 #ifdef CONFIG_OFFCHANNEL
   6552 		offchannel_remain_on_channel_cb(
   6553 			wpa_s, data->remain_on_channel.freq,
   6554 			data->remain_on_channel.duration);
   6555 #endif /* CONFIG_OFFCHANNEL */
   6556 		wpas_p2p_remain_on_channel_cb(
   6557 			wpa_s, data->remain_on_channel.freq,
   6558 			data->remain_on_channel.duration);
   6559 #ifdef CONFIG_DPP
   6560 		wpas_dpp_remain_on_channel_cb(
   6561 			wpa_s, data->remain_on_channel.freq,
   6562 			data->remain_on_channel.duration);
   6563 #endif /* CONFIG_DPP */
   6564 #ifdef CONFIG_NAN_USD
   6565 		wpas_nan_usd_remain_on_channel_cb(
   6566 			wpa_s, data->remain_on_channel.freq,
   6567 			data->remain_on_channel.duration);
   6568 #endif /* CONFIG_NAN_USD */
   6569 		break;
   6570 	case EVENT_CANCEL_REMAIN_ON_CHANNEL:
   6571 #ifdef CONFIG_OFFCHANNEL
   6572 		offchannel_cancel_remain_on_channel_cb(
   6573 			wpa_s, data->remain_on_channel.freq);
   6574 #endif /* CONFIG_OFFCHANNEL */
   6575 		wpas_p2p_cancel_remain_on_channel_cb(
   6576 			wpa_s, data->remain_on_channel.freq);
   6577 #ifdef CONFIG_DPP
   6578 		wpas_dpp_cancel_remain_on_channel_cb(
   6579 			wpa_s, data->remain_on_channel.freq);
   6580 #endif /* CONFIG_DPP */
   6581 #ifdef CONFIG_NAN_USD
   6582 		wpas_nan_usd_cancel_remain_on_channel_cb(
   6583 			wpa_s, data->remain_on_channel.freq);
   6584 #endif /* CONFIG_NAN_USD */
   6585 		break;
   6586 	case EVENT_EAPOL_RX:
   6587 		wpa_supplicant_rx_eapol(wpa_s, data->eapol_rx.src,
   6588 					data->eapol_rx.data,
   6589 					data->eapol_rx.data_len,
   6590 					data->eapol_rx.encrypted);
   6591 		break;
   6592 	case EVENT_SIGNAL_CHANGE:
   6593 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SIGNAL_CHANGE
   6594 			"above=%d signal=%d noise=%d txrate=%lu",
   6595 			data->signal_change.above_threshold,
   6596 			data->signal_change.data.signal,
   6597 			data->signal_change.current_noise,
   6598 			data->signal_change.data.current_tx_rate);
   6599 		wpa_bss_update_level(wpa_s->current_bss,
   6600 				     data->signal_change.data.signal);
   6601 		bgscan_notify_signal_change(
   6602 			wpa_s, data->signal_change.above_threshold,
   6603 			data->signal_change.data.signal,
   6604 			data->signal_change.current_noise,
   6605 			data->signal_change.data.current_tx_rate);
   6606 		os_memcpy(&wpa_s->last_signal_info, data,
   6607 			  sizeof(struct wpa_signal_info));
   6608 		wpas_notify_signal_change(wpa_s);
   6609 		break;
   6610 	case EVENT_INTERFACE_MAC_CHANGED:
   6611 		wpa_supplicant_update_mac_addr(wpa_s);
   6612 		wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
   6613 		break;
   6614 	case EVENT_INTERFACE_ENABLED:
   6615 		wpa_dbg(wpa_s, MSG_DEBUG, "Interface was enabled");
   6616 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
   6617 			u8 addr[ETH_ALEN];
   6618 
   6619 			eloop_cancel_timeout(wpas_clear_disabled_interface,
   6620 					     wpa_s, NULL);
   6621 			os_memcpy(addr, wpa_s->own_addr, ETH_ALEN);
   6622 			wpa_supplicant_update_mac_addr(wpa_s);
   6623 			if (!ether_addr_equal(addr, wpa_s->own_addr))
   6624 				wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
   6625 			else
   6626 				wpa_sm_pmksa_cache_reconfig(wpa_s->wpa);
   6627 			wpa_supplicant_set_default_scan_ies(wpa_s);
   6628 			if (wpa_s->p2p_mgmt) {
   6629 				wpa_supplicant_set_state(wpa_s,
   6630 							 WPA_DISCONNECTED);
   6631 				break;
   6632 			}
   6633 
   6634 #ifdef CONFIG_AP
   6635 			if (!wpa_s->ap_iface) {
   6636 				wpa_supplicant_set_state(wpa_s,
   6637 							 WPA_DISCONNECTED);
   6638 				wpa_s->scan_req = NORMAL_SCAN_REQ;
   6639 				wpa_supplicant_req_scan(wpa_s, 0, 0);
   6640 			} else
   6641 				wpa_supplicant_set_state(wpa_s,
   6642 							 WPA_COMPLETED);
   6643 #else /* CONFIG_AP */
   6644 			wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
   6645 			wpa_supplicant_req_scan(wpa_s, 0, 0);
   6646 #endif /* CONFIG_AP */
   6647 		}
   6648 		break;
   6649 	case EVENT_INTERFACE_DISABLED:
   6650 		wpa_dbg(wpa_s, MSG_DEBUG, "Interface was disabled");
   6651 #ifdef CONFIG_P2P
   6652 		if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
   6653 		    (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group &&
   6654 		     wpa_s->current_ssid->mode == WPAS_MODE_P2P_GO)) {
   6655 			/*
   6656 			 * Mark interface disabled if this happens to end up not
   6657 			 * being removed as a separate P2P group interface.
   6658 			 */
   6659 			wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
   6660 			/*
   6661 			 * The interface was externally disabled. Remove
   6662 			 * it assuming an external entity will start a
   6663 			 * new session if needed.
   6664 			 */
   6665 			if (wpa_s->current_ssid &&
   6666 			    wpa_s->current_ssid->p2p_group)
   6667 				wpas_p2p_interface_unavailable(wpa_s);
   6668 			else
   6669 				wpas_p2p_disconnect(wpa_s);
   6670 			/*
   6671 			 * wpa_s instance may have been freed, so must not use
   6672 			 * it here anymore.
   6673 			 */
   6674 			break;
   6675 		}
   6676 		if (wpa_s->p2p_scan_work && wpa_s->global->p2p &&
   6677 		    p2p_in_progress(wpa_s->global->p2p) > 1) {
   6678 			/* This radio work will be cancelled, so clear P2P
   6679 			 * state as well.
   6680 			 */
   6681 			p2p_stop_find(wpa_s->global->p2p);
   6682 		}
   6683 #endif /* CONFIG_P2P */
   6684 
   6685 		if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
   6686 			/*
   6687 			 * Indicate disconnection to keep ctrl_iface events
   6688 			 * consistent.
   6689 			 */
   6690 			wpa_supplicant_event_disassoc(
   6691 				wpa_s, WLAN_REASON_DEAUTH_LEAVING, 1);
   6692 		}
   6693 		wpa_supplicant_mark_disassoc(wpa_s);
   6694 		os_reltime_age(&wpa_s->last_scan, &age);
   6695 		if (age.sec >= wpa_s->conf->scan_res_valid_for_connect) {
   6696 			clear_at.sec = wpa_s->conf->scan_res_valid_for_connect;
   6697 			clear_at.usec = 0;
   6698 		} else {
   6699 			struct os_reltime tmp;
   6700 
   6701 			tmp.sec = wpa_s->conf->scan_res_valid_for_connect;
   6702 			tmp.usec = 0;
   6703 			os_reltime_sub(&tmp, &age, &clear_at);
   6704 		}
   6705 		eloop_register_timeout(clear_at.sec, clear_at.usec,
   6706 				       wpas_clear_disabled_interface,
   6707 				       wpa_s, NULL);
   6708 		radio_remove_works(wpa_s, NULL, 0);
   6709 
   6710 		wpa_supplicant_set_state(wpa_s, WPA_INTERFACE_DISABLED);
   6711 		break;
   6712 	case EVENT_CHANNEL_LIST_CHANGED:
   6713 		wpa_supplicant_update_channel_list(
   6714 			wpa_s, &data->channel_list_changed);
   6715 		break;
   6716 	case EVENT_INTERFACE_UNAVAILABLE:
   6717 		wpas_p2p_interface_unavailable(wpa_s);
   6718 		break;
   6719 	case EVENT_BEST_CHANNEL:
   6720 		wpa_dbg(wpa_s, MSG_DEBUG, "Best channel event received "
   6721 			"(%d %d %d)",
   6722 			data->best_chan.freq_24, data->best_chan.freq_5,
   6723 			data->best_chan.freq_overall);
   6724 		wpa_s->best_24_freq = data->best_chan.freq_24;
   6725 		wpa_s->best_5_freq = data->best_chan.freq_5;
   6726 		wpa_s->best_overall_freq = data->best_chan.freq_overall;
   6727 		wpas_p2p_update_best_channels(wpa_s, data->best_chan.freq_24,
   6728 					      data->best_chan.freq_5,
   6729 					      data->best_chan.freq_overall);
   6730 		break;
   6731 	case EVENT_UNPROT_DEAUTH:
   6732 		wpa_supplicant_event_unprot_deauth(wpa_s,
   6733 						   &data->unprot_deauth);
   6734 		break;
   6735 	case EVENT_UNPROT_DISASSOC:
   6736 		wpa_supplicant_event_unprot_disassoc(wpa_s,
   6737 						     &data->unprot_disassoc);
   6738 		break;
   6739 	case EVENT_STATION_LOW_ACK:
   6740 #ifdef CONFIG_AP
   6741 		if (wpa_s->ap_iface && data)
   6742 			hostapd_event_sta_low_ack(wpa_s->ap_iface->bss[0],
   6743 						  data->low_ack.addr);
   6744 #endif /* CONFIG_AP */
   6745 #ifdef CONFIG_TDLS
   6746 		if (data)
   6747 			wpa_tdls_disable_unreachable_link(wpa_s->wpa,
   6748 							  data->low_ack.addr);
   6749 #endif /* CONFIG_TDLS */
   6750 		break;
   6751 	case EVENT_IBSS_PEER_LOST:
   6752 #ifdef CONFIG_IBSS_RSN
   6753 		ibss_rsn_stop(wpa_s->ibss_rsn, data->ibss_peer_lost.peer);
   6754 #endif /* CONFIG_IBSS_RSN */
   6755 		break;
   6756 	case EVENT_DRIVER_GTK_REKEY:
   6757 		if (!ether_addr_equal(data->driver_gtk_rekey.bssid,
   6758 				      wpa_s->bssid))
   6759 			break;
   6760 		if (!wpa_s->wpa)
   6761 			break;
   6762 		wpa_sm_update_replay_ctr(wpa_s->wpa,
   6763 					 data->driver_gtk_rekey.replay_ctr);
   6764 		break;
   6765 	case EVENT_SCHED_SCAN_STOPPED:
   6766 		wpa_s->sched_scanning = 0;
   6767 		resched = wpa_s->scanning && wpas_scan_scheduled(wpa_s);
   6768 		wpa_supplicant_notify_scanning(wpa_s, 0);
   6769 
   6770 		if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
   6771 			break;
   6772 
   6773 		/*
   6774 		 * If the driver stopped scanning without being requested to,
   6775 		 * request a new scan to continue scanning for networks.
   6776 		 */
   6777 		if (!wpa_s->sched_scan_stop_req &&
   6778 		    wpa_s->wpa_state == WPA_SCANNING) {
   6779 			wpa_dbg(wpa_s, MSG_DEBUG,
   6780 				"Restart scanning after unexpected sched_scan stop event");
   6781 			wpa_supplicant_req_scan(wpa_s, 1, 0);
   6782 			break;
   6783 		}
   6784 
   6785 		wpa_s->sched_scan_stop_req = 0;
   6786 
   6787 		/*
   6788 		 * Start a new sched scan to continue searching for more SSIDs
   6789 		 * either if timed out or PNO schedule scan is pending.
   6790 		 */
   6791 		if (wpa_s->sched_scan_timed_out) {
   6792 			wpa_supplicant_req_sched_scan(wpa_s);
   6793 		} else if (wpa_s->pno_sched_pending) {
   6794 			wpa_s->pno_sched_pending = 0;
   6795 			wpas_start_pno(wpa_s);
   6796 		} else if (resched) {
   6797 			wpa_supplicant_req_scan(wpa_s, 0, 0);
   6798 		}
   6799 
   6800 		break;
   6801 	case EVENT_WPS_BUTTON_PUSHED:
   6802 #ifdef CONFIG_WPS
   6803 		wpas_wps_start_pbc(wpa_s, NULL, 0, 0);
   6804 #endif /* CONFIG_WPS */
   6805 		break;
   6806 	case EVENT_AVOID_FREQUENCIES:
   6807 		wpa_supplicant_notify_avoid_freq(wpa_s, data);
   6808 		break;
   6809 	case EVENT_CONNECT_FAILED_REASON:
   6810 #ifdef CONFIG_AP
   6811 		if (!wpa_s->ap_iface || !data)
   6812 			break;
   6813 		hostapd_event_connect_failed_reason(
   6814 			wpa_s->ap_iface->bss[0],
   6815 			data->connect_failed_reason.addr,
   6816 			data->connect_failed_reason.code);
   6817 #endif /* CONFIG_AP */
   6818 		break;
   6819 	case EVENT_NEW_PEER_CANDIDATE:
   6820 #ifdef CONFIG_MESH
   6821 		if (!wpa_s->ifmsh || !data)
   6822 			break;
   6823 		wpa_mesh_notify_peer(wpa_s, data->mesh_peer.peer,
   6824 				     data->mesh_peer.ies,
   6825 				     data->mesh_peer.ie_len);
   6826 #endif /* CONFIG_MESH */
   6827 		break;
   6828 	case EVENT_SURVEY:
   6829 #ifdef CONFIG_AP
   6830 		if (!wpa_s->ap_iface)
   6831 			break;
   6832 		hostapd_event_get_survey(wpa_s->ap_iface,
   6833 					 &data->survey_results);
   6834 #endif /* CONFIG_AP */
   6835 		break;
   6836 	case EVENT_ACS_CHANNEL_SELECTED:
   6837 #ifdef CONFIG_AP
   6838 #ifdef CONFIG_ACS
   6839 		if (!wpa_s->ap_iface)
   6840 			break;
   6841 		hostapd_acs_channel_selected(wpa_s->ap_iface->bss[0],
   6842 					     &data->acs_selected_channels);
   6843 #endif /* CONFIG_ACS */
   6844 #endif /* CONFIG_AP */
   6845 		break;
   6846 	case EVENT_P2P_LO_STOP:
   6847 #ifdef CONFIG_P2P
   6848 		wpa_s->p2p_lo_started = 0;
   6849 		wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_LISTEN_OFFLOAD_STOP
   6850 			P2P_LISTEN_OFFLOAD_STOP_REASON "reason=%d",
   6851 			data->p2p_lo_stop.reason_code);
   6852 #endif /* CONFIG_P2P */
   6853 		break;
   6854 	case EVENT_BEACON_LOSS:
   6855 		if (!wpa_s->current_bss || !wpa_s->current_ssid)
   6856 			break;
   6857 		wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_BEACON_LOSS);
   6858 		bgscan_notify_beacon_loss(wpa_s);
   6859 		break;
   6860 	case EVENT_EXTERNAL_AUTH:
   6861 #ifdef CONFIG_SAE
   6862 		if (!wpa_s->current_ssid) {
   6863 			wpa_printf(MSG_DEBUG, "SAE: current_ssid is NULL");
   6864 			break;
   6865 		}
   6866 		sme_external_auth_trigger(wpa_s, data);
   6867 #endif /* CONFIG_SAE */
   6868 		break;
   6869 #ifdef CONFIG_PASN
   6870 	case EVENT_PASN_AUTH:
   6871 		wpas_pasn_auth_trigger(wpa_s, &data->pasn_auth);
   6872 		break;
   6873 #endif /* CONFIG_PASN */
   6874 	case EVENT_PORT_AUTHORIZED:
   6875 #ifdef CONFIG_AP
   6876 		if (wpa_s->ap_iface && wpa_s->ap_iface->bss[0]) {
   6877 			struct sta_info *sta;
   6878 
   6879 			sta = ap_get_sta(wpa_s->ap_iface->bss[0],
   6880 					 data->port_authorized.sta_addr);
   6881 			if (sta)
   6882 				ap_sta_set_authorized(wpa_s->ap_iface->bss[0],
   6883 						      sta, 1);
   6884 			else
   6885 				wpa_printf(MSG_DEBUG,
   6886 					   "No STA info matching port authorized event found");
   6887 			break;
   6888 		}
   6889 #endif /* CONFIG_AP */
   6890 #ifndef CONFIG_NO_WPA
   6891 		if (data->port_authorized.td_bitmap_len) {
   6892 			wpa_printf(MSG_DEBUG,
   6893 				   "WPA3: Transition Disable bitmap from the driver event: 0x%x",
   6894 				   data->port_authorized.td_bitmap[0]);
   6895 			wpas_transition_disable(
   6896 				wpa_s, data->port_authorized.td_bitmap[0]);
   6897 		}
   6898 #endif /* CONFIG_NO_WPA */
   6899 		wpa_supplicant_event_port_authorized(wpa_s);
   6900 		break;
   6901 	case EVENT_STATION_OPMODE_CHANGED:
   6902 #ifdef CONFIG_AP
   6903 		if (!wpa_s->ap_iface || !data)
   6904 			break;
   6905 
   6906 		hostapd_event_sta_opmode_changed(wpa_s->ap_iface->bss[0],
   6907 						 data->sta_opmode.addr,
   6908 						 data->sta_opmode.smps_mode,
   6909 						 data->sta_opmode.chan_width,
   6910 						 data->sta_opmode.rx_nss);
   6911 #endif /* CONFIG_AP */
   6912 		break;
   6913 	case EVENT_UNPROT_BEACON:
   6914 		wpas_event_unprot_beacon(wpa_s, &data->unprot_beacon);
   6915 		break;
   6916 	case EVENT_TX_WAIT_EXPIRE:
   6917 #ifdef CONFIG_DPP
   6918 		wpas_dpp_tx_wait_expire(wpa_s);
   6919 #endif /* CONFIG_DPP */
   6920 #ifdef CONFIG_NAN_USD
   6921 		wpas_nan_usd_tx_wait_expire(wpa_s);
   6922 #endif /* CONFIG_NAN_USD */
   6923 		break;
   6924 	case EVENT_TID_LINK_MAP:
   6925 		if (data)
   6926 			wpas_tid_link_map(wpa_s, &data->t2l_map_info);
   6927 		break;
   6928 	default:
   6929 		wpa_msg(wpa_s, MSG_INFO, "Unknown event %d", event);
   6930 		break;
   6931 	}
   6932 }
   6933 
   6934 
   6935 void wpa_supplicant_event_global(void *ctx, enum wpa_event_type event,
   6936 				 union wpa_event_data *data)
   6937 {
   6938 	struct wpa_supplicant *wpa_s;
   6939 
   6940 	if (event != EVENT_INTERFACE_STATUS)
   6941 		return;
   6942 
   6943 	wpa_s = wpa_supplicant_get_iface(ctx, data->interface_status.ifname);
   6944 	if (wpa_s && wpa_s->driver->get_ifindex) {
   6945 		unsigned int ifindex;
   6946 
   6947 		ifindex = wpa_s->driver->get_ifindex(wpa_s->drv_priv);
   6948 		if (ifindex != data->interface_status.ifindex) {
   6949 			wpa_dbg(wpa_s, MSG_DEBUG,
   6950 				"interface status ifindex %d mismatch (%d)",
   6951 				ifindex, data->interface_status.ifindex);
   6952 			return;
   6953 		}
   6954 	}
   6955 #ifdef CONFIG_MATCH_IFACE
   6956 	else if (data->interface_status.ievent == EVENT_INTERFACE_ADDED) {
   6957 		struct wpa_interface *wpa_i;
   6958 
   6959 		wpa_i = wpa_supplicant_match_iface(
   6960 			ctx, data->interface_status.ifname);
   6961 		if (!wpa_i)
   6962 			return;
   6963 		wpa_s = wpa_supplicant_add_iface(ctx, wpa_i, NULL);
   6964 		os_free(wpa_i);
   6965 	}
   6966 #endif /* CONFIG_MATCH_IFACE */
   6967 
   6968 	if (wpa_s)
   6969 		wpa_supplicant_event(wpa_s, event, data);
   6970 }
   6971