18d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/*
28d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * WPA Supplicant - RSN PMKSA cache
340b07208ef4402da20a7f666835a62f6ec098e16Dmitry Shmidt * Copyright (c) 2004-2009, 2011-2015, Jouni Malinen <j@w1.fi>
48d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
5c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt * This software may be distributed under the terms of the BSD license.
6c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt * See README for more details.
78d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
88d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
98d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "includes.h"
108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "common.h"
128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "eloop.h"
138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "eapol_supp/eapol_supp_sm.h"
148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "wpa.h"
158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "wpa_i.h"
168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt#include "pmksa_cache.h"
178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
181d6bf427f4769edb60865a3999d01eeb8f8fcb19Dmitry Shmidt#if defined(IEEE8021X_EAPOL) && !defined(CONFIG_NO_WPA)
198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic const int pmksa_cache_max_entries = 32;
218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct rsn_pmksa_cache {
238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct rsn_pmksa_cache_entry *pmksa; /* PMKSA cache */
248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int pmksa_count; /* number of entries in PMKSA cache */
258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct wpa_sm *sm; /* TODO: get rid of this reference(?) */
268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	void (*free_cb)(struct rsn_pmksa_cache_entry *entry, void *ctx,
28d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			enum pmksa_free_reason reason);
298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	void *ctx;
308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt};
318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa);
348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void _pmksa_cache_free_entry(struct rsn_pmksa_cache_entry *entry)
378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
389ead16e203b81d44a2d84eadc2901ceeb7daf805Dmitry Shmidt	bin_clear_free(entry, sizeof(*entry));
398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void pmksa_cache_free_entry(struct rsn_pmksa_cache *pmksa,
438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				   struct rsn_pmksa_cache_entry *entry,
44d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt				   enum pmksa_free_reason reason)
458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
461f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_sm_remove_pmkid(pmksa->sm, entry->aa, entry->pmkid);
478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pmksa->pmksa_count--;
48d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt	pmksa->free_cb(entry, pmksa->ctx, reason);
498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	_pmksa_cache_free_entry(entry);
508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void pmksa_cache_expire(void *eloop_ctx, void *timeout_ctx)
548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct rsn_pmksa_cache *pmksa = eloop_ctx;
56fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	struct os_reltime now;
578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
58fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	os_get_reltime(&now);
598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	while (pmksa->pmksa && pmksa->pmksa->expiration <= now.sec) {
608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pmksa->pmksa = entry->next;
628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		wpa_printf(MSG_DEBUG, "RSN: expired PMKSA cache entry for "
638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			   MACSTR, MAC2STR(entry->aa));
64d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt		pmksa_cache_free_entry(pmksa, entry, PMKSA_EXPIRE);
658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pmksa_cache_set_expiration(pmksa);
688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void pmksa_cache_reauth(void *eloop_ctx, void *timeout_ctx)
728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct rsn_pmksa_cache *pmksa = eloop_ctx;
748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pmksa->sm->cur_pmksa = NULL;
758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eapol_sm_request_reauth(pmksa->sm->eapol);
768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa)
808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int sec;
828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct rsn_pmksa_cache_entry *entry;
83fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	struct os_reltime now;
848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_cancel_timeout(pmksa_cache_reauth, pmksa, NULL);
878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (pmksa->pmksa == NULL)
888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
89fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	os_get_reltime(&now);
908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	sec = pmksa->pmksa->expiration - now.sec;
918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (sec < 0)
928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		sec = 0;
938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	eloop_register_timeout(sec + 1, 0, pmksa_cache_expire, pmksa, NULL);
948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	entry = pmksa->sm->cur_pmksa ? pmksa->sm->cur_pmksa :
96c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		pmksa_cache_get(pmksa, pmksa->sm->bssid, NULL, NULL);
978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (entry) {
988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		sec = pmksa->pmksa->reauth_time - now.sec;
998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (sec < 0)
1008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			sec = 0;
1018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		eloop_register_timeout(sec, 0, pmksa_cache_reauth, pmksa,
1028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				       NULL);
1038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
1048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
1058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
1088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * pmksa_cache_add - Add a PMKSA cache entry
1098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
1108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @pmk: The new pairwise master key
1118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @pmk_len: PMK length in bytes, usually PMK_LEN (32)
11257c2d39d85825f38c5fdac9b73bb0088406ffc85Dmitry Shmidt * @pmkid: Calculated PMKID
113fb45fd5cfed8bdccd0859c7fc05449fc187e2d06Dmitry Shmidt * @kck: Key confirmation key or %NULL if not yet derived
114fb45fd5cfed8bdccd0859c7fc05449fc187e2d06Dmitry Shmidt * @kck_len: KCK length in bytes
1158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @aa: Authenticator address
1168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @spa: Supplicant address
1178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @network_ctx: Network configuration context for this PMK
1188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @akmp: WPA_KEY_MGMT_* used in key derivation
1198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: Pointer to the added PMKSA cache entry or %NULL on error
1208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
1218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * This function create a PMKSA entry for a new PMK and adds it to the PMKSA
1228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * cache. If an old entry is already in the cache for the same Authenticator,
1238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * this entry will be replaced with the new entry. PMKID will be calculated
1248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * based on the PMK and the driver interface is notified of the new PMKID.
1258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
1268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct rsn_pmksa_cache_entry *
1278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtpmksa_cache_add(struct rsn_pmksa_cache *pmksa, const u8 *pmk, size_t pmk_len,
12857c2d39d85825f38c5fdac9b73bb0088406ffc85Dmitry Shmidt		const u8 *pmkid, const u8 *kck, size_t kck_len,
1298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		const u8 *aa, const u8 *spa, void *network_ctx, int akmp)
1308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
1318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct rsn_pmksa_cache_entry *entry, *pos, *prev;
132fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	struct os_reltime now;
1338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
134d80a401aed31d06f261efd19223cf55d1a2a8228Dmitry Shmidt	if (pmk_len > PMK_LEN_MAX)
1358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
1368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
137fb45fd5cfed8bdccd0859c7fc05449fc187e2d06Dmitry Shmidt	if (wpa_key_mgmt_suite_b(akmp) && !kck)
138fb45fd5cfed8bdccd0859c7fc05449fc187e2d06Dmitry Shmidt		return NULL;
139fb45fd5cfed8bdccd0859c7fc05449fc187e2d06Dmitry Shmidt
1408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	entry = os_zalloc(sizeof(*entry));
1418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (entry == NULL)
1428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
1438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(entry->pmk, pmk, pmk_len);
1448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	entry->pmk_len = pmk_len;
14557c2d39d85825f38c5fdac9b73bb0088406ffc85Dmitry Shmidt	if (pmkid)
14657c2d39d85825f38c5fdac9b73bb0088406ffc85Dmitry Shmidt		os_memcpy(entry->pmkid, pmkid, PMKID_LEN);
14757c2d39d85825f38c5fdac9b73bb0088406ffc85Dmitry Shmidt	else if (akmp == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
14840b07208ef4402da20a7f666835a62f6ec098e16Dmitry Shmidt		rsn_pmkid_suite_b_192(kck, kck_len, aa, spa, entry->pmkid);
14940b07208ef4402da20a7f666835a62f6ec098e16Dmitry Shmidt	else if (wpa_key_mgmt_suite_b(akmp))
150fb45fd5cfed8bdccd0859c7fc05449fc187e2d06Dmitry Shmidt		rsn_pmkid_suite_b(kck, kck_len, aa, spa, entry->pmkid);
151fb45fd5cfed8bdccd0859c7fc05449fc187e2d06Dmitry Shmidt	else
152fb45fd5cfed8bdccd0859c7fc05449fc187e2d06Dmitry Shmidt		rsn_pmkid(pmk, pmk_len, aa, spa, entry->pmkid,
153fb45fd5cfed8bdccd0859c7fc05449fc187e2d06Dmitry Shmidt			  wpa_key_mgmt_sha256(akmp));
154fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	os_get_reltime(&now);
1558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	entry->expiration = now.sec + pmksa->sm->dot11RSNAConfigPMKLifetime;
1568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	entry->reauth_time = now.sec + pmksa->sm->dot11RSNAConfigPMKLifetime *
1578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pmksa->sm->dot11RSNAConfigPMKReauthThreshold / 100;
1588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	entry->akmp = akmp;
1598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_memcpy(entry->aa, aa, ETH_ALEN);
1608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	entry->network_ctx = network_ctx;
1618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
1628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Replace an old entry for the same Authenticator (if found) with the
1638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	 * new entry */
1648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = pmksa->pmksa;
1658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	prev = NULL;
1668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	while (pos) {
1678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (os_memcmp(aa, pos->aa, ETH_ALEN) == 0) {
1688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (pos->pmk_len == pmk_len &&
169c28170251eb54dbf64a9074a07fee377587425b2Dmitry Shmidt			    os_memcmp_const(pos->pmk, pmk, pmk_len) == 0 &&
170c28170251eb54dbf64a9074a07fee377587425b2Dmitry Shmidt			    os_memcmp_const(pos->pmkid, entry->pmkid,
171c28170251eb54dbf64a9074a07fee377587425b2Dmitry Shmidt					    PMKID_LEN) == 0) {
1728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				wpa_printf(MSG_DEBUG, "WPA: reusing previous "
1738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   "PMKSA entry");
1748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				os_free(entry);
1758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				return pos;
1768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			}
1778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (prev == NULL)
1788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				pmksa->pmksa = pos->next;
1798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			else
1808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				prev->next = pos->next;
1811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
1821f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			/*
1831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			 * If OKC is used, there may be other PMKSA cache
1841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			 * entries based on the same PMK. These needs to be
1851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			 * flushed so that a new entry can be created based on
186f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt			 * the new PMK. Only clear other entries if they have a
187f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt			 * matching PMK and this PMK has been used successfully
188f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt			 * with the current AP, i.e., if opportunistic flag has
189f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt			 * been cleared in wpa_supplicant_key_neg_complete().
1901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			 */
191f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt			wpa_printf(MSG_DEBUG, "RSN: Replace PMKSA entry for "
192f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt				   "the current AP and any PMKSA cache entry "
193f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt				   "that was based on the old PMK");
194f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt			if (!pos->opportunistic)
195f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt				pmksa_cache_flush(pmksa, network_ctx, pos->pmk,
196f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt						  pos->pmk_len);
197f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt			pmksa_cache_free_entry(pmksa, pos, PMKSA_REPLACE);
1988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
1998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
2008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		prev = pos;
2018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos = pos->next;
2028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
2038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (pmksa->pmksa_count >= pmksa_cache_max_entries && pmksa->pmksa) {
2058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		/* Remove the oldest entry to make room for the new entry */
2068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos = pmksa->pmksa;
20761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
20861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		if (pos == pmksa->sm->cur_pmksa) {
20961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			/*
21061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			 * Never remove the current PMKSA cache entry, since
21161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			 * it's in use, and removing it triggers a needless
21261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			 * deauthentication.
21361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			 */
21461d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			pos = pos->next;
21561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			pmksa->pmksa->next = pos ? pos->next : NULL;
21661d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		} else
21761d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			pmksa->pmksa = pos->next;
21861d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt
21961d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		if (pos) {
22061d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt			wpa_printf(MSG_DEBUG, "RSN: removed the oldest idle "
22161d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt				   "PMKSA cache entry (for " MACSTR ") to "
22261d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt				   "make room for new one",
22361d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt				   MAC2STR(pos->aa));
224d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			pmksa_cache_free_entry(pmksa, pos, PMKSA_FREE);
22561d9df3e62aaa0e87ad05452fcb95142159a17b6Dmitry Shmidt		}
2268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
2278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* Add the new entry; order by expiration time */
2298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos = pmksa->pmksa;
2308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	prev = NULL;
2318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	while (pos) {
2328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (pos->expiration > entry->expiration)
2338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			break;
2348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		prev = pos;
2358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos = pos->next;
2368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
2378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (prev == NULL) {
2388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		entry->next = pmksa->pmksa;
2398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pmksa->pmksa = entry;
2408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pmksa_cache_set_expiration(pmksa);
2418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	} else {
2428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		entry->next = prev->next;
2438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		prev->next = entry;
2448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
2458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pmksa->pmksa_count++;
246c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt	wpa_printf(MSG_DEBUG, "RSN: Added PMKSA cache entry for " MACSTR
247c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		   " network_ctx=%p", MAC2STR(entry->aa), network_ctx);
2488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	wpa_sm_add_pmkid(pmksa->sm, entry->aa, entry->pmkid);
2498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2508d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return entry;
2518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
2528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
2548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
2551f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt * pmksa_cache_flush - Flush PMKSA cache entries for a specific network
2561f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
2571f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt * @network_ctx: Network configuration context or %NULL to flush all entries
258f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt * @pmk: PMK to match for or %NYLL to match all PMKs
259f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt * @pmk_len: PMK length
2601f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt */
261f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidtvoid pmksa_cache_flush(struct rsn_pmksa_cache *pmksa, void *network_ctx,
262f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt		       const u8 *pmk, size_t pmk_len)
2631f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt{
2641f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	struct rsn_pmksa_cache_entry *entry, *prev = NULL, *tmp;
2651f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	int removed = 0;
2661f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
2671f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	entry = pmksa->pmksa;
2681f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	while (entry) {
269f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt		if ((entry->network_ctx == network_ctx ||
270f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt		     network_ctx == NULL) &&
271f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt		    (pmk == NULL ||
272f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt		     (pmk_len == entry->pmk_len &&
273f7e0a9905988e62e4f70fed8b795722abeab719bDmitry Shmidt		      os_memcmp(pmk, entry->pmk, pmk_len) == 0))) {
2741f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			wpa_printf(MSG_DEBUG, "RSN: Flush PMKSA cache entry "
2751f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				   "for " MACSTR, MAC2STR(entry->aa));
2761f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			if (prev)
2771f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				prev->next = entry->next;
2781f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			else
2791f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt				pmksa->pmksa = entry->next;
2801f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			tmp = entry;
2811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			entry = entry->next;
282d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt			pmksa_cache_free_entry(pmksa, tmp, PMKSA_FREE);
2831f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			removed++;
2841f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		} else {
2851f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			prev = entry;
2861f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt			entry = entry->next;
2871f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		}
2881f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	}
2891f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	if (removed)
2901f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt		pmksa_cache_set_expiration(pmksa);
2911f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt}
2921f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
2931f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt
2941f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt/**
2958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * pmksa_cache_deinit - Free all entries in PMKSA cache
2968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
2978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
2988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid pmksa_cache_deinit(struct rsn_pmksa_cache *pmksa)
2998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
3008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct rsn_pmksa_cache_entry *entry, *prev;
3018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (pmksa == NULL)
3038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
3048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	entry = pmksa->pmksa;
3068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pmksa->pmksa = NULL;
3078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	while (entry) {
3088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		prev = entry;
3098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		entry = entry->next;
3108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		os_free(prev);
3118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
3128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pmksa_cache_set_expiration(pmksa);
3138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	os_free(pmksa);
3148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
3158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
3188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * pmksa_cache_get - Fetch a PMKSA cache entry
3198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
3208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @aa: Authenticator address or %NULL to match any
3218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @pmkid: PMKID or %NULL to match any
322c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt * @network_ctx: Network context or %NULL to match any
3238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: Pointer to PMKSA cache entry or %NULL if no match was found
3248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
3258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct rsn_pmksa_cache_entry * pmksa_cache_get(struct rsn_pmksa_cache *pmksa,
326c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt					       const u8 *aa, const u8 *pmkid,
327c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt					       const void *network_ctx)
3288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
3298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
3308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	while (entry) {
3318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if ((aa == NULL || os_memcmp(entry->aa, aa, ETH_ALEN) == 0) &&
3328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		    (pmkid == NULL ||
333c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		     os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0) &&
334c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		    (network_ctx == NULL || network_ctx == entry->network_ctx))
3358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return entry;
3368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		entry = entry->next;
3378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
3388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NULL;
3398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
3408d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3418d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3428d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstatic struct rsn_pmksa_cache_entry *
3438d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtpmksa_cache_clone_entry(struct rsn_pmksa_cache *pmksa,
3448d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			const struct rsn_pmksa_cache_entry *old_entry,
3458d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			const u8 *aa)
3468d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
3478d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct rsn_pmksa_cache_entry *new_entry;
3488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	new_entry = pmksa_cache_add(pmksa, old_entry->pmk, old_entry->pmk_len,
35057c2d39d85825f38c5fdac9b73bb0088406ffc85Dmitry Shmidt				    NULL, NULL, 0,
3518d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				    aa, pmksa->sm->own_addr,
3528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				    old_entry->network_ctx, old_entry->akmp);
3538d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (new_entry == NULL)
3548d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
3558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	/* TODO: reorder entries based on expiration time? */
3578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	new_entry->expiration = old_entry->expiration;
3588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	new_entry->opportunistic = 1;
3598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3608d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return new_entry;
3618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
3628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3648d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
3658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * pmksa_cache_get_opportunistic - Try to get an opportunistic PMKSA entry
3668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
3678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @network_ctx: Network configuration context
3688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @aa: Authenticator address for the new AP
3698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: Pointer to a new PMKSA cache entry or %NULL if not available
3708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
3718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Try to create a new PMKSA cache entry opportunistically by guessing that the
3728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * new AP is sharing the same PMK as another AP that has the same SSID and has
3738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * already an entry in PMKSA cache.
3748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
3758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct rsn_pmksa_cache_entry *
3768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtpmksa_cache_get_opportunistic(struct rsn_pmksa_cache *pmksa, void *network_ctx,
3778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			      const u8 *aa)
3788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
3798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
3808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3811f69aa52ea2e0a73ac502565df8c666ee49cab6aDmitry Shmidt	wpa_printf(MSG_DEBUG, "RSN: Consider " MACSTR " for OKC", MAC2STR(aa));
3828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (network_ctx == NULL)
3838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
3848d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	while (entry) {
3858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		if (entry->network_ctx == network_ctx) {
3868d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			entry = pmksa_cache_clone_entry(pmksa, entry, aa);
3878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			if (entry) {
3888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				wpa_printf(MSG_DEBUG, "RSN: added "
3898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   "opportunistic PMKSA cache entry "
3908d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					   "for " MACSTR, MAC2STR(aa));
3918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			}
3928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return entry;
3938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		}
3948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		entry = entry->next;
3958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
3968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return NULL;
3978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
3988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
3998d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
4018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * pmksa_cache_get_current - Get the current used PMKSA entry
4028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @sm: Pointer to WPA state machine data from wpa_sm_init()
4038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: Pointer to the current PMKSA cache entry or %NULL if not available
4048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
4058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct rsn_pmksa_cache_entry * pmksa_cache_get_current(struct wpa_sm *sm)
4068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
4078d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (sm == NULL)
4088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return NULL;
4098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return sm->cur_pmksa;
4108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
4118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
4148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * pmksa_cache_clear_current - Clear the current PMKSA entry selection
4158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @sm: Pointer to WPA state machine data from wpa_sm_init()
4168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
4178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtvoid pmksa_cache_clear_current(struct wpa_sm *sm)
4188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
4198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (sm == NULL)
4208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return;
4218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	sm->cur_pmksa = NULL;
4228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
4238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4258d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
4268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * pmksa_cache_set_current - Set the current PMKSA entry selection
4278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @sm: Pointer to WPA state machine data from wpa_sm_init()
4288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @pmkid: PMKID for selecting PMKSA or %NULL if not used
4298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @bssid: BSSID for PMKSA or %NULL if not used
4308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @network_ctx: Network configuration context
4318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @try_opportunistic: Whether to allow opportunistic PMKSA caching
4328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: 0 if PMKSA was found or -1 if no matching entry was found
4338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
4348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint pmksa_cache_set_current(struct wpa_sm *sm, const u8 *pmkid,
4358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    const u8 *bssid, void *network_ctx,
4368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    int try_opportunistic)
4378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
4388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct rsn_pmksa_cache *pmksa = sm->pmksa;
439c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt	wpa_printf(MSG_DEBUG, "RSN: PMKSA cache search - network_ctx=%p "
440c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		   "try_opportunistic=%d", network_ctx, try_opportunistic);
441c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt	if (pmkid)
442c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		wpa_hexdump(MSG_DEBUG, "RSN: Search for PMKID",
443c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt			    pmkid, PMKID_LEN);
444c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt	if (bssid)
445c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		wpa_printf(MSG_DEBUG, "RSN: Search for BSSID " MACSTR,
446c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt			   MAC2STR(bssid));
447c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt
4488d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	sm->cur_pmksa = NULL;
4498d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (pmkid)
450c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		sm->cur_pmksa = pmksa_cache_get(pmksa, NULL, pmkid,
451c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt						network_ctx);
4528d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (sm->cur_pmksa == NULL && bssid)
453c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		sm->cur_pmksa = pmksa_cache_get(pmksa, bssid, NULL,
454c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt						network_ctx);
4558d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (sm->cur_pmksa == NULL && try_opportunistic && bssid)
4568d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		sm->cur_pmksa = pmksa_cache_get_opportunistic(pmksa,
4578d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt							      network_ctx,
4588d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt							      bssid);
4598d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (sm->cur_pmksa) {
460c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt		wpa_hexdump(MSG_DEBUG, "RSN: PMKSA cache entry found - PMKID",
4618d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			    sm->cur_pmksa->pmkid, PMKID_LEN);
4628d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return 0;
4638d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
464c5ec7f57ead87efa365800228aa0b09a12d9e6c4Dmitry Shmidt	wpa_printf(MSG_DEBUG, "RSN: No PMKSA cache entry found");
4658d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return -1;
4668d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
4678d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4688d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
4698d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
4708d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * pmksa_cache_list - Dump text list of entries in PMKSA cache
4718d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init()
4728d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @buf: Buffer for the list
4738d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @len: Length of the buffer
4748d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: number of bytes written to buffer
4758d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt *
4768d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * This function is used to generate a text format representation of the
4778d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * current PMKSA cache contents for the ctrl_iface PMKSA command.
4788d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
4798d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtint pmksa_cache_list(struct rsn_pmksa_cache *pmksa, char *buf, size_t len)
4808d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
4818d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	int i, ret;
4828d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	char *pos = buf;
4838d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct rsn_pmksa_cache_entry *entry;
484fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	struct os_reltime now;
4858d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
486fb79edc9df1f20461e90e478363d207348213d35Dmitry Shmidt	os_get_reltime(&now);
4878d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	ret = os_snprintf(pos, buf + len - pos,
4888d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  "Index / AA / PMKID / expiration (in seconds) / "
4898d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			  "opportunistic\n");
490fb45fd5cfed8bdccd0859c7fc05449fc187e2d06Dmitry Shmidt	if (os_snprintf_error(buf + len - pos, ret))
4918d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		return pos - buf;
4928d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pos += ret;
4938d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	i = 0;
4948d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	entry = pmksa->pmksa;
4958d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	while (entry) {
4968d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		i++;
4978d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = os_snprintf(pos, buf + len - pos, "%d " MACSTR " ",
4988d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  i, MAC2STR(entry->aa));
499fb45fd5cfed8bdccd0859c7fc05449fc187e2d06Dmitry Shmidt		if (os_snprintf_error(buf + len - pos, ret))
5008d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return pos - buf;
5018d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos += ret;
5028d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos += wpa_snprintf_hex(pos, buf + len - pos, entry->pmkid,
5038d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt					PMKID_LEN);
5048d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		ret = os_snprintf(pos, buf + len - pos, " %d %d\n",
5058d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  (int) (entry->expiration - now.sec),
5068d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt				  entry->opportunistic);
507fb45fd5cfed8bdccd0859c7fc05449fc187e2d06Dmitry Shmidt		if (os_snprintf_error(buf + len - pos, ret))
5088d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt			return pos - buf;
5098d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pos += ret;
5108d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		entry = entry->next;
5118d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
5128d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return pos - buf;
5138d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
5148d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5158d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5168d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt/**
5178d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * pmksa_cache_init - Initialize PMKSA cache
5188d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @free_cb: Callback function to be called when a PMKSA cache entry is freed
5198d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @ctx: Context pointer for free_cb function
5208d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * @sm: Pointer to WPA state machine data from wpa_sm_init()
5218d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt * Returns: Pointer to PMKSA cache data or %NULL on failure
5228d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt */
5238d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtstruct rsn_pmksa_cache *
5248d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidtpmksa_cache_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
525d5e4923d04122f81300fa68fb07d64ede28fd44dDmitry Shmidt				 void *ctx, enum pmksa_free_reason reason),
5268d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		 void *ctx, struct wpa_sm *sm)
5278d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt{
5288d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	struct rsn_pmksa_cache *pmksa;
5298d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5308d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	pmksa = os_zalloc(sizeof(*pmksa));
5318d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	if (pmksa) {
5328d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pmksa->free_cb = free_cb;
5338d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pmksa->ctx = ctx;
5348d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt		pmksa->sm = sm;
5358d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	}
5368d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
5378d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt	return pmksa;
5388d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt}
5398d520ff1dc2da35cdca849e982051b86468016d8Dmitry Shmidt
54034af306c42b7ccf956508e7cd23f0ba90606e360Dmitry Shmidt#endif /* IEEE8021X_EAPOL */
541