2 * hostapd - IEEE 802.11i-2004 / WPA Authenticator
3 * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
17 #ifndef CONFIG_NATIVE_WINDOWS
21 #include "crypto/aes_wrap.h"
22 #include "crypto/crypto.h"
23 #include "crypto/sha1.h"
24 #include "crypto/sha256.h"
25 #include "eapol_auth/eapol_auth_sm.h"
28 #include "ieee802_11.h"
29 #include "pmksa_cache.h"
30 #include "state_machine.h"
31 #include "wpa_auth_i.h"
32 #include "wpa_auth_ie.h"
34 #define STATE_MACHINE_DATA struct wpa_state_machine
35 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
36 #define STATE_MACHINE_ADDR sm->addr
39 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
40 static void wpa_sm_step(struct wpa_state_machine *sm);
41 static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len);
42 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
43 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
44 struct wpa_group *group);
45 static void wpa_request_new_ptk(struct wpa_state_machine *sm);
46 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
47 struct wpa_group *group);
49 static const u32 dot11RSNAConfigGroupUpdateCount = 4;
50 static const u32 dot11RSNAConfigPairwiseUpdateCount = 4;
51 static const u32 eapol_key_timeout_first = 100; /* ms */
52 static const u32 eapol_key_timeout_subseq = 1000; /* ms */
54 /* TODO: make these configurable */
55 static const int dot11RSNAConfigPMKLifetime = 43200;
56 static const int dot11RSNAConfigPMKReauthThreshold = 70;
57 static const int dot11RSNAConfigSATimeout = 60;
60 static inline void wpa_auth_mic_failure_report(
61 struct wpa_authenticator *wpa_auth, const u8 *addr)
63 if (wpa_auth->cb.mic_failure_report)
64 wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr);
68 static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
69 const u8 *addr, wpa_eapol_variable var,
72 if (wpa_auth->cb.set_eapol)
73 wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value);
77 static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
78 const u8 *addr, wpa_eapol_variable var)
80 if (wpa_auth->cb.get_eapol == NULL)
82 return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var);
86 static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
87 const u8 *addr, const u8 *prev_psk)
89 if (wpa_auth->cb.get_psk == NULL)
91 return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, prev_psk);
95 static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
96 const u8 *addr, u8 *msk, size_t *len)
98 if (wpa_auth->cb.get_msk == NULL)
100 return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len);
104 static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
106 wpa_alg alg, const u8 *addr, int idx,
107 u8 *key, size_t key_len)
109 if (wpa_auth->cb.set_key == NULL)
111 return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
116 static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
117 const u8 *addr, int idx, u8 *seq)
119 if (wpa_auth->cb.get_seqnum == NULL)
121 return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
125 static inline int wpa_auth_get_seqnum_igtk(struct wpa_authenticator *wpa_auth,
126 const u8 *addr, int idx, u8 *seq)
128 if (wpa_auth->cb.get_seqnum_igtk == NULL)
130 return wpa_auth->cb.get_seqnum_igtk(wpa_auth->cb.ctx, addr, idx, seq);
135 wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
136 const u8 *data, size_t data_len, int encrypt)
138 if (wpa_auth->cb.send_eapol == NULL)
140 return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
145 int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
146 int (*cb)(struct wpa_state_machine *sm, void *ctx),
149 if (wpa_auth->cb.for_each_sta == NULL)
151 return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
155 int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
156 int (*cb)(struct wpa_authenticator *a, void *ctx),
159 if (wpa_auth->cb.for_each_auth == NULL)
161 return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
165 void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
166 logger_level level, const char *txt)
168 if (wpa_auth->cb.logger == NULL)
170 wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
174 void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
175 logger_level level, const char *fmt, ...)
181 if (wpa_auth->cb.logger == NULL)
184 maxlen = os_strlen(fmt) + 100;
185 format = os_malloc(maxlen);
190 vsnprintf(format, maxlen, fmt, ap);
193 wpa_auth_logger(wpa_auth, addr, level, format);
199 static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
202 if (wpa_auth->cb.disconnect == NULL)
204 wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
205 WLAN_REASON_PREV_AUTH_NOT_VALID);
209 static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
212 #ifdef CONFIG_IEEE80211R
213 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
215 #endif /* CONFIG_IEEE80211R */
216 #ifdef CONFIG_IEEE80211W
217 if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
219 #endif /* CONFIG_IEEE80211W */
224 static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
226 struct wpa_authenticator *wpa_auth = eloop_ctx;
228 if (os_get_random(wpa_auth->group->GMK, WPA_GMK_LEN)) {
229 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
232 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
235 if (wpa_auth->conf.wpa_gmk_rekey) {
236 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
237 wpa_rekey_gmk, wpa_auth, NULL);
242 static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
244 struct wpa_authenticator *wpa_auth = eloop_ctx;
245 struct wpa_group *group;
247 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
248 for (group = wpa_auth->group; group; group = group->next) {
249 group->GTKReKey = TRUE;
251 group->changed = FALSE;
252 wpa_group_sm_step(wpa_auth, group);
253 } while (group->changed);
256 if (wpa_auth->conf.wpa_group_rekey) {
257 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
258 0, wpa_rekey_gtk, wpa_auth, NULL);
263 static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
265 struct wpa_authenticator *wpa_auth = eloop_ctx;
266 struct wpa_state_machine *sm = timeout_ctx;
268 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
269 wpa_request_new_ptk(sm);
274 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
276 if (sm->pmksa == ctx)
282 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
285 struct wpa_authenticator *wpa_auth = ctx;
286 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
290 static void wpa_group_set_key_len(struct wpa_group *group, int cipher)
293 case WPA_CIPHER_CCMP:
296 case WPA_CIPHER_TKIP:
299 case WPA_CIPHER_WEP104:
302 case WPA_CIPHER_WEP40:
309 static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
312 struct wpa_group *group;
313 u8 buf[ETH_ALEN + 8 + sizeof(group)];
316 group = os_zalloc(sizeof(struct wpa_group));
320 group->GTKAuthenticator = TRUE;
321 group->vlan_id = vlan_id;
323 wpa_group_set_key_len(group, wpa_auth->conf.wpa_group);
325 /* Counter = PRF-256(Random number, "Init Counter",
326 * Local MAC Address || Time)
328 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
329 wpa_get_ntp_timestamp(buf + ETH_ALEN);
330 os_memcpy(buf + ETH_ALEN + 8, &group, sizeof(group));
331 if (os_get_random(rkey, sizeof(rkey)) ||
332 os_get_random(group->GMK, WPA_GMK_LEN)) {
333 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
339 sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
340 group->Counter, WPA_NONCE_LEN);
343 wpa_group_sm_step(wpa_auth, group);
344 group->GInit = FALSE;
345 wpa_group_sm_step(wpa_auth, group);
352 * wpa_init - Initialize WPA authenticator
353 * @addr: Authenticator address
354 * @conf: Configuration for WPA authenticator
355 * @cb: Callback functions for WPA authenticator
356 * Returns: Pointer to WPA authenticator data or %NULL on failure
358 struct wpa_authenticator * wpa_init(const u8 *addr,
359 struct wpa_auth_config *conf,
360 struct wpa_auth_callbacks *cb)
362 struct wpa_authenticator *wpa_auth;
364 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
365 if (wpa_auth == NULL)
367 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
368 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
369 os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
371 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
372 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
377 wpa_auth->group = wpa_group_init(wpa_auth, 0);
378 if (wpa_auth->group == NULL) {
379 os_free(wpa_auth->wpa_ie);
384 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
386 if (wpa_auth->pmksa == NULL) {
387 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
388 os_free(wpa_auth->wpa_ie);
393 #ifdef CONFIG_IEEE80211R
394 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
395 if (wpa_auth->ft_pmk_cache == NULL) {
396 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
397 os_free(wpa_auth->wpa_ie);
398 pmksa_cache_auth_deinit(wpa_auth->pmksa);
402 #endif /* CONFIG_IEEE80211R */
404 if (wpa_auth->conf.wpa_gmk_rekey) {
405 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
406 wpa_rekey_gmk, wpa_auth, NULL);
409 if (wpa_auth->conf.wpa_group_rekey) {
410 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
411 wpa_rekey_gtk, wpa_auth, NULL);
419 * wpa_deinit - Deinitialize WPA authenticator
420 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
422 void wpa_deinit(struct wpa_authenticator *wpa_auth)
424 struct wpa_group *group, *prev;
426 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
427 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
429 #ifdef CONFIG_PEERKEY
430 while (wpa_auth->stsl_negotiations)
431 wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
432 #endif /* CONFIG_PEERKEY */
434 pmksa_cache_auth_deinit(wpa_auth->pmksa);
436 #ifdef CONFIG_IEEE80211R
437 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
438 wpa_auth->ft_pmk_cache = NULL;
439 #endif /* CONFIG_IEEE80211R */
441 os_free(wpa_auth->wpa_ie);
443 group = wpa_auth->group;
455 * wpa_reconfig - Update WPA authenticator configuration
456 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
457 * @conf: Configuration for WPA authenticator
459 int wpa_reconfig(struct wpa_authenticator *wpa_auth,
460 struct wpa_auth_config *conf)
462 struct wpa_group *group;
463 if (wpa_auth == NULL)
466 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
467 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
468 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
473 * Reinitialize GTK to make sure it is suitable for the new
476 group = wpa_auth->group;
477 wpa_group_set_key_len(group, wpa_auth->conf.wpa_group);
479 wpa_group_sm_step(wpa_auth, group);
480 group->GInit = FALSE;
481 wpa_group_sm_step(wpa_auth, group);
487 struct wpa_state_machine *
488 wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr)
490 struct wpa_state_machine *sm;
492 sm = os_zalloc(sizeof(struct wpa_state_machine));
495 os_memcpy(sm->addr, addr, ETH_ALEN);
497 sm->wpa_auth = wpa_auth;
498 sm->group = wpa_auth->group;
504 void wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
505 struct wpa_state_machine *sm)
507 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
510 #ifdef CONFIG_IEEE80211R
511 if (sm->ft_completed) {
512 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
513 "FT authentication already completed - do not "
514 "start 4-way handshake");
517 #endif /* CONFIG_IEEE80211R */
520 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
521 sm->ReAuthenticationRequest = TRUE;
526 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
527 "start authentication");
533 sm->AuthenticationRequest = TRUE;
538 void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
540 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
541 * reassociates back to the same AP while the previous entry for the
542 * STA has not yet been removed. */
546 sm->wpa_key_mgmt = 0;
550 static void wpa_free_sta_sm(struct wpa_state_machine *sm)
552 os_free(sm->last_rx_eapol_key);
558 void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
563 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
564 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
565 "strict rekeying - force GTK rekey since STA "
567 eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
568 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
572 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
573 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
574 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
575 if (sm->in_step_loop) {
576 /* Must not free state machine while wpa_sm_step() is running.
577 * Freeing will be completed in the end of wpa_sm_step(). */
578 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
579 "machine deinit for " MACSTR, MAC2STR(sm->addr));
580 sm->pending_deinit = 1;
586 static void wpa_request_new_ptk(struct wpa_state_machine *sm)
591 sm->PTKRequest = TRUE;
596 static int wpa_replay_counter_valid(struct wpa_state_machine *sm,
597 const u8 *replay_counter)
600 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
601 if (!sm->key_replay[i].valid)
603 if (os_memcmp(replay_counter, sm->key_replay[i].counter,
604 WPA_REPLAY_COUNTER_LEN) == 0)
611 void wpa_receive(struct wpa_authenticator *wpa_auth,
612 struct wpa_state_machine *sm,
613 u8 *data, size_t data_len)
615 struct ieee802_1x_hdr *hdr;
616 struct wpa_eapol_key *key;
617 u16 key_info, key_data_length;
618 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
619 SMK_M1, SMK_M3, SMK_ERROR } msg;
621 struct wpa_eapol_ie_parse kde;
623 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
626 if (data_len < sizeof(*hdr) + sizeof(*key))
629 hdr = (struct ieee802_1x_hdr *) data;
630 key = (struct wpa_eapol_key *) (hdr + 1);
631 key_info = WPA_GET_BE16(key->key_info);
632 key_data_length = WPA_GET_BE16(key->key_data_length);
633 if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) {
634 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
635 "key_data overflow (%d > %lu)",
637 (unsigned long) (data_len - sizeof(*hdr) -
642 if (sm->wpa == WPA_VERSION_WPA2) {
643 if (key->type != EAPOL_KEY_TYPE_RSN) {
644 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
645 "unexpected type %d in RSN mode",
650 if (key->type != EAPOL_KEY_TYPE_WPA) {
651 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
652 "unexpected type %d in WPA mode",
658 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
661 if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
662 (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
663 if (key_info & WPA_KEY_INFO_ERROR) {
665 msgtxt = "SMK Error";
670 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
673 } else if (key_info & WPA_KEY_INFO_REQUEST) {
676 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
678 msgtxt = "2/2 Group";
679 } else if (key_data_length == 0) {
681 msgtxt = "4/4 Pairwise";
684 msgtxt = "2/4 Pairwise";
687 /* TODO: key_info type validation for PeerKey */
688 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
690 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
691 if (sm->pairwise == WPA_CIPHER_CCMP) {
692 if (wpa_use_aes_cmac(sm) &&
693 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
694 wpa_auth_logger(wpa_auth, sm->addr,
696 "advertised support for "
697 "AES-128-CMAC, but did not "
702 if (!wpa_use_aes_cmac(sm) &&
703 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
704 wpa_auth_logger(wpa_auth, sm->addr,
706 "did not use HMAC-SHA1-AES "
713 if (key_info & WPA_KEY_INFO_REQUEST) {
714 if (sm->req_replay_counter_used &&
715 os_memcmp(key->replay_counter, sm->req_replay_counter,
716 WPA_REPLAY_COUNTER_LEN) <= 0) {
717 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
718 "received EAPOL-Key request with "
724 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
725 !wpa_replay_counter_valid(sm, key->replay_counter)) {
727 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
728 "received EAPOL-Key %s with unexpected "
729 "replay counter", msgtxt);
730 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
731 if (!sm->key_replay[i].valid)
733 wpa_hexdump(MSG_DEBUG, "pending replay counter",
734 sm->key_replay[i].counter,
735 WPA_REPLAY_COUNTER_LEN);
737 wpa_hexdump(MSG_DEBUG, "received replay counter",
738 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
744 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
745 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING) {
746 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
747 "received EAPOL-Key msg 2/4 in "
748 "invalid state (%d) - dropped",
752 if (sm->wpa_ie == NULL ||
753 sm->wpa_ie_len != key_data_length ||
754 os_memcmp(sm->wpa_ie, key + 1, key_data_length) != 0) {
755 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
756 "WPA IE from (Re)AssocReq did not "
757 "match with msg 2/4");
759 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
760 sm->wpa_ie, sm->wpa_ie_len);
762 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
763 (u8 *) (key + 1), key_data_length);
764 /* MLME-DEAUTHENTICATE.request */
765 wpa_sta_disconnect(wpa_auth, sm->addr);
770 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
772 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
773 "received EAPOL-Key msg 4/4 in "
774 "invalid state (%d) - dropped",
780 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
782 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
783 "received EAPOL-Key msg 2/2 in "
784 "invalid state (%d) - dropped",
785 sm->wpa_ptk_group_state);
789 #ifdef CONFIG_PEERKEY
793 if (!wpa_auth->conf.peerkey) {
794 wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
795 "PeerKey use disabled - ignoring message");
798 if (!sm->PTK_valid) {
799 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
800 "received EAPOL-Key msg SMK in "
801 "invalid state - dropped");
805 #else /* CONFIG_PEERKEY */
809 return; /* STSL disabled - ignore SMK messages */
810 #endif /* CONFIG_PEERKEY */
815 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
816 "received EAPOL-Key frame (%s)", msgtxt);
818 if (key_info & WPA_KEY_INFO_ACK) {
819 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
820 "received invalid EAPOL-Key: Key Ack set");
824 if (!(key_info & WPA_KEY_INFO_MIC)) {
825 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
826 "received invalid EAPOL-Key: Key MIC not set");
830 sm->MICVerified = FALSE;
832 if (wpa_verify_key_mic(&sm->PTK, data, data_len)) {
833 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
834 "received EAPOL-Key with invalid MIC");
837 sm->MICVerified = TRUE;
838 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
841 if (key_info & WPA_KEY_INFO_REQUEST) {
842 if (sm->MICVerified) {
843 sm->req_replay_counter_used = 1;
844 os_memcpy(sm->req_replay_counter, key->replay_counter,
845 WPA_REPLAY_COUNTER_LEN);
847 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
848 "received EAPOL-Key request with "
854 * TODO: should decrypt key data field if encryption was used;
855 * even though MAC address KDE is not normally encrypted,
856 * supplicant is allowed to encrypt it.
858 if (msg == SMK_ERROR) {
859 #ifdef CONFIG_PEERKEY
860 wpa_smk_error(wpa_auth, sm, key);
861 #endif /* CONFIG_PEERKEY */
863 } else if (key_info & WPA_KEY_INFO_ERROR) {
864 /* Supplicant reported a Michael MIC error */
865 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
866 "received EAPOL-Key Error Request "
867 "(STA detected Michael MIC failure)");
868 wpa_auth_mic_failure_report(wpa_auth, sm->addr);
869 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
870 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
871 /* Error report is not a request for a new key
872 * handshake, but since Authenticator may do it, let's
873 * change the keys now anyway. */
874 wpa_request_new_ptk(sm);
875 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
876 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
877 "received EAPOL-Key Request for new "
879 wpa_request_new_ptk(sm);
880 #ifdef CONFIG_PEERKEY
881 } else if (msg == SMK_M1) {
882 wpa_smk_m1(wpa_auth, sm, key);
883 #endif /* CONFIG_PEERKEY */
884 } else if (key_data_length > 0 &&
885 wpa_parse_kde_ies((const u8 *) (key + 1),
886 key_data_length, &kde) == 0 &&
889 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
890 "received EAPOL-Key Request for GTK "
892 /* FIX: why was this triggering PTK rekeying for the
893 * STA that requested Group Key rekeying?? */
894 /* wpa_request_new_ptk(sta->wpa_sm); */
895 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
896 wpa_rekey_gtk(wpa_auth, NULL);
899 /* Do not allow the same key replay counter to be reused. This
900 * does also invalidate all other pending replay counters if
901 * retransmissions were used, i.e., we will only process one of
902 * the pending replies and ignore rest if more than one is
904 sm->key_replay[0].valid = FALSE;
907 #ifdef CONFIG_PEERKEY
909 wpa_smk_m3(wpa_auth, sm, key);
912 #endif /* CONFIG_PEERKEY */
914 os_free(sm->last_rx_eapol_key);
915 sm->last_rx_eapol_key = os_malloc(data_len);
916 if (sm->last_rx_eapol_key == NULL)
918 os_memcpy(sm->last_rx_eapol_key, data, data_len);
919 sm->last_rx_eapol_key_len = data_len;
921 sm->EAPOLKeyReceived = TRUE;
922 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
923 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
924 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
929 static void wpa_gmk_to_gtk(const u8 *gmk, const u8 *addr, const u8 *gnonce,
930 u8 *gtk, size_t gtk_len)
932 u8 data[ETH_ALEN + WPA_NONCE_LEN];
934 /* GTK = PRF-X(GMK, "Group key expansion", AA || GNonce) */
935 os_memcpy(data, addr, ETH_ALEN);
936 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
938 #ifdef CONFIG_IEEE80211W
939 sha256_prf(gmk, WPA_GMK_LEN, "Group key expansion",
940 data, sizeof(data), gtk, gtk_len);
941 #else /* CONFIG_IEEE80211W */
942 sha1_prf(gmk, WPA_GMK_LEN, "Group key expansion",
943 data, sizeof(data), gtk, gtk_len);
944 #endif /* CONFIG_IEEE80211W */
946 wpa_hexdump_key(MSG_DEBUG, "GMK", gmk, WPA_GMK_LEN);
947 wpa_hexdump_key(MSG_DEBUG, "GTK", gtk, gtk_len);
951 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
953 struct wpa_authenticator *wpa_auth = eloop_ctx;
954 struct wpa_state_machine *sm = timeout_ctx;
956 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
957 sm->TimeoutEvt = TRUE;
962 void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
963 struct wpa_state_machine *sm, int key_info,
964 const u8 *key_rsc, const u8 *nonce,
965 const u8 *kde, size_t kde_len,
966 int keyidx, int encr, int force_version)
968 struct ieee802_1x_hdr *hdr;
969 struct wpa_eapol_key *key;
972 int key_data_len, pad_len = 0;
974 int version, pairwise;
977 len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
980 version = force_version;
981 else if (wpa_use_aes_cmac(sm))
982 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
983 else if (sm->pairwise == WPA_CIPHER_CCMP)
984 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
986 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
988 pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
990 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
991 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
994 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
995 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
996 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
997 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
998 pairwise, (unsigned long) kde_len, keyidx, encr);
1000 key_data_len = kde_len;
1002 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1003 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1004 pad_len = key_data_len % 8;
1006 pad_len = 8 - pad_len;
1007 key_data_len += pad_len + 8;
1010 len += key_data_len;
1012 hdr = os_zalloc(len);
1015 hdr->version = wpa_auth->conf.eapol_version;
1016 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1017 hdr->length = host_to_be16(len - sizeof(*hdr));
1018 key = (struct wpa_eapol_key *) (hdr + 1);
1020 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1021 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1022 key_info |= version;
1023 if (encr && sm->wpa == WPA_VERSION_WPA2)
1024 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1025 if (sm->wpa != WPA_VERSION_WPA2)
1026 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1027 WPA_PUT_BE16(key->key_info, key_info);
1029 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
1031 case WPA_CIPHER_CCMP:
1032 WPA_PUT_BE16(key->key_length, 16);
1034 case WPA_CIPHER_TKIP:
1035 WPA_PUT_BE16(key->key_length, 32);
1037 case WPA_CIPHER_WEP40:
1038 WPA_PUT_BE16(key->key_length, 5);
1040 case WPA_CIPHER_WEP104:
1041 WPA_PUT_BE16(key->key_length, 13);
1044 if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
1045 WPA_PUT_BE16(key->key_length, 0);
1047 /* FIX: STSL: what to use as key_replay_counter? */
1048 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1049 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1050 os_memcpy(sm->key_replay[i].counter,
1051 sm->key_replay[i - 1].counter,
1052 WPA_REPLAY_COUNTER_LEN);
1054 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1055 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1056 WPA_REPLAY_COUNTER_LEN);
1057 sm->key_replay[0].valid = TRUE;
1060 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1063 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1066 os_memcpy(key + 1, kde, kde_len);
1067 WPA_PUT_BE16(key->key_data_length, kde_len);
1068 } else if (encr && kde) {
1069 buf = os_zalloc(key_data_len);
1075 os_memcpy(pos, kde, kde_len);
1081 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1083 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1084 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1085 if (aes_wrap(sm->PTK.kek, (key_data_len - 8) / 8, buf,
1086 (u8 *) (key + 1))) {
1091 WPA_PUT_BE16(key->key_data_length, key_data_len);
1094 os_memcpy(key->key_iv,
1095 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1096 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1097 os_memcpy(ek, key->key_iv, 16);
1098 os_memcpy(ek + 16, sm->PTK.kek, 16);
1099 os_memcpy(key + 1, buf, key_data_len);
1100 rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
1101 WPA_PUT_BE16(key->key_data_length, key_data_len);
1106 if (key_info & WPA_KEY_INFO_MIC) {
1107 if (!sm->PTK_valid) {
1108 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1109 "PTK not valid when sending EAPOL-Key "
1114 wpa_eapol_key_mic(sm->PTK.kck, version, (u8 *) hdr, len,
1118 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1120 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1126 static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1127 struct wpa_state_machine *sm, int key_info,
1128 const u8 *key_rsc, const u8 *nonce,
1129 const u8 *kde, size_t kde_len,
1130 int keyidx, int encr)
1133 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1139 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1142 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1144 timeout_ms = eapol_key_timeout_first;
1146 timeout_ms = eapol_key_timeout_subseq;
1147 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1148 wpa_send_eapol_timeout, wpa_auth, sm);
1152 static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len)
1154 struct ieee802_1x_hdr *hdr;
1155 struct wpa_eapol_key *key;
1160 if (data_len < sizeof(*hdr) + sizeof(*key))
1163 hdr = (struct ieee802_1x_hdr *) data;
1164 key = (struct wpa_eapol_key *) (hdr + 1);
1165 key_info = WPA_GET_BE16(key->key_info);
1166 os_memcpy(mic, key->key_mic, 16);
1167 os_memset(key->key_mic, 0, 16);
1168 if (wpa_eapol_key_mic(PTK->kck, key_info & WPA_KEY_INFO_TYPE_MASK,
1169 data, data_len, key->key_mic) ||
1170 os_memcmp(mic, key->key_mic, 16) != 0)
1172 os_memcpy(key->key_mic, mic, 16);
1177 void wpa_remove_ptk(struct wpa_state_machine *sm)
1179 sm->PTK_valid = FALSE;
1180 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1181 wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, (u8 *) "",
1183 sm->pairwise_set = FALSE;
1184 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1188 void wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
1195 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1196 "event %d notification", event);
1204 sm->DeauthenticationRequest = TRUE;
1207 case WPA_REAUTH_EAPOL:
1208 if (sm->GUpdateStationKeys) {
1210 * Reauthentication cancels the pending group key
1211 * update for this STA.
1213 sm->group->GKeyDoneStations--;
1214 sm->GUpdateStationKeys = FALSE;
1215 sm->PtkGroupInit = TRUE;
1217 sm->ReAuthenticationRequest = TRUE;
1220 #ifdef CONFIG_IEEE80211R
1221 /* Using FT protocol, not WPA auth state machine */
1222 sm->ft_completed = 1;
1224 #else /* CONFIG_IEEE80211R */
1226 #endif /* CONFIG_IEEE80211R */
1229 #ifdef CONFIG_IEEE80211R
1230 sm->ft_completed = 0;
1231 #endif /* CONFIG_IEEE80211R */
1233 #ifdef CONFIG_IEEE80211W
1234 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1236 #endif /* CONFIG_IEEE80211W */
1239 sm->PTK_valid = FALSE;
1240 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1242 if (event != WPA_REAUTH_EAPOL)
1250 static wpa_alg wpa_alg_enum(int alg)
1253 case WPA_CIPHER_CCMP:
1254 return WPA_ALG_CCMP;
1255 case WPA_CIPHER_TKIP:
1256 return WPA_ALG_TKIP;
1257 case WPA_CIPHER_WEP104:
1258 case WPA_CIPHER_WEP40:
1261 return WPA_ALG_NONE;
1266 SM_STATE(WPA_PTK, INITIALIZE)
1268 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1270 /* Init flag is not cleared here, so avoid busy
1271 * loop by claiming nothing changed. */
1272 sm->changed = FALSE;
1276 if (sm->GUpdateStationKeys)
1277 sm->group->GKeyDoneStations--;
1278 sm->GUpdateStationKeys = FALSE;
1279 if (sm->wpa == WPA_VERSION_WPA)
1280 sm->PInitAKeys = FALSE;
1281 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1282 * Local AA > Remote AA)) */) {
1285 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1287 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1289 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1290 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1291 WPA_EAPOL_authorized, 0);
1296 SM_STATE(WPA_PTK, DISCONNECT)
1298 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1299 sm->Disconnect = FALSE;
1300 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1304 SM_STATE(WPA_PTK, DISCONNECTED)
1306 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1307 sm->DeauthenticationRequest = FALSE;
1311 SM_STATE(WPA_PTK, AUTHENTICATION)
1313 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1314 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1315 sm->PTK_valid = FALSE;
1316 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1318 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1319 sm->AuthenticationRequest = FALSE;
1323 SM_STATE(WPA_PTK, AUTHENTICATION2)
1325 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1326 os_memcpy(sm->ANonce, sm->group->Counter, WPA_NONCE_LEN);
1327 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1328 sm->ReAuthenticationRequest = FALSE;
1329 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1330 * logical place than INITIALIZE since AUTHENTICATION2 can be
1331 * re-entered on ReAuthenticationRequest without going through
1337 SM_STATE(WPA_PTK, INITPMK)
1339 u8 msk[2 * PMK_LEN];
1340 size_t len = 2 * PMK_LEN;
1342 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1343 #ifdef CONFIG_IEEE80211R
1345 #endif /* CONFIG_IEEE80211R */
1347 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1348 os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
1349 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1350 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1351 "(len=%lu)", (unsigned long) len);
1352 os_memcpy(sm->PMK, msk, PMK_LEN);
1353 #ifdef CONFIG_IEEE80211R
1354 if (len >= 2 * PMK_LEN) {
1355 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1356 sm->xxkey_len = PMK_LEN;
1358 #endif /* CONFIG_IEEE80211R */
1360 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
1363 sm->req_replay_counter_used = 0;
1364 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1365 * will break reauthentication since EAPOL state machines may not be
1366 * get into AUTHENTICATING state that clears keyRun before WPA state
1367 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1368 * state and takes PMK from the previously used AAA Key. This will
1369 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1370 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1371 * be good workaround for this issue. */
1372 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1376 SM_STATE(WPA_PTK, INITPSK)
1379 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
1380 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL);
1382 os_memcpy(sm->PMK, psk, PMK_LEN);
1383 #ifdef CONFIG_IEEE80211R
1384 os_memcpy(sm->xxkey, psk, PMK_LEN);
1385 sm->xxkey_len = PMK_LEN;
1386 #endif /* CONFIG_IEEE80211R */
1388 sm->req_replay_counter_used = 0;
1392 SM_STATE(WPA_PTK, PTKSTART)
1394 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1395 size_t pmkid_len = 0;
1397 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
1398 sm->PTKRequest = FALSE;
1399 sm->TimeoutEvt = FALSE;
1402 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1403 /* No point in sending the EAPOL-Key - we will disconnect
1404 * immediately following this. */
1408 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1409 "sending 1/4 msg of 4-Way Handshake");
1411 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1412 * one possible PSK for this STA.
1414 if (sm->wpa == WPA_VERSION_WPA2 &&
1415 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt)) {
1417 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
1418 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
1419 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
1420 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
1422 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
1423 sm->pmksa->pmkid, PMKID_LEN);
1426 * Calculate PMKID since no PMKSA cache entry was
1427 * available with pre-calculated PMKID.
1429 rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
1430 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
1431 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1434 wpa_send_eapol(sm->wpa_auth, sm,
1435 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
1436 sm->ANonce, pmkid, pmkid_len, 0, 0);
1440 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk,
1441 struct wpa_ptk *ptk)
1443 size_t ptk_len = sm->pairwise == WPA_CIPHER_CCMP ? 48 : 64;
1444 #ifdef CONFIG_IEEE80211R
1445 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
1446 return wpa_auth_derive_ptk_ft(sm, pmk, ptk, ptk_len);
1447 #endif /* CONFIG_IEEE80211R */
1449 wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
1450 sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce,
1451 (u8 *) ptk, ptk_len,
1452 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1458 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
1462 const u8 *pmk = NULL;
1464 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
1465 sm->EAPOLKeyReceived = FALSE;
1467 /* WPA with IEEE 802.1X: use the derived PMK from EAP
1468 * WPA-PSK: iterate through possible PSKs and select the one matching
1471 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1472 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, pmk);
1478 wpa_derive_ptk(sm, pmk, &PTK);
1480 if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key,
1481 sm->last_rx_eapol_key_len) == 0) {
1486 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
1491 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1492 "invalid MIC in msg 2/4 of 4-Way Handshake");
1496 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
1498 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1499 /* PSK may have changed from the previous choice, so update
1500 * state machine data based on whatever PSK was selected here.
1502 os_memcpy(sm->PMK, pmk, PMK_LEN);
1505 sm->MICVerified = TRUE;
1507 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
1508 sm->PTK_valid = TRUE;
1512 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
1514 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
1519 #ifdef CONFIG_IEEE80211W
1521 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1523 if (sm->mgmt_frame_prot) {
1524 return 2 + RSN_SELECTOR_LEN + sizeof(struct wpa_igtk_kde);
1531 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1533 struct wpa_igtk_kde igtk;
1534 struct wpa_group *gsm = sm->group;
1536 if (!sm->mgmt_frame_prot)
1539 igtk.keyid[0] = gsm->GN_igtk;
1541 if (wpa_auth_get_seqnum_igtk(sm->wpa_auth, NULL, gsm->GN_igtk, igtk.pn)
1543 os_memset(igtk.pn, 0, sizeof(igtk.pn));
1544 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
1545 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
1546 (const u8 *) &igtk, sizeof(igtk), NULL, 0);
1551 #else /* CONFIG_IEEE80211W */
1553 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1559 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1564 #endif /* CONFIG_IEEE80211W */
1567 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
1569 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
1570 size_t gtk_len, kde_len;
1571 struct wpa_group *gsm = sm->group;
1573 int wpa_ie_len, secure, keyidx, encr = 0;
1575 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
1576 sm->TimeoutEvt = FALSE;
1579 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1580 /* No point in sending the EAPOL-Key - we will disconnect
1581 * immediately following this. */
1585 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, GTK[GN])
1587 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
1588 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
1589 wpa_ie = sm->wpa_auth->wpa_ie;
1590 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
1591 if (sm->wpa == WPA_VERSION_WPA &&
1592 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
1593 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
1594 /* WPA-only STA, remove RSN IE */
1595 wpa_ie = wpa_ie + wpa_ie[1] + 2;
1596 wpa_ie_len = wpa_ie[1] + 2;
1598 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1599 "sending 3/4 msg of 4-Way Handshake");
1600 if (sm->wpa == WPA_VERSION_WPA2) {
1601 /* WPA2 send GTK in the 4-way handshake */
1603 gtk = gsm->GTK[gsm->GN - 1];
1604 gtk_len = gsm->GTK_len;
1609 /* WPA does not include GTK in msg 3/4 */
1617 kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
1619 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
1620 kde = os_malloc(kde_len);
1625 os_memcpy(pos, wpa_ie, wpa_ie_len);
1629 hdr[0] = keyidx & 0x03;
1631 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
1634 pos = ieee80211w_kde_add(sm, pos);
1636 wpa_send_eapol(sm->wpa_auth, sm,
1637 (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
1638 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
1639 WPA_KEY_INFO_KEY_TYPE,
1640 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
1645 SM_STATE(WPA_PTK, PTKINITDONE)
1647 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
1648 sm->EAPOLKeyReceived = FALSE;
1652 if (sm->pairwise == WPA_CIPHER_TKIP) {
1659 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
1660 sm->PTK.tk1, klen)) {
1661 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1664 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
1665 sm->pairwise_set = TRUE;
1667 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
1668 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1669 eloop_register_timeout(sm->wpa_auth->conf.
1670 wpa_ptk_rekey, 0, wpa_rekey_ptk,
1674 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1675 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1676 WPA_EAPOL_authorized, 1);
1680 if (0 /* IBSS == TRUE */) {
1682 if (sm->keycount == 2) {
1683 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1684 WPA_EAPOL_portValid, 1);
1687 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
1690 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
1691 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
1692 if (sm->wpa == WPA_VERSION_WPA)
1693 sm->PInitAKeys = TRUE;
1696 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1697 "pairwise key handshake completed (%s)",
1698 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
1700 #ifdef CONFIG_IEEE80211R
1701 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
1702 #endif /* CONFIG_IEEE80211R */
1708 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
1711 SM_ENTER(WPA_PTK, INITIALIZE);
1712 else if (sm->Disconnect
1713 /* || FIX: dot11RSNAConfigSALifetime timeout */)
1714 SM_ENTER(WPA_PTK, DISCONNECT);
1715 else if (sm->DeauthenticationRequest)
1716 SM_ENTER(WPA_PTK, DISCONNECTED);
1717 else if (sm->AuthenticationRequest)
1718 SM_ENTER(WPA_PTK, AUTHENTICATION);
1719 else if (sm->ReAuthenticationRequest)
1720 SM_ENTER(WPA_PTK, AUTHENTICATION2);
1721 else if (sm->PTKRequest)
1722 SM_ENTER(WPA_PTK, PTKSTART);
1723 else switch (sm->wpa_ptk_state) {
1724 case WPA_PTK_INITIALIZE:
1726 case WPA_PTK_DISCONNECT:
1727 SM_ENTER(WPA_PTK, DISCONNECTED);
1729 case WPA_PTK_DISCONNECTED:
1730 SM_ENTER(WPA_PTK, INITIALIZE);
1732 case WPA_PTK_AUTHENTICATION:
1733 SM_ENTER(WPA_PTK, AUTHENTICATION2);
1735 case WPA_PTK_AUTHENTICATION2:
1736 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
1737 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
1738 WPA_EAPOL_keyRun) > 0)
1739 SM_ENTER(WPA_PTK, INITPMK);
1740 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
1741 /* FIX: && 802.1X::keyRun */)
1742 SM_ENTER(WPA_PTK, INITPSK);
1744 case WPA_PTK_INITPMK:
1745 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
1746 WPA_EAPOL_keyAvailable) > 0)
1747 SM_ENTER(WPA_PTK, PTKSTART);
1749 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1750 SM_ENTER(WPA_PTK, DISCONNECT);
1753 case WPA_PTK_INITPSK:
1754 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL))
1755 SM_ENTER(WPA_PTK, PTKSTART);
1757 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1758 "no PSK configured for the STA");
1759 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1760 SM_ENTER(WPA_PTK, DISCONNECT);
1763 case WPA_PTK_PTKSTART:
1764 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1765 sm->EAPOLKeyPairwise)
1766 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
1767 else if (sm->TimeoutCtr >
1768 (int) dot11RSNAConfigPairwiseUpdateCount) {
1769 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1770 SM_ENTER(WPA_PTK, DISCONNECT);
1771 } else if (sm->TimeoutEvt)
1772 SM_ENTER(WPA_PTK, PTKSTART);
1774 case WPA_PTK_PTKCALCNEGOTIATING:
1775 if (sm->MICVerified)
1776 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
1777 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1778 sm->EAPOLKeyPairwise)
1779 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
1780 else if (sm->TimeoutEvt)
1781 SM_ENTER(WPA_PTK, PTKSTART);
1783 case WPA_PTK_PTKCALCNEGOTIATING2:
1784 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
1786 case WPA_PTK_PTKINITNEGOTIATING:
1787 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1788 sm->EAPOLKeyPairwise && sm->MICVerified)
1789 SM_ENTER(WPA_PTK, PTKINITDONE);
1790 else if (sm->TimeoutCtr >
1791 (int) dot11RSNAConfigPairwiseUpdateCount) {
1792 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1793 SM_ENTER(WPA_PTK, DISCONNECT);
1794 } else if (sm->TimeoutEvt)
1795 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
1797 case WPA_PTK_PTKINITDONE:
1803 SM_STATE(WPA_PTK_GROUP, IDLE)
1805 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
1807 /* Init flag is not cleared here, so avoid busy
1808 * loop by claiming nothing changed. */
1809 sm->changed = FALSE;
1811 sm->GTimeoutCtr = 0;
1815 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
1817 u8 rsc[WPA_KEY_RSC_LEN];
1818 struct wpa_group *gsm = sm->group;
1819 u8 *kde, *pos, hdr[2];
1822 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
1825 if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
1826 /* No point in sending the EAPOL-Key - we will disconnect
1827 * immediately following this. */
1831 if (sm->wpa == WPA_VERSION_WPA)
1832 sm->PInitAKeys = FALSE;
1833 sm->TimeoutEvt = FALSE;
1834 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
1835 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
1836 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
1837 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
1838 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1839 "sending 1/2 msg of Group Key Handshake");
1841 if (sm->wpa == WPA_VERSION_WPA2) {
1842 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
1843 ieee80211w_kde_len(sm);
1844 kde = os_malloc(kde_len);
1849 hdr[0] = gsm->GN & 0x03;
1851 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
1852 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
1853 pos = ieee80211w_kde_add(sm, pos);
1855 kde = gsm->GTK[gsm->GN - 1];
1856 pos = kde + gsm->GTK_len;
1859 wpa_send_eapol(sm->wpa_auth, sm,
1860 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
1862 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
1863 rsc, gsm->GNonce, kde, pos - kde, gsm->GN, 1);
1864 if (sm->wpa == WPA_VERSION_WPA2)
1869 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
1871 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
1872 sm->EAPOLKeyReceived = FALSE;
1873 if (sm->GUpdateStationKeys)
1874 sm->group->GKeyDoneStations--;
1875 sm->GUpdateStationKeys = FALSE;
1876 sm->GTimeoutCtr = 0;
1877 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
1878 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1879 "group key handshake completed (%s)",
1880 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
1885 SM_STATE(WPA_PTK_GROUP, KEYERROR)
1887 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
1888 if (sm->GUpdateStationKeys)
1889 sm->group->GKeyDoneStations--;
1890 sm->GUpdateStationKeys = FALSE;
1891 sm->Disconnect = TRUE;
1895 SM_STEP(WPA_PTK_GROUP)
1897 if (sm->Init || sm->PtkGroupInit) {
1898 SM_ENTER(WPA_PTK_GROUP, IDLE);
1899 sm->PtkGroupInit = FALSE;
1900 } else switch (sm->wpa_ptk_group_state) {
1901 case WPA_PTK_GROUP_IDLE:
1902 if (sm->GUpdateStationKeys ||
1903 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
1904 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
1906 case WPA_PTK_GROUP_REKEYNEGOTIATING:
1907 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1908 !sm->EAPOLKeyPairwise && sm->MICVerified)
1909 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
1910 else if (sm->GTimeoutCtr >
1911 (int) dot11RSNAConfigGroupUpdateCount)
1912 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
1913 else if (sm->TimeoutEvt)
1914 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
1916 case WPA_PTK_GROUP_KEYERROR:
1917 SM_ENTER(WPA_PTK_GROUP, IDLE);
1919 case WPA_PTK_GROUP_REKEYESTABLISHED:
1920 SM_ENTER(WPA_PTK_GROUP, IDLE);
1926 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
1927 struct wpa_group *group)
1931 /* FIX: is this the correct way of getting GNonce? */
1932 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
1933 inc_byte_array(group->Counter, WPA_NONCE_LEN);
1934 wpa_gmk_to_gtk(group->GMK, wpa_auth->addr, group->GNonce,
1935 group->GTK[group->GN - 1], group->GTK_len);
1937 #ifdef CONFIG_IEEE80211W
1938 if (wpa_auth->conf.ieee80211w != WPA_NO_IEEE80211W) {
1939 if (os_get_random(group->IGTK[group->GN_igtk - 4],
1940 WPA_IGTK_LEN) < 0) {
1941 wpa_printf(MSG_INFO, "RSN: Failed to get new random "
1945 wpa_hexdump_key(MSG_DEBUG, "IGTK",
1946 group->IGTK[group->GN_igtk - 4], WPA_IGTK_LEN);
1948 #endif /* CONFIG_IEEE80211W */
1954 static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
1955 struct wpa_group *group)
1957 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
1958 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
1959 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
1960 group->wpa_group_state = WPA_GROUP_GTK_INIT;
1963 os_memset(group->GTK, 0, sizeof(group->GTK));
1966 #ifdef CONFIG_IEEE80211W
1969 #endif /* CONFIG_IEEE80211W */
1970 /* GTK[GN] = CalcGTK() */
1971 wpa_gtk_update(wpa_auth, group);
1975 static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
1977 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
1978 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1979 "Not in PTKINITDONE; skip Group Key update");
1982 if (sm->GUpdateStationKeys) {
1984 * This should not really happen, but just in case, make sure
1985 * we do not count the same STA twice in GKeyDoneStations.
1987 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1988 "GUpdateStationKeys already set - do not "
1989 "increment GKeyDoneStations");
1991 sm->group->GKeyDoneStations++;
1992 sm->GUpdateStationKeys = TRUE;
1999 static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
2000 struct wpa_group *group)
2004 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2005 "SETKEYS (VLAN-ID %d)", group->vlan_id);
2006 group->changed = TRUE;
2007 group->wpa_group_state = WPA_GROUP_SETKEYS;
2008 group->GTKReKey = FALSE;
2010 group->GM = group->GN;
2012 #ifdef CONFIG_IEEE80211W
2013 tmp = group->GM_igtk;
2014 group->GM_igtk = group->GN_igtk;
2015 group->GN_igtk = tmp;
2016 #endif /* CONFIG_IEEE80211W */
2017 /* "GKeyDoneStations = GNoStations" is done in more robust way by
2018 * counting the STAs that are marked with GUpdateStationKeys instead of
2019 * including all STAs that could be in not-yet-completed state. */
2020 wpa_gtk_update(wpa_auth, group);
2022 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, NULL);
2023 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
2024 group->GKeyDoneStations);
2028 static void wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
2029 struct wpa_group *group)
2031 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2032 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
2033 group->changed = TRUE;
2034 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
2035 wpa_auth_set_key(wpa_auth, group->vlan_id,
2036 wpa_alg_enum(wpa_auth->conf.wpa_group),
2037 NULL, group->GN, group->GTK[group->GN - 1],
2040 #ifdef CONFIG_IEEE80211W
2041 if (wpa_auth->conf.ieee80211w != WPA_NO_IEEE80211W) {
2042 wpa_auth_set_key(wpa_auth, group->vlan_id, WPA_ALG_IGTK,
2043 NULL, group->GN_igtk,
2044 group->IGTK[group->GN_igtk - 4],
2047 #endif /* CONFIG_IEEE80211W */
2051 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
2052 struct wpa_group *group)
2055 wpa_group_gtk_init(wpa_auth, group);
2056 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
2057 group->GTKAuthenticator) {
2058 wpa_group_setkeysdone(wpa_auth, group);
2059 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
2061 wpa_group_setkeys(wpa_auth, group);
2062 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
2063 if (group->GKeyDoneStations == 0)
2064 wpa_group_setkeysdone(wpa_auth, group);
2065 else if (group->GTKReKey)
2066 wpa_group_setkeys(wpa_auth, group);
2071 static void wpa_sm_step(struct wpa_state_machine *sm)
2076 if (sm->in_step_loop) {
2077 /* This should not happen, but if it does, make sure we do not
2078 * end up freeing the state machine too early by exiting the
2079 * recursive call. */
2080 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
2084 sm->in_step_loop = 1;
2086 if (sm->pending_deinit)
2089 sm->changed = FALSE;
2090 sm->wpa_auth->group->changed = FALSE;
2092 SM_STEP_RUN(WPA_PTK);
2093 if (sm->pending_deinit)
2095 SM_STEP_RUN(WPA_PTK_GROUP);
2096 if (sm->pending_deinit)
2098 wpa_group_sm_step(sm->wpa_auth, sm->group);
2099 } while (sm->changed || sm->wpa_auth->group->changed);
2100 sm->in_step_loop = 0;
2102 if (sm->pending_deinit) {
2103 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
2104 "machine deinit for " MACSTR, MAC2STR(sm->addr));
2105 wpa_free_sta_sm(sm);
2110 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
2112 struct wpa_state_machine *sm = eloop_ctx;
2117 void wpa_auth_sm_notify(struct wpa_state_machine *sm)
2121 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
2125 void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
2128 struct wpa_group *group;
2130 if (wpa_auth == NULL)
2133 group = wpa_auth->group;
2135 for (i = 0; i < 2; i++) {
2137 group->GM = group->GN;
2139 #ifdef CONFIG_IEEE80211W
2140 tmp = group->GM_igtk;
2141 group->GM_igtk = group->GN_igtk;
2142 group->GN_igtk = tmp;
2143 #endif /* CONFIG_IEEE80211W */
2144 wpa_gtk_update(wpa_auth, group);
2149 static const char * wpa_bool_txt(int bool)
2151 return bool ? "TRUE" : "FALSE";
2155 static int wpa_cipher_bits(int cipher)
2158 case WPA_CIPHER_CCMP:
2160 case WPA_CIPHER_TKIP:
2162 case WPA_CIPHER_WEP104:
2164 case WPA_CIPHER_WEP40:
2172 #define RSN_SUITE "%02x-%02x-%02x-%d"
2173 #define RSN_SUITE_ARG(s) \
2174 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2176 int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
2179 char pmkid_txt[PMKID_LEN * 2 + 1];
2181 if (wpa_auth == NULL)
2184 ret = os_snprintf(buf + len, buflen - len,
2185 "dot11RSNAOptionImplemented=TRUE\n"
2186 #ifdef CONFIG_RSN_PREAUTH
2187 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2188 #else /* CONFIG_RSN_PREAUTH */
2189 "dot11RSNAPreauthenticationImplemented=FALSE\n"
2190 #endif /* CONFIG_RSN_PREAUTH */
2191 "dot11RSNAEnabled=%s\n"
2192 "dot11RSNAPreauthenticationEnabled=%s\n",
2193 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
2194 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
2195 if (ret < 0 || (size_t) ret >= buflen - len)
2199 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2200 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
2203 buf + len, buflen - len,
2204 "dot11RSNAConfigVersion=%u\n"
2205 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
2206 /* FIX: dot11RSNAConfigGroupCipher */
2207 /* FIX: dot11RSNAConfigGroupRekeyMethod */
2208 /* FIX: dot11RSNAConfigGroupRekeyTime */
2209 /* FIX: dot11RSNAConfigGroupRekeyPackets */
2210 "dot11RSNAConfigGroupRekeyStrict=%u\n"
2211 "dot11RSNAConfigGroupUpdateCount=%u\n"
2212 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
2213 "dot11RSNAConfigGroupCipherSize=%u\n"
2214 "dot11RSNAConfigPMKLifetime=%u\n"
2215 "dot11RSNAConfigPMKReauthThreshold=%u\n"
2216 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
2217 "dot11RSNAConfigSATimeout=%u\n"
2218 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2219 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2220 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2221 "dot11RSNAPMKIDUsed=%s\n"
2222 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2223 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2224 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2225 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
2226 "dot11RSNA4WayHandshakeFailures=%u\n"
2227 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
2229 !!wpa_auth->conf.wpa_strict_rekey,
2230 dot11RSNAConfigGroupUpdateCount,
2231 dot11RSNAConfigPairwiseUpdateCount,
2232 wpa_cipher_bits(wpa_auth->conf.wpa_group),
2233 dot11RSNAConfigPMKLifetime,
2234 dot11RSNAConfigPMKReauthThreshold,
2235 dot11RSNAConfigSATimeout,
2236 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
2237 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
2238 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
2240 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
2241 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
2242 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
2243 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
2244 wpa_auth->dot11RSNA4WayHandshakeFailures);
2245 if (ret < 0 || (size_t) ret >= buflen - len)
2249 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
2250 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
2253 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
2254 wpa_auth->group->wpa_group_state);
2255 if (ret < 0 || (size_t) ret >= buflen - len)
2263 int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
2271 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
2273 /* dot11RSNAStatsEntry */
2275 if (sm->wpa == WPA_VERSION_WPA) {
2276 if (sm->pairwise == WPA_CIPHER_CCMP)
2277 pairwise = WPA_CIPHER_SUITE_CCMP;
2278 else if (sm->pairwise == WPA_CIPHER_TKIP)
2279 pairwise = WPA_CIPHER_SUITE_TKIP;
2280 else if (sm->pairwise == WPA_CIPHER_WEP104)
2281 pairwise = WPA_CIPHER_SUITE_WEP104;
2282 else if (sm->pairwise == WPA_CIPHER_WEP40)
2283 pairwise = WPA_CIPHER_SUITE_WEP40;
2284 else if (sm->pairwise == WPA_CIPHER_NONE)
2285 pairwise = WPA_CIPHER_SUITE_NONE;
2286 } else if (sm->wpa == WPA_VERSION_WPA2) {
2287 if (sm->pairwise == WPA_CIPHER_CCMP)
2288 pairwise = RSN_CIPHER_SUITE_CCMP;
2289 else if (sm->pairwise == WPA_CIPHER_TKIP)
2290 pairwise = RSN_CIPHER_SUITE_TKIP;
2291 else if (sm->pairwise == WPA_CIPHER_WEP104)
2292 pairwise = RSN_CIPHER_SUITE_WEP104;
2293 else if (sm->pairwise == WPA_CIPHER_WEP40)
2294 pairwise = RSN_CIPHER_SUITE_WEP40;
2295 else if (sm->pairwise == WPA_CIPHER_NONE)
2296 pairwise = RSN_CIPHER_SUITE_NONE;
2301 buf + len, buflen - len,
2302 /* TODO: dot11RSNAStatsIndex */
2303 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
2304 "dot11RSNAStatsVersion=1\n"
2305 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
2306 /* TODO: dot11RSNAStatsTKIPICVErrors */
2307 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
2308 "dot11RSNAStatsTKIPRemoveMICFailures=%u\n"
2309 /* TODO: dot11RSNAStatsCCMPReplays */
2310 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
2311 /* TODO: dot11RSNAStatsTKIPReplays */,
2313 RSN_SUITE_ARG(pairwise),
2314 sm->dot11RSNAStatsTKIPLocalMICFailures,
2315 sm->dot11RSNAStatsTKIPRemoteMICFailures);
2316 if (ret < 0 || (size_t) ret >= buflen - len)
2321 ret = os_snprintf(buf + len, buflen - len,
2322 "hostapdWPAPTKState=%d\n"
2323 "hostapdWPAPTKGroupState=%d\n",
2325 sm->wpa_ptk_group_state);
2326 if (ret < 0 || (size_t) ret >= buflen - len)
2334 void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
2337 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
2341 int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
2343 return sm && sm->pairwise_set;
2347 int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
2349 return sm->pairwise;
2353 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
2357 return sm->wpa_key_mgmt;
2361 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
2369 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
2370 struct rsn_pmksa_cache_entry *entry)
2372 if (sm == NULL || sm->pmksa != entry)
2379 struct rsn_pmksa_cache_entry *
2380 wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
2382 return sm ? sm->pmksa : NULL;
2386 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
2389 sm->dot11RSNAStatsTKIPLocalMICFailures++;
2393 const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
2395 if (wpa_auth == NULL)
2397 *len = wpa_auth->wpa_ie_len;
2398 return wpa_auth->wpa_ie;
2402 int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
2403 int session_timeout, struct eapol_state_machine *eapol)
2405 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2)
2408 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
2409 sm->wpa_auth->addr, sm->addr, session_timeout,
2410 eapol, sm->wpa_key_mgmt))
2417 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
2418 const u8 *pmk, size_t len, const u8 *sta_addr,
2419 int session_timeout,
2420 struct eapol_state_machine *eapol)
2422 if (wpa_auth == NULL)
2425 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, wpa_auth->addr,
2426 sta_addr, session_timeout, eapol,
2427 WPA_KEY_MGMT_IEEE8021X))
2434 static struct wpa_group *
2435 wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
2437 struct wpa_group *group;
2439 if (wpa_auth == NULL || wpa_auth->group == NULL)
2442 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
2444 group = wpa_group_init(wpa_auth, vlan_id);
2448 group->next = wpa_auth->group->next;
2449 wpa_auth->group->next = group;
2455 int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
2457 struct wpa_group *group;
2459 if (sm == NULL || sm->wpa_auth == NULL)
2462 group = sm->wpa_auth->group;
2464 if (group->vlan_id == vlan_id)
2466 group = group->next;
2469 if (group == NULL) {
2470 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
2475 if (sm->group == group)
2478 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
2479 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
2485 #endif /* CONFIG_NATIVE_WINDOWS */