2 * hostapd - PMKSA cache for IEEE 802.11i RSN
3 * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
22 #include "eapol_auth/eapol_auth_sm.h"
23 #include "pmksa_cache.h"
26 static const int pmksa_cache_max_entries
= 1024;
27 static const int dot11RSNAConfigPMKLifetime
= 43200;
29 struct rsn_pmksa_cache
{
30 #define PMKID_HASH_SIZE 128
31 #define PMKID_HASH(pmkid) (unsigned int) ((pmkid)[0] & 0x7f)
32 struct rsn_pmksa_cache_entry
*pmkid
[PMKID_HASH_SIZE
];
33 struct rsn_pmksa_cache_entry
*pmksa
;
36 void (*free_cb
)(struct rsn_pmksa_cache_entry
*entry
, void *ctx
);
41 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache
*pmksa
);
44 static void _pmksa_cache_free_entry(struct rsn_pmksa_cache_entry
*entry
)
48 os_free(entry
->identity
);
49 radius_free_class(&entry
->radius_class
);
54 static void pmksa_cache_free_entry(struct rsn_pmksa_cache
*pmksa
,
55 struct rsn_pmksa_cache_entry
*entry
)
57 struct rsn_pmksa_cache_entry
*pos
, *prev
;
60 pmksa
->free_cb(entry
, pmksa
->ctx
);
61 pos
= pmksa
->pmkid
[PMKID_HASH(entry
->pmkid
)];
66 prev
->hnext
= pos
->hnext
;
68 pmksa
->pmkid
[PMKID_HASH(entry
->pmkid
)] =
82 prev
->next
= pos
->next
;
84 pmksa
->pmksa
= pos
->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
;
100 while (pmksa
->pmksa
&& pmksa
->pmksa
->expiration
<= now
.sec
) {
101 struct rsn_pmksa_cache_entry
*entry
= pmksa
->pmksa
;
102 pmksa
->pmksa
= entry
->next
;
103 wpa_printf(MSG_DEBUG
, "RSN: expired PMKSA cache entry for "
104 MACSTR
, MAC2STR(entry
->spa
));
105 pmksa_cache_free_entry(pmksa
, entry
);
108 pmksa_cache_set_expiration(pmksa
);
112 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache
*pmksa
)
117 eloop_cancel_timeout(pmksa_cache_expire
, pmksa
, NULL
);
118 if (pmksa
->pmksa
== NULL
)
121 sec
= pmksa
->pmksa
->expiration
- now
.sec
;
124 eloop_register_timeout(sec
+ 1, 0, pmksa_cache_expire
, pmksa
, NULL
);
128 static void pmksa_cache_from_eapol_data(struct rsn_pmksa_cache_entry
*entry
,
129 struct eapol_state_machine
*eapol
)
134 if (eapol
->identity
) {
135 entry
->identity
= os_malloc(eapol
->identity_len
);
136 if (entry
->identity
) {
137 entry
->identity_len
= eapol
->identity_len
;
138 os_memcpy(entry
->identity
, eapol
->identity
,
139 eapol
->identity_len
);
143 radius_copy_class(&entry
->radius_class
, &eapol
->radius_class
);
145 entry
->eap_type_authsrv
= eapol
->eap_type_authsrv
;
146 entry
->vlan_id
= ((struct sta_info
*) eapol
->sta
)->vlan_id
;
150 void pmksa_cache_to_eapol_data(struct rsn_pmksa_cache_entry
*entry
,
151 struct eapol_state_machine
*eapol
)
153 if (entry
== NULL
|| eapol
== NULL
)
156 if (entry
->identity
) {
157 os_free(eapol
->identity
);
158 eapol
->identity
= os_malloc(entry
->identity_len
);
159 if (eapol
->identity
) {
160 eapol
->identity_len
= entry
->identity_len
;
161 os_memcpy(eapol
->identity
, entry
->identity
,
162 entry
->identity_len
);
164 wpa_hexdump_ascii(MSG_DEBUG
, "STA identity from PMKSA",
165 eapol
->identity
, eapol
->identity_len
);
168 radius_free_class(&eapol
->radius_class
);
169 radius_copy_class(&eapol
->radius_class
, &entry
->radius_class
);
170 if (eapol
->radius_class
.attr
) {
171 wpa_printf(MSG_DEBUG
, "Copied %lu Class attribute(s) from "
172 "PMKSA", (unsigned long) eapol
->radius_class
.count
);
175 eapol
->eap_type_authsrv
= entry
->eap_type_authsrv
;
176 ((struct sta_info
*) eapol
->sta
)->vlan_id
= entry
->vlan_id
;
180 static void pmksa_cache_link_entry(struct rsn_pmksa_cache
*pmksa
,
181 struct rsn_pmksa_cache_entry
*entry
)
183 struct rsn_pmksa_cache_entry
*pos
, *prev
;
185 /* Add the new entry; order by expiration time */
189 if (pos
->expiration
> entry
->expiration
)
195 entry
->next
= pmksa
->pmksa
;
196 pmksa
->pmksa
= entry
;
198 entry
->next
= prev
->next
;
201 entry
->hnext
= pmksa
->pmkid
[PMKID_HASH(entry
->pmkid
)];
202 pmksa
->pmkid
[PMKID_HASH(entry
->pmkid
)] = entry
;
204 pmksa
->pmksa_count
++;
205 wpa_printf(MSG_DEBUG
, "RSN: added PMKSA cache entry for " MACSTR
,
206 MAC2STR(entry
->spa
));
207 wpa_hexdump(MSG_DEBUG
, "RSN: added PMKID", entry
->pmkid
, PMKID_LEN
);
212 * pmksa_cache_auth_add - Add a PMKSA cache entry
213 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
214 * @pmk: The new pairwise master key
215 * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
216 * @aa: Authenticator address
217 * @spa: Supplicant address
218 * @session_timeout: Session timeout
219 * @eapol: Pointer to EAPOL state machine data
220 * @akmp: WPA_KEY_MGMT_* used in key derivation
221 * Returns: Pointer to the added PMKSA cache entry or %NULL on error
223 * This function create a PMKSA entry for a new PMK and adds it to the PMKSA
224 * cache. If an old entry is already in the cache for the same Supplicant,
225 * this entry will be replaced with the new entry. PMKID will be calculated
228 struct rsn_pmksa_cache_entry
*
229 pmksa_cache_auth_add(struct rsn_pmksa_cache
*pmksa
,
230 const u8
*pmk
, size_t pmk_len
,
231 const u8
*aa
, const u8
*spa
, int session_timeout
,
232 struct eapol_state_machine
*eapol
, int akmp
)
234 struct rsn_pmksa_cache_entry
*entry
, *pos
;
237 if (pmk_len
> PMK_LEN
)
240 entry
= os_zalloc(sizeof(*entry
));
243 os_memcpy(entry
->pmk
, pmk
, pmk_len
);
244 entry
->pmk_len
= pmk_len
;
245 rsn_pmkid(pmk
, pmk_len
, aa
, spa
, entry
->pmkid
,
246 wpa_key_mgmt_sha256(akmp
));
248 entry
->expiration
= now
.sec
;
249 if (session_timeout
> 0)
250 entry
->expiration
+= session_timeout
;
252 entry
->expiration
+= dot11RSNAConfigPMKLifetime
;
254 os_memcpy(entry
->spa
, spa
, ETH_ALEN
);
255 pmksa_cache_from_eapol_data(entry
, eapol
);
257 /* Replace an old entry for the same STA (if found) with the new entry
259 pos
= pmksa_cache_auth_get(pmksa
, spa
, NULL
);
261 pmksa_cache_free_entry(pmksa
, pos
);
263 if (pmksa
->pmksa_count
>= pmksa_cache_max_entries
&& pmksa
->pmksa
) {
264 /* Remove the oldest entry to make room for the new entry */
265 wpa_printf(MSG_DEBUG
, "RSN: removed the oldest PMKSA cache "
266 "entry (for " MACSTR
") to make room for new one",
267 MAC2STR(pmksa
->pmksa
->spa
));
268 pmksa_cache_free_entry(pmksa
, pmksa
->pmksa
);
271 pmksa_cache_link_entry(pmksa
, entry
);
277 struct rsn_pmksa_cache_entry
*
278 pmksa_cache_add_okc(struct rsn_pmksa_cache
*pmksa
,
279 const struct rsn_pmksa_cache_entry
*old_entry
,
280 const u8
*aa
, const u8
*pmkid
)
282 struct rsn_pmksa_cache_entry
*entry
;
284 entry
= os_zalloc(sizeof(*entry
));
287 os_memcpy(entry
->pmkid
, pmkid
, PMKID_LEN
);
288 os_memcpy(entry
->pmk
, old_entry
->pmk
, old_entry
->pmk_len
);
289 entry
->pmk_len
= old_entry
->pmk_len
;
290 entry
->expiration
= old_entry
->expiration
;
291 entry
->akmp
= old_entry
->akmp
;
292 os_memcpy(entry
->spa
, old_entry
->spa
, ETH_ALEN
);
293 entry
->opportunistic
= 1;
294 if (old_entry
->identity
) {
295 entry
->identity
= os_malloc(old_entry
->identity_len
);
296 if (entry
->identity
) {
297 entry
->identity_len
= old_entry
->identity_len
;
298 os_memcpy(entry
->identity
, old_entry
->identity
,
299 old_entry
->identity_len
);
302 radius_copy_class(&entry
->radius_class
, &old_entry
->radius_class
);
303 entry
->eap_type_authsrv
= old_entry
->eap_type_authsrv
;
304 entry
->vlan_id
= old_entry
->vlan_id
;
305 entry
->opportunistic
= 1;
307 pmksa_cache_link_entry(pmksa
, entry
);
314 * pmksa_cache_auth_deinit - Free all entries in PMKSA cache
315 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
317 void pmksa_cache_auth_deinit(struct rsn_pmksa_cache
*pmksa
)
319 struct rsn_pmksa_cache_entry
*entry
, *prev
;
325 entry
= pmksa
->pmksa
;
329 _pmksa_cache_free_entry(prev
);
331 eloop_cancel_timeout(pmksa_cache_expire
, pmksa
, NULL
);
332 for (i
= 0; i
< PMKID_HASH_SIZE
; i
++)
333 pmksa
->pmkid
[i
] = NULL
;
339 * pmksa_cache_auth_get - Fetch a PMKSA cache entry
340 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
341 * @spa: Supplicant address or %NULL to match any
342 * @pmkid: PMKID or %NULL to match any
343 * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
345 struct rsn_pmksa_cache_entry
*
346 pmksa_cache_auth_get(struct rsn_pmksa_cache
*pmksa
,
347 const u8
*spa
, const u8
*pmkid
)
349 struct rsn_pmksa_cache_entry
*entry
;
352 entry
= pmksa
->pmkid
[PMKID_HASH(pmkid
)];
354 entry
= pmksa
->pmksa
;
357 os_memcmp(entry
->spa
, spa
, ETH_ALEN
) == 0) &&
359 os_memcmp(entry
->pmkid
, pmkid
, PMKID_LEN
) == 0))
361 entry
= pmkid
? entry
->hnext
: entry
->next
;
368 * pmksa_cache_get_okc - Fetch a PMKSA cache entry using OKC
369 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_auth_init()
370 * @aa: Authenticator address
371 * @spa: Supplicant address
373 * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
375 * Use opportunistic key caching (OKC) to find a PMK for a supplicant.
377 struct rsn_pmksa_cache_entry
* pmksa_cache_get_okc(
378 struct rsn_pmksa_cache
*pmksa
, const u8
*aa
, const u8
*spa
,
381 struct rsn_pmksa_cache_entry
*entry
;
382 u8 new_pmkid
[PMKID_LEN
];
384 entry
= pmksa
->pmksa
;
386 if (os_memcmp(entry
->spa
, spa
, ETH_ALEN
) != 0)
388 rsn_pmkid(entry
->pmk
, entry
->pmk_len
, aa
, spa
, new_pmkid
,
389 wpa_key_mgmt_sha256(entry
->akmp
));
390 if (os_memcmp(new_pmkid
, pmkid
, PMKID_LEN
) == 0)
399 * pmksa_cache_auth_init - Initialize PMKSA cache
400 * @free_cb: Callback function to be called when a PMKSA cache entry is freed
401 * @ctx: Context pointer for free_cb function
402 * Returns: Pointer to PMKSA cache data or %NULL on failure
404 struct rsn_pmksa_cache
*
405 pmksa_cache_auth_init(void (*free_cb
)(struct rsn_pmksa_cache_entry
*entry
,
406 void *ctx
), void *ctx
)
408 struct rsn_pmksa_cache
*pmksa
;
410 pmksa
= os_zalloc(sizeof(*pmksa
));
412 pmksa
->free_cb
= free_cb
;