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