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