• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * hostapd - PMKSA cache for IEEE 802.11i RSN
3  * Copyright (c) 2004-2008, 2012-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 "utils/includes.h"
10 
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "eapol_auth/eapol_auth_sm.h"
14 #include "eapol_auth/eapol_auth_sm_i.h"
15 #include "radius/radius_das.h"
16 #include "sta_info.h"
17 #include "ap_config.h"
18 #include "pmksa_cache_auth.h"
19 
20 
21 static const int pmksa_cache_max_entries = 1024;
22 static const int dot11RSNAConfigPMKLifetime = 43200;
23 
24 struct rsn_pmksa_cache {
25 #define PMKID_HASH_SIZE 128
26 #define PMKID_HASH(pmkid) (unsigned int) ((pmkid)[0] & 0x7f)
27 	struct rsn_pmksa_cache_entry *pmkid[PMKID_HASH_SIZE];
28 	struct rsn_pmksa_cache_entry *pmksa;
29 	int pmksa_count;
30 
31 	void (*free_cb)(struct rsn_pmksa_cache_entry *entry, void *ctx);
32 	void *ctx;
33 };
34 
35 
36 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa);
37 
38 
_pmksa_cache_free_entry(struct rsn_pmksa_cache_entry * entry)39 static void _pmksa_cache_free_entry(struct rsn_pmksa_cache_entry *entry)
40 {
41 	os_free(entry->vlan_desc);
42 	os_free(entry->identity);
43 	os_free(entry->dpp_pkhash);
44 	wpabuf_free(entry->cui);
45 #ifndef CONFIG_NO_RADIUS
46 	radius_free_class(&entry->radius_class);
47 #endif /* CONFIG_NO_RADIUS */
48 	bin_clear_free(entry, sizeof(*entry));
49 }
50 
51 
pmksa_cache_free_entry(struct rsn_pmksa_cache * pmksa,struct rsn_pmksa_cache_entry * entry)52 void pmksa_cache_free_entry(struct rsn_pmksa_cache *pmksa,
53 			    struct rsn_pmksa_cache_entry *entry)
54 {
55 	struct rsn_pmksa_cache_entry *pos, *prev;
56 	unsigned int hash;
57 
58 	pmksa->pmksa_count--;
59 	pmksa->free_cb(entry, pmksa->ctx);
60 
61 	/* unlink from hash list */
62 	hash = PMKID_HASH(entry->pmkid);
63 	pos = pmksa->pmkid[hash];
64 	prev = NULL;
65 	while (pos) {
66 		if (pos == entry) {
67 			if (prev != NULL)
68 				prev->hnext = entry->hnext;
69 			else
70 				pmksa->pmkid[hash] = entry->hnext;
71 			break;
72 		}
73 		prev = pos;
74 		pos = pos->hnext;
75 	}
76 
77 	/* unlink from entry list */
78 	pos = pmksa->pmksa;
79 	prev = NULL;
80 	while (pos) {
81 		if (pos == entry) {
82 			if (prev != NULL)
83 				prev->next = entry->next;
84 			else
85 				pmksa->pmksa = entry->next;
86 			break;
87 		}
88 		prev = pos;
89 		pos = pos->next;
90 	}
91 
92 	_pmksa_cache_free_entry(entry);
93 }
94 
95 
96 /**
97  * pmksa_cache_auth_flush - Flush all PMKSA cache entries
98  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
99  */
pmksa_cache_auth_flush(struct rsn_pmksa_cache * pmksa)100 void pmksa_cache_auth_flush(struct rsn_pmksa_cache *pmksa)
101 {
102 	while (pmksa->pmksa) {
103 		wpa_printf(MSG_DEBUG, "RSN: Flush PMKSA cache entry for "
104 			   MACSTR, MAC2STR(pmksa->pmksa->spa));
105 		pmksa_cache_free_entry(pmksa, pmksa->pmksa);
106 	}
107 }
108 
109 
pmksa_cache_expire(void * eloop_ctx,void * timeout_ctx)110 static void pmksa_cache_expire(void *eloop_ctx, void *timeout_ctx)
111 {
112 	struct rsn_pmksa_cache *pmksa = eloop_ctx;
113 	struct os_reltime now;
114 
115 	os_get_reltime(&now);
116 	while (pmksa->pmksa && pmksa->pmksa->expiration <= now.sec) {
117 		wpa_printf(MSG_DEBUG, "RSN: expired PMKSA cache entry for "
118 			   MACSTR, MAC2STR(pmksa->pmksa->spa));
119 		pmksa_cache_free_entry(pmksa, pmksa->pmksa);
120 	}
121 
122 	pmksa_cache_set_expiration(pmksa);
123 }
124 
125 
pmksa_cache_set_expiration(struct rsn_pmksa_cache * pmksa)126 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa)
127 {
128 	int sec;
129 	struct os_reltime now;
130 
131 	eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
132 	if (pmksa->pmksa == NULL)
133 		return;
134 	os_get_reltime(&now);
135 	sec = pmksa->pmksa->expiration - now.sec;
136 	if (sec < 0)
137 		sec = 0;
138 	eloop_register_timeout(sec + 1, 0, pmksa_cache_expire, pmksa, NULL);
139 }
140 
141 
pmksa_cache_from_eapol_data(struct rsn_pmksa_cache_entry * entry,struct eapol_state_machine * eapol)142 static void pmksa_cache_from_eapol_data(struct rsn_pmksa_cache_entry *entry,
143 					struct eapol_state_machine *eapol)
144 {
145 	struct vlan_description *vlan_desc;
146 
147 	if (eapol == NULL)
148 		return;
149 
150 	if (eapol->identity) {
151 		entry->identity = os_malloc(eapol->identity_len);
152 		if (entry->identity) {
153 			entry->identity_len = eapol->identity_len;
154 			os_memcpy(entry->identity, eapol->identity,
155 				  eapol->identity_len);
156 		}
157 	}
158 
159 	if (eapol->radius_cui)
160 		entry->cui = wpabuf_dup(eapol->radius_cui);
161 
162 #ifndef CONFIG_NO_RADIUS
163 	radius_copy_class(&entry->radius_class, &eapol->radius_class);
164 #endif /* CONFIG_NO_RADIUS */
165 
166 	entry->eap_type_authsrv = eapol->eap_type_authsrv;
167 
168 	vlan_desc = ((struct sta_info *) eapol->sta)->vlan_desc;
169 	if (vlan_desc && vlan_desc->notempty) {
170 		entry->vlan_desc = os_zalloc(sizeof(struct vlan_description));
171 		if (entry->vlan_desc)
172 			*entry->vlan_desc = *vlan_desc;
173 	} else {
174 		entry->vlan_desc = NULL;
175 	}
176 
177 	entry->acct_multi_session_id = eapol->acct_multi_session_id;
178 }
179 
180 
pmksa_cache_to_eapol_data(struct hostapd_data * hapd,struct rsn_pmksa_cache_entry * entry,struct eapol_state_machine * eapol)181 void pmksa_cache_to_eapol_data(struct hostapd_data *hapd,
182 			       struct rsn_pmksa_cache_entry *entry,
183 			       struct eapol_state_machine *eapol)
184 {
185 	if (entry == NULL || eapol == NULL)
186 		return;
187 
188 	if (entry->identity) {
189 		os_free(eapol->identity);
190 		eapol->identity = os_malloc(entry->identity_len);
191 		if (eapol->identity) {
192 			eapol->identity_len = entry->identity_len;
193 			os_memcpy(eapol->identity, entry->identity,
194 				  entry->identity_len);
195 		}
196 		wpa_hexdump_ascii(MSG_DEBUG, "STA identity from PMKSA",
197 				  eapol->identity, eapol->identity_len);
198 	}
199 
200 	if (entry->cui) {
201 		wpabuf_free(eapol->radius_cui);
202 		eapol->radius_cui = wpabuf_dup(entry->cui);
203 	}
204 
205 #ifndef CONFIG_NO_RADIUS
206 	radius_free_class(&eapol->radius_class);
207 	radius_copy_class(&eapol->radius_class, &entry->radius_class);
208 #endif /* CONFIG_NO_RADIUS */
209 	if (eapol->radius_class.attr) {
210 		wpa_printf(MSG_DEBUG, "Copied %lu Class attribute(s) from "
211 			   "PMKSA", (unsigned long) eapol->radius_class.count);
212 	}
213 
214 	eapol->eap_type_authsrv = entry->eap_type_authsrv;
215 #ifndef CONFIG_NO_VLAN
216 	ap_sta_set_vlan(hapd, eapol->sta, entry->vlan_desc);
217 #endif /* CONFIG_NO_VLAN */
218 
219 	eapol->acct_multi_session_id = entry->acct_multi_session_id;
220 }
221 
222 
pmksa_cache_link_entry(struct rsn_pmksa_cache * pmksa,struct rsn_pmksa_cache_entry * entry)223 static void pmksa_cache_link_entry(struct rsn_pmksa_cache *pmksa,
224 				   struct rsn_pmksa_cache_entry *entry)
225 {
226 	struct rsn_pmksa_cache_entry *pos, *prev;
227 	int hash;
228 
229 	/* Add the new entry; order by expiration time */
230 	pos = pmksa->pmksa;
231 	prev = NULL;
232 	while (pos) {
233 		if (pos->expiration > entry->expiration)
234 			break;
235 		prev = pos;
236 		pos = pos->next;
237 	}
238 	if (prev == NULL) {
239 		entry->next = pmksa->pmksa;
240 		pmksa->pmksa = entry;
241 	} else {
242 		entry->next = prev->next;
243 		prev->next = entry;
244 	}
245 
246 	hash = PMKID_HASH(entry->pmkid);
247 	entry->hnext = pmksa->pmkid[hash];
248 	pmksa->pmkid[hash] = entry;
249 
250 	pmksa->pmksa_count++;
251 	if (prev == NULL)
252 		pmksa_cache_set_expiration(pmksa);
253 	wpa_printf(MSG_DEBUG, "RSN: added PMKSA cache entry for " MACSTR,
254 		   MAC2STR(entry->spa));
255 	wpa_hexdump(MSG_DEBUG, "RSN: added PMKID", entry->pmkid, PMKID_LEN);
256 }
257 
258 
259 /**
260  * pmksa_cache_auth_add - Add a PMKSA cache entry
261  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
262  * @pmk: The new pairwise master key
263  * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
264  * @pmkid: Calculated PMKID
265  * @kck: Key confirmation key or %NULL if not yet derived
266  * @kck_len: KCK length in bytes
267  * @aa: Authenticator address
268  * @spa: Supplicant address
269  * @session_timeout: Session timeout
270  * @eapol: Pointer to EAPOL state machine data
271  * @akmp: WPA_KEY_MGMT_* used in key derivation
272  * Returns: Pointer to the added PMKSA cache entry or %NULL on error
273  *
274  * This function create a PMKSA entry for a new PMK and adds it to the PMKSA
275  * cache. If an old entry is already in the cache for the same Supplicant,
276  * this entry will be replaced with the new entry. PMKID will be calculated
277  * based on the PMK.
278  */
279 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)280 pmksa_cache_auth_add(struct rsn_pmksa_cache *pmksa,
281 		     const u8 *pmk, size_t pmk_len, const u8 *pmkid,
282 		     const u8 *kck, size_t kck_len,
283 		     const u8 *aa, const u8 *spa, int session_timeout,
284 		     struct eapol_state_machine *eapol, int akmp)
285 {
286 	struct rsn_pmksa_cache_entry *entry;
287 
288 	entry = pmksa_cache_auth_create_entry(pmk, pmk_len, pmkid, kck, kck_len,
289 					      aa, spa, session_timeout, eapol,
290 					      akmp);
291 
292 	if (pmksa_cache_auth_add_entry(pmksa, entry) < 0)
293 		return NULL;
294 
295 	return entry;
296 }
297 
298 
299 /**
300  * pmksa_cache_auth_create_entry - Create a PMKSA cache entry
301  * @pmk: The new pairwise master key
302  * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
303  * @pmkid: Calculated PMKID
304  * @kck: Key confirmation key or %NULL if not yet derived
305  * @kck_len: KCK length in bytes
306  * @aa: Authenticator address
307  * @spa: Supplicant address
308  * @session_timeout: Session timeout
309  * @eapol: Pointer to EAPOL state machine data
310  * @akmp: WPA_KEY_MGMT_* used in key derivation
311  * Returns: Pointer to the added PMKSA cache entry or %NULL on error
312  *
313  * This function creates a PMKSA entry.
314  */
315 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)316 pmksa_cache_auth_create_entry(const u8 *pmk, size_t pmk_len, const u8 *pmkid,
317 			      const u8 *kck, size_t kck_len, const u8 *aa,
318 			      const u8 *spa, int session_timeout,
319 			      struct eapol_state_machine *eapol, int akmp)
320 {
321 	struct rsn_pmksa_cache_entry *entry;
322 	struct os_reltime now;
323 
324 	if (pmk_len > PMK_LEN_MAX)
325 		return NULL;
326 
327 	if (wpa_key_mgmt_suite_b(akmp) && !kck)
328 		return NULL;
329 
330 	entry = os_zalloc(sizeof(*entry));
331 	if (entry == NULL)
332 		return NULL;
333 	os_memcpy(entry->pmk, pmk, pmk_len);
334 	entry->pmk_len = pmk_len;
335 	if (pmkid)
336 		os_memcpy(entry->pmkid, pmkid, PMKID_LEN);
337 	else if (akmp == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
338 		rsn_pmkid_suite_b_192(kck, kck_len, aa, spa, entry->pmkid);
339 	else if (wpa_key_mgmt_suite_b(akmp))
340 		rsn_pmkid_suite_b(kck, kck_len, aa, spa, entry->pmkid);
341 	else
342 		rsn_pmkid(pmk, pmk_len, aa, spa, entry->pmkid, akmp);
343 	os_get_reltime(&now);
344 	entry->expiration = now.sec;
345 	if (session_timeout > 0)
346 		entry->expiration += session_timeout;
347 	else
348 		entry->expiration += dot11RSNAConfigPMKLifetime;
349 	entry->akmp = akmp;
350 	os_memcpy(entry->spa, spa, ETH_ALEN);
351 	pmksa_cache_from_eapol_data(entry, eapol);
352 
353 	return entry;
354 }
355 
356 
357 /**
358  * pmksa_cache_auth_add_entry - Add a PMKSA cache entry
359  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
360  * @entry: Pointer to PMKSA cache entry
361  *
362  * This function adds PMKSA cache entry to the PMKSA cache. If an old entry is
363  * already in the cache for the same Supplicant, this entry will be replaced
364  * with the new entry. PMKID will be calculated based on the PMK.
365  */
pmksa_cache_auth_add_entry(struct rsn_pmksa_cache * pmksa,struct rsn_pmksa_cache_entry * entry)366 int pmksa_cache_auth_add_entry(struct rsn_pmksa_cache *pmksa,
367 			       struct rsn_pmksa_cache_entry *entry)
368 {
369 	struct rsn_pmksa_cache_entry *pos;
370 
371 	if (entry == NULL)
372 		return -1;
373 
374 	/* Replace an old entry for the same STA (if found) with the new entry
375 	 */
376 	pos = pmksa_cache_auth_get(pmksa, entry->spa, NULL);
377 	if (pos)
378 		pmksa_cache_free_entry(pmksa, pos);
379 
380 	if (pmksa->pmksa_count >= pmksa_cache_max_entries && pmksa->pmksa) {
381 		/* Remove the oldest entry to make room for the new entry */
382 		wpa_printf(MSG_DEBUG, "RSN: removed the oldest PMKSA cache "
383 			   "entry (for " MACSTR ") to make room for new one",
384 			   MAC2STR(pmksa->pmksa->spa));
385 		pmksa_cache_free_entry(pmksa, pmksa->pmksa);
386 	}
387 
388 	pmksa_cache_link_entry(pmksa, entry);
389 
390 	return 0;
391 }
392 
393 
394 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)395 pmksa_cache_add_okc(struct rsn_pmksa_cache *pmksa,
396 		    const struct rsn_pmksa_cache_entry *old_entry,
397 		    const u8 *aa, const u8 *pmkid)
398 {
399 	struct rsn_pmksa_cache_entry *entry;
400 
401 	entry = os_zalloc(sizeof(*entry));
402 	if (entry == NULL)
403 		return NULL;
404 	os_memcpy(entry->pmkid, pmkid, PMKID_LEN);
405 	os_memcpy(entry->pmk, old_entry->pmk, old_entry->pmk_len);
406 	entry->pmk_len = old_entry->pmk_len;
407 	entry->expiration = old_entry->expiration;
408 	entry->akmp = old_entry->akmp;
409 	os_memcpy(entry->spa, old_entry->spa, ETH_ALEN);
410 	entry->opportunistic = 1;
411 	if (old_entry->identity) {
412 		entry->identity = os_malloc(old_entry->identity_len);
413 		if (entry->identity) {
414 			entry->identity_len = old_entry->identity_len;
415 			os_memcpy(entry->identity, old_entry->identity,
416 				  old_entry->identity_len);
417 		}
418 	}
419 	if (old_entry->cui)
420 		entry->cui = wpabuf_dup(old_entry->cui);
421 #ifndef CONFIG_NO_RADIUS
422 	radius_copy_class(&entry->radius_class, &old_entry->radius_class);
423 #endif /* CONFIG_NO_RADIUS */
424 	entry->eap_type_authsrv = old_entry->eap_type_authsrv;
425 	if (old_entry->vlan_desc) {
426 		entry->vlan_desc = os_zalloc(sizeof(struct vlan_description));
427 		if (entry->vlan_desc)
428 			*entry->vlan_desc = *old_entry->vlan_desc;
429 	} else {
430 		entry->vlan_desc = NULL;
431 	}
432 	entry->opportunistic = 1;
433 
434 	pmksa_cache_link_entry(pmksa, entry);
435 
436 	return entry;
437 }
438 
439 
440 /**
441  * pmksa_cache_auth_deinit - Free all entries in PMKSA cache
442  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
443  */
pmksa_cache_auth_deinit(struct rsn_pmksa_cache * pmksa)444 void pmksa_cache_auth_deinit(struct rsn_pmksa_cache *pmksa)
445 {
446 	struct rsn_pmksa_cache_entry *entry, *prev;
447 	int i;
448 
449 	if (pmksa == NULL)
450 		return;
451 
452 	entry = pmksa->pmksa;
453 	while (entry) {
454 		prev = entry;
455 		entry = entry->next;
456 		_pmksa_cache_free_entry(prev);
457 	}
458 	eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
459 	pmksa->pmksa_count = 0;
460 	pmksa->pmksa = NULL;
461 	for (i = 0; i < PMKID_HASH_SIZE; i++)
462 		pmksa->pmkid[i] = NULL;
463 	os_free(pmksa);
464 }
465 
466 
467 /**
468  * pmksa_cache_auth_get - Fetch a PMKSA cache entry
469  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
470  * @spa: Supplicant address or %NULL to match any
471  * @pmkid: PMKID or %NULL to match any
472  * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
473  */
474 struct rsn_pmksa_cache_entry *
pmksa_cache_auth_get(struct rsn_pmksa_cache * pmksa,const u8 * spa,const u8 * pmkid)475 pmksa_cache_auth_get(struct rsn_pmksa_cache *pmksa,
476 		     const u8 *spa, const u8 *pmkid)
477 {
478 	struct rsn_pmksa_cache_entry *entry;
479 
480 	if (pmkid) {
481 		for (entry = pmksa->pmkid[PMKID_HASH(pmkid)]; entry;
482 		     entry = entry->hnext) {
483 			if ((spa == NULL ||
484 			     os_memcmp(entry->spa, spa, ETH_ALEN) == 0) &&
485 			    os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0)
486 				return entry;
487 		}
488 	} else {
489 		for (entry = pmksa->pmksa; entry; entry = entry->next) {
490 			if (spa == NULL ||
491 			    os_memcmp(entry->spa, spa, ETH_ALEN) == 0)
492 				return entry;
493 		}
494 	}
495 
496 	return NULL;
497 }
498 
499 
500 /**
501  * pmksa_cache_get_okc - Fetch a PMKSA cache entry using OKC
502  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
503  * @aa: Authenticator address
504  * @spa: Supplicant address
505  * @pmkid: PMKID
506  * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
507  *
508  * Use opportunistic key caching (OKC) to find a PMK for a supplicant.
509  */
pmksa_cache_get_okc(struct rsn_pmksa_cache * pmksa,const u8 * aa,const u8 * spa,const u8 * pmkid)510 struct rsn_pmksa_cache_entry * pmksa_cache_get_okc(
511 	struct rsn_pmksa_cache *pmksa, const u8 *aa, const u8 *spa,
512 	const u8 *pmkid)
513 {
514 	struct rsn_pmksa_cache_entry *entry;
515 	u8 new_pmkid[PMKID_LEN];
516 
517 	for (entry = pmksa->pmksa; entry; entry = entry->next) {
518 		if (os_memcmp(entry->spa, spa, ETH_ALEN) != 0)
519 			continue;
520 		if (wpa_key_mgmt_sae(entry->akmp) ||
521 		    wpa_key_mgmt_fils(entry->akmp)) {
522 			if (os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0)
523 				return entry;
524 			continue;
525 		}
526 		rsn_pmkid(entry->pmk, entry->pmk_len, aa, spa, new_pmkid,
527 			  entry->akmp);
528 		if (os_memcmp(new_pmkid, pmkid, PMKID_LEN) == 0)
529 			return entry;
530 	}
531 	return NULL;
532 }
533 
534 
535 /**
536  * pmksa_cache_auth_init - Initialize PMKSA cache
537  * @free_cb: Callback function to be called when a PMKSA cache entry is freed
538  * @ctx: Context pointer for free_cb function
539  * Returns: Pointer to PMKSA cache data or %NULL on failure
540  */
541 struct rsn_pmksa_cache *
pmksa_cache_auth_init(void (* free_cb)(struct rsn_pmksa_cache_entry * entry,void * ctx),void * ctx)542 pmksa_cache_auth_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
543 				      void *ctx), void *ctx)
544 {
545 	struct rsn_pmksa_cache *pmksa;
546 
547 	pmksa = os_zalloc(sizeof(*pmksa));
548 	if (pmksa) {
549 		pmksa->free_cb = free_cb;
550 		pmksa->ctx = ctx;
551 	}
552 
553 	return pmksa;
554 }
555 
556 
das_attr_match(struct rsn_pmksa_cache_entry * entry,struct radius_das_attrs * attr)557 static int das_attr_match(struct rsn_pmksa_cache_entry *entry,
558 			  struct radius_das_attrs *attr)
559 {
560 	int match = 0;
561 
562 	if (attr->sta_addr) {
563 		if (os_memcmp(attr->sta_addr, entry->spa, ETH_ALEN) != 0)
564 			return 0;
565 		match++;
566 	}
567 
568 	if (attr->acct_multi_session_id) {
569 		char buf[20];
570 
571 		if (attr->acct_multi_session_id_len != 16)
572 			return 0;
573 		os_snprintf(buf, sizeof(buf), "%016llX",
574 			    (unsigned long long) entry->acct_multi_session_id);
575 		if (os_memcmp(attr->acct_multi_session_id, buf, 16) != 0)
576 			return 0;
577 		match++;
578 	}
579 
580 	if (attr->cui) {
581 		if (!entry->cui ||
582 		    attr->cui_len != wpabuf_len(entry->cui) ||
583 		    os_memcmp(attr->cui, wpabuf_head(entry->cui),
584 			      attr->cui_len) != 0)
585 			return 0;
586 		match++;
587 	}
588 
589 	if (attr->user_name) {
590 		if (!entry->identity ||
591 		    attr->user_name_len != entry->identity_len ||
592 		    os_memcmp(attr->user_name, entry->identity,
593 			      attr->user_name_len) != 0)
594 			return 0;
595 		match++;
596 	}
597 
598 	return match;
599 }
600 
601 
pmksa_cache_auth_radius_das_disconnect(struct rsn_pmksa_cache * pmksa,struct radius_das_attrs * attr)602 int pmksa_cache_auth_radius_das_disconnect(struct rsn_pmksa_cache *pmksa,
603 					   struct radius_das_attrs *attr)
604 {
605 	int found = 0;
606 	struct rsn_pmksa_cache_entry *entry, *prev;
607 
608 	if (attr->acct_session_id)
609 		return -1;
610 
611 	entry = pmksa->pmksa;
612 	while (entry) {
613 		if (das_attr_match(entry, attr)) {
614 			found++;
615 			prev = entry;
616 			entry = entry->next;
617 			pmksa_cache_free_entry(pmksa, prev);
618 			continue;
619 		}
620 		entry = entry->next;
621 	}
622 
623 	return found ? 0 : -1;
624 }
625 
626 
627 /**
628  * pmksa_cache_auth_list - Dump text list of entries in PMKSA cache
629  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
630  * @buf: Buffer for the list
631  * @len: Length of the buffer
632  * Returns: Number of bytes written to buffer
633  *
634  * This function is used to generate a text format representation of the
635  * current PMKSA cache contents for the ctrl_iface PMKSA command.
636  */
pmksa_cache_auth_list(struct rsn_pmksa_cache * pmksa,char * buf,size_t len)637 int pmksa_cache_auth_list(struct rsn_pmksa_cache *pmksa, char *buf, size_t len)
638 {
639 	int i, ret;
640 	char *pos = buf;
641 	struct rsn_pmksa_cache_entry *entry;
642 	struct os_reltime now;
643 
644 	os_get_reltime(&now);
645 	ret = os_snprintf(pos, buf + len - pos,
646 			  "Index / SPA / PMKID / expiration (in seconds) / opportunistic\n");
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 		ret = os_snprintf(pos, buf + len - pos, "%d " MACSTR " ",
654 				  i, MAC2STR(entry->spa));
655 		if (os_snprintf_error(buf + len - pos, ret))
656 			return pos - buf;
657 		pos += ret;
658 		pos += wpa_snprintf_hex(pos, buf + len - pos, entry->pmkid,
659 					PMKID_LEN);
660 		ret = os_snprintf(pos, buf + len - pos, " %d %d\n",
661 				  (int) (entry->expiration - now.sec),
662 				  entry->opportunistic);
663 		if (os_snprintf_error(buf + len - pos, ret))
664 			return pos - buf;
665 		pos += ret;
666 		entry = entry->next;
667 	}
668 	return pos - buf;
669 }
670 
671 
672 #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
673 #ifdef CONFIG_MESH
674 
675 /**
676  * pmksa_cache_auth_list_mesh - Dump text list of entries in PMKSA cache
677  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
678  * @addr: MAC address of the peer (NULL means any)
679  * @buf: Buffer for the list
680  * @len: Length of the buffer
681  * Returns: Number of bytes written to buffer
682  *
683  * This function is used to generate a text format representation of the
684  * current PMKSA cache contents for the ctrl_iface PMKSA_GET command to store
685  * in external storage.
686  */
pmksa_cache_auth_list_mesh(struct rsn_pmksa_cache * pmksa,const u8 * addr,char * buf,size_t len)687 int pmksa_cache_auth_list_mesh(struct rsn_pmksa_cache *pmksa, const u8 *addr,
688 			       char *buf, size_t len)
689 {
690 	int ret;
691 	char *pos, *end;
692 	struct rsn_pmksa_cache_entry *entry;
693 	struct os_reltime now;
694 
695 	pos = buf;
696 	end = buf + len;
697 	os_get_reltime(&now);
698 
699 
700 	/*
701 	 * Entry format:
702 	 * <BSSID> <PMKID> <PMK> <expiration in seconds>
703 	 */
704 	for (entry = pmksa->pmksa; entry; entry = entry->next) {
705 		if (addr && os_memcmp(entry->spa, addr, ETH_ALEN) != 0)
706 			continue;
707 
708 		ret = os_snprintf(pos, end - pos, MACSTR " ",
709 				  MAC2STR(entry->spa));
710 		if (os_snprintf_error(end - pos, ret))
711 			return 0;
712 		pos += ret;
713 
714 		pos += wpa_snprintf_hex(pos, end - pos, entry->pmkid,
715 					PMKID_LEN);
716 
717 		ret = os_snprintf(pos, end - pos, " ");
718 		if (os_snprintf_error(end - pos, ret))
719 			return 0;
720 		pos += ret;
721 
722 		pos += wpa_snprintf_hex(pos, end - pos, entry->pmk,
723 					entry->pmk_len);
724 
725 		ret = os_snprintf(pos, end - pos, " %d\n",
726 				  (int) (entry->expiration - now.sec));
727 		if (os_snprintf_error(end - pos, ret))
728 			return 0;
729 		pos += ret;
730 	}
731 
732 	return pos - buf;
733 }
734 
735 #endif /* CONFIG_MESH */
736 #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
737