2 * hostapd - PMKSA cache for IEEE 802.11i RSN
3 * Copyright (c) 2004-2008, 2012-2015, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
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"
17 #include "ap_config.h"
18 #include "pmksa_cache_auth.h"
21 static const int pmksa_cache_max_entries = 1024;
22 static const int dot11RSNAConfigPMKLifetime = 43200;
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;
31 void (*free_cb)(struct rsn_pmksa_cache_entry *entry, void *ctx);
36 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa);
39 static void _pmksa_cache_free_entry(struct rsn_pmksa_cache_entry *entry)
41 os_free(entry->vlan_desc);
42 os_free(entry->identity);
43 wpabuf_free(entry->cui);
44 #ifndef CONFIG_NO_RADIUS
45 radius_free_class(&entry->radius_class);
46 #endif /* CONFIG_NO_RADIUS */
47 bin_clear_free(entry, sizeof(*entry));
51 void pmksa_cache_free_entry(struct rsn_pmksa_cache *pmksa,
52 struct rsn_pmksa_cache_entry *entry)
54 struct rsn_pmksa_cache_entry *pos, *prev;
58 pmksa->free_cb(entry, pmksa->ctx);
60 /* unlink from hash list */
61 hash = PMKID_HASH(entry->pmkid);
62 pos = pmksa->pmkid[hash];
67 prev->hnext = entry->hnext;
69 pmksa->pmkid[hash] = entry->hnext;
76 /* unlink from entry list */
82 prev->next = entry->next;
84 pmksa->pmksa = entry->next;
91 _pmksa_cache_free_entry(entry);
95 static void pmksa_cache_expire(void *eloop_ctx, void *timeout_ctx)
97 struct rsn_pmksa_cache *pmksa = eloop_ctx;
98 struct os_reltime now;
100 os_get_reltime(&now);
101 while (pmksa->pmksa && pmksa->pmksa->expiration <= now.sec) {
102 wpa_printf(MSG_DEBUG, "RSN: expired PMKSA cache entry for "
103 MACSTR, MAC2STR(pmksa->pmksa->spa));
104 pmksa_cache_free_entry(pmksa, pmksa->pmksa);
107 pmksa_cache_set_expiration(pmksa);
111 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa)
114 struct os_reltime now;
116 eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
117 if (pmksa->pmksa == NULL)
119 os_get_reltime(&now);
120 sec = pmksa->pmksa->expiration - now.sec;
123 eloop_register_timeout(sec + 1, 0, pmksa_cache_expire, pmksa, NULL);
127 static void pmksa_cache_from_eapol_data(struct rsn_pmksa_cache_entry *entry,
128 struct eapol_state_machine *eapol)
130 struct vlan_description *vlan_desc;
135 if (eapol->identity) {
136 entry->identity = os_malloc(eapol->identity_len);
137 if (entry->identity) {
138 entry->identity_len = eapol->identity_len;
139 os_memcpy(entry->identity, eapol->identity,
140 eapol->identity_len);
144 if (eapol->radius_cui)
145 entry->cui = wpabuf_dup(eapol->radius_cui);
147 #ifndef CONFIG_NO_RADIUS
148 radius_copy_class(&entry->radius_class, &eapol->radius_class);
149 #endif /* CONFIG_NO_RADIUS */
151 entry->eap_type_authsrv = eapol->eap_type_authsrv;
153 vlan_desc = ((struct sta_info *) eapol->sta)->vlan_desc;
154 if (vlan_desc && vlan_desc->notempty) {
155 entry->vlan_desc = os_zalloc(sizeof(struct vlan_description));
156 if (entry->vlan_desc)
157 *entry->vlan_desc = *vlan_desc;
159 entry->vlan_desc = NULL;
162 entry->acct_multi_session_id = eapol->acct_multi_session_id;
166 void pmksa_cache_to_eapol_data(struct hostapd_data *hapd,
167 struct rsn_pmksa_cache_entry *entry,
168 struct eapol_state_machine *eapol)
170 struct sta_info *sta;
172 if (entry == NULL || eapol == NULL)
175 if (entry->identity) {
176 os_free(eapol->identity);
177 eapol->identity = os_malloc(entry->identity_len);
178 if (eapol->identity) {
179 eapol->identity_len = entry->identity_len;
180 os_memcpy(eapol->identity, entry->identity,
181 entry->identity_len);
183 wpa_hexdump_ascii(MSG_DEBUG, "STA identity from PMKSA",
184 eapol->identity, eapol->identity_len);
188 wpabuf_free(eapol->radius_cui);
189 eapol->radius_cui = wpabuf_dup(entry->cui);
192 #ifndef CONFIG_NO_RADIUS
193 radius_free_class(&eapol->radius_class);
194 radius_copy_class(&eapol->radius_class, &entry->radius_class);
195 #endif /* CONFIG_NO_RADIUS */
196 if (eapol->radius_class.attr) {
197 wpa_printf(MSG_DEBUG, "Copied %lu Class attribute(s) from "
198 "PMKSA", (unsigned long) eapol->radius_class.count);
201 eapol->eap_type_authsrv = entry->eap_type_authsrv;
202 sta = (struct sta_info *) eapol->sta;
203 ap_sta_set_vlan(hapd, sta, entry->vlan_desc);
205 eapol->acct_multi_session_id = entry->acct_multi_session_id;
209 static void pmksa_cache_link_entry(struct rsn_pmksa_cache *pmksa,
210 struct rsn_pmksa_cache_entry *entry)
212 struct rsn_pmksa_cache_entry *pos, *prev;
215 /* Add the new entry; order by expiration time */
219 if (pos->expiration > entry->expiration)
225 entry->next = pmksa->pmksa;
226 pmksa->pmksa = entry;
228 entry->next = prev->next;
232 hash = PMKID_HASH(entry->pmkid);
233 entry->hnext = pmksa->pmkid[hash];
234 pmksa->pmkid[hash] = entry;
236 pmksa->pmksa_count++;
238 pmksa_cache_set_expiration(pmksa);
239 wpa_printf(MSG_DEBUG, "RSN: added PMKSA cache entry for " MACSTR,
240 MAC2STR(entry->spa));
241 wpa_hexdump(MSG_DEBUG, "RSN: added PMKID", entry->pmkid, PMKID_LEN);
246 * pmksa_cache_auth_add - Add a PMKSA cache entry
247 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
248 * @pmk: The new pairwise master key
249 * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
250 * @pmkid: Calculated PMKID
251 * @kck: Key confirmation key or %NULL if not yet derived
252 * @kck_len: KCK length in bytes
253 * @aa: Authenticator address
254 * @spa: Supplicant address
255 * @session_timeout: Session timeout
256 * @eapol: Pointer to EAPOL state machine data
257 * @akmp: WPA_KEY_MGMT_* used in key derivation
258 * Returns: Pointer to the added PMKSA cache entry or %NULL on error
260 * This function create a PMKSA entry for a new PMK and adds it to the PMKSA
261 * cache. If an old entry is already in the cache for the same Supplicant,
262 * this entry will be replaced with the new entry. PMKID will be calculated
265 struct rsn_pmksa_cache_entry *
266 pmksa_cache_auth_add(struct rsn_pmksa_cache *pmksa,
267 const u8 *pmk, size_t pmk_len, const u8 *pmkid,
268 const u8 *kck, size_t kck_len,
269 const u8 *aa, const u8 *spa, int session_timeout,
270 struct eapol_state_machine *eapol, int akmp)
272 struct rsn_pmksa_cache_entry *entry, *pos;
273 struct os_reltime now;
275 if (pmk_len > PMK_LEN_MAX)
278 if (wpa_key_mgmt_suite_b(akmp) && !kck)
281 entry = os_zalloc(sizeof(*entry));
284 os_memcpy(entry->pmk, pmk, pmk_len);
285 entry->pmk_len = pmk_len;
287 os_memcpy(entry->pmkid, pmkid, PMKID_LEN);
288 else if (akmp == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
289 rsn_pmkid_suite_b_192(kck, kck_len, aa, spa, entry->pmkid);
290 else if (wpa_key_mgmt_suite_b(akmp))
291 rsn_pmkid_suite_b(kck, kck_len, aa, spa, entry->pmkid);
293 rsn_pmkid(pmk, pmk_len, aa, spa, entry->pmkid,
294 wpa_key_mgmt_sha256(akmp));
295 os_get_reltime(&now);
296 entry->expiration = now.sec;
297 if (session_timeout > 0)
298 entry->expiration += session_timeout;
300 entry->expiration += dot11RSNAConfigPMKLifetime;
302 os_memcpy(entry->spa, spa, ETH_ALEN);
303 pmksa_cache_from_eapol_data(entry, eapol);
305 /* Replace an old entry for the same STA (if found) with the new entry
307 pos = pmksa_cache_auth_get(pmksa, spa, NULL);
309 pmksa_cache_free_entry(pmksa, pos);
311 if (pmksa->pmksa_count >= pmksa_cache_max_entries && pmksa->pmksa) {
312 /* Remove the oldest entry to make room for the new entry */
313 wpa_printf(MSG_DEBUG, "RSN: removed the oldest PMKSA cache "
314 "entry (for " MACSTR ") to make room for new one",
315 MAC2STR(pmksa->pmksa->spa));
316 pmksa_cache_free_entry(pmksa, pmksa->pmksa);
319 pmksa_cache_link_entry(pmksa, entry);
325 struct rsn_pmksa_cache_entry *
326 pmksa_cache_add_okc(struct rsn_pmksa_cache *pmksa,
327 const struct rsn_pmksa_cache_entry *old_entry,
328 const u8 *aa, const u8 *pmkid)
330 struct rsn_pmksa_cache_entry *entry;
332 entry = os_zalloc(sizeof(*entry));
335 os_memcpy(entry->pmkid, pmkid, PMKID_LEN);
336 os_memcpy(entry->pmk, old_entry->pmk, old_entry->pmk_len);
337 entry->pmk_len = old_entry->pmk_len;
338 entry->expiration = old_entry->expiration;
339 entry->akmp = old_entry->akmp;
340 os_memcpy(entry->spa, old_entry->spa, ETH_ALEN);
341 entry->opportunistic = 1;
342 if (old_entry->identity) {
343 entry->identity = os_malloc(old_entry->identity_len);
344 if (entry->identity) {
345 entry->identity_len = old_entry->identity_len;
346 os_memcpy(entry->identity, old_entry->identity,
347 old_entry->identity_len);
351 entry->cui = wpabuf_dup(old_entry->cui);
352 #ifndef CONFIG_NO_RADIUS
353 radius_copy_class(&entry->radius_class, &old_entry->radius_class);
354 #endif /* CONFIG_NO_RADIUS */
355 entry->eap_type_authsrv = old_entry->eap_type_authsrv;
356 if (old_entry->vlan_desc) {
357 entry->vlan_desc = os_zalloc(sizeof(struct vlan_description));
358 if (entry->vlan_desc)
359 *entry->vlan_desc = *old_entry->vlan_desc;
361 entry->vlan_desc = NULL;
363 entry->opportunistic = 1;
365 pmksa_cache_link_entry(pmksa, entry);
372 * pmksa_cache_auth_deinit - Free all entries in PMKSA cache
373 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
375 void pmksa_cache_auth_deinit(struct rsn_pmksa_cache *pmksa)
377 struct rsn_pmksa_cache_entry *entry, *prev;
383 entry = pmksa->pmksa;
387 _pmksa_cache_free_entry(prev);
389 eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
390 pmksa->pmksa_count = 0;
392 for (i = 0; i < PMKID_HASH_SIZE; i++)
393 pmksa->pmkid[i] = NULL;
399 * pmksa_cache_auth_get - Fetch a PMKSA cache entry
400 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
401 * @spa: Supplicant address or %NULL to match any
402 * @pmkid: PMKID or %NULL to match any
403 * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
405 struct rsn_pmksa_cache_entry *
406 pmksa_cache_auth_get(struct rsn_pmksa_cache *pmksa,
407 const u8 *spa, const u8 *pmkid)
409 struct rsn_pmksa_cache_entry *entry;
412 for (entry = pmksa->pmkid[PMKID_HASH(pmkid)]; entry;
413 entry = entry->hnext) {
415 os_memcmp(entry->spa, spa, ETH_ALEN) == 0) &&
416 os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0)
420 for (entry = pmksa->pmksa; entry; entry = entry->next) {
422 os_memcmp(entry->spa, spa, ETH_ALEN) == 0)
432 * pmksa_cache_get_okc - Fetch a PMKSA cache entry using OKC
433 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
434 * @aa: Authenticator address
435 * @spa: Supplicant address
437 * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
439 * Use opportunistic key caching (OKC) to find a PMK for a supplicant.
441 struct rsn_pmksa_cache_entry * pmksa_cache_get_okc(
442 struct rsn_pmksa_cache *pmksa, const u8 *aa, const u8 *spa,
445 struct rsn_pmksa_cache_entry *entry;
446 u8 new_pmkid[PMKID_LEN];
448 for (entry = pmksa->pmksa; entry; entry = entry->next) {
449 if (os_memcmp(entry->spa, spa, ETH_ALEN) != 0)
451 rsn_pmkid(entry->pmk, entry->pmk_len, aa, spa, new_pmkid,
452 wpa_key_mgmt_sha256(entry->akmp));
453 if (os_memcmp(new_pmkid, pmkid, PMKID_LEN) == 0)
461 * pmksa_cache_auth_init - Initialize PMKSA cache
462 * @free_cb: Callback function to be called when a PMKSA cache entry is freed
463 * @ctx: Context pointer for free_cb function
464 * Returns: Pointer to PMKSA cache data or %NULL on failure
466 struct rsn_pmksa_cache *
467 pmksa_cache_auth_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
468 void *ctx), void *ctx)
470 struct rsn_pmksa_cache *pmksa;
472 pmksa = os_zalloc(sizeof(*pmksa));
474 pmksa->free_cb = free_cb;
482 static int das_attr_match(struct rsn_pmksa_cache_entry *entry,
483 struct radius_das_attrs *attr)
487 if (attr->sta_addr) {
488 if (os_memcmp(attr->sta_addr, entry->spa, ETH_ALEN) != 0)
493 if (attr->acct_multi_session_id) {
496 if (attr->acct_multi_session_id_len != 16)
498 os_snprintf(buf, sizeof(buf), "%016llX",
499 (unsigned long long) entry->acct_multi_session_id);
500 if (os_memcmp(attr->acct_multi_session_id, buf, 16) != 0)
507 attr->cui_len != wpabuf_len(entry->cui) ||
508 os_memcmp(attr->cui, wpabuf_head(entry->cui),
514 if (attr->user_name) {
515 if (!entry->identity ||
516 attr->user_name_len != entry->identity_len ||
517 os_memcmp(attr->user_name, entry->identity,
518 attr->user_name_len) != 0)
527 int pmksa_cache_auth_radius_das_disconnect(struct rsn_pmksa_cache *pmksa,
528 struct radius_das_attrs *attr)
531 struct rsn_pmksa_cache_entry *entry, *prev;
533 if (attr->acct_session_id)
536 entry = pmksa->pmksa;
538 if (das_attr_match(entry, attr)) {
542 pmksa_cache_free_entry(pmksa, prev);
548 return found ? 0 : -1;
553 * pmksa_cache_auth_list - Dump text list of entries in PMKSA cache
554 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
555 * @buf: Buffer for the list
556 * @len: Length of the buffer
557 * Returns: Number of bytes written to buffer
559 * This function is used to generate a text format representation of the
560 * current PMKSA cache contents for the ctrl_iface PMKSA command.
562 int pmksa_cache_auth_list(struct rsn_pmksa_cache *pmksa, char *buf, size_t len)
566 struct rsn_pmksa_cache_entry *entry;
567 struct os_reltime now;
569 os_get_reltime(&now);
570 ret = os_snprintf(pos, buf + len - pos,
571 "Index / SPA / PMKID / expiration (in seconds) / opportunistic\n");
572 if (os_snprintf_error(buf + len - pos, ret))
576 entry = pmksa->pmksa;
578 ret = os_snprintf(pos, buf + len - pos, "%d " MACSTR " ",
579 i, MAC2STR(entry->spa));
580 if (os_snprintf_error(buf + len - pos, ret))
583 pos += wpa_snprintf_hex(pos, buf + len - pos, entry->pmkid,
585 ret = os_snprintf(pos, buf + len - pos, " %d %d\n",
586 (int) (entry->expiration - now.sec),
587 entry->opportunistic);
588 if (os_snprintf_error(buf + len - pos, ret))