2 * hostapd - IEEE 802.11i-2004 / WPA Authenticator
3 * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
17 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "utils/state_machine.h"
20 #include "common/ieee802_11_defs.h"
21 #include "crypto/aes_wrap.h"
22 #include "crypto/crypto.h"
23 #include "crypto/sha1.h"
24 #include "crypto/sha256.h"
25 #include "eapol_auth/eapol_auth_sm.h"
26 #include "ap_config.h"
27 #include "ieee802_11.h"
29 #include "pmksa_cache_auth.h"
30 #include "wpa_auth_i.h"
31 #include "wpa_auth_ie.h"
33 #define STATE_MACHINE_DATA struct wpa_state_machine
34 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
35 #define STATE_MACHINE_ADDR sm->addr
38 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
39 static int wpa_sm_step(struct wpa_state_machine *sm);
40 static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len);
41 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
42 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
43 struct wpa_group *group);
44 static void wpa_request_new_ptk(struct wpa_state_machine *sm);
45 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
46 struct wpa_group *group);
48 static const u32 dot11RSNAConfigGroupUpdateCount = 4;
49 static const u32 dot11RSNAConfigPairwiseUpdateCount = 4;
50 static const u32 eapol_key_timeout_first = 100; /* ms */
51 static const u32 eapol_key_timeout_subseq = 1000; /* ms */
53 /* TODO: make these configurable */
54 static const int dot11RSNAConfigPMKLifetime = 43200;
55 static const int dot11RSNAConfigPMKReauthThreshold = 70;
56 static const int dot11RSNAConfigSATimeout = 60;
59 static inline void wpa_auth_mic_failure_report(
60 struct wpa_authenticator *wpa_auth, const u8 *addr)
62 if (wpa_auth->cb.mic_failure_report)
63 wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr);
67 static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
68 const u8 *addr, wpa_eapol_variable var,
71 if (wpa_auth->cb.set_eapol)
72 wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value);
76 static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
77 const u8 *addr, wpa_eapol_variable var)
79 if (wpa_auth->cb.get_eapol == NULL)
81 return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var);
85 static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
86 const u8 *addr, const u8 *prev_psk)
88 if (wpa_auth->cb.get_psk == NULL)
90 return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, prev_psk);
94 static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
95 const u8 *addr, u8 *msk, size_t *len)
97 if (wpa_auth->cb.get_msk == NULL)
99 return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len);
103 static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
105 enum wpa_alg alg, const u8 *addr, int idx,
106 u8 *key, size_t key_len)
108 if (wpa_auth->cb.set_key == NULL)
110 return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
115 static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
116 const u8 *addr, int idx, u8 *seq)
118 if (wpa_auth->cb.get_seqnum == NULL)
120 return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
125 wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
126 const u8 *data, size_t data_len, int encrypt)
128 if (wpa_auth->cb.send_eapol == NULL)
130 return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
135 int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
136 int (*cb)(struct wpa_state_machine *sm, void *ctx),
139 if (wpa_auth->cb.for_each_sta == NULL)
141 return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
145 int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
146 int (*cb)(struct wpa_authenticator *a, void *ctx),
149 if (wpa_auth->cb.for_each_auth == NULL)
151 return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
155 void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
156 logger_level level, const char *txt)
158 if (wpa_auth->cb.logger == NULL)
160 wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
164 void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
165 logger_level level, const char *fmt, ...)
171 if (wpa_auth->cb.logger == NULL)
174 maxlen = os_strlen(fmt) + 100;
175 format = os_malloc(maxlen);
180 vsnprintf(format, maxlen, fmt, ap);
183 wpa_auth_logger(wpa_auth, addr, level, format);
189 static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
192 if (wpa_auth->cb.disconnect == NULL)
194 wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
195 WLAN_REASON_PREV_AUTH_NOT_VALID);
199 static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
202 #ifdef CONFIG_IEEE80211R
203 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
205 #endif /* CONFIG_IEEE80211R */
206 #ifdef CONFIG_IEEE80211W
207 if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
209 #endif /* CONFIG_IEEE80211W */
214 static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
216 struct wpa_authenticator *wpa_auth = eloop_ctx;
218 if (os_get_random(wpa_auth->group->GMK, WPA_GMK_LEN)) {
219 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
222 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
225 if (wpa_auth->conf.wpa_gmk_rekey) {
226 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
227 wpa_rekey_gmk, wpa_auth, NULL);
232 static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
234 struct wpa_authenticator *wpa_auth = eloop_ctx;
235 struct wpa_group *group;
237 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
238 for (group = wpa_auth->group; group; group = group->next) {
239 group->GTKReKey = TRUE;
241 group->changed = FALSE;
242 wpa_group_sm_step(wpa_auth, group);
243 } while (group->changed);
246 if (wpa_auth->conf.wpa_group_rekey) {
247 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
248 0, wpa_rekey_gtk, wpa_auth, NULL);
253 static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
255 struct wpa_authenticator *wpa_auth = eloop_ctx;
256 struct wpa_state_machine *sm = timeout_ctx;
258 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
259 wpa_request_new_ptk(sm);
264 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
266 if (sm->pmksa == ctx)
272 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
275 struct wpa_authenticator *wpa_auth = ctx;
276 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
280 static void wpa_group_set_key_len(struct wpa_group *group, int cipher)
283 case WPA_CIPHER_CCMP:
286 case WPA_CIPHER_TKIP:
289 case WPA_CIPHER_WEP104:
292 case WPA_CIPHER_WEP40:
299 static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
302 struct wpa_group *group;
303 u8 buf[ETH_ALEN + 8 + sizeof(group)];
306 group = os_zalloc(sizeof(struct wpa_group));
310 group->GTKAuthenticator = TRUE;
311 group->vlan_id = vlan_id;
313 wpa_group_set_key_len(group, wpa_auth->conf.wpa_group);
315 /* Counter = PRF-256(Random number, "Init Counter",
316 * Local MAC Address || Time)
318 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
319 wpa_get_ntp_timestamp(buf + ETH_ALEN);
320 os_memcpy(buf + ETH_ALEN + 8, &group, sizeof(group));
321 if (os_get_random(rkey, sizeof(rkey)) ||
322 os_get_random(group->GMK, WPA_GMK_LEN)) {
323 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
329 sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
330 group->Counter, WPA_NONCE_LEN);
333 wpa_group_sm_step(wpa_auth, group);
334 group->GInit = FALSE;
335 wpa_group_sm_step(wpa_auth, group);
342 * wpa_init - Initialize WPA authenticator
343 * @addr: Authenticator address
344 * @conf: Configuration for WPA authenticator
345 * @cb: Callback functions for WPA authenticator
346 * Returns: Pointer to WPA authenticator data or %NULL on failure
348 struct wpa_authenticator * wpa_init(const u8 *addr,
349 struct wpa_auth_config *conf,
350 struct wpa_auth_callbacks *cb)
352 struct wpa_authenticator *wpa_auth;
354 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
355 if (wpa_auth == NULL)
357 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
358 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
359 os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
361 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
362 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
367 wpa_auth->group = wpa_group_init(wpa_auth, 0);
368 if (wpa_auth->group == NULL) {
369 os_free(wpa_auth->wpa_ie);
374 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
376 if (wpa_auth->pmksa == NULL) {
377 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
378 os_free(wpa_auth->wpa_ie);
383 #ifdef CONFIG_IEEE80211R
384 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
385 if (wpa_auth->ft_pmk_cache == NULL) {
386 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
387 os_free(wpa_auth->wpa_ie);
388 pmksa_cache_auth_deinit(wpa_auth->pmksa);
392 #endif /* CONFIG_IEEE80211R */
394 if (wpa_auth->conf.wpa_gmk_rekey) {
395 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
396 wpa_rekey_gmk, wpa_auth, NULL);
399 if (wpa_auth->conf.wpa_group_rekey) {
400 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
401 wpa_rekey_gtk, wpa_auth, NULL);
409 * wpa_deinit - Deinitialize WPA authenticator
410 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
412 void wpa_deinit(struct wpa_authenticator *wpa_auth)
414 struct wpa_group *group, *prev;
416 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
417 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
419 #ifdef CONFIG_PEERKEY
420 while (wpa_auth->stsl_negotiations)
421 wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
422 #endif /* CONFIG_PEERKEY */
424 pmksa_cache_auth_deinit(wpa_auth->pmksa);
426 #ifdef CONFIG_IEEE80211R
427 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
428 wpa_auth->ft_pmk_cache = NULL;
429 #endif /* CONFIG_IEEE80211R */
431 os_free(wpa_auth->wpa_ie);
433 group = wpa_auth->group;
445 * wpa_reconfig - Update WPA authenticator configuration
446 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
447 * @conf: Configuration for WPA authenticator
449 int wpa_reconfig(struct wpa_authenticator *wpa_auth,
450 struct wpa_auth_config *conf)
452 struct wpa_group *group;
453 if (wpa_auth == NULL)
456 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
457 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
458 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
463 * Reinitialize GTK to make sure it is suitable for the new
466 group = wpa_auth->group;
467 wpa_group_set_key_len(group, wpa_auth->conf.wpa_group);
469 wpa_group_sm_step(wpa_auth, group);
470 group->GInit = FALSE;
471 wpa_group_sm_step(wpa_auth, group);
477 struct wpa_state_machine *
478 wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr)
480 struct wpa_state_machine *sm;
482 sm = os_zalloc(sizeof(struct wpa_state_machine));
485 os_memcpy(sm->addr, addr, ETH_ALEN);
487 sm->wpa_auth = wpa_auth;
488 sm->group = wpa_auth->group;
494 int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
495 struct wpa_state_machine *sm)
497 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
500 #ifdef CONFIG_IEEE80211R
501 if (sm->ft_completed) {
502 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
503 "FT authentication already completed - do not "
504 "start 4-way handshake");
507 #endif /* CONFIG_IEEE80211R */
510 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
511 sm->ReAuthenticationRequest = TRUE;
512 return wpa_sm_step(sm);
515 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
516 "start authentication");
520 if (wpa_sm_step(sm) == 1)
521 return 1; /* should not really happen */
523 sm->AuthenticationRequest = TRUE;
524 return wpa_sm_step(sm);
528 void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
530 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
531 * reassociates back to the same AP while the previous entry for the
532 * STA has not yet been removed. */
536 sm->wpa_key_mgmt = 0;
540 static void wpa_free_sta_sm(struct wpa_state_machine *sm)
542 os_free(sm->last_rx_eapol_key);
548 void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
553 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
554 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
555 "strict rekeying - force GTK rekey since STA "
557 eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
558 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
562 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
563 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
564 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
565 if (sm->in_step_loop) {
566 /* Must not free state machine while wpa_sm_step() is running.
567 * Freeing will be completed in the end of wpa_sm_step(). */
568 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
569 "machine deinit for " MACSTR, MAC2STR(sm->addr));
570 sm->pending_deinit = 1;
576 static void wpa_request_new_ptk(struct wpa_state_machine *sm)
581 sm->PTKRequest = TRUE;
586 static int wpa_replay_counter_valid(struct wpa_state_machine *sm,
587 const u8 *replay_counter)
590 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
591 if (!sm->key_replay[i].valid)
593 if (os_memcmp(replay_counter, sm->key_replay[i].counter,
594 WPA_REPLAY_COUNTER_LEN) == 0)
601 void wpa_receive(struct wpa_authenticator *wpa_auth,
602 struct wpa_state_machine *sm,
603 u8 *data, size_t data_len)
605 struct ieee802_1x_hdr *hdr;
606 struct wpa_eapol_key *key;
607 u16 key_info, key_data_length;
608 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
609 SMK_M1, SMK_M3, SMK_ERROR } msg;
611 struct wpa_eapol_ie_parse kde;
614 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
617 if (data_len < sizeof(*hdr) + sizeof(*key))
620 hdr = (struct ieee802_1x_hdr *) data;
621 key = (struct wpa_eapol_key *) (hdr + 1);
622 key_info = WPA_GET_BE16(key->key_info);
623 key_data_length = WPA_GET_BE16(key->key_data_length);
624 if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) {
625 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
626 "key_data overflow (%d > %lu)",
628 (unsigned long) (data_len - sizeof(*hdr) -
633 if (sm->wpa == WPA_VERSION_WPA2) {
634 if (key->type != EAPOL_KEY_TYPE_RSN) {
635 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
636 "unexpected type %d in RSN mode",
641 if (key->type != EAPOL_KEY_TYPE_WPA) {
642 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
643 "unexpected type %d in WPA mode",
649 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
652 if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
653 (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
654 if (key_info & WPA_KEY_INFO_ERROR) {
656 msgtxt = "SMK Error";
661 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
664 } else if (key_info & WPA_KEY_INFO_REQUEST) {
667 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
669 msgtxt = "2/2 Group";
670 } else if (key_data_length == 0) {
672 msgtxt = "4/4 Pairwise";
675 msgtxt = "2/4 Pairwise";
678 /* TODO: key_info type validation for PeerKey */
679 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
681 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
682 if (sm->pairwise == WPA_CIPHER_CCMP) {
683 if (wpa_use_aes_cmac(sm) &&
684 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
685 wpa_auth_logger(wpa_auth, sm->addr,
687 "advertised support for "
688 "AES-128-CMAC, but did not "
693 if (!wpa_use_aes_cmac(sm) &&
694 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
695 wpa_auth_logger(wpa_auth, sm->addr,
697 "did not use HMAC-SHA1-AES "
704 if (key_info & WPA_KEY_INFO_REQUEST) {
705 if (sm->req_replay_counter_used &&
706 os_memcmp(key->replay_counter, sm->req_replay_counter,
707 WPA_REPLAY_COUNTER_LEN) <= 0) {
708 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
709 "received EAPOL-Key request with "
715 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
716 !wpa_replay_counter_valid(sm, key->replay_counter)) {
718 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
719 "received EAPOL-Key %s with unexpected "
720 "replay counter", msgtxt);
721 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
722 if (!sm->key_replay[i].valid)
724 wpa_hexdump(MSG_DEBUG, "pending replay counter",
725 sm->key_replay[i].counter,
726 WPA_REPLAY_COUNTER_LEN);
728 wpa_hexdump(MSG_DEBUG, "received replay counter",
729 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
735 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
736 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING) {
737 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
738 "received EAPOL-Key msg 2/4 in "
739 "invalid state (%d) - dropped",
743 ft = sm->wpa == WPA_VERSION_WPA2 &&
744 wpa_key_mgmt_ft(sm->wpa_key_mgmt);
745 if (sm->wpa_ie == NULL ||
746 wpa_compare_rsn_ie(ft,
747 sm->wpa_ie, sm->wpa_ie_len,
748 (u8 *) (key + 1), key_data_length)) {
749 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
750 "WPA IE from (Re)AssocReq did not "
751 "match with msg 2/4");
753 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
754 sm->wpa_ie, sm->wpa_ie_len);
756 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
757 (u8 *) (key + 1), key_data_length);
758 /* MLME-DEAUTHENTICATE.request */
759 wpa_sta_disconnect(wpa_auth, sm->addr);
762 #ifdef CONFIG_IEEE80211R
764 struct wpa_ie_data ie;
765 if (wpa_parse_wpa_ie_rsn((u8 *) (key + 1),
766 key_data_length, &ie) < 0 ||
767 ie.num_pmkid != 1 || ie.pmkid == NULL) {
768 wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
769 "FT 4-way handshake message 2/4");
770 wpa_sta_disconnect(wpa_auth, sm->addr);
773 os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
774 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
775 sm->sup_pmk_r1_name, PMKID_LEN);
777 #endif /* CONFIG_IEEE80211R */
780 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
782 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
783 "received EAPOL-Key msg 4/4 in "
784 "invalid state (%d) - dropped",
790 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
792 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
793 "received EAPOL-Key msg 2/2 in "
794 "invalid state (%d) - dropped",
795 sm->wpa_ptk_group_state);
799 #ifdef CONFIG_PEERKEY
803 if (!wpa_auth->conf.peerkey) {
804 wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
805 "PeerKey use disabled - ignoring message");
808 if (!sm->PTK_valid) {
809 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
810 "received EAPOL-Key msg SMK in "
811 "invalid state - dropped");
815 #else /* CONFIG_PEERKEY */
819 return; /* STSL disabled - ignore SMK messages */
820 #endif /* CONFIG_PEERKEY */
825 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
826 "received EAPOL-Key frame (%s)", msgtxt);
828 if (key_info & WPA_KEY_INFO_ACK) {
829 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
830 "received invalid EAPOL-Key: Key Ack set");
834 if (!(key_info & WPA_KEY_INFO_MIC)) {
835 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
836 "received invalid EAPOL-Key: Key MIC not set");
840 sm->MICVerified = FALSE;
842 if (wpa_verify_key_mic(&sm->PTK, data, data_len)) {
843 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
844 "received EAPOL-Key with invalid MIC");
847 sm->MICVerified = TRUE;
848 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
851 if (key_info & WPA_KEY_INFO_REQUEST) {
852 if (sm->MICVerified) {
853 sm->req_replay_counter_used = 1;
854 os_memcpy(sm->req_replay_counter, key->replay_counter,
855 WPA_REPLAY_COUNTER_LEN);
857 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
858 "received EAPOL-Key request with "
864 * TODO: should decrypt key data field if encryption was used;
865 * even though MAC address KDE is not normally encrypted,
866 * supplicant is allowed to encrypt it.
868 if (msg == SMK_ERROR) {
869 #ifdef CONFIG_PEERKEY
870 wpa_smk_error(wpa_auth, sm, key);
871 #endif /* CONFIG_PEERKEY */
873 } else if (key_info & WPA_KEY_INFO_ERROR) {
874 /* Supplicant reported a Michael MIC error */
875 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
876 "received EAPOL-Key Error Request "
877 "(STA detected Michael MIC failure)");
878 wpa_auth_mic_failure_report(wpa_auth, sm->addr);
879 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
880 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
881 /* Error report is not a request for a new key
882 * handshake, but since Authenticator may do it, let's
883 * change the keys now anyway. */
884 wpa_request_new_ptk(sm);
885 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
886 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
887 "received EAPOL-Key Request for new "
889 wpa_request_new_ptk(sm);
890 #ifdef CONFIG_PEERKEY
891 } else if (msg == SMK_M1) {
892 wpa_smk_m1(wpa_auth, sm, key);
893 #endif /* CONFIG_PEERKEY */
894 } else if (key_data_length > 0 &&
895 wpa_parse_kde_ies((const u8 *) (key + 1),
896 key_data_length, &kde) == 0 &&
899 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
900 "received EAPOL-Key Request for GTK "
902 /* FIX: why was this triggering PTK rekeying for the
903 * STA that requested Group Key rekeying?? */
904 /* wpa_request_new_ptk(sta->wpa_sm); */
905 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
906 wpa_rekey_gtk(wpa_auth, NULL);
909 /* Do not allow the same key replay counter to be reused. This
910 * does also invalidate all other pending replay counters if
911 * retransmissions were used, i.e., we will only process one of
912 * the pending replies and ignore rest if more than one is
914 sm->key_replay[0].valid = FALSE;
917 #ifdef CONFIG_PEERKEY
919 wpa_smk_m3(wpa_auth, sm, key);
922 #endif /* CONFIG_PEERKEY */
924 os_free(sm->last_rx_eapol_key);
925 sm->last_rx_eapol_key = os_malloc(data_len);
926 if (sm->last_rx_eapol_key == NULL)
928 os_memcpy(sm->last_rx_eapol_key, data, data_len);
929 sm->last_rx_eapol_key_len = data_len;
931 sm->EAPOLKeyReceived = TRUE;
932 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
933 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
934 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
939 static void wpa_gmk_to_gtk(const u8 *gmk, const u8 *addr, const u8 *gnonce,
940 u8 *gtk, size_t gtk_len)
942 u8 data[ETH_ALEN + WPA_NONCE_LEN];
944 /* GTK = PRF-X(GMK, "Group key expansion", AA || GNonce) */
945 os_memcpy(data, addr, ETH_ALEN);
946 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
948 #ifdef CONFIG_IEEE80211W
949 sha256_prf(gmk, WPA_GMK_LEN, "Group key expansion",
950 data, sizeof(data), gtk, gtk_len);
951 #else /* CONFIG_IEEE80211W */
952 sha1_prf(gmk, WPA_GMK_LEN, "Group key expansion",
953 data, sizeof(data), gtk, gtk_len);
954 #endif /* CONFIG_IEEE80211W */
956 wpa_hexdump_key(MSG_DEBUG, "GMK", gmk, WPA_GMK_LEN);
957 wpa_hexdump_key(MSG_DEBUG, "GTK", gtk, gtk_len);
961 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
963 struct wpa_authenticator *wpa_auth = eloop_ctx;
964 struct wpa_state_machine *sm = timeout_ctx;
966 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
967 sm->TimeoutEvt = TRUE;
972 void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
973 struct wpa_state_machine *sm, int key_info,
974 const u8 *key_rsc, const u8 *nonce,
975 const u8 *kde, size_t kde_len,
976 int keyidx, int encr, int force_version)
978 struct ieee802_1x_hdr *hdr;
979 struct wpa_eapol_key *key;
982 int key_data_len, pad_len = 0;
984 int version, pairwise;
987 len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
990 version = force_version;
991 else if (wpa_use_aes_cmac(sm))
992 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
993 else if (sm->pairwise == WPA_CIPHER_CCMP)
994 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
996 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
998 pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1000 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1001 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1004 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
1005 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
1006 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
1007 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
1008 pairwise, (unsigned long) kde_len, keyidx, encr);
1010 key_data_len = kde_len;
1012 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1013 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1014 pad_len = key_data_len % 8;
1016 pad_len = 8 - pad_len;
1017 key_data_len += pad_len + 8;
1020 len += key_data_len;
1022 hdr = os_zalloc(len);
1025 hdr->version = wpa_auth->conf.eapol_version;
1026 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1027 hdr->length = host_to_be16(len - sizeof(*hdr));
1028 key = (struct wpa_eapol_key *) (hdr + 1);
1030 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1031 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1032 key_info |= version;
1033 if (encr && sm->wpa == WPA_VERSION_WPA2)
1034 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1035 if (sm->wpa != WPA_VERSION_WPA2)
1036 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1037 WPA_PUT_BE16(key->key_info, key_info);
1039 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
1041 case WPA_CIPHER_CCMP:
1042 WPA_PUT_BE16(key->key_length, 16);
1044 case WPA_CIPHER_TKIP:
1045 WPA_PUT_BE16(key->key_length, 32);
1047 case WPA_CIPHER_WEP40:
1048 WPA_PUT_BE16(key->key_length, 5);
1050 case WPA_CIPHER_WEP104:
1051 WPA_PUT_BE16(key->key_length, 13);
1054 if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
1055 WPA_PUT_BE16(key->key_length, 0);
1057 /* FIX: STSL: what to use as key_replay_counter? */
1058 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1059 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1060 os_memcpy(sm->key_replay[i].counter,
1061 sm->key_replay[i - 1].counter,
1062 WPA_REPLAY_COUNTER_LEN);
1064 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1065 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1066 WPA_REPLAY_COUNTER_LEN);
1067 sm->key_replay[0].valid = TRUE;
1070 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1073 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1076 os_memcpy(key + 1, kde, kde_len);
1077 WPA_PUT_BE16(key->key_data_length, kde_len);
1078 } else if (encr && kde) {
1079 buf = os_zalloc(key_data_len);
1085 os_memcpy(pos, kde, kde_len);
1091 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1093 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1094 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1095 if (aes_wrap(sm->PTK.kek, (key_data_len - 8) / 8, buf,
1096 (u8 *) (key + 1))) {
1101 WPA_PUT_BE16(key->key_data_length, key_data_len);
1104 os_memcpy(key->key_iv,
1105 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1106 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1107 os_memcpy(ek, key->key_iv, 16);
1108 os_memcpy(ek + 16, sm->PTK.kek, 16);
1109 os_memcpy(key + 1, buf, key_data_len);
1110 rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
1111 WPA_PUT_BE16(key->key_data_length, key_data_len);
1116 if (key_info & WPA_KEY_INFO_MIC) {
1117 if (!sm->PTK_valid) {
1118 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1119 "PTK not valid when sending EAPOL-Key "
1124 wpa_eapol_key_mic(sm->PTK.kck, version, (u8 *) hdr, len,
1128 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1130 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1136 static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1137 struct wpa_state_machine *sm, int key_info,
1138 const u8 *key_rsc, const u8 *nonce,
1139 const u8 *kde, size_t kde_len,
1140 int keyidx, int encr)
1143 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1149 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1152 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1154 timeout_ms = eapol_key_timeout_first;
1156 timeout_ms = eapol_key_timeout_subseq;
1157 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1158 wpa_send_eapol_timeout, wpa_auth, sm);
1162 static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len)
1164 struct ieee802_1x_hdr *hdr;
1165 struct wpa_eapol_key *key;
1170 if (data_len < sizeof(*hdr) + sizeof(*key))
1173 hdr = (struct ieee802_1x_hdr *) data;
1174 key = (struct wpa_eapol_key *) (hdr + 1);
1175 key_info = WPA_GET_BE16(key->key_info);
1176 os_memcpy(mic, key->key_mic, 16);
1177 os_memset(key->key_mic, 0, 16);
1178 if (wpa_eapol_key_mic(PTK->kck, key_info & WPA_KEY_INFO_TYPE_MASK,
1179 data, data_len, key->key_mic) ||
1180 os_memcmp(mic, key->key_mic, 16) != 0)
1182 os_memcpy(key->key_mic, mic, 16);
1187 void wpa_remove_ptk(struct wpa_state_machine *sm)
1189 sm->PTK_valid = FALSE;
1190 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1191 wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, (u8 *) "",
1193 sm->pairwise_set = FALSE;
1194 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1198 int wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
1205 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1206 "event %d notification", event);
1214 sm->DeauthenticationRequest = TRUE;
1217 case WPA_REAUTH_EAPOL:
1218 if (sm->GUpdateStationKeys) {
1220 * Reauthentication cancels the pending group key
1221 * update for this STA.
1223 sm->group->GKeyDoneStations--;
1224 sm->GUpdateStationKeys = FALSE;
1225 sm->PtkGroupInit = TRUE;
1227 sm->ReAuthenticationRequest = TRUE;
1230 #ifdef CONFIG_IEEE80211R
1231 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1232 "after association");
1233 wpa_ft_install_ptk(sm);
1235 /* Using FT protocol, not WPA auth state machine */
1236 sm->ft_completed = 1;
1238 #else /* CONFIG_IEEE80211R */
1240 #endif /* CONFIG_IEEE80211R */
1243 #ifdef CONFIG_IEEE80211R
1244 sm->ft_completed = 0;
1245 #endif /* CONFIG_IEEE80211R */
1247 #ifdef CONFIG_IEEE80211W
1248 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1250 #endif /* CONFIG_IEEE80211W */
1253 sm->PTK_valid = FALSE;
1254 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1256 if (event != WPA_REAUTH_EAPOL)
1260 return wpa_sm_step(sm);
1264 static enum wpa_alg wpa_alg_enum(int alg)
1267 case WPA_CIPHER_CCMP:
1268 return WPA_ALG_CCMP;
1269 case WPA_CIPHER_TKIP:
1270 return WPA_ALG_TKIP;
1271 case WPA_CIPHER_WEP104:
1272 case WPA_CIPHER_WEP40:
1275 return WPA_ALG_NONE;
1280 SM_STATE(WPA_PTK, INITIALIZE)
1282 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1284 /* Init flag is not cleared here, so avoid busy
1285 * loop by claiming nothing changed. */
1286 sm->changed = FALSE;
1290 if (sm->GUpdateStationKeys)
1291 sm->group->GKeyDoneStations--;
1292 sm->GUpdateStationKeys = FALSE;
1293 if (sm->wpa == WPA_VERSION_WPA)
1294 sm->PInitAKeys = FALSE;
1295 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1296 * Local AA > Remote AA)) */) {
1299 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1301 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1303 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1304 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1305 WPA_EAPOL_authorized, 0);
1310 SM_STATE(WPA_PTK, DISCONNECT)
1312 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1313 sm->Disconnect = FALSE;
1314 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1318 SM_STATE(WPA_PTK, DISCONNECTED)
1320 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1321 sm->DeauthenticationRequest = FALSE;
1325 SM_STATE(WPA_PTK, AUTHENTICATION)
1327 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1328 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1329 sm->PTK_valid = FALSE;
1330 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1332 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1333 sm->AuthenticationRequest = FALSE;
1337 SM_STATE(WPA_PTK, AUTHENTICATION2)
1339 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1340 os_memcpy(sm->ANonce, sm->group->Counter, WPA_NONCE_LEN);
1341 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1342 sm->ReAuthenticationRequest = FALSE;
1343 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1344 * logical place than INITIALIZE since AUTHENTICATION2 can be
1345 * re-entered on ReAuthenticationRequest without going through
1351 SM_STATE(WPA_PTK, INITPMK)
1353 u8 msk[2 * PMK_LEN];
1354 size_t len = 2 * PMK_LEN;
1356 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1357 #ifdef CONFIG_IEEE80211R
1359 #endif /* CONFIG_IEEE80211R */
1361 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1362 os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
1363 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1364 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1365 "(len=%lu)", (unsigned long) len);
1366 os_memcpy(sm->PMK, msk, PMK_LEN);
1367 #ifdef CONFIG_IEEE80211R
1368 if (len >= 2 * PMK_LEN) {
1369 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1370 sm->xxkey_len = PMK_LEN;
1372 #endif /* CONFIG_IEEE80211R */
1374 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
1377 sm->req_replay_counter_used = 0;
1378 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1379 * will break reauthentication since EAPOL state machines may not be
1380 * get into AUTHENTICATING state that clears keyRun before WPA state
1381 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1382 * state and takes PMK from the previously used AAA Key. This will
1383 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1384 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1385 * be good workaround for this issue. */
1386 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1390 SM_STATE(WPA_PTK, INITPSK)
1393 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
1394 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL);
1396 os_memcpy(sm->PMK, psk, PMK_LEN);
1397 #ifdef CONFIG_IEEE80211R
1398 os_memcpy(sm->xxkey, psk, PMK_LEN);
1399 sm->xxkey_len = PMK_LEN;
1400 #endif /* CONFIG_IEEE80211R */
1402 sm->req_replay_counter_used = 0;
1406 SM_STATE(WPA_PTK, PTKSTART)
1408 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1409 size_t pmkid_len = 0;
1411 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
1412 sm->PTKRequest = FALSE;
1413 sm->TimeoutEvt = FALSE;
1416 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1417 /* No point in sending the EAPOL-Key - we will disconnect
1418 * immediately following this. */
1422 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1423 "sending 1/4 msg of 4-Way Handshake");
1425 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1426 * one possible PSK for this STA.
1428 if (sm->wpa == WPA_VERSION_WPA2 &&
1429 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt)) {
1431 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
1432 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
1433 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
1434 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
1436 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
1437 sm->pmksa->pmkid, PMKID_LEN);
1440 * Calculate PMKID since no PMKSA cache entry was
1441 * available with pre-calculated PMKID.
1443 rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
1444 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
1445 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1448 wpa_send_eapol(sm->wpa_auth, sm,
1449 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
1450 sm->ANonce, pmkid, pmkid_len, 0, 0);
1454 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk,
1455 struct wpa_ptk *ptk)
1457 size_t ptk_len = sm->pairwise == WPA_CIPHER_CCMP ? 48 : 64;
1458 #ifdef CONFIG_IEEE80211R
1459 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
1460 return wpa_auth_derive_ptk_ft(sm, pmk, ptk, ptk_len);
1461 #endif /* CONFIG_IEEE80211R */
1463 wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
1464 sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce,
1465 (u8 *) ptk, ptk_len,
1466 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1472 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
1476 const u8 *pmk = NULL;
1478 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
1479 sm->EAPOLKeyReceived = FALSE;
1481 /* WPA with IEEE 802.1X: use the derived PMK from EAP
1482 * WPA-PSK: iterate through possible PSKs and select the one matching
1485 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1486 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, pmk);
1492 wpa_derive_ptk(sm, pmk, &PTK);
1494 if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key,
1495 sm->last_rx_eapol_key_len) == 0) {
1500 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
1505 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1506 "invalid MIC in msg 2/4 of 4-Way Handshake");
1510 #ifdef CONFIG_IEEE80211R
1511 if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
1513 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
1514 * with the value we derived.
1516 if (os_memcmp(sm->sup_pmk_r1_name, sm->pmk_r1_name,
1517 WPA_PMK_NAME_LEN) != 0) {
1518 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1519 "PMKR1Name mismatch in FT 4-way "
1521 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
1523 sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
1524 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1525 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1529 #endif /* CONFIG_IEEE80211R */
1531 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
1533 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1534 /* PSK may have changed from the previous choice, so update
1535 * state machine data based on whatever PSK was selected here.
1537 os_memcpy(sm->PMK, pmk, PMK_LEN);
1540 sm->MICVerified = TRUE;
1542 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
1543 sm->PTK_valid = TRUE;
1547 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
1549 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
1554 #ifdef CONFIG_IEEE80211W
1556 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1558 if (sm->mgmt_frame_prot) {
1559 return 2 + RSN_SELECTOR_LEN + sizeof(struct wpa_igtk_kde);
1566 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1568 struct wpa_igtk_kde igtk;
1569 struct wpa_group *gsm = sm->group;
1571 if (!sm->mgmt_frame_prot)
1574 igtk.keyid[0] = gsm->GN_igtk;
1576 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
1577 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, igtk.pn) < 0)
1578 os_memset(igtk.pn, 0, sizeof(igtk.pn));
1579 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
1580 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
1581 (const u8 *) &igtk, sizeof(igtk), NULL, 0);
1586 #else /* CONFIG_IEEE80211W */
1588 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1594 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1599 #endif /* CONFIG_IEEE80211W */
1602 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
1604 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
1605 size_t gtk_len, kde_len;
1606 struct wpa_group *gsm = sm->group;
1608 int wpa_ie_len, secure, keyidx, encr = 0;
1610 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
1611 sm->TimeoutEvt = FALSE;
1614 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1615 /* No point in sending the EAPOL-Key - we will disconnect
1616 * immediately following this. */
1620 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, GTK[GN])
1622 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
1623 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
1624 wpa_ie = sm->wpa_auth->wpa_ie;
1625 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
1626 if (sm->wpa == WPA_VERSION_WPA &&
1627 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
1628 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
1629 /* WPA-only STA, remove RSN IE */
1630 wpa_ie = wpa_ie + wpa_ie[1] + 2;
1631 wpa_ie_len = wpa_ie[1] + 2;
1633 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1634 "sending 3/4 msg of 4-Way Handshake");
1635 if (sm->wpa == WPA_VERSION_WPA2) {
1636 /* WPA2 send GTK in the 4-way handshake */
1638 gtk = gsm->GTK[gsm->GN - 1];
1639 gtk_len = gsm->GTK_len;
1644 /* WPA does not include GTK in msg 3/4 */
1652 kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
1654 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
1655 #ifdef CONFIG_IEEE80211R
1656 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
1657 kde_len += 2 + PMKID_LEN;
1658 #endif /* CONFIG_IEEE80211R */
1659 kde = os_malloc(kde_len);
1664 os_memcpy(pos, wpa_ie, wpa_ie_len);
1666 #ifdef CONFIG_IEEE80211R
1667 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
1668 int res = wpa_insert_pmkid(kde, pos - kde, sm->pmk_r1_name);
1670 wpa_printf(MSG_ERROR, "FT: Failed to insert "
1671 "PMKR1Name into RSN IE in EAPOL-Key data");
1677 #endif /* CONFIG_IEEE80211R */
1680 hdr[0] = keyidx & 0x03;
1682 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
1685 pos = ieee80211w_kde_add(sm, pos);
1687 wpa_send_eapol(sm->wpa_auth, sm,
1688 (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
1689 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
1690 WPA_KEY_INFO_KEY_TYPE,
1691 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
1696 SM_STATE(WPA_PTK, PTKINITDONE)
1698 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
1699 sm->EAPOLKeyReceived = FALSE;
1703 if (sm->pairwise == WPA_CIPHER_TKIP) {
1710 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
1711 sm->PTK.tk1, klen)) {
1712 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1715 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
1716 sm->pairwise_set = TRUE;
1718 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
1719 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1720 eloop_register_timeout(sm->wpa_auth->conf.
1721 wpa_ptk_rekey, 0, wpa_rekey_ptk,
1725 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1726 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1727 WPA_EAPOL_authorized, 1);
1731 if (0 /* IBSS == TRUE */) {
1733 if (sm->keycount == 2) {
1734 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1735 WPA_EAPOL_portValid, 1);
1738 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
1741 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
1742 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
1743 if (sm->wpa == WPA_VERSION_WPA)
1744 sm->PInitAKeys = TRUE;
1747 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1748 "pairwise key handshake completed (%s)",
1749 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
1751 #ifdef CONFIG_IEEE80211R
1752 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
1753 #endif /* CONFIG_IEEE80211R */
1759 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
1762 SM_ENTER(WPA_PTK, INITIALIZE);
1763 else if (sm->Disconnect
1764 /* || FIX: dot11RSNAConfigSALifetime timeout */)
1765 SM_ENTER(WPA_PTK, DISCONNECT);
1766 else if (sm->DeauthenticationRequest)
1767 SM_ENTER(WPA_PTK, DISCONNECTED);
1768 else if (sm->AuthenticationRequest)
1769 SM_ENTER(WPA_PTK, AUTHENTICATION);
1770 else if (sm->ReAuthenticationRequest)
1771 SM_ENTER(WPA_PTK, AUTHENTICATION2);
1772 else if (sm->PTKRequest)
1773 SM_ENTER(WPA_PTK, PTKSTART);
1774 else switch (sm->wpa_ptk_state) {
1775 case WPA_PTK_INITIALIZE:
1777 case WPA_PTK_DISCONNECT:
1778 SM_ENTER(WPA_PTK, DISCONNECTED);
1780 case WPA_PTK_DISCONNECTED:
1781 SM_ENTER(WPA_PTK, INITIALIZE);
1783 case WPA_PTK_AUTHENTICATION:
1784 SM_ENTER(WPA_PTK, AUTHENTICATION2);
1786 case WPA_PTK_AUTHENTICATION2:
1787 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
1788 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
1789 WPA_EAPOL_keyRun) > 0)
1790 SM_ENTER(WPA_PTK, INITPMK);
1791 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
1792 /* FIX: && 802.1X::keyRun */)
1793 SM_ENTER(WPA_PTK, INITPSK);
1795 case WPA_PTK_INITPMK:
1796 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
1797 WPA_EAPOL_keyAvailable) > 0)
1798 SM_ENTER(WPA_PTK, PTKSTART);
1800 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1801 SM_ENTER(WPA_PTK, DISCONNECT);
1804 case WPA_PTK_INITPSK:
1805 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL))
1806 SM_ENTER(WPA_PTK, PTKSTART);
1808 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1809 "no PSK configured for the STA");
1810 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1811 SM_ENTER(WPA_PTK, DISCONNECT);
1814 case WPA_PTK_PTKSTART:
1815 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1816 sm->EAPOLKeyPairwise)
1817 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
1818 else if (sm->TimeoutCtr >
1819 (int) dot11RSNAConfigPairwiseUpdateCount) {
1820 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1821 SM_ENTER(WPA_PTK, DISCONNECT);
1822 } else if (sm->TimeoutEvt)
1823 SM_ENTER(WPA_PTK, PTKSTART);
1825 case WPA_PTK_PTKCALCNEGOTIATING:
1826 if (sm->MICVerified)
1827 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
1828 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1829 sm->EAPOLKeyPairwise)
1830 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
1831 else if (sm->TimeoutEvt)
1832 SM_ENTER(WPA_PTK, PTKSTART);
1834 case WPA_PTK_PTKCALCNEGOTIATING2:
1835 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
1837 case WPA_PTK_PTKINITNEGOTIATING:
1838 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1839 sm->EAPOLKeyPairwise && sm->MICVerified)
1840 SM_ENTER(WPA_PTK, PTKINITDONE);
1841 else if (sm->TimeoutCtr >
1842 (int) dot11RSNAConfigPairwiseUpdateCount) {
1843 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1844 SM_ENTER(WPA_PTK, DISCONNECT);
1845 } else if (sm->TimeoutEvt)
1846 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
1848 case WPA_PTK_PTKINITDONE:
1854 SM_STATE(WPA_PTK_GROUP, IDLE)
1856 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
1858 /* Init flag is not cleared here, so avoid busy
1859 * loop by claiming nothing changed. */
1860 sm->changed = FALSE;
1862 sm->GTimeoutCtr = 0;
1866 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
1868 u8 rsc[WPA_KEY_RSC_LEN];
1869 struct wpa_group *gsm = sm->group;
1870 u8 *kde, *pos, hdr[2];
1873 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
1876 if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
1877 /* No point in sending the EAPOL-Key - we will disconnect
1878 * immediately following this. */
1882 if (sm->wpa == WPA_VERSION_WPA)
1883 sm->PInitAKeys = FALSE;
1884 sm->TimeoutEvt = FALSE;
1885 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
1886 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
1887 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
1888 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
1889 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1890 "sending 1/2 msg of Group Key Handshake");
1892 if (sm->wpa == WPA_VERSION_WPA2) {
1893 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
1894 ieee80211w_kde_len(sm);
1895 kde = os_malloc(kde_len);
1900 hdr[0] = gsm->GN & 0x03;
1902 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
1903 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
1904 pos = ieee80211w_kde_add(sm, pos);
1906 kde = gsm->GTK[gsm->GN - 1];
1907 pos = kde + gsm->GTK_len;
1910 wpa_send_eapol(sm->wpa_auth, sm,
1911 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
1913 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
1914 rsc, gsm->GNonce, kde, pos - kde, gsm->GN, 1);
1915 if (sm->wpa == WPA_VERSION_WPA2)
1920 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
1922 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
1923 sm->EAPOLKeyReceived = FALSE;
1924 if (sm->GUpdateStationKeys)
1925 sm->group->GKeyDoneStations--;
1926 sm->GUpdateStationKeys = FALSE;
1927 sm->GTimeoutCtr = 0;
1928 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
1929 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1930 "group key handshake completed (%s)",
1931 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
1936 SM_STATE(WPA_PTK_GROUP, KEYERROR)
1938 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
1939 if (sm->GUpdateStationKeys)
1940 sm->group->GKeyDoneStations--;
1941 sm->GUpdateStationKeys = FALSE;
1942 sm->Disconnect = TRUE;
1946 SM_STEP(WPA_PTK_GROUP)
1948 if (sm->Init || sm->PtkGroupInit) {
1949 SM_ENTER(WPA_PTK_GROUP, IDLE);
1950 sm->PtkGroupInit = FALSE;
1951 } else switch (sm->wpa_ptk_group_state) {
1952 case WPA_PTK_GROUP_IDLE:
1953 if (sm->GUpdateStationKeys ||
1954 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
1955 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
1957 case WPA_PTK_GROUP_REKEYNEGOTIATING:
1958 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1959 !sm->EAPOLKeyPairwise && sm->MICVerified)
1960 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
1961 else if (sm->GTimeoutCtr >
1962 (int) dot11RSNAConfigGroupUpdateCount)
1963 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
1964 else if (sm->TimeoutEvt)
1965 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
1967 case WPA_PTK_GROUP_KEYERROR:
1968 SM_ENTER(WPA_PTK_GROUP, IDLE);
1970 case WPA_PTK_GROUP_REKEYESTABLISHED:
1971 SM_ENTER(WPA_PTK_GROUP, IDLE);
1977 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
1978 struct wpa_group *group)
1982 /* FIX: is this the correct way of getting GNonce? */
1983 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
1984 inc_byte_array(group->Counter, WPA_NONCE_LEN);
1985 wpa_gmk_to_gtk(group->GMK, wpa_auth->addr, group->GNonce,
1986 group->GTK[group->GN - 1], group->GTK_len);
1988 #ifdef CONFIG_IEEE80211W
1989 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
1990 if (os_get_random(group->IGTK[group->GN_igtk - 4],
1991 WPA_IGTK_LEN) < 0) {
1992 wpa_printf(MSG_INFO, "RSN: Failed to get new random "
1996 wpa_hexdump_key(MSG_DEBUG, "IGTK",
1997 group->IGTK[group->GN_igtk - 4], WPA_IGTK_LEN);
1999 #endif /* CONFIG_IEEE80211W */
2005 static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
2006 struct wpa_group *group)
2008 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2009 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
2010 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
2011 group->wpa_group_state = WPA_GROUP_GTK_INIT;
2014 os_memset(group->GTK, 0, sizeof(group->GTK));
2017 #ifdef CONFIG_IEEE80211W
2020 #endif /* CONFIG_IEEE80211W */
2021 /* GTK[GN] = CalcGTK() */
2022 wpa_gtk_update(wpa_auth, group);
2026 static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
2028 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
2029 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2030 "Not in PTKINITDONE; skip Group Key update");
2033 if (sm->GUpdateStationKeys) {
2035 * This should not really happen, but just in case, make sure
2036 * we do not count the same STA twice in GKeyDoneStations.
2038 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2039 "GUpdateStationKeys already set - do not "
2040 "increment GKeyDoneStations");
2042 sm->group->GKeyDoneStations++;
2043 sm->GUpdateStationKeys = TRUE;
2050 static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
2051 struct wpa_group *group)
2055 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2056 "SETKEYS (VLAN-ID %d)", group->vlan_id);
2057 group->changed = TRUE;
2058 group->wpa_group_state = WPA_GROUP_SETKEYS;
2059 group->GTKReKey = FALSE;
2061 group->GM = group->GN;
2063 #ifdef CONFIG_IEEE80211W
2064 tmp = group->GM_igtk;
2065 group->GM_igtk = group->GN_igtk;
2066 group->GN_igtk = tmp;
2067 #endif /* CONFIG_IEEE80211W */
2068 /* "GKeyDoneStations = GNoStations" is done in more robust way by
2069 * counting the STAs that are marked with GUpdateStationKeys instead of
2070 * including all STAs that could be in not-yet-completed state. */
2071 wpa_gtk_update(wpa_auth, group);
2073 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, NULL);
2074 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
2075 group->GKeyDoneStations);
2079 static void wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
2080 struct wpa_group *group)
2082 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2083 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
2084 group->changed = TRUE;
2085 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
2086 wpa_auth_set_key(wpa_auth, group->vlan_id,
2087 wpa_alg_enum(wpa_auth->conf.wpa_group),
2088 NULL, group->GN, group->GTK[group->GN - 1],
2091 #ifdef CONFIG_IEEE80211W
2092 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
2093 wpa_auth_set_key(wpa_auth, group->vlan_id, WPA_ALG_IGTK,
2094 NULL, group->GN_igtk,
2095 group->IGTK[group->GN_igtk - 4],
2098 #endif /* CONFIG_IEEE80211W */
2102 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
2103 struct wpa_group *group)
2106 wpa_group_gtk_init(wpa_auth, group);
2107 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
2108 group->GTKAuthenticator) {
2109 wpa_group_setkeysdone(wpa_auth, group);
2110 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
2112 wpa_group_setkeys(wpa_auth, group);
2113 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
2114 if (group->GKeyDoneStations == 0)
2115 wpa_group_setkeysdone(wpa_auth, group);
2116 else if (group->GTKReKey)
2117 wpa_group_setkeys(wpa_auth, group);
2122 static int wpa_sm_step(struct wpa_state_machine *sm)
2127 if (sm->in_step_loop) {
2128 /* This should not happen, but if it does, make sure we do not
2129 * end up freeing the state machine too early by exiting the
2130 * recursive call. */
2131 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
2135 sm->in_step_loop = 1;
2137 if (sm->pending_deinit)
2140 sm->changed = FALSE;
2141 sm->wpa_auth->group->changed = FALSE;
2143 SM_STEP_RUN(WPA_PTK);
2144 if (sm->pending_deinit)
2146 SM_STEP_RUN(WPA_PTK_GROUP);
2147 if (sm->pending_deinit)
2149 wpa_group_sm_step(sm->wpa_auth, sm->group);
2150 } while (sm->changed || sm->wpa_auth->group->changed);
2151 sm->in_step_loop = 0;
2153 if (sm->pending_deinit) {
2154 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
2155 "machine deinit for " MACSTR, MAC2STR(sm->addr));
2156 wpa_free_sta_sm(sm);
2163 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
2165 struct wpa_state_machine *sm = eloop_ctx;
2170 void wpa_auth_sm_notify(struct wpa_state_machine *sm)
2174 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
2178 void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
2181 struct wpa_group *group;
2183 if (wpa_auth == NULL)
2186 group = wpa_auth->group;
2188 for (i = 0; i < 2; i++) {
2190 group->GM = group->GN;
2192 #ifdef CONFIG_IEEE80211W
2193 tmp = group->GM_igtk;
2194 group->GM_igtk = group->GN_igtk;
2195 group->GN_igtk = tmp;
2196 #endif /* CONFIG_IEEE80211W */
2197 wpa_gtk_update(wpa_auth, group);
2202 static const char * wpa_bool_txt(int bool)
2204 return bool ? "TRUE" : "FALSE";
2208 static int wpa_cipher_bits(int cipher)
2211 case WPA_CIPHER_CCMP:
2213 case WPA_CIPHER_TKIP:
2215 case WPA_CIPHER_WEP104:
2217 case WPA_CIPHER_WEP40:
2225 #define RSN_SUITE "%02x-%02x-%02x-%d"
2226 #define RSN_SUITE_ARG(s) \
2227 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2229 int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
2232 char pmkid_txt[PMKID_LEN * 2 + 1];
2234 if (wpa_auth == NULL)
2237 ret = os_snprintf(buf + len, buflen - len,
2238 "dot11RSNAOptionImplemented=TRUE\n"
2239 #ifdef CONFIG_RSN_PREAUTH
2240 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2241 #else /* CONFIG_RSN_PREAUTH */
2242 "dot11RSNAPreauthenticationImplemented=FALSE\n"
2243 #endif /* CONFIG_RSN_PREAUTH */
2244 "dot11RSNAEnabled=%s\n"
2245 "dot11RSNAPreauthenticationEnabled=%s\n",
2246 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
2247 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
2248 if (ret < 0 || (size_t) ret >= buflen - len)
2252 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2253 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
2256 buf + len, buflen - len,
2257 "dot11RSNAConfigVersion=%u\n"
2258 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
2259 /* FIX: dot11RSNAConfigGroupCipher */
2260 /* FIX: dot11RSNAConfigGroupRekeyMethod */
2261 /* FIX: dot11RSNAConfigGroupRekeyTime */
2262 /* FIX: dot11RSNAConfigGroupRekeyPackets */
2263 "dot11RSNAConfigGroupRekeyStrict=%u\n"
2264 "dot11RSNAConfigGroupUpdateCount=%u\n"
2265 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
2266 "dot11RSNAConfigGroupCipherSize=%u\n"
2267 "dot11RSNAConfigPMKLifetime=%u\n"
2268 "dot11RSNAConfigPMKReauthThreshold=%u\n"
2269 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
2270 "dot11RSNAConfigSATimeout=%u\n"
2271 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2272 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2273 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2274 "dot11RSNAPMKIDUsed=%s\n"
2275 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2276 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2277 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2278 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
2279 "dot11RSNA4WayHandshakeFailures=%u\n"
2280 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
2282 !!wpa_auth->conf.wpa_strict_rekey,
2283 dot11RSNAConfigGroupUpdateCount,
2284 dot11RSNAConfigPairwiseUpdateCount,
2285 wpa_cipher_bits(wpa_auth->conf.wpa_group),
2286 dot11RSNAConfigPMKLifetime,
2287 dot11RSNAConfigPMKReauthThreshold,
2288 dot11RSNAConfigSATimeout,
2289 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
2290 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
2291 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
2293 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
2294 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
2295 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
2296 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
2297 wpa_auth->dot11RSNA4WayHandshakeFailures);
2298 if (ret < 0 || (size_t) ret >= buflen - len)
2302 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
2303 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
2306 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
2307 wpa_auth->group->wpa_group_state);
2308 if (ret < 0 || (size_t) ret >= buflen - len)
2316 int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
2324 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
2326 /* dot11RSNAStatsEntry */
2328 if (sm->wpa == WPA_VERSION_WPA) {
2329 if (sm->pairwise == WPA_CIPHER_CCMP)
2330 pairwise = WPA_CIPHER_SUITE_CCMP;
2331 else if (sm->pairwise == WPA_CIPHER_TKIP)
2332 pairwise = WPA_CIPHER_SUITE_TKIP;
2333 else if (sm->pairwise == WPA_CIPHER_WEP104)
2334 pairwise = WPA_CIPHER_SUITE_WEP104;
2335 else if (sm->pairwise == WPA_CIPHER_WEP40)
2336 pairwise = WPA_CIPHER_SUITE_WEP40;
2337 else if (sm->pairwise == WPA_CIPHER_NONE)
2338 pairwise = WPA_CIPHER_SUITE_NONE;
2339 } else if (sm->wpa == WPA_VERSION_WPA2) {
2340 if (sm->pairwise == WPA_CIPHER_CCMP)
2341 pairwise = RSN_CIPHER_SUITE_CCMP;
2342 else if (sm->pairwise == WPA_CIPHER_TKIP)
2343 pairwise = RSN_CIPHER_SUITE_TKIP;
2344 else if (sm->pairwise == WPA_CIPHER_WEP104)
2345 pairwise = RSN_CIPHER_SUITE_WEP104;
2346 else if (sm->pairwise == WPA_CIPHER_WEP40)
2347 pairwise = RSN_CIPHER_SUITE_WEP40;
2348 else if (sm->pairwise == WPA_CIPHER_NONE)
2349 pairwise = RSN_CIPHER_SUITE_NONE;
2354 buf + len, buflen - len,
2355 /* TODO: dot11RSNAStatsIndex */
2356 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
2357 "dot11RSNAStatsVersion=1\n"
2358 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
2359 /* TODO: dot11RSNAStatsTKIPICVErrors */
2360 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
2361 "dot11RSNAStatsTKIPRemoveMICFailures=%u\n"
2362 /* TODO: dot11RSNAStatsCCMPReplays */
2363 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
2364 /* TODO: dot11RSNAStatsTKIPReplays */,
2366 RSN_SUITE_ARG(pairwise),
2367 sm->dot11RSNAStatsTKIPLocalMICFailures,
2368 sm->dot11RSNAStatsTKIPRemoteMICFailures);
2369 if (ret < 0 || (size_t) ret >= buflen - len)
2374 ret = os_snprintf(buf + len, buflen - len,
2375 "hostapdWPAPTKState=%d\n"
2376 "hostapdWPAPTKGroupState=%d\n",
2378 sm->wpa_ptk_group_state);
2379 if (ret < 0 || (size_t) ret >= buflen - len)
2387 void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
2390 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
2394 int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
2396 return sm && sm->pairwise_set;
2400 int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
2402 return sm->pairwise;
2406 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
2410 return sm->wpa_key_mgmt;
2414 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
2422 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
2423 struct rsn_pmksa_cache_entry *entry)
2425 if (sm == NULL || sm->pmksa != entry)
2432 struct rsn_pmksa_cache_entry *
2433 wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
2435 return sm ? sm->pmksa : NULL;
2439 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
2442 sm->dot11RSNAStatsTKIPLocalMICFailures++;
2446 const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
2448 if (wpa_auth == NULL)
2450 *len = wpa_auth->wpa_ie_len;
2451 return wpa_auth->wpa_ie;
2455 int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
2456 int session_timeout, struct eapol_state_machine *eapol)
2458 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2)
2461 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
2462 sm->wpa_auth->addr, sm->addr, session_timeout,
2463 eapol, sm->wpa_key_mgmt))
2470 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
2471 const u8 *pmk, size_t len, const u8 *sta_addr,
2472 int session_timeout,
2473 struct eapol_state_machine *eapol)
2475 if (wpa_auth == NULL)
2478 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, wpa_auth->addr,
2479 sta_addr, session_timeout, eapol,
2480 WPA_KEY_MGMT_IEEE8021X))
2487 static struct wpa_group *
2488 wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
2490 struct wpa_group *group;
2492 if (wpa_auth == NULL || wpa_auth->group == NULL)
2495 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
2497 group = wpa_group_init(wpa_auth, vlan_id);
2501 group->next = wpa_auth->group->next;
2502 wpa_auth->group->next = group;
2508 int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
2510 struct wpa_group *group;
2512 if (sm == NULL || sm->wpa_auth == NULL)
2515 group = sm->wpa_auth->group;
2517 if (group->vlan_id == vlan_id)
2519 group = group->next;
2522 if (group == NULL) {
2523 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
2528 if (sm->group == group)
2531 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
2532 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);