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 SM_STATE(WPA_PTK, INITPMK)
1628 u8 msk[2 * PMK_LEN];
1629 size_t len = 2 * PMK_LEN;
1631 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1632 #ifdef CONFIG_IEEE80211R
1634 #endif /* CONFIG_IEEE80211R */
1636 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1637 os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
1638 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1639 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1640 "(len=%lu)", (unsigned long) len);
1641 os_memcpy(sm->PMK, msk, PMK_LEN);
1642 #ifdef CONFIG_IEEE80211R
1643 if (len >= 2 * PMK_LEN) {
1644 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1645 sm->xxkey_len = PMK_LEN;
1647 #endif /* CONFIG_IEEE80211R */
1649 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
1652 sm->req_replay_counter_used = 0;
1653 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1654 * will break reauthentication since EAPOL state machines may not be
1655 * get into AUTHENTICATING state that clears keyRun before WPA state
1656 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1657 * state and takes PMK from the previously used AAA Key. This will
1658 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1659 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1660 * be good workaround for this issue. */
1661 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1665 SM_STATE(WPA_PTK, INITPSK)
1668 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
1669 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL);
1671 os_memcpy(sm->PMK, psk, PMK_LEN);
1672 #ifdef CONFIG_IEEE80211R
1673 os_memcpy(sm->xxkey, psk, PMK_LEN);
1674 sm->xxkey_len = PMK_LEN;
1675 #endif /* CONFIG_IEEE80211R */
1677 sm->req_replay_counter_used = 0;
1681 SM_STATE(WPA_PTK, PTKSTART)
1683 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1684 size_t pmkid_len = 0;
1686 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
1687 sm->PTKRequest = FALSE;
1688 sm->TimeoutEvt = FALSE;
1691 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1692 /* No point in sending the EAPOL-Key - we will disconnect
1693 * immediately following this. */
1697 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1698 "sending 1/4 msg of 4-Way Handshake");
1700 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1701 * one possible PSK for this STA.
1703 if (sm->wpa == WPA_VERSION_WPA2 &&
1704 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt)) {
1706 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
1707 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
1708 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
1709 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
1711 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
1712 sm->pmksa->pmkid, PMKID_LEN);
1715 * Calculate PMKID since no PMKSA cache entry was
1716 * available with pre-calculated PMKID.
1718 rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
1719 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
1720 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1723 wpa_send_eapol(sm->wpa_auth, sm,
1724 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
1725 sm->ANonce, pmkid, pmkid_len, 0, 0);
1729 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk,
1730 struct wpa_ptk *ptk)
1732 size_t ptk_len = sm->pairwise != WPA_CIPHER_TKIP ? 48 : 64;
1733 #ifdef CONFIG_IEEE80211R
1734 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
1735 return wpa_auth_derive_ptk_ft(sm, pmk, ptk, ptk_len);
1736 #endif /* CONFIG_IEEE80211R */
1738 wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
1739 sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce,
1740 (u8 *) ptk, ptk_len,
1741 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1747 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
1751 const u8 *pmk = NULL;
1753 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
1754 sm->EAPOLKeyReceived = FALSE;
1755 sm->update_snonce = FALSE;
1757 /* WPA with IEEE 802.1X: use the derived PMK from EAP
1758 * WPA-PSK: iterate through possible PSKs and select the one matching
1761 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1762 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, pmk);
1768 wpa_derive_ptk(sm, pmk, &PTK);
1770 if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key,
1771 sm->last_rx_eapol_key_len) == 0) {
1776 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
1781 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1782 "invalid MIC in msg 2/4 of 4-Way Handshake");
1786 #ifdef CONFIG_IEEE80211R
1787 if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
1789 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
1790 * with the value we derived.
1792 if (os_memcmp(sm->sup_pmk_r1_name, sm->pmk_r1_name,
1793 WPA_PMK_NAME_LEN) != 0) {
1794 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1795 "PMKR1Name mismatch in FT 4-way "
1797 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
1799 sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
1800 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1801 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1805 #endif /* CONFIG_IEEE80211R */
1807 sm->pending_1_of_4_timeout = 0;
1808 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
1810 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1811 /* PSK may have changed from the previous choice, so update
1812 * state machine data based on whatever PSK was selected here.
1814 os_memcpy(sm->PMK, pmk, PMK_LEN);
1817 sm->MICVerified = TRUE;
1819 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
1820 sm->PTK_valid = TRUE;
1824 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
1826 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
1831 #ifdef CONFIG_IEEE80211W
1833 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1835 if (sm->mgmt_frame_prot) {
1836 return 2 + RSN_SELECTOR_LEN + sizeof(struct wpa_igtk_kde);
1843 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1845 struct wpa_igtk_kde igtk;
1846 struct wpa_group *gsm = sm->group;
1848 if (!sm->mgmt_frame_prot)
1851 igtk.keyid[0] = gsm->GN_igtk;
1853 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
1854 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, igtk.pn) < 0)
1855 os_memset(igtk.pn, 0, sizeof(igtk.pn));
1856 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
1857 if (sm->wpa_auth->conf.disable_gtk) {
1859 * Provide unique random IGTK to each STA to prevent use of
1862 if (random_get_bytes(igtk.igtk, WPA_IGTK_LEN) < 0)
1865 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
1866 (const u8 *) &igtk, sizeof(igtk), NULL, 0);
1871 #else /* CONFIG_IEEE80211W */
1873 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1879 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1884 #endif /* CONFIG_IEEE80211W */
1887 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
1889 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
1890 size_t gtk_len, kde_len;
1891 struct wpa_group *gsm = sm->group;
1893 int wpa_ie_len, secure, keyidx, encr = 0;
1895 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
1896 sm->TimeoutEvt = FALSE;
1899 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1900 /* No point in sending the EAPOL-Key - we will disconnect
1901 * immediately following this. */
1905 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
1906 GTK[GN], IGTK, [FTIE], [TIE * 2])
1908 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
1909 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
1910 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
1911 wpa_ie = sm->wpa_auth->wpa_ie;
1912 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
1913 if (sm->wpa == WPA_VERSION_WPA &&
1914 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
1915 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
1916 /* WPA-only STA, remove RSN IE */
1917 wpa_ie = wpa_ie + wpa_ie[1] + 2;
1918 wpa_ie_len = wpa_ie[1] + 2;
1920 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1921 "sending 3/4 msg of 4-Way Handshake");
1922 if (sm->wpa == WPA_VERSION_WPA2) {
1923 /* WPA2 send GTK in the 4-way handshake */
1925 gtk = gsm->GTK[gsm->GN - 1];
1926 gtk_len = gsm->GTK_len;
1927 if (sm->wpa_auth->conf.disable_gtk) {
1929 * Provide unique random GTK to each STA to prevent use
1930 * of GTK in the BSS.
1932 if (random_get_bytes(dummy_gtk, gtk_len) < 0)
1940 /* WPA does not include GTK in msg 3/4 */
1946 if (sm->rx_eapol_key_secure) {
1948 * It looks like Windows 7 supplicant tries to use
1949 * Secure bit in msg 2/4 after having reported Michael
1950 * MIC failure and it then rejects the 4-way handshake
1951 * if msg 3/4 does not set Secure bit. Work around this
1952 * by setting the Secure bit here even in the case of
1953 * WPA if the supplicant used it first.
1955 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1956 "STA used Secure bit in WPA msg 2/4 - "
1957 "set Secure for 3/4 as workaround");
1962 kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
1964 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
1965 #ifdef CONFIG_IEEE80211R
1966 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
1967 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
1968 kde_len += 300; /* FTIE + 2 * TIE */
1970 #endif /* CONFIG_IEEE80211R */
1971 kde = os_malloc(kde_len);
1976 os_memcpy(pos, wpa_ie, wpa_ie_len);
1978 #ifdef CONFIG_IEEE80211R
1979 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
1980 int res = wpa_insert_pmkid(kde, pos - kde, sm->pmk_r1_name);
1982 wpa_printf(MSG_ERROR, "FT: Failed to insert "
1983 "PMKR1Name into RSN IE in EAPOL-Key data");
1989 #endif /* CONFIG_IEEE80211R */
1992 hdr[0] = keyidx & 0x03;
1994 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
1997 pos = ieee80211w_kde_add(sm, pos);
1999 #ifdef CONFIG_IEEE80211R
2000 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2002 struct wpa_auth_config *conf;
2004 conf = &sm->wpa_auth->conf;
2005 res = wpa_write_ftie(conf, conf->r0_key_holder,
2006 conf->r0_key_holder_len,
2007 NULL, NULL, pos, kde + kde_len - pos,
2010 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
2011 "into EAPOL-Key Key Data");
2017 /* TIE[ReassociationDeadline] (TU) */
2018 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2020 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
2021 WPA_PUT_LE32(pos, conf->reassociation_deadline);
2024 /* TIE[KeyLifetime] (seconds) */
2025 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2027 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
2028 WPA_PUT_LE32(pos, conf->r0_key_lifetime * 60);
2031 #endif /* CONFIG_IEEE80211R */
2033 wpa_send_eapol(sm->wpa_auth, sm,
2034 (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
2035 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
2036 WPA_KEY_INFO_KEY_TYPE,
2037 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
2042 SM_STATE(WPA_PTK, PTKINITDONE)
2044 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
2045 sm->EAPOLKeyReceived = FALSE;
2047 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
2048 int klen = wpa_cipher_key_len(sm->pairwise);
2049 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
2050 sm->PTK.tk1, klen)) {
2051 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
2054 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
2055 sm->pairwise_set = TRUE;
2057 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
2058 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
2059 eloop_register_timeout(sm->wpa_auth->conf.
2060 wpa_ptk_rekey, 0, wpa_rekey_ptk,
2064 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2065 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2066 WPA_EAPOL_authorized, 1);
2070 if (0 /* IBSS == TRUE */) {
2072 if (sm->keycount == 2) {
2073 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2074 WPA_EAPOL_portValid, 1);
2077 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
2080 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
2081 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
2082 if (sm->wpa == WPA_VERSION_WPA)
2083 sm->PInitAKeys = TRUE;
2086 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2087 "pairwise key handshake completed (%s)",
2088 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2090 #ifdef CONFIG_IEEE80211R
2091 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
2092 #endif /* CONFIG_IEEE80211R */
2098 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2101 SM_ENTER(WPA_PTK, INITIALIZE);
2102 else if (sm->Disconnect
2103 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
2104 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
2105 "WPA_PTK: sm->Disconnect");
2106 SM_ENTER(WPA_PTK, DISCONNECT);
2108 else if (sm->DeauthenticationRequest)
2109 SM_ENTER(WPA_PTK, DISCONNECTED);
2110 else if (sm->AuthenticationRequest)
2111 SM_ENTER(WPA_PTK, AUTHENTICATION);
2112 else if (sm->ReAuthenticationRequest)
2113 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2114 else if (sm->PTKRequest)
2115 SM_ENTER(WPA_PTK, PTKSTART);
2116 else switch (sm->wpa_ptk_state) {
2117 case WPA_PTK_INITIALIZE:
2119 case WPA_PTK_DISCONNECT:
2120 SM_ENTER(WPA_PTK, DISCONNECTED);
2122 case WPA_PTK_DISCONNECTED:
2123 SM_ENTER(WPA_PTK, INITIALIZE);
2125 case WPA_PTK_AUTHENTICATION:
2126 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2128 case WPA_PTK_AUTHENTICATION2:
2129 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
2130 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2131 WPA_EAPOL_keyRun) > 0)
2132 SM_ENTER(WPA_PTK, INITPMK);
2133 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
2134 /* FIX: && 802.1X::keyRun */)
2135 SM_ENTER(WPA_PTK, INITPSK);
2137 case WPA_PTK_INITPMK:
2138 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2139 WPA_EAPOL_keyAvailable) > 0)
2140 SM_ENTER(WPA_PTK, PTKSTART);
2142 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2143 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2144 "INITPMK - keyAvailable = false");
2145 SM_ENTER(WPA_PTK, DISCONNECT);
2148 case WPA_PTK_INITPSK:
2149 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL))
2150 SM_ENTER(WPA_PTK, PTKSTART);
2152 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2153 "no PSK configured for the STA");
2154 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2155 SM_ENTER(WPA_PTK, DISCONNECT);
2158 case WPA_PTK_PTKSTART:
2159 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2160 sm->EAPOLKeyPairwise)
2161 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2162 else if (sm->TimeoutCtr >
2163 (int) dot11RSNAConfigPairwiseUpdateCount) {
2164 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2165 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2166 "PTKSTART: Retry limit %d reached",
2167 dot11RSNAConfigPairwiseUpdateCount);
2168 SM_ENTER(WPA_PTK, DISCONNECT);
2169 } else if (sm->TimeoutEvt)
2170 SM_ENTER(WPA_PTK, PTKSTART);
2172 case WPA_PTK_PTKCALCNEGOTIATING:
2173 if (sm->MICVerified)
2174 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
2175 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2176 sm->EAPOLKeyPairwise)
2177 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2178 else if (sm->TimeoutEvt)
2179 SM_ENTER(WPA_PTK, PTKSTART);
2181 case WPA_PTK_PTKCALCNEGOTIATING2:
2182 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2184 case WPA_PTK_PTKINITNEGOTIATING:
2185 if (sm->update_snonce)
2186 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2187 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2188 sm->EAPOLKeyPairwise && sm->MICVerified)
2189 SM_ENTER(WPA_PTK, PTKINITDONE);
2190 else if (sm->TimeoutCtr >
2191 (int) dot11RSNAConfigPairwiseUpdateCount) {
2192 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2193 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2194 "PTKINITNEGOTIATING: Retry limit %d "
2196 dot11RSNAConfigPairwiseUpdateCount);
2197 SM_ENTER(WPA_PTK, DISCONNECT);
2198 } else if (sm->TimeoutEvt)
2199 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2201 case WPA_PTK_PTKINITDONE:
2207 SM_STATE(WPA_PTK_GROUP, IDLE)
2209 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
2211 /* Init flag is not cleared here, so avoid busy
2212 * loop by claiming nothing changed. */
2213 sm->changed = FALSE;
2215 sm->GTimeoutCtr = 0;
2219 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
2221 u8 rsc[WPA_KEY_RSC_LEN];
2222 struct wpa_group *gsm = sm->group;
2223 u8 *kde, *pos, hdr[2];
2225 u8 *gtk, dummy_gtk[32];
2227 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
2230 if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
2231 /* No point in sending the EAPOL-Key - we will disconnect
2232 * immediately following this. */
2236 if (sm->wpa == WPA_VERSION_WPA)
2237 sm->PInitAKeys = FALSE;
2238 sm->TimeoutEvt = FALSE;
2239 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
2240 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2241 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
2242 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2243 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2244 "sending 1/2 msg of Group Key Handshake");
2246 gtk = gsm->GTK[gsm->GN - 1];
2247 if (sm->wpa_auth->conf.disable_gtk) {
2249 * Provide unique random GTK to each STA to prevent use
2250 * of GTK in the BSS.
2252 if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
2256 if (sm->wpa == WPA_VERSION_WPA2) {
2257 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
2258 ieee80211w_kde_len(sm);
2259 kde = os_malloc(kde_len);
2264 hdr[0] = gsm->GN & 0x03;
2266 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2268 pos = ieee80211w_kde_add(sm, pos);
2271 pos = kde + gsm->GTK_len;
2274 wpa_send_eapol(sm->wpa_auth, sm,
2275 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
2277 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
2278 rsc, gsm->GNonce, kde, pos - kde, gsm->GN, 1);
2279 if (sm->wpa == WPA_VERSION_WPA2)
2284 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
2286 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
2287 sm->EAPOLKeyReceived = FALSE;
2288 if (sm->GUpdateStationKeys)
2289 sm->group->GKeyDoneStations--;
2290 sm->GUpdateStationKeys = FALSE;
2291 sm->GTimeoutCtr = 0;
2292 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
2293 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2294 "group key handshake completed (%s)",
2295 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2300 SM_STATE(WPA_PTK_GROUP, KEYERROR)
2302 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
2303 if (sm->GUpdateStationKeys)
2304 sm->group->GKeyDoneStations--;
2305 sm->GUpdateStationKeys = FALSE;
2306 sm->Disconnect = TRUE;
2310 SM_STEP(WPA_PTK_GROUP)
2312 if (sm->Init || sm->PtkGroupInit) {
2313 SM_ENTER(WPA_PTK_GROUP, IDLE);
2314 sm->PtkGroupInit = FALSE;
2315 } else switch (sm->wpa_ptk_group_state) {
2316 case WPA_PTK_GROUP_IDLE:
2317 if (sm->GUpdateStationKeys ||
2318 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
2319 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2321 case WPA_PTK_GROUP_REKEYNEGOTIATING:
2322 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2323 !sm->EAPOLKeyPairwise && sm->MICVerified)
2324 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
2325 else if (sm->GTimeoutCtr >
2326 (int) dot11RSNAConfigGroupUpdateCount)
2327 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
2328 else if (sm->TimeoutEvt)
2329 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2331 case WPA_PTK_GROUP_KEYERROR:
2332 SM_ENTER(WPA_PTK_GROUP, IDLE);
2334 case WPA_PTK_GROUP_REKEYESTABLISHED:
2335 SM_ENTER(WPA_PTK_GROUP, IDLE);
2341 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
2342 struct wpa_group *group)
2346 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
2347 inc_byte_array(group->Counter, WPA_NONCE_LEN);
2348 if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
2349 wpa_auth->addr, group->GNonce,
2350 group->GTK[group->GN - 1], group->GTK_len) < 0)
2352 wpa_hexdump_key(MSG_DEBUG, "GTK",
2353 group->GTK[group->GN - 1], group->GTK_len);
2355 #ifdef CONFIG_IEEE80211W
2356 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
2357 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
2358 inc_byte_array(group->Counter, WPA_NONCE_LEN);
2359 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
2360 wpa_auth->addr, group->GNonce,
2361 group->IGTK[group->GN_igtk - 4],
2364 wpa_hexdump_key(MSG_DEBUG, "IGTK",
2365 group->IGTK[group->GN_igtk - 4], WPA_IGTK_LEN);
2367 #endif /* CONFIG_IEEE80211W */
2373 static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
2374 struct wpa_group *group)
2376 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2377 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
2378 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
2379 group->wpa_group_state = WPA_GROUP_GTK_INIT;
2382 os_memset(group->GTK, 0, sizeof(group->GTK));
2385 #ifdef CONFIG_IEEE80211W
2388 #endif /* CONFIG_IEEE80211W */
2389 /* GTK[GN] = CalcGTK() */
2390 wpa_gtk_update(wpa_auth, group);
2394 static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
2396 if (ctx != NULL && ctx != sm->group)
2399 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
2400 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2401 "Not in PTKINITDONE; skip Group Key update");
2402 sm->GUpdateStationKeys = FALSE;
2405 if (sm->GUpdateStationKeys) {
2407 * This should not really happen, so add a debug log entry.
2408 * Since we clear the GKeyDoneStations before the loop, the
2409 * station needs to be counted here anyway.
2411 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2412 "GUpdateStationKeys was already set when "
2413 "marking station for GTK rekeying");
2416 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
2417 if (sm->is_wnmsleep)
2420 sm->group->GKeyDoneStations++;
2421 sm->GUpdateStationKeys = TRUE;
2429 /* update GTK when exiting WNM-Sleep Mode */
2430 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
2432 if (sm->is_wnmsleep)
2435 wpa_group_update_sta(sm, NULL);
2439 void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
2441 sm->is_wnmsleep = !!flag;
2445 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
2447 struct wpa_group *gsm = sm->group;
2452 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
2455 *pos++ = WNM_SLEEP_SUBELEM_GTK;
2456 *pos++ = 11 + gsm->GTK_len;
2457 /* Key ID in B0-B1 of Key Info */
2458 WPA_PUT_LE16(pos, gsm->GN & 0x03);
2460 *pos++ = gsm->GTK_len;
2461 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
2464 os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
2465 pos += gsm->GTK_len;
2467 wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
2469 wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
2470 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
2476 #ifdef CONFIG_IEEE80211W
2477 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
2479 struct wpa_group *gsm = sm->group;
2484 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
2486 *pos++ = WNM_SLEEP_SUBELEM_IGTK;
2487 *pos++ = 2 + 6 + WPA_IGTK_LEN;
2488 WPA_PUT_LE16(pos, gsm->GN_igtk);
2490 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
2494 os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
2495 pos += WPA_IGTK_LEN;
2497 wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
2499 wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
2500 gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
2504 #endif /* CONFIG_IEEE80211W */
2505 #endif /* CONFIG_WNM */
2508 static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
2509 struct wpa_group *group)
2513 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2514 "SETKEYS (VLAN-ID %d)", group->vlan_id);
2515 group->changed = TRUE;
2516 group->wpa_group_state = WPA_GROUP_SETKEYS;
2517 group->GTKReKey = FALSE;
2519 group->GM = group->GN;
2521 #ifdef CONFIG_IEEE80211W
2522 tmp = group->GM_igtk;
2523 group->GM_igtk = group->GN_igtk;
2524 group->GN_igtk = tmp;
2525 #endif /* CONFIG_IEEE80211W */
2526 /* "GKeyDoneStations = GNoStations" is done in more robust way by
2527 * counting the STAs that are marked with GUpdateStationKeys instead of
2528 * including all STAs that could be in not-yet-completed state. */
2529 wpa_gtk_update(wpa_auth, group);
2531 if (group->GKeyDoneStations) {
2532 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
2533 "GKeyDoneStations=%d when starting new GTK rekey",
2534 group->GKeyDoneStations);
2535 group->GKeyDoneStations = 0;
2537 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
2538 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
2539 group->GKeyDoneStations);
2543 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
2544 struct wpa_group *group)
2548 if (wpa_auth_set_key(wpa_auth, group->vlan_id,
2549 wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
2550 broadcast_ether_addr, group->GN,
2551 group->GTK[group->GN - 1], group->GTK_len) < 0)
2554 #ifdef CONFIG_IEEE80211W
2555 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION &&
2556 wpa_auth_set_key(wpa_auth, group->vlan_id, WPA_ALG_IGTK,
2557 broadcast_ether_addr, group->GN_igtk,
2558 group->IGTK[group->GN_igtk - 4],
2561 #endif /* CONFIG_IEEE80211W */
2567 static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
2568 struct wpa_group *group)
2570 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2571 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
2572 group->changed = TRUE;
2573 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
2575 if (wpa_group_config_group_keys(wpa_auth, group) < 0)
2582 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
2583 struct wpa_group *group)
2586 wpa_group_gtk_init(wpa_auth, group);
2587 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
2588 group->GTKAuthenticator) {
2589 wpa_group_setkeysdone(wpa_auth, group);
2590 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
2592 wpa_group_setkeys(wpa_auth, group);
2593 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
2594 if (group->GKeyDoneStations == 0)
2595 wpa_group_setkeysdone(wpa_auth, group);
2596 else if (group->GTKReKey)
2597 wpa_group_setkeys(wpa_auth, group);
2602 static int wpa_sm_step(struct wpa_state_machine *sm)
2607 if (sm->in_step_loop) {
2608 /* This should not happen, but if it does, make sure we do not
2609 * end up freeing the state machine too early by exiting the
2610 * recursive call. */
2611 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
2615 sm->in_step_loop = 1;
2617 if (sm->pending_deinit)
2620 sm->changed = FALSE;
2621 sm->wpa_auth->group->changed = FALSE;
2623 SM_STEP_RUN(WPA_PTK);
2624 if (sm->pending_deinit)
2626 SM_STEP_RUN(WPA_PTK_GROUP);
2627 if (sm->pending_deinit)
2629 wpa_group_sm_step(sm->wpa_auth, sm->group);
2630 } while (sm->changed || sm->wpa_auth->group->changed);
2631 sm->in_step_loop = 0;
2633 if (sm->pending_deinit) {
2634 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
2635 "machine deinit for " MACSTR, MAC2STR(sm->addr));
2636 wpa_free_sta_sm(sm);
2643 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
2645 struct wpa_state_machine *sm = eloop_ctx;
2650 void wpa_auth_sm_notify(struct wpa_state_machine *sm)
2654 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
2658 void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
2661 struct wpa_group *group;
2663 if (wpa_auth == NULL)
2666 group = wpa_auth->group;
2668 for (i = 0; i < 2; i++) {
2670 group->GM = group->GN;
2672 #ifdef CONFIG_IEEE80211W
2673 tmp = group->GM_igtk;
2674 group->GM_igtk = group->GN_igtk;
2675 group->GN_igtk = tmp;
2676 #endif /* CONFIG_IEEE80211W */
2677 wpa_gtk_update(wpa_auth, group);
2678 wpa_group_config_group_keys(wpa_auth, group);
2683 static const char * wpa_bool_txt(int bool)
2685 return bool ? "TRUE" : "FALSE";
2689 #define RSN_SUITE "%02x-%02x-%02x-%d"
2690 #define RSN_SUITE_ARG(s) \
2691 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2693 int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
2696 char pmkid_txt[PMKID_LEN * 2 + 1];
2697 #ifdef CONFIG_RSN_PREAUTH
2698 const int preauth = 1;
2699 #else /* CONFIG_RSN_PREAUTH */
2700 const int preauth = 0;
2701 #endif /* CONFIG_RSN_PREAUTH */
2703 if (wpa_auth == NULL)
2706 ret = os_snprintf(buf + len, buflen - len,
2707 "dot11RSNAOptionImplemented=TRUE\n"
2708 "dot11RSNAPreauthenticationImplemented=%s\n"
2709 "dot11RSNAEnabled=%s\n"
2710 "dot11RSNAPreauthenticationEnabled=%s\n",
2711 wpa_bool_txt(preauth),
2712 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
2713 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
2714 if (ret < 0 || (size_t) ret >= buflen - len)
2718 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2719 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
2722 buf + len, buflen - len,
2723 "dot11RSNAConfigVersion=%u\n"
2724 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
2725 /* FIX: dot11RSNAConfigGroupCipher */
2726 /* FIX: dot11RSNAConfigGroupRekeyMethod */
2727 /* FIX: dot11RSNAConfigGroupRekeyTime */
2728 /* FIX: dot11RSNAConfigGroupRekeyPackets */
2729 "dot11RSNAConfigGroupRekeyStrict=%u\n"
2730 "dot11RSNAConfigGroupUpdateCount=%u\n"
2731 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
2732 "dot11RSNAConfigGroupCipherSize=%u\n"
2733 "dot11RSNAConfigPMKLifetime=%u\n"
2734 "dot11RSNAConfigPMKReauthThreshold=%u\n"
2735 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
2736 "dot11RSNAConfigSATimeout=%u\n"
2737 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2738 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2739 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2740 "dot11RSNAPMKIDUsed=%s\n"
2741 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2742 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2743 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2744 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
2745 "dot11RSNA4WayHandshakeFailures=%u\n"
2746 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
2748 !!wpa_auth->conf.wpa_strict_rekey,
2749 dot11RSNAConfigGroupUpdateCount,
2750 dot11RSNAConfigPairwiseUpdateCount,
2751 wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
2752 dot11RSNAConfigPMKLifetime,
2753 dot11RSNAConfigPMKReauthThreshold,
2754 dot11RSNAConfigSATimeout,
2755 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
2756 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
2757 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
2759 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
2760 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
2761 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
2762 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
2763 wpa_auth->dot11RSNA4WayHandshakeFailures);
2764 if (ret < 0 || (size_t) ret >= buflen - len)
2768 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
2769 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
2772 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
2773 wpa_auth->group->wpa_group_state);
2774 if (ret < 0 || (size_t) ret >= buflen - len)
2782 int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
2790 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
2792 /* dot11RSNAStatsEntry */
2794 pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
2795 WPA_PROTO_RSN : WPA_PROTO_WPA,
2801 buf + len, buflen - len,
2802 /* TODO: dot11RSNAStatsIndex */
2803 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
2804 "dot11RSNAStatsVersion=1\n"
2805 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
2806 /* TODO: dot11RSNAStatsTKIPICVErrors */
2807 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
2808 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
2809 /* TODO: dot11RSNAStatsCCMPReplays */
2810 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
2811 /* TODO: dot11RSNAStatsTKIPReplays */,
2813 RSN_SUITE_ARG(pairwise),
2814 sm->dot11RSNAStatsTKIPLocalMICFailures,
2815 sm->dot11RSNAStatsTKIPRemoteMICFailures);
2816 if (ret < 0 || (size_t) ret >= buflen - len)
2821 ret = os_snprintf(buf + len, buflen - len,
2822 "hostapdWPAPTKState=%d\n"
2823 "hostapdWPAPTKGroupState=%d\n",
2825 sm->wpa_ptk_group_state);
2826 if (ret < 0 || (size_t) ret >= buflen - len)
2834 void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
2837 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
2841 int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
2843 return sm && sm->pairwise_set;
2847 int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
2849 return sm->pairwise;
2853 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
2857 return sm->wpa_key_mgmt;
2861 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
2869 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
2870 struct rsn_pmksa_cache_entry *entry)
2872 if (sm == NULL || sm->pmksa != entry)
2879 struct rsn_pmksa_cache_entry *
2880 wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
2882 return sm ? sm->pmksa : NULL;
2886 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
2889 sm->dot11RSNAStatsTKIPLocalMICFailures++;
2893 const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
2895 if (wpa_auth == NULL)
2897 *len = wpa_auth->wpa_ie_len;
2898 return wpa_auth->wpa_ie;
2902 int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
2903 int session_timeout, struct eapol_state_machine *eapol)
2905 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
2906 sm->wpa_auth->conf.disable_pmksa_caching)
2909 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
2910 sm->wpa_auth->addr, sm->addr, session_timeout,
2911 eapol, sm->wpa_key_mgmt))
2918 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
2919 const u8 *pmk, size_t len, const u8 *sta_addr,
2920 int session_timeout,
2921 struct eapol_state_machine *eapol)
2923 if (wpa_auth == NULL)
2926 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, wpa_auth->addr,
2927 sta_addr, session_timeout, eapol,
2928 WPA_KEY_MGMT_IEEE8021X))
2935 static struct wpa_group *
2936 wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
2938 struct wpa_group *group;
2940 if (wpa_auth == NULL || wpa_auth->group == NULL)
2943 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
2945 group = wpa_group_init(wpa_auth, vlan_id, 0);
2949 group->next = wpa_auth->group->next;
2950 wpa_auth->group->next = group;
2956 int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
2958 struct wpa_group *group;
2960 if (sm == NULL || sm->wpa_auth == NULL)
2963 group = sm->wpa_auth->group;
2965 if (group->vlan_id == vlan_id)
2967 group = group->next;
2970 if (group == NULL) {
2971 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
2976 if (sm->group == group)
2979 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
2980 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
2987 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
2988 struct wpa_state_machine *sm, int ack)
2990 if (wpa_auth == NULL || sm == NULL)
2992 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
2993 " ack=%d", MAC2STR(sm->addr), ack);
2994 if (sm->pending_1_of_4_timeout && ack) {
2996 * Some deployed supplicant implementations update their SNonce
2997 * for each EAPOL-Key 2/4 message even within the same 4-way
2998 * handshake and then fail to use the first SNonce when
2999 * deriving the PTK. This results in unsuccessful 4-way
3000 * handshake whenever the relatively short initial timeout is
3001 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
3002 * around this by increasing the timeout now that we know that
3003 * the station has received the frame.
3005 int timeout_ms = eapol_key_timeout_subseq;
3006 wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
3007 "timeout by %u ms because of acknowledged frame",
3009 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
3010 eloop_register_timeout(timeout_ms / 1000,
3011 (timeout_ms % 1000) * 1000,
3012 wpa_send_eapol_timeout, wpa_auth, sm);
3017 int wpa_auth_uses_sae(struct wpa_state_machine *sm)
3021 return wpa_key_mgmt_sae(sm->wpa_key_mgmt);