2 * WPA Supplicant - RSN PMKSA cache
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.
19 #include "eapol_supp/eapol_supp_sm.h"
22 #include "pmksa_cache.h"
24 #if defined(IEEE8021X_EAPOL) && !defined(CONFIG_NO_WPA2)
26 static const int pmksa_cache_max_entries
= 32;
28 struct rsn_pmksa_cache
{
29 struct rsn_pmksa_cache_entry
*pmksa
; /* PMKSA cache */
30 int pmksa_count
; /* number of entries in PMKSA cache */
31 struct wpa_sm
*sm
; /* TODO: get rid of this reference(?) */
33 void (*free_cb
)(struct rsn_pmksa_cache_entry
*entry
, void *ctx
,
39 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache
*pmksa
);
42 static void _pmksa_cache_free_entry(struct rsn_pmksa_cache_entry
*entry
)
48 static void pmksa_cache_free_entry(struct rsn_pmksa_cache
*pmksa
,
49 struct rsn_pmksa_cache_entry
*entry
,
53 pmksa
->free_cb(entry
, pmksa
->ctx
, replace
);
54 _pmksa_cache_free_entry(entry
);
58 static void pmksa_cache_expire(void *eloop_ctx
, void *timeout_ctx
)
60 struct rsn_pmksa_cache
*pmksa
= eloop_ctx
;
64 while (pmksa
->pmksa
&& pmksa
->pmksa
->expiration
<= now
.sec
) {
65 struct rsn_pmksa_cache_entry
*entry
= pmksa
->pmksa
;
66 pmksa
->pmksa
= entry
->next
;
67 wpa_printf(MSG_DEBUG
, "RSN: expired PMKSA cache entry for "
68 MACSTR
, MAC2STR(entry
->aa
));
69 wpa_sm_remove_pmkid(pmksa
->sm
, entry
->aa
, entry
->pmkid
);
70 pmksa_cache_free_entry(pmksa
, entry
, 0);
73 pmksa_cache_set_expiration(pmksa
);
77 static void pmksa_cache_reauth(void *eloop_ctx
, void *timeout_ctx
)
79 struct rsn_pmksa_cache
*pmksa
= eloop_ctx
;
80 pmksa
->sm
->cur_pmksa
= NULL
;
81 eapol_sm_request_reauth(pmksa
->sm
->eapol
);
85 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache
*pmksa
)
88 struct rsn_pmksa_cache_entry
*entry
;
91 eloop_cancel_timeout(pmksa_cache_expire
, pmksa
, NULL
);
92 eloop_cancel_timeout(pmksa_cache_reauth
, pmksa
, NULL
);
93 if (pmksa
->pmksa
== NULL
)
96 sec
= pmksa
->pmksa
->expiration
- now
.sec
;
99 eloop_register_timeout(sec
+ 1, 0, pmksa_cache_expire
, pmksa
, NULL
);
101 entry
= pmksa
->sm
->cur_pmksa
? pmksa
->sm
->cur_pmksa
:
102 pmksa_cache_get(pmksa
, pmksa
->sm
->bssid
, NULL
);
104 sec
= pmksa
->pmksa
->reauth_time
- now
.sec
;
107 eloop_register_timeout(sec
, 0, pmksa_cache_reauth
, pmksa
,
114 * pmksa_cache_add - Add a PMKSA cache entry
115 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
116 * @pmk: The new pairwise master key
117 * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
118 * @aa: Authenticator address
119 * @spa: Supplicant address
120 * @network_ctx: Network configuration context for this PMK
121 * @akmp: WPA_KEY_MGMT_* used in key derivation
122 * Returns: Pointer to the added PMKSA cache entry or %NULL on error
124 * This function create a PMKSA entry for a new PMK and adds it to the PMKSA
125 * cache. If an old entry is already in the cache for the same Authenticator,
126 * this entry will be replaced with the new entry. PMKID will be calculated
127 * based on the PMK and the driver interface is notified of the new PMKID.
129 struct rsn_pmksa_cache_entry
*
130 pmksa_cache_add(struct rsn_pmksa_cache
*pmksa
, const u8
*pmk
, size_t pmk_len
,
131 const u8
*aa
, const u8
*spa
, void *network_ctx
, int akmp
)
133 struct rsn_pmksa_cache_entry
*entry
, *pos
, *prev
;
136 if (pmk_len
> PMK_LEN
)
139 entry
= os_zalloc(sizeof(*entry
));
142 os_memcpy(entry
->pmk
, pmk
, pmk_len
);
143 entry
->pmk_len
= pmk_len
;
144 rsn_pmkid(pmk
, pmk_len
, aa
, spa
, entry
->pmkid
,
145 wpa_key_mgmt_sha256(akmp
));
147 entry
->expiration
= now
.sec
+ pmksa
->sm
->dot11RSNAConfigPMKLifetime
;
148 entry
->reauth_time
= now
.sec
+ pmksa
->sm
->dot11RSNAConfigPMKLifetime
*
149 pmksa
->sm
->dot11RSNAConfigPMKReauthThreshold
/ 100;
151 os_memcpy(entry
->aa
, aa
, ETH_ALEN
);
152 entry
->network_ctx
= network_ctx
;
154 /* Replace an old entry for the same Authenticator (if found) with the
159 if (os_memcmp(aa
, pos
->aa
, ETH_ALEN
) == 0) {
160 if (pos
->pmk_len
== pmk_len
&&
161 os_memcmp(pos
->pmk
, pmk
, pmk_len
) == 0 &&
162 os_memcmp(pos
->pmkid
, entry
->pmkid
, PMKID_LEN
) ==
164 wpa_printf(MSG_DEBUG
, "WPA: reusing previous "
170 pmksa
->pmksa
= pos
->next
;
172 prev
->next
= pos
->next
;
173 if (pos
== pmksa
->sm
->cur_pmksa
) {
174 /* We are about to replace the current PMKSA
175 * cache entry. This happens when the PMKSA
176 * caching attempt fails, so we don't want to
177 * force pmksa_cache_free_entry() to disconnect
178 * at this point. Let's just make sure the old
179 * PMKSA cache entry will not be used in the
182 wpa_printf(MSG_DEBUG
, "RSN: replacing current "
184 pmksa
->sm
->cur_pmksa
= NULL
;
186 wpa_printf(MSG_DEBUG
, "RSN: Replace PMKSA entry for "
188 pmksa_cache_free_entry(pmksa
, pos
, 1);
195 if (pmksa
->pmksa_count
>= pmksa_cache_max_entries
&& pmksa
->pmksa
) {
196 /* Remove the oldest entry to make room for the new entry */
198 pmksa
->pmksa
= pos
->next
;
199 wpa_printf(MSG_DEBUG
, "RSN: removed the oldest PMKSA cache "
200 "entry (for " MACSTR
") to make room for new one",
202 wpa_sm_remove_pmkid(pmksa
->sm
, pos
->aa
, pos
->pmkid
);
203 pmksa_cache_free_entry(pmksa
, pos
, 0);
206 /* Add the new entry; order by expiration time */
210 if (pos
->expiration
> entry
->expiration
)
216 entry
->next
= pmksa
->pmksa
;
217 pmksa
->pmksa
= entry
;
218 pmksa_cache_set_expiration(pmksa
);
220 entry
->next
= prev
->next
;
223 pmksa
->pmksa_count
++;
224 wpa_printf(MSG_DEBUG
, "RSN: added PMKSA cache entry for " MACSTR
,
226 wpa_sm_add_pmkid(pmksa
->sm
, entry
->aa
, entry
->pmkid
);
233 * pmksa_cache_flush - Flush PMKSA cache entries for a specific network
234 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
235 * @network_ctx: Network configuration context or %NULL to flush all entries
237 void pmksa_cache_flush(struct rsn_pmksa_cache
*pmksa
, void *network_ctx
)
239 struct rsn_pmksa_cache_entry
*entry
, *prev
= NULL
, *tmp
;
242 entry
= pmksa
->pmksa
;
244 if (entry
->network_ctx
== network_ctx
|| network_ctx
== NULL
) {
245 wpa_printf(MSG_DEBUG
, "RSN: Flush PMKSA cache entry "
246 "for " MACSTR
, MAC2STR(entry
->aa
));
248 prev
->next
= entry
->next
;
250 pmksa
->pmksa
= entry
->next
;
253 wpa_sm_remove_pmkid(pmksa
->sm
, tmp
->aa
, tmp
->pmkid
);
254 pmksa_cache_free_entry(pmksa
, tmp
, 0);
262 pmksa_cache_set_expiration(pmksa
);
267 * pmksa_cache_deinit - Free all entries in PMKSA cache
268 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
270 void pmksa_cache_deinit(struct rsn_pmksa_cache
*pmksa
)
272 struct rsn_pmksa_cache_entry
*entry
, *prev
;
277 entry
= pmksa
->pmksa
;
284 pmksa_cache_set_expiration(pmksa
);
290 * pmksa_cache_get - Fetch a PMKSA cache entry
291 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
292 * @aa: Authenticator address or %NULL to match any
293 * @pmkid: PMKID or %NULL to match any
294 * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
296 struct rsn_pmksa_cache_entry
* pmksa_cache_get(struct rsn_pmksa_cache
*pmksa
,
297 const u8
*aa
, const u8
*pmkid
)
299 struct rsn_pmksa_cache_entry
*entry
= pmksa
->pmksa
;
301 if ((aa
== NULL
|| os_memcmp(entry
->aa
, aa
, ETH_ALEN
) == 0) &&
303 os_memcmp(entry
->pmkid
, pmkid
, PMKID_LEN
) == 0))
311 static struct rsn_pmksa_cache_entry
*
312 pmksa_cache_clone_entry(struct rsn_pmksa_cache
*pmksa
,
313 const struct rsn_pmksa_cache_entry
*old_entry
,
316 struct rsn_pmksa_cache_entry
*new_entry
;
318 new_entry
= pmksa_cache_add(pmksa
, old_entry
->pmk
, old_entry
->pmk_len
,
319 aa
, pmksa
->sm
->own_addr
,
320 old_entry
->network_ctx
, old_entry
->akmp
);
321 if (new_entry
== NULL
)
324 /* TODO: reorder entries based on expiration time? */
325 new_entry
->expiration
= old_entry
->expiration
;
326 new_entry
->opportunistic
= 1;
333 * pmksa_cache_get_opportunistic - Try to get an opportunistic PMKSA entry
334 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
335 * @network_ctx: Network configuration context
336 * @aa: Authenticator address for the new AP
337 * Returns: Pointer to a new PMKSA cache entry or %NULL if not available
339 * Try to create a new PMKSA cache entry opportunistically by guessing that the
340 * new AP is sharing the same PMK as another AP that has the same SSID and has
341 * already an entry in PMKSA cache.
343 struct rsn_pmksa_cache_entry
*
344 pmksa_cache_get_opportunistic(struct rsn_pmksa_cache
*pmksa
, void *network_ctx
,
347 struct rsn_pmksa_cache_entry
*entry
= pmksa
->pmksa
;
349 wpa_printf(MSG_DEBUG
, "RSN: Consider " MACSTR
" for OKC", MAC2STR(aa
));
350 if (network_ctx
== NULL
)
353 if (entry
->network_ctx
== network_ctx
) {
354 entry
= pmksa_cache_clone_entry(pmksa
, entry
, aa
);
356 wpa_printf(MSG_DEBUG
, "RSN: added "
357 "opportunistic PMKSA cache entry "
358 "for " MACSTR
, MAC2STR(aa
));
369 * pmksa_cache_get_current - Get the current used PMKSA entry
370 * @sm: Pointer to WPA state machine data from wpa_sm_init()
371 * Returns: Pointer to the current PMKSA cache entry or %NULL if not available
373 struct rsn_pmksa_cache_entry
* pmksa_cache_get_current(struct wpa_sm
*sm
)
377 return sm
->cur_pmksa
;
382 * pmksa_cache_clear_current - Clear the current PMKSA entry selection
383 * @sm: Pointer to WPA state machine data from wpa_sm_init()
385 void pmksa_cache_clear_current(struct wpa_sm
*sm
)
389 sm
->cur_pmksa
= NULL
;
394 * pmksa_cache_set_current - Set the current PMKSA entry selection
395 * @sm: Pointer to WPA state machine data from wpa_sm_init()
396 * @pmkid: PMKID for selecting PMKSA or %NULL if not used
397 * @bssid: BSSID for PMKSA or %NULL if not used
398 * @network_ctx: Network configuration context
399 * @try_opportunistic: Whether to allow opportunistic PMKSA caching
400 * Returns: 0 if PMKSA was found or -1 if no matching entry was found
402 int pmksa_cache_set_current(struct wpa_sm
*sm
, const u8
*pmkid
,
403 const u8
*bssid
, void *network_ctx
,
404 int try_opportunistic
)
406 struct rsn_pmksa_cache
*pmksa
= sm
->pmksa
;
407 sm
->cur_pmksa
= NULL
;
409 sm
->cur_pmksa
= pmksa_cache_get(pmksa
, NULL
, pmkid
);
410 if (sm
->cur_pmksa
== NULL
&& bssid
)
411 sm
->cur_pmksa
= pmksa_cache_get(pmksa
, bssid
, NULL
);
412 if (sm
->cur_pmksa
== NULL
&& try_opportunistic
&& bssid
)
413 sm
->cur_pmksa
= pmksa_cache_get_opportunistic(pmksa
,
417 wpa_hexdump(MSG_DEBUG
, "RSN: PMKID",
418 sm
->cur_pmksa
->pmkid
, PMKID_LEN
);
426 * pmksa_cache_list - Dump text list of entries in PMKSA cache
427 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
428 * @buf: Buffer for the list
429 * @len: Length of the buffer
430 * Returns: number of bytes written to buffer
432 * This function is used to generate a text format representation of the
433 * current PMKSA cache contents for the ctrl_iface PMKSA command.
435 int pmksa_cache_list(struct rsn_pmksa_cache
*pmksa
, char *buf
, size_t len
)
439 struct rsn_pmksa_cache_entry
*entry
;
443 ret
= os_snprintf(pos
, buf
+ len
- pos
,
444 "Index / AA / PMKID / expiration (in seconds) / "
446 if (ret
< 0 || ret
>= buf
+ len
- pos
)
450 entry
= pmksa
->pmksa
;
453 ret
= os_snprintf(pos
, buf
+ len
- pos
, "%d " MACSTR
" ",
454 i
, MAC2STR(entry
->aa
));
455 if (ret
< 0 || ret
>= buf
+ len
- pos
)
458 pos
+= wpa_snprintf_hex(pos
, buf
+ len
- pos
, entry
->pmkid
,
460 ret
= os_snprintf(pos
, buf
+ len
- pos
, " %d %d\n",
461 (int) (entry
->expiration
- now
.sec
),
462 entry
->opportunistic
);
463 if (ret
< 0 || ret
>= buf
+ len
- pos
)
473 * pmksa_cache_init - Initialize PMKSA cache
474 * @free_cb: Callback function to be called when a PMKSA cache entry is freed
475 * @ctx: Context pointer for free_cb function
476 * @sm: Pointer to WPA state machine data from wpa_sm_init()
477 * Returns: Pointer to PMKSA cache data or %NULL on failure
479 struct rsn_pmksa_cache
*
480 pmksa_cache_init(void (*free_cb
)(struct rsn_pmksa_cache_entry
*entry
,
481 void *ctx
, int replace
),
482 void *ctx
, struct wpa_sm
*sm
)
484 struct rsn_pmksa_cache
*pmksa
;
486 pmksa
= os_zalloc(sizeof(*pmksa
));
488 pmksa
->free_cb
= free_cb
;
496 #endif /* IEEE8021X_EAPOL and !CONFIG_NO_WPA2 */