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