2 * IEEE 802.11 RSN / WPA Authenticator
3 * Copyright (c) 2004-2013, 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(struct wpa_ptk *PTK, u8 *data, size_t data_len);
37 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
38 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
39 struct wpa_group *group);
40 static void wpa_request_new_ptk(struct wpa_state_machine *sm);
41 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
42 struct wpa_group *group);
43 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
44 struct wpa_group *group);
46 static const u32 dot11RSNAConfigGroupUpdateCount = 4;
47 static const u32 dot11RSNAConfigPairwiseUpdateCount = 4;
48 static const u32 eapol_key_timeout_first = 100; /* ms */
49 static const u32 eapol_key_timeout_subseq = 1000; /* ms */
50 static const u32 eapol_key_timeout_first_group = 500; /* ms */
52 /* TODO: make these configurable */
53 static const int dot11RSNAConfigPMKLifetime = 43200;
54 static const int dot11RSNAConfigPMKReauthThreshold = 70;
55 static const int dot11RSNAConfigSATimeout = 60;
58 static inline int wpa_auth_mic_failure_report(
59 struct wpa_authenticator *wpa_auth, const u8 *addr)
61 if (wpa_auth->cb.mic_failure_report)
62 return wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr);
67 static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
68 const u8 *addr, wpa_eapol_variable var,
71 if (wpa_auth->cb.set_eapol)
72 wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value);
76 static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
77 const u8 *addr, wpa_eapol_variable var)
79 if (wpa_auth->cb.get_eapol == NULL)
81 return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var);
85 static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
87 const u8 *p2p_dev_addr,
90 if (wpa_auth->cb.get_psk == NULL)
92 return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, p2p_dev_addr,
97 static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
98 const u8 *addr, u8 *msk, size_t *len)
100 if (wpa_auth->cb.get_msk == NULL)
102 return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len);
106 static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
108 enum wpa_alg alg, const u8 *addr, int idx,
109 u8 *key, size_t key_len)
111 if (wpa_auth->cb.set_key == NULL)
113 return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
118 static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
119 const u8 *addr, int idx, u8 *seq)
121 if (wpa_auth->cb.get_seqnum == NULL)
123 return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
128 wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
129 const u8 *data, size_t data_len, int encrypt)
131 if (wpa_auth->cb.send_eapol == NULL)
133 return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
138 int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
139 int (*cb)(struct wpa_state_machine *sm, void *ctx),
142 if (wpa_auth->cb.for_each_sta == NULL)
144 return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
148 int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
149 int (*cb)(struct wpa_authenticator *a, void *ctx),
152 if (wpa_auth->cb.for_each_auth == NULL)
154 return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
158 void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
159 logger_level level, const char *txt)
161 if (wpa_auth->cb.logger == NULL)
163 wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
167 void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
168 logger_level level, const char *fmt, ...)
174 if (wpa_auth->cb.logger == NULL)
177 maxlen = os_strlen(fmt) + 100;
178 format = os_malloc(maxlen);
183 vsnprintf(format, maxlen, fmt, ap);
186 wpa_auth_logger(wpa_auth, addr, level, format);
192 static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
195 if (wpa_auth->cb.disconnect == NULL)
197 wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR, MAC2STR(addr));
198 wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
199 WLAN_REASON_PREV_AUTH_NOT_VALID);
203 static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
206 #ifdef CONFIG_IEEE80211R
207 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
209 #endif /* CONFIG_IEEE80211R */
210 #ifdef CONFIG_IEEE80211W
211 if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
213 #endif /* CONFIG_IEEE80211W */
214 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN)
220 static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
222 struct wpa_authenticator *wpa_auth = eloop_ctx;
224 if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
225 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
228 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
229 wpa_hexdump_key(MSG_DEBUG, "GMK",
230 wpa_auth->group->GMK, WPA_GMK_LEN);
233 if (wpa_auth->conf.wpa_gmk_rekey) {
234 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
235 wpa_rekey_gmk, wpa_auth, NULL);
240 static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
242 struct wpa_authenticator *wpa_auth = eloop_ctx;
243 struct wpa_group *group;
245 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
246 for (group = wpa_auth->group; group; group = group->next) {
247 group->GTKReKey = TRUE;
249 group->changed = FALSE;
250 wpa_group_sm_step(wpa_auth, group);
251 } while (group->changed);
254 if (wpa_auth->conf.wpa_group_rekey) {
255 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
256 0, wpa_rekey_gtk, wpa_auth, NULL);
261 static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
263 struct wpa_authenticator *wpa_auth = eloop_ctx;
264 struct wpa_state_machine *sm = timeout_ctx;
266 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
267 wpa_request_new_ptk(sm);
272 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
274 if (sm->pmksa == ctx)
280 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
283 struct wpa_authenticator *wpa_auth = ctx;
284 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
288 static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
289 struct wpa_group *group)
291 u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
295 if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
297 wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
300 * Counter = PRF-256(Random number, "Init Counter",
301 * Local MAC Address || Time)
303 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
304 wpa_get_ntp_timestamp(buf + ETH_ALEN);
305 ptr = (unsigned long) group;
306 os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
307 if (random_get_bytes(rkey, sizeof(rkey)) < 0)
310 if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
311 group->Counter, WPA_NONCE_LEN) < 0)
313 wpa_hexdump_key(MSG_DEBUG, "Key Counter",
314 group->Counter, WPA_NONCE_LEN);
320 static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
321 int vlan_id, int delay_init)
323 struct wpa_group *group;
325 group = os_zalloc(sizeof(struct wpa_group));
329 group->GTKAuthenticator = TRUE;
330 group->vlan_id = vlan_id;
331 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
333 if (random_pool_ready() != 1) {
334 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
335 "for secure operations - update keys later when "
336 "the first station connects");
340 * Set initial GMK/Counter value here. The actual values that will be
341 * used in negotiations will be set once the first station tries to
342 * connect. This allows more time for collecting additional randomness
343 * on embedded devices.
345 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
346 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
354 wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start "
355 "until Beacon frames have been configured");
356 /* Initialization is completed in wpa_init_keys(). */
358 wpa_group_sm_step(wpa_auth, group);
359 group->GInit = FALSE;
360 wpa_group_sm_step(wpa_auth, group);
368 * wpa_init - Initialize WPA authenticator
369 * @addr: Authenticator address
370 * @conf: Configuration for WPA authenticator
371 * @cb: Callback functions for WPA authenticator
372 * Returns: Pointer to WPA authenticator data or %NULL on failure
374 struct wpa_authenticator * wpa_init(const u8 *addr,
375 struct wpa_auth_config *conf,
376 struct wpa_auth_callbacks *cb)
378 struct wpa_authenticator *wpa_auth;
380 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
381 if (wpa_auth == NULL)
383 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
384 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
385 os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
387 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
388 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
393 wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
394 if (wpa_auth->group == NULL) {
395 os_free(wpa_auth->wpa_ie);
400 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
402 if (wpa_auth->pmksa == NULL) {
403 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
404 os_free(wpa_auth->wpa_ie);
409 #ifdef CONFIG_IEEE80211R
410 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
411 if (wpa_auth->ft_pmk_cache == NULL) {
412 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
413 os_free(wpa_auth->wpa_ie);
414 pmksa_cache_auth_deinit(wpa_auth->pmksa);
418 #endif /* CONFIG_IEEE80211R */
420 if (wpa_auth->conf.wpa_gmk_rekey) {
421 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
422 wpa_rekey_gmk, wpa_auth, NULL);
425 if (wpa_auth->conf.wpa_group_rekey) {
426 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
427 wpa_rekey_gtk, wpa_auth, NULL);
431 if (WPA_GET_BE32(conf->ip_addr_start)) {
432 int count = WPA_GET_BE32(conf->ip_addr_end) -
433 WPA_GET_BE32(conf->ip_addr_start) + 1;
437 wpa_auth->ip_pool = bitfield_alloc(count);
439 #endif /* CONFIG_P2P */
445 int wpa_init_keys(struct wpa_authenticator *wpa_auth)
447 struct wpa_group *group = wpa_auth->group;
449 wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial "
451 wpa_group_sm_step(wpa_auth, group);
452 group->GInit = FALSE;
453 wpa_group_sm_step(wpa_auth, group);
454 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
461 * wpa_deinit - Deinitialize WPA authenticator
462 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
464 void wpa_deinit(struct wpa_authenticator *wpa_auth)
466 struct wpa_group *group, *prev;
468 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
469 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
471 #ifdef CONFIG_PEERKEY
472 while (wpa_auth->stsl_negotiations)
473 wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
474 #endif /* CONFIG_PEERKEY */
476 pmksa_cache_auth_deinit(wpa_auth->pmksa);
478 #ifdef CONFIG_IEEE80211R
479 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
480 wpa_auth->ft_pmk_cache = NULL;
481 #endif /* CONFIG_IEEE80211R */
484 bitfield_free(wpa_auth->ip_pool);
485 #endif /* CONFIG_P2P */
488 os_free(wpa_auth->wpa_ie);
490 group = wpa_auth->group;
502 * wpa_reconfig - Update WPA authenticator configuration
503 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
504 * @conf: Configuration for WPA authenticator
506 int wpa_reconfig(struct wpa_authenticator *wpa_auth,
507 struct wpa_auth_config *conf)
509 struct wpa_group *group;
510 if (wpa_auth == NULL)
513 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
514 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
515 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
520 * Reinitialize GTK to make sure it is suitable for the new
523 group = wpa_auth->group;
524 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
526 wpa_group_sm_step(wpa_auth, group);
527 group->GInit = FALSE;
528 wpa_group_sm_step(wpa_auth, group);
534 struct wpa_state_machine *
535 wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
536 const u8 *p2p_dev_addr)
538 struct wpa_state_machine *sm;
540 if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
543 sm = os_zalloc(sizeof(struct wpa_state_machine));
546 os_memcpy(sm->addr, addr, ETH_ALEN);
548 os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
550 sm->wpa_auth = wpa_auth;
551 sm->group = wpa_auth->group;
557 int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
558 struct wpa_state_machine *sm)
560 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
563 #ifdef CONFIG_IEEE80211R
564 if (sm->ft_completed) {
565 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
566 "FT authentication already completed - do not "
567 "start 4-way handshake");
568 /* Go to PTKINITDONE state to allow GTK rekeying */
569 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
572 #endif /* CONFIG_IEEE80211R */
575 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
576 sm->ReAuthenticationRequest = TRUE;
577 return wpa_sm_step(sm);
580 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
581 "start authentication");
585 if (wpa_sm_step(sm) == 1)
586 return 1; /* should not really happen */
588 sm->AuthenticationRequest = TRUE;
589 return wpa_sm_step(sm);
593 void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
595 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
596 * reassociates back to the same AP while the previous entry for the
597 * STA has not yet been removed. */
601 sm->wpa_key_mgmt = 0;
605 static void wpa_free_sta_sm(struct wpa_state_machine *sm)
608 if (WPA_GET_BE32(sm->ip_addr)) {
610 wpa_printf(MSG_DEBUG, "P2P: Free assigned IP "
611 "address %u.%u.%u.%u from " MACSTR,
612 sm->ip_addr[0], sm->ip_addr[1],
613 sm->ip_addr[2], sm->ip_addr[3],
615 start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start);
616 bitfield_clear(sm->wpa_auth->ip_pool,
617 WPA_GET_BE32(sm->ip_addr) - start);
619 #endif /* CONFIG_P2P */
620 if (sm->GUpdateStationKeys) {
621 sm->group->GKeyDoneStations--;
622 sm->GUpdateStationKeys = FALSE;
624 #ifdef CONFIG_IEEE80211R
625 os_free(sm->assoc_resp_ftie);
626 wpabuf_free(sm->ft_pending_req_ies);
627 #endif /* CONFIG_IEEE80211R */
628 os_free(sm->last_rx_eapol_key);
634 void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
639 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
640 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
641 "strict rekeying - force GTK rekey since STA "
643 eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
644 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
648 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
649 sm->pending_1_of_4_timeout = 0;
650 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
651 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
652 if (sm->in_step_loop) {
653 /* Must not free state machine while wpa_sm_step() is running.
654 * Freeing will be completed in the end of wpa_sm_step(). */
655 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
656 "machine deinit for " MACSTR, MAC2STR(sm->addr));
657 sm->pending_deinit = 1;
663 static void wpa_request_new_ptk(struct wpa_state_machine *sm)
668 sm->PTKRequest = TRUE;
673 static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
674 const u8 *replay_counter)
677 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
680 if (os_memcmp(replay_counter, ctr[i].counter,
681 WPA_REPLAY_COUNTER_LEN) == 0)
688 static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
689 const u8 *replay_counter)
692 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
694 (replay_counter == NULL ||
695 os_memcmp(replay_counter, ctr[i].counter,
696 WPA_REPLAY_COUNTER_LEN) == 0))
697 ctr[i].valid = FALSE;
702 #ifdef CONFIG_IEEE80211R
703 static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
704 struct wpa_state_machine *sm,
705 struct wpa_eapol_ie_parse *kde)
707 struct wpa_ie_data ie;
708 struct rsn_mdie *mdie;
710 if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
711 ie.num_pmkid != 1 || ie.pmkid == NULL) {
712 wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
713 "FT 4-way handshake message 2/4");
717 os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
718 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
719 sm->sup_pmk_r1_name, PMKID_LEN);
721 if (!kde->mdie || !kde->ftie) {
722 wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
723 "message 2/4", kde->mdie ? "FTIE" : "MDIE");
727 mdie = (struct rsn_mdie *) (kde->mdie + 2);
728 if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
729 os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
730 MOBILITY_DOMAIN_ID_LEN) != 0) {
731 wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
735 if (sm->assoc_resp_ftie &&
736 (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
737 os_memcmp(kde->ftie, sm->assoc_resp_ftie,
738 2 + sm->assoc_resp_ftie[1]) != 0)) {
739 wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
740 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
741 kde->ftie, kde->ftie_len);
742 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
743 sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
749 #endif /* CONFIG_IEEE80211R */
752 static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
753 struct wpa_state_machine *sm, int group)
755 /* Supplicant reported a Michael MIC error */
756 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
757 "received EAPOL-Key Error Request "
758 "(STA detected Michael MIC failure (group=%d))",
761 if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
762 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
763 "ignore Michael MIC failure report since "
764 "group cipher is not TKIP");
765 } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
766 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
767 "ignore Michael MIC failure report since "
768 "pairwise cipher is not TKIP");
770 if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
771 return 1; /* STA entry was removed */
772 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
773 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
777 * Error report is not a request for a new key handshake, but since
778 * Authenticator may do it, let's change the keys now anyway.
780 wpa_request_new_ptk(sm);
785 void wpa_receive(struct wpa_authenticator *wpa_auth,
786 struct wpa_state_machine *sm,
787 u8 *data, size_t data_len)
789 struct ieee802_1x_hdr *hdr;
790 struct wpa_eapol_key *key;
791 u16 key_info, key_data_length;
792 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
793 SMK_M1, SMK_M3, SMK_ERROR } msg;
795 struct wpa_eapol_ie_parse kde;
797 const u8 *eapol_key_ie;
798 size_t eapol_key_ie_len;
800 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
803 if (data_len < sizeof(*hdr) + sizeof(*key))
806 hdr = (struct ieee802_1x_hdr *) data;
807 key = (struct wpa_eapol_key *) (hdr + 1);
808 key_info = WPA_GET_BE16(key->key_info);
809 key_data_length = WPA_GET_BE16(key->key_data_length);
810 wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
811 " key_info=0x%x type=%u key_data_length=%u",
812 MAC2STR(sm->addr), key_info, key->type, key_data_length);
813 if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) {
814 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
815 "key_data overflow (%d > %lu)",
817 (unsigned long) (data_len - sizeof(*hdr) -
822 if (sm->wpa == WPA_VERSION_WPA2) {
823 if (key->type == EAPOL_KEY_TYPE_WPA) {
825 * Some deployed station implementations seem to send
826 * msg 4/4 with incorrect type value in WPA2 mode.
828 wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
829 "with unexpected WPA type in RSN mode");
830 } else if (key->type != EAPOL_KEY_TYPE_RSN) {
831 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
832 "unexpected type %d in RSN mode",
837 if (key->type != EAPOL_KEY_TYPE_WPA) {
838 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
839 "unexpected type %d in WPA mode",
845 wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
847 wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
848 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
850 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
853 if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
854 (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
855 if (key_info & WPA_KEY_INFO_ERROR) {
857 msgtxt = "SMK Error";
862 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
865 } else if (key_info & WPA_KEY_INFO_REQUEST) {
868 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
870 msgtxt = "2/2 Group";
871 } else if (key_data_length == 0) {
873 msgtxt = "4/4 Pairwise";
876 msgtxt = "2/4 Pairwise";
879 /* TODO: key_info type validation for PeerKey */
880 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
882 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
883 if (sm->pairwise == WPA_CIPHER_CCMP ||
884 sm->pairwise == WPA_CIPHER_GCMP) {
885 if (wpa_use_aes_cmac(sm) &&
886 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN &&
887 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
888 wpa_auth_logger(wpa_auth, sm->addr,
890 "advertised support for "
891 "AES-128-CMAC, but did not "
896 if (!wpa_use_aes_cmac(sm) &&
897 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
898 wpa_auth_logger(wpa_auth, sm->addr,
900 "did not use HMAC-SHA1-AES "
907 if (key_info & WPA_KEY_INFO_REQUEST) {
908 if (sm->req_replay_counter_used &&
909 os_memcmp(key->replay_counter, sm->req_replay_counter,
910 WPA_REPLAY_COUNTER_LEN) <= 0) {
911 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
912 "received EAPOL-Key request with "
918 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
919 !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
922 if (msg == PAIRWISE_2 &&
923 wpa_replay_counter_valid(sm->prev_key_replay,
924 key->replay_counter) &&
925 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
926 os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
929 * Some supplicant implementations (e.g., Windows XP
930 * WZC) update SNonce for each EAPOL-Key 2/4. This
931 * breaks the workaround on accepting any of the
932 * pending requests, so allow the SNonce to be updated
933 * even if we have already sent out EAPOL-Key 3/4.
935 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
936 "Process SNonce update from STA "
937 "based on retransmitted EAPOL-Key "
939 sm->update_snonce = 1;
940 wpa_replay_counter_mark_invalid(sm->prev_key_replay,
941 key->replay_counter);
942 goto continue_processing;
945 if (msg == PAIRWISE_2 &&
946 wpa_replay_counter_valid(sm->prev_key_replay,
947 key->replay_counter) &&
948 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
949 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
950 "ignore retransmitted EAPOL-Key %s - "
951 "SNonce did not change", msgtxt);
953 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
954 "received EAPOL-Key %s with "
955 "unexpected replay counter", msgtxt);
957 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
958 if (!sm->key_replay[i].valid)
960 wpa_hexdump(MSG_DEBUG, "pending replay counter",
961 sm->key_replay[i].counter,
962 WPA_REPLAY_COUNTER_LEN);
964 wpa_hexdump(MSG_DEBUG, "received replay counter",
965 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
972 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
973 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
974 (!sm->update_snonce ||
975 sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
976 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
977 "received EAPOL-Key msg 2/4 in "
978 "invalid state (%d) - dropped",
982 random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
983 if (sm->group->reject_4way_hs_for_entropy) {
985 * The system did not have enough entropy to generate
986 * strong random numbers. Reject the first 4-way
987 * handshake(s) and collect some entropy based on the
988 * information from it. Once enough entropy is
989 * available, the next atempt will trigger GMK/Key
990 * Counter update and the station will be allowed to
993 wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
994 "collect more entropy for random number "
996 random_mark_pool_ready();
997 wpa_sta_disconnect(wpa_auth, sm->addr);
1000 if (wpa_parse_kde_ies((u8 *) (key + 1), key_data_length,
1002 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1003 "received EAPOL-Key msg 2/4 with "
1004 "invalid Key Data contents");
1008 eapol_key_ie = kde.rsn_ie;
1009 eapol_key_ie_len = kde.rsn_ie_len;
1010 } else if (kde.osen) {
1011 eapol_key_ie = kde.osen;
1012 eapol_key_ie_len = kde.osen_len;
1014 eapol_key_ie = kde.wpa_ie;
1015 eapol_key_ie_len = kde.wpa_ie_len;
1017 ft = sm->wpa == WPA_VERSION_WPA2 &&
1018 wpa_key_mgmt_ft(sm->wpa_key_mgmt);
1019 if (sm->wpa_ie == NULL ||
1020 wpa_compare_rsn_ie(ft,
1021 sm->wpa_ie, sm->wpa_ie_len,
1022 eapol_key_ie, eapol_key_ie_len)) {
1023 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1024 "WPA IE from (Re)AssocReq did not "
1025 "match with msg 2/4");
1027 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
1028 sm->wpa_ie, sm->wpa_ie_len);
1030 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
1031 eapol_key_ie, eapol_key_ie_len);
1032 /* MLME-DEAUTHENTICATE.request */
1033 wpa_sta_disconnect(wpa_auth, sm->addr);
1036 #ifdef CONFIG_IEEE80211R
1037 if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
1038 wpa_sta_disconnect(wpa_auth, sm->addr);
1041 #endif /* CONFIG_IEEE80211R */
1043 if (kde.ip_addr_req && kde.ip_addr_req[0] &&
1044 wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
1046 wpa_printf(MSG_DEBUG, "P2P: IP address requested in "
1047 "EAPOL-Key exchange");
1048 idx = bitfield_get_first_zero(wpa_auth->ip_pool);
1050 u32 start = WPA_GET_BE32(wpa_auth->conf.
1052 bitfield_set(wpa_auth->ip_pool, idx);
1053 WPA_PUT_BE32(sm->ip_addr, start + idx);
1054 wpa_printf(MSG_DEBUG, "P2P: Assigned IP "
1055 "address %u.%u.%u.%u to " MACSTR,
1056 sm->ip_addr[0], sm->ip_addr[1],
1057 sm->ip_addr[2], sm->ip_addr[3],
1061 #endif /* CONFIG_P2P */
1064 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1066 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1067 "received EAPOL-Key msg 4/4 in "
1068 "invalid state (%d) - dropped",
1074 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1075 || !sm->PTK_valid) {
1076 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1077 "received EAPOL-Key msg 2/2 in "
1078 "invalid state (%d) - dropped",
1079 sm->wpa_ptk_group_state);
1083 #ifdef CONFIG_PEERKEY
1087 if (!wpa_auth->conf.peerkey) {
1088 wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
1089 "PeerKey use disabled - ignoring message");
1092 if (!sm->PTK_valid) {
1093 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1094 "received EAPOL-Key msg SMK in "
1095 "invalid state - dropped");
1099 #else /* CONFIG_PEERKEY */
1103 return; /* STSL disabled - ignore SMK messages */
1104 #endif /* CONFIG_PEERKEY */
1109 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1110 "received EAPOL-Key frame (%s)", msgtxt);
1112 if (key_info & WPA_KEY_INFO_ACK) {
1113 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1114 "received invalid EAPOL-Key: Key Ack set");
1118 if (!(key_info & WPA_KEY_INFO_MIC)) {
1119 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1120 "received invalid EAPOL-Key: Key MIC not set");
1124 sm->MICVerified = FALSE;
1125 if (sm->PTK_valid && !sm->update_snonce) {
1126 if (wpa_verify_key_mic(&sm->PTK, data, data_len)) {
1127 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1128 "received EAPOL-Key with invalid MIC");
1131 sm->MICVerified = TRUE;
1132 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
1133 sm->pending_1_of_4_timeout = 0;
1136 if (key_info & WPA_KEY_INFO_REQUEST) {
1137 if (sm->MICVerified) {
1138 sm->req_replay_counter_used = 1;
1139 os_memcpy(sm->req_replay_counter, key->replay_counter,
1140 WPA_REPLAY_COUNTER_LEN);
1142 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1143 "received EAPOL-Key request with "
1149 * TODO: should decrypt key data field if encryption was used;
1150 * even though MAC address KDE is not normally encrypted,
1151 * supplicant is allowed to encrypt it.
1153 if (msg == SMK_ERROR) {
1154 #ifdef CONFIG_PEERKEY
1155 wpa_smk_error(wpa_auth, sm, key);
1156 #endif /* CONFIG_PEERKEY */
1158 } else if (key_info & WPA_KEY_INFO_ERROR) {
1159 if (wpa_receive_error_report(
1161 !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
1162 return; /* STA entry was removed */
1163 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1164 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1165 "received EAPOL-Key Request for new "
1167 wpa_request_new_ptk(sm);
1168 #ifdef CONFIG_PEERKEY
1169 } else if (msg == SMK_M1) {
1170 wpa_smk_m1(wpa_auth, sm, key);
1171 #endif /* CONFIG_PEERKEY */
1172 } else if (key_data_length > 0 &&
1173 wpa_parse_kde_ies((const u8 *) (key + 1),
1174 key_data_length, &kde) == 0 &&
1177 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1178 "received EAPOL-Key Request for GTK "
1180 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
1181 wpa_rekey_gtk(wpa_auth, NULL);
1184 /* Do not allow the same key replay counter to be reused. */
1185 wpa_replay_counter_mark_invalid(sm->key_replay,
1186 key->replay_counter);
1188 if (msg == PAIRWISE_2) {
1190 * Maintain a copy of the pending EAPOL-Key frames in
1191 * case the EAPOL-Key frame was retransmitted. This is
1192 * needed to allow EAPOL-Key msg 2/4 reply to another
1193 * pending msg 1/4 to update the SNonce to work around
1194 * unexpected supplicant behavior.
1196 os_memcpy(sm->prev_key_replay, sm->key_replay,
1197 sizeof(sm->key_replay));
1199 os_memset(sm->prev_key_replay, 0,
1200 sizeof(sm->prev_key_replay));
1204 * Make sure old valid counters are not accepted anymore and
1205 * do not get copied again.
1207 wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
1210 #ifdef CONFIG_PEERKEY
1211 if (msg == SMK_M3) {
1212 wpa_smk_m3(wpa_auth, sm, key);
1215 #endif /* CONFIG_PEERKEY */
1217 os_free(sm->last_rx_eapol_key);
1218 sm->last_rx_eapol_key = os_malloc(data_len);
1219 if (sm->last_rx_eapol_key == NULL)
1221 os_memcpy(sm->last_rx_eapol_key, data, data_len);
1222 sm->last_rx_eapol_key_len = data_len;
1224 sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
1225 sm->EAPOLKeyReceived = TRUE;
1226 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1227 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1228 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1233 static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
1234 const u8 *gnonce, u8 *gtk, size_t gtk_len)
1236 u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + 16];
1240 /* GTK = PRF-X(GMK, "Group key expansion",
1241 * AA || GNonce || Time || random data)
1242 * The example described in the IEEE 802.11 standard uses only AA and
1243 * GNonce as inputs here. Add some more entropy since this derivation
1244 * is done only at the Authenticator and as such, does not need to be
1247 os_memcpy(data, addr, ETH_ALEN);
1248 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
1249 pos = data + ETH_ALEN + WPA_NONCE_LEN;
1250 wpa_get_ntp_timestamp(pos);
1252 if (random_get_bytes(pos, 16) < 0)
1255 #ifdef CONFIG_IEEE80211W
1256 sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len);
1257 #else /* CONFIG_IEEE80211W */
1258 if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len)
1261 #endif /* CONFIG_IEEE80211W */
1267 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1269 struct wpa_authenticator *wpa_auth = eloop_ctx;
1270 struct wpa_state_machine *sm = timeout_ctx;
1272 sm->pending_1_of_4_timeout = 0;
1273 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
1274 sm->TimeoutEvt = TRUE;
1279 void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1280 struct wpa_state_machine *sm, int key_info,
1281 const u8 *key_rsc, const u8 *nonce,
1282 const u8 *kde, size_t kde_len,
1283 int keyidx, int encr, int force_version)
1285 struct ieee802_1x_hdr *hdr;
1286 struct wpa_eapol_key *key;
1289 int key_data_len, pad_len = 0;
1291 int version, pairwise;
1294 len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
1297 version = force_version;
1298 else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN)
1299 version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
1300 else if (wpa_use_aes_cmac(sm))
1301 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
1302 else if (sm->pairwise != WPA_CIPHER_TKIP)
1303 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1305 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1307 pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1309 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1310 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1313 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
1314 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
1315 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
1316 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
1317 pairwise, (unsigned long) kde_len, keyidx, encr);
1319 key_data_len = kde_len;
1321 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1322 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
1323 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1324 pad_len = key_data_len % 8;
1326 pad_len = 8 - pad_len;
1327 key_data_len += pad_len + 8;
1330 len += key_data_len;
1332 hdr = os_zalloc(len);
1335 hdr->version = wpa_auth->conf.eapol_version;
1336 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1337 hdr->length = host_to_be16(len - sizeof(*hdr));
1338 key = (struct wpa_eapol_key *) (hdr + 1);
1340 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1341 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1342 key_info |= version;
1343 if (encr && sm->wpa == WPA_VERSION_WPA2)
1344 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1345 if (sm->wpa != WPA_VERSION_WPA2)
1346 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1347 WPA_PUT_BE16(key->key_info, key_info);
1349 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
1350 WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
1351 if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
1352 WPA_PUT_BE16(key->key_length, 0);
1354 /* FIX: STSL: what to use as key_replay_counter? */
1355 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1356 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1357 os_memcpy(sm->key_replay[i].counter,
1358 sm->key_replay[i - 1].counter,
1359 WPA_REPLAY_COUNTER_LEN);
1361 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1362 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1363 WPA_REPLAY_COUNTER_LEN);
1364 sm->key_replay[0].valid = TRUE;
1367 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1370 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1373 os_memcpy(key + 1, kde, kde_len);
1374 WPA_PUT_BE16(key->key_data_length, kde_len);
1375 } else if (encr && kde) {
1376 buf = os_zalloc(key_data_len);
1382 os_memcpy(pos, kde, kde_len);
1388 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1390 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1391 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
1392 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1393 if (aes_wrap(sm->PTK.kek, 16,
1394 (key_data_len - 8) / 8, buf,
1395 (u8 *) (key + 1))) {
1400 WPA_PUT_BE16(key->key_data_length, key_data_len);
1403 os_memcpy(key->key_iv,
1404 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1405 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1406 os_memcpy(ek, key->key_iv, 16);
1407 os_memcpy(ek + 16, sm->PTK.kek, 16);
1408 os_memcpy(key + 1, buf, key_data_len);
1409 rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
1410 WPA_PUT_BE16(key->key_data_length, key_data_len);
1415 if (key_info & WPA_KEY_INFO_MIC) {
1416 if (!sm->PTK_valid) {
1417 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1418 "PTK not valid when sending EAPOL-Key "
1423 wpa_eapol_key_mic(sm->PTK.kck, version, (u8 *) hdr, len,
1425 #ifdef CONFIG_TESTING_OPTIONS
1427 wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0 &&
1429 wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
1430 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1431 "Corrupting group EAPOL-Key Key MIC");
1434 #endif /* CONFIG_TESTING_OPTIONS */
1437 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1439 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1445 static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1446 struct wpa_state_machine *sm, int key_info,
1447 const u8 *key_rsc, const u8 *nonce,
1448 const u8 *kde, size_t kde_len,
1449 int keyidx, int encr)
1452 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1458 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1461 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1462 if (ctr == 1 && wpa_auth->conf.tx_status)
1463 timeout_ms = pairwise ? eapol_key_timeout_first :
1464 eapol_key_timeout_first_group;
1466 timeout_ms = eapol_key_timeout_subseq;
1467 if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
1468 sm->pending_1_of_4_timeout = 1;
1469 wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1470 "counter %d)", timeout_ms, ctr);
1471 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1472 wpa_send_eapol_timeout, wpa_auth, sm);
1476 static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len)
1478 struct ieee802_1x_hdr *hdr;
1479 struct wpa_eapol_key *key;
1484 if (data_len < sizeof(*hdr) + sizeof(*key))
1487 hdr = (struct ieee802_1x_hdr *) data;
1488 key = (struct wpa_eapol_key *) (hdr + 1);
1489 key_info = WPA_GET_BE16(key->key_info);
1490 os_memcpy(mic, key->key_mic, 16);
1491 os_memset(key->key_mic, 0, 16);
1492 if (wpa_eapol_key_mic(PTK->kck, key_info & WPA_KEY_INFO_TYPE_MASK,
1493 data, data_len, key->key_mic) ||
1494 os_memcmp_const(mic, key->key_mic, 16) != 0)
1496 os_memcpy(key->key_mic, mic, 16);
1501 void wpa_remove_ptk(struct wpa_state_machine *sm)
1503 sm->PTK_valid = FALSE;
1504 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1505 wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL, 0);
1506 sm->pairwise_set = FALSE;
1507 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1511 int wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
1518 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1519 "event %d notification", event);
1527 sm->DeauthenticationRequest = TRUE;
1530 case WPA_REAUTH_EAPOL:
1533 * When using WPS, we may end up here if the STA
1534 * manages to re-associate without the previous STA
1535 * entry getting removed. Consequently, we need to make
1536 * sure that the WPA state machines gets initialized
1537 * properly at this point.
1539 wpa_printf(MSG_DEBUG, "WPA state machine had not been "
1540 "started - initialize now");
1543 if (wpa_sm_step(sm) == 1)
1544 return 1; /* should not really happen */
1546 sm->AuthenticationRequest = TRUE;
1549 if (sm->GUpdateStationKeys) {
1551 * Reauthentication cancels the pending group key
1552 * update for this STA.
1554 sm->group->GKeyDoneStations--;
1555 sm->GUpdateStationKeys = FALSE;
1556 sm->PtkGroupInit = TRUE;
1558 sm->ReAuthenticationRequest = TRUE;
1561 #ifdef CONFIG_IEEE80211R
1562 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1563 "after association");
1564 wpa_ft_install_ptk(sm);
1566 /* Using FT protocol, not WPA auth state machine */
1567 sm->ft_completed = 1;
1569 #else /* CONFIG_IEEE80211R */
1571 #endif /* CONFIG_IEEE80211R */
1574 #ifdef CONFIG_IEEE80211R
1575 sm->ft_completed = 0;
1576 #endif /* CONFIG_IEEE80211R */
1578 #ifdef CONFIG_IEEE80211W
1579 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1581 #endif /* CONFIG_IEEE80211W */
1584 sm->PTK_valid = FALSE;
1585 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1587 if (event != WPA_REAUTH_EAPOL)
1591 return wpa_sm_step(sm);
1595 SM_STATE(WPA_PTK, INITIALIZE)
1597 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1599 /* Init flag is not cleared here, so avoid busy
1600 * loop by claiming nothing changed. */
1601 sm->changed = FALSE;
1605 if (sm->GUpdateStationKeys)
1606 sm->group->GKeyDoneStations--;
1607 sm->GUpdateStationKeys = FALSE;
1608 if (sm->wpa == WPA_VERSION_WPA)
1609 sm->PInitAKeys = FALSE;
1610 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1611 * Local AA > Remote AA)) */) {
1614 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1616 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1618 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1619 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1620 WPA_EAPOL_authorized, 0);
1625 SM_STATE(WPA_PTK, DISCONNECT)
1627 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1628 sm->Disconnect = FALSE;
1629 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1633 SM_STATE(WPA_PTK, DISCONNECTED)
1635 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1636 sm->DeauthenticationRequest = FALSE;
1640 SM_STATE(WPA_PTK, AUTHENTICATION)
1642 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1643 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1644 sm->PTK_valid = FALSE;
1645 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1647 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1648 sm->AuthenticationRequest = FALSE;
1652 static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
1653 struct wpa_group *group)
1655 if (group->first_sta_seen)
1658 * System has run bit further than at the time hostapd was started
1659 * potentially very early during boot up. This provides better chances
1660 * of collecting more randomness on embedded systems. Re-initialize the
1661 * GMK and Counter here to improve their strength if there was not
1662 * enough entropy available immediately after system startup.
1664 wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
1666 if (random_pool_ready() != 1) {
1667 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
1668 "to proceed - reject first 4-way handshake");
1669 group->reject_4way_hs_for_entropy = TRUE;
1671 group->first_sta_seen = TRUE;
1672 group->reject_4way_hs_for_entropy = FALSE;
1675 wpa_group_init_gmk_and_counter(wpa_auth, group);
1676 wpa_gtk_update(wpa_auth, group);
1677 wpa_group_config_group_keys(wpa_auth, group);
1681 SM_STATE(WPA_PTK, AUTHENTICATION2)
1683 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1685 wpa_group_ensure_init(sm->wpa_auth, sm->group);
1686 sm->ReAuthenticationRequest = FALSE;
1689 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1690 * ambiguous. The Authenticator state machine uses a counter that is
1691 * incremented by one for each 4-way handshake. However, the security
1692 * analysis of 4-way handshake points out that unpredictable nonces
1693 * help in preventing precomputation attacks. Instead of the state
1694 * machine definition, use an unpredictable nonce value here to provide
1695 * stronger protection against potential precomputation attacks.
1697 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1698 wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
1700 sm->Disconnect = TRUE;
1703 wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
1705 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1706 * logical place than INITIALIZE since AUTHENTICATION2 can be
1707 * re-entered on ReAuthenticationRequest without going through
1713 SM_STATE(WPA_PTK, INITPMK)
1715 u8 msk[2 * PMK_LEN];
1716 size_t len = 2 * PMK_LEN;
1718 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1719 #ifdef CONFIG_IEEE80211R
1721 #endif /* CONFIG_IEEE80211R */
1723 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1724 os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
1725 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1726 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1727 "(len=%lu)", (unsigned long) len);
1728 os_memcpy(sm->PMK, msk, PMK_LEN);
1729 #ifdef CONFIG_IEEE80211R
1730 if (len >= 2 * PMK_LEN) {
1731 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1732 sm->xxkey_len = PMK_LEN;
1734 #endif /* CONFIG_IEEE80211R */
1736 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
1739 sm->req_replay_counter_used = 0;
1740 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1741 * will break reauthentication since EAPOL state machines may not be
1742 * get into AUTHENTICATING state that clears keyRun before WPA state
1743 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1744 * state and takes PMK from the previously used AAA Key. This will
1745 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1746 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1747 * be good workaround for this issue. */
1748 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1752 SM_STATE(WPA_PTK, INITPSK)
1755 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
1756 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL);
1758 os_memcpy(sm->PMK, psk, PMK_LEN);
1759 #ifdef CONFIG_IEEE80211R
1760 os_memcpy(sm->xxkey, psk, PMK_LEN);
1761 sm->xxkey_len = PMK_LEN;
1762 #endif /* CONFIG_IEEE80211R */
1764 sm->req_replay_counter_used = 0;
1768 SM_STATE(WPA_PTK, PTKSTART)
1770 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1771 size_t pmkid_len = 0;
1773 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
1774 sm->PTKRequest = FALSE;
1775 sm->TimeoutEvt = FALSE;
1778 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1779 /* No point in sending the EAPOL-Key - we will disconnect
1780 * immediately following this. */
1784 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1785 "sending 1/4 msg of 4-Way Handshake");
1787 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1788 * one possible PSK for this STA.
1790 if (sm->wpa == WPA_VERSION_WPA2 &&
1791 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
1792 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
1794 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
1795 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
1796 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
1797 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
1799 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
1800 sm->pmksa->pmkid, PMKID_LEN);
1803 * Calculate PMKID since no PMKSA cache entry was
1804 * available with pre-calculated PMKID.
1806 rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
1807 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
1808 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1811 wpa_send_eapol(sm->wpa_auth, sm,
1812 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
1813 sm->ANonce, pmkid, pmkid_len, 0, 0);
1817 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk,
1818 struct wpa_ptk *ptk)
1820 size_t ptk_len = wpa_cipher_key_len(sm->pairwise) + 32;
1821 #ifdef CONFIG_IEEE80211R
1822 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
1823 return wpa_auth_derive_ptk_ft(sm, pmk, ptk, ptk_len);
1824 #endif /* CONFIG_IEEE80211R */
1826 wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
1827 sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce,
1828 (u8 *) ptk, ptk_len,
1829 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1835 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
1839 const u8 *pmk = NULL;
1841 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
1842 sm->EAPOLKeyReceived = FALSE;
1843 sm->update_snonce = FALSE;
1845 /* WPA with IEEE 802.1X: use the derived PMK from EAP
1846 * WPA-PSK: iterate through possible PSKs and select the one matching
1849 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1850 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
1851 sm->p2p_dev_addr, pmk);
1857 wpa_derive_ptk(sm, pmk, &PTK);
1859 if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key,
1860 sm->last_rx_eapol_key_len) == 0) {
1865 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
1870 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1871 "invalid MIC in msg 2/4 of 4-Way Handshake");
1875 #ifdef CONFIG_IEEE80211R
1876 if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
1878 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
1879 * with the value we derived.
1881 if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name,
1882 WPA_PMK_NAME_LEN) != 0) {
1883 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1884 "PMKR1Name mismatch in FT 4-way "
1886 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
1888 sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
1889 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1890 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1894 #endif /* CONFIG_IEEE80211R */
1896 sm->pending_1_of_4_timeout = 0;
1897 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
1899 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1900 /* PSK may have changed from the previous choice, so update
1901 * state machine data based on whatever PSK was selected here.
1903 os_memcpy(sm->PMK, pmk, PMK_LEN);
1906 sm->MICVerified = TRUE;
1908 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
1909 sm->PTK_valid = TRUE;
1913 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
1915 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
1920 #ifdef CONFIG_IEEE80211W
1922 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1924 if (sm->mgmt_frame_prot) {
1926 len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
1927 return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len;
1934 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1936 struct wpa_igtk_kde igtk;
1937 struct wpa_group *gsm = sm->group;
1938 u8 rsc[WPA_KEY_RSC_LEN];
1939 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
1941 if (!sm->mgmt_frame_prot)
1944 igtk.keyid[0] = gsm->GN_igtk;
1946 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
1947 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
1948 os_memset(igtk.pn, 0, sizeof(igtk.pn));
1950 os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
1951 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
1952 if (sm->wpa_auth->conf.disable_gtk) {
1954 * Provide unique random IGTK to each STA to prevent use of
1957 if (random_get_bytes(igtk.igtk, len) < 0)
1960 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
1961 (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
1967 #else /* CONFIG_IEEE80211W */
1969 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1975 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1980 #endif /* CONFIG_IEEE80211W */
1983 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
1985 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
1986 size_t gtk_len, kde_len;
1987 struct wpa_group *gsm = sm->group;
1989 int wpa_ie_len, secure, keyidx, encr = 0;
1991 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
1992 sm->TimeoutEvt = FALSE;
1995 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1996 /* No point in sending the EAPOL-Key - we will disconnect
1997 * immediately following this. */
2001 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
2002 GTK[GN], IGTK, [FTIE], [TIE * 2])
2004 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2005 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2006 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
2007 wpa_ie = sm->wpa_auth->wpa_ie;
2008 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
2009 if (sm->wpa == WPA_VERSION_WPA &&
2010 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
2011 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
2012 /* WPA-only STA, remove RSN IE */
2013 wpa_ie = wpa_ie + wpa_ie[1] + 2;
2014 wpa_ie_len = wpa_ie[1] + 2;
2016 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2017 "sending 3/4 msg of 4-Way Handshake");
2018 if (sm->wpa == WPA_VERSION_WPA2) {
2019 /* WPA2 send GTK in the 4-way handshake */
2021 gtk = gsm->GTK[gsm->GN - 1];
2022 gtk_len = gsm->GTK_len;
2023 if (sm->wpa_auth->conf.disable_gtk) {
2025 * Provide unique random GTK to each STA to prevent use
2026 * of GTK in the BSS.
2028 if (random_get_bytes(dummy_gtk, gtk_len) < 0)
2036 /* WPA does not include GTK in msg 3/4 */
2042 if (sm->rx_eapol_key_secure) {
2044 * It looks like Windows 7 supplicant tries to use
2045 * Secure bit in msg 2/4 after having reported Michael
2046 * MIC failure and it then rejects the 4-way handshake
2047 * if msg 3/4 does not set Secure bit. Work around this
2048 * by setting the Secure bit here even in the case of
2049 * WPA if the supplicant used it first.
2051 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2052 "STA used Secure bit in WPA msg 2/4 - "
2053 "set Secure for 3/4 as workaround");
2058 kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
2060 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
2061 #ifdef CONFIG_IEEE80211R
2062 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2063 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
2064 kde_len += 300; /* FTIE + 2 * TIE */
2066 #endif /* CONFIG_IEEE80211R */
2068 if (WPA_GET_BE32(sm->ip_addr) > 0)
2069 kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
2070 #endif /* CONFIG_P2P */
2071 kde = os_malloc(kde_len);
2076 os_memcpy(pos, wpa_ie, wpa_ie_len);
2078 #ifdef CONFIG_IEEE80211R
2079 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2080 int res = wpa_insert_pmkid(kde, pos - kde, sm->pmk_r1_name);
2082 wpa_printf(MSG_ERROR, "FT: Failed to insert "
2083 "PMKR1Name into RSN IE in EAPOL-Key data");
2089 #endif /* CONFIG_IEEE80211R */
2092 hdr[0] = keyidx & 0x03;
2094 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2097 pos = ieee80211w_kde_add(sm, pos);
2099 #ifdef CONFIG_IEEE80211R
2100 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2102 struct wpa_auth_config *conf;
2104 conf = &sm->wpa_auth->conf;
2105 res = wpa_write_ftie(conf, conf->r0_key_holder,
2106 conf->r0_key_holder_len,
2107 NULL, NULL, pos, kde + kde_len - pos,
2110 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
2111 "into EAPOL-Key Key Data");
2117 /* TIE[ReassociationDeadline] (TU) */
2118 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2120 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
2121 WPA_PUT_LE32(pos, conf->reassociation_deadline);
2124 /* TIE[KeyLifetime] (seconds) */
2125 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2127 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
2128 WPA_PUT_LE32(pos, conf->r0_key_lifetime * 60);
2131 #endif /* CONFIG_IEEE80211R */
2133 if (WPA_GET_BE32(sm->ip_addr) > 0) {
2135 os_memcpy(addr, sm->ip_addr, 4);
2136 os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
2137 os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
2138 pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
2139 addr, sizeof(addr), NULL, 0);
2141 #endif /* CONFIG_P2P */
2143 wpa_send_eapol(sm->wpa_auth, sm,
2144 (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
2145 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
2146 WPA_KEY_INFO_KEY_TYPE,
2147 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
2152 SM_STATE(WPA_PTK, PTKINITDONE)
2154 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
2155 sm->EAPOLKeyReceived = FALSE;
2157 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
2158 int klen = wpa_cipher_key_len(sm->pairwise);
2159 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
2160 sm->PTK.tk1, klen)) {
2161 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
2164 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
2165 sm->pairwise_set = TRUE;
2167 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
2168 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
2169 eloop_register_timeout(sm->wpa_auth->conf.
2170 wpa_ptk_rekey, 0, wpa_rekey_ptk,
2174 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2175 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2176 WPA_EAPOL_authorized, 1);
2180 if (0 /* IBSS == TRUE */) {
2182 if (sm->keycount == 2) {
2183 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2184 WPA_EAPOL_portValid, 1);
2187 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
2190 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
2191 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
2192 if (sm->wpa == WPA_VERSION_WPA)
2193 sm->PInitAKeys = TRUE;
2196 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2197 "pairwise key handshake completed (%s)",
2198 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2200 #ifdef CONFIG_IEEE80211R
2201 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
2202 #endif /* CONFIG_IEEE80211R */
2208 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2211 SM_ENTER(WPA_PTK, INITIALIZE);
2212 else if (sm->Disconnect
2213 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
2214 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
2215 "WPA_PTK: sm->Disconnect");
2216 SM_ENTER(WPA_PTK, DISCONNECT);
2218 else if (sm->DeauthenticationRequest)
2219 SM_ENTER(WPA_PTK, DISCONNECTED);
2220 else if (sm->AuthenticationRequest)
2221 SM_ENTER(WPA_PTK, AUTHENTICATION);
2222 else if (sm->ReAuthenticationRequest)
2223 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2224 else if (sm->PTKRequest)
2225 SM_ENTER(WPA_PTK, PTKSTART);
2226 else switch (sm->wpa_ptk_state) {
2227 case WPA_PTK_INITIALIZE:
2229 case WPA_PTK_DISCONNECT:
2230 SM_ENTER(WPA_PTK, DISCONNECTED);
2232 case WPA_PTK_DISCONNECTED:
2233 SM_ENTER(WPA_PTK, INITIALIZE);
2235 case WPA_PTK_AUTHENTICATION:
2236 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2238 case WPA_PTK_AUTHENTICATION2:
2239 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
2240 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2241 WPA_EAPOL_keyRun) > 0)
2242 SM_ENTER(WPA_PTK, INITPMK);
2243 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
2244 /* FIX: && 802.1X::keyRun */)
2245 SM_ENTER(WPA_PTK, INITPSK);
2247 case WPA_PTK_INITPMK:
2248 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2249 WPA_EAPOL_keyAvailable) > 0)
2250 SM_ENTER(WPA_PTK, PTKSTART);
2252 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2253 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2254 "INITPMK - keyAvailable = false");
2255 SM_ENTER(WPA_PTK, DISCONNECT);
2258 case WPA_PTK_INITPSK:
2259 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
2261 SM_ENTER(WPA_PTK, PTKSTART);
2263 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2264 "no PSK configured for the STA");
2265 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2266 SM_ENTER(WPA_PTK, DISCONNECT);
2269 case WPA_PTK_PTKSTART:
2270 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2271 sm->EAPOLKeyPairwise)
2272 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2273 else if (sm->TimeoutCtr >
2274 (int) dot11RSNAConfigPairwiseUpdateCount) {
2275 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2276 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2277 "PTKSTART: Retry limit %d reached",
2278 dot11RSNAConfigPairwiseUpdateCount);
2279 SM_ENTER(WPA_PTK, DISCONNECT);
2280 } else if (sm->TimeoutEvt)
2281 SM_ENTER(WPA_PTK, PTKSTART);
2283 case WPA_PTK_PTKCALCNEGOTIATING:
2284 if (sm->MICVerified)
2285 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
2286 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2287 sm->EAPOLKeyPairwise)
2288 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2289 else if (sm->TimeoutEvt)
2290 SM_ENTER(WPA_PTK, PTKSTART);
2292 case WPA_PTK_PTKCALCNEGOTIATING2:
2293 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2295 case WPA_PTK_PTKINITNEGOTIATING:
2296 if (sm->update_snonce)
2297 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2298 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2299 sm->EAPOLKeyPairwise && sm->MICVerified)
2300 SM_ENTER(WPA_PTK, PTKINITDONE);
2301 else if (sm->TimeoutCtr >
2302 (int) dot11RSNAConfigPairwiseUpdateCount) {
2303 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2304 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2305 "PTKINITNEGOTIATING: Retry limit %d "
2307 dot11RSNAConfigPairwiseUpdateCount);
2308 SM_ENTER(WPA_PTK, DISCONNECT);
2309 } else if (sm->TimeoutEvt)
2310 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2312 case WPA_PTK_PTKINITDONE:
2318 SM_STATE(WPA_PTK_GROUP, IDLE)
2320 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
2322 /* Init flag is not cleared here, so avoid busy
2323 * loop by claiming nothing changed. */
2324 sm->changed = FALSE;
2326 sm->GTimeoutCtr = 0;
2330 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
2332 u8 rsc[WPA_KEY_RSC_LEN];
2333 struct wpa_group *gsm = sm->group;
2335 u8 *kde_buf = NULL, *pos, hdr[2];
2337 u8 *gtk, dummy_gtk[32];
2339 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
2342 if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
2343 /* No point in sending the EAPOL-Key - we will disconnect
2344 * immediately following this. */
2348 if (sm->wpa == WPA_VERSION_WPA)
2349 sm->PInitAKeys = FALSE;
2350 sm->TimeoutEvt = FALSE;
2351 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
2352 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2353 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
2354 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2355 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2356 "sending 1/2 msg of Group Key Handshake");
2358 gtk = gsm->GTK[gsm->GN - 1];
2359 if (sm->wpa_auth->conf.disable_gtk) {
2361 * Provide unique random GTK to each STA to prevent use
2362 * of GTK in the BSS.
2364 if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
2368 if (sm->wpa == WPA_VERSION_WPA2) {
2369 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
2370 ieee80211w_kde_len(sm);
2371 kde_buf = os_malloc(kde_len);
2372 if (kde_buf == NULL)
2375 kde = pos = kde_buf;
2376 hdr[0] = gsm->GN & 0x03;
2378 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2380 pos = ieee80211w_kde_add(sm, pos);
2381 kde_len = pos - kde;
2384 kde_len = gsm->GTK_len;
2387 wpa_send_eapol(sm->wpa_auth, sm,
2388 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
2390 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
2391 rsc, gsm->GNonce, kde, kde_len, gsm->GN, 1);
2397 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
2399 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
2400 sm->EAPOLKeyReceived = FALSE;
2401 if (sm->GUpdateStationKeys)
2402 sm->group->GKeyDoneStations--;
2403 sm->GUpdateStationKeys = FALSE;
2404 sm->GTimeoutCtr = 0;
2405 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
2406 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2407 "group key handshake completed (%s)",
2408 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2413 SM_STATE(WPA_PTK_GROUP, KEYERROR)
2415 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
2416 if (sm->GUpdateStationKeys)
2417 sm->group->GKeyDoneStations--;
2418 sm->GUpdateStationKeys = FALSE;
2419 sm->Disconnect = TRUE;
2423 SM_STEP(WPA_PTK_GROUP)
2425 if (sm->Init || sm->PtkGroupInit) {
2426 SM_ENTER(WPA_PTK_GROUP, IDLE);
2427 sm->PtkGroupInit = FALSE;
2428 } else switch (sm->wpa_ptk_group_state) {
2429 case WPA_PTK_GROUP_IDLE:
2430 if (sm->GUpdateStationKeys ||
2431 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
2432 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2434 case WPA_PTK_GROUP_REKEYNEGOTIATING:
2435 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2436 !sm->EAPOLKeyPairwise && sm->MICVerified)
2437 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
2438 else if (sm->GTimeoutCtr >
2439 (int) dot11RSNAConfigGroupUpdateCount)
2440 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
2441 else if (sm->TimeoutEvt)
2442 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2444 case WPA_PTK_GROUP_KEYERROR:
2445 SM_ENTER(WPA_PTK_GROUP, IDLE);
2447 case WPA_PTK_GROUP_REKEYESTABLISHED:
2448 SM_ENTER(WPA_PTK_GROUP, IDLE);
2454 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
2455 struct wpa_group *group)
2459 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
2460 inc_byte_array(group->Counter, WPA_NONCE_LEN);
2461 if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
2462 wpa_auth->addr, group->GNonce,
2463 group->GTK[group->GN - 1], group->GTK_len) < 0)
2465 wpa_hexdump_key(MSG_DEBUG, "GTK",
2466 group->GTK[group->GN - 1], group->GTK_len);
2468 #ifdef CONFIG_IEEE80211W
2469 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
2471 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
2472 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
2473 inc_byte_array(group->Counter, WPA_NONCE_LEN);
2474 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
2475 wpa_auth->addr, group->GNonce,
2476 group->IGTK[group->GN_igtk - 4], len) < 0)
2478 wpa_hexdump_key(MSG_DEBUG, "IGTK",
2479 group->IGTK[group->GN_igtk - 4], len);
2481 #endif /* CONFIG_IEEE80211W */
2487 static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
2488 struct wpa_group *group)
2490 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2491 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
2492 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
2493 group->wpa_group_state = WPA_GROUP_GTK_INIT;
2496 os_memset(group->GTK, 0, sizeof(group->GTK));
2499 #ifdef CONFIG_IEEE80211W
2502 #endif /* CONFIG_IEEE80211W */
2503 /* GTK[GN] = CalcGTK() */
2504 wpa_gtk_update(wpa_auth, group);
2508 static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
2510 if (ctx != NULL && ctx != sm->group)
2513 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
2514 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2515 "Not in PTKINITDONE; skip Group Key update");
2516 sm->GUpdateStationKeys = FALSE;
2519 if (sm->GUpdateStationKeys) {
2521 * This should not really happen, so add a debug log entry.
2522 * Since we clear the GKeyDoneStations before the loop, the
2523 * station needs to be counted here anyway.
2525 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2526 "GUpdateStationKeys was already set when "
2527 "marking station for GTK rekeying");
2530 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
2531 if (sm->is_wnmsleep)
2534 sm->group->GKeyDoneStations++;
2535 sm->GUpdateStationKeys = TRUE;
2543 /* update GTK when exiting WNM-Sleep Mode */
2544 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
2546 if (sm == NULL || sm->is_wnmsleep)
2549 wpa_group_update_sta(sm, NULL);
2553 void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
2556 sm->is_wnmsleep = !!flag;
2560 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
2562 struct wpa_group *gsm = sm->group;
2567 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
2570 *pos++ = WNM_SLEEP_SUBELEM_GTK;
2571 *pos++ = 11 + gsm->GTK_len;
2572 /* Key ID in B0-B1 of Key Info */
2573 WPA_PUT_LE16(pos, gsm->GN & 0x03);
2575 *pos++ = gsm->GTK_len;
2576 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
2579 os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
2580 pos += gsm->GTK_len;
2582 wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
2584 wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
2585 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
2591 #ifdef CONFIG_IEEE80211W
2592 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
2594 struct wpa_group *gsm = sm->group;
2596 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2600 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
2602 *pos++ = WNM_SLEEP_SUBELEM_IGTK;
2603 *pos++ = 2 + 6 + len;
2604 WPA_PUT_LE16(pos, gsm->GN_igtk);
2606 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
2610 os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
2613 wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
2615 wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
2616 gsm->IGTK[gsm->GN_igtk - 4], len);
2620 #endif /* CONFIG_IEEE80211W */
2621 #endif /* CONFIG_WNM */
2624 static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
2625 struct wpa_group *group)
2629 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2630 "SETKEYS (VLAN-ID %d)", group->vlan_id);
2631 group->changed = TRUE;
2632 group->wpa_group_state = WPA_GROUP_SETKEYS;
2633 group->GTKReKey = FALSE;
2635 group->GM = group->GN;
2637 #ifdef CONFIG_IEEE80211W
2638 tmp = group->GM_igtk;
2639 group->GM_igtk = group->GN_igtk;
2640 group->GN_igtk = tmp;
2641 #endif /* CONFIG_IEEE80211W */
2642 /* "GKeyDoneStations = GNoStations" is done in more robust way by
2643 * counting the STAs that are marked with GUpdateStationKeys instead of
2644 * including all STAs that could be in not-yet-completed state. */
2645 wpa_gtk_update(wpa_auth, group);
2647 if (group->GKeyDoneStations) {
2648 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
2649 "GKeyDoneStations=%d when starting new GTK rekey",
2650 group->GKeyDoneStations);
2651 group->GKeyDoneStations = 0;
2653 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
2654 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
2655 group->GKeyDoneStations);
2659 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
2660 struct wpa_group *group)
2664 if (wpa_auth_set_key(wpa_auth, group->vlan_id,
2665 wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
2666 broadcast_ether_addr, group->GN,
2667 group->GTK[group->GN - 1], group->GTK_len) < 0)
2670 #ifdef CONFIG_IEEE80211W
2671 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
2675 alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher);
2676 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
2679 wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
2680 broadcast_ether_addr, group->GN_igtk,
2681 group->IGTK[group->GN_igtk - 4], len) < 0)
2684 #endif /* CONFIG_IEEE80211W */
2690 static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
2692 if (sm->group == ctx) {
2693 wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
2694 " for discconnection due to fatal failure",
2696 sm->Disconnect = TRUE;
2703 static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
2704 struct wpa_group *group)
2706 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
2707 group->changed = TRUE;
2708 group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
2709 wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
2713 static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
2714 struct wpa_group *group)
2716 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2717 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
2718 group->changed = TRUE;
2719 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
2721 if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
2722 wpa_group_fatal_failure(wpa_auth, group);
2730 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
2731 struct wpa_group *group)
2734 wpa_group_gtk_init(wpa_auth, group);
2735 } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
2736 /* Do not allow group operations */
2737 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
2738 group->GTKAuthenticator) {
2739 wpa_group_setkeysdone(wpa_auth, group);
2740 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
2742 wpa_group_setkeys(wpa_auth, group);
2743 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
2744 if (group->GKeyDoneStations == 0)
2745 wpa_group_setkeysdone(wpa_auth, group);
2746 else if (group->GTKReKey)
2747 wpa_group_setkeys(wpa_auth, group);
2752 static int wpa_sm_step(struct wpa_state_machine *sm)
2757 if (sm->in_step_loop) {
2758 /* This should not happen, but if it does, make sure we do not
2759 * end up freeing the state machine too early by exiting the
2760 * recursive call. */
2761 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
2765 sm->in_step_loop = 1;
2767 if (sm->pending_deinit)
2770 sm->changed = FALSE;
2771 sm->wpa_auth->group->changed = FALSE;
2773 SM_STEP_RUN(WPA_PTK);
2774 if (sm->pending_deinit)
2776 SM_STEP_RUN(WPA_PTK_GROUP);
2777 if (sm->pending_deinit)
2779 wpa_group_sm_step(sm->wpa_auth, sm->group);
2780 } while (sm->changed || sm->wpa_auth->group->changed);
2781 sm->in_step_loop = 0;
2783 if (sm->pending_deinit) {
2784 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
2785 "machine deinit for " MACSTR, MAC2STR(sm->addr));
2786 wpa_free_sta_sm(sm);
2793 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
2795 struct wpa_state_machine *sm = eloop_ctx;
2800 void wpa_auth_sm_notify(struct wpa_state_machine *sm)
2804 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
2808 void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
2811 struct wpa_group *group;
2813 if (wpa_auth == NULL)
2816 group = wpa_auth->group;
2818 for (i = 0; i < 2; i++) {
2820 group->GM = group->GN;
2822 #ifdef CONFIG_IEEE80211W
2823 tmp = group->GM_igtk;
2824 group->GM_igtk = group->GN_igtk;
2825 group->GN_igtk = tmp;
2826 #endif /* CONFIG_IEEE80211W */
2827 wpa_gtk_update(wpa_auth, group);
2828 wpa_group_config_group_keys(wpa_auth, group);
2833 static const char * wpa_bool_txt(int bool)
2835 return bool ? "TRUE" : "FALSE";
2839 #define RSN_SUITE "%02x-%02x-%02x-%d"
2840 #define RSN_SUITE_ARG(s) \
2841 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2843 int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
2846 char pmkid_txt[PMKID_LEN * 2 + 1];
2847 #ifdef CONFIG_RSN_PREAUTH
2848 const int preauth = 1;
2849 #else /* CONFIG_RSN_PREAUTH */
2850 const int preauth = 0;
2851 #endif /* CONFIG_RSN_PREAUTH */
2853 if (wpa_auth == NULL)
2856 ret = os_snprintf(buf + len, buflen - len,
2857 "dot11RSNAOptionImplemented=TRUE\n"
2858 "dot11RSNAPreauthenticationImplemented=%s\n"
2859 "dot11RSNAEnabled=%s\n"
2860 "dot11RSNAPreauthenticationEnabled=%s\n",
2861 wpa_bool_txt(preauth),
2862 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
2863 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
2864 if (ret < 0 || (size_t) ret >= buflen - len)
2868 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2869 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
2872 buf + len, buflen - len,
2873 "dot11RSNAConfigVersion=%u\n"
2874 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
2875 /* FIX: dot11RSNAConfigGroupCipher */
2876 /* FIX: dot11RSNAConfigGroupRekeyMethod */
2877 /* FIX: dot11RSNAConfigGroupRekeyTime */
2878 /* FIX: dot11RSNAConfigGroupRekeyPackets */
2879 "dot11RSNAConfigGroupRekeyStrict=%u\n"
2880 "dot11RSNAConfigGroupUpdateCount=%u\n"
2881 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
2882 "dot11RSNAConfigGroupCipherSize=%u\n"
2883 "dot11RSNAConfigPMKLifetime=%u\n"
2884 "dot11RSNAConfigPMKReauthThreshold=%u\n"
2885 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
2886 "dot11RSNAConfigSATimeout=%u\n"
2887 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2888 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2889 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2890 "dot11RSNAPMKIDUsed=%s\n"
2891 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2892 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2893 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2894 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
2895 "dot11RSNA4WayHandshakeFailures=%u\n"
2896 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
2898 !!wpa_auth->conf.wpa_strict_rekey,
2899 dot11RSNAConfigGroupUpdateCount,
2900 dot11RSNAConfigPairwiseUpdateCount,
2901 wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
2902 dot11RSNAConfigPMKLifetime,
2903 dot11RSNAConfigPMKReauthThreshold,
2904 dot11RSNAConfigSATimeout,
2905 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
2906 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
2907 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
2909 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
2910 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
2911 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
2912 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
2913 wpa_auth->dot11RSNA4WayHandshakeFailures);
2914 if (ret < 0 || (size_t) ret >= buflen - len)
2918 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
2919 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
2922 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
2923 wpa_auth->group->wpa_group_state);
2924 if (ret < 0 || (size_t) ret >= buflen - len)
2932 int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
2940 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
2942 /* dot11RSNAStatsEntry */
2944 pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
2945 WPA_PROTO_RSN : WPA_PROTO_WPA,
2951 buf + len, buflen - len,
2952 /* TODO: dot11RSNAStatsIndex */
2953 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
2954 "dot11RSNAStatsVersion=1\n"
2955 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
2956 /* TODO: dot11RSNAStatsTKIPICVErrors */
2957 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
2958 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
2959 /* TODO: dot11RSNAStatsCCMPReplays */
2960 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
2961 /* TODO: dot11RSNAStatsTKIPReplays */,
2963 RSN_SUITE_ARG(pairwise),
2964 sm->dot11RSNAStatsTKIPLocalMICFailures,
2965 sm->dot11RSNAStatsTKIPRemoteMICFailures);
2966 if (ret < 0 || (size_t) ret >= buflen - len)
2971 ret = os_snprintf(buf + len, buflen - len,
2972 "hostapdWPAPTKState=%d\n"
2973 "hostapdWPAPTKGroupState=%d\n",
2975 sm->wpa_ptk_group_state);
2976 if (ret < 0 || (size_t) ret >= buflen - len)
2984 void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
2987 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
2991 int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
2993 return sm && sm->pairwise_set;
2997 int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
2999 return sm->pairwise;
3003 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
3007 return sm->wpa_key_mgmt;
3011 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
3019 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
3020 struct rsn_pmksa_cache_entry *entry)
3022 if (sm == NULL || sm->pmksa != entry)
3029 struct rsn_pmksa_cache_entry *
3030 wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
3032 return sm ? sm->pmksa : NULL;
3036 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
3039 sm->dot11RSNAStatsTKIPLocalMICFailures++;
3043 const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
3045 if (wpa_auth == NULL)
3047 *len = wpa_auth->wpa_ie_len;
3048 return wpa_auth->wpa_ie;
3052 int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
3053 int session_timeout, struct eapol_state_machine *eapol)
3055 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
3056 sm->wpa_auth->conf.disable_pmksa_caching)
3059 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
3060 sm->PTK.kck, sizeof(sm->PTK.kck),
3061 sm->wpa_auth->addr, sm->addr, session_timeout,
3062 eapol, sm->wpa_key_mgmt))
3069 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
3070 const u8 *pmk, size_t len, const u8 *sta_addr,
3071 int session_timeout,
3072 struct eapol_state_machine *eapol)
3074 if (wpa_auth == NULL)
3077 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len,
3080 sta_addr, session_timeout, eapol,
3081 WPA_KEY_MGMT_IEEE8021X))
3088 int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
3091 if (wpa_auth->conf.disable_pmksa_caching)
3094 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN,
3096 wpa_auth->addr, addr, 0, NULL,
3104 void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
3107 struct rsn_pmksa_cache_entry *pmksa;
3109 if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
3111 pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
3113 wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
3114 MACSTR " based on request", MAC2STR(sta_addr));
3115 pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
3120 static struct wpa_group *
3121 wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
3123 struct wpa_group *group;
3125 if (wpa_auth == NULL || wpa_auth->group == NULL)
3128 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
3130 group = wpa_group_init(wpa_auth, vlan_id, 0);
3134 group->next = wpa_auth->group->next;
3135 wpa_auth->group->next = group;
3141 int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
3143 struct wpa_group *group;
3145 if (sm == NULL || sm->wpa_auth == NULL)
3148 group = sm->wpa_auth->group;
3150 if (group->vlan_id == vlan_id)
3152 group = group->next;
3155 if (group == NULL) {
3156 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
3161 if (sm->group == group)
3164 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
3167 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
3168 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
3175 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
3176 struct wpa_state_machine *sm, int ack)
3178 if (wpa_auth == NULL || sm == NULL)
3180 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
3181 " ack=%d", MAC2STR(sm->addr), ack);
3182 if (sm->pending_1_of_4_timeout && ack) {
3184 * Some deployed supplicant implementations update their SNonce
3185 * for each EAPOL-Key 2/4 message even within the same 4-way
3186 * handshake and then fail to use the first SNonce when
3187 * deriving the PTK. This results in unsuccessful 4-way
3188 * handshake whenever the relatively short initial timeout is
3189 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
3190 * around this by increasing the timeout now that we know that
3191 * the station has received the frame.
3193 int timeout_ms = eapol_key_timeout_subseq;
3194 wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
3195 "timeout by %u ms because of acknowledged frame",
3197 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
3198 eloop_register_timeout(timeout_ms / 1000,
3199 (timeout_ms % 1000) * 1000,
3200 wpa_send_eapol_timeout, wpa_auth, sm);
3205 int wpa_auth_uses_sae(struct wpa_state_machine *sm)
3209 return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
3213 int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
3217 return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
3222 int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
3224 if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
3226 os_memcpy(addr, sm->ip_addr, 4);
3229 #endif /* CONFIG_P2P */