1 /* 2 * RSN pre-authentication (supplicant) 3 * Copyright (c) 2003-2015, 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 "wpa.h" 13 #include "eloop.h" 14 #include "l2_packet/l2_packet.h" 15 #include "eapol_supp/eapol_supp_sm.h" 16 #include "preauth.h" 17 #include "pmksa_cache.h" 18 #include "wpa_i.h" 19 20 21 #if defined(IEEE8021X_EAPOL) && !defined(CONFIG_NO_WPA) 22 23 #define PMKID_CANDIDATE_PRIO_SCAN 1000 24 25 26 struct rsn_pmksa_candidate { 27 struct dl_list list; 28 u8 bssid[ETH_ALEN]; 29 int priority; 30 }; 31 32 33 /** 34 * pmksa_candidate_free - Free all entries in PMKSA candidate list 35 * @sm: Pointer to WPA state machine data from wpa_sm_init() 36 */ 37 void pmksa_candidate_free(struct wpa_sm *sm) 38 { 39 struct rsn_pmksa_candidate *entry, *n; 40 41 if (sm == NULL) 42 return; 43 44 dl_list_for_each_safe(entry, n, &sm->pmksa_candidates, 45 struct rsn_pmksa_candidate, list) { 46 dl_list_del(&entry->list); 47 os_free(entry); 48 } 49 } 50 51 52 static int rsn_preauth_key_mgmt(int akmp) 53 { 54 return !!(akmp & (WPA_KEY_MGMT_IEEE8021X | 55 WPA_KEY_MGMT_IEEE8021X_SHA256 | 56 WPA_KEY_MGMT_IEEE8021X_SUITE_B | 57 WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)); 58 } 59 60 61 static void rsn_preauth_receive(void *ctx, const u8 *src_addr, 62 const u8 *buf, size_t len) 63 { 64 struct wpa_sm *sm = ctx; 65 66 wpa_printf(MSG_DEBUG, "RX pre-auth from " MACSTR, MAC2STR(src_addr)); 67 wpa_hexdump(MSG_MSGDUMP, "RX pre-auth", buf, len); 68 69 if (sm->preauth_eapol == NULL || 70 is_zero_ether_addr(sm->preauth_bssid) || 71 os_memcmp(sm->preauth_bssid, src_addr, ETH_ALEN) != 0) { 72 wpa_printf(MSG_WARNING, "RSN pre-auth frame received from " 73 "unexpected source " MACSTR " - dropped", 74 MAC2STR(src_addr)); 75 return; 76 } 77 78 eapol_sm_rx_eapol(sm->preauth_eapol, src_addr, buf, len); 79 } 80 81 82 static void rsn_preauth_eapol_cb(struct eapol_sm *eapol, 83 enum eapol_supp_result result, 84 void *ctx) 85 { 86 struct wpa_sm *sm = ctx; 87 u8 pmk[PMK_LEN]; 88 89 if (result == EAPOL_SUPP_RESULT_SUCCESS) { 90 int res, pmk_len; 91 pmk_len = PMK_LEN; 92 res = eapol_sm_get_key(eapol, pmk, PMK_LEN); 93 if (res) { 94 /* 95 * EAP-LEAP is an exception from other EAP methods: it 96 * uses only 16-byte PMK. 97 */ 98 res = eapol_sm_get_key(eapol, pmk, 16); 99 pmk_len = 16; 100 } 101 if (res == 0) { 102 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from pre-auth", 103 pmk, pmk_len); 104 sm->pmk_len = pmk_len; 105 pmksa_cache_add(sm->pmksa, pmk, pmk_len, NULL, 106 NULL, 0, 107 sm->preauth_bssid, sm->own_addr, 108 sm->network_ctx, 109 WPA_KEY_MGMT_IEEE8021X, NULL); 110 } else { 111 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, 112 "RSN: failed to get master session key from " 113 "pre-auth EAPOL state machines"); 114 result = EAPOL_SUPP_RESULT_FAILURE; 115 } 116 } 117 118 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "RSN: pre-authentication with " 119 MACSTR " %s", MAC2STR(sm->preauth_bssid), 120 result == EAPOL_SUPP_RESULT_SUCCESS ? "completed successfully" : 121 "failed"); 122 123 rsn_preauth_deinit(sm); 124 rsn_preauth_candidate_process(sm); 125 } 126 127 128 static void rsn_preauth_timeout(void *eloop_ctx, void *timeout_ctx) 129 { 130 struct wpa_sm *sm = eloop_ctx; 131 132 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "RSN: pre-authentication with " 133 MACSTR " timed out", MAC2STR(sm->preauth_bssid)); 134 rsn_preauth_deinit(sm); 135 rsn_preauth_candidate_process(sm); 136 } 137 138 139 static int rsn_preauth_eapol_send(void *ctx, int type, const u8 *buf, 140 size_t len) 141 { 142 struct wpa_sm *sm = ctx; 143 u8 *msg; 144 size_t msglen; 145 int res; 146 147 /* TODO: could add l2_packet_sendmsg that allows fragments to avoid 148 * extra copy here */ 149 150 if (sm->l2_preauth == NULL) 151 return -1; 152 153 msg = wpa_sm_alloc_eapol(sm, type, buf, len, &msglen, NULL); 154 if (msg == NULL) 155 return -1; 156 157 wpa_hexdump(MSG_MSGDUMP, "TX EAPOL (preauth)", msg, msglen); 158 res = l2_packet_send(sm->l2_preauth, sm->preauth_bssid, 159 ETH_P_RSN_PREAUTH, msg, msglen); 160 os_free(msg); 161 return res; 162 } 163 164 165 /** 166 * rsn_preauth_init - Start new RSN pre-authentication 167 * @sm: Pointer to WPA state machine data from wpa_sm_init() 168 * @dst: Authenticator address (BSSID) with which to preauthenticate 169 * @eap_conf: Current EAP configuration 170 * Returns: 0 on success, -1 on another pre-authentication is in progress, 171 * -2 on layer 2 packet initialization failure, -3 on EAPOL state machine 172 * initialization failure, -4 on memory allocation failure 173 * 174 * This function request an RSN pre-authentication with a given destination 175 * address. This is usually called for PMKSA candidates found from scan results 176 * or from driver reports. In addition, ctrl_iface PREAUTH command can trigger 177 * pre-authentication. 178 */ 179 int rsn_preauth_init(struct wpa_sm *sm, const u8 *dst, 180 struct eap_peer_config *eap_conf) 181 { 182 struct eapol_config eapol_conf; 183 struct eapol_ctx *ctx; 184 int ret; 185 186 if (sm->preauth_eapol) 187 return -1; 188 189 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, 190 "RSN: starting pre-authentication with " MACSTR, MAC2STR(dst)); 191 192 sm->l2_preauth = l2_packet_init(sm->ifname, sm->own_addr, 193 ETH_P_RSN_PREAUTH, 194 rsn_preauth_receive, sm, 0); 195 if (sm->l2_preauth == NULL) { 196 wpa_printf(MSG_WARNING, "RSN: Failed to initialize L2 packet " 197 "processing for pre-authentication"); 198 return -2; 199 } 200 201 if (sm->bridge_ifname) { 202 sm->l2_preauth_br = l2_packet_init(sm->bridge_ifname, 203 sm->own_addr, 204 ETH_P_RSN_PREAUTH, 205 rsn_preauth_receive, sm, 0); 206 if (sm->l2_preauth_br == NULL) { 207 wpa_printf(MSG_WARNING, "RSN: Failed to initialize L2 " 208 "packet processing (bridge) for " 209 "pre-authentication"); 210 ret = -2; 211 goto fail; 212 } 213 } 214 215 ctx = os_zalloc(sizeof(*ctx)); 216 if (ctx == NULL) { 217 wpa_printf(MSG_WARNING, "Failed to allocate EAPOL context."); 218 ret = -4; 219 goto fail; 220 } 221 ctx->ctx = sm->ctx->ctx; 222 ctx->msg_ctx = sm->ctx->ctx; 223 ctx->preauth = 1; 224 ctx->cb = rsn_preauth_eapol_cb; 225 ctx->cb_ctx = sm; 226 ctx->scard_ctx = sm->scard_ctx; 227 ctx->eapol_send = rsn_preauth_eapol_send; 228 ctx->eapol_send_ctx = sm; 229 ctx->set_config_blob = sm->ctx->set_config_blob; 230 ctx->get_config_blob = sm->ctx->get_config_blob; 231 232 sm->preauth_eapol = eapol_sm_init(ctx); 233 if (sm->preauth_eapol == NULL) { 234 os_free(ctx); 235 wpa_printf(MSG_WARNING, "RSN: Failed to initialize EAPOL " 236 "state machines for pre-authentication"); 237 ret = -3; 238 goto fail; 239 } 240 os_memset(&eapol_conf, 0, sizeof(eapol_conf)); 241 eapol_conf.accept_802_1x_keys = 0; 242 eapol_conf.required_keys = 0; 243 eapol_conf.fast_reauth = sm->fast_reauth; 244 eapol_conf.workaround = sm->eap_workaround; 245 eapol_sm_notify_config(sm->preauth_eapol, eap_conf, &eapol_conf); 246 /* 247 * Use a shorter startPeriod with preauthentication since the first 248 * preauth EAPOL-Start frame may end up being dropped due to race 249 * condition in the AP between the data receive and key configuration 250 * after the 4-Way Handshake. 251 */ 252 eapol_sm_configure(sm->preauth_eapol, -1, -1, 5, 6); 253 os_memcpy(sm->preauth_bssid, dst, ETH_ALEN); 254 255 eapol_sm_notify_portValid(sm->preauth_eapol, true); 256 /* 802.1X::portControl = Auto */ 257 eapol_sm_notify_portEnabled(sm->preauth_eapol, true); 258 259 eloop_register_timeout(sm->dot11RSNAConfigSATimeout, 0, 260 rsn_preauth_timeout, sm, NULL); 261 262 return 0; 263 264 fail: 265 if (sm->l2_preauth_br) { 266 l2_packet_deinit(sm->l2_preauth_br); 267 sm->l2_preauth_br = NULL; 268 } 269 l2_packet_deinit(sm->l2_preauth); 270 sm->l2_preauth = NULL; 271 return ret; 272 } 273 274 275 /** 276 * rsn_preauth_deinit - Abort RSN pre-authentication 277 * @sm: Pointer to WPA state machine data from wpa_sm_init() 278 * 279 * This function aborts the current RSN pre-authentication (if one is started) 280 * and frees resources allocated for it. 281 */ 282 void rsn_preauth_deinit(struct wpa_sm *sm) 283 { 284 if (sm == NULL || !sm->preauth_eapol) 285 return; 286 287 eloop_cancel_timeout(rsn_preauth_timeout, sm, NULL); 288 eapol_sm_deinit(sm->preauth_eapol); 289 sm->preauth_eapol = NULL; 290 os_memset(sm->preauth_bssid, 0, ETH_ALEN); 291 292 l2_packet_deinit(sm->l2_preauth); 293 sm->l2_preauth = NULL; 294 if (sm->l2_preauth_br) { 295 l2_packet_deinit(sm->l2_preauth_br); 296 sm->l2_preauth_br = NULL; 297 } 298 } 299 300 301 /** 302 * rsn_preauth_candidate_process - Process PMKSA candidates 303 * @sm: Pointer to WPA state machine data from wpa_sm_init() 304 * 305 * Go through the PMKSA candidates and start pre-authentication if a candidate 306 * without an existing PMKSA cache entry is found. Processed candidates will be 307 * removed from the list. 308 */ 309 void rsn_preauth_candidate_process(struct wpa_sm *sm) 310 { 311 struct rsn_pmksa_candidate *candidate, *n; 312 313 if (dl_list_empty(&sm->pmksa_candidates)) 314 return; 315 316 /* TODO: drop priority for old candidate entries */ 317 318 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: processing PMKSA candidate " 319 "list"); 320 if (sm->preauth_eapol || 321 sm->proto != WPA_PROTO_RSN || 322 wpa_sm_get_state(sm) != WPA_COMPLETED || 323 !rsn_preauth_key_mgmt(sm->key_mgmt)) { 324 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: not in suitable " 325 "state for new pre-authentication"); 326 return; /* invalid state for new pre-auth */ 327 } 328 329 dl_list_for_each_safe(candidate, n, &sm->pmksa_candidates, 330 struct rsn_pmksa_candidate, list) { 331 struct rsn_pmksa_cache_entry *p = NULL; 332 p = pmksa_cache_get(sm->pmksa, candidate->bssid, NULL, NULL, 0); 333 if (os_memcmp(sm->bssid, candidate->bssid, ETH_ALEN) != 0 && 334 (p == NULL || p->opportunistic)) { 335 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA " 336 "candidate " MACSTR 337 " selected for pre-authentication", 338 MAC2STR(candidate->bssid)); 339 dl_list_del(&candidate->list); 340 rsn_preauth_init(sm, candidate->bssid, 341 sm->eap_conf_ctx); 342 os_free(candidate); 343 return; 344 } 345 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA candidate " 346 MACSTR " does not need pre-authentication anymore", 347 MAC2STR(candidate->bssid)); 348 /* Some drivers (e.g., NDIS) expect to get notified about the 349 * PMKIDs again, so report the existing data now. */ 350 if (p) { 351 wpa_sm_add_pmkid(sm, NULL, candidate->bssid, p->pmkid, 352 NULL, p->pmk, p->pmk_len, 0, 0, 353 p->akmp); 354 } 355 356 dl_list_del(&candidate->list); 357 os_free(candidate); 358 } 359 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: no more pending PMKSA " 360 "candidates"); 361 } 362 363 364 /** 365 * pmksa_candidate_add - Add a new PMKSA candidate 366 * @sm: Pointer to WPA state machine data from wpa_sm_init() 367 * @bssid: BSSID (authenticator address) of the candidate 368 * @prio: Priority (the smaller number, the higher priority) 369 * @preauth: Whether the candidate AP advertises support for pre-authentication 370 * 371 * This function is used to add PMKSA candidates for RSN pre-authentication. It 372 * is called from scan result processing and from driver events for PMKSA 373 * candidates, i.e., EVENT_PMKID_CANDIDATE events to wpa_supplicant_event(). 374 */ 375 void pmksa_candidate_add(struct wpa_sm *sm, const u8 *bssid, 376 int prio, int preauth) 377 { 378 struct rsn_pmksa_candidate *cand, *pos; 379 380 if (sm->network_ctx && sm->proactive_key_caching) 381 pmksa_cache_get_opportunistic(sm->pmksa, sm->network_ctx, 382 bssid, 0); 383 384 if (!preauth) { 385 wpa_printf(MSG_DEBUG, "RSN: Ignored PMKID candidate without " 386 "preauth flag"); 387 return; 388 } 389 390 /* If BSSID already on candidate list, update the priority of the old 391 * entry. Do not override priority based on normal scan results. */ 392 cand = NULL; 393 dl_list_for_each(pos, &sm->pmksa_candidates, 394 struct rsn_pmksa_candidate, list) { 395 if (os_memcmp(pos->bssid, bssid, ETH_ALEN) == 0) { 396 cand = pos; 397 break; 398 } 399 } 400 401 if (cand) { 402 dl_list_del(&cand->list); 403 if (prio < PMKID_CANDIDATE_PRIO_SCAN) 404 cand->priority = prio; 405 } else { 406 cand = os_zalloc(sizeof(*cand)); 407 if (cand == NULL) 408 return; 409 os_memcpy(cand->bssid, bssid, ETH_ALEN); 410 cand->priority = prio; 411 } 412 413 /* Add candidate to the list; order by increasing priority value. i.e., 414 * highest priority (smallest value) first. */ 415 dl_list_for_each(pos, &sm->pmksa_candidates, 416 struct rsn_pmksa_candidate, list) { 417 if (cand->priority <= pos->priority) { 418 if (!pos->list.prev) { 419 /* 420 * This cannot really happen in pracrice since 421 * pos was fetched from the list and the prev 422 * pointer must be set. It looks like clang 423 * static analyzer gets confused with the 424 * dl_list_del(&cand->list) call above and ends 425 * up assuming pos->list.prev could be NULL. 426 */ 427 os_free(cand); 428 return; 429 } 430 dl_list_add(pos->list.prev, &cand->list); 431 cand = NULL; 432 break; 433 } 434 } 435 if (cand) 436 dl_list_add_tail(&sm->pmksa_candidates, &cand->list); 437 438 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: added PMKSA cache " 439 "candidate " MACSTR " prio %d", MAC2STR(bssid), prio); 440 rsn_preauth_candidate_process(sm); 441 } 442 443 444 /* TODO: schedule periodic scans if current AP supports preauth */ 445 446 /** 447 * rsn_preauth_scan_results - Start processing scan results for canditates 448 * @sm: Pointer to WPA state machine data from wpa_sm_init() 449 * Returns: 0 if ready to process results or -1 to skip processing 450 * 451 * This functions is used to notify RSN code about start of new scan results 452 * processing. The actual scan results will be provided by calling 453 * rsn_preauth_scan_result() for each BSS if this function returned 0. 454 */ 455 int rsn_preauth_scan_results(struct wpa_sm *sm) 456 { 457 if (sm->ssid_len == 0) 458 return -1; 459 460 /* 461 * TODO: is it ok to free all candidates? What about the entries 462 * received from EVENT_PMKID_CANDIDATE? 463 */ 464 pmksa_candidate_free(sm); 465 466 return 0; 467 } 468 469 470 /** 471 * rsn_preauth_scan_result - Processing scan result for PMKSA canditates 472 * @sm: Pointer to WPA state machine data from wpa_sm_init() 473 * 474 * Add all suitable APs (Authenticators) from scan results into PMKSA 475 * candidate list. 476 */ 477 void rsn_preauth_scan_result(struct wpa_sm *sm, const u8 *bssid, 478 const u8 *ssid, const u8 *rsn) 479 { 480 struct wpa_ie_data ie; 481 struct rsn_pmksa_cache_entry *pmksa; 482 483 if (ssid[1] != sm->ssid_len || 484 os_memcmp(ssid + 2, sm->ssid, sm->ssid_len) != 0) 485 return; /* Not for the current SSID */ 486 487 if (os_memcmp(bssid, sm->bssid, ETH_ALEN) == 0) 488 return; /* Ignore current AP */ 489 490 if (wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie)) 491 return; 492 493 pmksa = pmksa_cache_get(sm->pmksa, bssid, NULL, NULL, 0); 494 if (pmksa && (!pmksa->opportunistic || 495 !(ie.capabilities & WPA_CAPABILITY_PREAUTH))) 496 return; 497 498 if (!rsn_preauth_key_mgmt(ie.key_mgmt)) 499 return; 500 501 /* Give less priority to candidates found from normal scan results. */ 502 pmksa_candidate_add(sm, bssid, PMKID_CANDIDATE_PRIO_SCAN, 503 ie.capabilities & WPA_CAPABILITY_PREAUTH); 504 } 505 506 507 #ifdef CONFIG_CTRL_IFACE 508 /** 509 * rsn_preauth_get_status - Get pre-authentication status 510 * @sm: Pointer to WPA state machine data from wpa_sm_init() 511 * @buf: Buffer for status information 512 * @buflen: Maximum buffer length 513 * @verbose: Whether to include verbose status information 514 * Returns: Number of bytes written to buf. 515 * 516 * Query WPA2 pre-authentication for status information. This function fills in 517 * a text area with current status information. If the buffer (buf) is not 518 * large enough, status information will be truncated to fit the buffer. 519 */ 520 int rsn_preauth_get_status(struct wpa_sm *sm, char *buf, size_t buflen, 521 int verbose) 522 { 523 char *pos = buf, *end = buf + buflen; 524 int res, ret; 525 526 if (sm->preauth_eapol) { 527 ret = os_snprintf(pos, end - pos, "Pre-authentication " 528 "EAPOL state machines:\n"); 529 if (os_snprintf_error(end - pos, ret)) 530 return pos - buf; 531 pos += ret; 532 res = eapol_sm_get_status(sm->preauth_eapol, 533 pos, end - pos, verbose); 534 if (res >= 0) 535 pos += res; 536 } 537 538 return pos - buf; 539 } 540 #endif /* CONFIG_CTRL_IFACE */ 541 542 543 /** 544 * rsn_preauth_in_progress - Verify whether pre-authentication is in progress 545 * @sm: Pointer to WPA state machine data from wpa_sm_init() 546 */ 547 int rsn_preauth_in_progress(struct wpa_sm *sm) 548 { 549 return sm->preauth_eapol != NULL; 550 } 551 552 #endif /* IEEE8021X_EAPOL && !CONFIG_NO_WPA */ 553