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