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, sm->key_mgmt, ver, msg, msg_len, key_mic)) {
60 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
61 "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
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 wpa_key_mgmt_suite_b(sm->key_mgmt))
94 ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
95 else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
96 wpa_key_mgmt_sha256(sm->key_mgmt))
97 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
98 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
99 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
101 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
103 if (wpa_sm_get_bssid(sm, bssid) < 0) {
104 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
105 "Failed to read BSSID for EAPOL-Key request");
109 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
110 sizeof(*reply), &rlen, (void *) &reply);
114 reply->type = (sm->proto == WPA_PROTO_RSN ||
115 sm->proto == WPA_PROTO_OSEN) ?
116 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
117 key_info = WPA_KEY_INFO_REQUEST | ver;
119 key_info |= WPA_KEY_INFO_MIC;
121 key_info |= WPA_KEY_INFO_ERROR;
123 key_info |= WPA_KEY_INFO_KEY_TYPE;
124 WPA_PUT_BE16(reply->key_info, key_info);
125 WPA_PUT_BE16(reply->key_length, 0);
126 os_memcpy(reply->replay_counter, sm->request_counter,
127 WPA_REPLAY_COUNTER_LEN);
128 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
130 WPA_PUT_BE16(reply->key_data_length, 0);
132 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
133 "WPA: Sending EAPOL-Key Request (error=%d "
134 "pairwise=%d ptk_set=%d len=%lu)",
135 error, pairwise, sm->ptk_set, (unsigned long) rlen);
136 wpa_eapol_key_send(sm, sm->ptk.kck, ver, bssid, ETH_P_EAPOL,
137 rbuf, rlen, key_info & WPA_KEY_INFO_MIC ?
138 reply->key_mic : NULL);
142 static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
144 #ifdef CONFIG_IEEE80211R
145 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
146 if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len))
147 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
148 "RSN: Cannot set low order 256 bits of MSK for key management offload");
150 #endif /* CONFIG_IEEE80211R */
151 if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len))
152 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
153 "RSN: Cannot set PMK for key management offload");
154 #ifdef CONFIG_IEEE80211R
156 #endif /* CONFIG_IEEE80211R */
160 static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
161 const unsigned char *src_addr,
164 int abort_cached = 0;
166 if (pmkid && !sm->cur_pmksa) {
167 /* When using drivers that generate RSN IE, wpa_supplicant may
168 * not have enough time to get the association information
169 * event before receiving this 1/4 message, so try to find a
170 * matching PMKSA cache entry here. */
171 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
174 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
175 "RSN: found matching PMKID from PMKSA cache");
177 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
178 "RSN: no matching PMKID found");
183 if (pmkid && sm->cur_pmksa &&
184 os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
185 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
186 wpa_sm_set_pmk_from_pmksa(sm);
187 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
188 sm->pmk, sm->pmk_len);
189 eapol_sm_notify_cached(sm->eapol);
190 #ifdef CONFIG_IEEE80211R
192 #endif /* CONFIG_IEEE80211R */
193 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
196 res = eapol_sm_get_key(sm->eapol, sm->pmk, PMK_LEN);
199 * EAP-LEAP is an exception from other EAP methods: it
200 * uses only 16-byte PMK.
202 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
205 #ifdef CONFIG_IEEE80211R
207 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
209 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
210 sm->xxkey_len = PMK_LEN;
211 os_memset(buf, 0, sizeof(buf));
213 #endif /* CONFIG_IEEE80211R */
216 struct rsn_pmksa_cache_entry *sa = NULL;
217 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
218 "machines", sm->pmk, pmk_len);
219 sm->pmk_len = pmk_len;
220 wpa_supplicant_key_mgmt_set_pmk(sm);
221 if (sm->proto == WPA_PROTO_RSN &&
222 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
223 !wpa_key_mgmt_ft(sm->key_mgmt)) {
224 sa = pmksa_cache_add(sm->pmksa,
227 src_addr, sm->own_addr,
231 if (!sm->cur_pmksa && pmkid &&
232 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL))
234 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
235 "RSN: the new PMK matches with the "
243 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
244 "WPA: Failed to get master session key from "
245 "EAPOL state machines - key handshake "
248 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
249 "RSN: Cancelled PMKSA caching "
251 sm->cur_pmksa = NULL;
253 } else if (!abort_cached) {
259 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
260 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
261 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
263 /* Send EAPOL-Start to trigger full EAP authentication. */
267 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
268 "RSN: no PMKSA entry found - trigger "
269 "full EAP authentication");
270 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
271 NULL, 0, &buflen, NULL);
273 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
287 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
288 * @sm: Pointer to WPA state machine data from wpa_sm_init()
289 * @dst: Destination address for the frame
290 * @key: Pointer to the EAPOL-Key frame header
291 * @ver: Version bits from EAPOL-Key Key Info
292 * @nonce: Nonce value for the EAPOL-Key frame
293 * @wpa_ie: WPA/RSN IE
294 * @wpa_ie_len: Length of the WPA/RSN IE
295 * @ptk: PTK to use for keyed hash and encryption
296 * Returns: 0 on success, -1 on failure
298 int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
299 const struct wpa_eapol_key *key,
300 int ver, const u8 *nonce,
301 const u8 *wpa_ie, size_t wpa_ie_len,
305 struct wpa_eapol_key *reply;
307 u8 *rsn_ie_buf = NULL;
309 if (wpa_ie == NULL) {
310 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
311 "cannot generate msg 2/4");
315 #ifdef CONFIG_IEEE80211R
316 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
320 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
321 * FTIE from (Re)Association Response.
323 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
324 sm->assoc_resp_ies_len);
325 if (rsn_ie_buf == NULL)
327 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
328 res = wpa_insert_pmkid(rsn_ie_buf, wpa_ie_len,
336 if (sm->assoc_resp_ies) {
337 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
338 sm->assoc_resp_ies_len);
339 wpa_ie_len += sm->assoc_resp_ies_len;
344 #endif /* CONFIG_IEEE80211R */
346 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
348 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
349 NULL, sizeof(*reply) + wpa_ie_len,
350 &rlen, (void *) &reply);
356 reply->type = (sm->proto == WPA_PROTO_RSN ||
357 sm->proto == WPA_PROTO_OSEN) ?
358 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
359 WPA_PUT_BE16(reply->key_info,
360 ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
361 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
362 WPA_PUT_BE16(reply->key_length, 0);
364 os_memcpy(reply->key_length, key->key_length, 2);
365 os_memcpy(reply->replay_counter, key->replay_counter,
366 WPA_REPLAY_COUNTER_LEN);
367 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
368 WPA_REPLAY_COUNTER_LEN);
370 WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
371 os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
374 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
376 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
377 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
378 rbuf, rlen, reply->key_mic);
384 static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
385 const struct wpa_eapol_key *key,
388 size_t ptk_len = wpa_cipher_key_len(sm->pairwise_cipher) + 32;
389 #ifdef CONFIG_IEEE80211R
390 if (wpa_key_mgmt_ft(sm->key_mgmt))
391 return wpa_derive_ptk_ft(sm, src_addr, key, ptk, ptk_len);
392 #endif /* CONFIG_IEEE80211R */
394 wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
395 sm->own_addr, sm->bssid, sm->snonce, key->key_nonce,
397 wpa_key_mgmt_sha256(sm->key_mgmt));
402 static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
403 const unsigned char *src_addr,
404 const struct wpa_eapol_key *key,
405 u16 ver, const u8 *key_data,
408 struct wpa_eapol_ie_parse ie;
411 u8 *kde, *kde_buf = NULL;
414 if (wpa_sm_get_network_ctx(sm) == NULL) {
415 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
416 "found (msg 1 of 4)");
420 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
421 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
422 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
424 os_memset(&ie, 0, sizeof(ie));
426 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
427 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
428 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
429 key_data, key_data_len);
430 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
433 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
434 "Authenticator", ie.pmkid, PMKID_LEN);
438 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
440 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
441 "msg 1/4 - requesting full EAP authentication");
447 if (sm->renew_snonce) {
448 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
449 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
450 "WPA: Failed to get random data for SNonce");
453 sm->renew_snonce = 0;
454 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
455 sm->snonce, WPA_NONCE_LEN);
458 /* Calculate PTK which will be stored as a temporary PTK until it has
459 * been verified when processing message 3/4. */
461 wpa_derive_ptk(sm, src_addr, key, ptk);
462 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
464 /* Supplicant: swap tx/rx Mic keys */
465 os_memcpy(buf, ptk->u.auth.tx_mic_key, 8);
466 os_memcpy(ptk->u.auth.tx_mic_key, ptk->u.auth.rx_mic_key, 8);
467 os_memcpy(ptk->u.auth.rx_mic_key, buf, 8);
468 os_memset(buf, 0, sizeof(buf));
472 kde = sm->assoc_wpa_ie;
473 kde_len = sm->assoc_wpa_ie_len;
477 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
480 wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
481 "into EAPOL-Key 2/4");
482 os_memcpy(kde_buf, kde, kde_len);
485 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
486 *pos++ = RSN_SELECTOR_LEN + 1;
487 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
488 pos += RSN_SELECTOR_LEN;
493 #endif /* CONFIG_P2P */
495 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
500 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
505 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
509 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
511 struct wpa_sm *sm = eloop_ctx;
512 rsn_preauth_candidate_process(sm);
516 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
517 const u8 *addr, int secure)
519 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
520 "WPA: Key negotiation completed with "
521 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
522 wpa_cipher_txt(sm->pairwise_cipher),
523 wpa_cipher_txt(sm->group_cipher));
524 wpa_sm_cancel_auth_timeout(sm);
525 wpa_sm_set_state(sm, WPA_COMPLETED);
528 wpa_sm_mlme_setprotection(
529 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
530 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
531 eapol_sm_notify_portValid(sm->eapol, TRUE);
532 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
533 eapol_sm_notify_eap_success(sm->eapol, TRUE);
535 * Start preauthentication after a short wait to avoid a
536 * possible race condition between the data receive and key
537 * configuration after the 4-Way Handshake. This increases the
538 * likelihood of the first preauth EAPOL-Start frame getting to
541 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
544 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
545 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
546 "RSN: Authenticator accepted "
547 "opportunistic PMKSA entry - marking it valid");
548 sm->cur_pmksa->opportunistic = 0;
551 #ifdef CONFIG_IEEE80211R
552 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
553 /* Prepare for the next transition */
554 wpa_ft_prepare_auth_request(sm, NULL);
556 #endif /* CONFIG_IEEE80211R */
560 static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
562 struct wpa_sm *sm = eloop_ctx;
563 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
564 wpa_sm_key_request(sm, 0, 1);
568 static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
569 const struct wpa_eapol_key *key)
574 u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
576 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
577 "WPA: Installing PTK to the driver");
579 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
580 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
581 "Suite: NONE - do not use pairwise keys");
585 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
586 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
587 "WPA: Unsupported pairwise cipher %d",
588 sm->pairwise_cipher);
592 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
593 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
594 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
596 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
599 key_rsc = key->key_rsc;
600 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
603 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
604 (u8 *) sm->ptk.tk1, keylen) < 0) {
605 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
606 "WPA: Failed to set PTK to the "
607 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
608 alg, keylen, MAC2STR(sm->bssid));
612 if (sm->wpa_ptk_rekey) {
613 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
614 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
622 static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
624 int keylen, int maxkeylen,
630 *alg = wpa_cipher_to_alg(group_cipher);
631 if (*alg == WPA_ALG_NONE) {
632 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
633 "WPA: Unsupported Group Cipher %d",
637 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
639 klen = wpa_cipher_key_len(group_cipher);
640 if (keylen != klen || maxkeylen < klen) {
641 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
642 "WPA: Unsupported %s Group Cipher key length %d (%d)",
643 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
650 struct wpa_gtk_data {
652 int tx, key_rsc_len, keyidx;
658 static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
659 const struct wpa_gtk_data *gd,
662 const u8 *_gtk = gd->gtk;
665 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
666 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
667 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
668 gd->keyidx, gd->tx, gd->gtk_len);
669 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
670 if (sm->group_cipher == WPA_CIPHER_TKIP) {
671 /* Swap Tx/Rx keys for Michael MIC */
672 os_memcpy(gtk_buf, gd->gtk, 16);
673 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
674 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
677 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
678 if (wpa_sm_set_key(sm, gd->alg, NULL,
679 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
680 _gtk, gd->gtk_len) < 0) {
681 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
682 "WPA: Failed to set GTK to the driver "
684 os_memset(gtk_buf, 0, sizeof(gtk_buf));
687 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
688 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
689 _gtk, gd->gtk_len) < 0) {
690 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
691 "WPA: Failed to set GTK to "
692 "the driver (alg=%d keylen=%d keyidx=%d)",
693 gd->alg, gd->gtk_len, gd->keyidx);
694 os_memset(gtk_buf, 0, sizeof(gtk_buf));
697 os_memset(gtk_buf, 0, sizeof(gtk_buf));
703 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
706 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
707 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
708 * seemed to set this bit (incorrectly, since Tx is only when
709 * doing Group Key only APs) and without this workaround, the
710 * data connection does not work because wpa_supplicant
711 * configured non-zero keyidx to be used for unicast. */
712 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
713 "WPA: Tx bit set for GTK, but pairwise "
714 "keys are used - ignore Tx bit");
721 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
722 const struct wpa_eapol_key *key,
723 const u8 *gtk, size_t gtk_len,
726 struct wpa_gtk_data gd;
729 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
731 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
732 * Reserved [bits 0-7]
736 os_memset(&gd, 0, sizeof(gd));
737 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
740 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
743 gd.keyidx = gtk[0] & 0x3;
744 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
745 !!(gtk[0] & BIT(2)));
749 os_memcpy(gd.gtk, gtk, gtk_len);
750 gd.gtk_len = gtk_len;
752 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
753 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
755 &gd.key_rsc_len, &gd.alg) ||
756 wpa_supplicant_install_gtk(sm, &gd, key->key_rsc))) {
757 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
758 "RSN: Failed to install GTK");
759 os_memset(&gd, 0, sizeof(gd));
762 os_memset(&gd, 0, sizeof(gd));
764 wpa_supplicant_key_neg_complete(sm, sm->bssid,
765 key_info & WPA_KEY_INFO_SECURE);
770 static int ieee80211w_set_keys(struct wpa_sm *sm,
771 struct wpa_eapol_ie_parse *ie)
773 #ifdef CONFIG_IEEE80211W
774 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
779 const struct wpa_igtk_kde *igtk;
781 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
782 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
784 igtk = (const struct wpa_igtk_kde *) ie->igtk;
785 keyidx = WPA_GET_LE16(igtk->keyid);
786 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: IGTK keyid %d "
787 "pn %02x%02x%02x%02x%02x%02x",
788 keyidx, MAC2STR(igtk->pn));
789 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
792 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
793 "WPA: Invalid IGTK KeyID %d", keyidx);
796 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
797 broadcast_ether_addr,
798 keyidx, 0, igtk->pn, sizeof(igtk->pn),
799 igtk->igtk, len) < 0) {
800 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
801 "WPA: Failed to configure IGTK to the driver");
807 #else /* CONFIG_IEEE80211W */
809 #endif /* CONFIG_IEEE80211W */
813 static void wpa_report_ie_mismatch(struct wpa_sm *sm,
814 const char *reason, const u8 *src_addr,
815 const u8 *wpa_ie, size_t wpa_ie_len,
816 const u8 *rsn_ie, size_t rsn_ie_len)
818 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
819 reason, MAC2STR(src_addr));
822 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
823 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
826 if (!sm->ap_wpa_ie) {
827 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
828 "WPA: No WPA IE in Beacon/ProbeResp");
830 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
835 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
836 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
839 if (!sm->ap_rsn_ie) {
840 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
841 "WPA: No RSN IE in Beacon/ProbeResp");
843 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
847 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
851 #ifdef CONFIG_IEEE80211R
853 static int ft_validate_mdie(struct wpa_sm *sm,
854 const unsigned char *src_addr,
855 struct wpa_eapol_ie_parse *ie,
856 const u8 *assoc_resp_mdie)
858 struct rsn_mdie *mdie;
860 mdie = (struct rsn_mdie *) (ie->mdie + 2);
861 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
862 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
863 MOBILITY_DOMAIN_ID_LEN) != 0) {
864 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
865 "not match with the current mobility domain");
869 if (assoc_resp_mdie &&
870 (assoc_resp_mdie[1] != ie->mdie[1] ||
871 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
872 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
873 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
874 ie->mdie, 2 + ie->mdie[1]);
875 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
876 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
884 static int ft_validate_ftie(struct wpa_sm *sm,
885 const unsigned char *src_addr,
886 struct wpa_eapol_ie_parse *ie,
887 const u8 *assoc_resp_ftie)
889 if (ie->ftie == NULL) {
890 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
891 "FT: No FTIE in EAPOL-Key msg 3/4");
895 if (assoc_resp_ftie == NULL)
898 if (assoc_resp_ftie[1] != ie->ftie[1] ||
899 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
900 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
901 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
902 ie->ftie, 2 + ie->ftie[1]);
903 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
904 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
912 static int ft_validate_rsnie(struct wpa_sm *sm,
913 const unsigned char *src_addr,
914 struct wpa_eapol_ie_parse *ie)
916 struct wpa_ie_data rsn;
922 * Verify that PMKR1Name from EAPOL-Key message 3/4
923 * matches with the value we derived.
925 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
926 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
927 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
928 "FT 4-way handshake message 3/4");
932 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
934 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
935 "FT: PMKR1Name mismatch in "
936 "FT 4-way handshake message 3/4");
937 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
938 rsn.pmkid, WPA_PMK_NAME_LEN);
939 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
940 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
948 static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
949 const unsigned char *src_addr,
950 struct wpa_eapol_ie_parse *ie)
952 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
954 if (sm->assoc_resp_ies) {
955 pos = sm->assoc_resp_ies;
956 end = pos + sm->assoc_resp_ies_len;
957 while (pos + 2 < end) {
958 if (pos + 2 + pos[1] > end)
961 case WLAN_EID_MOBILITY_DOMAIN:
964 case WLAN_EID_FAST_BSS_TRANSITION:
972 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
973 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
974 ft_validate_rsnie(sm, src_addr, ie) < 0)
980 #endif /* CONFIG_IEEE80211R */
983 static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
984 const unsigned char *src_addr,
985 struct wpa_eapol_ie_parse *ie)
987 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
988 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
989 "WPA: No WPA/RSN IE for this AP known. "
990 "Trying to get from scan results");
991 if (wpa_sm_get_beacon_ie(sm) < 0) {
992 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
993 "WPA: Could not find AP from "
996 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
997 "WPA: Found the current AP from "
998 "updated scan results");
1002 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1003 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1004 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1005 "with IE in Beacon/ProbeResp (no IE?)",
1006 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1007 ie->rsn_ie, ie->rsn_ie_len);
1011 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1012 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1013 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1014 (ie->rsn_ie && sm->ap_rsn_ie &&
1015 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1016 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1017 ie->rsn_ie, ie->rsn_ie_len))) {
1018 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1019 "with IE in Beacon/ProbeResp",
1020 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1021 ie->rsn_ie, ie->rsn_ie_len);
1025 if (sm->proto == WPA_PROTO_WPA &&
1026 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1027 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1028 "detected - RSN was enabled and RSN IE "
1029 "was in msg 3/4, but not in "
1031 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1032 ie->rsn_ie, ie->rsn_ie_len);
1036 #ifdef CONFIG_IEEE80211R
1037 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1038 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1040 #endif /* CONFIG_IEEE80211R */
1047 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1048 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1049 * @dst: Destination address for the frame
1050 * @key: Pointer to the EAPOL-Key frame header
1051 * @ver: Version bits from EAPOL-Key Key Info
1052 * @key_info: Key Info
1053 * @ptk: PTK to use for keyed hash and encryption
1054 * Returns: 0 on success, -1 on failure
1056 int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1057 const struct wpa_eapol_key *key,
1058 u16 ver, u16 key_info,
1059 struct wpa_ptk *ptk)
1062 struct wpa_eapol_key *reply;
1065 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1066 sizeof(*reply), &rlen, (void *) &reply);
1070 reply->type = (sm->proto == WPA_PROTO_RSN ||
1071 sm->proto == WPA_PROTO_OSEN) ?
1072 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1073 key_info &= WPA_KEY_INFO_SECURE;
1074 key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
1075 WPA_PUT_BE16(reply->key_info, key_info);
1076 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
1077 WPA_PUT_BE16(reply->key_length, 0);
1079 os_memcpy(reply->key_length, key->key_length, 2);
1080 os_memcpy(reply->replay_counter, key->replay_counter,
1081 WPA_REPLAY_COUNTER_LEN);
1083 WPA_PUT_BE16(reply->key_data_length, 0);
1085 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
1086 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
1087 rbuf, rlen, reply->key_mic);
1093 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1094 const struct wpa_eapol_key *key,
1095 u16 ver, const u8 *key_data,
1096 size_t key_data_len)
1098 u16 key_info, keylen;
1099 struct wpa_eapol_ie_parse ie;
1101 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1102 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
1103 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1105 key_info = WPA_GET_BE16(key->key_info);
1107 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1108 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
1110 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1111 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1112 "WPA: GTK IE in unencrypted key data");
1115 #ifdef CONFIG_IEEE80211W
1116 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1117 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1118 "WPA: IGTK KDE in unencrypted key data");
1123 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1124 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1125 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
1126 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1127 "WPA: Invalid IGTK KDE length %lu",
1128 (unsigned long) ie.igtk_len);
1131 #endif /* CONFIG_IEEE80211W */
1133 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1136 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1137 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1138 "WPA: ANonce from message 1 of 4-Way Handshake "
1139 "differs from 3 of 4-Way Handshake - drop packet (src="
1140 MACSTR ")", MAC2STR(sm->bssid));
1144 keylen = WPA_GET_BE16(key->key_length);
1145 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1146 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1147 "WPA: Invalid %s key length %d (src=" MACSTR
1148 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1149 MAC2STR(sm->bssid));
1154 if (ie.ip_addr_alloc) {
1155 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1156 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1157 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1159 #endif /* CONFIG_P2P */
1161 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
1166 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1167 * for the next 4-Way Handshake. If msg 3 is received again, the old
1168 * SNonce will still be used to avoid changing PTK. */
1169 sm->renew_snonce = 1;
1171 if (key_info & WPA_KEY_INFO_INSTALL) {
1172 if (wpa_supplicant_install_ptk(sm, key))
1176 if (key_info & WPA_KEY_INFO_SECURE) {
1177 wpa_sm_mlme_setprotection(
1178 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1179 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1180 eapol_sm_notify_portValid(sm->eapol, TRUE);
1182 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1184 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
1185 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1186 key_info & WPA_KEY_INFO_SECURE);
1187 } else if (ie.gtk &&
1188 wpa_supplicant_pairwise_gtk(sm, key,
1189 ie.gtk, ie.gtk_len, key_info) < 0) {
1190 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1191 "RSN: Failed to configure GTK");
1195 if (ieee80211w_set_keys(sm, &ie) < 0) {
1196 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1197 "RSN: Failed to configure IGTK");
1202 wpa_sm_set_rekey_offload(sm);
1204 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1205 struct rsn_pmksa_cache_entry *sa;
1207 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
1208 sm->ptk.kck, sizeof(sm->ptk.kck),
1209 sm->bssid, sm->own_addr,
1210 sm->network_ctx, sm->key_mgmt);
1218 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1222 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1226 struct wpa_gtk_data *gd)
1229 struct wpa_eapol_ie_parse ie;
1231 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
1232 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1234 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1235 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1236 "WPA: GTK IE in unencrypted key data");
1239 if (ie.gtk == NULL) {
1240 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1241 "WPA: No GTK IE in Group Key msg 1/2");
1244 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1246 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1247 gd->gtk_len, maxkeylen,
1248 &gd->key_rsc_len, &gd->alg))
1251 wpa_hexdump(MSG_DEBUG, "RSN: received GTK in group key handshake",
1252 ie.gtk, ie.gtk_len);
1253 gd->keyidx = ie.gtk[0] & 0x3;
1254 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1255 !!(ie.gtk[0] & BIT(2)));
1256 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1257 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1258 "RSN: Too long GTK in GTK IE (len=%lu)",
1259 (unsigned long) ie.gtk_len - 2);
1262 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1264 if (ieee80211w_set_keys(sm, &ie) < 0)
1265 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1266 "RSN: Failed to configure IGTK");
1272 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1273 const struct wpa_eapol_key *key,
1275 size_t key_data_len, u16 key_info,
1276 u16 ver, struct wpa_gtk_data *gd)
1281 gtk_len = WPA_GET_BE16(key->key_length);
1282 maxkeylen = key_data_len;
1283 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1284 if (maxkeylen < 8) {
1285 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1286 "WPA: Too short maxkeylen (%lu)",
1287 (unsigned long) maxkeylen);
1293 if (gtk_len > maxkeylen ||
1294 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1296 &gd->key_rsc_len, &gd->alg))
1299 gd->gtk_len = gtk_len;
1300 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1301 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1302 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1304 if (key_data_len > sizeof(gd->gtk)) {
1305 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1306 "WPA: RC4 key data too long (%lu)",
1307 (unsigned long) key_data_len);
1310 os_memcpy(ek, key->key_iv, 16);
1311 os_memcpy(ek + 16, sm->ptk.kek, 16);
1312 os_memcpy(gd->gtk, key_data, key_data_len);
1313 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
1314 os_memset(ek, 0, sizeof(ek));
1315 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1319 os_memset(ek, 0, sizeof(ek));
1320 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1321 if (maxkeylen % 8) {
1322 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1323 "WPA: Unsupported AES-WRAP len %lu",
1324 (unsigned long) maxkeylen);
1327 if (maxkeylen > sizeof(gd->gtk)) {
1328 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1329 "WPA: AES-WRAP key data "
1330 "too long (keydatalen=%lu maxkeylen=%lu)",
1331 (unsigned long) key_data_len,
1332 (unsigned long) maxkeylen);
1335 if (aes_unwrap(sm->ptk.kek, 16, maxkeylen / 8, key_data,
1337 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1338 "WPA: AES unwrap failed - could not decrypt "
1343 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1344 "WPA: Unsupported key_info type %d", ver);
1347 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1348 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1353 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1354 const struct wpa_eapol_key *key,
1355 int ver, u16 key_info)
1358 struct wpa_eapol_key *reply;
1361 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1362 sizeof(*reply), &rlen, (void *) &reply);
1366 reply->type = (sm->proto == WPA_PROTO_RSN ||
1367 sm->proto == WPA_PROTO_OSEN) ?
1368 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1369 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1370 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1371 WPA_PUT_BE16(reply->key_info, key_info);
1372 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
1373 WPA_PUT_BE16(reply->key_length, 0);
1375 os_memcpy(reply->key_length, key->key_length, 2);
1376 os_memcpy(reply->replay_counter, key->replay_counter,
1377 WPA_REPLAY_COUNTER_LEN);
1379 WPA_PUT_BE16(reply->key_data_length, 0);
1381 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1382 wpa_eapol_key_send(sm, sm->ptk.kck, ver, sm->bssid, ETH_P_EAPOL,
1383 rbuf, rlen, reply->key_mic);
1389 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1390 const unsigned char *src_addr,
1391 const struct wpa_eapol_key *key,
1393 size_t key_data_len, u16 ver)
1397 struct wpa_gtk_data gd;
1399 os_memset(&gd, 0, sizeof(gd));
1401 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1402 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1403 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1405 key_info = WPA_GET_BE16(key->key_info);
1407 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
1408 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1409 key_data_len, key_info,
1412 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1414 key_info, ver, &gd);
1417 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1422 if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc) ||
1423 wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
1427 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1428 "completed with " MACSTR " [GTK=%s]",
1429 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1430 wpa_sm_cancel_auth_timeout(sm);
1431 wpa_sm_set_state(sm, WPA_COMPLETED);
1433 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1435 WPA_KEY_INFO_SECURE);
1438 wpa_sm_set_rekey_offload(sm);
1443 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1447 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1448 struct wpa_eapol_key *key,
1450 const u8 *buf, size_t len)
1455 os_memcpy(mic, key->key_mic, 16);
1457 os_memset(key->key_mic, 0, 16);
1458 wpa_eapol_key_mic(sm->tptk.kck, sm->key_mgmt, ver, buf, len,
1460 if (os_memcmp_const(mic, key->key_mic, 16) != 0) {
1461 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1462 "WPA: Invalid EAPOL-Key MIC "
1463 "when using TPTK - ignoring TPTK");
1468 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1469 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
1473 if (!ok && sm->ptk_set) {
1474 os_memset(key->key_mic, 0, 16);
1475 wpa_eapol_key_mic(sm->ptk.kck, sm->key_mgmt, ver, buf, len,
1477 if (os_memcmp_const(mic, key->key_mic, 16) != 0) {
1478 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1479 "WPA: Invalid EAPOL-Key MIC - "
1487 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1488 "WPA: Could not verify EAPOL-Key MIC - "
1493 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1494 WPA_REPLAY_COUNTER_LEN);
1495 sm->rx_replay_counter_set = 1;
1500 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1501 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
1502 struct wpa_eapol_key *key, u16 ver,
1503 u8 *key_data, size_t *key_data_len)
1505 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
1506 key_data, *key_data_len);
1508 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1509 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1514 /* Decrypt key data here so that this operation does not need
1515 * to be implemented separately for each message type. */
1516 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1518 os_memcpy(ek, key->key_iv, 16);
1519 os_memcpy(ek + 16, sm->ptk.kek, 16);
1520 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
1521 os_memset(ek, 0, sizeof(ek));
1522 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1526 os_memset(ek, 0, sizeof(ek));
1527 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1528 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
1529 sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
1530 wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1532 if (*key_data_len < 8 || *key_data_len % 8) {
1533 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1534 "WPA: Unsupported AES-WRAP len %u",
1535 (unsigned int) *key_data_len);
1538 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
1539 buf = os_malloc(*key_data_len);
1541 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1542 "WPA: No memory for AES-UNWRAP buffer");
1545 if (aes_unwrap(sm->ptk.kek, 16, *key_data_len / 8,
1548 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1549 "WPA: AES unwrap failed - "
1550 "could not decrypt EAPOL-Key key data");
1553 os_memcpy(key_data, buf, *key_data_len);
1555 WPA_PUT_BE16(key->key_data_length, *key_data_len);
1557 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1558 "WPA: Unsupported key_info type %d", ver);
1561 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
1562 key_data, *key_data_len);
1568 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1569 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1571 void wpa_sm_aborted_cached(struct wpa_sm *sm)
1573 if (sm && sm->cur_pmksa) {
1574 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1575 "RSN: Cancelling PMKSA caching attempt");
1576 sm->cur_pmksa = NULL;
1581 static void wpa_eapol_key_dump(struct wpa_sm *sm,
1582 const struct wpa_eapol_key *key)
1584 #ifndef CONFIG_NO_STDOUT_DEBUG
1585 u16 key_info = WPA_GET_BE16(key->key_info);
1587 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1588 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1589 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1590 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1591 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1592 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1593 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1594 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1595 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1596 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1597 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1598 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1599 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1600 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1601 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1602 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1603 " key_length=%u key_data_length=%u",
1604 WPA_GET_BE16(key->key_length),
1605 WPA_GET_BE16(key->key_data_length));
1606 wpa_hexdump(MSG_DEBUG, " replay_counter",
1607 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1608 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1609 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1610 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1611 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
1612 wpa_hexdump(MSG_DEBUG, " key_mic", key->key_mic, 16);
1613 #endif /* CONFIG_NO_STDOUT_DEBUG */
1618 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1619 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1620 * @src_addr: Source MAC address of the EAPOL packet
1621 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1622 * @len: Length of the EAPOL frame
1623 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1625 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1626 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1627 * only processing WPA and WPA2 EAPOL-Key frames.
1629 * The received EAPOL-Key packets are validated and valid packets are replied
1630 * to. In addition, key material (PTK, GTK) is configured at the end of a
1631 * successful key handshake.
1633 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1634 const u8 *buf, size_t len)
1636 size_t plen, data_len, key_data_len;
1637 const struct ieee802_1x_hdr *hdr;
1638 struct wpa_eapol_key *key;
1642 struct wpa_peerkey *peerkey = NULL;
1645 #ifdef CONFIG_IEEE80211R
1646 sm->ft_completed = 0;
1647 #endif /* CONFIG_IEEE80211R */
1649 if (len < sizeof(*hdr) + sizeof(*key)) {
1650 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1651 "WPA: EAPOL frame too short to be a WPA "
1652 "EAPOL-Key (len %lu, expecting at least %lu)",
1653 (unsigned long) len,
1654 (unsigned long) sizeof(*hdr) + sizeof(*key));
1658 hdr = (const struct ieee802_1x_hdr *) buf;
1659 plen = be_to_host16(hdr->length);
1660 data_len = plen + sizeof(*hdr);
1661 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1662 "IEEE 802.1X RX: version=%d type=%d length=%lu",
1663 hdr->version, hdr->type, (unsigned long) plen);
1665 if (hdr->version < EAPOL_VERSION) {
1666 /* TODO: backwards compatibility */
1668 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1669 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1670 "WPA: EAPOL frame (type %u) discarded, "
1671 "not a Key frame", hdr->type);
1675 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
1676 if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
1677 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1678 "WPA: EAPOL frame payload size %lu "
1679 "invalid (frame size %lu)",
1680 (unsigned long) plen, (unsigned long) len);
1684 if (data_len < len) {
1685 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1686 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
1687 (unsigned long) len - data_len);
1691 * Make a copy of the frame since we need to modify the buffer during
1692 * MAC validation and Key Data decryption.
1694 tmp = os_malloc(data_len);
1697 os_memcpy(tmp, buf, data_len);
1698 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
1699 key_data = (u8 *) (key + 1);
1701 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1703 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1704 "WPA: EAPOL-Key type (%d) unknown, discarded",
1709 wpa_eapol_key_dump(sm, key);
1711 key_data_len = WPA_GET_BE16(key->key_data_length);
1712 if (key_data_len > plen - sizeof(struct wpa_eapol_key)) {
1713 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1714 "frame - key_data overflow (%u > %u)",
1715 (unsigned int) key_data_len,
1716 (unsigned int) (plen - sizeof(struct wpa_eapol_key)));
1720 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
1721 key_info = WPA_GET_BE16(key->key_info);
1722 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1723 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1724 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1725 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1726 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
1727 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
1728 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1729 sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
1730 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1731 "WPA: Unsupported EAPOL-Key descriptor version %d",
1736 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN &&
1737 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1738 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1739 "OSEN: Unsupported EAPOL-Key descriptor version %d",
1744 if (wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1745 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1746 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1747 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
1752 #ifdef CONFIG_IEEE80211R
1753 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1754 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1755 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1756 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1757 "FT: AP did not use AES-128-CMAC");
1761 #endif /* CONFIG_IEEE80211R */
1762 #ifdef CONFIG_IEEE80211W
1763 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
1764 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1765 sm->key_mgmt != WPA_KEY_MGMT_OSEN &&
1766 !wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1767 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1768 "WPA: AP did not use the "
1769 "negotiated AES-128-CMAC");
1773 #endif /* CONFIG_IEEE80211W */
1774 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
1775 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1776 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1777 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1778 "WPA: CCMP is used, but EAPOL-Key "
1779 "descriptor version (%d) is not 2", ver);
1780 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1781 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1782 /* Earlier versions of IEEE 802.11i did not explicitly
1783 * require version 2 descriptor for all EAPOL-Key
1784 * packets, so allow group keys to use version 1 if
1785 * CCMP is not used for them. */
1786 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1787 "WPA: Backwards compatibility: allow invalid "
1788 "version for non-CCMP group keys");
1789 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1790 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1791 "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
1794 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
1795 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1796 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1797 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1798 "WPA: GCMP is used, but EAPOL-Key "
1799 "descriptor version (%d) is not 2", ver);
1803 #ifdef CONFIG_PEERKEY
1804 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1805 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1809 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1810 if (!peerkey->initiator && peerkey->replay_counter_set &&
1811 os_memcmp(key->replay_counter, peerkey->replay_counter,
1812 WPA_REPLAY_COUNTER_LEN) <= 0) {
1813 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1814 "RSN: EAPOL-Key Replay Counter did not "
1815 "increase (STK) - dropping packet");
1817 } else if (peerkey->initiator) {
1818 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1819 os_memcpy(_tmp, key->replay_counter,
1820 WPA_REPLAY_COUNTER_LEN);
1821 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
1822 if (os_memcmp(_tmp, peerkey->replay_counter,
1823 WPA_REPLAY_COUNTER_LEN) != 0) {
1824 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1825 "RSN: EAPOL-Key Replay "
1826 "Counter did not match (STK) - "
1833 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
1834 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1835 "RSN: Ack bit in key_info from STK peer");
1838 #endif /* CONFIG_PEERKEY */
1840 if (!peerkey && sm->rx_replay_counter_set &&
1841 os_memcmp(key->replay_counter, sm->rx_replay_counter,
1842 WPA_REPLAY_COUNTER_LEN) <= 0) {
1843 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1844 "WPA: EAPOL-Key Replay Counter did not increase - "
1849 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
1850 #ifdef CONFIG_PEERKEY
1851 && (peerkey == NULL || !peerkey->initiator)
1852 #endif /* CONFIG_PEERKEY */
1854 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1855 "WPA: No Ack bit in key_info");
1859 if (key_info & WPA_KEY_INFO_REQUEST) {
1860 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1861 "WPA: EAPOL-Key with Request bit - dropped");
1865 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
1866 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
1869 #ifdef CONFIG_PEERKEY
1870 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
1871 peerkey_verify_eapol_key_mic(sm, peerkey, key, ver, tmp, data_len))
1873 #endif /* CONFIG_PEERKEY */
1875 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
1876 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1877 if (wpa_supplicant_decrypt_key_data(sm, key, ver, key_data,
1882 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1883 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
1884 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1885 "WPA: Ignored EAPOL-Key (Pairwise) with "
1886 "non-zero key index");
1890 /* PeerKey 4-Way Handshake */
1891 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver,
1892 key_data, key_data_len);
1893 } else if (key_info & WPA_KEY_INFO_MIC) {
1894 /* 3/4 4-Way Handshake */
1895 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
1898 /* 1/4 4-Way Handshake */
1899 wpa_supplicant_process_1_of_4(sm, src_addr, key,
1903 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1904 /* PeerKey SMK Handshake */
1905 peerkey_rx_eapol_smk(sm, src_addr, key, key_data_len, key_info,
1908 if (key_info & WPA_KEY_INFO_MIC) {
1909 /* 1/2 Group Key Handshake */
1910 wpa_supplicant_process_1_of_2(sm, src_addr, key,
1911 key_data, key_data_len,
1914 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1915 "WPA: EAPOL-Key (Group) without Mic bit - "
1928 #ifdef CONFIG_CTRL_IFACE
1929 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
1931 switch (sm->key_mgmt) {
1932 case WPA_KEY_MGMT_IEEE8021X:
1933 return ((sm->proto == WPA_PROTO_RSN ||
1934 sm->proto == WPA_PROTO_OSEN) ?
1935 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
1936 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
1937 case WPA_KEY_MGMT_PSK:
1938 return (sm->proto == WPA_PROTO_RSN ?
1939 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
1940 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
1941 #ifdef CONFIG_IEEE80211R
1942 case WPA_KEY_MGMT_FT_IEEE8021X:
1943 return RSN_AUTH_KEY_MGMT_FT_802_1X;
1944 case WPA_KEY_MGMT_FT_PSK:
1945 return RSN_AUTH_KEY_MGMT_FT_PSK;
1946 #endif /* CONFIG_IEEE80211R */
1947 #ifdef CONFIG_IEEE80211W
1948 case WPA_KEY_MGMT_IEEE8021X_SHA256:
1949 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
1950 case WPA_KEY_MGMT_PSK_SHA256:
1951 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
1952 #endif /* CONFIG_IEEE80211W */
1953 case WPA_KEY_MGMT_CCKM:
1954 return (sm->proto == WPA_PROTO_RSN ?
1955 RSN_AUTH_KEY_MGMT_CCKM:
1956 WPA_AUTH_KEY_MGMT_CCKM);
1957 case WPA_KEY_MGMT_WPA_NONE:
1958 return WPA_AUTH_KEY_MGMT_NONE;
1959 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
1960 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
1967 #define RSN_SUITE "%02x-%02x-%02x-%d"
1968 #define RSN_SUITE_ARG(s) \
1969 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
1972 * wpa_sm_get_mib - Dump text list of MIB entries
1973 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1974 * @buf: Buffer for the list
1975 * @buflen: Length of the buffer
1976 * Returns: Number of bytes written to buffer
1978 * This function is used fetch dot11 MIB variables.
1980 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
1982 char pmkid_txt[PMKID_LEN * 2 + 1];
1986 if (sm->cur_pmksa) {
1987 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
1988 sm->cur_pmksa->pmkid, PMKID_LEN);
1990 pmkid_txt[0] = '\0';
1992 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
1993 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
1994 sm->proto == WPA_PROTO_RSN)
1999 ret = os_snprintf(buf, buflen,
2000 "dot11RSNAOptionImplemented=TRUE\n"
2001 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2002 "dot11RSNAEnabled=%s\n"
2003 "dot11RSNAPreauthenticationEnabled=%s\n"
2004 "dot11RSNAConfigVersion=%d\n"
2005 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2006 "dot11RSNAConfigGroupCipherSize=%d\n"
2007 "dot11RSNAConfigPMKLifetime=%d\n"
2008 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2009 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2010 "dot11RSNAConfigSATimeout=%d\n",
2011 rsna ? "TRUE" : "FALSE",
2012 rsna ? "TRUE" : "FALSE",
2014 wpa_cipher_key_len(sm->group_cipher) * 8,
2015 sm->dot11RSNAConfigPMKLifetime,
2016 sm->dot11RSNAConfigPMKReauthThreshold,
2017 sm->dot11RSNAConfigSATimeout);
2018 if (ret < 0 || (size_t) ret >= buflen)
2023 buf + len, buflen - len,
2024 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2025 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2026 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2027 "dot11RSNAPMKIDUsed=%s\n"
2028 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2029 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2030 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2031 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2032 "dot11RSNA4WayHandshakeFailures=%u\n",
2033 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
2034 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2035 sm->pairwise_cipher)),
2036 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2039 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
2040 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2041 sm->pairwise_cipher)),
2042 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2044 sm->dot11RSNA4WayHandshakeFailures);
2045 if (ret >= 0 && (size_t) ret < buflen)
2050 #endif /* CONFIG_CTRL_IFACE */
2053 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
2054 void *ctx, enum pmksa_free_reason reason)
2056 struct wpa_sm *sm = ctx;
2059 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2060 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2062 if (sm->cur_pmksa == entry) {
2063 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2064 "RSN: %s current PMKSA entry",
2065 reason == PMKSA_REPLACE ? "replaced" : "removed");
2066 pmksa_cache_clear_current(sm);
2069 * If an entry is simply being replaced, there's no need to
2070 * deauthenticate because it will be immediately re-added.
2071 * This happens when EAP authentication is completed again
2072 * (reauth or failed PMKSA caching attempt).
2074 if (reason != PMKSA_REPLACE)
2078 if (reason == PMKSA_EXPIRE &&
2079 (sm->pmk_len == entry->pmk_len &&
2080 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2081 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2082 "RSN: deauthenticating due to expired PMK");
2083 pmksa_cache_clear_current(sm);
2088 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2089 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2095 * wpa_sm_init - Initialize WPA state machine
2096 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2097 * Returns: Pointer to the allocated WPA state machine data
2099 * This function is used to allocate a new WPA state machine and the returned
2100 * value is passed to all WPA state machine calls.
2102 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2106 sm = os_zalloc(sizeof(*sm));
2109 dl_list_init(&sm->pmksa_candidates);
2110 sm->renew_snonce = 1;
2113 sm->dot11RSNAConfigPMKLifetime = 43200;
2114 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2115 sm->dot11RSNAConfigSATimeout = 60;
2117 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2118 if (sm->pmksa == NULL) {
2119 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2120 "RSN: PMKSA cache initialization failed");
2130 * wpa_sm_deinit - Deinitialize WPA state machine
2131 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2133 void wpa_sm_deinit(struct wpa_sm *sm)
2137 pmksa_cache_deinit(sm->pmksa);
2138 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2139 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2140 os_free(sm->assoc_wpa_ie);
2141 os_free(sm->ap_wpa_ie);
2142 os_free(sm->ap_rsn_ie);
2145 #ifdef CONFIG_IEEE80211R
2146 os_free(sm->assoc_resp_ies);
2147 #endif /* CONFIG_IEEE80211R */
2153 * wpa_sm_notify_assoc - Notify WPA state machine about association
2154 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2155 * @bssid: The BSSID of the new association
2157 * This function is called to let WPA state machine know that the connection
2160 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2167 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2168 "WPA: Association event - clear replay counter");
2169 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2170 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2171 sm->rx_replay_counter_set = 0;
2172 sm->renew_snonce = 1;
2173 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2174 rsn_preauth_deinit(sm);
2176 #ifdef CONFIG_IEEE80211R
2177 if (wpa_ft_is_completed(sm)) {
2179 * Clear portValid to kick EAPOL state machine to re-enter
2180 * AUTHENTICATED state to get the EAPOL port Authorized.
2182 eapol_sm_notify_portValid(sm->eapol, FALSE);
2183 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2185 /* Prepare for the next transition */
2186 wpa_ft_prepare_auth_request(sm, NULL);
2190 #endif /* CONFIG_IEEE80211R */
2194 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2195 * this is not part of a Fast BSS Transition.
2197 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2199 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2201 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2206 #endif /* CONFIG_TDLS */
2209 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2210 #endif /* CONFIG_P2P */
2215 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2216 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2218 * This function is called to let WPA state machine know that the connection
2219 * was lost. This will abort any existing pre-authentication session.
2221 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2224 rsn_preauth_deinit(sm);
2225 pmksa_cache_clear_current(sm);
2226 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2227 sm->dot11RSNA4WayHandshakeFailures++;
2229 wpa_tdls_disassoc(sm);
2230 #endif /* CONFIG_TDLS */
2235 * wpa_sm_set_pmk - Set PMK
2236 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2238 * @pmk_len: The length of the new PMK in bytes
2239 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
2241 * Configure the PMK for WPA state machine.
2243 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
2249 sm->pmk_len = pmk_len;
2250 os_memcpy(sm->pmk, pmk, pmk_len);
2252 #ifdef CONFIG_IEEE80211R
2253 /* Set XXKey to be PSK for FT key derivation */
2254 sm->xxkey_len = pmk_len;
2255 os_memcpy(sm->xxkey, pmk, pmk_len);
2256 #endif /* CONFIG_IEEE80211R */
2259 pmksa_cache_add(sm->pmksa, pmk, pmk_len, NULL, 0,
2260 bssid, sm->own_addr,
2261 sm->network_ctx, sm->key_mgmt);
2267 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2268 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2270 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2273 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2278 if (sm->cur_pmksa) {
2279 sm->pmk_len = sm->cur_pmksa->pmk_len;
2280 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2282 sm->pmk_len = PMK_LEN;
2283 os_memset(sm->pmk, 0, PMK_LEN);
2289 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2290 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2291 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2293 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2296 sm->fast_reauth = fast_reauth;
2301 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2302 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2303 * @scard_ctx: Context pointer for smartcard related callback functions
2305 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2309 sm->scard_ctx = scard_ctx;
2310 if (sm->preauth_eapol)
2311 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2316 * wpa_sm_set_config - Notification of current configration change
2317 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2318 * @config: Pointer to current network configuration
2320 * Notify WPA state machine that configuration has changed. config will be
2321 * stored as a backpointer to network configuration. This can be %NULL to clear
2322 * the stored pointed.
2324 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2330 sm->network_ctx = config->network_ctx;
2331 sm->peerkey_enabled = config->peerkey_enabled;
2332 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2333 sm->proactive_key_caching = config->proactive_key_caching;
2334 sm->eap_workaround = config->eap_workaround;
2335 sm->eap_conf_ctx = config->eap_conf_ctx;
2337 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2338 sm->ssid_len = config->ssid_len;
2341 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
2342 sm->p2p = config->p2p;
2344 sm->network_ctx = NULL;
2345 sm->peerkey_enabled = 0;
2346 sm->allowed_pairwise_cipher = 0;
2347 sm->proactive_key_caching = 0;
2348 sm->eap_workaround = 0;
2349 sm->eap_conf_ctx = NULL;
2351 sm->wpa_ptk_rekey = 0;
2358 * wpa_sm_set_own_addr - Set own MAC address
2359 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2360 * @addr: Own MAC address
2362 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2365 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2370 * wpa_sm_set_ifname - Set network interface name
2371 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2372 * @ifname: Interface name
2373 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2375 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2376 const char *bridge_ifname)
2379 sm->ifname = ifname;
2380 sm->bridge_ifname = bridge_ifname;
2386 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2387 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2388 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2390 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2398 * wpa_sm_set_param - Set WPA state machine parameters
2399 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2400 * @param: Parameter field
2401 * @value: Parameter value
2402 * Returns: 0 on success, -1 on failure
2404 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2413 case RSNA_PMK_LIFETIME:
2415 sm->dot11RSNAConfigPMKLifetime = value;
2419 case RSNA_PMK_REAUTH_THRESHOLD:
2420 if (value > 0 && value <= 100)
2421 sm->dot11RSNAConfigPMKReauthThreshold = value;
2425 case RSNA_SA_TIMEOUT:
2427 sm->dot11RSNAConfigSATimeout = value;
2431 case WPA_PARAM_PROTO:
2434 case WPA_PARAM_PAIRWISE:
2435 sm->pairwise_cipher = value;
2437 case WPA_PARAM_GROUP:
2438 sm->group_cipher = value;
2440 case WPA_PARAM_KEY_MGMT:
2441 sm->key_mgmt = value;
2443 #ifdef CONFIG_IEEE80211W
2444 case WPA_PARAM_MGMT_GROUP:
2445 sm->mgmt_group_cipher = value;
2447 #endif /* CONFIG_IEEE80211W */
2448 case WPA_PARAM_RSN_ENABLED:
2449 sm->rsn_enabled = value;
2463 * wpa_sm_get_status - Get WPA state machine
2464 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2465 * @buf: Buffer for status information
2466 * @buflen: Maximum buffer length
2467 * @verbose: Whether to include verbose status information
2468 * Returns: Number of bytes written to buf.
2470 * Query WPA state machine for status information. This function fills in
2471 * a text area with current status information. If the buffer (buf) is not
2472 * large enough, status information will be truncated to fit the buffer.
2474 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2477 char *pos = buf, *end = buf + buflen;
2480 ret = os_snprintf(pos, end - pos,
2481 "pairwise_cipher=%s\n"
2484 wpa_cipher_txt(sm->pairwise_cipher),
2485 wpa_cipher_txt(sm->group_cipher),
2486 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
2487 if (ret < 0 || ret >= end - pos)
2491 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
2492 struct wpa_ie_data rsn;
2493 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
2495 rsn.capabilities & (WPA_CAPABILITY_MFPR |
2496 WPA_CAPABILITY_MFPC)) {
2497 ret = os_snprintf(pos, end - pos, "pmf=%d\n",
2499 WPA_CAPABILITY_MFPR) ? 2 : 1);
2500 if (ret < 0 || ret >= end - pos)
2510 int wpa_sm_pmf_enabled(struct wpa_sm *sm)
2512 struct wpa_ie_data rsn;
2514 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
2517 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
2518 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
2526 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2527 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2528 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2529 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2530 * Returns: 0 on success, -1 on failure
2532 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2540 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2545 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2546 wpa_ie, *wpa_ie_len);
2548 if (sm->assoc_wpa_ie == NULL) {
2550 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2551 * the correct version of the IE even if PMKSA caching is
2552 * aborted (which would remove PMKID from IE generation).
2554 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2555 if (sm->assoc_wpa_ie == NULL)
2558 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2559 sm->assoc_wpa_ie_len = *wpa_ie_len;
2567 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2568 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2569 * @ie: Pointer to IE data (starting from id)
2571 * Returns: 0 on success, -1 on failure
2573 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2574 * Request frame. The IE will be used to override the default value generated
2575 * with wpa_sm_set_assoc_wpa_ie_default().
2577 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2582 os_free(sm->assoc_wpa_ie);
2583 if (ie == NULL || len == 0) {
2584 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2585 "WPA: clearing own WPA/RSN IE");
2586 sm->assoc_wpa_ie = NULL;
2587 sm->assoc_wpa_ie_len = 0;
2589 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2590 sm->assoc_wpa_ie = os_malloc(len);
2591 if (sm->assoc_wpa_ie == NULL)
2594 os_memcpy(sm->assoc_wpa_ie, ie, len);
2595 sm->assoc_wpa_ie_len = len;
2603 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2604 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2605 * @ie: Pointer to IE data (starting from id)
2607 * Returns: 0 on success, -1 on failure
2609 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2612 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2617 os_free(sm->ap_wpa_ie);
2618 if (ie == NULL || len == 0) {
2619 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2620 "WPA: clearing AP WPA IE");
2621 sm->ap_wpa_ie = NULL;
2622 sm->ap_wpa_ie_len = 0;
2624 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2625 sm->ap_wpa_ie = os_malloc(len);
2626 if (sm->ap_wpa_ie == NULL)
2629 os_memcpy(sm->ap_wpa_ie, ie, len);
2630 sm->ap_wpa_ie_len = len;
2638 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2639 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2640 * @ie: Pointer to IE data (starting from id)
2642 * Returns: 0 on success, -1 on failure
2644 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2647 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2652 os_free(sm->ap_rsn_ie);
2653 if (ie == NULL || len == 0) {
2654 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2655 "WPA: clearing AP RSN IE");
2656 sm->ap_rsn_ie = NULL;
2657 sm->ap_rsn_ie_len = 0;
2659 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2660 sm->ap_rsn_ie = os_malloc(len);
2661 if (sm->ap_rsn_ie == NULL)
2664 os_memcpy(sm->ap_rsn_ie, ie, len);
2665 sm->ap_rsn_ie_len = len;
2673 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2674 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2675 * @data: Pointer to data area for parsing results
2676 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2678 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2679 * parsed data into data.
2681 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2686 if (sm->assoc_wpa_ie == NULL) {
2687 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2688 "WPA: No WPA/RSN IE available from association info");
2691 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2697 int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
2699 return pmksa_cache_list(sm->pmksa, buf, len);
2703 #ifdef CONFIG_TESTING_OPTIONS
2704 void wpa_sm_drop_sa(struct wpa_sm *sm)
2706 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
2709 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2710 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2711 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2713 #endif /* CONFIG_TESTING_OPTIONS */
2716 int wpa_sm_has_ptk(struct wpa_sm *sm)
2724 void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
2726 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
2730 void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
2732 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
2737 int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
2740 u8 keylen; /* plaintext key len */
2743 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
2744 struct wpa_gtk_data gd;
2746 os_memset(&gd, 0, sizeof(gd));
2747 keylen = wpa_cipher_key_len(sm->group_cipher);
2748 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
2749 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
2750 if (gd.alg == WPA_ALG_NONE) {
2751 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
2756 keyinfo = WPA_GET_LE16(buf + 2);
2757 gd.gtk_len = keylen;
2758 if (gd.gtk_len != buf[4]) {
2759 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
2760 gd.gtk_len, buf[4]);
2763 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
2764 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
2765 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
2767 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
2769 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
2770 gd.gtk, gd.gtk_len);
2771 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc)) {
2772 os_memset(&gd, 0, sizeof(gd));
2773 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
2777 os_memset(&gd, 0, sizeof(gd));
2778 #ifdef CONFIG_IEEE80211W
2779 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
2780 struct wpa_igtk_kde igd;
2783 os_memset(&igd, 0, sizeof(igd));
2784 keylen = wpa_cipher_key_len(sm->mgmt_group_cipher);
2785 os_memcpy(igd.keyid, buf + 2, 2);
2786 os_memcpy(igd.pn, buf + 4, 6);
2788 keyidx = WPA_GET_LE16(igd.keyid);
2789 os_memcpy(igd.igtk, buf + 10, keylen);
2791 wpa_hexdump_key(MSG_DEBUG, "Install IGTK (WNM SLEEP)",
2793 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
2794 broadcast_ether_addr,
2795 keyidx, 0, igd.pn, sizeof(igd.pn),
2796 igd.igtk, keylen) < 0) {
2797 wpa_printf(MSG_DEBUG, "Failed to install the IGTK in "
2799 os_memset(&igd, 0, sizeof(igd));
2802 os_memset(&igd, 0, sizeof(igd));
2803 #endif /* CONFIG_IEEE80211W */
2805 wpa_printf(MSG_DEBUG, "Unknown element id");
2811 #endif /* CONFIG_WNM */
2814 #ifdef CONFIG_PEERKEY
2815 int wpa_sm_rx_eapol_peerkey(struct wpa_sm *sm, const u8 *src_addr,
2816 const u8 *buf, size_t len)
2818 struct wpa_peerkey *peerkey;
2820 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
2821 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
2828 wpa_sm_rx_eapol(sm, src_addr, buf, len);
2832 #endif /* CONFIG_PEERKEY */
2837 int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
2839 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
2841 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
2845 #endif /* CONFIG_P2P */
2848 void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
2850 if (rx_replay_counter == NULL)
2853 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
2854 WPA_REPLAY_COUNTER_LEN);
2855 sm->rx_replay_counter_set = 1;
2856 wpa_printf(MSG_DEBUG, "Updated key replay counter");
2860 void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm, const u8 *ptk_kck,
2864 os_memcpy(sm->ptk.kck, ptk_kck, 16);
2865 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
2868 os_memcpy(sm->ptk.kek, ptk_kek, 16);
2869 wpa_printf(MSG_DEBUG, "Updated PTK KEK");