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->identity);
42 wpabuf_free(entry->cui);
43 #ifndef CONFIG_NO_RADIUS
44 radius_free_class(&entry->radius_class);
45 #endif /* CONFIG_NO_RADIUS */
46 bin_clear_free(entry, sizeof(*entry));
50 void pmksa_cache_free_entry(struct rsn_pmksa_cache *pmksa,
51 struct rsn_pmksa_cache_entry *entry)
53 struct rsn_pmksa_cache_entry *pos, *prev;
57 pmksa->free_cb(entry, pmksa->ctx);
59 /* unlink from hash list */
60 hash = PMKID_HASH(entry->pmkid);
61 pos = pmksa->pmkid[hash];
66 prev->hnext = entry->hnext;
68 pmksa->pmkid[hash] = entry->hnext;
75 /* unlink from entry list */
81 prev->next = entry->next;
83 pmksa->pmksa = entry->next;
90 _pmksa_cache_free_entry(entry);
94 static void pmksa_cache_expire(void *eloop_ctx, void *timeout_ctx)
96 struct rsn_pmksa_cache *pmksa = eloop_ctx;
97 struct os_reltime now;
100 while (pmksa->pmksa && pmksa->pmksa->expiration <= now.sec) {
101 wpa_printf(MSG_DEBUG, "RSN: expired PMKSA cache entry for "
102 MACSTR, MAC2STR(pmksa->pmksa->spa));
103 pmksa_cache_free_entry(pmksa, pmksa->pmksa);
106 pmksa_cache_set_expiration(pmksa);
110 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa)
113 struct os_reltime now;
115 eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
116 if (pmksa->pmksa == NULL)
118 os_get_reltime(&now);
119 sec = pmksa->pmksa->expiration - now.sec;
122 eloop_register_timeout(sec + 1, 0, pmksa_cache_expire, pmksa, NULL);
126 static void pmksa_cache_from_eapol_data(struct rsn_pmksa_cache_entry *entry,
127 struct eapol_state_machine *eapol)
132 if (eapol->identity) {
133 entry->identity = os_malloc(eapol->identity_len);
134 if (entry->identity) {
135 entry->identity_len = eapol->identity_len;
136 os_memcpy(entry->identity, eapol->identity,
137 eapol->identity_len);
141 if (eapol->radius_cui)
142 entry->cui = wpabuf_dup(eapol->radius_cui);
144 #ifndef CONFIG_NO_RADIUS
145 radius_copy_class(&entry->radius_class, &eapol->radius_class);
146 #endif /* CONFIG_NO_RADIUS */
148 entry->eap_type_authsrv = eapol->eap_type_authsrv;
149 entry->vlan_id = ((struct sta_info *) eapol->sta)->vlan_id;
151 entry->acct_multi_session_id_hi = eapol->acct_multi_session_id_hi;
152 entry->acct_multi_session_id_lo = eapol->acct_multi_session_id_lo;
156 void pmksa_cache_to_eapol_data(struct rsn_pmksa_cache_entry *entry,
157 struct eapol_state_machine *eapol)
159 if (entry == NULL || eapol == NULL)
162 if (entry->identity) {
163 os_free(eapol->identity);
164 eapol->identity = os_malloc(entry->identity_len);
165 if (eapol->identity) {
166 eapol->identity_len = entry->identity_len;
167 os_memcpy(eapol->identity, entry->identity,
168 entry->identity_len);
170 wpa_hexdump_ascii(MSG_DEBUG, "STA identity from PMKSA",
171 eapol->identity, eapol->identity_len);
175 wpabuf_free(eapol->radius_cui);
176 eapol->radius_cui = wpabuf_dup(entry->cui);
179 #ifndef CONFIG_NO_RADIUS
180 radius_free_class(&eapol->radius_class);
181 radius_copy_class(&eapol->radius_class, &entry->radius_class);
182 #endif /* CONFIG_NO_RADIUS */
183 if (eapol->radius_class.attr) {
184 wpa_printf(MSG_DEBUG, "Copied %lu Class attribute(s) from "
185 "PMKSA", (unsigned long) eapol->radius_class.count);
188 eapol->eap_type_authsrv = entry->eap_type_authsrv;
189 ((struct sta_info *) eapol->sta)->vlan_id = entry->vlan_id;
191 eapol->acct_multi_session_id_hi = entry->acct_multi_session_id_hi;
192 eapol->acct_multi_session_id_lo = entry->acct_multi_session_id_lo;
196 static void pmksa_cache_link_entry(struct rsn_pmksa_cache *pmksa,
197 struct rsn_pmksa_cache_entry *entry)
199 struct rsn_pmksa_cache_entry *pos, *prev;
202 /* Add the new entry; order by expiration time */
206 if (pos->expiration > entry->expiration)
212 entry->next = pmksa->pmksa;
213 pmksa->pmksa = entry;
215 entry->next = prev->next;
219 hash = PMKID_HASH(entry->pmkid);
220 entry->hnext = pmksa->pmkid[hash];
221 pmksa->pmkid[hash] = entry;
223 pmksa->pmksa_count++;
225 pmksa_cache_set_expiration(pmksa);
226 wpa_printf(MSG_DEBUG, "RSN: added PMKSA cache entry for " MACSTR,
227 MAC2STR(entry->spa));
228 wpa_hexdump(MSG_DEBUG, "RSN: added PMKID", entry->pmkid, PMKID_LEN);
233 * pmksa_cache_auth_add - Add a PMKSA cache entry
234 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
235 * @pmk: The new pairwise master key
236 * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
237 * @kck: Key confirmation key or %NULL if not yet derived
238 * @kck_len: KCK length in bytes
239 * @aa: Authenticator address
240 * @spa: Supplicant address
241 * @session_timeout: Session timeout
242 * @eapol: Pointer to EAPOL state machine data
243 * @akmp: WPA_KEY_MGMT_* used in key derivation
244 * Returns: Pointer to the added PMKSA cache entry or %NULL on error
246 * This function create a PMKSA entry for a new PMK and adds it to the PMKSA
247 * cache. If an old entry is already in the cache for the same Supplicant,
248 * this entry will be replaced with the new entry. PMKID will be calculated
251 struct rsn_pmksa_cache_entry *
252 pmksa_cache_auth_add(struct rsn_pmksa_cache *pmksa,
253 const u8 *pmk, size_t pmk_len,
254 const u8 *kck, size_t kck_len,
255 const u8 *aa, const u8 *spa, int session_timeout,
256 struct eapol_state_machine *eapol, int akmp)
258 struct rsn_pmksa_cache_entry *entry, *pos;
259 struct os_reltime now;
261 if (pmk_len > PMK_LEN)
264 if (wpa_key_mgmt_suite_b(akmp) && !kck)
267 entry = os_zalloc(sizeof(*entry));
270 os_memcpy(entry->pmk, pmk, pmk_len);
271 entry->pmk_len = pmk_len;
272 if (akmp == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
273 rsn_pmkid_suite_b_192(kck, kck_len, aa, spa, entry->pmkid);
274 else if (wpa_key_mgmt_suite_b(akmp))
275 rsn_pmkid_suite_b(kck, kck_len, aa, spa, entry->pmkid);
277 rsn_pmkid(pmk, pmk_len, aa, spa, entry->pmkid,
278 wpa_key_mgmt_sha256(akmp));
279 os_get_reltime(&now);
280 entry->expiration = now.sec;
281 if (session_timeout > 0)
282 entry->expiration += session_timeout;
284 entry->expiration += dot11RSNAConfigPMKLifetime;
286 os_memcpy(entry->spa, spa, ETH_ALEN);
287 pmksa_cache_from_eapol_data(entry, eapol);
289 /* Replace an old entry for the same STA (if found) with the new entry
291 pos = pmksa_cache_auth_get(pmksa, spa, NULL);
293 pmksa_cache_free_entry(pmksa, pos);
295 if (pmksa->pmksa_count >= pmksa_cache_max_entries && pmksa->pmksa) {
296 /* Remove the oldest entry to make room for the new entry */
297 wpa_printf(MSG_DEBUG, "RSN: removed the oldest PMKSA cache "
298 "entry (for " MACSTR ") to make room for new one",
299 MAC2STR(pmksa->pmksa->spa));
300 pmksa_cache_free_entry(pmksa, pmksa->pmksa);
303 pmksa_cache_link_entry(pmksa, entry);
309 struct rsn_pmksa_cache_entry *
310 pmksa_cache_add_okc(struct rsn_pmksa_cache *pmksa,
311 const struct rsn_pmksa_cache_entry *old_entry,
312 const u8 *aa, const u8 *pmkid)
314 struct rsn_pmksa_cache_entry *entry;
316 entry = os_zalloc(sizeof(*entry));
319 os_memcpy(entry->pmkid, pmkid, PMKID_LEN);
320 os_memcpy(entry->pmk, old_entry->pmk, old_entry->pmk_len);
321 entry->pmk_len = old_entry->pmk_len;
322 entry->expiration = old_entry->expiration;
323 entry->akmp = old_entry->akmp;
324 os_memcpy(entry->spa, old_entry->spa, ETH_ALEN);
325 entry->opportunistic = 1;
326 if (old_entry->identity) {
327 entry->identity = os_malloc(old_entry->identity_len);
328 if (entry->identity) {
329 entry->identity_len = old_entry->identity_len;
330 os_memcpy(entry->identity, old_entry->identity,
331 old_entry->identity_len);
335 entry->cui = wpabuf_dup(old_entry->cui);
336 #ifndef CONFIG_NO_RADIUS
337 radius_copy_class(&entry->radius_class, &old_entry->radius_class);
338 #endif /* CONFIG_NO_RADIUS */
339 entry->eap_type_authsrv = old_entry->eap_type_authsrv;
340 entry->vlan_id = old_entry->vlan_id;
341 entry->opportunistic = 1;
343 pmksa_cache_link_entry(pmksa, entry);
350 * pmksa_cache_auth_deinit - Free all entries in PMKSA cache
351 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
353 void pmksa_cache_auth_deinit(struct rsn_pmksa_cache *pmksa)
355 struct rsn_pmksa_cache_entry *entry, *prev;
361 entry = pmksa->pmksa;
365 _pmksa_cache_free_entry(prev);
367 eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
368 pmksa->pmksa_count = 0;
370 for (i = 0; i < PMKID_HASH_SIZE; i++)
371 pmksa->pmkid[i] = NULL;
377 * pmksa_cache_auth_get - Fetch a PMKSA cache entry
378 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
379 * @spa: Supplicant address or %NULL to match any
380 * @pmkid: PMKID or %NULL to match any
381 * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
383 struct rsn_pmksa_cache_entry *
384 pmksa_cache_auth_get(struct rsn_pmksa_cache *pmksa,
385 const u8 *spa, const u8 *pmkid)
387 struct rsn_pmksa_cache_entry *entry;
390 for (entry = pmksa->pmkid[PMKID_HASH(pmkid)]; entry;
391 entry = entry->hnext) {
393 os_memcmp(entry->spa, spa, ETH_ALEN) == 0) &&
394 os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0)
398 for (entry = pmksa->pmksa; entry; entry = entry->next) {
400 os_memcmp(entry->spa, spa, ETH_ALEN) == 0)
410 * pmksa_cache_get_okc - Fetch a PMKSA cache entry using OKC
411 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
412 * @aa: Authenticator address
413 * @spa: Supplicant address
415 * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
417 * Use opportunistic key caching (OKC) to find a PMK for a supplicant.
419 struct rsn_pmksa_cache_entry * pmksa_cache_get_okc(
420 struct rsn_pmksa_cache *pmksa, const u8 *aa, const u8 *spa,
423 struct rsn_pmksa_cache_entry *entry;
424 u8 new_pmkid[PMKID_LEN];
426 for (entry = pmksa->pmksa; entry; entry = entry->next) {
427 if (os_memcmp(entry->spa, spa, ETH_ALEN) != 0)
429 rsn_pmkid(entry->pmk, entry->pmk_len, aa, spa, new_pmkid,
430 wpa_key_mgmt_sha256(entry->akmp));
431 if (os_memcmp(new_pmkid, pmkid, PMKID_LEN) == 0)
439 * pmksa_cache_auth_init - Initialize PMKSA cache
440 * @free_cb: Callback function to be called when a PMKSA cache entry is freed
441 * @ctx: Context pointer for free_cb function
442 * Returns: Pointer to PMKSA cache data or %NULL on failure
444 struct rsn_pmksa_cache *
445 pmksa_cache_auth_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
446 void *ctx), void *ctx)
448 struct rsn_pmksa_cache *pmksa;
450 pmksa = os_zalloc(sizeof(*pmksa));
452 pmksa->free_cb = free_cb;
460 static int das_attr_match(struct rsn_pmksa_cache_entry *entry,
461 struct radius_das_attrs *attr)
465 if (attr->sta_addr) {
466 if (os_memcmp(attr->sta_addr, entry->spa, ETH_ALEN) != 0)
471 if (attr->acct_multi_session_id) {
474 if (attr->acct_multi_session_id_len != 17)
476 os_snprintf(buf, sizeof(buf), "%08X+%08X",
477 entry->acct_multi_session_id_hi,
478 entry->acct_multi_session_id_lo);
479 if (os_memcmp(attr->acct_multi_session_id, buf, 17) != 0)
486 attr->cui_len != wpabuf_len(entry->cui) ||
487 os_memcmp(attr->cui, wpabuf_head(entry->cui),
493 if (attr->user_name) {
494 if (!entry->identity ||
495 attr->user_name_len != entry->identity_len ||
496 os_memcmp(attr->user_name, entry->identity,
497 attr->user_name_len) != 0)
506 int pmksa_cache_auth_radius_das_disconnect(struct rsn_pmksa_cache *pmksa,
507 struct radius_das_attrs *attr)
510 struct rsn_pmksa_cache_entry *entry, *prev;
512 if (attr->acct_session_id)
515 entry = pmksa->pmksa;
517 if (das_attr_match(entry, attr)) {
521 pmksa_cache_free_entry(pmksa, prev);
527 return found ? 0 : -1;