2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
3 * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
22 #include "eapol_supp/eapol_supp_sm.h"
24 #include "pmksa_cache.h"
28 #include "ieee802_11_defs.h"
32 * wpa_cipher_txt - Convert cipher suite to a text string
33 * @cipher: Cipher suite (WPA_CIPHER_* enum)
34 * Returns: Pointer to a text string of the cipher suite name
36 static const char * wpa_cipher_txt(int cipher)
41 case WPA_CIPHER_WEP40:
43 case WPA_CIPHER_WEP104:
56 * wpa_key_mgmt_txt - Convert key management suite to a text string
57 * @key_mgmt: Key management suite (WPA_KEY_MGMT_* enum)
58 * @proto: WPA/WPA2 version (WPA_PROTO_*)
59 * Returns: Pointer to a text string of the key management suite name
61 static const char * wpa_key_mgmt_txt(int key_mgmt, int proto)
64 case WPA_KEY_MGMT_IEEE8021X:
65 return proto == WPA_PROTO_RSN ?
66 "WPA2/IEEE 802.1X/EAP" : "WPA/IEEE 802.1X/EAP";
67 case WPA_KEY_MGMT_PSK:
68 return proto == WPA_PROTO_RSN ?
69 "WPA2-PSK" : "WPA-PSK";
70 case WPA_KEY_MGMT_NONE:
72 case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
73 return "IEEE 802.1X (no WPA)";
74 #ifdef CONFIG_IEEE80211R
75 case WPA_KEY_MGMT_FT_IEEE8021X:
77 case WPA_KEY_MGMT_FT_PSK:
79 #endif /* CONFIG_IEEE80211R */
87 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
88 * @sm: Pointer to WPA state machine data from wpa_sm_init()
89 * @kck: Key Confirmation Key (KCK, part of PTK)
90 * @ver: Version field from Key Info
91 * @dest: Destination address for the frame
92 * @proto: Ethertype (usually ETH_P_EAPOL)
93 * @msg: EAPOL-Key message
94 * @msg_len: Length of message
95 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
97 void wpa_eapol_key_send(struct wpa_sm *sm, const u8 *kck,
98 int ver, const u8 *dest, u16 proto,
99 u8 *msg, size_t msg_len, u8 *key_mic)
101 if (os_memcmp(dest, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) == 0 &&
102 os_memcmp(sm->bssid, "\x00\x00\x00\x00\x00\x00", ETH_ALEN) == 0) {
104 * Association event was not yet received; try to fetch
105 * BSSID from the driver.
107 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
108 wpa_printf(MSG_DEBUG, "WPA: Failed to read BSSID for "
109 "EAPOL-Key destination address");
112 wpa_printf(MSG_DEBUG, "WPA: Use BSSID (" MACSTR
113 ") as the destination for EAPOL-Key",
118 wpa_eapol_key_mic(kck, ver, msg, msg_len, key_mic);
119 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
120 wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
121 eapol_sm_notify_tx_eapol_key(sm->eapol);
127 * wpa_sm_key_request - Send EAPOL-Key Request
128 * @sm: Pointer to WPA state machine data from wpa_sm_init()
129 * @error: Indicate whether this is an Michael MIC error report
130 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
131 * Returns: Pointer to the current network structure or %NULL on failure
133 * Send an EAPOL-Key Request to the current authenticator. This function is
134 * used to request rekeying and it is usually called when a local Michael MIC
135 * failure is detected.
137 void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
140 struct wpa_eapol_key *reply;
142 u8 bssid[ETH_ALEN], *rbuf;
144 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X ||
145 sm->key_mgmt == WPA_KEY_MGMT_FT_PSK)
146 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
147 else if (sm->pairwise_cipher == WPA_CIPHER_CCMP)
148 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
150 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
152 if (wpa_sm_get_bssid(sm, bssid) < 0) {
153 wpa_printf(MSG_WARNING, "Failed to read BSSID for EAPOL-Key "
158 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
159 sizeof(*reply), &rlen, (void *) &reply);
163 reply->type = sm->proto == WPA_PROTO_RSN ?
164 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
165 key_info = WPA_KEY_INFO_REQUEST | ver;
167 key_info |= WPA_KEY_INFO_MIC;
169 key_info |= WPA_KEY_INFO_ERROR;
171 key_info |= WPA_KEY_INFO_KEY_TYPE;
172 WPA_PUT_BE16(reply->key_info, key_info);
173 WPA_PUT_BE16(reply->key_length, 0);
174 os_memcpy(reply->replay_counter, sm->request_counter,
175 WPA_REPLAY_COUNTER_LEN);
176 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
178 WPA_PUT_BE16(reply->key_data_length, 0);
180 wpa_printf(MSG_INFO, "WPA: Sending EAPOL-Key Request (error=%d "
181 "pairwise=%d ptk_set=%d len=%lu)",
182 error, pairwise, sm->ptk_set, (unsigned long) rlen);
183 wpa_eapol_key_send(sm, sm->ptk.kck, ver, bssid, ETH_P_EAPOL,
184 rbuf, rlen, key_info & WPA_KEY_INFO_MIC ?
185 reply->key_mic : NULL);
189 static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
190 const unsigned char *src_addr,
193 int abort_cached = 0;
195 if (pmkid && !sm->cur_pmksa) {
196 /* When using drivers that generate RSN IE, wpa_supplicant may
197 * not have enough time to get the association information
198 * event before receiving this 1/4 message, so try to find a
199 * matching PMKSA cache entry here. */
200 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid);
202 wpa_printf(MSG_DEBUG, "RSN: found matching PMKID from "
205 wpa_printf(MSG_DEBUG, "RSN: no matching PMKID found");
210 if (pmkid && sm->cur_pmksa &&
211 os_memcmp(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
212 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
213 wpa_sm_set_pmk_from_pmksa(sm);
214 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
215 sm->pmk, sm->pmk_len);
216 eapol_sm_notify_cached(sm->eapol);
217 #ifdef CONFIG_IEEE80211R
219 #endif /* CONFIG_IEEE80211R */
220 } else if ((sm->key_mgmt == WPA_KEY_MGMT_IEEE8021X ||
221 sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) && sm->eapol) {
224 res = eapol_sm_get_key(sm->eapol, sm->pmk, PMK_LEN);
227 * EAP-LEAP is an exception from other EAP methods: it
228 * uses only 16-byte PMK.
230 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
233 #ifdef CONFIG_IEEE80211R
235 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
237 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
238 sm->xxkey_len = PMK_LEN;
239 os_memset(buf, 0, sizeof(buf));
241 #endif /* CONFIG_IEEE80211R */
244 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
245 "machines", sm->pmk, pmk_len);
246 sm->pmk_len = pmk_len;
247 pmksa_cache_add(sm->pmksa, sm->pmk, pmk_len, src_addr,
248 sm->own_addr, sm->network_ctx);
249 if (!sm->cur_pmksa && pmkid &&
250 pmksa_cache_get(sm->pmksa, src_addr, pmkid)) {
251 wpa_printf(MSG_DEBUG, "RSN: the new PMK "
252 "matches with the PMKID");
256 wpa_msg(sm->ctx->ctx, MSG_WARNING,
257 "WPA: Failed to get master session key from "
258 "EAPOL state machines");
259 wpa_msg(sm->ctx->ctx, MSG_WARNING,
260 "WPA: Key handshake aborted");
262 wpa_printf(MSG_DEBUG, "RSN: Cancelled PMKSA "
264 sm->cur_pmksa = NULL;
272 if (abort_cached && (sm->key_mgmt == WPA_KEY_MGMT_IEEE8021X ||
273 sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X)) {
274 /* Send EAPOL-Start to trigger full EAP authentication. */
278 wpa_printf(MSG_DEBUG, "RSN: no PMKSA entry found - trigger "
279 "full EAP authentication");
280 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
281 NULL, 0, &buflen, NULL);
283 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
296 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
297 * @sm: Pointer to WPA state machine data from wpa_sm_init()
298 * @dst: Destination address for the frame
299 * @key: Pointer to the EAPOL-Key frame header
300 * @ver: Version bits from EAPOL-Key Key Info
301 * @nonce: Nonce value for the EAPOL-Key frame
302 * @wpa_ie: WPA/RSN IE
303 * @wpa_ie_len: Length of the WPA/RSN IE
304 * @ptk: PTK to use for keyed hash and encryption
305 * Returns: 0 on success, -1 on failure
307 int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
308 const struct wpa_eapol_key *key,
309 int ver, const u8 *nonce,
310 const u8 *wpa_ie, size_t wpa_ie_len,
314 struct wpa_eapol_key *reply;
317 if (wpa_ie == NULL) {
318 wpa_printf(MSG_WARNING, "WPA: No wpa_ie set - cannot "
323 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
325 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
326 NULL, sizeof(*reply) + wpa_ie_len,
327 &rlen, (void *) &reply);
331 reply->type = sm->proto == WPA_PROTO_RSN ?
332 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
333 WPA_PUT_BE16(reply->key_info,
334 ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
335 if (sm->proto == WPA_PROTO_RSN)
336 WPA_PUT_BE16(reply->key_length, 0);
338 os_memcpy(reply->key_length, key->key_length, 2);
339 os_memcpy(reply->replay_counter, key->replay_counter,
340 WPA_REPLAY_COUNTER_LEN);
342 WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
343 os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
345 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
347 wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
348 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
349 rbuf, rlen, reply->key_mic);
355 static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
356 const struct wpa_eapol_key *key,
359 #ifdef CONFIG_IEEE80211R
360 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X ||
361 sm->key_mgmt == WPA_KEY_MGMT_FT_PSK)
362 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
363 #endif /* CONFIG_IEEE80211R */
365 wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
366 sm->own_addr, sm->bssid, sm->snonce, key->key_nonce,
367 (u8 *) ptk, sizeof(*ptk));
372 static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
373 const unsigned char *src_addr,
374 const struct wpa_eapol_key *key,
377 struct wpa_eapol_ie_parse ie;
381 if (wpa_sm_get_network_ctx(sm) == NULL) {
382 wpa_printf(MSG_WARNING, "WPA: No SSID info found (msg 1 of "
387 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
388 wpa_printf(MSG_DEBUG, "WPA: RX message 1 of 4-Way Handshake from "
389 MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
391 os_memset(&ie, 0, sizeof(ie));
393 #ifndef CONFIG_NO_WPA2
394 if (sm->proto == WPA_PROTO_RSN) {
395 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
396 const u8 *_buf = (const u8 *) (key + 1);
397 size_t len = WPA_GET_BE16(key->key_data_length);
398 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data", _buf, len);
399 wpa_supplicant_parse_ies(_buf, len, &ie);
401 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
402 "Authenticator", ie.pmkid, PMKID_LEN);
405 #endif /* CONFIG_NO_WPA2 */
407 if (wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid))
410 if (sm->renew_snonce) {
411 if (os_get_random(sm->snonce, WPA_NONCE_LEN)) {
412 wpa_msg(sm->ctx->ctx, MSG_WARNING,
413 "WPA: Failed to get random data for SNonce");
416 sm->renew_snonce = 0;
417 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
418 sm->snonce, WPA_NONCE_LEN);
421 /* Calculate PTK which will be stored as a temporary PTK until it has
422 * been verified when processing message 3/4. */
424 wpa_derive_ptk(sm, src_addr, key, ptk);
425 /* Supplicant: swap tx/rx Mic keys */
426 os_memcpy(buf, ptk->u.auth.tx_mic_key, 8);
427 os_memcpy(ptk->u.auth.tx_mic_key, ptk->u.auth.rx_mic_key, 8);
428 os_memcpy(ptk->u.auth.rx_mic_key, buf, 8);
431 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
432 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len,
436 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
440 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
442 struct wpa_sm *sm = eloop_ctx;
443 rsn_preauth_candidate_process(sm);
447 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
448 const u8 *addr, int secure)
450 wpa_msg(sm->ctx->ctx, MSG_INFO, "WPA: Key negotiation completed with "
451 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
452 wpa_cipher_txt(sm->pairwise_cipher),
453 wpa_cipher_txt(sm->group_cipher));
454 wpa_sm_cancel_scan(sm);
455 wpa_sm_cancel_auth_timeout(sm);
456 wpa_sm_set_state(sm, WPA_COMPLETED);
459 wpa_sm_mlme_setprotection(
460 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
461 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
462 eapol_sm_notify_portValid(sm->eapol, TRUE);
463 if (sm->key_mgmt == WPA_KEY_MGMT_PSK ||
464 sm->key_mgmt == WPA_KEY_MGMT_FT_PSK)
465 eapol_sm_notify_eap_success(sm->eapol, TRUE);
467 * Start preauthentication after a short wait to avoid a
468 * possible race condition between the data receive and key
469 * configuration after the 4-Way Handshake. This increases the
470 * likelyhood of the first preauth EAPOL-Start frame getting to
473 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
476 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
477 wpa_printf(MSG_DEBUG, "RSN: Authenticator accepted "
478 "opportunistic PMKSA entry - marking it valid");
479 sm->cur_pmksa->opportunistic = 0;
482 #ifdef CONFIG_IEEE80211R
483 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X ||
484 sm->key_mgmt == WPA_KEY_MGMT_FT_PSK) {
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);
786 wpa_sm_req_scan(sm, 0, 0);
790 static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
791 const unsigned char *src_addr,
792 struct wpa_eapol_ie_parse *ie)
794 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
795 wpa_printf(MSG_DEBUG, "WPA: No WPA/RSN IE for this AP known. "
796 "Trying to get from scan results");
797 if (wpa_sm_get_beacon_ie(sm) < 0) {
798 wpa_printf(MSG_WARNING, "WPA: Could not find AP from "
801 wpa_printf(MSG_DEBUG, "WPA: Found the current AP from "
802 "updated scan results");
806 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
807 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
808 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
809 "with IE in Beacon/ProbeResp (no IE?)",
810 src_addr, ie->wpa_ie, ie->wpa_ie_len,
811 ie->rsn_ie, ie->rsn_ie_len);
815 if ((ie->wpa_ie && sm->ap_wpa_ie &&
816 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
817 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
818 (ie->rsn_ie && sm->ap_rsn_ie &&
819 (ie->rsn_ie_len != sm->ap_rsn_ie_len ||
820 os_memcmp(ie->rsn_ie, sm->ap_rsn_ie, ie->rsn_ie_len) != 0))) {
821 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
822 "with IE in Beacon/ProbeResp",
823 src_addr, ie->wpa_ie, ie->wpa_ie_len,
824 ie->rsn_ie, ie->rsn_ie_len);
828 if (sm->proto == WPA_PROTO_WPA &&
829 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
830 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
831 "detected - RSN was enabled and RSN IE "
832 "was in msg 3/4, but not in "
834 src_addr, ie->wpa_ie, ie->wpa_ie_len,
835 ie->rsn_ie, ie->rsn_ie_len);
839 #ifdef CONFIG_IEEE80211R
840 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X ||
841 sm->key_mgmt == WPA_KEY_MGMT_FT_PSK) {
842 struct rsn_mdie *mdie;
843 /* TODO: verify that full MDIE matches with the one from scan
844 * results, not only mobility domain */
845 mdie = (struct rsn_mdie *) (ie->mdie + 2);
846 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
847 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
848 MOBILITY_DOMAIN_ID_LEN) != 0) {
849 wpa_printf(MSG_DEBUG, "FT: MDIE in msg 3/4 did not "
850 "match with the current mobility domain");
854 #endif /* CONFIG_IEEE80211R */
861 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
862 * @sm: Pointer to WPA state machine data from wpa_sm_init()
863 * @dst: Destination address for the frame
864 * @key: Pointer to the EAPOL-Key frame header
865 * @ver: Version bits from EAPOL-Key Key Info
866 * @key_info: Key Info
867 * @kde: KDEs to include the EAPOL-Key frame
868 * @kde_len: Length of KDEs
869 * @ptk: PTK to use for keyed hash and encryption
870 * Returns: 0 on success, -1 on failure
872 int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
873 const struct wpa_eapol_key *key,
874 u16 ver, u16 key_info,
875 const u8 *kde, size_t kde_len,
879 struct wpa_eapol_key *reply;
883 wpa_hexdump(MSG_DEBUG, "WPA: KDE for msg 4/4", kde, kde_len);
885 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
886 sizeof(*reply) + kde_len,
887 &rlen, (void *) &reply);
891 reply->type = sm->proto == WPA_PROTO_RSN ?
892 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
893 key_info &= WPA_KEY_INFO_SECURE;
894 key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
895 WPA_PUT_BE16(reply->key_info, key_info);
896 if (sm->proto == WPA_PROTO_RSN)
897 WPA_PUT_BE16(reply->key_length, 0);
899 os_memcpy(reply->key_length, key->key_length, 2);
900 os_memcpy(reply->replay_counter, key->replay_counter,
901 WPA_REPLAY_COUNTER_LEN);
903 WPA_PUT_BE16(reply->key_data_length, kde_len);
905 os_memcpy(reply + 1, kde, kde_len);
907 wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
908 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
909 rbuf, rlen, reply->key_mic);
915 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
916 const struct wpa_eapol_key *key,
919 u16 key_info, keylen, len;
921 struct wpa_eapol_ie_parse ie;
923 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
924 wpa_printf(MSG_DEBUG, "WPA: RX message 3 of 4-Way Handshake from "
925 MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
927 key_info = WPA_GET_BE16(key->key_info);
929 pos = (const u8 *) (key + 1);
930 len = WPA_GET_BE16(key->key_data_length);
931 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", pos, len);
932 wpa_supplicant_parse_ies(pos, len, &ie);
933 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
934 wpa_printf(MSG_WARNING, "WPA: GTK IE in unencrypted key data");
937 #ifdef CONFIG_IEEE80211W
938 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
939 wpa_printf(MSG_WARNING, "WPA: IGTK KDE in unencrypted key "
944 if (ie.igtk && ie.igtk_len != sizeof(struct wpa_igtk_kde)) {
945 wpa_printf(MSG_WARNING, "WPA: Invalid IGTK KDE length %lu",
946 (unsigned long) ie.igtk_len);
949 #endif /* CONFIG_IEEE80211W */
951 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
954 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
955 wpa_printf(MSG_WARNING, "WPA: ANonce from message 1 of 4-Way "
956 "Handshake differs from 3 of 4-Way Handshake - drop"
957 " packet (src=" MACSTR ")", MAC2STR(sm->bssid));
961 keylen = WPA_GET_BE16(key->key_length);
962 switch (sm->pairwise_cipher) {
963 case WPA_CIPHER_CCMP:
965 wpa_printf(MSG_WARNING, "WPA: Invalid CCMP key length "
966 "%d (src=" MACSTR ")",
967 keylen, MAC2STR(sm->bssid));
971 case WPA_CIPHER_TKIP:
973 wpa_printf(MSG_WARNING, "WPA: Invalid TKIP key length "
974 "%d (src=" MACSTR ")",
975 keylen, MAC2STR(sm->bssid));
981 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
985 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
986 * for the next 4-Way Handshake. If msg 3 is received again, the old
987 * SNonce will still be used to avoid changing PTK. */
988 sm->renew_snonce = 1;
990 if (key_info & WPA_KEY_INFO_INSTALL) {
991 wpa_supplicant_install_ptk(sm, key);
994 if (key_info & WPA_KEY_INFO_SECURE) {
995 wpa_sm_mlme_setprotection(
996 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
997 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
998 eapol_sm_notify_portValid(sm->eapol, TRUE);
1000 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1003 wpa_supplicant_pairwise_gtk(sm, key,
1004 ie.gtk, ie.gtk_len, key_info) < 0) {
1005 wpa_printf(MSG_INFO, "RSN: Failed to configure GTK");
1008 if (ieee80211w_set_keys(sm, &ie) < 0)
1009 wpa_printf(MSG_INFO, "RSN: Failed to configure IGTK");
1013 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1017 struct wpa_gtk_data *gd)
1020 struct wpa_eapol_ie_parse ie;
1022 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
1023 wpa_supplicant_parse_ies(keydata, keydatalen, &ie);
1024 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1025 wpa_printf(MSG_WARNING, "WPA: GTK IE in unencrypted key data");
1028 if (ie.gtk == NULL) {
1029 wpa_printf(MSG_INFO, "WPA: No GTK IE in Group Key msg 1/2");
1032 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1034 if (wpa_supplicant_check_group_cipher(sm->group_cipher,
1035 gd->gtk_len, maxkeylen,
1036 &gd->key_rsc_len, &gd->alg))
1039 wpa_hexdump(MSG_DEBUG, "RSN: received GTK in group key handshake",
1040 ie.gtk, ie.gtk_len);
1041 gd->keyidx = ie.gtk[0] & 0x3;
1042 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1043 !!(ie.gtk[0] & BIT(2)));
1044 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1045 wpa_printf(MSG_INFO, "RSN: Too long GTK in GTK IE "
1046 "(len=%lu)", (unsigned long) ie.gtk_len - 2);
1049 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1051 if (ieee80211w_set_keys(sm, &ie) < 0)
1052 wpa_printf(MSG_INFO, "RSN: Failed to configure IGTK");
1058 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1059 const struct wpa_eapol_key *key,
1060 size_t keydatalen, int key_info,
1061 size_t extra_len, u16 ver,
1062 struct wpa_gtk_data *gd)
1067 gd->gtk_len = WPA_GET_BE16(key->key_length);
1068 maxkeylen = keydatalen;
1069 if (keydatalen > extra_len) {
1070 wpa_printf(MSG_INFO, "WPA: Truncated EAPOL-Key packet:"
1071 " key_data_length=%lu > extra_len=%lu",
1072 (unsigned long) keydatalen,
1073 (unsigned long) extra_len);
1076 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1077 if (maxkeylen < 8) {
1078 wpa_printf(MSG_INFO, "WPA: Too short maxkeylen (%lu)",
1079 (unsigned long) maxkeylen);
1085 if (wpa_supplicant_check_group_cipher(sm->group_cipher,
1086 gd->gtk_len, maxkeylen,
1087 &gd->key_rsc_len, &gd->alg))
1090 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1091 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1092 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1093 os_memcpy(ek, key->key_iv, 16);
1094 os_memcpy(ek + 16, sm->ptk.kek, 16);
1095 if (keydatalen > sizeof(gd->gtk)) {
1096 wpa_printf(MSG_WARNING, "WPA: RC4 key data "
1098 (unsigned long) keydatalen);
1101 os_memcpy(gd->gtk, key + 1, keydatalen);
1102 rc4_skip(ek, 32, 256, gd->gtk, keydatalen);
1103 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1104 if (keydatalen % 8) {
1105 wpa_printf(MSG_WARNING, "WPA: Unsupported AES-WRAP "
1106 "len %lu", (unsigned long) keydatalen);
1109 if (maxkeylen > sizeof(gd->gtk)) {
1110 wpa_printf(MSG_WARNING, "WPA: AES-WRAP key data "
1111 "too long (keydatalen=%lu maxkeylen=%lu)",
1112 (unsigned long) keydatalen,
1113 (unsigned long) maxkeylen);
1116 if (aes_unwrap(sm->ptk.kek, maxkeylen / 8,
1117 (const u8 *) (key + 1), gd->gtk)) {
1118 wpa_printf(MSG_WARNING, "WPA: AES unwrap "
1119 "failed - could not decrypt GTK");
1123 wpa_printf(MSG_WARNING, "WPA: Unsupported key_info type %d",
1127 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1128 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1133 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1134 const struct wpa_eapol_key *key,
1135 int ver, u16 key_info)
1138 struct wpa_eapol_key *reply;
1141 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1142 sizeof(*reply), &rlen, (void *) &reply);
1146 reply->type = sm->proto == WPA_PROTO_RSN ?
1147 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1148 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1149 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1150 WPA_PUT_BE16(reply->key_info, key_info);
1151 if (sm->proto == WPA_PROTO_RSN)
1152 WPA_PUT_BE16(reply->key_length, 0);
1154 os_memcpy(reply->key_length, key->key_length, 2);
1155 os_memcpy(reply->replay_counter, key->replay_counter,
1156 WPA_REPLAY_COUNTER_LEN);
1158 WPA_PUT_BE16(reply->key_data_length, 0);
1160 wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1161 wpa_eapol_key_send(sm, sm->ptk.kck, ver, sm->bssid, ETH_P_EAPOL,
1162 rbuf, rlen, reply->key_mic);
1168 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1169 const unsigned char *src_addr,
1170 const struct wpa_eapol_key *key,
1171 int extra_len, u16 ver)
1173 u16 key_info, keydatalen;
1175 struct wpa_gtk_data gd;
1177 os_memset(&gd, 0, sizeof(gd));
1179 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1180 wpa_printf(MSG_DEBUG, "WPA: RX message 1 of Group Key Handshake from "
1181 MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1183 key_info = WPA_GET_BE16(key->key_info);
1184 keydatalen = WPA_GET_BE16(key->key_data_length);
1186 if (sm->proto == WPA_PROTO_RSN) {
1187 ret = wpa_supplicant_process_1_of_2_rsn(sm,
1188 (const u8 *) (key + 1),
1189 keydatalen, key_info,
1192 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, keydatalen,
1193 key_info, extra_len,
1197 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1202 if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc) ||
1203 wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
1207 wpa_msg(sm->ctx->ctx, MSG_INFO, "WPA: Group rekeying "
1208 "completed with " MACSTR " [GTK=%s]",
1209 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1210 wpa_sm_cancel_auth_timeout(sm);
1211 wpa_sm_set_state(sm, WPA_COMPLETED);
1213 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1215 WPA_KEY_INFO_SECURE);
1220 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1221 struct wpa_eapol_key *key,
1223 const u8 *buf, size_t len)
1228 os_memcpy(mic, key->key_mic, 16);
1230 os_memset(key->key_mic, 0, 16);
1231 wpa_eapol_key_mic(sm->tptk.kck, ver, buf, len,
1233 if (os_memcmp(mic, key->key_mic, 16) != 0) {
1234 wpa_printf(MSG_WARNING, "WPA: Invalid EAPOL-Key MIC "
1235 "when using TPTK - ignoring TPTK");
1240 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1244 if (!ok && sm->ptk_set) {
1245 os_memset(key->key_mic, 0, 16);
1246 wpa_eapol_key_mic(sm->ptk.kck, ver, buf, len,
1248 if (os_memcmp(mic, key->key_mic, 16) != 0) {
1249 wpa_printf(MSG_WARNING, "WPA: Invalid EAPOL-Key MIC "
1250 "- dropping packet");
1257 wpa_printf(MSG_WARNING, "WPA: Could not verify EAPOL-Key MIC "
1258 "- dropping packet");
1262 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1263 WPA_REPLAY_COUNTER_LEN);
1264 sm->rx_replay_counter_set = 1;
1269 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1270 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
1271 struct wpa_eapol_key *key, u16 ver)
1273 u16 keydatalen = WPA_GET_BE16(key->key_data_length);
1275 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
1276 (u8 *) (key + 1), keydatalen);
1278 wpa_printf(MSG_WARNING, "WPA: PTK not available, "
1279 "cannot decrypt EAPOL-Key key data.");
1283 /* Decrypt key data here so that this operation does not need
1284 * to be implemented separately for each message type. */
1285 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1287 os_memcpy(ek, key->key_iv, 16);
1288 os_memcpy(ek + 16, sm->ptk.kek, 16);
1289 rc4_skip(ek, 32, 256, (u8 *) (key + 1), keydatalen);
1290 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1291 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1293 if (keydatalen % 8) {
1294 wpa_printf(MSG_WARNING, "WPA: Unsupported "
1295 "AES-WRAP len %d", keydatalen);
1298 keydatalen -= 8; /* AES-WRAP adds 8 bytes */
1299 buf = os_malloc(keydatalen);
1301 wpa_printf(MSG_WARNING, "WPA: No memory for "
1302 "AES-UNWRAP buffer");
1305 if (aes_unwrap(sm->ptk.kek, keydatalen / 8,
1306 (u8 *) (key + 1), buf)) {
1308 wpa_printf(MSG_WARNING, "WPA: AES unwrap failed - "
1309 "could not decrypt EAPOL-Key key data");
1312 os_memcpy(key + 1, buf, keydatalen);
1314 WPA_PUT_BE16(key->key_data_length, keydatalen);
1316 wpa_printf(MSG_WARNING, "WPA: Unsupported key_info type %d",
1320 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
1321 (u8 *) (key + 1), keydatalen);
1327 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1328 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1330 void wpa_sm_aborted_cached(struct wpa_sm *sm)
1332 if (sm && sm->cur_pmksa) {
1333 wpa_printf(MSG_DEBUG, "RSN: Cancelling PMKSA caching attempt");
1334 sm->cur_pmksa = NULL;
1339 static void wpa_eapol_key_dump(const struct wpa_eapol_key *key)
1341 #ifndef CONFIG_NO_STDOUT_DEBUG
1342 u16 key_info = WPA_GET_BE16(key->key_info);
1344 wpa_printf(MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1345 wpa_printf(MSG_DEBUG, " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s"
1347 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1348 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1349 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1350 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1351 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1352 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1353 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1354 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1355 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1356 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1357 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1358 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1359 wpa_printf(MSG_DEBUG, " key_length=%u key_data_length=%u",
1360 WPA_GET_BE16(key->key_length),
1361 WPA_GET_BE16(key->key_data_length));
1362 wpa_hexdump(MSG_DEBUG, " replay_counter",
1363 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1364 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1365 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1366 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1367 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
1368 wpa_hexdump(MSG_DEBUG, " key_mic", key->key_mic, 16);
1369 #endif /* CONFIG_NO_STDOUT_DEBUG */
1374 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1375 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1376 * @src_addr: Source MAC address of the EAPOL packet
1377 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1378 * @len: Length of the EAPOL frame
1379 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1381 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1382 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1383 * only processing WPA and WPA2 EAPOL-Key frames.
1385 * The received EAPOL-Key packets are validated and valid packets are replied
1386 * to. In addition, key material (PTK, GTK) is configured at the end of a
1387 * successful key handshake.
1389 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1390 const u8 *buf, size_t len)
1392 size_t plen, data_len, extra_len;
1393 struct ieee802_1x_hdr *hdr;
1394 struct wpa_eapol_key *key;
1398 struct wpa_peerkey *peerkey = NULL;
1400 #ifdef CONFIG_IEEE80211R
1401 sm->ft_completed = 0;
1402 #endif /* CONFIG_IEEE80211R */
1404 if (len < sizeof(*hdr) + sizeof(*key)) {
1405 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame too short to be a WPA "
1406 "EAPOL-Key (len %lu, expecting at least %lu)",
1407 (unsigned long) len,
1408 (unsigned long) sizeof(*hdr) + sizeof(*key));
1412 tmp = os_malloc(len);
1415 os_memcpy(tmp, buf, len);
1417 hdr = (struct ieee802_1x_hdr *) tmp;
1418 key = (struct wpa_eapol_key *) (hdr + 1);
1419 plen = be_to_host16(hdr->length);
1420 data_len = plen + sizeof(*hdr);
1421 wpa_printf(MSG_DEBUG, "IEEE 802.1X RX: version=%d type=%d length=%lu",
1422 hdr->version, hdr->type, (unsigned long) plen);
1424 if (hdr->version < EAPOL_VERSION) {
1425 /* TODO: backwards compatibility */
1427 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1428 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame (type %u) discarded, "
1429 "not a Key frame", hdr->type);
1433 if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
1434 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame payload size %lu "
1435 "invalid (frame size %lu)",
1436 (unsigned long) plen, (unsigned long) len);
1441 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1443 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key type (%d) unknown, "
1444 "discarded", key->type);
1448 wpa_eapol_key_dump(key);
1450 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
1451 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", tmp, len);
1452 if (data_len < len) {
1453 wpa_printf(MSG_DEBUG, "WPA: ignoring %lu bytes after the IEEE "
1454 "802.1X data", (unsigned long) len - data_len);
1456 key_info = WPA_GET_BE16(key->key_info);
1457 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1458 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1459 #ifdef CONFIG_IEEE80211R
1460 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1461 #endif /* CONFIG_IEEE80211R */
1462 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1463 wpa_printf(MSG_INFO, "WPA: Unsupported EAPOL-Key descriptor "
1464 "version %d.", ver);
1468 #ifdef CONFIG_IEEE80211R
1469 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X ||
1470 sm->key_mgmt == WPA_KEY_MGMT_FT_PSK) {
1471 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1472 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1473 wpa_printf(MSG_INFO, "FT: AP did not use "
1478 #endif /* CONFIG_IEEE80211R */
1479 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
1480 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1481 wpa_printf(MSG_INFO, "WPA: CCMP is used, but EAPOL-Key "
1482 "descriptor version (%d) is not 2.", ver);
1483 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1484 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1485 /* Earlier versions of IEEE 802.11i did not explicitly
1486 * require version 2 descriptor for all EAPOL-Key
1487 * packets, so allow group keys to use version 1 if
1488 * CCMP is not used for them. */
1489 wpa_printf(MSG_INFO, "WPA: Backwards compatibility: "
1490 "allow invalid version for non-CCMP group "
1496 #ifdef CONFIG_PEERKEY
1497 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1498 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1502 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1503 if (!peerkey->initiator && peerkey->replay_counter_set &&
1504 os_memcmp(key->replay_counter, peerkey->replay_counter,
1505 WPA_REPLAY_COUNTER_LEN) <= 0) {
1506 wpa_printf(MSG_WARNING, "RSN: EAPOL-Key Replay "
1507 "Counter did not increase (STK) - dropping "
1510 } else if (peerkey->initiator) {
1511 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1512 os_memcpy(_tmp, key->replay_counter,
1513 WPA_REPLAY_COUNTER_LEN);
1514 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
1515 if (os_memcmp(_tmp, peerkey->replay_counter,
1516 WPA_REPLAY_COUNTER_LEN) != 0) {
1517 wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key Replay "
1518 "Counter did not match (STK) - "
1525 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
1526 wpa_printf(MSG_INFO, "RSN: Ack bit in key_info from STK peer");
1529 #endif /* CONFIG_PEERKEY */
1531 if (!peerkey && sm->rx_replay_counter_set &&
1532 os_memcmp(key->replay_counter, sm->rx_replay_counter,
1533 WPA_REPLAY_COUNTER_LEN) <= 0) {
1534 wpa_printf(MSG_WARNING, "WPA: EAPOL-Key Replay Counter did not"
1535 " increase - dropping packet");
1539 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
1540 #ifdef CONFIG_PEERKEY
1541 && (peerkey == NULL || !peerkey->initiator)
1542 #endif /* CONFIG_PEERKEY */
1544 wpa_printf(MSG_INFO, "WPA: No Ack bit in key_info");
1548 if (key_info & WPA_KEY_INFO_REQUEST) {
1549 wpa_printf(MSG_INFO, "WPA: EAPOL-Key with Request bit - "
1554 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
1555 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
1558 #ifdef CONFIG_PEERKEY
1559 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
1560 peerkey_verify_eapol_key_mic(sm, peerkey, key, ver, tmp, data_len))
1562 #endif /* CONFIG_PEERKEY */
1564 extra_len = data_len - sizeof(*hdr) - sizeof(*key);
1566 if (WPA_GET_BE16(key->key_data_length) > extra_len) {
1567 wpa_msg(sm->ctx->ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1568 "frame - key_data overflow (%d > %lu)",
1569 WPA_GET_BE16(key->key_data_length),
1570 (unsigned long) extra_len);
1573 extra_len = WPA_GET_BE16(key->key_data_length);
1575 if (sm->proto == WPA_PROTO_RSN &&
1576 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1577 if (wpa_supplicant_decrypt_key_data(sm, key, ver))
1579 extra_len = WPA_GET_BE16(key->key_data_length);
1582 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1583 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
1584 wpa_printf(MSG_WARNING, "WPA: Ignored EAPOL-Key "
1585 "(Pairwise) with non-zero key index");
1589 /* PeerKey 4-Way Handshake */
1590 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver);
1591 } else if (key_info & WPA_KEY_INFO_MIC) {
1592 /* 3/4 4-Way Handshake */
1593 wpa_supplicant_process_3_of_4(sm, key, ver);
1595 /* 1/4 4-Way Handshake */
1596 wpa_supplicant_process_1_of_4(sm, src_addr, key,
1599 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1600 /* PeerKey SMK Handshake */
1601 peerkey_rx_eapol_smk(sm, src_addr, key, extra_len, key_info,
1604 if (key_info & WPA_KEY_INFO_MIC) {
1605 /* 1/2 Group Key Handshake */
1606 wpa_supplicant_process_1_of_2(sm, src_addr, key,
1609 wpa_printf(MSG_WARNING, "WPA: EAPOL-Key (Group) "
1610 "without Mic bit - dropped");
1622 #ifdef CONFIG_CTRL_IFACE
1623 static int wpa_cipher_bits(int cipher)
1626 case WPA_CIPHER_CCMP:
1628 case WPA_CIPHER_TKIP:
1630 case WPA_CIPHER_WEP104:
1632 case WPA_CIPHER_WEP40:
1640 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
1642 switch (sm->key_mgmt) {
1643 case WPA_KEY_MGMT_IEEE8021X:
1644 return (sm->proto == WPA_PROTO_RSN ?
1645 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
1646 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
1647 case WPA_KEY_MGMT_PSK:
1648 return (sm->proto == WPA_PROTO_RSN ?
1649 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
1650 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
1651 #ifdef CONFIG_IEEE80211R
1652 case WPA_KEY_MGMT_FT_IEEE8021X:
1653 return RSN_AUTH_KEY_MGMT_FT_802_1X;
1654 case WPA_KEY_MGMT_FT_PSK:
1655 return RSN_AUTH_KEY_MGMT_FT_PSK;
1656 #endif /* CONFIG_IEEE80211R */
1657 case WPA_KEY_MGMT_WPA_NONE:
1658 return WPA_AUTH_KEY_MGMT_NONE;
1665 static u32 wpa_cipher_suite(struct wpa_sm *sm, int cipher)
1668 case WPA_CIPHER_CCMP:
1669 return (sm->proto == WPA_PROTO_RSN ?
1670 RSN_CIPHER_SUITE_CCMP : WPA_CIPHER_SUITE_CCMP);
1671 case WPA_CIPHER_TKIP:
1672 return (sm->proto == WPA_PROTO_RSN ?
1673 RSN_CIPHER_SUITE_TKIP : WPA_CIPHER_SUITE_TKIP);
1674 case WPA_CIPHER_WEP104:
1675 return (sm->proto == WPA_PROTO_RSN ?
1676 RSN_CIPHER_SUITE_WEP104 : WPA_CIPHER_SUITE_WEP104);
1677 case WPA_CIPHER_WEP40:
1678 return (sm->proto == WPA_PROTO_RSN ?
1679 RSN_CIPHER_SUITE_WEP40 : WPA_CIPHER_SUITE_WEP40);
1680 case WPA_CIPHER_NONE:
1681 return (sm->proto == WPA_PROTO_RSN ?
1682 RSN_CIPHER_SUITE_NONE : WPA_CIPHER_SUITE_NONE);
1689 #define RSN_SUITE "%02x-%02x-%02x-%d"
1690 #define RSN_SUITE_ARG(s) \
1691 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
1694 * wpa_sm_get_mib - Dump text list of MIB entries
1695 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1696 * @buf: Buffer for the list
1697 * @buflen: Length of the buffer
1698 * Returns: Number of bytes written to buffer
1700 * This function is used fetch dot11 MIB variables.
1702 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
1704 char pmkid_txt[PMKID_LEN * 2 + 1];
1708 if (sm->cur_pmksa) {
1709 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
1710 sm->cur_pmksa->pmkid, PMKID_LEN);
1712 pmkid_txt[0] = '\0';
1714 if ((sm->key_mgmt == WPA_KEY_MGMT_PSK ||
1715 sm->key_mgmt == WPA_KEY_MGMT_IEEE8021X ||
1716 sm->key_mgmt == WPA_KEY_MGMT_FT_PSK ||
1717 sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) &&
1718 sm->proto == WPA_PROTO_RSN)
1723 ret = os_snprintf(buf, buflen,
1724 "dot11RSNAOptionImplemented=TRUE\n"
1725 "dot11RSNAPreauthenticationImplemented=TRUE\n"
1726 "dot11RSNAEnabled=%s\n"
1727 "dot11RSNAPreauthenticationEnabled=%s\n"
1728 "dot11RSNAConfigVersion=%d\n"
1729 "dot11RSNAConfigPairwiseKeysSupported=5\n"
1730 "dot11RSNAConfigGroupCipherSize=%d\n"
1731 "dot11RSNAConfigPMKLifetime=%d\n"
1732 "dot11RSNAConfigPMKReauthThreshold=%d\n"
1733 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
1734 "dot11RSNAConfigSATimeout=%d\n",
1735 rsna ? "TRUE" : "FALSE",
1736 rsna ? "TRUE" : "FALSE",
1738 wpa_cipher_bits(sm->group_cipher),
1739 sm->dot11RSNAConfigPMKLifetime,
1740 sm->dot11RSNAConfigPMKReauthThreshold,
1741 sm->dot11RSNAConfigSATimeout);
1742 if (ret < 0 || (size_t) ret >= buflen)
1747 buf + len, buflen - len,
1748 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
1749 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
1750 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
1751 "dot11RSNAPMKIDUsed=%s\n"
1752 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
1753 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
1754 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
1755 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
1756 "dot11RSNA4WayHandshakeFailures=%u\n",
1757 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1758 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->pairwise_cipher)),
1759 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->group_cipher)),
1761 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1762 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->pairwise_cipher)),
1763 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->group_cipher)),
1764 sm->dot11RSNA4WayHandshakeFailures);
1765 if (ret >= 0 && (size_t) ret < buflen)
1770 #endif /* CONFIG_CTRL_IFACE */
1773 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
1774 void *ctx, int replace)
1776 struct wpa_sm *sm = ctx;
1778 if (sm->cur_pmksa == entry ||
1779 (sm->pmk_len == entry->pmk_len &&
1780 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
1781 wpa_printf(MSG_DEBUG, "RSN: removed current PMKSA entry");
1782 sm->cur_pmksa = NULL;
1785 /* A new entry is being added, so no need to
1786 * deauthenticate in this case. This happens when EAP
1787 * authentication is completed again (reauth or failed
1788 * PMKSA caching attempt). */
1792 os_memset(sm->pmk, 0, sizeof(sm->pmk));
1793 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1794 wpa_sm_req_scan(sm, 0, 0);
1800 * wpa_sm_init - Initialize WPA state machine
1801 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
1802 * Returns: Pointer to the allocated WPA state machine data
1804 * This function is used to allocate a new WPA state machine and the returned
1805 * value is passed to all WPA state machine calls.
1807 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
1811 sm = os_zalloc(sizeof(*sm));
1814 sm->renew_snonce = 1;
1817 sm->dot11RSNAConfigPMKLifetime = 43200;
1818 sm->dot11RSNAConfigPMKReauthThreshold = 70;
1819 sm->dot11RSNAConfigSATimeout = 60;
1821 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
1822 if (sm->pmksa == NULL) {
1823 wpa_printf(MSG_ERROR, "RSN: PMKSA cache initialization "
1834 * wpa_sm_deinit - Deinitialize WPA state machine
1835 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1837 void wpa_sm_deinit(struct wpa_sm *sm)
1841 pmksa_cache_deinit(sm->pmksa);
1842 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
1843 os_free(sm->assoc_wpa_ie);
1844 os_free(sm->ap_wpa_ie);
1845 os_free(sm->ap_rsn_ie);
1853 * wpa_sm_notify_assoc - Notify WPA state machine about association
1854 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1855 * @bssid: The BSSID of the new association
1857 * This function is called to let WPA state machine know that the connection
1860 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
1865 wpa_printf(MSG_DEBUG, "WPA: Association event - clear replay counter");
1866 os_memcpy(sm->bssid, bssid, ETH_ALEN);
1867 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
1868 sm->rx_replay_counter_set = 0;
1869 sm->renew_snonce = 1;
1870 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
1871 rsn_preauth_deinit(sm);
1873 #ifdef CONFIG_IEEE80211R
1874 if ((sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X ||
1875 sm->key_mgmt == WPA_KEY_MGMT_FT_PSK) &&
1876 wpa_ft_is_completed(sm)) {
1877 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
1879 /* Prepare for the next transition */
1880 wpa_ft_prepare_auth_request(sm);
1882 #endif /* CONFIG_IEEE80211R */
1887 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
1888 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1890 * This function is called to let WPA state machine know that the connection
1891 * was lost. This will abort any existing pre-authentication session.
1893 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
1895 rsn_preauth_deinit(sm);
1896 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
1897 sm->dot11RSNA4WayHandshakeFailures++;
1902 * wpa_sm_set_pmk - Set PMK
1903 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1905 * @pmk_len: The length of the new PMK in bytes
1907 * Configure the PMK for WPA state machine.
1909 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len)
1914 sm->pmk_len = pmk_len;
1915 os_memcpy(sm->pmk, pmk, pmk_len);
1917 #ifdef CONFIG_IEEE80211R
1918 /* Set XXKey to be PSK for FT key derivation */
1919 sm->xxkey_len = pmk_len;
1920 os_memcpy(sm->xxkey, pmk, pmk_len);
1921 #endif /* CONFIG_IEEE80211R */
1926 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
1927 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1929 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
1932 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
1937 if (sm->cur_pmksa) {
1938 sm->pmk_len = sm->cur_pmksa->pmk_len;
1939 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
1941 sm->pmk_len = PMK_LEN;
1942 os_memset(sm->pmk, 0, PMK_LEN);
1948 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
1949 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1950 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
1952 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
1955 sm->fast_reauth = fast_reauth;
1960 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
1961 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1962 * @scard_ctx: Context pointer for smartcard related callback functions
1964 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
1968 sm->scard_ctx = scard_ctx;
1969 if (sm->preauth_eapol)
1970 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
1975 * wpa_sm_set_config - Notification of current configration change
1976 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1977 * @config: Pointer to current network configuration
1979 * Notify WPA state machine that configuration has changed. config will be
1980 * stored as a backpointer to network configuration. This can be %NULL to clear
1981 * the stored pointed.
1983 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
1988 sm->network_ctx = config;
1990 sm->peerkey_enabled = config->peerkey_enabled;
1991 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
1992 sm->proactive_key_caching = config->proactive_key_caching;
1993 sm->eap_workaround = config->eap_workaround;
1994 sm->eap_conf_ctx = config->eap_conf_ctx;
1996 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
1997 sm->ssid_len = config->ssid_len;
2001 sm->peerkey_enabled = 0;
2002 sm->allowed_pairwise_cipher = 0;
2003 sm->proactive_key_caching = 0;
2004 sm->eap_workaround = 0;
2005 sm->eap_conf_ctx = NULL;
2008 pmksa_cache_notify_reconfig(sm->pmksa);
2013 * wpa_sm_set_own_addr - Set own MAC address
2014 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2015 * @addr: Own MAC address
2017 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2020 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2025 * wpa_sm_set_ifname - Set network interface name
2026 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2027 * @ifname: Interface name
2028 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2030 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2031 const char *bridge_ifname)
2034 sm->ifname = ifname;
2035 sm->bridge_ifname = bridge_ifname;
2041 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2042 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2043 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2045 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2053 * wpa_sm_set_param - Set WPA state machine parameters
2054 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2055 * @param: Parameter field
2056 * @value: Parameter value
2057 * Returns: 0 on success, -1 on failure
2059 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2068 case RSNA_PMK_LIFETIME:
2070 sm->dot11RSNAConfigPMKLifetime = value;
2074 case RSNA_PMK_REAUTH_THRESHOLD:
2075 if (value > 0 && value <= 100)
2076 sm->dot11RSNAConfigPMKReauthThreshold = value;
2080 case RSNA_SA_TIMEOUT:
2082 sm->dot11RSNAConfigSATimeout = value;
2086 case WPA_PARAM_PROTO:
2089 case WPA_PARAM_PAIRWISE:
2090 sm->pairwise_cipher = value;
2092 case WPA_PARAM_GROUP:
2093 sm->group_cipher = value;
2095 case WPA_PARAM_KEY_MGMT:
2096 sm->key_mgmt = value;
2098 #ifdef CONFIG_IEEE80211W
2099 case WPA_PARAM_MGMT_GROUP:
2100 sm->mgmt_group_cipher = value;
2102 #endif /* CONFIG_IEEE80211W */
2103 case WPA_PARAM_RSN_ENABLED:
2104 sm->rsn_enabled = value;
2115 * wpa_sm_get_param - Get WPA state machine parameters
2116 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2117 * @param: Parameter field
2118 * Returns: Parameter value
2120 unsigned int wpa_sm_get_param(struct wpa_sm *sm, enum wpa_sm_conf_params param)
2126 case RSNA_PMK_LIFETIME:
2127 return sm->dot11RSNAConfigPMKLifetime;
2128 case RSNA_PMK_REAUTH_THRESHOLD:
2129 return sm->dot11RSNAConfigPMKReauthThreshold;
2130 case RSNA_SA_TIMEOUT:
2131 return sm->dot11RSNAConfigSATimeout;
2132 case WPA_PARAM_PROTO:
2134 case WPA_PARAM_PAIRWISE:
2135 return sm->pairwise_cipher;
2136 case WPA_PARAM_GROUP:
2137 return sm->group_cipher;
2138 case WPA_PARAM_KEY_MGMT:
2139 return sm->key_mgmt;
2140 #ifdef CONFIG_IEEE80211W
2141 case WPA_PARAM_MGMT_GROUP:
2142 return sm->mgmt_group_cipher;
2143 #endif /* CONFIG_IEEE80211W */
2144 case WPA_PARAM_RSN_ENABLED:
2145 return sm->rsn_enabled;
2153 * wpa_sm_get_status - Get WPA state machine
2154 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2155 * @buf: Buffer for status information
2156 * @buflen: Maximum buffer length
2157 * @verbose: Whether to include verbose status information
2158 * Returns: Number of bytes written to buf.
2160 * Query WPA state machine for status information. This function fills in
2161 * a text area with current status information. If the buffer (buf) is not
2162 * large enough, status information will be truncated to fit the buffer.
2164 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2167 char *pos = buf, *end = buf + buflen;
2170 ret = os_snprintf(pos, end - pos,
2171 "pairwise_cipher=%s\n"
2174 wpa_cipher_txt(sm->pairwise_cipher),
2175 wpa_cipher_txt(sm->group_cipher),
2176 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
2177 if (ret < 0 || ret >= end - pos)
2185 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2186 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2187 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2188 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2189 * Returns: 0 on success, -1 on failure
2191 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2199 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2204 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2205 wpa_ie, *wpa_ie_len);
2207 if (sm->assoc_wpa_ie == NULL) {
2209 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2210 * the correct version of the IE even if PMKSA caching is
2211 * aborted (which would remove PMKID from IE generation).
2213 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2214 if (sm->assoc_wpa_ie == NULL)
2217 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2218 sm->assoc_wpa_ie_len = *wpa_ie_len;
2226 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2227 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2228 * @ie: Pointer to IE data (starting from id)
2230 * Returns: 0 on success, -1 on failure
2232 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2233 * Request frame. The IE will be used to override the default value generated
2234 * with wpa_sm_set_assoc_wpa_ie_default().
2236 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2241 os_free(sm->assoc_wpa_ie);
2242 if (ie == NULL || len == 0) {
2243 wpa_printf(MSG_DEBUG, "WPA: clearing own WPA/RSN IE");
2244 sm->assoc_wpa_ie = NULL;
2245 sm->assoc_wpa_ie_len = 0;
2247 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2248 sm->assoc_wpa_ie = os_malloc(len);
2249 if (sm->assoc_wpa_ie == NULL)
2252 os_memcpy(sm->assoc_wpa_ie, ie, len);
2253 sm->assoc_wpa_ie_len = len;
2261 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2262 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2263 * @ie: Pointer to IE data (starting from id)
2265 * Returns: 0 on success, -1 on failure
2267 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2270 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2275 os_free(sm->ap_wpa_ie);
2276 if (ie == NULL || len == 0) {
2277 wpa_printf(MSG_DEBUG, "WPA: clearing AP WPA IE");
2278 sm->ap_wpa_ie = NULL;
2279 sm->ap_wpa_ie_len = 0;
2281 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2282 sm->ap_wpa_ie = os_malloc(len);
2283 if (sm->ap_wpa_ie == NULL)
2286 os_memcpy(sm->ap_wpa_ie, ie, len);
2287 sm->ap_wpa_ie_len = len;
2295 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2296 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2297 * @ie: Pointer to IE data (starting from id)
2299 * Returns: 0 on success, -1 on failure
2301 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2304 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2309 os_free(sm->ap_rsn_ie);
2310 if (ie == NULL || len == 0) {
2311 wpa_printf(MSG_DEBUG, "WPA: clearing AP RSN IE");
2312 sm->ap_rsn_ie = NULL;
2313 sm->ap_rsn_ie_len = 0;
2315 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2316 sm->ap_rsn_ie = os_malloc(len);
2317 if (sm->ap_rsn_ie == NULL)
2320 os_memcpy(sm->ap_rsn_ie, ie, len);
2321 sm->ap_rsn_ie_len = len;
2329 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2330 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2331 * @data: Pointer to data area for parsing results
2332 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2334 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2335 * parsed data into data.
2337 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2339 if (sm == NULL || sm->assoc_wpa_ie == NULL) {
2340 wpa_printf(MSG_DEBUG, "WPA: No WPA/RSN IE available from "
2341 "association info");
2344 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))