Clear OKC-based PMKSA caching entries if PMK is changed
[mech_eap.git] / src / rsn_supp / pmksa_cache.c
1 /*
2  * WPA Supplicant - RSN PMKSA cache
3  * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
4  *
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.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "eloop.h"
19 #include "eapol_supp/eapol_supp_sm.h"
20 #include "wpa.h"
21 #include "wpa_i.h"
22 #include "pmksa_cache.h"
23
24 #if defined(IEEE8021X_EAPOL) && !defined(CONFIG_NO_WPA2)
25
26 static const int pmksa_cache_max_entries = 32;
27
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(?) */
32
33         void (*free_cb)(struct rsn_pmksa_cache_entry *entry, void *ctx,
34                         int replace);
35         void *ctx;
36 };
37
38
39 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa);
40
41
42 static void _pmksa_cache_free_entry(struct rsn_pmksa_cache_entry *entry)
43 {
44         os_free(entry);
45 }
46
47
48 static void pmksa_cache_free_entry(struct rsn_pmksa_cache *pmksa,
49                                    struct rsn_pmksa_cache_entry *entry,
50                                    int replace)
51 {
52         pmksa->pmksa_count--;
53         pmksa->free_cb(entry, pmksa->ctx, replace);
54         _pmksa_cache_free_entry(entry);
55 }
56
57
58 static void pmksa_cache_expire(void *eloop_ctx, void *timeout_ctx)
59 {
60         struct rsn_pmksa_cache *pmksa = eloop_ctx;
61         struct os_time now;
62
63         os_get_time(&now);
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);
71         }
72
73         pmksa_cache_set_expiration(pmksa);
74 }
75
76
77 static void pmksa_cache_reauth(void *eloop_ctx, void *timeout_ctx)
78 {
79         struct rsn_pmksa_cache *pmksa = eloop_ctx;
80         pmksa->sm->cur_pmksa = NULL;
81         eapol_sm_request_reauth(pmksa->sm->eapol);
82 }
83
84
85 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa)
86 {
87         int sec;
88         struct rsn_pmksa_cache_entry *entry;
89         struct os_time now;
90
91         eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
92         eloop_cancel_timeout(pmksa_cache_reauth, pmksa, NULL);
93         if (pmksa->pmksa == NULL)
94                 return;
95         os_get_time(&now);
96         sec = pmksa->pmksa->expiration - now.sec;
97         if (sec < 0)
98                 sec = 0;
99         eloop_register_timeout(sec + 1, 0, pmksa_cache_expire, pmksa, NULL);
100
101         entry = pmksa->sm->cur_pmksa ? pmksa->sm->cur_pmksa :
102                 pmksa_cache_get(pmksa, pmksa->sm->bssid, NULL);
103         if (entry) {
104                 sec = pmksa->pmksa->reauth_time - now.sec;
105                 if (sec < 0)
106                         sec = 0;
107                 eloop_register_timeout(sec, 0, pmksa_cache_reauth, pmksa,
108                                        NULL);
109         }
110 }
111
112
113 /**
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
123  *
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.
128  */
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)
132 {
133         struct rsn_pmksa_cache_entry *entry, *pos, *prev;
134         struct os_time now;
135
136         if (pmk_len > PMK_LEN)
137                 return NULL;
138
139         entry = os_zalloc(sizeof(*entry));
140         if (entry == NULL)
141                 return NULL;
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));
146         os_get_time(&now);
147         entry->expiration = now.sec + pmksa->sm->dot11RSNAConfigPMKLifetime;
148         entry->reauth_time = now.sec + pmksa->sm->dot11RSNAConfigPMKLifetime *
149                 pmksa->sm->dot11RSNAConfigPMKReauthThreshold / 100;
150         entry->akmp = akmp;
151         os_memcpy(entry->aa, aa, ETH_ALEN);
152         entry->network_ctx = network_ctx;
153
154         /* Replace an old entry for the same Authenticator (if found) with the
155          * new entry */
156         pos = pmksa->pmksa;
157         prev = NULL;
158         while (pos) {
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) ==
163                             0) {
164                                 wpa_printf(MSG_DEBUG, "WPA: reusing previous "
165                                            "PMKSA entry");
166                                 os_free(entry);
167                                 return pos;
168                         }
169                         if (prev == NULL)
170                                 pmksa->pmksa = pos->next;
171                         else
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
180                                  * future.
181                                  */
182                                 wpa_printf(MSG_DEBUG, "RSN: replacing current "
183                                            "PMKSA entry");
184                                 pmksa->sm->cur_pmksa = NULL;
185                         }
186                         wpa_printf(MSG_DEBUG, "RSN: Replace PMKSA entry for "
187                                    "the current AP");
188                         pmksa_cache_free_entry(pmksa, pos, 1);
189
190                         /*
191                          * If OKC is used, there may be other PMKSA cache
192                          * entries based on the same PMK. These needs to be
193                          * flushed so that a new entry can be created based on
194                          * the new PMK.
195                          */
196                         pmksa_cache_flush(pmksa, network_ctx);
197                         break;
198                 }
199                 prev = pos;
200                 pos = pos->next;
201         }
202
203         if (pmksa->pmksa_count >= pmksa_cache_max_entries && pmksa->pmksa) {
204                 /* Remove the oldest entry to make room for the new entry */
205                 pos = pmksa->pmksa;
206                 pmksa->pmksa = pos->next;
207                 wpa_printf(MSG_DEBUG, "RSN: removed the oldest PMKSA cache "
208                            "entry (for " MACSTR ") to make room for new one",
209                            MAC2STR(pos->aa));
210                 wpa_sm_remove_pmkid(pmksa->sm, pos->aa, pos->pmkid);
211                 pmksa_cache_free_entry(pmksa, pos, 0);
212         }
213
214         /* Add the new entry; order by expiration time */
215         pos = pmksa->pmksa;
216         prev = NULL;
217         while (pos) {
218                 if (pos->expiration > entry->expiration)
219                         break;
220                 prev = pos;
221                 pos = pos->next;
222         }
223         if (prev == NULL) {
224                 entry->next = pmksa->pmksa;
225                 pmksa->pmksa = entry;
226                 pmksa_cache_set_expiration(pmksa);
227         } else {
228                 entry->next = prev->next;
229                 prev->next = entry;
230         }
231         pmksa->pmksa_count++;
232         wpa_printf(MSG_DEBUG, "RSN: added PMKSA cache entry for " MACSTR,
233                    MAC2STR(entry->aa));
234         wpa_sm_add_pmkid(pmksa->sm, entry->aa, entry->pmkid);
235
236         return entry;
237 }
238
239
240 /**
241  * pmksa_cache_flush - Flush PMKSA cache entries for a specific network
242  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
243  * @network_ctx: Network configuration context or %NULL to flush all entries
244  */
245 void pmksa_cache_flush(struct rsn_pmksa_cache *pmksa, void *network_ctx)
246 {
247         struct rsn_pmksa_cache_entry *entry, *prev = NULL, *tmp;
248         int removed = 0;
249
250         entry = pmksa->pmksa;
251         while (entry) {
252                 if (entry->network_ctx == network_ctx || network_ctx == NULL) {
253                         wpa_printf(MSG_DEBUG, "RSN: Flush PMKSA cache entry "
254                                    "for " MACSTR, MAC2STR(entry->aa));
255                         if (prev)
256                                 prev->next = entry->next;
257                         else
258                                 pmksa->pmksa = entry->next;
259                         tmp = entry;
260                         entry = entry->next;
261                         wpa_sm_remove_pmkid(pmksa->sm, tmp->aa, tmp->pmkid);
262                         pmksa_cache_free_entry(pmksa, tmp, 0);
263                         removed++;
264                 } else {
265                         prev = entry;
266                         entry = entry->next;
267                 }
268         }
269         if (removed)
270                 pmksa_cache_set_expiration(pmksa);
271 }
272
273
274 /**
275  * pmksa_cache_deinit - Free all entries in PMKSA cache
276  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
277  */
278 void pmksa_cache_deinit(struct rsn_pmksa_cache *pmksa)
279 {
280         struct rsn_pmksa_cache_entry *entry, *prev;
281
282         if (pmksa == NULL)
283                 return;
284
285         entry = pmksa->pmksa;
286         pmksa->pmksa = NULL;
287         while (entry) {
288                 prev = entry;
289                 entry = entry->next;
290                 os_free(prev);
291         }
292         pmksa_cache_set_expiration(pmksa);
293         os_free(pmksa);
294 }
295
296
297 /**
298  * pmksa_cache_get - Fetch a PMKSA cache entry
299  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
300  * @aa: Authenticator address or %NULL to match any
301  * @pmkid: PMKID or %NULL to match any
302  * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
303  */
304 struct rsn_pmksa_cache_entry * pmksa_cache_get(struct rsn_pmksa_cache *pmksa,
305                                                const u8 *aa, const u8 *pmkid)
306 {
307         struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
308         while (entry) {
309                 if ((aa == NULL || os_memcmp(entry->aa, aa, ETH_ALEN) == 0) &&
310                     (pmkid == NULL ||
311                      os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0))
312                         return entry;
313                 entry = entry->next;
314         }
315         return NULL;
316 }
317
318
319 static struct rsn_pmksa_cache_entry *
320 pmksa_cache_clone_entry(struct rsn_pmksa_cache *pmksa,
321                         const struct rsn_pmksa_cache_entry *old_entry,
322                         const u8 *aa)
323 {
324         struct rsn_pmksa_cache_entry *new_entry;
325
326         new_entry = pmksa_cache_add(pmksa, old_entry->pmk, old_entry->pmk_len,
327                                     aa, pmksa->sm->own_addr,
328                                     old_entry->network_ctx, old_entry->akmp);
329         if (new_entry == NULL)
330                 return NULL;
331
332         /* TODO: reorder entries based on expiration time? */
333         new_entry->expiration = old_entry->expiration;
334         new_entry->opportunistic = 1;
335
336         return new_entry;
337 }
338
339
340 /**
341  * pmksa_cache_get_opportunistic - Try to get an opportunistic PMKSA entry
342  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
343  * @network_ctx: Network configuration context
344  * @aa: Authenticator address for the new AP
345  * Returns: Pointer to a new PMKSA cache entry or %NULL if not available
346  *
347  * Try to create a new PMKSA cache entry opportunistically by guessing that the
348  * new AP is sharing the same PMK as another AP that has the same SSID and has
349  * already an entry in PMKSA cache.
350  */
351 struct rsn_pmksa_cache_entry *
352 pmksa_cache_get_opportunistic(struct rsn_pmksa_cache *pmksa, void *network_ctx,
353                               const u8 *aa)
354 {
355         struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
356
357         wpa_printf(MSG_DEBUG, "RSN: Consider " MACSTR " for OKC", MAC2STR(aa));
358         if (network_ctx == NULL)
359                 return NULL;
360         while (entry) {
361                 if (entry->network_ctx == network_ctx) {
362                         entry = pmksa_cache_clone_entry(pmksa, entry, aa);
363                         if (entry) {
364                                 wpa_printf(MSG_DEBUG, "RSN: added "
365                                            "opportunistic PMKSA cache entry "
366                                            "for " MACSTR, MAC2STR(aa));
367                         }
368                         return entry;
369                 }
370                 entry = entry->next;
371         }
372         return NULL;
373 }
374
375
376 /**
377  * pmksa_cache_get_current - Get the current used PMKSA entry
378  * @sm: Pointer to WPA state machine data from wpa_sm_init()
379  * Returns: Pointer to the current PMKSA cache entry or %NULL if not available
380  */
381 struct rsn_pmksa_cache_entry * pmksa_cache_get_current(struct wpa_sm *sm)
382 {
383         if (sm == NULL)
384                 return NULL;
385         return sm->cur_pmksa;
386 }
387
388
389 /**
390  * pmksa_cache_clear_current - Clear the current PMKSA entry selection
391  * @sm: Pointer to WPA state machine data from wpa_sm_init()
392  */
393 void pmksa_cache_clear_current(struct wpa_sm *sm)
394 {
395         if (sm == NULL)
396                 return;
397         sm->cur_pmksa = NULL;
398 }
399
400
401 /**
402  * pmksa_cache_set_current - Set the current PMKSA entry selection
403  * @sm: Pointer to WPA state machine data from wpa_sm_init()
404  * @pmkid: PMKID for selecting PMKSA or %NULL if not used
405  * @bssid: BSSID for PMKSA or %NULL if not used
406  * @network_ctx: Network configuration context
407  * @try_opportunistic: Whether to allow opportunistic PMKSA caching
408  * Returns: 0 if PMKSA was found or -1 if no matching entry was found
409  */
410 int pmksa_cache_set_current(struct wpa_sm *sm, const u8 *pmkid,
411                             const u8 *bssid, void *network_ctx,
412                             int try_opportunistic)
413 {
414         struct rsn_pmksa_cache *pmksa = sm->pmksa;
415         sm->cur_pmksa = NULL;
416         if (pmkid)
417                 sm->cur_pmksa = pmksa_cache_get(pmksa, NULL, pmkid);
418         if (sm->cur_pmksa == NULL && bssid)
419                 sm->cur_pmksa = pmksa_cache_get(pmksa, bssid, NULL);
420         if (sm->cur_pmksa == NULL && try_opportunistic && bssid)
421                 sm->cur_pmksa = pmksa_cache_get_opportunistic(pmksa,
422                                                               network_ctx,
423                                                               bssid);
424         if (sm->cur_pmksa) {
425                 wpa_hexdump(MSG_DEBUG, "RSN: PMKID",
426                             sm->cur_pmksa->pmkid, PMKID_LEN);
427                 return 0;
428         }
429         return -1;
430 }
431
432
433 /**
434  * pmksa_cache_list - Dump text list of entries in PMKSA cache
435  * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
436  * @buf: Buffer for the list
437  * @len: Length of the buffer
438  * Returns: number of bytes written to buffer
439  *
440  * This function is used to generate a text format representation of the
441  * current PMKSA cache contents for the ctrl_iface PMKSA command.
442  */
443 int pmksa_cache_list(struct rsn_pmksa_cache *pmksa, char *buf, size_t len)
444 {
445         int i, ret;
446         char *pos = buf;
447         struct rsn_pmksa_cache_entry *entry;
448         struct os_time now;
449
450         os_get_time(&now);
451         ret = os_snprintf(pos, buf + len - pos,
452                           "Index / AA / PMKID / expiration (in seconds) / "
453                           "opportunistic\n");
454         if (ret < 0 || ret >= buf + len - pos)
455                 return pos - buf;
456         pos += ret;
457         i = 0;
458         entry = pmksa->pmksa;
459         while (entry) {
460                 i++;
461                 ret = os_snprintf(pos, buf + len - pos, "%d " MACSTR " ",
462                                   i, MAC2STR(entry->aa));
463                 if (ret < 0 || ret >= buf + len - pos)
464                         return pos - buf;
465                 pos += ret;
466                 pos += wpa_snprintf_hex(pos, buf + len - pos, entry->pmkid,
467                                         PMKID_LEN);
468                 ret = os_snprintf(pos, buf + len - pos, " %d %d\n",
469                                   (int) (entry->expiration - now.sec),
470                                   entry->opportunistic);
471                 if (ret < 0 || ret >= buf + len - pos)
472                         return pos - buf;
473                 pos += ret;
474                 entry = entry->next;
475         }
476         return pos - buf;
477 }
478
479
480 /**
481  * pmksa_cache_init - Initialize PMKSA cache
482  * @free_cb: Callback function to be called when a PMKSA cache entry is freed
483  * @ctx: Context pointer for free_cb function
484  * @sm: Pointer to WPA state machine data from wpa_sm_init()
485  * Returns: Pointer to PMKSA cache data or %NULL on failure
486  */
487 struct rsn_pmksa_cache *
488 pmksa_cache_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
489                                  void *ctx, int replace),
490                  void *ctx, struct wpa_sm *sm)
491 {
492         struct rsn_pmksa_cache *pmksa;
493
494         pmksa = os_zalloc(sizeof(*pmksa));
495         if (pmksa) {
496                 pmksa->free_cb = free_cb;
497                 pmksa->ctx = ctx;
498                 pmksa->sm = sm;
499         }
500
501         return pmksa;
502 }
503
504 #endif /* IEEE8021X_EAPOL and !CONFIG_NO_WPA2 */