2 * IEEE 802.11 RSN / WPA Authenticator
3 * Copyright (c) 2004-2015, 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 "utils/bitfield.h"
15 #include "common/ieee802_11_defs.h"
16 #include "crypto/aes_wrap.h"
17 #include "crypto/crypto.h"
18 #include "crypto/sha1.h"
19 #include "crypto/sha256.h"
20 #include "crypto/random.h"
21 #include "eapol_auth/eapol_auth_sm.h"
22 #include "ap_config.h"
23 #include "ieee802_11.h"
25 #include "pmksa_cache_auth.h"
26 #include "wpa_auth_i.h"
27 #include "wpa_auth_ie.h"
29 #define STATE_MACHINE_DATA struct wpa_state_machine
30 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
31 #define STATE_MACHINE_ADDR sm->addr
34 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
35 static int wpa_sm_step(struct wpa_state_machine *sm);
36 static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
38 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
39 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
40 struct wpa_group *group);
41 static void wpa_request_new_ptk(struct wpa_state_machine *sm);
42 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
43 struct wpa_group *group);
44 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
45 struct wpa_group *group);
46 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
47 const u8 *pmk, struct wpa_ptk *ptk);
48 static void wpa_group_free(struct wpa_authenticator *wpa_auth,
49 struct wpa_group *group);
50 static void wpa_group_get(struct wpa_authenticator *wpa_auth,
51 struct wpa_group *group);
52 static void wpa_group_put(struct wpa_authenticator *wpa_auth,
53 struct wpa_group *group);
55 static const u32 dot11RSNAConfigGroupUpdateCount = 4;
56 static const u32 dot11RSNAConfigPairwiseUpdateCount = 4;
57 static const u32 eapol_key_timeout_first = 100; /* ms */
58 static const u32 eapol_key_timeout_subseq = 1000; /* ms */
59 static const u32 eapol_key_timeout_first_group = 500; /* ms */
61 /* TODO: make these configurable */
62 static const int dot11RSNAConfigPMKLifetime = 43200;
63 static const int dot11RSNAConfigPMKReauthThreshold = 70;
64 static const int dot11RSNAConfigSATimeout = 60;
67 static inline int wpa_auth_mic_failure_report(
68 struct wpa_authenticator *wpa_auth, const u8 *addr)
70 if (wpa_auth->cb.mic_failure_report)
71 return wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr);
76 static inline void wpa_auth_psk_failure_report(
77 struct wpa_authenticator *wpa_auth, const u8 *addr)
79 if (wpa_auth->cb.psk_failure_report)
80 wpa_auth->cb.psk_failure_report(wpa_auth->cb.ctx, addr);
84 static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
85 const u8 *addr, wpa_eapol_variable var,
88 if (wpa_auth->cb.set_eapol)
89 wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value);
93 static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
94 const u8 *addr, wpa_eapol_variable var)
96 if (wpa_auth->cb.get_eapol == NULL)
98 return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var);
102 static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
104 const u8 *p2p_dev_addr,
107 if (wpa_auth->cb.get_psk == NULL)
109 return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, p2p_dev_addr,
114 static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
115 const u8 *addr, u8 *msk, size_t *len)
117 if (wpa_auth->cb.get_msk == NULL)
119 return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len);
123 static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
125 enum wpa_alg alg, const u8 *addr, int idx,
126 u8 *key, size_t key_len)
128 if (wpa_auth->cb.set_key == NULL)
130 return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
135 static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
136 const u8 *addr, int idx, u8 *seq)
138 if (wpa_auth->cb.get_seqnum == NULL)
140 return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
145 wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
146 const u8 *data, size_t data_len, int encrypt)
148 if (wpa_auth->cb.send_eapol == NULL)
150 return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
156 static inline int wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth,
159 if (wpa_auth->cb.start_ampe == NULL)
161 return wpa_auth->cb.start_ampe(wpa_auth->cb.ctx, addr);
163 #endif /* CONFIG_MESH */
166 int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
167 int (*cb)(struct wpa_state_machine *sm, void *ctx),
170 if (wpa_auth->cb.for_each_sta == NULL)
172 return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
176 int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
177 int (*cb)(struct wpa_authenticator *a, void *ctx),
180 if (wpa_auth->cb.for_each_auth == NULL)
182 return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
186 void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
187 logger_level level, const char *txt)
189 if (wpa_auth->cb.logger == NULL)
191 wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
195 void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
196 logger_level level, const char *fmt, ...)
202 if (wpa_auth->cb.logger == NULL)
205 maxlen = os_strlen(fmt) + 100;
206 format = os_malloc(maxlen);
211 vsnprintf(format, maxlen, fmt, ap);
214 wpa_auth_logger(wpa_auth, addr, level, format);
220 static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
223 if (wpa_auth->cb.disconnect == NULL)
225 wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR, MAC2STR(addr));
226 wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
227 WLAN_REASON_PREV_AUTH_NOT_VALID);
231 static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
234 #ifdef CONFIG_IEEE80211R
235 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
237 #endif /* CONFIG_IEEE80211R */
238 #ifdef CONFIG_IEEE80211W
239 if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
241 #endif /* CONFIG_IEEE80211W */
242 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN)
248 static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
250 struct wpa_authenticator *wpa_auth = eloop_ctx;
252 if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
253 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
256 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
257 wpa_hexdump_key(MSG_DEBUG, "GMK",
258 wpa_auth->group->GMK, WPA_GMK_LEN);
261 if (wpa_auth->conf.wpa_gmk_rekey) {
262 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
263 wpa_rekey_gmk, wpa_auth, NULL);
268 static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
270 struct wpa_authenticator *wpa_auth = eloop_ctx;
271 struct wpa_group *group, *next;
273 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
274 group = wpa_auth->group;
276 wpa_group_get(wpa_auth, group);
278 group->GTKReKey = TRUE;
280 group->changed = FALSE;
281 wpa_group_sm_step(wpa_auth, group);
282 } while (group->changed);
285 wpa_group_put(wpa_auth, group);
289 if (wpa_auth->conf.wpa_group_rekey) {
290 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
291 0, wpa_rekey_gtk, wpa_auth, NULL);
296 static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
298 struct wpa_authenticator *wpa_auth = eloop_ctx;
299 struct wpa_state_machine *sm = timeout_ctx;
301 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
302 wpa_request_new_ptk(sm);
307 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
309 if (sm->pmksa == ctx)
315 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
318 struct wpa_authenticator *wpa_auth = ctx;
319 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
323 static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
324 struct wpa_group *group)
326 u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
330 if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
332 wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
335 * Counter = PRF-256(Random number, "Init Counter",
336 * Local MAC Address || Time)
338 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
339 wpa_get_ntp_timestamp(buf + ETH_ALEN);
340 ptr = (unsigned long) group;
341 os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
342 if (random_get_bytes(rkey, sizeof(rkey)) < 0)
345 if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
346 group->Counter, WPA_NONCE_LEN) < 0)
348 wpa_hexdump_key(MSG_DEBUG, "Key Counter",
349 group->Counter, WPA_NONCE_LEN);
355 static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
356 int vlan_id, int delay_init)
358 struct wpa_group *group;
360 group = os_zalloc(sizeof(struct wpa_group));
364 group->GTKAuthenticator = TRUE;
365 group->vlan_id = vlan_id;
366 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
368 if (random_pool_ready() != 1) {
369 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
370 "for secure operations - update keys later when "
371 "the first station connects");
375 * Set initial GMK/Counter value here. The actual values that will be
376 * used in negotiations will be set once the first station tries to
377 * connect. This allows more time for collecting additional randomness
378 * on embedded devices.
380 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
381 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
389 wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start "
390 "until Beacon frames have been configured");
391 /* Initialization is completed in wpa_init_keys(). */
393 wpa_group_sm_step(wpa_auth, group);
394 group->GInit = FALSE;
395 wpa_group_sm_step(wpa_auth, group);
403 * wpa_init - Initialize WPA authenticator
404 * @addr: Authenticator address
405 * @conf: Configuration for WPA authenticator
406 * @cb: Callback functions for WPA authenticator
407 * Returns: Pointer to WPA authenticator data or %NULL on failure
409 struct wpa_authenticator * wpa_init(const u8 *addr,
410 struct wpa_auth_config *conf,
411 struct wpa_auth_callbacks *cb)
413 struct wpa_authenticator *wpa_auth;
415 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
416 if (wpa_auth == NULL)
418 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
419 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
420 os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
422 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
423 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
428 wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
429 if (wpa_auth->group == NULL) {
430 os_free(wpa_auth->wpa_ie);
435 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
437 if (wpa_auth->pmksa == NULL) {
438 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
439 os_free(wpa_auth->group);
440 os_free(wpa_auth->wpa_ie);
445 #ifdef CONFIG_IEEE80211R
446 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
447 if (wpa_auth->ft_pmk_cache == NULL) {
448 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
449 os_free(wpa_auth->group);
450 os_free(wpa_auth->wpa_ie);
451 pmksa_cache_auth_deinit(wpa_auth->pmksa);
455 #endif /* CONFIG_IEEE80211R */
457 if (wpa_auth->conf.wpa_gmk_rekey) {
458 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
459 wpa_rekey_gmk, wpa_auth, NULL);
462 if (wpa_auth->conf.wpa_group_rekey) {
463 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
464 wpa_rekey_gtk, wpa_auth, NULL);
468 if (WPA_GET_BE32(conf->ip_addr_start)) {
469 int count = WPA_GET_BE32(conf->ip_addr_end) -
470 WPA_GET_BE32(conf->ip_addr_start) + 1;
474 wpa_auth->ip_pool = bitfield_alloc(count);
476 #endif /* CONFIG_P2P */
482 int wpa_init_keys(struct wpa_authenticator *wpa_auth)
484 struct wpa_group *group = wpa_auth->group;
486 wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial "
488 wpa_group_sm_step(wpa_auth, group);
489 group->GInit = FALSE;
490 wpa_group_sm_step(wpa_auth, group);
491 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
498 * wpa_deinit - Deinitialize WPA authenticator
499 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
501 void wpa_deinit(struct wpa_authenticator *wpa_auth)
503 struct wpa_group *group, *prev;
505 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
506 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
508 #ifdef CONFIG_PEERKEY
509 while (wpa_auth->stsl_negotiations)
510 wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
511 #endif /* CONFIG_PEERKEY */
513 pmksa_cache_auth_deinit(wpa_auth->pmksa);
515 #ifdef CONFIG_IEEE80211R
516 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
517 wpa_auth->ft_pmk_cache = NULL;
518 #endif /* CONFIG_IEEE80211R */
521 bitfield_free(wpa_auth->ip_pool);
522 #endif /* CONFIG_P2P */
525 os_free(wpa_auth->wpa_ie);
527 group = wpa_auth->group;
539 * wpa_reconfig - Update WPA authenticator configuration
540 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
541 * @conf: Configuration for WPA authenticator
543 int wpa_reconfig(struct wpa_authenticator *wpa_auth,
544 struct wpa_auth_config *conf)
546 struct wpa_group *group;
547 if (wpa_auth == NULL)
550 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
551 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
552 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
557 * Reinitialize GTK to make sure it is suitable for the new
560 group = wpa_auth->group;
561 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
563 wpa_group_sm_step(wpa_auth, group);
564 group->GInit = FALSE;
565 wpa_group_sm_step(wpa_auth, group);
571 struct wpa_state_machine *
572 wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
573 const u8 *p2p_dev_addr)
575 struct wpa_state_machine *sm;
577 if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
580 sm = os_zalloc(sizeof(struct wpa_state_machine));
583 os_memcpy(sm->addr, addr, ETH_ALEN);
585 os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
587 sm->wpa_auth = wpa_auth;
588 sm->group = wpa_auth->group;
589 wpa_group_get(sm->wpa_auth, sm->group);
595 int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
596 struct wpa_state_machine *sm)
598 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
601 #ifdef CONFIG_IEEE80211R
602 if (sm->ft_completed) {
603 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
604 "FT authentication already completed - do not "
605 "start 4-way handshake");
606 /* Go to PTKINITDONE state to allow GTK rekeying */
607 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
610 #endif /* CONFIG_IEEE80211R */
613 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
614 sm->ReAuthenticationRequest = TRUE;
615 return wpa_sm_step(sm);
618 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
619 "start authentication");
623 if (wpa_sm_step(sm) == 1)
624 return 1; /* should not really happen */
626 sm->AuthenticationRequest = TRUE;
627 return wpa_sm_step(sm);
631 void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
633 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
634 * reassociates back to the same AP while the previous entry for the
635 * STA has not yet been removed. */
639 sm->wpa_key_mgmt = 0;
643 static void wpa_free_sta_sm(struct wpa_state_machine *sm)
646 if (WPA_GET_BE32(sm->ip_addr)) {
648 wpa_printf(MSG_DEBUG, "P2P: Free assigned IP "
649 "address %u.%u.%u.%u from " MACSTR,
650 sm->ip_addr[0], sm->ip_addr[1],
651 sm->ip_addr[2], sm->ip_addr[3],
653 start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start);
654 bitfield_clear(sm->wpa_auth->ip_pool,
655 WPA_GET_BE32(sm->ip_addr) - start);
657 #endif /* CONFIG_P2P */
658 if (sm->GUpdateStationKeys) {
659 sm->group->GKeyDoneStations--;
660 sm->GUpdateStationKeys = FALSE;
662 #ifdef CONFIG_IEEE80211R
663 os_free(sm->assoc_resp_ftie);
664 wpabuf_free(sm->ft_pending_req_ies);
665 #endif /* CONFIG_IEEE80211R */
666 os_free(sm->last_rx_eapol_key);
668 wpa_group_put(sm->wpa_auth, sm->group);
673 void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
678 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
679 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
680 "strict rekeying - force GTK rekey since STA "
682 eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
683 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
687 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
688 sm->pending_1_of_4_timeout = 0;
689 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
690 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
691 if (sm->in_step_loop) {
692 /* Must not free state machine while wpa_sm_step() is running.
693 * Freeing will be completed in the end of wpa_sm_step(). */
694 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
695 "machine deinit for " MACSTR, MAC2STR(sm->addr));
696 sm->pending_deinit = 1;
702 static void wpa_request_new_ptk(struct wpa_state_machine *sm)
707 sm->PTKRequest = TRUE;
712 static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
713 const u8 *replay_counter)
716 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
719 if (os_memcmp(replay_counter, ctr[i].counter,
720 WPA_REPLAY_COUNTER_LEN) == 0)
727 static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
728 const u8 *replay_counter)
731 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
733 (replay_counter == NULL ||
734 os_memcmp(replay_counter, ctr[i].counter,
735 WPA_REPLAY_COUNTER_LEN) == 0))
736 ctr[i].valid = FALSE;
741 #ifdef CONFIG_IEEE80211R
742 static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
743 struct wpa_state_machine *sm,
744 struct wpa_eapol_ie_parse *kde)
746 struct wpa_ie_data ie;
747 struct rsn_mdie *mdie;
749 if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
750 ie.num_pmkid != 1 || ie.pmkid == NULL) {
751 wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
752 "FT 4-way handshake message 2/4");
756 os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
757 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
758 sm->sup_pmk_r1_name, PMKID_LEN);
760 if (!kde->mdie || !kde->ftie) {
761 wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
762 "message 2/4", kde->mdie ? "FTIE" : "MDIE");
766 mdie = (struct rsn_mdie *) (kde->mdie + 2);
767 if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
768 os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
769 MOBILITY_DOMAIN_ID_LEN) != 0) {
770 wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
774 if (sm->assoc_resp_ftie &&
775 (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
776 os_memcmp(kde->ftie, sm->assoc_resp_ftie,
777 2 + sm->assoc_resp_ftie[1]) != 0)) {
778 wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
779 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
780 kde->ftie, kde->ftie_len);
781 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
782 sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
788 #endif /* CONFIG_IEEE80211R */
791 static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
792 struct wpa_state_machine *sm, int group)
794 /* Supplicant reported a Michael MIC error */
795 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
796 "received EAPOL-Key Error Request "
797 "(STA detected Michael MIC failure (group=%d))",
800 if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
801 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
802 "ignore Michael MIC failure report since "
803 "group cipher is not TKIP");
804 } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
805 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
806 "ignore Michael MIC failure report since "
807 "pairwise cipher is not TKIP");
809 if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
810 return 1; /* STA entry was removed */
811 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
812 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
816 * Error report is not a request for a new key handshake, but since
817 * Authenticator may do it, let's change the keys now anyway.
819 wpa_request_new_ptk(sm);
824 static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data,
829 const u8 *pmk = NULL;
832 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
833 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
834 sm->p2p_dev_addr, pmk);
840 wpa_derive_ptk(sm, sm->alt_SNonce, pmk, &PTK);
842 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK, data, data_len)
848 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
853 wpa_printf(MSG_DEBUG,
854 "WPA: Earlier SNonce did not result in matching MIC");
858 wpa_printf(MSG_DEBUG,
859 "WPA: Earlier SNonce resulted in matching MIC");
860 sm->alt_snonce_valid = 0;
861 os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN);
862 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
863 sm->PTK_valid = TRUE;
869 void wpa_receive(struct wpa_authenticator *wpa_auth,
870 struct wpa_state_machine *sm,
871 u8 *data, size_t data_len)
873 struct ieee802_1x_hdr *hdr;
874 struct wpa_eapol_key *key;
875 struct wpa_eapol_key_192 *key192;
876 u16 key_info, key_data_length;
877 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
878 SMK_M1, SMK_M3, SMK_ERROR } msg;
880 struct wpa_eapol_ie_parse kde;
882 const u8 *eapol_key_ie, *key_data;
883 size_t eapol_key_ie_len, keyhdrlen, mic_len;
885 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
888 mic_len = wpa_mic_len(sm->wpa_key_mgmt);
889 keyhdrlen = mic_len == 24 ? sizeof(*key192) : sizeof(*key);
891 if (data_len < sizeof(*hdr) + keyhdrlen)
894 hdr = (struct ieee802_1x_hdr *) data;
895 key = (struct wpa_eapol_key *) (hdr + 1);
896 key192 = (struct wpa_eapol_key_192 *) (hdr + 1);
897 key_info = WPA_GET_BE16(key->key_info);
899 key_data = (const u8 *) (key192 + 1);
900 key_data_length = WPA_GET_BE16(key192->key_data_length);
902 key_data = (const u8 *) (key + 1);
903 key_data_length = WPA_GET_BE16(key->key_data_length);
905 wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
906 " key_info=0x%x type=%u key_data_length=%u",
907 MAC2STR(sm->addr), key_info, key->type, key_data_length);
908 if (key_data_length > data_len - sizeof(*hdr) - keyhdrlen) {
909 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
910 "key_data overflow (%d > %lu)",
912 (unsigned long) (data_len - sizeof(*hdr) -
917 if (sm->wpa == WPA_VERSION_WPA2) {
918 if (key->type == EAPOL_KEY_TYPE_WPA) {
920 * Some deployed station implementations seem to send
921 * msg 4/4 with incorrect type value in WPA2 mode.
923 wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
924 "with unexpected WPA type in RSN mode");
925 } else if (key->type != EAPOL_KEY_TYPE_RSN) {
926 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
927 "unexpected type %d in RSN mode",
932 if (key->type != EAPOL_KEY_TYPE_WPA) {
933 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
934 "unexpected type %d in WPA mode",
940 wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
942 wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
943 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
945 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
948 if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
949 (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
950 if (key_info & WPA_KEY_INFO_ERROR) {
952 msgtxt = "SMK Error";
957 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
960 } else if (key_info & WPA_KEY_INFO_REQUEST) {
963 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
965 msgtxt = "2/2 Group";
966 } else if (key_data_length == 0) {
968 msgtxt = "4/4 Pairwise";
971 msgtxt = "2/4 Pairwise";
974 /* TODO: key_info type validation for PeerKey */
975 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
977 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
978 if (sm->pairwise == WPA_CIPHER_CCMP ||
979 sm->pairwise == WPA_CIPHER_GCMP) {
980 if (wpa_use_aes_cmac(sm) &&
981 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN &&
982 !wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) &&
983 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
984 wpa_auth_logger(wpa_auth, sm->addr,
986 "advertised support for "
987 "AES-128-CMAC, but did not "
992 if (!wpa_use_aes_cmac(sm) &&
993 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
994 wpa_auth_logger(wpa_auth, sm->addr,
996 "did not use HMAC-SHA1-AES "
1002 if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) &&
1003 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1004 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1005 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
1010 if (key_info & WPA_KEY_INFO_REQUEST) {
1011 if (sm->req_replay_counter_used &&
1012 os_memcmp(key->replay_counter, sm->req_replay_counter,
1013 WPA_REPLAY_COUNTER_LEN) <= 0) {
1014 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1015 "received EAPOL-Key request with "
1016 "replayed counter");
1021 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
1022 !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
1025 if (msg == PAIRWISE_2 &&
1026 wpa_replay_counter_valid(sm->prev_key_replay,
1027 key->replay_counter) &&
1028 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1029 os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
1032 * Some supplicant implementations (e.g., Windows XP
1033 * WZC) update SNonce for each EAPOL-Key 2/4. This
1034 * breaks the workaround on accepting any of the
1035 * pending requests, so allow the SNonce to be updated
1036 * even if we have already sent out EAPOL-Key 3/4.
1038 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1039 "Process SNonce update from STA "
1040 "based on retransmitted EAPOL-Key "
1042 sm->update_snonce = 1;
1043 os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN);
1044 sm->alt_snonce_valid = TRUE;
1045 os_memcpy(sm->alt_replay_counter,
1046 sm->key_replay[0].counter,
1047 WPA_REPLAY_COUNTER_LEN);
1048 goto continue_processing;
1051 if (msg == PAIRWISE_4 && sm->alt_snonce_valid &&
1052 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1053 os_memcmp(key->replay_counter, sm->alt_replay_counter,
1054 WPA_REPLAY_COUNTER_LEN) == 0) {
1056 * Supplicant may still be using the old SNonce since
1057 * there was two EAPOL-Key 2/4 messages and they had
1058 * different SNonce values.
1060 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1061 "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
1062 goto continue_processing;
1065 if (msg == PAIRWISE_2 &&
1066 wpa_replay_counter_valid(sm->prev_key_replay,
1067 key->replay_counter) &&
1068 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
1069 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1070 "ignore retransmitted EAPOL-Key %s - "
1071 "SNonce did not change", msgtxt);
1073 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1074 "received EAPOL-Key %s with "
1075 "unexpected replay counter", msgtxt);
1077 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
1078 if (!sm->key_replay[i].valid)
1080 wpa_hexdump(MSG_DEBUG, "pending replay counter",
1081 sm->key_replay[i].counter,
1082 WPA_REPLAY_COUNTER_LEN);
1084 wpa_hexdump(MSG_DEBUG, "received replay counter",
1085 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1089 continue_processing:
1092 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
1093 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
1094 (!sm->update_snonce ||
1095 sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
1096 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1097 "received EAPOL-Key msg 2/4 in "
1098 "invalid state (%d) - dropped",
1102 random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
1103 if (sm->group->reject_4way_hs_for_entropy) {
1105 * The system did not have enough entropy to generate
1106 * strong random numbers. Reject the first 4-way
1107 * handshake(s) and collect some entropy based on the
1108 * information from it. Once enough entropy is
1109 * available, the next atempt will trigger GMK/Key
1110 * Counter update and the station will be allowed to
1113 wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
1114 "collect more entropy for random number "
1116 random_mark_pool_ready();
1117 wpa_sta_disconnect(wpa_auth, sm->addr);
1120 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
1121 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1122 "received EAPOL-Key msg 2/4 with "
1123 "invalid Key Data contents");
1127 eapol_key_ie = kde.rsn_ie;
1128 eapol_key_ie_len = kde.rsn_ie_len;
1129 } else if (kde.osen) {
1130 eapol_key_ie = kde.osen;
1131 eapol_key_ie_len = kde.osen_len;
1133 eapol_key_ie = kde.wpa_ie;
1134 eapol_key_ie_len = kde.wpa_ie_len;
1136 ft = sm->wpa == WPA_VERSION_WPA2 &&
1137 wpa_key_mgmt_ft(sm->wpa_key_mgmt);
1138 if (sm->wpa_ie == NULL ||
1139 wpa_compare_rsn_ie(ft,
1140 sm->wpa_ie, sm->wpa_ie_len,
1141 eapol_key_ie, eapol_key_ie_len)) {
1142 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1143 "WPA IE from (Re)AssocReq did not "
1144 "match with msg 2/4");
1146 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
1147 sm->wpa_ie, sm->wpa_ie_len);
1149 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
1150 eapol_key_ie, eapol_key_ie_len);
1151 /* MLME-DEAUTHENTICATE.request */
1152 wpa_sta_disconnect(wpa_auth, sm->addr);
1155 #ifdef CONFIG_IEEE80211R
1156 if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
1157 wpa_sta_disconnect(wpa_auth, sm->addr);
1160 #endif /* CONFIG_IEEE80211R */
1162 if (kde.ip_addr_req && kde.ip_addr_req[0] &&
1163 wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
1165 wpa_printf(MSG_DEBUG, "P2P: IP address requested in "
1166 "EAPOL-Key exchange");
1167 idx = bitfield_get_first_zero(wpa_auth->ip_pool);
1169 u32 start = WPA_GET_BE32(wpa_auth->conf.
1171 bitfield_set(wpa_auth->ip_pool, idx);
1172 WPA_PUT_BE32(sm->ip_addr, start + idx);
1173 wpa_printf(MSG_DEBUG, "P2P: Assigned IP "
1174 "address %u.%u.%u.%u to " MACSTR,
1175 sm->ip_addr[0], sm->ip_addr[1],
1176 sm->ip_addr[2], sm->ip_addr[3],
1180 #endif /* CONFIG_P2P */
1183 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1185 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1186 "received EAPOL-Key msg 4/4 in "
1187 "invalid state (%d) - dropped",
1193 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1194 || !sm->PTK_valid) {
1195 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1196 "received EAPOL-Key msg 2/2 in "
1197 "invalid state (%d) - dropped",
1198 sm->wpa_ptk_group_state);
1202 #ifdef CONFIG_PEERKEY
1206 if (!wpa_auth->conf.peerkey) {
1207 wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
1208 "PeerKey use disabled - ignoring message");
1211 if (!sm->PTK_valid) {
1212 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1213 "received EAPOL-Key msg SMK in "
1214 "invalid state - dropped");
1218 #else /* CONFIG_PEERKEY */
1222 return; /* STSL disabled - ignore SMK messages */
1223 #endif /* CONFIG_PEERKEY */
1228 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1229 "received EAPOL-Key frame (%s)", msgtxt);
1231 if (key_info & WPA_KEY_INFO_ACK) {
1232 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1233 "received invalid EAPOL-Key: Key Ack set");
1237 if (!(key_info & WPA_KEY_INFO_MIC)) {
1238 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1239 "received invalid EAPOL-Key: Key MIC not set");
1243 sm->MICVerified = FALSE;
1244 if (sm->PTK_valid && !sm->update_snonce) {
1245 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &sm->PTK, data,
1247 (msg != PAIRWISE_4 || !sm->alt_snonce_valid ||
1248 wpa_try_alt_snonce(sm, data, data_len))) {
1249 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1250 "received EAPOL-Key with invalid MIC");
1253 sm->MICVerified = TRUE;
1254 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
1255 sm->pending_1_of_4_timeout = 0;
1258 if (key_info & WPA_KEY_INFO_REQUEST) {
1259 if (sm->MICVerified) {
1260 sm->req_replay_counter_used = 1;
1261 os_memcpy(sm->req_replay_counter, key->replay_counter,
1262 WPA_REPLAY_COUNTER_LEN);
1264 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1265 "received EAPOL-Key request with "
1271 * TODO: should decrypt key data field if encryption was used;
1272 * even though MAC address KDE is not normally encrypted,
1273 * supplicant is allowed to encrypt it.
1275 if (msg == SMK_ERROR) {
1276 #ifdef CONFIG_PEERKEY
1277 wpa_smk_error(wpa_auth, sm, key_data, key_data_length);
1278 #endif /* CONFIG_PEERKEY */
1280 } else if (key_info & WPA_KEY_INFO_ERROR) {
1281 if (wpa_receive_error_report(
1283 !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
1284 return; /* STA entry was removed */
1285 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1286 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1287 "received EAPOL-Key Request for new "
1289 wpa_request_new_ptk(sm);
1290 #ifdef CONFIG_PEERKEY
1291 } else if (msg == SMK_M1) {
1292 wpa_smk_m1(wpa_auth, sm, key, key_data,
1294 #endif /* CONFIG_PEERKEY */
1295 } else if (key_data_length > 0 &&
1296 wpa_parse_kde_ies(key_data, key_data_length,
1300 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1301 "received EAPOL-Key Request for GTK "
1303 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
1304 wpa_rekey_gtk(wpa_auth, NULL);
1307 /* Do not allow the same key replay counter to be reused. */
1308 wpa_replay_counter_mark_invalid(sm->key_replay,
1309 key->replay_counter);
1311 if (msg == PAIRWISE_2) {
1313 * Maintain a copy of the pending EAPOL-Key frames in
1314 * case the EAPOL-Key frame was retransmitted. This is
1315 * needed to allow EAPOL-Key msg 2/4 reply to another
1316 * pending msg 1/4 to update the SNonce to work around
1317 * unexpected supplicant behavior.
1319 os_memcpy(sm->prev_key_replay, sm->key_replay,
1320 sizeof(sm->key_replay));
1322 os_memset(sm->prev_key_replay, 0,
1323 sizeof(sm->prev_key_replay));
1327 * Make sure old valid counters are not accepted anymore and
1328 * do not get copied again.
1330 wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
1333 #ifdef CONFIG_PEERKEY
1334 if (msg == SMK_M3) {
1335 wpa_smk_m3(wpa_auth, sm, key, key_data, key_data_length);
1338 #endif /* CONFIG_PEERKEY */
1340 os_free(sm->last_rx_eapol_key);
1341 sm->last_rx_eapol_key = os_malloc(data_len);
1342 if (sm->last_rx_eapol_key == NULL)
1344 os_memcpy(sm->last_rx_eapol_key, data, data_len);
1345 sm->last_rx_eapol_key_len = data_len;
1347 sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
1348 sm->EAPOLKeyReceived = TRUE;
1349 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1350 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1351 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1356 static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
1357 const u8 *gnonce, u8 *gtk, size_t gtk_len)
1359 u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + 16];
1363 /* GTK = PRF-X(GMK, "Group key expansion",
1364 * AA || GNonce || Time || random data)
1365 * The example described in the IEEE 802.11 standard uses only AA and
1366 * GNonce as inputs here. Add some more entropy since this derivation
1367 * is done only at the Authenticator and as such, does not need to be
1370 os_memcpy(data, addr, ETH_ALEN);
1371 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
1372 pos = data + ETH_ALEN + WPA_NONCE_LEN;
1373 wpa_get_ntp_timestamp(pos);
1375 if (random_get_bytes(pos, 16) < 0)
1378 #ifdef CONFIG_IEEE80211W
1379 sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len);
1380 #else /* CONFIG_IEEE80211W */
1381 if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len)
1384 #endif /* CONFIG_IEEE80211W */
1390 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1392 struct wpa_authenticator *wpa_auth = eloop_ctx;
1393 struct wpa_state_machine *sm = timeout_ctx;
1395 sm->pending_1_of_4_timeout = 0;
1396 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
1397 sm->TimeoutEvt = TRUE;
1402 void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1403 struct wpa_state_machine *sm, int key_info,
1404 const u8 *key_rsc, const u8 *nonce,
1405 const u8 *kde, size_t kde_len,
1406 int keyidx, int encr, int force_version)
1408 struct ieee802_1x_hdr *hdr;
1409 struct wpa_eapol_key *key;
1410 struct wpa_eapol_key_192 *key192;
1411 size_t len, mic_len, keyhdrlen;
1413 int key_data_len, pad_len = 0;
1415 int version, pairwise;
1419 mic_len = wpa_mic_len(sm->wpa_key_mgmt);
1420 keyhdrlen = mic_len == 24 ? sizeof(*key192) : sizeof(*key);
1422 len = sizeof(struct ieee802_1x_hdr) + keyhdrlen;
1425 version = force_version;
1426 else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
1427 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt))
1428 version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
1429 else if (wpa_use_aes_cmac(sm))
1430 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
1431 else if (sm->pairwise != WPA_CIPHER_TKIP)
1432 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1434 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1436 pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1438 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1439 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1442 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
1443 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
1444 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
1445 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
1446 pairwise, (unsigned long) kde_len, keyidx, encr);
1448 key_data_len = kde_len;
1450 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1451 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
1452 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
1453 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1454 pad_len = key_data_len % 8;
1456 pad_len = 8 - pad_len;
1457 key_data_len += pad_len + 8;
1460 len += key_data_len;
1462 hdr = os_zalloc(len);
1465 hdr->version = wpa_auth->conf.eapol_version;
1466 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1467 hdr->length = host_to_be16(len - sizeof(*hdr));
1468 key = (struct wpa_eapol_key *) (hdr + 1);
1469 key192 = (struct wpa_eapol_key_192 *) (hdr + 1);
1470 key_data = ((u8 *) (hdr + 1)) + keyhdrlen;
1472 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1473 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1474 key_info |= version;
1475 if (encr && sm->wpa == WPA_VERSION_WPA2)
1476 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1477 if (sm->wpa != WPA_VERSION_WPA2)
1478 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1479 WPA_PUT_BE16(key->key_info, key_info);
1481 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
1482 WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
1483 if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
1484 WPA_PUT_BE16(key->key_length, 0);
1486 /* FIX: STSL: what to use as key_replay_counter? */
1487 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1488 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1489 os_memcpy(sm->key_replay[i].counter,
1490 sm->key_replay[i - 1].counter,
1491 WPA_REPLAY_COUNTER_LEN);
1493 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1494 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1495 WPA_REPLAY_COUNTER_LEN);
1496 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter",
1497 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1498 sm->key_replay[0].valid = TRUE;
1501 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1504 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1507 os_memcpy(key_data, kde, kde_len);
1509 WPA_PUT_BE16(key192->key_data_length, kde_len);
1511 WPA_PUT_BE16(key->key_data_length, kde_len);
1512 } else if (encr && kde) {
1513 buf = os_zalloc(key_data_len);
1519 os_memcpy(pos, kde, kde_len);
1525 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1527 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1528 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
1529 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
1530 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1531 if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len,
1532 (key_data_len - 8) / 8, buf, key_data)) {
1538 WPA_PUT_BE16(key192->key_data_length,
1541 WPA_PUT_BE16(key->key_data_length,
1543 #ifndef CONFIG_NO_RC4
1544 } else if (sm->PTK.kek_len == 16) {
1546 os_memcpy(key->key_iv,
1547 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1548 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1549 os_memcpy(ek, key->key_iv, 16);
1550 os_memcpy(ek + 16, sm->PTK.kek, sm->PTK.kek_len);
1551 os_memcpy(key_data, buf, key_data_len);
1552 rc4_skip(ek, 32, 256, key_data, key_data_len);
1554 WPA_PUT_BE16(key192->key_data_length,
1557 WPA_PUT_BE16(key->key_data_length,
1559 #endif /* CONFIG_NO_RC4 */
1568 if (key_info & WPA_KEY_INFO_MIC) {
1571 if (!sm->PTK_valid) {
1572 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1573 "PTK not valid when sending EAPOL-Key "
1579 key_mic = key192->key_mic; /* same offset for key and key192 */
1580 wpa_eapol_key_mic(sm->PTK.kck, sm->PTK.kck_len,
1581 sm->wpa_key_mgmt, version,
1582 (u8 *) hdr, len, key_mic);
1583 #ifdef CONFIG_TESTING_OPTIONS
1585 wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0 &&
1587 wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
1588 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1589 "Corrupting group EAPOL-Key Key MIC");
1592 #endif /* CONFIG_TESTING_OPTIONS */
1595 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1597 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1603 static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1604 struct wpa_state_machine *sm, int key_info,
1605 const u8 *key_rsc, const u8 *nonce,
1606 const u8 *kde, size_t kde_len,
1607 int keyidx, int encr)
1610 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1616 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1619 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1620 if (ctr == 1 && wpa_auth->conf.tx_status)
1621 timeout_ms = pairwise ? eapol_key_timeout_first :
1622 eapol_key_timeout_first_group;
1624 timeout_ms = eapol_key_timeout_subseq;
1625 if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
1626 sm->pending_1_of_4_timeout = 1;
1627 wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1628 "counter %d)", timeout_ms, ctr);
1629 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1630 wpa_send_eapol_timeout, wpa_auth, sm);
1634 static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
1637 struct ieee802_1x_hdr *hdr;
1638 struct wpa_eapol_key *key;
1639 struct wpa_eapol_key_192 *key192;
1642 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
1643 size_t mic_len = wpa_mic_len(akmp);
1645 if (data_len < sizeof(*hdr) + sizeof(*key))
1648 hdr = (struct ieee802_1x_hdr *) data;
1649 key = (struct wpa_eapol_key *) (hdr + 1);
1650 key192 = (struct wpa_eapol_key_192 *) (hdr + 1);
1651 key_info = WPA_GET_BE16(key->key_info);
1652 os_memcpy(mic, key192->key_mic, mic_len);
1653 os_memset(key192->key_mic, 0, mic_len);
1654 if (wpa_eapol_key_mic(PTK->kck, PTK->kck_len, akmp,
1655 key_info & WPA_KEY_INFO_TYPE_MASK,
1656 data, data_len, key192->key_mic) ||
1657 os_memcmp_const(mic, key192->key_mic, mic_len) != 0)
1659 os_memcpy(key192->key_mic, mic, mic_len);
1664 void wpa_remove_ptk(struct wpa_state_machine *sm)
1666 sm->PTK_valid = FALSE;
1667 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1668 wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL, 0);
1669 sm->pairwise_set = FALSE;
1670 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1674 int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event)
1681 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1682 "event %d notification", event);
1687 /* PTKs are derived through AMPE */
1688 if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) {
1693 #endif /* CONFIG_MESH */
1698 sm->DeauthenticationRequest = TRUE;
1701 case WPA_REAUTH_EAPOL:
1704 * When using WPS, we may end up here if the STA
1705 * manages to re-associate without the previous STA
1706 * entry getting removed. Consequently, we need to make
1707 * sure that the WPA state machines gets initialized
1708 * properly at this point.
1710 wpa_printf(MSG_DEBUG, "WPA state machine had not been "
1711 "started - initialize now");
1714 if (wpa_sm_step(sm) == 1)
1715 return 1; /* should not really happen */
1717 sm->AuthenticationRequest = TRUE;
1720 if (sm->GUpdateStationKeys) {
1722 * Reauthentication cancels the pending group key
1723 * update for this STA.
1725 sm->group->GKeyDoneStations--;
1726 sm->GUpdateStationKeys = FALSE;
1727 sm->PtkGroupInit = TRUE;
1729 sm->ReAuthenticationRequest = TRUE;
1732 #ifdef CONFIG_IEEE80211R
1733 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1734 "after association");
1735 wpa_ft_install_ptk(sm);
1737 /* Using FT protocol, not WPA auth state machine */
1738 sm->ft_completed = 1;
1740 #else /* CONFIG_IEEE80211R */
1742 #endif /* CONFIG_IEEE80211R */
1745 #ifdef CONFIG_IEEE80211R
1746 sm->ft_completed = 0;
1747 #endif /* CONFIG_IEEE80211R */
1749 #ifdef CONFIG_IEEE80211W
1750 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1752 #endif /* CONFIG_IEEE80211W */
1755 sm->PTK_valid = FALSE;
1756 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1758 if (event != WPA_REAUTH_EAPOL)
1762 if (sm->in_step_loop) {
1764 * wpa_sm_step() is already running - avoid recursive call to
1765 * it by making the existing loop process the new update.
1770 return wpa_sm_step(sm);
1774 SM_STATE(WPA_PTK, INITIALIZE)
1776 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1778 /* Init flag is not cleared here, so avoid busy
1779 * loop by claiming nothing changed. */
1780 sm->changed = FALSE;
1784 if (sm->GUpdateStationKeys)
1785 sm->group->GKeyDoneStations--;
1786 sm->GUpdateStationKeys = FALSE;
1787 if (sm->wpa == WPA_VERSION_WPA)
1788 sm->PInitAKeys = FALSE;
1789 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1790 * Local AA > Remote AA)) */) {
1793 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1795 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1797 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1798 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1799 WPA_EAPOL_authorized, 0);
1804 SM_STATE(WPA_PTK, DISCONNECT)
1806 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1807 sm->Disconnect = FALSE;
1808 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1812 SM_STATE(WPA_PTK, DISCONNECTED)
1814 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1815 sm->DeauthenticationRequest = FALSE;
1819 SM_STATE(WPA_PTK, AUTHENTICATION)
1821 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1822 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1823 sm->PTK_valid = FALSE;
1824 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1826 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1827 sm->AuthenticationRequest = FALSE;
1831 static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
1832 struct wpa_group *group)
1834 if (group->first_sta_seen)
1837 * System has run bit further than at the time hostapd was started
1838 * potentially very early during boot up. This provides better chances
1839 * of collecting more randomness on embedded systems. Re-initialize the
1840 * GMK and Counter here to improve their strength if there was not
1841 * enough entropy available immediately after system startup.
1843 wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
1845 if (random_pool_ready() != 1) {
1846 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
1847 "to proceed - reject first 4-way handshake");
1848 group->reject_4way_hs_for_entropy = TRUE;
1850 group->first_sta_seen = TRUE;
1851 group->reject_4way_hs_for_entropy = FALSE;
1854 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0 ||
1855 wpa_gtk_update(wpa_auth, group) < 0 ||
1856 wpa_group_config_group_keys(wpa_auth, group) < 0) {
1857 wpa_printf(MSG_INFO, "WPA: GMK/GTK setup failed");
1858 group->first_sta_seen = FALSE;
1859 group->reject_4way_hs_for_entropy = TRUE;
1864 SM_STATE(WPA_PTK, AUTHENTICATION2)
1866 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1868 wpa_group_ensure_init(sm->wpa_auth, sm->group);
1869 sm->ReAuthenticationRequest = FALSE;
1872 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1873 * ambiguous. The Authenticator state machine uses a counter that is
1874 * incremented by one for each 4-way handshake. However, the security
1875 * analysis of 4-way handshake points out that unpredictable nonces
1876 * help in preventing precomputation attacks. Instead of the state
1877 * machine definition, use an unpredictable nonce value here to provide
1878 * stronger protection against potential precomputation attacks.
1880 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1881 wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
1883 sm->Disconnect = TRUE;
1886 wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
1888 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1889 * logical place than INITIALIZE since AUTHENTICATION2 can be
1890 * re-entered on ReAuthenticationRequest without going through
1896 SM_STATE(WPA_PTK, INITPMK)
1898 u8 msk[2 * PMK_LEN];
1899 size_t len = 2 * PMK_LEN;
1901 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1902 #ifdef CONFIG_IEEE80211R
1904 #endif /* CONFIG_IEEE80211R */
1906 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1907 os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
1908 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1909 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1910 "(len=%lu)", (unsigned long) len);
1911 os_memcpy(sm->PMK, msk, PMK_LEN);
1912 #ifdef CONFIG_IEEE80211R
1913 if (len >= 2 * PMK_LEN) {
1914 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1915 sm->xxkey_len = PMK_LEN;
1917 #endif /* CONFIG_IEEE80211R */
1919 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK, get_msk: %p",
1920 sm->wpa_auth->cb.get_msk);
1921 sm->Disconnect = TRUE;
1924 os_memset(msk, 0, sizeof(msk));
1926 sm->req_replay_counter_used = 0;
1927 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1928 * will break reauthentication since EAPOL state machines may not be
1929 * get into AUTHENTICATING state that clears keyRun before WPA state
1930 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1931 * state and takes PMK from the previously used AAA Key. This will
1932 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1933 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1934 * be good workaround for this issue. */
1935 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1939 SM_STATE(WPA_PTK, INITPSK)
1942 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
1943 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL);
1945 os_memcpy(sm->PMK, psk, PMK_LEN);
1946 #ifdef CONFIG_IEEE80211R
1947 os_memcpy(sm->xxkey, psk, PMK_LEN);
1948 sm->xxkey_len = PMK_LEN;
1949 #endif /* CONFIG_IEEE80211R */
1951 sm->req_replay_counter_used = 0;
1955 SM_STATE(WPA_PTK, PTKSTART)
1957 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1958 size_t pmkid_len = 0;
1960 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
1961 sm->PTKRequest = FALSE;
1962 sm->TimeoutEvt = FALSE;
1963 sm->alt_snonce_valid = FALSE;
1966 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1967 /* No point in sending the EAPOL-Key - we will disconnect
1968 * immediately following this. */
1972 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1973 "sending 1/4 msg of 4-Way Handshake");
1975 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1976 * one possible PSK for this STA.
1978 if (sm->wpa == WPA_VERSION_WPA2 &&
1979 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
1980 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
1982 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
1983 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
1984 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
1985 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
1987 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
1988 sm->pmksa->pmkid, PMKID_LEN);
1989 } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) {
1990 /* No KCK available to derive PMKID */
1994 * Calculate PMKID since no PMKSA cache entry was
1995 * available with pre-calculated PMKID.
1997 rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
1998 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
1999 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
2002 wpa_send_eapol(sm->wpa_auth, sm,
2003 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
2004 sm->ANonce, pmkid, pmkid_len, 0, 0);
2008 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
2009 const u8 *pmk, struct wpa_ptk *ptk)
2011 #ifdef CONFIG_IEEE80211R
2012 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
2013 return wpa_auth_derive_ptk_ft(sm, pmk, ptk);
2014 #endif /* CONFIG_IEEE80211R */
2016 return wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
2017 sm->wpa_auth->addr, sm->addr, sm->ANonce, snonce,
2018 ptk, sm->wpa_key_mgmt, sm->pairwise);
2022 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
2025 int ok = 0, psk_found = 0;
2026 const u8 *pmk = NULL;
2028 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
2029 sm->EAPOLKeyReceived = FALSE;
2030 sm->update_snonce = FALSE;
2032 /* WPA with IEEE 802.1X: use the derived PMK from EAP
2033 * WPA-PSK: iterate through possible PSKs and select the one matching
2036 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2037 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
2038 sm->p2p_dev_addr, pmk);
2045 wpa_derive_ptk(sm, sm->SNonce, pmk, &PTK);
2047 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK,
2048 sm->last_rx_eapol_key,
2049 sm->last_rx_eapol_key_len) == 0) {
2054 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
2059 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2060 "invalid MIC in msg 2/4 of 4-Way Handshake");
2062 wpa_auth_psk_failure_report(sm->wpa_auth, sm->addr);
2066 #ifdef CONFIG_IEEE80211R
2067 if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2069 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
2070 * with the value we derived.
2072 if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name,
2073 WPA_PMK_NAME_LEN) != 0) {
2074 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2075 "PMKR1Name mismatch in FT 4-way "
2077 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
2079 sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
2080 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
2081 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
2085 #endif /* CONFIG_IEEE80211R */
2087 sm->pending_1_of_4_timeout = 0;
2088 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
2090 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2091 /* PSK may have changed from the previous choice, so update
2092 * state machine data based on whatever PSK was selected here.
2094 os_memcpy(sm->PMK, pmk, PMK_LEN);
2097 sm->MICVerified = TRUE;
2099 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
2100 sm->PTK_valid = TRUE;
2104 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
2106 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
2111 #ifdef CONFIG_IEEE80211W
2113 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2115 if (sm->mgmt_frame_prot) {
2117 len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2118 return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len;
2125 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2127 struct wpa_igtk_kde igtk;
2128 struct wpa_group *gsm = sm->group;
2129 u8 rsc[WPA_KEY_RSC_LEN];
2130 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2132 if (!sm->mgmt_frame_prot)
2135 igtk.keyid[0] = gsm->GN_igtk;
2137 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
2138 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
2139 os_memset(igtk.pn, 0, sizeof(igtk.pn));
2141 os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
2142 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
2143 if (sm->wpa_auth->conf.disable_gtk) {
2145 * Provide unique random IGTK to each STA to prevent use of
2148 if (random_get_bytes(igtk.igtk, len) < 0)
2151 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
2152 (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
2158 #else /* CONFIG_IEEE80211W */
2160 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2166 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2171 #endif /* CONFIG_IEEE80211W */
2174 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
2176 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
2177 size_t gtk_len, kde_len;
2178 struct wpa_group *gsm = sm->group;
2180 int wpa_ie_len, secure, keyidx, encr = 0;
2182 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
2183 sm->TimeoutEvt = FALSE;
2186 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
2187 /* No point in sending the EAPOL-Key - we will disconnect
2188 * immediately following this. */
2192 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
2193 GTK[GN], IGTK, [FTIE], [TIE * 2])
2195 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2196 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2197 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
2198 wpa_ie = sm->wpa_auth->wpa_ie;
2199 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
2200 if (sm->wpa == WPA_VERSION_WPA &&
2201 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
2202 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
2203 /* WPA-only STA, remove RSN IE and possible MDIE */
2204 wpa_ie = wpa_ie + wpa_ie[1] + 2;
2205 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
2206 wpa_ie = wpa_ie + wpa_ie[1] + 2;
2207 wpa_ie_len = wpa_ie[1] + 2;
2209 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2210 "sending 3/4 msg of 4-Way Handshake");
2211 if (sm->wpa == WPA_VERSION_WPA2) {
2212 /* WPA2 send GTK in the 4-way handshake */
2214 gtk = gsm->GTK[gsm->GN - 1];
2215 gtk_len = gsm->GTK_len;
2216 if (sm->wpa_auth->conf.disable_gtk) {
2218 * Provide unique random GTK to each STA to prevent use
2219 * of GTK in the BSS.
2221 if (random_get_bytes(dummy_gtk, gtk_len) < 0)
2229 /* WPA does not include GTK in msg 3/4 */
2235 if (sm->rx_eapol_key_secure) {
2237 * It looks like Windows 7 supplicant tries to use
2238 * Secure bit in msg 2/4 after having reported Michael
2239 * MIC failure and it then rejects the 4-way handshake
2240 * if msg 3/4 does not set Secure bit. Work around this
2241 * by setting the Secure bit here even in the case of
2242 * WPA if the supplicant used it first.
2244 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2245 "STA used Secure bit in WPA msg 2/4 - "
2246 "set Secure for 3/4 as workaround");
2251 kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
2253 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
2254 #ifdef CONFIG_IEEE80211R
2255 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2256 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
2257 kde_len += 300; /* FTIE + 2 * TIE */
2259 #endif /* CONFIG_IEEE80211R */
2261 if (WPA_GET_BE32(sm->ip_addr) > 0)
2262 kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
2263 #endif /* CONFIG_P2P */
2264 kde = os_malloc(kde_len);
2269 os_memcpy(pos, wpa_ie, wpa_ie_len);
2271 #ifdef CONFIG_IEEE80211R
2272 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2273 int res = wpa_insert_pmkid(kde, pos - kde, sm->pmk_r1_name);
2275 wpa_printf(MSG_ERROR, "FT: Failed to insert "
2276 "PMKR1Name into RSN IE in EAPOL-Key data");
2282 #endif /* CONFIG_IEEE80211R */
2285 hdr[0] = keyidx & 0x03;
2287 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2290 pos = ieee80211w_kde_add(sm, pos);
2292 #ifdef CONFIG_IEEE80211R
2293 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2295 struct wpa_auth_config *conf;
2297 conf = &sm->wpa_auth->conf;
2298 res = wpa_write_ftie(conf, conf->r0_key_holder,
2299 conf->r0_key_holder_len,
2300 NULL, NULL, pos, kde + kde_len - pos,
2303 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
2304 "into EAPOL-Key Key Data");
2310 /* TIE[ReassociationDeadline] (TU) */
2311 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2313 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
2314 WPA_PUT_LE32(pos, conf->reassociation_deadline);
2317 /* TIE[KeyLifetime] (seconds) */
2318 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2320 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
2321 WPA_PUT_LE32(pos, conf->r0_key_lifetime * 60);
2324 #endif /* CONFIG_IEEE80211R */
2326 if (WPA_GET_BE32(sm->ip_addr) > 0) {
2328 os_memcpy(addr, sm->ip_addr, 4);
2329 os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
2330 os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
2331 pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
2332 addr, sizeof(addr), NULL, 0);
2334 #endif /* CONFIG_P2P */
2336 wpa_send_eapol(sm->wpa_auth, sm,
2337 (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
2338 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
2339 WPA_KEY_INFO_KEY_TYPE,
2340 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
2345 SM_STATE(WPA_PTK, PTKINITDONE)
2347 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
2348 sm->EAPOLKeyReceived = FALSE;
2350 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
2351 int klen = wpa_cipher_key_len(sm->pairwise);
2352 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
2353 sm->PTK.tk, klen)) {
2354 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
2357 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
2358 sm->pairwise_set = TRUE;
2360 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
2361 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
2362 eloop_register_timeout(sm->wpa_auth->conf.
2363 wpa_ptk_rekey, 0, wpa_rekey_ptk,
2367 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2368 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2369 WPA_EAPOL_authorized, 1);
2373 if (0 /* IBSS == TRUE */) {
2375 if (sm->keycount == 2) {
2376 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2377 WPA_EAPOL_portValid, 1);
2380 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
2383 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
2384 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
2385 if (sm->wpa == WPA_VERSION_WPA)
2386 sm->PInitAKeys = TRUE;
2389 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2390 "pairwise key handshake completed (%s)",
2391 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2393 #ifdef CONFIG_IEEE80211R
2394 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
2395 #endif /* CONFIG_IEEE80211R */
2401 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2404 SM_ENTER(WPA_PTK, INITIALIZE);
2405 else if (sm->Disconnect
2406 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
2407 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
2408 "WPA_PTK: sm->Disconnect");
2409 SM_ENTER(WPA_PTK, DISCONNECT);
2411 else if (sm->DeauthenticationRequest)
2412 SM_ENTER(WPA_PTK, DISCONNECTED);
2413 else if (sm->AuthenticationRequest)
2414 SM_ENTER(WPA_PTK, AUTHENTICATION);
2415 else if (sm->ReAuthenticationRequest)
2416 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2417 else if (sm->PTKRequest)
2418 SM_ENTER(WPA_PTK, PTKSTART);
2419 else switch (sm->wpa_ptk_state) {
2420 case WPA_PTK_INITIALIZE:
2422 case WPA_PTK_DISCONNECT:
2423 SM_ENTER(WPA_PTK, DISCONNECTED);
2425 case WPA_PTK_DISCONNECTED:
2426 SM_ENTER(WPA_PTK, INITIALIZE);
2428 case WPA_PTK_AUTHENTICATION:
2429 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2431 case WPA_PTK_AUTHENTICATION2:
2432 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
2433 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2434 WPA_EAPOL_keyRun) > 0)
2435 SM_ENTER(WPA_PTK, INITPMK);
2436 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
2437 /* FIX: && 802.1X::keyRun */)
2438 SM_ENTER(WPA_PTK, INITPSK);
2440 case WPA_PTK_INITPMK:
2441 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2442 WPA_EAPOL_keyAvailable) > 0)
2443 SM_ENTER(WPA_PTK, PTKSTART);
2445 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2446 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2447 "INITPMK - keyAvailable = false");
2448 SM_ENTER(WPA_PTK, DISCONNECT);
2451 case WPA_PTK_INITPSK:
2452 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
2454 SM_ENTER(WPA_PTK, PTKSTART);
2456 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2457 "no PSK configured for the STA");
2458 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2459 SM_ENTER(WPA_PTK, DISCONNECT);
2462 case WPA_PTK_PTKSTART:
2463 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2464 sm->EAPOLKeyPairwise)
2465 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2466 else if (sm->TimeoutCtr >
2467 (int) dot11RSNAConfigPairwiseUpdateCount) {
2468 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2469 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2470 "PTKSTART: Retry limit %d reached",
2471 dot11RSNAConfigPairwiseUpdateCount);
2472 SM_ENTER(WPA_PTK, DISCONNECT);
2473 } else if (sm->TimeoutEvt)
2474 SM_ENTER(WPA_PTK, PTKSTART);
2476 case WPA_PTK_PTKCALCNEGOTIATING:
2477 if (sm->MICVerified)
2478 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
2479 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2480 sm->EAPOLKeyPairwise)
2481 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2482 else if (sm->TimeoutEvt)
2483 SM_ENTER(WPA_PTK, PTKSTART);
2485 case WPA_PTK_PTKCALCNEGOTIATING2:
2486 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2488 case WPA_PTK_PTKINITNEGOTIATING:
2489 if (sm->update_snonce)
2490 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2491 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2492 sm->EAPOLKeyPairwise && sm->MICVerified)
2493 SM_ENTER(WPA_PTK, PTKINITDONE);
2494 else if (sm->TimeoutCtr >
2495 (int) dot11RSNAConfigPairwiseUpdateCount) {
2496 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2497 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2498 "PTKINITNEGOTIATING: Retry limit %d "
2500 dot11RSNAConfigPairwiseUpdateCount);
2501 SM_ENTER(WPA_PTK, DISCONNECT);
2502 } else if (sm->TimeoutEvt)
2503 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2505 case WPA_PTK_PTKINITDONE:
2511 SM_STATE(WPA_PTK_GROUP, IDLE)
2513 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
2515 /* Init flag is not cleared here, so avoid busy
2516 * loop by claiming nothing changed. */
2517 sm->changed = FALSE;
2519 sm->GTimeoutCtr = 0;
2523 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
2525 u8 rsc[WPA_KEY_RSC_LEN];
2526 struct wpa_group *gsm = sm->group;
2528 u8 *kde_buf = NULL, *pos, hdr[2];
2530 u8 *gtk, dummy_gtk[32];
2532 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
2535 if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
2536 /* No point in sending the EAPOL-Key - we will disconnect
2537 * immediately following this. */
2541 if (sm->wpa == WPA_VERSION_WPA)
2542 sm->PInitAKeys = FALSE;
2543 sm->TimeoutEvt = FALSE;
2544 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
2545 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2546 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
2547 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2548 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2549 "sending 1/2 msg of Group Key Handshake");
2551 gtk = gsm->GTK[gsm->GN - 1];
2552 if (sm->wpa_auth->conf.disable_gtk) {
2554 * Provide unique random GTK to each STA to prevent use
2555 * of GTK in the BSS.
2557 if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
2561 if (sm->wpa == WPA_VERSION_WPA2) {
2562 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
2563 ieee80211w_kde_len(sm);
2564 kde_buf = os_malloc(kde_len);
2565 if (kde_buf == NULL)
2568 kde = pos = kde_buf;
2569 hdr[0] = gsm->GN & 0x03;
2571 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2573 pos = ieee80211w_kde_add(sm, pos);
2574 kde_len = pos - kde;
2577 kde_len = gsm->GTK_len;
2580 wpa_send_eapol(sm->wpa_auth, sm,
2581 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
2583 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
2584 rsc, gsm->GNonce, kde, kde_len, gsm->GN, 1);
2590 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
2592 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
2593 sm->EAPOLKeyReceived = FALSE;
2594 if (sm->GUpdateStationKeys)
2595 sm->group->GKeyDoneStations--;
2596 sm->GUpdateStationKeys = FALSE;
2597 sm->GTimeoutCtr = 0;
2598 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
2599 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2600 "group key handshake completed (%s)",
2601 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2606 SM_STATE(WPA_PTK_GROUP, KEYERROR)
2608 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
2609 if (sm->GUpdateStationKeys)
2610 sm->group->GKeyDoneStations--;
2611 sm->GUpdateStationKeys = FALSE;
2612 sm->Disconnect = TRUE;
2616 SM_STEP(WPA_PTK_GROUP)
2618 if (sm->Init || sm->PtkGroupInit) {
2619 SM_ENTER(WPA_PTK_GROUP, IDLE);
2620 sm->PtkGroupInit = FALSE;
2621 } else switch (sm->wpa_ptk_group_state) {
2622 case WPA_PTK_GROUP_IDLE:
2623 if (sm->GUpdateStationKeys ||
2624 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
2625 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2627 case WPA_PTK_GROUP_REKEYNEGOTIATING:
2628 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2629 !sm->EAPOLKeyPairwise && sm->MICVerified)
2630 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
2631 else if (sm->GTimeoutCtr >
2632 (int) dot11RSNAConfigGroupUpdateCount)
2633 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
2634 else if (sm->TimeoutEvt)
2635 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2637 case WPA_PTK_GROUP_KEYERROR:
2638 SM_ENTER(WPA_PTK_GROUP, IDLE);
2640 case WPA_PTK_GROUP_REKEYESTABLISHED:
2641 SM_ENTER(WPA_PTK_GROUP, IDLE);
2647 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
2648 struct wpa_group *group)
2652 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
2653 inc_byte_array(group->Counter, WPA_NONCE_LEN);
2654 if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
2655 wpa_auth->addr, group->GNonce,
2656 group->GTK[group->GN - 1], group->GTK_len) < 0)
2658 wpa_hexdump_key(MSG_DEBUG, "GTK",
2659 group->GTK[group->GN - 1], group->GTK_len);
2661 #ifdef CONFIG_IEEE80211W
2662 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
2664 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
2665 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
2666 inc_byte_array(group->Counter, WPA_NONCE_LEN);
2667 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
2668 wpa_auth->addr, group->GNonce,
2669 group->IGTK[group->GN_igtk - 4], len) < 0)
2671 wpa_hexdump_key(MSG_DEBUG, "IGTK",
2672 group->IGTK[group->GN_igtk - 4], len);
2674 #endif /* CONFIG_IEEE80211W */
2680 static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
2681 struct wpa_group *group)
2683 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2684 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
2685 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
2686 group->wpa_group_state = WPA_GROUP_GTK_INIT;
2689 os_memset(group->GTK, 0, sizeof(group->GTK));
2692 #ifdef CONFIG_IEEE80211W
2695 #endif /* CONFIG_IEEE80211W */
2696 /* GTK[GN] = CalcGTK() */
2697 wpa_gtk_update(wpa_auth, group);
2701 static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
2703 if (ctx != NULL && ctx != sm->group)
2706 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
2707 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2708 "Not in PTKINITDONE; skip Group Key update");
2709 sm->GUpdateStationKeys = FALSE;
2712 if (sm->GUpdateStationKeys) {
2714 * This should not really happen, so add a debug log entry.
2715 * Since we clear the GKeyDoneStations before the loop, the
2716 * station needs to be counted here anyway.
2718 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2719 "GUpdateStationKeys was already set when "
2720 "marking station for GTK rekeying");
2723 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
2724 if (sm->is_wnmsleep)
2727 sm->group->GKeyDoneStations++;
2728 sm->GUpdateStationKeys = TRUE;
2736 /* update GTK when exiting WNM-Sleep Mode */
2737 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
2739 if (sm == NULL || sm->is_wnmsleep)
2742 wpa_group_update_sta(sm, NULL);
2746 void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
2749 sm->is_wnmsleep = !!flag;
2753 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
2755 struct wpa_group *gsm = sm->group;
2760 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
2763 *pos++ = WNM_SLEEP_SUBELEM_GTK;
2764 *pos++ = 11 + gsm->GTK_len;
2765 /* Key ID in B0-B1 of Key Info */
2766 WPA_PUT_LE16(pos, gsm->GN & 0x03);
2768 *pos++ = gsm->GTK_len;
2769 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
2772 os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
2773 pos += gsm->GTK_len;
2775 wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
2777 wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
2778 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
2784 #ifdef CONFIG_IEEE80211W
2785 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
2787 struct wpa_group *gsm = sm->group;
2789 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2793 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
2795 *pos++ = WNM_SLEEP_SUBELEM_IGTK;
2796 *pos++ = 2 + 6 + len;
2797 WPA_PUT_LE16(pos, gsm->GN_igtk);
2799 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
2803 os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
2806 wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
2808 wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
2809 gsm->IGTK[gsm->GN_igtk - 4], len);
2813 #endif /* CONFIG_IEEE80211W */
2814 #endif /* CONFIG_WNM */
2817 static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
2818 struct wpa_group *group)
2822 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2823 "SETKEYS (VLAN-ID %d)", group->vlan_id);
2824 group->changed = TRUE;
2825 group->wpa_group_state = WPA_GROUP_SETKEYS;
2826 group->GTKReKey = FALSE;
2828 group->GM = group->GN;
2830 #ifdef CONFIG_IEEE80211W
2831 tmp = group->GM_igtk;
2832 group->GM_igtk = group->GN_igtk;
2833 group->GN_igtk = tmp;
2834 #endif /* CONFIG_IEEE80211W */
2835 /* "GKeyDoneStations = GNoStations" is done in more robust way by
2836 * counting the STAs that are marked with GUpdateStationKeys instead of
2837 * including all STAs that could be in not-yet-completed state. */
2838 wpa_gtk_update(wpa_auth, group);
2840 if (group->GKeyDoneStations) {
2841 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
2842 "GKeyDoneStations=%d when starting new GTK rekey",
2843 group->GKeyDoneStations);
2844 group->GKeyDoneStations = 0;
2846 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
2847 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
2848 group->GKeyDoneStations);
2852 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
2853 struct wpa_group *group)
2857 if (wpa_auth_set_key(wpa_auth, group->vlan_id,
2858 wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
2859 broadcast_ether_addr, group->GN,
2860 group->GTK[group->GN - 1], group->GTK_len) < 0)
2863 #ifdef CONFIG_IEEE80211W
2864 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
2868 alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher);
2869 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
2872 wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
2873 broadcast_ether_addr, group->GN_igtk,
2874 group->IGTK[group->GN_igtk - 4], len) < 0)
2877 #endif /* CONFIG_IEEE80211W */
2883 static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
2885 if (sm->group == ctx) {
2886 wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
2887 " for discconnection due to fatal failure",
2889 sm->Disconnect = TRUE;
2896 static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
2897 struct wpa_group *group)
2899 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
2900 group->changed = TRUE;
2901 group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
2902 wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
2906 static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
2907 struct wpa_group *group)
2909 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2910 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
2911 group->changed = TRUE;
2912 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
2914 if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
2915 wpa_group_fatal_failure(wpa_auth, group);
2923 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
2924 struct wpa_group *group)
2927 wpa_group_gtk_init(wpa_auth, group);
2928 } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
2929 /* Do not allow group operations */
2930 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
2931 group->GTKAuthenticator) {
2932 wpa_group_setkeysdone(wpa_auth, group);
2933 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
2935 wpa_group_setkeys(wpa_auth, group);
2936 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
2937 if (group->GKeyDoneStations == 0)
2938 wpa_group_setkeysdone(wpa_auth, group);
2939 else if (group->GTKReKey)
2940 wpa_group_setkeys(wpa_auth, group);
2945 static int wpa_sm_step(struct wpa_state_machine *sm)
2950 if (sm->in_step_loop) {
2951 /* This should not happen, but if it does, make sure we do not
2952 * end up freeing the state machine too early by exiting the
2953 * recursive call. */
2954 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
2958 sm->in_step_loop = 1;
2960 if (sm->pending_deinit)
2963 sm->changed = FALSE;
2964 sm->wpa_auth->group->changed = FALSE;
2966 SM_STEP_RUN(WPA_PTK);
2967 if (sm->pending_deinit)
2969 SM_STEP_RUN(WPA_PTK_GROUP);
2970 if (sm->pending_deinit)
2972 wpa_group_sm_step(sm->wpa_auth, sm->group);
2973 } while (sm->changed || sm->wpa_auth->group->changed);
2974 sm->in_step_loop = 0;
2976 if (sm->pending_deinit) {
2977 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
2978 "machine deinit for " MACSTR, MAC2STR(sm->addr));
2979 wpa_free_sta_sm(sm);
2986 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
2988 struct wpa_state_machine *sm = eloop_ctx;
2993 void wpa_auth_sm_notify(struct wpa_state_machine *sm)
2997 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
3001 void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
3004 struct wpa_group *group;
3006 if (wpa_auth == NULL)
3009 group = wpa_auth->group;
3011 for (i = 0; i < 2; i++) {
3013 group->GM = group->GN;
3015 #ifdef CONFIG_IEEE80211W
3016 tmp = group->GM_igtk;
3017 group->GM_igtk = group->GN_igtk;
3018 group->GN_igtk = tmp;
3019 #endif /* CONFIG_IEEE80211W */
3020 wpa_gtk_update(wpa_auth, group);
3021 wpa_group_config_group_keys(wpa_auth, group);
3026 static const char * wpa_bool_txt(int val)
3028 return val ? "TRUE" : "FALSE";
3032 #define RSN_SUITE "%02x-%02x-%02x-%d"
3033 #define RSN_SUITE_ARG(s) \
3034 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
3036 int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
3039 char pmkid_txt[PMKID_LEN * 2 + 1];
3040 #ifdef CONFIG_RSN_PREAUTH
3041 const int preauth = 1;
3042 #else /* CONFIG_RSN_PREAUTH */
3043 const int preauth = 0;
3044 #endif /* CONFIG_RSN_PREAUTH */
3046 if (wpa_auth == NULL)
3049 ret = os_snprintf(buf + len, buflen - len,
3050 "dot11RSNAOptionImplemented=TRUE\n"
3051 "dot11RSNAPreauthenticationImplemented=%s\n"
3052 "dot11RSNAEnabled=%s\n"
3053 "dot11RSNAPreauthenticationEnabled=%s\n",
3054 wpa_bool_txt(preauth),
3055 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
3056 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
3057 if (os_snprintf_error(buflen - len, ret))
3061 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
3062 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
3065 buf + len, buflen - len,
3066 "dot11RSNAConfigVersion=%u\n"
3067 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
3068 /* FIX: dot11RSNAConfigGroupCipher */
3069 /* FIX: dot11RSNAConfigGroupRekeyMethod */
3070 /* FIX: dot11RSNAConfigGroupRekeyTime */
3071 /* FIX: dot11RSNAConfigGroupRekeyPackets */
3072 "dot11RSNAConfigGroupRekeyStrict=%u\n"
3073 "dot11RSNAConfigGroupUpdateCount=%u\n"
3074 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
3075 "dot11RSNAConfigGroupCipherSize=%u\n"
3076 "dot11RSNAConfigPMKLifetime=%u\n"
3077 "dot11RSNAConfigPMKReauthThreshold=%u\n"
3078 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
3079 "dot11RSNAConfigSATimeout=%u\n"
3080 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
3081 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
3082 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
3083 "dot11RSNAPMKIDUsed=%s\n"
3084 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
3085 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
3086 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
3087 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
3088 "dot11RSNA4WayHandshakeFailures=%u\n"
3089 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
3091 !!wpa_auth->conf.wpa_strict_rekey,
3092 dot11RSNAConfigGroupUpdateCount,
3093 dot11RSNAConfigPairwiseUpdateCount,
3094 wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
3095 dot11RSNAConfigPMKLifetime,
3096 dot11RSNAConfigPMKReauthThreshold,
3097 dot11RSNAConfigSATimeout,
3098 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
3099 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
3100 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
3102 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
3103 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
3104 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
3105 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
3106 wpa_auth->dot11RSNA4WayHandshakeFailures);
3107 if (os_snprintf_error(buflen - len, ret))
3111 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
3112 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
3115 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
3116 wpa_auth->group->wpa_group_state);
3117 if (os_snprintf_error(buflen - len, ret))
3125 int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
3133 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
3135 /* dot11RSNAStatsEntry */
3137 pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
3138 WPA_PROTO_RSN : WPA_PROTO_WPA,
3144 buf + len, buflen - len,
3145 /* TODO: dot11RSNAStatsIndex */
3146 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
3147 "dot11RSNAStatsVersion=1\n"
3148 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
3149 /* TODO: dot11RSNAStatsTKIPICVErrors */
3150 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
3151 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
3152 /* TODO: dot11RSNAStatsCCMPReplays */
3153 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
3154 /* TODO: dot11RSNAStatsTKIPReplays */,
3156 RSN_SUITE_ARG(pairwise),
3157 sm->dot11RSNAStatsTKIPLocalMICFailures,
3158 sm->dot11RSNAStatsTKIPRemoteMICFailures);
3159 if (os_snprintf_error(buflen - len, ret))
3164 ret = os_snprintf(buf + len, buflen - len,
3165 "hostapdWPAPTKState=%d\n"
3166 "hostapdWPAPTKGroupState=%d\n",
3168 sm->wpa_ptk_group_state);
3169 if (os_snprintf_error(buflen - len, ret))
3177 void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
3180 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
3184 int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
3186 return sm && sm->pairwise_set;
3190 int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
3192 return sm->pairwise;
3196 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
3200 return sm->wpa_key_mgmt;
3204 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
3212 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
3213 struct rsn_pmksa_cache_entry *entry)
3215 if (sm == NULL || sm->pmksa != entry)
3222 struct rsn_pmksa_cache_entry *
3223 wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
3225 return sm ? sm->pmksa : NULL;
3229 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
3232 sm->dot11RSNAStatsTKIPLocalMICFailures++;
3236 const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
3238 if (wpa_auth == NULL)
3240 *len = wpa_auth->wpa_ie_len;
3241 return wpa_auth->wpa_ie;
3245 int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
3246 int session_timeout, struct eapol_state_machine *eapol)
3248 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
3249 sm->wpa_auth->conf.disable_pmksa_caching)
3252 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
3253 sm->PTK.kck, sm->PTK.kck_len,
3254 sm->wpa_auth->addr, sm->addr, session_timeout,
3255 eapol, sm->wpa_key_mgmt))
3262 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
3263 const u8 *pmk, size_t len, const u8 *sta_addr,
3264 int session_timeout,
3265 struct eapol_state_machine *eapol)
3267 if (wpa_auth == NULL)
3270 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len,
3273 sta_addr, session_timeout, eapol,
3274 WPA_KEY_MGMT_IEEE8021X))
3281 int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
3284 if (wpa_auth->conf.disable_pmksa_caching)
3287 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN,
3289 wpa_auth->addr, addr, 0, NULL,
3297 void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
3300 struct rsn_pmksa_cache_entry *pmksa;
3302 if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
3304 pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
3306 wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
3307 MACSTR " based on request", MAC2STR(sta_addr));
3308 pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
3314 * Remove and free the group from wpa_authenticator. This is triggered by a
3315 * callback to make sure nobody is currently iterating the group list while it
3318 static void wpa_group_free(struct wpa_authenticator *wpa_auth,
3319 struct wpa_group *group)
3321 struct wpa_group *prev = wpa_auth->group;
3323 wpa_printf(MSG_DEBUG, "WPA: Remove group state machine for VLAN-ID %d",
3327 if (prev->next == group) {
3328 /* This never frees the special first group as needed */
3329 prev->next = group->next;
3339 /* Increase the reference counter for group */
3340 static void wpa_group_get(struct wpa_authenticator *wpa_auth,
3341 struct wpa_group *group)
3343 /* Skip the special first group */
3344 if (wpa_auth->group == group)
3347 group->references++;
3351 /* Decrease the reference counter and maybe free the group */
3352 static void wpa_group_put(struct wpa_authenticator *wpa_auth,
3353 struct wpa_group *group)
3355 /* Skip the special first group */
3356 if (wpa_auth->group == group)
3359 group->references--;
3360 if (group->references)
3362 wpa_group_free(wpa_auth, group);
3367 * Add a group that has its references counter set to zero. Caller needs to
3368 * call wpa_group_get() on the return value to mark the entry in use.
3370 static struct wpa_group *
3371 wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
3373 struct wpa_group *group;
3375 if (wpa_auth == NULL || wpa_auth->group == NULL)
3378 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
3380 group = wpa_group_init(wpa_auth, vlan_id, 0);
3384 group->next = wpa_auth->group->next;
3385 wpa_auth->group->next = group;
3391 int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
3393 struct wpa_group *group;
3395 if (sm == NULL || sm->wpa_auth == NULL)
3398 group = sm->wpa_auth->group;
3400 if (group->vlan_id == vlan_id)
3402 group = group->next;
3405 if (group == NULL) {
3406 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
3411 if (sm->group == group)
3414 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
3417 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
3418 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
3420 wpa_group_get(sm->wpa_auth, group);
3421 wpa_group_put(sm->wpa_auth, sm->group);
3428 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
3429 struct wpa_state_machine *sm, int ack)
3431 if (wpa_auth == NULL || sm == NULL)
3433 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
3434 " ack=%d", MAC2STR(sm->addr), ack);
3435 if (sm->pending_1_of_4_timeout && ack) {
3437 * Some deployed supplicant implementations update their SNonce
3438 * for each EAPOL-Key 2/4 message even within the same 4-way
3439 * handshake and then fail to use the first SNonce when
3440 * deriving the PTK. This results in unsuccessful 4-way
3441 * handshake whenever the relatively short initial timeout is
3442 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
3443 * around this by increasing the timeout now that we know that
3444 * the station has received the frame.
3446 int timeout_ms = eapol_key_timeout_subseq;
3447 wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
3448 "timeout by %u ms because of acknowledged frame",
3450 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
3451 eloop_register_timeout(timeout_ms / 1000,
3452 (timeout_ms % 1000) * 1000,
3453 wpa_send_eapol_timeout, wpa_auth, sm);
3458 int wpa_auth_uses_sae(struct wpa_state_machine *sm)
3462 return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
3466 int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
3470 return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
3475 int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
3477 if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
3479 os_memcpy(addr, sm->ip_addr, 4);
3482 #endif /* CONFIG_P2P */
3485 int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth,
3486 struct radius_das_attrs *attr)
3488 return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr);
3492 void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth)
3494 struct wpa_group *group;
3498 for (group = wpa_auth->group; group; group = group->next)
3499 wpa_group_config_group_keys(wpa_auth, group);