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