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