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