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