• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * wpa_supplicant - PASN processing
3  *
4  * Copyright (C) 2019 Intel Corporation
5  *
6  * This software may be distributed under the terms of the BSD license.
7  * See README for more details.
8  */
9 
10 #include "includes.h"
11 
12 #include "common/ieee802_11_defs.h"
13 #include "common/ieee802_11_common.h"
14 #include "common/dragonfly.h"
15 #include "common/ptksa_cache.h"
16 #include "utils/eloop.h"
17 #include "drivers/driver.h"
18 #include "crypto/crypto.h"
19 #include "crypto/random.h"
20 #include "eap_common/eap_defs.h"
21 #include "rsn_supp/wpa.h"
22 #include "rsn_supp/pmksa_cache.h"
23 #include "wpa_supplicant_i.h"
24 #include "driver_i.h"
25 #include "bss.h"
26 #include "scan.h"
27 #include "config.h"
28 
29 static const int dot11RSNAConfigPMKLifetime = 43200;
30 
31 struct wpa_pasn_auth_work {
32 	u8 own_addr[ETH_ALEN];
33 	u8 peer_addr[ETH_ALEN];
34 	int akmp;
35 	int cipher;
36 	u16 group;
37 	int network_id;
38 	struct wpabuf *comeback;
39 };
40 
41 
wpas_pasn_send_mlme(void * ctx,const u8 * data,size_t data_len,int noack,unsigned int freq,unsigned int wait)42 static int wpas_pasn_send_mlme(void *ctx, const u8 *data, size_t data_len,
43 			       int noack, unsigned int freq, unsigned int wait)
44 {
45 	struct wpa_supplicant *wpa_s = ctx;
46 
47 	return wpa_drv_send_mlme(wpa_s, data, data_len, noack, freq, wait);
48 }
49 
50 
wpas_pasn_free_auth_work(struct wpa_pasn_auth_work * awork)51 static void wpas_pasn_free_auth_work(struct wpa_pasn_auth_work *awork)
52 {
53 	wpabuf_free(awork->comeback);
54 	awork->comeback = NULL;
55 	os_free(awork);
56 }
57 
58 
wpas_pasn_auth_work_timeout(void * eloop_ctx,void * timeout_ctx)59 static void wpas_pasn_auth_work_timeout(void *eloop_ctx, void *timeout_ctx)
60 {
61 	struct wpa_supplicant *wpa_s = eloop_ctx;
62 
63 	wpa_printf(MSG_DEBUG, "PASN: Auth work timeout - stopping auth");
64 
65 	wpas_pasn_auth_stop(wpa_s);
66 
67 	wpas_pasn_auth_work_done(wpa_s, PASN_STATUS_FAILURE);
68 }
69 
70 
wpas_pasn_cancel_auth_work(struct wpa_supplicant * wpa_s)71 static void wpas_pasn_cancel_auth_work(struct wpa_supplicant *wpa_s)
72 {
73 	wpa_printf(MSG_DEBUG, "PASN: Cancel pasn-start-auth work");
74 
75 	/* Remove pending/started work */
76 	radio_remove_works(wpa_s, "pasn-start-auth", 0);
77 }
78 
79 
wpas_pasn_auth_status(struct wpa_supplicant * wpa_s,const u8 * peer_addr,int akmp,int cipher,u8 status,struct wpabuf * comeback,u16 comeback_after)80 static void wpas_pasn_auth_status(struct wpa_supplicant *wpa_s,
81 				  const u8 *peer_addr,
82 				  int akmp, int cipher, u8 status,
83 				  struct wpabuf *comeback,
84 				  u16 comeback_after)
85 {
86 	if (comeback) {
87 		size_t comeback_len = wpabuf_len(comeback);
88 		size_t buflen = comeback_len * 2 + 1;
89 		char *comeback_txt = os_malloc(buflen);
90 
91 		if (comeback_txt) {
92 			wpa_snprintf_hex(comeback_txt, buflen,
93 					 wpabuf_head(comeback), comeback_len);
94 
95 			wpa_msg(wpa_s, MSG_INFO, PASN_AUTH_STATUS MACSTR
96 				" akmp=%s, status=%u comeback_after=%u comeback=%s",
97 				MAC2STR(peer_addr),
98 				wpa_key_mgmt_txt(akmp, WPA_PROTO_RSN),
99 				status, comeback_after, comeback_txt);
100 
101 			os_free(comeback_txt);
102 			return;
103 		}
104 	}
105 
106 	wpa_msg(wpa_s, MSG_INFO,
107 		PASN_AUTH_STATUS MACSTR " akmp=%s, status=%u",
108 		MAC2STR(peer_addr), wpa_key_mgmt_txt(akmp, WPA_PROTO_RSN),
109 		status);
110 }
111 
112 
113 #ifdef CONFIG_SAE
114 
115 static struct sae_pt *
wpas_pasn_sae_derive_pt(struct wpa_ssid * ssid,int group)116 wpas_pasn_sae_derive_pt(struct wpa_ssid *ssid, int group)
117 {
118 	const char *password = ssid->sae_password;
119 	int groups[2] = { group, 0 };
120 
121 	if (!password)
122 		password = ssid->passphrase;
123 
124 	if (!password) {
125 		wpa_printf(MSG_DEBUG, "PASN: SAE without a password");
126 		return NULL;
127 	}
128 
129 	return sae_derive_pt(groups, ssid->ssid, ssid->ssid_len,
130 			    (const u8 *) password, os_strlen(password),
131 			    ssid->sae_password_id);
132 }
133 
134 
wpas_pasn_sae_setup_pt(struct wpa_ssid * ssid,int group)135 static int wpas_pasn_sae_setup_pt(struct wpa_ssid *ssid, int group)
136 {
137 	if (!ssid->sae_password && !ssid->passphrase) {
138 		wpa_printf(MSG_DEBUG, "PASN: SAE without a password");
139 		return -1;
140 	}
141 
142 	if (ssid->pt)
143 		return 0; /* PT already derived */
144 
145 	ssid->pt = wpas_pasn_sae_derive_pt(ssid, group);
146 
147 	return ssid->pt ? 0 : -1;
148 }
149 
150 #endif /* CONFIG_SAE */
151 
152 
wpas_pasn_get_params_from_bss(struct wpa_supplicant * wpa_s,struct pasn_peer * peer)153 static int wpas_pasn_get_params_from_bss(struct wpa_supplicant *wpa_s,
154 					 struct pasn_peer *peer)
155 {
156 	int ret;
157 	const u8 *rsne, *rsnxe;
158 	struct wpa_bss *bss;
159 	struct wpa_ie_data rsne_data;
160 	int sel, key_mgmt, pairwise_cipher;
161 	int network_id = 0, group = 19;
162 	struct wpa_ssid *ssid = NULL;
163 	size_t ssid_str_len = 0;
164 	const u8 *ssid_str = NULL;
165 	const u8 *peer_addr = peer->peer_addr;
166 
167 	bss = wpa_bss_get_bssid(wpa_s, peer_addr);
168 	if (!bss) {
169 		wpa_supplicant_update_scan_results(wpa_s);
170 		bss = wpa_bss_get_bssid(wpa_s, peer_addr);
171 		if (!bss) {
172 			wpa_printf(MSG_DEBUG, "PASN: BSS not found");
173 			return -1;
174 		}
175 	}
176 
177 	rsne = wpa_bss_get_ie(bss, WLAN_EID_RSN);
178 	if (!rsne) {
179 		wpa_printf(MSG_DEBUG, "PASN: BSS without RSNE");
180 		return -1;
181 	}
182 
183 	ret = wpa_parse_wpa_ie(rsne, *(rsne + 1) + 2, &rsne_data);
184 	if (ret) {
185 		wpa_printf(MSG_DEBUG, "PASN: Failed parsing RSNE data");
186 		return -1;
187 	}
188 
189 	rsnxe = wpa_bss_get_ie(bss, WLAN_EID_RSNX);
190 
191 	ssid_str_len = bss->ssid_len;
192 	ssid_str = bss->ssid;
193 
194 	/* Get the network configuration based on the obtained SSID */
195 	for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
196 		if (!wpas_network_disabled(wpa_s, ssid) &&
197 		    ssid_str_len == ssid->ssid_len &&
198 		    os_memcmp(ssid_str, ssid->ssid, ssid_str_len) == 0)
199 			break;
200 	}
201 
202 	if (ssid)
203 		network_id = ssid->id;
204 
205 	sel = rsne_data.pairwise_cipher;
206 	if (ssid && ssid->pairwise_cipher)
207 		sel &= ssid->pairwise_cipher;
208 
209 	wpa_printf(MSG_DEBUG, "PASN: peer pairwise 0x%x, select 0x%x",
210 		   rsne_data.pairwise_cipher, sel);
211 
212 	pairwise_cipher = wpa_pick_pairwise_cipher(sel, 1);
213 	if (pairwise_cipher < 0) {
214 		wpa_msg(wpa_s, MSG_WARNING,
215 			"PASN: Failed to select pairwise cipher");
216 		return -1;
217 	}
218 
219 	sel = rsne_data.key_mgmt;
220 	if (ssid && ssid->key_mgmt)
221 		sel &= ssid->key_mgmt;
222 
223 	wpa_printf(MSG_DEBUG, "PASN: peer AKMP 0x%x, select 0x%x",
224 		   rsne_data.key_mgmt, sel);
225 #ifdef CONFIG_SAE
226 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) || !ssid)
227 		sel &= ~(WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_SAE_EXT_KEY |
228 			 WPA_KEY_MGMT_FT_SAE | WPA_KEY_MGMT_FT_SAE_EXT_KEY);
229 #endif /* CONFIG_SAE */
230 #ifdef CONFIG_IEEE80211R
231 	if (!(wpa_s->drv_flags & (WPA_DRIVER_FLAGS_SME |
232 				  WPA_DRIVER_FLAGS_UPDATE_FT_IES)))
233 		sel &= ~WPA_KEY_MGMT_FT;
234 #endif /* CONFIG_IEEE80211R */
235 	if (0) {
236 #ifdef CONFIG_IEEE80211R
237 #ifdef CONFIG_SHA384
238 	} else if ((sel & WPA_KEY_MGMT_FT_IEEE8021X_SHA384) &&
239 		   os_strcmp(wpa_supplicant_get_eap_mode(wpa_s), "LEAP") != 0) {
240 		key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
241 		wpa_printf(MSG_DEBUG, "PASN: using KEY_MGMT FT/802.1X-SHA384");
242 		if (ssid && !ssid->ft_eap_pmksa_caching &&
243 		    pmksa_cache_get_current(wpa_s->wpa)) {
244 			/* PMKSA caching with FT may have interoperability
245 			 * issues, so disable that case by default for now.
246 			 */
247 			wpa_printf(MSG_DEBUG,
248 				   "PASN: Disable PMKSA caching for FT/802.1X connection");
249 			pmksa_cache_clear_current(wpa_s->wpa);
250 		}
251 #endif /* CONFIG_SHA384 */
252 #endif /* CONFIG_IEEE80211R */
253 #ifdef CONFIG_SAE
254 	} else if ((sel & WPA_KEY_MGMT_SAE_EXT_KEY) &&
255 		   (ieee802_11_rsnx_capab(rsnxe,
256 					   WLAN_RSNX_CAPAB_SAE_H2E)) &&
257 		   (wpas_pasn_sae_setup_pt(ssid, group) == 0)) {
258 		key_mgmt = WPA_KEY_MGMT_SAE_EXT_KEY;
259 		wpa_printf(MSG_DEBUG, "PASN: using KEY_MGMT SAE (ext key)");
260 	} else if ((sel & WPA_KEY_MGMT_SAE) &&
261 		   (ieee802_11_rsnx_capab(rsnxe,
262 					   WLAN_RSNX_CAPAB_SAE_H2E)) &&
263 		   (wpas_pasn_sae_setup_pt(ssid, group) == 0)) {
264 		key_mgmt = WPA_KEY_MGMT_SAE;
265 		wpa_printf(MSG_DEBUG, "PASN: using KEY_MGMT SAE");
266 #endif /* CONFIG_SAE */
267 #ifdef CONFIG_FILS
268 	} else if (sel & WPA_KEY_MGMT_FILS_SHA384) {
269 		key_mgmt = WPA_KEY_MGMT_FILS_SHA384;
270 		wpa_printf(MSG_DEBUG, "PASN: using KEY_MGMT FILS-SHA384");
271 	} else if (sel & WPA_KEY_MGMT_FILS_SHA256) {
272 		key_mgmt = WPA_KEY_MGMT_FILS_SHA256;
273 		wpa_printf(MSG_DEBUG, "PASN: using KEY_MGMT FILS-SHA256");
274 #endif /* CONFIG_FILS */
275 #ifdef CONFIG_IEEE80211R
276 	} else if ((sel & WPA_KEY_MGMT_FT_IEEE8021X) &&
277 		   os_strcmp(wpa_supplicant_get_eap_mode(wpa_s), "LEAP") != 0) {
278 		key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
279 		wpa_printf(MSG_DEBUG, "PASN: using KEY_MGMT FT/802.1X");
280 		if (ssid && !ssid->ft_eap_pmksa_caching &&
281 		    pmksa_cache_get_current(wpa_s->wpa)) {
282 			/* PMKSA caching with FT may have interoperability
283 			 * issues, so disable that case by default for now.
284 			 */
285 			wpa_printf(MSG_DEBUG,
286 				   "PASN: Disable PMKSA caching for FT/802.1X connection");
287 			pmksa_cache_clear_current(wpa_s->wpa);
288 		}
289 	} else if (sel & WPA_KEY_MGMT_FT_PSK) {
290 		key_mgmt = WPA_KEY_MGMT_FT_PSK;
291 		wpa_printf(MSG_DEBUG, "PASN: using KEY_MGMT FT/PSK");
292 #endif /* CONFIG_IEEE80211R */
293 	} else if (sel & WPA_KEY_MGMT_PASN) {
294 		key_mgmt = WPA_KEY_MGMT_PASN;
295 		wpa_printf(MSG_DEBUG, "PASN: using KEY_MGMT PASN");
296 	} else {
297 		wpa_printf(MSG_DEBUG, "PASN: invalid AKMP");
298 		return -1;
299 	}
300 
301 	peer->akmp = key_mgmt;
302 	peer->cipher = pairwise_cipher;
303 	peer->network_id = network_id;
304 	peer->group = group;
305 	return 0;
306 }
307 
308 
wpas_pasn_set_keys_from_cache(struct wpa_supplicant * wpa_s,const u8 * own_addr,const u8 * peer_addr,int cipher,int akmp)309 static int wpas_pasn_set_keys_from_cache(struct wpa_supplicant *wpa_s,
310 					 const u8 *own_addr,
311 					 const u8 *peer_addr,
312 					 int cipher, int akmp)
313 {
314 	struct ptksa_cache_entry *entry;
315 
316 	entry = ptksa_cache_get(wpa_s->ptksa, peer_addr, cipher);
317 	if (!entry) {
318 		wpa_printf(MSG_DEBUG, "PASN: peer " MACSTR
319 			   " not present in PTKSA cache", MAC2STR(peer_addr));
320 		return -1;
321 	}
322 
323 	if (os_memcmp(entry->own_addr, own_addr, ETH_ALEN) != 0) {
324 		wpa_printf(MSG_DEBUG,
325 			   "PASN: own addr " MACSTR " and PTKSA entry own addr "
326 			   MACSTR " differ",
327 			   MAC2STR(own_addr), MAC2STR(entry->own_addr));
328 		return -1;
329 	}
330 
331 	wpa_printf(MSG_DEBUG, "PASN: " MACSTR " present in PTKSA cache",
332 		   MAC2STR(peer_addr));
333 	wpa_drv_set_secure_ranging_ctx(wpa_s, own_addr, peer_addr, cipher,
334 				       entry->ptk.tk_len,
335 				       entry->ptk.tk,
336 				       entry->ptk.ltf_keyseed_len,
337 				       entry->ptk.ltf_keyseed, 0);
338 	return 0;
339 }
340 
341 
wpas_pasn_configure_next_peer(struct wpa_supplicant * wpa_s,struct pasn_auth * pasn_params)342 static void wpas_pasn_configure_next_peer(struct wpa_supplicant *wpa_s,
343 					  struct pasn_auth *pasn_params)
344 {
345 	struct pasn_peer *peer;
346 	u8 comeback_len = 0;
347 	const u8 *comeback = NULL;
348 
349 	if (!pasn_params)
350 		return;
351 
352 	while (wpa_s->pasn_count < pasn_params->num_peers) {
353 		peer = &pasn_params->peer[wpa_s->pasn_count];
354 
355 		if (os_memcmp(wpa_s->bssid, peer->peer_addr, ETH_ALEN) == 0) {
356 			wpa_printf(MSG_DEBUG,
357 				   "PASN: Associated peer is not expected");
358 			peer->status = PASN_STATUS_FAILURE;
359 			wpa_s->pasn_count++;
360 			continue;
361 		}
362 
363 		if (wpas_pasn_set_keys_from_cache(wpa_s, peer->own_addr,
364 						  peer->peer_addr,
365 						  peer->cipher,
366 						  peer->akmp) == 0) {
367 			peer->status = PASN_STATUS_SUCCESS;
368 			wpa_s->pasn_count++;
369 			continue;
370 		}
371 
372 		if (wpas_pasn_get_params_from_bss(wpa_s, peer)) {
373 			peer->status = PASN_STATUS_FAILURE;
374 			wpa_s->pasn_count++;
375 			continue;
376 		}
377 
378 		if (wpas_pasn_auth_start(wpa_s, peer->own_addr,
379 					 peer->peer_addr, peer->akmp,
380 					 peer->cipher, peer->group,
381 					 peer->network_id,
382 					 comeback, comeback_len)) {
383 			peer->status = PASN_STATUS_FAILURE;
384 			wpa_s->pasn_count++;
385 			continue;
386 		}
387 		wpa_printf(MSG_DEBUG, "PASN: Sent PASN auth start for " MACSTR,
388 			   MAC2STR(peer->peer_addr));
389 		return;
390 	}
391 
392 	if (wpa_s->pasn_count == pasn_params->num_peers) {
393 		wpa_drv_send_pasn_resp(wpa_s, pasn_params);
394 		wpa_printf(MSG_DEBUG, "PASN: Response sent");
395 		os_free(wpa_s->pasn_params);
396 		wpa_s->pasn_params = NULL;
397 	}
398 }
399 
400 
wpas_pasn_auth_work_done(struct wpa_supplicant * wpa_s,int status)401 void wpas_pasn_auth_work_done(struct wpa_supplicant *wpa_s, int status)
402 {
403 	if (!wpa_s->pasn_params)
404 		return;
405 
406 	wpa_s->pasn_params->peer[wpa_s->pasn_count].status = status;
407 	wpa_s->pasn_count++;
408 	wpas_pasn_configure_next_peer(wpa_s, wpa_s->pasn_params);
409 }
410 
411 
wpas_pasn_delete_peers(struct wpa_supplicant * wpa_s,struct pasn_auth * pasn_params)412 static void wpas_pasn_delete_peers(struct wpa_supplicant *wpa_s,
413 				   struct pasn_auth *pasn_params)
414 {
415 	struct pasn_peer *peer;
416 	unsigned int i;
417 
418 	if (!pasn_params)
419 		return;
420 
421 	for (i = 0; i < pasn_params->num_peers; i++) {
422 		peer = &pasn_params->peer[i];
423 		ptksa_cache_flush(wpa_s->ptksa, peer->peer_addr,
424 				  WPA_CIPHER_NONE);
425 	}
426 }
427 
428 
wpas_pasn_initiate_eapol(struct pasn_data * pasn,struct wpa_ssid * ssid)429 static void wpas_pasn_initiate_eapol(struct pasn_data *pasn,
430 				     struct wpa_ssid *ssid)
431 {
432 	struct eapol_config eapol_conf;
433 
434 	wpa_printf(MSG_DEBUG, "PASN: FILS: Initiating EAPOL");
435 
436 	eapol_sm_notify_eap_success(pasn->eapol, false);
437 	eapol_sm_notify_eap_fail(pasn->eapol, false);
438 	eapol_sm_notify_portControl(pasn->eapol, Auto);
439 
440 	os_memset(&eapol_conf, 0, sizeof(eapol_conf));
441 	eapol_conf.fast_reauth = pasn->fast_reauth;
442 	eapol_conf.workaround = ssid->eap_workaround;
443 
444 	eapol_sm_notify_config(pasn->eapol, &ssid->eap, &eapol_conf);
445 }
446 
447 
wpas_pasn_reset(struct wpa_supplicant * wpa_s)448 static void wpas_pasn_reset(struct wpa_supplicant *wpa_s)
449 {
450 	struct pasn_data *pasn = &wpa_s->pasn;
451 
452 	wpas_pasn_cancel_auth_work(wpa_s);
453 	wpa_s->pasn_auth_work = NULL;
454 	eloop_cancel_timeout(wpas_pasn_auth_work_timeout, wpa_s, NULL);
455 
456 	wpa_pasn_reset(pasn);
457 }
458 
459 
wpas_pasn_allowed(struct wpa_supplicant * wpa_s,const u8 * peer_addr,int akmp,int cipher)460 static struct wpa_bss * wpas_pasn_allowed(struct wpa_supplicant *wpa_s,
461 					  const u8 *peer_addr, int akmp,
462 					  int cipher)
463 {
464 	struct wpa_bss *bss;
465 	const u8 *rsne;
466 	struct wpa_ie_data rsne_data;
467 	int ret;
468 
469 	if (os_memcmp(wpa_s->bssid, peer_addr, ETH_ALEN) == 0) {
470 		wpa_printf(MSG_DEBUG,
471 			   "PASN: Not doing authentication with current BSS");
472 		return NULL;
473 	}
474 
475 	bss = wpa_bss_get_bssid(wpa_s, peer_addr);
476 	if (!bss) {
477 		wpa_printf(MSG_DEBUG, "PASN: BSS not found");
478 		return NULL;
479 	}
480 
481 	rsne = wpa_bss_get_ie(bss, WLAN_EID_RSN);
482 	if (!rsne) {
483 		wpa_printf(MSG_DEBUG, "PASN: BSS without RSNE");
484 		return NULL;
485 	}
486 
487 	ret = wpa_parse_wpa_ie(rsne, *(rsne + 1) + 2, &rsne_data);
488 	if (ret) {
489 		wpa_printf(MSG_DEBUG, "PASN: Failed parsing RSNE data");
490 		return NULL;
491 	}
492 
493 	if (!(rsne_data.key_mgmt & akmp) ||
494 	    !(rsne_data.pairwise_cipher & cipher)) {
495 		wpa_printf(MSG_DEBUG,
496 			   "PASN: AP does not support requested AKMP or cipher");
497 		return NULL;
498 	}
499 
500 	return bss;
501 }
502 
503 
wpas_pasn_auth_start_cb(struct wpa_radio_work * work,int deinit)504 static void wpas_pasn_auth_start_cb(struct wpa_radio_work *work, int deinit)
505 {
506 	struct wpa_supplicant *wpa_s = work->wpa_s;
507 	struct wpa_pasn_auth_work *awork = work->ctx;
508 	struct pasn_data *pasn = &wpa_s->pasn;
509 	struct wpa_ssid *ssid;
510 	struct wpa_bss *bss;
511 	const u8 *rsne, *rsnxe;
512 	const u8 *indic;
513 	u16 fils_info;
514 	u16 capab = 0;
515 	bool derive_kdk;
516 	int ret;
517 
518 	wpa_printf(MSG_DEBUG, "PASN: auth_start_cb: deinit=%d", deinit);
519 
520 	if (deinit) {
521 		if (work->started) {
522 			eloop_cancel_timeout(wpas_pasn_auth_work_timeout,
523 					     wpa_s, NULL);
524 			wpa_s->pasn_auth_work = NULL;
525 		}
526 
527 		wpas_pasn_free_auth_work(awork);
528 		return;
529 	}
530 
531 	/*
532 	 * It is possible that by the time the callback is called, the PASN
533 	 * authentication is not allowed, e.g., a connection with the AP was
534 	 * established.
535 	 */
536 	bss = wpas_pasn_allowed(wpa_s, awork->peer_addr, awork->akmp,
537 				awork->cipher);
538 	if (!bss) {
539 		wpa_printf(MSG_DEBUG, "PASN: auth_start_cb: Not allowed");
540 		goto fail;
541 	}
542 
543 	rsne = wpa_bss_get_ie(bss, WLAN_EID_RSN);
544 	if (!rsne) {
545 		wpa_printf(MSG_DEBUG, "PASN: BSS without RSNE");
546 		goto fail;
547 	}
548 
549 	rsnxe = wpa_bss_get_ie(bss, WLAN_EID_RSNX);
550 
551 	derive_kdk = (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_SEC_LTF_STA) &&
552 		ieee802_11_rsnx_capab(rsnxe,
553 				      WLAN_RSNX_CAPAB_SECURE_LTF);
554 #ifdef CONFIG_TESTING_OPTIONS
555 	if (!derive_kdk)
556 		derive_kdk = wpa_s->conf->force_kdk_derivation;
557 #endif /* CONFIG_TESTING_OPTIONS */
558 	if (derive_kdk)
559 		pasn->kdk_len = WPA_KDK_MAX_LEN;
560 	else
561 		pasn->kdk_len = 0;
562 	wpa_printf(MSG_DEBUG, "PASN: kdk_len=%zu", pasn->kdk_len);
563 
564 	if ((wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_SEC_LTF_STA) &&
565 	    ieee802_11_rsnx_capab(rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF))
566 		pasn->secure_ltf = true;
567 	else
568 		pasn->secure_ltf = false;
569 
570 #ifdef CONFIG_TESTING_OPTIONS
571 	pasn->corrupt_mic = wpa_s->conf->pasn_corrupt_mic;
572 #endif /* CONFIG_TESTING_OPTIONS */
573 
574 	capab |= BIT(WLAN_RSNX_CAPAB_SAE_H2E);
575 	if (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_SEC_LTF_STA)
576 		capab |= BIT(WLAN_RSNX_CAPAB_SECURE_LTF);
577 	if (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_SEC_RTT_STA)
578 		capab |= BIT(WLAN_RSNX_CAPAB_SECURE_RTT);
579 	if (wpa_s->drv_flags2 & WPA_DRIVER_FLAGS2_PROT_RANGE_NEG_STA)
580 		capab |= BIT(WLAN_RSNX_CAPAB_URNM_MFPR);
581 	pasn->rsnxe_capab = capab;
582 	pasn->send_mgmt = wpas_pasn_send_mlme;
583 
584 	ssid = wpa_config_get_network(wpa_s->conf, awork->network_id);
585 
586 #ifdef CONFIG_SAE
587 	if (awork->akmp == WPA_KEY_MGMT_SAE) {
588 		if (!ssid) {
589 			wpa_printf(MSG_DEBUG,
590 				   "PASN: No network profile found for SAE");
591 			goto fail;
592 		}
593 		pasn->pt = wpas_pasn_sae_derive_pt(ssid, awork->group);
594 		if (!pasn->pt) {
595 			wpa_printf(MSG_DEBUG, "PASN: Failed to derive PT");
596 			goto fail;
597 		}
598 		pasn->network_id = ssid->id;
599 	}
600 #endif /* CONFIG_SAE */
601 
602 #ifdef CONFIG_FILS
603 	/* Prepare needed information for wpas_pasn_wd_fils_auth(). */
604 	if (awork->akmp == WPA_KEY_MGMT_FILS_SHA256 ||
605 	    awork->akmp == WPA_KEY_MGMT_FILS_SHA384) {
606 		indic = wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION);
607 		if (!ssid) {
608 			wpa_printf(MSG_DEBUG, "PASN: FILS: No network block");
609 		} else if (!indic || indic[1] < 2) {
610 			wpa_printf(MSG_DEBUG,
611 				   "PASN: Missing FILS Indication IE");
612 		} else {
613 			fils_info = WPA_GET_LE16(indic + 2);
614 			if ((fils_info & BIT(9)) && ssid) {
615 				pasn->eapol = wpa_s->eapol;
616 				pasn->network_id = ssid->id;
617 				wpas_pasn_initiate_eapol(pasn, ssid);
618 				pasn->fils_eapol = true;
619 			} else {
620 				wpa_printf(MSG_DEBUG,
621 					   "PASN: FILS auth without PFS not supported");
622 			}
623 		}
624 		pasn->fast_reauth = wpa_s->conf->fast_reauth;
625 	}
626 #endif /* CONFIG_FILS */
627 
628 	pasn->cb_ctx = wpa_s;
629 	pasn->pmksa = wpa_sm_get_pmksa_cache(wpa_s->wpa);
630 
631 	if (wpa_key_mgmt_ft(awork->akmp)) {
632 #ifdef CONFIG_IEEE80211R
633 		ret = wpa_pasn_ft_derive_pmk_r1(wpa_s->wpa, awork->akmp,
634 						awork->peer_addr,
635 						pasn->pmk_r1,
636 						&pasn->pmk_r1_len,
637 						pasn->pmk_r1_name);
638 		if (ret) {
639 			wpa_printf(MSG_DEBUG,
640 				   "PASN: FT: Failed to derive keys");
641 			goto fail;
642 		}
643 #else /* CONFIG_IEEE80211R */
644 		goto fail;
645 #endif /* CONFIG_IEEE80211R */
646 	}
647 
648 
649 	ret = wpas_pasn_start(pasn, awork->own_addr, awork->peer_addr,
650 			      awork->peer_addr, awork->akmp, awork->cipher,
651 			      awork->group, bss->freq, rsne, *(rsne + 1) + 2,
652 			      rsnxe, rsnxe ? *(rsnxe + 1) + 2 : 0,
653 			      awork->comeback);
654 	if (ret) {
655 		wpa_printf(MSG_DEBUG,
656 			   "PASN: Failed to start PASN authentication");
657 		goto fail;
658 	}
659 	eloop_register_timeout(2, 0, wpas_pasn_auth_work_timeout, wpa_s, NULL);
660 
661 	/* comeback token is no longer needed at this stage */
662 	wpabuf_free(awork->comeback);
663 	awork->comeback = NULL;
664 
665 	wpa_s->pasn_auth_work = work;
666 	return;
667 fail:
668 	wpas_pasn_free_auth_work(awork);
669 	work->ctx = NULL;
670 	radio_work_done(work);
671 }
672 
673 
wpas_pasn_auth_start(struct wpa_supplicant * wpa_s,const u8 * own_addr,const u8 * peer_addr,int akmp,int cipher,u16 group,int network_id,const u8 * comeback,size_t comeback_len)674 int wpas_pasn_auth_start(struct wpa_supplicant *wpa_s,
675 			 const u8 *own_addr, const u8 *peer_addr,
676 			 int akmp, int cipher, u16 group, int network_id,
677 			 const u8 *comeback, size_t comeback_len)
678 {
679 	struct wpa_pasn_auth_work *awork;
680 	struct wpa_bss *bss;
681 
682 	wpa_printf(MSG_DEBUG, "PASN: Start: " MACSTR " akmp=0x%x, cipher=0x%x",
683 		   MAC2STR(peer_addr), akmp, cipher);
684 
685 	/*
686 	 * TODO: Consider modifying the offchannel logic to handle additional
687 	 * Management frames other then Action frames. For now allow PASN only
688 	 * with drivers that support off-channel TX.
689 	 */
690 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_TX)) {
691 		wpa_printf(MSG_DEBUG,
692 			   "PASN: Driver does not support offchannel TX");
693 		return -1;
694 	}
695 
696 	if (radio_work_pending(wpa_s, "pasn-start-auth")) {
697 		wpa_printf(MSG_DEBUG,
698 			   "PASN: send_auth: Work is already pending");
699 		return -1;
700 	}
701 
702 	if (wpa_s->pasn_auth_work) {
703 		wpa_printf(MSG_DEBUG, "PASN: send_auth: Already in progress");
704 		return -1;
705 	}
706 
707 	bss = wpas_pasn_allowed(wpa_s, peer_addr, akmp, cipher);
708 	if (!bss)
709 		return -1;
710 
711 	wpas_pasn_reset(wpa_s);
712 
713 	awork = os_zalloc(sizeof(*awork));
714 	if (!awork)
715 		return -1;
716 
717 	os_memcpy(awork->own_addr, own_addr, ETH_ALEN);
718 	os_memcpy(awork->peer_addr, peer_addr, ETH_ALEN);
719 	awork->akmp = akmp;
720 	awork->cipher = cipher;
721 	awork->group = group;
722 	awork->network_id = network_id;
723 
724 	if (comeback && comeback_len) {
725 		awork->comeback = wpabuf_alloc_copy(comeback, comeback_len);
726 		if (!awork->comeback) {
727 			wpas_pasn_free_auth_work(awork);
728 			return -1;
729 		}
730 	}
731 
732 	if (radio_add_work(wpa_s, bss->freq, "pasn-start-auth", 1,
733 			   wpas_pasn_auth_start_cb, awork) < 0) {
734 		wpas_pasn_free_auth_work(awork);
735 		return -1;
736 	}
737 
738 	wpa_printf(MSG_DEBUG, "PASN: Auth work successfully added");
739 	return 0;
740 }
741 
742 
wpas_pasn_auth_stop(struct wpa_supplicant * wpa_s)743 void wpas_pasn_auth_stop(struct wpa_supplicant *wpa_s)
744 {
745 	struct pasn_data *pasn = &wpa_s->pasn;
746 
747 	if (!wpa_s->pasn.ecdh)
748 		return;
749 
750 	wpa_printf(MSG_DEBUG, "PASN: Stopping authentication");
751 
752 	wpas_pasn_auth_status(wpa_s, pasn->peer_addr, pasn->akmp, pasn->cipher,
753 			      pasn->status, pasn->comeback,
754 			      pasn->comeback_after);
755 
756 	wpas_pasn_reset(wpa_s);
757 }
758 
759 
wpas_pasn_immediate_retry(struct wpa_supplicant * wpa_s,struct pasn_data * pasn,struct wpa_pasn_params_data * params)760 static int wpas_pasn_immediate_retry(struct wpa_supplicant *wpa_s,
761 				     struct pasn_data *pasn,
762 				     struct wpa_pasn_params_data *params)
763 {
764 	int akmp = pasn->akmp;
765 	int cipher = pasn->cipher;
766 	u16 group = pasn->group;
767 	u8 own_addr[ETH_ALEN];
768 	u8 peer_addr[ETH_ALEN];
769 
770 	wpa_printf(MSG_DEBUG, "PASN: Immediate retry");
771 	os_memcpy(own_addr, pasn->own_addr, ETH_ALEN);
772 	os_memcpy(peer_addr, pasn->peer_addr, ETH_ALEN);
773 	wpas_pasn_reset(wpa_s);
774 
775 	return wpas_pasn_auth_start(wpa_s, own_addr, peer_addr, akmp, cipher,
776 				    group, pasn->network_id,
777 				    params->comeback, params->comeback_len);
778 }
779 
780 
wpas_pasn_deauth_cb(struct ptksa_cache_entry * entry)781 static void wpas_pasn_deauth_cb(struct ptksa_cache_entry *entry)
782 {
783 	struct wpa_supplicant *wpa_s = entry->ctx;
784 	u8 own_addr[ETH_ALEN];
785 	u8 peer_addr[ETH_ALEN];
786 
787 	/* Use a copy of the addresses from the entry to avoid issues with the
788 	 * entry getting freed during deauthentication processing. */
789 	os_memcpy(own_addr, entry->own_addr, ETH_ALEN);
790 	os_memcpy(peer_addr, entry->addr, ETH_ALEN);
791 	wpas_pasn_deauthenticate(wpa_s, own_addr, peer_addr);
792 }
793 
794 
wpas_pasn_auth_rx(struct wpa_supplicant * wpa_s,const struct ieee80211_mgmt * mgmt,size_t len)795 int wpas_pasn_auth_rx(struct wpa_supplicant *wpa_s,
796 		      const struct ieee80211_mgmt *mgmt, size_t len)
797 {
798 	struct pasn_data *pasn = &wpa_s->pasn;
799 	struct wpa_pasn_params_data pasn_data;
800 	int ret;
801 
802 	if (!wpa_s->pasn_auth_work)
803 		return -2;
804 
805 	pasn->cb_ctx = wpa_s;
806 	ret = wpa_pasn_auth_rx(pasn, (const u8 *) mgmt, len, &pasn_data);
807 	if (ret == 0) {
808 		ptksa_cache_add(wpa_s->ptksa, pasn->own_addr, pasn->peer_addr,
809 				pasn->cipher, dot11RSNAConfigPMKLifetime,
810 				&pasn->ptk,
811 				wpa_s->pasn_params ? wpas_pasn_deauth_cb : NULL,
812 				wpa_s->pasn_params ? wpa_s : NULL, pasn->akmp);
813 
814 		if (pasn->pmksa_entry)
815 			wpa_sm_set_cur_pmksa(wpa_s->wpa, pasn->pmksa_entry);
816 	}
817 
818 	forced_memzero(&pasn->ptk, sizeof(pasn->ptk));
819 
820 	if (ret == -1) {
821 		wpas_pasn_auth_stop(wpa_s);
822 		wpas_pasn_auth_work_done(wpa_s, PASN_STATUS_FAILURE);
823 	}
824 
825 	if (ret == 1)
826 		ret = wpas_pasn_immediate_retry(wpa_s, pasn, &pasn_data);
827 
828 	return ret;
829 }
830 
831 
wpas_pasn_auth_trigger(struct wpa_supplicant * wpa_s,struct pasn_auth * pasn_auth)832 void wpas_pasn_auth_trigger(struct wpa_supplicant *wpa_s,
833 			    struct pasn_auth *pasn_auth)
834 {
835 	struct pasn_peer *src, *dst;
836 	unsigned int i, num_peers = pasn_auth->num_peers;
837 
838 	if (wpa_s->pasn_params) {
839 		wpa_printf(MSG_DEBUG,
840 			   "PASN: auth_trigger: Already in progress");
841 		return;
842 	}
843 
844 	if (!num_peers || num_peers > WPAS_MAX_PASN_PEERS) {
845 		wpa_printf(MSG_DEBUG,
846 			   "PASN: auth trigger: Invalid number of peers");
847 		return;
848 	}
849 
850 	wpa_s->pasn_params = os_zalloc(sizeof(struct pasn_auth));
851 	if (!wpa_s->pasn_params) {
852 		wpa_printf(MSG_DEBUG,
853 			   "PASN: auth trigger: Failed to allocate a buffer");
854 		return;
855 	}
856 
857 	wpa_s->pasn_count = 0;
858 	wpa_s->pasn_params->num_peers = num_peers;
859 
860 	for (i = 0; i < num_peers; i++) {
861 		dst = &wpa_s->pasn_params->peer[i];
862 		src = &pasn_auth->peer[i];
863 		os_memcpy(dst->own_addr, wpa_s->own_addr, ETH_ALEN);
864 		os_memcpy(dst->peer_addr, src->peer_addr, ETH_ALEN);
865 		dst->ltf_keyseed_required = src->ltf_keyseed_required;
866 		dst->status = PASN_STATUS_SUCCESS;
867 
868 		if (!is_zero_ether_addr(src->own_addr)) {
869 			os_memcpy(dst->own_addr, src->own_addr, ETH_ALEN);
870 			wpa_printf(MSG_DEBUG, "PASN: Own (source) MAC addr: "
871 				   MACSTR, MAC2STR(dst->own_addr));
872 		}
873 	}
874 
875 	if (pasn_auth->action == PASN_ACTION_DELETE_SECURE_RANGING_CONTEXT) {
876 		wpas_pasn_delete_peers(wpa_s, wpa_s->pasn_params);
877 		os_free(wpa_s->pasn_params);
878 		wpa_s->pasn_params = NULL;
879 	} else if (pasn_auth->action == PASN_ACTION_AUTH) {
880 		wpas_pasn_configure_next_peer(wpa_s, wpa_s->pasn_params);
881 	}
882 }
883 
884 
wpas_pasn_auth_tx_status(struct wpa_supplicant * wpa_s,const u8 * data,size_t data_len,u8 acked)885 int wpas_pasn_auth_tx_status(struct wpa_supplicant *wpa_s,
886 			     const u8 *data, size_t data_len, u8 acked)
887 
888 {
889 	struct pasn_data *pasn = &wpa_s->pasn;
890 	int ret;
891 
892 	if (!wpa_s->pasn_auth_work) {
893 		wpa_printf(MSG_DEBUG,
894 			   "PASN: auth_tx_status: no work in progress");
895 		return -1;
896 	}
897 
898 	ret = wpa_pasn_auth_tx_status(pasn, data, data_len, acked);
899 	if (ret != 1)
900 		return ret;
901 
902 	if (!wpa_s->pasn_params) {
903 		wpas_pasn_auth_stop(wpa_s);
904 		return 0;
905 	}
906 
907 	wpas_pasn_set_keys_from_cache(wpa_s, pasn->own_addr, pasn->peer_addr,
908 				      pasn->cipher, pasn->akmp);
909 	wpas_pasn_auth_stop(wpa_s);
910 	wpas_pasn_auth_work_done(wpa_s, PASN_STATUS_SUCCESS);
911 
912 	return 0;
913 }
914 
915 
wpas_pasn_deauthenticate(struct wpa_supplicant * wpa_s,const u8 * own_addr,const u8 * peer_addr)916 int wpas_pasn_deauthenticate(struct wpa_supplicant *wpa_s, const u8 *own_addr,
917 			     const u8 *peer_addr)
918 {
919 	struct wpa_bss *bss;
920 	struct wpabuf *buf;
921 	struct ieee80211_mgmt *deauth;
922 	int ret;
923 
924 	if (os_memcmp(wpa_s->bssid, peer_addr, ETH_ALEN) == 0) {
925 		wpa_printf(MSG_DEBUG,
926 			   "PASN: Cannot deauthenticate from current BSS");
927 		return -1;
928 	}
929 
930 	wpa_drv_set_secure_ranging_ctx(wpa_s, own_addr, peer_addr, 0, 0, NULL,
931 				       0, NULL, 1);
932 
933 	wpa_printf(MSG_DEBUG, "PASN: deauth: Flushing all PTKSA entries for "
934 		   MACSTR, MAC2STR(peer_addr));
935 	ptksa_cache_flush(wpa_s->ptksa, peer_addr, WPA_CIPHER_NONE);
936 
937 	bss = wpa_bss_get_bssid(wpa_s, peer_addr);
938 	if (!bss) {
939 		wpa_printf(MSG_DEBUG, "PASN: deauth: BSS not found");
940 		return -1;
941 	}
942 
943 	buf = wpabuf_alloc(64);
944 	if (!buf) {
945 		wpa_printf(MSG_DEBUG, "PASN: deauth: Failed wpabuf allocate");
946 		return -1;
947 	}
948 
949 	deauth = wpabuf_put(buf, offsetof(struct ieee80211_mgmt,
950 					  u.deauth.variable));
951 
952 	deauth->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
953 					     (WLAN_FC_STYPE_DEAUTH << 4));
954 
955 	os_memcpy(deauth->da, peer_addr, ETH_ALEN);
956 	os_memcpy(deauth->sa, own_addr, ETH_ALEN);
957 	os_memcpy(deauth->bssid, peer_addr, ETH_ALEN);
958 	deauth->u.deauth.reason_code =
959 		host_to_le16(WLAN_REASON_PREV_AUTH_NOT_VALID);
960 
961 	/*
962 	 * Since we do not expect any response from the AP, implement the
963 	 * Deauthentication frame transmission using direct call to the driver
964 	 * without a radio work.
965 	 */
966 	ret = wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1,
967 				bss->freq, 0);
968 
969 	wpabuf_free(buf);
970 	wpa_printf(MSG_DEBUG, "PASN: deauth: send_mlme ret=%d", ret);
971 
972 	return ret;
973 }
974