2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
3 * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/aes_wrap.h"
13 #include "crypto/crypto.h"
14 #include "crypto/random.h"
15 #include "common/ieee802_11_defs.h"
16 #include "eapol_supp/eapol_supp_sm.h"
20 #include "pmksa_cache.h"
27 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
28 * @sm: Pointer to WPA state machine data from wpa_sm_init()
29 * @kck: Key Confirmation Key (KCK, part of PTK)
30 * @ver: Version field from Key Info
31 * @dest: Destination address for the frame
32 * @proto: Ethertype (usually ETH_P_EAPOL)
33 * @msg: EAPOL-Key message
34 * @msg_len: Length of message
35 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
37 void wpa_eapol_key_send(struct wpa_sm *sm, const u8 *kck,
38 int ver, const u8 *dest, u16 proto,
39 u8 *msg, size_t msg_len, u8 *key_mic)
41 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
43 * Association event was not yet received; try to fetch
44 * BSSID from the driver.
46 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
47 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
48 "WPA: Failed to read BSSID for "
49 "EAPOL-Key destination address");
52 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
53 "WPA: Use BSSID (" MACSTR
54 ") as the destination for EAPOL-Key",
59 wpa_eapol_key_mic(kck, ver, msg, msg_len, key_mic)) {
60 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
61 "WPA: Failed to generate EAPOL-Key "
62 "version %d MIC", ver);
65 wpa_hexdump_key(MSG_DEBUG, "WPA: KCK", kck, 16);
66 wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC", key_mic, 16);
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 (sm->key_mgmt == WPA_KEY_MGMT_OSEN)
93 ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
94 else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
95 wpa_key_mgmt_sha256(sm->key_mgmt))
96 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
97 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
98 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
100 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
102 if (wpa_sm_get_bssid(sm, bssid) < 0) {
103 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
104 "Failed to read BSSID for EAPOL-Key request");
108 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
109 sizeof(*reply), &rlen, (void *) &reply);
113 reply->type = (sm->proto == WPA_PROTO_RSN ||
114 sm->proto == WPA_PROTO_OSEN) ?
115 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
116 key_info = WPA_KEY_INFO_REQUEST | ver;
118 key_info |= WPA_KEY_INFO_MIC;
120 key_info |= WPA_KEY_INFO_ERROR;
122 key_info |= WPA_KEY_INFO_KEY_TYPE;
123 WPA_PUT_BE16(reply->key_info, key_info);
124 WPA_PUT_BE16(reply->key_length, 0);
125 os_memcpy(reply->replay_counter, sm->request_counter,
126 WPA_REPLAY_COUNTER_LEN);
127 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
129 WPA_PUT_BE16(reply->key_data_length, 0);
131 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
132 "WPA: Sending EAPOL-Key Request (error=%d "
133 "pairwise=%d ptk_set=%d len=%lu)",
134 error, pairwise, sm->ptk_set, (unsigned long) rlen);
135 wpa_eapol_key_send(sm, sm->ptk.kck, ver, bssid, ETH_P_EAPOL,
136 rbuf, rlen, key_info & WPA_KEY_INFO_MIC ?
137 reply->key_mic : NULL);
141 static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
142 const unsigned char *src_addr,
145 int abort_cached = 0;
147 if (pmkid && !sm->cur_pmksa) {
148 /* When using drivers that generate RSN IE, wpa_supplicant may
149 * not have enough time to get the association information
150 * event before receiving this 1/4 message, so try to find a
151 * matching PMKSA cache entry here. */
152 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
155 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
156 "RSN: found matching PMKID from PMKSA cache");
158 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
159 "RSN: no matching PMKID found");
164 if (pmkid && sm->cur_pmksa &&
165 os_memcmp(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
166 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
167 wpa_sm_set_pmk_from_pmksa(sm);
168 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
169 sm->pmk, sm->pmk_len);
170 eapol_sm_notify_cached(sm->eapol);
171 #ifdef CONFIG_IEEE80211R
173 #endif /* CONFIG_IEEE80211R */
174 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
177 res = eapol_sm_get_key(sm->eapol, sm->pmk, PMK_LEN);
180 * EAP-LEAP is an exception from other EAP methods: it
181 * uses only 16-byte PMK.
183 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
186 #ifdef CONFIG_IEEE80211R
188 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
190 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
191 sm->xxkey_len = PMK_LEN;
192 os_memset(buf, 0, sizeof(buf));
194 #endif /* CONFIG_IEEE80211R */
197 struct rsn_pmksa_cache_entry *sa = NULL;
198 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
199 "machines", sm->pmk, pmk_len);
200 sm->pmk_len = pmk_len;
201 if (sm->proto == WPA_PROTO_RSN &&
202 !wpa_key_mgmt_ft(sm->key_mgmt)) {
203 sa = pmksa_cache_add(sm->pmksa,
205 src_addr, sm->own_addr,
209 if (!sm->cur_pmksa && pmkid &&
210 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL))
212 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
213 "RSN: the new PMK matches with the "
221 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
222 "WPA: Failed to get master session key from "
223 "EAPOL state machines - key handshake "
226 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
227 "RSN: Cancelled PMKSA caching "
229 sm->cur_pmksa = NULL;
231 } else if (!abort_cached) {
237 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
238 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
240 /* Send EAPOL-Start to trigger full EAP authentication. */
244 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
245 "RSN: no PMKSA entry found - trigger "
246 "full EAP authentication");
247 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
248 NULL, 0, &buflen, NULL);
250 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
264 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
265 * @sm: Pointer to WPA state machine data from wpa_sm_init()
266 * @dst: Destination address for the frame
267 * @key: Pointer to the EAPOL-Key frame header
268 * @ver: Version bits from EAPOL-Key Key Info
269 * @nonce: Nonce value for the EAPOL-Key frame
270 * @wpa_ie: WPA/RSN IE
271 * @wpa_ie_len: Length of the WPA/RSN IE
272 * @ptk: PTK to use for keyed hash and encryption
273 * Returns: 0 on success, -1 on failure
275 int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
276 const struct wpa_eapol_key *key,
277 int ver, const u8 *nonce,
278 const u8 *wpa_ie, size_t wpa_ie_len,
282 struct wpa_eapol_key *reply;
284 u8 *rsn_ie_buf = NULL;
286 if (wpa_ie == NULL) {
287 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
288 "cannot generate msg 2/4");
292 #ifdef CONFIG_IEEE80211R
293 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
297 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
298 * FTIE from (Re)Association Response.
300 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
301 sm->assoc_resp_ies_len);
302 if (rsn_ie_buf == NULL)
304 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
305 res = wpa_insert_pmkid(rsn_ie_buf, wpa_ie_len,
313 if (sm->assoc_resp_ies) {
314 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
315 sm->assoc_resp_ies_len);
316 wpa_ie_len += sm->assoc_resp_ies_len;
321 #endif /* CONFIG_IEEE80211R */
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);
333 reply->type = (sm->proto == WPA_PROTO_RSN ||
334 sm->proto == WPA_PROTO_OSEN) ?
335 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
336 WPA_PUT_BE16(reply->key_info,
337 ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
338 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
339 WPA_PUT_BE16(reply->key_length, 0);
341 os_memcpy(reply->key_length, key->key_length, 2);
342 os_memcpy(reply->replay_counter, key->replay_counter,
343 WPA_REPLAY_COUNTER_LEN);
344 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
345 WPA_REPLAY_COUNTER_LEN);
347 WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
348 os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
351 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
353 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
354 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
355 rbuf, rlen, reply->key_mic);
361 static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
362 const struct wpa_eapol_key *key,
365 size_t ptk_len = wpa_cipher_key_len(sm->pairwise_cipher) + 32;
366 #ifdef CONFIG_IEEE80211R
367 if (wpa_key_mgmt_ft(sm->key_mgmt))
368 return wpa_derive_ptk_ft(sm, src_addr, key, ptk, ptk_len);
369 #endif /* CONFIG_IEEE80211R */
371 wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
372 sm->own_addr, sm->bssid, sm->snonce, key->key_nonce,
374 wpa_key_mgmt_sha256(sm->key_mgmt));
379 static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
380 const unsigned char *src_addr,
381 const struct wpa_eapol_key *key,
384 struct wpa_eapol_ie_parse ie;
388 u8 *kde, *kde_buf = NULL;
391 if (wpa_sm_get_network_ctx(sm) == NULL) {
392 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
393 "found (msg 1 of 4)");
397 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
398 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
399 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
401 os_memset(&ie, 0, sizeof(ie));
403 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
404 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
405 const u8 *_buf = (const u8 *) (key + 1);
406 size_t len = WPA_GET_BE16(key->key_data_length);
407 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data", _buf, len);
408 if (wpa_supplicant_parse_ies(_buf, len, &ie) < 0)
411 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
412 "Authenticator", ie.pmkid, PMKID_LEN);
416 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
418 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
419 "msg 1/4 - requesting full EAP authentication");
425 if (sm->renew_snonce) {
426 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
427 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
428 "WPA: Failed to get random data for SNonce");
431 sm->renew_snonce = 0;
432 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
433 sm->snonce, WPA_NONCE_LEN);
436 /* Calculate PTK which will be stored as a temporary PTK until it has
437 * been verified when processing message 3/4. */
439 wpa_derive_ptk(sm, src_addr, key, ptk);
440 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
441 /* Supplicant: swap tx/rx Mic keys */
442 os_memcpy(buf, ptk->u.auth.tx_mic_key, 8);
443 os_memcpy(ptk->u.auth.tx_mic_key, ptk->u.auth.rx_mic_key, 8);
444 os_memcpy(ptk->u.auth.rx_mic_key, buf, 8);
448 kde = sm->assoc_wpa_ie;
449 kde_len = sm->assoc_wpa_ie_len;
453 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
456 wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
457 "into EAPOL-Key 2/4");
458 os_memcpy(kde_buf, kde, kde_len);
461 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
462 *pos++ = RSN_SELECTOR_LEN + 1;
463 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
464 pos += RSN_SELECTOR_LEN;
469 #endif /* CONFIG_P2P */
471 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
476 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
481 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
485 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
487 struct wpa_sm *sm = eloop_ctx;
488 rsn_preauth_candidate_process(sm);
492 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
493 const u8 *addr, int secure)
495 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
496 "WPA: Key negotiation completed with "
497 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
498 wpa_cipher_txt(sm->pairwise_cipher),
499 wpa_cipher_txt(sm->group_cipher));
500 wpa_sm_cancel_auth_timeout(sm);
501 wpa_sm_set_state(sm, WPA_COMPLETED);
504 wpa_sm_mlme_setprotection(
505 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
506 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
507 eapol_sm_notify_portValid(sm->eapol, TRUE);
508 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
509 eapol_sm_notify_eap_success(sm->eapol, TRUE);
511 * Start preauthentication after a short wait to avoid a
512 * possible race condition between the data receive and key
513 * configuration after the 4-Way Handshake. This increases the
514 * likelihood of the first preauth EAPOL-Start frame getting to
517 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
520 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
521 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
522 "RSN: Authenticator accepted "
523 "opportunistic PMKSA entry - marking it valid");
524 sm->cur_pmksa->opportunistic = 0;
527 #ifdef CONFIG_IEEE80211R
528 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
529 /* Prepare for the next transition */
530 wpa_ft_prepare_auth_request(sm, NULL);
532 #endif /* CONFIG_IEEE80211R */
536 static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
538 struct wpa_sm *sm = eloop_ctx;
539 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
540 wpa_sm_key_request(sm, 0, 1);
544 static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
545 const struct wpa_eapol_key *key)
550 u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
552 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
553 "WPA: Installing PTK to the driver");
555 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
556 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
557 "Suite: NONE - do not use pairwise keys");
561 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
562 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
563 "WPA: Unsupported pairwise cipher %d",
564 sm->pairwise_cipher);
568 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
569 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
570 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
572 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
575 key_rsc = key->key_rsc;
576 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
579 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
580 (u8 *) sm->ptk.tk1, keylen) < 0) {
581 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
582 "WPA: Failed to set PTK to the "
583 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
584 alg, keylen, MAC2STR(sm->bssid));
588 if (sm->wpa_ptk_rekey) {
589 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
590 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
598 static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
600 int keylen, int maxkeylen,
606 *alg = wpa_cipher_to_alg(group_cipher);
607 if (*alg == WPA_ALG_NONE) {
608 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
609 "WPA: Unsupported Group Cipher %d",
613 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
615 klen = wpa_cipher_key_len(group_cipher);
616 if (keylen != klen || maxkeylen < klen) {
617 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
618 "WPA: Unsupported %s Group Cipher key length %d (%d)",
619 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
626 struct wpa_gtk_data {
628 int tx, key_rsc_len, keyidx;
634 static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
635 const struct wpa_gtk_data *gd,
638 const u8 *_gtk = gd->gtk;
641 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
642 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
643 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
644 gd->keyidx, gd->tx, gd->gtk_len);
645 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
646 if (sm->group_cipher == WPA_CIPHER_TKIP) {
647 /* Swap Tx/Rx keys for Michael MIC */
648 os_memcpy(gtk_buf, gd->gtk, 16);
649 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
650 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
653 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
654 if (wpa_sm_set_key(sm, gd->alg, NULL,
655 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
656 _gtk, gd->gtk_len) < 0) {
657 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
658 "WPA: Failed to set GTK to the driver "
662 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
663 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
664 _gtk, gd->gtk_len) < 0) {
665 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
666 "WPA: Failed to set GTK to "
667 "the driver (alg=%d keylen=%d keyidx=%d)",
668 gd->alg, gd->gtk_len, gd->keyidx);
676 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
679 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
680 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
681 * seemed to set this bit (incorrectly, since Tx is only when
682 * doing Group Key only APs) and without this workaround, the
683 * data connection does not work because wpa_supplicant
684 * configured non-zero keyidx to be used for unicast. */
685 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
686 "WPA: Tx bit set for GTK, but pairwise "
687 "keys are used - ignore Tx bit");
694 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
695 const struct wpa_eapol_key *key,
696 const u8 *gtk, size_t gtk_len,
699 struct wpa_gtk_data gd;
702 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
704 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
705 * Reserved [bits 0-7]
709 os_memset(&gd, 0, sizeof(gd));
710 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
713 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
716 gd.keyidx = gtk[0] & 0x3;
717 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
718 !!(gtk[0] & BIT(2)));
722 os_memcpy(gd.gtk, gtk, gtk_len);
723 gd.gtk_len = gtk_len;
725 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
726 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
728 &gd.key_rsc_len, &gd.alg) ||
729 wpa_supplicant_install_gtk(sm, &gd, key->key_rsc))) {
730 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
731 "RSN: Failed to install GTK");
735 wpa_supplicant_key_neg_complete(sm, sm->bssid,
736 key_info & WPA_KEY_INFO_SECURE);
741 static int ieee80211w_set_keys(struct wpa_sm *sm,
742 struct wpa_eapol_ie_parse *ie)
744 #ifdef CONFIG_IEEE80211W
745 if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC)
749 const struct wpa_igtk_kde *igtk;
751 if (ie->igtk_len != sizeof(*igtk))
753 igtk = (const struct wpa_igtk_kde *) ie->igtk;
754 keyidx = WPA_GET_LE16(igtk->keyid);
755 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: IGTK keyid %d "
756 "pn %02x%02x%02x%02x%02x%02x",
757 keyidx, MAC2STR(igtk->pn));
758 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
759 igtk->igtk, WPA_IGTK_LEN);
761 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
762 "WPA: Invalid IGTK KeyID %d", keyidx);
765 if (wpa_sm_set_key(sm, WPA_ALG_IGTK, broadcast_ether_addr,
766 keyidx, 0, igtk->pn, sizeof(igtk->pn),
767 igtk->igtk, WPA_IGTK_LEN) < 0) {
768 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
769 "WPA: Failed to configure IGTK to the driver");
775 #else /* CONFIG_IEEE80211W */
777 #endif /* CONFIG_IEEE80211W */
781 static void wpa_report_ie_mismatch(struct wpa_sm *sm,
782 const char *reason, const u8 *src_addr,
783 const u8 *wpa_ie, size_t wpa_ie_len,
784 const u8 *rsn_ie, size_t rsn_ie_len)
786 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
787 reason, MAC2STR(src_addr));
790 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
791 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
794 if (!sm->ap_wpa_ie) {
795 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
796 "WPA: No WPA IE in Beacon/ProbeResp");
798 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
803 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
804 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
807 if (!sm->ap_rsn_ie) {
808 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
809 "WPA: No RSN IE in Beacon/ProbeResp");
811 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
815 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
819 #ifdef CONFIG_IEEE80211R
821 static int ft_validate_mdie(struct wpa_sm *sm,
822 const unsigned char *src_addr,
823 struct wpa_eapol_ie_parse *ie,
824 const u8 *assoc_resp_mdie)
826 struct rsn_mdie *mdie;
828 mdie = (struct rsn_mdie *) (ie->mdie + 2);
829 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
830 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
831 MOBILITY_DOMAIN_ID_LEN) != 0) {
832 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
833 "not match with the current mobility domain");
837 if (assoc_resp_mdie &&
838 (assoc_resp_mdie[1] != ie->mdie[1] ||
839 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
840 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
841 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
842 ie->mdie, 2 + ie->mdie[1]);
843 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
844 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
852 static int ft_validate_ftie(struct wpa_sm *sm,
853 const unsigned char *src_addr,
854 struct wpa_eapol_ie_parse *ie,
855 const u8 *assoc_resp_ftie)
857 if (ie->ftie == NULL) {
858 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
859 "FT: No FTIE in EAPOL-Key msg 3/4");
863 if (assoc_resp_ftie == NULL)
866 if (assoc_resp_ftie[1] != ie->ftie[1] ||
867 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
868 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
869 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
870 ie->ftie, 2 + ie->ftie[1]);
871 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
872 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
880 static int ft_validate_rsnie(struct wpa_sm *sm,
881 const unsigned char *src_addr,
882 struct wpa_eapol_ie_parse *ie)
884 struct wpa_ie_data rsn;
890 * Verify that PMKR1Name from EAPOL-Key message 3/4
891 * matches with the value we derived.
893 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
894 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
895 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
896 "FT 4-way handshake message 3/4");
900 if (os_memcmp(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0) {
901 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
902 "FT: PMKR1Name mismatch in "
903 "FT 4-way handshake message 3/4");
904 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
905 rsn.pmkid, WPA_PMK_NAME_LEN);
906 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
907 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
915 static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
916 const unsigned char *src_addr,
917 struct wpa_eapol_ie_parse *ie)
919 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
921 if (sm->assoc_resp_ies) {
922 pos = sm->assoc_resp_ies;
923 end = pos + sm->assoc_resp_ies_len;
924 while (pos + 2 < end) {
925 if (pos + 2 + pos[1] > end)
928 case WLAN_EID_MOBILITY_DOMAIN:
931 case WLAN_EID_FAST_BSS_TRANSITION:
939 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
940 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
941 ft_validate_rsnie(sm, src_addr, ie) < 0)
947 #endif /* CONFIG_IEEE80211R */
950 static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
951 const unsigned char *src_addr,
952 struct wpa_eapol_ie_parse *ie)
954 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
955 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
956 "WPA: No WPA/RSN IE for this AP known. "
957 "Trying to get from scan results");
958 if (wpa_sm_get_beacon_ie(sm) < 0) {
959 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
960 "WPA: Could not find AP from "
963 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
964 "WPA: Found the current AP from "
965 "updated scan results");
969 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
970 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
971 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
972 "with IE in Beacon/ProbeResp (no IE?)",
973 src_addr, ie->wpa_ie, ie->wpa_ie_len,
974 ie->rsn_ie, ie->rsn_ie_len);
978 if ((ie->wpa_ie && sm->ap_wpa_ie &&
979 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
980 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
981 (ie->rsn_ie && sm->ap_rsn_ie &&
982 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
983 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
984 ie->rsn_ie, ie->rsn_ie_len))) {
985 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
986 "with IE in Beacon/ProbeResp",
987 src_addr, ie->wpa_ie, ie->wpa_ie_len,
988 ie->rsn_ie, ie->rsn_ie_len);
992 if (sm->proto == WPA_PROTO_WPA &&
993 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
994 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
995 "detected - RSN was enabled and RSN IE "
996 "was in msg 3/4, but not in "
998 src_addr, ie->wpa_ie, ie->wpa_ie_len,
999 ie->rsn_ie, ie->rsn_ie_len);
1003 #ifdef CONFIG_IEEE80211R
1004 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1005 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1007 #endif /* CONFIG_IEEE80211R */
1014 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1015 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1016 * @dst: Destination address for the frame
1017 * @key: Pointer to the EAPOL-Key frame header
1018 * @ver: Version bits from EAPOL-Key Key Info
1019 * @key_info: Key Info
1020 * @kde: KDEs to include the EAPOL-Key frame
1021 * @kde_len: Length of KDEs
1022 * @ptk: PTK to use for keyed hash and encryption
1023 * Returns: 0 on success, -1 on failure
1025 int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1026 const struct wpa_eapol_key *key,
1027 u16 ver, u16 key_info,
1028 const u8 *kde, size_t kde_len,
1029 struct wpa_ptk *ptk)
1032 struct wpa_eapol_key *reply;
1036 wpa_hexdump(MSG_DEBUG, "WPA: KDE for msg 4/4", kde, kde_len);
1038 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1039 sizeof(*reply) + kde_len,
1040 &rlen, (void *) &reply);
1044 reply->type = (sm->proto == WPA_PROTO_RSN ||
1045 sm->proto == WPA_PROTO_OSEN) ?
1046 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1047 key_info &= WPA_KEY_INFO_SECURE;
1048 key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
1049 WPA_PUT_BE16(reply->key_info, key_info);
1050 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
1051 WPA_PUT_BE16(reply->key_length, 0);
1053 os_memcpy(reply->key_length, key->key_length, 2);
1054 os_memcpy(reply->replay_counter, key->replay_counter,
1055 WPA_REPLAY_COUNTER_LEN);
1057 WPA_PUT_BE16(reply->key_data_length, kde_len);
1059 os_memcpy(reply + 1, kde, kde_len);
1061 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
1062 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
1063 rbuf, rlen, reply->key_mic);
1069 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1070 const struct wpa_eapol_key *key,
1073 u16 key_info, keylen, len;
1075 struct wpa_eapol_ie_parse ie;
1077 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1078 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
1079 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1081 key_info = WPA_GET_BE16(key->key_info);
1083 pos = (const u8 *) (key + 1);
1084 len = WPA_GET_BE16(key->key_data_length);
1085 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", pos, len);
1086 if (wpa_supplicant_parse_ies(pos, len, &ie) < 0)
1088 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1089 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1090 "WPA: GTK IE in unencrypted key data");
1093 #ifdef CONFIG_IEEE80211W
1094 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1095 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1096 "WPA: IGTK KDE in unencrypted key data");
1100 if (ie.igtk && ie.igtk_len != sizeof(struct wpa_igtk_kde)) {
1101 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1102 "WPA: Invalid IGTK KDE length %lu",
1103 (unsigned long) ie.igtk_len);
1106 #endif /* CONFIG_IEEE80211W */
1108 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1111 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1112 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1113 "WPA: ANonce from message 1 of 4-Way Handshake "
1114 "differs from 3 of 4-Way Handshake - drop packet (src="
1115 MACSTR ")", MAC2STR(sm->bssid));
1119 keylen = WPA_GET_BE16(key->key_length);
1120 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1121 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1122 "WPA: Invalid %s key length %d (src=" MACSTR
1123 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1124 MAC2STR(sm->bssid));
1129 if (ie.ip_addr_alloc) {
1130 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1131 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1132 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1134 #endif /* CONFIG_P2P */
1136 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
1137 NULL, 0, &sm->ptk)) {
1141 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1142 * for the next 4-Way Handshake. If msg 3 is received again, the old
1143 * SNonce will still be used to avoid changing PTK. */
1144 sm->renew_snonce = 1;
1146 if (key_info & WPA_KEY_INFO_INSTALL) {
1147 if (wpa_supplicant_install_ptk(sm, key))
1151 if (key_info & WPA_KEY_INFO_SECURE) {
1152 wpa_sm_mlme_setprotection(
1153 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1154 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1155 eapol_sm_notify_portValid(sm->eapol, TRUE);
1157 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1159 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
1160 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1161 key_info & WPA_KEY_INFO_SECURE);
1162 } else if (ie.gtk &&
1163 wpa_supplicant_pairwise_gtk(sm, key,
1164 ie.gtk, ie.gtk_len, key_info) < 0) {
1165 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1166 "RSN: Failed to configure GTK");
1170 if (ieee80211w_set_keys(sm, &ie) < 0) {
1171 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1172 "RSN: Failed to configure IGTK");
1177 wpa_sm_set_rekey_offload(sm);
1182 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1186 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1190 struct wpa_gtk_data *gd)
1193 struct wpa_eapol_ie_parse ie;
1195 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
1196 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1198 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1199 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1200 "WPA: GTK IE in unencrypted key data");
1203 if (ie.gtk == NULL) {
1204 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1205 "WPA: No GTK IE in Group Key msg 1/2");
1208 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1210 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1211 gd->gtk_len, maxkeylen,
1212 &gd->key_rsc_len, &gd->alg))
1215 wpa_hexdump(MSG_DEBUG, "RSN: received GTK in group key handshake",
1216 ie.gtk, ie.gtk_len);
1217 gd->keyidx = ie.gtk[0] & 0x3;
1218 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1219 !!(ie.gtk[0] & BIT(2)));
1220 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1221 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1222 "RSN: Too long GTK in GTK IE (len=%lu)",
1223 (unsigned long) ie.gtk_len - 2);
1226 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1228 if (ieee80211w_set_keys(sm, &ie) < 0)
1229 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1230 "RSN: Failed to configure IGTK");
1236 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1237 const struct wpa_eapol_key *key,
1238 size_t keydatalen, int key_info,
1239 size_t extra_len, u16 ver,
1240 struct wpa_gtk_data *gd)
1245 gd->gtk_len = WPA_GET_BE16(key->key_length);
1246 maxkeylen = keydatalen;
1247 if (keydatalen > extra_len) {
1248 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1249 "WPA: Truncated EAPOL-Key packet: "
1250 "key_data_length=%lu > extra_len=%lu",
1251 (unsigned long) keydatalen, (unsigned long) extra_len);
1254 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1255 if (maxkeylen < 8) {
1256 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1257 "WPA: Too short maxkeylen (%lu)",
1258 (unsigned long) maxkeylen);
1264 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1265 gd->gtk_len, maxkeylen,
1266 &gd->key_rsc_len, &gd->alg))
1269 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1270 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1271 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1272 os_memcpy(ek, key->key_iv, 16);
1273 os_memcpy(ek + 16, sm->ptk.kek, 16);
1274 if (keydatalen > sizeof(gd->gtk)) {
1275 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1276 "WPA: RC4 key data too long (%lu)",
1277 (unsigned long) keydatalen);
1280 os_memcpy(gd->gtk, key + 1, keydatalen);
1281 if (rc4_skip(ek, 32, 256, gd->gtk, keydatalen)) {
1282 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1286 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1287 if (keydatalen % 8) {
1288 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1289 "WPA: Unsupported AES-WRAP len %lu",
1290 (unsigned long) keydatalen);
1293 if (maxkeylen > sizeof(gd->gtk)) {
1294 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1295 "WPA: AES-WRAP key data "
1296 "too long (keydatalen=%lu maxkeylen=%lu)",
1297 (unsigned long) keydatalen,
1298 (unsigned long) maxkeylen);
1301 if (aes_unwrap(sm->ptk.kek, maxkeylen / 8,
1302 (const u8 *) (key + 1), gd->gtk)) {
1303 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1304 "WPA: AES unwrap failed - could not decrypt "
1309 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1310 "WPA: Unsupported key_info type %d", ver);
1313 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1314 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1319 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1320 const struct wpa_eapol_key *key,
1321 int ver, u16 key_info)
1324 struct wpa_eapol_key *reply;
1327 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1328 sizeof(*reply), &rlen, (void *) &reply);
1332 reply->type = (sm->proto == WPA_PROTO_RSN ||
1333 sm->proto == WPA_PROTO_OSEN) ?
1334 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1335 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1336 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1337 WPA_PUT_BE16(reply->key_info, key_info);
1338 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
1339 WPA_PUT_BE16(reply->key_length, 0);
1341 os_memcpy(reply->key_length, key->key_length, 2);
1342 os_memcpy(reply->replay_counter, key->replay_counter,
1343 WPA_REPLAY_COUNTER_LEN);
1345 WPA_PUT_BE16(reply->key_data_length, 0);
1347 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1348 wpa_eapol_key_send(sm, sm->ptk.kck, ver, sm->bssid, ETH_P_EAPOL,
1349 rbuf, rlen, reply->key_mic);
1355 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1356 const unsigned char *src_addr,
1357 const struct wpa_eapol_key *key,
1358 int extra_len, u16 ver)
1360 u16 key_info, keydatalen;
1362 struct wpa_gtk_data gd;
1364 os_memset(&gd, 0, sizeof(gd));
1366 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1367 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1368 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1370 key_info = WPA_GET_BE16(key->key_info);
1371 keydatalen = WPA_GET_BE16(key->key_data_length);
1373 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
1374 ret = wpa_supplicant_process_1_of_2_rsn(sm,
1375 (const u8 *) (key + 1),
1376 keydatalen, key_info,
1379 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, keydatalen,
1380 key_info, extra_len,
1384 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1389 if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc) ||
1390 wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
1394 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1395 "completed with " MACSTR " [GTK=%s]",
1396 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1397 wpa_sm_cancel_auth_timeout(sm);
1398 wpa_sm_set_state(sm, WPA_COMPLETED);
1400 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1402 WPA_KEY_INFO_SECURE);
1405 wpa_sm_set_rekey_offload(sm);
1410 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1414 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1415 struct wpa_eapol_key *key,
1417 const u8 *buf, size_t len)
1422 os_memcpy(mic, key->key_mic, 16);
1424 os_memset(key->key_mic, 0, 16);
1425 wpa_eapol_key_mic(sm->tptk.kck, ver, buf, len,
1427 if (os_memcmp(mic, key->key_mic, 16) != 0) {
1428 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1429 "WPA: Invalid EAPOL-Key MIC "
1430 "when using TPTK - ignoring TPTK");
1435 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1439 if (!ok && sm->ptk_set) {
1440 os_memset(key->key_mic, 0, 16);
1441 wpa_eapol_key_mic(sm->ptk.kck, ver, buf, len,
1443 if (os_memcmp(mic, key->key_mic, 16) != 0) {
1444 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1445 "WPA: Invalid EAPOL-Key MIC - "
1453 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1454 "WPA: Could not verify EAPOL-Key MIC - "
1459 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1460 WPA_REPLAY_COUNTER_LEN);
1461 sm->rx_replay_counter_set = 1;
1466 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1467 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
1468 struct wpa_eapol_key *key, u16 ver)
1470 u16 keydatalen = WPA_GET_BE16(key->key_data_length);
1472 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
1473 (u8 *) (key + 1), keydatalen);
1475 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1476 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1481 /* Decrypt key data here so that this operation does not need
1482 * to be implemented separately for each message type. */
1483 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1485 os_memcpy(ek, key->key_iv, 16);
1486 os_memcpy(ek + 16, sm->ptk.kek, 16);
1487 if (rc4_skip(ek, 32, 256, (u8 *) (key + 1), keydatalen)) {
1488 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1492 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1493 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
1494 sm->key_mgmt == WPA_KEY_MGMT_OSEN) {
1496 if (keydatalen % 8) {
1497 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1498 "WPA: Unsupported AES-WRAP len %d",
1502 keydatalen -= 8; /* AES-WRAP adds 8 bytes */
1503 buf = os_malloc(keydatalen);
1505 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1506 "WPA: No memory for AES-UNWRAP buffer");
1509 if (aes_unwrap(sm->ptk.kek, keydatalen / 8,
1510 (u8 *) (key + 1), buf)) {
1512 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1513 "WPA: AES unwrap failed - "
1514 "could not decrypt EAPOL-Key key data");
1517 os_memcpy(key + 1, buf, keydatalen);
1519 WPA_PUT_BE16(key->key_data_length, keydatalen);
1521 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1522 "WPA: Unsupported key_info type %d", ver);
1525 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
1526 (u8 *) (key + 1), keydatalen);
1532 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1533 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1535 void wpa_sm_aborted_cached(struct wpa_sm *sm)
1537 if (sm && sm->cur_pmksa) {
1538 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1539 "RSN: Cancelling PMKSA caching attempt");
1540 sm->cur_pmksa = NULL;
1545 static void wpa_eapol_key_dump(struct wpa_sm *sm,
1546 const struct wpa_eapol_key *key)
1548 #ifndef CONFIG_NO_STDOUT_DEBUG
1549 u16 key_info = WPA_GET_BE16(key->key_info);
1551 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1552 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1553 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1554 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1555 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1556 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1557 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1558 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1559 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1560 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1561 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1562 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1563 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1564 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1565 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1566 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1567 " key_length=%u key_data_length=%u",
1568 WPA_GET_BE16(key->key_length),
1569 WPA_GET_BE16(key->key_data_length));
1570 wpa_hexdump(MSG_DEBUG, " replay_counter",
1571 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1572 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1573 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1574 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1575 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
1576 wpa_hexdump(MSG_DEBUG, " key_mic", key->key_mic, 16);
1577 #endif /* CONFIG_NO_STDOUT_DEBUG */
1582 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1583 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1584 * @src_addr: Source MAC address of the EAPOL packet
1585 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1586 * @len: Length of the EAPOL frame
1587 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1589 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1590 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1591 * only processing WPA and WPA2 EAPOL-Key frames.
1593 * The received EAPOL-Key packets are validated and valid packets are replied
1594 * to. In addition, key material (PTK, GTK) is configured at the end of a
1595 * successful key handshake.
1597 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1598 const u8 *buf, size_t len)
1600 size_t plen, data_len, extra_len;
1601 struct ieee802_1x_hdr *hdr;
1602 struct wpa_eapol_key *key;
1606 struct wpa_peerkey *peerkey = NULL;
1608 #ifdef CONFIG_IEEE80211R
1609 sm->ft_completed = 0;
1610 #endif /* CONFIG_IEEE80211R */
1612 if (len < sizeof(*hdr) + sizeof(*key)) {
1613 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1614 "WPA: EAPOL frame too short to be a WPA "
1615 "EAPOL-Key (len %lu, expecting at least %lu)",
1616 (unsigned long) len,
1617 (unsigned long) sizeof(*hdr) + sizeof(*key));
1621 tmp = os_malloc(len);
1624 os_memcpy(tmp, buf, len);
1626 hdr = (struct ieee802_1x_hdr *) tmp;
1627 key = (struct wpa_eapol_key *) (hdr + 1);
1628 plen = be_to_host16(hdr->length);
1629 data_len = plen + sizeof(*hdr);
1630 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1631 "IEEE 802.1X RX: version=%d type=%d length=%lu",
1632 hdr->version, hdr->type, (unsigned long) plen);
1634 if (hdr->version < EAPOL_VERSION) {
1635 /* TODO: backwards compatibility */
1637 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1638 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1639 "WPA: EAPOL frame (type %u) discarded, "
1640 "not a Key frame", hdr->type);
1644 if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
1645 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1646 "WPA: EAPOL frame payload size %lu "
1647 "invalid (frame size %lu)",
1648 (unsigned long) plen, (unsigned long) len);
1653 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1655 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1656 "WPA: EAPOL-Key type (%d) unknown, discarded",
1661 wpa_eapol_key_dump(sm, key);
1663 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
1664 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", tmp, len);
1665 if (data_len < len) {
1666 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1667 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
1668 (unsigned long) len - data_len);
1670 key_info = WPA_GET_BE16(key->key_info);
1671 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1672 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1673 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1674 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1675 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
1676 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
1677 sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
1678 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1679 "WPA: Unsupported EAPOL-Key descriptor version %d",
1684 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN &&
1685 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1686 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1687 "OSEN: Unsupported EAPOL-Key descriptor version %d",
1692 #ifdef CONFIG_IEEE80211R
1693 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1694 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1695 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1696 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1697 "FT: AP did not use AES-128-CMAC");
1701 #endif /* CONFIG_IEEE80211R */
1702 #ifdef CONFIG_IEEE80211W
1703 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
1704 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1705 sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
1706 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1707 "WPA: AP did not use the "
1708 "negotiated AES-128-CMAC");
1712 #endif /* CONFIG_IEEE80211W */
1713 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
1714 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1715 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1716 "WPA: CCMP is used, but EAPOL-Key "
1717 "descriptor version (%d) is not 2", ver);
1718 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1719 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1720 /* Earlier versions of IEEE 802.11i did not explicitly
1721 * require version 2 descriptor for all EAPOL-Key
1722 * packets, so allow group keys to use version 1 if
1723 * CCMP is not used for them. */
1724 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1725 "WPA: Backwards compatibility: allow invalid "
1726 "version for non-CCMP group keys");
1730 if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
1731 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1732 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1733 "WPA: GCMP is used, but EAPOL-Key "
1734 "descriptor version (%d) is not 2", ver);
1738 #ifdef CONFIG_PEERKEY
1739 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1740 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1744 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1745 if (!peerkey->initiator && peerkey->replay_counter_set &&
1746 os_memcmp(key->replay_counter, peerkey->replay_counter,
1747 WPA_REPLAY_COUNTER_LEN) <= 0) {
1748 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1749 "RSN: EAPOL-Key Replay Counter did not "
1750 "increase (STK) - dropping packet");
1752 } else if (peerkey->initiator) {
1753 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1754 os_memcpy(_tmp, key->replay_counter,
1755 WPA_REPLAY_COUNTER_LEN);
1756 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
1757 if (os_memcmp(_tmp, peerkey->replay_counter,
1758 WPA_REPLAY_COUNTER_LEN) != 0) {
1759 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1760 "RSN: EAPOL-Key Replay "
1761 "Counter did not match (STK) - "
1768 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
1769 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1770 "RSN: Ack bit in key_info from STK peer");
1773 #endif /* CONFIG_PEERKEY */
1775 if (!peerkey && sm->rx_replay_counter_set &&
1776 os_memcmp(key->replay_counter, sm->rx_replay_counter,
1777 WPA_REPLAY_COUNTER_LEN) <= 0) {
1778 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1779 "WPA: EAPOL-Key Replay Counter did not increase - "
1784 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
1785 #ifdef CONFIG_PEERKEY
1786 && (peerkey == NULL || !peerkey->initiator)
1787 #endif /* CONFIG_PEERKEY */
1789 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1790 "WPA: No Ack bit in key_info");
1794 if (key_info & WPA_KEY_INFO_REQUEST) {
1795 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1796 "WPA: EAPOL-Key with Request bit - dropped");
1800 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
1801 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
1804 #ifdef CONFIG_PEERKEY
1805 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
1806 peerkey_verify_eapol_key_mic(sm, peerkey, key, ver, tmp, data_len))
1808 #endif /* CONFIG_PEERKEY */
1810 extra_len = data_len - sizeof(*hdr) - sizeof(*key);
1812 if (WPA_GET_BE16(key->key_data_length) > extra_len) {
1813 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1814 "frame - key_data overflow (%d > %lu)",
1815 WPA_GET_BE16(key->key_data_length),
1816 (unsigned long) extra_len);
1819 extra_len = WPA_GET_BE16(key->key_data_length);
1821 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
1822 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1823 if (wpa_supplicant_decrypt_key_data(sm, key, ver))
1825 extra_len = WPA_GET_BE16(key->key_data_length);
1828 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1829 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
1830 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1831 "WPA: Ignored EAPOL-Key (Pairwise) with "
1832 "non-zero key index");
1836 /* PeerKey 4-Way Handshake */
1837 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver);
1838 } else if (key_info & WPA_KEY_INFO_MIC) {
1839 /* 3/4 4-Way Handshake */
1840 wpa_supplicant_process_3_of_4(sm, key, ver);
1842 /* 1/4 4-Way Handshake */
1843 wpa_supplicant_process_1_of_4(sm, src_addr, key,
1846 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1847 /* PeerKey SMK Handshake */
1848 peerkey_rx_eapol_smk(sm, src_addr, key, extra_len, key_info,
1851 if (key_info & WPA_KEY_INFO_MIC) {
1852 /* 1/2 Group Key Handshake */
1853 wpa_supplicant_process_1_of_2(sm, src_addr, key,
1856 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1857 "WPA: EAPOL-Key (Group) without Mic bit - "
1870 #ifdef CONFIG_CTRL_IFACE
1871 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
1873 switch (sm->key_mgmt) {
1874 case WPA_KEY_MGMT_IEEE8021X:
1875 return ((sm->proto == WPA_PROTO_RSN ||
1876 sm->proto == WPA_PROTO_OSEN) ?
1877 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
1878 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
1879 case WPA_KEY_MGMT_PSK:
1880 return (sm->proto == WPA_PROTO_RSN ?
1881 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
1882 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
1883 #ifdef CONFIG_IEEE80211R
1884 case WPA_KEY_MGMT_FT_IEEE8021X:
1885 return RSN_AUTH_KEY_MGMT_FT_802_1X;
1886 case WPA_KEY_MGMT_FT_PSK:
1887 return RSN_AUTH_KEY_MGMT_FT_PSK;
1888 #endif /* CONFIG_IEEE80211R */
1889 #ifdef CONFIG_IEEE80211W
1890 case WPA_KEY_MGMT_IEEE8021X_SHA256:
1891 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
1892 case WPA_KEY_MGMT_PSK_SHA256:
1893 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
1894 #endif /* CONFIG_IEEE80211W */
1895 case WPA_KEY_MGMT_CCKM:
1896 return (sm->proto == WPA_PROTO_RSN ?
1897 RSN_AUTH_KEY_MGMT_CCKM:
1898 WPA_AUTH_KEY_MGMT_CCKM);
1899 case WPA_KEY_MGMT_WPA_NONE:
1900 return WPA_AUTH_KEY_MGMT_NONE;
1907 #define RSN_SUITE "%02x-%02x-%02x-%d"
1908 #define RSN_SUITE_ARG(s) \
1909 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
1912 * wpa_sm_get_mib - Dump text list of MIB entries
1913 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1914 * @buf: Buffer for the list
1915 * @buflen: Length of the buffer
1916 * Returns: Number of bytes written to buffer
1918 * This function is used fetch dot11 MIB variables.
1920 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
1922 char pmkid_txt[PMKID_LEN * 2 + 1];
1926 if (sm->cur_pmksa) {
1927 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
1928 sm->cur_pmksa->pmkid, PMKID_LEN);
1930 pmkid_txt[0] = '\0';
1932 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
1933 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
1934 sm->proto == WPA_PROTO_RSN)
1939 ret = os_snprintf(buf, buflen,
1940 "dot11RSNAOptionImplemented=TRUE\n"
1941 "dot11RSNAPreauthenticationImplemented=TRUE\n"
1942 "dot11RSNAEnabled=%s\n"
1943 "dot11RSNAPreauthenticationEnabled=%s\n"
1944 "dot11RSNAConfigVersion=%d\n"
1945 "dot11RSNAConfigPairwiseKeysSupported=5\n"
1946 "dot11RSNAConfigGroupCipherSize=%d\n"
1947 "dot11RSNAConfigPMKLifetime=%d\n"
1948 "dot11RSNAConfigPMKReauthThreshold=%d\n"
1949 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
1950 "dot11RSNAConfigSATimeout=%d\n",
1951 rsna ? "TRUE" : "FALSE",
1952 rsna ? "TRUE" : "FALSE",
1954 wpa_cipher_key_len(sm->group_cipher) * 8,
1955 sm->dot11RSNAConfigPMKLifetime,
1956 sm->dot11RSNAConfigPMKReauthThreshold,
1957 sm->dot11RSNAConfigSATimeout);
1958 if (ret < 0 || (size_t) ret >= buflen)
1963 buf + len, buflen - len,
1964 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
1965 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
1966 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
1967 "dot11RSNAPMKIDUsed=%s\n"
1968 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
1969 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
1970 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
1971 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
1972 "dot11RSNA4WayHandshakeFailures=%u\n",
1973 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1974 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
1975 sm->pairwise_cipher)),
1976 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
1979 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1980 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
1981 sm->pairwise_cipher)),
1982 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
1984 sm->dot11RSNA4WayHandshakeFailures);
1985 if (ret >= 0 && (size_t) ret < buflen)
1990 #endif /* CONFIG_CTRL_IFACE */
1993 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
1994 void *ctx, enum pmksa_free_reason reason)
1996 struct wpa_sm *sm = ctx;
1999 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2000 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2002 if (sm->cur_pmksa == entry) {
2003 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2004 "RSN: %s current PMKSA entry",
2005 reason == PMKSA_REPLACE ? "replaced" : "removed");
2006 pmksa_cache_clear_current(sm);
2009 * If an entry is simply being replaced, there's no need to
2010 * deauthenticate because it will be immediately re-added.
2011 * This happens when EAP authentication is completed again
2012 * (reauth or failed PMKSA caching attempt).
2014 if (reason != PMKSA_REPLACE)
2018 if (reason == PMKSA_EXPIRE &&
2019 (sm->pmk_len == entry->pmk_len &&
2020 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2021 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2022 "RSN: deauthenticating due to expired PMK");
2023 pmksa_cache_clear_current(sm);
2028 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2029 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2035 * wpa_sm_init - Initialize WPA state machine
2036 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2037 * Returns: Pointer to the allocated WPA state machine data
2039 * This function is used to allocate a new WPA state machine and the returned
2040 * value is passed to all WPA state machine calls.
2042 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2046 sm = os_zalloc(sizeof(*sm));
2049 dl_list_init(&sm->pmksa_candidates);
2050 sm->renew_snonce = 1;
2053 sm->dot11RSNAConfigPMKLifetime = 43200;
2054 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2055 sm->dot11RSNAConfigSATimeout = 60;
2057 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2058 if (sm->pmksa == NULL) {
2059 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2060 "RSN: PMKSA cache initialization failed");
2070 * wpa_sm_deinit - Deinitialize WPA state machine
2071 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2073 void wpa_sm_deinit(struct wpa_sm *sm)
2077 pmksa_cache_deinit(sm->pmksa);
2078 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2079 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2080 os_free(sm->assoc_wpa_ie);
2081 os_free(sm->ap_wpa_ie);
2082 os_free(sm->ap_rsn_ie);
2085 #ifdef CONFIG_IEEE80211R
2086 os_free(sm->assoc_resp_ies);
2087 #endif /* CONFIG_IEEE80211R */
2093 * wpa_sm_notify_assoc - Notify WPA state machine about association
2094 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2095 * @bssid: The BSSID of the new association
2097 * This function is called to let WPA state machine know that the connection
2100 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2107 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2108 "WPA: Association event - clear replay counter");
2109 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2110 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2111 sm->rx_replay_counter_set = 0;
2112 sm->renew_snonce = 1;
2113 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2114 rsn_preauth_deinit(sm);
2116 #ifdef CONFIG_IEEE80211R
2117 if (wpa_ft_is_completed(sm)) {
2119 * Clear portValid to kick EAPOL state machine to re-enter
2120 * AUTHENTICATED state to get the EAPOL port Authorized.
2122 eapol_sm_notify_portValid(sm->eapol, FALSE);
2123 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2125 /* Prepare for the next transition */
2126 wpa_ft_prepare_auth_request(sm, NULL);
2130 #endif /* CONFIG_IEEE80211R */
2134 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2135 * this is not part of a Fast BSS Transition.
2137 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2144 #endif /* CONFIG_TDLS */
2147 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2148 #endif /* CONFIG_P2P */
2153 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2154 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2156 * This function is called to let WPA state machine know that the connection
2157 * was lost. This will abort any existing pre-authentication session.
2159 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2162 rsn_preauth_deinit(sm);
2163 pmksa_cache_clear_current(sm);
2164 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2165 sm->dot11RSNA4WayHandshakeFailures++;
2167 wpa_tdls_disassoc(sm);
2168 #endif /* CONFIG_TDLS */
2173 * wpa_sm_set_pmk - Set PMK
2174 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2176 * @pmk_len: The length of the new PMK in bytes
2178 * Configure the PMK for WPA state machine.
2180 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len)
2185 sm->pmk_len = pmk_len;
2186 os_memcpy(sm->pmk, pmk, pmk_len);
2188 #ifdef CONFIG_IEEE80211R
2189 /* Set XXKey to be PSK for FT key derivation */
2190 sm->xxkey_len = pmk_len;
2191 os_memcpy(sm->xxkey, pmk, pmk_len);
2192 #endif /* CONFIG_IEEE80211R */
2197 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2198 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2200 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2203 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2208 if (sm->cur_pmksa) {
2209 sm->pmk_len = sm->cur_pmksa->pmk_len;
2210 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2212 sm->pmk_len = PMK_LEN;
2213 os_memset(sm->pmk, 0, PMK_LEN);
2219 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2220 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2221 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2223 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2226 sm->fast_reauth = fast_reauth;
2231 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2232 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2233 * @scard_ctx: Context pointer for smartcard related callback functions
2235 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2239 sm->scard_ctx = scard_ctx;
2240 if (sm->preauth_eapol)
2241 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2246 * wpa_sm_set_config - Notification of current configration change
2247 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2248 * @config: Pointer to current network configuration
2250 * Notify WPA state machine that configuration has changed. config will be
2251 * stored as a backpointer to network configuration. This can be %NULL to clear
2252 * the stored pointed.
2254 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2260 sm->network_ctx = config->network_ctx;
2261 sm->peerkey_enabled = config->peerkey_enabled;
2262 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2263 sm->proactive_key_caching = config->proactive_key_caching;
2264 sm->eap_workaround = config->eap_workaround;
2265 sm->eap_conf_ctx = config->eap_conf_ctx;
2267 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2268 sm->ssid_len = config->ssid_len;
2271 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
2272 sm->p2p = config->p2p;
2274 sm->network_ctx = NULL;
2275 sm->peerkey_enabled = 0;
2276 sm->allowed_pairwise_cipher = 0;
2277 sm->proactive_key_caching = 0;
2278 sm->eap_workaround = 0;
2279 sm->eap_conf_ctx = NULL;
2281 sm->wpa_ptk_rekey = 0;
2288 * wpa_sm_set_own_addr - Set own MAC address
2289 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2290 * @addr: Own MAC address
2292 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2295 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2300 * wpa_sm_set_ifname - Set network interface name
2301 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2302 * @ifname: Interface name
2303 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2305 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2306 const char *bridge_ifname)
2309 sm->ifname = ifname;
2310 sm->bridge_ifname = bridge_ifname;
2316 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2317 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2318 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2320 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2328 * wpa_sm_set_param - Set WPA state machine parameters
2329 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2330 * @param: Parameter field
2331 * @value: Parameter value
2332 * Returns: 0 on success, -1 on failure
2334 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2343 case RSNA_PMK_LIFETIME:
2345 sm->dot11RSNAConfigPMKLifetime = value;
2349 case RSNA_PMK_REAUTH_THRESHOLD:
2350 if (value > 0 && value <= 100)
2351 sm->dot11RSNAConfigPMKReauthThreshold = value;
2355 case RSNA_SA_TIMEOUT:
2357 sm->dot11RSNAConfigSATimeout = value;
2361 case WPA_PARAM_PROTO:
2364 case WPA_PARAM_PAIRWISE:
2365 sm->pairwise_cipher = value;
2367 case WPA_PARAM_GROUP:
2368 sm->group_cipher = value;
2370 case WPA_PARAM_KEY_MGMT:
2371 sm->key_mgmt = value;
2373 #ifdef CONFIG_IEEE80211W
2374 case WPA_PARAM_MGMT_GROUP:
2375 sm->mgmt_group_cipher = value;
2377 #endif /* CONFIG_IEEE80211W */
2378 case WPA_PARAM_RSN_ENABLED:
2379 sm->rsn_enabled = value;
2393 * wpa_sm_get_param - Get WPA state machine parameters
2394 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2395 * @param: Parameter field
2396 * Returns: Parameter value
2398 unsigned int wpa_sm_get_param(struct wpa_sm *sm, enum wpa_sm_conf_params param)
2404 case RSNA_PMK_LIFETIME:
2405 return sm->dot11RSNAConfigPMKLifetime;
2406 case RSNA_PMK_REAUTH_THRESHOLD:
2407 return sm->dot11RSNAConfigPMKReauthThreshold;
2408 case RSNA_SA_TIMEOUT:
2409 return sm->dot11RSNAConfigSATimeout;
2410 case WPA_PARAM_PROTO:
2412 case WPA_PARAM_PAIRWISE:
2413 return sm->pairwise_cipher;
2414 case WPA_PARAM_GROUP:
2415 return sm->group_cipher;
2416 case WPA_PARAM_KEY_MGMT:
2417 return sm->key_mgmt;
2418 #ifdef CONFIG_IEEE80211W
2419 case WPA_PARAM_MGMT_GROUP:
2420 return sm->mgmt_group_cipher;
2421 #endif /* CONFIG_IEEE80211W */
2422 case WPA_PARAM_RSN_ENABLED:
2423 return sm->rsn_enabled;
2431 * wpa_sm_get_status - Get WPA state machine
2432 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2433 * @buf: Buffer for status information
2434 * @buflen: Maximum buffer length
2435 * @verbose: Whether to include verbose status information
2436 * Returns: Number of bytes written to buf.
2438 * Query WPA state machine for status information. This function fills in
2439 * a text area with current status information. If the buffer (buf) is not
2440 * large enough, status information will be truncated to fit the buffer.
2442 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2445 char *pos = buf, *end = buf + buflen;
2448 ret = os_snprintf(pos, end - pos,
2449 "pairwise_cipher=%s\n"
2452 wpa_cipher_txt(sm->pairwise_cipher),
2453 wpa_cipher_txt(sm->group_cipher),
2454 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
2455 if (ret < 0 || ret >= end - pos)
2459 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
2460 struct wpa_ie_data rsn;
2461 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
2463 rsn.capabilities & (WPA_CAPABILITY_MFPR |
2464 WPA_CAPABILITY_MFPC)) {
2465 ret = os_snprintf(pos, end - pos, "pmf=%d\n",
2467 WPA_CAPABILITY_MFPR) ? 2 : 1);
2468 if (ret < 0 || ret >= end - pos)
2478 int wpa_sm_pmf_enabled(struct wpa_sm *sm)
2480 struct wpa_ie_data rsn;
2482 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
2485 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
2486 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
2494 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2495 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2496 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2497 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2498 * Returns: 0 on success, -1 on failure
2500 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2508 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2513 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2514 wpa_ie, *wpa_ie_len);
2516 if (sm->assoc_wpa_ie == NULL) {
2518 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2519 * the correct version of the IE even if PMKSA caching is
2520 * aborted (which would remove PMKID from IE generation).
2522 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2523 if (sm->assoc_wpa_ie == NULL)
2526 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2527 sm->assoc_wpa_ie_len = *wpa_ie_len;
2535 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2536 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2537 * @ie: Pointer to IE data (starting from id)
2539 * Returns: 0 on success, -1 on failure
2541 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2542 * Request frame. The IE will be used to override the default value generated
2543 * with wpa_sm_set_assoc_wpa_ie_default().
2545 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2550 os_free(sm->assoc_wpa_ie);
2551 if (ie == NULL || len == 0) {
2552 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2553 "WPA: clearing own WPA/RSN IE");
2554 sm->assoc_wpa_ie = NULL;
2555 sm->assoc_wpa_ie_len = 0;
2557 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2558 sm->assoc_wpa_ie = os_malloc(len);
2559 if (sm->assoc_wpa_ie == NULL)
2562 os_memcpy(sm->assoc_wpa_ie, ie, len);
2563 sm->assoc_wpa_ie_len = len;
2571 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2572 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2573 * @ie: Pointer to IE data (starting from id)
2575 * Returns: 0 on success, -1 on failure
2577 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2580 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2585 os_free(sm->ap_wpa_ie);
2586 if (ie == NULL || len == 0) {
2587 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2588 "WPA: clearing AP WPA IE");
2589 sm->ap_wpa_ie = NULL;
2590 sm->ap_wpa_ie_len = 0;
2592 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2593 sm->ap_wpa_ie = os_malloc(len);
2594 if (sm->ap_wpa_ie == NULL)
2597 os_memcpy(sm->ap_wpa_ie, ie, len);
2598 sm->ap_wpa_ie_len = len;
2606 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2607 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2608 * @ie: Pointer to IE data (starting from id)
2610 * Returns: 0 on success, -1 on failure
2612 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2615 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2620 os_free(sm->ap_rsn_ie);
2621 if (ie == NULL || len == 0) {
2622 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2623 "WPA: clearing AP RSN IE");
2624 sm->ap_rsn_ie = NULL;
2625 sm->ap_rsn_ie_len = 0;
2627 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2628 sm->ap_rsn_ie = os_malloc(len);
2629 if (sm->ap_rsn_ie == NULL)
2632 os_memcpy(sm->ap_rsn_ie, ie, len);
2633 sm->ap_rsn_ie_len = len;
2641 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2642 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2643 * @data: Pointer to data area for parsing results
2644 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2646 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2647 * parsed data into data.
2649 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2654 if (sm->assoc_wpa_ie == NULL) {
2655 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2656 "WPA: No WPA/RSN IE available from association info");
2659 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2665 int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
2667 return pmksa_cache_list(sm->pmksa, buf, len);
2671 void wpa_sm_drop_sa(struct wpa_sm *sm)
2673 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
2676 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2677 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2678 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2682 int wpa_sm_has_ptk(struct wpa_sm *sm)
2690 void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
2692 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
2696 void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
2698 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
2703 int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
2705 struct wpa_gtk_data gd;
2706 #ifdef CONFIG_IEEE80211W
2707 struct wpa_igtk_kde igd;
2709 #endif /* CONFIG_IEEE80211W */
2711 u8 keylen; /* plaintext key len */
2714 os_memset(&gd, 0, sizeof(gd));
2715 #ifdef CONFIG_IEEE80211W
2716 os_memset(&igd, 0, sizeof(igd));
2717 #endif /* CONFIG_IEEE80211W */
2719 keylen = wpa_cipher_key_len(sm->group_cipher);
2720 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
2721 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
2722 if (gd.alg == WPA_ALG_NONE) {
2723 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
2727 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
2729 keyinfo = WPA_GET_LE16(buf + 2);
2730 gd.gtk_len = keylen;
2731 if (gd.gtk_len != buf[4]) {
2732 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
2733 gd.gtk_len, buf[4]);
2736 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
2737 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
2738 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
2740 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
2742 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
2743 gd.gtk, gd.gtk_len);
2744 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc)) {
2745 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
2749 #ifdef CONFIG_IEEE80211W
2750 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
2751 os_memcpy(igd.keyid, buf + 2, 2);
2752 os_memcpy(igd.pn, buf + 4, 6);
2754 keyidx = WPA_GET_LE16(igd.keyid);
2755 os_memcpy(igd.igtk, buf + 10, WPA_IGTK_LEN);
2757 wpa_hexdump_key(MSG_DEBUG, "Install IGTK (WNM SLEEP)",
2758 igd.igtk, WPA_IGTK_LEN);
2759 if (wpa_sm_set_key(sm, WPA_ALG_IGTK, broadcast_ether_addr,
2760 keyidx, 0, igd.pn, sizeof(igd.pn),
2761 igd.igtk, WPA_IGTK_LEN) < 0) {
2762 wpa_printf(MSG_DEBUG, "Failed to install the IGTK in "
2766 #endif /* CONFIG_IEEE80211W */
2768 wpa_printf(MSG_DEBUG, "Unknown element id");
2774 #endif /* CONFIG_WNM */
2777 #ifdef CONFIG_PEERKEY
2778 int wpa_sm_rx_eapol_peerkey(struct wpa_sm *sm, const u8 *src_addr,
2779 const u8 *buf, size_t len)
2781 struct wpa_peerkey *peerkey;
2783 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
2784 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
2791 wpa_sm_rx_eapol(sm, src_addr, buf, len);
2795 #endif /* CONFIG_PEERKEY */
2800 int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
2802 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
2804 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
2808 #endif /* CONFIG_P2P */