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 "common/ieee802_11_defs.h"
32 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
33 * @sm: Pointer to WPA state machine data from wpa_sm_init()
34 * @kck: Key Confirmation Key (KCK, part of PTK)
35 * @ver: Version field from Key Info
36 * @dest: Destination address for the frame
37 * @proto: Ethertype (usually ETH_P_EAPOL)
38 * @msg: EAPOL-Key message
39 * @msg_len: Length of message
40 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
42 void wpa_eapol_key_send(struct wpa_sm *sm, const u8 *kck,
43 int ver, const u8 *dest, u16 proto,
44 u8 *msg, size_t msg_len, u8 *key_mic)
46 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
48 * Association event was not yet received; try to fetch
49 * BSSID from the driver.
51 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
52 wpa_printf(MSG_DEBUG, "WPA: Failed to read BSSID for "
53 "EAPOL-Key destination address");
56 wpa_printf(MSG_DEBUG, "WPA: Use BSSID (" MACSTR
57 ") as the destination for EAPOL-Key",
62 wpa_eapol_key_mic(kck, ver, msg, msg_len, key_mic)) {
63 wpa_printf(MSG_ERROR, "WPA: Failed to generate EAPOL-Key "
64 "version %d MIC", ver);
67 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
68 wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
69 eapol_sm_notify_tx_eapol_key(sm->eapol);
76 * wpa_sm_key_request - Send EAPOL-Key Request
77 * @sm: Pointer to WPA state machine data from wpa_sm_init()
78 * @error: Indicate whether this is an Michael MIC error report
79 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
81 * Send an EAPOL-Key Request to the current authenticator. This function is
82 * used to request rekeying and it is usually called when a local Michael MIC
83 * failure is detected.
85 void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
88 struct wpa_eapol_key *reply;
90 u8 bssid[ETH_ALEN], *rbuf;
92 if (wpa_key_mgmt_ft(sm->key_mgmt) || wpa_key_mgmt_sha256(sm->key_mgmt))
93 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
94 else if (sm->pairwise_cipher == WPA_CIPHER_CCMP)
95 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
97 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
99 if (wpa_sm_get_bssid(sm, bssid) < 0) {
100 wpa_printf(MSG_WARNING, "Failed to read BSSID for EAPOL-Key "
105 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
106 sizeof(*reply), &rlen, (void *) &reply);
110 reply->type = sm->proto == WPA_PROTO_RSN ?
111 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
112 key_info = WPA_KEY_INFO_REQUEST | ver;
114 key_info |= WPA_KEY_INFO_MIC;
116 key_info |= WPA_KEY_INFO_ERROR;
118 key_info |= WPA_KEY_INFO_KEY_TYPE;
119 WPA_PUT_BE16(reply->key_info, key_info);
120 WPA_PUT_BE16(reply->key_length, 0);
121 os_memcpy(reply->replay_counter, sm->request_counter,
122 WPA_REPLAY_COUNTER_LEN);
123 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
125 WPA_PUT_BE16(reply->key_data_length, 0);
127 wpa_printf(MSG_INFO, "WPA: Sending EAPOL-Key Request (error=%d "
128 "pairwise=%d ptk_set=%d len=%lu)",
129 error, pairwise, sm->ptk_set, (unsigned long) rlen);
130 wpa_eapol_key_send(sm, sm->ptk.kck, ver, bssid, ETH_P_EAPOL,
131 rbuf, rlen, key_info & WPA_KEY_INFO_MIC ?
132 reply->key_mic : NULL);
136 static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
137 const unsigned char *src_addr,
140 int abort_cached = 0;
142 if (pmkid && !sm->cur_pmksa) {
143 /* When using drivers that generate RSN IE, wpa_supplicant may
144 * not have enough time to get the association information
145 * event before receiving this 1/4 message, so try to find a
146 * matching PMKSA cache entry here. */
147 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid);
149 wpa_printf(MSG_DEBUG, "RSN: found matching PMKID from "
152 wpa_printf(MSG_DEBUG, "RSN: no matching PMKID found");
157 if (pmkid && sm->cur_pmksa &&
158 os_memcmp(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
159 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
160 wpa_sm_set_pmk_from_pmksa(sm);
161 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
162 sm->pmk, sm->pmk_len);
163 eapol_sm_notify_cached(sm->eapol);
164 #ifdef CONFIG_IEEE80211R
166 #endif /* CONFIG_IEEE80211R */
167 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
170 res = eapol_sm_get_key(sm->eapol, sm->pmk, PMK_LEN);
173 * EAP-LEAP is an exception from other EAP methods: it
174 * uses only 16-byte PMK.
176 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
179 #ifdef CONFIG_IEEE80211R
181 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
183 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
184 sm->xxkey_len = PMK_LEN;
185 os_memset(buf, 0, sizeof(buf));
187 #endif /* CONFIG_IEEE80211R */
190 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
191 "machines", sm->pmk, pmk_len);
192 sm->pmk_len = pmk_len;
193 if (sm->proto == WPA_PROTO_RSN) {
194 pmksa_cache_add(sm->pmksa, sm->pmk, pmk_len,
195 src_addr, sm->own_addr,
196 sm->network_ctx, sm->key_mgmt);
198 if (!sm->cur_pmksa && pmkid &&
199 pmksa_cache_get(sm->pmksa, src_addr, pmkid)) {
200 wpa_printf(MSG_DEBUG, "RSN: the new PMK "
201 "matches with the PMKID");
205 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
206 "WPA: Failed to get master session key from "
207 "EAPOL state machines");
208 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
209 "WPA: Key handshake aborted");
211 wpa_printf(MSG_DEBUG, "RSN: Cancelled PMKSA "
213 sm->cur_pmksa = NULL;
215 } else if (!abort_cached) {
221 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) {
222 /* Send EAPOL-Start to trigger full EAP authentication. */
226 wpa_printf(MSG_DEBUG, "RSN: no PMKSA entry found - trigger "
227 "full EAP authentication");
228 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
229 NULL, 0, &buflen, NULL);
231 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
244 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
245 * @sm: Pointer to WPA state machine data from wpa_sm_init()
246 * @dst: Destination address for the frame
247 * @key: Pointer to the EAPOL-Key frame header
248 * @ver: Version bits from EAPOL-Key Key Info
249 * @nonce: Nonce value for the EAPOL-Key frame
250 * @wpa_ie: WPA/RSN IE
251 * @wpa_ie_len: Length of the WPA/RSN IE
252 * @ptk: PTK to use for keyed hash and encryption
253 * Returns: 0 on success, -1 on failure
255 int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
256 const struct wpa_eapol_key *key,
257 int ver, const u8 *nonce,
258 const u8 *wpa_ie, size_t wpa_ie_len,
262 struct wpa_eapol_key *reply;
265 if (wpa_ie == NULL) {
266 wpa_printf(MSG_WARNING, "WPA: No wpa_ie set - cannot "
271 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
273 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
274 NULL, sizeof(*reply) + wpa_ie_len,
275 &rlen, (void *) &reply);
279 reply->type = sm->proto == WPA_PROTO_RSN ?
280 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
281 WPA_PUT_BE16(reply->key_info,
282 ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
283 if (sm->proto == WPA_PROTO_RSN)
284 WPA_PUT_BE16(reply->key_length, 0);
286 os_memcpy(reply->key_length, key->key_length, 2);
287 os_memcpy(reply->replay_counter, key->replay_counter,
288 WPA_REPLAY_COUNTER_LEN);
290 WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
291 os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
293 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
295 wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
296 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
297 rbuf, rlen, reply->key_mic);
303 static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
304 const struct wpa_eapol_key *key,
307 size_t ptk_len = sm->pairwise_cipher == WPA_CIPHER_CCMP ? 48 : 64;
308 #ifdef CONFIG_IEEE80211R
309 if (wpa_key_mgmt_ft(sm->key_mgmt))
310 return wpa_derive_ptk_ft(sm, src_addr, key, ptk, ptk_len);
311 #endif /* CONFIG_IEEE80211R */
313 wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
314 sm->own_addr, sm->bssid, sm->snonce, key->key_nonce,
316 wpa_key_mgmt_sha256(sm->key_mgmt));
321 static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
322 const unsigned char *src_addr,
323 const struct wpa_eapol_key *key,
326 struct wpa_eapol_ie_parse ie;
330 if (wpa_sm_get_network_ctx(sm) == NULL) {
331 wpa_printf(MSG_WARNING, "WPA: No SSID info found (msg 1 of "
336 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
337 wpa_printf(MSG_DEBUG, "WPA: RX message 1 of 4-Way Handshake from "
338 MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
340 os_memset(&ie, 0, sizeof(ie));
342 #ifndef CONFIG_NO_WPA2
343 if (sm->proto == WPA_PROTO_RSN) {
344 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
345 const u8 *_buf = (const u8 *) (key + 1);
346 size_t len = WPA_GET_BE16(key->key_data_length);
347 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data", _buf, len);
348 wpa_supplicant_parse_ies(_buf, len, &ie);
350 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
351 "Authenticator", ie.pmkid, PMKID_LEN);
354 #endif /* CONFIG_NO_WPA2 */
356 if (wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid))
359 if (sm->renew_snonce) {
360 if (os_get_random(sm->snonce, WPA_NONCE_LEN)) {
361 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
362 "WPA: Failed to get random data for SNonce");
365 sm->renew_snonce = 0;
366 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
367 sm->snonce, WPA_NONCE_LEN);
370 /* Calculate PTK which will be stored as a temporary PTK until it has
371 * been verified when processing message 3/4. */
373 wpa_derive_ptk(sm, src_addr, key, ptk);
374 /* Supplicant: swap tx/rx Mic keys */
375 os_memcpy(buf, ptk->u.auth.tx_mic_key, 8);
376 os_memcpy(ptk->u.auth.tx_mic_key, ptk->u.auth.rx_mic_key, 8);
377 os_memcpy(ptk->u.auth.rx_mic_key, buf, 8);
380 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
381 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len,
385 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
389 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
393 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
395 struct wpa_sm *sm = eloop_ctx;
396 rsn_preauth_candidate_process(sm);
400 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
401 const u8 *addr, int secure)
403 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
404 "WPA: Key negotiation completed with "
405 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
406 wpa_cipher_txt(sm->pairwise_cipher),
407 wpa_cipher_txt(sm->group_cipher));
408 wpa_sm_cancel_auth_timeout(sm);
409 wpa_sm_set_state(sm, WPA_COMPLETED);
412 wpa_sm_mlme_setprotection(
413 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
414 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
415 eapol_sm_notify_portValid(sm->eapol, TRUE);
416 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
417 eapol_sm_notify_eap_success(sm->eapol, TRUE);
419 * Start preauthentication after a short wait to avoid a
420 * possible race condition between the data receive and key
421 * configuration after the 4-Way Handshake. This increases the
422 * likelyhood of the first preauth EAPOL-Start frame getting to
425 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
428 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
429 wpa_printf(MSG_DEBUG, "RSN: Authenticator accepted "
430 "opportunistic PMKSA entry - marking it valid");
431 sm->cur_pmksa->opportunistic = 0;
434 #ifdef CONFIG_IEEE80211R
435 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
436 /* Prepare for the next transition */
437 wpa_ft_prepare_auth_request(sm);
439 #endif /* CONFIG_IEEE80211R */
443 static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
445 struct wpa_sm *sm = eloop_ctx;
446 wpa_printf(MSG_DEBUG, "WPA: Request PTK rekeying");
447 wpa_sm_key_request(sm, 0, 1);
451 static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
452 const struct wpa_eapol_key *key)
457 u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
459 wpa_printf(MSG_DEBUG, "WPA: Installing PTK to the driver.");
461 switch (sm->pairwise_cipher) {
462 case WPA_CIPHER_CCMP:
467 case WPA_CIPHER_TKIP:
472 case WPA_CIPHER_NONE:
473 wpa_printf(MSG_DEBUG, "WPA: Pairwise Cipher Suite: "
474 "NONE - do not use pairwise keys");
477 wpa_printf(MSG_WARNING, "WPA: Unsupported pairwise cipher %d",
478 sm->pairwise_cipher);
482 if (sm->proto == WPA_PROTO_RSN) {
485 key_rsc = key->key_rsc;
486 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
489 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
490 (u8 *) sm->ptk.tk1, keylen) < 0) {
491 wpa_printf(MSG_WARNING, "WPA: Failed to set PTK to the "
492 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
493 alg, keylen, MAC2STR(sm->bssid));
497 if (sm->wpa_ptk_rekey) {
498 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
499 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
507 static int wpa_supplicant_check_group_cipher(int group_cipher,
508 int keylen, int maxkeylen,
509 int *key_rsc_len, wpa_alg *alg)
513 switch (group_cipher) {
514 case WPA_CIPHER_CCMP:
515 if (keylen != 16 || maxkeylen < 16) {
522 case WPA_CIPHER_TKIP:
523 if (keylen != 32 || maxkeylen < 32) {
530 case WPA_CIPHER_WEP104:
531 if (keylen != 13 || maxkeylen < 13) {
538 case WPA_CIPHER_WEP40:
539 if (keylen != 5 || maxkeylen < 5) {
547 wpa_printf(MSG_WARNING, "WPA: Unsupported Group Cipher %d",
553 wpa_printf(MSG_WARNING, "WPA: Unsupported %s Group Cipher key "
555 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
562 struct wpa_gtk_data {
564 int tx, key_rsc_len, keyidx;
570 static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
571 const struct wpa_gtk_data *gd,
574 const u8 *_gtk = gd->gtk;
577 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
578 wpa_printf(MSG_DEBUG, "WPA: Installing GTK to the driver "
579 "(keyidx=%d tx=%d len=%d).", gd->keyidx, gd->tx,
581 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
582 if (sm->group_cipher == WPA_CIPHER_TKIP) {
583 /* Swap Tx/Rx keys for Michael MIC */
584 os_memcpy(gtk_buf, gd->gtk, 16);
585 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
586 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
589 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
590 if (wpa_sm_set_key(sm, gd->alg,
591 (u8 *) "\xff\xff\xff\xff\xff\xff",
592 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
593 _gtk, gd->gtk_len) < 0) {
594 wpa_printf(MSG_WARNING, "WPA: Failed to set "
595 "GTK to the driver (Group only).");
598 } else if (wpa_sm_set_key(sm, gd->alg,
599 (u8 *) "\xff\xff\xff\xff\xff\xff",
600 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
601 _gtk, gd->gtk_len) < 0) {
602 wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to "
603 "the driver (alg=%d keylen=%d keyidx=%d)",
604 gd->alg, gd->gtk_len, gd->keyidx);
612 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
615 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
616 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
617 * seemed to set this bit (incorrectly, since Tx is only when
618 * doing Group Key only APs) and without this workaround, the
619 * data connection does not work because wpa_supplicant
620 * configured non-zero keyidx to be used for unicast. */
621 wpa_printf(MSG_INFO, "WPA: Tx bit set for GTK, but pairwise "
622 "keys are used - ignore Tx bit");
629 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
630 const struct wpa_eapol_key *key,
631 const u8 *gtk, size_t gtk_len,
634 #ifndef CONFIG_NO_WPA2
635 struct wpa_gtk_data gd;
638 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
640 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
641 * Reserved [bits 0-7]
645 os_memset(&gd, 0, sizeof(gd));
646 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
649 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
652 gd.keyidx = gtk[0] & 0x3;
653 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
654 !!(gtk[0] & BIT(2)));
658 os_memcpy(gd.gtk, gtk, gtk_len);
659 gd.gtk_len = gtk_len;
661 if (wpa_supplicant_check_group_cipher(sm->group_cipher,
663 &gd.key_rsc_len, &gd.alg) ||
664 wpa_supplicant_install_gtk(sm, &gd, key->key_rsc)) {
665 wpa_printf(MSG_DEBUG, "RSN: Failed to install GTK");
669 wpa_supplicant_key_neg_complete(sm, sm->bssid,
670 key_info & WPA_KEY_INFO_SECURE);
672 #else /* CONFIG_NO_WPA2 */
674 #endif /* CONFIG_NO_WPA2 */
678 static int ieee80211w_set_keys(struct wpa_sm *sm,
679 struct wpa_eapol_ie_parse *ie)
681 #ifdef CONFIG_IEEE80211W
682 if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC)
686 const struct wpa_igtk_kde *igtk;
688 if (ie->igtk_len != sizeof(*igtk))
690 igtk = (const struct wpa_igtk_kde *) ie->igtk;
691 keyidx = WPA_GET_LE16(igtk->keyid);
692 wpa_printf(MSG_DEBUG, "WPA: IGTK keyid %d "
693 "pn %02x%02x%02x%02x%02x%02x",
694 keyidx, MAC2STR(igtk->pn));
695 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
696 igtk->igtk, WPA_IGTK_LEN);
698 wpa_printf(MSG_WARNING, "WPA: Invalid IGTK KeyID %d",
702 if (wpa_sm_set_key(sm, WPA_ALG_IGTK,
703 (u8 *) "\xff\xff\xff\xff\xff\xff",
704 keyidx, 0, igtk->pn, sizeof(igtk->pn),
705 igtk->igtk, WPA_IGTK_LEN) < 0) {
706 wpa_printf(MSG_WARNING, "WPA: Failed to configure IGTK"
713 #else /* CONFIG_IEEE80211W */
715 #endif /* CONFIG_IEEE80211W */
719 static void wpa_report_ie_mismatch(struct wpa_sm *sm,
720 const char *reason, const u8 *src_addr,
721 const u8 *wpa_ie, size_t wpa_ie_len,
722 const u8 *rsn_ie, size_t rsn_ie_len)
724 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
725 reason, MAC2STR(src_addr));
728 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
729 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
732 if (!sm->ap_wpa_ie) {
733 wpa_printf(MSG_INFO, "WPA: No WPA IE in "
736 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
741 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
742 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
745 if (!sm->ap_rsn_ie) {
746 wpa_printf(MSG_INFO, "WPA: No RSN IE in "
749 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
753 wpa_sm_disassociate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
757 static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
758 const unsigned char *src_addr,
759 struct wpa_eapol_ie_parse *ie)
761 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
762 wpa_printf(MSG_DEBUG, "WPA: No WPA/RSN IE for this AP known. "
763 "Trying to get from scan results");
764 if (wpa_sm_get_beacon_ie(sm) < 0) {
765 wpa_printf(MSG_WARNING, "WPA: Could not find AP from "
768 wpa_printf(MSG_DEBUG, "WPA: Found the current AP from "
769 "updated scan results");
773 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
774 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
775 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
776 "with IE in Beacon/ProbeResp (no IE?)",
777 src_addr, ie->wpa_ie, ie->wpa_ie_len,
778 ie->rsn_ie, ie->rsn_ie_len);
782 if ((ie->wpa_ie && sm->ap_wpa_ie &&
783 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
784 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
785 (ie->rsn_ie && sm->ap_rsn_ie &&
786 (ie->rsn_ie_len != sm->ap_rsn_ie_len ||
787 os_memcmp(ie->rsn_ie, sm->ap_rsn_ie, ie->rsn_ie_len) != 0))) {
788 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
789 "with IE in Beacon/ProbeResp",
790 src_addr, ie->wpa_ie, ie->wpa_ie_len,
791 ie->rsn_ie, ie->rsn_ie_len);
795 if (sm->proto == WPA_PROTO_WPA &&
796 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
797 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
798 "detected - RSN was enabled and RSN IE "
799 "was in msg 3/4, but not in "
801 src_addr, ie->wpa_ie, ie->wpa_ie_len,
802 ie->rsn_ie, ie->rsn_ie_len);
806 #ifdef CONFIG_IEEE80211R
807 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
808 struct rsn_mdie *mdie;
809 /* TODO: verify that full MDIE matches with the one from scan
810 * results, not only mobility domain */
811 mdie = (struct rsn_mdie *) (ie->mdie + 2);
812 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
813 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
814 MOBILITY_DOMAIN_ID_LEN) != 0) {
815 wpa_printf(MSG_DEBUG, "FT: MDIE in msg 3/4 did not "
816 "match with the current mobility domain");
820 #endif /* CONFIG_IEEE80211R */
827 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
828 * @sm: Pointer to WPA state machine data from wpa_sm_init()
829 * @dst: Destination address for the frame
830 * @key: Pointer to the EAPOL-Key frame header
831 * @ver: Version bits from EAPOL-Key Key Info
832 * @key_info: Key Info
833 * @kde: KDEs to include the EAPOL-Key frame
834 * @kde_len: Length of KDEs
835 * @ptk: PTK to use for keyed hash and encryption
836 * Returns: 0 on success, -1 on failure
838 int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
839 const struct wpa_eapol_key *key,
840 u16 ver, u16 key_info,
841 const u8 *kde, size_t kde_len,
845 struct wpa_eapol_key *reply;
849 wpa_hexdump(MSG_DEBUG, "WPA: KDE for msg 4/4", kde, kde_len);
851 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
852 sizeof(*reply) + kde_len,
853 &rlen, (void *) &reply);
857 reply->type = sm->proto == WPA_PROTO_RSN ?
858 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
859 key_info &= WPA_KEY_INFO_SECURE;
860 key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
861 WPA_PUT_BE16(reply->key_info, key_info);
862 if (sm->proto == WPA_PROTO_RSN)
863 WPA_PUT_BE16(reply->key_length, 0);
865 os_memcpy(reply->key_length, key->key_length, 2);
866 os_memcpy(reply->replay_counter, key->replay_counter,
867 WPA_REPLAY_COUNTER_LEN);
869 WPA_PUT_BE16(reply->key_data_length, kde_len);
871 os_memcpy(reply + 1, kde, kde_len);
873 wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
874 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
875 rbuf, rlen, reply->key_mic);
881 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
882 const struct wpa_eapol_key *key,
885 u16 key_info, keylen, len;
887 struct wpa_eapol_ie_parse ie;
889 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
890 wpa_printf(MSG_DEBUG, "WPA: RX message 3 of 4-Way Handshake from "
891 MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
893 key_info = WPA_GET_BE16(key->key_info);
895 pos = (const u8 *) (key + 1);
896 len = WPA_GET_BE16(key->key_data_length);
897 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", pos, len);
898 wpa_supplicant_parse_ies(pos, len, &ie);
899 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
900 wpa_printf(MSG_WARNING, "WPA: GTK IE in unencrypted key data");
903 #ifdef CONFIG_IEEE80211W
904 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
905 wpa_printf(MSG_WARNING, "WPA: IGTK KDE in unencrypted key "
910 if (ie.igtk && ie.igtk_len != sizeof(struct wpa_igtk_kde)) {
911 wpa_printf(MSG_WARNING, "WPA: Invalid IGTK KDE length %lu",
912 (unsigned long) ie.igtk_len);
915 #endif /* CONFIG_IEEE80211W */
917 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
920 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
921 wpa_printf(MSG_WARNING, "WPA: ANonce from message 1 of 4-Way "
922 "Handshake differs from 3 of 4-Way Handshake - drop"
923 " packet (src=" MACSTR ")", MAC2STR(sm->bssid));
927 keylen = WPA_GET_BE16(key->key_length);
928 switch (sm->pairwise_cipher) {
929 case WPA_CIPHER_CCMP:
931 wpa_printf(MSG_WARNING, "WPA: Invalid CCMP key length "
932 "%d (src=" MACSTR ")",
933 keylen, MAC2STR(sm->bssid));
937 case WPA_CIPHER_TKIP:
939 wpa_printf(MSG_WARNING, "WPA: Invalid TKIP key length "
940 "%d (src=" MACSTR ")",
941 keylen, MAC2STR(sm->bssid));
947 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
948 NULL, 0, &sm->ptk)) {
952 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
953 * for the next 4-Way Handshake. If msg 3 is received again, the old
954 * SNonce will still be used to avoid changing PTK. */
955 sm->renew_snonce = 1;
957 if (key_info & WPA_KEY_INFO_INSTALL) {
958 if (wpa_supplicant_install_ptk(sm, key))
962 if (key_info & WPA_KEY_INFO_SECURE) {
963 wpa_sm_mlme_setprotection(
964 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
965 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
966 eapol_sm_notify_portValid(sm->eapol, TRUE);
968 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
971 wpa_supplicant_pairwise_gtk(sm, key,
972 ie.gtk, ie.gtk_len, key_info) < 0) {
973 wpa_printf(MSG_INFO, "RSN: Failed to configure GTK");
977 if (ieee80211w_set_keys(sm, &ie) < 0) {
978 wpa_printf(MSG_INFO, "RSN: Failed to configure IGTK");
985 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
989 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
993 struct wpa_gtk_data *gd)
996 struct wpa_eapol_ie_parse ie;
998 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
999 wpa_supplicant_parse_ies(keydata, keydatalen, &ie);
1000 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1001 wpa_printf(MSG_WARNING, "WPA: GTK IE in unencrypted key data");
1004 if (ie.gtk == NULL) {
1005 wpa_printf(MSG_INFO, "WPA: No GTK IE in Group Key msg 1/2");
1008 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1010 if (wpa_supplicant_check_group_cipher(sm->group_cipher,
1011 gd->gtk_len, maxkeylen,
1012 &gd->key_rsc_len, &gd->alg))
1015 wpa_hexdump(MSG_DEBUG, "RSN: received GTK in group key handshake",
1016 ie.gtk, ie.gtk_len);
1017 gd->keyidx = ie.gtk[0] & 0x3;
1018 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1019 !!(ie.gtk[0] & BIT(2)));
1020 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1021 wpa_printf(MSG_INFO, "RSN: Too long GTK in GTK IE "
1022 "(len=%lu)", (unsigned long) ie.gtk_len - 2);
1025 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1027 if (ieee80211w_set_keys(sm, &ie) < 0)
1028 wpa_printf(MSG_INFO, "RSN: Failed to configure IGTK");
1034 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1035 const struct wpa_eapol_key *key,
1036 size_t keydatalen, int key_info,
1037 size_t extra_len, u16 ver,
1038 struct wpa_gtk_data *gd)
1043 gd->gtk_len = WPA_GET_BE16(key->key_length);
1044 maxkeylen = keydatalen;
1045 if (keydatalen > extra_len) {
1046 wpa_printf(MSG_INFO, "WPA: Truncated EAPOL-Key packet:"
1047 " key_data_length=%lu > extra_len=%lu",
1048 (unsigned long) keydatalen,
1049 (unsigned long) extra_len);
1052 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1053 if (maxkeylen < 8) {
1054 wpa_printf(MSG_INFO, "WPA: Too short maxkeylen (%lu)",
1055 (unsigned long) maxkeylen);
1061 if (wpa_supplicant_check_group_cipher(sm->group_cipher,
1062 gd->gtk_len, maxkeylen,
1063 &gd->key_rsc_len, &gd->alg))
1066 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1067 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1068 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1069 os_memcpy(ek, key->key_iv, 16);
1070 os_memcpy(ek + 16, sm->ptk.kek, 16);
1071 if (keydatalen > sizeof(gd->gtk)) {
1072 wpa_printf(MSG_WARNING, "WPA: RC4 key data "
1074 (unsigned long) keydatalen);
1077 os_memcpy(gd->gtk, key + 1, keydatalen);
1078 if (rc4_skip(ek, 32, 256, gd->gtk, keydatalen)) {
1079 wpa_printf(MSG_ERROR, "WPA: RC4 failed");
1082 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1083 if (keydatalen % 8) {
1084 wpa_printf(MSG_WARNING, "WPA: Unsupported AES-WRAP "
1085 "len %lu", (unsigned long) keydatalen);
1088 if (maxkeylen > sizeof(gd->gtk)) {
1089 wpa_printf(MSG_WARNING, "WPA: AES-WRAP key data "
1090 "too long (keydatalen=%lu maxkeylen=%lu)",
1091 (unsigned long) keydatalen,
1092 (unsigned long) maxkeylen);
1095 if (aes_unwrap(sm->ptk.kek, maxkeylen / 8,
1096 (const u8 *) (key + 1), gd->gtk)) {
1097 wpa_printf(MSG_WARNING, "WPA: AES unwrap "
1098 "failed - could not decrypt GTK");
1102 wpa_printf(MSG_WARNING, "WPA: Unsupported key_info type %d",
1106 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1107 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1112 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1113 const struct wpa_eapol_key *key,
1114 int ver, u16 key_info)
1117 struct wpa_eapol_key *reply;
1120 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1121 sizeof(*reply), &rlen, (void *) &reply);
1125 reply->type = sm->proto == WPA_PROTO_RSN ?
1126 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1127 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1128 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1129 WPA_PUT_BE16(reply->key_info, key_info);
1130 if (sm->proto == WPA_PROTO_RSN)
1131 WPA_PUT_BE16(reply->key_length, 0);
1133 os_memcpy(reply->key_length, key->key_length, 2);
1134 os_memcpy(reply->replay_counter, key->replay_counter,
1135 WPA_REPLAY_COUNTER_LEN);
1137 WPA_PUT_BE16(reply->key_data_length, 0);
1139 wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1140 wpa_eapol_key_send(sm, sm->ptk.kck, ver, sm->bssid, ETH_P_EAPOL,
1141 rbuf, rlen, reply->key_mic);
1147 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1148 const unsigned char *src_addr,
1149 const struct wpa_eapol_key *key,
1150 int extra_len, u16 ver)
1152 u16 key_info, keydatalen;
1154 struct wpa_gtk_data gd;
1156 os_memset(&gd, 0, sizeof(gd));
1158 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1159 wpa_printf(MSG_DEBUG, "WPA: RX message 1 of Group Key Handshake from "
1160 MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1162 key_info = WPA_GET_BE16(key->key_info);
1163 keydatalen = WPA_GET_BE16(key->key_data_length);
1165 if (sm->proto == WPA_PROTO_RSN) {
1166 ret = wpa_supplicant_process_1_of_2_rsn(sm,
1167 (const u8 *) (key + 1),
1168 keydatalen, key_info,
1171 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, keydatalen,
1172 key_info, extra_len,
1176 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1181 if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc) ||
1182 wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
1186 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1187 "completed with " MACSTR " [GTK=%s]",
1188 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1189 wpa_sm_cancel_auth_timeout(sm);
1190 wpa_sm_set_state(sm, WPA_COMPLETED);
1192 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1194 WPA_KEY_INFO_SECURE);
1199 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1203 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1204 struct wpa_eapol_key *key,
1206 const u8 *buf, size_t len)
1211 os_memcpy(mic, key->key_mic, 16);
1213 os_memset(key->key_mic, 0, 16);
1214 wpa_eapol_key_mic(sm->tptk.kck, ver, buf, len,
1216 if (os_memcmp(mic, key->key_mic, 16) != 0) {
1217 wpa_printf(MSG_WARNING, "WPA: Invalid EAPOL-Key MIC "
1218 "when using TPTK - ignoring TPTK");
1223 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1227 if (!ok && sm->ptk_set) {
1228 os_memset(key->key_mic, 0, 16);
1229 wpa_eapol_key_mic(sm->ptk.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 "- dropping packet");
1240 wpa_printf(MSG_WARNING, "WPA: Could not verify EAPOL-Key MIC "
1241 "- dropping packet");
1245 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1246 WPA_REPLAY_COUNTER_LEN);
1247 sm->rx_replay_counter_set = 1;
1252 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1253 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
1254 struct wpa_eapol_key *key, u16 ver)
1256 u16 keydatalen = WPA_GET_BE16(key->key_data_length);
1258 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
1259 (u8 *) (key + 1), keydatalen);
1261 wpa_printf(MSG_WARNING, "WPA: PTK not available, "
1262 "cannot decrypt EAPOL-Key key data.");
1266 /* Decrypt key data here so that this operation does not need
1267 * to be implemented separately for each message type. */
1268 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1270 os_memcpy(ek, key->key_iv, 16);
1271 os_memcpy(ek + 16, sm->ptk.kek, 16);
1272 if (rc4_skip(ek, 32, 256, (u8 *) (key + 1), keydatalen)) {
1273 wpa_printf(MSG_ERROR, "WPA: RC4 failed");
1276 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1277 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1279 if (keydatalen % 8) {
1280 wpa_printf(MSG_WARNING, "WPA: Unsupported "
1281 "AES-WRAP len %d", keydatalen);
1284 keydatalen -= 8; /* AES-WRAP adds 8 bytes */
1285 buf = os_malloc(keydatalen);
1287 wpa_printf(MSG_WARNING, "WPA: No memory for "
1288 "AES-UNWRAP buffer");
1291 if (aes_unwrap(sm->ptk.kek, keydatalen / 8,
1292 (u8 *) (key + 1), buf)) {
1294 wpa_printf(MSG_WARNING, "WPA: AES unwrap failed - "
1295 "could not decrypt EAPOL-Key key data");
1298 os_memcpy(key + 1, buf, keydatalen);
1300 WPA_PUT_BE16(key->key_data_length, keydatalen);
1302 wpa_printf(MSG_WARNING, "WPA: Unsupported key_info type %d",
1306 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
1307 (u8 *) (key + 1), keydatalen);
1313 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1314 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1316 void wpa_sm_aborted_cached(struct wpa_sm *sm)
1318 if (sm && sm->cur_pmksa) {
1319 wpa_printf(MSG_DEBUG, "RSN: Cancelling PMKSA caching attempt");
1320 sm->cur_pmksa = NULL;
1325 static void wpa_eapol_key_dump(const struct wpa_eapol_key *key)
1327 #ifndef CONFIG_NO_STDOUT_DEBUG
1328 u16 key_info = WPA_GET_BE16(key->key_info);
1330 wpa_printf(MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1331 wpa_printf(MSG_DEBUG, " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s"
1333 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1334 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1335 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1336 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1337 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1338 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1339 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1340 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1341 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1342 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1343 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1344 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1345 wpa_printf(MSG_DEBUG, " key_length=%u key_data_length=%u",
1346 WPA_GET_BE16(key->key_length),
1347 WPA_GET_BE16(key->key_data_length));
1348 wpa_hexdump(MSG_DEBUG, " replay_counter",
1349 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1350 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1351 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1352 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1353 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
1354 wpa_hexdump(MSG_DEBUG, " key_mic", key->key_mic, 16);
1355 #endif /* CONFIG_NO_STDOUT_DEBUG */
1360 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1361 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1362 * @src_addr: Source MAC address of the EAPOL packet
1363 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1364 * @len: Length of the EAPOL frame
1365 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1367 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1368 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1369 * only processing WPA and WPA2 EAPOL-Key frames.
1371 * The received EAPOL-Key packets are validated and valid packets are replied
1372 * to. In addition, key material (PTK, GTK) is configured at the end of a
1373 * successful key handshake.
1375 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1376 const u8 *buf, size_t len)
1378 size_t plen, data_len, extra_len;
1379 struct ieee802_1x_hdr *hdr;
1380 struct wpa_eapol_key *key;
1384 struct wpa_peerkey *peerkey = NULL;
1386 #ifdef CONFIG_IEEE80211R
1387 sm->ft_completed = 0;
1388 #endif /* CONFIG_IEEE80211R */
1390 if (len < sizeof(*hdr) + sizeof(*key)) {
1391 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame too short to be a WPA "
1392 "EAPOL-Key (len %lu, expecting at least %lu)",
1393 (unsigned long) len,
1394 (unsigned long) sizeof(*hdr) + sizeof(*key));
1398 tmp = os_malloc(len);
1401 os_memcpy(tmp, buf, len);
1403 hdr = (struct ieee802_1x_hdr *) tmp;
1404 key = (struct wpa_eapol_key *) (hdr + 1);
1405 plen = be_to_host16(hdr->length);
1406 data_len = plen + sizeof(*hdr);
1407 wpa_printf(MSG_DEBUG, "IEEE 802.1X RX: version=%d type=%d length=%lu",
1408 hdr->version, hdr->type, (unsigned long) plen);
1410 if (hdr->version < EAPOL_VERSION) {
1411 /* TODO: backwards compatibility */
1413 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1414 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame (type %u) discarded, "
1415 "not a Key frame", hdr->type);
1419 if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
1420 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame payload size %lu "
1421 "invalid (frame size %lu)",
1422 (unsigned long) plen, (unsigned long) len);
1427 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1429 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key type (%d) unknown, "
1430 "discarded", key->type);
1434 wpa_eapol_key_dump(key);
1436 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
1437 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", tmp, len);
1438 if (data_len < len) {
1439 wpa_printf(MSG_DEBUG, "WPA: ignoring %lu bytes after the IEEE "
1440 "802.1X data", (unsigned long) len - data_len);
1442 key_info = WPA_GET_BE16(key->key_info);
1443 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1444 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1445 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1446 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1447 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
1448 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1449 wpa_printf(MSG_INFO, "WPA: Unsupported EAPOL-Key descriptor "
1450 "version %d.", ver);
1454 #ifdef CONFIG_IEEE80211R
1455 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1456 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1457 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1458 wpa_printf(MSG_INFO, "FT: AP did not use "
1463 #endif /* CONFIG_IEEE80211R */
1464 #ifdef CONFIG_IEEE80211W
1465 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
1466 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1467 wpa_printf(MSG_INFO, "WPA: AP did not use the "
1468 "negotiated AES-128-CMAC.");
1472 #endif /* CONFIG_IEEE80211W */
1473 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
1474 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1475 wpa_printf(MSG_INFO, "WPA: CCMP is used, but EAPOL-Key "
1476 "descriptor version (%d) is not 2.", ver);
1477 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1478 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1479 /* Earlier versions of IEEE 802.11i did not explicitly
1480 * require version 2 descriptor for all EAPOL-Key
1481 * packets, so allow group keys to use version 1 if
1482 * CCMP is not used for them. */
1483 wpa_printf(MSG_INFO, "WPA: Backwards compatibility: "
1484 "allow invalid version for non-CCMP group "
1490 #ifdef CONFIG_PEERKEY
1491 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1492 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1496 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1497 if (!peerkey->initiator && peerkey->replay_counter_set &&
1498 os_memcmp(key->replay_counter, peerkey->replay_counter,
1499 WPA_REPLAY_COUNTER_LEN) <= 0) {
1500 wpa_printf(MSG_WARNING, "RSN: EAPOL-Key Replay "
1501 "Counter did not increase (STK) - dropping "
1504 } else if (peerkey->initiator) {
1505 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1506 os_memcpy(_tmp, key->replay_counter,
1507 WPA_REPLAY_COUNTER_LEN);
1508 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
1509 if (os_memcmp(_tmp, peerkey->replay_counter,
1510 WPA_REPLAY_COUNTER_LEN) != 0) {
1511 wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key Replay "
1512 "Counter did not match (STK) - "
1519 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
1520 wpa_printf(MSG_INFO, "RSN: Ack bit in key_info from STK peer");
1523 #endif /* CONFIG_PEERKEY */
1525 if (!peerkey && sm->rx_replay_counter_set &&
1526 os_memcmp(key->replay_counter, sm->rx_replay_counter,
1527 WPA_REPLAY_COUNTER_LEN) <= 0) {
1528 wpa_printf(MSG_WARNING, "WPA: EAPOL-Key Replay Counter did not"
1529 " increase - dropping packet");
1533 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
1534 #ifdef CONFIG_PEERKEY
1535 && (peerkey == NULL || !peerkey->initiator)
1536 #endif /* CONFIG_PEERKEY */
1538 wpa_printf(MSG_INFO, "WPA: No Ack bit in key_info");
1542 if (key_info & WPA_KEY_INFO_REQUEST) {
1543 wpa_printf(MSG_INFO, "WPA: EAPOL-Key with Request bit - "
1548 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
1549 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
1552 #ifdef CONFIG_PEERKEY
1553 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
1554 peerkey_verify_eapol_key_mic(sm, peerkey, key, ver, tmp, data_len))
1556 #endif /* CONFIG_PEERKEY */
1558 extra_len = data_len - sizeof(*hdr) - sizeof(*key);
1560 if (WPA_GET_BE16(key->key_data_length) > extra_len) {
1561 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1562 "frame - key_data overflow (%d > %lu)",
1563 WPA_GET_BE16(key->key_data_length),
1564 (unsigned long) extra_len);
1567 extra_len = WPA_GET_BE16(key->key_data_length);
1569 if (sm->proto == WPA_PROTO_RSN &&
1570 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1571 if (wpa_supplicant_decrypt_key_data(sm, key, ver))
1573 extra_len = WPA_GET_BE16(key->key_data_length);
1576 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1577 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
1578 wpa_printf(MSG_WARNING, "WPA: Ignored EAPOL-Key "
1579 "(Pairwise) with non-zero key index");
1583 /* PeerKey 4-Way Handshake */
1584 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver);
1585 } else if (key_info & WPA_KEY_INFO_MIC) {
1586 /* 3/4 4-Way Handshake */
1587 wpa_supplicant_process_3_of_4(sm, key, ver);
1589 /* 1/4 4-Way Handshake */
1590 wpa_supplicant_process_1_of_4(sm, src_addr, key,
1593 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1594 /* PeerKey SMK Handshake */
1595 peerkey_rx_eapol_smk(sm, src_addr, key, extra_len, key_info,
1598 if (key_info & WPA_KEY_INFO_MIC) {
1599 /* 1/2 Group Key Handshake */
1600 wpa_supplicant_process_1_of_2(sm, src_addr, key,
1603 wpa_printf(MSG_WARNING, "WPA: EAPOL-Key (Group) "
1604 "without Mic bit - dropped");
1616 #ifdef CONFIG_CTRL_IFACE
1617 static int wpa_cipher_bits(int cipher)
1620 case WPA_CIPHER_CCMP:
1622 case WPA_CIPHER_TKIP:
1624 case WPA_CIPHER_WEP104:
1626 case WPA_CIPHER_WEP40:
1634 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
1636 switch (sm->key_mgmt) {
1637 case WPA_KEY_MGMT_IEEE8021X:
1638 return (sm->proto == WPA_PROTO_RSN ?
1639 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
1640 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
1641 case WPA_KEY_MGMT_PSK:
1642 return (sm->proto == WPA_PROTO_RSN ?
1643 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
1644 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
1645 #ifdef CONFIG_IEEE80211R
1646 case WPA_KEY_MGMT_FT_IEEE8021X:
1647 return RSN_AUTH_KEY_MGMT_FT_802_1X;
1648 case WPA_KEY_MGMT_FT_PSK:
1649 return RSN_AUTH_KEY_MGMT_FT_PSK;
1650 #endif /* CONFIG_IEEE80211R */
1651 #ifdef CONFIG_IEEE80211W
1652 case WPA_KEY_MGMT_IEEE8021X_SHA256:
1653 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
1654 case WPA_KEY_MGMT_PSK_SHA256:
1655 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
1656 #endif /* CONFIG_IEEE80211W */
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 ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
1715 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
1716 sm->proto == WPA_PROTO_RSN)
1721 ret = os_snprintf(buf, buflen,
1722 "dot11RSNAOptionImplemented=TRUE\n"
1723 "dot11RSNAPreauthenticationImplemented=TRUE\n"
1724 "dot11RSNAEnabled=%s\n"
1725 "dot11RSNAPreauthenticationEnabled=%s\n"
1726 "dot11RSNAConfigVersion=%d\n"
1727 "dot11RSNAConfigPairwiseKeysSupported=5\n"
1728 "dot11RSNAConfigGroupCipherSize=%d\n"
1729 "dot11RSNAConfigPMKLifetime=%d\n"
1730 "dot11RSNAConfigPMKReauthThreshold=%d\n"
1731 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
1732 "dot11RSNAConfigSATimeout=%d\n",
1733 rsna ? "TRUE" : "FALSE",
1734 rsna ? "TRUE" : "FALSE",
1736 wpa_cipher_bits(sm->group_cipher),
1737 sm->dot11RSNAConfigPMKLifetime,
1738 sm->dot11RSNAConfigPMKReauthThreshold,
1739 sm->dot11RSNAConfigSATimeout);
1740 if (ret < 0 || (size_t) ret >= buflen)
1745 buf + len, buflen - len,
1746 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
1747 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
1748 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
1749 "dot11RSNAPMKIDUsed=%s\n"
1750 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
1751 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
1752 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
1753 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
1754 "dot11RSNA4WayHandshakeFailures=%u\n",
1755 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1756 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->pairwise_cipher)),
1757 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->group_cipher)),
1759 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1760 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->pairwise_cipher)),
1761 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->group_cipher)),
1762 sm->dot11RSNA4WayHandshakeFailures);
1763 if (ret >= 0 && (size_t) ret < buflen)
1768 #endif /* CONFIG_CTRL_IFACE */
1771 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
1772 void *ctx, int replace)
1774 struct wpa_sm *sm = ctx;
1776 if (sm->cur_pmksa == entry ||
1777 (sm->pmk_len == entry->pmk_len &&
1778 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
1779 wpa_printf(MSG_DEBUG, "RSN: removed current PMKSA entry");
1780 sm->cur_pmksa = NULL;
1783 /* A new entry is being added, so no need to
1784 * deauthenticate in this case. This happens when EAP
1785 * authentication is completed again (reauth or failed
1786 * PMKSA caching attempt). */
1790 os_memset(sm->pmk, 0, sizeof(sm->pmk));
1791 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1797 * wpa_sm_init - Initialize WPA state machine
1798 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
1799 * Returns: Pointer to the allocated WPA state machine data
1801 * This function is used to allocate a new WPA state machine and the returned
1802 * value is passed to all WPA state machine calls.
1804 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
1808 sm = os_zalloc(sizeof(*sm));
1811 sm->renew_snonce = 1;
1814 sm->dot11RSNAConfigPMKLifetime = 43200;
1815 sm->dot11RSNAConfigPMKReauthThreshold = 70;
1816 sm->dot11RSNAConfigSATimeout = 60;
1818 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
1819 if (sm->pmksa == NULL) {
1820 wpa_printf(MSG_ERROR, "RSN: PMKSA cache initialization "
1831 * wpa_sm_deinit - Deinitialize WPA state machine
1832 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1834 void wpa_sm_deinit(struct wpa_sm *sm)
1838 pmksa_cache_deinit(sm->pmksa);
1839 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
1840 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
1841 os_free(sm->assoc_wpa_ie);
1842 os_free(sm->ap_wpa_ie);
1843 os_free(sm->ap_rsn_ie);
1851 * wpa_sm_notify_assoc - Notify WPA state machine about association
1852 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1853 * @bssid: The BSSID of the new association
1855 * This function is called to let WPA state machine know that the connection
1858 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 (wpa_ft_is_completed(sm)) {
1875 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
1877 /* Prepare for the next transition */
1878 wpa_ft_prepare_auth_request(sm);
1882 #endif /* CONFIG_IEEE80211R */
1886 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
1887 * this is not part of a Fast BSS Transition.
1889 wpa_printf(MSG_DEBUG, "WPA: Clear old PTK");
1897 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
1898 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1900 * This function is called to let WPA state machine know that the connection
1901 * was lost. This will abort any existing pre-authentication session.
1903 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
1905 rsn_preauth_deinit(sm);
1906 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
1907 sm->dot11RSNA4WayHandshakeFailures++;
1912 * wpa_sm_set_pmk - Set PMK
1913 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1915 * @pmk_len: The length of the new PMK in bytes
1917 * Configure the PMK for WPA state machine.
1919 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len)
1924 sm->pmk_len = pmk_len;
1925 os_memcpy(sm->pmk, pmk, pmk_len);
1927 #ifdef CONFIG_IEEE80211R
1928 /* Set XXKey to be PSK for FT key derivation */
1929 sm->xxkey_len = pmk_len;
1930 os_memcpy(sm->xxkey, pmk, pmk_len);
1931 #endif /* CONFIG_IEEE80211R */
1936 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
1937 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1939 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
1942 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
1947 if (sm->cur_pmksa) {
1948 sm->pmk_len = sm->cur_pmksa->pmk_len;
1949 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
1951 sm->pmk_len = PMK_LEN;
1952 os_memset(sm->pmk, 0, PMK_LEN);
1958 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
1959 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1960 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
1962 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
1965 sm->fast_reauth = fast_reauth;
1970 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
1971 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1972 * @scard_ctx: Context pointer for smartcard related callback functions
1974 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
1978 sm->scard_ctx = scard_ctx;
1979 if (sm->preauth_eapol)
1980 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
1985 * wpa_sm_set_config - Notification of current configration change
1986 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1987 * @config: Pointer to current network configuration
1989 * Notify WPA state machine that configuration has changed. config will be
1990 * stored as a backpointer to network configuration. This can be %NULL to clear
1991 * the stored pointed.
1993 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
1999 sm->network_ctx = config->network_ctx;
2000 sm->peerkey_enabled = config->peerkey_enabled;
2001 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2002 sm->proactive_key_caching = config->proactive_key_caching;
2003 sm->eap_workaround = config->eap_workaround;
2004 sm->eap_conf_ctx = config->eap_conf_ctx;
2006 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2007 sm->ssid_len = config->ssid_len;
2010 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
2012 sm->network_ctx = NULL;
2013 sm->peerkey_enabled = 0;
2014 sm->allowed_pairwise_cipher = 0;
2015 sm->proactive_key_caching = 0;
2016 sm->eap_workaround = 0;
2017 sm->eap_conf_ctx = NULL;
2019 sm->wpa_ptk_rekey = 0;
2021 if (config == NULL || config->network_ctx != sm->network_ctx)
2022 pmksa_cache_notify_reconfig(sm->pmksa);
2027 * wpa_sm_set_own_addr - Set own MAC address
2028 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2029 * @addr: Own MAC address
2031 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2034 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2039 * wpa_sm_set_ifname - Set network interface name
2040 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2041 * @ifname: Interface name
2042 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2044 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2045 const char *bridge_ifname)
2048 sm->ifname = ifname;
2049 sm->bridge_ifname = bridge_ifname;
2055 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2056 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2057 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2059 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2067 * wpa_sm_set_param - Set WPA state machine parameters
2068 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2069 * @param: Parameter field
2070 * @value: Parameter value
2071 * Returns: 0 on success, -1 on failure
2073 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2082 case RSNA_PMK_LIFETIME:
2084 sm->dot11RSNAConfigPMKLifetime = value;
2088 case RSNA_PMK_REAUTH_THRESHOLD:
2089 if (value > 0 && value <= 100)
2090 sm->dot11RSNAConfigPMKReauthThreshold = value;
2094 case RSNA_SA_TIMEOUT:
2096 sm->dot11RSNAConfigSATimeout = value;
2100 case WPA_PARAM_PROTO:
2103 case WPA_PARAM_PAIRWISE:
2104 sm->pairwise_cipher = value;
2106 case WPA_PARAM_GROUP:
2107 sm->group_cipher = value;
2109 case WPA_PARAM_KEY_MGMT:
2110 sm->key_mgmt = value;
2112 #ifdef CONFIG_IEEE80211W
2113 case WPA_PARAM_MGMT_GROUP:
2114 sm->mgmt_group_cipher = value;
2116 #endif /* CONFIG_IEEE80211W */
2117 case WPA_PARAM_RSN_ENABLED:
2118 sm->rsn_enabled = value;
2129 * wpa_sm_get_param - Get WPA state machine parameters
2130 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2131 * @param: Parameter field
2132 * Returns: Parameter value
2134 unsigned int wpa_sm_get_param(struct wpa_sm *sm, enum wpa_sm_conf_params param)
2140 case RSNA_PMK_LIFETIME:
2141 return sm->dot11RSNAConfigPMKLifetime;
2142 case RSNA_PMK_REAUTH_THRESHOLD:
2143 return sm->dot11RSNAConfigPMKReauthThreshold;
2144 case RSNA_SA_TIMEOUT:
2145 return sm->dot11RSNAConfigSATimeout;
2146 case WPA_PARAM_PROTO:
2148 case WPA_PARAM_PAIRWISE:
2149 return sm->pairwise_cipher;
2150 case WPA_PARAM_GROUP:
2151 return sm->group_cipher;
2152 case WPA_PARAM_KEY_MGMT:
2153 return sm->key_mgmt;
2154 #ifdef CONFIG_IEEE80211W
2155 case WPA_PARAM_MGMT_GROUP:
2156 return sm->mgmt_group_cipher;
2157 #endif /* CONFIG_IEEE80211W */
2158 case WPA_PARAM_RSN_ENABLED:
2159 return sm->rsn_enabled;
2167 * wpa_sm_get_status - Get WPA state machine
2168 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2169 * @buf: Buffer for status information
2170 * @buflen: Maximum buffer length
2171 * @verbose: Whether to include verbose status information
2172 * Returns: Number of bytes written to buf.
2174 * Query WPA state machine for status information. This function fills in
2175 * a text area with current status information. If the buffer (buf) is not
2176 * large enough, status information will be truncated to fit the buffer.
2178 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2181 char *pos = buf, *end = buf + buflen;
2184 ret = os_snprintf(pos, end - pos,
2185 "pairwise_cipher=%s\n"
2188 wpa_cipher_txt(sm->pairwise_cipher),
2189 wpa_cipher_txt(sm->group_cipher),
2190 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
2191 if (ret < 0 || ret >= end - pos)
2199 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2200 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2201 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2202 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2203 * Returns: 0 on success, -1 on failure
2205 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2213 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2218 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2219 wpa_ie, *wpa_ie_len);
2221 if (sm->assoc_wpa_ie == NULL) {
2223 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2224 * the correct version of the IE even if PMKSA caching is
2225 * aborted (which would remove PMKID from IE generation).
2227 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2228 if (sm->assoc_wpa_ie == NULL)
2231 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2232 sm->assoc_wpa_ie_len = *wpa_ie_len;
2240 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2241 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2242 * @ie: Pointer to IE data (starting from id)
2244 * Returns: 0 on success, -1 on failure
2246 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2247 * Request frame. The IE will be used to override the default value generated
2248 * with wpa_sm_set_assoc_wpa_ie_default().
2250 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2255 os_free(sm->assoc_wpa_ie);
2256 if (ie == NULL || len == 0) {
2257 wpa_printf(MSG_DEBUG, "WPA: clearing own WPA/RSN IE");
2258 sm->assoc_wpa_ie = NULL;
2259 sm->assoc_wpa_ie_len = 0;
2261 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2262 sm->assoc_wpa_ie = os_malloc(len);
2263 if (sm->assoc_wpa_ie == NULL)
2266 os_memcpy(sm->assoc_wpa_ie, ie, len);
2267 sm->assoc_wpa_ie_len = len;
2275 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2276 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2277 * @ie: Pointer to IE data (starting from id)
2279 * Returns: 0 on success, -1 on failure
2281 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2284 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2289 os_free(sm->ap_wpa_ie);
2290 if (ie == NULL || len == 0) {
2291 wpa_printf(MSG_DEBUG, "WPA: clearing AP WPA IE");
2292 sm->ap_wpa_ie = NULL;
2293 sm->ap_wpa_ie_len = 0;
2295 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2296 sm->ap_wpa_ie = os_malloc(len);
2297 if (sm->ap_wpa_ie == NULL)
2300 os_memcpy(sm->ap_wpa_ie, ie, len);
2301 sm->ap_wpa_ie_len = len;
2309 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2310 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2311 * @ie: Pointer to IE data (starting from id)
2313 * Returns: 0 on success, -1 on failure
2315 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2318 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2323 os_free(sm->ap_rsn_ie);
2324 if (ie == NULL || len == 0) {
2325 wpa_printf(MSG_DEBUG, "WPA: clearing AP RSN IE");
2326 sm->ap_rsn_ie = NULL;
2327 sm->ap_rsn_ie_len = 0;
2329 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2330 sm->ap_rsn_ie = os_malloc(len);
2331 if (sm->ap_rsn_ie == NULL)
2334 os_memcpy(sm->ap_rsn_ie, ie, len);
2335 sm->ap_rsn_ie_len = len;
2343 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2344 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2345 * @data: Pointer to data area for parsing results
2346 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2348 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2349 * parsed data into data.
2351 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2353 if (sm == NULL || sm->assoc_wpa_ie == NULL) {
2354 wpa_printf(MSG_DEBUG, "WPA: No WPA/RSN IE available from "
2355 "association info");
2358 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2364 int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
2366 #ifndef CONFIG_NO_WPA2
2367 return pmksa_cache_list(sm->pmksa, buf, len);
2368 #else /* CONFIG_NO_WPA2 */
2370 #endif /* CONFIG_NO_WPA2 */