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.
15 #include "utils/includes.h"
17 #ifndef CONFIG_NATIVE_WINDOWS
19 #include "utils/common.h"
20 #include "utils/eloop.h"
21 #include "utils/state_machine.h"
22 #include "common/ieee802_11_defs.h"
23 #include "crypto/aes_wrap.h"
24 #include "crypto/crypto.h"
25 #include "crypto/sha1.h"
26 #include "crypto/sha256.h"
27 #include "eapol_auth/eapol_auth_sm.h"
28 #include "ap_config.h"
29 #include "ieee802_11.h"
31 #include "pmksa_cache_auth.h"
32 #include "wpa_auth_i.h"
33 #include "wpa_auth_ie.h"
35 #define STATE_MACHINE_DATA struct wpa_state_machine
36 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
37 #define STATE_MACHINE_ADDR sm->addr
40 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
41 static int wpa_sm_step(struct wpa_state_machine *sm);
42 static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len);
43 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
44 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
45 struct wpa_group *group);
46 static void wpa_request_new_ptk(struct wpa_state_machine *sm);
47 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
48 struct wpa_group *group);
50 static const u32 dot11RSNAConfigGroupUpdateCount = 4;
51 static const u32 dot11RSNAConfigPairwiseUpdateCount = 4;
52 static const u32 eapol_key_timeout_first = 100; /* ms */
53 static const u32 eapol_key_timeout_subseq = 1000; /* ms */
55 /* TODO: make these configurable */
56 static const int dot11RSNAConfigPMKLifetime = 43200;
57 static const int dot11RSNAConfigPMKReauthThreshold = 70;
58 static const int dot11RSNAConfigSATimeout = 60;
61 static inline void wpa_auth_mic_failure_report(
62 struct wpa_authenticator *wpa_auth, const u8 *addr)
64 if (wpa_auth->cb.mic_failure_report)
65 wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr);
69 static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
70 const u8 *addr, wpa_eapol_variable var,
73 if (wpa_auth->cb.set_eapol)
74 wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value);
78 static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
79 const u8 *addr, wpa_eapol_variable var)
81 if (wpa_auth->cb.get_eapol == NULL)
83 return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var);
87 static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
88 const u8 *addr, const u8 *prev_psk)
90 if (wpa_auth->cb.get_psk == NULL)
92 return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, prev_psk);
96 static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
97 const u8 *addr, u8 *msk, size_t *len)
99 if (wpa_auth->cb.get_msk == NULL)
101 return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len);
105 static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
107 enum wpa_alg alg, const u8 *addr, int idx,
108 u8 *key, size_t key_len)
110 if (wpa_auth->cb.set_key == NULL)
112 return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
117 static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
118 const u8 *addr, int idx, u8 *seq)
120 if (wpa_auth->cb.get_seqnum == NULL)
122 return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
127 wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
128 const u8 *data, size_t data_len, int encrypt)
130 if (wpa_auth->cb.send_eapol == NULL)
132 return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
137 int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
138 int (*cb)(struct wpa_state_machine *sm, void *ctx),
141 if (wpa_auth->cb.for_each_sta == NULL)
143 return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
147 int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
148 int (*cb)(struct wpa_authenticator *a, void *ctx),
151 if (wpa_auth->cb.for_each_auth == NULL)
153 return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
157 void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
158 logger_level level, const char *txt)
160 if (wpa_auth->cb.logger == NULL)
162 wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
166 void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
167 logger_level level, const char *fmt, ...)
173 if (wpa_auth->cb.logger == NULL)
176 maxlen = os_strlen(fmt) + 100;
177 format = os_malloc(maxlen);
182 vsnprintf(format, maxlen, fmt, ap);
185 wpa_auth_logger(wpa_auth, addr, level, format);
191 static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
194 if (wpa_auth->cb.disconnect == NULL)
196 wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
197 WLAN_REASON_PREV_AUTH_NOT_VALID);
201 static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
204 #ifdef CONFIG_IEEE80211R
205 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
207 #endif /* CONFIG_IEEE80211R */
208 #ifdef CONFIG_IEEE80211W
209 if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
211 #endif /* CONFIG_IEEE80211W */
216 static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
218 struct wpa_authenticator *wpa_auth = eloop_ctx;
220 if (os_get_random(wpa_auth->group->GMK, WPA_GMK_LEN)) {
221 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
224 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
227 if (wpa_auth->conf.wpa_gmk_rekey) {
228 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
229 wpa_rekey_gmk, wpa_auth, NULL);
234 static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
236 struct wpa_authenticator *wpa_auth = eloop_ctx;
237 struct wpa_group *group;
239 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
240 for (group = wpa_auth->group; group; group = group->next) {
241 group->GTKReKey = TRUE;
243 group->changed = FALSE;
244 wpa_group_sm_step(wpa_auth, group);
245 } while (group->changed);
248 if (wpa_auth->conf.wpa_group_rekey) {
249 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
250 0, wpa_rekey_gtk, wpa_auth, NULL);
255 static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
257 struct wpa_authenticator *wpa_auth = eloop_ctx;
258 struct wpa_state_machine *sm = timeout_ctx;
260 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
261 wpa_request_new_ptk(sm);
266 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
268 if (sm->pmksa == ctx)
274 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
277 struct wpa_authenticator *wpa_auth = ctx;
278 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
282 static void wpa_group_set_key_len(struct wpa_group *group, int cipher)
285 case WPA_CIPHER_CCMP:
288 case WPA_CIPHER_TKIP:
291 case WPA_CIPHER_WEP104:
294 case WPA_CIPHER_WEP40:
301 static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
304 struct wpa_group *group;
305 u8 buf[ETH_ALEN + 8 + sizeof(group)];
308 group = os_zalloc(sizeof(struct wpa_group));
312 group->GTKAuthenticator = TRUE;
313 group->vlan_id = vlan_id;
315 wpa_group_set_key_len(group, wpa_auth->conf.wpa_group);
317 /* Counter = PRF-256(Random number, "Init Counter",
318 * Local MAC Address || Time)
320 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
321 wpa_get_ntp_timestamp(buf + ETH_ALEN);
322 os_memcpy(buf + ETH_ALEN + 8, &group, sizeof(group));
323 if (os_get_random(rkey, sizeof(rkey)) ||
324 os_get_random(group->GMK, WPA_GMK_LEN)) {
325 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
331 sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
332 group->Counter, WPA_NONCE_LEN);
335 wpa_group_sm_step(wpa_auth, group);
336 group->GInit = FALSE;
337 wpa_group_sm_step(wpa_auth, group);
344 * wpa_init - Initialize WPA authenticator
345 * @addr: Authenticator address
346 * @conf: Configuration for WPA authenticator
347 * @cb: Callback functions for WPA authenticator
348 * Returns: Pointer to WPA authenticator data or %NULL on failure
350 struct wpa_authenticator * wpa_init(const u8 *addr,
351 struct wpa_auth_config *conf,
352 struct wpa_auth_callbacks *cb)
354 struct wpa_authenticator *wpa_auth;
356 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
357 if (wpa_auth == NULL)
359 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
360 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
361 os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
363 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
364 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
369 wpa_auth->group = wpa_group_init(wpa_auth, 0);
370 if (wpa_auth->group == NULL) {
371 os_free(wpa_auth->wpa_ie);
376 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
378 if (wpa_auth->pmksa == NULL) {
379 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
380 os_free(wpa_auth->wpa_ie);
385 #ifdef CONFIG_IEEE80211R
386 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
387 if (wpa_auth->ft_pmk_cache == NULL) {
388 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
389 os_free(wpa_auth->wpa_ie);
390 pmksa_cache_auth_deinit(wpa_auth->pmksa);
394 #endif /* CONFIG_IEEE80211R */
396 if (wpa_auth->conf.wpa_gmk_rekey) {
397 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
398 wpa_rekey_gmk, wpa_auth, NULL);
401 if (wpa_auth->conf.wpa_group_rekey) {
402 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
403 wpa_rekey_gtk, wpa_auth, NULL);
411 * wpa_deinit - Deinitialize WPA authenticator
412 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
414 void wpa_deinit(struct wpa_authenticator *wpa_auth)
416 struct wpa_group *group, *prev;
418 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
419 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
421 #ifdef CONFIG_PEERKEY
422 while (wpa_auth->stsl_negotiations)
423 wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
424 #endif /* CONFIG_PEERKEY */
426 pmksa_cache_auth_deinit(wpa_auth->pmksa);
428 #ifdef CONFIG_IEEE80211R
429 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
430 wpa_auth->ft_pmk_cache = NULL;
431 #endif /* CONFIG_IEEE80211R */
433 os_free(wpa_auth->wpa_ie);
435 group = wpa_auth->group;
447 * wpa_reconfig - Update WPA authenticator configuration
448 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
449 * @conf: Configuration for WPA authenticator
451 int wpa_reconfig(struct wpa_authenticator *wpa_auth,
452 struct wpa_auth_config *conf)
454 struct wpa_group *group;
455 if (wpa_auth == NULL)
458 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
459 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
460 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
465 * Reinitialize GTK to make sure it is suitable for the new
468 group = wpa_auth->group;
469 wpa_group_set_key_len(group, wpa_auth->conf.wpa_group);
471 wpa_group_sm_step(wpa_auth, group);
472 group->GInit = FALSE;
473 wpa_group_sm_step(wpa_auth, group);
479 struct wpa_state_machine *
480 wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr)
482 struct wpa_state_machine *sm;
484 sm = os_zalloc(sizeof(struct wpa_state_machine));
487 os_memcpy(sm->addr, addr, ETH_ALEN);
489 sm->wpa_auth = wpa_auth;
490 sm->group = wpa_auth->group;
496 int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
497 struct wpa_state_machine *sm)
499 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
502 #ifdef CONFIG_IEEE80211R
503 if (sm->ft_completed) {
504 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
505 "FT authentication already completed - do not "
506 "start 4-way handshake");
509 #endif /* CONFIG_IEEE80211R */
512 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
513 sm->ReAuthenticationRequest = TRUE;
514 return wpa_sm_step(sm);
517 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
518 "start authentication");
522 if (wpa_sm_step(sm) == 1)
523 return 1; /* should not really happen */
525 sm->AuthenticationRequest = TRUE;
526 return wpa_sm_step(sm);
530 void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
532 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
533 * reassociates back to the same AP while the previous entry for the
534 * STA has not yet been removed. */
538 sm->wpa_key_mgmt = 0;
542 static void wpa_free_sta_sm(struct wpa_state_machine *sm)
544 os_free(sm->last_rx_eapol_key);
550 void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
555 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
556 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
557 "strict rekeying - force GTK rekey since STA "
559 eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
560 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
564 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
565 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
566 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
567 if (sm->in_step_loop) {
568 /* Must not free state machine while wpa_sm_step() is running.
569 * Freeing will be completed in the end of wpa_sm_step(). */
570 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
571 "machine deinit for " MACSTR, MAC2STR(sm->addr));
572 sm->pending_deinit = 1;
578 static void wpa_request_new_ptk(struct wpa_state_machine *sm)
583 sm->PTKRequest = TRUE;
588 static int wpa_replay_counter_valid(struct wpa_state_machine *sm,
589 const u8 *replay_counter)
592 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
593 if (!sm->key_replay[i].valid)
595 if (os_memcmp(replay_counter, sm->key_replay[i].counter,
596 WPA_REPLAY_COUNTER_LEN) == 0)
603 void wpa_receive(struct wpa_authenticator *wpa_auth,
604 struct wpa_state_machine *sm,
605 u8 *data, size_t data_len)
607 struct ieee802_1x_hdr *hdr;
608 struct wpa_eapol_key *key;
609 u16 key_info, key_data_length;
610 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
611 SMK_M1, SMK_M3, SMK_ERROR } msg;
613 struct wpa_eapol_ie_parse kde;
615 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
618 if (data_len < sizeof(*hdr) + sizeof(*key))
621 hdr = (struct ieee802_1x_hdr *) data;
622 key = (struct wpa_eapol_key *) (hdr + 1);
623 key_info = WPA_GET_BE16(key->key_info);
624 key_data_length = WPA_GET_BE16(key->key_data_length);
625 if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) {
626 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
627 "key_data overflow (%d > %lu)",
629 (unsigned long) (data_len - sizeof(*hdr) -
634 if (sm->wpa == WPA_VERSION_WPA2) {
635 if (key->type != EAPOL_KEY_TYPE_RSN) {
636 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
637 "unexpected type %d in RSN mode",
642 if (key->type != EAPOL_KEY_TYPE_WPA) {
643 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
644 "unexpected type %d in WPA mode",
650 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
653 if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
654 (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
655 if (key_info & WPA_KEY_INFO_ERROR) {
657 msgtxt = "SMK Error";
662 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
665 } else if (key_info & WPA_KEY_INFO_REQUEST) {
668 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
670 msgtxt = "2/2 Group";
671 } else if (key_data_length == 0) {
673 msgtxt = "4/4 Pairwise";
676 msgtxt = "2/4 Pairwise";
679 /* TODO: key_info type validation for PeerKey */
680 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
682 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
683 if (sm->pairwise == WPA_CIPHER_CCMP) {
684 if (wpa_use_aes_cmac(sm) &&
685 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
686 wpa_auth_logger(wpa_auth, sm->addr,
688 "advertised support for "
689 "AES-128-CMAC, but did not "
694 if (!wpa_use_aes_cmac(sm) &&
695 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
696 wpa_auth_logger(wpa_auth, sm->addr,
698 "did not use HMAC-SHA1-AES "
705 if (key_info & WPA_KEY_INFO_REQUEST) {
706 if (sm->req_replay_counter_used &&
707 os_memcmp(key->replay_counter, sm->req_replay_counter,
708 WPA_REPLAY_COUNTER_LEN) <= 0) {
709 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
710 "received EAPOL-Key request with "
716 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
717 !wpa_replay_counter_valid(sm, key->replay_counter)) {
719 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
720 "received EAPOL-Key %s with unexpected "
721 "replay counter", msgtxt);
722 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
723 if (!sm->key_replay[i].valid)
725 wpa_hexdump(MSG_DEBUG, "pending replay counter",
726 sm->key_replay[i].counter,
727 WPA_REPLAY_COUNTER_LEN);
729 wpa_hexdump(MSG_DEBUG, "received replay counter",
730 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
736 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
737 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING) {
738 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
739 "received EAPOL-Key msg 2/4 in "
740 "invalid state (%d) - dropped",
744 if (sm->wpa_ie == NULL ||
745 sm->wpa_ie_len != key_data_length ||
746 os_memcmp(sm->wpa_ie, key + 1, key_data_length) != 0) {
747 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
748 "WPA IE from (Re)AssocReq did not "
749 "match with msg 2/4");
751 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
752 sm->wpa_ie, sm->wpa_ie_len);
754 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
755 (u8 *) (key + 1), key_data_length);
756 /* MLME-DEAUTHENTICATE.request */
757 wpa_sta_disconnect(wpa_auth, sm->addr);
762 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
764 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
765 "received EAPOL-Key msg 4/4 in "
766 "invalid state (%d) - dropped",
772 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
774 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
775 "received EAPOL-Key msg 2/2 in "
776 "invalid state (%d) - dropped",
777 sm->wpa_ptk_group_state);
781 #ifdef CONFIG_PEERKEY
785 if (!wpa_auth->conf.peerkey) {
786 wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
787 "PeerKey use disabled - ignoring message");
790 if (!sm->PTK_valid) {
791 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
792 "received EAPOL-Key msg SMK in "
793 "invalid state - dropped");
797 #else /* CONFIG_PEERKEY */
801 return; /* STSL disabled - ignore SMK messages */
802 #endif /* CONFIG_PEERKEY */
807 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
808 "received EAPOL-Key frame (%s)", msgtxt);
810 if (key_info & WPA_KEY_INFO_ACK) {
811 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
812 "received invalid EAPOL-Key: Key Ack set");
816 if (!(key_info & WPA_KEY_INFO_MIC)) {
817 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
818 "received invalid EAPOL-Key: Key MIC not set");
822 sm->MICVerified = FALSE;
824 if (wpa_verify_key_mic(&sm->PTK, data, data_len)) {
825 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
826 "received EAPOL-Key with invalid MIC");
829 sm->MICVerified = TRUE;
830 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
833 if (key_info & WPA_KEY_INFO_REQUEST) {
834 if (sm->MICVerified) {
835 sm->req_replay_counter_used = 1;
836 os_memcpy(sm->req_replay_counter, key->replay_counter,
837 WPA_REPLAY_COUNTER_LEN);
839 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
840 "received EAPOL-Key request with "
846 * TODO: should decrypt key data field if encryption was used;
847 * even though MAC address KDE is not normally encrypted,
848 * supplicant is allowed to encrypt it.
850 if (msg == SMK_ERROR) {
851 #ifdef CONFIG_PEERKEY
852 wpa_smk_error(wpa_auth, sm, key);
853 #endif /* CONFIG_PEERKEY */
855 } else if (key_info & WPA_KEY_INFO_ERROR) {
856 /* Supplicant reported a Michael MIC error */
857 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
858 "received EAPOL-Key Error Request "
859 "(STA detected Michael MIC failure)");
860 wpa_auth_mic_failure_report(wpa_auth, sm->addr);
861 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
862 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
863 /* Error report is not a request for a new key
864 * handshake, but since Authenticator may do it, let's
865 * change the keys now anyway. */
866 wpa_request_new_ptk(sm);
867 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
868 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
869 "received EAPOL-Key Request for new "
871 wpa_request_new_ptk(sm);
872 #ifdef CONFIG_PEERKEY
873 } else if (msg == SMK_M1) {
874 wpa_smk_m1(wpa_auth, sm, key);
875 #endif /* CONFIG_PEERKEY */
876 } else if (key_data_length > 0 &&
877 wpa_parse_kde_ies((const u8 *) (key + 1),
878 key_data_length, &kde) == 0 &&
881 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
882 "received EAPOL-Key Request for GTK "
884 /* FIX: why was this triggering PTK rekeying for the
885 * STA that requested Group Key rekeying?? */
886 /* wpa_request_new_ptk(sta->wpa_sm); */
887 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
888 wpa_rekey_gtk(wpa_auth, NULL);
891 /* Do not allow the same key replay counter to be reused. This
892 * does also invalidate all other pending replay counters if
893 * retransmissions were used, i.e., we will only process one of
894 * the pending replies and ignore rest if more than one is
896 sm->key_replay[0].valid = FALSE;
899 #ifdef CONFIG_PEERKEY
901 wpa_smk_m3(wpa_auth, sm, key);
904 #endif /* CONFIG_PEERKEY */
906 os_free(sm->last_rx_eapol_key);
907 sm->last_rx_eapol_key = os_malloc(data_len);
908 if (sm->last_rx_eapol_key == NULL)
910 os_memcpy(sm->last_rx_eapol_key, data, data_len);
911 sm->last_rx_eapol_key_len = data_len;
913 sm->EAPOLKeyReceived = TRUE;
914 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
915 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
916 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
921 static void wpa_gmk_to_gtk(const u8 *gmk, const u8 *addr, const u8 *gnonce,
922 u8 *gtk, size_t gtk_len)
924 u8 data[ETH_ALEN + WPA_NONCE_LEN];
926 /* GTK = PRF-X(GMK, "Group key expansion", AA || GNonce) */
927 os_memcpy(data, addr, ETH_ALEN);
928 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
930 #ifdef CONFIG_IEEE80211W
931 sha256_prf(gmk, WPA_GMK_LEN, "Group key expansion",
932 data, sizeof(data), gtk, gtk_len);
933 #else /* CONFIG_IEEE80211W */
934 sha1_prf(gmk, WPA_GMK_LEN, "Group key expansion",
935 data, sizeof(data), gtk, gtk_len);
936 #endif /* CONFIG_IEEE80211W */
938 wpa_hexdump_key(MSG_DEBUG, "GMK", gmk, WPA_GMK_LEN);
939 wpa_hexdump_key(MSG_DEBUG, "GTK", gtk, gtk_len);
943 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
945 struct wpa_authenticator *wpa_auth = eloop_ctx;
946 struct wpa_state_machine *sm = timeout_ctx;
948 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
949 sm->TimeoutEvt = TRUE;
954 void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
955 struct wpa_state_machine *sm, int key_info,
956 const u8 *key_rsc, const u8 *nonce,
957 const u8 *kde, size_t kde_len,
958 int keyidx, int encr, int force_version)
960 struct ieee802_1x_hdr *hdr;
961 struct wpa_eapol_key *key;
964 int key_data_len, pad_len = 0;
966 int version, pairwise;
969 len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
972 version = force_version;
973 else if (wpa_use_aes_cmac(sm))
974 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
975 else if (sm->pairwise == WPA_CIPHER_CCMP)
976 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
978 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
980 pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
982 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
983 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
986 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
987 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
988 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
989 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
990 pairwise, (unsigned long) kde_len, keyidx, encr);
992 key_data_len = kde_len;
994 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
995 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
996 pad_len = key_data_len % 8;
998 pad_len = 8 - pad_len;
999 key_data_len += pad_len + 8;
1002 len += key_data_len;
1004 hdr = os_zalloc(len);
1007 hdr->version = wpa_auth->conf.eapol_version;
1008 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1009 hdr->length = host_to_be16(len - sizeof(*hdr));
1010 key = (struct wpa_eapol_key *) (hdr + 1);
1012 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1013 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1014 key_info |= version;
1015 if (encr && sm->wpa == WPA_VERSION_WPA2)
1016 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1017 if (sm->wpa != WPA_VERSION_WPA2)
1018 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1019 WPA_PUT_BE16(key->key_info, key_info);
1021 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
1023 case WPA_CIPHER_CCMP:
1024 WPA_PUT_BE16(key->key_length, 16);
1026 case WPA_CIPHER_TKIP:
1027 WPA_PUT_BE16(key->key_length, 32);
1029 case WPA_CIPHER_WEP40:
1030 WPA_PUT_BE16(key->key_length, 5);
1032 case WPA_CIPHER_WEP104:
1033 WPA_PUT_BE16(key->key_length, 13);
1036 if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
1037 WPA_PUT_BE16(key->key_length, 0);
1039 /* FIX: STSL: what to use as key_replay_counter? */
1040 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1041 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1042 os_memcpy(sm->key_replay[i].counter,
1043 sm->key_replay[i - 1].counter,
1044 WPA_REPLAY_COUNTER_LEN);
1046 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1047 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1048 WPA_REPLAY_COUNTER_LEN);
1049 sm->key_replay[0].valid = TRUE;
1052 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1055 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1058 os_memcpy(key + 1, kde, kde_len);
1059 WPA_PUT_BE16(key->key_data_length, kde_len);
1060 } else if (encr && kde) {
1061 buf = os_zalloc(key_data_len);
1067 os_memcpy(pos, kde, kde_len);
1073 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1075 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1076 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1077 if (aes_wrap(sm->PTK.kek, (key_data_len - 8) / 8, buf,
1078 (u8 *) (key + 1))) {
1083 WPA_PUT_BE16(key->key_data_length, key_data_len);
1086 os_memcpy(key->key_iv,
1087 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1088 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1089 os_memcpy(ek, key->key_iv, 16);
1090 os_memcpy(ek + 16, sm->PTK.kek, 16);
1091 os_memcpy(key + 1, buf, key_data_len);
1092 rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
1093 WPA_PUT_BE16(key->key_data_length, key_data_len);
1098 if (key_info & WPA_KEY_INFO_MIC) {
1099 if (!sm->PTK_valid) {
1100 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1101 "PTK not valid when sending EAPOL-Key "
1106 wpa_eapol_key_mic(sm->PTK.kck, version, (u8 *) hdr, len,
1110 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1112 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1118 static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1119 struct wpa_state_machine *sm, int key_info,
1120 const u8 *key_rsc, const u8 *nonce,
1121 const u8 *kde, size_t kde_len,
1122 int keyidx, int encr)
1125 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1131 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1134 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1136 timeout_ms = eapol_key_timeout_first;
1138 timeout_ms = eapol_key_timeout_subseq;
1139 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1140 wpa_send_eapol_timeout, wpa_auth, sm);
1144 static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len)
1146 struct ieee802_1x_hdr *hdr;
1147 struct wpa_eapol_key *key;
1152 if (data_len < sizeof(*hdr) + sizeof(*key))
1155 hdr = (struct ieee802_1x_hdr *) data;
1156 key = (struct wpa_eapol_key *) (hdr + 1);
1157 key_info = WPA_GET_BE16(key->key_info);
1158 os_memcpy(mic, key->key_mic, 16);
1159 os_memset(key->key_mic, 0, 16);
1160 if (wpa_eapol_key_mic(PTK->kck, key_info & WPA_KEY_INFO_TYPE_MASK,
1161 data, data_len, key->key_mic) ||
1162 os_memcmp(mic, key->key_mic, 16) != 0)
1164 os_memcpy(key->key_mic, mic, 16);
1169 void wpa_remove_ptk(struct wpa_state_machine *sm)
1171 sm->PTK_valid = FALSE;
1172 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1173 wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, (u8 *) "",
1175 sm->pairwise_set = FALSE;
1176 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1180 int wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
1187 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1188 "event %d notification", event);
1196 sm->DeauthenticationRequest = TRUE;
1199 case WPA_REAUTH_EAPOL:
1200 if (sm->GUpdateStationKeys) {
1202 * Reauthentication cancels the pending group key
1203 * update for this STA.
1205 sm->group->GKeyDoneStations--;
1206 sm->GUpdateStationKeys = FALSE;
1207 sm->PtkGroupInit = TRUE;
1209 sm->ReAuthenticationRequest = TRUE;
1212 #ifdef CONFIG_IEEE80211R
1213 if (!sm->pairwise_set) {
1214 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1215 "after association");
1216 wpa_ft_install_ptk(sm);
1218 /* Using FT protocol, not WPA auth state machine */
1219 sm->ft_completed = 1;
1221 #else /* CONFIG_IEEE80211R */
1223 #endif /* CONFIG_IEEE80211R */
1226 #ifdef CONFIG_IEEE80211R
1227 sm->ft_completed = 0;
1228 #endif /* CONFIG_IEEE80211R */
1230 #ifdef CONFIG_IEEE80211W
1231 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1233 #endif /* CONFIG_IEEE80211W */
1236 sm->PTK_valid = FALSE;
1237 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1239 if (event != WPA_REAUTH_EAPOL)
1243 return wpa_sm_step(sm);
1247 static enum wpa_alg wpa_alg_enum(int alg)
1250 case WPA_CIPHER_CCMP:
1251 return WPA_ALG_CCMP;
1252 case WPA_CIPHER_TKIP:
1253 return WPA_ALG_TKIP;
1254 case WPA_CIPHER_WEP104:
1255 case WPA_CIPHER_WEP40:
1258 return WPA_ALG_NONE;
1263 SM_STATE(WPA_PTK, INITIALIZE)
1265 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1267 /* Init flag is not cleared here, so avoid busy
1268 * loop by claiming nothing changed. */
1269 sm->changed = FALSE;
1273 if (sm->GUpdateStationKeys)
1274 sm->group->GKeyDoneStations--;
1275 sm->GUpdateStationKeys = FALSE;
1276 if (sm->wpa == WPA_VERSION_WPA)
1277 sm->PInitAKeys = FALSE;
1278 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1279 * Local AA > Remote AA)) */) {
1282 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1284 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1286 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1287 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1288 WPA_EAPOL_authorized, 0);
1293 SM_STATE(WPA_PTK, DISCONNECT)
1295 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1296 sm->Disconnect = FALSE;
1297 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1301 SM_STATE(WPA_PTK, DISCONNECTED)
1303 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1304 sm->DeauthenticationRequest = FALSE;
1308 SM_STATE(WPA_PTK, AUTHENTICATION)
1310 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1311 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1312 sm->PTK_valid = FALSE;
1313 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1315 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1316 sm->AuthenticationRequest = FALSE;
1320 SM_STATE(WPA_PTK, AUTHENTICATION2)
1322 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1323 os_memcpy(sm->ANonce, sm->group->Counter, WPA_NONCE_LEN);
1324 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1325 sm->ReAuthenticationRequest = FALSE;
1326 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1327 * logical place than INITIALIZE since AUTHENTICATION2 can be
1328 * re-entered on ReAuthenticationRequest without going through
1334 SM_STATE(WPA_PTK, INITPMK)
1336 u8 msk[2 * PMK_LEN];
1337 size_t len = 2 * PMK_LEN;
1339 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1340 #ifdef CONFIG_IEEE80211R
1342 #endif /* CONFIG_IEEE80211R */
1344 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1345 os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
1346 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1347 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1348 "(len=%lu)", (unsigned long) len);
1349 os_memcpy(sm->PMK, msk, PMK_LEN);
1350 #ifdef CONFIG_IEEE80211R
1351 if (len >= 2 * PMK_LEN) {
1352 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1353 sm->xxkey_len = PMK_LEN;
1355 #endif /* CONFIG_IEEE80211R */
1357 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
1360 sm->req_replay_counter_used = 0;
1361 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1362 * will break reauthentication since EAPOL state machines may not be
1363 * get into AUTHENTICATING state that clears keyRun before WPA state
1364 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1365 * state and takes PMK from the previously used AAA Key. This will
1366 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1367 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1368 * be good workaround for this issue. */
1369 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1373 SM_STATE(WPA_PTK, INITPSK)
1376 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
1377 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL);
1379 os_memcpy(sm->PMK, psk, PMK_LEN);
1380 #ifdef CONFIG_IEEE80211R
1381 os_memcpy(sm->xxkey, psk, PMK_LEN);
1382 sm->xxkey_len = PMK_LEN;
1383 #endif /* CONFIG_IEEE80211R */
1385 sm->req_replay_counter_used = 0;
1389 SM_STATE(WPA_PTK, PTKSTART)
1391 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1392 size_t pmkid_len = 0;
1394 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
1395 sm->PTKRequest = FALSE;
1396 sm->TimeoutEvt = FALSE;
1399 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1400 /* No point in sending the EAPOL-Key - we will disconnect
1401 * immediately following this. */
1405 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1406 "sending 1/4 msg of 4-Way Handshake");
1408 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1409 * one possible PSK for this STA.
1411 if (sm->wpa == WPA_VERSION_WPA2 &&
1412 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt)) {
1414 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
1415 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
1416 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
1417 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
1419 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
1420 sm->pmksa->pmkid, PMKID_LEN);
1423 * Calculate PMKID since no PMKSA cache entry was
1424 * available with pre-calculated PMKID.
1426 rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
1427 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
1428 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1431 wpa_send_eapol(sm->wpa_auth, sm,
1432 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
1433 sm->ANonce, pmkid, pmkid_len, 0, 0);
1437 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk,
1438 struct wpa_ptk *ptk)
1440 size_t ptk_len = sm->pairwise == WPA_CIPHER_CCMP ? 48 : 64;
1441 #ifdef CONFIG_IEEE80211R
1442 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
1443 return wpa_auth_derive_ptk_ft(sm, pmk, ptk, ptk_len);
1444 #endif /* CONFIG_IEEE80211R */
1446 wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
1447 sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce,
1448 (u8 *) ptk, ptk_len,
1449 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1455 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
1459 const u8 *pmk = NULL;
1461 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
1462 sm->EAPOLKeyReceived = FALSE;
1464 /* WPA with IEEE 802.1X: use the derived PMK from EAP
1465 * WPA-PSK: iterate through possible PSKs and select the one matching
1468 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1469 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, pmk);
1475 wpa_derive_ptk(sm, pmk, &PTK);
1477 if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key,
1478 sm->last_rx_eapol_key_len) == 0) {
1483 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
1488 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1489 "invalid MIC in msg 2/4 of 4-Way Handshake");
1493 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
1495 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1496 /* PSK may have changed from the previous choice, so update
1497 * state machine data based on whatever PSK was selected here.
1499 os_memcpy(sm->PMK, pmk, PMK_LEN);
1502 sm->MICVerified = TRUE;
1504 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
1505 sm->PTK_valid = TRUE;
1509 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
1511 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
1516 #ifdef CONFIG_IEEE80211W
1518 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1520 if (sm->mgmt_frame_prot) {
1521 return 2 + RSN_SELECTOR_LEN + sizeof(struct wpa_igtk_kde);
1528 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1530 struct wpa_igtk_kde igtk;
1531 struct wpa_group *gsm = sm->group;
1533 if (!sm->mgmt_frame_prot)
1536 igtk.keyid[0] = gsm->GN_igtk;
1538 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, igtk.pn) < 0)
1539 os_memset(igtk.pn, 0, sizeof(igtk.pn));
1540 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
1541 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
1542 (const u8 *) &igtk, sizeof(igtk), NULL, 0);
1547 #else /* CONFIG_IEEE80211W */
1549 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1555 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1560 #endif /* CONFIG_IEEE80211W */
1563 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
1565 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
1566 size_t gtk_len, kde_len;
1567 struct wpa_group *gsm = sm->group;
1569 int wpa_ie_len, secure, keyidx, encr = 0;
1571 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
1572 sm->TimeoutEvt = FALSE;
1575 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1576 /* No point in sending the EAPOL-Key - we will disconnect
1577 * immediately following this. */
1581 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, GTK[GN])
1583 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
1584 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
1585 wpa_ie = sm->wpa_auth->wpa_ie;
1586 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
1587 if (sm->wpa == WPA_VERSION_WPA &&
1588 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
1589 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
1590 /* WPA-only STA, remove RSN IE */
1591 wpa_ie = wpa_ie + wpa_ie[1] + 2;
1592 wpa_ie_len = wpa_ie[1] + 2;
1594 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1595 "sending 3/4 msg of 4-Way Handshake");
1596 if (sm->wpa == WPA_VERSION_WPA2) {
1597 /* WPA2 send GTK in the 4-way handshake */
1599 gtk = gsm->GTK[gsm->GN - 1];
1600 gtk_len = gsm->GTK_len;
1605 /* WPA does not include GTK in msg 3/4 */
1613 kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
1615 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
1616 kde = os_malloc(kde_len);
1621 os_memcpy(pos, wpa_ie, wpa_ie_len);
1625 hdr[0] = keyidx & 0x03;
1627 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
1630 pos = ieee80211w_kde_add(sm, pos);
1632 wpa_send_eapol(sm->wpa_auth, sm,
1633 (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
1634 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
1635 WPA_KEY_INFO_KEY_TYPE,
1636 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
1641 SM_STATE(WPA_PTK, PTKINITDONE)
1643 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
1644 sm->EAPOLKeyReceived = FALSE;
1648 if (sm->pairwise == WPA_CIPHER_TKIP) {
1655 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
1656 sm->PTK.tk1, klen)) {
1657 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1660 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
1661 sm->pairwise_set = TRUE;
1663 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
1664 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1665 eloop_register_timeout(sm->wpa_auth->conf.
1666 wpa_ptk_rekey, 0, wpa_rekey_ptk,
1670 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1671 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1672 WPA_EAPOL_authorized, 1);
1676 if (0 /* IBSS == TRUE */) {
1678 if (sm->keycount == 2) {
1679 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1680 WPA_EAPOL_portValid, 1);
1683 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
1686 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
1687 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
1688 if (sm->wpa == WPA_VERSION_WPA)
1689 sm->PInitAKeys = TRUE;
1692 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1693 "pairwise key handshake completed (%s)",
1694 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
1696 #ifdef CONFIG_IEEE80211R
1697 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
1698 #endif /* CONFIG_IEEE80211R */
1704 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
1707 SM_ENTER(WPA_PTK, INITIALIZE);
1708 else if (sm->Disconnect
1709 /* || FIX: dot11RSNAConfigSALifetime timeout */)
1710 SM_ENTER(WPA_PTK, DISCONNECT);
1711 else if (sm->DeauthenticationRequest)
1712 SM_ENTER(WPA_PTK, DISCONNECTED);
1713 else if (sm->AuthenticationRequest)
1714 SM_ENTER(WPA_PTK, AUTHENTICATION);
1715 else if (sm->ReAuthenticationRequest)
1716 SM_ENTER(WPA_PTK, AUTHENTICATION2);
1717 else if (sm->PTKRequest)
1718 SM_ENTER(WPA_PTK, PTKSTART);
1719 else switch (sm->wpa_ptk_state) {
1720 case WPA_PTK_INITIALIZE:
1722 case WPA_PTK_DISCONNECT:
1723 SM_ENTER(WPA_PTK, DISCONNECTED);
1725 case WPA_PTK_DISCONNECTED:
1726 SM_ENTER(WPA_PTK, INITIALIZE);
1728 case WPA_PTK_AUTHENTICATION:
1729 SM_ENTER(WPA_PTK, AUTHENTICATION2);
1731 case WPA_PTK_AUTHENTICATION2:
1732 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
1733 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
1734 WPA_EAPOL_keyRun) > 0)
1735 SM_ENTER(WPA_PTK, INITPMK);
1736 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
1737 /* FIX: && 802.1X::keyRun */)
1738 SM_ENTER(WPA_PTK, INITPSK);
1740 case WPA_PTK_INITPMK:
1741 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
1742 WPA_EAPOL_keyAvailable) > 0)
1743 SM_ENTER(WPA_PTK, PTKSTART);
1745 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1746 SM_ENTER(WPA_PTK, DISCONNECT);
1749 case WPA_PTK_INITPSK:
1750 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL))
1751 SM_ENTER(WPA_PTK, PTKSTART);
1753 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1754 "no PSK configured for the STA");
1755 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1756 SM_ENTER(WPA_PTK, DISCONNECT);
1759 case WPA_PTK_PTKSTART:
1760 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1761 sm->EAPOLKeyPairwise)
1762 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
1763 else if (sm->TimeoutCtr >
1764 (int) dot11RSNAConfigPairwiseUpdateCount) {
1765 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1766 SM_ENTER(WPA_PTK, DISCONNECT);
1767 } else if (sm->TimeoutEvt)
1768 SM_ENTER(WPA_PTK, PTKSTART);
1770 case WPA_PTK_PTKCALCNEGOTIATING:
1771 if (sm->MICVerified)
1772 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
1773 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1774 sm->EAPOLKeyPairwise)
1775 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
1776 else if (sm->TimeoutEvt)
1777 SM_ENTER(WPA_PTK, PTKSTART);
1779 case WPA_PTK_PTKCALCNEGOTIATING2:
1780 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
1782 case WPA_PTK_PTKINITNEGOTIATING:
1783 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1784 sm->EAPOLKeyPairwise && sm->MICVerified)
1785 SM_ENTER(WPA_PTK, PTKINITDONE);
1786 else if (sm->TimeoutCtr >
1787 (int) dot11RSNAConfigPairwiseUpdateCount) {
1788 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1789 SM_ENTER(WPA_PTK, DISCONNECT);
1790 } else if (sm->TimeoutEvt)
1791 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
1793 case WPA_PTK_PTKINITDONE:
1799 SM_STATE(WPA_PTK_GROUP, IDLE)
1801 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
1803 /* Init flag is not cleared here, so avoid busy
1804 * loop by claiming nothing changed. */
1805 sm->changed = FALSE;
1807 sm->GTimeoutCtr = 0;
1811 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
1813 u8 rsc[WPA_KEY_RSC_LEN];
1814 struct wpa_group *gsm = sm->group;
1815 u8 *kde, *pos, hdr[2];
1818 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
1821 if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
1822 /* No point in sending the EAPOL-Key - we will disconnect
1823 * immediately following this. */
1827 if (sm->wpa == WPA_VERSION_WPA)
1828 sm->PInitAKeys = FALSE;
1829 sm->TimeoutEvt = FALSE;
1830 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
1831 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
1832 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
1833 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
1834 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1835 "sending 1/2 msg of Group Key Handshake");
1837 if (sm->wpa == WPA_VERSION_WPA2) {
1838 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
1839 ieee80211w_kde_len(sm);
1840 kde = os_malloc(kde_len);
1845 hdr[0] = gsm->GN & 0x03;
1847 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
1848 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
1849 pos = ieee80211w_kde_add(sm, pos);
1851 kde = gsm->GTK[gsm->GN - 1];
1852 pos = kde + gsm->GTK_len;
1855 wpa_send_eapol(sm->wpa_auth, sm,
1856 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
1858 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
1859 rsc, gsm->GNonce, kde, pos - kde, gsm->GN, 1);
1860 if (sm->wpa == WPA_VERSION_WPA2)
1865 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
1867 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
1868 sm->EAPOLKeyReceived = FALSE;
1869 if (sm->GUpdateStationKeys)
1870 sm->group->GKeyDoneStations--;
1871 sm->GUpdateStationKeys = FALSE;
1872 sm->GTimeoutCtr = 0;
1873 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
1874 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1875 "group key handshake completed (%s)",
1876 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
1881 SM_STATE(WPA_PTK_GROUP, KEYERROR)
1883 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
1884 if (sm->GUpdateStationKeys)
1885 sm->group->GKeyDoneStations--;
1886 sm->GUpdateStationKeys = FALSE;
1887 sm->Disconnect = TRUE;
1891 SM_STEP(WPA_PTK_GROUP)
1893 if (sm->Init || sm->PtkGroupInit) {
1894 SM_ENTER(WPA_PTK_GROUP, IDLE);
1895 sm->PtkGroupInit = FALSE;
1896 } else switch (sm->wpa_ptk_group_state) {
1897 case WPA_PTK_GROUP_IDLE:
1898 if (sm->GUpdateStationKeys ||
1899 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
1900 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
1902 case WPA_PTK_GROUP_REKEYNEGOTIATING:
1903 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1904 !sm->EAPOLKeyPairwise && sm->MICVerified)
1905 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
1906 else if (sm->GTimeoutCtr >
1907 (int) dot11RSNAConfigGroupUpdateCount)
1908 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
1909 else if (sm->TimeoutEvt)
1910 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
1912 case WPA_PTK_GROUP_KEYERROR:
1913 SM_ENTER(WPA_PTK_GROUP, IDLE);
1915 case WPA_PTK_GROUP_REKEYESTABLISHED:
1916 SM_ENTER(WPA_PTK_GROUP, IDLE);
1922 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
1923 struct wpa_group *group)
1927 /* FIX: is this the correct way of getting GNonce? */
1928 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
1929 inc_byte_array(group->Counter, WPA_NONCE_LEN);
1930 wpa_gmk_to_gtk(group->GMK, wpa_auth->addr, group->GNonce,
1931 group->GTK[group->GN - 1], group->GTK_len);
1933 #ifdef CONFIG_IEEE80211W
1934 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
1935 if (os_get_random(group->IGTK[group->GN_igtk - 4],
1936 WPA_IGTK_LEN) < 0) {
1937 wpa_printf(MSG_INFO, "RSN: Failed to get new random "
1941 wpa_hexdump_key(MSG_DEBUG, "IGTK",
1942 group->IGTK[group->GN_igtk - 4], WPA_IGTK_LEN);
1944 #endif /* CONFIG_IEEE80211W */
1950 static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
1951 struct wpa_group *group)
1953 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
1954 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
1955 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
1956 group->wpa_group_state = WPA_GROUP_GTK_INIT;
1959 os_memset(group->GTK, 0, sizeof(group->GTK));
1962 #ifdef CONFIG_IEEE80211W
1965 #endif /* CONFIG_IEEE80211W */
1966 /* GTK[GN] = CalcGTK() */
1967 wpa_gtk_update(wpa_auth, group);
1971 static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
1973 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
1974 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1975 "Not in PTKINITDONE; skip Group Key update");
1978 if (sm->GUpdateStationKeys) {
1980 * This should not really happen, but just in case, make sure
1981 * we do not count the same STA twice in GKeyDoneStations.
1983 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1984 "GUpdateStationKeys already set - do not "
1985 "increment GKeyDoneStations");
1987 sm->group->GKeyDoneStations++;
1988 sm->GUpdateStationKeys = TRUE;
1995 static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
1996 struct wpa_group *group)
2000 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2001 "SETKEYS (VLAN-ID %d)", group->vlan_id);
2002 group->changed = TRUE;
2003 group->wpa_group_state = WPA_GROUP_SETKEYS;
2004 group->GTKReKey = FALSE;
2006 group->GM = group->GN;
2008 #ifdef CONFIG_IEEE80211W
2009 tmp = group->GM_igtk;
2010 group->GM_igtk = group->GN_igtk;
2011 group->GN_igtk = tmp;
2012 #endif /* CONFIG_IEEE80211W */
2013 /* "GKeyDoneStations = GNoStations" is done in more robust way by
2014 * counting the STAs that are marked with GUpdateStationKeys instead of
2015 * including all STAs that could be in not-yet-completed state. */
2016 wpa_gtk_update(wpa_auth, group);
2018 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, NULL);
2019 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
2020 group->GKeyDoneStations);
2024 static void wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
2025 struct wpa_group *group)
2027 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2028 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
2029 group->changed = TRUE;
2030 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
2031 wpa_auth_set_key(wpa_auth, group->vlan_id,
2032 wpa_alg_enum(wpa_auth->conf.wpa_group),
2033 NULL, group->GN, group->GTK[group->GN - 1],
2036 #ifdef CONFIG_IEEE80211W
2037 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
2038 wpa_auth_set_key(wpa_auth, group->vlan_id, WPA_ALG_IGTK,
2039 NULL, group->GN_igtk,
2040 group->IGTK[group->GN_igtk - 4],
2043 #endif /* CONFIG_IEEE80211W */
2047 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
2048 struct wpa_group *group)
2051 wpa_group_gtk_init(wpa_auth, group);
2052 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
2053 group->GTKAuthenticator) {
2054 wpa_group_setkeysdone(wpa_auth, group);
2055 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
2057 wpa_group_setkeys(wpa_auth, group);
2058 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
2059 if (group->GKeyDoneStations == 0)
2060 wpa_group_setkeysdone(wpa_auth, group);
2061 else if (group->GTKReKey)
2062 wpa_group_setkeys(wpa_auth, group);
2067 static int wpa_sm_step(struct wpa_state_machine *sm)
2072 if (sm->in_step_loop) {
2073 /* This should not happen, but if it does, make sure we do not
2074 * end up freeing the state machine too early by exiting the
2075 * recursive call. */
2076 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
2080 sm->in_step_loop = 1;
2082 if (sm->pending_deinit)
2085 sm->changed = FALSE;
2086 sm->wpa_auth->group->changed = FALSE;
2088 SM_STEP_RUN(WPA_PTK);
2089 if (sm->pending_deinit)
2091 SM_STEP_RUN(WPA_PTK_GROUP);
2092 if (sm->pending_deinit)
2094 wpa_group_sm_step(sm->wpa_auth, sm->group);
2095 } while (sm->changed || sm->wpa_auth->group->changed);
2096 sm->in_step_loop = 0;
2098 if (sm->pending_deinit) {
2099 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
2100 "machine deinit for " MACSTR, MAC2STR(sm->addr));
2101 wpa_free_sta_sm(sm);
2108 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
2110 struct wpa_state_machine *sm = eloop_ctx;
2115 void wpa_auth_sm_notify(struct wpa_state_machine *sm)
2119 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
2123 void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
2126 struct wpa_group *group;
2128 if (wpa_auth == NULL)
2131 group = wpa_auth->group;
2133 for (i = 0; i < 2; i++) {
2135 group->GM = group->GN;
2137 #ifdef CONFIG_IEEE80211W
2138 tmp = group->GM_igtk;
2139 group->GM_igtk = group->GN_igtk;
2140 group->GN_igtk = tmp;
2141 #endif /* CONFIG_IEEE80211W */
2142 wpa_gtk_update(wpa_auth, group);
2147 static const char * wpa_bool_txt(int bool)
2149 return bool ? "TRUE" : "FALSE";
2153 static int wpa_cipher_bits(int cipher)
2156 case WPA_CIPHER_CCMP:
2158 case WPA_CIPHER_TKIP:
2160 case WPA_CIPHER_WEP104:
2162 case WPA_CIPHER_WEP40:
2170 #define RSN_SUITE "%02x-%02x-%02x-%d"
2171 #define RSN_SUITE_ARG(s) \
2172 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2174 int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
2177 char pmkid_txt[PMKID_LEN * 2 + 1];
2179 if (wpa_auth == NULL)
2182 ret = os_snprintf(buf + len, buflen - len,
2183 "dot11RSNAOptionImplemented=TRUE\n"
2184 #ifdef CONFIG_RSN_PREAUTH
2185 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2186 #else /* CONFIG_RSN_PREAUTH */
2187 "dot11RSNAPreauthenticationImplemented=FALSE\n"
2188 #endif /* CONFIG_RSN_PREAUTH */
2189 "dot11RSNAEnabled=%s\n"
2190 "dot11RSNAPreauthenticationEnabled=%s\n",
2191 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
2192 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
2193 if (ret < 0 || (size_t) ret >= buflen - len)
2197 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2198 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
2201 buf + len, buflen - len,
2202 "dot11RSNAConfigVersion=%u\n"
2203 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
2204 /* FIX: dot11RSNAConfigGroupCipher */
2205 /* FIX: dot11RSNAConfigGroupRekeyMethod */
2206 /* FIX: dot11RSNAConfigGroupRekeyTime */
2207 /* FIX: dot11RSNAConfigGroupRekeyPackets */
2208 "dot11RSNAConfigGroupRekeyStrict=%u\n"
2209 "dot11RSNAConfigGroupUpdateCount=%u\n"
2210 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
2211 "dot11RSNAConfigGroupCipherSize=%u\n"
2212 "dot11RSNAConfigPMKLifetime=%u\n"
2213 "dot11RSNAConfigPMKReauthThreshold=%u\n"
2214 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
2215 "dot11RSNAConfigSATimeout=%u\n"
2216 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2217 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2218 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2219 "dot11RSNAPMKIDUsed=%s\n"
2220 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2221 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2222 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2223 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
2224 "dot11RSNA4WayHandshakeFailures=%u\n"
2225 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
2227 !!wpa_auth->conf.wpa_strict_rekey,
2228 dot11RSNAConfigGroupUpdateCount,
2229 dot11RSNAConfigPairwiseUpdateCount,
2230 wpa_cipher_bits(wpa_auth->conf.wpa_group),
2231 dot11RSNAConfigPMKLifetime,
2232 dot11RSNAConfigPMKReauthThreshold,
2233 dot11RSNAConfigSATimeout,
2234 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
2235 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
2236 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
2238 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
2239 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
2240 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
2241 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
2242 wpa_auth->dot11RSNA4WayHandshakeFailures);
2243 if (ret < 0 || (size_t) ret >= buflen - len)
2247 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
2248 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
2251 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
2252 wpa_auth->group->wpa_group_state);
2253 if (ret < 0 || (size_t) ret >= buflen - len)
2261 int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
2269 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
2271 /* dot11RSNAStatsEntry */
2273 if (sm->wpa == WPA_VERSION_WPA) {
2274 if (sm->pairwise == WPA_CIPHER_CCMP)
2275 pairwise = WPA_CIPHER_SUITE_CCMP;
2276 else if (sm->pairwise == WPA_CIPHER_TKIP)
2277 pairwise = WPA_CIPHER_SUITE_TKIP;
2278 else if (sm->pairwise == WPA_CIPHER_WEP104)
2279 pairwise = WPA_CIPHER_SUITE_WEP104;
2280 else if (sm->pairwise == WPA_CIPHER_WEP40)
2281 pairwise = WPA_CIPHER_SUITE_WEP40;
2282 else if (sm->pairwise == WPA_CIPHER_NONE)
2283 pairwise = WPA_CIPHER_SUITE_NONE;
2284 } else if (sm->wpa == WPA_VERSION_WPA2) {
2285 if (sm->pairwise == WPA_CIPHER_CCMP)
2286 pairwise = RSN_CIPHER_SUITE_CCMP;
2287 else if (sm->pairwise == WPA_CIPHER_TKIP)
2288 pairwise = RSN_CIPHER_SUITE_TKIP;
2289 else if (sm->pairwise == WPA_CIPHER_WEP104)
2290 pairwise = RSN_CIPHER_SUITE_WEP104;
2291 else if (sm->pairwise == WPA_CIPHER_WEP40)
2292 pairwise = RSN_CIPHER_SUITE_WEP40;
2293 else if (sm->pairwise == WPA_CIPHER_NONE)
2294 pairwise = RSN_CIPHER_SUITE_NONE;
2299 buf + len, buflen - len,
2300 /* TODO: dot11RSNAStatsIndex */
2301 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
2302 "dot11RSNAStatsVersion=1\n"
2303 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
2304 /* TODO: dot11RSNAStatsTKIPICVErrors */
2305 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
2306 "dot11RSNAStatsTKIPRemoveMICFailures=%u\n"
2307 /* TODO: dot11RSNAStatsCCMPReplays */
2308 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
2309 /* TODO: dot11RSNAStatsTKIPReplays */,
2311 RSN_SUITE_ARG(pairwise),
2312 sm->dot11RSNAStatsTKIPLocalMICFailures,
2313 sm->dot11RSNAStatsTKIPRemoteMICFailures);
2314 if (ret < 0 || (size_t) ret >= buflen - len)
2319 ret = os_snprintf(buf + len, buflen - len,
2320 "hostapdWPAPTKState=%d\n"
2321 "hostapdWPAPTKGroupState=%d\n",
2323 sm->wpa_ptk_group_state);
2324 if (ret < 0 || (size_t) ret >= buflen - len)
2332 void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
2335 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
2339 int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
2341 return sm && sm->pairwise_set;
2345 int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
2347 return sm->pairwise;
2351 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
2355 return sm->wpa_key_mgmt;
2359 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
2367 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
2368 struct rsn_pmksa_cache_entry *entry)
2370 if (sm == NULL || sm->pmksa != entry)
2377 struct rsn_pmksa_cache_entry *
2378 wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
2380 return sm ? sm->pmksa : NULL;
2384 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
2387 sm->dot11RSNAStatsTKIPLocalMICFailures++;
2391 const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
2393 if (wpa_auth == NULL)
2395 *len = wpa_auth->wpa_ie_len;
2396 return wpa_auth->wpa_ie;
2400 int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
2401 int session_timeout, struct eapol_state_machine *eapol)
2403 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2)
2406 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
2407 sm->wpa_auth->addr, sm->addr, session_timeout,
2408 eapol, sm->wpa_key_mgmt))
2415 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
2416 const u8 *pmk, size_t len, const u8 *sta_addr,
2417 int session_timeout,
2418 struct eapol_state_machine *eapol)
2420 if (wpa_auth == NULL)
2423 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, wpa_auth->addr,
2424 sta_addr, session_timeout, eapol,
2425 WPA_KEY_MGMT_IEEE8021X))
2432 static struct wpa_group *
2433 wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
2435 struct wpa_group *group;
2437 if (wpa_auth == NULL || wpa_auth->group == NULL)
2440 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
2442 group = wpa_group_init(wpa_auth, vlan_id);
2446 group->next = wpa_auth->group->next;
2447 wpa_auth->group->next = group;
2453 int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
2455 struct wpa_group *group;
2457 if (sm == NULL || sm->wpa_auth == NULL)
2460 group = sm->wpa_auth->group;
2462 if (group->vlan_id == vlan_id)
2464 group = group->next;
2467 if (group == NULL) {
2468 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
2473 if (sm->group == group)
2476 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
2477 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
2483 #endif /* CONFIG_NATIVE_WINDOWS */