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