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