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 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "utils/state_machine.h"
20 #include "common/ieee802_11_defs.h"
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"
26 #include "ap_config.h"
27 #include "ieee802_11.h"
29 #include "pmksa_cache_auth.h"
30 #include "wpa_auth_i.h"
31 #include "wpa_auth_ie.h"
33 #define STATE_MACHINE_DATA struct wpa_state_machine
34 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
35 #define STATE_MACHINE_ADDR sm->addr
38 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
39 static int wpa_sm_step(struct wpa_state_machine *sm);
40 static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len);
41 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
42 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
43 struct wpa_group *group);
44 static void wpa_request_new_ptk(struct wpa_state_machine *sm);
45 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
46 struct wpa_group *group);
48 static const u32 dot11RSNAConfigGroupUpdateCount = 4;
49 static const u32 dot11RSNAConfigPairwiseUpdateCount = 4;
50 static const u32 eapol_key_timeout_first = 100; /* ms */
51 static const u32 eapol_key_timeout_subseq = 1000; /* ms */
53 /* TODO: make these configurable */
54 static const int dot11RSNAConfigPMKLifetime = 43200;
55 static const int dot11RSNAConfigPMKReauthThreshold = 70;
56 static const int dot11RSNAConfigSATimeout = 60;
59 static inline void wpa_auth_mic_failure_report(
60 struct wpa_authenticator *wpa_auth, const u8 *addr)
62 if (wpa_auth->cb.mic_failure_report)
63 wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr);
67 static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
68 const u8 *addr, wpa_eapol_variable var,
71 if (wpa_auth->cb.set_eapol)
72 wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value);
76 static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
77 const u8 *addr, wpa_eapol_variable var)
79 if (wpa_auth->cb.get_eapol == NULL)
81 return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var);
85 static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
86 const u8 *addr, const u8 *prev_psk)
88 if (wpa_auth->cb.get_psk == NULL)
90 return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, prev_psk);
94 static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
95 const u8 *addr, u8 *msk, size_t *len)
97 if (wpa_auth->cb.get_msk == NULL)
99 return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len);
103 static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
105 enum wpa_alg alg, const u8 *addr, int idx,
106 u8 *key, size_t key_len)
108 if (wpa_auth->cb.set_key == NULL)
110 return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
115 static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
116 const u8 *addr, int idx, u8 *seq)
118 if (wpa_auth->cb.get_seqnum == NULL)
120 return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
125 wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
126 const u8 *data, size_t data_len, int encrypt)
128 if (wpa_auth->cb.send_eapol == NULL)
130 return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
135 int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
136 int (*cb)(struct wpa_state_machine *sm, void *ctx),
139 if (wpa_auth->cb.for_each_sta == NULL)
141 return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
145 int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
146 int (*cb)(struct wpa_authenticator *a, void *ctx),
149 if (wpa_auth->cb.for_each_auth == NULL)
151 return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
155 void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
156 logger_level level, const char *txt)
158 if (wpa_auth->cb.logger == NULL)
160 wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
164 void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
165 logger_level level, const char *fmt, ...)
171 if (wpa_auth->cb.logger == NULL)
174 maxlen = os_strlen(fmt) + 100;
175 format = os_malloc(maxlen);
180 vsnprintf(format, maxlen, fmt, ap);
183 wpa_auth_logger(wpa_auth, addr, level, format);
189 static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
192 if (wpa_auth->cb.disconnect == NULL)
194 wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
195 WLAN_REASON_PREV_AUTH_NOT_VALID);
199 static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
202 #ifdef CONFIG_IEEE80211R
203 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
205 #endif /* CONFIG_IEEE80211R */
206 #ifdef CONFIG_IEEE80211W
207 if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
209 #endif /* CONFIG_IEEE80211W */
214 static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
216 struct wpa_authenticator *wpa_auth = eloop_ctx;
218 if (os_get_random(wpa_auth->group->GMK, WPA_GMK_LEN)) {
219 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
222 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
225 if (wpa_auth->conf.wpa_gmk_rekey) {
226 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
227 wpa_rekey_gmk, wpa_auth, NULL);
232 static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
234 struct wpa_authenticator *wpa_auth = eloop_ctx;
235 struct wpa_group *group;
237 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
238 for (group = wpa_auth->group; group; group = group->next) {
239 group->GTKReKey = TRUE;
241 group->changed = FALSE;
242 wpa_group_sm_step(wpa_auth, group);
243 } while (group->changed);
246 if (wpa_auth->conf.wpa_group_rekey) {
247 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
248 0, wpa_rekey_gtk, wpa_auth, NULL);
253 static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
255 struct wpa_authenticator *wpa_auth = eloop_ctx;
256 struct wpa_state_machine *sm = timeout_ctx;
258 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
259 wpa_request_new_ptk(sm);
264 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
266 if (sm->pmksa == ctx)
272 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
275 struct wpa_authenticator *wpa_auth = ctx;
276 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
280 static void wpa_group_set_key_len(struct wpa_group *group, int cipher)
283 case WPA_CIPHER_CCMP:
286 case WPA_CIPHER_TKIP:
289 case WPA_CIPHER_WEP104:
292 case WPA_CIPHER_WEP40:
299 static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
302 struct wpa_group *group;
303 u8 buf[ETH_ALEN + 8 + sizeof(group)];
306 group = os_zalloc(sizeof(struct wpa_group));
310 group->GTKAuthenticator = TRUE;
311 group->vlan_id = vlan_id;
313 wpa_group_set_key_len(group, wpa_auth->conf.wpa_group);
315 /* Counter = PRF-256(Random number, "Init Counter",
316 * Local MAC Address || Time)
318 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
319 wpa_get_ntp_timestamp(buf + ETH_ALEN);
320 os_memcpy(buf + ETH_ALEN + 8, &group, sizeof(group));
321 if (os_get_random(rkey, sizeof(rkey)) ||
322 os_get_random(group->GMK, WPA_GMK_LEN)) {
323 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
329 sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
330 group->Counter, WPA_NONCE_LEN);
333 wpa_group_sm_step(wpa_auth, group);
334 group->GInit = FALSE;
335 wpa_group_sm_step(wpa_auth, group);
342 * wpa_init - Initialize WPA authenticator
343 * @addr: Authenticator address
344 * @conf: Configuration for WPA authenticator
345 * @cb: Callback functions for WPA authenticator
346 * Returns: Pointer to WPA authenticator data or %NULL on failure
348 struct wpa_authenticator * wpa_init(const u8 *addr,
349 struct wpa_auth_config *conf,
350 struct wpa_auth_callbacks *cb)
352 struct wpa_authenticator *wpa_auth;
354 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
355 if (wpa_auth == NULL)
357 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
358 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
359 os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
361 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
362 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
367 wpa_auth->group = wpa_group_init(wpa_auth, 0);
368 if (wpa_auth->group == NULL) {
369 os_free(wpa_auth->wpa_ie);
374 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
376 if (wpa_auth->pmksa == NULL) {
377 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
378 os_free(wpa_auth->wpa_ie);
383 #ifdef CONFIG_IEEE80211R
384 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
385 if (wpa_auth->ft_pmk_cache == NULL) {
386 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
387 os_free(wpa_auth->wpa_ie);
388 pmksa_cache_auth_deinit(wpa_auth->pmksa);
392 #endif /* CONFIG_IEEE80211R */
394 if (wpa_auth->conf.wpa_gmk_rekey) {
395 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
396 wpa_rekey_gmk, wpa_auth, NULL);
399 if (wpa_auth->conf.wpa_group_rekey) {
400 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
401 wpa_rekey_gtk, wpa_auth, NULL);
409 * wpa_deinit - Deinitialize WPA authenticator
410 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
412 void wpa_deinit(struct wpa_authenticator *wpa_auth)
414 struct wpa_group *group, *prev;
416 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
417 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
419 #ifdef CONFIG_PEERKEY
420 while (wpa_auth->stsl_negotiations)
421 wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
422 #endif /* CONFIG_PEERKEY */
424 pmksa_cache_auth_deinit(wpa_auth->pmksa);
426 #ifdef CONFIG_IEEE80211R
427 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
428 wpa_auth->ft_pmk_cache = NULL;
429 #endif /* CONFIG_IEEE80211R */
431 os_free(wpa_auth->wpa_ie);
433 group = wpa_auth->group;
445 * wpa_reconfig - Update WPA authenticator configuration
446 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
447 * @conf: Configuration for WPA authenticator
449 int wpa_reconfig(struct wpa_authenticator *wpa_auth,
450 struct wpa_auth_config *conf)
452 struct wpa_group *group;
453 if (wpa_auth == NULL)
456 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
457 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
458 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
463 * Reinitialize GTK to make sure it is suitable for the new
466 group = wpa_auth->group;
467 wpa_group_set_key_len(group, wpa_auth->conf.wpa_group);
469 wpa_group_sm_step(wpa_auth, group);
470 group->GInit = FALSE;
471 wpa_group_sm_step(wpa_auth, group);
477 struct wpa_state_machine *
478 wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr)
480 struct wpa_state_machine *sm;
482 sm = os_zalloc(sizeof(struct wpa_state_machine));
485 os_memcpy(sm->addr, addr, ETH_ALEN);
487 sm->wpa_auth = wpa_auth;
488 sm->group = wpa_auth->group;
494 int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
495 struct wpa_state_machine *sm)
497 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
500 #ifdef CONFIG_IEEE80211R
501 if (sm->ft_completed) {
502 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
503 "FT authentication already completed - do not "
504 "start 4-way handshake");
507 #endif /* CONFIG_IEEE80211R */
510 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
511 sm->ReAuthenticationRequest = TRUE;
512 return wpa_sm_step(sm);
515 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
516 "start authentication");
520 if (wpa_sm_step(sm) == 1)
521 return 1; /* should not really happen */
523 sm->AuthenticationRequest = TRUE;
524 return wpa_sm_step(sm);
528 void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
530 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
531 * reassociates back to the same AP while the previous entry for the
532 * STA has not yet been removed. */
536 sm->wpa_key_mgmt = 0;
540 static void wpa_free_sta_sm(struct wpa_state_machine *sm)
542 os_free(sm->last_rx_eapol_key);
548 void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
553 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
554 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
555 "strict rekeying - force GTK rekey since STA "
557 eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
558 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
562 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
563 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
564 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
565 if (sm->in_step_loop) {
566 /* Must not free state machine while wpa_sm_step() is running.
567 * Freeing will be completed in the end of wpa_sm_step(). */
568 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
569 "machine deinit for " MACSTR, MAC2STR(sm->addr));
570 sm->pending_deinit = 1;
576 static void wpa_request_new_ptk(struct wpa_state_machine *sm)
581 sm->PTKRequest = TRUE;
586 static int wpa_replay_counter_valid(struct wpa_state_machine *sm,
587 const u8 *replay_counter)
590 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
591 if (!sm->key_replay[i].valid)
593 if (os_memcmp(replay_counter, sm->key_replay[i].counter,
594 WPA_REPLAY_COUNTER_LEN) == 0)
601 void wpa_receive(struct wpa_authenticator *wpa_auth,
602 struct wpa_state_machine *sm,
603 u8 *data, size_t data_len)
605 struct ieee802_1x_hdr *hdr;
606 struct wpa_eapol_key *key;
607 u16 key_info, key_data_length;
608 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
609 SMK_M1, SMK_M3, SMK_ERROR } msg;
611 struct wpa_eapol_ie_parse kde;
613 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
616 if (data_len < sizeof(*hdr) + sizeof(*key))
619 hdr = (struct ieee802_1x_hdr *) data;
620 key = (struct wpa_eapol_key *) (hdr + 1);
621 key_info = WPA_GET_BE16(key->key_info);
622 key_data_length = WPA_GET_BE16(key->key_data_length);
623 if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) {
624 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
625 "key_data overflow (%d > %lu)",
627 (unsigned long) (data_len - sizeof(*hdr) -
632 if (sm->wpa == WPA_VERSION_WPA2) {
633 if (key->type != EAPOL_KEY_TYPE_RSN) {
634 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
635 "unexpected type %d in RSN mode",
640 if (key->type != EAPOL_KEY_TYPE_WPA) {
641 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
642 "unexpected type %d in WPA mode",
648 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
651 if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
652 (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
653 if (key_info & WPA_KEY_INFO_ERROR) {
655 msgtxt = "SMK Error";
660 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
663 } else if (key_info & WPA_KEY_INFO_REQUEST) {
666 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
668 msgtxt = "2/2 Group";
669 } else if (key_data_length == 0) {
671 msgtxt = "4/4 Pairwise";
674 msgtxt = "2/4 Pairwise";
677 /* TODO: key_info type validation for PeerKey */
678 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
680 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
681 if (sm->pairwise == WPA_CIPHER_CCMP) {
682 if (wpa_use_aes_cmac(sm) &&
683 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
684 wpa_auth_logger(wpa_auth, sm->addr,
686 "advertised support for "
687 "AES-128-CMAC, but did not "
692 if (!wpa_use_aes_cmac(sm) &&
693 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
694 wpa_auth_logger(wpa_auth, sm->addr,
696 "did not use HMAC-SHA1-AES "
703 if (key_info & WPA_KEY_INFO_REQUEST) {
704 if (sm->req_replay_counter_used &&
705 os_memcmp(key->replay_counter, sm->req_replay_counter,
706 WPA_REPLAY_COUNTER_LEN) <= 0) {
707 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
708 "received EAPOL-Key request with "
714 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
715 !wpa_replay_counter_valid(sm, key->replay_counter)) {
717 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
718 "received EAPOL-Key %s with unexpected "
719 "replay counter", msgtxt);
720 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
721 if (!sm->key_replay[i].valid)
723 wpa_hexdump(MSG_DEBUG, "pending replay counter",
724 sm->key_replay[i].counter,
725 WPA_REPLAY_COUNTER_LEN);
727 wpa_hexdump(MSG_DEBUG, "received replay counter",
728 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
734 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
735 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING) {
736 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
737 "received EAPOL-Key msg 2/4 in "
738 "invalid state (%d) - dropped",
742 if (sm->wpa_ie == NULL ||
743 sm->wpa_ie_len != key_data_length ||
744 os_memcmp(sm->wpa_ie, key + 1, key_data_length) != 0) {
745 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
746 "WPA IE from (Re)AssocReq did not "
747 "match with msg 2/4");
749 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
750 sm->wpa_ie, sm->wpa_ie_len);
752 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
753 (u8 *) (key + 1), key_data_length);
754 /* MLME-DEAUTHENTICATE.request */
755 wpa_sta_disconnect(wpa_auth, sm->addr);
760 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
762 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
763 "received EAPOL-Key msg 4/4 in "
764 "invalid state (%d) - dropped",
770 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
772 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
773 "received EAPOL-Key msg 2/2 in "
774 "invalid state (%d) - dropped",
775 sm->wpa_ptk_group_state);
779 #ifdef CONFIG_PEERKEY
783 if (!wpa_auth->conf.peerkey) {
784 wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
785 "PeerKey use disabled - ignoring message");
788 if (!sm->PTK_valid) {
789 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
790 "received EAPOL-Key msg SMK in "
791 "invalid state - dropped");
795 #else /* CONFIG_PEERKEY */
799 return; /* STSL disabled - ignore SMK messages */
800 #endif /* CONFIG_PEERKEY */
805 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
806 "received EAPOL-Key frame (%s)", msgtxt);
808 if (key_info & WPA_KEY_INFO_ACK) {
809 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
810 "received invalid EAPOL-Key: Key Ack set");
814 if (!(key_info & WPA_KEY_INFO_MIC)) {
815 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
816 "received invalid EAPOL-Key: Key MIC not set");
820 sm->MICVerified = FALSE;
822 if (wpa_verify_key_mic(&sm->PTK, data, data_len)) {
823 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
824 "received EAPOL-Key with invalid MIC");
827 sm->MICVerified = TRUE;
828 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
831 if (key_info & WPA_KEY_INFO_REQUEST) {
832 if (sm->MICVerified) {
833 sm->req_replay_counter_used = 1;
834 os_memcpy(sm->req_replay_counter, key->replay_counter,
835 WPA_REPLAY_COUNTER_LEN);
837 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
838 "received EAPOL-Key request with "
844 * TODO: should decrypt key data field if encryption was used;
845 * even though MAC address KDE is not normally encrypted,
846 * supplicant is allowed to encrypt it.
848 if (msg == SMK_ERROR) {
849 #ifdef CONFIG_PEERKEY
850 wpa_smk_error(wpa_auth, sm, key);
851 #endif /* CONFIG_PEERKEY */
853 } else if (key_info & WPA_KEY_INFO_ERROR) {
854 /* Supplicant reported a Michael MIC error */
855 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
856 "received EAPOL-Key Error Request "
857 "(STA detected Michael MIC failure)");
858 wpa_auth_mic_failure_report(wpa_auth, sm->addr);
859 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
860 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
861 /* Error report is not a request for a new key
862 * handshake, but since Authenticator may do it, let's
863 * change the keys now anyway. */
864 wpa_request_new_ptk(sm);
865 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
866 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
867 "received EAPOL-Key Request for new "
869 wpa_request_new_ptk(sm);
870 #ifdef CONFIG_PEERKEY
871 } else if (msg == SMK_M1) {
872 wpa_smk_m1(wpa_auth, sm, key);
873 #endif /* CONFIG_PEERKEY */
874 } else if (key_data_length > 0 &&
875 wpa_parse_kde_ies((const u8 *) (key + 1),
876 key_data_length, &kde) == 0 &&
879 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
880 "received EAPOL-Key Request for GTK "
882 /* FIX: why was this triggering PTK rekeying for the
883 * STA that requested Group Key rekeying?? */
884 /* wpa_request_new_ptk(sta->wpa_sm); */
885 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
886 wpa_rekey_gtk(wpa_auth, NULL);
889 /* Do not allow the same key replay counter to be reused. This
890 * does also invalidate all other pending replay counters if
891 * retransmissions were used, i.e., we will only process one of
892 * the pending replies and ignore rest if more than one is
894 sm->key_replay[0].valid = FALSE;
897 #ifdef CONFIG_PEERKEY
899 wpa_smk_m3(wpa_auth, sm, key);
902 #endif /* CONFIG_PEERKEY */
904 os_free(sm->last_rx_eapol_key);
905 sm->last_rx_eapol_key = os_malloc(data_len);
906 if (sm->last_rx_eapol_key == NULL)
908 os_memcpy(sm->last_rx_eapol_key, data, data_len);
909 sm->last_rx_eapol_key_len = data_len;
911 sm->EAPOLKeyReceived = TRUE;
912 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
913 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
914 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
919 static void wpa_gmk_to_gtk(const u8 *gmk, const u8 *addr, const u8 *gnonce,
920 u8 *gtk, size_t gtk_len)
922 u8 data[ETH_ALEN + WPA_NONCE_LEN];
924 /* GTK = PRF-X(GMK, "Group key expansion", AA || GNonce) */
925 os_memcpy(data, addr, ETH_ALEN);
926 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
928 #ifdef CONFIG_IEEE80211W
929 sha256_prf(gmk, WPA_GMK_LEN, "Group key expansion",
930 data, sizeof(data), gtk, gtk_len);
931 #else /* CONFIG_IEEE80211W */
932 sha1_prf(gmk, WPA_GMK_LEN, "Group key expansion",
933 data, sizeof(data), gtk, gtk_len);
934 #endif /* CONFIG_IEEE80211W */
936 wpa_hexdump_key(MSG_DEBUG, "GMK", gmk, WPA_GMK_LEN);
937 wpa_hexdump_key(MSG_DEBUG, "GTK", gtk, gtk_len);
941 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
943 struct wpa_authenticator *wpa_auth = eloop_ctx;
944 struct wpa_state_machine *sm = timeout_ctx;
946 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
947 sm->TimeoutEvt = TRUE;
952 void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
953 struct wpa_state_machine *sm, int key_info,
954 const u8 *key_rsc, const u8 *nonce,
955 const u8 *kde, size_t kde_len,
956 int keyidx, int encr, int force_version)
958 struct ieee802_1x_hdr *hdr;
959 struct wpa_eapol_key *key;
962 int key_data_len, pad_len = 0;
964 int version, pairwise;
967 len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
970 version = force_version;
971 else if (wpa_use_aes_cmac(sm))
972 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
973 else if (sm->pairwise == WPA_CIPHER_CCMP)
974 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
976 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
978 pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
980 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
981 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
984 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
985 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
986 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
987 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
988 pairwise, (unsigned long) kde_len, keyidx, encr);
990 key_data_len = kde_len;
992 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
993 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
994 pad_len = key_data_len % 8;
996 pad_len = 8 - pad_len;
997 key_data_len += pad_len + 8;
1000 len += key_data_len;
1002 hdr = os_zalloc(len);
1005 hdr->version = wpa_auth->conf.eapol_version;
1006 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1007 hdr->length = host_to_be16(len - sizeof(*hdr));
1008 key = (struct wpa_eapol_key *) (hdr + 1);
1010 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1011 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1012 key_info |= version;
1013 if (encr && sm->wpa == WPA_VERSION_WPA2)
1014 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1015 if (sm->wpa != WPA_VERSION_WPA2)
1016 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1017 WPA_PUT_BE16(key->key_info, key_info);
1019 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
1021 case WPA_CIPHER_CCMP:
1022 WPA_PUT_BE16(key->key_length, 16);
1024 case WPA_CIPHER_TKIP:
1025 WPA_PUT_BE16(key->key_length, 32);
1027 case WPA_CIPHER_WEP40:
1028 WPA_PUT_BE16(key->key_length, 5);
1030 case WPA_CIPHER_WEP104:
1031 WPA_PUT_BE16(key->key_length, 13);
1034 if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
1035 WPA_PUT_BE16(key->key_length, 0);
1037 /* FIX: STSL: what to use as key_replay_counter? */
1038 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1039 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1040 os_memcpy(sm->key_replay[i].counter,
1041 sm->key_replay[i - 1].counter,
1042 WPA_REPLAY_COUNTER_LEN);
1044 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1045 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1046 WPA_REPLAY_COUNTER_LEN);
1047 sm->key_replay[0].valid = TRUE;
1050 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1053 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1056 os_memcpy(key + 1, kde, kde_len);
1057 WPA_PUT_BE16(key->key_data_length, kde_len);
1058 } else if (encr && kde) {
1059 buf = os_zalloc(key_data_len);
1065 os_memcpy(pos, kde, kde_len);
1071 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1073 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1074 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1075 if (aes_wrap(sm->PTK.kek, (key_data_len - 8) / 8, buf,
1076 (u8 *) (key + 1))) {
1081 WPA_PUT_BE16(key->key_data_length, key_data_len);
1084 os_memcpy(key->key_iv,
1085 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1086 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1087 os_memcpy(ek, key->key_iv, 16);
1088 os_memcpy(ek + 16, sm->PTK.kek, 16);
1089 os_memcpy(key + 1, buf, key_data_len);
1090 rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
1091 WPA_PUT_BE16(key->key_data_length, key_data_len);
1096 if (key_info & WPA_KEY_INFO_MIC) {
1097 if (!sm->PTK_valid) {
1098 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1099 "PTK not valid when sending EAPOL-Key "
1104 wpa_eapol_key_mic(sm->PTK.kck, version, (u8 *) hdr, len,
1108 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1110 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1116 static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1117 struct wpa_state_machine *sm, int key_info,
1118 const u8 *key_rsc, const u8 *nonce,
1119 const u8 *kde, size_t kde_len,
1120 int keyidx, int encr)
1123 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1129 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1132 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1134 timeout_ms = eapol_key_timeout_first;
1136 timeout_ms = eapol_key_timeout_subseq;
1137 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1138 wpa_send_eapol_timeout, wpa_auth, sm);
1142 static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len)
1144 struct ieee802_1x_hdr *hdr;
1145 struct wpa_eapol_key *key;
1150 if (data_len < sizeof(*hdr) + sizeof(*key))
1153 hdr = (struct ieee802_1x_hdr *) data;
1154 key = (struct wpa_eapol_key *) (hdr + 1);
1155 key_info = WPA_GET_BE16(key->key_info);
1156 os_memcpy(mic, key->key_mic, 16);
1157 os_memset(key->key_mic, 0, 16);
1158 if (wpa_eapol_key_mic(PTK->kck, key_info & WPA_KEY_INFO_TYPE_MASK,
1159 data, data_len, key->key_mic) ||
1160 os_memcmp(mic, key->key_mic, 16) != 0)
1162 os_memcpy(key->key_mic, mic, 16);
1167 void wpa_remove_ptk(struct wpa_state_machine *sm)
1169 sm->PTK_valid = FALSE;
1170 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1171 wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, (u8 *) "",
1173 sm->pairwise_set = FALSE;
1174 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1178 int wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
1185 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1186 "event %d notification", event);
1194 sm->DeauthenticationRequest = TRUE;
1197 case WPA_REAUTH_EAPOL:
1198 if (sm->GUpdateStationKeys) {
1200 * Reauthentication cancels the pending group key
1201 * update for this STA.
1203 sm->group->GKeyDoneStations--;
1204 sm->GUpdateStationKeys = FALSE;
1205 sm->PtkGroupInit = TRUE;
1207 sm->ReAuthenticationRequest = TRUE;
1210 #ifdef CONFIG_IEEE80211R
1211 if (!sm->pairwise_set) {
1212 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1213 "after association");
1214 wpa_ft_install_ptk(sm);
1216 /* Using FT protocol, not WPA auth state machine */
1217 sm->ft_completed = 1;
1219 #else /* CONFIG_IEEE80211R */
1221 #endif /* CONFIG_IEEE80211R */
1224 #ifdef CONFIG_IEEE80211R
1225 sm->ft_completed = 0;
1226 #endif /* CONFIG_IEEE80211R */
1228 #ifdef CONFIG_IEEE80211W
1229 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1231 #endif /* CONFIG_IEEE80211W */
1234 sm->PTK_valid = FALSE;
1235 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1237 if (event != WPA_REAUTH_EAPOL)
1241 return wpa_sm_step(sm);
1245 static enum wpa_alg wpa_alg_enum(int alg)
1248 case WPA_CIPHER_CCMP:
1249 return WPA_ALG_CCMP;
1250 case WPA_CIPHER_TKIP:
1251 return WPA_ALG_TKIP;
1252 case WPA_CIPHER_WEP104:
1253 case WPA_CIPHER_WEP40:
1256 return WPA_ALG_NONE;
1261 SM_STATE(WPA_PTK, INITIALIZE)
1263 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1265 /* Init flag is not cleared here, so avoid busy
1266 * loop by claiming nothing changed. */
1267 sm->changed = FALSE;
1271 if (sm->GUpdateStationKeys)
1272 sm->group->GKeyDoneStations--;
1273 sm->GUpdateStationKeys = FALSE;
1274 if (sm->wpa == WPA_VERSION_WPA)
1275 sm->PInitAKeys = FALSE;
1276 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1277 * Local AA > Remote AA)) */) {
1280 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1282 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1284 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1285 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1286 WPA_EAPOL_authorized, 0);
1291 SM_STATE(WPA_PTK, DISCONNECT)
1293 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1294 sm->Disconnect = FALSE;
1295 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1299 SM_STATE(WPA_PTK, DISCONNECTED)
1301 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1302 sm->DeauthenticationRequest = FALSE;
1306 SM_STATE(WPA_PTK, AUTHENTICATION)
1308 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1309 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1310 sm->PTK_valid = FALSE;
1311 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1313 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1314 sm->AuthenticationRequest = FALSE;
1318 SM_STATE(WPA_PTK, AUTHENTICATION2)
1320 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1321 os_memcpy(sm->ANonce, sm->group->Counter, WPA_NONCE_LEN);
1322 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1323 sm->ReAuthenticationRequest = FALSE;
1324 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1325 * logical place than INITIALIZE since AUTHENTICATION2 can be
1326 * re-entered on ReAuthenticationRequest without going through
1332 SM_STATE(WPA_PTK, INITPMK)
1334 u8 msk[2 * PMK_LEN];
1335 size_t len = 2 * PMK_LEN;
1337 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1338 #ifdef CONFIG_IEEE80211R
1340 #endif /* CONFIG_IEEE80211R */
1342 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1343 os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
1344 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1345 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1346 "(len=%lu)", (unsigned long) len);
1347 os_memcpy(sm->PMK, msk, PMK_LEN);
1348 #ifdef CONFIG_IEEE80211R
1349 if (len >= 2 * PMK_LEN) {
1350 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1351 sm->xxkey_len = PMK_LEN;
1353 #endif /* CONFIG_IEEE80211R */
1355 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
1358 sm->req_replay_counter_used = 0;
1359 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1360 * will break reauthentication since EAPOL state machines may not be
1361 * get into AUTHENTICATING state that clears keyRun before WPA state
1362 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1363 * state and takes PMK from the previously used AAA Key. This will
1364 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1365 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1366 * be good workaround for this issue. */
1367 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1371 SM_STATE(WPA_PTK, INITPSK)
1374 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
1375 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL);
1377 os_memcpy(sm->PMK, psk, PMK_LEN);
1378 #ifdef CONFIG_IEEE80211R
1379 os_memcpy(sm->xxkey, psk, PMK_LEN);
1380 sm->xxkey_len = PMK_LEN;
1381 #endif /* CONFIG_IEEE80211R */
1383 sm->req_replay_counter_used = 0;
1387 SM_STATE(WPA_PTK, PTKSTART)
1389 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1390 size_t pmkid_len = 0;
1392 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
1393 sm->PTKRequest = FALSE;
1394 sm->TimeoutEvt = FALSE;
1397 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1398 /* No point in sending the EAPOL-Key - we will disconnect
1399 * immediately following this. */
1403 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1404 "sending 1/4 msg of 4-Way Handshake");
1406 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1407 * one possible PSK for this STA.
1409 if (sm->wpa == WPA_VERSION_WPA2 &&
1410 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt)) {
1412 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
1413 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
1414 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
1415 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
1417 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
1418 sm->pmksa->pmkid, PMKID_LEN);
1421 * Calculate PMKID since no PMKSA cache entry was
1422 * available with pre-calculated PMKID.
1424 rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
1425 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
1426 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1429 wpa_send_eapol(sm->wpa_auth, sm,
1430 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
1431 sm->ANonce, pmkid, pmkid_len, 0, 0);
1435 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk,
1436 struct wpa_ptk *ptk)
1438 size_t ptk_len = sm->pairwise == WPA_CIPHER_CCMP ? 48 : 64;
1439 #ifdef CONFIG_IEEE80211R
1440 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
1441 return wpa_auth_derive_ptk_ft(sm, pmk, ptk, ptk_len);
1442 #endif /* CONFIG_IEEE80211R */
1444 wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
1445 sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce,
1446 (u8 *) ptk, ptk_len,
1447 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1453 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
1457 const u8 *pmk = NULL;
1459 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
1460 sm->EAPOLKeyReceived = FALSE;
1462 /* WPA with IEEE 802.1X: use the derived PMK from EAP
1463 * WPA-PSK: iterate through possible PSKs and select the one matching
1466 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1467 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, pmk);
1473 wpa_derive_ptk(sm, pmk, &PTK);
1475 if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key,
1476 sm->last_rx_eapol_key_len) == 0) {
1481 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
1486 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1487 "invalid MIC in msg 2/4 of 4-Way Handshake");
1491 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
1493 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1494 /* PSK may have changed from the previous choice, so update
1495 * state machine data based on whatever PSK was selected here.
1497 os_memcpy(sm->PMK, pmk, PMK_LEN);
1500 sm->MICVerified = TRUE;
1502 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
1503 sm->PTK_valid = TRUE;
1507 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
1509 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
1514 #ifdef CONFIG_IEEE80211W
1516 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1518 if (sm->mgmt_frame_prot) {
1519 return 2 + RSN_SELECTOR_LEN + sizeof(struct wpa_igtk_kde);
1526 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1528 struct wpa_igtk_kde igtk;
1529 struct wpa_group *gsm = sm->group;
1531 if (!sm->mgmt_frame_prot)
1534 igtk.keyid[0] = gsm->GN_igtk;
1536 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
1537 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, igtk.pn) < 0)
1538 os_memset(igtk.pn, 0, sizeof(igtk.pn));
1539 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
1540 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
1541 (const u8 *) &igtk, sizeof(igtk), NULL, 0);
1546 #else /* CONFIG_IEEE80211W */
1548 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1554 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1559 #endif /* CONFIG_IEEE80211W */
1562 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
1564 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
1565 size_t gtk_len, kde_len;
1566 struct wpa_group *gsm = sm->group;
1568 int wpa_ie_len, secure, keyidx, encr = 0;
1570 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
1571 sm->TimeoutEvt = FALSE;
1574 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1575 /* No point in sending the EAPOL-Key - we will disconnect
1576 * immediately following this. */
1580 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, GTK[GN])
1582 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
1583 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
1584 wpa_ie = sm->wpa_auth->wpa_ie;
1585 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
1586 if (sm->wpa == WPA_VERSION_WPA &&
1587 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
1588 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
1589 /* WPA-only STA, remove RSN IE */
1590 wpa_ie = wpa_ie + wpa_ie[1] + 2;
1591 wpa_ie_len = wpa_ie[1] + 2;
1593 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1594 "sending 3/4 msg of 4-Way Handshake");
1595 if (sm->wpa == WPA_VERSION_WPA2) {
1596 /* WPA2 send GTK in the 4-way handshake */
1598 gtk = gsm->GTK[gsm->GN - 1];
1599 gtk_len = gsm->GTK_len;
1604 /* WPA does not include GTK in msg 3/4 */
1612 kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
1614 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
1615 kde = os_malloc(kde_len);
1620 os_memcpy(pos, wpa_ie, wpa_ie_len);
1624 hdr[0] = keyidx & 0x03;
1626 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
1629 pos = ieee80211w_kde_add(sm, pos);
1631 wpa_send_eapol(sm->wpa_auth, sm,
1632 (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
1633 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
1634 WPA_KEY_INFO_KEY_TYPE,
1635 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
1640 SM_STATE(WPA_PTK, PTKINITDONE)
1642 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
1643 sm->EAPOLKeyReceived = FALSE;
1647 if (sm->pairwise == WPA_CIPHER_TKIP) {
1654 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
1655 sm->PTK.tk1, klen)) {
1656 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1659 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
1660 sm->pairwise_set = TRUE;
1662 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
1663 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1664 eloop_register_timeout(sm->wpa_auth->conf.
1665 wpa_ptk_rekey, 0, wpa_rekey_ptk,
1669 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1670 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1671 WPA_EAPOL_authorized, 1);
1675 if (0 /* IBSS == TRUE */) {
1677 if (sm->keycount == 2) {
1678 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1679 WPA_EAPOL_portValid, 1);
1682 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
1685 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
1686 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
1687 if (sm->wpa == WPA_VERSION_WPA)
1688 sm->PInitAKeys = TRUE;
1691 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1692 "pairwise key handshake completed (%s)",
1693 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
1695 #ifdef CONFIG_IEEE80211R
1696 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
1697 #endif /* CONFIG_IEEE80211R */
1703 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
1706 SM_ENTER(WPA_PTK, INITIALIZE);
1707 else if (sm->Disconnect
1708 /* || FIX: dot11RSNAConfigSALifetime timeout */)
1709 SM_ENTER(WPA_PTK, DISCONNECT);
1710 else if (sm->DeauthenticationRequest)
1711 SM_ENTER(WPA_PTK, DISCONNECTED);
1712 else if (sm->AuthenticationRequest)
1713 SM_ENTER(WPA_PTK, AUTHENTICATION);
1714 else if (sm->ReAuthenticationRequest)
1715 SM_ENTER(WPA_PTK, AUTHENTICATION2);
1716 else if (sm->PTKRequest)
1717 SM_ENTER(WPA_PTK, PTKSTART);
1718 else switch (sm->wpa_ptk_state) {
1719 case WPA_PTK_INITIALIZE:
1721 case WPA_PTK_DISCONNECT:
1722 SM_ENTER(WPA_PTK, DISCONNECTED);
1724 case WPA_PTK_DISCONNECTED:
1725 SM_ENTER(WPA_PTK, INITIALIZE);
1727 case WPA_PTK_AUTHENTICATION:
1728 SM_ENTER(WPA_PTK, AUTHENTICATION2);
1730 case WPA_PTK_AUTHENTICATION2:
1731 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
1732 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
1733 WPA_EAPOL_keyRun) > 0)
1734 SM_ENTER(WPA_PTK, INITPMK);
1735 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
1736 /* FIX: && 802.1X::keyRun */)
1737 SM_ENTER(WPA_PTK, INITPSK);
1739 case WPA_PTK_INITPMK:
1740 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
1741 WPA_EAPOL_keyAvailable) > 0)
1742 SM_ENTER(WPA_PTK, PTKSTART);
1744 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1745 SM_ENTER(WPA_PTK, DISCONNECT);
1748 case WPA_PTK_INITPSK:
1749 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL))
1750 SM_ENTER(WPA_PTK, PTKSTART);
1752 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1753 "no PSK configured for the STA");
1754 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1755 SM_ENTER(WPA_PTK, DISCONNECT);
1758 case WPA_PTK_PTKSTART:
1759 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1760 sm->EAPOLKeyPairwise)
1761 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
1762 else if (sm->TimeoutCtr >
1763 (int) dot11RSNAConfigPairwiseUpdateCount) {
1764 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1765 SM_ENTER(WPA_PTK, DISCONNECT);
1766 } else if (sm->TimeoutEvt)
1767 SM_ENTER(WPA_PTK, PTKSTART);
1769 case WPA_PTK_PTKCALCNEGOTIATING:
1770 if (sm->MICVerified)
1771 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
1772 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1773 sm->EAPOLKeyPairwise)
1774 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
1775 else if (sm->TimeoutEvt)
1776 SM_ENTER(WPA_PTK, PTKSTART);
1778 case WPA_PTK_PTKCALCNEGOTIATING2:
1779 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
1781 case WPA_PTK_PTKINITNEGOTIATING:
1782 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1783 sm->EAPOLKeyPairwise && sm->MICVerified)
1784 SM_ENTER(WPA_PTK, PTKINITDONE);
1785 else if (sm->TimeoutCtr >
1786 (int) dot11RSNAConfigPairwiseUpdateCount) {
1787 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1788 SM_ENTER(WPA_PTK, DISCONNECT);
1789 } else if (sm->TimeoutEvt)
1790 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
1792 case WPA_PTK_PTKINITDONE:
1798 SM_STATE(WPA_PTK_GROUP, IDLE)
1800 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
1802 /* Init flag is not cleared here, so avoid busy
1803 * loop by claiming nothing changed. */
1804 sm->changed = FALSE;
1806 sm->GTimeoutCtr = 0;
1810 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
1812 u8 rsc[WPA_KEY_RSC_LEN];
1813 struct wpa_group *gsm = sm->group;
1814 u8 *kde, *pos, hdr[2];
1817 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
1820 if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
1821 /* No point in sending the EAPOL-Key - we will disconnect
1822 * immediately following this. */
1826 if (sm->wpa == WPA_VERSION_WPA)
1827 sm->PInitAKeys = FALSE;
1828 sm->TimeoutEvt = FALSE;
1829 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
1830 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
1831 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
1832 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
1833 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1834 "sending 1/2 msg of Group Key Handshake");
1836 if (sm->wpa == WPA_VERSION_WPA2) {
1837 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
1838 ieee80211w_kde_len(sm);
1839 kde = os_malloc(kde_len);
1844 hdr[0] = gsm->GN & 0x03;
1846 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
1847 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
1848 pos = ieee80211w_kde_add(sm, pos);
1850 kde = gsm->GTK[gsm->GN - 1];
1851 pos = kde + gsm->GTK_len;
1854 wpa_send_eapol(sm->wpa_auth, sm,
1855 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
1857 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
1858 rsc, gsm->GNonce, kde, pos - kde, gsm->GN, 1);
1859 if (sm->wpa == WPA_VERSION_WPA2)
1864 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
1866 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
1867 sm->EAPOLKeyReceived = FALSE;
1868 if (sm->GUpdateStationKeys)
1869 sm->group->GKeyDoneStations--;
1870 sm->GUpdateStationKeys = FALSE;
1871 sm->GTimeoutCtr = 0;
1872 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
1873 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1874 "group key handshake completed (%s)",
1875 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
1880 SM_STATE(WPA_PTK_GROUP, KEYERROR)
1882 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
1883 if (sm->GUpdateStationKeys)
1884 sm->group->GKeyDoneStations--;
1885 sm->GUpdateStationKeys = FALSE;
1886 sm->Disconnect = TRUE;
1890 SM_STEP(WPA_PTK_GROUP)
1892 if (sm->Init || sm->PtkGroupInit) {
1893 SM_ENTER(WPA_PTK_GROUP, IDLE);
1894 sm->PtkGroupInit = FALSE;
1895 } else switch (sm->wpa_ptk_group_state) {
1896 case WPA_PTK_GROUP_IDLE:
1897 if (sm->GUpdateStationKeys ||
1898 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
1899 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
1901 case WPA_PTK_GROUP_REKEYNEGOTIATING:
1902 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1903 !sm->EAPOLKeyPairwise && sm->MICVerified)
1904 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
1905 else if (sm->GTimeoutCtr >
1906 (int) dot11RSNAConfigGroupUpdateCount)
1907 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
1908 else if (sm->TimeoutEvt)
1909 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
1911 case WPA_PTK_GROUP_KEYERROR:
1912 SM_ENTER(WPA_PTK_GROUP, IDLE);
1914 case WPA_PTK_GROUP_REKEYESTABLISHED:
1915 SM_ENTER(WPA_PTK_GROUP, IDLE);
1921 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
1922 struct wpa_group *group)
1926 /* FIX: is this the correct way of getting GNonce? */
1927 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
1928 inc_byte_array(group->Counter, WPA_NONCE_LEN);
1929 wpa_gmk_to_gtk(group->GMK, wpa_auth->addr, group->GNonce,
1930 group->GTK[group->GN - 1], group->GTK_len);
1932 #ifdef CONFIG_IEEE80211W
1933 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
1934 if (os_get_random(group->IGTK[group->GN_igtk - 4],
1935 WPA_IGTK_LEN) < 0) {
1936 wpa_printf(MSG_INFO, "RSN: Failed to get new random "
1940 wpa_hexdump_key(MSG_DEBUG, "IGTK",
1941 group->IGTK[group->GN_igtk - 4], WPA_IGTK_LEN);
1943 #endif /* CONFIG_IEEE80211W */
1949 static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
1950 struct wpa_group *group)
1952 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
1953 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
1954 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
1955 group->wpa_group_state = WPA_GROUP_GTK_INIT;
1958 os_memset(group->GTK, 0, sizeof(group->GTK));
1961 #ifdef CONFIG_IEEE80211W
1964 #endif /* CONFIG_IEEE80211W */
1965 /* GTK[GN] = CalcGTK() */
1966 wpa_gtk_update(wpa_auth, group);
1970 static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
1972 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
1973 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1974 "Not in PTKINITDONE; skip Group Key update");
1977 if (sm->GUpdateStationKeys) {
1979 * This should not really happen, but just in case, make sure
1980 * we do not count the same STA twice in GKeyDoneStations.
1982 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1983 "GUpdateStationKeys already set - do not "
1984 "increment GKeyDoneStations");
1986 sm->group->GKeyDoneStations++;
1987 sm->GUpdateStationKeys = TRUE;
1994 static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
1995 struct wpa_group *group)
1999 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2000 "SETKEYS (VLAN-ID %d)", group->vlan_id);
2001 group->changed = TRUE;
2002 group->wpa_group_state = WPA_GROUP_SETKEYS;
2003 group->GTKReKey = FALSE;
2005 group->GM = group->GN;
2007 #ifdef CONFIG_IEEE80211W
2008 tmp = group->GM_igtk;
2009 group->GM_igtk = group->GN_igtk;
2010 group->GN_igtk = tmp;
2011 #endif /* CONFIG_IEEE80211W */
2012 /* "GKeyDoneStations = GNoStations" is done in more robust way by
2013 * counting the STAs that are marked with GUpdateStationKeys instead of
2014 * including all STAs that could be in not-yet-completed state. */
2015 wpa_gtk_update(wpa_auth, group);
2017 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, NULL);
2018 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
2019 group->GKeyDoneStations);
2023 static void wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
2024 struct wpa_group *group)
2026 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2027 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
2028 group->changed = TRUE;
2029 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
2030 wpa_auth_set_key(wpa_auth, group->vlan_id,
2031 wpa_alg_enum(wpa_auth->conf.wpa_group),
2032 NULL, group->GN, group->GTK[group->GN - 1],
2035 #ifdef CONFIG_IEEE80211W
2036 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
2037 wpa_auth_set_key(wpa_auth, group->vlan_id, WPA_ALG_IGTK,
2038 NULL, group->GN_igtk,
2039 group->IGTK[group->GN_igtk - 4],
2042 #endif /* CONFIG_IEEE80211W */
2046 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
2047 struct wpa_group *group)
2050 wpa_group_gtk_init(wpa_auth, group);
2051 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
2052 group->GTKAuthenticator) {
2053 wpa_group_setkeysdone(wpa_auth, group);
2054 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
2056 wpa_group_setkeys(wpa_auth, group);
2057 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
2058 if (group->GKeyDoneStations == 0)
2059 wpa_group_setkeysdone(wpa_auth, group);
2060 else if (group->GTKReKey)
2061 wpa_group_setkeys(wpa_auth, group);
2066 static int wpa_sm_step(struct wpa_state_machine *sm)
2071 if (sm->in_step_loop) {
2072 /* This should not happen, but if it does, make sure we do not
2073 * end up freeing the state machine too early by exiting the
2074 * recursive call. */
2075 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
2079 sm->in_step_loop = 1;
2081 if (sm->pending_deinit)
2084 sm->changed = FALSE;
2085 sm->wpa_auth->group->changed = FALSE;
2087 SM_STEP_RUN(WPA_PTK);
2088 if (sm->pending_deinit)
2090 SM_STEP_RUN(WPA_PTK_GROUP);
2091 if (sm->pending_deinit)
2093 wpa_group_sm_step(sm->wpa_auth, sm->group);
2094 } while (sm->changed || sm->wpa_auth->group->changed);
2095 sm->in_step_loop = 0;
2097 if (sm->pending_deinit) {
2098 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
2099 "machine deinit for " MACSTR, MAC2STR(sm->addr));
2100 wpa_free_sta_sm(sm);
2107 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
2109 struct wpa_state_machine *sm = eloop_ctx;
2114 void wpa_auth_sm_notify(struct wpa_state_machine *sm)
2118 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
2122 void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
2125 struct wpa_group *group;
2127 if (wpa_auth == NULL)
2130 group = wpa_auth->group;
2132 for (i = 0; i < 2; i++) {
2134 group->GM = group->GN;
2136 #ifdef CONFIG_IEEE80211W
2137 tmp = group->GM_igtk;
2138 group->GM_igtk = group->GN_igtk;
2139 group->GN_igtk = tmp;
2140 #endif /* CONFIG_IEEE80211W */
2141 wpa_gtk_update(wpa_auth, group);
2146 static const char * wpa_bool_txt(int bool)
2148 return bool ? "TRUE" : "FALSE";
2152 static int wpa_cipher_bits(int cipher)
2155 case WPA_CIPHER_CCMP:
2157 case WPA_CIPHER_TKIP:
2159 case WPA_CIPHER_WEP104:
2161 case WPA_CIPHER_WEP40:
2169 #define RSN_SUITE "%02x-%02x-%02x-%d"
2170 #define RSN_SUITE_ARG(s) \
2171 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2173 int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
2176 char pmkid_txt[PMKID_LEN * 2 + 1];
2178 if (wpa_auth == NULL)
2181 ret = os_snprintf(buf + len, buflen - len,
2182 "dot11RSNAOptionImplemented=TRUE\n"
2183 #ifdef CONFIG_RSN_PREAUTH
2184 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2185 #else /* CONFIG_RSN_PREAUTH */
2186 "dot11RSNAPreauthenticationImplemented=FALSE\n"
2187 #endif /* CONFIG_RSN_PREAUTH */
2188 "dot11RSNAEnabled=%s\n"
2189 "dot11RSNAPreauthenticationEnabled=%s\n",
2190 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
2191 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
2192 if (ret < 0 || (size_t) ret >= buflen - len)
2196 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2197 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
2200 buf + len, buflen - len,
2201 "dot11RSNAConfigVersion=%u\n"
2202 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
2203 /* FIX: dot11RSNAConfigGroupCipher */
2204 /* FIX: dot11RSNAConfigGroupRekeyMethod */
2205 /* FIX: dot11RSNAConfigGroupRekeyTime */
2206 /* FIX: dot11RSNAConfigGroupRekeyPackets */
2207 "dot11RSNAConfigGroupRekeyStrict=%u\n"
2208 "dot11RSNAConfigGroupUpdateCount=%u\n"
2209 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
2210 "dot11RSNAConfigGroupCipherSize=%u\n"
2211 "dot11RSNAConfigPMKLifetime=%u\n"
2212 "dot11RSNAConfigPMKReauthThreshold=%u\n"
2213 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
2214 "dot11RSNAConfigSATimeout=%u\n"
2215 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2216 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2217 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2218 "dot11RSNAPMKIDUsed=%s\n"
2219 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2220 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2221 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2222 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
2223 "dot11RSNA4WayHandshakeFailures=%u\n"
2224 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
2226 !!wpa_auth->conf.wpa_strict_rekey,
2227 dot11RSNAConfigGroupUpdateCount,
2228 dot11RSNAConfigPairwiseUpdateCount,
2229 wpa_cipher_bits(wpa_auth->conf.wpa_group),
2230 dot11RSNAConfigPMKLifetime,
2231 dot11RSNAConfigPMKReauthThreshold,
2232 dot11RSNAConfigSATimeout,
2233 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
2234 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
2235 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
2237 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
2238 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
2239 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
2240 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
2241 wpa_auth->dot11RSNA4WayHandshakeFailures);
2242 if (ret < 0 || (size_t) ret >= buflen - len)
2246 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
2247 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
2250 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
2251 wpa_auth->group->wpa_group_state);
2252 if (ret < 0 || (size_t) ret >= buflen - len)
2260 int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
2268 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
2270 /* dot11RSNAStatsEntry */
2272 if (sm->wpa == WPA_VERSION_WPA) {
2273 if (sm->pairwise == WPA_CIPHER_CCMP)
2274 pairwise = WPA_CIPHER_SUITE_CCMP;
2275 else if (sm->pairwise == WPA_CIPHER_TKIP)
2276 pairwise = WPA_CIPHER_SUITE_TKIP;
2277 else if (sm->pairwise == WPA_CIPHER_WEP104)
2278 pairwise = WPA_CIPHER_SUITE_WEP104;
2279 else if (sm->pairwise == WPA_CIPHER_WEP40)
2280 pairwise = WPA_CIPHER_SUITE_WEP40;
2281 else if (sm->pairwise == WPA_CIPHER_NONE)
2282 pairwise = WPA_CIPHER_SUITE_NONE;
2283 } else if (sm->wpa == WPA_VERSION_WPA2) {
2284 if (sm->pairwise == WPA_CIPHER_CCMP)
2285 pairwise = RSN_CIPHER_SUITE_CCMP;
2286 else if (sm->pairwise == WPA_CIPHER_TKIP)
2287 pairwise = RSN_CIPHER_SUITE_TKIP;
2288 else if (sm->pairwise == WPA_CIPHER_WEP104)
2289 pairwise = RSN_CIPHER_SUITE_WEP104;
2290 else if (sm->pairwise == WPA_CIPHER_WEP40)
2291 pairwise = RSN_CIPHER_SUITE_WEP40;
2292 else if (sm->pairwise == WPA_CIPHER_NONE)
2293 pairwise = RSN_CIPHER_SUITE_NONE;
2298 buf + len, buflen - len,
2299 /* TODO: dot11RSNAStatsIndex */
2300 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
2301 "dot11RSNAStatsVersion=1\n"
2302 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
2303 /* TODO: dot11RSNAStatsTKIPICVErrors */
2304 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
2305 "dot11RSNAStatsTKIPRemoveMICFailures=%u\n"
2306 /* TODO: dot11RSNAStatsCCMPReplays */
2307 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
2308 /* TODO: dot11RSNAStatsTKIPReplays */,
2310 RSN_SUITE_ARG(pairwise),
2311 sm->dot11RSNAStatsTKIPLocalMICFailures,
2312 sm->dot11RSNAStatsTKIPRemoteMICFailures);
2313 if (ret < 0 || (size_t) ret >= buflen - len)
2318 ret = os_snprintf(buf + len, buflen - len,
2319 "hostapdWPAPTKState=%d\n"
2320 "hostapdWPAPTKGroupState=%d\n",
2322 sm->wpa_ptk_group_state);
2323 if (ret < 0 || (size_t) ret >= buflen - len)
2331 void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
2334 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
2338 int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
2340 return sm && sm->pairwise_set;
2344 int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
2346 return sm->pairwise;
2350 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
2354 return sm->wpa_key_mgmt;
2358 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
2366 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
2367 struct rsn_pmksa_cache_entry *entry)
2369 if (sm == NULL || sm->pmksa != entry)
2376 struct rsn_pmksa_cache_entry *
2377 wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
2379 return sm ? sm->pmksa : NULL;
2383 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
2386 sm->dot11RSNAStatsTKIPLocalMICFailures++;
2390 const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
2392 if (wpa_auth == NULL)
2394 *len = wpa_auth->wpa_ie_len;
2395 return wpa_auth->wpa_ie;
2399 int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
2400 int session_timeout, struct eapol_state_machine *eapol)
2402 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2)
2405 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
2406 sm->wpa_auth->addr, sm->addr, session_timeout,
2407 eapol, sm->wpa_key_mgmt))
2414 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
2415 const u8 *pmk, size_t len, const u8 *sta_addr,
2416 int session_timeout,
2417 struct eapol_state_machine *eapol)
2419 if (wpa_auth == NULL)
2422 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, wpa_auth->addr,
2423 sta_addr, session_timeout, eapol,
2424 WPA_KEY_MGMT_IEEE8021X))
2431 static struct wpa_group *
2432 wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
2434 struct wpa_group *group;
2436 if (wpa_auth == NULL || wpa_auth->group == NULL)
2439 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
2441 group = wpa_group_init(wpa_auth, vlan_id);
2445 group->next = wpa_auth->group->next;
2446 wpa_auth->group->next = group;
2452 int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
2454 struct wpa_group *group;
2456 if (sm == NULL || sm->wpa_auth == NULL)
2459 group = sm->wpa_auth->group;
2461 if (group->vlan_id == vlan_id)
2463 group = group->next;
2466 if (group == NULL) {
2467 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
2472 if (sm->group == group)
2475 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
2476 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);