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
136 * Returns: Pointer to the current network structure or %NULL on failure
138 * Send an EAPOL-Key Request to the current authenticator. This function is
139 * used to request rekeying and it is usually called when a local Michael MIC
140 * failure is detected.
142 void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
145 struct wpa_eapol_key *reply;
147 u8 bssid[ETH_ALEN], *rbuf;
149 if (wpa_key_mgmt_ft(sm->key_mgmt) || wpa_key_mgmt_sha256(sm->key_mgmt))
150 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
151 else if (sm->pairwise_cipher == WPA_CIPHER_CCMP)
152 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
154 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
156 if (wpa_sm_get_bssid(sm, bssid) < 0) {
157 wpa_printf(MSG_WARNING, "Failed to read BSSID for EAPOL-Key "
162 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
163 sizeof(*reply), &rlen, (void *) &reply);
167 reply->type = sm->proto == WPA_PROTO_RSN ?
168 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
169 key_info = WPA_KEY_INFO_REQUEST | ver;
171 key_info |= WPA_KEY_INFO_MIC;
173 key_info |= WPA_KEY_INFO_ERROR;
175 key_info |= WPA_KEY_INFO_KEY_TYPE;
176 WPA_PUT_BE16(reply->key_info, key_info);
177 WPA_PUT_BE16(reply->key_length, 0);
178 os_memcpy(reply->replay_counter, sm->request_counter,
179 WPA_REPLAY_COUNTER_LEN);
180 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
182 WPA_PUT_BE16(reply->key_data_length, 0);
184 wpa_printf(MSG_INFO, "WPA: Sending EAPOL-Key Request (error=%d "
185 "pairwise=%d ptk_set=%d len=%lu)",
186 error, pairwise, sm->ptk_set, (unsigned long) rlen);
187 wpa_eapol_key_send(sm, sm->ptk.kck, ver, bssid, ETH_P_EAPOL,
188 rbuf, rlen, key_info & WPA_KEY_INFO_MIC ?
189 reply->key_mic : NULL);
193 static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
194 const unsigned char *src_addr,
197 int abort_cached = 0;
199 if (pmkid && !sm->cur_pmksa) {
200 /* When using drivers that generate RSN IE, wpa_supplicant may
201 * not have enough time to get the association information
202 * event before receiving this 1/4 message, so try to find a
203 * matching PMKSA cache entry here. */
204 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid);
206 wpa_printf(MSG_DEBUG, "RSN: found matching PMKID from "
209 wpa_printf(MSG_DEBUG, "RSN: no matching PMKID found");
214 if (pmkid && sm->cur_pmksa &&
215 os_memcmp(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
216 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
217 wpa_sm_set_pmk_from_pmksa(sm);
218 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
219 sm->pmk, sm->pmk_len);
220 eapol_sm_notify_cached(sm->eapol);
221 #ifdef CONFIG_IEEE80211R
223 #endif /* CONFIG_IEEE80211R */
224 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
227 res = eapol_sm_get_key(sm->eapol, sm->pmk, PMK_LEN);
230 * EAP-LEAP is an exception from other EAP methods: it
231 * uses only 16-byte PMK.
233 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
236 #ifdef CONFIG_IEEE80211R
238 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
240 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
241 sm->xxkey_len = PMK_LEN;
242 os_memset(buf, 0, sizeof(buf));
244 #endif /* CONFIG_IEEE80211R */
247 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
248 "machines", sm->pmk, pmk_len);
249 sm->pmk_len = pmk_len;
250 pmksa_cache_add(sm->pmksa, sm->pmk, pmk_len, src_addr,
251 sm->own_addr, sm->network_ctx,
253 if (!sm->cur_pmksa && pmkid &&
254 pmksa_cache_get(sm->pmksa, src_addr, pmkid)) {
255 wpa_printf(MSG_DEBUG, "RSN: the new PMK "
256 "matches with the PMKID");
260 wpa_msg(sm->ctx->ctx, MSG_WARNING,
261 "WPA: Failed to get master session key from "
262 "EAPOL state machines");
263 wpa_msg(sm->ctx->ctx, MSG_WARNING,
264 "WPA: Key handshake aborted");
266 wpa_printf(MSG_DEBUG, "RSN: Cancelled PMKSA "
268 sm->cur_pmksa = NULL;
276 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) {
277 /* Send EAPOL-Start to trigger full EAP authentication. */
281 wpa_printf(MSG_DEBUG, "RSN: no PMKSA entry found - trigger "
282 "full EAP authentication");
283 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
284 NULL, 0, &buflen, NULL);
286 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
299 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
300 * @sm: Pointer to WPA state machine data from wpa_sm_init()
301 * @dst: Destination address for the frame
302 * @key: Pointer to the EAPOL-Key frame header
303 * @ver: Version bits from EAPOL-Key Key Info
304 * @nonce: Nonce value for the EAPOL-Key frame
305 * @wpa_ie: WPA/RSN IE
306 * @wpa_ie_len: Length of the WPA/RSN IE
307 * @ptk: PTK to use for keyed hash and encryption
308 * Returns: 0 on success, -1 on failure
310 int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
311 const struct wpa_eapol_key *key,
312 int ver, const u8 *nonce,
313 const u8 *wpa_ie, size_t wpa_ie_len,
317 struct wpa_eapol_key *reply;
320 if (wpa_ie == NULL) {
321 wpa_printf(MSG_WARNING, "WPA: No wpa_ie set - cannot "
326 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
328 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
329 NULL, sizeof(*reply) + wpa_ie_len,
330 &rlen, (void *) &reply);
334 reply->type = sm->proto == WPA_PROTO_RSN ?
335 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
336 WPA_PUT_BE16(reply->key_info,
337 ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
338 if (sm->proto == WPA_PROTO_RSN)
339 WPA_PUT_BE16(reply->key_length, 0);
341 os_memcpy(reply->key_length, key->key_length, 2);
342 os_memcpy(reply->replay_counter, key->replay_counter,
343 WPA_REPLAY_COUNTER_LEN);
345 WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
346 os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
348 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
350 wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
351 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
352 rbuf, rlen, reply->key_mic);
358 static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
359 const struct wpa_eapol_key *key,
362 #ifdef CONFIG_IEEE80211R
363 if (wpa_key_mgmt_ft(sm->key_mgmt))
364 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
365 #endif /* CONFIG_IEEE80211R */
367 wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
368 sm->own_addr, sm->bssid, sm->snonce, key->key_nonce,
369 (u8 *) ptk, sizeof(*ptk),
370 wpa_key_mgmt_sha256(sm->key_mgmt));
375 static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
376 const unsigned char *src_addr,
377 const struct wpa_eapol_key *key,
380 struct wpa_eapol_ie_parse ie;
384 if (wpa_sm_get_network_ctx(sm) == NULL) {
385 wpa_printf(MSG_WARNING, "WPA: No SSID info found (msg 1 of "
390 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
391 wpa_printf(MSG_DEBUG, "WPA: RX message 1 of 4-Way Handshake from "
392 MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
394 os_memset(&ie, 0, sizeof(ie));
396 #ifndef CONFIG_NO_WPA2
397 if (sm->proto == WPA_PROTO_RSN) {
398 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
399 const u8 *_buf = (const u8 *) (key + 1);
400 size_t len = WPA_GET_BE16(key->key_data_length);
401 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data", _buf, len);
402 wpa_supplicant_parse_ies(_buf, len, &ie);
404 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
405 "Authenticator", ie.pmkid, PMKID_LEN);
408 #endif /* CONFIG_NO_WPA2 */
410 if (wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid))
413 if (sm->renew_snonce) {
414 if (os_get_random(sm->snonce, WPA_NONCE_LEN)) {
415 wpa_msg(sm->ctx->ctx, MSG_WARNING,
416 "WPA: Failed to get random data for SNonce");
419 sm->renew_snonce = 0;
420 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
421 sm->snonce, WPA_NONCE_LEN);
424 /* Calculate PTK which will be stored as a temporary PTK until it has
425 * been verified when processing message 3/4. */
427 wpa_derive_ptk(sm, src_addr, key, ptk);
428 /* Supplicant: swap tx/rx Mic keys */
429 os_memcpy(buf, ptk->u.auth.tx_mic_key, 8);
430 os_memcpy(ptk->u.auth.tx_mic_key, ptk->u.auth.rx_mic_key, 8);
431 os_memcpy(ptk->u.auth.rx_mic_key, buf, 8);
434 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
435 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len,
439 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
443 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
445 struct wpa_sm *sm = eloop_ctx;
446 rsn_preauth_candidate_process(sm);
450 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
451 const u8 *addr, int secure)
453 wpa_msg(sm->ctx->ctx, MSG_INFO, "WPA: Key negotiation completed with "
454 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
455 wpa_cipher_txt(sm->pairwise_cipher),
456 wpa_cipher_txt(sm->group_cipher));
457 wpa_sm_cancel_auth_timeout(sm);
458 wpa_sm_set_state(sm, WPA_COMPLETED);
461 wpa_sm_mlme_setprotection(
462 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
463 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
464 eapol_sm_notify_portValid(sm->eapol, TRUE);
465 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
466 eapol_sm_notify_eap_success(sm->eapol, TRUE);
468 * Start preauthentication after a short wait to avoid a
469 * possible race condition between the data receive and key
470 * configuration after the 4-Way Handshake. This increases the
471 * likelyhood of the first preauth EAPOL-Start frame getting to
474 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
477 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
478 wpa_printf(MSG_DEBUG, "RSN: Authenticator accepted "
479 "opportunistic PMKSA entry - marking it valid");
480 sm->cur_pmksa->opportunistic = 0;
483 #ifdef CONFIG_IEEE80211R
484 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
485 /* Prepare for the next transition */
486 wpa_ft_prepare_auth_request(sm);
488 #endif /* CONFIG_IEEE80211R */
492 static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
493 const struct wpa_eapol_key *key)
498 u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
500 wpa_printf(MSG_DEBUG, "WPA: Installing PTK to the driver.");
502 switch (sm->pairwise_cipher) {
503 case WPA_CIPHER_CCMP:
508 case WPA_CIPHER_TKIP:
513 case WPA_CIPHER_NONE:
514 wpa_printf(MSG_DEBUG, "WPA: Pairwise Cipher Suite: "
515 "NONE - do not use pairwise keys");
518 wpa_printf(MSG_WARNING, "WPA: Unsupported pairwise cipher %d",
519 sm->pairwise_cipher);
523 if (sm->proto == WPA_PROTO_RSN) {
526 key_rsc = key->key_rsc;
527 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
530 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
531 (u8 *) sm->ptk.tk1, keylen) < 0) {
532 wpa_printf(MSG_WARNING, "WPA: Failed to set PTK to the "
540 static int wpa_supplicant_check_group_cipher(int group_cipher,
541 int keylen, int maxkeylen,
542 int *key_rsc_len, wpa_alg *alg)
546 switch (group_cipher) {
547 case WPA_CIPHER_CCMP:
548 if (keylen != 16 || maxkeylen < 16) {
555 case WPA_CIPHER_TKIP:
556 if (keylen != 32 || maxkeylen < 32) {
563 case WPA_CIPHER_WEP104:
564 if (keylen != 13 || maxkeylen < 13) {
571 case WPA_CIPHER_WEP40:
572 if (keylen != 5 || maxkeylen < 5) {
580 wpa_printf(MSG_WARNING, "WPA: Unsupported Group Cipher %d",
586 wpa_printf(MSG_WARNING, "WPA: Unsupported %s Group Cipher key "
588 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
595 struct wpa_gtk_data {
597 int tx, key_rsc_len, keyidx;
603 static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
604 const struct wpa_gtk_data *gd,
607 const u8 *_gtk = gd->gtk;
610 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
611 wpa_printf(MSG_DEBUG, "WPA: Installing GTK to the driver "
612 "(keyidx=%d tx=%d len=%d).", gd->keyidx, gd->tx,
614 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
615 if (sm->group_cipher == WPA_CIPHER_TKIP) {
616 /* Swap Tx/Rx keys for Michael MIC */
617 os_memcpy(gtk_buf, gd->gtk, 16);
618 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
619 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
622 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
623 if (wpa_sm_set_key(sm, gd->alg,
624 (u8 *) "\xff\xff\xff\xff\xff\xff",
625 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
626 _gtk, gd->gtk_len) < 0) {
627 wpa_printf(MSG_WARNING, "WPA: Failed to set "
628 "GTK to the driver (Group only).");
631 } else if (wpa_sm_set_key(sm, gd->alg,
632 (u8 *) "\xff\xff\xff\xff\xff\xff",
633 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
634 _gtk, gd->gtk_len) < 0) {
635 wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to "
644 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
647 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
648 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
649 * seemed to set this bit (incorrectly, since Tx is only when
650 * doing Group Key only APs) and without this workaround, the
651 * data connection does not work because wpa_supplicant
652 * configured non-zero keyidx to be used for unicast. */
653 wpa_printf(MSG_INFO, "WPA: Tx bit set for GTK, but pairwise "
654 "keys are used - ignore Tx bit");
661 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
662 const struct wpa_eapol_key *key,
663 const u8 *gtk, size_t gtk_len,
666 #ifndef CONFIG_NO_WPA2
667 struct wpa_gtk_data gd;
670 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
672 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
673 * Reserved [bits 0-7]
677 os_memset(&gd, 0, sizeof(gd));
678 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
681 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
684 gd.keyidx = gtk[0] & 0x3;
685 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
686 !!(gtk[0] & BIT(2)));
690 os_memcpy(gd.gtk, gtk, gtk_len);
691 gd.gtk_len = gtk_len;
693 if (wpa_supplicant_check_group_cipher(sm->group_cipher,
695 &gd.key_rsc_len, &gd.alg) ||
696 wpa_supplicant_install_gtk(sm, &gd, key->key_rsc)) {
697 wpa_printf(MSG_DEBUG, "RSN: Failed to install GTK");
701 wpa_supplicant_key_neg_complete(sm, sm->bssid,
702 key_info & WPA_KEY_INFO_SECURE);
704 #else /* CONFIG_NO_WPA2 */
706 #endif /* CONFIG_NO_WPA2 */
710 static int ieee80211w_set_keys(struct wpa_sm *sm,
711 struct wpa_eapol_ie_parse *ie)
713 #ifdef CONFIG_IEEE80211W
714 if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC)
718 const struct wpa_igtk_kde *igtk;
720 if (ie->igtk_len != sizeof(*igtk))
722 igtk = (const struct wpa_igtk_kde *) ie->igtk;
723 keyidx = WPA_GET_LE16(igtk->keyid);
724 wpa_printf(MSG_DEBUG, "WPA: IGTK keyid %d "
725 "pn %02x%02x%02x%02x%02x%02x",
726 keyidx, MAC2STR(igtk->pn));
727 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
728 igtk->igtk, WPA_IGTK_LEN);
730 wpa_printf(MSG_WARNING, "WPA: Invalid IGTK KeyID %d",
734 if (wpa_sm_set_key(sm, WPA_ALG_IGTK,
735 (u8 *) "\xff\xff\xff\xff\xff\xff",
736 keyidx, 0, igtk->pn, sizeof(igtk->pn),
737 igtk->igtk, WPA_IGTK_LEN) < 0) {
738 wpa_printf(MSG_WARNING, "WPA: Failed to configure IGTK"
745 #else /* CONFIG_IEEE80211W */
747 #endif /* CONFIG_IEEE80211W */
751 static void wpa_report_ie_mismatch(struct wpa_sm *sm,
752 const char *reason, const u8 *src_addr,
753 const u8 *wpa_ie, size_t wpa_ie_len,
754 const u8 *rsn_ie, size_t rsn_ie_len)
756 wpa_msg(sm->ctx->ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
757 reason, MAC2STR(src_addr));
760 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
761 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
764 if (!sm->ap_wpa_ie) {
765 wpa_printf(MSG_INFO, "WPA: No WPA IE in "
768 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
773 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
774 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
777 if (!sm->ap_rsn_ie) {
778 wpa_printf(MSG_INFO, "WPA: No RSN IE in "
781 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
785 wpa_sm_disassociate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
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 (wpa_key_mgmt_ft(sm->key_mgmt)) {
840 struct rsn_mdie *mdie;
841 /* TODO: verify that full MDIE matches with the one from scan
842 * results, not only mobility domain */
843 mdie = (struct rsn_mdie *) (ie->mdie + 2);
844 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
845 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
846 MOBILITY_DOMAIN_ID_LEN) != 0) {
847 wpa_printf(MSG_DEBUG, "FT: MDIE in msg 3/4 did not "
848 "match with the current mobility domain");
852 #endif /* CONFIG_IEEE80211R */
859 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
860 * @sm: Pointer to WPA state machine data from wpa_sm_init()
861 * @dst: Destination address for the frame
862 * @key: Pointer to the EAPOL-Key frame header
863 * @ver: Version bits from EAPOL-Key Key Info
864 * @key_info: Key Info
865 * @kde: KDEs to include the EAPOL-Key frame
866 * @kde_len: Length of KDEs
867 * @ptk: PTK to use for keyed hash and encryption
868 * Returns: 0 on success, -1 on failure
870 int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
871 const struct wpa_eapol_key *key,
872 u16 ver, u16 key_info,
873 const u8 *kde, size_t kde_len,
877 struct wpa_eapol_key *reply;
881 wpa_hexdump(MSG_DEBUG, "WPA: KDE for msg 4/4", kde, kde_len);
883 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
884 sizeof(*reply) + kde_len,
885 &rlen, (void *) &reply);
889 reply->type = sm->proto == WPA_PROTO_RSN ?
890 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
891 key_info &= WPA_KEY_INFO_SECURE;
892 key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
893 WPA_PUT_BE16(reply->key_info, key_info);
894 if (sm->proto == WPA_PROTO_RSN)
895 WPA_PUT_BE16(reply->key_length, 0);
897 os_memcpy(reply->key_length, key->key_length, 2);
898 os_memcpy(reply->replay_counter, key->replay_counter,
899 WPA_REPLAY_COUNTER_LEN);
901 WPA_PUT_BE16(reply->key_data_length, kde_len);
903 os_memcpy(reply + 1, kde, kde_len);
905 wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
906 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
907 rbuf, rlen, reply->key_mic);
913 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
914 const struct wpa_eapol_key *key,
917 u16 key_info, keylen, len;
919 struct wpa_eapol_ie_parse ie;
921 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
922 wpa_printf(MSG_DEBUG, "WPA: RX message 3 of 4-Way Handshake from "
923 MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
925 key_info = WPA_GET_BE16(key->key_info);
927 pos = (const u8 *) (key + 1);
928 len = WPA_GET_BE16(key->key_data_length);
929 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", pos, len);
930 wpa_supplicant_parse_ies(pos, len, &ie);
931 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
932 wpa_printf(MSG_WARNING, "WPA: GTK IE in unencrypted key data");
935 #ifdef CONFIG_IEEE80211W
936 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
937 wpa_printf(MSG_WARNING, "WPA: IGTK KDE in unencrypted key "
942 if (ie.igtk && ie.igtk_len != sizeof(struct wpa_igtk_kde)) {
943 wpa_printf(MSG_WARNING, "WPA: Invalid IGTK KDE length %lu",
944 (unsigned long) ie.igtk_len);
947 #endif /* CONFIG_IEEE80211W */
949 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
952 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
953 wpa_printf(MSG_WARNING, "WPA: ANonce from message 1 of 4-Way "
954 "Handshake differs from 3 of 4-Way Handshake - drop"
955 " packet (src=" MACSTR ")", MAC2STR(sm->bssid));
959 keylen = WPA_GET_BE16(key->key_length);
960 switch (sm->pairwise_cipher) {
961 case WPA_CIPHER_CCMP:
963 wpa_printf(MSG_WARNING, "WPA: Invalid CCMP key length "
964 "%d (src=" MACSTR ")",
965 keylen, MAC2STR(sm->bssid));
969 case WPA_CIPHER_TKIP:
971 wpa_printf(MSG_WARNING, "WPA: Invalid TKIP key length "
972 "%d (src=" MACSTR ")",
973 keylen, MAC2STR(sm->bssid));
979 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
983 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
984 * for the next 4-Way Handshake. If msg 3 is received again, the old
985 * SNonce will still be used to avoid changing PTK. */
986 sm->renew_snonce = 1;
988 if (key_info & WPA_KEY_INFO_INSTALL) {
989 wpa_supplicant_install_ptk(sm, key);
992 if (key_info & WPA_KEY_INFO_SECURE) {
993 wpa_sm_mlme_setprotection(
994 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
995 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
996 eapol_sm_notify_portValid(sm->eapol, TRUE);
998 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1001 wpa_supplicant_pairwise_gtk(sm, key,
1002 ie.gtk, ie.gtk_len, key_info) < 0) {
1003 wpa_printf(MSG_INFO, "RSN: Failed to configure GTK");
1006 if (ieee80211w_set_keys(sm, &ie) < 0)
1007 wpa_printf(MSG_INFO, "RSN: Failed to configure IGTK");
1011 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1015 struct wpa_gtk_data *gd)
1018 struct wpa_eapol_ie_parse ie;
1020 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
1021 wpa_supplicant_parse_ies(keydata, keydatalen, &ie);
1022 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1023 wpa_printf(MSG_WARNING, "WPA: GTK IE in unencrypted key data");
1026 if (ie.gtk == NULL) {
1027 wpa_printf(MSG_INFO, "WPA: No GTK IE in Group Key msg 1/2");
1030 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1032 if (wpa_supplicant_check_group_cipher(sm->group_cipher,
1033 gd->gtk_len, maxkeylen,
1034 &gd->key_rsc_len, &gd->alg))
1037 wpa_hexdump(MSG_DEBUG, "RSN: received GTK in group key handshake",
1038 ie.gtk, ie.gtk_len);
1039 gd->keyidx = ie.gtk[0] & 0x3;
1040 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1041 !!(ie.gtk[0] & BIT(2)));
1042 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1043 wpa_printf(MSG_INFO, "RSN: Too long GTK in GTK IE "
1044 "(len=%lu)", (unsigned long) ie.gtk_len - 2);
1047 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1049 if (ieee80211w_set_keys(sm, &ie) < 0)
1050 wpa_printf(MSG_INFO, "RSN: Failed to configure IGTK");
1056 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1057 const struct wpa_eapol_key *key,
1058 size_t keydatalen, int key_info,
1059 size_t extra_len, u16 ver,
1060 struct wpa_gtk_data *gd)
1065 gd->gtk_len = WPA_GET_BE16(key->key_length);
1066 maxkeylen = keydatalen;
1067 if (keydatalen > extra_len) {
1068 wpa_printf(MSG_INFO, "WPA: Truncated EAPOL-Key packet:"
1069 " key_data_length=%lu > extra_len=%lu",
1070 (unsigned long) keydatalen,
1071 (unsigned long) extra_len);
1074 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1075 if (maxkeylen < 8) {
1076 wpa_printf(MSG_INFO, "WPA: Too short maxkeylen (%lu)",
1077 (unsigned long) maxkeylen);
1083 if (wpa_supplicant_check_group_cipher(sm->group_cipher,
1084 gd->gtk_len, maxkeylen,
1085 &gd->key_rsc_len, &gd->alg))
1088 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1089 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1090 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1091 os_memcpy(ek, key->key_iv, 16);
1092 os_memcpy(ek + 16, sm->ptk.kek, 16);
1093 if (keydatalen > sizeof(gd->gtk)) {
1094 wpa_printf(MSG_WARNING, "WPA: RC4 key data "
1096 (unsigned long) keydatalen);
1099 os_memcpy(gd->gtk, key + 1, keydatalen);
1100 rc4_skip(ek, 32, 256, gd->gtk, keydatalen);
1101 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1102 if (keydatalen % 8) {
1103 wpa_printf(MSG_WARNING, "WPA: Unsupported AES-WRAP "
1104 "len %lu", (unsigned long) keydatalen);
1107 if (maxkeylen > sizeof(gd->gtk)) {
1108 wpa_printf(MSG_WARNING, "WPA: AES-WRAP key data "
1109 "too long (keydatalen=%lu maxkeylen=%lu)",
1110 (unsigned long) keydatalen,
1111 (unsigned long) maxkeylen);
1114 if (aes_unwrap(sm->ptk.kek, maxkeylen / 8,
1115 (const u8 *) (key + 1), gd->gtk)) {
1116 wpa_printf(MSG_WARNING, "WPA: AES unwrap "
1117 "failed - could not decrypt GTK");
1121 wpa_printf(MSG_WARNING, "WPA: Unsupported key_info type %d",
1125 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1126 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1131 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1132 const struct wpa_eapol_key *key,
1133 int ver, u16 key_info)
1136 struct wpa_eapol_key *reply;
1139 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1140 sizeof(*reply), &rlen, (void *) &reply);
1144 reply->type = sm->proto == WPA_PROTO_RSN ?
1145 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1146 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1147 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1148 WPA_PUT_BE16(reply->key_info, key_info);
1149 if (sm->proto == WPA_PROTO_RSN)
1150 WPA_PUT_BE16(reply->key_length, 0);
1152 os_memcpy(reply->key_length, key->key_length, 2);
1153 os_memcpy(reply->replay_counter, key->replay_counter,
1154 WPA_REPLAY_COUNTER_LEN);
1156 WPA_PUT_BE16(reply->key_data_length, 0);
1158 wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1159 wpa_eapol_key_send(sm, sm->ptk.kck, ver, sm->bssid, ETH_P_EAPOL,
1160 rbuf, rlen, reply->key_mic);
1166 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1167 const unsigned char *src_addr,
1168 const struct wpa_eapol_key *key,
1169 int extra_len, u16 ver)
1171 u16 key_info, keydatalen;
1173 struct wpa_gtk_data gd;
1175 os_memset(&gd, 0, sizeof(gd));
1177 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1178 wpa_printf(MSG_DEBUG, "WPA: RX message 1 of Group Key Handshake from "
1179 MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1181 key_info = WPA_GET_BE16(key->key_info);
1182 keydatalen = WPA_GET_BE16(key->key_data_length);
1184 if (sm->proto == WPA_PROTO_RSN) {
1185 ret = wpa_supplicant_process_1_of_2_rsn(sm,
1186 (const u8 *) (key + 1),
1187 keydatalen, key_info,
1190 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, keydatalen,
1191 key_info, extra_len,
1195 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1200 if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc) ||
1201 wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
1205 wpa_msg(sm->ctx->ctx, MSG_INFO, "WPA: Group rekeying "
1206 "completed with " MACSTR " [GTK=%s]",
1207 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1208 wpa_sm_cancel_auth_timeout(sm);
1209 wpa_sm_set_state(sm, WPA_COMPLETED);
1211 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1213 WPA_KEY_INFO_SECURE);
1218 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1219 struct wpa_eapol_key *key,
1221 const u8 *buf, size_t len)
1226 os_memcpy(mic, key->key_mic, 16);
1228 os_memset(key->key_mic, 0, 16);
1229 wpa_eapol_key_mic(sm->tptk.kck, ver, buf, len,
1231 if (os_memcmp(mic, key->key_mic, 16) != 0) {
1232 wpa_printf(MSG_WARNING, "WPA: Invalid EAPOL-Key MIC "
1233 "when using TPTK - ignoring TPTK");
1238 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1242 if (!ok && sm->ptk_set) {
1243 os_memset(key->key_mic, 0, 16);
1244 wpa_eapol_key_mic(sm->ptk.kck, ver, buf, len,
1246 if (os_memcmp(mic, key->key_mic, 16) != 0) {
1247 wpa_printf(MSG_WARNING, "WPA: Invalid EAPOL-Key MIC "
1248 "- dropping packet");
1255 wpa_printf(MSG_WARNING, "WPA: Could not verify EAPOL-Key MIC "
1256 "- dropping packet");
1260 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1261 WPA_REPLAY_COUNTER_LEN);
1262 sm->rx_replay_counter_set = 1;
1267 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1268 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
1269 struct wpa_eapol_key *key, u16 ver)
1271 u16 keydatalen = WPA_GET_BE16(key->key_data_length);
1273 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
1274 (u8 *) (key + 1), keydatalen);
1276 wpa_printf(MSG_WARNING, "WPA: PTK not available, "
1277 "cannot decrypt EAPOL-Key key data.");
1281 /* Decrypt key data here so that this operation does not need
1282 * to be implemented separately for each message type. */
1283 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1285 os_memcpy(ek, key->key_iv, 16);
1286 os_memcpy(ek + 16, sm->ptk.kek, 16);
1287 rc4_skip(ek, 32, 256, (u8 *) (key + 1), keydatalen);
1288 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1289 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1291 if (keydatalen % 8) {
1292 wpa_printf(MSG_WARNING, "WPA: Unsupported "
1293 "AES-WRAP len %d", keydatalen);
1296 keydatalen -= 8; /* AES-WRAP adds 8 bytes */
1297 buf = os_malloc(keydatalen);
1299 wpa_printf(MSG_WARNING, "WPA: No memory for "
1300 "AES-UNWRAP buffer");
1303 if (aes_unwrap(sm->ptk.kek, keydatalen / 8,
1304 (u8 *) (key + 1), buf)) {
1306 wpa_printf(MSG_WARNING, "WPA: AES unwrap failed - "
1307 "could not decrypt EAPOL-Key key data");
1310 os_memcpy(key + 1, buf, keydatalen);
1312 WPA_PUT_BE16(key->key_data_length, keydatalen);
1314 wpa_printf(MSG_WARNING, "WPA: Unsupported key_info type %d",
1318 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
1319 (u8 *) (key + 1), keydatalen);
1325 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1326 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1328 void wpa_sm_aborted_cached(struct wpa_sm *sm)
1330 if (sm && sm->cur_pmksa) {
1331 wpa_printf(MSG_DEBUG, "RSN: Cancelling PMKSA caching attempt");
1332 sm->cur_pmksa = NULL;
1337 static void wpa_eapol_key_dump(const struct wpa_eapol_key *key)
1339 #ifndef CONFIG_NO_STDOUT_DEBUG
1340 u16 key_info = WPA_GET_BE16(key->key_info);
1342 wpa_printf(MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1343 wpa_printf(MSG_DEBUG, " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s"
1345 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1346 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1347 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1348 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1349 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1350 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1351 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1352 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1353 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1354 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1355 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1356 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1357 wpa_printf(MSG_DEBUG, " key_length=%u key_data_length=%u",
1358 WPA_GET_BE16(key->key_length),
1359 WPA_GET_BE16(key->key_data_length));
1360 wpa_hexdump(MSG_DEBUG, " replay_counter",
1361 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1362 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1363 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1364 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1365 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
1366 wpa_hexdump(MSG_DEBUG, " key_mic", key->key_mic, 16);
1367 #endif /* CONFIG_NO_STDOUT_DEBUG */
1372 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1373 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1374 * @src_addr: Source MAC address of the EAPOL packet
1375 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1376 * @len: Length of the EAPOL frame
1377 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1379 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1380 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1381 * only processing WPA and WPA2 EAPOL-Key frames.
1383 * The received EAPOL-Key packets are validated and valid packets are replied
1384 * to. In addition, key material (PTK, GTK) is configured at the end of a
1385 * successful key handshake.
1387 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1388 const u8 *buf, size_t len)
1390 size_t plen, data_len, extra_len;
1391 struct ieee802_1x_hdr *hdr;
1392 struct wpa_eapol_key *key;
1396 struct wpa_peerkey *peerkey = NULL;
1398 #ifdef CONFIG_IEEE80211R
1399 sm->ft_completed = 0;
1400 #endif /* CONFIG_IEEE80211R */
1402 if (len < sizeof(*hdr) + sizeof(*key)) {
1403 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame too short to be a WPA "
1404 "EAPOL-Key (len %lu, expecting at least %lu)",
1405 (unsigned long) len,
1406 (unsigned long) sizeof(*hdr) + sizeof(*key));
1410 tmp = os_malloc(len);
1413 os_memcpy(tmp, buf, len);
1415 hdr = (struct ieee802_1x_hdr *) tmp;
1416 key = (struct wpa_eapol_key *) (hdr + 1);
1417 plen = be_to_host16(hdr->length);
1418 data_len = plen + sizeof(*hdr);
1419 wpa_printf(MSG_DEBUG, "IEEE 802.1X RX: version=%d type=%d length=%lu",
1420 hdr->version, hdr->type, (unsigned long) plen);
1422 if (hdr->version < EAPOL_VERSION) {
1423 /* TODO: backwards compatibility */
1425 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1426 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame (type %u) discarded, "
1427 "not a Key frame", hdr->type);
1431 if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
1432 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame payload size %lu "
1433 "invalid (frame size %lu)",
1434 (unsigned long) plen, (unsigned long) len);
1439 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1441 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key type (%d) unknown, "
1442 "discarded", key->type);
1446 wpa_eapol_key_dump(key);
1448 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
1449 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", tmp, len);
1450 if (data_len < len) {
1451 wpa_printf(MSG_DEBUG, "WPA: ignoring %lu bytes after the IEEE "
1452 "802.1X data", (unsigned long) len - data_len);
1454 key_info = WPA_GET_BE16(key->key_info);
1455 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1456 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1457 #ifdef CONFIG_IEEE80211R
1458 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1459 #endif /* CONFIG_IEEE80211R */
1460 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1461 wpa_printf(MSG_INFO, "WPA: Unsupported EAPOL-Key descriptor "
1462 "version %d.", ver);
1466 #ifdef CONFIG_IEEE80211R
1467 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1468 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1469 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1470 wpa_printf(MSG_INFO, "FT: AP did not use "
1475 #endif /* CONFIG_IEEE80211R */
1476 #ifdef CONFIG_IEEE80211W
1477 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
1478 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1479 wpa_printf(MSG_INFO, "WPA: AP did not use the "
1480 "negotiated AES-128-CMAC.");
1484 #endif /* CONFIG_IEEE80211W */
1485 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
1486 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1487 wpa_printf(MSG_INFO, "WPA: CCMP is used, but EAPOL-Key "
1488 "descriptor version (%d) is not 2.", ver);
1489 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1490 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1491 /* Earlier versions of IEEE 802.11i did not explicitly
1492 * require version 2 descriptor for all EAPOL-Key
1493 * packets, so allow group keys to use version 1 if
1494 * CCMP is not used for them. */
1495 wpa_printf(MSG_INFO, "WPA: Backwards compatibility: "
1496 "allow invalid version for non-CCMP group "
1502 #ifdef CONFIG_PEERKEY
1503 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1504 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1508 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1509 if (!peerkey->initiator && peerkey->replay_counter_set &&
1510 os_memcmp(key->replay_counter, peerkey->replay_counter,
1511 WPA_REPLAY_COUNTER_LEN) <= 0) {
1512 wpa_printf(MSG_WARNING, "RSN: EAPOL-Key Replay "
1513 "Counter did not increase (STK) - dropping "
1516 } else if (peerkey->initiator) {
1517 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1518 os_memcpy(_tmp, key->replay_counter,
1519 WPA_REPLAY_COUNTER_LEN);
1520 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
1521 if (os_memcmp(_tmp, peerkey->replay_counter,
1522 WPA_REPLAY_COUNTER_LEN) != 0) {
1523 wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key Replay "
1524 "Counter did not match (STK) - "
1531 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
1532 wpa_printf(MSG_INFO, "RSN: Ack bit in key_info from STK peer");
1535 #endif /* CONFIG_PEERKEY */
1537 if (!peerkey && sm->rx_replay_counter_set &&
1538 os_memcmp(key->replay_counter, sm->rx_replay_counter,
1539 WPA_REPLAY_COUNTER_LEN) <= 0) {
1540 wpa_printf(MSG_WARNING, "WPA: EAPOL-Key Replay Counter did not"
1541 " increase - dropping packet");
1545 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
1546 #ifdef CONFIG_PEERKEY
1547 && (peerkey == NULL || !peerkey->initiator)
1548 #endif /* CONFIG_PEERKEY */
1550 wpa_printf(MSG_INFO, "WPA: No Ack bit in key_info");
1554 if (key_info & WPA_KEY_INFO_REQUEST) {
1555 wpa_printf(MSG_INFO, "WPA: EAPOL-Key with Request bit - "
1560 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
1561 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
1564 #ifdef CONFIG_PEERKEY
1565 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
1566 peerkey_verify_eapol_key_mic(sm, peerkey, key, ver, tmp, data_len))
1568 #endif /* CONFIG_PEERKEY */
1570 extra_len = data_len - sizeof(*hdr) - sizeof(*key);
1572 if (WPA_GET_BE16(key->key_data_length) > extra_len) {
1573 wpa_msg(sm->ctx->ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1574 "frame - key_data overflow (%d > %lu)",
1575 WPA_GET_BE16(key->key_data_length),
1576 (unsigned long) extra_len);
1579 extra_len = WPA_GET_BE16(key->key_data_length);
1581 if (sm->proto == WPA_PROTO_RSN &&
1582 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1583 if (wpa_supplicant_decrypt_key_data(sm, key, ver))
1585 extra_len = WPA_GET_BE16(key->key_data_length);
1588 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1589 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
1590 wpa_printf(MSG_WARNING, "WPA: Ignored EAPOL-Key "
1591 "(Pairwise) with non-zero key index");
1595 /* PeerKey 4-Way Handshake */
1596 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver);
1597 } else if (key_info & WPA_KEY_INFO_MIC) {
1598 /* 3/4 4-Way Handshake */
1599 wpa_supplicant_process_3_of_4(sm, key, ver);
1601 /* 1/4 4-Way Handshake */
1602 wpa_supplicant_process_1_of_4(sm, src_addr, key,
1605 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1606 /* PeerKey SMK Handshake */
1607 peerkey_rx_eapol_smk(sm, src_addr, key, extra_len, key_info,
1610 if (key_info & WPA_KEY_INFO_MIC) {
1611 /* 1/2 Group Key Handshake */
1612 wpa_supplicant_process_1_of_2(sm, src_addr, key,
1615 wpa_printf(MSG_WARNING, "WPA: EAPOL-Key (Group) "
1616 "without Mic bit - dropped");
1628 #ifdef CONFIG_CTRL_IFACE
1629 static int wpa_cipher_bits(int cipher)
1632 case WPA_CIPHER_CCMP:
1634 case WPA_CIPHER_TKIP:
1636 case WPA_CIPHER_WEP104:
1638 case WPA_CIPHER_WEP40:
1646 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
1648 switch (sm->key_mgmt) {
1649 case WPA_KEY_MGMT_IEEE8021X:
1650 return (sm->proto == WPA_PROTO_RSN ?
1651 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
1652 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
1653 case WPA_KEY_MGMT_PSK:
1654 return (sm->proto == WPA_PROTO_RSN ?
1655 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
1656 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
1657 #ifdef CONFIG_IEEE80211R
1658 case WPA_KEY_MGMT_FT_IEEE8021X:
1659 return RSN_AUTH_KEY_MGMT_FT_802_1X;
1660 case WPA_KEY_MGMT_FT_PSK:
1661 return RSN_AUTH_KEY_MGMT_FT_PSK;
1662 #endif /* CONFIG_IEEE80211R */
1663 #ifdef CONFIG_IEEE80211W
1664 case WPA_KEY_MGMT_IEEE8021X_SHA256:
1665 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
1666 case WPA_KEY_MGMT_PSK_SHA256:
1667 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
1668 #endif /* CONFIG_IEEE80211W */
1669 case WPA_KEY_MGMT_WPA_NONE:
1670 return WPA_AUTH_KEY_MGMT_NONE;
1677 static u32 wpa_cipher_suite(struct wpa_sm *sm, int cipher)
1680 case WPA_CIPHER_CCMP:
1681 return (sm->proto == WPA_PROTO_RSN ?
1682 RSN_CIPHER_SUITE_CCMP : WPA_CIPHER_SUITE_CCMP);
1683 case WPA_CIPHER_TKIP:
1684 return (sm->proto == WPA_PROTO_RSN ?
1685 RSN_CIPHER_SUITE_TKIP : WPA_CIPHER_SUITE_TKIP);
1686 case WPA_CIPHER_WEP104:
1687 return (sm->proto == WPA_PROTO_RSN ?
1688 RSN_CIPHER_SUITE_WEP104 : WPA_CIPHER_SUITE_WEP104);
1689 case WPA_CIPHER_WEP40:
1690 return (sm->proto == WPA_PROTO_RSN ?
1691 RSN_CIPHER_SUITE_WEP40 : WPA_CIPHER_SUITE_WEP40);
1692 case WPA_CIPHER_NONE:
1693 return (sm->proto == WPA_PROTO_RSN ?
1694 RSN_CIPHER_SUITE_NONE : WPA_CIPHER_SUITE_NONE);
1701 #define RSN_SUITE "%02x-%02x-%02x-%d"
1702 #define RSN_SUITE_ARG(s) \
1703 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
1706 * wpa_sm_get_mib - Dump text list of MIB entries
1707 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1708 * @buf: Buffer for the list
1709 * @buflen: Length of the buffer
1710 * Returns: Number of bytes written to buffer
1712 * This function is used fetch dot11 MIB variables.
1714 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
1716 char pmkid_txt[PMKID_LEN * 2 + 1];
1720 if (sm->cur_pmksa) {
1721 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
1722 sm->cur_pmksa->pmkid, PMKID_LEN);
1724 pmkid_txt[0] = '\0';
1726 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
1727 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
1728 sm->proto == WPA_PROTO_RSN)
1733 ret = os_snprintf(buf, buflen,
1734 "dot11RSNAOptionImplemented=TRUE\n"
1735 "dot11RSNAPreauthenticationImplemented=TRUE\n"
1736 "dot11RSNAEnabled=%s\n"
1737 "dot11RSNAPreauthenticationEnabled=%s\n"
1738 "dot11RSNAConfigVersion=%d\n"
1739 "dot11RSNAConfigPairwiseKeysSupported=5\n"
1740 "dot11RSNAConfigGroupCipherSize=%d\n"
1741 "dot11RSNAConfigPMKLifetime=%d\n"
1742 "dot11RSNAConfigPMKReauthThreshold=%d\n"
1743 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
1744 "dot11RSNAConfigSATimeout=%d\n",
1745 rsna ? "TRUE" : "FALSE",
1746 rsna ? "TRUE" : "FALSE",
1748 wpa_cipher_bits(sm->group_cipher),
1749 sm->dot11RSNAConfigPMKLifetime,
1750 sm->dot11RSNAConfigPMKReauthThreshold,
1751 sm->dot11RSNAConfigSATimeout);
1752 if (ret < 0 || (size_t) ret >= buflen)
1757 buf + len, buflen - len,
1758 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
1759 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
1760 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
1761 "dot11RSNAPMKIDUsed=%s\n"
1762 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
1763 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
1764 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
1765 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
1766 "dot11RSNA4WayHandshakeFailures=%u\n",
1767 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1768 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->pairwise_cipher)),
1769 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->group_cipher)),
1771 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1772 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->pairwise_cipher)),
1773 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->group_cipher)),
1774 sm->dot11RSNA4WayHandshakeFailures);
1775 if (ret >= 0 && (size_t) ret < buflen)
1780 #endif /* CONFIG_CTRL_IFACE */
1783 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
1784 void *ctx, int replace)
1786 struct wpa_sm *sm = ctx;
1788 if (sm->cur_pmksa == entry ||
1789 (sm->pmk_len == entry->pmk_len &&
1790 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
1791 wpa_printf(MSG_DEBUG, "RSN: removed current PMKSA entry");
1792 sm->cur_pmksa = NULL;
1795 /* A new entry is being added, so no need to
1796 * deauthenticate in this case. This happens when EAP
1797 * authentication is completed again (reauth or failed
1798 * PMKSA caching attempt). */
1802 os_memset(sm->pmk, 0, sizeof(sm->pmk));
1803 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1809 * wpa_sm_init - Initialize WPA state machine
1810 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
1811 * Returns: Pointer to the allocated WPA state machine data
1813 * This function is used to allocate a new WPA state machine and the returned
1814 * value is passed to all WPA state machine calls.
1816 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
1820 sm = os_zalloc(sizeof(*sm));
1823 sm->renew_snonce = 1;
1826 sm->dot11RSNAConfigPMKLifetime = 43200;
1827 sm->dot11RSNAConfigPMKReauthThreshold = 70;
1828 sm->dot11RSNAConfigSATimeout = 60;
1830 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
1831 if (sm->pmksa == NULL) {
1832 wpa_printf(MSG_ERROR, "RSN: PMKSA cache initialization "
1843 * wpa_sm_deinit - Deinitialize WPA state machine
1844 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1846 void wpa_sm_deinit(struct wpa_sm *sm)
1850 pmksa_cache_deinit(sm->pmksa);
1851 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
1852 os_free(sm->assoc_wpa_ie);
1853 os_free(sm->ap_wpa_ie);
1854 os_free(sm->ap_rsn_ie);
1862 * wpa_sm_notify_assoc - Notify WPA state machine about association
1863 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1864 * @bssid: The BSSID of the new association
1866 * This function is called to let WPA state machine know that the connection
1869 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
1876 wpa_printf(MSG_DEBUG, "WPA: Association event - clear replay counter");
1877 os_memcpy(sm->bssid, bssid, ETH_ALEN);
1878 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
1879 sm->rx_replay_counter_set = 0;
1880 sm->renew_snonce = 1;
1881 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
1882 rsn_preauth_deinit(sm);
1884 #ifdef CONFIG_IEEE80211R
1885 if (wpa_ft_is_completed(sm)) {
1886 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
1888 /* Prepare for the next transition */
1889 wpa_ft_prepare_auth_request(sm);
1893 #endif /* CONFIG_IEEE80211R */
1897 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
1898 * this is not part of a Fast BSS Transition.
1900 wpa_printf(MSG_DEBUG, "WPA: Clear old PTK");
1908 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
1909 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1911 * This function is called to let WPA state machine know that the connection
1912 * was lost. This will abort any existing pre-authentication session.
1914 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
1916 rsn_preauth_deinit(sm);
1917 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
1918 sm->dot11RSNA4WayHandshakeFailures++;
1923 * wpa_sm_set_pmk - Set PMK
1924 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1926 * @pmk_len: The length of the new PMK in bytes
1928 * Configure the PMK for WPA state machine.
1930 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len)
1935 sm->pmk_len = pmk_len;
1936 os_memcpy(sm->pmk, pmk, pmk_len);
1938 #ifdef CONFIG_IEEE80211R
1939 /* Set XXKey to be PSK for FT key derivation */
1940 sm->xxkey_len = pmk_len;
1941 os_memcpy(sm->xxkey, pmk, pmk_len);
1942 #endif /* CONFIG_IEEE80211R */
1947 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
1948 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1950 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
1953 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
1958 if (sm->cur_pmksa) {
1959 sm->pmk_len = sm->cur_pmksa->pmk_len;
1960 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
1962 sm->pmk_len = PMK_LEN;
1963 os_memset(sm->pmk, 0, PMK_LEN);
1969 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
1970 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1971 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
1973 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
1976 sm->fast_reauth = fast_reauth;
1981 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
1982 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1983 * @scard_ctx: Context pointer for smartcard related callback functions
1985 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
1989 sm->scard_ctx = scard_ctx;
1990 if (sm->preauth_eapol)
1991 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
1996 * wpa_sm_set_config - Notification of current configration change
1997 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1998 * @config: Pointer to current network configuration
2000 * Notify WPA state machine that configuration has changed. config will be
2001 * stored as a backpointer to network configuration. This can be %NULL to clear
2002 * the stored pointed.
2004 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2010 sm->network_ctx = config->network_ctx;
2011 sm->peerkey_enabled = config->peerkey_enabled;
2012 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2013 sm->proactive_key_caching = config->proactive_key_caching;
2014 sm->eap_workaround = config->eap_workaround;
2015 sm->eap_conf_ctx = config->eap_conf_ctx;
2017 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2018 sm->ssid_len = config->ssid_len;
2022 sm->network_ctx = NULL;
2023 sm->peerkey_enabled = 0;
2024 sm->allowed_pairwise_cipher = 0;
2025 sm->proactive_key_caching = 0;
2026 sm->eap_workaround = 0;
2027 sm->eap_conf_ctx = NULL;
2030 if (config == NULL || config->network_ctx != sm->network_ctx)
2031 pmksa_cache_notify_reconfig(sm->pmksa);
2036 * wpa_sm_set_own_addr - Set own MAC address
2037 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2038 * @addr: Own MAC address
2040 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2043 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2048 * wpa_sm_set_ifname - Set network interface name
2049 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2050 * @ifname: Interface name
2051 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2053 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2054 const char *bridge_ifname)
2057 sm->ifname = ifname;
2058 sm->bridge_ifname = bridge_ifname;
2064 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2065 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2066 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2068 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2076 * wpa_sm_set_param - Set WPA state machine parameters
2077 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2078 * @param: Parameter field
2079 * @value: Parameter value
2080 * Returns: 0 on success, -1 on failure
2082 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2091 case RSNA_PMK_LIFETIME:
2093 sm->dot11RSNAConfigPMKLifetime = value;
2097 case RSNA_PMK_REAUTH_THRESHOLD:
2098 if (value > 0 && value <= 100)
2099 sm->dot11RSNAConfigPMKReauthThreshold = value;
2103 case RSNA_SA_TIMEOUT:
2105 sm->dot11RSNAConfigSATimeout = value;
2109 case WPA_PARAM_PROTO:
2112 case WPA_PARAM_PAIRWISE:
2113 sm->pairwise_cipher = value;
2115 case WPA_PARAM_GROUP:
2116 sm->group_cipher = value;
2118 case WPA_PARAM_KEY_MGMT:
2119 sm->key_mgmt = value;
2121 #ifdef CONFIG_IEEE80211W
2122 case WPA_PARAM_MGMT_GROUP:
2123 sm->mgmt_group_cipher = value;
2125 #endif /* CONFIG_IEEE80211W */
2126 case WPA_PARAM_RSN_ENABLED:
2127 sm->rsn_enabled = value;
2138 * wpa_sm_get_param - Get WPA state machine parameters
2139 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2140 * @param: Parameter field
2141 * Returns: Parameter value
2143 unsigned int wpa_sm_get_param(struct wpa_sm *sm, enum wpa_sm_conf_params param)
2149 case RSNA_PMK_LIFETIME:
2150 return sm->dot11RSNAConfigPMKLifetime;
2151 case RSNA_PMK_REAUTH_THRESHOLD:
2152 return sm->dot11RSNAConfigPMKReauthThreshold;
2153 case RSNA_SA_TIMEOUT:
2154 return sm->dot11RSNAConfigSATimeout;
2155 case WPA_PARAM_PROTO:
2157 case WPA_PARAM_PAIRWISE:
2158 return sm->pairwise_cipher;
2159 case WPA_PARAM_GROUP:
2160 return sm->group_cipher;
2161 case WPA_PARAM_KEY_MGMT:
2162 return sm->key_mgmt;
2163 #ifdef CONFIG_IEEE80211W
2164 case WPA_PARAM_MGMT_GROUP:
2165 return sm->mgmt_group_cipher;
2166 #endif /* CONFIG_IEEE80211W */
2167 case WPA_PARAM_RSN_ENABLED:
2168 return sm->rsn_enabled;
2176 * wpa_sm_get_status - Get WPA state machine
2177 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2178 * @buf: Buffer for status information
2179 * @buflen: Maximum buffer length
2180 * @verbose: Whether to include verbose status information
2181 * Returns: Number of bytes written to buf.
2183 * Query WPA state machine for status information. This function fills in
2184 * a text area with current status information. If the buffer (buf) is not
2185 * large enough, status information will be truncated to fit the buffer.
2187 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2190 char *pos = buf, *end = buf + buflen;
2193 ret = os_snprintf(pos, end - pos,
2194 "pairwise_cipher=%s\n"
2197 wpa_cipher_txt(sm->pairwise_cipher),
2198 wpa_cipher_txt(sm->group_cipher),
2199 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
2200 if (ret < 0 || ret >= end - pos)
2208 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2209 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2210 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2211 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2212 * Returns: 0 on success, -1 on failure
2214 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2222 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2227 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2228 wpa_ie, *wpa_ie_len);
2230 if (sm->assoc_wpa_ie == NULL) {
2232 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2233 * the correct version of the IE even if PMKSA caching is
2234 * aborted (which would remove PMKID from IE generation).
2236 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2237 if (sm->assoc_wpa_ie == NULL)
2240 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2241 sm->assoc_wpa_ie_len = *wpa_ie_len;
2249 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2250 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2251 * @ie: Pointer to IE data (starting from id)
2253 * Returns: 0 on success, -1 on failure
2255 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2256 * Request frame. The IE will be used to override the default value generated
2257 * with wpa_sm_set_assoc_wpa_ie_default().
2259 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2264 os_free(sm->assoc_wpa_ie);
2265 if (ie == NULL || len == 0) {
2266 wpa_printf(MSG_DEBUG, "WPA: clearing own WPA/RSN IE");
2267 sm->assoc_wpa_ie = NULL;
2268 sm->assoc_wpa_ie_len = 0;
2270 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2271 sm->assoc_wpa_ie = os_malloc(len);
2272 if (sm->assoc_wpa_ie == NULL)
2275 os_memcpy(sm->assoc_wpa_ie, ie, len);
2276 sm->assoc_wpa_ie_len = len;
2284 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2285 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2286 * @ie: Pointer to IE data (starting from id)
2288 * Returns: 0 on success, -1 on failure
2290 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2293 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2298 os_free(sm->ap_wpa_ie);
2299 if (ie == NULL || len == 0) {
2300 wpa_printf(MSG_DEBUG, "WPA: clearing AP WPA IE");
2301 sm->ap_wpa_ie = NULL;
2302 sm->ap_wpa_ie_len = 0;
2304 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2305 sm->ap_wpa_ie = os_malloc(len);
2306 if (sm->ap_wpa_ie == NULL)
2309 os_memcpy(sm->ap_wpa_ie, ie, len);
2310 sm->ap_wpa_ie_len = len;
2318 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2319 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2320 * @ie: Pointer to IE data (starting from id)
2322 * Returns: 0 on success, -1 on failure
2324 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2327 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2332 os_free(sm->ap_rsn_ie);
2333 if (ie == NULL || len == 0) {
2334 wpa_printf(MSG_DEBUG, "WPA: clearing AP RSN IE");
2335 sm->ap_rsn_ie = NULL;
2336 sm->ap_rsn_ie_len = 0;
2338 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2339 sm->ap_rsn_ie = os_malloc(len);
2340 if (sm->ap_rsn_ie == NULL)
2343 os_memcpy(sm->ap_rsn_ie, ie, len);
2344 sm->ap_rsn_ie_len = len;
2352 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2353 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2354 * @data: Pointer to data area for parsing results
2355 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2357 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2358 * parsed data into data.
2360 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2362 if (sm == NULL || sm->assoc_wpa_ie == NULL) {
2363 wpa_printf(MSG_DEBUG, "WPA: No WPA/RSN IE available from "
2364 "association info");
2367 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))