1 /* 2 * WPA Supplicant - RSN PMKSA cache 3 * Copyright (c) 2004-2009, 2011-2012, Jouni Malinen <j@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 enum pmksa_free_reason reason); 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 enum pmksa_free_reason reason) 45 { 46 wpa_sm_remove_pmkid(pmksa->sm, entry->aa, entry->pmkid); 47 pmksa->pmksa_count--; 48 pmksa->free_cb(entry, pmksa->ctx, reason); 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, PMKSA_EXPIRE); 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 wpa_printf(MSG_DEBUG, "RSN: Replace PMKSA entry for " 168 "the current AP"); 169 pmksa_cache_free_entry(pmksa, pos, PMKSA_REPLACE); 170 171 /* 172 * If OKC is used, there may be other PMKSA cache 173 * entries based on the same PMK. These needs to be 174 * flushed so that a new entry can be created based on 175 * the new PMK. 176 */ 177 pmksa_cache_flush(pmksa, network_ctx); 178 break; 179 } 180 prev = pos; 181 pos = pos->next; 182 } 183 184 if (pmksa->pmksa_count >= pmksa_cache_max_entries && pmksa->pmksa) { 185 /* Remove the oldest entry to make room for the new entry */ 186 pos = pmksa->pmksa; 187 188 if (pos == pmksa->sm->cur_pmksa) { 189 /* 190 * Never remove the current PMKSA cache entry, since 191 * it's in use, and removing it triggers a needless 192 * deauthentication. 193 */ 194 pos = pos->next; 195 pmksa->pmksa->next = pos ? pos->next : NULL; 196 } else 197 pmksa->pmksa = pos->next; 198 199 if (pos) { 200 wpa_printf(MSG_DEBUG, "RSN: removed the oldest idle " 201 "PMKSA cache entry (for " MACSTR ") to " 202 "make room for new one", 203 MAC2STR(pos->aa)); 204 pmksa_cache_free_entry(pmksa, pos, PMKSA_FREE); 205 } 206 } 207 208 /* Add the new entry; order by expiration time */ 209 pos = pmksa->pmksa; 210 prev = NULL; 211 while (pos) { 212 if (pos->expiration > entry->expiration) 213 break; 214 prev = pos; 215 pos = pos->next; 216 } 217 if (prev == NULL) { 218 entry->next = pmksa->pmksa; 219 pmksa->pmksa = entry; 220 pmksa_cache_set_expiration(pmksa); 221 } else { 222 entry->next = prev->next; 223 prev->next = entry; 224 } 225 pmksa->pmksa_count++; 226 wpa_printf(MSG_DEBUG, "RSN: Added PMKSA cache entry for " MACSTR 227 " network_ctx=%p", MAC2STR(entry->aa), network_ctx); 228 wpa_sm_add_pmkid(pmksa->sm, entry->aa, entry->pmkid); 229 230 return entry; 231 } 232 233 234 /** 235 * pmksa_cache_flush - Flush PMKSA cache entries for a specific network 236 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init() 237 * @network_ctx: Network configuration context or %NULL to flush all entries 238 */ 239 void pmksa_cache_flush(struct rsn_pmksa_cache *pmksa, void *network_ctx) 240 { 241 struct rsn_pmksa_cache_entry *entry, *prev = NULL, *tmp; 242 int removed = 0; 243 244 entry = pmksa->pmksa; 245 while (entry) { 246 if (entry->network_ctx == network_ctx || network_ctx == NULL) { 247 wpa_printf(MSG_DEBUG, "RSN: Flush PMKSA cache entry " 248 "for " MACSTR, MAC2STR(entry->aa)); 249 if (prev) 250 prev->next = entry->next; 251 else 252 pmksa->pmksa = entry->next; 253 tmp = entry; 254 entry = entry->next; 255 pmksa_cache_free_entry(pmksa, tmp, PMKSA_FREE); 256 removed++; 257 } else { 258 prev = entry; 259 entry = entry->next; 260 } 261 } 262 if (removed) 263 pmksa_cache_set_expiration(pmksa); 264 } 265 266 267 /** 268 * pmksa_cache_deinit - Free all entries in PMKSA cache 269 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init() 270 */ 271 void pmksa_cache_deinit(struct rsn_pmksa_cache *pmksa) 272 { 273 struct rsn_pmksa_cache_entry *entry, *prev; 274 275 if (pmksa == NULL) 276 return; 277 278 entry = pmksa->pmksa; 279 pmksa->pmksa = NULL; 280 while (entry) { 281 prev = entry; 282 entry = entry->next; 283 os_free(prev); 284 } 285 pmksa_cache_set_expiration(pmksa); 286 os_free(pmksa); 287 } 288 289 290 /** 291 * pmksa_cache_get - Fetch a PMKSA cache entry 292 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init() 293 * @aa: Authenticator address or %NULL to match any 294 * @pmkid: PMKID or %NULL to match any 295 * @network_ctx: Network context or %NULL to match any 296 * Returns: Pointer to PMKSA cache entry or %NULL if no match was found 297 */ 298 struct rsn_pmksa_cache_entry * pmksa_cache_get(struct rsn_pmksa_cache *pmksa, 299 const u8 *aa, const u8 *pmkid, 300 const void *network_ctx) 301 { 302 struct rsn_pmksa_cache_entry *entry = pmksa->pmksa; 303 while (entry) { 304 if ((aa == NULL || os_memcmp(entry->aa, aa, ETH_ALEN) == 0) && 305 (pmkid == NULL || 306 os_memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0) && 307 (network_ctx == NULL || network_ctx == entry->network_ctx)) 308 return entry; 309 entry = entry->next; 310 } 311 return NULL; 312 } 313 314 315 static struct rsn_pmksa_cache_entry * 316 pmksa_cache_clone_entry(struct rsn_pmksa_cache *pmksa, 317 const struct rsn_pmksa_cache_entry *old_entry, 318 const u8 *aa) 319 { 320 struct rsn_pmksa_cache_entry *new_entry; 321 322 new_entry = pmksa_cache_add(pmksa, old_entry->pmk, old_entry->pmk_len, 323 aa, pmksa->sm->own_addr, 324 old_entry->network_ctx, old_entry->akmp); 325 if (new_entry == NULL) 326 return NULL; 327 328 /* TODO: reorder entries based on expiration time? */ 329 new_entry->expiration = old_entry->expiration; 330 new_entry->opportunistic = 1; 331 332 return new_entry; 333 } 334 335 336 /** 337 * pmksa_cache_get_opportunistic - Try to get an opportunistic PMKSA entry 338 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init() 339 * @network_ctx: Network configuration context 340 * @aa: Authenticator address for the new AP 341 * Returns: Pointer to a new PMKSA cache entry or %NULL if not available 342 * 343 * Try to create a new PMKSA cache entry opportunistically by guessing that the 344 * new AP is sharing the same PMK as another AP that has the same SSID and has 345 * already an entry in PMKSA cache. 346 */ 347 struct rsn_pmksa_cache_entry * 348 pmksa_cache_get_opportunistic(struct rsn_pmksa_cache *pmksa, void *network_ctx, 349 const u8 *aa) 350 { 351 struct rsn_pmksa_cache_entry *entry = pmksa->pmksa; 352 353 wpa_printf(MSG_DEBUG, "RSN: Consider " MACSTR " for OKC", MAC2STR(aa)); 354 if (network_ctx == NULL) 355 return NULL; 356 while (entry) { 357 if (entry->network_ctx == network_ctx) { 358 entry = pmksa_cache_clone_entry(pmksa, entry, aa); 359 if (entry) { 360 wpa_printf(MSG_DEBUG, "RSN: added " 361 "opportunistic PMKSA cache entry " 362 "for " MACSTR, MAC2STR(aa)); 363 } 364 return entry; 365 } 366 entry = entry->next; 367 } 368 return NULL; 369 } 370 371 372 /** 373 * pmksa_cache_get_current - Get the current used PMKSA entry 374 * @sm: Pointer to WPA state machine data from wpa_sm_init() 375 * Returns: Pointer to the current PMKSA cache entry or %NULL if not available 376 */ 377 struct rsn_pmksa_cache_entry * pmksa_cache_get_current(struct wpa_sm *sm) 378 { 379 if (sm == NULL) 380 return NULL; 381 return sm->cur_pmksa; 382 } 383 384 385 /** 386 * pmksa_cache_clear_current - Clear the current PMKSA entry selection 387 * @sm: Pointer to WPA state machine data from wpa_sm_init() 388 */ 389 void pmksa_cache_clear_current(struct wpa_sm *sm) 390 { 391 if (sm == NULL) 392 return; 393 sm->cur_pmksa = NULL; 394 } 395 396 397 /** 398 * pmksa_cache_set_current - Set the current PMKSA entry selection 399 * @sm: Pointer to WPA state machine data from wpa_sm_init() 400 * @pmkid: PMKID for selecting PMKSA or %NULL if not used 401 * @bssid: BSSID for PMKSA or %NULL if not used 402 * @network_ctx: Network configuration context 403 * @try_opportunistic: Whether to allow opportunistic PMKSA caching 404 * Returns: 0 if PMKSA was found or -1 if no matching entry was found 405 */ 406 int pmksa_cache_set_current(struct wpa_sm *sm, const u8 *pmkid, 407 const u8 *bssid, void *network_ctx, 408 int try_opportunistic) 409 { 410 struct rsn_pmksa_cache *pmksa = sm->pmksa; 411 wpa_printf(MSG_DEBUG, "RSN: PMKSA cache search - network_ctx=%p " 412 "try_opportunistic=%d", network_ctx, try_opportunistic); 413 if (pmkid) 414 wpa_hexdump(MSG_DEBUG, "RSN: Search for PMKID", 415 pmkid, PMKID_LEN); 416 if (bssid) 417 wpa_printf(MSG_DEBUG, "RSN: Search for BSSID " MACSTR, 418 MAC2STR(bssid)); 419 420 sm->cur_pmksa = NULL; 421 if (pmkid) 422 sm->cur_pmksa = pmksa_cache_get(pmksa, NULL, pmkid, 423 network_ctx); 424 if (sm->cur_pmksa == NULL && bssid) 425 sm->cur_pmksa = pmksa_cache_get(pmksa, bssid, NULL, 426 network_ctx); 427 if (sm->cur_pmksa == NULL && try_opportunistic && bssid) 428 sm->cur_pmksa = pmksa_cache_get_opportunistic(pmksa, 429 network_ctx, 430 bssid); 431 if (sm->cur_pmksa) { 432 wpa_hexdump(MSG_DEBUG, "RSN: PMKSA cache entry found - PMKID", 433 sm->cur_pmksa->pmkid, PMKID_LEN); 434 return 0; 435 } 436 wpa_printf(MSG_DEBUG, "RSN: No PMKSA cache entry found"); 437 return -1; 438 } 439 440 441 /** 442 * pmksa_cache_list - Dump text list of entries in PMKSA cache 443 * @pmksa: Pointer to PMKSA cache data from pmksa_cache_init() 444 * @buf: Buffer for the list 445 * @len: Length of the buffer 446 * Returns: number of bytes written to buffer 447 * 448 * This function is used to generate a text format representation of the 449 * current PMKSA cache contents for the ctrl_iface PMKSA command. 450 */ 451 int pmksa_cache_list(struct rsn_pmksa_cache *pmksa, char *buf, size_t len) 452 { 453 int i, ret; 454 char *pos = buf; 455 struct rsn_pmksa_cache_entry *entry; 456 struct os_time now; 457 458 os_get_time(&now); 459 ret = os_snprintf(pos, buf + len - pos, 460 "Index / AA / PMKID / expiration (in seconds) / " 461 "opportunistic\n"); 462 if (ret < 0 || ret >= buf + len - pos) 463 return pos - buf; 464 pos += ret; 465 i = 0; 466 entry = pmksa->pmksa; 467 while (entry) { 468 i++; 469 ret = os_snprintf(pos, buf + len - pos, "%d " MACSTR " ", 470 i, MAC2STR(entry->aa)); 471 if (ret < 0 || ret >= buf + len - pos) 472 return pos - buf; 473 pos += ret; 474 pos += wpa_snprintf_hex(pos, buf + len - pos, entry->pmkid, 475 PMKID_LEN); 476 ret = os_snprintf(pos, buf + len - pos, " %d %d\n", 477 (int) (entry->expiration - now.sec), 478 entry->opportunistic); 479 if (ret < 0 || ret >= buf + len - pos) 480 return pos - buf; 481 pos += ret; 482 entry = entry->next; 483 } 484 return pos - buf; 485 } 486 487 488 /** 489 * pmksa_cache_init - Initialize PMKSA cache 490 * @free_cb: Callback function to be called when a PMKSA cache entry is freed 491 * @ctx: Context pointer for free_cb function 492 * @sm: Pointer to WPA state machine data from wpa_sm_init() 493 * Returns: Pointer to PMKSA cache data or %NULL on failure 494 */ 495 struct rsn_pmksa_cache * 496 pmksa_cache_init(void (*free_cb)(struct rsn_pmksa_cache_entry *entry, 497 void *ctx, enum pmksa_free_reason reason), 498 void *ctx, struct wpa_sm *sm) 499 { 500 struct rsn_pmksa_cache *pmksa; 501 502 pmksa = os_zalloc(sizeof(*pmksa)); 503 if (pmksa) { 504 pmksa->free_cb = free_cb; 505 pmksa->ctx = ctx; 506 pmksa->sm = sm; 507 } 508 509 return pmksa; 510 } 511 512 #endif /* IEEE8021X_EAPOL and !CONFIG_NO_WPA2 */ 513