xref: /freebsd/contrib/wpa/src/ap/pmksa_cache_auth.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1e28a4053SRui Paulo /*
2e28a4053SRui Paulo  * hostapd - PMKSA cache for IEEE 802.11i RSN
35b9c547cSRui Paulo  * Copyright (c) 2004-2008, 2012-2015, Jouni Malinen <j@w1.fi>
4e28a4053SRui Paulo  *
5f05cddf9SRui Paulo  * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo  * See README for more details.
7e28a4053SRui Paulo  */
8e28a4053SRui Paulo 
9e28a4053SRui Paulo #include "utils/includes.h"
10e28a4053SRui Paulo 
11e28a4053SRui Paulo #include "utils/common.h"
12e28a4053SRui Paulo #include "utils/eloop.h"
13e28a4053SRui Paulo #include "eapol_auth/eapol_auth_sm.h"
14e28a4053SRui Paulo #include "eapol_auth/eapol_auth_sm_i.h"
155b9c547cSRui Paulo #include "radius/radius_das.h"
16e28a4053SRui Paulo #include "sta_info.h"
17e28a4053SRui Paulo #include "ap_config.h"
18e28a4053SRui Paulo #include "pmksa_cache_auth.h"
19e28a4053SRui Paulo 
20e28a4053SRui Paulo 
21e28a4053SRui Paulo static const int pmksa_cache_max_entries = 1024;
22e28a4053SRui Paulo static const int dot11RSNAConfigPMKLifetime = 43200;
23e28a4053SRui Paulo 
24e28a4053SRui Paulo struct rsn_pmksa_cache {
25e28a4053SRui Paulo #define PMKID_HASH_SIZE 128
26e28a4053SRui Paulo #define PMKID_HASH(pmkid) (unsigned int) ((pmkid)[0] & 0x7f)
27e28a4053SRui Paulo 	struct rsn_pmksa_cache_entry *pmkid[PMKID_HASH_SIZE];
28e28a4053SRui Paulo 	struct rsn_pmksa_cache_entry *pmksa;
29e28a4053SRui Paulo 	int pmksa_count;
30e28a4053SRui Paulo 
31e28a4053SRui Paulo 	void (*free_cb)(struct rsn_pmksa_cache_entry *entry, void *ctx);
32e28a4053SRui Paulo 	void *ctx;
33e28a4053SRui Paulo };
34e28a4053SRui Paulo 
35e28a4053SRui Paulo 
36e28a4053SRui Paulo static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa);
37e28a4053SRui Paulo 
38e28a4053SRui Paulo 
_pmksa_cache_free_entry(struct rsn_pmksa_cache_entry * entry)39e28a4053SRui Paulo static void _pmksa_cache_free_entry(struct rsn_pmksa_cache_entry *entry)
40e28a4053SRui Paulo {
41780fb4a2SCy Schubert 	os_free(entry->vlan_desc);
42e28a4053SRui Paulo 	os_free(entry->identity);
43*a90b9d01SCy Schubert 	os_free(entry->dpp_pkhash);
44f05cddf9SRui Paulo 	wpabuf_free(entry->cui);
45e28a4053SRui Paulo #ifndef CONFIG_NO_RADIUS
46e28a4053SRui Paulo 	radius_free_class(&entry->radius_class);
47e28a4053SRui Paulo #endif /* CONFIG_NO_RADIUS */
485b9c547cSRui Paulo 	bin_clear_free(entry, sizeof(*entry));
49e28a4053SRui Paulo }
50e28a4053SRui Paulo 
51e28a4053SRui Paulo 
pmksa_cache_free_entry(struct rsn_pmksa_cache * pmksa,struct rsn_pmksa_cache_entry * entry)525b9c547cSRui Paulo void pmksa_cache_free_entry(struct rsn_pmksa_cache *pmksa,
53e28a4053SRui Paulo 			    struct rsn_pmksa_cache_entry *entry)
54e28a4053SRui Paulo {
55e28a4053SRui Paulo 	struct rsn_pmksa_cache_entry *pos, *prev;
565b9c547cSRui Paulo 	unsigned int hash;
57e28a4053SRui Paulo 
58e28a4053SRui Paulo 	pmksa->pmksa_count--;
59*a90b9d01SCy Schubert 
60*a90b9d01SCy Schubert 	if (pmksa->free_cb)
61e28a4053SRui Paulo 		pmksa->free_cb(entry, pmksa->ctx);
625b9c547cSRui Paulo 
635b9c547cSRui Paulo 	/* unlink from hash list */
645b9c547cSRui Paulo 	hash = PMKID_HASH(entry->pmkid);
655b9c547cSRui Paulo 	pos = pmksa->pmkid[hash];
66e28a4053SRui Paulo 	prev = NULL;
67e28a4053SRui Paulo 	while (pos) {
68e28a4053SRui Paulo 		if (pos == entry) {
695b9c547cSRui Paulo 			if (prev != NULL)
705b9c547cSRui Paulo 				prev->hnext = entry->hnext;
715b9c547cSRui Paulo 			else
725b9c547cSRui Paulo 				pmksa->pmkid[hash] = entry->hnext;
73e28a4053SRui Paulo 			break;
74e28a4053SRui Paulo 		}
75e28a4053SRui Paulo 		prev = pos;
76e28a4053SRui Paulo 		pos = pos->hnext;
77e28a4053SRui Paulo 	}
78e28a4053SRui Paulo 
795b9c547cSRui Paulo 	/* unlink from entry list */
80e28a4053SRui Paulo 	pos = pmksa->pmksa;
81e28a4053SRui Paulo 	prev = NULL;
82e28a4053SRui Paulo 	while (pos) {
83e28a4053SRui Paulo 		if (pos == entry) {
84e28a4053SRui Paulo 			if (prev != NULL)
855b9c547cSRui Paulo 				prev->next = entry->next;
86e28a4053SRui Paulo 			else
875b9c547cSRui Paulo 				pmksa->pmksa = entry->next;
88e28a4053SRui Paulo 			break;
89e28a4053SRui Paulo 		}
90e28a4053SRui Paulo 		prev = pos;
91e28a4053SRui Paulo 		pos = pos->next;
92e28a4053SRui Paulo 	}
935b9c547cSRui Paulo 
94e28a4053SRui Paulo 	_pmksa_cache_free_entry(entry);
95e28a4053SRui Paulo }
96e28a4053SRui Paulo 
97e28a4053SRui Paulo 
98780fb4a2SCy Schubert /**
99780fb4a2SCy Schubert  * pmksa_cache_auth_flush - Flush all PMKSA cache entries
100780fb4a2SCy Schubert  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
101780fb4a2SCy Schubert  */
pmksa_cache_auth_flush(struct rsn_pmksa_cache * pmksa)102780fb4a2SCy Schubert void pmksa_cache_auth_flush(struct rsn_pmksa_cache *pmksa)
103780fb4a2SCy Schubert {
104780fb4a2SCy Schubert 	while (pmksa->pmksa) {
105780fb4a2SCy Schubert 		wpa_printf(MSG_DEBUG, "RSN: Flush PMKSA cache entry for "
106780fb4a2SCy Schubert 			   MACSTR, MAC2STR(pmksa->pmksa->spa));
107780fb4a2SCy Schubert 		pmksa_cache_free_entry(pmksa, pmksa->pmksa);
108780fb4a2SCy Schubert 	}
109780fb4a2SCy Schubert }
110780fb4a2SCy Schubert 
111780fb4a2SCy Schubert 
pmksa_cache_expire(void * eloop_ctx,void * timeout_ctx)112e28a4053SRui Paulo static void pmksa_cache_expire(void *eloop_ctx, void *timeout_ctx)
113e28a4053SRui Paulo {
114e28a4053SRui Paulo 	struct rsn_pmksa_cache *pmksa = eloop_ctx;
1155b9c547cSRui Paulo 	struct os_reltime now;
116e28a4053SRui Paulo 
1175b9c547cSRui Paulo 	os_get_reltime(&now);
118e28a4053SRui Paulo 	while (pmksa->pmksa && pmksa->pmksa->expiration <= now.sec) {
119e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "RSN: expired PMKSA cache entry for "
120f05cddf9SRui Paulo 			   MACSTR, MAC2STR(pmksa->pmksa->spa));
121f05cddf9SRui Paulo 		pmksa_cache_free_entry(pmksa, pmksa->pmksa);
122e28a4053SRui Paulo 	}
123e28a4053SRui Paulo 
124e28a4053SRui Paulo 	pmksa_cache_set_expiration(pmksa);
125e28a4053SRui Paulo }
126e28a4053SRui Paulo 
127e28a4053SRui Paulo 
pmksa_cache_set_expiration(struct rsn_pmksa_cache * pmksa)128e28a4053SRui Paulo static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa)
129e28a4053SRui Paulo {
130e28a4053SRui Paulo 	int sec;
1315b9c547cSRui Paulo 	struct os_reltime now;
132e28a4053SRui Paulo 
133e28a4053SRui Paulo 	eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
134e28a4053SRui Paulo 	if (pmksa->pmksa == NULL)
135e28a4053SRui Paulo 		return;
1365b9c547cSRui Paulo 	os_get_reltime(&now);
137e28a4053SRui Paulo 	sec = pmksa->pmksa->expiration - now.sec;
138e28a4053SRui Paulo 	if (sec < 0)
139e28a4053SRui Paulo 		sec = 0;
140e28a4053SRui Paulo 	eloop_register_timeout(sec + 1, 0, pmksa_cache_expire, pmksa, NULL);
141e28a4053SRui Paulo }
142e28a4053SRui Paulo 
143e28a4053SRui Paulo 
pmksa_cache_from_eapol_data(struct rsn_pmksa_cache_entry * entry,struct eapol_state_machine * eapol)144e28a4053SRui Paulo static void pmksa_cache_from_eapol_data(struct rsn_pmksa_cache_entry *entry,
145e28a4053SRui Paulo 					struct eapol_state_machine *eapol)
146e28a4053SRui Paulo {
147780fb4a2SCy Schubert 	struct vlan_description *vlan_desc;
148780fb4a2SCy Schubert 
149e28a4053SRui Paulo 	if (eapol == NULL)
150e28a4053SRui Paulo 		return;
151e28a4053SRui Paulo 
152e28a4053SRui Paulo 	if (eapol->identity) {
153e28a4053SRui Paulo 		entry->identity = os_malloc(eapol->identity_len);
154e28a4053SRui Paulo 		if (entry->identity) {
155e28a4053SRui Paulo 			entry->identity_len = eapol->identity_len;
156e28a4053SRui Paulo 			os_memcpy(entry->identity, eapol->identity,
157e28a4053SRui Paulo 				  eapol->identity_len);
158e28a4053SRui Paulo 		}
159e28a4053SRui Paulo 	}
160e28a4053SRui Paulo 
161f05cddf9SRui Paulo 	if (eapol->radius_cui)
162f05cddf9SRui Paulo 		entry->cui = wpabuf_dup(eapol->radius_cui);
163f05cddf9SRui Paulo 
164e28a4053SRui Paulo #ifndef CONFIG_NO_RADIUS
165e28a4053SRui Paulo 	radius_copy_class(&entry->radius_class, &eapol->radius_class);
166e28a4053SRui Paulo #endif /* CONFIG_NO_RADIUS */
167e28a4053SRui Paulo 
168e28a4053SRui Paulo 	entry->eap_type_authsrv = eapol->eap_type_authsrv;
1695b9c547cSRui Paulo 
170780fb4a2SCy Schubert 	vlan_desc = ((struct sta_info *) eapol->sta)->vlan_desc;
171780fb4a2SCy Schubert 	if (vlan_desc && vlan_desc->notempty) {
172780fb4a2SCy Schubert 		entry->vlan_desc = os_zalloc(sizeof(struct vlan_description));
173780fb4a2SCy Schubert 		if (entry->vlan_desc)
174780fb4a2SCy Schubert 			*entry->vlan_desc = *vlan_desc;
175780fb4a2SCy Schubert 	} else {
176780fb4a2SCy Schubert 		entry->vlan_desc = NULL;
177780fb4a2SCy Schubert 	}
178780fb4a2SCy Schubert 
179780fb4a2SCy Schubert 	entry->acct_multi_session_id = eapol->acct_multi_session_id;
180e28a4053SRui Paulo }
181e28a4053SRui Paulo 
182e28a4053SRui Paulo 
pmksa_cache_to_eapol_data(struct hostapd_data * hapd,struct rsn_pmksa_cache_entry * entry,struct eapol_state_machine * eapol)183780fb4a2SCy Schubert void pmksa_cache_to_eapol_data(struct hostapd_data *hapd,
184780fb4a2SCy Schubert 			       struct rsn_pmksa_cache_entry *entry,
185e28a4053SRui Paulo 			       struct eapol_state_machine *eapol)
186e28a4053SRui Paulo {
187e28a4053SRui Paulo 	if (entry == NULL || eapol == NULL)
188e28a4053SRui Paulo 		return;
189e28a4053SRui Paulo 
190e28a4053SRui Paulo 	if (entry->identity) {
191e28a4053SRui Paulo 		os_free(eapol->identity);
192e28a4053SRui Paulo 		eapol->identity = os_malloc(entry->identity_len);
193e28a4053SRui Paulo 		if (eapol->identity) {
194e28a4053SRui Paulo 			eapol->identity_len = entry->identity_len;
195e28a4053SRui Paulo 			os_memcpy(eapol->identity, entry->identity,
196e28a4053SRui Paulo 				  entry->identity_len);
197e28a4053SRui Paulo 		}
198e28a4053SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "STA identity from PMKSA",
199e28a4053SRui Paulo 				  eapol->identity, eapol->identity_len);
200e28a4053SRui Paulo 	}
201e28a4053SRui Paulo 
202f05cddf9SRui Paulo 	if (entry->cui) {
203f05cddf9SRui Paulo 		wpabuf_free(eapol->radius_cui);
204f05cddf9SRui Paulo 		eapol->radius_cui = wpabuf_dup(entry->cui);
205f05cddf9SRui Paulo 	}
206f05cddf9SRui Paulo 
207e28a4053SRui Paulo #ifndef CONFIG_NO_RADIUS
208e28a4053SRui Paulo 	radius_free_class(&eapol->radius_class);
209e28a4053SRui Paulo 	radius_copy_class(&eapol->radius_class, &entry->radius_class);
210e28a4053SRui Paulo #endif /* CONFIG_NO_RADIUS */
211e28a4053SRui Paulo 	if (eapol->radius_class.attr) {
212e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "Copied %lu Class attribute(s) from "
213e28a4053SRui Paulo 			   "PMKSA", (unsigned long) eapol->radius_class.count);
214e28a4053SRui Paulo 	}
215e28a4053SRui Paulo 
216e28a4053SRui Paulo 	eapol->eap_type_authsrv = entry->eap_type_authsrv;
217780fb4a2SCy Schubert #ifndef CONFIG_NO_VLAN
218780fb4a2SCy Schubert 	ap_sta_set_vlan(hapd, eapol->sta, entry->vlan_desc);
219780fb4a2SCy Schubert #endif /* CONFIG_NO_VLAN */
2205b9c547cSRui Paulo 
221780fb4a2SCy Schubert 	eapol->acct_multi_session_id = entry->acct_multi_session_id;
222e28a4053SRui Paulo }
223e28a4053SRui Paulo 
224e28a4053SRui Paulo 
pmksa_cache_link_entry(struct rsn_pmksa_cache * pmksa,struct rsn_pmksa_cache_entry * entry)225e28a4053SRui Paulo static void pmksa_cache_link_entry(struct rsn_pmksa_cache *pmksa,
226e28a4053SRui Paulo 				   struct rsn_pmksa_cache_entry *entry)
227e28a4053SRui Paulo {
228e28a4053SRui Paulo 	struct rsn_pmksa_cache_entry *pos, *prev;
2295b9c547cSRui Paulo 	int hash;
230e28a4053SRui Paulo 
231e28a4053SRui Paulo 	/* Add the new entry; order by expiration time */
232e28a4053SRui Paulo 	pos = pmksa->pmksa;
233e28a4053SRui Paulo 	prev = NULL;
234e28a4053SRui Paulo 	while (pos) {
235e28a4053SRui Paulo 		if (pos->expiration > entry->expiration)
236e28a4053SRui Paulo 			break;
237e28a4053SRui Paulo 		prev = pos;
238e28a4053SRui Paulo 		pos = pos->next;
239e28a4053SRui Paulo 	}
240e28a4053SRui Paulo 	if (prev == NULL) {
241e28a4053SRui Paulo 		entry->next = pmksa->pmksa;
242e28a4053SRui Paulo 		pmksa->pmksa = entry;
243e28a4053SRui Paulo 	} else {
244e28a4053SRui Paulo 		entry->next = prev->next;
245e28a4053SRui Paulo 		prev->next = entry;
246e28a4053SRui Paulo 	}
2475b9c547cSRui Paulo 
2485b9c547cSRui Paulo 	hash = PMKID_HASH(entry->pmkid);
2495b9c547cSRui Paulo 	entry->hnext = pmksa->pmkid[hash];
2505b9c547cSRui Paulo 	pmksa->pmkid[hash] = entry;
251e28a4053SRui Paulo 
252e28a4053SRui Paulo 	pmksa->pmksa_count++;
253f05cddf9SRui Paulo 	if (prev == NULL)
254f05cddf9SRui Paulo 		pmksa_cache_set_expiration(pmksa);
255e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "RSN: added PMKSA cache entry for " MACSTR,
256e28a4053SRui Paulo 		   MAC2STR(entry->spa));
257e28a4053SRui Paulo 	wpa_hexdump(MSG_DEBUG, "RSN: added PMKID", entry->pmkid, PMKID_LEN);
258e28a4053SRui Paulo }
259e28a4053SRui Paulo 
260e28a4053SRui Paulo 
261e28a4053SRui Paulo /**
262e28a4053SRui Paulo  * pmksa_cache_auth_add - Add a PMKSA cache entry
263e28a4053SRui Paulo  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
264e28a4053SRui Paulo  * @pmk: The new pairwise master key
265e28a4053SRui Paulo  * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
266780fb4a2SCy Schubert  * @pmkid: Calculated PMKID
2675b9c547cSRui Paulo  * @kck: Key confirmation key or %NULL if not yet derived
2685b9c547cSRui Paulo  * @kck_len: KCK length in bytes
269e28a4053SRui Paulo  * @aa: Authenticator address
270e28a4053SRui Paulo  * @spa: Supplicant address
271e28a4053SRui Paulo  * @session_timeout: Session timeout
272e28a4053SRui Paulo  * @eapol: Pointer to EAPOL state machine data
273e28a4053SRui Paulo  * @akmp: WPA_KEY_MGMT_* used in key derivation
274e28a4053SRui Paulo  * Returns: Pointer to the added PMKSA cache entry or %NULL on error
275e28a4053SRui Paulo  *
276e28a4053SRui Paulo  * This function create a PMKSA entry for a new PMK and adds it to the PMKSA
277e28a4053SRui Paulo  * cache. If an old entry is already in the cache for the same Supplicant,
278e28a4053SRui Paulo  * this entry will be replaced with the new entry. PMKID will be calculated
279e28a4053SRui Paulo  * based on the PMK.
280e28a4053SRui Paulo  */
281e28a4053SRui Paulo struct rsn_pmksa_cache_entry *
pmksa_cache_auth_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,int session_timeout,struct eapol_state_machine * eapol,int akmp)282e28a4053SRui Paulo pmksa_cache_auth_add(struct rsn_pmksa_cache *pmksa,
283780fb4a2SCy Schubert 		     const u8 *pmk, size_t pmk_len, const u8 *pmkid,
2845b9c547cSRui Paulo 		     const u8 *kck, size_t kck_len,
285e28a4053SRui Paulo 		     const u8 *aa, const u8 *spa, int session_timeout,
286e28a4053SRui Paulo 		     struct eapol_state_machine *eapol, int akmp)
287e28a4053SRui Paulo {
28885732ac8SCy Schubert 	struct rsn_pmksa_cache_entry *entry;
28985732ac8SCy Schubert 
29085732ac8SCy Schubert 	entry = pmksa_cache_auth_create_entry(pmk, pmk_len, pmkid, kck, kck_len,
29185732ac8SCy Schubert 					      aa, spa, session_timeout, eapol,
29285732ac8SCy Schubert 					      akmp);
29385732ac8SCy Schubert 
29485732ac8SCy Schubert 	if (pmksa_cache_auth_add_entry(pmksa, entry) < 0)
29585732ac8SCy Schubert 		return NULL;
29685732ac8SCy Schubert 
29785732ac8SCy Schubert 	return entry;
29885732ac8SCy Schubert }
29985732ac8SCy Schubert 
30085732ac8SCy Schubert 
30185732ac8SCy Schubert /**
30285732ac8SCy Schubert  * pmksa_cache_auth_create_entry - Create a PMKSA cache entry
30385732ac8SCy Schubert  * @pmk: The new pairwise master key
30485732ac8SCy Schubert  * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
30585732ac8SCy Schubert  * @pmkid: Calculated PMKID
30685732ac8SCy Schubert  * @kck: Key confirmation key or %NULL if not yet derived
30785732ac8SCy Schubert  * @kck_len: KCK length in bytes
30885732ac8SCy Schubert  * @aa: Authenticator address
30985732ac8SCy Schubert  * @spa: Supplicant address
31085732ac8SCy Schubert  * @session_timeout: Session timeout
31185732ac8SCy Schubert  * @eapol: Pointer to EAPOL state machine data
31285732ac8SCy Schubert  * @akmp: WPA_KEY_MGMT_* used in key derivation
31385732ac8SCy Schubert  * Returns: Pointer to the added PMKSA cache entry or %NULL on error
31485732ac8SCy Schubert  *
31585732ac8SCy Schubert  * This function creates a PMKSA entry.
31685732ac8SCy Schubert  */
31785732ac8SCy Schubert struct rsn_pmksa_cache_entry *
pmksa_cache_auth_create_entry(const u8 * pmk,size_t pmk_len,const u8 * pmkid,const u8 * kck,size_t kck_len,const u8 * aa,const u8 * spa,int session_timeout,struct eapol_state_machine * eapol,int akmp)31885732ac8SCy Schubert pmksa_cache_auth_create_entry(const u8 *pmk, size_t pmk_len, const u8 *pmkid,
31985732ac8SCy Schubert 			      const u8 *kck, size_t kck_len, const u8 *aa,
32085732ac8SCy Schubert 			      const u8 *spa, int session_timeout,
32185732ac8SCy Schubert 			      struct eapol_state_machine *eapol, int akmp)
32285732ac8SCy Schubert {
32385732ac8SCy Schubert 	struct rsn_pmksa_cache_entry *entry;
3245b9c547cSRui Paulo 	struct os_reltime now;
325e28a4053SRui Paulo 
326780fb4a2SCy Schubert 	if (pmk_len > PMK_LEN_MAX)
327e28a4053SRui Paulo 		return NULL;
328e28a4053SRui Paulo 
3295b9c547cSRui Paulo 	if (wpa_key_mgmt_suite_b(akmp) && !kck)
3305b9c547cSRui Paulo 		return NULL;
3315b9c547cSRui Paulo 
332e28a4053SRui Paulo 	entry = os_zalloc(sizeof(*entry));
333e28a4053SRui Paulo 	if (entry == NULL)
334e28a4053SRui Paulo 		return NULL;
335e28a4053SRui Paulo 	os_memcpy(entry->pmk, pmk, pmk_len);
336e28a4053SRui Paulo 	entry->pmk_len = pmk_len;
337*a90b9d01SCy Schubert 	if (kck && kck_len && kck_len < WPA_KCK_MAX_LEN) {
338*a90b9d01SCy Schubert 		os_memcpy(entry->kck, kck, kck_len);
339*a90b9d01SCy Schubert 		entry->kck_len = kck_len;
340*a90b9d01SCy Schubert 	}
341780fb4a2SCy Schubert 	if (pmkid)
342780fb4a2SCy Schubert 		os_memcpy(entry->pmkid, pmkid, PMKID_LEN);
343780fb4a2SCy Schubert 	else if (akmp == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
3445b9c547cSRui Paulo 		rsn_pmkid_suite_b_192(kck, kck_len, aa, spa, entry->pmkid);
3455b9c547cSRui Paulo 	else if (wpa_key_mgmt_suite_b(akmp))
3465b9c547cSRui Paulo 		rsn_pmkid_suite_b(kck, kck_len, aa, spa, entry->pmkid);
3475b9c547cSRui Paulo 	else
34885732ac8SCy Schubert 		rsn_pmkid(pmk, pmk_len, aa, spa, entry->pmkid, akmp);
3495b9c547cSRui Paulo 	os_get_reltime(&now);
350e28a4053SRui Paulo 	entry->expiration = now.sec;
351e28a4053SRui Paulo 	if (session_timeout > 0)
352e28a4053SRui Paulo 		entry->expiration += session_timeout;
353e28a4053SRui Paulo 	else
354e28a4053SRui Paulo 		entry->expiration += dot11RSNAConfigPMKLifetime;
355e28a4053SRui Paulo 	entry->akmp = akmp;
356e28a4053SRui Paulo 	os_memcpy(entry->spa, spa, ETH_ALEN);
357e28a4053SRui Paulo 	pmksa_cache_from_eapol_data(entry, eapol);
358e28a4053SRui Paulo 
35985732ac8SCy Schubert 	return entry;
36085732ac8SCy Schubert }
36185732ac8SCy Schubert 
36285732ac8SCy Schubert 
36385732ac8SCy Schubert /**
36485732ac8SCy Schubert  * pmksa_cache_auth_add_entry - Add a PMKSA cache entry
36585732ac8SCy Schubert  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
36685732ac8SCy Schubert  * @entry: Pointer to PMKSA cache entry
36785732ac8SCy Schubert  *
36885732ac8SCy Schubert  * This function adds PMKSA cache entry to the PMKSA cache. If an old entry is
36985732ac8SCy Schubert  * already in the cache for the same Supplicant, this entry will be replaced
37085732ac8SCy Schubert  * with the new entry. PMKID will be calculated based on the PMK.
37185732ac8SCy Schubert  */
pmksa_cache_auth_add_entry(struct rsn_pmksa_cache * pmksa,struct rsn_pmksa_cache_entry * entry)37285732ac8SCy Schubert int pmksa_cache_auth_add_entry(struct rsn_pmksa_cache *pmksa,
37385732ac8SCy Schubert 			       struct rsn_pmksa_cache_entry *entry)
37485732ac8SCy Schubert {
37585732ac8SCy Schubert 	struct rsn_pmksa_cache_entry *pos;
37685732ac8SCy Schubert 
37785732ac8SCy Schubert 	if (entry == NULL)
37885732ac8SCy Schubert 		return -1;
37985732ac8SCy Schubert 
380e28a4053SRui Paulo 	/* Replace an old entry for the same STA (if found) with the new entry
381e28a4053SRui Paulo 	 */
38285732ac8SCy Schubert 	pos = pmksa_cache_auth_get(pmksa, entry->spa, NULL);
383e28a4053SRui Paulo 	if (pos)
384e28a4053SRui Paulo 		pmksa_cache_free_entry(pmksa, pos);
385e28a4053SRui Paulo 
386e28a4053SRui Paulo 	if (pmksa->pmksa_count >= pmksa_cache_max_entries && pmksa->pmksa) {
387e28a4053SRui Paulo 		/* Remove the oldest entry to make room for the new entry */
388e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "RSN: removed the oldest PMKSA cache "
389e28a4053SRui Paulo 			   "entry (for " MACSTR ") to make room for new one",
390e28a4053SRui Paulo 			   MAC2STR(pmksa->pmksa->spa));
391e28a4053SRui Paulo 		pmksa_cache_free_entry(pmksa, pmksa->pmksa);
392e28a4053SRui Paulo 	}
393e28a4053SRui Paulo 
394e28a4053SRui Paulo 	pmksa_cache_link_entry(pmksa, entry);
395e28a4053SRui Paulo 
39685732ac8SCy Schubert 	return 0;
397e28a4053SRui Paulo }
398e28a4053SRui Paulo 
399e28a4053SRui Paulo 
400e28a4053SRui Paulo struct rsn_pmksa_cache_entry *
pmksa_cache_add_okc(struct rsn_pmksa_cache * pmksa,const struct rsn_pmksa_cache_entry * old_entry,const u8 * aa,const u8 * pmkid)401e28a4053SRui Paulo pmksa_cache_add_okc(struct rsn_pmksa_cache *pmksa,
402e28a4053SRui Paulo 		    const struct rsn_pmksa_cache_entry *old_entry,
403e28a4053SRui Paulo 		    const u8 *aa, const u8 *pmkid)
404e28a4053SRui Paulo {
405e28a4053SRui Paulo 	struct rsn_pmksa_cache_entry *entry;
406e28a4053SRui Paulo 
407e28a4053SRui Paulo 	entry = os_zalloc(sizeof(*entry));
408e28a4053SRui Paulo 	if (entry == NULL)
409e28a4053SRui Paulo 		return NULL;
410e28a4053SRui Paulo 	os_memcpy(entry->pmkid, pmkid, PMKID_LEN);
411e28a4053SRui Paulo 	os_memcpy(entry->pmk, old_entry->pmk, old_entry->pmk_len);
412e28a4053SRui Paulo 	entry->pmk_len = old_entry->pmk_len;
413e28a4053SRui Paulo 	entry->expiration = old_entry->expiration;
414e28a4053SRui Paulo 	entry->akmp = old_entry->akmp;
415e28a4053SRui Paulo 	os_memcpy(entry->spa, old_entry->spa, ETH_ALEN);
416e28a4053SRui Paulo 	entry->opportunistic = 1;
417e28a4053SRui Paulo 	if (old_entry->identity) {
418e28a4053SRui Paulo 		entry->identity = os_malloc(old_entry->identity_len);
419e28a4053SRui Paulo 		if (entry->identity) {
420e28a4053SRui Paulo 			entry->identity_len = old_entry->identity_len;
421e28a4053SRui Paulo 			os_memcpy(entry->identity, old_entry->identity,
422e28a4053SRui Paulo 				  old_entry->identity_len);
423e28a4053SRui Paulo 		}
424e28a4053SRui Paulo 	}
425f05cddf9SRui Paulo 	if (old_entry->cui)
426f05cddf9SRui Paulo 		entry->cui = wpabuf_dup(old_entry->cui);
427e28a4053SRui Paulo #ifndef CONFIG_NO_RADIUS
428e28a4053SRui Paulo 	radius_copy_class(&entry->radius_class, &old_entry->radius_class);
429e28a4053SRui Paulo #endif /* CONFIG_NO_RADIUS */
430e28a4053SRui Paulo 	entry->eap_type_authsrv = old_entry->eap_type_authsrv;
431780fb4a2SCy Schubert 	if (old_entry->vlan_desc) {
432780fb4a2SCy Schubert 		entry->vlan_desc = os_zalloc(sizeof(struct vlan_description));
433780fb4a2SCy Schubert 		if (entry->vlan_desc)
434780fb4a2SCy Schubert 			*entry->vlan_desc = *old_entry->vlan_desc;
435780fb4a2SCy Schubert 	} else {
436780fb4a2SCy Schubert 		entry->vlan_desc = NULL;
437780fb4a2SCy Schubert 	}
438e28a4053SRui Paulo 	entry->opportunistic = 1;
439e28a4053SRui Paulo 
440e28a4053SRui Paulo 	pmksa_cache_link_entry(pmksa, entry);
441e28a4053SRui Paulo 
442e28a4053SRui Paulo 	return entry;
443e28a4053SRui Paulo }
444e28a4053SRui Paulo 
445e28a4053SRui Paulo 
446e28a4053SRui Paulo /**
447e28a4053SRui Paulo  * pmksa_cache_auth_deinit - Free all entries in PMKSA cache
448e28a4053SRui Paulo  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
449e28a4053SRui Paulo  */
pmksa_cache_auth_deinit(struct rsn_pmksa_cache * pmksa)450e28a4053SRui Paulo void pmksa_cache_auth_deinit(struct rsn_pmksa_cache *pmksa)
451e28a4053SRui Paulo {
452e28a4053SRui Paulo 	struct rsn_pmksa_cache_entry *entry, *prev;
453e28a4053SRui Paulo 	int i;
454e28a4053SRui Paulo 
455e28a4053SRui Paulo 	if (pmksa == NULL)
456e28a4053SRui Paulo 		return;
457e28a4053SRui Paulo 
458e28a4053SRui Paulo 	entry = pmksa->pmksa;
459e28a4053SRui Paulo 	while (entry) {
460e28a4053SRui Paulo 		prev = entry;
461e28a4053SRui Paulo 		entry = entry->next;
462e28a4053SRui Paulo 		_pmksa_cache_free_entry(prev);
463e28a4053SRui Paulo 	}
464e28a4053SRui Paulo 	eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
4655b9c547cSRui Paulo 	pmksa->pmksa_count = 0;
4665b9c547cSRui Paulo 	pmksa->pmksa = NULL;
467e28a4053SRui Paulo 	for (i = 0; i < PMKID_HASH_SIZE; i++)
468e28a4053SRui Paulo 		pmksa->pmkid[i] = NULL;
469e28a4053SRui Paulo 	os_free(pmksa);
470e28a4053SRui Paulo }
471e28a4053SRui Paulo 
472e28a4053SRui Paulo 
473e28a4053SRui Paulo /**
474e28a4053SRui Paulo  * pmksa_cache_auth_get - Fetch a PMKSA cache entry
475e28a4053SRui Paulo  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
476e28a4053SRui Paulo  * @spa: Supplicant address or %NULL to match any
477e28a4053SRui Paulo  * @pmkid: PMKID or %NULL to match any
478e28a4053SRui Paulo  * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
479e28a4053SRui Paulo  */
480e28a4053SRui Paulo struct rsn_pmksa_cache_entry *
pmksa_cache_auth_get(struct rsn_pmksa_cache * pmksa,const u8 * spa,const u8 * pmkid)481e28a4053SRui Paulo pmksa_cache_auth_get(struct rsn_pmksa_cache *pmksa,
482e28a4053SRui Paulo 		     const u8 *spa, const u8 *pmkid)
483e28a4053SRui Paulo {
484e28a4053SRui Paulo 	struct rsn_pmksa_cache_entry *entry;
485e28a4053SRui Paulo 
4865b9c547cSRui Paulo 	if (pmkid) {
4875b9c547cSRui Paulo 		for (entry = pmksa->pmkid[PMKID_HASH(pmkid)]; entry;
4885b9c547cSRui Paulo 		     entry = entry->hnext) {
489e28a4053SRui Paulo 			if ((spa == NULL ||
490*a90b9d01SCy Schubert 			     ether_addr_equal(entry->spa, spa)) &&
4915b9c547cSRui Paulo 			    os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0)
492e28a4053SRui Paulo 				return entry;
493e28a4053SRui Paulo 		}
4945b9c547cSRui Paulo 	} else {
4955b9c547cSRui Paulo 		for (entry = pmksa->pmksa; entry; entry = entry->next) {
4965b9c547cSRui Paulo 			if (spa == NULL ||
497*a90b9d01SCy Schubert 			    ether_addr_equal(entry->spa, spa))
4985b9c547cSRui Paulo 				return entry;
4995b9c547cSRui Paulo 		}
5005b9c547cSRui Paulo 	}
5015b9c547cSRui Paulo 
502e28a4053SRui Paulo 	return NULL;
503e28a4053SRui Paulo }
504e28a4053SRui Paulo 
505e28a4053SRui Paulo 
506e28a4053SRui Paulo /**
507e28a4053SRui Paulo  * pmksa_cache_get_okc - Fetch a PMKSA cache entry using OKC
508e28a4053SRui Paulo  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
509e28a4053SRui Paulo  * @aa: Authenticator address
510e28a4053SRui Paulo  * @spa: Supplicant address
511e28a4053SRui Paulo  * @pmkid: PMKID
512e28a4053SRui Paulo  * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
513e28a4053SRui Paulo  *
514e28a4053SRui Paulo  * Use opportunistic key caching (OKC) to find a PMK for a supplicant.
515e28a4053SRui Paulo  */
pmksa_cache_get_okc(struct rsn_pmksa_cache * pmksa,const u8 * aa,const u8 * spa,const u8 * pmkid)516e28a4053SRui Paulo struct rsn_pmksa_cache_entry * pmksa_cache_get_okc(
517e28a4053SRui Paulo 	struct rsn_pmksa_cache *pmksa, const u8 *aa, const u8 *spa,
518e28a4053SRui Paulo 	const u8 *pmkid)
519e28a4053SRui Paulo {
520e28a4053SRui Paulo 	struct rsn_pmksa_cache_entry *entry;
521e28a4053SRui Paulo 	u8 new_pmkid[PMKID_LEN];
522e28a4053SRui Paulo 
5235b9c547cSRui Paulo 	for (entry = pmksa->pmksa; entry; entry = entry->next) {
524*a90b9d01SCy Schubert 		if (!ether_addr_equal(entry->spa, spa))
525e28a4053SRui Paulo 			continue;
526c1d255d3SCy Schubert 		if (wpa_key_mgmt_sae(entry->akmp) ||
527c1d255d3SCy Schubert 		    wpa_key_mgmt_fils(entry->akmp)) {
528c1d255d3SCy Schubert 			if (os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0)
529c1d255d3SCy Schubert 				return entry;
530c1d255d3SCy Schubert 			continue;
531c1d255d3SCy Schubert 		}
532*a90b9d01SCy Schubert 		if (entry->akmp == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192 &&
533*a90b9d01SCy Schubert 		    entry->kck_len > 0)
534*a90b9d01SCy Schubert 			rsn_pmkid_suite_b_192(entry->kck, entry->kck_len,
535*a90b9d01SCy Schubert 					      aa, spa, new_pmkid);
536*a90b9d01SCy Schubert 		else if (wpa_key_mgmt_suite_b(entry->akmp) &&
537*a90b9d01SCy Schubert 			 entry->kck_len > 0)
538*a90b9d01SCy Schubert 		rsn_pmkid_suite_b(entry->kck, entry->kck_len, aa, spa,
539*a90b9d01SCy Schubert 				  new_pmkid);
540*a90b9d01SCy Schubert 		else
541*a90b9d01SCy Schubert 			rsn_pmkid(entry->pmk, entry->pmk_len, aa, spa,
542*a90b9d01SCy Schubert 				  new_pmkid, entry->akmp);
543e28a4053SRui Paulo 		if (os_memcmp(new_pmkid, pmkid, PMKID_LEN) == 0)
544e28a4053SRui Paulo 			return entry;
545e28a4053SRui Paulo 	}
546e28a4053SRui Paulo 	return NULL;
547e28a4053SRui Paulo }
548e28a4053SRui Paulo 
549e28a4053SRui Paulo 
550e28a4053SRui Paulo /**
551e28a4053SRui Paulo  * pmksa_cache_auth_init - Initialize PMKSA cache
552e28a4053SRui Paulo  * @free_cb: Callback function to be called when a PMKSA cache entry is freed
553e28a4053SRui Paulo  * @ctx: Context pointer for free_cb function
554e28a4053SRui Paulo  * Returns: Pointer to PMKSA cache data or %NULL on failure
555e28a4053SRui Paulo  */
556e28a4053SRui Paulo struct rsn_pmksa_cache *
pmksa_cache_auth_init(void (* free_cb)(struct rsn_pmksa_cache_entry * entry,void * ctx),void * ctx)557e28a4053SRui Paulo pmksa_cache_auth_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
558e28a4053SRui Paulo 				      void *ctx), void *ctx)
559e28a4053SRui Paulo {
560e28a4053SRui Paulo 	struct rsn_pmksa_cache *pmksa;
561e28a4053SRui Paulo 
562e28a4053SRui Paulo 	pmksa = os_zalloc(sizeof(*pmksa));
563e28a4053SRui Paulo 	if (pmksa) {
564e28a4053SRui Paulo 		pmksa->free_cb = free_cb;
565e28a4053SRui Paulo 		pmksa->ctx = ctx;
566e28a4053SRui Paulo 	}
567e28a4053SRui Paulo 
568e28a4053SRui Paulo 	return pmksa;
569e28a4053SRui Paulo }
5705b9c547cSRui Paulo 
5715b9c547cSRui Paulo 
das_attr_match(struct rsn_pmksa_cache_entry * entry,struct radius_das_attrs * attr)5725b9c547cSRui Paulo static int das_attr_match(struct rsn_pmksa_cache_entry *entry,
5735b9c547cSRui Paulo 			  struct radius_das_attrs *attr)
5745b9c547cSRui Paulo {
5755b9c547cSRui Paulo 	int match = 0;
5765b9c547cSRui Paulo 
5775b9c547cSRui Paulo 	if (attr->sta_addr) {
578*a90b9d01SCy Schubert 		if (!ether_addr_equal(attr->sta_addr, entry->spa))
5795b9c547cSRui Paulo 			return 0;
5805b9c547cSRui Paulo 		match++;
5815b9c547cSRui Paulo 	}
5825b9c547cSRui Paulo 
5835b9c547cSRui Paulo 	if (attr->acct_multi_session_id) {
5845b9c547cSRui Paulo 		char buf[20];
5855b9c547cSRui Paulo 
586780fb4a2SCy Schubert 		if (attr->acct_multi_session_id_len != 16)
5875b9c547cSRui Paulo 			return 0;
588780fb4a2SCy Schubert 		os_snprintf(buf, sizeof(buf), "%016llX",
589780fb4a2SCy Schubert 			    (unsigned long long) entry->acct_multi_session_id);
590780fb4a2SCy Schubert 		if (os_memcmp(attr->acct_multi_session_id, buf, 16) != 0)
5915b9c547cSRui Paulo 			return 0;
5925b9c547cSRui Paulo 		match++;
5935b9c547cSRui Paulo 	}
5945b9c547cSRui Paulo 
5955b9c547cSRui Paulo 	if (attr->cui) {
5965b9c547cSRui Paulo 		if (!entry->cui ||
5975b9c547cSRui Paulo 		    attr->cui_len != wpabuf_len(entry->cui) ||
5985b9c547cSRui Paulo 		    os_memcmp(attr->cui, wpabuf_head(entry->cui),
5995b9c547cSRui Paulo 			      attr->cui_len) != 0)
6005b9c547cSRui Paulo 			return 0;
6015b9c547cSRui Paulo 		match++;
6025b9c547cSRui Paulo 	}
6035b9c547cSRui Paulo 
6045b9c547cSRui Paulo 	if (attr->user_name) {
6055b9c547cSRui Paulo 		if (!entry->identity ||
6065b9c547cSRui Paulo 		    attr->user_name_len != entry->identity_len ||
6075b9c547cSRui Paulo 		    os_memcmp(attr->user_name, entry->identity,
6085b9c547cSRui Paulo 			      attr->user_name_len) != 0)
6095b9c547cSRui Paulo 			return 0;
6105b9c547cSRui Paulo 		match++;
6115b9c547cSRui Paulo 	}
6125b9c547cSRui Paulo 
6135b9c547cSRui Paulo 	return match;
6145b9c547cSRui Paulo }
6155b9c547cSRui Paulo 
6165b9c547cSRui Paulo 
pmksa_cache_auth_radius_das_disconnect(struct rsn_pmksa_cache * pmksa,struct radius_das_attrs * attr)6175b9c547cSRui Paulo int pmksa_cache_auth_radius_das_disconnect(struct rsn_pmksa_cache *pmksa,
6185b9c547cSRui Paulo 					   struct radius_das_attrs *attr)
6195b9c547cSRui Paulo {
6205b9c547cSRui Paulo 	int found = 0;
6215b9c547cSRui Paulo 	struct rsn_pmksa_cache_entry *entry, *prev;
6225b9c547cSRui Paulo 
6235b9c547cSRui Paulo 	if (attr->acct_session_id)
6245b9c547cSRui Paulo 		return -1;
6255b9c547cSRui Paulo 
6265b9c547cSRui Paulo 	entry = pmksa->pmksa;
6275b9c547cSRui Paulo 	while (entry) {
6285b9c547cSRui Paulo 		if (das_attr_match(entry, attr)) {
6295b9c547cSRui Paulo 			found++;
6305b9c547cSRui Paulo 			prev = entry;
6315b9c547cSRui Paulo 			entry = entry->next;
6325b9c547cSRui Paulo 			pmksa_cache_free_entry(pmksa, prev);
6335b9c547cSRui Paulo 			continue;
6345b9c547cSRui Paulo 		}
6355b9c547cSRui Paulo 		entry = entry->next;
6365b9c547cSRui Paulo 	}
6375b9c547cSRui Paulo 
6385b9c547cSRui Paulo 	return found ? 0 : -1;
6395b9c547cSRui Paulo }
640780fb4a2SCy Schubert 
641780fb4a2SCy Schubert 
642780fb4a2SCy Schubert /**
643780fb4a2SCy Schubert  * pmksa_cache_auth_list - Dump text list of entries in PMKSA cache
644780fb4a2SCy Schubert  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
645780fb4a2SCy Schubert  * @buf: Buffer for the list
646780fb4a2SCy Schubert  * @len: Length of the buffer
647780fb4a2SCy Schubert  * Returns: Number of bytes written to buffer
648780fb4a2SCy Schubert  *
649780fb4a2SCy Schubert  * This function is used to generate a text format representation of the
650780fb4a2SCy Schubert  * current PMKSA cache contents for the ctrl_iface PMKSA command.
651780fb4a2SCy Schubert  */
pmksa_cache_auth_list(struct rsn_pmksa_cache * pmksa,char * buf,size_t len)652780fb4a2SCy Schubert int pmksa_cache_auth_list(struct rsn_pmksa_cache *pmksa, char *buf, size_t len)
653780fb4a2SCy Schubert {
654780fb4a2SCy Schubert 	int i, ret;
655780fb4a2SCy Schubert 	char *pos = buf;
656780fb4a2SCy Schubert 	struct rsn_pmksa_cache_entry *entry;
657780fb4a2SCy Schubert 	struct os_reltime now;
658780fb4a2SCy Schubert 
659780fb4a2SCy Schubert 	os_get_reltime(&now);
660780fb4a2SCy Schubert 	ret = os_snprintf(pos, buf + len - pos,
661780fb4a2SCy Schubert 			  "Index / SPA / PMKID / expiration (in seconds) / opportunistic\n");
662780fb4a2SCy Schubert 	if (os_snprintf_error(buf + len - pos, ret))
663780fb4a2SCy Schubert 		return pos - buf;
664780fb4a2SCy Schubert 	pos += ret;
665780fb4a2SCy Schubert 	i = 0;
666780fb4a2SCy Schubert 	entry = pmksa->pmksa;
667780fb4a2SCy Schubert 	while (entry) {
668780fb4a2SCy Schubert 		ret = os_snprintf(pos, buf + len - pos, "%d " MACSTR " ",
669780fb4a2SCy Schubert 				  i, MAC2STR(entry->spa));
670780fb4a2SCy Schubert 		if (os_snprintf_error(buf + len - pos, ret))
671780fb4a2SCy Schubert 			return pos - buf;
672780fb4a2SCy Schubert 		pos += ret;
673780fb4a2SCy Schubert 		pos += wpa_snprintf_hex(pos, buf + len - pos, entry->pmkid,
674780fb4a2SCy Schubert 					PMKID_LEN);
675780fb4a2SCy Schubert 		ret = os_snprintf(pos, buf + len - pos, " %d %d\n",
676780fb4a2SCy Schubert 				  (int) (entry->expiration - now.sec),
677780fb4a2SCy Schubert 				  entry->opportunistic);
678780fb4a2SCy Schubert 		if (os_snprintf_error(buf + len - pos, ret))
679780fb4a2SCy Schubert 			return pos - buf;
680780fb4a2SCy Schubert 		pos += ret;
681780fb4a2SCy Schubert 		entry = entry->next;
682780fb4a2SCy Schubert 	}
683780fb4a2SCy Schubert 	return pos - buf;
684780fb4a2SCy Schubert }
68585732ac8SCy Schubert 
68685732ac8SCy Schubert 
68785732ac8SCy Schubert #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
68885732ac8SCy Schubert #ifdef CONFIG_MESH
68985732ac8SCy Schubert 
69085732ac8SCy Schubert /**
69185732ac8SCy Schubert  * pmksa_cache_auth_list_mesh - Dump text list of entries in PMKSA cache
69285732ac8SCy Schubert  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
69385732ac8SCy Schubert  * @addr: MAC address of the peer (NULL means any)
69485732ac8SCy Schubert  * @buf: Buffer for the list
69585732ac8SCy Schubert  * @len: Length of the buffer
69685732ac8SCy Schubert  * Returns: Number of bytes written to buffer
69785732ac8SCy Schubert  *
69885732ac8SCy Schubert  * This function is used to generate a text format representation of the
69985732ac8SCy Schubert  * current PMKSA cache contents for the ctrl_iface PMKSA_GET command to store
70085732ac8SCy Schubert  * in external storage.
70185732ac8SCy Schubert  */
pmksa_cache_auth_list_mesh(struct rsn_pmksa_cache * pmksa,const u8 * addr,char * buf,size_t len)70285732ac8SCy Schubert int pmksa_cache_auth_list_mesh(struct rsn_pmksa_cache *pmksa, const u8 *addr,
70385732ac8SCy Schubert 			       char *buf, size_t len)
70485732ac8SCy Schubert {
70585732ac8SCy Schubert 	int ret;
70685732ac8SCy Schubert 	char *pos, *end;
70785732ac8SCy Schubert 	struct rsn_pmksa_cache_entry *entry;
70885732ac8SCy Schubert 	struct os_reltime now;
70985732ac8SCy Schubert 
71085732ac8SCy Schubert 	pos = buf;
71185732ac8SCy Schubert 	end = buf + len;
71285732ac8SCy Schubert 	os_get_reltime(&now);
71385732ac8SCy Schubert 
71485732ac8SCy Schubert 
71585732ac8SCy Schubert 	/*
71685732ac8SCy Schubert 	 * Entry format:
71785732ac8SCy Schubert 	 * <BSSID> <PMKID> <PMK> <expiration in seconds>
71885732ac8SCy Schubert 	 */
71985732ac8SCy Schubert 	for (entry = pmksa->pmksa; entry; entry = entry->next) {
720*a90b9d01SCy Schubert 		if (addr && !ether_addr_equal(entry->spa, addr))
72185732ac8SCy Schubert 			continue;
72285732ac8SCy Schubert 
72385732ac8SCy Schubert 		ret = os_snprintf(pos, end - pos, MACSTR " ",
72485732ac8SCy Schubert 				  MAC2STR(entry->spa));
72585732ac8SCy Schubert 		if (os_snprintf_error(end - pos, ret))
72685732ac8SCy Schubert 			return 0;
72785732ac8SCy Schubert 		pos += ret;
72885732ac8SCy Schubert 
72985732ac8SCy Schubert 		pos += wpa_snprintf_hex(pos, end - pos, entry->pmkid,
73085732ac8SCy Schubert 					PMKID_LEN);
73185732ac8SCy Schubert 
73285732ac8SCy Schubert 		ret = os_snprintf(pos, end - pos, " ");
73385732ac8SCy Schubert 		if (os_snprintf_error(end - pos, ret))
73485732ac8SCy Schubert 			return 0;
73585732ac8SCy Schubert 		pos += ret;
73685732ac8SCy Schubert 
73785732ac8SCy Schubert 		pos += wpa_snprintf_hex(pos, end - pos, entry->pmk,
73885732ac8SCy Schubert 					entry->pmk_len);
73985732ac8SCy Schubert 
74085732ac8SCy Schubert 		ret = os_snprintf(pos, end - pos, " %d\n",
74185732ac8SCy Schubert 				  (int) (entry->expiration - now.sec));
74285732ac8SCy Schubert 		if (os_snprintf_error(end - pos, ret))
74385732ac8SCy Schubert 			return 0;
74485732ac8SCy Schubert 		pos += ret;
74585732ac8SCy Schubert 	}
74685732ac8SCy Schubert 
74785732ac8SCy Schubert 	return pos - buf;
74885732ac8SCy Schubert }
74985732ac8SCy Schubert 
75085732ac8SCy Schubert #endif /* CONFIG_MESH */
75185732ac8SCy Schubert #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
752