wpa.c revision 324739
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		if (wpa_supplicant_decrypt_key_data(sm, key, ver))
1833			goto out;
1834		extra_len = WPA_GET_BE16(key->key_data_length);
1835	}
1836
1837	if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1838		if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
1839			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1840				"WPA: Ignored EAPOL-Key (Pairwise) with "
1841				"non-zero key index");
1842			goto out;
1843		}
1844		if (peerkey) {
1845			/* PeerKey 4-Way Handshake */
1846			peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver);
1847		} else if (key_info & WPA_KEY_INFO_MIC) {
1848			/* 3/4 4-Way Handshake */
1849			wpa_supplicant_process_3_of_4(sm, key, ver);
1850		} else {
1851			/* 1/4 4-Way Handshake */
1852			wpa_supplicant_process_1_of_4(sm, src_addr, key,
1853						      ver);
1854		}
1855	} else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1856		/* PeerKey SMK Handshake */
1857		peerkey_rx_eapol_smk(sm, src_addr, key, extra_len, key_info,
1858				     ver);
1859	} else {
1860		if (key_info & WPA_KEY_INFO_MIC) {
1861			/* 1/2 Group Key Handshake */
1862			wpa_supplicant_process_1_of_2(sm, src_addr, key,
1863						      extra_len, ver);
1864		} else {
1865			wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1866				"WPA: EAPOL-Key (Group) without Mic bit - "
1867				"dropped");
1868		}
1869	}
1870
1871	ret = 1;
1872
1873out:
1874	os_free(tmp);
1875	return ret;
1876}
1877
1878
1879#ifdef CONFIG_CTRL_IFACE
1880static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
1881{
1882	switch (sm->key_mgmt) {
1883	case WPA_KEY_MGMT_IEEE8021X:
1884		return (sm->proto == WPA_PROTO_RSN ?
1885			RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
1886			WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
1887	case WPA_KEY_MGMT_PSK:
1888		return (sm->proto == WPA_PROTO_RSN ?
1889			RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
1890			WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
1891#ifdef CONFIG_IEEE80211R
1892	case WPA_KEY_MGMT_FT_IEEE8021X:
1893		return RSN_AUTH_KEY_MGMT_FT_802_1X;
1894	case WPA_KEY_MGMT_FT_PSK:
1895		return RSN_AUTH_KEY_MGMT_FT_PSK;
1896#endif /* CONFIG_IEEE80211R */
1897#ifdef CONFIG_IEEE80211W
1898	case WPA_KEY_MGMT_IEEE8021X_SHA256:
1899		return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
1900	case WPA_KEY_MGMT_PSK_SHA256:
1901		return RSN_AUTH_KEY_MGMT_PSK_SHA256;
1902#endif /* CONFIG_IEEE80211W */
1903	case WPA_KEY_MGMT_CCKM:
1904		return (sm->proto == WPA_PROTO_RSN ?
1905			RSN_AUTH_KEY_MGMT_CCKM:
1906			WPA_AUTH_KEY_MGMT_CCKM);
1907	case WPA_KEY_MGMT_WPA_NONE:
1908		return WPA_AUTH_KEY_MGMT_NONE;
1909	default:
1910		return 0;
1911	}
1912}
1913
1914
1915#define RSN_SUITE "%02x-%02x-%02x-%d"
1916#define RSN_SUITE_ARG(s) \
1917((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
1918
1919/**
1920 * wpa_sm_get_mib - Dump text list of MIB entries
1921 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1922 * @buf: Buffer for the list
1923 * @buflen: Length of the buffer
1924 * Returns: Number of bytes written to buffer
1925 *
1926 * This function is used fetch dot11 MIB variables.
1927 */
1928int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
1929{
1930	char pmkid_txt[PMKID_LEN * 2 + 1];
1931	int rsna, ret;
1932	size_t len;
1933
1934	if (sm->cur_pmksa) {
1935		wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
1936				 sm->cur_pmksa->pmkid, PMKID_LEN);
1937	} else
1938		pmkid_txt[0] = '\0';
1939
1940	if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
1941	     wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
1942	    sm->proto == WPA_PROTO_RSN)
1943		rsna = 1;
1944	else
1945		rsna = 0;
1946
1947	ret = os_snprintf(buf, buflen,
1948			  "dot11RSNAOptionImplemented=TRUE\n"
1949			  "dot11RSNAPreauthenticationImplemented=TRUE\n"
1950			  "dot11RSNAEnabled=%s\n"
1951			  "dot11RSNAPreauthenticationEnabled=%s\n"
1952			  "dot11RSNAConfigVersion=%d\n"
1953			  "dot11RSNAConfigPairwiseKeysSupported=5\n"
1954			  "dot11RSNAConfigGroupCipherSize=%d\n"
1955			  "dot11RSNAConfigPMKLifetime=%d\n"
1956			  "dot11RSNAConfigPMKReauthThreshold=%d\n"
1957			  "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
1958			  "dot11RSNAConfigSATimeout=%d\n",
1959			  rsna ? "TRUE" : "FALSE",
1960			  rsna ? "TRUE" : "FALSE",
1961			  RSN_VERSION,
1962			  wpa_cipher_key_len(sm->group_cipher) * 8,
1963			  sm->dot11RSNAConfigPMKLifetime,
1964			  sm->dot11RSNAConfigPMKReauthThreshold,
1965			  sm->dot11RSNAConfigSATimeout);
1966	if (ret < 0 || (size_t) ret >= buflen)
1967		return 0;
1968	len = ret;
1969
1970	ret = os_snprintf(
1971		buf + len, buflen - len,
1972		"dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
1973		"dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
1974		"dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
1975		"dot11RSNAPMKIDUsed=%s\n"
1976		"dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
1977		"dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
1978		"dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
1979		"dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
1980		"dot11RSNA4WayHandshakeFailures=%u\n",
1981		RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1982		RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
1983						  sm->pairwise_cipher)),
1984		RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
1985						  sm->group_cipher)),
1986		pmkid_txt,
1987		RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1988		RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
1989						  sm->pairwise_cipher)),
1990		RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
1991						  sm->group_cipher)),
1992		sm->dot11RSNA4WayHandshakeFailures);
1993	if (ret >= 0 && (size_t) ret < buflen)
1994		len += ret;
1995
1996	return (int) len;
1997}
1998#endif /* CONFIG_CTRL_IFACE */
1999
2000
2001static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
2002				 void *ctx, enum pmksa_free_reason reason)
2003{
2004	struct wpa_sm *sm = ctx;
2005	int deauth = 0;
2006
2007	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2008		MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2009
2010	if (sm->cur_pmksa == entry) {
2011		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2012			"RSN: %s current PMKSA entry",
2013			reason == PMKSA_REPLACE ? "replaced" : "removed");
2014		pmksa_cache_clear_current(sm);
2015
2016		/*
2017		 * If an entry is simply being replaced, there's no need to
2018		 * deauthenticate because it will be immediately re-added.
2019		 * This happens when EAP authentication is completed again
2020		 * (reauth or failed PMKSA caching attempt).
2021		 */
2022		if (reason != PMKSA_REPLACE)
2023			deauth = 1;
2024	}
2025
2026	if (reason == PMKSA_EXPIRE &&
2027	    (sm->pmk_len == entry->pmk_len &&
2028	     os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2029		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2030			"RSN: deauthenticating due to expired PMK");
2031		pmksa_cache_clear_current(sm);
2032		deauth = 1;
2033	}
2034
2035	if (deauth) {
2036		os_memset(sm->pmk, 0, sizeof(sm->pmk));
2037		wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2038	}
2039}
2040
2041
2042/**
2043 * wpa_sm_init - Initialize WPA state machine
2044 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2045 * Returns: Pointer to the allocated WPA state machine data
2046 *
2047 * This function is used to allocate a new WPA state machine and the returned
2048 * value is passed to all WPA state machine calls.
2049 */
2050struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2051{
2052	struct wpa_sm *sm;
2053
2054	sm = os_zalloc(sizeof(*sm));
2055	if (sm == NULL)
2056		return NULL;
2057	dl_list_init(&sm->pmksa_candidates);
2058	sm->renew_snonce = 1;
2059	sm->ctx = ctx;
2060
2061	sm->dot11RSNAConfigPMKLifetime = 43200;
2062	sm->dot11RSNAConfigPMKReauthThreshold = 70;
2063	sm->dot11RSNAConfigSATimeout = 60;
2064
2065	sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2066	if (sm->pmksa == NULL) {
2067		wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2068			"RSN: PMKSA cache initialization failed");
2069		os_free(sm);
2070		return NULL;
2071	}
2072
2073	return sm;
2074}
2075
2076
2077/**
2078 * wpa_sm_deinit - Deinitialize WPA state machine
2079 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2080 */
2081void wpa_sm_deinit(struct wpa_sm *sm)
2082{
2083	if (sm == NULL)
2084		return;
2085	pmksa_cache_deinit(sm->pmksa);
2086	eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2087	eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2088	os_free(sm->assoc_wpa_ie);
2089	os_free(sm->ap_wpa_ie);
2090	os_free(sm->ap_rsn_ie);
2091	os_free(sm->ctx);
2092	peerkey_deinit(sm);
2093#ifdef CONFIG_IEEE80211R
2094	os_free(sm->assoc_resp_ies);
2095#endif /* CONFIG_IEEE80211R */
2096	os_free(sm);
2097}
2098
2099
2100/**
2101 * wpa_sm_notify_assoc - Notify WPA state machine about association
2102 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2103 * @bssid: The BSSID of the new association
2104 *
2105 * This function is called to let WPA state machine know that the connection
2106 * was established.
2107 */
2108void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2109{
2110	int clear_keys = 1;
2111
2112	if (sm == NULL)
2113		return;
2114
2115	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2116		"WPA: Association event - clear replay counter");
2117	os_memcpy(sm->bssid, bssid, ETH_ALEN);
2118	os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2119	sm->rx_replay_counter_set = 0;
2120	sm->renew_snonce = 1;
2121	if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2122		rsn_preauth_deinit(sm);
2123
2124#ifdef CONFIG_IEEE80211R
2125	if (wpa_ft_is_completed(sm)) {
2126		/*
2127		 * Clear portValid to kick EAPOL state machine to re-enter
2128		 * AUTHENTICATED state to get the EAPOL port Authorized.
2129		 */
2130		eapol_sm_notify_portValid(sm->eapol, FALSE);
2131		wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2132
2133		/* Prepare for the next transition */
2134		wpa_ft_prepare_auth_request(sm, NULL);
2135
2136		clear_keys = 0;
2137	}
2138#endif /* CONFIG_IEEE80211R */
2139
2140	if (clear_keys) {
2141		/*
2142		 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2143		 * this is not part of a Fast BSS Transition.
2144		 */
2145		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2146		sm->ptk_set = 0;
2147		sm->tptk_set = 0;
2148		os_memset(&sm->gtk, 0, sizeof(sm->gtk));
2149		os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
2150#ifdef CONFIG_IEEE80211W
2151		os_memset(&sm->igtk, 0, sizeof(sm->igtk));
2152		os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
2153#endif /* CONFIG_IEEE80211W */
2154	}
2155
2156#ifdef CONFIG_TDLS
2157	wpa_tdls_assoc(sm);
2158#endif /* CONFIG_TDLS */
2159}
2160
2161
2162/**
2163 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2164 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2165 *
2166 * This function is called to let WPA state machine know that the connection
2167 * was lost. This will abort any existing pre-authentication session.
2168 */
2169void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2170{
2171	rsn_preauth_deinit(sm);
2172	pmksa_cache_clear_current(sm);
2173	if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2174		sm->dot11RSNA4WayHandshakeFailures++;
2175#ifdef CONFIG_TDLS
2176	wpa_tdls_disassoc(sm);
2177#endif /* CONFIG_TDLS */
2178#ifdef CONFIG_IEEE80211R
2179	sm->ft_reassoc_completed = 0;
2180#endif /* CONFIG_IEEE80211R */
2181}
2182
2183
2184/**
2185 * wpa_sm_set_pmk - Set PMK
2186 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2187 * @pmk: The new PMK
2188 * @pmk_len: The length of the new PMK in bytes
2189 *
2190 * Configure the PMK for WPA state machine.
2191 */
2192void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len)
2193{
2194	if (sm == NULL)
2195		return;
2196
2197	sm->pmk_len = pmk_len;
2198	os_memcpy(sm->pmk, pmk, pmk_len);
2199
2200#ifdef CONFIG_IEEE80211R
2201	/* Set XXKey to be PSK for FT key derivation */
2202	sm->xxkey_len = pmk_len;
2203	os_memcpy(sm->xxkey, pmk, pmk_len);
2204#endif /* CONFIG_IEEE80211R */
2205}
2206
2207
2208/**
2209 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2210 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2211 *
2212 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2213 * will be cleared.
2214 */
2215void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2216{
2217	if (sm == NULL)
2218		return;
2219
2220	if (sm->cur_pmksa) {
2221		sm->pmk_len = sm->cur_pmksa->pmk_len;
2222		os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2223	} else {
2224		sm->pmk_len = PMK_LEN;
2225		os_memset(sm->pmk, 0, PMK_LEN);
2226	}
2227}
2228
2229
2230/**
2231 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2232 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2233 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2234 */
2235void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2236{
2237	if (sm)
2238		sm->fast_reauth = fast_reauth;
2239}
2240
2241
2242/**
2243 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2244 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2245 * @scard_ctx: Context pointer for smartcard related callback functions
2246 */
2247void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2248{
2249	if (sm == NULL)
2250		return;
2251	sm->scard_ctx = scard_ctx;
2252	if (sm->preauth_eapol)
2253		eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2254}
2255
2256
2257/**
2258 * wpa_sm_set_config - Notification of current configration change
2259 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2260 * @config: Pointer to current network configuration
2261 *
2262 * Notify WPA state machine that configuration has changed. config will be
2263 * stored as a backpointer to network configuration. This can be %NULL to clear
2264 * the stored pointed.
2265 */
2266void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2267{
2268	if (!sm)
2269		return;
2270
2271	if (config) {
2272		sm->network_ctx = config->network_ctx;
2273		sm->peerkey_enabled = config->peerkey_enabled;
2274		sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2275		sm->proactive_key_caching = config->proactive_key_caching;
2276		sm->eap_workaround = config->eap_workaround;
2277		sm->eap_conf_ctx = config->eap_conf_ctx;
2278		if (config->ssid) {
2279			os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2280			sm->ssid_len = config->ssid_len;
2281		} else
2282			sm->ssid_len = 0;
2283		sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
2284	} else {
2285		sm->network_ctx = NULL;
2286		sm->peerkey_enabled = 0;
2287		sm->allowed_pairwise_cipher = 0;
2288		sm->proactive_key_caching = 0;
2289		sm->eap_workaround = 0;
2290		sm->eap_conf_ctx = NULL;
2291		sm->ssid_len = 0;
2292		sm->wpa_ptk_rekey = 0;
2293	}
2294}
2295
2296
2297/**
2298 * wpa_sm_set_own_addr - Set own MAC address
2299 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2300 * @addr: Own MAC address
2301 */
2302void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2303{
2304	if (sm)
2305		os_memcpy(sm->own_addr, addr, ETH_ALEN);
2306}
2307
2308
2309/**
2310 * wpa_sm_set_ifname - Set network interface name
2311 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2312 * @ifname: Interface name
2313 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2314 */
2315void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2316		       const char *bridge_ifname)
2317{
2318	if (sm) {
2319		sm->ifname = ifname;
2320		sm->bridge_ifname = bridge_ifname;
2321	}
2322}
2323
2324
2325/**
2326 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2327 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2328 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2329 */
2330void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2331{
2332	if (sm)
2333		sm->eapol = eapol;
2334}
2335
2336
2337/**
2338 * wpa_sm_set_param - Set WPA state machine parameters
2339 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2340 * @param: Parameter field
2341 * @value: Parameter value
2342 * Returns: 0 on success, -1 on failure
2343 */
2344int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2345		     unsigned int value)
2346{
2347	int ret = 0;
2348
2349	if (sm == NULL)
2350		return -1;
2351
2352	switch (param) {
2353	case RSNA_PMK_LIFETIME:
2354		if (value > 0)
2355			sm->dot11RSNAConfigPMKLifetime = value;
2356		else
2357			ret = -1;
2358		break;
2359	case RSNA_PMK_REAUTH_THRESHOLD:
2360		if (value > 0 && value <= 100)
2361			sm->dot11RSNAConfigPMKReauthThreshold = value;
2362		else
2363			ret = -1;
2364		break;
2365	case RSNA_SA_TIMEOUT:
2366		if (value > 0)
2367			sm->dot11RSNAConfigSATimeout = value;
2368		else
2369			ret = -1;
2370		break;
2371	case WPA_PARAM_PROTO:
2372		sm->proto = value;
2373		break;
2374	case WPA_PARAM_PAIRWISE:
2375		sm->pairwise_cipher = value;
2376		break;
2377	case WPA_PARAM_GROUP:
2378		sm->group_cipher = value;
2379		break;
2380	case WPA_PARAM_KEY_MGMT:
2381		sm->key_mgmt = value;
2382		break;
2383#ifdef CONFIG_IEEE80211W
2384	case WPA_PARAM_MGMT_GROUP:
2385		sm->mgmt_group_cipher = value;
2386		break;
2387#endif /* CONFIG_IEEE80211W */
2388	case WPA_PARAM_RSN_ENABLED:
2389		sm->rsn_enabled = value;
2390		break;
2391	case WPA_PARAM_MFP:
2392		sm->mfp = value;
2393		break;
2394	default:
2395		break;
2396	}
2397
2398	return ret;
2399}
2400
2401
2402/**
2403 * wpa_sm_get_param - Get WPA state machine parameters
2404 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2405 * @param: Parameter field
2406 * Returns: Parameter value
2407 */
2408unsigned int wpa_sm_get_param(struct wpa_sm *sm, enum wpa_sm_conf_params param)
2409{
2410	if (sm == NULL)
2411		return 0;
2412
2413	switch (param) {
2414	case RSNA_PMK_LIFETIME:
2415		return sm->dot11RSNAConfigPMKLifetime;
2416	case RSNA_PMK_REAUTH_THRESHOLD:
2417		return sm->dot11RSNAConfigPMKReauthThreshold;
2418	case RSNA_SA_TIMEOUT:
2419		return sm->dot11RSNAConfigSATimeout;
2420	case WPA_PARAM_PROTO:
2421		return sm->proto;
2422	case WPA_PARAM_PAIRWISE:
2423		return sm->pairwise_cipher;
2424	case WPA_PARAM_GROUP:
2425		return sm->group_cipher;
2426	case WPA_PARAM_KEY_MGMT:
2427		return sm->key_mgmt;
2428#ifdef CONFIG_IEEE80211W
2429	case WPA_PARAM_MGMT_GROUP:
2430		return sm->mgmt_group_cipher;
2431#endif /* CONFIG_IEEE80211W */
2432	case WPA_PARAM_RSN_ENABLED:
2433		return sm->rsn_enabled;
2434	default:
2435		return 0;
2436	}
2437}
2438
2439
2440/**
2441 * wpa_sm_get_status - Get WPA state machine
2442 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2443 * @buf: Buffer for status information
2444 * @buflen: Maximum buffer length
2445 * @verbose: Whether to include verbose status information
2446 * Returns: Number of bytes written to buf.
2447 *
2448 * Query WPA state machine for status information. This function fills in
2449 * a text area with current status information. If the buffer (buf) is not
2450 * large enough, status information will be truncated to fit the buffer.
2451 */
2452int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2453		      int verbose)
2454{
2455	char *pos = buf, *end = buf + buflen;
2456	int ret;
2457
2458	ret = os_snprintf(pos, end - pos,
2459			  "pairwise_cipher=%s\n"
2460			  "group_cipher=%s\n"
2461			  "key_mgmt=%s\n",
2462			  wpa_cipher_txt(sm->pairwise_cipher),
2463			  wpa_cipher_txt(sm->group_cipher),
2464			  wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
2465	if (ret < 0 || ret >= end - pos)
2466		return pos - buf;
2467	pos += ret;
2468
2469	if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
2470		struct wpa_ie_data rsn;
2471		if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
2472		    >= 0 &&
2473		    rsn.capabilities & (WPA_CAPABILITY_MFPR |
2474					WPA_CAPABILITY_MFPC)) {
2475			ret = os_snprintf(pos, end - pos, "pmf=%d\n",
2476					  (rsn.capabilities &
2477					   WPA_CAPABILITY_MFPR) ? 2 : 1);
2478			if (ret < 0 || ret >= end - pos)
2479				return pos - buf;
2480			pos += ret;
2481		}
2482	}
2483
2484	return pos - buf;
2485}
2486
2487
2488/**
2489 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2490 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2491 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2492 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2493 * Returns: 0 on success, -1 on failure
2494 */
2495int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2496				    size_t *wpa_ie_len)
2497{
2498	int res;
2499
2500	if (sm == NULL)
2501		return -1;
2502
2503	res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2504	if (res < 0)
2505		return -1;
2506	*wpa_ie_len = res;
2507
2508	wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2509		    wpa_ie, *wpa_ie_len);
2510
2511	if (sm->assoc_wpa_ie == NULL) {
2512		/*
2513		 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2514		 * the correct version of the IE even if PMKSA caching is
2515		 * aborted (which would remove PMKID from IE generation).
2516		 */
2517		sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2518		if (sm->assoc_wpa_ie == NULL)
2519			return -1;
2520
2521		os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2522		sm->assoc_wpa_ie_len = *wpa_ie_len;
2523	}
2524
2525	return 0;
2526}
2527
2528
2529/**
2530 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2531 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2532 * @ie: Pointer to IE data (starting from id)
2533 * @len: IE length
2534 * Returns: 0 on success, -1 on failure
2535 *
2536 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2537 * Request frame. The IE will be used to override the default value generated
2538 * with wpa_sm_set_assoc_wpa_ie_default().
2539 */
2540int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2541{
2542	if (sm == NULL)
2543		return -1;
2544
2545	os_free(sm->assoc_wpa_ie);
2546	if (ie == NULL || len == 0) {
2547		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2548			"WPA: clearing own WPA/RSN IE");
2549		sm->assoc_wpa_ie = NULL;
2550		sm->assoc_wpa_ie_len = 0;
2551	} else {
2552		wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2553		sm->assoc_wpa_ie = os_malloc(len);
2554		if (sm->assoc_wpa_ie == NULL)
2555			return -1;
2556
2557		os_memcpy(sm->assoc_wpa_ie, ie, len);
2558		sm->assoc_wpa_ie_len = len;
2559	}
2560
2561	return 0;
2562}
2563
2564
2565/**
2566 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2567 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2568 * @ie: Pointer to IE data (starting from id)
2569 * @len: IE length
2570 * Returns: 0 on success, -1 on failure
2571 *
2572 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2573 * frame.
2574 */
2575int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2576{
2577	if (sm == NULL)
2578		return -1;
2579
2580	os_free(sm->ap_wpa_ie);
2581	if (ie == NULL || len == 0) {
2582		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2583			"WPA: clearing AP WPA IE");
2584		sm->ap_wpa_ie = NULL;
2585		sm->ap_wpa_ie_len = 0;
2586	} else {
2587		wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2588		sm->ap_wpa_ie = os_malloc(len);
2589		if (sm->ap_wpa_ie == NULL)
2590			return -1;
2591
2592		os_memcpy(sm->ap_wpa_ie, ie, len);
2593		sm->ap_wpa_ie_len = len;
2594	}
2595
2596	return 0;
2597}
2598
2599
2600/**
2601 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2602 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2603 * @ie: Pointer to IE data (starting from id)
2604 * @len: IE length
2605 * Returns: 0 on success, -1 on failure
2606 *
2607 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2608 * frame.
2609 */
2610int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2611{
2612	if (sm == NULL)
2613		return -1;
2614
2615	os_free(sm->ap_rsn_ie);
2616	if (ie == NULL || len == 0) {
2617		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2618			"WPA: clearing AP RSN IE");
2619		sm->ap_rsn_ie = NULL;
2620		sm->ap_rsn_ie_len = 0;
2621	} else {
2622		wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2623		sm->ap_rsn_ie = os_malloc(len);
2624		if (sm->ap_rsn_ie == NULL)
2625			return -1;
2626
2627		os_memcpy(sm->ap_rsn_ie, ie, len);
2628		sm->ap_rsn_ie_len = len;
2629	}
2630
2631	return 0;
2632}
2633
2634
2635/**
2636 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2637 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2638 * @data: Pointer to data area for parsing results
2639 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2640 *
2641 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2642 * parsed data into data.
2643 */
2644int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2645{
2646	if (sm == NULL)
2647		return -1;
2648
2649	if (sm->assoc_wpa_ie == NULL) {
2650		wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2651			"WPA: No WPA/RSN IE available from association info");
2652		return -1;
2653	}
2654	if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2655		return -2;
2656	return 0;
2657}
2658
2659
2660int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
2661{
2662#ifndef CONFIG_NO_WPA2
2663	return pmksa_cache_list(sm->pmksa, buf, len);
2664#else /* CONFIG_NO_WPA2 */
2665	return -1;
2666#endif /* CONFIG_NO_WPA2 */
2667}
2668
2669
2670void wpa_sm_drop_sa(struct wpa_sm *sm)
2671{
2672	wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
2673	sm->ptk_set = 0;
2674	sm->tptk_set = 0;
2675	os_memset(sm->pmk, 0, sizeof(sm->pmk));
2676	os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2677	os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2678	os_memset(&sm->gtk, 0, sizeof(sm->gtk));
2679	os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
2680#ifdef CONFIG_IEEE80211W
2681	os_memset(&sm->igtk, 0, sizeof(sm->igtk));
2682	os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
2683#endif /* CONFIG_IEEE80211W */
2684}
2685
2686
2687int wpa_sm_has_ptk(struct wpa_sm *sm)
2688{
2689	if (sm == NULL)
2690		return 0;
2691	return sm->ptk_set;
2692}
2693
2694
2695void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
2696{
2697	os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
2698}
2699
2700
2701void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
2702{
2703#ifndef CONFIG_NO_WPA2
2704	pmksa_cache_flush(sm->pmksa, network_ctx);
2705#endif /* CONFIG_NO_WPA2 */
2706}
2707
2708
2709#ifdef CONFIG_WNM
2710int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
2711{
2712	struct wpa_gtk_data gd;
2713#ifdef CONFIG_IEEE80211W
2714	struct wpa_igtk_kde igd;
2715	u16 keyidx;
2716#endif /* CONFIG_IEEE80211W */
2717	u16 keyinfo;
2718	u8 keylen;  /* plaintext key len */
2719	u8 *key_rsc;
2720
2721	os_memset(&gd, 0, sizeof(gd));
2722#ifdef CONFIG_IEEE80211W
2723	os_memset(&igd, 0, sizeof(igd));
2724#endif /* CONFIG_IEEE80211W */
2725
2726	keylen = wpa_cipher_key_len(sm->group_cipher);
2727	gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
2728	gd.alg = wpa_cipher_to_alg(sm->group_cipher);
2729	if (gd.alg == WPA_ALG_NONE) {
2730		wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
2731		return -1;
2732	}
2733
2734	if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
2735		key_rsc = buf + 5;
2736		keyinfo = WPA_GET_LE16(buf + 2);
2737		gd.gtk_len = keylen;
2738		if (gd.gtk_len != buf[4]) {
2739			wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
2740				   gd.gtk_len, buf[4]);
2741			return -1;
2742		}
2743		gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
2744		gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
2745		         sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
2746
2747		os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
2748
2749		wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
2750				gd.gtk, gd.gtk_len);
2751		if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
2752			wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
2753				   "WNM mode");
2754			return -1;
2755		}
2756#ifdef CONFIG_IEEE80211W
2757	} else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
2758		const struct wpa_igtk_kde *igtk;
2759
2760		igtk = (const struct wpa_igtk_kde *) (buf + 2);
2761		if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
2762			return -1;
2763#endif /* CONFIG_IEEE80211W */
2764	} else {
2765		wpa_printf(MSG_DEBUG, "Unknown element id");
2766		return -1;
2767	}
2768
2769	return 0;
2770}
2771#endif /* CONFIG_WNM */
2772