xref: /aosp_15_r20/external/wpa_supplicant_8/src/rsn_supp/pmksa_cache.c (revision 03f9172ca588f91df233974f4258bab95191f931)
1 /*
2  * WPA Supplicant - RSN PMKSA cache
3  * Copyright (c) 2004-2009, 2011-2015, Jouni Malinen <[email protected]>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 
11 #include "common.h"
12 #include "eloop.h"
13 #include "eapol_supp/eapol_supp_sm.h"
14 #include "wpa.h"
15 #include "wpa_i.h"
16 #include "pmksa_cache.h"
17 #include "wpa_supplicant_i.h"
18 #include "notify.h"
19 
20 #if defined(IEEE8021X_EAPOL) && !defined(CONFIG_NO_WPA)
21 
22 static const int pmksa_cache_max_entries = 32;
23 
24 struct rsn_pmksa_cache {
25 	struct rsn_pmksa_cache_entry *pmksa; /* PMKSA cache */
26 	int pmksa_count; /* number of entries in PMKSA cache */
27 	struct wpa_sm *sm; /* TODO: get rid of this reference(?) */
28 
29 	void (*free_cb)(struct rsn_pmksa_cache_entry *entry, void *ctx,
30 			enum pmksa_free_reason reason);
31 	bool (*is_current_cb)(struct rsn_pmksa_cache_entry *entry,
32 			      void *ctx);
33 	void (*notify_cb)(struct rsn_pmksa_cache_entry *entry, void *ctx);
34 	void *ctx;
35 };
36 
37 
38 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa);
39 
40 
_pmksa_cache_free_entry(struct rsn_pmksa_cache_entry * entry)41 static void _pmksa_cache_free_entry(struct rsn_pmksa_cache_entry *entry)
42 {
43 	bin_clear_free(entry, sizeof(*entry));
44 }
45 
46 
pmksa_cache_free_entry(struct rsn_pmksa_cache * pmksa,struct rsn_pmksa_cache_entry * entry,enum pmksa_free_reason reason)47 static void pmksa_cache_free_entry(struct rsn_pmksa_cache *pmksa,
48 				   struct rsn_pmksa_cache_entry *entry,
49 				   enum pmksa_free_reason reason)
50 {
51 	if (pmksa->sm)
52 		wpa_sm_remove_pmkid(pmksa->sm, entry->network_ctx, entry->aa,
53 				    entry->pmkid,
54 				    entry->fils_cache_id_set ?
55 				    entry->fils_cache_id : NULL);
56 	pmksa->pmksa_count--;
57 	if (pmksa->free_cb)
58 		pmksa->free_cb(entry, pmksa->ctx, reason);
59 	_pmksa_cache_free_entry(entry);
60 }
61 
62 
pmksa_cache_remove(struct rsn_pmksa_cache * pmksa,struct rsn_pmksa_cache_entry * entry)63 void pmksa_cache_remove(struct rsn_pmksa_cache *pmksa,
64 			struct rsn_pmksa_cache_entry *entry)
65 {
66 	struct rsn_pmksa_cache_entry *e;
67 
68 	e = pmksa->pmksa;
69 	while (e) {
70 		if (e == entry) {
71 			pmksa->pmksa = entry->next;
72 			break;
73 		}
74 		if (e->next == entry) {
75 			e->next = entry->next;
76 			break;
77 		}
78 	}
79 
80 	if (!e) {
81 		wpa_printf(MSG_DEBUG,
82 			   "RSN: Could not remove PMKSA cache entry %p since it is not in the list",
83 			   entry);
84 		return;
85 	}
86 
87 	pmksa_cache_free_entry(pmksa, entry, PMKSA_FREE);
88 }
89 
90 
pmksa_cache_expire(void * eloop_ctx,void * timeout_ctx)91 static void pmksa_cache_expire(void *eloop_ctx, void *timeout_ctx)
92 {
93 	struct rsn_pmksa_cache *pmksa = eloop_ctx;
94 	struct os_reltime now;
95 	struct rsn_pmksa_cache_entry *prev = NULL, *tmp;
96 	struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
97 
98 	os_get_reltime(&now);
99 	while (entry && entry->expiration <= now.sec) {
100 		if (wpa_key_mgmt_sae(entry->akmp) && pmksa->is_current_cb &&
101 		    pmksa->is_current_cb(entry, pmksa->ctx)) {
102 			/* Do not expire the currently used PMKSA entry for SAE
103 			 * since there is no convenient mechanism for
104 			 * reauthenticating during an association with SAE. The
105 			 * expired entry will be removed after this association
106 			 * has been lost. */
107 			wpa_printf(MSG_DEBUG,
108 				   "RSN: postpone PMKSA cache entry expiration for SAE with "
109 				   MACSTR, MAC2STR(entry->aa));
110 			prev = entry;
111 			entry = entry->next;
112 			continue;
113 		}
114 
115 		wpa_printf(MSG_DEBUG, "RSN: expired PMKSA cache entry for "
116 			   MACSTR, MAC2STR(entry->aa));
117 		if (prev)
118 			prev->next = entry->next;
119 		else
120 			pmksa->pmksa = entry->next;
121 		tmp = entry;
122 		entry = entry->next;
123 		pmksa_cache_free_entry(pmksa, tmp, PMKSA_EXPIRE);
124 	}
125 
126 	pmksa_cache_set_expiration(pmksa);
127 }
128 
129 
pmksa_cache_reauth(void * eloop_ctx,void * timeout_ctx)130 static void pmksa_cache_reauth(void *eloop_ctx, void *timeout_ctx)
131 {
132 	struct rsn_pmksa_cache *pmksa = eloop_ctx;
133 
134 	if (!pmksa->sm)
135 		return;
136 
137 	if (pmksa->sm->driver_bss_selection) {
138 		struct rsn_pmksa_cache_entry *entry;
139 
140 		entry = pmksa->sm->cur_pmksa ?
141 			pmksa->sm->cur_pmksa :
142 			pmksa_cache_get(pmksa, pmksa->sm->bssid, NULL, NULL,
143 					NULL, 0);
144 		if (entry && wpa_key_mgmt_sae(entry->akmp)) {
145 			wpa_printf(MSG_DEBUG,
146 				   "RSN: remove reauth threshold passed PMKSA from the driver for SAE");
147 			entry->sae_reauth_scheduled = true;
148 			wpa_sm_remove_pmkid(pmksa->sm, entry->network_ctx,
149 					    entry->aa, entry->pmkid, NULL);
150 			return;
151 		}
152 	}
153 
154 	pmksa->sm->cur_pmksa = NULL;
155 	eapol_sm_request_reauth(pmksa->sm->eapol);
156 }
157 
158 
pmksa_cache_set_expiration(struct rsn_pmksa_cache * pmksa)159 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa)
160 {
161 	int sec;
162 	struct rsn_pmksa_cache_entry *entry;
163 	struct os_reltime now;
164 
165 	eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
166 	eloop_cancel_timeout(pmksa_cache_reauth, pmksa, NULL);
167 	if (pmksa->pmksa == NULL)
168 		return;
169 	os_get_reltime(&now);
170 	sec = pmksa->pmksa->expiration - now.sec;
171 	if (sec < 0) {
172 		sec = 0;
173 		if (wpa_key_mgmt_sae(pmksa->pmksa->akmp) &&
174 		    pmksa->is_current_cb &&
175 		    pmksa->is_current_cb(pmksa->pmksa, pmksa->ctx)) {
176 			/* Do not continue polling for the current PMKSA entry
177 			 * from SAE to expire every second. Use the expiration
178 			 * time to the following entry, if any, and wait at
179 			 * maximum 10 minutes to check again.
180 			 */
181 			entry = pmksa->pmksa->next;
182 			if (entry) {
183 				sec = entry->expiration - now.sec;
184 				if (sec < 0)
185 					sec = 0;
186 				else if (sec > 600)
187 					sec = 600;
188 			} else {
189 				sec = 600;
190 			}
191 		}
192 	}
193 	eloop_register_timeout(sec + 1, 0, pmksa_cache_expire, pmksa, NULL);
194 
195 	if (!pmksa->sm)
196 		return;
197 
198 	entry = pmksa->sm->cur_pmksa ? pmksa->sm->cur_pmksa :
199 		pmksa_cache_get(pmksa, pmksa->sm->bssid, NULL, NULL, NULL, 0);
200 	if (entry &&
201 	    (!wpa_key_mgmt_sae(entry->akmp) ||
202 	     (pmksa->sm->driver_bss_selection &&
203 	      !entry->sae_reauth_scheduled))) {
204 		sec = pmksa->pmksa->reauth_time - now.sec;
205 		if (sec < 0)
206 			sec = 0;
207 		eloop_register_timeout(sec, 0, pmksa_cache_reauth, pmksa,
208 				       NULL);
209 	}
210 }
211 
212 
213 /**
214  * pmksa_cache_add - Add a PMKSA cache entry
215  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
216  * @pmk: The new pairwise master key
217  * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
218  * @pmkid: Calculated PMKID
219  * @kck: Key confirmation key or %NULL if not yet derived
220  * @kck_len: KCK length in bytes
221  * @aa: Authenticator address
222  * @spa: Supplicant address
223  * @network_ctx: Network configuration context for this PMK
224  * @akmp: WPA_KEY_MGMT_* used in key derivation
225  * @cache_id: Pointer to FILS Cache Identifier or %NULL if not advertised
226  * Returns: Pointer to the added PMKSA cache entry or %NULL on error
227  *
228  * This function create a PMKSA entry for a new PMK and adds it to the PMKSA
229  * cache. If an old entry is already in the cache for the same Authenticator,
230  * this entry will be replaced with the new entry. PMKID will be calculated
231  * based on the PMK and the driver interface is notified of the new PMKID.
232  */
233 struct rsn_pmksa_cache_entry *
pmksa_cache_add(struct rsn_pmksa_cache * pmksa,const u8 * pmk,size_t pmk_len,const u8 * pmkid,const u8 * kck,size_t kck_len,const u8 * aa,const u8 * spa,void * network_ctx,int akmp,const u8 * cache_id)234 pmksa_cache_add(struct rsn_pmksa_cache *pmksa, const u8 *pmk, size_t pmk_len,
235 		const u8 *pmkid, const u8 *kck, size_t kck_len,
236 		const u8 *aa, const u8 *spa, void *network_ctx, int akmp,
237 		const u8 *cache_id)
238 {
239 	struct rsn_pmksa_cache_entry *entry;
240 	struct os_reltime now;
241 	unsigned int pmk_lifetime = 43200;
242 	unsigned int pmk_reauth_threshold = 70;
243 
244 	if (pmk_len > PMK_LEN_MAX)
245 		return NULL;
246 
247 	if (kck_len > WPA_KCK_MAX_LEN)
248 		return NULL;
249 
250 	entry = os_zalloc(sizeof(*entry));
251 	if (entry == NULL)
252 		return NULL;
253 	os_memcpy(entry->pmk, pmk, pmk_len);
254 	entry->pmk_len = pmk_len;
255 	if (pmkid) {
256  		os_memcpy(entry->pmkid, pmkid, PMKID_LEN);
257 	} else if (akmp == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
258 		if (kck && kck_len > 0) {
259 			rsn_pmkid_suite_b_192(kck, kck_len, aa, spa, entry->pmkid);
260 			os_memcpy(entry->kck, kck, kck_len);
261 			entry->kck_len = kck_len;
262 		}
263 	} else if (wpa_key_mgmt_suite_b(akmp)) {
264 		if (kck && kck_len > 0) {
265 			rsn_pmkid_suite_b(kck, kck_len, aa, spa, entry->pmkid);
266 			os_memcpy(entry->kck, kck, kck_len);
267 			entry->kck_len = kck_len;
268 		}
269 	} else {
270  		rsn_pmkid(pmk, pmk_len, aa, spa, entry->pmkid, akmp);
271 	}
272 	os_get_reltime(&now);
273 	if (pmksa->sm) {
274 		pmk_lifetime = pmksa->sm->dot11RSNAConfigPMKLifetime;
275 		pmk_reauth_threshold =
276 			pmksa->sm->dot11RSNAConfigPMKReauthThreshold;
277 	}
278 	entry->expiration = now.sec + pmk_lifetime;
279 	entry->reauth_time = now.sec +
280 		pmk_lifetime * pmk_reauth_threshold / 100;
281 	entry->akmp = akmp;
282 	if (cache_id) {
283 		entry->fils_cache_id_set = 1;
284 		os_memcpy(entry->fils_cache_id, cache_id, FILS_CACHE_ID_LEN);
285 	}
286 	os_memcpy(entry->aa, aa, ETH_ALEN);
287 	os_memcpy(entry->spa, spa, ETH_ALEN);
288 	entry->network_ctx = network_ctx;
289 
290 	return pmksa_cache_add_entry(pmksa, entry);
291 }
292 
293 
294 struct rsn_pmksa_cache_entry *
pmksa_cache_add_entry(struct rsn_pmksa_cache * pmksa,struct rsn_pmksa_cache_entry * entry)295 pmksa_cache_add_entry(struct rsn_pmksa_cache *pmksa,
296 		      struct rsn_pmksa_cache_entry *entry)
297 {
298 	struct rsn_pmksa_cache_entry *pos, *prev;
299 
300 	/* Replace an old entry for the same Authenticator (if found) with the
301 	 * new entry */
302 	pos = pmksa->pmksa;
303 	prev = NULL;
304 	while (pos) {
305 		if (ether_addr_equal(entry->aa, pos->aa) &&
306 		    ether_addr_equal(entry->spa, pos->spa)) {
307 			if (pos->pmk_len == entry->pmk_len &&
308 			    os_memcmp_const(pos->pmk, entry->pmk,
309 					    entry->pmk_len) == 0 &&
310 			    os_memcmp_const(pos->pmkid, entry->pmkid,
311 					    PMKID_LEN) == 0) {
312 				wpa_printf(MSG_DEBUG, "WPA: reusing previous "
313 					   "PMKSA entry");
314 				os_free(entry);
315 				return pos;
316 			}
317 			if (prev == NULL)
318 				pmksa->pmksa = pos->next;
319 			else
320 				prev->next = pos->next;
321 
322 			/*
323 			 * If OKC is used, there may be other PMKSA cache
324 			 * entries based on the same PMK. These needs to be
325 			 * flushed so that a new entry can be created based on
326 			 * the new PMK. Only clear other entries if they have a
327 			 * matching PMK and this PMK has been used successfully
328 			 * with the current AP, i.e., if opportunistic flag has
329 			 * been cleared in wpa_supplicant_key_neg_complete().
330 			 */
331 			wpa_printf(MSG_DEBUG, "RSN: Replace PMKSA entry for "
332 				   "the current AP and any PMKSA cache entry "
333 				   "that was based on the old PMK");
334 			if (!pos->opportunistic)
335 				pmksa_cache_flush(pmksa, entry->network_ctx,
336 						  pos->pmk, pos->pmk_len,
337 						  false);
338 			pmksa_cache_free_entry(pmksa, pos, PMKSA_REPLACE);
339 			break;
340 		}
341 		prev = pos;
342 		pos = pos->next;
343 	}
344 
345 	if (pmksa->pmksa_count >= pmksa_cache_max_entries && pmksa->pmksa) {
346 		/* Remove the oldest entry to make room for the new entry */
347 		pos = pmksa->pmksa;
348 
349 		if (pmksa->sm && pos == pmksa->sm->cur_pmksa) {
350 			/*
351 			 * Never remove the current PMKSA cache entry, since
352 			 * it's in use, and removing it triggers a needless
353 			 * deauthentication.
354 			 */
355 			pos = pos->next;
356 			pmksa->pmksa->next = pos ? pos->next : NULL;
357 		} else
358 			pmksa->pmksa = pos->next;
359 
360 		if (pos) {
361 			wpa_printf(MSG_DEBUG, "RSN: removed the oldest idle "
362 				   "PMKSA cache entry (for " MACSTR ") to "
363 				   "make room for new one",
364 				   MAC2STR(pos->aa));
365 			pmksa_cache_free_entry(pmksa, pos, PMKSA_FREE);
366 		}
367 	}
368 
369 	/* Add the new entry; order by expiration time */
370 	pos = pmksa->pmksa;
371 	prev = NULL;
372 	while (pos) {
373 		if (pos->expiration > entry->expiration)
374 			break;
375 		prev = pos;
376 		pos = pos->next;
377 	}
378 	if (prev == NULL) {
379 		entry->next = pmksa->pmksa;
380 		pmksa->pmksa = entry;
381 		pmksa_cache_set_expiration(pmksa);
382 	} else {
383 		entry->next = prev->next;
384 		prev->next = entry;
385 	}
386 	pmksa->pmksa_count++;
387 	wpa_printf(MSG_DEBUG, "RSN: Added PMKSA cache entry for " MACSTR
388 		   " spa=" MACSTR " network_ctx=%p akmp=0x%x",
389 		   MAC2STR(entry->aa), MAC2STR(entry->spa),
390 		   entry->network_ctx, entry->akmp);
391 
392 	if (!pmksa->sm)
393 		return entry;
394 
395 	if (pmksa->notify_cb)
396 		pmksa->notify_cb(entry, pmksa->ctx);
397 
398 	wpa_sm_add_pmkid(pmksa->sm, entry->network_ctx, entry->aa, entry->pmkid,
399 			 entry->fils_cache_id_set ? entry->fils_cache_id : NULL,
400 			 entry->pmk, entry->pmk_len,
401 			 pmksa->sm->dot11RSNAConfigPMKLifetime,
402 			 pmksa->sm->dot11RSNAConfigPMKReauthThreshold,
403 			 entry->akmp);
404 
405 	return entry;
406 }
407 
408 
409 /**
410  * pmksa_cache_flush - Flush PMKSA cache entries for a specific network
411  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
412  * @network_ctx: Network configuration context or %NULL to flush all entries
413  * @pmk: PMK to match for or %NULL to match all PMKs
414  * @pmk_len: PMK length
415  * @external_only: Flush only PMKSA cache entries configured by external
416  * applications
417  */
pmksa_cache_flush(struct rsn_pmksa_cache * pmksa,void * network_ctx,const u8 * pmk,size_t pmk_len,bool external_only)418 void pmksa_cache_flush(struct rsn_pmksa_cache *pmksa, void *network_ctx,
419 		       const u8 *pmk, size_t pmk_len, bool external_only)
420 {
421 	struct rsn_pmksa_cache_entry *entry, *prev = NULL, *tmp;
422 	int removed = 0;
423 
424 	entry = pmksa->pmksa;
425 	while (entry) {
426 		if ((entry->network_ctx == network_ctx ||
427 		     network_ctx == NULL) &&
428 		    (pmk == NULL ||
429 		     (pmk_len == entry->pmk_len &&
430 		      os_memcmp(pmk, entry->pmk, pmk_len) == 0)) &&
431 		    (!external_only || entry->external)) {
432 			wpa_printf(MSG_DEBUG, "RSN: Flush PMKSA cache entry "
433 				   "for " MACSTR, MAC2STR(entry->aa));
434 			if (prev)
435 				prev->next = entry->next;
436 			else
437 				pmksa->pmksa = entry->next;
438 			tmp = entry;
439 			entry = entry->next;
440 			pmksa_cache_free_entry(pmksa, tmp, PMKSA_FREE);
441 			removed++;
442 		} else {
443 			prev = entry;
444 			entry = entry->next;
445 		}
446 	}
447 	if (removed)
448 		pmksa_cache_set_expiration(pmksa);
449 }
450 
451 
452 /**
453  * pmksa_cache_deinit - Free all entries in PMKSA cache
454  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
455  */
pmksa_cache_deinit(struct rsn_pmksa_cache * pmksa)456 void pmksa_cache_deinit(struct rsn_pmksa_cache *pmksa)
457 {
458 	struct rsn_pmksa_cache_entry *entry, *prev;
459 
460 	if (pmksa == NULL)
461 		return;
462 
463 	entry = pmksa->pmksa;
464 	pmksa->pmksa = NULL;
465 	while (entry) {
466 		prev = entry;
467 		entry = entry->next;
468 		os_free(prev);
469 	}
470 	pmksa_cache_set_expiration(pmksa);
471 	os_free(pmksa);
472 }
473 
474 
475 /**
476  * pmksa_cache_get - Fetch a PMKSA cache entry
477  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
478  * @aa: Authenticator address or %NULL to match any
479  * @pmkid: PMKID or %NULL to match any
480  * @network_ctx: Network context or %NULL to match any
481  * @akmp: Specific AKMP to search for or 0 for any
482  * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
483  */
pmksa_cache_get(struct rsn_pmksa_cache * pmksa,const u8 * aa,const u8 * spa,const u8 * pmkid,const void * network_ctx,int akmp)484 struct rsn_pmksa_cache_entry * pmksa_cache_get(struct rsn_pmksa_cache *pmksa,
485 					       const u8 *aa, const u8 *spa,
486 					       const u8 *pmkid,
487 					       const void *network_ctx,
488 					       int akmp)
489 {
490 	struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
491 	while (entry) {
492 		if ((aa == NULL || ether_addr_equal(entry->aa, aa)) &&
493 		    (!spa || ether_addr_equal(entry->spa, spa)) &&
494 		    (pmkid == NULL ||
495 		     os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0) &&
496 		    (!akmp || akmp == entry->akmp) &&
497 		    (network_ctx == NULL || network_ctx == entry->network_ctx))
498 			return entry;
499 		entry = entry->next;
500 	}
501 	return NULL;
502 }
503 
504 
505 static struct rsn_pmksa_cache_entry *
pmksa_cache_clone_entry(struct rsn_pmksa_cache * pmksa,const struct rsn_pmksa_cache_entry * old_entry,const u8 * aa)506 pmksa_cache_clone_entry(struct rsn_pmksa_cache *pmksa,
507 			const struct rsn_pmksa_cache_entry *old_entry,
508 			const u8 *aa)
509 {
510 	struct rsn_pmksa_cache_entry *new_entry;
511 	os_time_t old_expiration = old_entry->expiration;
512 	os_time_t old_reauth_time = old_entry->reauth_time;
513 	const u8 *pmkid = NULL;
514 
515 	if (!pmksa->sm)
516 		return NULL;
517 
518 	if (wpa_key_mgmt_sae(old_entry->akmp) ||
519 	    wpa_key_mgmt_fils(old_entry->akmp))
520 		pmkid = old_entry->pmkid;
521 	new_entry = pmksa_cache_add(pmksa, old_entry->pmk, old_entry->pmk_len,
522 				    pmkid, old_entry->kck, old_entry->kck_len,
523 				    aa, pmksa->sm->own_addr,
524 				    old_entry->network_ctx, old_entry->akmp,
525 				    old_entry->fils_cache_id_set ?
526 				    old_entry->fils_cache_id : NULL);
527 	if (new_entry == NULL)
528 		return NULL;
529 
530 	/* TODO: reorder entries based on expiration time? */
531 	new_entry->expiration = old_expiration;
532 	new_entry->reauth_time = old_reauth_time;
533 	new_entry->opportunistic = 1;
534 
535 	return new_entry;
536 }
537 
538 
539 /**
540  * pmksa_cache_get_opportunistic - Try to get an opportunistic PMKSA entry
541  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
542  * @network_ctx: Network configuration context
543  * @aa: Authenticator address for the new AP
544  * @akmp: Specific AKMP to search for or 0 for any
545  * Returns: Pointer to a new PMKSA cache entry or %NULL if not available
546  *
547  * Try to create a new PMKSA cache entry opportunistically by guessing that the
548  * new AP is sharing the same PMK as another AP that has the same SSID and has
549  * already an entry in PMKSA cache.
550  */
551 struct rsn_pmksa_cache_entry *
pmksa_cache_get_opportunistic(struct rsn_pmksa_cache * pmksa,void * network_ctx,const u8 * aa,int akmp)552 pmksa_cache_get_opportunistic(struct rsn_pmksa_cache *pmksa, void *network_ctx,
553 			      const u8 *aa, int akmp)
554 {
555 	struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
556 
557 	wpa_printf(MSG_DEBUG, "RSN: Consider " MACSTR " for OKC", MAC2STR(aa));
558 	if (network_ctx == NULL)
559 		return NULL;
560 	while (entry) {
561 		if (entry->network_ctx == network_ctx &&
562 		    (!akmp || entry->akmp == akmp)) {
563 			struct os_reltime now;
564 
565 			if (wpa_key_mgmt_sae(entry->akmp) &&
566 			    os_get_reltime(&now) == 0 &&
567 			    entry->reauth_time < now.sec) {
568 				wpa_printf(MSG_DEBUG,
569 					   "RSN: Do not clone PMKSA cache entry for "
570 					   MACSTR
571 					   " since its reauth threshold has passed",
572 					   MAC2STR(entry->aa));
573 				entry = entry->next;
574 				continue;
575 			}
576 
577 			entry = pmksa_cache_clone_entry(pmksa, entry, aa);
578 			if (entry) {
579 				wpa_printf(MSG_DEBUG, "RSN: added "
580 					   "opportunistic PMKSA cache entry "
581 					   "for " MACSTR, MAC2STR(aa));
582 			}
583 			return entry;
584 		}
585 		entry = entry->next;
586 	}
587 	return NULL;
588 }
589 
590 
591 static struct rsn_pmksa_cache_entry *
pmksa_cache_get_fils_cache_id(struct rsn_pmksa_cache * pmksa,const void * network_ctx,const u8 * cache_id)592 pmksa_cache_get_fils_cache_id(struct rsn_pmksa_cache *pmksa,
593 			      const void *network_ctx, const u8 *cache_id)
594 {
595 	struct rsn_pmksa_cache_entry *entry;
596 
597 	for (entry = pmksa->pmksa; entry; entry = entry->next) {
598 		if (network_ctx == entry->network_ctx &&
599 		    entry->fils_cache_id_set &&
600 		    os_memcmp(cache_id, entry->fils_cache_id,
601 			      FILS_CACHE_ID_LEN) == 0)
602 			return entry;
603 	}
604 
605 	return NULL;
606 }
607 
608 
609 /**
610  * pmksa_cache_get_current - Get the current used PMKSA entry
611  * @sm: Pointer to WPA state machine data from wpa_sm_init()
612  * Returns: Pointer to the current PMKSA cache entry or %NULL if not available
613  */
pmksa_cache_get_current(struct wpa_sm * sm)614 struct rsn_pmksa_cache_entry * pmksa_cache_get_current(struct wpa_sm *sm)
615 {
616 	if (sm == NULL)
617 		return NULL;
618 	return sm->cur_pmksa;
619 }
620 
621 
622 /**
623  * pmksa_cache_clear_current - Clear the current PMKSA entry selection
624  * @sm: Pointer to WPA state machine data from wpa_sm_init()
625  */
pmksa_cache_clear_current(struct wpa_sm * sm)626 void pmksa_cache_clear_current(struct wpa_sm *sm)
627 {
628 	if (sm == NULL)
629 		return;
630 	if (sm->cur_pmksa)
631 		wpa_printf(MSG_DEBUG,
632 			   "RSN: Clear current PMKSA entry selection");
633 	sm->cur_pmksa = NULL;
634 }
635 
636 
637 /**
638  * pmksa_cache_set_current - Set the current PMKSA entry selection
639  * @sm: Pointer to WPA state machine data from wpa_sm_init()
640  * @pmkid: PMKID for selecting PMKSA or %NULL if not used
641  * @bssid: BSSID for PMKSA or %NULL if not used
642  * @network_ctx: Network configuration context
643  * @try_opportunistic: Whether to allow opportunistic PMKSA caching
644  * @fils_cache_id: Pointer to FILS Cache Identifier or %NULL if not used
645  * @associated: Whether the device is associated
646  * Returns: 0 if PMKSA was found or -1 if no matching entry was found
647  */
pmksa_cache_set_current(struct wpa_sm * sm,const u8 * pmkid,const u8 * bssid,void * network_ctx,int try_opportunistic,const u8 * fils_cache_id,int akmp,bool associated)648 int pmksa_cache_set_current(struct wpa_sm *sm, const u8 *pmkid,
649 			    const u8 *bssid, void *network_ctx,
650 			    int try_opportunistic, const u8 *fils_cache_id,
651 			    int akmp, bool associated)
652 {
653 	struct rsn_pmksa_cache *pmksa = sm->pmksa;
654 	wpa_printf(MSG_DEBUG, "RSN: PMKSA cache search - network_ctx=%p "
655 		   "try_opportunistic=%d akmp=0x%x",
656 		   network_ctx, try_opportunistic, akmp);
657 	if (pmkid)
658 		wpa_hexdump(MSG_DEBUG, "RSN: Search for PMKID",
659 			    pmkid, PMKID_LEN);
660 	if (bssid)
661 		wpa_printf(MSG_DEBUG, "RSN: Search for BSSID " MACSTR,
662 			   MAC2STR(bssid));
663 	if (fils_cache_id)
664 		wpa_printf(MSG_DEBUG,
665 			   "RSN: Search for FILS Cache Identifier %02x%02x",
666 			   fils_cache_id[0], fils_cache_id[1]);
667 
668 	sm->cur_pmksa = NULL;
669 	if (pmkid)
670 		sm->cur_pmksa = pmksa_cache_get(pmksa, NULL, sm->own_addr,
671 						pmkid, network_ctx, akmp);
672 	if (sm->cur_pmksa == NULL && bssid)
673 		sm->cur_pmksa = pmksa_cache_get(pmksa, bssid, sm->own_addr,
674 						NULL, network_ctx, akmp);
675 	if (sm->cur_pmksa == NULL && try_opportunistic && bssid)
676 		sm->cur_pmksa = pmksa_cache_get_opportunistic(pmksa,
677 							      network_ctx,
678 							      bssid, akmp);
679 	if (sm->cur_pmksa == NULL && fils_cache_id)
680 		sm->cur_pmksa = pmksa_cache_get_fils_cache_id(pmksa,
681 							      network_ctx,
682 							      fils_cache_id);
683 	if (sm->cur_pmksa) {
684 		struct os_reltime now;
685 
686 		if (wpa_key_mgmt_sae(sm->cur_pmksa->akmp) &&
687 		    os_get_reltime(&now) == 0 &&
688 		    sm->cur_pmksa->reauth_time < now.sec) {
689 			/* Driver-based roaming might have used a PMKSA entry
690 			 * that is already past the reauthentication threshold.
691 			 * Remove the related PMKID from the driver to avoid
692 			 * further uses for this PMKSA, but allow the
693 			 * association to continue since the PMKSA has not yet
694 			 * expired. */
695 			wpa_sm_remove_pmkid(sm, sm->cur_pmksa->network_ctx,
696 					    sm->cur_pmksa->aa,
697 					    sm->cur_pmksa->pmkid, NULL);
698 			if (associated) {
699 				wpa_printf(MSG_DEBUG,
700 					   "RSN: Associated with " MACSTR
701 					   " using reauth threshold passed PMKSA cache entry",
702 					   MAC2STR(sm->cur_pmksa->aa));
703 			} else {
704 				wpa_printf(MSG_DEBUG,
705 					   "RSN: Do not allow PMKSA cache entry for "
706 					   MACSTR
707 					   " to be used for SAE since its reauth threshold has passed",
708 					   MAC2STR(sm->cur_pmksa->aa));
709 				sm->cur_pmksa = NULL;
710 				return -1;
711 			}
712 		}
713 
714 		wpa_hexdump(MSG_DEBUG, "RSN: PMKSA cache entry found - PMKID",
715 			    sm->cur_pmksa->pmkid, PMKID_LEN);
716 		return 0;
717 	}
718 	wpa_printf(MSG_DEBUG, "RSN: No PMKSA cache entry found");
719 	return -1;
720 }
721 
722 
723 /**
724  * pmksa_cache_list - Dump text list of entries in PMKSA cache
725  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
726  * @buf: Buffer for the list
727  * @len: Length of the buffer
728  * Returns: number of bytes written to buffer
729  *
730  * This function is used to generate a text format representation of the
731  * current PMKSA cache contents for the ctrl_iface PMKSA command.
732  */
pmksa_cache_list(struct rsn_pmksa_cache * pmksa,char * buf,size_t len)733 int pmksa_cache_list(struct rsn_pmksa_cache *pmksa, char *buf, size_t len)
734 {
735 	int i, ret;
736 	char *pos = buf;
737 	struct rsn_pmksa_cache_entry *entry;
738 	struct os_reltime now;
739 	int cache_id_used = 0;
740 
741 	for (entry = pmksa->pmksa; entry; entry = entry->next) {
742 		if (entry->fils_cache_id_set) {
743 			cache_id_used = 1;
744 			break;
745 		}
746 	}
747 
748 	os_get_reltime(&now);
749 	ret = os_snprintf(pos, buf + len - pos,
750 			  "Index / AA / PMKID / expiration (in seconds) / "
751 			  "opportunistic%s\n",
752 			  cache_id_used ? " / FILS Cache Identifier" : "");
753 	if (os_snprintf_error(buf + len - pos, ret))
754 		return pos - buf;
755 	pos += ret;
756 	i = 0;
757 	entry = pmksa->pmksa;
758 	while (entry) {
759 		i++;
760 		ret = os_snprintf(pos, buf + len - pos, "%d " MACSTR " ",
761 				  i, MAC2STR(entry->aa));
762 		if (os_snprintf_error(buf + len - pos, ret))
763 			return pos - buf;
764 		pos += ret;
765 		pos += wpa_snprintf_hex(pos, buf + len - pos, entry->pmkid,
766 					PMKID_LEN);
767 		ret = os_snprintf(pos, buf + len - pos, " %d %d",
768 				  (int) (entry->expiration - now.sec),
769 				  entry->opportunistic);
770 		if (os_snprintf_error(buf + len - pos, ret))
771 			return pos - buf;
772 		pos += ret;
773 		if (entry->fils_cache_id_set) {
774 			ret = os_snprintf(pos, buf + len - pos, " %02x%02x",
775 					  entry->fils_cache_id[0],
776 					  entry->fils_cache_id[1]);
777 			if (os_snprintf_error(buf + len - pos, ret))
778 				return pos - buf;
779 			pos += ret;
780 		}
781 		ret = os_snprintf(pos, buf + len - pos, "\n");
782 		if (os_snprintf_error(buf + len - pos, ret))
783 			return pos - buf;
784 		pos += ret;
785 		entry = entry->next;
786 	}
787 	return pos - buf;
788 }
789 
790 
pmksa_cache_head(struct rsn_pmksa_cache * pmksa)791 struct rsn_pmksa_cache_entry * pmksa_cache_head(struct rsn_pmksa_cache *pmksa)
792 {
793 	return pmksa->pmksa;
794 }
795 
796 
797 /**
798  * pmksa_cache_init - Initialize PMKSA cache
799  * @free_cb: Callback function to be called when a PMKSA cache entry is freed
800  * @ctx: Context pointer for free_cb function
801  * @sm: Pointer to WPA state machine data from wpa_sm_init()
802  * Returns: Pointer to PMKSA cache data or %NULL on failure
803  */
804 struct rsn_pmksa_cache *
pmksa_cache_init(void (* free_cb)(struct rsn_pmksa_cache_entry * entry,void * ctx,enum pmksa_free_reason reason),bool (* is_current_cb)(struct rsn_pmksa_cache_entry * entry,void * ctx),void (* notify_cb)(struct rsn_pmksa_cache_entry * entry,void * ctx),void * ctx,struct wpa_sm * sm)805 pmksa_cache_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
806 				 void *ctx, enum pmksa_free_reason reason),
807 		 bool (*is_current_cb)(struct rsn_pmksa_cache_entry *entry,
808 				       void *ctx),
809 		 void (*notify_cb)(struct rsn_pmksa_cache_entry *entry,
810 				   void *ctx),
811 		 void *ctx, struct wpa_sm *sm)
812 {
813 	struct rsn_pmksa_cache *pmksa;
814 
815 	pmksa = os_zalloc(sizeof(*pmksa));
816 	if (pmksa) {
817 		pmksa->free_cb = free_cb;
818 		pmksa->is_current_cb = is_current_cb;
819 		pmksa->notify_cb = notify_cb;
820 		pmksa->ctx = ctx;
821 		pmksa->sm = sm;
822 	}
823 
824 	return pmksa;
825 }
826 
827 
pmksa_cache_reconfig(struct rsn_pmksa_cache * pmksa)828 void pmksa_cache_reconfig(struct rsn_pmksa_cache *pmksa)
829 {
830 	struct rsn_pmksa_cache_entry *entry;
831 	struct os_reltime now;
832 
833 	if (!pmksa || !pmksa->pmksa)
834 		return;
835 
836 	os_get_reltime(&now);
837 	for (entry = pmksa->pmksa; entry; entry = entry->next) {
838 		u32 life_time;
839 		u8 reauth_threshold;
840 
841 		if (entry->expiration - now.sec < 1 ||
842 		    entry->reauth_time - now.sec < 1)
843 			continue;
844 
845 		life_time = entry->expiration - now.sec;
846 		reauth_threshold = (entry->reauth_time - now.sec) * 100 /
847 			life_time;
848 		if (!reauth_threshold)
849 			continue;
850 
851 		wpa_sm_add_pmkid(pmksa->sm, entry->network_ctx, entry->aa,
852 				 entry->pmkid,
853 				 entry->fils_cache_id_set ?
854 				 entry->fils_cache_id : NULL,
855 				 entry->pmk, entry->pmk_len, life_time,
856 				 reauth_threshold, entry->akmp);
857 	}
858 }
859 
860 #else /* IEEE8021X_EAPOL */
861 
862 struct rsn_pmksa_cache *
pmksa_cache_init(void (* free_cb)(struct rsn_pmksa_cache_entry * entry,void * ctx,enum pmksa_free_reason reason),bool (* is_current_cb)(struct rsn_pmksa_cache_entry * entry,void * ctx),void (* notify_cb)(struct rsn_pmksa_cache_entry * entry,void * ctx),void * ctx,struct wpa_sm * sm)863 pmksa_cache_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
864 				 void *ctx, enum pmksa_free_reason reason),
865 		 bool (*is_current_cb)(struct rsn_pmksa_cache_entry *entry,
866 				       void *ctx),
867 		 void (*notify_cb)(struct rsn_pmksa_cache_entry *entry,
868 				   void *ctx),
869 		 void *ctx, struct wpa_sm *sm)
870 {
871 	return (void *) -1;
872 }
873 
874 
pmksa_cache_deinit(struct rsn_pmksa_cache * pmksa)875 void pmksa_cache_deinit(struct rsn_pmksa_cache *pmksa)
876 {
877 }
878 
879 
880 struct rsn_pmksa_cache_entry *
pmksa_cache_get(struct rsn_pmksa_cache * pmksa,const u8 * aa,const u8 * spa,const u8 * pmkid,const void * network_ctx,int akmp)881 pmksa_cache_get(struct rsn_pmksa_cache *pmksa, const u8 *aa, const u8 *spa,
882 		const u8 *pmkid, const void *network_ctx, int akmp)
883 {
884 	return NULL;
885 }
886 
887 
888 struct rsn_pmksa_cache_entry *
pmksa_cache_get_current(struct wpa_sm * sm)889 pmksa_cache_get_current(struct wpa_sm *sm)
890 {
891 	return NULL;
892 }
893 
894 
pmksa_cache_list(struct rsn_pmksa_cache * pmksa,char * buf,size_t len)895 int pmksa_cache_list(struct rsn_pmksa_cache *pmksa, char *buf, size_t len)
896 {
897 	return -1;
898 }
899 
900 
901 struct rsn_pmksa_cache_entry *
pmksa_cache_head(struct rsn_pmksa_cache * pmksa)902 pmksa_cache_head(struct rsn_pmksa_cache *pmksa)
903 {
904 	return NULL;
905 }
906 
907 
908 struct rsn_pmksa_cache_entry *
pmksa_cache_add_entry(struct rsn_pmksa_cache * pmksa,struct rsn_pmksa_cache_entry * entry)909 pmksa_cache_add_entry(struct rsn_pmksa_cache *pmksa,
910 		      struct rsn_pmksa_cache_entry *entry)
911 {
912 	return NULL;
913 }
914 
915 
916 struct rsn_pmksa_cache_entry *
pmksa_cache_add(struct rsn_pmksa_cache * pmksa,const u8 * pmk,size_t pmk_len,const u8 * pmkid,const u8 * kck,size_t kck_len,const u8 * aa,const u8 * spa,void * network_ctx,int akmp,const u8 * cache_id)917 pmksa_cache_add(struct rsn_pmksa_cache *pmksa, const u8 *pmk, size_t pmk_len,
918 		const u8 *pmkid, const u8 *kck, size_t kck_len,
919 		const u8 *aa, const u8 *spa, void *network_ctx, int akmp,
920 		const u8 *cache_id)
921 {
922 	return NULL;
923 }
924 
925 
pmksa_cache_clear_current(struct wpa_sm * sm)926 void pmksa_cache_clear_current(struct wpa_sm *sm)
927 {
928 }
929 
930 
pmksa_cache_set_current(struct wpa_sm * sm,const u8 * pmkid,const u8 * bssid,void * network_ctx,int try_opportunistic,const u8 * fils_cache_id,int akmp,bool associated)931 int pmksa_cache_set_current(struct wpa_sm *sm, const u8 *pmkid, const u8 *bssid,
932 			    void *network_ctx, int try_opportunistic,
933 			    const u8 *fils_cache_id, int akmp, bool associated)
934 {
935 	return -1;
936 }
937 
938 
pmksa_cache_flush(struct rsn_pmksa_cache * pmksa,void * network_ctx,const u8 * pmk,size_t pmk_len,bool external_only)939 void pmksa_cache_flush(struct rsn_pmksa_cache *pmksa, void *network_ctx,
940 		       const u8 *pmk, size_t pmk_len, bool external_only)
941 {
942 }
943 
944 
pmksa_cache_remove(struct rsn_pmksa_cache * pmksa,struct rsn_pmksa_cache_entry * entry)945 void pmksa_cache_remove(struct rsn_pmksa_cache *pmksa,
946 			struct rsn_pmksa_cache_entry *entry)
947 {
948 }
949 
950 
pmksa_cache_reconfig(struct rsn_pmksa_cache * pmksa)951 void pmksa_cache_reconfig(struct rsn_pmksa_cache *pmksa)
952 {
953 }
954 
955 #endif /* IEEE8021X_EAPOL */
956