2 * IEEE 802.11 RSN / WPA Authenticator
3 * Copyright (c) 2004-2011, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "utils/state_machine.h"
14 #include "common/ieee802_11_defs.h"
15 #include "crypto/aes_wrap.h"
16 #include "crypto/crypto.h"
17 #include "crypto/sha1.h"
18 #include "crypto/sha256.h"
19 #include "crypto/random.h"
20 #include "eapol_auth/eapol_auth_sm.h"
21 #include "ap_config.h"
22 #include "ieee802_11.h"
24 #include "pmksa_cache_auth.h"
25 #include "wpa_auth_i.h"
26 #include "wpa_auth_ie.h"
28 #define STATE_MACHINE_DATA struct wpa_state_machine
29 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
30 #define STATE_MACHINE_ADDR sm->addr
33 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
34 static int wpa_sm_step(struct wpa_state_machine *sm);
35 static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len);
36 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
37 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
38 struct wpa_group *group);
39 static void wpa_request_new_ptk(struct wpa_state_machine *sm);
40 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
41 struct wpa_group *group);
42 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
43 struct wpa_group *group);
45 static const u32 dot11RSNAConfigGroupUpdateCount = 4;
46 static const u32 dot11RSNAConfigPairwiseUpdateCount = 4;
47 static const u32 eapol_key_timeout_first = 100; /* ms */
48 static const u32 eapol_key_timeout_subseq = 1000; /* ms */
49 static const u32 eapol_key_timeout_first_group = 500; /* ms */
51 /* TODO: make these configurable */
52 static const int dot11RSNAConfigPMKLifetime = 43200;
53 static const int dot11RSNAConfigPMKReauthThreshold = 70;
54 static const int dot11RSNAConfigSATimeout = 60;
57 static inline int wpa_auth_mic_failure_report(
58 struct wpa_authenticator *wpa_auth, const u8 *addr)
60 if (wpa_auth->cb.mic_failure_report)
61 return wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr);
66 static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
67 const u8 *addr, wpa_eapol_variable var,
70 if (wpa_auth->cb.set_eapol)
71 wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value);
75 static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
76 const u8 *addr, wpa_eapol_variable var)
78 if (wpa_auth->cb.get_eapol == NULL)
80 return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var);
84 static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
85 const u8 *addr, const u8 *prev_psk)
87 if (wpa_auth->cb.get_psk == NULL)
89 return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, prev_psk);
93 static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
94 const u8 *addr, u8 *msk, size_t *len)
96 if (wpa_auth->cb.get_msk == NULL)
98 return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len);
102 static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
104 enum wpa_alg alg, const u8 *addr, int idx,
105 u8 *key, size_t key_len)
107 if (wpa_auth->cb.set_key == NULL)
109 return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
114 static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
115 const u8 *addr, int idx, u8 *seq)
117 if (wpa_auth->cb.get_seqnum == NULL)
119 return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
124 wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
125 const u8 *data, size_t data_len, int encrypt)
127 if (wpa_auth->cb.send_eapol == NULL)
129 return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
134 int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
135 int (*cb)(struct wpa_state_machine *sm, void *ctx),
138 if (wpa_auth->cb.for_each_sta == NULL)
140 return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
144 int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
145 int (*cb)(struct wpa_authenticator *a, void *ctx),
148 if (wpa_auth->cb.for_each_auth == NULL)
150 return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
154 void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
155 logger_level level, const char *txt)
157 if (wpa_auth->cb.logger == NULL)
159 wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
163 void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
164 logger_level level, const char *fmt, ...)
170 if (wpa_auth->cb.logger == NULL)
173 maxlen = os_strlen(fmt) + 100;
174 format = os_malloc(maxlen);
179 vsnprintf(format, maxlen, fmt, ap);
182 wpa_auth_logger(wpa_auth, addr, level, format);
188 static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
191 if (wpa_auth->cb.disconnect == NULL)
193 wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR, MAC2STR(addr));
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 (random_get_bytes(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");
223 wpa_hexdump_key(MSG_DEBUG, "GMK",
224 wpa_auth->group->GMK, WPA_GMK_LEN);
227 if (wpa_auth->conf.wpa_gmk_rekey) {
228 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
229 wpa_rekey_gmk, wpa_auth, NULL);
234 static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
236 struct wpa_authenticator *wpa_auth = eloop_ctx;
237 struct wpa_group *group;
239 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
240 for (group = wpa_auth->group; group; group = group->next) {
241 group->GTKReKey = TRUE;
243 group->changed = FALSE;
244 wpa_group_sm_step(wpa_auth, group);
245 } while (group->changed);
248 if (wpa_auth->conf.wpa_group_rekey) {
249 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
250 0, wpa_rekey_gtk, wpa_auth, NULL);
255 static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
257 struct wpa_authenticator *wpa_auth = eloop_ctx;
258 struct wpa_state_machine *sm = timeout_ctx;
260 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
261 wpa_request_new_ptk(sm);
266 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
268 if (sm->pmksa == ctx)
274 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
277 struct wpa_authenticator *wpa_auth = ctx;
278 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
282 static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
283 struct wpa_group *group)
285 u8 buf[ETH_ALEN + 8 + sizeof(group)];
288 if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
290 wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
293 * Counter = PRF-256(Random number, "Init Counter",
294 * Local MAC Address || Time)
296 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
297 wpa_get_ntp_timestamp(buf + ETH_ALEN);
298 os_memcpy(buf + ETH_ALEN + 8, &group, sizeof(group));
299 if (random_get_bytes(rkey, sizeof(rkey)) < 0)
302 if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
303 group->Counter, WPA_NONCE_LEN) < 0)
305 wpa_hexdump_key(MSG_DEBUG, "Key Counter",
306 group->Counter, WPA_NONCE_LEN);
312 static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
313 int vlan_id, int delay_init)
315 struct wpa_group *group;
317 group = os_zalloc(sizeof(struct wpa_group));
321 group->GTKAuthenticator = TRUE;
322 group->vlan_id = vlan_id;
323 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
325 if (random_pool_ready() != 1) {
326 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
327 "for secure operations - update keys later when "
328 "the first station connects");
332 * Set initial GMK/Counter value here. The actual values that will be
333 * used in negotiations will be set once the first station tries to
334 * connect. This allows more time for collecting additional randomness
335 * on embedded devices.
337 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
338 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
346 wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start "
347 "until Beacon frames have been configured");
348 /* Initialization is completed in wpa_init_keys(). */
350 wpa_group_sm_step(wpa_auth, group);
351 group->GInit = FALSE;
352 wpa_group_sm_step(wpa_auth, group);
360 * wpa_init - Initialize WPA authenticator
361 * @addr: Authenticator address
362 * @conf: Configuration for WPA authenticator
363 * @cb: Callback functions for WPA authenticator
364 * Returns: Pointer to WPA authenticator data or %NULL on failure
366 struct wpa_authenticator * wpa_init(const u8 *addr,
367 struct wpa_auth_config *conf,
368 struct wpa_auth_callbacks *cb)
370 struct wpa_authenticator *wpa_auth;
372 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
373 if (wpa_auth == NULL)
375 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
376 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
377 os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
379 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
380 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
385 wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
386 if (wpa_auth->group == NULL) {
387 os_free(wpa_auth->wpa_ie);
392 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
394 if (wpa_auth->pmksa == NULL) {
395 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
396 os_free(wpa_auth->wpa_ie);
401 #ifdef CONFIG_IEEE80211R
402 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
403 if (wpa_auth->ft_pmk_cache == NULL) {
404 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
405 os_free(wpa_auth->wpa_ie);
406 pmksa_cache_auth_deinit(wpa_auth->pmksa);
410 #endif /* CONFIG_IEEE80211R */
412 if (wpa_auth->conf.wpa_gmk_rekey) {
413 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
414 wpa_rekey_gmk, wpa_auth, NULL);
417 if (wpa_auth->conf.wpa_group_rekey) {
418 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
419 wpa_rekey_gtk, wpa_auth, NULL);
426 int wpa_init_keys(struct wpa_authenticator *wpa_auth)
428 struct wpa_group *group = wpa_auth->group;
430 wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial "
432 wpa_group_sm_step(wpa_auth, group);
433 group->GInit = FALSE;
434 wpa_group_sm_step(wpa_auth, group);
440 * wpa_deinit - Deinitialize WPA authenticator
441 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
443 void wpa_deinit(struct wpa_authenticator *wpa_auth)
445 struct wpa_group *group, *prev;
447 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
448 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
450 #ifdef CONFIG_PEERKEY
451 while (wpa_auth->stsl_negotiations)
452 wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
453 #endif /* CONFIG_PEERKEY */
455 pmksa_cache_auth_deinit(wpa_auth->pmksa);
457 #ifdef CONFIG_IEEE80211R
458 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
459 wpa_auth->ft_pmk_cache = NULL;
460 #endif /* CONFIG_IEEE80211R */
462 os_free(wpa_auth->wpa_ie);
464 group = wpa_auth->group;
476 * wpa_reconfig - Update WPA authenticator configuration
477 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
478 * @conf: Configuration for WPA authenticator
480 int wpa_reconfig(struct wpa_authenticator *wpa_auth,
481 struct wpa_auth_config *conf)
483 struct wpa_group *group;
484 if (wpa_auth == NULL)
487 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
488 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
489 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
494 * Reinitialize GTK to make sure it is suitable for the new
497 group = wpa_auth->group;
498 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
500 wpa_group_sm_step(wpa_auth, group);
501 group->GInit = FALSE;
502 wpa_group_sm_step(wpa_auth, group);
508 struct wpa_state_machine *
509 wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr)
511 struct wpa_state_machine *sm;
513 sm = os_zalloc(sizeof(struct wpa_state_machine));
516 os_memcpy(sm->addr, addr, ETH_ALEN);
518 sm->wpa_auth = wpa_auth;
519 sm->group = wpa_auth->group;
525 int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
526 struct wpa_state_machine *sm)
528 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
531 #ifdef CONFIG_IEEE80211R
532 if (sm->ft_completed) {
533 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
534 "FT authentication already completed - do not "
535 "start 4-way handshake");
538 #endif /* CONFIG_IEEE80211R */
541 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
542 sm->ReAuthenticationRequest = TRUE;
543 return wpa_sm_step(sm);
546 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
547 "start authentication");
551 if (wpa_sm_step(sm) == 1)
552 return 1; /* should not really happen */
554 sm->AuthenticationRequest = TRUE;
555 return wpa_sm_step(sm);
559 void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
561 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
562 * reassociates back to the same AP while the previous entry for the
563 * STA has not yet been removed. */
567 sm->wpa_key_mgmt = 0;
571 static void wpa_free_sta_sm(struct wpa_state_machine *sm)
573 if (sm->GUpdateStationKeys) {
574 sm->group->GKeyDoneStations--;
575 sm->GUpdateStationKeys = FALSE;
577 #ifdef CONFIG_IEEE80211R
578 os_free(sm->assoc_resp_ftie);
579 #endif /* CONFIG_IEEE80211R */
580 os_free(sm->last_rx_eapol_key);
586 void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
591 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
592 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
593 "strict rekeying - force GTK rekey since STA "
595 eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
596 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
600 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
601 sm->pending_1_of_4_timeout = 0;
602 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
603 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
604 if (sm->in_step_loop) {
605 /* Must not free state machine while wpa_sm_step() is running.
606 * Freeing will be completed in the end of wpa_sm_step(). */
607 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
608 "machine deinit for " MACSTR, MAC2STR(sm->addr));
609 sm->pending_deinit = 1;
615 static void wpa_request_new_ptk(struct wpa_state_machine *sm)
620 sm->PTKRequest = TRUE;
625 static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
626 const u8 *replay_counter)
629 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
632 if (os_memcmp(replay_counter, ctr[i].counter,
633 WPA_REPLAY_COUNTER_LEN) == 0)
640 static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
641 const u8 *replay_counter)
644 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
646 (replay_counter == NULL ||
647 os_memcmp(replay_counter, ctr[i].counter,
648 WPA_REPLAY_COUNTER_LEN) == 0))
649 ctr[i].valid = FALSE;
654 #ifdef CONFIG_IEEE80211R
655 static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
656 struct wpa_state_machine *sm,
657 struct wpa_eapol_ie_parse *kde)
659 struct wpa_ie_data ie;
660 struct rsn_mdie *mdie;
662 if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
663 ie.num_pmkid != 1 || ie.pmkid == NULL) {
664 wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
665 "FT 4-way handshake message 2/4");
669 os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
670 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
671 sm->sup_pmk_r1_name, PMKID_LEN);
673 if (!kde->mdie || !kde->ftie) {
674 wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
675 "message 2/4", kde->mdie ? "FTIE" : "MDIE");
679 mdie = (struct rsn_mdie *) (kde->mdie + 2);
680 if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
681 os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
682 MOBILITY_DOMAIN_ID_LEN) != 0) {
683 wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
687 if (sm->assoc_resp_ftie &&
688 (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
689 os_memcmp(kde->ftie, sm->assoc_resp_ftie,
690 2 + sm->assoc_resp_ftie[1]) != 0)) {
691 wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
692 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
693 kde->ftie, kde->ftie_len);
694 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
695 sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
701 #endif /* CONFIG_IEEE80211R */
704 static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
705 struct wpa_state_machine *sm, int group)
707 /* Supplicant reported a Michael MIC error */
708 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
709 "received EAPOL-Key Error Request "
710 "(STA detected Michael MIC failure (group=%d))",
713 if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
714 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
715 "ignore Michael MIC failure report since "
716 "group cipher is not TKIP");
717 } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
718 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
719 "ignore Michael MIC failure report since "
720 "pairwise cipher is not TKIP");
722 if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
723 return 1; /* STA entry was removed */
724 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
725 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
729 * Error report is not a request for a new key handshake, but since
730 * Authenticator may do it, let's change the keys now anyway.
732 wpa_request_new_ptk(sm);
737 void wpa_receive(struct wpa_authenticator *wpa_auth,
738 struct wpa_state_machine *sm,
739 u8 *data, size_t data_len)
741 struct ieee802_1x_hdr *hdr;
742 struct wpa_eapol_key *key;
743 u16 key_info, key_data_length;
744 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
745 SMK_M1, SMK_M3, SMK_ERROR } msg;
747 struct wpa_eapol_ie_parse kde;
749 const u8 *eapol_key_ie;
750 size_t eapol_key_ie_len;
752 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
755 if (data_len < sizeof(*hdr) + sizeof(*key))
758 hdr = (struct ieee802_1x_hdr *) data;
759 key = (struct wpa_eapol_key *) (hdr + 1);
760 key_info = WPA_GET_BE16(key->key_info);
761 key_data_length = WPA_GET_BE16(key->key_data_length);
762 wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
763 " key_info=0x%x type=%u key_data_length=%u",
764 MAC2STR(sm->addr), key_info, key->type, key_data_length);
765 if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) {
766 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
767 "key_data overflow (%d > %lu)",
769 (unsigned long) (data_len - sizeof(*hdr) -
774 if (sm->wpa == WPA_VERSION_WPA2) {
775 if (key->type == EAPOL_KEY_TYPE_WPA) {
777 * Some deployed station implementations seem to send
778 * msg 4/4 with incorrect type value in WPA2 mode.
780 wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
781 "with unexpected WPA type in RSN mode");
782 } else if (key->type != EAPOL_KEY_TYPE_RSN) {
783 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
784 "unexpected type %d in RSN mode",
789 if (key->type != EAPOL_KEY_TYPE_WPA) {
790 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
791 "unexpected type %d in WPA mode",
797 wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
799 wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
800 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
802 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
805 if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
806 (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
807 if (key_info & WPA_KEY_INFO_ERROR) {
809 msgtxt = "SMK Error";
814 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
817 } else if (key_info & WPA_KEY_INFO_REQUEST) {
820 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
822 msgtxt = "2/2 Group";
823 } else if (key_data_length == 0) {
825 msgtxt = "4/4 Pairwise";
828 msgtxt = "2/4 Pairwise";
831 /* TODO: key_info type validation for PeerKey */
832 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
834 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
835 if (sm->pairwise == WPA_CIPHER_CCMP ||
836 sm->pairwise == WPA_CIPHER_GCMP) {
837 if (wpa_use_aes_cmac(sm) &&
838 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
839 wpa_auth_logger(wpa_auth, sm->addr,
841 "advertised support for "
842 "AES-128-CMAC, but did not "
847 if (!wpa_use_aes_cmac(sm) &&
848 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
849 wpa_auth_logger(wpa_auth, sm->addr,
851 "did not use HMAC-SHA1-AES "
858 if (key_info & WPA_KEY_INFO_REQUEST) {
859 if (sm->req_replay_counter_used &&
860 os_memcmp(key->replay_counter, sm->req_replay_counter,
861 WPA_REPLAY_COUNTER_LEN) <= 0) {
862 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
863 "received EAPOL-Key request with "
869 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
870 !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
873 if (msg == PAIRWISE_2 &&
874 wpa_replay_counter_valid(sm->prev_key_replay,
875 key->replay_counter) &&
876 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
877 os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
880 * Some supplicant implementations (e.g., Windows XP
881 * WZC) update SNonce for each EAPOL-Key 2/4. This
882 * breaks the workaround on accepting any of the
883 * pending requests, so allow the SNonce to be updated
884 * even if we have already sent out EAPOL-Key 3/4.
886 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
887 "Process SNonce update from STA "
888 "based on retransmitted EAPOL-Key "
890 sm->update_snonce = 1;
891 wpa_replay_counter_mark_invalid(sm->prev_key_replay,
892 key->replay_counter);
893 goto continue_processing;
896 if (msg == PAIRWISE_2 &&
897 wpa_replay_counter_valid(sm->prev_key_replay,
898 key->replay_counter) &&
899 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
900 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
901 "ignore retransmitted EAPOL-Key %s - "
902 "SNonce did not change", msgtxt);
904 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
905 "received EAPOL-Key %s with "
906 "unexpected replay counter", msgtxt);
908 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
909 if (!sm->key_replay[i].valid)
911 wpa_hexdump(MSG_DEBUG, "pending replay counter",
912 sm->key_replay[i].counter,
913 WPA_REPLAY_COUNTER_LEN);
915 wpa_hexdump(MSG_DEBUG, "received replay counter",
916 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
923 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
924 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
925 (!sm->update_snonce ||
926 sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
927 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
928 "received EAPOL-Key msg 2/4 in "
929 "invalid state (%d) - dropped",
933 random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
934 if (sm->group->reject_4way_hs_for_entropy) {
936 * The system did not have enough entropy to generate
937 * strong random numbers. Reject the first 4-way
938 * handshake(s) and collect some entropy based on the
939 * information from it. Once enough entropy is
940 * available, the next atempt will trigger GMK/Key
941 * Counter update and the station will be allowed to
944 wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
945 "collect more entropy for random number "
947 random_mark_pool_ready();
948 wpa_sta_disconnect(wpa_auth, sm->addr);
951 if (wpa_parse_kde_ies((u8 *) (key + 1), key_data_length,
953 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
954 "received EAPOL-Key msg 2/4 with "
955 "invalid Key Data contents");
959 eapol_key_ie = kde.rsn_ie;
960 eapol_key_ie_len = kde.rsn_ie_len;
962 eapol_key_ie = kde.wpa_ie;
963 eapol_key_ie_len = kde.wpa_ie_len;
965 ft = sm->wpa == WPA_VERSION_WPA2 &&
966 wpa_key_mgmt_ft(sm->wpa_key_mgmt);
967 if (sm->wpa_ie == NULL ||
968 wpa_compare_rsn_ie(ft,
969 sm->wpa_ie, sm->wpa_ie_len,
970 eapol_key_ie, eapol_key_ie_len)) {
971 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
972 "WPA IE from (Re)AssocReq did not "
973 "match with msg 2/4");
975 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
976 sm->wpa_ie, sm->wpa_ie_len);
978 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
979 eapol_key_ie, eapol_key_ie_len);
980 /* MLME-DEAUTHENTICATE.request */
981 wpa_sta_disconnect(wpa_auth, sm->addr);
984 #ifdef CONFIG_IEEE80211R
985 if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
986 wpa_sta_disconnect(wpa_auth, sm->addr);
989 #endif /* CONFIG_IEEE80211R */
992 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
994 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
995 "received EAPOL-Key msg 4/4 in "
996 "invalid state (%d) - dropped",
1002 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1003 || !sm->PTK_valid) {
1004 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1005 "received EAPOL-Key msg 2/2 in "
1006 "invalid state (%d) - dropped",
1007 sm->wpa_ptk_group_state);
1011 #ifdef CONFIG_PEERKEY
1015 if (!wpa_auth->conf.peerkey) {
1016 wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
1017 "PeerKey use disabled - ignoring message");
1020 if (!sm->PTK_valid) {
1021 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1022 "received EAPOL-Key msg SMK in "
1023 "invalid state - dropped");
1027 #else /* CONFIG_PEERKEY */
1031 return; /* STSL disabled - ignore SMK messages */
1032 #endif /* CONFIG_PEERKEY */
1037 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1038 "received EAPOL-Key frame (%s)", msgtxt);
1040 if (key_info & WPA_KEY_INFO_ACK) {
1041 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1042 "received invalid EAPOL-Key: Key Ack set");
1046 if (!(key_info & WPA_KEY_INFO_MIC)) {
1047 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1048 "received invalid EAPOL-Key: Key MIC not set");
1052 sm->MICVerified = FALSE;
1053 if (sm->PTK_valid && !sm->update_snonce) {
1054 if (wpa_verify_key_mic(&sm->PTK, data, data_len)) {
1055 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1056 "received EAPOL-Key with invalid MIC");
1059 sm->MICVerified = TRUE;
1060 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
1061 sm->pending_1_of_4_timeout = 0;
1064 if (key_info & WPA_KEY_INFO_REQUEST) {
1065 if (sm->MICVerified) {
1066 sm->req_replay_counter_used = 1;
1067 os_memcpy(sm->req_replay_counter, key->replay_counter,
1068 WPA_REPLAY_COUNTER_LEN);
1070 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1071 "received EAPOL-Key request with "
1077 * TODO: should decrypt key data field if encryption was used;
1078 * even though MAC address KDE is not normally encrypted,
1079 * supplicant is allowed to encrypt it.
1081 if (msg == SMK_ERROR) {
1082 #ifdef CONFIG_PEERKEY
1083 wpa_smk_error(wpa_auth, sm, key);
1084 #endif /* CONFIG_PEERKEY */
1086 } else if (key_info & WPA_KEY_INFO_ERROR) {
1087 if (wpa_receive_error_report(
1089 !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
1090 return; /* STA entry was removed */
1091 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1092 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1093 "received EAPOL-Key Request for new "
1095 wpa_request_new_ptk(sm);
1096 #ifdef CONFIG_PEERKEY
1097 } else if (msg == SMK_M1) {
1098 wpa_smk_m1(wpa_auth, sm, key);
1099 #endif /* CONFIG_PEERKEY */
1100 } else if (key_data_length > 0 &&
1101 wpa_parse_kde_ies((const u8 *) (key + 1),
1102 key_data_length, &kde) == 0 &&
1105 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1106 "received EAPOL-Key Request for GTK "
1108 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
1109 wpa_rekey_gtk(wpa_auth, NULL);
1112 /* Do not allow the same key replay counter to be reused. */
1113 wpa_replay_counter_mark_invalid(sm->key_replay,
1114 key->replay_counter);
1116 if (msg == PAIRWISE_2) {
1118 * Maintain a copy of the pending EAPOL-Key frames in
1119 * case the EAPOL-Key frame was retransmitted. This is
1120 * needed to allow EAPOL-Key msg 2/4 reply to another
1121 * pending msg 1/4 to update the SNonce to work around
1122 * unexpected supplicant behavior.
1124 os_memcpy(sm->prev_key_replay, sm->key_replay,
1125 sizeof(sm->key_replay));
1127 os_memset(sm->prev_key_replay, 0,
1128 sizeof(sm->prev_key_replay));
1132 * Make sure old valid counters are not accepted anymore and
1133 * do not get copied again.
1135 wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
1138 #ifdef CONFIG_PEERKEY
1139 if (msg == SMK_M3) {
1140 wpa_smk_m3(wpa_auth, sm, key);
1143 #endif /* CONFIG_PEERKEY */
1145 os_free(sm->last_rx_eapol_key);
1146 sm->last_rx_eapol_key = os_malloc(data_len);
1147 if (sm->last_rx_eapol_key == NULL)
1149 os_memcpy(sm->last_rx_eapol_key, data, data_len);
1150 sm->last_rx_eapol_key_len = data_len;
1152 sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
1153 sm->EAPOLKeyReceived = TRUE;
1154 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1155 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1156 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1161 static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
1162 const u8 *gnonce, u8 *gtk, size_t gtk_len)
1164 u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + 16];
1168 /* GTK = PRF-X(GMK, "Group key expansion",
1169 * AA || GNonce || Time || random data)
1170 * The example described in the IEEE 802.11 standard uses only AA and
1171 * GNonce as inputs here. Add some more entropy since this derivation
1172 * is done only at the Authenticator and as such, does not need to be
1175 os_memcpy(data, addr, ETH_ALEN);
1176 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
1177 pos = data + ETH_ALEN + WPA_NONCE_LEN;
1178 wpa_get_ntp_timestamp(pos);
1180 if (random_get_bytes(pos, 16) < 0)
1183 #ifdef CONFIG_IEEE80211W
1184 sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len);
1185 #else /* CONFIG_IEEE80211W */
1186 if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len)
1189 #endif /* CONFIG_IEEE80211W */
1195 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1197 struct wpa_authenticator *wpa_auth = eloop_ctx;
1198 struct wpa_state_machine *sm = timeout_ctx;
1200 sm->pending_1_of_4_timeout = 0;
1201 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
1202 sm->TimeoutEvt = TRUE;
1207 void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1208 struct wpa_state_machine *sm, int key_info,
1209 const u8 *key_rsc, const u8 *nonce,
1210 const u8 *kde, size_t kde_len,
1211 int keyidx, int encr, int force_version)
1213 struct ieee802_1x_hdr *hdr;
1214 struct wpa_eapol_key *key;
1217 int key_data_len, pad_len = 0;
1219 int version, pairwise;
1222 len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
1225 version = force_version;
1226 else if (wpa_use_aes_cmac(sm))
1227 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
1228 else if (sm->pairwise != WPA_CIPHER_TKIP)
1229 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1231 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1233 pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1235 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1236 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1239 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
1240 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
1241 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
1242 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
1243 pairwise, (unsigned long) kde_len, keyidx, encr);
1245 key_data_len = kde_len;
1247 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1248 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1249 pad_len = key_data_len % 8;
1251 pad_len = 8 - pad_len;
1252 key_data_len += pad_len + 8;
1255 len += key_data_len;
1257 hdr = os_zalloc(len);
1260 hdr->version = wpa_auth->conf.eapol_version;
1261 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1262 hdr->length = host_to_be16(len - sizeof(*hdr));
1263 key = (struct wpa_eapol_key *) (hdr + 1);
1265 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1266 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1267 key_info |= version;
1268 if (encr && sm->wpa == WPA_VERSION_WPA2)
1269 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1270 if (sm->wpa != WPA_VERSION_WPA2)
1271 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1272 WPA_PUT_BE16(key->key_info, key_info);
1274 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
1275 WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
1276 if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
1277 WPA_PUT_BE16(key->key_length, 0);
1279 /* FIX: STSL: what to use as key_replay_counter? */
1280 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1281 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1282 os_memcpy(sm->key_replay[i].counter,
1283 sm->key_replay[i - 1].counter,
1284 WPA_REPLAY_COUNTER_LEN);
1286 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1287 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1288 WPA_REPLAY_COUNTER_LEN);
1289 sm->key_replay[0].valid = TRUE;
1292 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1295 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1298 os_memcpy(key + 1, kde, kde_len);
1299 WPA_PUT_BE16(key->key_data_length, kde_len);
1300 } else if (encr && kde) {
1301 buf = os_zalloc(key_data_len);
1307 os_memcpy(pos, kde, kde_len);
1313 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1315 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1316 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1317 if (aes_wrap(sm->PTK.kek, (key_data_len - 8) / 8, buf,
1318 (u8 *) (key + 1))) {
1323 WPA_PUT_BE16(key->key_data_length, key_data_len);
1326 os_memcpy(key->key_iv,
1327 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1328 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1329 os_memcpy(ek, key->key_iv, 16);
1330 os_memcpy(ek + 16, sm->PTK.kek, 16);
1331 os_memcpy(key + 1, buf, key_data_len);
1332 rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
1333 WPA_PUT_BE16(key->key_data_length, key_data_len);
1338 if (key_info & WPA_KEY_INFO_MIC) {
1339 if (!sm->PTK_valid) {
1340 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1341 "PTK not valid when sending EAPOL-Key "
1346 wpa_eapol_key_mic(sm->PTK.kck, version, (u8 *) hdr, len,
1350 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1352 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1358 static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1359 struct wpa_state_machine *sm, int key_info,
1360 const u8 *key_rsc, const u8 *nonce,
1361 const u8 *kde, size_t kde_len,
1362 int keyidx, int encr)
1365 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1371 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1374 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1375 if (ctr == 1 && wpa_auth->conf.tx_status)
1376 timeout_ms = pairwise ? eapol_key_timeout_first :
1377 eapol_key_timeout_first_group;
1379 timeout_ms = eapol_key_timeout_subseq;
1380 if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
1381 sm->pending_1_of_4_timeout = 1;
1382 wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1383 "counter %d)", timeout_ms, ctr);
1384 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1385 wpa_send_eapol_timeout, wpa_auth, sm);
1389 static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len)
1391 struct ieee802_1x_hdr *hdr;
1392 struct wpa_eapol_key *key;
1397 if (data_len < sizeof(*hdr) + sizeof(*key))
1400 hdr = (struct ieee802_1x_hdr *) data;
1401 key = (struct wpa_eapol_key *) (hdr + 1);
1402 key_info = WPA_GET_BE16(key->key_info);
1403 os_memcpy(mic, key->key_mic, 16);
1404 os_memset(key->key_mic, 0, 16);
1405 if (wpa_eapol_key_mic(PTK->kck, key_info & WPA_KEY_INFO_TYPE_MASK,
1406 data, data_len, key->key_mic) ||
1407 os_memcmp(mic, key->key_mic, 16) != 0)
1409 os_memcpy(key->key_mic, mic, 16);
1414 void wpa_remove_ptk(struct wpa_state_machine *sm)
1416 sm->PTK_valid = FALSE;
1417 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1418 wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL, 0);
1419 sm->pairwise_set = FALSE;
1420 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1424 int wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
1431 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1432 "event %d notification", event);
1440 sm->DeauthenticationRequest = TRUE;
1443 case WPA_REAUTH_EAPOL:
1446 * When using WPS, we may end up here if the STA
1447 * manages to re-associate without the previous STA
1448 * entry getting removed. Consequently, we need to make
1449 * sure that the WPA state machines gets initialized
1450 * properly at this point.
1452 wpa_printf(MSG_DEBUG, "WPA state machine had not been "
1453 "started - initialize now");
1456 if (wpa_sm_step(sm) == 1)
1457 return 1; /* should not really happen */
1459 sm->AuthenticationRequest = TRUE;
1462 if (sm->GUpdateStationKeys) {
1464 * Reauthentication cancels the pending group key
1465 * update for this STA.
1467 sm->group->GKeyDoneStations--;
1468 sm->GUpdateStationKeys = FALSE;
1469 sm->PtkGroupInit = TRUE;
1471 sm->ReAuthenticationRequest = TRUE;
1474 #ifdef CONFIG_IEEE80211R
1475 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1476 "after association");
1477 wpa_ft_install_ptk(sm);
1479 /* Using FT protocol, not WPA auth state machine */
1480 sm->ft_completed = 1;
1482 #else /* CONFIG_IEEE80211R */
1484 #endif /* CONFIG_IEEE80211R */
1487 #ifdef CONFIG_IEEE80211R
1488 sm->ft_completed = 0;
1489 #endif /* CONFIG_IEEE80211R */
1491 #ifdef CONFIG_IEEE80211W
1492 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1494 #endif /* CONFIG_IEEE80211W */
1497 sm->PTK_valid = FALSE;
1498 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1500 if (event != WPA_REAUTH_EAPOL)
1504 return wpa_sm_step(sm);
1508 SM_STATE(WPA_PTK, INITIALIZE)
1510 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1512 /* Init flag is not cleared here, so avoid busy
1513 * loop by claiming nothing changed. */
1514 sm->changed = FALSE;
1518 if (sm->GUpdateStationKeys)
1519 sm->group->GKeyDoneStations--;
1520 sm->GUpdateStationKeys = FALSE;
1521 if (sm->wpa == WPA_VERSION_WPA)
1522 sm->PInitAKeys = FALSE;
1523 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1524 * Local AA > Remote AA)) */) {
1527 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1529 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1531 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1532 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1533 WPA_EAPOL_authorized, 0);
1538 SM_STATE(WPA_PTK, DISCONNECT)
1540 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1541 sm->Disconnect = FALSE;
1542 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1546 SM_STATE(WPA_PTK, DISCONNECTED)
1548 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1549 sm->DeauthenticationRequest = FALSE;
1553 SM_STATE(WPA_PTK, AUTHENTICATION)
1555 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1556 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1557 sm->PTK_valid = FALSE;
1558 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1560 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1561 sm->AuthenticationRequest = FALSE;
1565 static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
1566 struct wpa_group *group)
1568 if (group->first_sta_seen)
1571 * System has run bit further than at the time hostapd was started
1572 * potentially very early during boot up. This provides better chances
1573 * of collecting more randomness on embedded systems. Re-initialize the
1574 * GMK and Counter here to improve their strength if there was not
1575 * enough entropy available immediately after system startup.
1577 wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
1579 if (random_pool_ready() != 1) {
1580 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
1581 "to proceed - reject first 4-way handshake");
1582 group->reject_4way_hs_for_entropy = TRUE;
1584 group->first_sta_seen = TRUE;
1585 group->reject_4way_hs_for_entropy = FALSE;
1588 wpa_group_init_gmk_and_counter(wpa_auth, group);
1589 wpa_gtk_update(wpa_auth, group);
1590 wpa_group_config_group_keys(wpa_auth, group);
1594 SM_STATE(WPA_PTK, AUTHENTICATION2)
1596 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1598 wpa_group_ensure_init(sm->wpa_auth, sm->group);
1601 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1602 * ambiguous. The Authenticator state machine uses a counter that is
1603 * incremented by one for each 4-way handshake. However, the security
1604 * analysis of 4-way handshake points out that unpredictable nonces
1605 * help in preventing precomputation attacks. Instead of the state
1606 * machine definition, use an unpredictable nonce value here to provide
1607 * stronger protection against potential precomputation attacks.
1609 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1610 wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
1612 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1615 wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
1617 sm->ReAuthenticationRequest = FALSE;
1618 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1619 * logical place than INITIALIZE since AUTHENTICATION2 can be
1620 * re-entered on ReAuthenticationRequest without going through
1626 static int wpa_auth_sm_ptk_update(struct wpa_state_machine *sm)
1628 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1629 wpa_printf(MSG_ERROR,
1630 "WPA: Failed to get random data for ANonce");
1631 sm->Disconnect = TRUE;
1634 wpa_hexdump(MSG_DEBUG, "WPA: Assign new ANonce", sm->ANonce,
1641 SM_STATE(WPA_PTK, INITPMK)
1643 u8 msk[2 * PMK_LEN];
1644 size_t len = 2 * PMK_LEN;
1646 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1647 #ifdef CONFIG_IEEE80211R
1649 #endif /* CONFIG_IEEE80211R */
1651 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1652 os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
1653 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1654 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1655 "(len=%lu)", (unsigned long) len);
1656 os_memcpy(sm->PMK, msk, PMK_LEN);
1657 #ifdef CONFIG_IEEE80211R
1658 if (len >= 2 * PMK_LEN) {
1659 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1660 sm->xxkey_len = PMK_LEN;
1662 #endif /* CONFIG_IEEE80211R */
1664 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
1667 sm->req_replay_counter_used = 0;
1668 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1669 * will break reauthentication since EAPOL state machines may not be
1670 * get into AUTHENTICATING state that clears keyRun before WPA state
1671 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1672 * state and takes PMK from the previously used AAA Key. This will
1673 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1674 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1675 * be good workaround for this issue. */
1676 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1680 SM_STATE(WPA_PTK, INITPSK)
1683 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
1684 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL);
1686 os_memcpy(sm->PMK, psk, PMK_LEN);
1687 #ifdef CONFIG_IEEE80211R
1688 os_memcpy(sm->xxkey, psk, PMK_LEN);
1689 sm->xxkey_len = PMK_LEN;
1690 #endif /* CONFIG_IEEE80211R */
1692 sm->req_replay_counter_used = 0;
1696 SM_STATE(WPA_PTK, PTKSTART)
1698 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1699 size_t pmkid_len = 0;
1701 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
1702 sm->PTKRequest = FALSE;
1703 sm->TimeoutEvt = FALSE;
1706 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1707 /* No point in sending the EAPOL-Key - we will disconnect
1708 * immediately following this. */
1712 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1713 "sending 1/4 msg of 4-Way Handshake");
1715 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1716 * one possible PSK for this STA.
1718 if (sm->wpa == WPA_VERSION_WPA2 &&
1719 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt)) {
1721 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
1722 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
1723 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
1724 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
1726 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
1727 sm->pmksa->pmkid, PMKID_LEN);
1730 * Calculate PMKID since no PMKSA cache entry was
1731 * available with pre-calculated PMKID.
1733 rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
1734 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
1735 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1738 wpa_send_eapol(sm->wpa_auth, sm,
1739 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
1740 sm->ANonce, pmkid, pmkid_len, 0, 0);
1744 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk,
1745 struct wpa_ptk *ptk)
1747 size_t ptk_len = sm->pairwise != WPA_CIPHER_TKIP ? 48 : 64;
1748 #ifdef CONFIG_IEEE80211R
1749 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
1750 return wpa_auth_derive_ptk_ft(sm, pmk, ptk, ptk_len);
1751 #endif /* CONFIG_IEEE80211R */
1753 wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
1754 sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce,
1755 (u8 *) ptk, ptk_len,
1756 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1762 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
1766 const u8 *pmk = NULL;
1768 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
1769 sm->EAPOLKeyReceived = FALSE;
1770 sm->update_snonce = FALSE;
1772 /* WPA with IEEE 802.1X: use the derived PMK from EAP
1773 * WPA-PSK: iterate through possible PSKs and select the one matching
1776 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1777 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, pmk);
1783 wpa_derive_ptk(sm, pmk, &PTK);
1785 if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key,
1786 sm->last_rx_eapol_key_len) == 0) {
1791 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
1796 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1797 "invalid MIC in msg 2/4 of 4-Way Handshake");
1801 #ifdef CONFIG_IEEE80211R
1802 if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
1804 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
1805 * with the value we derived.
1807 if (os_memcmp(sm->sup_pmk_r1_name, sm->pmk_r1_name,
1808 WPA_PMK_NAME_LEN) != 0) {
1809 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1810 "PMKR1Name mismatch in FT 4-way "
1812 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
1814 sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
1815 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1816 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1820 #endif /* CONFIG_IEEE80211R */
1822 sm->pending_1_of_4_timeout = 0;
1823 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
1825 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1826 /* PSK may have changed from the previous choice, so update
1827 * state machine data based on whatever PSK was selected here.
1829 os_memcpy(sm->PMK, pmk, PMK_LEN);
1832 sm->MICVerified = TRUE;
1834 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
1835 sm->PTK_valid = TRUE;
1839 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
1841 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
1846 #ifdef CONFIG_IEEE80211W
1848 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1850 if (sm->mgmt_frame_prot) {
1851 return 2 + RSN_SELECTOR_LEN + sizeof(struct wpa_igtk_kde);
1858 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1860 struct wpa_igtk_kde igtk;
1861 struct wpa_group *gsm = sm->group;
1863 if (!sm->mgmt_frame_prot)
1866 igtk.keyid[0] = gsm->GN_igtk;
1868 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
1869 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, igtk.pn) < 0)
1870 os_memset(igtk.pn, 0, sizeof(igtk.pn));
1871 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
1872 if (sm->wpa_auth->conf.disable_gtk) {
1874 * Provide unique random IGTK to each STA to prevent use of
1877 if (random_get_bytes(igtk.igtk, WPA_IGTK_LEN) < 0)
1880 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
1881 (const u8 *) &igtk, sizeof(igtk), NULL, 0);
1886 #else /* CONFIG_IEEE80211W */
1888 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1894 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1899 #endif /* CONFIG_IEEE80211W */
1902 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
1904 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
1905 size_t gtk_len, kde_len;
1906 struct wpa_group *gsm = sm->group;
1908 int wpa_ie_len, secure, keyidx, encr = 0;
1910 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
1911 sm->TimeoutEvt = FALSE;
1914 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1915 /* No point in sending the EAPOL-Key - we will disconnect
1916 * immediately following this. */
1920 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
1921 GTK[GN], IGTK, [FTIE], [TIE * 2])
1923 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
1924 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
1925 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
1926 wpa_ie = sm->wpa_auth->wpa_ie;
1927 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
1928 if (sm->wpa == WPA_VERSION_WPA &&
1929 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
1930 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
1931 /* WPA-only STA, remove RSN IE */
1932 wpa_ie = wpa_ie + wpa_ie[1] + 2;
1933 wpa_ie_len = wpa_ie[1] + 2;
1935 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1936 "sending 3/4 msg of 4-Way Handshake");
1937 if (sm->wpa == WPA_VERSION_WPA2) {
1938 /* WPA2 send GTK in the 4-way handshake */
1940 gtk = gsm->GTK[gsm->GN - 1];
1941 gtk_len = gsm->GTK_len;
1942 if (sm->wpa_auth->conf.disable_gtk) {
1944 * Provide unique random GTK to each STA to prevent use
1945 * of GTK in the BSS.
1947 if (random_get_bytes(dummy_gtk, gtk_len) < 0)
1955 /* WPA does not include GTK in msg 3/4 */
1961 if (sm->rx_eapol_key_secure) {
1963 * It looks like Windows 7 supplicant tries to use
1964 * Secure bit in msg 2/4 after having reported Michael
1965 * MIC failure and it then rejects the 4-way handshake
1966 * if msg 3/4 does not set Secure bit. Work around this
1967 * by setting the Secure bit here even in the case of
1968 * WPA if the supplicant used it first.
1970 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1971 "STA used Secure bit in WPA msg 2/4 - "
1972 "set Secure for 3/4 as workaround");
1977 kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
1979 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
1980 #ifdef CONFIG_IEEE80211R
1981 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
1982 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
1983 kde_len += 300; /* FTIE + 2 * TIE */
1985 #endif /* CONFIG_IEEE80211R */
1986 kde = os_malloc(kde_len);
1991 os_memcpy(pos, wpa_ie, wpa_ie_len);
1993 #ifdef CONFIG_IEEE80211R
1994 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
1995 int res = wpa_insert_pmkid(kde, pos - kde, sm->pmk_r1_name);
1997 wpa_printf(MSG_ERROR, "FT: Failed to insert "
1998 "PMKR1Name into RSN IE in EAPOL-Key data");
2004 #endif /* CONFIG_IEEE80211R */
2007 hdr[0] = keyidx & 0x03;
2009 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2012 pos = ieee80211w_kde_add(sm, pos);
2014 #ifdef CONFIG_IEEE80211R
2015 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2017 struct wpa_auth_config *conf;
2019 conf = &sm->wpa_auth->conf;
2020 res = wpa_write_ftie(conf, conf->r0_key_holder,
2021 conf->r0_key_holder_len,
2022 NULL, NULL, pos, kde + kde_len - pos,
2025 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
2026 "into EAPOL-Key Key Data");
2032 /* TIE[ReassociationDeadline] (TU) */
2033 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2035 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
2036 WPA_PUT_LE32(pos, conf->reassociation_deadline);
2039 /* TIE[KeyLifetime] (seconds) */
2040 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2042 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
2043 WPA_PUT_LE32(pos, conf->r0_key_lifetime * 60);
2046 #endif /* CONFIG_IEEE80211R */
2048 wpa_send_eapol(sm->wpa_auth, sm,
2049 (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
2050 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
2051 WPA_KEY_INFO_KEY_TYPE,
2052 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
2057 SM_STATE(WPA_PTK, PTKINITDONE)
2059 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
2060 sm->EAPOLKeyReceived = FALSE;
2062 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
2063 int klen = wpa_cipher_key_len(sm->pairwise);
2064 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
2065 sm->PTK.tk1, klen)) {
2066 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
2069 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
2070 sm->pairwise_set = TRUE;
2072 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
2073 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
2074 eloop_register_timeout(sm->wpa_auth->conf.
2075 wpa_ptk_rekey, 0, wpa_rekey_ptk,
2079 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2080 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2081 WPA_EAPOL_authorized, 1);
2085 if (0 /* IBSS == TRUE */) {
2087 if (sm->keycount == 2) {
2088 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2089 WPA_EAPOL_portValid, 1);
2092 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
2095 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
2096 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
2097 if (sm->wpa == WPA_VERSION_WPA)
2098 sm->PInitAKeys = TRUE;
2101 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2102 "pairwise key handshake completed (%s)",
2103 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2105 #ifdef CONFIG_IEEE80211R
2106 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
2107 #endif /* CONFIG_IEEE80211R */
2113 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2116 SM_ENTER(WPA_PTK, INITIALIZE);
2117 else if (sm->Disconnect
2118 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
2119 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
2120 "WPA_PTK: sm->Disconnect");
2121 SM_ENTER(WPA_PTK, DISCONNECT);
2123 else if (sm->DeauthenticationRequest)
2124 SM_ENTER(WPA_PTK, DISCONNECTED);
2125 else if (sm->AuthenticationRequest)
2126 SM_ENTER(WPA_PTK, AUTHENTICATION);
2127 else if (sm->ReAuthenticationRequest)
2128 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2129 else if (sm->PTKRequest) {
2130 if (wpa_auth_sm_ptk_update(sm) < 0)
2131 SM_ENTER(WPA_PTK, DISCONNECTED);
2133 SM_ENTER(WPA_PTK, PTKSTART);
2134 } else switch (sm->wpa_ptk_state) {
2135 case WPA_PTK_INITIALIZE:
2137 case WPA_PTK_DISCONNECT:
2138 SM_ENTER(WPA_PTK, DISCONNECTED);
2140 case WPA_PTK_DISCONNECTED:
2141 SM_ENTER(WPA_PTK, INITIALIZE);
2143 case WPA_PTK_AUTHENTICATION:
2144 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2146 case WPA_PTK_AUTHENTICATION2:
2147 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
2148 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2149 WPA_EAPOL_keyRun) > 0)
2150 SM_ENTER(WPA_PTK, INITPMK);
2151 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
2152 /* FIX: && 802.1X::keyRun */)
2153 SM_ENTER(WPA_PTK, INITPSK);
2155 case WPA_PTK_INITPMK:
2156 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2157 WPA_EAPOL_keyAvailable) > 0)
2158 SM_ENTER(WPA_PTK, PTKSTART);
2160 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2161 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2162 "INITPMK - keyAvailable = false");
2163 SM_ENTER(WPA_PTK, DISCONNECT);
2166 case WPA_PTK_INITPSK:
2167 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL))
2168 SM_ENTER(WPA_PTK, PTKSTART);
2170 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2171 "no PSK configured for the STA");
2172 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2173 SM_ENTER(WPA_PTK, DISCONNECT);
2176 case WPA_PTK_PTKSTART:
2177 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2178 sm->EAPOLKeyPairwise)
2179 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2180 else if (sm->TimeoutCtr >
2181 (int) dot11RSNAConfigPairwiseUpdateCount) {
2182 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2183 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2184 "PTKSTART: Retry limit %d reached",
2185 dot11RSNAConfigPairwiseUpdateCount);
2186 SM_ENTER(WPA_PTK, DISCONNECT);
2187 } else if (sm->TimeoutEvt)
2188 SM_ENTER(WPA_PTK, PTKSTART);
2190 case WPA_PTK_PTKCALCNEGOTIATING:
2191 if (sm->MICVerified)
2192 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
2193 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2194 sm->EAPOLKeyPairwise)
2195 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2196 else if (sm->TimeoutEvt)
2197 SM_ENTER(WPA_PTK, PTKSTART);
2199 case WPA_PTK_PTKCALCNEGOTIATING2:
2200 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2202 case WPA_PTK_PTKINITNEGOTIATING:
2203 if (sm->update_snonce)
2204 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2205 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2206 sm->EAPOLKeyPairwise && sm->MICVerified)
2207 SM_ENTER(WPA_PTK, PTKINITDONE);
2208 else if (sm->TimeoutCtr >
2209 (int) dot11RSNAConfigPairwiseUpdateCount) {
2210 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2211 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2212 "PTKINITNEGOTIATING: Retry limit %d "
2214 dot11RSNAConfigPairwiseUpdateCount);
2215 SM_ENTER(WPA_PTK, DISCONNECT);
2216 } else if (sm->TimeoutEvt)
2217 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2219 case WPA_PTK_PTKINITDONE:
2225 SM_STATE(WPA_PTK_GROUP, IDLE)
2227 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
2229 /* Init flag is not cleared here, so avoid busy
2230 * loop by claiming nothing changed. */
2231 sm->changed = FALSE;
2233 sm->GTimeoutCtr = 0;
2237 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
2239 u8 rsc[WPA_KEY_RSC_LEN];
2240 struct wpa_group *gsm = sm->group;
2241 u8 *kde, *pos, hdr[2];
2243 u8 *gtk, dummy_gtk[32];
2245 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
2248 if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
2249 /* No point in sending the EAPOL-Key - we will disconnect
2250 * immediately following this. */
2254 if (sm->wpa == WPA_VERSION_WPA)
2255 sm->PInitAKeys = FALSE;
2256 sm->TimeoutEvt = FALSE;
2257 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
2258 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2259 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
2260 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2261 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2262 "sending 1/2 msg of Group Key Handshake");
2264 gtk = gsm->GTK[gsm->GN - 1];
2265 if (sm->wpa_auth->conf.disable_gtk) {
2267 * Provide unique random GTK to each STA to prevent use
2268 * of GTK in the BSS.
2270 if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
2274 if (sm->wpa == WPA_VERSION_WPA2) {
2275 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
2276 ieee80211w_kde_len(sm);
2277 kde = os_malloc(kde_len);
2282 hdr[0] = gsm->GN & 0x03;
2284 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2286 pos = ieee80211w_kde_add(sm, pos);
2289 pos = kde + gsm->GTK_len;
2292 wpa_send_eapol(sm->wpa_auth, sm,
2293 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
2295 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
2296 rsc, gsm->GNonce, kde, pos - kde, gsm->GN, 1);
2297 if (sm->wpa == WPA_VERSION_WPA2)
2302 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
2304 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
2305 sm->EAPOLKeyReceived = FALSE;
2306 if (sm->GUpdateStationKeys)
2307 sm->group->GKeyDoneStations--;
2308 sm->GUpdateStationKeys = FALSE;
2309 sm->GTimeoutCtr = 0;
2310 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
2311 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2312 "group key handshake completed (%s)",
2313 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2318 SM_STATE(WPA_PTK_GROUP, KEYERROR)
2320 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
2321 if (sm->GUpdateStationKeys)
2322 sm->group->GKeyDoneStations--;
2323 sm->GUpdateStationKeys = FALSE;
2324 sm->Disconnect = TRUE;
2328 SM_STEP(WPA_PTK_GROUP)
2330 if (sm->Init || sm->PtkGroupInit) {
2331 SM_ENTER(WPA_PTK_GROUP, IDLE);
2332 sm->PtkGroupInit = FALSE;
2333 } else switch (sm->wpa_ptk_group_state) {
2334 case WPA_PTK_GROUP_IDLE:
2335 if (sm->GUpdateStationKeys ||
2336 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
2337 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2339 case WPA_PTK_GROUP_REKEYNEGOTIATING:
2340 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2341 !sm->EAPOLKeyPairwise && sm->MICVerified)
2342 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
2343 else if (sm->GTimeoutCtr >
2344 (int) dot11RSNAConfigGroupUpdateCount)
2345 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
2346 else if (sm->TimeoutEvt)
2347 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2349 case WPA_PTK_GROUP_KEYERROR:
2350 SM_ENTER(WPA_PTK_GROUP, IDLE);
2352 case WPA_PTK_GROUP_REKEYESTABLISHED:
2353 SM_ENTER(WPA_PTK_GROUP, IDLE);
2359 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
2360 struct wpa_group *group)
2364 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
2365 inc_byte_array(group->Counter, WPA_NONCE_LEN);
2366 if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
2367 wpa_auth->addr, group->GNonce,
2368 group->GTK[group->GN - 1], group->GTK_len) < 0)
2370 wpa_hexdump_key(MSG_DEBUG, "GTK",
2371 group->GTK[group->GN - 1], group->GTK_len);
2373 #ifdef CONFIG_IEEE80211W
2374 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
2375 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
2376 inc_byte_array(group->Counter, WPA_NONCE_LEN);
2377 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
2378 wpa_auth->addr, group->GNonce,
2379 group->IGTK[group->GN_igtk - 4],
2382 wpa_hexdump_key(MSG_DEBUG, "IGTK",
2383 group->IGTK[group->GN_igtk - 4], WPA_IGTK_LEN);
2385 #endif /* CONFIG_IEEE80211W */
2391 static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
2392 struct wpa_group *group)
2394 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2395 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
2396 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
2397 group->wpa_group_state = WPA_GROUP_GTK_INIT;
2400 os_memset(group->GTK, 0, sizeof(group->GTK));
2403 #ifdef CONFIG_IEEE80211W
2406 #endif /* CONFIG_IEEE80211W */
2407 /* GTK[GN] = CalcGTK() */
2408 wpa_gtk_update(wpa_auth, group);
2412 static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
2414 if (ctx != NULL && ctx != sm->group)
2417 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
2418 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2419 "Not in PTKINITDONE; skip Group Key update");
2420 sm->GUpdateStationKeys = FALSE;
2423 if (sm->GUpdateStationKeys) {
2425 * This should not really happen, so add a debug log entry.
2426 * Since we clear the GKeyDoneStations before the loop, the
2427 * station needs to be counted here anyway.
2429 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2430 "GUpdateStationKeys was already set when "
2431 "marking station for GTK rekeying");
2434 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
2435 if (sm->is_wnmsleep)
2438 sm->group->GKeyDoneStations++;
2439 sm->GUpdateStationKeys = TRUE;
2447 /* update GTK when exiting WNM-Sleep Mode */
2448 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
2450 if (sm->is_wnmsleep)
2453 wpa_group_update_sta(sm, NULL);
2457 void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
2459 sm->is_wnmsleep = !!flag;
2463 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
2465 struct wpa_group *gsm = sm->group;
2470 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
2473 *pos++ = WNM_SLEEP_SUBELEM_GTK;
2474 *pos++ = 11 + gsm->GTK_len;
2475 /* Key ID in B0-B1 of Key Info */
2476 WPA_PUT_LE16(pos, gsm->GN & 0x03);
2478 *pos++ = gsm->GTK_len;
2479 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
2482 os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
2483 pos += gsm->GTK_len;
2485 wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
2487 wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
2488 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
2494 #ifdef CONFIG_IEEE80211W
2495 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
2497 struct wpa_group *gsm = sm->group;
2502 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
2504 *pos++ = WNM_SLEEP_SUBELEM_IGTK;
2505 *pos++ = 2 + 6 + WPA_IGTK_LEN;
2506 WPA_PUT_LE16(pos, gsm->GN_igtk);
2508 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
2512 os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
2513 pos += WPA_IGTK_LEN;
2515 wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
2517 wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
2518 gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
2522 #endif /* CONFIG_IEEE80211W */
2523 #endif /* CONFIG_WNM */
2526 static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
2527 struct wpa_group *group)
2531 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2532 "SETKEYS (VLAN-ID %d)", group->vlan_id);
2533 group->changed = TRUE;
2534 group->wpa_group_state = WPA_GROUP_SETKEYS;
2535 group->GTKReKey = FALSE;
2537 group->GM = group->GN;
2539 #ifdef CONFIG_IEEE80211W
2540 tmp = group->GM_igtk;
2541 group->GM_igtk = group->GN_igtk;
2542 group->GN_igtk = tmp;
2543 #endif /* CONFIG_IEEE80211W */
2544 /* "GKeyDoneStations = GNoStations" is done in more robust way by
2545 * counting the STAs that are marked with GUpdateStationKeys instead of
2546 * including all STAs that could be in not-yet-completed state. */
2547 wpa_gtk_update(wpa_auth, group);
2549 if (group->GKeyDoneStations) {
2550 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
2551 "GKeyDoneStations=%d when starting new GTK rekey",
2552 group->GKeyDoneStations);
2553 group->GKeyDoneStations = 0;
2555 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
2556 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
2557 group->GKeyDoneStations);
2561 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
2562 struct wpa_group *group)
2566 if (wpa_auth_set_key(wpa_auth, group->vlan_id,
2567 wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
2568 broadcast_ether_addr, group->GN,
2569 group->GTK[group->GN - 1], group->GTK_len) < 0)
2572 #ifdef CONFIG_IEEE80211W
2573 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION &&
2574 wpa_auth_set_key(wpa_auth, group->vlan_id, WPA_ALG_IGTK,
2575 broadcast_ether_addr, group->GN_igtk,
2576 group->IGTK[group->GN_igtk - 4],
2579 #endif /* CONFIG_IEEE80211W */
2585 static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
2586 struct wpa_group *group)
2588 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2589 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
2590 group->changed = TRUE;
2591 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
2593 if (wpa_group_config_group_keys(wpa_auth, group) < 0)
2600 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
2601 struct wpa_group *group)
2604 wpa_group_gtk_init(wpa_auth, group);
2605 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
2606 group->GTKAuthenticator) {
2607 wpa_group_setkeysdone(wpa_auth, group);
2608 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
2610 wpa_group_setkeys(wpa_auth, group);
2611 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
2612 if (group->GKeyDoneStations == 0)
2613 wpa_group_setkeysdone(wpa_auth, group);
2614 else if (group->GTKReKey)
2615 wpa_group_setkeys(wpa_auth, group);
2620 static int wpa_sm_step(struct wpa_state_machine *sm)
2625 if (sm->in_step_loop) {
2626 /* This should not happen, but if it does, make sure we do not
2627 * end up freeing the state machine too early by exiting the
2628 * recursive call. */
2629 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
2633 sm->in_step_loop = 1;
2635 if (sm->pending_deinit)
2638 sm->changed = FALSE;
2639 sm->wpa_auth->group->changed = FALSE;
2641 SM_STEP_RUN(WPA_PTK);
2642 if (sm->pending_deinit)
2644 SM_STEP_RUN(WPA_PTK_GROUP);
2645 if (sm->pending_deinit)
2647 wpa_group_sm_step(sm->wpa_auth, sm->group);
2648 } while (sm->changed || sm->wpa_auth->group->changed);
2649 sm->in_step_loop = 0;
2651 if (sm->pending_deinit) {
2652 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
2653 "machine deinit for " MACSTR, MAC2STR(sm->addr));
2654 wpa_free_sta_sm(sm);
2661 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
2663 struct wpa_state_machine *sm = eloop_ctx;
2668 void wpa_auth_sm_notify(struct wpa_state_machine *sm)
2672 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
2676 void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
2679 struct wpa_group *group;
2681 if (wpa_auth == NULL)
2684 group = wpa_auth->group;
2686 for (i = 0; i < 2; i++) {
2688 group->GM = group->GN;
2690 #ifdef CONFIG_IEEE80211W
2691 tmp = group->GM_igtk;
2692 group->GM_igtk = group->GN_igtk;
2693 group->GN_igtk = tmp;
2694 #endif /* CONFIG_IEEE80211W */
2695 wpa_gtk_update(wpa_auth, group);
2696 wpa_group_config_group_keys(wpa_auth, group);
2701 static const char * wpa_bool_txt(int bool)
2703 return bool ? "TRUE" : "FALSE";
2707 #define RSN_SUITE "%02x-%02x-%02x-%d"
2708 #define RSN_SUITE_ARG(s) \
2709 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2711 int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
2714 char pmkid_txt[PMKID_LEN * 2 + 1];
2715 #ifdef CONFIG_RSN_PREAUTH
2716 const int preauth = 1;
2717 #else /* CONFIG_RSN_PREAUTH */
2718 const int preauth = 0;
2719 #endif /* CONFIG_RSN_PREAUTH */
2721 if (wpa_auth == NULL)
2724 ret = os_snprintf(buf + len, buflen - len,
2725 "dot11RSNAOptionImplemented=TRUE\n"
2726 "dot11RSNAPreauthenticationImplemented=%s\n"
2727 "dot11RSNAEnabled=%s\n"
2728 "dot11RSNAPreauthenticationEnabled=%s\n",
2729 wpa_bool_txt(preauth),
2730 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
2731 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
2732 if (ret < 0 || (size_t) ret >= buflen - len)
2736 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2737 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
2740 buf + len, buflen - len,
2741 "dot11RSNAConfigVersion=%u\n"
2742 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
2743 /* FIX: dot11RSNAConfigGroupCipher */
2744 /* FIX: dot11RSNAConfigGroupRekeyMethod */
2745 /* FIX: dot11RSNAConfigGroupRekeyTime */
2746 /* FIX: dot11RSNAConfigGroupRekeyPackets */
2747 "dot11RSNAConfigGroupRekeyStrict=%u\n"
2748 "dot11RSNAConfigGroupUpdateCount=%u\n"
2749 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
2750 "dot11RSNAConfigGroupCipherSize=%u\n"
2751 "dot11RSNAConfigPMKLifetime=%u\n"
2752 "dot11RSNAConfigPMKReauthThreshold=%u\n"
2753 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
2754 "dot11RSNAConfigSATimeout=%u\n"
2755 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2756 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2757 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2758 "dot11RSNAPMKIDUsed=%s\n"
2759 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2760 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2761 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2762 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
2763 "dot11RSNA4WayHandshakeFailures=%u\n"
2764 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
2766 !!wpa_auth->conf.wpa_strict_rekey,
2767 dot11RSNAConfigGroupUpdateCount,
2768 dot11RSNAConfigPairwiseUpdateCount,
2769 wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
2770 dot11RSNAConfigPMKLifetime,
2771 dot11RSNAConfigPMKReauthThreshold,
2772 dot11RSNAConfigSATimeout,
2773 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
2774 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
2775 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
2777 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
2778 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
2779 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
2780 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
2781 wpa_auth->dot11RSNA4WayHandshakeFailures);
2782 if (ret < 0 || (size_t) ret >= buflen - len)
2786 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
2787 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
2790 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
2791 wpa_auth->group->wpa_group_state);
2792 if (ret < 0 || (size_t) ret >= buflen - len)
2800 int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
2808 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
2810 /* dot11RSNAStatsEntry */
2812 pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
2813 WPA_PROTO_RSN : WPA_PROTO_WPA,
2819 buf + len, buflen - len,
2820 /* TODO: dot11RSNAStatsIndex */
2821 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
2822 "dot11RSNAStatsVersion=1\n"
2823 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
2824 /* TODO: dot11RSNAStatsTKIPICVErrors */
2825 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
2826 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
2827 /* TODO: dot11RSNAStatsCCMPReplays */
2828 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
2829 /* TODO: dot11RSNAStatsTKIPReplays */,
2831 RSN_SUITE_ARG(pairwise),
2832 sm->dot11RSNAStatsTKIPLocalMICFailures,
2833 sm->dot11RSNAStatsTKIPRemoteMICFailures);
2834 if (ret < 0 || (size_t) ret >= buflen - len)
2839 ret = os_snprintf(buf + len, buflen - len,
2840 "hostapdWPAPTKState=%d\n"
2841 "hostapdWPAPTKGroupState=%d\n",
2843 sm->wpa_ptk_group_state);
2844 if (ret < 0 || (size_t) ret >= buflen - len)
2852 void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
2855 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
2859 int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
2861 return sm && sm->pairwise_set;
2865 int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
2867 return sm->pairwise;
2871 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
2875 return sm->wpa_key_mgmt;
2879 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
2887 int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine *sm)
2889 if (!sm || !wpa_key_mgmt_ft(sm->wpa_key_mgmt))
2891 return sm->tk_already_set;
2895 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
2896 struct rsn_pmksa_cache_entry *entry)
2898 if (sm == NULL || sm->pmksa != entry)
2905 struct rsn_pmksa_cache_entry *
2906 wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
2908 return sm ? sm->pmksa : NULL;
2912 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
2915 sm->dot11RSNAStatsTKIPLocalMICFailures++;
2919 const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
2921 if (wpa_auth == NULL)
2923 *len = wpa_auth->wpa_ie_len;
2924 return wpa_auth->wpa_ie;
2928 int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
2929 int session_timeout, struct eapol_state_machine *eapol)
2931 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
2932 sm->wpa_auth->conf.disable_pmksa_caching)
2935 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
2936 sm->wpa_auth->addr, sm->addr, session_timeout,
2937 eapol, sm->wpa_key_mgmt))
2944 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
2945 const u8 *pmk, size_t len, const u8 *sta_addr,
2946 int session_timeout,
2947 struct eapol_state_machine *eapol)
2949 if (wpa_auth == NULL)
2952 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, wpa_auth->addr,
2953 sta_addr, session_timeout, eapol,
2954 WPA_KEY_MGMT_IEEE8021X))
2961 static struct wpa_group *
2962 wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
2964 struct wpa_group *group;
2966 if (wpa_auth == NULL || wpa_auth->group == NULL)
2969 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
2971 group = wpa_group_init(wpa_auth, vlan_id, 0);
2975 group->next = wpa_auth->group->next;
2976 wpa_auth->group->next = group;
2982 int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
2984 struct wpa_group *group;
2986 if (sm == NULL || sm->wpa_auth == NULL)
2989 group = sm->wpa_auth->group;
2991 if (group->vlan_id == vlan_id)
2993 group = group->next;
2996 if (group == NULL) {
2997 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
3002 if (sm->group == group)
3005 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
3006 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
3013 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
3014 struct wpa_state_machine *sm, int ack)
3016 if (wpa_auth == NULL || sm == NULL)
3018 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
3019 " ack=%d", MAC2STR(sm->addr), ack);
3020 if (sm->pending_1_of_4_timeout && ack) {
3022 * Some deployed supplicant implementations update their SNonce
3023 * for each EAPOL-Key 2/4 message even within the same 4-way
3024 * handshake and then fail to use the first SNonce when
3025 * deriving the PTK. This results in unsuccessful 4-way
3026 * handshake whenever the relatively short initial timeout is
3027 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
3028 * around this by increasing the timeout now that we know that
3029 * the station has received the frame.
3031 int timeout_ms = eapol_key_timeout_subseq;
3032 wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
3033 "timeout by %u ms because of acknowledged frame",
3035 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
3036 eloop_register_timeout(timeout_ms / 1000,
3037 (timeout_ms % 1000) * 1000,
3038 wpa_send_eapol_timeout, wpa_auth, sm);
3043 int wpa_auth_uses_sae(struct wpa_state_machine *sm)
3047 return wpa_key_mgmt_sae(sm->wpa_key_mgmt);