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 */
80 #ifdef CONFIG_IEEE80211W
81 case WPA_KEY_MGMT_IEEE8021X_SHA256:
82 return "WPA2-EAP-SHA256";
83 case WPA_KEY_MGMT_PSK_SHA256:
84 return "WPA2-PSK-SHA256";
85 #endif /* CONFIG_IEEE80211W */
93 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
94 * @sm: Pointer to WPA state machine data from wpa_sm_init()
95 * @kck: Key Confirmation Key (KCK, part of PTK)
96 * @ver: Version field from Key Info
97 * @dest: Destination address for the frame
98 * @proto: Ethertype (usually ETH_P_EAPOL)
99 * @msg: EAPOL-Key message
100 * @msg_len: Length of message
101 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
103 void wpa_eapol_key_send(struct wpa_sm *sm, const u8 *kck,
104 int ver, const u8 *dest, u16 proto,
105 u8 *msg, size_t msg_len, u8 *key_mic)
107 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
109 * Association event was not yet received; try to fetch
110 * BSSID from the driver.
112 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
113 wpa_printf(MSG_DEBUG, "WPA: Failed to read BSSID for "
114 "EAPOL-Key destination address");
117 wpa_printf(MSG_DEBUG, "WPA: Use BSSID (" MACSTR
118 ") as the destination for EAPOL-Key",
123 wpa_eapol_key_mic(kck, ver, msg, msg_len, key_mic);
124 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
125 wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
126 eapol_sm_notify_tx_eapol_key(sm->eapol);
132 * wpa_sm_key_request - Send EAPOL-Key Request
133 * @sm: Pointer to WPA state machine data from wpa_sm_init()
134 * @error: Indicate whether this is an Michael MIC error report
135 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
137 * Send an EAPOL-Key Request to the current authenticator. This function is
138 * used to request rekeying and it is usually called when a local Michael MIC
139 * failure is detected.
141 void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
144 struct wpa_eapol_key *reply;
146 u8 bssid[ETH_ALEN], *rbuf;
148 if (wpa_key_mgmt_ft(sm->key_mgmt) || wpa_key_mgmt_sha256(sm->key_mgmt))
149 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
150 else if (sm->pairwise_cipher == WPA_CIPHER_CCMP)
151 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
153 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
155 if (wpa_sm_get_bssid(sm, bssid) < 0) {
156 wpa_printf(MSG_WARNING, "Failed to read BSSID for EAPOL-Key "
161 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
162 sizeof(*reply), &rlen, (void *) &reply);
166 reply->type = sm->proto == WPA_PROTO_RSN ?
167 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
168 key_info = WPA_KEY_INFO_REQUEST | ver;
170 key_info |= WPA_KEY_INFO_MIC;
172 key_info |= WPA_KEY_INFO_ERROR;
174 key_info |= WPA_KEY_INFO_KEY_TYPE;
175 WPA_PUT_BE16(reply->key_info, key_info);
176 WPA_PUT_BE16(reply->key_length, 0);
177 os_memcpy(reply->replay_counter, sm->request_counter,
178 WPA_REPLAY_COUNTER_LEN);
179 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
181 WPA_PUT_BE16(reply->key_data_length, 0);
183 wpa_printf(MSG_INFO, "WPA: Sending EAPOL-Key Request (error=%d "
184 "pairwise=%d ptk_set=%d len=%lu)",
185 error, pairwise, sm->ptk_set, (unsigned long) rlen);
186 wpa_eapol_key_send(sm, sm->ptk.kck, ver, bssid, ETH_P_EAPOL,
187 rbuf, rlen, key_info & WPA_KEY_INFO_MIC ?
188 reply->key_mic : NULL);
192 static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
193 const unsigned char *src_addr,
196 int abort_cached = 0;
198 if (pmkid && !sm->cur_pmksa) {
199 /* When using drivers that generate RSN IE, wpa_supplicant may
200 * not have enough time to get the association information
201 * event before receiving this 1/4 message, so try to find a
202 * matching PMKSA cache entry here. */
203 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid);
205 wpa_printf(MSG_DEBUG, "RSN: found matching PMKID from "
208 wpa_printf(MSG_DEBUG, "RSN: no matching PMKID found");
213 if (pmkid && sm->cur_pmksa &&
214 os_memcmp(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
215 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
216 wpa_sm_set_pmk_from_pmksa(sm);
217 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
218 sm->pmk, sm->pmk_len);
219 eapol_sm_notify_cached(sm->eapol);
220 #ifdef CONFIG_IEEE80211R
222 #endif /* CONFIG_IEEE80211R */
223 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
226 res = eapol_sm_get_key(sm->eapol, sm->pmk, PMK_LEN);
229 * EAP-LEAP is an exception from other EAP methods: it
230 * uses only 16-byte PMK.
232 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
235 #ifdef CONFIG_IEEE80211R
237 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
239 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
240 sm->xxkey_len = PMK_LEN;
241 os_memset(buf, 0, sizeof(buf));
243 #endif /* CONFIG_IEEE80211R */
246 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
247 "machines", sm->pmk, pmk_len);
248 sm->pmk_len = pmk_len;
249 if (sm->proto == WPA_PROTO_RSN) {
250 pmksa_cache_add(sm->pmksa, sm->pmk, pmk_len,
251 src_addr, sm->own_addr,
252 sm->network_ctx, sm->key_mgmt);
254 if (!sm->cur_pmksa && pmkid &&
255 pmksa_cache_get(sm->pmksa, src_addr, pmkid)) {
256 wpa_printf(MSG_DEBUG, "RSN: the new PMK "
257 "matches with the PMKID");
261 wpa_msg(sm->ctx->ctx, MSG_WARNING,
262 "WPA: Failed to get master session key from "
263 "EAPOL state machines");
264 wpa_msg(sm->ctx->ctx, MSG_WARNING,
265 "WPA: Key handshake aborted");
267 wpa_printf(MSG_DEBUG, "RSN: Cancelled PMKSA "
269 sm->cur_pmksa = NULL;
271 } else if (!abort_cached) {
277 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) {
278 /* Send EAPOL-Start to trigger full EAP authentication. */
282 wpa_printf(MSG_DEBUG, "RSN: no PMKSA entry found - trigger "
283 "full EAP authentication");
284 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
285 NULL, 0, &buflen, NULL);
287 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
300 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
301 * @sm: Pointer to WPA state machine data from wpa_sm_init()
302 * @dst: Destination address for the frame
303 * @key: Pointer to the EAPOL-Key frame header
304 * @ver: Version bits from EAPOL-Key Key Info
305 * @nonce: Nonce value for the EAPOL-Key frame
306 * @wpa_ie: WPA/RSN IE
307 * @wpa_ie_len: Length of the WPA/RSN IE
308 * @ptk: PTK to use for keyed hash and encryption
309 * Returns: 0 on success, -1 on failure
311 int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
312 const struct wpa_eapol_key *key,
313 int ver, const u8 *nonce,
314 const u8 *wpa_ie, size_t wpa_ie_len,
318 struct wpa_eapol_key *reply;
321 if (wpa_ie == NULL) {
322 wpa_printf(MSG_WARNING, "WPA: No wpa_ie set - cannot "
327 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
329 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
330 NULL, sizeof(*reply) + wpa_ie_len,
331 &rlen, (void *) &reply);
335 reply->type = sm->proto == WPA_PROTO_RSN ?
336 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
337 WPA_PUT_BE16(reply->key_info,
338 ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
339 if (sm->proto == WPA_PROTO_RSN)
340 WPA_PUT_BE16(reply->key_length, 0);
342 os_memcpy(reply->key_length, key->key_length, 2);
343 os_memcpy(reply->replay_counter, key->replay_counter,
344 WPA_REPLAY_COUNTER_LEN);
346 WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
347 os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
349 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
351 wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
352 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
353 rbuf, rlen, reply->key_mic);
359 static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
360 const struct wpa_eapol_key *key,
363 #ifdef CONFIG_IEEE80211R
364 if (wpa_key_mgmt_ft(sm->key_mgmt))
365 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
366 #endif /* CONFIG_IEEE80211R */
368 wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
369 sm->own_addr, sm->bssid, sm->snonce, key->key_nonce,
370 (u8 *) ptk, sizeof(*ptk),
371 wpa_key_mgmt_sha256(sm->key_mgmt));
376 static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
377 const unsigned char *src_addr,
378 const struct wpa_eapol_key *key,
381 struct wpa_eapol_ie_parse ie;
385 if (wpa_sm_get_network_ctx(sm) == NULL) {
386 wpa_printf(MSG_WARNING, "WPA: No SSID info found (msg 1 of "
391 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
392 wpa_printf(MSG_DEBUG, "WPA: RX message 1 of 4-Way Handshake from "
393 MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
395 os_memset(&ie, 0, sizeof(ie));
397 #ifndef CONFIG_NO_WPA2
398 if (sm->proto == WPA_PROTO_RSN) {
399 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
400 const u8 *_buf = (const u8 *) (key + 1);
401 size_t len = WPA_GET_BE16(key->key_data_length);
402 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data", _buf, len);
403 wpa_supplicant_parse_ies(_buf, len, &ie);
405 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
406 "Authenticator", ie.pmkid, PMKID_LEN);
409 #endif /* CONFIG_NO_WPA2 */
411 if (wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid))
414 if (sm->renew_snonce) {
415 if (os_get_random(sm->snonce, WPA_NONCE_LEN)) {
416 wpa_msg(sm->ctx->ctx, MSG_WARNING,
417 "WPA: Failed to get random data for SNonce");
420 sm->renew_snonce = 0;
421 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
422 sm->snonce, WPA_NONCE_LEN);
425 /* Calculate PTK which will be stored as a temporary PTK until it has
426 * been verified when processing message 3/4. */
428 wpa_derive_ptk(sm, src_addr, key, ptk);
429 /* Supplicant: swap tx/rx Mic keys */
430 os_memcpy(buf, ptk->u.auth.tx_mic_key, 8);
431 os_memcpy(ptk->u.auth.tx_mic_key, ptk->u.auth.rx_mic_key, 8);
432 os_memcpy(ptk->u.auth.rx_mic_key, buf, 8);
435 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
436 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len,
440 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
444 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
446 struct wpa_sm *sm = eloop_ctx;
447 rsn_preauth_candidate_process(sm);
451 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
452 const u8 *addr, int secure)
454 wpa_msg(sm->ctx->ctx, MSG_INFO, "WPA: Key negotiation completed with "
455 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
456 wpa_cipher_txt(sm->pairwise_cipher),
457 wpa_cipher_txt(sm->group_cipher));
458 wpa_sm_cancel_auth_timeout(sm);
459 wpa_sm_set_state(sm, WPA_COMPLETED);
462 wpa_sm_mlme_setprotection(
463 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
464 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
465 eapol_sm_notify_portValid(sm->eapol, TRUE);
466 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
467 eapol_sm_notify_eap_success(sm->eapol, TRUE);
469 * Start preauthentication after a short wait to avoid a
470 * possible race condition between the data receive and key
471 * configuration after the 4-Way Handshake. This increases the
472 * likelyhood of the first preauth EAPOL-Start frame getting to
475 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
478 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
479 wpa_printf(MSG_DEBUG, "RSN: Authenticator accepted "
480 "opportunistic PMKSA entry - marking it valid");
481 sm->cur_pmksa->opportunistic = 0;
484 #ifdef CONFIG_IEEE80211R
485 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
486 /* Prepare for the next transition */
487 wpa_ft_prepare_auth_request(sm);
489 #endif /* CONFIG_IEEE80211R */
493 static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
495 struct wpa_sm *sm = eloop_ctx;
496 wpa_printf(MSG_DEBUG, "WPA: Request PTK rekeying");
497 wpa_sm_key_request(sm, 0, 1);
501 static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
502 const struct wpa_eapol_key *key)
507 u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
509 wpa_printf(MSG_DEBUG, "WPA: Installing PTK to the driver.");
511 switch (sm->pairwise_cipher) {
512 case WPA_CIPHER_CCMP:
517 case WPA_CIPHER_TKIP:
522 case WPA_CIPHER_NONE:
523 wpa_printf(MSG_DEBUG, "WPA: Pairwise Cipher Suite: "
524 "NONE - do not use pairwise keys");
527 wpa_printf(MSG_WARNING, "WPA: Unsupported pairwise cipher %d",
528 sm->pairwise_cipher);
532 if (sm->proto == WPA_PROTO_RSN) {
535 key_rsc = key->key_rsc;
536 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
539 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
540 (u8 *) sm->ptk.tk1, keylen) < 0) {
541 wpa_printf(MSG_WARNING, "WPA: Failed to set PTK to the "
546 if (sm->wpa_ptk_rekey) {
547 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
548 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
556 static int wpa_supplicant_check_group_cipher(int group_cipher,
557 int keylen, int maxkeylen,
558 int *key_rsc_len, wpa_alg *alg)
562 switch (group_cipher) {
563 case WPA_CIPHER_CCMP:
564 if (keylen != 16 || maxkeylen < 16) {
571 case WPA_CIPHER_TKIP:
572 if (keylen != 32 || maxkeylen < 32) {
579 case WPA_CIPHER_WEP104:
580 if (keylen != 13 || maxkeylen < 13) {
587 case WPA_CIPHER_WEP40:
588 if (keylen != 5 || maxkeylen < 5) {
596 wpa_printf(MSG_WARNING, "WPA: Unsupported Group Cipher %d",
602 wpa_printf(MSG_WARNING, "WPA: Unsupported %s Group Cipher key "
604 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
611 struct wpa_gtk_data {
613 int tx, key_rsc_len, keyidx;
619 static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
620 const struct wpa_gtk_data *gd,
623 const u8 *_gtk = gd->gtk;
626 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
627 wpa_printf(MSG_DEBUG, "WPA: Installing GTK to the driver "
628 "(keyidx=%d tx=%d len=%d).", gd->keyidx, gd->tx,
630 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
631 if (sm->group_cipher == WPA_CIPHER_TKIP) {
632 /* Swap Tx/Rx keys for Michael MIC */
633 os_memcpy(gtk_buf, gd->gtk, 16);
634 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
635 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
638 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
639 if (wpa_sm_set_key(sm, gd->alg,
640 (u8 *) "\xff\xff\xff\xff\xff\xff",
641 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
642 _gtk, gd->gtk_len) < 0) {
643 wpa_printf(MSG_WARNING, "WPA: Failed to set "
644 "GTK to the driver (Group only).");
647 } else if (wpa_sm_set_key(sm, gd->alg,
648 (u8 *) "\xff\xff\xff\xff\xff\xff",
649 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
650 _gtk, gd->gtk_len) < 0) {
651 wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to "
660 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
663 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
664 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
665 * seemed to set this bit (incorrectly, since Tx is only when
666 * doing Group Key only APs) and without this workaround, the
667 * data connection does not work because wpa_supplicant
668 * configured non-zero keyidx to be used for unicast. */
669 wpa_printf(MSG_INFO, "WPA: Tx bit set for GTK, but pairwise "
670 "keys are used - ignore Tx bit");
677 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
678 const struct wpa_eapol_key *key,
679 const u8 *gtk, size_t gtk_len,
682 #ifndef CONFIG_NO_WPA2
683 struct wpa_gtk_data gd;
686 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
688 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
689 * Reserved [bits 0-7]
693 os_memset(&gd, 0, sizeof(gd));
694 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
697 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
700 gd.keyidx = gtk[0] & 0x3;
701 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
702 !!(gtk[0] & BIT(2)));
706 os_memcpy(gd.gtk, gtk, gtk_len);
707 gd.gtk_len = gtk_len;
709 if (wpa_supplicant_check_group_cipher(sm->group_cipher,
711 &gd.key_rsc_len, &gd.alg) ||
712 wpa_supplicant_install_gtk(sm, &gd, key->key_rsc)) {
713 wpa_printf(MSG_DEBUG, "RSN: Failed to install GTK");
717 wpa_supplicant_key_neg_complete(sm, sm->bssid,
718 key_info & WPA_KEY_INFO_SECURE);
720 #else /* CONFIG_NO_WPA2 */
722 #endif /* CONFIG_NO_WPA2 */
726 static int ieee80211w_set_keys(struct wpa_sm *sm,
727 struct wpa_eapol_ie_parse *ie)
729 #ifdef CONFIG_IEEE80211W
730 if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC)
734 const struct wpa_igtk_kde *igtk;
736 if (ie->igtk_len != sizeof(*igtk))
738 igtk = (const struct wpa_igtk_kde *) ie->igtk;
739 keyidx = WPA_GET_LE16(igtk->keyid);
740 wpa_printf(MSG_DEBUG, "WPA: IGTK keyid %d "
741 "pn %02x%02x%02x%02x%02x%02x",
742 keyidx, MAC2STR(igtk->pn));
743 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
744 igtk->igtk, WPA_IGTK_LEN);
746 wpa_printf(MSG_WARNING, "WPA: Invalid IGTK KeyID %d",
750 if (wpa_sm_set_key(sm, WPA_ALG_IGTK,
751 (u8 *) "\xff\xff\xff\xff\xff\xff",
752 keyidx, 0, igtk->pn, sizeof(igtk->pn),
753 igtk->igtk, WPA_IGTK_LEN) < 0) {
754 wpa_printf(MSG_WARNING, "WPA: Failed to configure IGTK"
761 #else /* CONFIG_IEEE80211W */
763 #endif /* CONFIG_IEEE80211W */
767 static void wpa_report_ie_mismatch(struct wpa_sm *sm,
768 const char *reason, const u8 *src_addr,
769 const u8 *wpa_ie, size_t wpa_ie_len,
770 const u8 *rsn_ie, size_t rsn_ie_len)
772 wpa_msg(sm->ctx->ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
773 reason, MAC2STR(src_addr));
776 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
777 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
780 if (!sm->ap_wpa_ie) {
781 wpa_printf(MSG_INFO, "WPA: No WPA IE in "
784 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
789 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
790 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
793 if (!sm->ap_rsn_ie) {
794 wpa_printf(MSG_INFO, "WPA: No RSN IE in "
797 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
801 wpa_sm_disassociate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
805 static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
806 const unsigned char *src_addr,
807 struct wpa_eapol_ie_parse *ie)
809 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
810 wpa_printf(MSG_DEBUG, "WPA: No WPA/RSN IE for this AP known. "
811 "Trying to get from scan results");
812 if (wpa_sm_get_beacon_ie(sm) < 0) {
813 wpa_printf(MSG_WARNING, "WPA: Could not find AP from "
816 wpa_printf(MSG_DEBUG, "WPA: Found the current AP from "
817 "updated scan results");
821 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
822 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
823 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
824 "with IE in Beacon/ProbeResp (no IE?)",
825 src_addr, ie->wpa_ie, ie->wpa_ie_len,
826 ie->rsn_ie, ie->rsn_ie_len);
830 if ((ie->wpa_ie && sm->ap_wpa_ie &&
831 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
832 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
833 (ie->rsn_ie && sm->ap_rsn_ie &&
834 (ie->rsn_ie_len != sm->ap_rsn_ie_len ||
835 os_memcmp(ie->rsn_ie, sm->ap_rsn_ie, ie->rsn_ie_len) != 0))) {
836 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
837 "with IE in Beacon/ProbeResp",
838 src_addr, ie->wpa_ie, ie->wpa_ie_len,
839 ie->rsn_ie, ie->rsn_ie_len);
843 if (sm->proto == WPA_PROTO_WPA &&
844 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
845 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
846 "detected - RSN was enabled and RSN IE "
847 "was in msg 3/4, but not in "
849 src_addr, ie->wpa_ie, ie->wpa_ie_len,
850 ie->rsn_ie, ie->rsn_ie_len);
854 #ifdef CONFIG_IEEE80211R
855 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
856 struct rsn_mdie *mdie;
857 /* TODO: verify that full MDIE matches with the one from scan
858 * results, not only mobility domain */
859 mdie = (struct rsn_mdie *) (ie->mdie + 2);
860 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
861 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
862 MOBILITY_DOMAIN_ID_LEN) != 0) {
863 wpa_printf(MSG_DEBUG, "FT: MDIE in msg 3/4 did not "
864 "match with the current mobility domain");
868 #endif /* CONFIG_IEEE80211R */
875 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
876 * @sm: Pointer to WPA state machine data from wpa_sm_init()
877 * @dst: Destination address for the frame
878 * @key: Pointer to the EAPOL-Key frame header
879 * @ver: Version bits from EAPOL-Key Key Info
880 * @key_info: Key Info
881 * @kde: KDEs to include the EAPOL-Key frame
882 * @kde_len: Length of KDEs
883 * @ptk: PTK to use for keyed hash and encryption
884 * Returns: 0 on success, -1 on failure
886 int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
887 const struct wpa_eapol_key *key,
888 u16 ver, u16 key_info,
889 const u8 *kde, size_t kde_len,
893 struct wpa_eapol_key *reply;
897 wpa_hexdump(MSG_DEBUG, "WPA: KDE for msg 4/4", kde, kde_len);
899 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
900 sizeof(*reply) + kde_len,
901 &rlen, (void *) &reply);
905 reply->type = sm->proto == WPA_PROTO_RSN ?
906 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
907 key_info &= WPA_KEY_INFO_SECURE;
908 key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
909 WPA_PUT_BE16(reply->key_info, key_info);
910 if (sm->proto == WPA_PROTO_RSN)
911 WPA_PUT_BE16(reply->key_length, 0);
913 os_memcpy(reply->key_length, key->key_length, 2);
914 os_memcpy(reply->replay_counter, key->replay_counter,
915 WPA_REPLAY_COUNTER_LEN);
917 WPA_PUT_BE16(reply->key_data_length, kde_len);
919 os_memcpy(reply + 1, kde, kde_len);
921 wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
922 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
923 rbuf, rlen, reply->key_mic);
929 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
930 const struct wpa_eapol_key *key,
933 u16 key_info, keylen, len;
935 struct wpa_eapol_ie_parse ie;
937 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
938 wpa_printf(MSG_DEBUG, "WPA: RX message 3 of 4-Way Handshake from "
939 MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
941 key_info = WPA_GET_BE16(key->key_info);
943 pos = (const u8 *) (key + 1);
944 len = WPA_GET_BE16(key->key_data_length);
945 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", pos, len);
946 wpa_supplicant_parse_ies(pos, len, &ie);
947 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
948 wpa_printf(MSG_WARNING, "WPA: GTK IE in unencrypted key data");
951 #ifdef CONFIG_IEEE80211W
952 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
953 wpa_printf(MSG_WARNING, "WPA: IGTK KDE in unencrypted key "
958 if (ie.igtk && ie.igtk_len != sizeof(struct wpa_igtk_kde)) {
959 wpa_printf(MSG_WARNING, "WPA: Invalid IGTK KDE length %lu",
960 (unsigned long) ie.igtk_len);
963 #endif /* CONFIG_IEEE80211W */
965 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
968 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
969 wpa_printf(MSG_WARNING, "WPA: ANonce from message 1 of 4-Way "
970 "Handshake differs from 3 of 4-Way Handshake - drop"
971 " packet (src=" MACSTR ")", MAC2STR(sm->bssid));
975 keylen = WPA_GET_BE16(key->key_length);
976 switch (sm->pairwise_cipher) {
977 case WPA_CIPHER_CCMP:
979 wpa_printf(MSG_WARNING, "WPA: Invalid CCMP key length "
980 "%d (src=" MACSTR ")",
981 keylen, MAC2STR(sm->bssid));
985 case WPA_CIPHER_TKIP:
987 wpa_printf(MSG_WARNING, "WPA: Invalid TKIP key length "
988 "%d (src=" MACSTR ")",
989 keylen, MAC2STR(sm->bssid));
995 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
999 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1000 * for the next 4-Way Handshake. If msg 3 is received again, the old
1001 * SNonce will still be used to avoid changing PTK. */
1002 sm->renew_snonce = 1;
1004 if (key_info & WPA_KEY_INFO_INSTALL) {
1005 wpa_supplicant_install_ptk(sm, key);
1008 if (key_info & WPA_KEY_INFO_SECURE) {
1009 wpa_sm_mlme_setprotection(
1010 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1011 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1012 eapol_sm_notify_portValid(sm->eapol, TRUE);
1014 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1017 wpa_supplicant_pairwise_gtk(sm, key,
1018 ie.gtk, ie.gtk_len, key_info) < 0) {
1019 wpa_printf(MSG_INFO, "RSN: Failed to configure GTK");
1022 if (ieee80211w_set_keys(sm, &ie) < 0)
1023 wpa_printf(MSG_INFO, "RSN: Failed to configure IGTK");
1027 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1031 struct wpa_gtk_data *gd)
1034 struct wpa_eapol_ie_parse ie;
1036 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
1037 wpa_supplicant_parse_ies(keydata, keydatalen, &ie);
1038 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1039 wpa_printf(MSG_WARNING, "WPA: GTK IE in unencrypted key data");
1042 if (ie.gtk == NULL) {
1043 wpa_printf(MSG_INFO, "WPA: No GTK IE in Group Key msg 1/2");
1046 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1048 if (wpa_supplicant_check_group_cipher(sm->group_cipher,
1049 gd->gtk_len, maxkeylen,
1050 &gd->key_rsc_len, &gd->alg))
1053 wpa_hexdump(MSG_DEBUG, "RSN: received GTK in group key handshake",
1054 ie.gtk, ie.gtk_len);
1055 gd->keyidx = ie.gtk[0] & 0x3;
1056 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1057 !!(ie.gtk[0] & BIT(2)));
1058 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1059 wpa_printf(MSG_INFO, "RSN: Too long GTK in GTK IE "
1060 "(len=%lu)", (unsigned long) ie.gtk_len - 2);
1063 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1065 if (ieee80211w_set_keys(sm, &ie) < 0)
1066 wpa_printf(MSG_INFO, "RSN: Failed to configure IGTK");
1072 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1073 const struct wpa_eapol_key *key,
1074 size_t keydatalen, int key_info,
1075 size_t extra_len, u16 ver,
1076 struct wpa_gtk_data *gd)
1081 gd->gtk_len = WPA_GET_BE16(key->key_length);
1082 maxkeylen = keydatalen;
1083 if (keydatalen > extra_len) {
1084 wpa_printf(MSG_INFO, "WPA: Truncated EAPOL-Key packet:"
1085 " key_data_length=%lu > extra_len=%lu",
1086 (unsigned long) keydatalen,
1087 (unsigned long) extra_len);
1090 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1091 if (maxkeylen < 8) {
1092 wpa_printf(MSG_INFO, "WPA: Too short maxkeylen (%lu)",
1093 (unsigned long) maxkeylen);
1099 if (wpa_supplicant_check_group_cipher(sm->group_cipher,
1100 gd->gtk_len, maxkeylen,
1101 &gd->key_rsc_len, &gd->alg))
1104 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1105 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1106 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1107 os_memcpy(ek, key->key_iv, 16);
1108 os_memcpy(ek + 16, sm->ptk.kek, 16);
1109 if (keydatalen > sizeof(gd->gtk)) {
1110 wpa_printf(MSG_WARNING, "WPA: RC4 key data "
1112 (unsigned long) keydatalen);
1115 os_memcpy(gd->gtk, key + 1, keydatalen);
1116 rc4_skip(ek, 32, 256, gd->gtk, keydatalen);
1117 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1118 if (keydatalen % 8) {
1119 wpa_printf(MSG_WARNING, "WPA: Unsupported AES-WRAP "
1120 "len %lu", (unsigned long) keydatalen);
1123 if (maxkeylen > sizeof(gd->gtk)) {
1124 wpa_printf(MSG_WARNING, "WPA: AES-WRAP key data "
1125 "too long (keydatalen=%lu maxkeylen=%lu)",
1126 (unsigned long) keydatalen,
1127 (unsigned long) maxkeylen);
1130 if (aes_unwrap(sm->ptk.kek, maxkeylen / 8,
1131 (const u8 *) (key + 1), gd->gtk)) {
1132 wpa_printf(MSG_WARNING, "WPA: AES unwrap "
1133 "failed - could not decrypt GTK");
1137 wpa_printf(MSG_WARNING, "WPA: Unsupported key_info type %d",
1141 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1142 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1147 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1148 const struct wpa_eapol_key *key,
1149 int ver, u16 key_info)
1152 struct wpa_eapol_key *reply;
1155 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1156 sizeof(*reply), &rlen, (void *) &reply);
1160 reply->type = sm->proto == WPA_PROTO_RSN ?
1161 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1162 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1163 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1164 WPA_PUT_BE16(reply->key_info, key_info);
1165 if (sm->proto == WPA_PROTO_RSN)
1166 WPA_PUT_BE16(reply->key_length, 0);
1168 os_memcpy(reply->key_length, key->key_length, 2);
1169 os_memcpy(reply->replay_counter, key->replay_counter,
1170 WPA_REPLAY_COUNTER_LEN);
1172 WPA_PUT_BE16(reply->key_data_length, 0);
1174 wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1175 wpa_eapol_key_send(sm, sm->ptk.kck, ver, sm->bssid, ETH_P_EAPOL,
1176 rbuf, rlen, reply->key_mic);
1182 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1183 const unsigned char *src_addr,
1184 const struct wpa_eapol_key *key,
1185 int extra_len, u16 ver)
1187 u16 key_info, keydatalen;
1189 struct wpa_gtk_data gd;
1191 os_memset(&gd, 0, sizeof(gd));
1193 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1194 wpa_printf(MSG_DEBUG, "WPA: RX message 1 of Group Key Handshake from "
1195 MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1197 key_info = WPA_GET_BE16(key->key_info);
1198 keydatalen = WPA_GET_BE16(key->key_data_length);
1200 if (sm->proto == WPA_PROTO_RSN) {
1201 ret = wpa_supplicant_process_1_of_2_rsn(sm,
1202 (const u8 *) (key + 1),
1203 keydatalen, key_info,
1206 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, keydatalen,
1207 key_info, extra_len,
1211 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1216 if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc) ||
1217 wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
1221 wpa_msg(sm->ctx->ctx, MSG_INFO, "WPA: Group rekeying "
1222 "completed with " MACSTR " [GTK=%s]",
1223 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1224 wpa_sm_cancel_auth_timeout(sm);
1225 wpa_sm_set_state(sm, WPA_COMPLETED);
1227 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1229 WPA_KEY_INFO_SECURE);
1234 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1235 struct wpa_eapol_key *key,
1237 const u8 *buf, size_t len)
1242 os_memcpy(mic, key->key_mic, 16);
1244 os_memset(key->key_mic, 0, 16);
1245 wpa_eapol_key_mic(sm->tptk.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 "when using TPTK - ignoring TPTK");
1254 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1258 if (!ok && sm->ptk_set) {
1259 os_memset(key->key_mic, 0, 16);
1260 wpa_eapol_key_mic(sm->ptk.kck, ver, buf, len,
1262 if (os_memcmp(mic, key->key_mic, 16) != 0) {
1263 wpa_printf(MSG_WARNING, "WPA: Invalid EAPOL-Key MIC "
1264 "- dropping packet");
1271 wpa_printf(MSG_WARNING, "WPA: Could not verify EAPOL-Key MIC "
1272 "- dropping packet");
1276 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1277 WPA_REPLAY_COUNTER_LEN);
1278 sm->rx_replay_counter_set = 1;
1283 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1284 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
1285 struct wpa_eapol_key *key, u16 ver)
1287 u16 keydatalen = WPA_GET_BE16(key->key_data_length);
1289 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
1290 (u8 *) (key + 1), keydatalen);
1292 wpa_printf(MSG_WARNING, "WPA: PTK not available, "
1293 "cannot decrypt EAPOL-Key key data.");
1297 /* Decrypt key data here so that this operation does not need
1298 * to be implemented separately for each message type. */
1299 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1301 os_memcpy(ek, key->key_iv, 16);
1302 os_memcpy(ek + 16, sm->ptk.kek, 16);
1303 rc4_skip(ek, 32, 256, (u8 *) (key + 1), keydatalen);
1304 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1305 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1307 if (keydatalen % 8) {
1308 wpa_printf(MSG_WARNING, "WPA: Unsupported "
1309 "AES-WRAP len %d", keydatalen);
1312 keydatalen -= 8; /* AES-WRAP adds 8 bytes */
1313 buf = os_malloc(keydatalen);
1315 wpa_printf(MSG_WARNING, "WPA: No memory for "
1316 "AES-UNWRAP buffer");
1319 if (aes_unwrap(sm->ptk.kek, keydatalen / 8,
1320 (u8 *) (key + 1), buf)) {
1322 wpa_printf(MSG_WARNING, "WPA: AES unwrap failed - "
1323 "could not decrypt EAPOL-Key key data");
1326 os_memcpy(key + 1, buf, keydatalen);
1328 WPA_PUT_BE16(key->key_data_length, keydatalen);
1330 wpa_printf(MSG_WARNING, "WPA: Unsupported key_info type %d",
1334 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
1335 (u8 *) (key + 1), keydatalen);
1341 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1342 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1344 void wpa_sm_aborted_cached(struct wpa_sm *sm)
1346 if (sm && sm->cur_pmksa) {
1347 wpa_printf(MSG_DEBUG, "RSN: Cancelling PMKSA caching attempt");
1348 sm->cur_pmksa = NULL;
1353 static void wpa_eapol_key_dump(const struct wpa_eapol_key *key)
1355 #ifndef CONFIG_NO_STDOUT_DEBUG
1356 u16 key_info = WPA_GET_BE16(key->key_info);
1358 wpa_printf(MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1359 wpa_printf(MSG_DEBUG, " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s"
1361 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1362 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1363 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1364 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1365 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1366 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1367 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1368 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1369 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1370 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1371 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1372 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1373 wpa_printf(MSG_DEBUG, " key_length=%u key_data_length=%u",
1374 WPA_GET_BE16(key->key_length),
1375 WPA_GET_BE16(key->key_data_length));
1376 wpa_hexdump(MSG_DEBUG, " replay_counter",
1377 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1378 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1379 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1380 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1381 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
1382 wpa_hexdump(MSG_DEBUG, " key_mic", key->key_mic, 16);
1383 #endif /* CONFIG_NO_STDOUT_DEBUG */
1388 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1389 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1390 * @src_addr: Source MAC address of the EAPOL packet
1391 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1392 * @len: Length of the EAPOL frame
1393 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1395 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1396 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1397 * only processing WPA and WPA2 EAPOL-Key frames.
1399 * The received EAPOL-Key packets are validated and valid packets are replied
1400 * to. In addition, key material (PTK, GTK) is configured at the end of a
1401 * successful key handshake.
1403 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1404 const u8 *buf, size_t len)
1406 size_t plen, data_len, extra_len;
1407 struct ieee802_1x_hdr *hdr;
1408 struct wpa_eapol_key *key;
1412 struct wpa_peerkey *peerkey = NULL;
1414 #ifdef CONFIG_IEEE80211R
1415 sm->ft_completed = 0;
1416 #endif /* CONFIG_IEEE80211R */
1418 if (len < sizeof(*hdr) + sizeof(*key)) {
1419 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame too short to be a WPA "
1420 "EAPOL-Key (len %lu, expecting at least %lu)",
1421 (unsigned long) len,
1422 (unsigned long) sizeof(*hdr) + sizeof(*key));
1426 tmp = os_malloc(len);
1429 os_memcpy(tmp, buf, len);
1431 hdr = (struct ieee802_1x_hdr *) tmp;
1432 key = (struct wpa_eapol_key *) (hdr + 1);
1433 plen = be_to_host16(hdr->length);
1434 data_len = plen + sizeof(*hdr);
1435 wpa_printf(MSG_DEBUG, "IEEE 802.1X RX: version=%d type=%d length=%lu",
1436 hdr->version, hdr->type, (unsigned long) plen);
1438 if (hdr->version < EAPOL_VERSION) {
1439 /* TODO: backwards compatibility */
1441 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1442 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame (type %u) discarded, "
1443 "not a Key frame", hdr->type);
1447 if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
1448 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame payload size %lu "
1449 "invalid (frame size %lu)",
1450 (unsigned long) plen, (unsigned long) len);
1455 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1457 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key type (%d) unknown, "
1458 "discarded", key->type);
1462 wpa_eapol_key_dump(key);
1464 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
1465 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", tmp, len);
1466 if (data_len < len) {
1467 wpa_printf(MSG_DEBUG, "WPA: ignoring %lu bytes after the IEEE "
1468 "802.1X data", (unsigned long) len - data_len);
1470 key_info = WPA_GET_BE16(key->key_info);
1471 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1472 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1473 #ifdef CONFIG_IEEE80211R
1474 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1475 #endif /* CONFIG_IEEE80211R */
1476 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1477 wpa_printf(MSG_INFO, "WPA: Unsupported EAPOL-Key descriptor "
1478 "version %d.", ver);
1482 #ifdef CONFIG_IEEE80211R
1483 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1484 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1485 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1486 wpa_printf(MSG_INFO, "FT: AP did not use "
1491 #endif /* CONFIG_IEEE80211R */
1492 #ifdef CONFIG_IEEE80211W
1493 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
1494 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1495 wpa_printf(MSG_INFO, "WPA: AP did not use the "
1496 "negotiated AES-128-CMAC.");
1500 #endif /* CONFIG_IEEE80211W */
1501 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
1502 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1503 wpa_printf(MSG_INFO, "WPA: CCMP is used, but EAPOL-Key "
1504 "descriptor version (%d) is not 2.", ver);
1505 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1506 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1507 /* Earlier versions of IEEE 802.11i did not explicitly
1508 * require version 2 descriptor for all EAPOL-Key
1509 * packets, so allow group keys to use version 1 if
1510 * CCMP is not used for them. */
1511 wpa_printf(MSG_INFO, "WPA: Backwards compatibility: "
1512 "allow invalid version for non-CCMP group "
1518 #ifdef CONFIG_PEERKEY
1519 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1520 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1524 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1525 if (!peerkey->initiator && peerkey->replay_counter_set &&
1526 os_memcmp(key->replay_counter, peerkey->replay_counter,
1527 WPA_REPLAY_COUNTER_LEN) <= 0) {
1528 wpa_printf(MSG_WARNING, "RSN: EAPOL-Key Replay "
1529 "Counter did not increase (STK) - dropping "
1532 } else if (peerkey->initiator) {
1533 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1534 os_memcpy(_tmp, key->replay_counter,
1535 WPA_REPLAY_COUNTER_LEN);
1536 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
1537 if (os_memcmp(_tmp, peerkey->replay_counter,
1538 WPA_REPLAY_COUNTER_LEN) != 0) {
1539 wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key Replay "
1540 "Counter did not match (STK) - "
1547 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
1548 wpa_printf(MSG_INFO, "RSN: Ack bit in key_info from STK peer");
1551 #endif /* CONFIG_PEERKEY */
1553 if (!peerkey && sm->rx_replay_counter_set &&
1554 os_memcmp(key->replay_counter, sm->rx_replay_counter,
1555 WPA_REPLAY_COUNTER_LEN) <= 0) {
1556 wpa_printf(MSG_WARNING, "WPA: EAPOL-Key Replay Counter did not"
1557 " increase - dropping packet");
1561 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
1562 #ifdef CONFIG_PEERKEY
1563 && (peerkey == NULL || !peerkey->initiator)
1564 #endif /* CONFIG_PEERKEY */
1566 wpa_printf(MSG_INFO, "WPA: No Ack bit in key_info");
1570 if (key_info & WPA_KEY_INFO_REQUEST) {
1571 wpa_printf(MSG_INFO, "WPA: EAPOL-Key with Request bit - "
1576 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
1577 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
1580 #ifdef CONFIG_PEERKEY
1581 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
1582 peerkey_verify_eapol_key_mic(sm, peerkey, key, ver, tmp, data_len))
1584 #endif /* CONFIG_PEERKEY */
1586 extra_len = data_len - sizeof(*hdr) - sizeof(*key);
1588 if (WPA_GET_BE16(key->key_data_length) > extra_len) {
1589 wpa_msg(sm->ctx->ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1590 "frame - key_data overflow (%d > %lu)",
1591 WPA_GET_BE16(key->key_data_length),
1592 (unsigned long) extra_len);
1595 extra_len = WPA_GET_BE16(key->key_data_length);
1597 if (sm->proto == WPA_PROTO_RSN &&
1598 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1599 if (wpa_supplicant_decrypt_key_data(sm, key, ver))
1601 extra_len = WPA_GET_BE16(key->key_data_length);
1604 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1605 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
1606 wpa_printf(MSG_WARNING, "WPA: Ignored EAPOL-Key "
1607 "(Pairwise) with non-zero key index");
1611 /* PeerKey 4-Way Handshake */
1612 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver);
1613 } else if (key_info & WPA_KEY_INFO_MIC) {
1614 /* 3/4 4-Way Handshake */
1615 wpa_supplicant_process_3_of_4(sm, key, ver);
1617 /* 1/4 4-Way Handshake */
1618 wpa_supplicant_process_1_of_4(sm, src_addr, key,
1621 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1622 /* PeerKey SMK Handshake */
1623 peerkey_rx_eapol_smk(sm, src_addr, key, extra_len, key_info,
1626 if (key_info & WPA_KEY_INFO_MIC) {
1627 /* 1/2 Group Key Handshake */
1628 wpa_supplicant_process_1_of_2(sm, src_addr, key,
1631 wpa_printf(MSG_WARNING, "WPA: EAPOL-Key (Group) "
1632 "without Mic bit - dropped");
1644 #ifdef CONFIG_CTRL_IFACE
1645 static int wpa_cipher_bits(int cipher)
1648 case WPA_CIPHER_CCMP:
1650 case WPA_CIPHER_TKIP:
1652 case WPA_CIPHER_WEP104:
1654 case WPA_CIPHER_WEP40:
1662 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
1664 switch (sm->key_mgmt) {
1665 case WPA_KEY_MGMT_IEEE8021X:
1666 return (sm->proto == WPA_PROTO_RSN ?
1667 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
1668 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
1669 case WPA_KEY_MGMT_PSK:
1670 return (sm->proto == WPA_PROTO_RSN ?
1671 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
1672 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
1673 #ifdef CONFIG_IEEE80211R
1674 case WPA_KEY_MGMT_FT_IEEE8021X:
1675 return RSN_AUTH_KEY_MGMT_FT_802_1X;
1676 case WPA_KEY_MGMT_FT_PSK:
1677 return RSN_AUTH_KEY_MGMT_FT_PSK;
1678 #endif /* CONFIG_IEEE80211R */
1679 #ifdef CONFIG_IEEE80211W
1680 case WPA_KEY_MGMT_IEEE8021X_SHA256:
1681 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
1682 case WPA_KEY_MGMT_PSK_SHA256:
1683 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
1684 #endif /* CONFIG_IEEE80211W */
1685 case WPA_KEY_MGMT_WPA_NONE:
1686 return WPA_AUTH_KEY_MGMT_NONE;
1693 static u32 wpa_cipher_suite(struct wpa_sm *sm, int cipher)
1696 case WPA_CIPHER_CCMP:
1697 return (sm->proto == WPA_PROTO_RSN ?
1698 RSN_CIPHER_SUITE_CCMP : WPA_CIPHER_SUITE_CCMP);
1699 case WPA_CIPHER_TKIP:
1700 return (sm->proto == WPA_PROTO_RSN ?
1701 RSN_CIPHER_SUITE_TKIP : WPA_CIPHER_SUITE_TKIP);
1702 case WPA_CIPHER_WEP104:
1703 return (sm->proto == WPA_PROTO_RSN ?
1704 RSN_CIPHER_SUITE_WEP104 : WPA_CIPHER_SUITE_WEP104);
1705 case WPA_CIPHER_WEP40:
1706 return (sm->proto == WPA_PROTO_RSN ?
1707 RSN_CIPHER_SUITE_WEP40 : WPA_CIPHER_SUITE_WEP40);
1708 case WPA_CIPHER_NONE:
1709 return (sm->proto == WPA_PROTO_RSN ?
1710 RSN_CIPHER_SUITE_NONE : WPA_CIPHER_SUITE_NONE);
1717 #define RSN_SUITE "%02x-%02x-%02x-%d"
1718 #define RSN_SUITE_ARG(s) \
1719 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
1722 * wpa_sm_get_mib - Dump text list of MIB entries
1723 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1724 * @buf: Buffer for the list
1725 * @buflen: Length of the buffer
1726 * Returns: Number of bytes written to buffer
1728 * This function is used fetch dot11 MIB variables.
1730 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
1732 char pmkid_txt[PMKID_LEN * 2 + 1];
1736 if (sm->cur_pmksa) {
1737 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
1738 sm->cur_pmksa->pmkid, PMKID_LEN);
1740 pmkid_txt[0] = '\0';
1742 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
1743 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
1744 sm->proto == WPA_PROTO_RSN)
1749 ret = os_snprintf(buf, buflen,
1750 "dot11RSNAOptionImplemented=TRUE\n"
1751 "dot11RSNAPreauthenticationImplemented=TRUE\n"
1752 "dot11RSNAEnabled=%s\n"
1753 "dot11RSNAPreauthenticationEnabled=%s\n"
1754 "dot11RSNAConfigVersion=%d\n"
1755 "dot11RSNAConfigPairwiseKeysSupported=5\n"
1756 "dot11RSNAConfigGroupCipherSize=%d\n"
1757 "dot11RSNAConfigPMKLifetime=%d\n"
1758 "dot11RSNAConfigPMKReauthThreshold=%d\n"
1759 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
1760 "dot11RSNAConfigSATimeout=%d\n",
1761 rsna ? "TRUE" : "FALSE",
1762 rsna ? "TRUE" : "FALSE",
1764 wpa_cipher_bits(sm->group_cipher),
1765 sm->dot11RSNAConfigPMKLifetime,
1766 sm->dot11RSNAConfigPMKReauthThreshold,
1767 sm->dot11RSNAConfigSATimeout);
1768 if (ret < 0 || (size_t) ret >= buflen)
1773 buf + len, buflen - len,
1774 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
1775 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
1776 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
1777 "dot11RSNAPMKIDUsed=%s\n"
1778 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
1779 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
1780 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
1781 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
1782 "dot11RSNA4WayHandshakeFailures=%u\n",
1783 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1784 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->pairwise_cipher)),
1785 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->group_cipher)),
1787 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1788 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->pairwise_cipher)),
1789 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->group_cipher)),
1790 sm->dot11RSNA4WayHandshakeFailures);
1791 if (ret >= 0 && (size_t) ret < buflen)
1796 #endif /* CONFIG_CTRL_IFACE */
1799 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
1800 void *ctx, int replace)
1802 struct wpa_sm *sm = ctx;
1804 if (sm->cur_pmksa == entry ||
1805 (sm->pmk_len == entry->pmk_len &&
1806 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
1807 wpa_printf(MSG_DEBUG, "RSN: removed current PMKSA entry");
1808 sm->cur_pmksa = NULL;
1811 /* A new entry is being added, so no need to
1812 * deauthenticate in this case. This happens when EAP
1813 * authentication is completed again (reauth or failed
1814 * PMKSA caching attempt). */
1818 os_memset(sm->pmk, 0, sizeof(sm->pmk));
1819 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1825 * wpa_sm_init - Initialize WPA state machine
1826 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
1827 * Returns: Pointer to the allocated WPA state machine data
1829 * This function is used to allocate a new WPA state machine and the returned
1830 * value is passed to all WPA state machine calls.
1832 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
1836 sm = os_zalloc(sizeof(*sm));
1839 sm->renew_snonce = 1;
1842 sm->dot11RSNAConfigPMKLifetime = 43200;
1843 sm->dot11RSNAConfigPMKReauthThreshold = 70;
1844 sm->dot11RSNAConfigSATimeout = 60;
1846 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
1847 if (sm->pmksa == NULL) {
1848 wpa_printf(MSG_ERROR, "RSN: PMKSA cache initialization "
1859 * wpa_sm_deinit - Deinitialize WPA state machine
1860 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1862 void wpa_sm_deinit(struct wpa_sm *sm)
1866 pmksa_cache_deinit(sm->pmksa);
1867 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
1868 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
1869 os_free(sm->assoc_wpa_ie);
1870 os_free(sm->ap_wpa_ie);
1871 os_free(sm->ap_rsn_ie);
1879 * wpa_sm_notify_assoc - Notify WPA state machine about association
1880 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1881 * @bssid: The BSSID of the new association
1883 * This function is called to let WPA state machine know that the connection
1886 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
1893 wpa_printf(MSG_DEBUG, "WPA: Association event - clear replay counter");
1894 os_memcpy(sm->bssid, bssid, ETH_ALEN);
1895 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
1896 sm->rx_replay_counter_set = 0;
1897 sm->renew_snonce = 1;
1898 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
1899 rsn_preauth_deinit(sm);
1901 #ifdef CONFIG_IEEE80211R
1902 if (wpa_ft_is_completed(sm)) {
1903 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
1905 /* Prepare for the next transition */
1906 wpa_ft_prepare_auth_request(sm);
1910 #endif /* CONFIG_IEEE80211R */
1914 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
1915 * this is not part of a Fast BSS Transition.
1917 wpa_printf(MSG_DEBUG, "WPA: Clear old PTK");
1925 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
1926 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1928 * This function is called to let WPA state machine know that the connection
1929 * was lost. This will abort any existing pre-authentication session.
1931 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
1933 rsn_preauth_deinit(sm);
1934 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
1935 sm->dot11RSNA4WayHandshakeFailures++;
1940 * wpa_sm_set_pmk - Set PMK
1941 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1943 * @pmk_len: The length of the new PMK in bytes
1945 * Configure the PMK for WPA state machine.
1947 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len)
1952 sm->pmk_len = pmk_len;
1953 os_memcpy(sm->pmk, pmk, pmk_len);
1955 #ifdef CONFIG_IEEE80211R
1956 /* Set XXKey to be PSK for FT key derivation */
1957 sm->xxkey_len = pmk_len;
1958 os_memcpy(sm->xxkey, pmk, pmk_len);
1959 #endif /* CONFIG_IEEE80211R */
1964 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
1965 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1967 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
1970 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
1975 if (sm->cur_pmksa) {
1976 sm->pmk_len = sm->cur_pmksa->pmk_len;
1977 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
1979 sm->pmk_len = PMK_LEN;
1980 os_memset(sm->pmk, 0, PMK_LEN);
1986 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
1987 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1988 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
1990 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
1993 sm->fast_reauth = fast_reauth;
1998 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
1999 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2000 * @scard_ctx: Context pointer for smartcard related callback functions
2002 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2006 sm->scard_ctx = scard_ctx;
2007 if (sm->preauth_eapol)
2008 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2013 * wpa_sm_set_config - Notification of current configration change
2014 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2015 * @config: Pointer to current network configuration
2017 * Notify WPA state machine that configuration has changed. config will be
2018 * stored as a backpointer to network configuration. This can be %NULL to clear
2019 * the stored pointed.
2021 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2027 sm->network_ctx = config->network_ctx;
2028 sm->peerkey_enabled = config->peerkey_enabled;
2029 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2030 sm->proactive_key_caching = config->proactive_key_caching;
2031 sm->eap_workaround = config->eap_workaround;
2032 sm->eap_conf_ctx = config->eap_conf_ctx;
2034 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2035 sm->ssid_len = config->ssid_len;
2038 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
2040 sm->network_ctx = NULL;
2041 sm->peerkey_enabled = 0;
2042 sm->allowed_pairwise_cipher = 0;
2043 sm->proactive_key_caching = 0;
2044 sm->eap_workaround = 0;
2045 sm->eap_conf_ctx = NULL;
2047 sm->wpa_ptk_rekey = 0;
2049 if (config == NULL || config->network_ctx != sm->network_ctx)
2050 pmksa_cache_notify_reconfig(sm->pmksa);
2055 * wpa_sm_set_own_addr - Set own MAC address
2056 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2057 * @addr: Own MAC address
2059 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2062 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2067 * wpa_sm_set_ifname - Set network interface name
2068 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2069 * @ifname: Interface name
2070 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2072 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2073 const char *bridge_ifname)
2076 sm->ifname = ifname;
2077 sm->bridge_ifname = bridge_ifname;
2083 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2084 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2085 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2087 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2095 * wpa_sm_set_param - Set WPA state machine parameters
2096 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2097 * @param: Parameter field
2098 * @value: Parameter value
2099 * Returns: 0 on success, -1 on failure
2101 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2110 case RSNA_PMK_LIFETIME:
2112 sm->dot11RSNAConfigPMKLifetime = value;
2116 case RSNA_PMK_REAUTH_THRESHOLD:
2117 if (value > 0 && value <= 100)
2118 sm->dot11RSNAConfigPMKReauthThreshold = value;
2122 case RSNA_SA_TIMEOUT:
2124 sm->dot11RSNAConfigSATimeout = value;
2128 case WPA_PARAM_PROTO:
2131 case WPA_PARAM_PAIRWISE:
2132 sm->pairwise_cipher = value;
2134 case WPA_PARAM_GROUP:
2135 sm->group_cipher = value;
2137 case WPA_PARAM_KEY_MGMT:
2138 sm->key_mgmt = value;
2140 #ifdef CONFIG_IEEE80211W
2141 case WPA_PARAM_MGMT_GROUP:
2142 sm->mgmt_group_cipher = value;
2144 #endif /* CONFIG_IEEE80211W */
2145 case WPA_PARAM_RSN_ENABLED:
2146 sm->rsn_enabled = value;
2157 * wpa_sm_get_param - Get WPA state machine parameters
2158 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2159 * @param: Parameter field
2160 * Returns: Parameter value
2162 unsigned int wpa_sm_get_param(struct wpa_sm *sm, enum wpa_sm_conf_params param)
2168 case RSNA_PMK_LIFETIME:
2169 return sm->dot11RSNAConfigPMKLifetime;
2170 case RSNA_PMK_REAUTH_THRESHOLD:
2171 return sm->dot11RSNAConfigPMKReauthThreshold;
2172 case RSNA_SA_TIMEOUT:
2173 return sm->dot11RSNAConfigSATimeout;
2174 case WPA_PARAM_PROTO:
2176 case WPA_PARAM_PAIRWISE:
2177 return sm->pairwise_cipher;
2178 case WPA_PARAM_GROUP:
2179 return sm->group_cipher;
2180 case WPA_PARAM_KEY_MGMT:
2181 return sm->key_mgmt;
2182 #ifdef CONFIG_IEEE80211W
2183 case WPA_PARAM_MGMT_GROUP:
2184 return sm->mgmt_group_cipher;
2185 #endif /* CONFIG_IEEE80211W */
2186 case WPA_PARAM_RSN_ENABLED:
2187 return sm->rsn_enabled;
2195 * wpa_sm_get_status - Get WPA state machine
2196 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2197 * @buf: Buffer for status information
2198 * @buflen: Maximum buffer length
2199 * @verbose: Whether to include verbose status information
2200 * Returns: Number of bytes written to buf.
2202 * Query WPA state machine for status information. This function fills in
2203 * a text area with current status information. If the buffer (buf) is not
2204 * large enough, status information will be truncated to fit the buffer.
2206 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2209 char *pos = buf, *end = buf + buflen;
2212 ret = os_snprintf(pos, end - pos,
2213 "pairwise_cipher=%s\n"
2216 wpa_cipher_txt(sm->pairwise_cipher),
2217 wpa_cipher_txt(sm->group_cipher),
2218 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
2219 if (ret < 0 || ret >= end - pos)
2227 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2228 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2229 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2230 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2231 * Returns: 0 on success, -1 on failure
2233 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2241 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2246 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2247 wpa_ie, *wpa_ie_len);
2249 if (sm->assoc_wpa_ie == NULL) {
2251 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2252 * the correct version of the IE even if PMKSA caching is
2253 * aborted (which would remove PMKID from IE generation).
2255 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2256 if (sm->assoc_wpa_ie == NULL)
2259 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2260 sm->assoc_wpa_ie_len = *wpa_ie_len;
2268 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2269 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2270 * @ie: Pointer to IE data (starting from id)
2272 * Returns: 0 on success, -1 on failure
2274 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2275 * Request frame. The IE will be used to override the default value generated
2276 * with wpa_sm_set_assoc_wpa_ie_default().
2278 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2283 os_free(sm->assoc_wpa_ie);
2284 if (ie == NULL || len == 0) {
2285 wpa_printf(MSG_DEBUG, "WPA: clearing own WPA/RSN IE");
2286 sm->assoc_wpa_ie = NULL;
2287 sm->assoc_wpa_ie_len = 0;
2289 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2290 sm->assoc_wpa_ie = os_malloc(len);
2291 if (sm->assoc_wpa_ie == NULL)
2294 os_memcpy(sm->assoc_wpa_ie, ie, len);
2295 sm->assoc_wpa_ie_len = len;
2303 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2304 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2305 * @ie: Pointer to IE data (starting from id)
2307 * Returns: 0 on success, -1 on failure
2309 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2312 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2317 os_free(sm->ap_wpa_ie);
2318 if (ie == NULL || len == 0) {
2319 wpa_printf(MSG_DEBUG, "WPA: clearing AP WPA IE");
2320 sm->ap_wpa_ie = NULL;
2321 sm->ap_wpa_ie_len = 0;
2323 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2324 sm->ap_wpa_ie = os_malloc(len);
2325 if (sm->ap_wpa_ie == NULL)
2328 os_memcpy(sm->ap_wpa_ie, ie, len);
2329 sm->ap_wpa_ie_len = len;
2337 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2338 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2339 * @ie: Pointer to IE data (starting from id)
2341 * Returns: 0 on success, -1 on failure
2343 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2346 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2351 os_free(sm->ap_rsn_ie);
2352 if (ie == NULL || len == 0) {
2353 wpa_printf(MSG_DEBUG, "WPA: clearing AP RSN IE");
2354 sm->ap_rsn_ie = NULL;
2355 sm->ap_rsn_ie_len = 0;
2357 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2358 sm->ap_rsn_ie = os_malloc(len);
2359 if (sm->ap_rsn_ie == NULL)
2362 os_memcpy(sm->ap_rsn_ie, ie, len);
2363 sm->ap_rsn_ie_len = len;
2371 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2372 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2373 * @data: Pointer to data area for parsing results
2374 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2376 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2377 * parsed data into data.
2379 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2381 if (sm == NULL || sm->assoc_wpa_ie == NULL) {
2382 wpa_printf(MSG_DEBUG, "WPA: No WPA/RSN IE available from "
2383 "association info");
2386 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))