xref: /aosp_15_r20/external/wpa_supplicant_8/wpa_supplicant/wpas_glue.c (revision 03f9172ca588f91df233974f4258bab95191f931)
1 /*
2  * WPA Supplicant - Glue code to setup EAPOL and RSN modules
3  * Copyright (c) 2003-2015, Jouni Malinen <[email protected]>
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 "eap_peer/eap.h"
14 #include "rsn_supp/wpa.h"
15 #include "eloop.h"
16 #include "config.h"
17 #include "l2_packet/l2_packet.h"
18 #include "common/wpa_common.h"
19 #include "common/ptksa_cache.h"
20 #include "wpa_supplicant_i.h"
21 #include "driver_i.h"
22 #include "rsn_supp/pmksa_cache.h"
23 #include "sme.h"
24 #include "common/ieee802_11_defs.h"
25 #include "common/wpa_ctrl.h"
26 #include "wpas_glue.h"
27 #include "wps_supplicant.h"
28 #include "bss.h"
29 #include "scan.h"
30 #include "notify.h"
31 #include "wpas_kay.h"
32 
33 
34 #ifndef CONFIG_NO_CONFIG_BLOBS
35 #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
wpa_supplicant_set_config_blob(void * ctx,struct wpa_config_blob * blob)36 static void wpa_supplicant_set_config_blob(void *ctx,
37 					   struct wpa_config_blob *blob)
38 {
39 	struct wpa_supplicant *wpa_s = ctx;
40 	wpa_config_set_blob(wpa_s->conf, blob);
41 	if (wpa_s->conf->update_config) {
42 		int ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
43 		if (ret) {
44 			wpa_printf(MSG_DEBUG, "Failed to update config after "
45 				   "blob set");
46 		}
47 	}
48 }
49 
50 
51 static const struct wpa_config_blob *
wpa_supplicant_get_config_blob(void * ctx,const char * name)52 wpa_supplicant_get_config_blob(void *ctx, const char *name)
53 {
54 	struct wpa_supplicant *wpa_s = ctx;
55 	return wpa_config_get_blob(wpa_s->conf, name);
56 }
57 #endif /* defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA) */
58 #endif /* CONFIG_NO_CONFIG_BLOBS */
59 
60 
61 #if defined(IEEE8021X_EAPOL) || !defined(CONFIG_NO_WPA)
wpa_alloc_eapol(const struct wpa_supplicant * wpa_s,u8 type,const void * data,u16 data_len,size_t * msg_len,void ** data_pos)62 static u8 * wpa_alloc_eapol(const struct wpa_supplicant *wpa_s, u8 type,
63 			    const void *data, u16 data_len,
64 			    size_t *msg_len, void **data_pos)
65 {
66 	struct ieee802_1x_hdr *hdr;
67 
68 	*msg_len = sizeof(*hdr) + data_len;
69 	hdr = os_malloc(*msg_len);
70 	if (hdr == NULL)
71 		return NULL;
72 
73 	hdr->version = wpa_s->conf->eapol_version;
74 	hdr->type = type;
75 	hdr->length = host_to_be16(data_len);
76 
77 	if (data)
78 		os_memcpy(hdr + 1, data, data_len);
79 	else
80 		os_memset(hdr + 1, 0, data_len);
81 
82 	if (data_pos)
83 		*data_pos = hdr + 1;
84 
85 	return (u8 *) hdr;
86 }
87 
88 
89 /**
90  * wpa_ether_send - Send Ethernet frame
91  * @wpa_s: Pointer to wpa_supplicant data
92  * @dest: Destination MAC address
93  * @proto: Ethertype in host byte order
94  * @buf: Frame payload starting from IEEE 802.1X header
95  * @len: Frame payload length
96  * Returns: >=0 on success, <0 on failure
97  */
wpa_ether_send(struct wpa_supplicant * wpa_s,const u8 * dest,u16 proto,const u8 * buf,size_t len)98 int wpa_ether_send(struct wpa_supplicant *wpa_s, const u8 *dest,
99 		   u16 proto, const u8 *buf, size_t len)
100 {
101 #ifdef CONFIG_TESTING_OPTIONS
102 	if (wpa_s->ext_eapol_frame_io && proto == ETH_P_EAPOL) {
103 		size_t hex_len = 2 * len + 1;
104 		char *hex = os_malloc(hex_len);
105 
106 		if (hex == NULL)
107 			return -1;
108 		wpa_snprintf_hex(hex, hex_len, buf, len);
109 		wpa_msg(wpa_s, MSG_INFO, "EAPOL-TX " MACSTR " %s",
110 			MAC2STR(dest), hex);
111 		os_free(hex);
112 		return 0;
113 	}
114 #endif /* CONFIG_TESTING_OPTIONS */
115 
116 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_CONTROL_PORT) {
117 		int encrypt = wpa_s->wpa &&
118 			wpa_sm_has_ptk_installed(wpa_s->wpa);
119 
120 		return wpa_drv_tx_control_port(wpa_s, dest, proto, buf, len,
121 					       !encrypt);
122 	}
123 
124 	if (wpa_s->l2) {
125 		return l2_packet_send(wpa_s->l2, dest, proto, buf, len);
126 	}
127 
128 	return -1;
129 }
130 #endif /* IEEE8021X_EAPOL || !CONFIG_NO_WPA */
131 
132 
133 #ifdef IEEE8021X_EAPOL
134 
135 /**
136  * wpa_supplicant_eapol_send - Send IEEE 802.1X EAPOL packet to Authenticator
137  * @ctx: Pointer to wpa_supplicant data (wpa_s)
138  * @type: IEEE 802.1X packet type (IEEE802_1X_TYPE_*)
139  * @buf: EAPOL payload (after IEEE 802.1X header)
140  * @len: EAPOL payload length
141  * Returns: >=0 on success, <0 on failure
142  *
143  * This function adds Ethernet and IEEE 802.1X header and sends the EAPOL frame
144  * to the current Authenticator.
145  */
wpa_supplicant_eapol_send(void * ctx,int type,const u8 * buf,size_t len)146 static int wpa_supplicant_eapol_send(void *ctx, int type, const u8 *buf,
147 				     size_t len)
148 {
149 	struct wpa_supplicant *wpa_s = ctx;
150 	u8 *msg, *dst, bssid[ETH_ALEN];
151 	struct driver_sta_mlo_info drv_mlo;
152 	size_t msglen;
153 	int res;
154 
155 	/* TODO: could add l2_packet_sendmsg that allows fragments to avoid
156 	 * extra copy here */
157 
158 	if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
159 	    wpa_s->key_mgmt == WPA_KEY_MGMT_OWE ||
160 	    wpa_s->key_mgmt == WPA_KEY_MGMT_DPP ||
161 	    wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
162 		/* Current SSID is not using IEEE 802.1X/EAP, so drop possible
163 		 * EAPOL frames (mainly, EAPOL-Start) from EAPOL state
164 		 * machines. */
165 		wpa_printf(MSG_DEBUG, "WPA: drop TX EAPOL in non-IEEE 802.1X "
166 			   "mode (type=%d len=%lu)", type,
167 			   (unsigned long) len);
168 		return -1;
169 	}
170 
171 	if (pmksa_cache_get_current(wpa_s->wpa) &&
172 	    type == IEEE802_1X_TYPE_EAPOL_START) {
173 		/*
174 		 * We were trying to use PMKSA caching and sending EAPOL-Start
175 		 * would abort that and trigger full EAPOL authentication.
176 		 * However, we've already waited for the AP/Authenticator to
177 		 * start 4-way handshake or EAP authentication, and apparently
178 		 * it has not done so since the startWhen timer has reached zero
179 		 * to get the state machine sending EAPOL-Start. This is not
180 		 * really supposed to happen, but an interoperability issue with
181 		 * a deployed AP has been identified where the connection fails
182 		 * due to that AP failing to operate correctly if PMKID is
183 		 * included in the Association Request frame. To work around
184 		 * this, assume PMKSA caching failed and try to initiate full
185 		 * EAP authentication.
186 		 */
187 		if (!wpa_s->current_ssid ||
188 		    wpa_s->current_ssid->eap_workaround) {
189 			wpa_printf(MSG_DEBUG,
190 				   "RSN: Timeout on waiting for the AP to initiate 4-way handshake for PMKSA caching or EAP authentication - try to force it to start EAP authentication");
191 		} else {
192 			wpa_printf(MSG_DEBUG,
193 				   "RSN: PMKSA caching - do not send EAPOL-Start");
194 			return -1;
195 		}
196 	}
197 
198 	if (is_zero_ether_addr(wpa_s->bssid)) {
199 		wpa_printf(MSG_DEBUG, "BSSID not set when trying to send an "
200 			   "EAPOL frame");
201 		os_memset(&drv_mlo, 0, sizeof(drv_mlo));
202 		if (wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
203 		    (!wpa_s->valid_links ||
204 		     wpas_drv_get_sta_mlo_info(wpa_s, &drv_mlo) == 0) &&
205 		    !is_zero_ether_addr(bssid)) {
206 			dst = drv_mlo.valid_links ? drv_mlo.ap_mld_addr : bssid;
207 			wpa_printf(MSG_DEBUG, "Using current %s " MACSTR
208 				   " from the driver as the EAPOL destination",
209 				   drv_mlo.valid_links ? "AP MLD MAC address" :
210 				   "BSSID",
211 				   MAC2STR(dst));
212 		} else {
213 			dst = wpa_s->last_eapol_src;
214 			wpa_printf(MSG_DEBUG, "Using the source address of the"
215 				   " last received EAPOL frame " MACSTR " as "
216 				   "the EAPOL destination",
217 				   MAC2STR(dst));
218 		}
219 	} else {
220 		/* BSSID was already set (from (Re)Assoc event, so use BSSID or
221 		 * AP MLD MAC address (in the case of MLO connection) as the
222 		 * EAPOL destination. */
223 		dst = wpa_s->valid_links ? wpa_s->ap_mld_addr : wpa_s->bssid;
224 	}
225 
226 	msg = wpa_alloc_eapol(wpa_s, type, buf, len, &msglen, NULL);
227 	if (msg == NULL)
228 		return -1;
229 
230 	wpa_printf(MSG_DEBUG, "TX EAPOL: dst=" MACSTR, MAC2STR(dst));
231 	wpa_hexdump(MSG_MSGDUMP, "TX EAPOL", msg, msglen);
232 	res = wpa_ether_send(wpa_s, dst, ETH_P_EAPOL, msg, msglen);
233 	os_free(msg);
234 	return res;
235 }
236 
237 
238 #ifdef CONFIG_WEP
239 /**
240  * wpa_eapol_set_wep_key - set WEP key for the driver
241  * @ctx: Pointer to wpa_supplicant data (wpa_s)
242  * @unicast: 1 = individual unicast key, 0 = broadcast key
243  * @keyidx: WEP key index (0..3)
244  * @key: Pointer to key data
245  * @keylen: Key length in bytes
246  * Returns: 0 on success or < 0 on error.
247  */
wpa_eapol_set_wep_key(void * ctx,int unicast,int keyidx,const u8 * key,size_t keylen)248 static int wpa_eapol_set_wep_key(void *ctx, int unicast, int keyidx,
249 				 const u8 *key, size_t keylen)
250 {
251 	struct wpa_supplicant *wpa_s = ctx;
252 	if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
253 		int cipher = (keylen == 5) ? WPA_CIPHER_WEP40 :
254 			WPA_CIPHER_WEP104;
255 		if (unicast)
256 			wpa_s->pairwise_cipher = cipher;
257 		else
258 			wpa_s->group_cipher = cipher;
259 	}
260 	return wpa_drv_set_key(wpa_s, -1, WPA_ALG_WEP,
261 			       unicast ? wpa_s->bssid : NULL,
262 			       keyidx, unicast, NULL, 0, key, keylen,
263 			       unicast ? KEY_FLAG_PAIRWISE_RX_TX :
264 			       KEY_FLAG_GROUP_RX_TX_DEFAULT);
265 }
266 #endif /* CONFIG_WEP */
267 
268 
wpa_supplicant_aborted_cached(void * ctx)269 static void wpa_supplicant_aborted_cached(void *ctx)
270 {
271 	struct wpa_supplicant *wpa_s = ctx;
272 	wpa_sm_aborted_cached(wpa_s->wpa);
273 }
274 
275 
result_str(enum eapol_supp_result result)276 static const char * result_str(enum eapol_supp_result result)
277 {
278 	switch (result) {
279 	case EAPOL_SUPP_RESULT_FAILURE:
280 		return "FAILURE";
281 	case EAPOL_SUPP_RESULT_SUCCESS:
282 		return "SUCCESS";
283 	case EAPOL_SUPP_RESULT_EXPECTED_FAILURE:
284 		return "EXPECTED_FAILURE";
285 	}
286 	return "?";
287 }
288 
289 
wpa_supplicant_eapol_cb(struct eapol_sm * eapol,enum eapol_supp_result result,void * ctx)290 static void wpa_supplicant_eapol_cb(struct eapol_sm *eapol,
291 				    enum eapol_supp_result result,
292 				    void *ctx)
293 {
294 	struct wpa_supplicant *wpa_s = ctx;
295 	int res, pmk_len;
296 	u8 pmk[PMK_LEN_MAX];
297 
298 	wpa_printf(MSG_DEBUG, "EAPOL authentication completed - result=%s",
299 		   result_str(result));
300 
301 	if (wpas_wps_eapol_cb(wpa_s) > 0)
302 		return;
303 
304 	wpa_s->eap_expected_failure = result ==
305 		EAPOL_SUPP_RESULT_EXPECTED_FAILURE;
306 
307 	if (result != EAPOL_SUPP_RESULT_SUCCESS) {
308 		int timeout = 2;
309 		/*
310 		 * Make sure we do not get stuck here waiting for long EAPOL
311 		 * timeout if the AP does not disconnect in case of
312 		 * authentication failure.
313 		 */
314 		if (wpa_s->eapol_failed) {
315 			wpa_printf(MSG_DEBUG,
316 				   "EAPOL authentication failed again and AP did not disconnect us");
317 			timeout = 0;
318 		}
319 		wpa_s->eapol_failed = 1;
320 		wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
321 	} else {
322 		wpa_s->eapol_failed = 0;
323 		ieee802_1x_notify_create_actor(wpa_s, wpa_s->last_eapol_src);
324 	}
325 
326 #if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
327 	if (result != EAPOL_SUPP_RESULT_SUCCESS)
328 #else
329 	if (result != EAPOL_SUPP_RESULT_SUCCESS ||
330 		!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X))
331 #endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
332 		return;
333 
334 #if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
335 	if (wpa_ft_is_ft_protocol(wpa_s->wpa)) {
336 		return;
337 	}
338 #endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
339 
340 	if (!wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt))
341 		return;
342 
343 	wpa_printf(MSG_DEBUG, "Configure PMK for driver-based RSN 4-way "
344 		   "handshake");
345 
346 	if (wpa_key_mgmt_sha384(wpa_s->key_mgmt))
347 		pmk_len = PMK_LEN_SUITE_B_192;
348 	else
349 		pmk_len = PMK_LEN;
350 
351 	if (wpa_key_mgmt_ft(wpa_s->key_mgmt)) {
352 #ifdef CONFIG_IEEE80211R
353 		u8 buf[2 * PMK_LEN];
354 		wpa_printf(MSG_DEBUG, "RSN: Use FT XXKey as PMK for "
355 			   "driver-based 4-way hs and FT");
356 		res = eapol_sm_get_key(eapol, buf, 2 * PMK_LEN);
357 		if (res == 0) {
358 			os_memcpy(pmk, buf + PMK_LEN, PMK_LEN);
359 			os_memset(buf, 0, sizeof(buf));
360 		}
361 #else /* CONFIG_IEEE80211R */
362 		res = -1;
363 #endif /* CONFIG_IEEE80211R */
364 	} else {
365 		res = eapol_sm_get_key(eapol, pmk, pmk_len);
366 		if (res) {
367 			/*
368 			 * EAP-LEAP is an exception from other EAP methods: it
369 			 * uses only 16-byte PMK.
370 			 */
371 			res = eapol_sm_get_key(eapol, pmk, 16);
372 			pmk_len = 16;
373 		}
374 	}
375 
376 	if (res) {
377 		wpa_printf(MSG_DEBUG, "Failed to get PMK from EAPOL state "
378 			   "machines");
379 		return;
380 	}
381 
382 	wpa_hexdump_key(MSG_DEBUG, "RSN: Configure PMK for driver-based 4-way "
383 			"handshake", pmk, pmk_len);
384 
385 	if (wpa_drv_set_key(wpa_s, -1, 0, NULL, 0, 0, NULL, 0, pmk,
386 			    pmk_len, KEY_FLAG_PMK)) {
387 		wpa_printf(MSG_DEBUG, "Failed to set PMK to the driver");
388 	}
389 
390 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE_8021X) {
391 		/* Add PMKSA cache entry */
392 		wpa_printf(MSG_INFO, "add pmksa entry for the PMK");
393 		wpa_sm_set_pmk(wpa_s->wpa, pmk, pmk_len, NULL, wpa_sm_get_auth_addr(wpa_s->wpa));
394 	}
395 
396 	wpa_supplicant_cancel_scan(wpa_s);
397 	wpa_supplicant_cancel_auth_timeout(wpa_s);
398 	wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
399 
400 }
401 
402 
wpa_supplicant_notify_eapol_done(void * ctx)403 static void wpa_supplicant_notify_eapol_done(void *ctx)
404 {
405 	struct wpa_supplicant *wpa_s = ctx;
406 	wpa_msg(wpa_s, MSG_DEBUG, "WPA: EAPOL processing complete");
407 	if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
408 		wpa_supplicant_set_state(wpa_s, WPA_4WAY_HANDSHAKE);
409 	} else {
410 		wpa_supplicant_cancel_auth_timeout(wpa_s);
411 		wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
412 	}
413 }
414 
415 #endif /* IEEE8021X_EAPOL */
416 
417 
418 #ifndef CONFIG_NO_WPA
419 
wpa_get_beacon_ie(struct wpa_supplicant * wpa_s)420 static int wpa_get_beacon_ie(struct wpa_supplicant *wpa_s)
421 {
422 	int ret = 0;
423 	struct wpa_bss *curr = NULL, *bss;
424 	struct wpa_ssid *ssid = wpa_s->current_ssid;
425 	const u8 *ie;
426 
427 	dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
428 		if (!ether_addr_equal(bss->bssid, wpa_s->bssid))
429 			continue;
430 		if (ssid == NULL ||
431 		    ((bss->ssid_len == ssid->ssid_len &&
432 		      os_memcmp(bss->ssid, ssid->ssid, ssid->ssid_len) == 0) ||
433 		     ssid->ssid_len == 0)) {
434 			curr = bss;
435 			break;
436 		}
437 #ifdef CONFIG_OWE
438 		if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_OWE) &&
439 		    (bss->flags & WPA_BSS_OWE_TRANSITION)) {
440 			curr = bss;
441 			break;
442 		}
443 #endif /* CONFIG_OWE */
444 	}
445 
446 	if (curr) {
447 		ie = wpa_bss_get_vendor_ie(curr, WPA_IE_VENDOR_TYPE);
448 		if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
449 			ret = -1;
450 
451 		ie = wpa_bss_get_ie(curr, WLAN_EID_RSN);
452 		if (wpa_sm_set_ap_rsn_ie(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
453 			ret = -1;
454 
455 		ie = wpa_bss_get_ie(curr, WLAN_EID_RSNX);
456 		if (wpa_sm_set_ap_rsnxe(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0))
457 			ret = -1;
458 
459 		ie = wpa_bss_get_vendor_ie(curr, RSNE_OVERRIDE_IE_VENDOR_TYPE);
460 		if (wpa_sm_set_ap_rsne_override(wpa_s->wpa, ie,
461 						ie ? 2 + ie[1] : 0))
462 			ret = -1;
463 
464 		ie = wpa_bss_get_vendor_ie(curr,
465 					   RSNE_OVERRIDE_2_IE_VENDOR_TYPE);
466 		if (wpa_sm_set_ap_rsne_override_2(wpa_s->wpa, ie,
467 						  ie ? 2 + ie[1] : 0))
468 			ret = -1;
469 
470 		ie = wpa_bss_get_vendor_ie(curr, RSNXE_OVERRIDE_IE_VENDOR_TYPE);
471 		if (wpa_sm_set_ap_rsnxe_override(wpa_s->wpa, ie,
472 						 ie ? 2 + ie[1] : 0))
473 			ret = -1;
474 	} else {
475 		ret = -1;
476 	}
477 
478 	return ret;
479 }
480 
481 
wpa_supplicant_get_beacon_ie(void * ctx)482 static int wpa_supplicant_get_beacon_ie(void *ctx)
483 {
484 	struct wpa_supplicant *wpa_s = ctx;
485 	if (wpa_get_beacon_ie(wpa_s) == 0) {
486 		return 0;
487 	}
488 
489 	/* No WPA/RSN IE found in the cached scan results. Try to get updated
490 	 * scan results from the driver. */
491 	if (wpa_supplicant_update_scan_results(wpa_s, wpa_s->bssid) < 0)
492 		return -1;
493 
494 	return wpa_get_beacon_ie(wpa_s);
495 }
496 
497 
_wpa_alloc_eapol(void * wpa_s,u8 type,const void * data,u16 data_len,size_t * msg_len,void ** data_pos)498 static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
499 			     const void *data, u16 data_len,
500 			     size_t *msg_len, void **data_pos)
501 {
502 	return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
503 }
504 
505 
_wpa_ether_send(void * wpa_s,const u8 * dest,u16 proto,const u8 * buf,size_t len)506 static int _wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
507 			   const u8 *buf, size_t len)
508 {
509 	return wpa_ether_send(wpa_s, dest, proto, buf, len);
510 }
511 
512 
_wpa_supplicant_cancel_auth_timeout(void * wpa_s)513 static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
514 {
515 	wpa_supplicant_cancel_auth_timeout(wpa_s);
516 }
517 
518 
_wpa_supplicant_set_state(void * wpa_s,enum wpa_states state)519 static void _wpa_supplicant_set_state(void *wpa_s, enum wpa_states state)
520 {
521 	wpa_supplicant_set_state(wpa_s, state);
522 }
523 
524 
525 /**
526  * wpa_supplicant_get_state - Get the connection state
527  * @wpa_s: Pointer to wpa_supplicant data
528  * Returns: The current connection state (WPA_*)
529  */
wpa_supplicant_get_state(struct wpa_supplicant * wpa_s)530 static enum wpa_states wpa_supplicant_get_state(struct wpa_supplicant *wpa_s)
531 {
532 	return wpa_s->wpa_state;
533 }
534 
535 
_wpa_supplicant_get_state(void * wpa_s)536 static enum wpa_states _wpa_supplicant_get_state(void *wpa_s)
537 {
538 	return wpa_supplicant_get_state(wpa_s);
539 }
540 
541 
_wpa_supplicant_deauthenticate(void * wpa_s,u16 reason_code)542 static void _wpa_supplicant_deauthenticate(void *wpa_s, u16 reason_code)
543 {
544 	wpa_supplicant_deauthenticate(wpa_s, reason_code);
545 	/* Schedule a scan to make sure we continue looking for networks */
546 	wpa_supplicant_req_scan(wpa_s, 5, 0);
547 }
548 
549 
_wpa_supplicant_reconnect(void * wpa_s)550 static void _wpa_supplicant_reconnect(void *wpa_s)
551 {
552 	wpa_supplicant_reconnect(wpa_s);
553 }
554 
555 
wpa_supplicant_get_network_ctx(void * wpa_s)556 static void * wpa_supplicant_get_network_ctx(void *wpa_s)
557 {
558 	return wpa_supplicant_get_ssid(wpa_s);
559 }
560 
561 
wpa_supplicant_get_bssid(void * ctx,u8 * bssid)562 static int wpa_supplicant_get_bssid(void *ctx, u8 *bssid)
563 {
564 	struct wpa_supplicant *wpa_s = ctx;
565 	return wpa_drv_get_bssid(wpa_s, bssid);
566 }
567 
568 
wpa_supplicant_set_key(void * _wpa_s,int link_id,enum wpa_alg alg,const u8 * addr,int key_idx,int set_tx,const u8 * seq,size_t seq_len,const u8 * key,size_t key_len,enum key_flag key_flag)569 static int wpa_supplicant_set_key(void *_wpa_s, int link_id, enum wpa_alg alg,
570 				  const u8 *addr, int key_idx, int set_tx,
571 				  const u8 *seq, size_t seq_len,
572 				  const u8 *key, size_t key_len,
573 				  enum key_flag key_flag)
574 {
575 	struct wpa_supplicant *wpa_s = _wpa_s;
576 	int ret;
577 
578 	if (alg == WPA_ALG_TKIP && key_idx == 0 && key_len == 32) {
579 		/* Clear the MIC error counter when setting a new PTK. */
580 		wpa_s->mic_errors_seen = 0;
581 	}
582 #ifdef CONFIG_TESTING_GET_GTK
583 	if (key_idx > 0 && addr && is_broadcast_ether_addr(addr) &&
584 	    alg != WPA_ALG_NONE && key_len <= sizeof(wpa_s->last_gtk)) {
585 		os_memcpy(wpa_s->last_gtk, key, key_len);
586 		wpa_s->last_gtk_len = key_len;
587 	}
588 #endif /* CONFIG_TESTING_GET_GTK */
589 #ifdef CONFIG_TESTING_OPTIONS
590 	if (addr && !is_broadcast_ether_addr(addr) &&
591 	    !(key_flag & KEY_FLAG_MODIFY)) {
592 		wpa_s->last_tk_alg = alg;
593 		os_memcpy(wpa_s->last_tk_addr, addr, ETH_ALEN);
594 		wpa_s->last_tk_key_idx = key_idx;
595 		if (key)
596 			os_memcpy(wpa_s->last_tk, key, key_len);
597 		wpa_s->last_tk_len = key_len;
598 	}
599 #endif /* CONFIG_TESTING_OPTIONS */
600 
601 	ret = wpa_drv_set_key(wpa_s, link_id, alg, addr, key_idx, set_tx, seq,
602 			      seq_len, key, key_len, key_flag);
603 	if (ret == 0 && (key_idx == 6 || key_idx == 7) &&
604 	    alg != WPA_ALG_NONE && key_len > 0)
605 		wpa_s->bigtk_set = true;
606 
607 	return ret;
608 }
609 
610 
wpa_supplicant_mlme_setprotection(void * wpa_s,const u8 * addr,int protection_type,int key_type)611 static int wpa_supplicant_mlme_setprotection(void *wpa_s, const u8 *addr,
612 					     int protection_type,
613 					     int key_type)
614 {
615 	return wpa_drv_mlme_setprotection(wpa_s, addr, protection_type,
616 					  key_type);
617 }
618 
619 
wpas_get_network_ctx(struct wpa_supplicant * wpa_s,void * network_ctx)620 static struct wpa_ssid * wpas_get_network_ctx(struct wpa_supplicant *wpa_s,
621 					      void *network_ctx)
622 {
623 	struct wpa_ssid *ssid;
624 
625 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
626 		if (network_ctx == ssid)
627 			return ssid;
628 	}
629 
630 	return NULL;
631 }
632 
633 
wpa_supplicant_add_pmkid(void * _wpa_s,void * network_ctx,const u8 * bssid,const u8 * pmkid,const u8 * fils_cache_id,const u8 * pmk,size_t pmk_len,u32 pmk_lifetime,u8 pmk_reauth_threshold,int akmp)634 static int wpa_supplicant_add_pmkid(void *_wpa_s, void *network_ctx,
635 				    const u8 *bssid, const u8 *pmkid,
636 				    const u8 *fils_cache_id,
637 				    const u8 *pmk, size_t pmk_len,
638 				    u32 pmk_lifetime, u8 pmk_reauth_threshold,
639 				    int akmp)
640 {
641 	struct wpa_supplicant *wpa_s = _wpa_s;
642 	struct wpa_ssid *ssid;
643 	struct wpa_pmkid_params params;
644 
645 	os_memset(&params, 0, sizeof(params));
646 	ssid = wpas_get_network_ctx(wpa_s, network_ctx);
647 	if (ssid) {
648 		wpa_msg(wpa_s, MSG_INFO, PMKSA_CACHE_ADDED MACSTR " %d",
649 			MAC2STR(bssid), ssid->id);
650 		if ((akmp == WPA_KEY_MGMT_FT_IEEE8021X ||
651 		     akmp == WPA_KEY_MGMT_FT_IEEE8021X_SHA384) &&
652 		    !ssid->ft_eap_pmksa_caching) {
653 			/* Since we will not be using PMKSA caching for FT-EAP
654 			 * within wpa_supplicant to avoid known interop issues
655 			 * with APs, do not add this PMKID to the driver either
656 			 * so that we won't be hitting those interop issues
657 			 * with driver-based RSNE generation. */
658 			wpa_printf(MSG_DEBUG,
659 				   "FT: Do not add PMKID entry to the driver since FT-EAP PMKSA caching is not enabled in configuration");
660 			return 0;
661 		}
662 	}
663 	if (ssid && fils_cache_id) {
664 		params.ssid = ssid->ssid;
665 		params.ssid_len = ssid->ssid_len;
666 		params.fils_cache_id = fils_cache_id;
667 	} else {
668 		params.bssid = bssid;
669 	}
670 
671 	params.pmkid = pmkid;
672 	params.pmk = pmk;
673 	params.pmk_len = pmk_len;
674 	params.pmk_lifetime = pmk_lifetime;
675 	params.pmk_reauth_threshold = pmk_reauth_threshold;
676 
677 	return wpa_drv_add_pmkid(wpa_s, &params);
678 }
679 
680 
wpa_supplicant_remove_pmkid(void * _wpa_s,void * network_ctx,const u8 * bssid,const u8 * pmkid,const u8 * fils_cache_id)681 static int wpa_supplicant_remove_pmkid(void *_wpa_s, void *network_ctx,
682 				       const u8 *bssid, const u8 *pmkid,
683 				       const u8 *fils_cache_id)
684 {
685 	struct wpa_supplicant *wpa_s = _wpa_s;
686 	struct wpa_ssid *ssid;
687 	struct wpa_pmkid_params params;
688 
689 	os_memset(&params, 0, sizeof(params));
690 	ssid = wpas_get_network_ctx(wpa_s, network_ctx);
691 	if (ssid)
692 		wpa_msg(wpa_s, MSG_INFO, PMKSA_CACHE_REMOVED MACSTR " %d",
693 			MAC2STR(bssid), ssid->id);
694 	if (ssid && fils_cache_id) {
695 		params.ssid = ssid->ssid;
696 		params.ssid_len = ssid->ssid_len;
697 		params.fils_cache_id = fils_cache_id;
698 	} else {
699 		params.bssid = bssid;
700 	}
701 
702 	params.pmkid = pmkid;
703 
704 	return wpa_drv_remove_pmkid(wpa_s, &params);
705 }
706 
707 
708 #ifdef CONFIG_IEEE80211R
wpa_supplicant_update_ft_ies(void * ctx,const u8 * md,const u8 * ies,size_t ies_len)709 static int wpa_supplicant_update_ft_ies(void *ctx, const u8 *md,
710 					const u8 *ies, size_t ies_len)
711 {
712 	struct wpa_supplicant *wpa_s = ctx;
713 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME)
714 		return sme_update_ft_ies(wpa_s, md, ies, ies_len);
715 	return wpa_drv_update_ft_ies(wpa_s, md, ies, ies_len);
716 }
717 
718 
wpa_supplicant_send_ft_action(void * ctx,u8 action,const u8 * target_ap,const u8 * ies,size_t ies_len)719 static int wpa_supplicant_send_ft_action(void *ctx, u8 action,
720 					 const u8 *target_ap,
721 					 const u8 *ies, size_t ies_len)
722 {
723 	struct wpa_supplicant *wpa_s = ctx;
724 	int ret;
725 	u8 *data, *pos;
726 	size_t data_len;
727 
728 	if (action != 1) {
729 		wpa_printf(MSG_ERROR, "Unsupported send_ft_action action %d",
730 			   action);
731 		return -1;
732 	}
733 
734 	/*
735 	 * Action frame payload:
736 	 * Category[1] = 6 (Fast BSS Transition)
737 	 * Action[1] = 1 (Fast BSS Transition Request)
738 	 * STA Address
739 	 * Target AP Address
740 	 * FT IEs
741 	 */
742 
743 	data_len = 2 + 2 * ETH_ALEN + ies_len;
744 	data = os_malloc(data_len);
745 	if (data == NULL)
746 		return -1;
747 	pos = data;
748 	*pos++ = 0x06; /* FT Action category */
749 	*pos++ = action;
750 	os_memcpy(pos, wpa_s->own_addr, ETH_ALEN);
751 	pos += ETH_ALEN;
752 	os_memcpy(pos, target_ap, ETH_ALEN);
753 	pos += ETH_ALEN;
754 	os_memcpy(pos, ies, ies_len);
755 
756 	ret = wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0,
757 				  wpa_s->bssid, wpa_s->own_addr, wpa_s->bssid,
758 				  data, data_len, 0);
759 	os_free(data);
760 
761 	return ret;
762 }
763 
764 
wpa_supplicant_mark_authenticated(void * ctx,const u8 * target_ap)765 static int wpa_supplicant_mark_authenticated(void *ctx, const u8 *target_ap)
766 {
767 	struct wpa_supplicant *wpa_s = ctx;
768 	struct wpa_driver_auth_params params;
769 	struct wpa_bss *bss;
770 
771 	bss = wpa_bss_get_bssid(wpa_s, target_ap);
772 	if (bss == NULL)
773 		return -1;
774 
775 	os_memset(&params, 0, sizeof(params));
776 	params.bssid = target_ap;
777 	params.freq = bss->freq;
778 	params.ssid = bss->ssid;
779 	params.ssid_len = bss->ssid_len;
780 	params.auth_alg = WPA_AUTH_ALG_FT;
781 	params.local_state_change = 1;
782 	return wpa_drv_authenticate(wpa_s, &params);
783 }
784 #endif /* CONFIG_IEEE80211R */
785 
786 
787 #ifdef CONFIG_TDLS
788 
wpa_supplicant_tdls_get_capa(void * ctx,int * tdls_supported,int * tdls_ext_setup,int * tdls_chan_switch)789 static int wpa_supplicant_tdls_get_capa(void *ctx, int *tdls_supported,
790 					int *tdls_ext_setup,
791 					int *tdls_chan_switch)
792 {
793 	struct wpa_supplicant *wpa_s = ctx;
794 
795 	*tdls_supported = 0;
796 	*tdls_ext_setup = 0;
797 	*tdls_chan_switch = 0;
798 
799 	if (!wpa_s->drv_capa_known)
800 		return -1;
801 
802 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT)
803 		*tdls_supported = 1;
804 
805 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP)
806 		*tdls_ext_setup = 1;
807 
808 	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_CHANNEL_SWITCH)
809 		*tdls_chan_switch = 1;
810 
811 	return 0;
812 }
813 
814 
wpa_supplicant_send_tdls_mgmt(void * ctx,const u8 * dst,u8 action_code,u8 dialog_token,u16 status_code,u32 peer_capab,int initiator,const u8 * buf,size_t len,int link_id)815 static int wpa_supplicant_send_tdls_mgmt(void *ctx, const u8 *dst,
816 					 u8 action_code, u8 dialog_token,
817 					 u16 status_code, u32 peer_capab,
818 					 int initiator, const u8 *buf,
819 					 size_t len, int link_id)
820 {
821 	struct wpa_supplicant *wpa_s = ctx;
822 	return wpa_drv_send_tdls_mgmt(wpa_s, dst, action_code, dialog_token,
823 				      status_code, peer_capab, initiator, buf,
824 				      len, link_id);
825 }
826 
827 
wpa_supplicant_tdls_oper(void * ctx,int oper,const u8 * peer)828 static int wpa_supplicant_tdls_oper(void *ctx, int oper, const u8 *peer)
829 {
830 	struct wpa_supplicant *wpa_s = ctx;
831 	return wpa_drv_tdls_oper(wpa_s, oper, peer);
832 }
833 
834 
wpa_supplicant_tdls_peer_addset(void * ctx,const u8 * peer,int add,u16 aid,u16 capability,const u8 * supp_rates,size_t supp_rates_len,const struct ieee80211_ht_capabilities * ht_capab,const struct ieee80211_vht_capabilities * vht_capab,const struct ieee80211_he_capabilities * he_capab,size_t he_capab_len,const struct ieee80211_he_6ghz_band_cap * he_6ghz_he_capab,u8 qosinfo,int wmm,const u8 * ext_capab,size_t ext_capab_len,const u8 * supp_channels,size_t supp_channels_len,const u8 * supp_oper_classes,size_t supp_oper_classes_len,const struct ieee80211_eht_capabilities * eht_capab,size_t eht_capab_len,int mld_link_id)835 static int wpa_supplicant_tdls_peer_addset(
836 	void *ctx, const u8 *peer, int add, u16 aid, u16 capability,
837 	const u8 *supp_rates, size_t supp_rates_len,
838 	const struct ieee80211_ht_capabilities *ht_capab,
839 	const struct ieee80211_vht_capabilities *vht_capab,
840 	const struct ieee80211_he_capabilities *he_capab,
841 	size_t he_capab_len,
842 	const struct ieee80211_he_6ghz_band_cap *he_6ghz_he_capab,
843 	u8 qosinfo, int wmm, const u8 *ext_capab, size_t ext_capab_len,
844 	const u8 *supp_channels, size_t supp_channels_len,
845 	const u8 *supp_oper_classes, size_t supp_oper_classes_len,
846 	const struct ieee80211_eht_capabilities *eht_capab,
847 	size_t eht_capab_len, int mld_link_id)
848 {
849 	struct wpa_supplicant *wpa_s = ctx;
850 	struct hostapd_sta_add_params params;
851 
852 	os_memset(&params, 0, sizeof(params));
853 
854 	params.addr = peer;
855 	params.aid = aid;
856 	params.capability = capability;
857 	params.flags = WPA_STA_TDLS_PEER | WPA_STA_AUTHORIZED;
858 
859 	/*
860 	 * Don't rely only on qosinfo for WMM capability. It may be 0 even when
861 	 * present. Allow the WMM IE to also indicate QoS support.
862 	 */
863 	if (wmm || qosinfo)
864 		params.flags |= WPA_STA_WMM;
865 
866 	params.ht_capabilities = ht_capab;
867 	params.vht_capabilities = vht_capab;
868 	params.he_capab = he_capab;
869 	params.he_capab_len = he_capab_len;
870 	params.he_6ghz_capab = he_6ghz_he_capab;
871 	params.qosinfo = qosinfo;
872 	params.listen_interval = 0;
873 	params.supp_rates = supp_rates;
874 	params.supp_rates_len = supp_rates_len;
875 	params.set = !add;
876 	params.ext_capab = ext_capab;
877 	params.ext_capab_len = ext_capab_len;
878 	params.supp_channels = supp_channels;
879 	params.supp_channels_len = supp_channels_len;
880 	params.supp_oper_classes = supp_oper_classes;
881 	params.supp_oper_classes_len = supp_oper_classes_len;
882 	params.eht_capab = eht_capab;
883 	params.eht_capab_len = eht_capab_len;
884 	params.mld_link_id = mld_link_id;
885 
886 	return wpa_drv_sta_add(wpa_s, &params);
887 }
888 
889 
wpa_supplicant_tdls_enable_channel_switch(void * ctx,const u8 * addr,u8 oper_class,const struct hostapd_freq_params * params)890 static int wpa_supplicant_tdls_enable_channel_switch(
891 	void *ctx, const u8 *addr, u8 oper_class,
892 	const struct hostapd_freq_params *params)
893 {
894 	struct wpa_supplicant *wpa_s = ctx;
895 
896 	return wpa_drv_tdls_enable_channel_switch(wpa_s, addr, oper_class,
897 						  params);
898 }
899 
900 
wpa_supplicant_tdls_disable_channel_switch(void * ctx,const u8 * addr)901 static int wpa_supplicant_tdls_disable_channel_switch(void *ctx, const u8 *addr)
902 {
903 	struct wpa_supplicant *wpa_s = ctx;
904 
905 	return wpa_drv_tdls_disable_channel_switch(wpa_s, addr);
906 }
907 
908 #endif /* CONFIG_TDLS */
909 
910 #endif /* CONFIG_NO_WPA */
911 
912 
wpa_supplicant_ctrl_req_from_string(const char * field)913 enum wpa_ctrl_req_type wpa_supplicant_ctrl_req_from_string(const char *field)
914 {
915 	if (os_strcmp(field, "IDENTITY") == 0)
916 		return WPA_CTRL_REQ_EAP_IDENTITY;
917 	else if (os_strcmp(field, "PASSWORD") == 0)
918 		return WPA_CTRL_REQ_EAP_PASSWORD;
919 	else if (os_strcmp(field, "NEW_PASSWORD") == 0)
920 		return WPA_CTRL_REQ_EAP_NEW_PASSWORD;
921 	else if (os_strcmp(field, "PIN") == 0)
922 		return WPA_CTRL_REQ_EAP_PIN;
923 	else if (os_strcmp(field, "OTP") == 0)
924 		return WPA_CTRL_REQ_EAP_OTP;
925 	else if (os_strcmp(field, "PASSPHRASE") == 0)
926 		return WPA_CTRL_REQ_EAP_PASSPHRASE;
927 	else if (os_strcmp(field, "SIM") == 0)
928 		return WPA_CTRL_REQ_SIM;
929 	else if (os_strcmp(field, "PSK_PASSPHRASE") == 0)
930 		return WPA_CTRL_REQ_PSK_PASSPHRASE;
931 	else if (os_strcmp(field, "EXT_CERT_CHECK") == 0)
932 		return WPA_CTRL_REQ_EXT_CERT_CHECK;
933 	return WPA_CTRL_REQ_UNKNOWN;
934 }
935 
936 
wpa_supplicant_ctrl_req_to_string(enum wpa_ctrl_req_type field,const char * default_txt,const char ** txt)937 const char * wpa_supplicant_ctrl_req_to_string(enum wpa_ctrl_req_type field,
938 					       const char *default_txt,
939 					       const char **txt)
940 {
941 	const char *ret = NULL;
942 
943 	*txt = default_txt;
944 
945 	switch (field) {
946 	case WPA_CTRL_REQ_EAP_IDENTITY:
947 		*txt = "Identity";
948 		ret = "IDENTITY";
949 		break;
950 	case WPA_CTRL_REQ_EAP_PASSWORD:
951 		*txt = "Password";
952 		ret = "PASSWORD";
953 		break;
954 	case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
955 		*txt = "New Password";
956 		ret = "NEW_PASSWORD";
957 		break;
958 	case WPA_CTRL_REQ_EAP_PIN:
959 		*txt = "PIN";
960 		ret = "PIN";
961 		break;
962 	case WPA_CTRL_REQ_EAP_OTP:
963 		ret = "OTP";
964 		break;
965 	case WPA_CTRL_REQ_EAP_PASSPHRASE:
966 		*txt = "Private key passphrase";
967 		ret = "PASSPHRASE";
968 		break;
969 	case WPA_CTRL_REQ_SIM:
970 		ret = "SIM";
971 		break;
972 	case WPA_CTRL_REQ_PSK_PASSPHRASE:
973 		*txt = "PSK or passphrase";
974 		ret = "PSK_PASSPHRASE";
975 		break;
976 	case WPA_CTRL_REQ_EXT_CERT_CHECK:
977 		*txt = "External server certificate validation";
978 		ret = "EXT_CERT_CHECK";
979 		break;
980 	default:
981 		break;
982 	}
983 
984 	/* txt needs to be something */
985 	if (*txt == NULL) {
986 		wpa_printf(MSG_WARNING, "No message for request %d", field);
987 		ret = NULL;
988 	}
989 
990 	return ret;
991 }
992 
993 
wpas_send_ctrl_req(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid,const char * field_name,const char * txt)994 void wpas_send_ctrl_req(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
995 			const char *field_name, const char *txt)
996 {
997 	wpa_msg(wpa_s, MSG_INFO, WPA_CTRL_REQ "%s-%d:%s needed for SSID %s",
998 		field_name, ssid->id, txt,
999 		wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1000 }
1001 
1002 
1003 #ifdef IEEE8021X_EAPOL
1004 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
wpa_supplicant_eap_param_needed(void * ctx,enum wpa_ctrl_req_type field,const char * default_txt)1005 static void wpa_supplicant_eap_param_needed(void *ctx,
1006 					    enum wpa_ctrl_req_type field,
1007 					    const char *default_txt)
1008 {
1009 	struct wpa_supplicant *wpa_s = ctx;
1010 	struct wpa_ssid *ssid = wpa_s->current_ssid;
1011 	const char *field_name, *txt = NULL;
1012 
1013 	if (ssid == NULL)
1014 		return;
1015 
1016 	if (field == WPA_CTRL_REQ_EXT_CERT_CHECK)
1017 		ssid->eap.pending_ext_cert_check = PENDING_CHECK;
1018 	wpas_notify_network_request(wpa_s, ssid, field, default_txt);
1019 
1020 	field_name = wpa_supplicant_ctrl_req_to_string(field, default_txt,
1021 						       &txt);
1022 	if (field_name == NULL) {
1023 		wpa_printf(MSG_WARNING, "Unhandled EAP param %d needed",
1024 			   field);
1025 		return;
1026 	}
1027 
1028 	wpas_notify_eap_status(wpa_s, "eap parameter needed", field_name);
1029 
1030 	wpas_send_ctrl_req(wpa_s, ssid, field_name, txt);
1031 }
1032 #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1033 #define wpa_supplicant_eap_param_needed NULL
1034 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1035 
1036 
1037 #ifdef CONFIG_EAP_PROXY
1038 
wpa_supplicant_eap_proxy_cb(void * ctx)1039 static void wpa_supplicant_eap_proxy_cb(void *ctx)
1040 {
1041 	struct wpa_supplicant *wpa_s = ctx;
1042 	size_t len;
1043 
1044 	wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, -1,
1045 						     wpa_s->imsi, &len);
1046 	if (wpa_s->mnc_len > 0) {
1047 		wpa_s->imsi[len] = '\0';
1048 		wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
1049 			   wpa_s->imsi, wpa_s->mnc_len);
1050 	} else {
1051 		wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
1052 	}
1053 }
1054 
1055 
wpa_sm_sim_state_error_handler(struct wpa_supplicant * wpa_s)1056 static void wpa_sm_sim_state_error_handler(struct wpa_supplicant *wpa_s)
1057 {
1058 	int i;
1059 	struct wpa_ssid *ssid;
1060 	const struct eap_method_type *eap_methods;
1061 
1062 	if (!wpa_s->conf)
1063 		return;
1064 
1065 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)	{
1066 		eap_methods = ssid->eap.eap_methods;
1067 		if (!eap_methods)
1068 			continue;
1069 
1070 		for (i = 0; eap_methods[i].method != EAP_TYPE_NONE; i++) {
1071 			if (eap_methods[i].vendor == EAP_VENDOR_IETF &&
1072 			    (eap_methods[i].method == EAP_TYPE_SIM ||
1073 			     eap_methods[i].method == EAP_TYPE_AKA ||
1074 			     eap_methods[i].method == EAP_TYPE_AKA_PRIME)) {
1075 				wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
1076 				break;
1077 			}
1078 		}
1079 	}
1080 }
1081 
1082 
1083 static void
wpa_supplicant_eap_proxy_notify_sim_status(void * ctx,enum eap_proxy_sim_state sim_state)1084 wpa_supplicant_eap_proxy_notify_sim_status(void *ctx,
1085 					   enum eap_proxy_sim_state sim_state)
1086 {
1087 	struct wpa_supplicant *wpa_s = ctx;
1088 
1089 	wpa_printf(MSG_DEBUG, "eap_proxy: SIM card status %u", sim_state);
1090 	switch (sim_state) {
1091 	case SIM_STATE_ERROR:
1092 		wpa_sm_sim_state_error_handler(wpa_s);
1093 		break;
1094 	default:
1095 		wpa_printf(MSG_DEBUG, "eap_proxy: SIM card status unknown");
1096 		break;
1097 	}
1098 }
1099 
1100 #endif /* CONFIG_EAP_PROXY */
1101 
1102 
wpa_supplicant_port_cb(void * ctx,int authorized)1103 static void wpa_supplicant_port_cb(void *ctx, int authorized)
1104 {
1105 	struct wpa_supplicant *wpa_s = ctx;
1106 #ifdef CONFIG_AP
1107 	if (wpa_s->ap_iface) {
1108 		wpa_printf(MSG_DEBUG, "AP mode active - skip EAPOL Supplicant "
1109 			   "port status: %s",
1110 			   authorized ? "Authorized" : "Unauthorized");
1111 		return;
1112 	}
1113 #endif /* CONFIG_AP */
1114 	wpa_printf(MSG_DEBUG, "EAPOL: Supplicant port status: %s",
1115 		   authorized ? "Authorized" : "Unauthorized");
1116 	wpa_drv_set_supp_port(wpa_s, authorized);
1117 }
1118 
wpa_supplicant_permanent_id_req_denied_cb(void * ctx)1119 static void wpa_supplicant_permanent_id_req_denied_cb(void *ctx)
1120 {
1121 	struct wpas_supplicant *wpa_s = ctx;
1122 
1123 	wpas_notify_permanent_id_req_denied(wpa_s);
1124 }
1125 
wpa_supplicant_cert_cb(void * ctx,struct tls_cert_data * cert,const char * cert_hash)1126 static void wpa_supplicant_cert_cb(void *ctx, struct tls_cert_data *cert,
1127 				   const char *cert_hash)
1128 {
1129 	struct wpa_supplicant *wpa_s = ctx;
1130 
1131 	wpas_notify_certification(wpa_s, cert, cert_hash);
1132 }
1133 
1134 
wpa_supplicant_status_cb(void * ctx,const char * status,const char * parameter)1135 static void wpa_supplicant_status_cb(void *ctx, const char *status,
1136 				     const char *parameter)
1137 {
1138 	struct wpa_supplicant *wpa_s = ctx;
1139 
1140 	wpas_notify_eap_status(wpa_s, status, parameter);
1141 }
1142 
1143 
wpa_supplicant_eap_error_cb(void * ctx,int error_code)1144 static void wpa_supplicant_eap_error_cb(void *ctx, int error_code)
1145 {
1146 	struct wpa_supplicant *wpa_s = ctx;
1147 
1148 	wpas_notify_eap_error(wpa_s, error_code);
1149 }
1150 
1151 
wpa_supplicant_eap_auth_start_cb(void * ctx)1152 static int wpa_supplicant_eap_auth_start_cb(void *ctx)
1153 {
1154 	struct wpa_supplicant *wpa_s = ctx;
1155 
1156 	if (!wpa_s->new_connection && wpa_s->deny_ptk0_rekey &&
1157 	    !wpa_sm_ext_key_id_active(wpa_s->wpa)) {
1158 		wpa_msg(wpa_s, MSG_INFO,
1159 			"WPA: PTK0 rekey not allowed, reconnecting");
1160 		wpa_supplicant_reconnect(wpa_s);
1161 		return -1;
1162 	}
1163 	return 0;
1164 }
1165 
1166 
wpa_supplicant_set_anon_id(void * ctx,const u8 * id,size_t len)1167 static void wpa_supplicant_set_anon_id(void *ctx, const u8 *id, size_t len)
1168 {
1169 	struct wpa_supplicant *wpa_s = ctx;
1170 	char *str;
1171 	int res;
1172 
1173 	wpa_hexdump_ascii(MSG_DEBUG, "EAP method updated anonymous_identity",
1174 			  id, len);
1175 
1176 	if (wpa_s->current_ssid == NULL)
1177 		return;
1178 
1179 	if (id == NULL) {
1180 		if (wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
1181 				   "NULL", 0) < 0)
1182 			return;
1183 	} else {
1184 		str = os_malloc(len * 2 + 1);
1185 		if (str == NULL)
1186 			return;
1187 		wpa_snprintf_hex(str, len * 2 + 1, id, len);
1188 		res = wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
1189 				     str, 0);
1190 		os_free(str);
1191 		if (res < 0)
1192 			return;
1193 	}
1194 
1195 	if (wpa_s->conf->update_config) {
1196 		res = wpa_config_write(wpa_s->confname, wpa_s->conf);
1197 		if (res) {
1198 			wpa_printf(MSG_DEBUG, "Failed to update config after "
1199 				   "anonymous_id update");
1200 		}
1201 	}
1202 }
1203 
wpa_supplicant_eap_method_selected_cb(void * ctx,const char * reason_string)1204 static void wpa_supplicant_eap_method_selected_cb(void *ctx,
1205 						const char* reason_string)
1206 {
1207 	struct wpa_supplicant *wpa_s = ctx;
1208 
1209 	wpas_notify_eap_method_selected(wpa_s, reason_string);
1210 }
1211 
wpa_supplicant_open_ssl_failure_cb(void * ctx,const char * reason_string)1212 static void wpa_supplicant_open_ssl_failure_cb(void *ctx,
1213 						const char* reason_string)
1214 {
1215 	struct wpa_supplicant *wpa_s = ctx;
1216 
1217 	wpas_notify_open_ssl_failure(wpa_s, reason_string);
1218 }
1219 
wpas_encryption_required(void * ctx)1220 static bool wpas_encryption_required(void *ctx)
1221 {
1222 	struct wpa_supplicant *wpa_s = ctx;
1223 
1224 	return wpa_s->wpa &&
1225 		wpa_sm_has_ptk_installed(wpa_s->wpa) &&
1226 		wpa_sm_pmf_enabled(wpa_s->wpa);
1227 }
1228 
wpa_supplicant_get_certificate_cb(const char * alias,uint8_t ** value)1229 static ssize_t wpa_supplicant_get_certificate_cb(
1230 						const char* alias, uint8_t** value)
1231 {
1232 	wpa_printf(MSG_INFO, "wpa_supplicant_get_certificate_cb");
1233 	return wpas_get_certificate(alias, value);
1234 }
1235 
1236 #endif /* IEEE8021X_EAPOL */
1237 
1238 
wpa_supplicant_init_eapol(struct wpa_supplicant * wpa_s)1239 int wpa_supplicant_init_eapol(struct wpa_supplicant *wpa_s)
1240 {
1241 #ifdef IEEE8021X_EAPOL
1242 	struct eapol_ctx *ctx;
1243 	ctx = os_zalloc(sizeof(*ctx));
1244 	if (ctx == NULL) {
1245 		wpa_printf(MSG_ERROR, "Failed to allocate EAPOL context.");
1246 		return -1;
1247 	}
1248 
1249 	ctx->ctx = wpa_s;
1250 	ctx->msg_ctx = wpa_s;
1251 	ctx->eapol_send_ctx = wpa_s;
1252 	ctx->preauth = 0;
1253 	ctx->eapol_done_cb = wpa_supplicant_notify_eapol_done;
1254 	ctx->eapol_send = wpa_supplicant_eapol_send;
1255 #ifdef CONFIG_WEP
1256 	ctx->set_wep_key = wpa_eapol_set_wep_key;
1257 #endif /* CONFIG_WEP */
1258 #ifndef CONFIG_NO_CONFIG_BLOBS
1259 	ctx->set_config_blob = wpa_supplicant_set_config_blob;
1260 	ctx->get_config_blob = wpa_supplicant_get_config_blob;
1261 #endif /* CONFIG_NO_CONFIG_BLOBS */
1262 	ctx->aborted_cached = wpa_supplicant_aborted_cached;
1263 #ifndef CONFIG_OPENSC_ENGINE_PATH
1264 	ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
1265 #endif /* CONFIG_OPENSC_ENGINE_PATH */
1266 #ifndef CONFIG_PKCS11_ENGINE_PATH
1267 	ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
1268 #endif /* CONFIG_PKCS11_ENGINE_PATH */
1269 #ifndef CONFIG_PKCS11_MODULE_PATH
1270 	ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
1271 #endif /* CONFIG_PKCS11_MODULE_PATH */
1272 	ctx->openssl_ciphers = wpa_s->conf->openssl_ciphers;
1273 	ctx->wps = wpa_s->wps;
1274 	ctx->eap_param_needed = wpa_supplicant_eap_param_needed;
1275 #ifdef CONFIG_EAP_PROXY
1276 	ctx->eap_proxy_cb = wpa_supplicant_eap_proxy_cb;
1277 	ctx->eap_proxy_notify_sim_status =
1278 		wpa_supplicant_eap_proxy_notify_sim_status;
1279 #endif /* CONFIG_EAP_PROXY */
1280 	ctx->port_cb = wpa_supplicant_port_cb;
1281 	ctx->cb = wpa_supplicant_eapol_cb;
1282 	ctx->cert_cb = wpa_supplicant_cert_cb;
1283 	ctx->permanent_id_req_denied_cb = wpa_supplicant_permanent_id_req_denied_cb;
1284 	ctx->cert_in_cb = wpa_s->conf->cert_in_cb;
1285 	ctx->status_cb = wpa_supplicant_status_cb;
1286 	ctx->eap_error_cb = wpa_supplicant_eap_error_cb;
1287 	ctx->confirm_auth_cb = wpa_supplicant_eap_auth_start_cb;
1288 	ctx->set_anon_id = wpa_supplicant_set_anon_id;
1289 	ctx->eap_method_selected_cb = wpa_supplicant_eap_method_selected_cb;
1290 	ctx->open_ssl_failure_cb = wpa_supplicant_open_ssl_failure_cb;
1291 	ctx->get_certificate_cb = wpa_supplicant_get_certificate_cb;
1292 	ctx->encryption_required = wpas_encryption_required;
1293 	ctx->cb_ctx = wpa_s;
1294 	wpa_s->eapol = eapol_sm_init(ctx);
1295 	if (wpa_s->eapol == NULL) {
1296 		os_free(ctx);
1297 		wpa_printf(MSG_ERROR, "Failed to initialize EAPOL state "
1298 			   "machines.");
1299 		return -1;
1300 	}
1301 #endif /* IEEE8021X_EAPOL */
1302 
1303 	return 0;
1304 }
1305 
1306 
1307 #ifndef CONFIG_NO_WPA
1308 
wpa_supplicant_set_rekey_offload(void * ctx,const u8 * kek,size_t kek_len,const u8 * kck,size_t kck_len,const u8 * replay_ctr)1309 static void wpa_supplicant_set_rekey_offload(void *ctx,
1310 					     const u8 *kek, size_t kek_len,
1311 					     const u8 *kck, size_t kck_len,
1312 					     const u8 *replay_ctr)
1313 {
1314 	struct wpa_supplicant *wpa_s = ctx;
1315 
1316 	wpa_drv_set_rekey_info(wpa_s, kek, kek_len, kck, kck_len, replay_ctr);
1317 }
1318 
1319 
wpa_supplicant_key_mgmt_set_pmk(void * ctx,const u8 * pmk,size_t pmk_len)1320 static int wpa_supplicant_key_mgmt_set_pmk(void *ctx, const u8 *pmk,
1321 					   size_t pmk_len)
1322 {
1323 	struct wpa_supplicant *wpa_s = ctx;
1324 
1325 	if (wpa_s->conf->key_mgmt_offload &&
1326 	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_KEY_MGMT_OFFLOAD))
1327 		return wpa_drv_set_key(wpa_s, -1, 0, NULL, 0, 0,
1328 				       NULL, 0, pmk, pmk_len, KEY_FLAG_PMK);
1329 	else
1330 		return 0;
1331 }
1332 
1333 
wpa_supplicant_fils_hlp_rx(void * ctx,const u8 * dst,const u8 * src,const u8 * pkt,size_t pkt_len)1334 static void wpa_supplicant_fils_hlp_rx(void *ctx, const u8 *dst, const u8 *src,
1335 				       const u8 *pkt, size_t pkt_len)
1336 {
1337 	struct wpa_supplicant *wpa_s = ctx;
1338 	char *hex;
1339 	size_t hexlen;
1340 
1341 	hexlen = pkt_len * 2 + 1;
1342 	hex = os_malloc(hexlen);
1343 	if (!hex)
1344 		return;
1345 	wpa_snprintf_hex(hex, hexlen, pkt, pkt_len);
1346 	wpa_msg(wpa_s, MSG_INFO, FILS_HLP_RX "dst=" MACSTR " src=" MACSTR
1347 		" frame=%s", MAC2STR(dst), MAC2STR(src), hex);
1348 	os_free(hex);
1349 }
1350 
1351 
wpa_supplicant_channel_info(void * _wpa_s,struct wpa_channel_info * ci)1352 static int wpa_supplicant_channel_info(void *_wpa_s,
1353 				       struct wpa_channel_info *ci)
1354 {
1355 	struct wpa_supplicant *wpa_s = _wpa_s;
1356 
1357 	return wpa_drv_channel_info(wpa_s, ci);
1358 }
1359 
1360 
disable_wpa_wpa2(struct wpa_ssid * ssid)1361 static void disable_wpa_wpa2(struct wpa_ssid *ssid)
1362 {
1363 	ssid->proto &= ~WPA_PROTO_WPA;
1364 	ssid->proto |= WPA_PROTO_RSN;
1365 	ssid->key_mgmt &= ~(WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK |
1366 			    WPA_KEY_MGMT_PSK_SHA256);
1367 	ssid->group_cipher &= ~WPA_CIPHER_TKIP;
1368 	if (!(ssid->group_cipher & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP |
1369 				    WPA_CIPHER_GCMP_256 | WPA_CIPHER_CCMP_256)))
1370 		ssid->group_cipher |= WPA_CIPHER_CCMP;
1371 	ssid->ieee80211w = MGMT_FRAME_PROTECTION_REQUIRED;
1372 }
1373 
1374 
wpas_transition_disable(struct wpa_supplicant * wpa_s,u8 bitmap)1375 void wpas_transition_disable(struct wpa_supplicant *wpa_s, u8 bitmap)
1376 {
1377 	struct wpa_ssid *ssid;
1378 	int changed = 0;
1379 
1380 	wpa_msg(wpa_s, MSG_INFO, TRANSITION_DISABLE "%02x", bitmap);
1381 
1382 	ssid = wpa_s->current_ssid;
1383 	if (!ssid)
1384 		return;
1385 
1386 #ifdef CONFIG_SAE
1387 	if ((bitmap & TRANSITION_DISABLE_WPA3_PERSONAL) &&
1388 	    wpa_key_mgmt_sae(wpa_s->key_mgmt) &&
1389 	    wpa_key_mgmt_sae(ssid->key_mgmt) &&
1390 	    (ssid->ieee80211w != MGMT_FRAME_PROTECTION_REQUIRED ||
1391 	     (ssid->group_cipher & WPA_CIPHER_TKIP))) {
1392 		wpa_printf(MSG_DEBUG,
1393 			   "WPA3-Personal transition mode disabled based on AP notification");
1394 		disable_wpa_wpa2(ssid);
1395 		changed = 1;
1396 	}
1397 
1398 	if ((bitmap & TRANSITION_DISABLE_SAE_PK) &&
1399 	    wpa_key_mgmt_sae(wpa_s->key_mgmt) &&
1400 #ifdef CONFIG_SME
1401 	    wpa_s->sme.sae.state == SAE_ACCEPTED &&
1402 	    wpa_s->sme.sae.pk &&
1403 #endif /* CONFIG_SME */
1404 	    wpa_key_mgmt_sae(ssid->key_mgmt) &&
1405 	    (ssid->sae_pk != SAE_PK_MODE_ONLY ||
1406 	     ssid->ieee80211w != MGMT_FRAME_PROTECTION_REQUIRED ||
1407 	     (ssid->group_cipher & WPA_CIPHER_TKIP))) {
1408 		wpa_printf(MSG_DEBUG,
1409 			   "SAE-PK: SAE authentication without PK disabled based on AP notification");
1410 		disable_wpa_wpa2(ssid);
1411 		ssid->sae_pk = SAE_PK_MODE_ONLY;
1412 		changed = 1;
1413 	}
1414 #endif /* CONFIG_SAE */
1415 
1416 	if ((bitmap & TRANSITION_DISABLE_WPA3_ENTERPRISE) &&
1417 	    wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
1418 	    (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X |
1419 			       WPA_KEY_MGMT_FT_IEEE8021X |
1420 			       WPA_KEY_MGMT_IEEE8021X_SHA256 |
1421 			       WPA_KEY_MGMT_IEEE8021X_SHA384)) &&
1422 	    (ssid->ieee80211w != MGMT_FRAME_PROTECTION_REQUIRED ||
1423 	     (ssid->group_cipher & WPA_CIPHER_TKIP))) {
1424 		disable_wpa_wpa2(ssid);
1425 		changed = 1;
1426 	}
1427 
1428 	if ((bitmap & TRANSITION_DISABLE_ENHANCED_OPEN) &&
1429 	    wpa_s->key_mgmt == WPA_KEY_MGMT_OWE &&
1430 	    (ssid->key_mgmt & WPA_KEY_MGMT_OWE) &&
1431 	    !ssid->owe_only) {
1432 		ssid->owe_only = 1;
1433 		changed = 1;
1434 	}
1435 
1436 #if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
1437 	/* driver call for transition disable */
1438 	{
1439 		struct wpa_driver_associate_params params;
1440 
1441 		os_memset(&params, 0, sizeof(params));
1442 		params.td_policy = bitmap;
1443 		wpa_drv_update_connect_params(wpa_s, &params, WPA_DRV_UPDATE_TD_POLICY);
1444 	}
1445 #endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
1446 
1447 	wpas_notify_transition_disable(wpa_s, ssid, bitmap);
1448 
1449 	if (!changed)
1450 		return;
1451 
1452 #ifndef CONFIG_NO_CONFIG_WRITE
1453 	if (wpa_s->conf->update_config &&
1454 	    wpa_config_write(wpa_s->confname, wpa_s->conf))
1455 		wpa_printf(MSG_DEBUG, "Failed to update configuration");
1456 #endif /* CONFIG_NO_CONFIG_WRITE */
1457 }
1458 
1459 
wpa_supplicant_transition_disable(void * _wpa_s,u8 bitmap)1460 static void wpa_supplicant_transition_disable(void *_wpa_s, u8 bitmap)
1461 {
1462 	struct wpa_supplicant *wpa_s = _wpa_s;
1463 	wpas_transition_disable(wpa_s, bitmap);
1464 }
1465 
1466 
wpa_supplicant_store_ptk(void * ctx,const u8 * addr,int cipher,u32 life_time,const struct wpa_ptk * ptk)1467 static void wpa_supplicant_store_ptk(void *ctx, const u8 *addr, int cipher,
1468 				     u32 life_time, const struct wpa_ptk *ptk)
1469 {
1470 	struct wpa_supplicant *wpa_s = ctx;
1471 
1472 	ptksa_cache_add(wpa_s->ptksa, wpa_s->own_addr, addr, cipher, life_time,
1473 			ptk, NULL, NULL, 0);
1474 }
1475 
1476 #endif /* CONFIG_NO_WPA */
1477 
1478 
1479 #ifdef CONFIG_PASN
wpa_supplicant_set_ltf_keyseed(void * _wpa_s,const u8 * own_addr,const u8 * peer_addr,size_t ltf_keyseed_len,const u8 * ltf_keyseed)1480 static int wpa_supplicant_set_ltf_keyseed(void *_wpa_s, const u8 *own_addr,
1481 					  const u8 *peer_addr,
1482 					  size_t ltf_keyseed_len,
1483 					  const u8 *ltf_keyseed)
1484 {
1485 	struct wpa_supplicant *wpa_s = _wpa_s;
1486 
1487 	return wpa_drv_set_secure_ranging_ctx(wpa_s, own_addr, peer_addr, 0, 0,
1488 					      NULL, ltf_keyseed_len,
1489 					      ltf_keyseed, 0);
1490 }
1491 #endif /* CONFIG_PASN */
1492 
1493 
1494 static void
wpa_supplicant_notify_pmksa_cache_entry(void * _wpa_s,struct rsn_pmksa_cache_entry * entry)1495 wpa_supplicant_notify_pmksa_cache_entry(void *_wpa_s,
1496 					struct rsn_pmksa_cache_entry *entry)
1497 {
1498 	struct wpa_supplicant *wpa_s = _wpa_s;
1499 
1500 	wpas_notify_pmk_cache_added(wpa_s, entry);
1501 }
1502 
1503 
wpa_supplicant_ssid_verified(void * _wpa_s)1504 static void wpa_supplicant_ssid_verified(void *_wpa_s)
1505 {
1506 	struct wpa_supplicant *wpa_s = _wpa_s;
1507 
1508 	wpa_s->ssid_verified = true;
1509 	wpa_msg(wpa_s, MSG_INFO, "RSN: SSID matched expected value");
1510 }
1511 
1512 
wpa_supplicant_init_wpa(struct wpa_supplicant * wpa_s)1513 int wpa_supplicant_init_wpa(struct wpa_supplicant *wpa_s)
1514 {
1515 #ifndef CONFIG_NO_WPA
1516 	struct wpa_sm_ctx *ctx;
1517 
1518 	wpa_s->ptksa = ptksa_cache_init();
1519 	if (!wpa_s->ptksa) {
1520 		wpa_printf(MSG_ERROR, "Failed to allocate PTKSA");
1521 		return -1;
1522 	}
1523 
1524 	ctx = os_zalloc(sizeof(*ctx));
1525 	if (ctx == NULL) {
1526 		wpa_printf(MSG_ERROR, "Failed to allocate WPA context.");
1527 
1528 		ptksa_cache_deinit(wpa_s->ptksa);
1529 		wpa_s->ptksa = NULL;
1530 
1531 		return -1;
1532 	}
1533 
1534 	ctx->ctx = wpa_s;
1535 	ctx->msg_ctx = wpa_s;
1536 	ctx->set_state = _wpa_supplicant_set_state;
1537 	ctx->get_state = _wpa_supplicant_get_state;
1538 	ctx->deauthenticate = _wpa_supplicant_deauthenticate;
1539 	ctx->reconnect = _wpa_supplicant_reconnect;
1540 	ctx->set_key = wpa_supplicant_set_key;
1541 	ctx->get_network_ctx = wpa_supplicant_get_network_ctx;
1542 	ctx->get_bssid = wpa_supplicant_get_bssid;
1543 	ctx->ether_send = _wpa_ether_send;
1544 	ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
1545 	ctx->alloc_eapol = _wpa_alloc_eapol;
1546 	ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
1547 	ctx->add_pmkid = wpa_supplicant_add_pmkid;
1548 	ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
1549 #ifndef CONFIG_NO_CONFIG_BLOBS
1550 	ctx->set_config_blob = wpa_supplicant_set_config_blob;
1551 	ctx->get_config_blob = wpa_supplicant_get_config_blob;
1552 #endif /* CONFIG_NO_CONFIG_BLOBS */
1553 	ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection;
1554 #ifdef CONFIG_IEEE80211R
1555 	ctx->update_ft_ies = wpa_supplicant_update_ft_ies;
1556 	ctx->send_ft_action = wpa_supplicant_send_ft_action;
1557 	ctx->mark_authenticated = wpa_supplicant_mark_authenticated;
1558 #endif /* CONFIG_IEEE80211R */
1559 #ifdef CONFIG_TDLS
1560 	ctx->tdls_get_capa = wpa_supplicant_tdls_get_capa;
1561 	ctx->send_tdls_mgmt = wpa_supplicant_send_tdls_mgmt;
1562 	ctx->tdls_oper = wpa_supplicant_tdls_oper;
1563 	ctx->tdls_peer_addset = wpa_supplicant_tdls_peer_addset;
1564 	ctx->tdls_enable_channel_switch =
1565 		wpa_supplicant_tdls_enable_channel_switch;
1566 	ctx->tdls_disable_channel_switch =
1567 		wpa_supplicant_tdls_disable_channel_switch;
1568 #endif /* CONFIG_TDLS */
1569 	ctx->set_rekey_offload = wpa_supplicant_set_rekey_offload;
1570 	ctx->key_mgmt_set_pmk = wpa_supplicant_key_mgmt_set_pmk;
1571 	ctx->fils_hlp_rx = wpa_supplicant_fils_hlp_rx;
1572 	ctx->channel_info = wpa_supplicant_channel_info;
1573 	ctx->transition_disable = wpa_supplicant_transition_disable;
1574 	ctx->store_ptk = wpa_supplicant_store_ptk;
1575 #ifdef CONFIG_PASN
1576 	ctx->set_ltf_keyseed = wpa_supplicant_set_ltf_keyseed;
1577 #endif /* CONFIG_PASN */
1578 	ctx->notify_pmksa_cache_entry = wpa_supplicant_notify_pmksa_cache_entry;
1579 	ctx->ssid_verified = wpa_supplicant_ssid_verified;
1580 
1581 	wpa_s->wpa = wpa_sm_init(ctx);
1582 	if (wpa_s->wpa == NULL) {
1583 		wpa_printf(MSG_ERROR,
1584 			   "Failed to initialize WPA state machine");
1585 		os_free(ctx);
1586 		ptksa_cache_deinit(wpa_s->ptksa);
1587 		wpa_s->ptksa = NULL;
1588 		return -1;
1589 	}
1590 #endif /* CONFIG_NO_WPA */
1591 
1592 	return 0;
1593 }
1594 
1595 
wpa_supplicant_rsn_supp_set_config(struct wpa_supplicant * wpa_s,struct wpa_ssid * ssid)1596 void wpa_supplicant_rsn_supp_set_config(struct wpa_supplicant *wpa_s,
1597 					struct wpa_ssid *ssid)
1598 {
1599 	struct rsn_supp_config conf;
1600 	if (ssid) {
1601 		os_memset(&conf, 0, sizeof(conf));
1602 		conf.network_ctx = ssid;
1603 		conf.allowed_pairwise_cipher = ssid->pairwise_cipher;
1604 #ifdef IEEE8021X_EAPOL
1605 		conf.proactive_key_caching = ssid->proactive_key_caching < 0 ?
1606 			wpa_s->conf->okc : ssid->proactive_key_caching;
1607 		conf.eap_workaround = ssid->eap_workaround;
1608 		conf.eap_conf_ctx = &ssid->eap;
1609 #endif /* IEEE8021X_EAPOL */
1610 		conf.ssid = ssid->ssid;
1611 		conf.ssid_len = ssid->ssid_len;
1612 		conf.wpa_ptk_rekey = ssid->wpa_ptk_rekey;
1613 		conf.wpa_deny_ptk0_rekey = ssid->wpa_deny_ptk0_rekey;
1614 		conf.owe_ptk_workaround = ssid->owe_ptk_workaround;
1615 #ifdef CONFIG_P2P
1616 		if (ssid->p2p_group && wpa_s->current_bss &&
1617 		    !wpa_s->p2p_disable_ip_addr_req) {
1618 			struct wpabuf *p2p;
1619 			p2p = wpa_bss_get_vendor_ie_multi(wpa_s->current_bss,
1620 							  P2P_IE_VENDOR_TYPE);
1621 			if (p2p) {
1622 				u8 group_capab;
1623 				group_capab = p2p_get_group_capab(p2p);
1624 				if (group_capab &
1625 				    P2P_GROUP_CAPAB_IP_ADDR_ALLOCATION)
1626 					conf.p2p = 1;
1627 				wpabuf_free(p2p);
1628 			}
1629 		}
1630 #endif /* CONFIG_P2P */
1631 		conf.wpa_rsc_relaxation = wpa_s->conf->wpa_rsc_relaxation;
1632 #ifdef CONFIG_FILS
1633 		if (wpa_key_mgmt_fils(wpa_s->key_mgmt))
1634 			conf.fils_cache_id =
1635 				wpa_bss_get_fils_cache_id(wpa_s->current_bss);
1636 #endif /* CONFIG_FILS */
1637 		if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_BEACON_PROTECTION) ||
1638 		    (wpa_s->drv_flags2 &
1639 		     WPA_DRIVER_FLAGS2_BEACON_PROTECTION_CLIENT))
1640 			conf.beacon_prot = ssid->beacon_prot;
1641 
1642 #ifdef CONFIG_PASN
1643 #ifdef CONFIG_TESTING_OPTIONS
1644 		conf.force_kdk_derivation = wpa_s->conf->force_kdk_derivation;
1645 #endif /* CONFIG_TESTING_OPTIONS */
1646 #endif /* CONFIG_PASN */
1647 	}
1648 	wpa_sm_set_config(wpa_s->wpa, ssid ? &conf : NULL);
1649 }
1650