• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * WPA Supplicant - RSN PMKSA cache
3  * Copyright (c) 2004-2009, 2011-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 "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 *ctx;
34 };
35 
36 
37 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa);
38 
39 
_pmksa_cache_free_entry(struct rsn_pmksa_cache_entry * entry)40 static void _pmksa_cache_free_entry(struct rsn_pmksa_cache_entry *entry)
41 {
42 	bin_clear_free(entry, sizeof(*entry));
43 }
44 
45 
pmksa_cache_free_entry(struct rsn_pmksa_cache * pmksa,struct rsn_pmksa_cache_entry * entry,enum pmksa_free_reason reason)46 static void pmksa_cache_free_entry(struct rsn_pmksa_cache *pmksa,
47 				   struct rsn_pmksa_cache_entry *entry,
48 				   enum pmksa_free_reason reason)
49 {
50 	wpa_sm_remove_pmkid(pmksa->sm, entry->network_ctx, entry->aa,
51 			    entry->pmkid,
52 			    entry->fils_cache_id_set ? entry->fils_cache_id :
53 			    NULL);
54 	pmksa->pmksa_count--;
55 	pmksa->free_cb(entry, pmksa->ctx, reason);
56 	_pmksa_cache_free_entry(entry);
57 }
58 
59 
pmksa_cache_expire(void * eloop_ctx,void * timeout_ctx)60 static void pmksa_cache_expire(void *eloop_ctx, void *timeout_ctx)
61 {
62 	struct rsn_pmksa_cache *pmksa = eloop_ctx;
63 	struct os_reltime now;
64 	struct rsn_pmksa_cache_entry *prev = NULL, *tmp;
65 	struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
66 
67 	os_get_reltime(&now);
68 	while (entry && entry->expiration <= now.sec) {
69 		if (wpa_key_mgmt_sae(entry->akmp) &&
70 		    pmksa->is_current_cb(entry, pmksa->ctx)) {
71 			/* Do not expire the currently used PMKSA entry for SAE
72 			 * since there is no convenient mechanism for
73 			 * reauthenticating during an association with SAE. The
74 			 * expired entry will be removed after this association
75 			 * has been lost. */
76 			wpa_printf(MSG_DEBUG,
77 				   "RSN: postpone PMKSA cache entry expiration for SAE with "
78 				   MACSTR, MAC2STR(entry->aa));
79 			prev = entry;
80 			entry = entry->next;
81 			continue;
82 		}
83 
84 		wpa_printf(MSG_DEBUG, "RSN: expired PMKSA cache entry for "
85 			   MACSTR, MAC2STR(entry->aa));
86 		if (prev)
87 			prev->next = entry->next;
88 		else
89 			pmksa->pmksa = entry->next;
90 		tmp = entry;
91 		entry = entry->next;
92 		pmksa_cache_free_entry(pmksa, tmp, PMKSA_EXPIRE);
93 	}
94 
95 	pmksa_cache_set_expiration(pmksa);
96 }
97 
98 
pmksa_cache_reauth(void * eloop_ctx,void * timeout_ctx)99 static void pmksa_cache_reauth(void *eloop_ctx, void *timeout_ctx)
100 {
101 	struct rsn_pmksa_cache *pmksa = eloop_ctx;
102 	pmksa->sm->cur_pmksa = NULL;
103 	eapol_sm_request_reauth(pmksa->sm->eapol);
104 }
105 
106 
pmksa_cache_set_expiration(struct rsn_pmksa_cache * pmksa)107 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa)
108 {
109 	int sec;
110 	struct rsn_pmksa_cache_entry *entry;
111 	struct os_reltime now;
112 
113 	eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
114 	eloop_cancel_timeout(pmksa_cache_reauth, pmksa, NULL);
115 	if (pmksa->pmksa == NULL)
116 		return;
117 	os_get_reltime(&now);
118 	sec = pmksa->pmksa->expiration - now.sec;
119 	if (sec < 0) {
120 		sec = 0;
121 		if (wpa_key_mgmt_sae(pmksa->pmksa->akmp) &&
122 		    pmksa->is_current_cb(pmksa->pmksa, pmksa->ctx)) {
123 			/* Do not continue polling for the current PMKSA entry
124 			 * from SAE to expire every second. Use the expiration
125 			 * time to the following entry, if any, and wait at
126 			 * maximum 10 minutes to check again.
127 			 */
128 			entry = pmksa->pmksa->next;
129 			if (entry) {
130 				sec = entry->expiration - now.sec;
131 				if (sec < 0)
132 					sec = 0;
133 				else if (sec > 600)
134 					sec = 600;
135 			} else {
136 				sec = 600;
137 			}
138 		}
139 	}
140 	eloop_register_timeout(sec + 1, 0, pmksa_cache_expire, pmksa, NULL);
141 
142 	entry = pmksa->sm->cur_pmksa ? pmksa->sm->cur_pmksa :
143 		pmksa_cache_get(pmksa, pmksa->sm->bssid, NULL, NULL, 0);
144 	if (entry && !wpa_key_mgmt_sae(entry->akmp)) {
145 		sec = pmksa->pmksa->reauth_time - now.sec;
146 		if (sec < 0)
147 			sec = 0;
148 		eloop_register_timeout(sec, 0, pmksa_cache_reauth, pmksa,
149 				       NULL);
150 	}
151 }
152 
153 
154 /**
155  * pmksa_cache_add - Add a PMKSA cache entry
156  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
157  * @pmk: The new pairwise master key
158  * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
159  * @pmkid: Calculated PMKID
160  * @kck: Key confirmation key or %NULL if not yet derived
161  * @kck_len: KCK length in bytes
162  * @aa: Authenticator address
163  * @spa: Supplicant address
164  * @network_ctx: Network configuration context for this PMK
165  * @akmp: WPA_KEY_MGMT_* used in key derivation
166  * @cache_id: Pointer to FILS Cache Identifier or %NULL if not advertised
167  * Returns: Pointer to the added PMKSA cache entry or %NULL on error
168  *
169  * This function create a PMKSA entry for a new PMK and adds it to the PMKSA
170  * cache. If an old entry is already in the cache for the same Authenticator,
171  * this entry will be replaced with the new entry. PMKID will be calculated
172  * based on the PMK and the driver interface is notified of the new PMKID.
173  */
174 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)175 pmksa_cache_add(struct rsn_pmksa_cache *pmksa, const u8 *pmk, size_t pmk_len,
176 		const u8 *pmkid, const u8 *kck, size_t kck_len,
177 		const u8 *aa, const u8 *spa, void *network_ctx, int akmp,
178 		const u8 *cache_id)
179 {
180 	struct rsn_pmksa_cache_entry *entry;
181 	struct os_reltime now;
182 
183 	if (pmk_len > PMK_LEN_MAX)
184 		return NULL;
185 
186 	if (wpa_key_mgmt_suite_b(akmp) && !kck)
187 		return NULL;
188 
189 	entry = os_zalloc(sizeof(*entry));
190 	if (entry == NULL)
191 		return NULL;
192 	os_memcpy(entry->pmk, pmk, pmk_len);
193 	entry->pmk_len = pmk_len;
194 	if (pmkid)
195 		os_memcpy(entry->pmkid, pmkid, PMKID_LEN);
196 	else if (akmp == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
197 		rsn_pmkid_suite_b_192(kck, kck_len, aa, spa, entry->pmkid);
198 	else if (wpa_key_mgmt_suite_b(akmp))
199 		rsn_pmkid_suite_b(kck, kck_len, aa, spa, entry->pmkid);
200 	else
201 		rsn_pmkid(pmk, pmk_len, aa, spa, entry->pmkid, akmp);
202 	os_get_reltime(&now);
203 	entry->expiration = now.sec + pmksa->sm->dot11RSNAConfigPMKLifetime;
204 	entry->reauth_time = now.sec + pmksa->sm->dot11RSNAConfigPMKLifetime *
205 		pmksa->sm->dot11RSNAConfigPMKReauthThreshold / 100;
206 	entry->akmp = akmp;
207 	if (cache_id) {
208 		entry->fils_cache_id_set = 1;
209 		os_memcpy(entry->fils_cache_id, cache_id, FILS_CACHE_ID_LEN);
210 	}
211 	os_memcpy(entry->aa, aa, ETH_ALEN);
212 	entry->network_ctx = network_ctx;
213 
214 	return pmksa_cache_add_entry(pmksa, entry);
215 }
216 
217 
218 struct rsn_pmksa_cache_entry *
pmksa_cache_add_entry(struct rsn_pmksa_cache * pmksa,struct rsn_pmksa_cache_entry * entry)219 pmksa_cache_add_entry(struct rsn_pmksa_cache *pmksa,
220 		      struct rsn_pmksa_cache_entry *entry)
221 {
222 	struct rsn_pmksa_cache_entry *pos, *prev;
223 
224 	/* Replace an old entry for the same Authenticator (if found) with the
225 	 * new entry */
226 	pos = pmksa->pmksa;
227 	prev = NULL;
228 	while (pos) {
229 		if (os_memcmp(entry->aa, pos->aa, ETH_ALEN) == 0) {
230 			if (pos->pmk_len == entry->pmk_len &&
231 			    os_memcmp_const(pos->pmk, entry->pmk,
232 					    entry->pmk_len) == 0 &&
233 			    os_memcmp_const(pos->pmkid, entry->pmkid,
234 					    PMKID_LEN) == 0) {
235 				wpa_printf(MSG_DEBUG, "WPA: reusing previous "
236 					   "PMKSA entry");
237 				os_free(entry);
238 				return pos;
239 			}
240 			if (prev == NULL)
241 				pmksa->pmksa = pos->next;
242 			else
243 				prev->next = pos->next;
244 
245 			/*
246 			 * If OKC is used, there may be other PMKSA cache
247 			 * entries based on the same PMK. These needs to be
248 			 * flushed so that a new entry can be created based on
249 			 * the new PMK. Only clear other entries if they have a
250 			 * matching PMK and this PMK has been used successfully
251 			 * with the current AP, i.e., if opportunistic flag has
252 			 * been cleared in wpa_supplicant_key_neg_complete().
253 			 */
254 			wpa_printf(MSG_DEBUG, "RSN: Replace PMKSA entry for "
255 				   "the current AP and any PMKSA cache entry "
256 				   "that was based on the old PMK");
257 			if (!pos->opportunistic)
258 				pmksa_cache_flush(pmksa, entry->network_ctx,
259 						  pos->pmk, pos->pmk_len,
260 						  false);
261 			pmksa_cache_free_entry(pmksa, pos, PMKSA_REPLACE);
262 			break;
263 		}
264 		prev = pos;
265 		pos = pos->next;
266 	}
267 
268 	if (pmksa->pmksa_count >= pmksa_cache_max_entries && pmksa->pmksa) {
269 		/* Remove the oldest entry to make room for the new entry */
270 		pos = pmksa->pmksa;
271 
272 		if (pos == pmksa->sm->cur_pmksa) {
273 			/*
274 			 * Never remove the current PMKSA cache entry, since
275 			 * it's in use, and removing it triggers a needless
276 			 * deauthentication.
277 			 */
278 			pos = pos->next;
279 			pmksa->pmksa->next = pos ? pos->next : NULL;
280 		} else
281 			pmksa->pmksa = pos->next;
282 
283 		if (pos) {
284 			wpa_printf(MSG_DEBUG, "RSN: removed the oldest idle "
285 				   "PMKSA cache entry (for " MACSTR ") to "
286 				   "make room for new one",
287 				   MAC2STR(pos->aa));
288 			pmksa_cache_free_entry(pmksa, pos, PMKSA_FREE);
289 		}
290 	}
291 
292 	/* Add the new entry; order by expiration time */
293 	pos = pmksa->pmksa;
294 	prev = NULL;
295 	while (pos) {
296 		if (pos->expiration > entry->expiration)
297 			break;
298 		prev = pos;
299 		pos = pos->next;
300 	}
301 	if (prev == NULL) {
302 		entry->next = pmksa->pmksa;
303 		pmksa->pmksa = entry;
304 		pmksa_cache_set_expiration(pmksa);
305 	} else {
306 		entry->next = prev->next;
307 		prev->next = entry;
308 	}
309 	pmksa->pmksa_count++;
310 	wpa_printf(MSG_DEBUG, "RSN: Added PMKSA cache entry for " MACSTR
311 		   " network_ctx=%p akmp=0x%x", MAC2STR(entry->aa),
312 		   entry->network_ctx, entry->akmp);
313 	wpas_notify_pmk_cache_added((struct wpa_supplicant *)pmksa->sm->ctx->ctx, entry);
314 	wpa_sm_add_pmkid(pmksa->sm, entry->network_ctx, entry->aa, entry->pmkid,
315 			 entry->fils_cache_id_set ? entry->fils_cache_id : NULL,
316 			 entry->pmk, entry->pmk_len,
317 			 pmksa->sm->dot11RSNAConfigPMKLifetime,
318 			 pmksa->sm->dot11RSNAConfigPMKReauthThreshold,
319 			 entry->akmp);
320 
321 	return entry;
322 }
323 
324 
325 /**
326  * pmksa_cache_flush - Flush PMKSA cache entries for a specific network
327  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
328  * @network_ctx: Network configuration context or %NULL to flush all entries
329  * @pmk: PMK to match for or %NULL to match all PMKs
330  * @pmk_len: PMK length
331  * @external_only: Flush only PMKSA cache entries configured by external
332  * applications
333  */
pmksa_cache_flush(struct rsn_pmksa_cache * pmksa,void * network_ctx,const u8 * pmk,size_t pmk_len,bool external_only)334 void pmksa_cache_flush(struct rsn_pmksa_cache *pmksa, void *network_ctx,
335 		       const u8 *pmk, size_t pmk_len, bool external_only)
336 {
337 	struct rsn_pmksa_cache_entry *entry, *prev = NULL, *tmp;
338 	int removed = 0;
339 
340 	entry = pmksa->pmksa;
341 	while (entry) {
342 		if ((entry->network_ctx == network_ctx ||
343 		     network_ctx == NULL) &&
344 		    (pmk == NULL ||
345 		     (pmk_len == entry->pmk_len &&
346 		      os_memcmp(pmk, entry->pmk, pmk_len) == 0)) &&
347 		    (!external_only || entry->external)) {
348 			wpa_printf(MSG_DEBUG, "RSN: Flush PMKSA cache entry "
349 				   "for " MACSTR, MAC2STR(entry->aa));
350 			if (prev)
351 				prev->next = entry->next;
352 			else
353 				pmksa->pmksa = entry->next;
354 			tmp = entry;
355 			entry = entry->next;
356 			pmksa_cache_free_entry(pmksa, tmp, PMKSA_FREE);
357 			removed++;
358 		} else {
359 			prev = entry;
360 			entry = entry->next;
361 		}
362 	}
363 	if (removed)
364 		pmksa_cache_set_expiration(pmksa);
365 }
366 
367 
368 /**
369  * pmksa_cache_deinit - Free all entries in PMKSA cache
370  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
371  */
pmksa_cache_deinit(struct rsn_pmksa_cache * pmksa)372 void pmksa_cache_deinit(struct rsn_pmksa_cache *pmksa)
373 {
374 	struct rsn_pmksa_cache_entry *entry, *prev;
375 
376 	if (pmksa == NULL)
377 		return;
378 
379 	entry = pmksa->pmksa;
380 	pmksa->pmksa = NULL;
381 	while (entry) {
382 		prev = entry;
383 		entry = entry->next;
384 		os_free(prev);
385 	}
386 	pmksa_cache_set_expiration(pmksa);
387 	os_free(pmksa);
388 }
389 
390 
391 /**
392  * pmksa_cache_get - Fetch a PMKSA cache entry
393  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
394  * @aa: Authenticator address or %NULL to match any
395  * @pmkid: PMKID or %NULL to match any
396  * @network_ctx: Network context or %NULL to match any
397  * @akmp: Specific AKMP to search for or 0 for any
398  * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
399  */
pmksa_cache_get(struct rsn_pmksa_cache * pmksa,const u8 * aa,const u8 * pmkid,const void * network_ctx,int akmp)400 struct rsn_pmksa_cache_entry * pmksa_cache_get(struct rsn_pmksa_cache *pmksa,
401 					       const u8 *aa, const u8 *pmkid,
402 					       const void *network_ctx,
403 					       int akmp)
404 {
405 	struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
406 	while (entry) {
407 		if ((aa == NULL || os_memcmp(entry->aa, aa, ETH_ALEN) == 0) &&
408 		    (pmkid == NULL ||
409 		     os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0) &&
410 		    (!akmp || akmp == entry->akmp) &&
411 		    (network_ctx == NULL || network_ctx == entry->network_ctx))
412 			return entry;
413 		entry = entry->next;
414 	}
415 	return NULL;
416 }
417 
418 
419 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)420 pmksa_cache_clone_entry(struct rsn_pmksa_cache *pmksa,
421 			const struct rsn_pmksa_cache_entry *old_entry,
422 			const u8 *aa)
423 {
424 	struct rsn_pmksa_cache_entry *new_entry;
425 	os_time_t old_expiration = old_entry->expiration;
426 	os_time_t old_reauth_time = old_entry->reauth_time;
427 	const u8 *pmkid = NULL;
428 
429 	if (wpa_key_mgmt_sae(old_entry->akmp) ||
430 	    wpa_key_mgmt_fils(old_entry->akmp))
431 		pmkid = old_entry->pmkid;
432 	new_entry = pmksa_cache_add(pmksa, old_entry->pmk, old_entry->pmk_len,
433 				    pmkid, NULL, 0,
434 				    aa, pmksa->sm->own_addr,
435 				    old_entry->network_ctx, old_entry->akmp,
436 				    old_entry->fils_cache_id_set ?
437 				    old_entry->fils_cache_id : NULL);
438 	if (new_entry == NULL)
439 		return NULL;
440 
441 	/* TODO: reorder entries based on expiration time? */
442 	new_entry->expiration = old_expiration;
443 	new_entry->reauth_time = old_reauth_time;
444 	new_entry->opportunistic = 1;
445 
446 	return new_entry;
447 }
448 
449 
450 /**
451  * pmksa_cache_get_opportunistic - Try to get an opportunistic PMKSA entry
452  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
453  * @network_ctx: Network configuration context
454  * @aa: Authenticator address for the new AP
455  * @akmp: Specific AKMP to search for or 0 for any
456  * Returns: Pointer to a new PMKSA cache entry or %NULL if not available
457  *
458  * Try to create a new PMKSA cache entry opportunistically by guessing that the
459  * new AP is sharing the same PMK as another AP that has the same SSID and has
460  * already an entry in PMKSA cache.
461  */
462 struct rsn_pmksa_cache_entry *
pmksa_cache_get_opportunistic(struct rsn_pmksa_cache * pmksa,void * network_ctx,const u8 * aa,int akmp)463 pmksa_cache_get_opportunistic(struct rsn_pmksa_cache *pmksa, void *network_ctx,
464 			      const u8 *aa, int akmp)
465 {
466 	struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
467 
468 	wpa_printf(MSG_DEBUG, "RSN: Consider " MACSTR " for OKC", MAC2STR(aa));
469 	if (network_ctx == NULL)
470 		return NULL;
471 	while (entry) {
472 		if (entry->network_ctx == network_ctx &&
473 		    (!akmp || entry->akmp == akmp)) {
474 			struct os_reltime now;
475 
476 			if (wpa_key_mgmt_sae(entry->akmp) &&
477 			    os_get_reltime(&now) == 0 &&
478 			    entry->reauth_time < now.sec) {
479 				wpa_printf(MSG_DEBUG,
480 					   "RSN: Do not clone PMKSA cache entry for "
481 					   MACSTR
482 					   " since its reauth threshold has passed",
483 					   MAC2STR(entry->aa));
484 				entry = entry->next;
485 				continue;
486 			}
487 
488 			entry = pmksa_cache_clone_entry(pmksa, entry, aa);
489 			if (entry) {
490 				wpa_printf(MSG_DEBUG, "RSN: added "
491 					   "opportunistic PMKSA cache entry "
492 					   "for " MACSTR, MAC2STR(aa));
493 			}
494 			return entry;
495 		}
496 		entry = entry->next;
497 	}
498 	return NULL;
499 }
500 
501 
502 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)503 pmksa_cache_get_fils_cache_id(struct rsn_pmksa_cache *pmksa,
504 			      const void *network_ctx, const u8 *cache_id)
505 {
506 	struct rsn_pmksa_cache_entry *entry;
507 
508 	for (entry = pmksa->pmksa; entry; entry = entry->next) {
509 		if (network_ctx == entry->network_ctx &&
510 		    entry->fils_cache_id_set &&
511 		    os_memcmp(cache_id, entry->fils_cache_id,
512 			      FILS_CACHE_ID_LEN) == 0)
513 			return entry;
514 	}
515 
516 	return NULL;
517 }
518 
519 
520 /**
521  * pmksa_cache_get_current - Get the current used PMKSA entry
522  * @sm: Pointer to WPA state machine data from wpa_sm_init()
523  * Returns: Pointer to the current PMKSA cache entry or %NULL if not available
524  */
pmksa_cache_get_current(struct wpa_sm * sm)525 struct rsn_pmksa_cache_entry * pmksa_cache_get_current(struct wpa_sm *sm)
526 {
527 	if (sm == NULL)
528 		return NULL;
529 	return sm->cur_pmksa;
530 }
531 
532 
533 /**
534  * pmksa_cache_clear_current - Clear the current PMKSA entry selection
535  * @sm: Pointer to WPA state machine data from wpa_sm_init()
536  */
pmksa_cache_clear_current(struct wpa_sm * sm)537 void pmksa_cache_clear_current(struct wpa_sm *sm)
538 {
539 	if (sm == NULL)
540 		return;
541 	if (sm->cur_pmksa)
542 		wpa_printf(MSG_DEBUG,
543 			   "RSN: Clear current PMKSA entry selection");
544 	sm->cur_pmksa = NULL;
545 }
546 
547 
548 /**
549  * pmksa_cache_set_current - Set the current PMKSA entry selection
550  * @sm: Pointer to WPA state machine data from wpa_sm_init()
551  * @pmkid: PMKID for selecting PMKSA or %NULL if not used
552  * @bssid: BSSID for PMKSA or %NULL if not used
553  * @network_ctx: Network configuration context
554  * @try_opportunistic: Whether to allow opportunistic PMKSA caching
555  * @fils_cache_id: Pointer to FILS Cache Identifier or %NULL if not used
556  * Returns: 0 if PMKSA was found or -1 if no matching entry was found
557  */
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)558 int pmksa_cache_set_current(struct wpa_sm *sm, const u8 *pmkid,
559 			    const u8 *bssid, void *network_ctx,
560 			    int try_opportunistic, const u8 *fils_cache_id,
561 			    int akmp)
562 {
563 	struct rsn_pmksa_cache *pmksa = sm->pmksa;
564 	wpa_printf(MSG_DEBUG, "RSN: PMKSA cache search - network_ctx=%p "
565 		   "try_opportunistic=%d akmp=0x%x",
566 		   network_ctx, try_opportunistic, akmp);
567 	if (pmkid)
568 		wpa_hexdump(MSG_DEBUG, "RSN: Search for PMKID",
569 			    pmkid, PMKID_LEN);
570 	if (bssid)
571 		wpa_printf(MSG_DEBUG, "RSN: Search for BSSID " MACSTR,
572 			   MAC2STR(bssid));
573 	if (fils_cache_id)
574 		wpa_printf(MSG_DEBUG,
575 			   "RSN: Search for FILS Cache Identifier %02x%02x",
576 			   fils_cache_id[0], fils_cache_id[1]);
577 
578 	sm->cur_pmksa = NULL;
579 	if (pmkid)
580 		sm->cur_pmksa = pmksa_cache_get(pmksa, NULL, pmkid,
581 						network_ctx, akmp);
582 	if (sm->cur_pmksa == NULL && bssid)
583 		sm->cur_pmksa = pmksa_cache_get(pmksa, bssid, NULL,
584 						network_ctx, akmp);
585 	if (sm->cur_pmksa == NULL && try_opportunistic && bssid)
586 		sm->cur_pmksa = pmksa_cache_get_opportunistic(pmksa,
587 							      network_ctx,
588 							      bssid, akmp);
589 	if (sm->cur_pmksa == NULL && fils_cache_id)
590 		sm->cur_pmksa = pmksa_cache_get_fils_cache_id(pmksa,
591 							      network_ctx,
592 							      fils_cache_id);
593 	if (sm->cur_pmksa) {
594 		struct os_reltime now;
595 
596 		if (wpa_key_mgmt_sae(sm->cur_pmksa->akmp) &&
597 		    os_get_reltime(&now) == 0 &&
598 		    sm->cur_pmksa->reauth_time < now.sec) {
599 			wpa_printf(MSG_DEBUG,
600 				   "RSN: Do not allow PMKSA cache entry for "
601 				   MACSTR
602 				   " to be used for SAE since its reauth threshold has passed",
603 				   MAC2STR(sm->cur_pmksa->aa));
604 			sm->cur_pmksa = NULL;
605 			return -1;
606 		}
607 
608 		wpa_hexdump(MSG_DEBUG, "RSN: PMKSA cache entry found - PMKID",
609 			    sm->cur_pmksa->pmkid, PMKID_LEN);
610 		return 0;
611 	}
612 	wpa_printf(MSG_DEBUG, "RSN: No PMKSA cache entry found");
613 	return -1;
614 }
615 
616 
617 /**
618  * pmksa_cache_list - Dump text list of entries in PMKSA cache
619  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
620  * @buf: Buffer for the list
621  * @len: Length of the buffer
622  * Returns: number of bytes written to buffer
623  *
624  * This function is used to generate a text format representation of the
625  * current PMKSA cache contents for the ctrl_iface PMKSA command.
626  */
pmksa_cache_list(struct rsn_pmksa_cache * pmksa,char * buf,size_t len)627 int pmksa_cache_list(struct rsn_pmksa_cache *pmksa, char *buf, size_t len)
628 {
629 	int i, ret;
630 	char *pos = buf;
631 	struct rsn_pmksa_cache_entry *entry;
632 	struct os_reltime now;
633 	int cache_id_used = 0;
634 
635 	for (entry = pmksa->pmksa; entry; entry = entry->next) {
636 		if (entry->fils_cache_id_set) {
637 			cache_id_used = 1;
638 			break;
639 		}
640 	}
641 
642 	os_get_reltime(&now);
643 	ret = os_snprintf(pos, buf + len - pos,
644 			  "Index / AA / PMKID / expiration (in seconds) / "
645 			  "opportunistic%s\n",
646 			  cache_id_used ? " / FILS Cache Identifier" : "");
647 	if (os_snprintf_error(buf + len - pos, ret))
648 		return pos - buf;
649 	pos += ret;
650 	i = 0;
651 	entry = pmksa->pmksa;
652 	while (entry) {
653 		i++;
654 		ret = os_snprintf(pos, buf + len - pos, "%d " MACSTR " ",
655 				  i, MAC2STR(entry->aa));
656 		if (os_snprintf_error(buf + len - pos, ret))
657 			return pos - buf;
658 		pos += ret;
659 		pos += wpa_snprintf_hex(pos, buf + len - pos, entry->pmkid,
660 					PMKID_LEN);
661 		ret = os_snprintf(pos, buf + len - pos, " %d %d",
662 				  (int) (entry->expiration - now.sec),
663 				  entry->opportunistic);
664 		if (os_snprintf_error(buf + len - pos, ret))
665 			return pos - buf;
666 		pos += ret;
667 		if (entry->fils_cache_id_set) {
668 			ret = os_snprintf(pos, buf + len - pos, " %02x%02x",
669 					  entry->fils_cache_id[0],
670 					  entry->fils_cache_id[1]);
671 			if (os_snprintf_error(buf + len - pos, ret))
672 				return pos - buf;
673 			pos += ret;
674 		}
675 		ret = os_snprintf(pos, buf + len - pos, "\n");
676 		if (os_snprintf_error(buf + len - pos, ret))
677 			return pos - buf;
678 		pos += ret;
679 		entry = entry->next;
680 	}
681 	return pos - buf;
682 }
683 
684 
pmksa_cache_head(struct rsn_pmksa_cache * pmksa)685 struct rsn_pmksa_cache_entry * pmksa_cache_head(struct rsn_pmksa_cache *pmksa)
686 {
687 	return pmksa->pmksa;
688 }
689 
690 
691 /**
692  * pmksa_cache_init - Initialize PMKSA cache
693  * @free_cb: Callback function to be called when a PMKSA cache entry is freed
694  * @ctx: Context pointer for free_cb function
695  * @sm: Pointer to WPA state machine data from wpa_sm_init()
696  * Returns: Pointer to PMKSA cache data or %NULL on failure
697  */
698 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 * ctx,struct wpa_sm * sm)699 pmksa_cache_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
700 				 void *ctx, enum pmksa_free_reason reason),
701 		 bool (*is_current_cb)(struct rsn_pmksa_cache_entry *entry,
702 				       void *ctx),
703 		 void *ctx, struct wpa_sm *sm)
704 {
705 	struct rsn_pmksa_cache *pmksa;
706 
707 	pmksa = os_zalloc(sizeof(*pmksa));
708 	if (pmksa) {
709 		pmksa->free_cb = free_cb;
710 		pmksa->is_current_cb = is_current_cb;
711 		pmksa->ctx = ctx;
712 		pmksa->sm = sm;
713 	}
714 
715 	return pmksa;
716 }
717 
718 
pmksa_cache_reconfig(struct rsn_pmksa_cache * pmksa)719 void pmksa_cache_reconfig(struct rsn_pmksa_cache *pmksa)
720 {
721 	struct rsn_pmksa_cache_entry *entry;
722 	struct os_reltime now;
723 
724 	if (!pmksa || !pmksa->pmksa)
725 		return;
726 
727 	os_get_reltime(&now);
728 	for (entry = pmksa->pmksa; entry; entry = entry->next) {
729 		u32 life_time;
730 		u8 reauth_threshold;
731 
732 		if (entry->expiration - now.sec < 1 ||
733 		    entry->reauth_time - now.sec < 1)
734 			continue;
735 
736 		life_time = entry->expiration - now.sec;
737 		reauth_threshold = (entry->reauth_time - now.sec) * 100 /
738 			life_time;
739 		if (!reauth_threshold)
740 			continue;
741 
742 		wpa_sm_add_pmkid(pmksa->sm, entry->network_ctx, entry->aa,
743 				 entry->pmkid,
744 				 entry->fils_cache_id_set ?
745 				 entry->fils_cache_id : NULL,
746 				 entry->pmk, entry->pmk_len, life_time,
747 				 reauth_threshold, entry->akmp);
748 	}
749 }
750 
751 #endif /* IEEE8021X_EAPOL */
752