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