Home | History | Annotate | Download | only in rsn_supp
      1 /*
      2  * WPA Supplicant - WPA state machine and EAPOL-Key processing
      3  * Copyright (c) 2003-2012, Jouni Malinen <j (at) 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 "crypto/aes_wrap.h"
     13 #include "crypto/crypto.h"
     14 #include "crypto/random.h"
     15 #include "common/ieee802_11_defs.h"
     16 #include "eapol_supp/eapol_supp_sm.h"
     17 #include "wpa.h"
     18 #include "eloop.h"
     19 #include "preauth.h"
     20 #include "pmksa_cache.h"
     21 #include "wpa_i.h"
     22 #include "wpa_ie.h"
     23 #include "peerkey.h"
     24 
     25 
     26 /**
     27  * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
     28  * @sm: Pointer to WPA state machine data from wpa_sm_init()
     29  * @kck: Key Confirmation Key (KCK, part of PTK)
     30  * @ver: Version field from Key Info
     31  * @dest: Destination address for the frame
     32  * @proto: Ethertype (usually ETH_P_EAPOL)
     33  * @msg: EAPOL-Key message
     34  * @msg_len: Length of message
     35  * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
     36  */
     37 void wpa_eapol_key_send(struct wpa_sm *sm, const u8 *kck,
     38 			int ver, const u8 *dest, u16 proto,
     39 			u8 *msg, size_t msg_len, u8 *key_mic)
     40 {
     41 	if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
     42 		/*
     43 		 * Association event was not yet received; try to fetch
     44 		 * BSSID from the driver.
     45 		 */
     46 		if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
     47 			wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
     48 				"WPA: Failed to read BSSID for "
     49 				"EAPOL-Key destination address");
     50 		} else {
     51 			dest = sm->bssid;
     52 			wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
     53 				"WPA: Use BSSID (" MACSTR
     54 				") as the destination for EAPOL-Key",
     55 				MAC2STR(dest));
     56 		}
     57 	}
     58 	if (key_mic &&
     59 	    wpa_eapol_key_mic(kck, ver, msg, msg_len, key_mic)) {
     60 		wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
     61 			"WPA: Failed to generate EAPOL-Key "
     62 			"version %d MIC", ver);
     63 		goto out;
     64 	}
     65 	wpa_hexdump_key(MSG_DEBUG, "WPA: KCK", kck, 16);
     66 	wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC", key_mic, 16);
     67 	wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
     68 	wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
     69 	eapol_sm_notify_tx_eapol_key(sm->eapol);
     70 out:
     71 	os_free(msg);
     72 }
     73 
     74 
     75 /**
     76  * wpa_sm_key_request - Send EAPOL-Key Request
     77  * @sm: Pointer to WPA state machine data from wpa_sm_init()
     78  * @error: Indicate whether this is an Michael MIC error report
     79  * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
     80  *
     81  * Send an EAPOL-Key Request to the current authenticator. This function is
     82  * used to request rekeying and it is usually called when a local Michael MIC
     83  * failure is detected.
     84  */
     85 void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
     86 {
     87 	size_t rlen;
     88 	struct wpa_eapol_key *reply;
     89 	int key_info, ver;
     90 	u8 bssid[ETH_ALEN], *rbuf;
     91 
     92 	if (sm->key_mgmt == WPA_KEY_MGMT_OSEN)
     93 		ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
     94 	else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
     95 		 wpa_key_mgmt_sha256(sm->key_mgmt))
     96 		ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
     97 	else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
     98 		ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
     99 	else
    100 		ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
    101 
    102 	if (wpa_sm_get_bssid(sm, bssid) < 0) {
    103 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
    104 			"Failed to read BSSID for EAPOL-Key request");
    105 		return;
    106 	}
    107 
    108 	rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
    109 				  sizeof(*reply), &rlen, (void *) &reply);
    110 	if (rbuf == NULL)
    111 		return;
    112 
    113 	reply->type = (sm->proto == WPA_PROTO_RSN ||
    114 		       sm->proto == WPA_PROTO_OSEN) ?
    115 		EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
    116 	key_info = WPA_KEY_INFO_REQUEST | ver;
    117 	if (sm->ptk_set)
    118 		key_info |= WPA_KEY_INFO_MIC;
    119 	if (error)
    120 		key_info |= WPA_KEY_INFO_ERROR;
    121 	if (pairwise)
    122 		key_info |= WPA_KEY_INFO_KEY_TYPE;
    123 	WPA_PUT_BE16(reply->key_info, key_info);
    124 	WPA_PUT_BE16(reply->key_length, 0);
    125 	os_memcpy(reply->replay_counter, sm->request_counter,
    126 		  WPA_REPLAY_COUNTER_LEN);
    127 	inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
    128 
    129 	WPA_PUT_BE16(reply->key_data_length, 0);
    130 
    131 	wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
    132 		"WPA: Sending EAPOL-Key Request (error=%d "
    133 		"pairwise=%d ptk_set=%d len=%lu)",
    134 		error, pairwise, sm->ptk_set, (unsigned long) rlen);
    135 	wpa_eapol_key_send(sm, sm->ptk.kck, ver, bssid, ETH_P_EAPOL,
    136 			   rbuf, rlen, key_info & WPA_KEY_INFO_MIC ?
    137 			   reply->key_mic : NULL);
    138 }
    139 
    140 
    141 static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
    142 				  const unsigned char *src_addr,
    143 				  const u8 *pmkid)
    144 {
    145 	int abort_cached = 0;
    146 
    147 	if (pmkid && !sm->cur_pmksa) {
    148 		/* When using drivers that generate RSN IE, wpa_supplicant may
    149 		 * not have enough time to get the association information
    150 		 * event before receiving this 1/4 message, so try to find a
    151 		 * matching PMKSA cache entry here. */
    152 		sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
    153 						NULL);
    154 		if (sm->cur_pmksa) {
    155 			wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
    156 				"RSN: found matching PMKID from PMKSA cache");
    157 		} else {
    158 			wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
    159 				"RSN: no matching PMKID found");
    160 			abort_cached = 1;
    161 		}
    162 	}
    163 
    164 	if (pmkid && sm->cur_pmksa &&
    165 	    os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
    166 		wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
    167 		wpa_sm_set_pmk_from_pmksa(sm);
    168 		wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
    169 				sm->pmk, sm->pmk_len);
    170 		eapol_sm_notify_cached(sm->eapol);
    171 #ifdef CONFIG_IEEE80211R
    172 		sm->xxkey_len = 0;
    173 #endif /* CONFIG_IEEE80211R */
    174 	} else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
    175 		int res, pmk_len;
    176 		pmk_len = PMK_LEN;
    177 		res = eapol_sm_get_key(sm->eapol, sm->pmk, PMK_LEN);
    178 		if (res) {
    179 			/*
    180 			 * EAP-LEAP is an exception from other EAP methods: it
    181 			 * uses only 16-byte PMK.
    182 			 */
    183 			res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
    184 			pmk_len = 16;
    185 		} else {
    186 #ifdef CONFIG_IEEE80211R
    187 			u8 buf[2 * PMK_LEN];
    188 			if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
    189 			{
    190 				os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
    191 				sm->xxkey_len = PMK_LEN;
    192 				os_memset(buf, 0, sizeof(buf));
    193 			}
    194 #endif /* CONFIG_IEEE80211R */
    195 		}
    196 		if (res == 0) {
    197 			struct rsn_pmksa_cache_entry *sa = NULL;
    198 			wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
    199 					"machines", sm->pmk, pmk_len);
    200 			sm->pmk_len = pmk_len;
    201 			if (sm->proto == WPA_PROTO_RSN &&
    202 			    !wpa_key_mgmt_ft(sm->key_mgmt)) {
    203 				sa = pmksa_cache_add(sm->pmksa,
    204 						     sm->pmk, pmk_len,
    205 						     src_addr, sm->own_addr,
    206 						     sm->network_ctx,
    207 						     sm->key_mgmt);
    208 			}
    209 			if (!sm->cur_pmksa && pmkid &&
    210 			    pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL))
    211 			{
    212 				wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
    213 					"RSN: the new PMK matches with the "
    214 					"PMKID");
    215 				abort_cached = 0;
    216 			}
    217 
    218 			if (!sm->cur_pmksa)
    219 				sm->cur_pmksa = sa;
    220 		} else {
    221 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
    222 				"WPA: Failed to get master session key from "
    223 				"EAPOL state machines - key handshake "
    224 				"aborted");
    225 			if (sm->cur_pmksa) {
    226 				wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
    227 					"RSN: Cancelled PMKSA caching "
    228 					"attempt");
    229 				sm->cur_pmksa = NULL;
    230 				abort_cached = 1;
    231 			} else if (!abort_cached) {
    232 				return -1;
    233 			}
    234 		}
    235 	}
    236 
    237 	if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
    238 	    !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
    239 	{
    240 		/* Send EAPOL-Start to trigger full EAP authentication. */
    241 		u8 *buf;
    242 		size_t buflen;
    243 
    244 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
    245 			"RSN: no PMKSA entry found - trigger "
    246 			"full EAP authentication");
    247 		buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
    248 					 NULL, 0, &buflen, NULL);
    249 		if (buf) {
    250 			wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
    251 					  buf, buflen);
    252 			os_free(buf);
    253 			return -2;
    254 		}
    255 
    256 		return -1;
    257 	}
    258 
    259 	return 0;
    260 }
    261 
    262 
    263 /**
    264  * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
    265  * @sm: Pointer to WPA state machine data from wpa_sm_init()
    266  * @dst: Destination address for the frame
    267  * @key: Pointer to the EAPOL-Key frame header
    268  * @ver: Version bits from EAPOL-Key Key Info
    269  * @nonce: Nonce value for the EAPOL-Key frame
    270  * @wpa_ie: WPA/RSN IE
    271  * @wpa_ie_len: Length of the WPA/RSN IE
    272  * @ptk: PTK to use for keyed hash and encryption
    273  * Returns: 0 on success, -1 on failure
    274  */
    275 int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
    276 			       const struct wpa_eapol_key *key,
    277 			       int ver, const u8 *nonce,
    278 			       const u8 *wpa_ie, size_t wpa_ie_len,
    279 			       struct wpa_ptk *ptk)
    280 {
    281 	size_t rlen;
    282 	struct wpa_eapol_key *reply;
    283 	u8 *rbuf;
    284 	u8 *rsn_ie_buf = NULL;
    285 
    286 	if (wpa_ie == NULL) {
    287 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
    288 			"cannot generate msg 2/4");
    289 		return -1;
    290 	}
    291 
    292 #ifdef CONFIG_IEEE80211R
    293 	if (wpa_key_mgmt_ft(sm->key_mgmt)) {
    294 		int res;
    295 
    296 		/*
    297 		 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
    298 		 * FTIE from (Re)Association Response.
    299 		 */
    300 		rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
    301 				       sm->assoc_resp_ies_len);
    302 		if (rsn_ie_buf == NULL)
    303 			return -1;
    304 		os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
    305 		res = wpa_insert_pmkid(rsn_ie_buf, wpa_ie_len,
    306 				       sm->pmk_r1_name);
    307 		if (res < 0) {
    308 			os_free(rsn_ie_buf);
    309 			return -1;
    310 		}
    311 		wpa_ie_len += res;
    312 
    313 		if (sm->assoc_resp_ies) {
    314 			os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
    315 				  sm->assoc_resp_ies_len);
    316 			wpa_ie_len += sm->assoc_resp_ies_len;
    317 		}
    318 
    319 		wpa_ie = rsn_ie_buf;
    320 	}
    321 #endif /* CONFIG_IEEE80211R */
    322 
    323 	wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
    324 
    325 	rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
    326 				  NULL, sizeof(*reply) + wpa_ie_len,
    327 				  &rlen, (void *) &reply);
    328 	if (rbuf == NULL) {
    329 		os_free(rsn_ie_buf);
    330 		return -1;
    331 	}
    332 
    333 	reply->type = (sm->proto == WPA_PROTO_RSN ||
    334 		       sm->proto == WPA_PROTO_OSEN) ?
    335 		EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
    336 	WPA_PUT_BE16(reply->key_info,
    337 		     ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
    338 	if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
    339 		WPA_PUT_BE16(reply->key_length, 0);
    340 	else
    341 		os_memcpy(reply->key_length, key->key_length, 2);
    342 	os_memcpy(reply->replay_counter, key->replay_counter,
    343 		  WPA_REPLAY_COUNTER_LEN);
    344 	wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
    345 		    WPA_REPLAY_COUNTER_LEN);
    346 
    347 	WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
    348 	os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
    349 	os_free(rsn_ie_buf);
    350 
    351 	os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
    352 
    353 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
    354 	wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
    355 			   rbuf, rlen, reply->key_mic);
    356 
    357 	return 0;
    358 }
    359 
    360 
    361 static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
    362 			  const struct wpa_eapol_key *key,
    363 			  struct wpa_ptk *ptk)
    364 {
    365 	size_t ptk_len = wpa_cipher_key_len(sm->pairwise_cipher) + 32;
    366 #ifdef CONFIG_IEEE80211R
    367 	if (wpa_key_mgmt_ft(sm->key_mgmt))
    368 		return wpa_derive_ptk_ft(sm, src_addr, key, ptk, ptk_len);
    369 #endif /* CONFIG_IEEE80211R */
    370 
    371 	wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
    372 		       sm->own_addr, sm->bssid, sm->snonce, key->key_nonce,
    373 		       (u8 *) ptk, ptk_len,
    374 		       wpa_key_mgmt_sha256(sm->key_mgmt));
    375 	return 0;
    376 }
    377 
    378 
    379 static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
    380 					  const unsigned char *src_addr,
    381 					  const struct wpa_eapol_key *key,
    382 					  u16 ver, const u8 *key_data,
    383 					  size_t key_data_len)
    384 {
    385 	struct wpa_eapol_ie_parse ie;
    386 	struct wpa_ptk *ptk;
    387 	int res;
    388 	u8 *kde, *kde_buf = NULL;
    389 	size_t kde_len;
    390 
    391 	if (wpa_sm_get_network_ctx(sm) == NULL) {
    392 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
    393 			"found (msg 1 of 4)");
    394 		return;
    395 	}
    396 
    397 	wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
    398 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
    399 		"Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
    400 
    401 	os_memset(&ie, 0, sizeof(ie));
    402 
    403 	if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
    404 		/* RSN: msg 1/4 should contain PMKID for the selected PMK */
    405 		wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
    406 			    key_data, key_data_len);
    407 		if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
    408 			goto failed;
    409 		if (ie.pmkid) {
    410 			wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
    411 				    "Authenticator", ie.pmkid, PMKID_LEN);
    412 		}
    413 	}
    414 
    415 	res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
    416 	if (res == -2) {
    417 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
    418 			"msg 1/4 - requesting full EAP authentication");
    419 		return;
    420 	}
    421 	if (res)
    422 		goto failed;
    423 
    424 	if (sm->renew_snonce) {
    425 		if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
    426 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
    427 				"WPA: Failed to get random data for SNonce");
    428 			goto failed;
    429 		}
    430 		sm->renew_snonce = 0;
    431 		wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
    432 			    sm->snonce, WPA_NONCE_LEN);
    433 	}
    434 
    435 	/* Calculate PTK which will be stored as a temporary PTK until it has
    436 	 * been verified when processing message 3/4. */
    437 	ptk = &sm->tptk;
    438 	wpa_derive_ptk(sm, src_addr, key, ptk);
    439 	if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
    440 		u8 buf[8];
    441 		/* Supplicant: swap tx/rx Mic keys */
    442 		os_memcpy(buf, ptk->u.auth.tx_mic_key, 8);
    443 		os_memcpy(ptk->u.auth.tx_mic_key, ptk->u.auth.rx_mic_key, 8);
    444 		os_memcpy(ptk->u.auth.rx_mic_key, buf, 8);
    445 		os_memset(buf, 0, sizeof(buf));
    446 	}
    447 	sm->tptk_set = 1;
    448 
    449 	kde = sm->assoc_wpa_ie;
    450 	kde_len = sm->assoc_wpa_ie_len;
    451 
    452 #ifdef CONFIG_P2P
    453 	if (sm->p2p) {
    454 		kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
    455 		if (kde_buf) {
    456 			u8 *pos;
    457 			wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
    458 				   "into EAPOL-Key 2/4");
    459 			os_memcpy(kde_buf, kde, kde_len);
    460 			kde = kde_buf;
    461 			pos = kde + kde_len;
    462 			*pos++ = WLAN_EID_VENDOR_SPECIFIC;
    463 			*pos++ = RSN_SELECTOR_LEN + 1;
    464 			RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
    465 			pos += RSN_SELECTOR_LEN;
    466 			*pos++ = 0x01;
    467 			kde_len = pos - kde;
    468 		}
    469 	}
    470 #endif /* CONFIG_P2P */
    471 
    472 	if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
    473 				       kde, kde_len, ptk))
    474 		goto failed;
    475 
    476 	os_free(kde_buf);
    477 	os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
    478 	return;
    479 
    480 failed:
    481 	os_free(kde_buf);
    482 	wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
    483 }
    484 
    485 
    486 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
    487 {
    488 	struct wpa_sm *sm = eloop_ctx;
    489 	rsn_preauth_candidate_process(sm);
    490 }
    491 
    492 
    493 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
    494 					    const u8 *addr, int secure)
    495 {
    496 	wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
    497 		"WPA: Key negotiation completed with "
    498 		MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
    499 		wpa_cipher_txt(sm->pairwise_cipher),
    500 		wpa_cipher_txt(sm->group_cipher));
    501 	wpa_sm_cancel_auth_timeout(sm);
    502 	wpa_sm_set_state(sm, WPA_COMPLETED);
    503 
    504 	if (secure) {
    505 		wpa_sm_mlme_setprotection(
    506 			sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
    507 			MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
    508 		eapol_sm_notify_portValid(sm->eapol, TRUE);
    509 		if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
    510 			eapol_sm_notify_eap_success(sm->eapol, TRUE);
    511 		/*
    512 		 * Start preauthentication after a short wait to avoid a
    513 		 * possible race condition between the data receive and key
    514 		 * configuration after the 4-Way Handshake. This increases the
    515 		 * likelihood of the first preauth EAPOL-Start frame getting to
    516 		 * the target AP.
    517 		 */
    518 		eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
    519 	}
    520 
    521 	if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
    522 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
    523 			"RSN: Authenticator accepted "
    524 			"opportunistic PMKSA entry - marking it valid");
    525 		sm->cur_pmksa->opportunistic = 0;
    526 	}
    527 
    528 #ifdef CONFIG_IEEE80211R
    529 	if (wpa_key_mgmt_ft(sm->key_mgmt)) {
    530 		/* Prepare for the next transition */
    531 		wpa_ft_prepare_auth_request(sm, NULL);
    532 	}
    533 #endif /* CONFIG_IEEE80211R */
    534 }
    535 
    536 
    537 static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
    538 {
    539 	struct wpa_sm *sm = eloop_ctx;
    540 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
    541 	wpa_sm_key_request(sm, 0, 1);
    542 }
    543 
    544 
    545 static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
    546 				      const struct wpa_eapol_key *key)
    547 {
    548 	int keylen, rsclen;
    549 	enum wpa_alg alg;
    550 	const u8 *key_rsc;
    551 	u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
    552 
    553 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
    554 		"WPA: Installing PTK to the driver");
    555 
    556 	if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
    557 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
    558 			"Suite: NONE - do not use pairwise keys");
    559 		return 0;
    560 	}
    561 
    562 	if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
    563 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
    564 			"WPA: Unsupported pairwise cipher %d",
    565 			sm->pairwise_cipher);
    566 		return -1;
    567 	}
    568 
    569 	alg = wpa_cipher_to_alg(sm->pairwise_cipher);
    570 	keylen = wpa_cipher_key_len(sm->pairwise_cipher);
    571 	rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
    572 
    573 	if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
    574 		key_rsc = null_rsc;
    575 	} else {
    576 		key_rsc = key->key_rsc;
    577 		wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
    578 	}
    579 
    580 	if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
    581 			   (u8 *) sm->ptk.tk1, keylen) < 0) {
    582 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
    583 			"WPA: Failed to set PTK to the "
    584 			"driver (alg=%d keylen=%d bssid=" MACSTR ")",
    585 			alg, keylen, MAC2STR(sm->bssid));
    586 		return -1;
    587 	}
    588 
    589 	if (sm->wpa_ptk_rekey) {
    590 		eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
    591 		eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
    592 				       sm, NULL);
    593 	}
    594 
    595 	return 0;
    596 }
    597 
    598 
    599 static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
    600 					     int group_cipher,
    601 					     int keylen, int maxkeylen,
    602 					     int *key_rsc_len,
    603 					     enum wpa_alg *alg)
    604 {
    605 	int klen;
    606 
    607 	*alg = wpa_cipher_to_alg(group_cipher);
    608 	if (*alg == WPA_ALG_NONE) {
    609 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
    610 			"WPA: Unsupported Group Cipher %d",
    611 			group_cipher);
    612 		return -1;
    613 	}
    614 	*key_rsc_len = wpa_cipher_rsc_len(group_cipher);
    615 
    616 	klen = wpa_cipher_key_len(group_cipher);
    617 	if (keylen != klen || maxkeylen < klen) {
    618 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
    619 			"WPA: Unsupported %s Group Cipher key length %d (%d)",
    620 			wpa_cipher_txt(group_cipher), keylen, maxkeylen);
    621 		return -1;
    622 	}
    623 	return 0;
    624 }
    625 
    626 
    627 struct wpa_gtk_data {
    628 	enum wpa_alg alg;
    629 	int tx, key_rsc_len, keyidx;
    630 	u8 gtk[32];
    631 	int gtk_len;
    632 };
    633 
    634 
    635 static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
    636 				      const struct wpa_gtk_data *gd,
    637 				      const u8 *key_rsc)
    638 {
    639 	const u8 *_gtk = gd->gtk;
    640 	u8 gtk_buf[32];
    641 
    642 	wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
    643 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
    644 		"WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
    645 		gd->keyidx, gd->tx, gd->gtk_len);
    646 	wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
    647 	if (sm->group_cipher == WPA_CIPHER_TKIP) {
    648 		/* Swap Tx/Rx keys for Michael MIC */
    649 		os_memcpy(gtk_buf, gd->gtk, 16);
    650 		os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
    651 		os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
    652 		_gtk = gtk_buf;
    653 	}
    654 	if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
    655 		if (wpa_sm_set_key(sm, gd->alg, NULL,
    656 				   gd->keyidx, 1, key_rsc, gd->key_rsc_len,
    657 				   _gtk, gd->gtk_len) < 0) {
    658 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
    659 				"WPA: Failed to set GTK to the driver "
    660 				"(Group only)");
    661 			os_memset(gtk_buf, 0, sizeof(gtk_buf));
    662 			return -1;
    663 		}
    664 	} else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
    665 				  gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
    666 				  _gtk, gd->gtk_len) < 0) {
    667 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
    668 			"WPA: Failed to set GTK to "
    669 			"the driver (alg=%d keylen=%d keyidx=%d)",
    670 			gd->alg, gd->gtk_len, gd->keyidx);
    671 		os_memset(gtk_buf, 0, sizeof(gtk_buf));
    672 		return -1;
    673 	}
    674 	os_memset(gtk_buf, 0, sizeof(gtk_buf));
    675 
    676 	return 0;
    677 }
    678 
    679 
    680 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
    681 						int tx)
    682 {
    683 	if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
    684 		/* Ignore Tx bit for GTK if a pairwise key is used. One AP
    685 		 * seemed to set this bit (incorrectly, since Tx is only when
    686 		 * doing Group Key only APs) and without this workaround, the
    687 		 * data connection does not work because wpa_supplicant
    688 		 * configured non-zero keyidx to be used for unicast. */
    689 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
    690 			"WPA: Tx bit set for GTK, but pairwise "
    691 			"keys are used - ignore Tx bit");
    692 		return 0;
    693 	}
    694 	return tx;
    695 }
    696 
    697 
    698 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
    699 				       const struct wpa_eapol_key *key,
    700 				       const u8 *gtk, size_t gtk_len,
    701 				       int key_info)
    702 {
    703 	struct wpa_gtk_data gd;
    704 
    705 	/*
    706 	 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
    707 	 * GTK KDE format:
    708 	 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
    709 	 * Reserved [bits 0-7]
    710 	 * GTK
    711 	 */
    712 
    713 	os_memset(&gd, 0, sizeof(gd));
    714 	wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
    715 			gtk, gtk_len);
    716 
    717 	if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
    718 		return -1;
    719 
    720 	gd.keyidx = gtk[0] & 0x3;
    721 	gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
    722 						     !!(gtk[0] & BIT(2)));
    723 	gtk += 2;
    724 	gtk_len -= 2;
    725 
    726 	os_memcpy(gd.gtk, gtk, gtk_len);
    727 	gd.gtk_len = gtk_len;
    728 
    729 	if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
    730 	    (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
    731 					       gtk_len, gtk_len,
    732 					       &gd.key_rsc_len, &gd.alg) ||
    733 	     wpa_supplicant_install_gtk(sm, &gd, key->key_rsc))) {
    734 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
    735 			"RSN: Failed to install GTK");
    736 		os_memset(&gd, 0, sizeof(gd));
    737 		return -1;
    738 	}
    739 	os_memset(&gd, 0, sizeof(gd));
    740 
    741 	wpa_supplicant_key_neg_complete(sm, sm->bssid,
    742 					key_info & WPA_KEY_INFO_SECURE);
    743 	return 0;
    744 }
    745 
    746 
    747 static int ieee80211w_set_keys(struct wpa_sm *sm,
    748 			       struct wpa_eapol_ie_parse *ie)
    749 {
    750 #ifdef CONFIG_IEEE80211W
    751 	if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
    752 		return 0;
    753 
    754 	if (ie->igtk) {
    755 		size_t len;
    756 		const struct wpa_igtk_kde *igtk;
    757 		u16 keyidx;
    758 		len = wpa_cipher_key_len(sm->mgmt_group_cipher);
    759 		if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
    760 			return -1;
    761 		igtk = (const struct wpa_igtk_kde *) ie->igtk;
    762 		keyidx = WPA_GET_LE16(igtk->keyid);
    763 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: IGTK keyid %d "
    764 			"pn %02x%02x%02x%02x%02x%02x",
    765 			keyidx, MAC2STR(igtk->pn));
    766 		wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
    767 				igtk->igtk, len);
    768 		if (keyidx > 4095) {
    769 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
    770 				"WPA: Invalid IGTK KeyID %d", keyidx);
    771 			return -1;
    772 		}
    773 		if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
    774 				   broadcast_ether_addr,
    775 				   keyidx, 0, igtk->pn, sizeof(igtk->pn),
    776 				   igtk->igtk, len) < 0) {
    777 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
    778 				"WPA: Failed to configure IGTK to the driver");
    779 			return -1;
    780 		}
    781 	}
    782 
    783 	return 0;
    784 #else /* CONFIG_IEEE80211W */
    785 	return 0;
    786 #endif /* CONFIG_IEEE80211W */
    787 }
    788 
    789 
    790 static void wpa_report_ie_mismatch(struct wpa_sm *sm,
    791 				   const char *reason, const u8 *src_addr,
    792 				   const u8 *wpa_ie, size_t wpa_ie_len,
    793 				   const u8 *rsn_ie, size_t rsn_ie_len)
    794 {
    795 	wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
    796 		reason, MAC2STR(src_addr));
    797 
    798 	if (sm->ap_wpa_ie) {
    799 		wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
    800 			    sm->ap_wpa_ie, sm->ap_wpa_ie_len);
    801 	}
    802 	if (wpa_ie) {
    803 		if (!sm->ap_wpa_ie) {
    804 			wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
    805 				"WPA: No WPA IE in Beacon/ProbeResp");
    806 		}
    807 		wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
    808 			    wpa_ie, wpa_ie_len);
    809 	}
    810 
    811 	if (sm->ap_rsn_ie) {
    812 		wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
    813 			    sm->ap_rsn_ie, sm->ap_rsn_ie_len);
    814 	}
    815 	if (rsn_ie) {
    816 		if (!sm->ap_rsn_ie) {
    817 			wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
    818 				"WPA: No RSN IE in Beacon/ProbeResp");
    819 		}
    820 		wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
    821 			    rsn_ie, rsn_ie_len);
    822 	}
    823 
    824 	wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
    825 }
    826 
    827 
    828 #ifdef CONFIG_IEEE80211R
    829 
    830 static int ft_validate_mdie(struct wpa_sm *sm,
    831 			    const unsigned char *src_addr,
    832 			    struct wpa_eapol_ie_parse *ie,
    833 			    const u8 *assoc_resp_mdie)
    834 {
    835 	struct rsn_mdie *mdie;
    836 
    837 	mdie = (struct rsn_mdie *) (ie->mdie + 2);
    838 	if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
    839 	    os_memcmp(mdie->mobility_domain, sm->mobility_domain,
    840 		      MOBILITY_DOMAIN_ID_LEN) != 0) {
    841 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
    842 			"not match with the current mobility domain");
    843 		return -1;
    844 	}
    845 
    846 	if (assoc_resp_mdie &&
    847 	    (assoc_resp_mdie[1] != ie->mdie[1] ||
    848 	     os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
    849 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
    850 		wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
    851 			    ie->mdie, 2 + ie->mdie[1]);
    852 		wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
    853 			    assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
    854 		return -1;
    855 	}
    856 
    857 	return 0;
    858 }
    859 
    860 
    861 static int ft_validate_ftie(struct wpa_sm *sm,
    862 			    const unsigned char *src_addr,
    863 			    struct wpa_eapol_ie_parse *ie,
    864 			    const u8 *assoc_resp_ftie)
    865 {
    866 	if (ie->ftie == NULL) {
    867 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
    868 			"FT: No FTIE in EAPOL-Key msg 3/4");
    869 		return -1;
    870 	}
    871 
    872 	if (assoc_resp_ftie == NULL)
    873 		return 0;
    874 
    875 	if (assoc_resp_ftie[1] != ie->ftie[1] ||
    876 	    os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
    877 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
    878 		wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
    879 			    ie->ftie, 2 + ie->ftie[1]);
    880 		wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
    881 			    assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
    882 		return -1;
    883 	}
    884 
    885 	return 0;
    886 }
    887 
    888 
    889 static int ft_validate_rsnie(struct wpa_sm *sm,
    890 			     const unsigned char *src_addr,
    891 			     struct wpa_eapol_ie_parse *ie)
    892 {
    893 	struct wpa_ie_data rsn;
    894 
    895 	if (!ie->rsn_ie)
    896 		return 0;
    897 
    898 	/*
    899 	 * Verify that PMKR1Name from EAPOL-Key message 3/4
    900 	 * matches with the value we derived.
    901 	 */
    902 	if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
    903 	    rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
    904 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
    905 			"FT 4-way handshake message 3/4");
    906 		return -1;
    907 	}
    908 
    909 	if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
    910 	{
    911 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
    912 			"FT: PMKR1Name mismatch in "
    913 			"FT 4-way handshake message 3/4");
    914 		wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
    915 			    rsn.pmkid, WPA_PMK_NAME_LEN);
    916 		wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
    917 			    sm->pmk_r1_name, WPA_PMK_NAME_LEN);
    918 		return -1;
    919 	}
    920 
    921 	return 0;
    922 }
    923 
    924 
    925 static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
    926 					 const unsigned char *src_addr,
    927 					 struct wpa_eapol_ie_parse *ie)
    928 {
    929 	const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
    930 
    931 	if (sm->assoc_resp_ies) {
    932 		pos = sm->assoc_resp_ies;
    933 		end = pos + sm->assoc_resp_ies_len;
    934 		while (pos + 2 < end) {
    935 			if (pos + 2 + pos[1] > end)
    936 				break;
    937 			switch (*pos) {
    938 			case WLAN_EID_MOBILITY_DOMAIN:
    939 				mdie = pos;
    940 				break;
    941 			case WLAN_EID_FAST_BSS_TRANSITION:
    942 				ftie = pos;
    943 				break;
    944 			}
    945 			pos += 2 + pos[1];
    946 		}
    947 	}
    948 
    949 	if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
    950 	    ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
    951 	    ft_validate_rsnie(sm, src_addr, ie) < 0)
    952 		return -1;
    953 
    954 	return 0;
    955 }
    956 
    957 #endif /* CONFIG_IEEE80211R */
    958 
    959 
    960 static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
    961 				      const unsigned char *src_addr,
    962 				      struct wpa_eapol_ie_parse *ie)
    963 {
    964 	if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
    965 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
    966 			"WPA: No WPA/RSN IE for this AP known. "
    967 			"Trying to get from scan results");
    968 		if (wpa_sm_get_beacon_ie(sm) < 0) {
    969 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
    970 				"WPA: Could not find AP from "
    971 				"the scan results");
    972 		} else {
    973 			wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
    974 				"WPA: Found the current AP from "
    975 				"updated scan results");
    976 		}
    977 	}
    978 
    979 	if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
    980 	    (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
    981 		wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
    982 				       "with IE in Beacon/ProbeResp (no IE?)",
    983 				       src_addr, ie->wpa_ie, ie->wpa_ie_len,
    984 				       ie->rsn_ie, ie->rsn_ie_len);
    985 		return -1;
    986 	}
    987 
    988 	if ((ie->wpa_ie && sm->ap_wpa_ie &&
    989 	     (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
    990 	      os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
    991 	    (ie->rsn_ie && sm->ap_rsn_ie &&
    992 	     wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
    993 				sm->ap_rsn_ie, sm->ap_rsn_ie_len,
    994 				ie->rsn_ie, ie->rsn_ie_len))) {
    995 		wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
    996 				       "with IE in Beacon/ProbeResp",
    997 				       src_addr, ie->wpa_ie, ie->wpa_ie_len,
    998 				       ie->rsn_ie, ie->rsn_ie_len);
    999 		return -1;
   1000 	}
   1001 
   1002 	if (sm->proto == WPA_PROTO_WPA &&
   1003 	    ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
   1004 		wpa_report_ie_mismatch(sm, "Possible downgrade attack "
   1005 				       "detected - RSN was enabled and RSN IE "
   1006 				       "was in msg 3/4, but not in "
   1007 				       "Beacon/ProbeResp",
   1008 				       src_addr, ie->wpa_ie, ie->wpa_ie_len,
   1009 				       ie->rsn_ie, ie->rsn_ie_len);
   1010 		return -1;
   1011 	}
   1012 
   1013 #ifdef CONFIG_IEEE80211R
   1014 	if (wpa_key_mgmt_ft(sm->key_mgmt) &&
   1015 	    wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
   1016 		return -1;
   1017 #endif /* CONFIG_IEEE80211R */
   1018 
   1019 	return 0;
   1020 }
   1021 
   1022 
   1023 /**
   1024  * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
   1025  * @sm: Pointer to WPA state machine data from wpa_sm_init()
   1026  * @dst: Destination address for the frame
   1027  * @key: Pointer to the EAPOL-Key frame header
   1028  * @ver: Version bits from EAPOL-Key Key Info
   1029  * @key_info: Key Info
   1030  * @ptk: PTK to use for keyed hash and encryption
   1031  * Returns: 0 on success, -1 on failure
   1032  */
   1033 int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
   1034 			       const struct wpa_eapol_key *key,
   1035 			       u16 ver, u16 key_info,
   1036 			       struct wpa_ptk *ptk)
   1037 {
   1038 	size_t rlen;
   1039 	struct wpa_eapol_key *reply;
   1040 	u8 *rbuf;
   1041 
   1042 	rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
   1043 				  sizeof(*reply), &rlen, (void *) &reply);
   1044 	if (rbuf == NULL)
   1045 		return -1;
   1046 
   1047 	reply->type = (sm->proto == WPA_PROTO_RSN ||
   1048 		       sm->proto == WPA_PROTO_OSEN) ?
   1049 		EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
   1050 	key_info &= WPA_KEY_INFO_SECURE;
   1051 	key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
   1052 	WPA_PUT_BE16(reply->key_info, key_info);
   1053 	if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
   1054 		WPA_PUT_BE16(reply->key_length, 0);
   1055 	else
   1056 		os_memcpy(reply->key_length, key->key_length, 2);
   1057 	os_memcpy(reply->replay_counter, key->replay_counter,
   1058 		  WPA_REPLAY_COUNTER_LEN);
   1059 
   1060 	WPA_PUT_BE16(reply->key_data_length, 0);
   1061 
   1062 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
   1063 	wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
   1064 			   rbuf, rlen, reply->key_mic);
   1065 
   1066 	return 0;
   1067 }
   1068 
   1069 
   1070 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
   1071 					  const struct wpa_eapol_key *key,
   1072 					  u16 ver, const u8 *key_data,
   1073 					  size_t key_data_len)
   1074 {
   1075 	u16 key_info, keylen;
   1076 	struct wpa_eapol_ie_parse ie;
   1077 
   1078 	wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
   1079 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
   1080 		"Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
   1081 
   1082 	key_info = WPA_GET_BE16(key->key_info);
   1083 
   1084 	wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
   1085 	if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
   1086 		goto failed;
   1087 	if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
   1088 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1089 			"WPA: GTK IE in unencrypted key data");
   1090 		goto failed;
   1091 	}
   1092 #ifdef CONFIG_IEEE80211W
   1093 	if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
   1094 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1095 			"WPA: IGTK KDE in unencrypted key data");
   1096 		goto failed;
   1097 	}
   1098 
   1099 	if (ie.igtk &&
   1100 	    wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
   1101 	    ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
   1102 	    (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
   1103 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1104 			"WPA: Invalid IGTK KDE length %lu",
   1105 			(unsigned long) ie.igtk_len);
   1106 		goto failed;
   1107 	}
   1108 #endif /* CONFIG_IEEE80211W */
   1109 
   1110 	if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
   1111 		goto failed;
   1112 
   1113 	if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
   1114 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1115 			"WPA: ANonce from message 1 of 4-Way Handshake "
   1116 			"differs from 3 of 4-Way Handshake - drop packet (src="
   1117 			MACSTR ")", MAC2STR(sm->bssid));
   1118 		goto failed;
   1119 	}
   1120 
   1121 	keylen = WPA_GET_BE16(key->key_length);
   1122 	if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
   1123 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1124 			"WPA: Invalid %s key length %d (src=" MACSTR
   1125 			")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
   1126 			MAC2STR(sm->bssid));
   1127 		goto failed;
   1128 	}
   1129 
   1130 #ifdef CONFIG_P2P
   1131 	if (ie.ip_addr_alloc) {
   1132 		os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
   1133 		wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
   1134 			    sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
   1135 	}
   1136 #endif /* CONFIG_P2P */
   1137 
   1138 	if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
   1139 				       &sm->ptk)) {
   1140 		goto failed;
   1141 	}
   1142 
   1143 	/* SNonce was successfully used in msg 3/4, so mark it to be renewed
   1144 	 * for the next 4-Way Handshake. If msg 3 is received again, the old
   1145 	 * SNonce will still be used to avoid changing PTK. */
   1146 	sm->renew_snonce = 1;
   1147 
   1148 	if (key_info & WPA_KEY_INFO_INSTALL) {
   1149 		if (wpa_supplicant_install_ptk(sm, key))
   1150 			goto failed;
   1151 	}
   1152 
   1153 	if (key_info & WPA_KEY_INFO_SECURE) {
   1154 		wpa_sm_mlme_setprotection(
   1155 			sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
   1156 			MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
   1157 		eapol_sm_notify_portValid(sm->eapol, TRUE);
   1158 	}
   1159 	wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
   1160 
   1161 	if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
   1162 		wpa_supplicant_key_neg_complete(sm, sm->bssid,
   1163 						key_info & WPA_KEY_INFO_SECURE);
   1164 	} else if (ie.gtk &&
   1165 	    wpa_supplicant_pairwise_gtk(sm, key,
   1166 					ie.gtk, ie.gtk_len, key_info) < 0) {
   1167 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
   1168 			"RSN: Failed to configure GTK");
   1169 		goto failed;
   1170 	}
   1171 
   1172 	if (ieee80211w_set_keys(sm, &ie) < 0) {
   1173 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
   1174 			"RSN: Failed to configure IGTK");
   1175 		goto failed;
   1176 	}
   1177 
   1178 	if (ie.gtk)
   1179 		wpa_sm_set_rekey_offload(sm);
   1180 
   1181 	return;
   1182 
   1183 failed:
   1184 	wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
   1185 }
   1186 
   1187 
   1188 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
   1189 					     const u8 *keydata,
   1190 					     size_t keydatalen,
   1191 					     u16 key_info,
   1192 					     struct wpa_gtk_data *gd)
   1193 {
   1194 	int maxkeylen;
   1195 	struct wpa_eapol_ie_parse ie;
   1196 
   1197 	wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
   1198 	if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
   1199 		return -1;
   1200 	if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
   1201 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1202 			"WPA: GTK IE in unencrypted key data");
   1203 		return -1;
   1204 	}
   1205 	if (ie.gtk == NULL) {
   1206 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
   1207 			"WPA: No GTK IE in Group Key msg 1/2");
   1208 		return -1;
   1209 	}
   1210 	maxkeylen = gd->gtk_len = ie.gtk_len - 2;
   1211 
   1212 	if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
   1213 					      gd->gtk_len, maxkeylen,
   1214 					      &gd->key_rsc_len, &gd->alg))
   1215 		return -1;
   1216 
   1217 	wpa_hexdump(MSG_DEBUG, "RSN: received GTK in group key handshake",
   1218 		    ie.gtk, ie.gtk_len);
   1219 	gd->keyidx = ie.gtk[0] & 0x3;
   1220 	gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
   1221 						      !!(ie.gtk[0] & BIT(2)));
   1222 	if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
   1223 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
   1224 			"RSN: Too long GTK in GTK IE (len=%lu)",
   1225 			(unsigned long) ie.gtk_len - 2);
   1226 		return -1;
   1227 	}
   1228 	os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
   1229 
   1230 	if (ieee80211w_set_keys(sm, &ie) < 0)
   1231 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
   1232 			"RSN: Failed to configure IGTK");
   1233 
   1234 	return 0;
   1235 }
   1236 
   1237 
   1238 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
   1239 					     const struct wpa_eapol_key *key,
   1240 					     const u8 *key_data,
   1241 					     size_t key_data_len, u16 key_info,
   1242 					     u16 ver, struct wpa_gtk_data *gd)
   1243 {
   1244 	size_t maxkeylen;
   1245 
   1246 	gd->gtk_len = WPA_GET_BE16(key->key_length);
   1247 	maxkeylen = key_data_len;
   1248 	if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
   1249 		if (maxkeylen < 8) {
   1250 			wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
   1251 				"WPA: Too short maxkeylen (%lu)",
   1252 				(unsigned long) maxkeylen);
   1253 			return -1;
   1254 		}
   1255 		maxkeylen -= 8;
   1256 	}
   1257 
   1258 	if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
   1259 					      gd->gtk_len, maxkeylen,
   1260 					      &gd->key_rsc_len, &gd->alg))
   1261 		return -1;
   1262 
   1263 	gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
   1264 		WPA_KEY_INFO_KEY_INDEX_SHIFT;
   1265 	if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
   1266 		u8 ek[32];
   1267 		if (key_data_len > sizeof(gd->gtk)) {
   1268 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1269 				"WPA: RC4 key data too long (%lu)",
   1270 				(unsigned long) key_data_len);
   1271 			return -1;
   1272 		}
   1273 		os_memcpy(ek, key->key_iv, 16);
   1274 		os_memcpy(ek + 16, sm->ptk.kek, 16);
   1275 		os_memcpy(gd->gtk, key_data, key_data_len);
   1276 		if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
   1277 			os_memset(ek, 0, sizeof(ek));
   1278 			wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
   1279 				"WPA: RC4 failed");
   1280 			return -1;
   1281 		}
   1282 		os_memset(ek, 0, sizeof(ek));
   1283 	} else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
   1284 		if (maxkeylen % 8) {
   1285 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1286 				"WPA: Unsupported AES-WRAP len %lu",
   1287 				(unsigned long) maxkeylen);
   1288 			return -1;
   1289 		}
   1290 		if (maxkeylen > sizeof(gd->gtk)) {
   1291 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1292 				"WPA: AES-WRAP key data "
   1293 				"too long (keydatalen=%lu maxkeylen=%lu)",
   1294 				(unsigned long) key_data_len,
   1295 				(unsigned long) maxkeylen);
   1296 			return -1;
   1297 		}
   1298 		if (aes_unwrap(sm->ptk.kek, maxkeylen / 8, key_data, gd->gtk)) {
   1299 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1300 				"WPA: AES unwrap failed - could not decrypt "
   1301 				"GTK");
   1302 			return -1;
   1303 		}
   1304 	} else {
   1305 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1306 			"WPA: Unsupported key_info type %d", ver);
   1307 		return -1;
   1308 	}
   1309 	gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
   1310 		sm, !!(key_info & WPA_KEY_INFO_TXRX));
   1311 	return 0;
   1312 }
   1313 
   1314 
   1315 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
   1316 				      const struct wpa_eapol_key *key,
   1317 				      int ver, u16 key_info)
   1318 {
   1319 	size_t rlen;
   1320 	struct wpa_eapol_key *reply;
   1321 	u8 *rbuf;
   1322 
   1323 	rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
   1324 				  sizeof(*reply), &rlen, (void *) &reply);
   1325 	if (rbuf == NULL)
   1326 		return -1;
   1327 
   1328 	reply->type = (sm->proto == WPA_PROTO_RSN ||
   1329 		       sm->proto == WPA_PROTO_OSEN) ?
   1330 		EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
   1331 	key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
   1332 	key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
   1333 	WPA_PUT_BE16(reply->key_info, key_info);
   1334 	if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
   1335 		WPA_PUT_BE16(reply->key_length, 0);
   1336 	else
   1337 		os_memcpy(reply->key_length, key->key_length, 2);
   1338 	os_memcpy(reply->replay_counter, key->replay_counter,
   1339 		  WPA_REPLAY_COUNTER_LEN);
   1340 
   1341 	WPA_PUT_BE16(reply->key_data_length, 0);
   1342 
   1343 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
   1344 	wpa_eapol_key_send(sm, sm->ptk.kck, ver, sm->bssid, ETH_P_EAPOL,
   1345 			   rbuf, rlen, reply->key_mic);
   1346 
   1347 	return 0;
   1348 }
   1349 
   1350 
   1351 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
   1352 					  const unsigned char *src_addr,
   1353 					  const struct wpa_eapol_key *key,
   1354 					  const u8 *key_data,
   1355 					  size_t key_data_len, u16 ver)
   1356 {
   1357 	u16 key_info;
   1358 	int rekey, ret;
   1359 	struct wpa_gtk_data gd;
   1360 
   1361 	os_memset(&gd, 0, sizeof(gd));
   1362 
   1363 	rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
   1364 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
   1365 		"Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
   1366 
   1367 	key_info = WPA_GET_BE16(key->key_info);
   1368 
   1369 	if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
   1370 		ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
   1371 							key_data_len, key_info,
   1372 							&gd);
   1373 	} else {
   1374 		ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
   1375 							key_data_len,
   1376 							key_info, ver, &gd);
   1377 	}
   1378 
   1379 	wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
   1380 
   1381 	if (ret)
   1382 		goto failed;
   1383 
   1384 	if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc) ||
   1385 	    wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
   1386 		goto failed;
   1387 
   1388 	if (rekey) {
   1389 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
   1390 			"completed with " MACSTR " [GTK=%s]",
   1391 			MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
   1392 		wpa_sm_cancel_auth_timeout(sm);
   1393 		wpa_sm_set_state(sm, WPA_COMPLETED);
   1394 	} else {
   1395 		wpa_supplicant_key_neg_complete(sm, sm->bssid,
   1396 						key_info &
   1397 						WPA_KEY_INFO_SECURE);
   1398 	}
   1399 
   1400 	wpa_sm_set_rekey_offload(sm);
   1401 
   1402 	return;
   1403 
   1404 failed:
   1405 	wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
   1406 }
   1407 
   1408 
   1409 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
   1410 					       struct wpa_eapol_key *key,
   1411 					       u16 ver,
   1412 					       const u8 *buf, size_t len)
   1413 {
   1414 	u8 mic[16];
   1415 	int ok = 0;
   1416 
   1417 	os_memcpy(mic, key->key_mic, 16);
   1418 	if (sm->tptk_set) {
   1419 		os_memset(key->key_mic, 0, 16);
   1420 		wpa_eapol_key_mic(sm->tptk.kck, ver, buf, len,
   1421 				  key->key_mic);
   1422 		if (os_memcmp_const(mic, key->key_mic, 16) != 0) {
   1423 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1424 				"WPA: Invalid EAPOL-Key MIC "
   1425 				"when using TPTK - ignoring TPTK");
   1426 		} else {
   1427 			ok = 1;
   1428 			sm->tptk_set = 0;
   1429 			sm->ptk_set = 1;
   1430 			os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
   1431 			os_memset(&sm->tptk, 0, sizeof(sm->tptk));
   1432 		}
   1433 	}
   1434 
   1435 	if (!ok && sm->ptk_set) {
   1436 		os_memset(key->key_mic, 0, 16);
   1437 		wpa_eapol_key_mic(sm->ptk.kck, ver, buf, len,
   1438 				  key->key_mic);
   1439 		if (os_memcmp_const(mic, key->key_mic, 16) != 0) {
   1440 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1441 				"WPA: Invalid EAPOL-Key MIC - "
   1442 				"dropping packet");
   1443 			return -1;
   1444 		}
   1445 		ok = 1;
   1446 	}
   1447 
   1448 	if (!ok) {
   1449 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1450 			"WPA: Could not verify EAPOL-Key MIC - "
   1451 			"dropping packet");
   1452 		return -1;
   1453 	}
   1454 
   1455 	os_memcpy(sm->rx_replay_counter, key->replay_counter,
   1456 		  WPA_REPLAY_COUNTER_LEN);
   1457 	sm->rx_replay_counter_set = 1;
   1458 	return 0;
   1459 }
   1460 
   1461 
   1462 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
   1463 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
   1464 					   struct wpa_eapol_key *key, u16 ver,
   1465 					   u8 *key_data, size_t *key_data_len)
   1466 {
   1467 	wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
   1468 		    key_data, *key_data_len);
   1469 	if (!sm->ptk_set) {
   1470 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1471 			"WPA: PTK not available, cannot decrypt EAPOL-Key Key "
   1472 			"Data");
   1473 		return -1;
   1474 	}
   1475 
   1476 	/* Decrypt key data here so that this operation does not need
   1477 	 * to be implemented separately for each message type. */
   1478 	if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
   1479 		u8 ek[32];
   1480 		os_memcpy(ek, key->key_iv, 16);
   1481 		os_memcpy(ek + 16, sm->ptk.kek, 16);
   1482 		if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
   1483 			os_memset(ek, 0, sizeof(ek));
   1484 			wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
   1485 				"WPA: RC4 failed");
   1486 			return -1;
   1487 		}
   1488 		os_memset(ek, 0, sizeof(ek));
   1489 	} else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
   1490 		   ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
   1491 		   sm->key_mgmt == WPA_KEY_MGMT_OSEN) {
   1492 		u8 *buf;
   1493 		if (*key_data_len < 8 || *key_data_len % 8) {
   1494 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1495 				"WPA: Unsupported AES-WRAP len %u",
   1496 				(unsigned int) *key_data_len);
   1497 			return -1;
   1498 		}
   1499 		*key_data_len -= 8; /* AES-WRAP adds 8 bytes */
   1500 		buf = os_malloc(*key_data_len);
   1501 		if (buf == NULL) {
   1502 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1503 				"WPA: No memory for AES-UNWRAP buffer");
   1504 			return -1;
   1505 		}
   1506 		if (aes_unwrap(sm->ptk.kek, *key_data_len / 8,
   1507 			       key_data, buf)) {
   1508 			os_free(buf);
   1509 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1510 				"WPA: AES unwrap failed - "
   1511 				"could not decrypt EAPOL-Key key data");
   1512 			return -1;
   1513 		}
   1514 		os_memcpy(key_data, buf, *key_data_len);
   1515 		os_free(buf);
   1516 		WPA_PUT_BE16(key->key_data_length, *key_data_len);
   1517 	} else {
   1518 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1519 			"WPA: Unsupported key_info type %d", ver);
   1520 		return -1;
   1521 	}
   1522 	wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
   1523 			key_data, *key_data_len);
   1524 	return 0;
   1525 }
   1526 
   1527 
   1528 /**
   1529  * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
   1530  * @sm: Pointer to WPA state machine data from wpa_sm_init()
   1531  */
   1532 void wpa_sm_aborted_cached(struct wpa_sm *sm)
   1533 {
   1534 	if (sm && sm->cur_pmksa) {
   1535 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
   1536 			"RSN: Cancelling PMKSA caching attempt");
   1537 		sm->cur_pmksa = NULL;
   1538 	}
   1539 }
   1540 
   1541 
   1542 static void wpa_eapol_key_dump(struct wpa_sm *sm,
   1543 			       const struct wpa_eapol_key *key)
   1544 {
   1545 #ifndef CONFIG_NO_STDOUT_DEBUG
   1546 	u16 key_info = WPA_GET_BE16(key->key_info);
   1547 
   1548 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "  EAPOL-Key type=%d", key->type);
   1549 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
   1550 		"  key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
   1551 		key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
   1552 		(key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
   1553 		WPA_KEY_INFO_KEY_INDEX_SHIFT,
   1554 		(key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
   1555 		key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
   1556 		key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
   1557 		key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
   1558 		key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
   1559 		key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
   1560 		key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
   1561 		key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
   1562 		key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
   1563 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
   1564 		"  key_length=%u key_data_length=%u",
   1565 		WPA_GET_BE16(key->key_length),
   1566 		WPA_GET_BE16(key->key_data_length));
   1567 	wpa_hexdump(MSG_DEBUG, "  replay_counter",
   1568 		    key->replay_counter, WPA_REPLAY_COUNTER_LEN);
   1569 	wpa_hexdump(MSG_DEBUG, "  key_nonce", key->key_nonce, WPA_NONCE_LEN);
   1570 	wpa_hexdump(MSG_DEBUG, "  key_iv", key->key_iv, 16);
   1571 	wpa_hexdump(MSG_DEBUG, "  key_rsc", key->key_rsc, 8);
   1572 	wpa_hexdump(MSG_DEBUG, "  key_id (reserved)", key->key_id, 8);
   1573 	wpa_hexdump(MSG_DEBUG, "  key_mic", key->key_mic, 16);
   1574 #endif /* CONFIG_NO_STDOUT_DEBUG */
   1575 }
   1576 
   1577 
   1578 /**
   1579  * wpa_sm_rx_eapol - Process received WPA EAPOL frames
   1580  * @sm: Pointer to WPA state machine data from wpa_sm_init()
   1581  * @src_addr: Source MAC address of the EAPOL packet
   1582  * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
   1583  * @len: Length of the EAPOL frame
   1584  * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
   1585  *
   1586  * This function is called for each received EAPOL frame. Other than EAPOL-Key
   1587  * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
   1588  * only processing WPA and WPA2 EAPOL-Key frames.
   1589  *
   1590  * The received EAPOL-Key packets are validated and valid packets are replied
   1591  * to. In addition, key material (PTK, GTK) is configured at the end of a
   1592  * successful key handshake.
   1593  */
   1594 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
   1595 		    const u8 *buf, size_t len)
   1596 {
   1597 	size_t plen, data_len, key_data_len;
   1598 	const struct ieee802_1x_hdr *hdr;
   1599 	struct wpa_eapol_key *key;
   1600 	u16 key_info, ver;
   1601 	u8 *tmp = NULL;
   1602 	int ret = -1;
   1603 	struct wpa_peerkey *peerkey = NULL;
   1604 	u8 *key_data;
   1605 
   1606 #ifdef CONFIG_IEEE80211R
   1607 	sm->ft_completed = 0;
   1608 #endif /* CONFIG_IEEE80211R */
   1609 
   1610 	if (len < sizeof(*hdr) + sizeof(*key)) {
   1611 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
   1612 			"WPA: EAPOL frame too short to be a WPA "
   1613 			"EAPOL-Key (len %lu, expecting at least %lu)",
   1614 			(unsigned long) len,
   1615 			(unsigned long) sizeof(*hdr) + sizeof(*key));
   1616 		return 0;
   1617 	}
   1618 
   1619 	hdr = (const struct ieee802_1x_hdr *) buf;
   1620 	plen = be_to_host16(hdr->length);
   1621 	data_len = plen + sizeof(*hdr);
   1622 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
   1623 		"IEEE 802.1X RX: version=%d type=%d length=%lu",
   1624 		hdr->version, hdr->type, (unsigned long) plen);
   1625 
   1626 	if (hdr->version < EAPOL_VERSION) {
   1627 		/* TODO: backwards compatibility */
   1628 	}
   1629 	if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
   1630 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
   1631 			"WPA: EAPOL frame (type %u) discarded, "
   1632 			"not a Key frame", hdr->type);
   1633 		ret = 0;
   1634 		goto out;
   1635 	}
   1636 	wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
   1637 	if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
   1638 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
   1639 			"WPA: EAPOL frame payload size %lu "
   1640 			"invalid (frame size %lu)",
   1641 			(unsigned long) plen, (unsigned long) len);
   1642 		ret = 0;
   1643 		goto out;
   1644 	}
   1645 	if (data_len < len) {
   1646 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
   1647 			"WPA: ignoring %lu bytes after the IEEE 802.1X data",
   1648 			(unsigned long) len - data_len);
   1649 	}
   1650 
   1651 	/*
   1652 	 * Make a copy of the frame since we need to modify the buffer during
   1653 	 * MAC validation and Key Data decryption.
   1654 	 */
   1655 	tmp = os_malloc(data_len);
   1656 	if (tmp == NULL)
   1657 		goto out;
   1658 	os_memcpy(tmp, buf, data_len);
   1659 	key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
   1660 	key_data = (u8 *) (key + 1);
   1661 
   1662 	if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
   1663 	{
   1664 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
   1665 			"WPA: EAPOL-Key type (%d) unknown, discarded",
   1666 			key->type);
   1667 		ret = 0;
   1668 		goto out;
   1669 	}
   1670 	wpa_eapol_key_dump(sm, key);
   1671 
   1672 	key_data_len = WPA_GET_BE16(key->key_data_length);
   1673 	if (key_data_len > plen - sizeof(struct wpa_eapol_key)) {
   1674 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
   1675 			"frame - key_data overflow (%u > %u)",
   1676 			(unsigned int) key_data_len,
   1677 			(unsigned int) (plen - sizeof(struct wpa_eapol_key)));
   1678 		goto out;
   1679 	}
   1680 
   1681 	eapol_sm_notify_lower_layer_success(sm->eapol, 0);
   1682 	key_info = WPA_GET_BE16(key->key_info);
   1683 	ver = key_info & WPA_KEY_INFO_TYPE_MASK;
   1684 	if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
   1685 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
   1686 	    ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
   1687 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
   1688 	    ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
   1689 	    sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
   1690 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
   1691 			"WPA: Unsupported EAPOL-Key descriptor version %d",
   1692 			ver);
   1693 		goto out;
   1694 	}
   1695 
   1696 	if (sm->key_mgmt == WPA_KEY_MGMT_OSEN &&
   1697 	    ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
   1698 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
   1699 			"OSEN: Unsupported EAPOL-Key descriptor version %d",
   1700 			ver);
   1701 		goto out;
   1702 	}
   1703 
   1704 #ifdef CONFIG_IEEE80211R
   1705 	if (wpa_key_mgmt_ft(sm->key_mgmt)) {
   1706 		/* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
   1707 		if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
   1708 			wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
   1709 				"FT: AP did not use AES-128-CMAC");
   1710 			goto out;
   1711 		}
   1712 	} else
   1713 #endif /* CONFIG_IEEE80211R */
   1714 #ifdef CONFIG_IEEE80211W
   1715 	if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
   1716 		if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
   1717 		    sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
   1718 			wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
   1719 				"WPA: AP did not use the "
   1720 				"negotiated AES-128-CMAC");
   1721 			goto out;
   1722 		}
   1723 	} else
   1724 #endif /* CONFIG_IEEE80211W */
   1725 	if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
   1726 	    ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
   1727 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
   1728 			"WPA: CCMP is used, but EAPOL-Key "
   1729 			"descriptor version (%d) is not 2", ver);
   1730 		if (sm->group_cipher != WPA_CIPHER_CCMP &&
   1731 		    !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
   1732 			/* Earlier versions of IEEE 802.11i did not explicitly
   1733 			 * require version 2 descriptor for all EAPOL-Key
   1734 			 * packets, so allow group keys to use version 1 if
   1735 			 * CCMP is not used for them. */
   1736 			wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
   1737 				"WPA: Backwards compatibility: allow invalid "
   1738 				"version for non-CCMP group keys");
   1739 		} else
   1740 			goto out;
   1741 	} else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
   1742 		   ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
   1743 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
   1744 			"WPA: GCMP is used, but EAPOL-Key "
   1745 			"descriptor version (%d) is not 2", ver);
   1746 		goto out;
   1747 	}
   1748 
   1749 #ifdef CONFIG_PEERKEY
   1750 	for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
   1751 		if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
   1752 			break;
   1753 	}
   1754 
   1755 	if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
   1756 		if (!peerkey->initiator && peerkey->replay_counter_set &&
   1757 		    os_memcmp(key->replay_counter, peerkey->replay_counter,
   1758 			      WPA_REPLAY_COUNTER_LEN) <= 0) {
   1759 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1760 				"RSN: EAPOL-Key Replay Counter did not "
   1761 				"increase (STK) - dropping packet");
   1762 			goto out;
   1763 		} else if (peerkey->initiator) {
   1764 			u8 _tmp[WPA_REPLAY_COUNTER_LEN];
   1765 			os_memcpy(_tmp, key->replay_counter,
   1766 				  WPA_REPLAY_COUNTER_LEN);
   1767 			inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
   1768 			if (os_memcmp(_tmp, peerkey->replay_counter,
   1769 				      WPA_REPLAY_COUNTER_LEN) != 0) {
   1770 				wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
   1771 					"RSN: EAPOL-Key Replay "
   1772 					"Counter did not match (STK) - "
   1773 					"dropping packet");
   1774 				goto out;
   1775 			}
   1776 		}
   1777 	}
   1778 
   1779 	if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
   1780 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
   1781 			"RSN: Ack bit in key_info from STK peer");
   1782 		goto out;
   1783 	}
   1784 #endif /* CONFIG_PEERKEY */
   1785 
   1786 	if (!peerkey && sm->rx_replay_counter_set &&
   1787 	    os_memcmp(key->replay_counter, sm->rx_replay_counter,
   1788 		      WPA_REPLAY_COUNTER_LEN) <= 0) {
   1789 		wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1790 			"WPA: EAPOL-Key Replay Counter did not increase - "
   1791 			"dropping packet");
   1792 		goto out;
   1793 	}
   1794 
   1795 	if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
   1796 #ifdef CONFIG_PEERKEY
   1797 	    && (peerkey == NULL || !peerkey->initiator)
   1798 #endif /* CONFIG_PEERKEY */
   1799 		) {
   1800 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
   1801 			"WPA: No Ack bit in key_info");
   1802 		goto out;
   1803 	}
   1804 
   1805 	if (key_info & WPA_KEY_INFO_REQUEST) {
   1806 		wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
   1807 			"WPA: EAPOL-Key with Request bit - dropped");
   1808 		goto out;
   1809 	}
   1810 
   1811 	if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
   1812 	    wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
   1813 		goto out;
   1814 
   1815 #ifdef CONFIG_PEERKEY
   1816 	if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
   1817 	    peerkey_verify_eapol_key_mic(sm, peerkey, key, ver, tmp, data_len))
   1818 		goto out;
   1819 #endif /* CONFIG_PEERKEY */
   1820 
   1821 	if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
   1822 	    (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
   1823 		if (wpa_supplicant_decrypt_key_data(sm, key, ver, key_data,
   1824 						    &key_data_len))
   1825 			goto out;
   1826 	}
   1827 
   1828 	if (key_info & WPA_KEY_INFO_KEY_TYPE) {
   1829 		if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
   1830 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1831 				"WPA: Ignored EAPOL-Key (Pairwise) with "
   1832 				"non-zero key index");
   1833 			goto out;
   1834 		}
   1835 		if (peerkey) {
   1836 			/* PeerKey 4-Way Handshake */
   1837 			peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver,
   1838 					      key_data, key_data_len);
   1839 		} else if (key_info & WPA_KEY_INFO_MIC) {
   1840 			/* 3/4 4-Way Handshake */
   1841 			wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
   1842 						      key_data_len);
   1843 		} else {
   1844 			/* 1/4 4-Way Handshake */
   1845 			wpa_supplicant_process_1_of_4(sm, src_addr, key,
   1846 						      ver, key_data,
   1847 						      key_data_len);
   1848 		}
   1849 	} else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
   1850 		/* PeerKey SMK Handshake */
   1851 		peerkey_rx_eapol_smk(sm, src_addr, key, key_data_len, key_info,
   1852 				     ver);
   1853 	} else {
   1854 		if (key_info & WPA_KEY_INFO_MIC) {
   1855 			/* 1/2 Group Key Handshake */
   1856 			wpa_supplicant_process_1_of_2(sm, src_addr, key,
   1857 						      key_data, key_data_len,
   1858 						      ver);
   1859 		} else {
   1860 			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
   1861 				"WPA: EAPOL-Key (Group) without Mic bit - "
   1862 				"dropped");
   1863 		}
   1864 	}
   1865 
   1866 	ret = 1;
   1867 
   1868 out:
   1869 	os_free(tmp);
   1870 	return ret;
   1871 }
   1872 
   1873 
   1874 #ifdef CONFIG_CTRL_IFACE
   1875 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
   1876 {
   1877 	switch (sm->key_mgmt) {
   1878 	case WPA_KEY_MGMT_IEEE8021X:
   1879 		return ((sm->proto == WPA_PROTO_RSN ||
   1880 			 sm->proto == WPA_PROTO_OSEN) ?
   1881 			RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
   1882 			WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
   1883 	case WPA_KEY_MGMT_PSK:
   1884 		return (sm->proto == WPA_PROTO_RSN ?
   1885 			RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
   1886 			WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
   1887 #ifdef CONFIG_IEEE80211R
   1888 	case WPA_KEY_MGMT_FT_IEEE8021X:
   1889 		return RSN_AUTH_KEY_MGMT_FT_802_1X;
   1890 	case WPA_KEY_MGMT_FT_PSK:
   1891 		return RSN_AUTH_KEY_MGMT_FT_PSK;
   1892 #endif /* CONFIG_IEEE80211R */
   1893 #ifdef CONFIG_IEEE80211W
   1894 	case WPA_KEY_MGMT_IEEE8021X_SHA256:
   1895 		return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
   1896 	case WPA_KEY_MGMT_PSK_SHA256:
   1897 		return RSN_AUTH_KEY_MGMT_PSK_SHA256;
   1898 #endif /* CONFIG_IEEE80211W */
   1899 	case WPA_KEY_MGMT_CCKM:
   1900 		return (sm->proto == WPA_PROTO_RSN ?
   1901 			RSN_AUTH_KEY_MGMT_CCKM:
   1902 			WPA_AUTH_KEY_MGMT_CCKM);
   1903 	case WPA_KEY_MGMT_WPA_NONE:
   1904 		return WPA_AUTH_KEY_MGMT_NONE;
   1905 	default:
   1906 		return 0;
   1907 	}
   1908 }
   1909 
   1910 
   1911 #define RSN_SUITE "%02x-%02x-%02x-%d"
   1912 #define RSN_SUITE_ARG(s) \
   1913 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
   1914 
   1915 /**
   1916  * wpa_sm_get_mib - Dump text list of MIB entries
   1917  * @sm: Pointer to WPA state machine data from wpa_sm_init()
   1918  * @buf: Buffer for the list
   1919  * @buflen: Length of the buffer
   1920  * Returns: Number of bytes written to buffer
   1921  *
   1922  * This function is used fetch dot11 MIB variables.
   1923  */
   1924 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
   1925 {
   1926 	char pmkid_txt[PMKID_LEN * 2 + 1];
   1927 	int rsna, ret;
   1928 	size_t len;
   1929 
   1930 	if (sm->cur_pmksa) {
   1931 		wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
   1932 				 sm->cur_pmksa->pmkid, PMKID_LEN);
   1933 	} else
   1934 		pmkid_txt[0] = '\0';
   1935 
   1936 	if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
   1937 	     wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
   1938 	    sm->proto == WPA_PROTO_RSN)
   1939 		rsna = 1;
   1940 	else
   1941 		rsna = 0;
   1942 
   1943 	ret = os_snprintf(buf, buflen,
   1944 			  "dot11RSNAOptionImplemented=TRUE\n"
   1945 			  "dot11RSNAPreauthenticationImplemented=TRUE\n"
   1946 			  "dot11RSNAEnabled=%s\n"
   1947 			  "dot11RSNAPreauthenticationEnabled=%s\n"
   1948 			  "dot11RSNAConfigVersion=%d\n"
   1949 			  "dot11RSNAConfigPairwiseKeysSupported=5\n"
   1950 			  "dot11RSNAConfigGroupCipherSize=%d\n"
   1951 			  "dot11RSNAConfigPMKLifetime=%d\n"
   1952 			  "dot11RSNAConfigPMKReauthThreshold=%d\n"
   1953 			  "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
   1954 			  "dot11RSNAConfigSATimeout=%d\n",
   1955 			  rsna ? "TRUE" : "FALSE",
   1956 			  rsna ? "TRUE" : "FALSE",
   1957 			  RSN_VERSION,
   1958 			  wpa_cipher_key_len(sm->group_cipher) * 8,
   1959 			  sm->dot11RSNAConfigPMKLifetime,
   1960 			  sm->dot11RSNAConfigPMKReauthThreshold,
   1961 			  sm->dot11RSNAConfigSATimeout);
   1962 	if (ret < 0 || (size_t) ret >= buflen)
   1963 		return 0;
   1964 	len = ret;
   1965 
   1966 	ret = os_snprintf(
   1967 		buf + len, buflen - len,
   1968 		"dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
   1969 		"dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
   1970 		"dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
   1971 		"dot11RSNAPMKIDUsed=%s\n"
   1972 		"dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
   1973 		"dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
   1974 		"dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
   1975 		"dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
   1976 		"dot11RSNA4WayHandshakeFailures=%u\n",
   1977 		RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
   1978 		RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
   1979 						  sm->pairwise_cipher)),
   1980 		RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
   1981 						  sm->group_cipher)),
   1982 		pmkid_txt,
   1983 		RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
   1984 		RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
   1985 						  sm->pairwise_cipher)),
   1986 		RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
   1987 						  sm->group_cipher)),
   1988 		sm->dot11RSNA4WayHandshakeFailures);
   1989 	if (ret >= 0 && (size_t) ret < buflen)
   1990 		len += ret;
   1991 
   1992 	return (int) len;
   1993 }
   1994 #endif /* CONFIG_CTRL_IFACE */
   1995 
   1996 
   1997 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
   1998 				 void *ctx, enum pmksa_free_reason reason)
   1999 {
   2000 	struct wpa_sm *sm = ctx;
   2001 	int deauth = 0;
   2002 
   2003 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
   2004 		MACSTR " reason=%d", MAC2STR(entry->aa), reason);
   2005 
   2006 	if (sm->cur_pmksa == entry) {
   2007 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
   2008 			"RSN: %s current PMKSA entry",
   2009 			reason == PMKSA_REPLACE ? "replaced" : "removed");
   2010 		pmksa_cache_clear_current(sm);
   2011 
   2012 		/*
   2013 		 * If an entry is simply being replaced, there's no need to
   2014 		 * deauthenticate because it will be immediately re-added.
   2015 		 * This happens when EAP authentication is completed again
   2016 		 * (reauth or failed PMKSA caching attempt).
   2017 		 */
   2018 		if (reason != PMKSA_REPLACE)
   2019 			deauth = 1;
   2020 	}
   2021 
   2022 	if (reason == PMKSA_EXPIRE &&
   2023 	    (sm->pmk_len == entry->pmk_len &&
   2024 	     os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
   2025 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
   2026 			"RSN: deauthenticating due to expired PMK");
   2027 		pmksa_cache_clear_current(sm);
   2028 		deauth = 1;
   2029 	}
   2030 
   2031 	if (deauth) {
   2032 		os_memset(sm->pmk, 0, sizeof(sm->pmk));
   2033 		wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
   2034 	}
   2035 }
   2036 
   2037 
   2038 /**
   2039  * wpa_sm_init - Initialize WPA state machine
   2040  * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
   2041  * Returns: Pointer to the allocated WPA state machine data
   2042  *
   2043  * This function is used to allocate a new WPA state machine and the returned
   2044  * value is passed to all WPA state machine calls.
   2045  */
   2046 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
   2047 {
   2048 	struct wpa_sm *sm;
   2049 
   2050 	sm = os_zalloc(sizeof(*sm));
   2051 	if (sm == NULL)
   2052 		return NULL;
   2053 	dl_list_init(&sm->pmksa_candidates);
   2054 	sm->renew_snonce = 1;
   2055 	sm->ctx = ctx;
   2056 
   2057 	sm->dot11RSNAConfigPMKLifetime = 43200;
   2058 	sm->dot11RSNAConfigPMKReauthThreshold = 70;
   2059 	sm->dot11RSNAConfigSATimeout = 60;
   2060 
   2061 	sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
   2062 	if (sm->pmksa == NULL) {
   2063 		wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
   2064 			"RSN: PMKSA cache initialization failed");
   2065 		os_free(sm);
   2066 		return NULL;
   2067 	}
   2068 
   2069 	return sm;
   2070 }
   2071 
   2072 
   2073 /**
   2074  * wpa_sm_deinit - Deinitialize WPA state machine
   2075  * @sm: Pointer to WPA state machine data from wpa_sm_init()
   2076  */
   2077 void wpa_sm_deinit(struct wpa_sm *sm)
   2078 {
   2079 	if (sm == NULL)
   2080 		return;
   2081 	pmksa_cache_deinit(sm->pmksa);
   2082 	eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
   2083 	eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
   2084 	os_free(sm->assoc_wpa_ie);
   2085 	os_free(sm->ap_wpa_ie);
   2086 	os_free(sm->ap_rsn_ie);
   2087 	os_free(sm->ctx);
   2088 	peerkey_deinit(sm);
   2089 #ifdef CONFIG_IEEE80211R
   2090 	os_free(sm->assoc_resp_ies);
   2091 #endif /* CONFIG_IEEE80211R */
   2092 	os_free(sm);
   2093 }
   2094 
   2095 
   2096 /**
   2097  * wpa_sm_notify_assoc - Notify WPA state machine about association
   2098  * @sm: Pointer to WPA state machine data from wpa_sm_init()
   2099  * @bssid: The BSSID of the new association
   2100  *
   2101  * This function is called to let WPA state machine know that the connection
   2102  * was established.
   2103  */
   2104 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
   2105 {
   2106 	int clear_ptk = 1;
   2107 
   2108 	if (sm == NULL)
   2109 		return;
   2110 
   2111 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
   2112 		"WPA: Association event - clear replay counter");
   2113 	os_memcpy(sm->bssid, bssid, ETH_ALEN);
   2114 	os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
   2115 	sm->rx_replay_counter_set = 0;
   2116 	sm->renew_snonce = 1;
   2117 	if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
   2118 		rsn_preauth_deinit(sm);
   2119 
   2120 #ifdef CONFIG_IEEE80211R
   2121 	if (wpa_ft_is_completed(sm)) {
   2122 		/*
   2123 		 * Clear portValid to kick EAPOL state machine to re-enter
   2124 		 * AUTHENTICATED state to get the EAPOL port Authorized.
   2125 		 */
   2126 		eapol_sm_notify_portValid(sm->eapol, FALSE);
   2127 		wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
   2128 
   2129 		/* Prepare for the next transition */
   2130 		wpa_ft_prepare_auth_request(sm, NULL);
   2131 
   2132 		clear_ptk = 0;
   2133 	}
   2134 #endif /* CONFIG_IEEE80211R */
   2135 
   2136 	if (clear_ptk) {
   2137 		/*
   2138 		 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
   2139 		 * this is not part of a Fast BSS Transition.
   2140 		 */
   2141 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
   2142 		sm->ptk_set = 0;
   2143 		os_memset(&sm->ptk, 0, sizeof(sm->ptk));
   2144 		sm->tptk_set = 0;
   2145 		os_memset(&sm->tptk, 0, sizeof(sm->tptk));
   2146 	}
   2147 
   2148 #ifdef CONFIG_TDLS
   2149 	wpa_tdls_assoc(sm);
   2150 #endif /* CONFIG_TDLS */
   2151 
   2152 #ifdef CONFIG_P2P
   2153 	os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
   2154 #endif /* CONFIG_P2P */
   2155 }
   2156 
   2157 
   2158 /**
   2159  * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
   2160  * @sm: Pointer to WPA state machine data from wpa_sm_init()
   2161  *
   2162  * This function is called to let WPA state machine know that the connection
   2163  * was lost. This will abort any existing pre-authentication session.
   2164  */
   2165 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
   2166 {
   2167 	peerkey_deinit(sm);
   2168 	rsn_preauth_deinit(sm);
   2169 	pmksa_cache_clear_current(sm);
   2170 	if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
   2171 		sm->dot11RSNA4WayHandshakeFailures++;
   2172 #ifdef CONFIG_TDLS
   2173 	wpa_tdls_disassoc(sm);
   2174 #endif /* CONFIG_TDLS */
   2175 }
   2176 
   2177 
   2178 /**
   2179  * wpa_sm_set_pmk - Set PMK
   2180  * @sm: Pointer to WPA state machine data from wpa_sm_init()
   2181  * @pmk: The new PMK
   2182  * @pmk_len: The length of the new PMK in bytes
   2183  *
   2184  * Configure the PMK for WPA state machine.
   2185  */
   2186 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len)
   2187 {
   2188 	if (sm == NULL)
   2189 		return;
   2190 
   2191 	sm->pmk_len = pmk_len;
   2192 	os_memcpy(sm->pmk, pmk, pmk_len);
   2193 
   2194 #ifdef CONFIG_IEEE80211R
   2195 	/* Set XXKey to be PSK for FT key derivation */
   2196 	sm->xxkey_len = pmk_len;
   2197 	os_memcpy(sm->xxkey, pmk, pmk_len);
   2198 #endif /* CONFIG_IEEE80211R */
   2199 }
   2200 
   2201 
   2202 /**
   2203  * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
   2204  * @sm: Pointer to WPA state machine data from wpa_sm_init()
   2205  *
   2206  * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
   2207  * will be cleared.
   2208  */
   2209 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
   2210 {
   2211 	if (sm == NULL)
   2212 		return;
   2213 
   2214 	if (sm->cur_pmksa) {
   2215 		sm->pmk_len = sm->cur_pmksa->pmk_len;
   2216 		os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
   2217 	} else {
   2218 		sm->pmk_len = PMK_LEN;
   2219 		os_memset(sm->pmk, 0, PMK_LEN);
   2220 	}
   2221 }
   2222 
   2223 
   2224 /**
   2225  * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
   2226  * @sm: Pointer to WPA state machine data from wpa_sm_init()
   2227  * @fast_reauth: Whether fast reauthentication (EAP) is allowed
   2228  */
   2229 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
   2230 {
   2231 	if (sm)
   2232 		sm->fast_reauth = fast_reauth;
   2233 }
   2234 
   2235 
   2236 /**
   2237  * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
   2238  * @sm: Pointer to WPA state machine data from wpa_sm_init()
   2239  * @scard_ctx: Context pointer for smartcard related callback functions
   2240  */
   2241 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
   2242 {
   2243 	if (sm == NULL)
   2244 		return;
   2245 	sm->scard_ctx = scard_ctx;
   2246 	if (sm->preauth_eapol)
   2247 		eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
   2248 }
   2249 
   2250 
   2251 /**
   2252  * wpa_sm_set_config - Notification of current configration change
   2253  * @sm: Pointer to WPA state machine data from wpa_sm_init()
   2254  * @config: Pointer to current network configuration
   2255  *
   2256  * Notify WPA state machine that configuration has changed. config will be
   2257  * stored as a backpointer to network configuration. This can be %NULL to clear
   2258  * the stored pointed.
   2259  */
   2260 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
   2261 {
   2262 	if (!sm)
   2263 		return;
   2264 
   2265 	if (config) {
   2266 		sm->network_ctx = config->network_ctx;
   2267 		sm->peerkey_enabled = config->peerkey_enabled;
   2268 		sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
   2269 		sm->proactive_key_caching = config->proactive_key_caching;
   2270 		sm->eap_workaround = config->eap_workaround;
   2271 		sm->eap_conf_ctx = config->eap_conf_ctx;
   2272 		if (config->ssid) {
   2273 			os_memcpy(sm->ssid, config->ssid, config->ssid_len);
   2274 			sm->ssid_len = config->ssid_len;
   2275 		} else
   2276 			sm->ssid_len = 0;
   2277 		sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
   2278 		sm->p2p = config->p2p;
   2279 	} else {
   2280 		sm->network_ctx = NULL;
   2281 		sm->peerkey_enabled = 0;
   2282 		sm->allowed_pairwise_cipher = 0;
   2283 		sm->proactive_key_caching = 0;
   2284 		sm->eap_workaround = 0;
   2285 		sm->eap_conf_ctx = NULL;
   2286 		sm->ssid_len = 0;
   2287 		sm->wpa_ptk_rekey = 0;
   2288 		sm->p2p = 0;
   2289 	}
   2290 }
   2291 
   2292 
   2293 /**
   2294  * wpa_sm_set_own_addr - Set own MAC address
   2295  * @sm: Pointer to WPA state machine data from wpa_sm_init()
   2296  * @addr: Own MAC address
   2297  */
   2298 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
   2299 {
   2300 	if (sm)
   2301 		os_memcpy(sm->own_addr, addr, ETH_ALEN);
   2302 }
   2303 
   2304 
   2305 /**
   2306  * wpa_sm_set_ifname - Set network interface name
   2307  * @sm: Pointer to WPA state machine data from wpa_sm_init()
   2308  * @ifname: Interface name
   2309  * @bridge_ifname: Optional bridge interface name (for pre-auth)
   2310  */
   2311 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
   2312 		       const char *bridge_ifname)
   2313 {
   2314 	if (sm) {
   2315 		sm->ifname = ifname;
   2316 		sm->bridge_ifname = bridge_ifname;
   2317 	}
   2318 }
   2319 
   2320 
   2321 /**
   2322  * wpa_sm_set_eapol - Set EAPOL state machine pointer
   2323  * @sm: Pointer to WPA state machine data from wpa_sm_init()
   2324  * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
   2325  */
   2326 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
   2327 {
   2328 	if (sm)
   2329 		sm->eapol = eapol;
   2330 }
   2331 
   2332 
   2333 /**
   2334  * wpa_sm_set_param - Set WPA state machine parameters
   2335  * @sm: Pointer to WPA state machine data from wpa_sm_init()
   2336  * @param: Parameter field
   2337  * @value: Parameter value
   2338  * Returns: 0 on success, -1 on failure
   2339  */
   2340 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
   2341 		     unsigned int value)
   2342 {
   2343 	int ret = 0;
   2344 
   2345 	if (sm == NULL)
   2346 		return -1;
   2347 
   2348 	switch (param) {
   2349 	case RSNA_PMK_LIFETIME:
   2350 		if (value > 0)
   2351 			sm->dot11RSNAConfigPMKLifetime = value;
   2352 		else
   2353 			ret = -1;
   2354 		break;
   2355 	case RSNA_PMK_REAUTH_THRESHOLD:
   2356 		if (value > 0 && value <= 100)
   2357 			sm->dot11RSNAConfigPMKReauthThreshold = value;
   2358 		else
   2359 			ret = -1;
   2360 		break;
   2361 	case RSNA_SA_TIMEOUT:
   2362 		if (value > 0)
   2363 			sm->dot11RSNAConfigSATimeout = value;
   2364 		else
   2365 			ret = -1;
   2366 		break;
   2367 	case WPA_PARAM_PROTO:
   2368 		sm->proto = value;
   2369 		break;
   2370 	case WPA_PARAM_PAIRWISE:
   2371 		sm->pairwise_cipher = value;
   2372 		break;
   2373 	case WPA_PARAM_GROUP:
   2374 		sm->group_cipher = value;
   2375 		break;
   2376 	case WPA_PARAM_KEY_MGMT:
   2377 		sm->key_mgmt = value;
   2378 		break;
   2379 #ifdef CONFIG_IEEE80211W
   2380 	case WPA_PARAM_MGMT_GROUP:
   2381 		sm->mgmt_group_cipher = value;
   2382 		break;
   2383 #endif /* CONFIG_IEEE80211W */
   2384 	case WPA_PARAM_RSN_ENABLED:
   2385 		sm->rsn_enabled = value;
   2386 		break;
   2387 	case WPA_PARAM_MFP:
   2388 		sm->mfp = value;
   2389 		break;
   2390 	default:
   2391 		break;
   2392 	}
   2393 
   2394 	return ret;
   2395 }
   2396 
   2397 
   2398 /**
   2399  * wpa_sm_get_status - Get WPA state machine
   2400  * @sm: Pointer to WPA state machine data from wpa_sm_init()
   2401  * @buf: Buffer for status information
   2402  * @buflen: Maximum buffer length
   2403  * @verbose: Whether to include verbose status information
   2404  * Returns: Number of bytes written to buf.
   2405  *
   2406  * Query WPA state machine for status information. This function fills in
   2407  * a text area with current status information. If the buffer (buf) is not
   2408  * large enough, status information will be truncated to fit the buffer.
   2409  */
   2410 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
   2411 		      int verbose)
   2412 {
   2413 	char *pos = buf, *end = buf + buflen;
   2414 	int ret;
   2415 
   2416 	ret = os_snprintf(pos, end - pos,
   2417 			  "pairwise_cipher=%s\n"
   2418 			  "group_cipher=%s\n"
   2419 			  "key_mgmt=%s\n",
   2420 			  wpa_cipher_txt(sm->pairwise_cipher),
   2421 			  wpa_cipher_txt(sm->group_cipher),
   2422 			  wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
   2423 	if (ret < 0 || ret >= end - pos)
   2424 		return pos - buf;
   2425 	pos += ret;
   2426 
   2427 	if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
   2428 		struct wpa_ie_data rsn;
   2429 		if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
   2430 		    >= 0 &&
   2431 		    rsn.capabilities & (WPA_CAPABILITY_MFPR |
   2432 					WPA_CAPABILITY_MFPC)) {
   2433 			ret = os_snprintf(pos, end - pos, "pmf=%d\n",
   2434 					  (rsn.capabilities &
   2435 					   WPA_CAPABILITY_MFPR) ? 2 : 1);
   2436 			if (ret < 0 || ret >= end - pos)
   2437 				return pos - buf;
   2438 			pos += ret;
   2439 		}
   2440 	}
   2441 
   2442 	return pos - buf;
   2443 }
   2444 
   2445 
   2446 int wpa_sm_pmf_enabled(struct wpa_sm *sm)
   2447 {
   2448 	struct wpa_ie_data rsn;
   2449 
   2450 	if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
   2451 		return 0;
   2452 
   2453 	if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
   2454 	    rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
   2455 		return 1;
   2456 
   2457 	return 0;
   2458 }
   2459 
   2460 
   2461 /**
   2462  * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
   2463  * @sm: Pointer to WPA state machine data from wpa_sm_init()
   2464  * @wpa_ie: Pointer to buffer for WPA/RSN IE
   2465  * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
   2466  * Returns: 0 on success, -1 on failure
   2467  */
   2468 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
   2469 				    size_t *wpa_ie_len)
   2470 {
   2471 	int res;
   2472 
   2473 	if (sm == NULL)
   2474 		return -1;
   2475 
   2476 	res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
   2477 	if (res < 0)
   2478 		return -1;
   2479 	*wpa_ie_len = res;
   2480 
   2481 	wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
   2482 		    wpa_ie, *wpa_ie_len);
   2483 
   2484 	if (sm->assoc_wpa_ie == NULL) {
   2485 		/*
   2486 		 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
   2487 		 * the correct version of the IE even if PMKSA caching is
   2488 		 * aborted (which would remove PMKID from IE generation).
   2489 		 */
   2490 		sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
   2491 		if (sm->assoc_wpa_ie == NULL)
   2492 			return -1;
   2493 
   2494 		os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
   2495 		sm->assoc_wpa_ie_len = *wpa_ie_len;
   2496 	}
   2497 
   2498 	return 0;
   2499 }
   2500 
   2501 
   2502 /**
   2503  * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
   2504  * @sm: Pointer to WPA state machine data from wpa_sm_init()
   2505  * @ie: Pointer to IE data (starting from id)
   2506  * @len: IE length
   2507  * Returns: 0 on success, -1 on failure
   2508  *
   2509  * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
   2510  * Request frame. The IE will be used to override the default value generated
   2511  * with wpa_sm_set_assoc_wpa_ie_default().
   2512  */
   2513 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
   2514 {
   2515 	if (sm == NULL)
   2516 		return -1;
   2517 
   2518 	os_free(sm->assoc_wpa_ie);
   2519 	if (ie == NULL || len == 0) {
   2520 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
   2521 			"WPA: clearing own WPA/RSN IE");
   2522 		sm->assoc_wpa_ie = NULL;
   2523 		sm->assoc_wpa_ie_len = 0;
   2524 	} else {
   2525 		wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
   2526 		sm->assoc_wpa_ie = os_malloc(len);
   2527 		if (sm->assoc_wpa_ie == NULL)
   2528 			return -1;
   2529 
   2530 		os_memcpy(sm->assoc_wpa_ie, ie, len);
   2531 		sm->assoc_wpa_ie_len = len;
   2532 	}
   2533 
   2534 	return 0;
   2535 }
   2536 
   2537 
   2538 /**
   2539  * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
   2540  * @sm: Pointer to WPA state machine data from wpa_sm_init()
   2541  * @ie: Pointer to IE data (starting from id)
   2542  * @len: IE length
   2543  * Returns: 0 on success, -1 on failure
   2544  *
   2545  * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
   2546  * frame.
   2547  */
   2548 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
   2549 {
   2550 	if (sm == NULL)
   2551 		return -1;
   2552 
   2553 	os_free(sm->ap_wpa_ie);
   2554 	if (ie == NULL || len == 0) {
   2555 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
   2556 			"WPA: clearing AP WPA IE");
   2557 		sm->ap_wpa_ie = NULL;
   2558 		sm->ap_wpa_ie_len = 0;
   2559 	} else {
   2560 		wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
   2561 		sm->ap_wpa_ie = os_malloc(len);
   2562 		if (sm->ap_wpa_ie == NULL)
   2563 			return -1;
   2564 
   2565 		os_memcpy(sm->ap_wpa_ie, ie, len);
   2566 		sm->ap_wpa_ie_len = len;
   2567 	}
   2568 
   2569 	return 0;
   2570 }
   2571 
   2572 
   2573 /**
   2574  * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
   2575  * @sm: Pointer to WPA state machine data from wpa_sm_init()
   2576  * @ie: Pointer to IE data (starting from id)
   2577  * @len: IE length
   2578  * Returns: 0 on success, -1 on failure
   2579  *
   2580  * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
   2581  * frame.
   2582  */
   2583 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
   2584 {
   2585 	if (sm == NULL)
   2586 		return -1;
   2587 
   2588 	os_free(sm->ap_rsn_ie);
   2589 	if (ie == NULL || len == 0) {
   2590 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
   2591 			"WPA: clearing AP RSN IE");
   2592 		sm->ap_rsn_ie = NULL;
   2593 		sm->ap_rsn_ie_len = 0;
   2594 	} else {
   2595 		wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
   2596 		sm->ap_rsn_ie = os_malloc(len);
   2597 		if (sm->ap_rsn_ie == NULL)
   2598 			return -1;
   2599 
   2600 		os_memcpy(sm->ap_rsn_ie, ie, len);
   2601 		sm->ap_rsn_ie_len = len;
   2602 	}
   2603 
   2604 	return 0;
   2605 }
   2606 
   2607 
   2608 /**
   2609  * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
   2610  * @sm: Pointer to WPA state machine data from wpa_sm_init()
   2611  * @data: Pointer to data area for parsing results
   2612  * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
   2613  *
   2614  * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
   2615  * parsed data into data.
   2616  */
   2617 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
   2618 {
   2619 	if (sm == NULL)
   2620 		return -1;
   2621 
   2622 	if (sm->assoc_wpa_ie == NULL) {
   2623 		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
   2624 			"WPA: No WPA/RSN IE available from association info");
   2625 		return -1;
   2626 	}
   2627 	if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
   2628 		return -2;
   2629 	return 0;
   2630 }
   2631 
   2632 
   2633 int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
   2634 {
   2635 	return pmksa_cache_list(sm->pmksa, buf, len);
   2636 }
   2637 
   2638 
   2639 #ifdef CONFIG_TESTING_OPTIONS
   2640 void wpa_sm_drop_sa(struct wpa_sm *sm)
   2641 {
   2642 	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
   2643 	sm->ptk_set = 0;
   2644 	sm->tptk_set = 0;
   2645 	os_memset(sm->pmk, 0, sizeof(sm->pmk));
   2646 	os_memset(&sm->ptk, 0, sizeof(sm->ptk));
   2647 	os_memset(&sm->tptk, 0, sizeof(sm->tptk));
   2648 }
   2649 #endif /* CONFIG_TESTING_OPTIONS */
   2650 
   2651 
   2652 int wpa_sm_has_ptk(struct wpa_sm *sm)
   2653 {
   2654 	if (sm == NULL)
   2655 		return 0;
   2656 	return sm->ptk_set;
   2657 }
   2658 
   2659 
   2660 void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
   2661 {
   2662 	os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
   2663 }
   2664 
   2665 
   2666 void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
   2667 {
   2668 	pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
   2669 }
   2670 
   2671 
   2672 #ifdef CONFIG_WNM
   2673 int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
   2674 {
   2675 	u16 keyinfo;
   2676 	u8 keylen;  /* plaintext key len */
   2677 	u8 *key_rsc;
   2678 
   2679 	if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
   2680 		struct wpa_gtk_data gd;
   2681 
   2682 		os_memset(&gd, 0, sizeof(gd));
   2683 		keylen = wpa_cipher_key_len(sm->group_cipher);
   2684 		gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
   2685 		gd.alg = wpa_cipher_to_alg(sm->group_cipher);
   2686 		if (gd.alg == WPA_ALG_NONE) {
   2687 			wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
   2688 			return -1;
   2689 		}
   2690 
   2691 		key_rsc = buf + 5;
   2692 		keyinfo = WPA_GET_LE16(buf + 2);
   2693 		gd.gtk_len = keylen;
   2694 		if (gd.gtk_len != buf[4]) {
   2695 			wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
   2696 				   gd.gtk_len, buf[4]);
   2697 			return -1;
   2698 		}
   2699 		gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
   2700 		gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
   2701 		         sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
   2702 
   2703 		os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
   2704 
   2705 		wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
   2706 				gd.gtk, gd.gtk_len);
   2707 		if (wpa_supplicant_install_gtk(sm, &gd, key_rsc)) {
   2708 			os_memset(&gd, 0, sizeof(gd));
   2709 			wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
   2710 				   "WNM mode");
   2711 			return -1;
   2712 		}
   2713 		os_memset(&gd, 0, sizeof(gd));
   2714 #ifdef CONFIG_IEEE80211W
   2715 	} else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
   2716 		struct wpa_igtk_kde igd;
   2717 		u16 keyidx;
   2718 
   2719 		os_memset(&igd, 0, sizeof(igd));
   2720 		keylen = wpa_cipher_key_len(sm->mgmt_group_cipher);
   2721 		os_memcpy(igd.keyid, buf + 2, 2);
   2722 		os_memcpy(igd.pn, buf + 4, 6);
   2723 
   2724 		keyidx = WPA_GET_LE16(igd.keyid);
   2725 		os_memcpy(igd.igtk, buf + 10, keylen);
   2726 
   2727 		wpa_hexdump_key(MSG_DEBUG, "Install IGTK (WNM SLEEP)",
   2728 				igd.igtk, keylen);
   2729 		if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
   2730 				   broadcast_ether_addr,
   2731 				   keyidx, 0, igd.pn, sizeof(igd.pn),
   2732 				   igd.igtk, keylen) < 0) {
   2733 			wpa_printf(MSG_DEBUG, "Failed to install the IGTK in "
   2734 				   "WNM mode");
   2735 			os_memset(&igd, 0, sizeof(igd));
   2736 			return -1;
   2737 		}
   2738 		os_memset(&igd, 0, sizeof(igd));
   2739 #endif /* CONFIG_IEEE80211W */
   2740 	} else {
   2741 		wpa_printf(MSG_DEBUG, "Unknown element id");
   2742 		return -1;
   2743 	}
   2744 
   2745 	return 0;
   2746 }
   2747 #endif /* CONFIG_WNM */
   2748 
   2749 
   2750 #ifdef CONFIG_PEERKEY
   2751 int wpa_sm_rx_eapol_peerkey(struct wpa_sm *sm, const u8 *src_addr,
   2752 			    const u8 *buf, size_t len)
   2753 {
   2754 	struct wpa_peerkey *peerkey;
   2755 
   2756 	for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
   2757 		if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
   2758 			break;
   2759 	}
   2760 
   2761 	if (!peerkey)
   2762 		return 0;
   2763 
   2764 	wpa_sm_rx_eapol(sm, src_addr, buf, len);
   2765 
   2766 	return 1;
   2767 }
   2768 #endif /* CONFIG_PEERKEY */
   2769 
   2770 
   2771 #ifdef CONFIG_P2P
   2772 
   2773 int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
   2774 {
   2775 	if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
   2776 		return -1;
   2777 	os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
   2778 	return 0;
   2779 }
   2780 
   2781 #endif /* CONFIG_P2P */
   2782