2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
3 * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
22 #include "eapol_supp/eapol_supp_sm.h"
24 #include "pmksa_cache.h"
28 #include "ieee802_11_defs.h"
32 * wpa_cipher_txt - Convert cipher suite to a text string
33 * @cipher: Cipher suite (WPA_CIPHER_* enum)
34 * Returns: Pointer to a text string of the cipher suite name
36 static const char * wpa_cipher_txt(int cipher)
41 case WPA_CIPHER_WEP40:
43 case WPA_CIPHER_WEP104:
56 * wpa_key_mgmt_txt - Convert key management suite to a text string
57 * @key_mgmt: Key management suite (WPA_KEY_MGMT_* enum)
58 * @proto: WPA/WPA2 version (WPA_PROTO_*)
59 * Returns: Pointer to a text string of the key management suite name
61 static const char * wpa_key_mgmt_txt(int key_mgmt, int proto)
64 case WPA_KEY_MGMT_IEEE8021X:
65 return proto == WPA_PROTO_RSN ?
66 "WPA2/IEEE 802.1X/EAP" : "WPA/IEEE 802.1X/EAP";
67 case WPA_KEY_MGMT_PSK:
68 return proto == WPA_PROTO_RSN ?
69 "WPA2-PSK" : "WPA-PSK";
70 case WPA_KEY_MGMT_NONE:
72 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
73 return "IEEE 802.1X (no WPA)";
74 #ifdef CONFIG_IEEE80211R
75 case WPA_KEY_MGMT_FT_IEEE8021X:
77 case WPA_KEY_MGMT_FT_PSK:
79 #endif /* CONFIG_IEEE80211R */
87 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
88 * @sm: Pointer to WPA state machine data from wpa_sm_init()
89 * @kck: Key Confirmation Key (KCK, part of PTK)
90 * @ver: Version field from Key Info
91 * @dest: Destination address for the frame
92 * @proto: Ethertype (usually ETH_P_EAPOL)
93 * @msg: EAPOL-Key message
94 * @msg_len: Length of message
95 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
97 void wpa_eapol_key_send(struct wpa_sm *sm, const u8 *kck,
98 int ver, const u8 *dest, u16 proto,
99 u8 *msg, size_t msg_len, u8 *key_mic)
101 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
103 * Association event was not yet received; try to fetch
104 * BSSID from the driver.
106 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
107 wpa_printf(MSG_DEBUG, "WPA: Failed to read BSSID for "
108 "EAPOL-Key destination address");
111 wpa_printf(MSG_DEBUG, "WPA: Use BSSID (" MACSTR
112 ") as the destination for EAPOL-Key",
117 wpa_eapol_key_mic(kck, ver, msg, msg_len, key_mic);
118 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
119 wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
120 eapol_sm_notify_tx_eapol_key(sm->eapol);
126 * wpa_sm_key_request - Send EAPOL-Key Request
127 * @sm: Pointer to WPA state machine data from wpa_sm_init()
128 * @error: Indicate whether this is an Michael MIC error report
129 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
130 * Returns: Pointer to the current network structure or %NULL on failure
132 * Send an EAPOL-Key Request to the current authenticator. This function is
133 * used to request rekeying and it is usually called when a local Michael MIC
134 * failure is detected.
136 void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
139 struct wpa_eapol_key *reply;
141 u8 bssid[ETH_ALEN], *rbuf;
143 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X ||
144 sm->key_mgmt == WPA_KEY_MGMT_FT_PSK)
145 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
146 else if (sm->pairwise_cipher == WPA_CIPHER_CCMP)
147 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
149 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
151 if (wpa_sm_get_bssid(sm, bssid) < 0) {
152 wpa_printf(MSG_WARNING, "Failed to read BSSID for EAPOL-Key "
157 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
158 sizeof(*reply), &rlen, (void *) &reply);
162 reply->type = sm->proto == WPA_PROTO_RSN ?
163 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
164 key_info = WPA_KEY_INFO_REQUEST | ver;
166 key_info |= WPA_KEY_INFO_MIC;
168 key_info |= WPA_KEY_INFO_ERROR;
170 key_info |= WPA_KEY_INFO_KEY_TYPE;
171 WPA_PUT_BE16(reply->key_info, key_info);
172 WPA_PUT_BE16(reply->key_length, 0);
173 os_memcpy(reply->replay_counter, sm->request_counter,
174 WPA_REPLAY_COUNTER_LEN);
175 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
177 WPA_PUT_BE16(reply->key_data_length, 0);
179 wpa_printf(MSG_INFO, "WPA: Sending EAPOL-Key Request (error=%d "
180 "pairwise=%d ptk_set=%d len=%lu)",
181 error, pairwise, sm->ptk_set, (unsigned long) rlen);
182 wpa_eapol_key_send(sm, sm->ptk.kck, ver, bssid, ETH_P_EAPOL,
183 rbuf, rlen, key_info & WPA_KEY_INFO_MIC ?
184 reply->key_mic : NULL);
188 static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
189 const unsigned char *src_addr,
192 int abort_cached = 0;
194 if (pmkid && !sm->cur_pmksa) {
195 /* When using drivers that generate RSN IE, wpa_supplicant may
196 * not have enough time to get the association information
197 * event before receiving this 1/4 message, so try to find a
198 * matching PMKSA cache entry here. */
199 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid);
201 wpa_printf(MSG_DEBUG, "RSN: found matching PMKID from "
204 wpa_printf(MSG_DEBUG, "RSN: no matching PMKID found");
209 if (pmkid && sm->cur_pmksa &&
210 os_memcmp(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
211 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
212 wpa_sm_set_pmk_from_pmksa(sm);
213 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
214 sm->pmk, sm->pmk_len);
215 eapol_sm_notify_cached(sm->eapol);
216 #ifdef CONFIG_IEEE80211R
218 #endif /* CONFIG_IEEE80211R */
219 } else if ((sm->key_mgmt == WPA_KEY_MGMT_IEEE8021X ||
220 sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) && sm->eapol) {
223 res = eapol_sm_get_key(sm->eapol, sm->pmk, PMK_LEN);
226 * EAP-LEAP is an exception from other EAP methods: it
227 * uses only 16-byte PMK.
229 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
232 #ifdef CONFIG_IEEE80211R
234 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
236 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
237 sm->xxkey_len = PMK_LEN;
238 os_memset(buf, 0, sizeof(buf));
240 #endif /* CONFIG_IEEE80211R */
243 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
244 "machines", sm->pmk, pmk_len);
245 sm->pmk_len = pmk_len;
246 pmksa_cache_add(sm->pmksa, sm->pmk, pmk_len, src_addr,
247 sm->own_addr, sm->network_ctx);
248 if (!sm->cur_pmksa && pmkid &&
249 pmksa_cache_get(sm->pmksa, src_addr, pmkid)) {
250 wpa_printf(MSG_DEBUG, "RSN: the new PMK "
251 "matches with the PMKID");
255 wpa_msg(sm->ctx->ctx, MSG_WARNING,
256 "WPA: Failed to get master session key from "
257 "EAPOL state machines");
258 wpa_msg(sm->ctx->ctx, MSG_WARNING,
259 "WPA: Key handshake aborted");
261 wpa_printf(MSG_DEBUG, "RSN: Cancelled PMKSA "
263 sm->cur_pmksa = NULL;
271 if (abort_cached && (sm->key_mgmt == WPA_KEY_MGMT_IEEE8021X ||
272 sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X)) {
273 /* Send EAPOL-Start to trigger full EAP authentication. */
277 wpa_printf(MSG_DEBUG, "RSN: no PMKSA entry found - trigger "
278 "full EAP authentication");
279 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
280 NULL, 0, &buflen, NULL);
282 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
295 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
296 * @sm: Pointer to WPA state machine data from wpa_sm_init()
297 * @dst: Destination address for the frame
298 * @key: Pointer to the EAPOL-Key frame header
299 * @ver: Version bits from EAPOL-Key Key Info
300 * @nonce: Nonce value for the EAPOL-Key frame
301 * @wpa_ie: WPA/RSN IE
302 * @wpa_ie_len: Length of the WPA/RSN IE
303 * @ptk: PTK to use for keyed hash and encryption
304 * Returns: 0 on success, -1 on failure
306 int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
307 const struct wpa_eapol_key *key,
308 int ver, const u8 *nonce,
309 const u8 *wpa_ie, size_t wpa_ie_len,
313 struct wpa_eapol_key *reply;
316 if (wpa_ie == NULL) {
317 wpa_printf(MSG_WARNING, "WPA: No wpa_ie set - cannot "
322 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
324 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
325 NULL, sizeof(*reply) + wpa_ie_len,
326 &rlen, (void *) &reply);
330 reply->type = sm->proto == WPA_PROTO_RSN ?
331 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
332 WPA_PUT_BE16(reply->key_info,
333 ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
334 if (sm->proto == WPA_PROTO_RSN)
335 WPA_PUT_BE16(reply->key_length, 0);
337 os_memcpy(reply->key_length, key->key_length, 2);
338 os_memcpy(reply->replay_counter, key->replay_counter,
339 WPA_REPLAY_COUNTER_LEN);
341 WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
342 os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
344 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
346 wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
347 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
348 rbuf, rlen, reply->key_mic);
354 static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
355 const struct wpa_eapol_key *key,
358 #ifdef CONFIG_IEEE80211R
359 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X ||
360 sm->key_mgmt == WPA_KEY_MGMT_FT_PSK)
361 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
362 #endif /* CONFIG_IEEE80211R */
364 wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
365 sm->own_addr, sm->bssid, sm->snonce, key->key_nonce,
366 (u8 *) ptk, sizeof(*ptk));
371 static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
372 const unsigned char *src_addr,
373 const struct wpa_eapol_key *key,
376 struct wpa_eapol_ie_parse ie;
380 if (wpa_sm_get_network_ctx(sm) == NULL) {
381 wpa_printf(MSG_WARNING, "WPA: No SSID info found (msg 1 of "
386 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
387 wpa_printf(MSG_DEBUG, "WPA: RX message 1 of 4-Way Handshake from "
388 MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
390 os_memset(&ie, 0, sizeof(ie));
392 #ifndef CONFIG_NO_WPA2
393 if (sm->proto == WPA_PROTO_RSN) {
394 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
395 const u8 *_buf = (const u8 *) (key + 1);
396 size_t len = WPA_GET_BE16(key->key_data_length);
397 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data", _buf, len);
398 wpa_supplicant_parse_ies(_buf, len, &ie);
400 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
401 "Authenticator", ie.pmkid, PMKID_LEN);
404 #endif /* CONFIG_NO_WPA2 */
406 if (wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid))
409 if (sm->renew_snonce) {
410 if (os_get_random(sm->snonce, WPA_NONCE_LEN)) {
411 wpa_msg(sm->ctx->ctx, MSG_WARNING,
412 "WPA: Failed to get random data for SNonce");
415 sm->renew_snonce = 0;
416 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
417 sm->snonce, WPA_NONCE_LEN);
420 /* Calculate PTK which will be stored as a temporary PTK until it has
421 * been verified when processing message 3/4. */
423 wpa_derive_ptk(sm, src_addr, key, ptk);
424 /* Supplicant: swap tx/rx Mic keys */
425 os_memcpy(buf, ptk->u.auth.tx_mic_key, 8);
426 os_memcpy(ptk->u.auth.tx_mic_key, ptk->u.auth.rx_mic_key, 8);
427 os_memcpy(ptk->u.auth.rx_mic_key, buf, 8);
430 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
431 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len,
435 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
439 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
441 struct wpa_sm *sm = eloop_ctx;
442 rsn_preauth_candidate_process(sm);
446 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
447 const u8 *addr, int secure)
449 wpa_msg(sm->ctx->ctx, MSG_INFO, "WPA: Key negotiation completed with "
450 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
451 wpa_cipher_txt(sm->pairwise_cipher),
452 wpa_cipher_txt(sm->group_cipher));
453 wpa_sm_cancel_scan(sm);
454 wpa_sm_cancel_auth_timeout(sm);
455 wpa_sm_set_state(sm, WPA_COMPLETED);
458 wpa_sm_mlme_setprotection(
459 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
460 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
461 eapol_sm_notify_portValid(sm->eapol, TRUE);
462 if (sm->key_mgmt == WPA_KEY_MGMT_PSK ||
463 sm->key_mgmt == WPA_KEY_MGMT_FT_PSK)
464 eapol_sm_notify_eap_success(sm->eapol, TRUE);
466 * Start preauthentication after a short wait to avoid a
467 * possible race condition between the data receive and key
468 * configuration after the 4-Way Handshake. This increases the
469 * likelyhood of the first preauth EAPOL-Start frame getting to
472 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
475 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
476 wpa_printf(MSG_DEBUG, "RSN: Authenticator accepted "
477 "opportunistic PMKSA entry - marking it valid");
478 sm->cur_pmksa->opportunistic = 0;
481 #ifdef CONFIG_IEEE80211R
482 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X ||
483 sm->key_mgmt == WPA_KEY_MGMT_FT_PSK) {
484 /* Prepare for the next transition */
485 wpa_ft_prepare_auth_request(sm);
487 #endif /* CONFIG_IEEE80211R */
491 static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
492 const struct wpa_eapol_key *key)
497 u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
499 wpa_printf(MSG_DEBUG, "WPA: Installing PTK to the driver.");
501 switch (sm->pairwise_cipher) {
502 case WPA_CIPHER_CCMP:
507 case WPA_CIPHER_TKIP:
512 case WPA_CIPHER_NONE:
513 wpa_printf(MSG_DEBUG, "WPA: Pairwise Cipher Suite: "
514 "NONE - do not use pairwise keys");
517 wpa_printf(MSG_WARNING, "WPA: Unsupported pairwise cipher %d",
518 sm->pairwise_cipher);
522 if (sm->proto == WPA_PROTO_RSN) {
525 key_rsc = key->key_rsc;
526 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
529 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
530 (u8 *) sm->ptk.tk1, keylen) < 0) {
531 wpa_printf(MSG_WARNING, "WPA: Failed to set PTK to the "
539 static int wpa_supplicant_check_group_cipher(int group_cipher,
540 int keylen, int maxkeylen,
541 int *key_rsc_len, wpa_alg *alg)
545 switch (group_cipher) {
546 case WPA_CIPHER_CCMP:
547 if (keylen != 16 || maxkeylen < 16) {
554 case WPA_CIPHER_TKIP:
555 if (keylen != 32 || maxkeylen < 32) {
562 case WPA_CIPHER_WEP104:
563 if (keylen != 13 || maxkeylen < 13) {
570 case WPA_CIPHER_WEP40:
571 if (keylen != 5 || maxkeylen < 5) {
579 wpa_printf(MSG_WARNING, "WPA: Unsupported Group Cipher %d",
585 wpa_printf(MSG_WARNING, "WPA: Unsupported %s Group Cipher key "
587 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
594 struct wpa_gtk_data {
596 int tx, key_rsc_len, keyidx;
602 static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
603 const struct wpa_gtk_data *gd,
606 const u8 *_gtk = gd->gtk;
609 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
610 wpa_printf(MSG_DEBUG, "WPA: Installing GTK to the driver "
611 "(keyidx=%d tx=%d len=%d).", gd->keyidx, gd->tx,
613 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
614 if (sm->group_cipher == WPA_CIPHER_TKIP) {
615 /* Swap Tx/Rx keys for Michael MIC */
616 os_memcpy(gtk_buf, gd->gtk, 16);
617 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
618 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
621 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
622 if (wpa_sm_set_key(sm, gd->alg,
623 (u8 *) "\xff\xff\xff\xff\xff\xff",
624 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
625 _gtk, gd->gtk_len) < 0) {
626 wpa_printf(MSG_WARNING, "WPA: Failed to set "
627 "GTK to the driver (Group only).");
630 } else if (wpa_sm_set_key(sm, gd->alg,
631 (u8 *) "\xff\xff\xff\xff\xff\xff",
632 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
633 _gtk, gd->gtk_len) < 0) {
634 wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to "
643 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
646 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
647 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
648 * seemed to set this bit (incorrectly, since Tx is only when
649 * doing Group Key only APs) and without this workaround, the
650 * data connection does not work because wpa_supplicant
651 * configured non-zero keyidx to be used for unicast. */
652 wpa_printf(MSG_INFO, "WPA: Tx bit set for GTK, but pairwise "
653 "keys are used - ignore Tx bit");
660 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
661 const struct wpa_eapol_key *key,
662 const u8 *gtk, size_t gtk_len,
665 #ifndef CONFIG_NO_WPA2
666 struct wpa_gtk_data gd;
669 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
671 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
672 * Reserved [bits 0-7]
676 os_memset(&gd, 0, sizeof(gd));
677 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
680 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
683 gd.keyidx = gtk[0] & 0x3;
684 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
685 !!(gtk[0] & BIT(2)));
689 os_memcpy(gd.gtk, gtk, gtk_len);
690 gd.gtk_len = gtk_len;
692 if (wpa_supplicant_check_group_cipher(sm->group_cipher,
694 &gd.key_rsc_len, &gd.alg) ||
695 wpa_supplicant_install_gtk(sm, &gd, key->key_rsc)) {
696 wpa_printf(MSG_DEBUG, "RSN: Failed to install GTK");
700 wpa_supplicant_key_neg_complete(sm, sm->bssid,
701 key_info & WPA_KEY_INFO_SECURE);
703 #else /* CONFIG_NO_WPA2 */
705 #endif /* CONFIG_NO_WPA2 */
709 static int ieee80211w_set_keys(struct wpa_sm *sm,
710 struct wpa_eapol_ie_parse *ie)
712 #ifdef CONFIG_IEEE80211W
713 if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC)
717 const struct wpa_igtk_kde *igtk;
719 if (ie->igtk_len != sizeof(*igtk))
721 igtk = (const struct wpa_igtk_kde *) ie->igtk;
722 keyidx = WPA_GET_LE16(igtk->keyid);
723 wpa_printf(MSG_DEBUG, "WPA: IGTK keyid %d "
724 "pn %02x%02x%02x%02x%02x%02x",
725 keyidx, MAC2STR(igtk->pn));
726 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
727 igtk->igtk, WPA_IGTK_LEN);
729 wpa_printf(MSG_WARNING, "WPA: Invalid IGTK KeyID %d",
733 if (wpa_sm_set_key(sm, WPA_ALG_IGTK,
734 (u8 *) "\xff\xff\xff\xff\xff\xff",
735 keyidx, 0, igtk->pn, sizeof(igtk->pn),
736 igtk->igtk, WPA_IGTK_LEN) < 0) {
737 wpa_printf(MSG_WARNING, "WPA: Failed to configure IGTK"
744 #else /* CONFIG_IEEE80211W */
746 #endif /* CONFIG_IEEE80211W */
750 static void wpa_report_ie_mismatch(struct wpa_sm *sm,
751 const char *reason, const u8 *src_addr,
752 const u8 *wpa_ie, size_t wpa_ie_len,
753 const u8 *rsn_ie, size_t rsn_ie_len)
755 wpa_msg(sm->ctx->ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
756 reason, MAC2STR(src_addr));
759 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
760 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
763 if (!sm->ap_wpa_ie) {
764 wpa_printf(MSG_INFO, "WPA: No WPA IE in "
767 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
772 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
773 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
776 if (!sm->ap_rsn_ie) {
777 wpa_printf(MSG_INFO, "WPA: No RSN IE in "
780 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
784 wpa_sm_disassociate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
785 wpa_sm_req_scan(sm, 0, 0);
789 static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
790 const unsigned char *src_addr,
791 struct wpa_eapol_ie_parse *ie)
793 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
794 wpa_printf(MSG_DEBUG, "WPA: No WPA/RSN IE for this AP known. "
795 "Trying to get from scan results");
796 if (wpa_sm_get_beacon_ie(sm) < 0) {
797 wpa_printf(MSG_WARNING, "WPA: Could not find AP from "
800 wpa_printf(MSG_DEBUG, "WPA: Found the current AP from "
801 "updated scan results");
805 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
806 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
807 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
808 "with IE in Beacon/ProbeResp (no IE?)",
809 src_addr, ie->wpa_ie, ie->wpa_ie_len,
810 ie->rsn_ie, ie->rsn_ie_len);
814 if ((ie->wpa_ie && sm->ap_wpa_ie &&
815 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
816 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
817 (ie->rsn_ie && sm->ap_rsn_ie &&
818 (ie->rsn_ie_len != sm->ap_rsn_ie_len ||
819 os_memcmp(ie->rsn_ie, sm->ap_rsn_ie, ie->rsn_ie_len) != 0))) {
820 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
821 "with IE in Beacon/ProbeResp",
822 src_addr, ie->wpa_ie, ie->wpa_ie_len,
823 ie->rsn_ie, ie->rsn_ie_len);
827 if (sm->proto == WPA_PROTO_WPA &&
828 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
829 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
830 "detected - RSN was enabled and RSN IE "
831 "was in msg 3/4, but not in "
833 src_addr, ie->wpa_ie, ie->wpa_ie_len,
834 ie->rsn_ie, ie->rsn_ie_len);
838 #ifdef CONFIG_IEEE80211R
839 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X ||
840 sm->key_mgmt == WPA_KEY_MGMT_FT_PSK) {
841 struct rsn_mdie *mdie;
842 /* TODO: verify that full MDIE matches with the one from scan
843 * results, not only mobility domain */
844 mdie = (struct rsn_mdie *) (ie->mdie + 2);
845 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
846 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
847 MOBILITY_DOMAIN_ID_LEN) != 0) {
848 wpa_printf(MSG_DEBUG, "FT: MDIE in msg 3/4 did not "
849 "match with the current mobility domain");
853 #endif /* CONFIG_IEEE80211R */
860 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
861 * @sm: Pointer to WPA state machine data from wpa_sm_init()
862 * @dst: Destination address for the frame
863 * @key: Pointer to the EAPOL-Key frame header
864 * @ver: Version bits from EAPOL-Key Key Info
865 * @key_info: Key Info
866 * @kde: KDEs to include the EAPOL-Key frame
867 * @kde_len: Length of KDEs
868 * @ptk: PTK to use for keyed hash and encryption
869 * Returns: 0 on success, -1 on failure
871 int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
872 const struct wpa_eapol_key *key,
873 u16 ver, u16 key_info,
874 const u8 *kde, size_t kde_len,
878 struct wpa_eapol_key *reply;
882 wpa_hexdump(MSG_DEBUG, "WPA: KDE for msg 4/4", kde, kde_len);
884 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
885 sizeof(*reply) + kde_len,
886 &rlen, (void *) &reply);
890 reply->type = sm->proto == WPA_PROTO_RSN ?
891 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
892 key_info &= WPA_KEY_INFO_SECURE;
893 key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
894 WPA_PUT_BE16(reply->key_info, key_info);
895 if (sm->proto == WPA_PROTO_RSN)
896 WPA_PUT_BE16(reply->key_length, 0);
898 os_memcpy(reply->key_length, key->key_length, 2);
899 os_memcpy(reply->replay_counter, key->replay_counter,
900 WPA_REPLAY_COUNTER_LEN);
902 WPA_PUT_BE16(reply->key_data_length, kde_len);
904 os_memcpy(reply + 1, kde, kde_len);
906 wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
907 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
908 rbuf, rlen, reply->key_mic);
914 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
915 const struct wpa_eapol_key *key,
918 u16 key_info, keylen, len;
920 struct wpa_eapol_ie_parse ie;
922 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
923 wpa_printf(MSG_DEBUG, "WPA: RX message 3 of 4-Way Handshake from "
924 MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
926 key_info = WPA_GET_BE16(key->key_info);
928 pos = (const u8 *) (key + 1);
929 len = WPA_GET_BE16(key->key_data_length);
930 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", pos, len);
931 wpa_supplicant_parse_ies(pos, len, &ie);
932 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
933 wpa_printf(MSG_WARNING, "WPA: GTK IE in unencrypted key data");
936 #ifdef CONFIG_IEEE80211W
937 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
938 wpa_printf(MSG_WARNING, "WPA: IGTK KDE in unencrypted key "
943 if (ie.igtk && ie.igtk_len != sizeof(struct wpa_igtk_kde)) {
944 wpa_printf(MSG_WARNING, "WPA: Invalid IGTK KDE length %lu",
945 (unsigned long) ie.igtk_len);
948 #endif /* CONFIG_IEEE80211W */
950 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
953 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
954 wpa_printf(MSG_WARNING, "WPA: ANonce from message 1 of 4-Way "
955 "Handshake differs from 3 of 4-Way Handshake - drop"
956 " packet (src=" MACSTR ")", MAC2STR(sm->bssid));
960 keylen = WPA_GET_BE16(key->key_length);
961 switch (sm->pairwise_cipher) {
962 case WPA_CIPHER_CCMP:
964 wpa_printf(MSG_WARNING, "WPA: Invalid CCMP key length "
965 "%d (src=" MACSTR ")",
966 keylen, MAC2STR(sm->bssid));
970 case WPA_CIPHER_TKIP:
972 wpa_printf(MSG_WARNING, "WPA: Invalid TKIP key length "
973 "%d (src=" MACSTR ")",
974 keylen, MAC2STR(sm->bssid));
980 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
984 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
985 * for the next 4-Way Handshake. If msg 3 is received again, the old
986 * SNonce will still be used to avoid changing PTK. */
987 sm->renew_snonce = 1;
989 if (key_info & WPA_KEY_INFO_INSTALL) {
990 wpa_supplicant_install_ptk(sm, key);
993 if (key_info & WPA_KEY_INFO_SECURE) {
994 wpa_sm_mlme_setprotection(
995 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
996 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
997 eapol_sm_notify_portValid(sm->eapol, TRUE);
999 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1002 wpa_supplicant_pairwise_gtk(sm, key,
1003 ie.gtk, ie.gtk_len, key_info) < 0) {
1004 wpa_printf(MSG_INFO, "RSN: Failed to configure GTK");
1007 if (ieee80211w_set_keys(sm, &ie) < 0)
1008 wpa_printf(MSG_INFO, "RSN: Failed to configure IGTK");
1012 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1016 struct wpa_gtk_data *gd)
1019 struct wpa_eapol_ie_parse ie;
1021 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
1022 wpa_supplicant_parse_ies(keydata, keydatalen, &ie);
1023 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1024 wpa_printf(MSG_WARNING, "WPA: GTK IE in unencrypted key data");
1027 if (ie.gtk == NULL) {
1028 wpa_printf(MSG_INFO, "WPA: No GTK IE in Group Key msg 1/2");
1031 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1033 if (wpa_supplicant_check_group_cipher(sm->group_cipher,
1034 gd->gtk_len, maxkeylen,
1035 &gd->key_rsc_len, &gd->alg))
1038 wpa_hexdump(MSG_DEBUG, "RSN: received GTK in group key handshake",
1039 ie.gtk, ie.gtk_len);
1040 gd->keyidx = ie.gtk[0] & 0x3;
1041 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1042 !!(ie.gtk[0] & BIT(2)));
1043 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1044 wpa_printf(MSG_INFO, "RSN: Too long GTK in GTK IE "
1045 "(len=%lu)", (unsigned long) ie.gtk_len - 2);
1048 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1050 if (ieee80211w_set_keys(sm, &ie) < 0)
1051 wpa_printf(MSG_INFO, "RSN: Failed to configure IGTK");
1057 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1058 const struct wpa_eapol_key *key,
1059 size_t keydatalen, int key_info,
1060 size_t extra_len, u16 ver,
1061 struct wpa_gtk_data *gd)
1066 gd->gtk_len = WPA_GET_BE16(key->key_length);
1067 maxkeylen = keydatalen;
1068 if (keydatalen > extra_len) {
1069 wpa_printf(MSG_INFO, "WPA: Truncated EAPOL-Key packet:"
1070 " key_data_length=%lu > extra_len=%lu",
1071 (unsigned long) keydatalen,
1072 (unsigned long) extra_len);
1075 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1076 if (maxkeylen < 8) {
1077 wpa_printf(MSG_INFO, "WPA: Too short maxkeylen (%lu)",
1078 (unsigned long) maxkeylen);
1084 if (wpa_supplicant_check_group_cipher(sm->group_cipher,
1085 gd->gtk_len, maxkeylen,
1086 &gd->key_rsc_len, &gd->alg))
1089 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1090 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1091 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1092 os_memcpy(ek, key->key_iv, 16);
1093 os_memcpy(ek + 16, sm->ptk.kek, 16);
1094 if (keydatalen > sizeof(gd->gtk)) {
1095 wpa_printf(MSG_WARNING, "WPA: RC4 key data "
1097 (unsigned long) keydatalen);
1100 os_memcpy(gd->gtk, key + 1, keydatalen);
1101 rc4_skip(ek, 32, 256, gd->gtk, keydatalen);
1102 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1103 if (keydatalen % 8) {
1104 wpa_printf(MSG_WARNING, "WPA: Unsupported AES-WRAP "
1105 "len %lu", (unsigned long) keydatalen);
1108 if (maxkeylen > sizeof(gd->gtk)) {
1109 wpa_printf(MSG_WARNING, "WPA: AES-WRAP key data "
1110 "too long (keydatalen=%lu maxkeylen=%lu)",
1111 (unsigned long) keydatalen,
1112 (unsigned long) maxkeylen);
1115 if (aes_unwrap(sm->ptk.kek, maxkeylen / 8,
1116 (const u8 *) (key + 1), gd->gtk)) {
1117 wpa_printf(MSG_WARNING, "WPA: AES unwrap "
1118 "failed - could not decrypt GTK");
1122 wpa_printf(MSG_WARNING, "WPA: Unsupported key_info type %d",
1126 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1127 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1132 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1133 const struct wpa_eapol_key *key,
1134 int ver, u16 key_info)
1137 struct wpa_eapol_key *reply;
1140 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1141 sizeof(*reply), &rlen, (void *) &reply);
1145 reply->type = sm->proto == WPA_PROTO_RSN ?
1146 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1147 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1148 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1149 WPA_PUT_BE16(reply->key_info, key_info);
1150 if (sm->proto == WPA_PROTO_RSN)
1151 WPA_PUT_BE16(reply->key_length, 0);
1153 os_memcpy(reply->key_length, key->key_length, 2);
1154 os_memcpy(reply->replay_counter, key->replay_counter,
1155 WPA_REPLAY_COUNTER_LEN);
1157 WPA_PUT_BE16(reply->key_data_length, 0);
1159 wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1160 wpa_eapol_key_send(sm, sm->ptk.kck, ver, sm->bssid, ETH_P_EAPOL,
1161 rbuf, rlen, reply->key_mic);
1167 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1168 const unsigned char *src_addr,
1169 const struct wpa_eapol_key *key,
1170 int extra_len, u16 ver)
1172 u16 key_info, keydatalen;
1174 struct wpa_gtk_data gd;
1176 os_memset(&gd, 0, sizeof(gd));
1178 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1179 wpa_printf(MSG_DEBUG, "WPA: RX message 1 of Group Key Handshake from "
1180 MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1182 key_info = WPA_GET_BE16(key->key_info);
1183 keydatalen = WPA_GET_BE16(key->key_data_length);
1185 if (sm->proto == WPA_PROTO_RSN) {
1186 ret = wpa_supplicant_process_1_of_2_rsn(sm,
1187 (const u8 *) (key + 1),
1188 keydatalen, key_info,
1191 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, keydatalen,
1192 key_info, extra_len,
1196 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1201 if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc) ||
1202 wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
1206 wpa_msg(sm->ctx->ctx, MSG_INFO, "WPA: Group rekeying "
1207 "completed with " MACSTR " [GTK=%s]",
1208 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1209 wpa_sm_cancel_auth_timeout(sm);
1210 wpa_sm_set_state(sm, WPA_COMPLETED);
1212 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1214 WPA_KEY_INFO_SECURE);
1219 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1220 struct wpa_eapol_key *key,
1222 const u8 *buf, size_t len)
1227 os_memcpy(mic, key->key_mic, 16);
1229 os_memset(key->key_mic, 0, 16);
1230 wpa_eapol_key_mic(sm->tptk.kck, ver, buf, len,
1232 if (os_memcmp(mic, key->key_mic, 16) != 0) {
1233 wpa_printf(MSG_WARNING, "WPA: Invalid EAPOL-Key MIC "
1234 "when using TPTK - ignoring TPTK");
1239 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1243 if (!ok && sm->ptk_set) {
1244 os_memset(key->key_mic, 0, 16);
1245 wpa_eapol_key_mic(sm->ptk.kck, ver, buf, len,
1247 if (os_memcmp(mic, key->key_mic, 16) != 0) {
1248 wpa_printf(MSG_WARNING, "WPA: Invalid EAPOL-Key MIC "
1249 "- dropping packet");
1256 wpa_printf(MSG_WARNING, "WPA: Could not verify EAPOL-Key MIC "
1257 "- dropping packet");
1261 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1262 WPA_REPLAY_COUNTER_LEN);
1263 sm->rx_replay_counter_set = 1;
1268 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1269 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
1270 struct wpa_eapol_key *key, u16 ver)
1272 u16 keydatalen = WPA_GET_BE16(key->key_data_length);
1274 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
1275 (u8 *) (key + 1), keydatalen);
1277 wpa_printf(MSG_WARNING, "WPA: PTK not available, "
1278 "cannot decrypt EAPOL-Key key data.");
1282 /* Decrypt key data here so that this operation does not need
1283 * to be implemented separately for each message type. */
1284 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1286 os_memcpy(ek, key->key_iv, 16);
1287 os_memcpy(ek + 16, sm->ptk.kek, 16);
1288 rc4_skip(ek, 32, 256, (u8 *) (key + 1), keydatalen);
1289 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1290 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1292 if (keydatalen % 8) {
1293 wpa_printf(MSG_WARNING, "WPA: Unsupported "
1294 "AES-WRAP len %d", keydatalen);
1297 keydatalen -= 8; /* AES-WRAP adds 8 bytes */
1298 buf = os_malloc(keydatalen);
1300 wpa_printf(MSG_WARNING, "WPA: No memory for "
1301 "AES-UNWRAP buffer");
1304 if (aes_unwrap(sm->ptk.kek, keydatalen / 8,
1305 (u8 *) (key + 1), buf)) {
1307 wpa_printf(MSG_WARNING, "WPA: AES unwrap failed - "
1308 "could not decrypt EAPOL-Key key data");
1311 os_memcpy(key + 1, buf, keydatalen);
1313 WPA_PUT_BE16(key->key_data_length, keydatalen);
1315 wpa_printf(MSG_WARNING, "WPA: Unsupported key_info type %d",
1319 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
1320 (u8 *) (key + 1), keydatalen);
1326 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1327 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1329 void wpa_sm_aborted_cached(struct wpa_sm *sm)
1331 if (sm && sm->cur_pmksa) {
1332 wpa_printf(MSG_DEBUG, "RSN: Cancelling PMKSA caching attempt");
1333 sm->cur_pmksa = NULL;
1338 static void wpa_eapol_key_dump(const struct wpa_eapol_key *key)
1340 #ifndef CONFIG_NO_STDOUT_DEBUG
1341 u16 key_info = WPA_GET_BE16(key->key_info);
1343 wpa_printf(MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1344 wpa_printf(MSG_DEBUG, " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s"
1346 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1347 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1348 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1349 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1350 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1351 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1352 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1353 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1354 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1355 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1356 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1357 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1358 wpa_printf(MSG_DEBUG, " key_length=%u key_data_length=%u",
1359 WPA_GET_BE16(key->key_length),
1360 WPA_GET_BE16(key->key_data_length));
1361 wpa_hexdump(MSG_DEBUG, " replay_counter",
1362 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1363 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1364 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1365 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1366 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
1367 wpa_hexdump(MSG_DEBUG, " key_mic", key->key_mic, 16);
1368 #endif /* CONFIG_NO_STDOUT_DEBUG */
1373 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1374 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1375 * @src_addr: Source MAC address of the EAPOL packet
1376 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1377 * @len: Length of the EAPOL frame
1378 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1380 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1381 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1382 * only processing WPA and WPA2 EAPOL-Key frames.
1384 * The received EAPOL-Key packets are validated and valid packets are replied
1385 * to. In addition, key material (PTK, GTK) is configured at the end of a
1386 * successful key handshake.
1388 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1389 const u8 *buf, size_t len)
1391 size_t plen, data_len, extra_len;
1392 struct ieee802_1x_hdr *hdr;
1393 struct wpa_eapol_key *key;
1397 struct wpa_peerkey *peerkey = NULL;
1399 #ifdef CONFIG_IEEE80211R
1400 sm->ft_completed = 0;
1401 #endif /* CONFIG_IEEE80211R */
1403 if (len < sizeof(*hdr) + sizeof(*key)) {
1404 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame too short to be a WPA "
1405 "EAPOL-Key (len %lu, expecting at least %lu)",
1406 (unsigned long) len,
1407 (unsigned long) sizeof(*hdr) + sizeof(*key));
1411 tmp = os_malloc(len);
1414 os_memcpy(tmp, buf, len);
1416 hdr = (struct ieee802_1x_hdr *) tmp;
1417 key = (struct wpa_eapol_key *) (hdr + 1);
1418 plen = be_to_host16(hdr->length);
1419 data_len = plen + sizeof(*hdr);
1420 wpa_printf(MSG_DEBUG, "IEEE 802.1X RX: version=%d type=%d length=%lu",
1421 hdr->version, hdr->type, (unsigned long) plen);
1423 if (hdr->version < EAPOL_VERSION) {
1424 /* TODO: backwards compatibility */
1426 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1427 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame (type %u) discarded, "
1428 "not a Key frame", hdr->type);
1432 if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
1433 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame payload size %lu "
1434 "invalid (frame size %lu)",
1435 (unsigned long) plen, (unsigned long) len);
1440 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1442 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key type (%d) unknown, "
1443 "discarded", key->type);
1447 wpa_eapol_key_dump(key);
1449 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
1450 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", tmp, len);
1451 if (data_len < len) {
1452 wpa_printf(MSG_DEBUG, "WPA: ignoring %lu bytes after the IEEE "
1453 "802.1X data", (unsigned long) len - data_len);
1455 key_info = WPA_GET_BE16(key->key_info);
1456 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1457 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1458 #ifdef CONFIG_IEEE80211R
1459 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1460 #endif /* CONFIG_IEEE80211R */
1461 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1462 wpa_printf(MSG_INFO, "WPA: Unsupported EAPOL-Key descriptor "
1463 "version %d.", ver);
1467 #ifdef CONFIG_IEEE80211R
1468 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X ||
1469 sm->key_mgmt == WPA_KEY_MGMT_FT_PSK) {
1470 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1471 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1472 wpa_printf(MSG_INFO, "FT: AP did not use "
1477 #endif /* CONFIG_IEEE80211R */
1478 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
1479 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1480 wpa_printf(MSG_INFO, "WPA: CCMP is used, but EAPOL-Key "
1481 "descriptor version (%d) is not 2.", ver);
1482 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1483 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1484 /* Earlier versions of IEEE 802.11i did not explicitly
1485 * require version 2 descriptor for all EAPOL-Key
1486 * packets, so allow group keys to use version 1 if
1487 * CCMP is not used for them. */
1488 wpa_printf(MSG_INFO, "WPA: Backwards compatibility: "
1489 "allow invalid version for non-CCMP group "
1495 #ifdef CONFIG_PEERKEY
1496 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1497 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1501 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1502 if (!peerkey->initiator && peerkey->replay_counter_set &&
1503 os_memcmp(key->replay_counter, peerkey->replay_counter,
1504 WPA_REPLAY_COUNTER_LEN) <= 0) {
1505 wpa_printf(MSG_WARNING, "RSN: EAPOL-Key Replay "
1506 "Counter did not increase (STK) - dropping "
1509 } else if (peerkey->initiator) {
1510 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1511 os_memcpy(_tmp, key->replay_counter,
1512 WPA_REPLAY_COUNTER_LEN);
1513 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
1514 if (os_memcmp(_tmp, peerkey->replay_counter,
1515 WPA_REPLAY_COUNTER_LEN) != 0) {
1516 wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key Replay "
1517 "Counter did not match (STK) - "
1524 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
1525 wpa_printf(MSG_INFO, "RSN: Ack bit in key_info from STK peer");
1528 #endif /* CONFIG_PEERKEY */
1530 if (!peerkey && sm->rx_replay_counter_set &&
1531 os_memcmp(key->replay_counter, sm->rx_replay_counter,
1532 WPA_REPLAY_COUNTER_LEN) <= 0) {
1533 wpa_printf(MSG_WARNING, "WPA: EAPOL-Key Replay Counter did not"
1534 " increase - dropping packet");
1538 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
1539 #ifdef CONFIG_PEERKEY
1540 && (peerkey == NULL || !peerkey->initiator)
1541 #endif /* CONFIG_PEERKEY */
1543 wpa_printf(MSG_INFO, "WPA: No Ack bit in key_info");
1547 if (key_info & WPA_KEY_INFO_REQUEST) {
1548 wpa_printf(MSG_INFO, "WPA: EAPOL-Key with Request bit - "
1553 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
1554 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
1557 #ifdef CONFIG_PEERKEY
1558 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
1559 peerkey_verify_eapol_key_mic(sm, peerkey, key, ver, tmp, data_len))
1561 #endif /* CONFIG_PEERKEY */
1563 extra_len = data_len - sizeof(*hdr) - sizeof(*key);
1565 if (WPA_GET_BE16(key->key_data_length) > extra_len) {
1566 wpa_msg(sm->ctx->ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1567 "frame - key_data overflow (%d > %lu)",
1568 WPA_GET_BE16(key->key_data_length),
1569 (unsigned long) extra_len);
1572 extra_len = WPA_GET_BE16(key->key_data_length);
1574 if (sm->proto == WPA_PROTO_RSN &&
1575 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1576 if (wpa_supplicant_decrypt_key_data(sm, key, ver))
1578 extra_len = WPA_GET_BE16(key->key_data_length);
1581 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1582 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
1583 wpa_printf(MSG_WARNING, "WPA: Ignored EAPOL-Key "
1584 "(Pairwise) with non-zero key index");
1588 /* PeerKey 4-Way Handshake */
1589 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver);
1590 } else if (key_info & WPA_KEY_INFO_MIC) {
1591 /* 3/4 4-Way Handshake */
1592 wpa_supplicant_process_3_of_4(sm, key, ver);
1594 /* 1/4 4-Way Handshake */
1595 wpa_supplicant_process_1_of_4(sm, src_addr, key,
1598 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1599 /* PeerKey SMK Handshake */
1600 peerkey_rx_eapol_smk(sm, src_addr, key, extra_len, key_info,
1603 if (key_info & WPA_KEY_INFO_MIC) {
1604 /* 1/2 Group Key Handshake */
1605 wpa_supplicant_process_1_of_2(sm, src_addr, key,
1608 wpa_printf(MSG_WARNING, "WPA: EAPOL-Key (Group) "
1609 "without Mic bit - dropped");
1621 #ifdef CONFIG_CTRL_IFACE
1622 static int wpa_cipher_bits(int cipher)
1625 case WPA_CIPHER_CCMP:
1627 case WPA_CIPHER_TKIP:
1629 case WPA_CIPHER_WEP104:
1631 case WPA_CIPHER_WEP40:
1639 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
1641 switch (sm->key_mgmt) {
1642 case WPA_KEY_MGMT_IEEE8021X:
1643 return (sm->proto == WPA_PROTO_RSN ?
1644 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
1645 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
1646 case WPA_KEY_MGMT_PSK:
1647 return (sm->proto == WPA_PROTO_RSN ?
1648 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
1649 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
1650 #ifdef CONFIG_IEEE80211R
1651 case WPA_KEY_MGMT_FT_IEEE8021X:
1652 return RSN_AUTH_KEY_MGMT_FT_802_1X;
1653 case WPA_KEY_MGMT_FT_PSK:
1654 return RSN_AUTH_KEY_MGMT_FT_PSK;
1655 #endif /* CONFIG_IEEE80211R */
1656 case WPA_KEY_MGMT_WPA_NONE:
1657 return WPA_AUTH_KEY_MGMT_NONE;
1664 static u32 wpa_cipher_suite(struct wpa_sm *sm, int cipher)
1667 case WPA_CIPHER_CCMP:
1668 return (sm->proto == WPA_PROTO_RSN ?
1669 RSN_CIPHER_SUITE_CCMP : WPA_CIPHER_SUITE_CCMP);
1670 case WPA_CIPHER_TKIP:
1671 return (sm->proto == WPA_PROTO_RSN ?
1672 RSN_CIPHER_SUITE_TKIP : WPA_CIPHER_SUITE_TKIP);
1673 case WPA_CIPHER_WEP104:
1674 return (sm->proto == WPA_PROTO_RSN ?
1675 RSN_CIPHER_SUITE_WEP104 : WPA_CIPHER_SUITE_WEP104);
1676 case WPA_CIPHER_WEP40:
1677 return (sm->proto == WPA_PROTO_RSN ?
1678 RSN_CIPHER_SUITE_WEP40 : WPA_CIPHER_SUITE_WEP40);
1679 case WPA_CIPHER_NONE:
1680 return (sm->proto == WPA_PROTO_RSN ?
1681 RSN_CIPHER_SUITE_NONE : WPA_CIPHER_SUITE_NONE);
1688 #define RSN_SUITE "%02x-%02x-%02x-%d"
1689 #define RSN_SUITE_ARG(s) \
1690 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
1693 * wpa_sm_get_mib - Dump text list of MIB entries
1694 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1695 * @buf: Buffer for the list
1696 * @buflen: Length of the buffer
1697 * Returns: Number of bytes written to buffer
1699 * This function is used fetch dot11 MIB variables.
1701 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
1703 char pmkid_txt[PMKID_LEN * 2 + 1];
1707 if (sm->cur_pmksa) {
1708 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
1709 sm->cur_pmksa->pmkid, PMKID_LEN);
1711 pmkid_txt[0] = '\0';
1713 if ((sm->key_mgmt == WPA_KEY_MGMT_PSK ||
1714 sm->key_mgmt == WPA_KEY_MGMT_IEEE8021X ||
1715 sm->key_mgmt == WPA_KEY_MGMT_FT_PSK ||
1716 sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) &&
1717 sm->proto == WPA_PROTO_RSN)
1722 ret = os_snprintf(buf, buflen,
1723 "dot11RSNAOptionImplemented=TRUE\n"
1724 "dot11RSNAPreauthenticationImplemented=TRUE\n"
1725 "dot11RSNAEnabled=%s\n"
1726 "dot11RSNAPreauthenticationEnabled=%s\n"
1727 "dot11RSNAConfigVersion=%d\n"
1728 "dot11RSNAConfigPairwiseKeysSupported=5\n"
1729 "dot11RSNAConfigGroupCipherSize=%d\n"
1730 "dot11RSNAConfigPMKLifetime=%d\n"
1731 "dot11RSNAConfigPMKReauthThreshold=%d\n"
1732 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
1733 "dot11RSNAConfigSATimeout=%d\n",
1734 rsna ? "TRUE" : "FALSE",
1735 rsna ? "TRUE" : "FALSE",
1737 wpa_cipher_bits(sm->group_cipher),
1738 sm->dot11RSNAConfigPMKLifetime,
1739 sm->dot11RSNAConfigPMKReauthThreshold,
1740 sm->dot11RSNAConfigSATimeout);
1741 if (ret < 0 || (size_t) ret >= buflen)
1746 buf + len, buflen - len,
1747 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
1748 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
1749 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
1750 "dot11RSNAPMKIDUsed=%s\n"
1751 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
1752 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
1753 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
1754 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
1755 "dot11RSNA4WayHandshakeFailures=%u\n",
1756 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1757 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->pairwise_cipher)),
1758 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->group_cipher)),
1760 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1761 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->pairwise_cipher)),
1762 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->group_cipher)),
1763 sm->dot11RSNA4WayHandshakeFailures);
1764 if (ret >= 0 && (size_t) ret < buflen)
1769 #endif /* CONFIG_CTRL_IFACE */
1772 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
1773 void *ctx, int replace)
1775 struct wpa_sm *sm = ctx;
1777 if (sm->cur_pmksa == entry ||
1778 (sm->pmk_len == entry->pmk_len &&
1779 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
1780 wpa_printf(MSG_DEBUG, "RSN: removed current PMKSA entry");
1781 sm->cur_pmksa = NULL;
1784 /* A new entry is being added, so no need to
1785 * deauthenticate in this case. This happens when EAP
1786 * authentication is completed again (reauth or failed
1787 * PMKSA caching attempt). */
1791 os_memset(sm->pmk, 0, sizeof(sm->pmk));
1792 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1793 wpa_sm_req_scan(sm, 0, 0);
1799 * wpa_sm_init - Initialize WPA state machine
1800 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
1801 * Returns: Pointer to the allocated WPA state machine data
1803 * This function is used to allocate a new WPA state machine and the returned
1804 * value is passed to all WPA state machine calls.
1806 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
1810 sm = os_zalloc(sizeof(*sm));
1813 sm->renew_snonce = 1;
1816 sm->dot11RSNAConfigPMKLifetime = 43200;
1817 sm->dot11RSNAConfigPMKReauthThreshold = 70;
1818 sm->dot11RSNAConfigSATimeout = 60;
1820 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
1821 if (sm->pmksa == NULL) {
1822 wpa_printf(MSG_ERROR, "RSN: PMKSA cache initialization "
1833 * wpa_sm_deinit - Deinitialize WPA state machine
1834 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1836 void wpa_sm_deinit(struct wpa_sm *sm)
1840 pmksa_cache_deinit(sm->pmksa);
1841 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
1842 os_free(sm->assoc_wpa_ie);
1843 os_free(sm->ap_wpa_ie);
1844 os_free(sm->ap_rsn_ie);
1852 * wpa_sm_notify_assoc - Notify WPA state machine about association
1853 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1854 * @bssid: The BSSID of the new association
1856 * This function is called to let WPA state machine know that the connection
1859 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
1866 wpa_printf(MSG_DEBUG, "WPA: Association event - clear replay counter");
1867 os_memcpy(sm->bssid, bssid, ETH_ALEN);
1868 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
1869 sm->rx_replay_counter_set = 0;
1870 sm->renew_snonce = 1;
1871 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
1872 rsn_preauth_deinit(sm);
1874 #ifdef CONFIG_IEEE80211R
1875 if (wpa_ft_is_completed(sm)) {
1876 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
1878 /* Prepare for the next transition */
1879 wpa_ft_prepare_auth_request(sm);
1883 #endif /* CONFIG_IEEE80211R */
1887 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
1888 * this is not part of a Fast BSS Transition.
1890 wpa_printf(MSG_DEBUG, "WPA: Clear old PTK");
1898 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
1899 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1901 * This function is called to let WPA state machine know that the connection
1902 * was lost. This will abort any existing pre-authentication session.
1904 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
1906 rsn_preauth_deinit(sm);
1907 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
1908 sm->dot11RSNA4WayHandshakeFailures++;
1913 * wpa_sm_set_pmk - Set PMK
1914 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1916 * @pmk_len: The length of the new PMK in bytes
1918 * Configure the PMK for WPA state machine.
1920 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len)
1925 sm->pmk_len = pmk_len;
1926 os_memcpy(sm->pmk, pmk, pmk_len);
1928 #ifdef CONFIG_IEEE80211R
1929 /* Set XXKey to be PSK for FT key derivation */
1930 sm->xxkey_len = pmk_len;
1931 os_memcpy(sm->xxkey, pmk, pmk_len);
1932 #endif /* CONFIG_IEEE80211R */
1937 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
1938 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1940 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
1943 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
1948 if (sm->cur_pmksa) {
1949 sm->pmk_len = sm->cur_pmksa->pmk_len;
1950 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
1952 sm->pmk_len = PMK_LEN;
1953 os_memset(sm->pmk, 0, PMK_LEN);
1959 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
1960 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1961 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
1963 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
1966 sm->fast_reauth = fast_reauth;
1971 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
1972 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1973 * @scard_ctx: Context pointer for smartcard related callback functions
1975 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
1979 sm->scard_ctx = scard_ctx;
1980 if (sm->preauth_eapol)
1981 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
1986 * wpa_sm_set_config - Notification of current configration change
1987 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1988 * @config: Pointer to current network configuration
1990 * Notify WPA state machine that configuration has changed. config will be
1991 * stored as a backpointer to network configuration. This can be %NULL to clear
1992 * the stored pointed.
1994 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
1999 sm->network_ctx = config;
2001 sm->peerkey_enabled = config->peerkey_enabled;
2002 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2003 sm->proactive_key_caching = config->proactive_key_caching;
2004 sm->eap_workaround = config->eap_workaround;
2005 sm->eap_conf_ctx = config->eap_conf_ctx;
2007 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2008 sm->ssid_len = config->ssid_len;
2012 sm->peerkey_enabled = 0;
2013 sm->allowed_pairwise_cipher = 0;
2014 sm->proactive_key_caching = 0;
2015 sm->eap_workaround = 0;
2016 sm->eap_conf_ctx = NULL;
2019 pmksa_cache_notify_reconfig(sm->pmksa);
2024 * wpa_sm_set_own_addr - Set own MAC address
2025 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2026 * @addr: Own MAC address
2028 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2031 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2036 * wpa_sm_set_ifname - Set network interface name
2037 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2038 * @ifname: Interface name
2039 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2041 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2042 const char *bridge_ifname)
2045 sm->ifname = ifname;
2046 sm->bridge_ifname = bridge_ifname;
2052 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2053 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2054 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2056 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2064 * wpa_sm_set_param - Set WPA state machine parameters
2065 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2066 * @param: Parameter field
2067 * @value: Parameter value
2068 * Returns: 0 on success, -1 on failure
2070 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2079 case RSNA_PMK_LIFETIME:
2081 sm->dot11RSNAConfigPMKLifetime = value;
2085 case RSNA_PMK_REAUTH_THRESHOLD:
2086 if (value > 0 && value <= 100)
2087 sm->dot11RSNAConfigPMKReauthThreshold = value;
2091 case RSNA_SA_TIMEOUT:
2093 sm->dot11RSNAConfigSATimeout = value;
2097 case WPA_PARAM_PROTO:
2100 case WPA_PARAM_PAIRWISE:
2101 sm->pairwise_cipher = value;
2103 case WPA_PARAM_GROUP:
2104 sm->group_cipher = value;
2106 case WPA_PARAM_KEY_MGMT:
2107 sm->key_mgmt = value;
2109 #ifdef CONFIG_IEEE80211W
2110 case WPA_PARAM_MGMT_GROUP:
2111 sm->mgmt_group_cipher = value;
2113 #endif /* CONFIG_IEEE80211W */
2114 case WPA_PARAM_RSN_ENABLED:
2115 sm->rsn_enabled = value;
2126 * wpa_sm_get_param - Get WPA state machine parameters
2127 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2128 * @param: Parameter field
2129 * Returns: Parameter value
2131 unsigned int wpa_sm_get_param(struct wpa_sm *sm, enum wpa_sm_conf_params param)
2137 case RSNA_PMK_LIFETIME:
2138 return sm->dot11RSNAConfigPMKLifetime;
2139 case RSNA_PMK_REAUTH_THRESHOLD:
2140 return sm->dot11RSNAConfigPMKReauthThreshold;
2141 case RSNA_SA_TIMEOUT:
2142 return sm->dot11RSNAConfigSATimeout;
2143 case WPA_PARAM_PROTO:
2145 case WPA_PARAM_PAIRWISE:
2146 return sm->pairwise_cipher;
2147 case WPA_PARAM_GROUP:
2148 return sm->group_cipher;
2149 case WPA_PARAM_KEY_MGMT:
2150 return sm->key_mgmt;
2151 #ifdef CONFIG_IEEE80211W
2152 case WPA_PARAM_MGMT_GROUP:
2153 return sm->mgmt_group_cipher;
2154 #endif /* CONFIG_IEEE80211W */
2155 case WPA_PARAM_RSN_ENABLED:
2156 return sm->rsn_enabled;
2164 * wpa_sm_get_status - Get WPA state machine
2165 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2166 * @buf: Buffer for status information
2167 * @buflen: Maximum buffer length
2168 * @verbose: Whether to include verbose status information
2169 * Returns: Number of bytes written to buf.
2171 * Query WPA state machine for status information. This function fills in
2172 * a text area with current status information. If the buffer (buf) is not
2173 * large enough, status information will be truncated to fit the buffer.
2175 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2178 char *pos = buf, *end = buf + buflen;
2181 ret = os_snprintf(pos, end - pos,
2182 "pairwise_cipher=%s\n"
2185 wpa_cipher_txt(sm->pairwise_cipher),
2186 wpa_cipher_txt(sm->group_cipher),
2187 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
2188 if (ret < 0 || ret >= end - pos)
2196 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2197 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2198 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2199 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2200 * Returns: 0 on success, -1 on failure
2202 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2210 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2215 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2216 wpa_ie, *wpa_ie_len);
2218 if (sm->assoc_wpa_ie == NULL) {
2220 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2221 * the correct version of the IE even if PMKSA caching is
2222 * aborted (which would remove PMKID from IE generation).
2224 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2225 if (sm->assoc_wpa_ie == NULL)
2228 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2229 sm->assoc_wpa_ie_len = *wpa_ie_len;
2237 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2238 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2239 * @ie: Pointer to IE data (starting from id)
2241 * Returns: 0 on success, -1 on failure
2243 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2244 * Request frame. The IE will be used to override the default value generated
2245 * with wpa_sm_set_assoc_wpa_ie_default().
2247 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2252 os_free(sm->assoc_wpa_ie);
2253 if (ie == NULL || len == 0) {
2254 wpa_printf(MSG_DEBUG, "WPA: clearing own WPA/RSN IE");
2255 sm->assoc_wpa_ie = NULL;
2256 sm->assoc_wpa_ie_len = 0;
2258 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2259 sm->assoc_wpa_ie = os_malloc(len);
2260 if (sm->assoc_wpa_ie == NULL)
2263 os_memcpy(sm->assoc_wpa_ie, ie, len);
2264 sm->assoc_wpa_ie_len = len;
2272 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2273 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2274 * @ie: Pointer to IE data (starting from id)
2276 * Returns: 0 on success, -1 on failure
2278 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2281 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2286 os_free(sm->ap_wpa_ie);
2287 if (ie == NULL || len == 0) {
2288 wpa_printf(MSG_DEBUG, "WPA: clearing AP WPA IE");
2289 sm->ap_wpa_ie = NULL;
2290 sm->ap_wpa_ie_len = 0;
2292 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2293 sm->ap_wpa_ie = os_malloc(len);
2294 if (sm->ap_wpa_ie == NULL)
2297 os_memcpy(sm->ap_wpa_ie, ie, len);
2298 sm->ap_wpa_ie_len = len;
2306 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2307 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2308 * @ie: Pointer to IE data (starting from id)
2310 * Returns: 0 on success, -1 on failure
2312 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2315 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2320 os_free(sm->ap_rsn_ie);
2321 if (ie == NULL || len == 0) {
2322 wpa_printf(MSG_DEBUG, "WPA: clearing AP RSN IE");
2323 sm->ap_rsn_ie = NULL;
2324 sm->ap_rsn_ie_len = 0;
2326 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2327 sm->ap_rsn_ie = os_malloc(len);
2328 if (sm->ap_rsn_ie == NULL)
2331 os_memcpy(sm->ap_rsn_ie, ie, len);
2332 sm->ap_rsn_ie_len = len;
2340 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2341 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2342 * @data: Pointer to data area for parsing results
2343 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2345 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2346 * parsed data into data.
2348 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2350 if (sm == NULL || sm->assoc_wpa_ie == NULL) {
2351 wpa_printf(MSG_DEBUG, "WPA: No WPA/RSN IE available from "
2352 "association info");
2355 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))