00001
00016 #include "includes.h"
00017
00018 #include "hostapd.h"
00019 #include "common.h"
00020 #include "wpa.h"
00021 #include "eloop.h"
00022 #include "sha1.h"
00023 #include "ieee802_1x.h"
00024 #include "eapol_sm.h"
00025 #include "pmksa_cache.h"
00026
00027
00028 static const int pmksa_cache_max_entries = 1024;
00029 static const int dot11RSNAConfigPMKLifetime = 43200;
00030
00031 struct rsn_pmksa_cache {
00032 #define PMKID_HASH_SIZE 128
00033 #define PMKID_HASH(pmkid) (unsigned int) ((pmkid)[0] & 0x7f)
00034 struct rsn_pmksa_cache_entry *pmkid[PMKID_HASH_SIZE];
00035 struct rsn_pmksa_cache_entry *pmksa;
00036 int pmksa_count;
00037
00038 void (*free_cb)(struct rsn_pmksa_cache_entry *entry, void *ctx);
00039 void *ctx;
00040 };
00041
00042
00054 void rsn_pmkid(const u8 *pmk, size_t pmk_len, const u8 *aa, const u8 *spa,
00055 u8 *pmkid)
00056 {
00057 char *title = "PMK Name";
00058 const u8 *addr[3];
00059 const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
00060 unsigned char hash[SHA1_MAC_LEN];
00061
00062 addr[0] = (u8 *) title;
00063 addr[1] = aa;
00064 addr[2] = spa;
00065
00066 hmac_sha1_vector(pmk, pmk_len, 3, addr, len, hash);
00067 memcpy(pmkid, hash, PMKID_LEN);
00068 }
00069
00070
00071 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa);
00072
00073
00074 static void _pmksa_cache_free_entry(struct rsn_pmksa_cache_entry *entry)
00075 {
00076 if (entry == NULL)
00077 return;
00078 free(entry->identity);
00079 ieee802_1x_free_radius_class(&entry->radius_class);
00080 free(entry);
00081 }
00082
00083
00084 static void pmksa_cache_free_entry(struct rsn_pmksa_cache *pmksa,
00085 struct rsn_pmksa_cache_entry *entry)
00086 {
00087 struct rsn_pmksa_cache_entry *pos, *prev;
00088
00089 pmksa->pmksa_count--;
00090 pmksa->free_cb(entry, pmksa->ctx);
00091 pos = pmksa->pmkid[PMKID_HASH(entry->pmkid)];
00092 prev = NULL;
00093 while (pos) {
00094 if (pos == entry) {
00095 if (prev != NULL) {
00096 prev->hnext = pos->hnext;
00097 } else {
00098 pmksa->pmkid[PMKID_HASH(entry->pmkid)] =
00099 pos->hnext;
00100 }
00101 break;
00102 }
00103 prev = pos;
00104 pos = pos->hnext;
00105 }
00106
00107 pos = pmksa->pmksa;
00108 prev = NULL;
00109 while (pos) {
00110 if (pos == entry) {
00111 if (prev != NULL)
00112 prev->next = pos->next;
00113 else
00114 pmksa->pmksa = pos->next;
00115 break;
00116 }
00117 prev = pos;
00118 pos = pos->next;
00119 }
00120 _pmksa_cache_free_entry(entry);
00121 }
00122
00123
00124 static void pmksa_cache_expire(void *eloop_ctx, void *timeout_ctx)
00125 {
00126 struct rsn_pmksa_cache *pmksa = eloop_ctx;
00127 struct os_time now;
00128
00129 os_get_time(&now);
00130 while (pmksa->pmksa && pmksa->pmksa->expiration <= now.sec) {
00131 struct rsn_pmksa_cache_entry *entry = pmksa->pmksa;
00132 pmksa->pmksa = entry->next;
00133 wpa_printf(MSG_DEBUG, "RSN: expired PMKSA cache entry for "
00134 MACSTR, MAC2STR(entry->spa));
00135 pmksa_cache_free_entry(pmksa, entry);
00136 }
00137
00138 pmksa_cache_set_expiration(pmksa);
00139 }
00140
00141
00142 static void pmksa_cache_set_expiration(struct rsn_pmksa_cache *pmksa)
00143 {
00144 int sec;
00145 struct os_time now;
00146
00147 eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
00148 if (pmksa->pmksa == NULL)
00149 return;
00150 os_get_time(&now);
00151 sec = pmksa->pmksa->expiration - now.sec;
00152 if (sec < 0)
00153 sec = 0;
00154 eloop_register_timeout(sec + 1, 0, pmksa_cache_expire, pmksa, NULL);
00155 }
00156
00157
00158 static void pmksa_cache_from_eapol_data(struct rsn_pmksa_cache_entry *entry,
00159 struct eapol_state_machine *eapol)
00160 {
00161 if (eapol == NULL)
00162 return;
00163
00164 if (eapol->identity) {
00165 entry->identity = malloc(eapol->identity_len);
00166 if (entry->identity) {
00167 entry->identity_len = eapol->identity_len;
00168 memcpy(entry->identity, eapol->identity,
00169 eapol->identity_len);
00170 }
00171 }
00172
00173 ieee802_1x_copy_radius_class(&entry->radius_class,
00174 &eapol->radius_class);
00175
00176 entry->eap_type_authsrv = eapol->eap_type_authsrv;
00177 entry->vlan_id = eapol->sta->vlan_id;
00178 }
00179
00180
00181 void pmksa_cache_to_eapol_data(struct rsn_pmksa_cache_entry *entry,
00182 struct eapol_state_machine *eapol)
00183 {
00184 if (entry == NULL || eapol == NULL)
00185 return;
00186
00187 if (entry->identity) {
00188 free(eapol->identity);
00189 eapol->identity = malloc(entry->identity_len);
00190 if (eapol->identity) {
00191 eapol->identity_len = entry->identity_len;
00192 memcpy(eapol->identity, entry->identity,
00193 entry->identity_len);
00194 }
00195 wpa_hexdump_ascii(MSG_DEBUG, "STA identity from PMKSA",
00196 eapol->identity, eapol->identity_len);
00197 }
00198
00199 ieee802_1x_free_radius_class(&eapol->radius_class);
00200 ieee802_1x_copy_radius_class(&eapol->radius_class,
00201 &entry->radius_class);
00202 if (eapol->radius_class.attr) {
00203 wpa_printf(MSG_DEBUG, "Copied %lu Class attribute(s) from "
00204 "PMKSA", (unsigned long) eapol->radius_class.count);
00205 }
00206
00207 eapol->eap_type_authsrv = entry->eap_type_authsrv;
00208 eapol->sta->vlan_id = entry->vlan_id;
00209 }
00210
00211
00229 struct rsn_pmksa_cache_entry *
00230 pmksa_cache_add(struct rsn_pmksa_cache *pmksa, const u8 *pmk, size_t pmk_len,
00231 const u8 *aa, const u8 *spa, int session_timeout,
00232 struct eapol_state_machine *eapol)
00233 {
00234 struct rsn_pmksa_cache_entry *entry, *pos, *prev;
00235 struct os_time now;
00236
00237 if (pmk_len > PMK_LEN)
00238 return NULL;
00239
00240 entry = wpa_zalloc(sizeof(*entry));
00241 if (entry == NULL)
00242 return NULL;
00243 memcpy(entry->pmk, pmk, pmk_len);
00244 entry->pmk_len = pmk_len;
00245 rsn_pmkid(pmk, pmk_len, aa, spa, entry->pmkid);
00246 os_get_time(&now);
00247 entry->expiration = now.sec;
00248 if (session_timeout > 0)
00249 entry->expiration += session_timeout;
00250 else
00251 entry->expiration += dot11RSNAConfigPMKLifetime;
00252 entry->akmp = WPA_KEY_MGMT_IEEE8021X;
00253 memcpy(entry->spa, spa, ETH_ALEN);
00254 pmksa_cache_from_eapol_data(entry, eapol);
00255
00256
00257
00258 pos = pmksa_cache_get(pmksa, spa, NULL);
00259 if (pos)
00260 pmksa_cache_free_entry(pmksa, pos);
00261
00262 if (pmksa->pmksa_count >= pmksa_cache_max_entries && pmksa->pmksa) {
00263
00264 wpa_printf(MSG_DEBUG, "RSN: removed the oldest PMKSA cache "
00265 "entry (for " MACSTR ") to make room for new one",
00266 MAC2STR(pmksa->pmksa->spa));
00267 pmksa_cache_free_entry(pmksa, pmksa->pmksa);
00268 }
00269
00270
00271 pos = pmksa->pmksa;
00272 prev = NULL;
00273 while (pos) {
00274 if (pos->expiration > entry->expiration)
00275 break;
00276 prev = pos;
00277 pos = pos->next;
00278 }
00279 if (prev == NULL) {
00280 entry->next = pmksa->pmksa;
00281 pmksa->pmksa = entry;
00282 } else {
00283 entry->next = prev->next;
00284 prev->next = entry;
00285 }
00286 entry->hnext = pmksa->pmkid[PMKID_HASH(entry->pmkid)];
00287 pmksa->pmkid[PMKID_HASH(entry->pmkid)] = entry;
00288
00289 pmksa->pmksa_count++;
00290 wpa_printf(MSG_DEBUG, "RSN: added PMKSA cache entry for " MACSTR,
00291 MAC2STR(entry->spa));
00292 wpa_hexdump(MSG_DEBUG, "RSN: added PMKID", entry->pmkid, PMKID_LEN);
00293
00294 return entry;
00295 }
00296
00297
00303 void pmksa_cache_deinit(struct rsn_pmksa_cache *pmksa)
00304 {
00305 struct rsn_pmksa_cache_entry *entry, *prev;
00306 int i;
00307
00308 if (pmksa == NULL)
00309 return;
00310
00311 entry = pmksa->pmksa;
00312 while (entry) {
00313 prev = entry;
00314 entry = entry->next;
00315 _pmksa_cache_free_entry(prev);
00316 }
00317 eloop_cancel_timeout(pmksa_cache_expire, pmksa, NULL);
00318 for (i = 0; i < PMKID_HASH_SIZE; i++)
00319 pmksa->pmkid[i] = NULL;
00320 free(pmksa);
00321 }
00322
00323
00332 struct rsn_pmksa_cache_entry * pmksa_cache_get(struct rsn_pmksa_cache *pmksa,
00333 const u8 *spa, const u8 *pmkid)
00334 {
00335 struct rsn_pmksa_cache_entry *entry;
00336
00337 if (pmkid)
00338 entry = pmksa->pmkid[PMKID_HASH(pmkid)];
00339 else
00340 entry = pmksa->pmksa;
00341 while (entry) {
00342 if ((spa == NULL || memcmp(entry->spa, spa, ETH_ALEN) == 0) &&
00343 (pmkid == NULL ||
00344 memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0))
00345 return entry;
00346 entry = pmkid ? entry->hnext : entry->next;
00347 }
00348 return NULL;
00349 }
00350
00351
00359 struct rsn_pmksa_cache *
00360 pmksa_cache_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry,
00361 void *ctx), void *ctx)
00362 {
00363 struct rsn_pmksa_cache *pmksa;
00364
00365 pmksa = wpa_zalloc(sizeof(*pmksa));
00366 if (pmksa) {
00367 pmksa->free_cb = free_cb;
00368 pmksa->ctx = ctx;
00369 }
00370
00371 return pmksa;
00372 }
00373