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