2 * hostapd - IEEE 802.11i-2004 / WPA Authenticator
3 * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
17 #ifndef CONFIG_NATIVE_WINDOWS
29 #include "ieee802_11.h"
30 #include "pmksa_cache.h"
31 #include "state_machine.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 void 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);
48 /* Default timeouts are 100 ms, but this seems to be a bit too fast for most
49 * WPA Supplicants, so use a bit longer timeout. */
50 static const u32 dot11RSNAConfigGroupUpdateTimeOut = 1000; /* ms */
51 static const u32 dot11RSNAConfigGroupUpdateCount = 3;
52 static const u32 dot11RSNAConfigPairwiseUpdateTimeOut = 1000; /* ms */
53 static const u32 dot11RSNAConfigPairwiseUpdateCount = 3;
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 const char *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);
126 static inline int wpa_auth_get_seqnum_igtk(struct wpa_authenticator *wpa_auth,
127 const u8 *addr, int idx, u8 *seq)
129 if (wpa_auth->cb.get_seqnum_igtk == NULL)
131 return wpa_auth->cb.get_seqnum_igtk(wpa_auth->cb.ctx, addr, idx, seq);
136 wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
137 const u8 *data, size_t data_len, int encrypt)
139 if (wpa_auth->cb.send_eapol == NULL)
141 return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
146 int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
147 int (*cb)(struct wpa_state_machine *sm, void *ctx),
150 if (wpa_auth->cb.for_each_sta == NULL)
152 return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
156 int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
157 int (*cb)(struct wpa_authenticator *a, void *ctx),
160 if (wpa_auth->cb.for_each_auth == NULL)
162 return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
166 void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
167 logger_level level, const char *txt)
169 if (wpa_auth->cb.logger == NULL)
171 wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
175 void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
176 logger_level level, const char *fmt, ...)
182 if (wpa_auth->cb.logger == NULL)
185 maxlen = os_strlen(fmt) + 100;
186 format = os_malloc(maxlen);
191 vsnprintf(format, maxlen, fmt, ap);
194 wpa_auth_logger(wpa_auth, addr, level, format);
200 static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
203 if (wpa_auth->cb.disconnect == NULL)
205 wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
206 WLAN_REASON_PREV_AUTH_NOT_VALID);
210 static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
213 #ifdef CONFIG_IEEE80211R
214 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
216 #endif /* CONFIG_IEEE80211R */
217 #ifdef CONFIG_IEEE80211W
218 if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
220 #endif /* CONFIG_IEEE80211W */
225 static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
227 struct wpa_authenticator *wpa_auth = eloop_ctx;
229 if (os_get_random(wpa_auth->group->GMK, WPA_GMK_LEN)) {
230 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
233 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
236 if (wpa_auth->conf.wpa_gmk_rekey) {
237 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
238 wpa_rekey_gmk, wpa_auth, NULL);
243 static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
245 struct wpa_authenticator *wpa_auth = eloop_ctx;
246 struct wpa_group *group;
248 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
249 for (group = wpa_auth->group; group; group = group->next) {
250 group->GTKReKey = TRUE;
252 group->changed = FALSE;
253 wpa_group_sm_step(wpa_auth, group);
254 } while (group->changed);
257 if (wpa_auth->conf.wpa_group_rekey) {
258 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
259 0, wpa_rekey_gtk, wpa_auth, NULL);
264 static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
266 struct wpa_authenticator *wpa_auth = eloop_ctx;
267 struct wpa_state_machine *sm = timeout_ctx;
269 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
270 wpa_request_new_ptk(sm);
275 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
277 if (sm->pmksa == ctx)
283 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
286 struct wpa_authenticator *wpa_auth = ctx;
287 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
291 static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
294 struct wpa_group *group;
295 u8 buf[ETH_ALEN + 8 + sizeof(group)];
298 group = os_zalloc(sizeof(struct wpa_group));
302 group->GTKAuthenticator = TRUE;
303 group->vlan_id = vlan_id;
305 switch (wpa_auth->conf.wpa_group) {
306 case WPA_CIPHER_CCMP:
309 case WPA_CIPHER_TKIP:
312 case WPA_CIPHER_WEP104:
315 case WPA_CIPHER_WEP40:
320 /* Counter = PRF-256(Random number, "Init Counter",
321 * Local MAC Address || Time)
323 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
324 wpa_get_ntp_timestamp(buf + ETH_ALEN);
325 os_memcpy(buf + ETH_ALEN + 8, &group, sizeof(group));
326 if (os_get_random(rkey, sizeof(rkey)) ||
327 os_get_random(group->GMK, WPA_GMK_LEN)) {
328 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
334 sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
335 group->Counter, WPA_NONCE_LEN);
338 wpa_group_sm_step(wpa_auth, group);
339 group->GInit = FALSE;
340 wpa_group_sm_step(wpa_auth, group);
347 * wpa_init - Initialize WPA authenticator
348 * @addr: Authenticator address
349 * @conf: Configuration for WPA authenticator
350 * Returns: Pointer to WPA authenticator data or %NULL on failure
352 struct wpa_authenticator * wpa_init(const u8 *addr,
353 struct wpa_auth_config *conf,
354 struct wpa_auth_callbacks *cb)
356 struct wpa_authenticator *wpa_auth;
358 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
359 if (wpa_auth == NULL)
361 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
362 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
363 os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
365 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
366 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
371 wpa_auth->group = wpa_group_init(wpa_auth, 0);
372 if (wpa_auth->group == NULL) {
373 os_free(wpa_auth->wpa_ie);
378 wpa_auth->pmksa = pmksa_cache_init(wpa_auth_pmksa_free_cb, wpa_auth);
379 if (wpa_auth->pmksa == NULL) {
380 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
381 os_free(wpa_auth->wpa_ie);
386 #ifdef CONFIG_IEEE80211R
387 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
388 if (wpa_auth->ft_pmk_cache == NULL) {
389 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
390 os_free(wpa_auth->wpa_ie);
391 pmksa_cache_deinit(wpa_auth->pmksa);
395 #endif /* CONFIG_IEEE80211R */
397 if (wpa_auth->conf.wpa_gmk_rekey) {
398 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
399 wpa_rekey_gmk, wpa_auth, NULL);
402 if (wpa_auth->conf.wpa_group_rekey) {
403 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
404 wpa_rekey_gtk, wpa_auth, NULL);
412 * wpa_deinit - Deinitialize WPA authenticator
413 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
415 void wpa_deinit(struct wpa_authenticator *wpa_auth)
417 struct wpa_group *group, *prev;
419 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
420 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
422 #ifdef CONFIG_PEERKEY
423 while (wpa_auth->stsl_negotiations)
424 wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
425 #endif /* CONFIG_PEERKEY */
427 pmksa_cache_deinit(wpa_auth->pmksa);
429 #ifdef CONFIG_IEEE80211R
430 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
431 wpa_auth->ft_pmk_cache = NULL;
432 #endif /* CONFIG_IEEE80211R */
434 os_free(wpa_auth->wpa_ie);
436 group = wpa_auth->group;
448 * wpa_reconfig - Update WPA authenticator configuration
449 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
450 * @conf: Configuration for WPA authenticator
452 int wpa_reconfig(struct wpa_authenticator *wpa_auth,
453 struct wpa_auth_config *conf)
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.");
468 struct wpa_state_machine *
469 wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr)
471 struct wpa_state_machine *sm;
473 sm = os_zalloc(sizeof(struct wpa_state_machine));
476 os_memcpy(sm->addr, addr, ETH_ALEN);
478 sm->wpa_auth = wpa_auth;
479 sm->group = wpa_auth->group;
485 void wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
486 struct wpa_state_machine *sm)
488 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
491 #ifdef CONFIG_IEEE80211R
492 if (sm->ft_completed) {
493 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
494 "FT authentication already completed - do not "
495 "start 4-way handshake");
498 #endif /* CONFIG_IEEE80211R */
501 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
502 sm->ReAuthenticationRequest = TRUE;
507 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
508 "start authentication");
514 sm->AuthenticationRequest = TRUE;
519 void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
521 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
522 * reassociates back to the same AP while the previous entry for the
523 * STA has not yet been removed. */
527 sm->wpa_key_mgmt = 0;
531 static void wpa_free_sta_sm(struct wpa_state_machine *sm)
533 os_free(sm->last_rx_eapol_key);
539 void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
544 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
545 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
546 "strict rekeying - force GTK rekey since STA "
548 eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
549 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
553 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
554 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
555 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
556 if (sm->in_step_loop) {
557 /* Must not free state machine while wpa_sm_step() is running.
558 * Freeing will be completed in the end of wpa_sm_step(). */
559 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
560 "machine deinit for " MACSTR, MAC2STR(sm->addr));
561 sm->pending_deinit = 1;
567 static void wpa_request_new_ptk(struct wpa_state_machine *sm)
572 sm->PTKRequest = TRUE;
577 static int wpa_replay_counter_valid(struct wpa_state_machine *sm,
578 const u8 *replay_counter)
581 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
582 if (!sm->key_replay[i].valid)
584 if (os_memcmp(replay_counter, sm->key_replay[i].counter,
585 WPA_REPLAY_COUNTER_LEN) == 0)
592 void wpa_receive(struct wpa_authenticator *wpa_auth,
593 struct wpa_state_machine *sm,
594 u8 *data, size_t data_len)
596 struct ieee802_1x_hdr *hdr;
597 struct wpa_eapol_key *key;
598 u16 key_info, key_data_length;
599 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
600 SMK_M1, SMK_M3, SMK_ERROR } msg;
602 struct wpa_eapol_ie_parse kde;
604 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
607 if (data_len < sizeof(*hdr) + sizeof(*key))
610 hdr = (struct ieee802_1x_hdr *) data;
611 key = (struct wpa_eapol_key *) (hdr + 1);
612 key_info = WPA_GET_BE16(key->key_info);
613 key_data_length = WPA_GET_BE16(key->key_data_length);
614 if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) {
615 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
616 "key_data overflow (%d > %lu)",
618 (unsigned long) (data_len - sizeof(*hdr) -
623 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
626 if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
627 (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
628 if (key_info & WPA_KEY_INFO_ERROR) {
630 msgtxt = "SMK Error";
635 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
638 } else if (key_info & WPA_KEY_INFO_REQUEST) {
641 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
643 msgtxt = "2/2 Group";
644 } else if (key_data_length == 0) {
646 msgtxt = "4/4 Pairwise";
649 msgtxt = "2/4 Pairwise";
652 /* TODO: key_info type validation for PeerKey */
653 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
655 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
656 if (sm->pairwise == WPA_CIPHER_CCMP) {
657 if (wpa_use_aes_cmac(sm) &&
658 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
659 wpa_auth_logger(wpa_auth, sm->addr,
661 "advertised support for "
662 "AES-128-CMAC, but did not "
667 if (!wpa_use_aes_cmac(sm) &&
668 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
669 wpa_auth_logger(wpa_auth, sm->addr,
671 "did not use HMAC-SHA1-AES "
678 if (key_info & WPA_KEY_INFO_REQUEST) {
679 if (sm->req_replay_counter_used &&
680 os_memcmp(key->replay_counter, sm->req_replay_counter,
681 WPA_REPLAY_COUNTER_LEN) <= 0) {
682 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
683 "received EAPOL-Key request with "
689 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
690 !wpa_replay_counter_valid(sm, key->replay_counter)) {
692 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
693 "received EAPOL-Key %s with unexpected "
694 "replay counter", msgtxt);
695 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
696 if (!sm->key_replay[i].valid)
698 wpa_hexdump(MSG_DEBUG, "pending replay counter",
699 sm->key_replay[i].counter,
700 WPA_REPLAY_COUNTER_LEN);
702 wpa_hexdump(MSG_DEBUG, "received replay counter",
703 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
709 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
710 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING) {
711 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
712 "received EAPOL-Key msg 2/4 in "
713 "invalid state (%d) - dropped",
717 if (sm->wpa_ie == NULL ||
718 sm->wpa_ie_len != key_data_length ||
719 os_memcmp(sm->wpa_ie, key + 1, key_data_length) != 0) {
720 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
721 "WPA IE from (Re)AssocReq did not "
722 "match with msg 2/4");
724 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
725 sm->wpa_ie, sm->wpa_ie_len);
727 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
728 (u8 *) (key + 1), key_data_length);
729 /* MLME-DEAUTHENTICATE.request */
730 wpa_sta_disconnect(wpa_auth, sm->addr);
735 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
737 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
738 "received EAPOL-Key msg 4/4 in "
739 "invalid state (%d) - dropped",
745 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
747 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
748 "received EAPOL-Key msg 2/2 in "
749 "invalid state (%d) - dropped",
750 sm->wpa_ptk_group_state);
754 #ifdef CONFIG_PEERKEY
758 if (!wpa_auth->conf.peerkey) {
759 wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
760 "PeerKey use disabled - ignoring message");
763 if (!sm->PTK_valid) {
764 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
765 "received EAPOL-Key msg SMK in "
766 "invalid state - dropped");
770 #else /* CONFIG_PEERKEY */
774 return; /* STSL disabled - ignore SMK messages */
775 #endif /* CONFIG_PEERKEY */
780 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
781 "received EAPOL-Key frame (%s)", msgtxt);
783 if (key_info & WPA_KEY_INFO_ACK) {
784 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
785 "received invalid EAPOL-Key: Key Ack set");
789 if (!(key_info & WPA_KEY_INFO_MIC)) {
790 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
791 "received invalid EAPOL-Key: Key MIC not set");
795 sm->MICVerified = FALSE;
797 if (wpa_verify_key_mic(&sm->PTK, data, data_len)) {
798 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
799 "received EAPOL-Key with invalid MIC");
802 sm->MICVerified = TRUE;
803 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
806 if (key_info & WPA_KEY_INFO_REQUEST) {
807 if (sm->MICVerified) {
808 sm->req_replay_counter_used = 1;
809 os_memcpy(sm->req_replay_counter, key->replay_counter,
810 WPA_REPLAY_COUNTER_LEN);
812 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
813 "received EAPOL-Key request with "
819 * TODO: should decrypt key data field if encryption was used;
820 * even though MAC address KDE is not normally encrypted,
821 * supplicant is allowed to encrypt it.
823 if (msg == SMK_ERROR) {
824 #ifdef CONFIG_PEERKEY
825 wpa_smk_error(wpa_auth, sm, key);
826 #endif /* CONFIG_PEERKEY */
828 } else if (key_info & WPA_KEY_INFO_ERROR) {
829 /* Supplicant reported a Michael MIC error */
830 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
831 "received EAPOL-Key Error Request "
832 "(STA detected Michael MIC failure)");
833 wpa_auth_mic_failure_report(wpa_auth, sm->addr);
834 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
835 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
836 /* Error report is not a request for a new key
837 * handshake, but since Authenticator may do it, let's
838 * change the keys now anyway. */
839 wpa_request_new_ptk(sm);
840 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
841 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
842 "received EAPOL-Key Request for new "
844 wpa_request_new_ptk(sm);
845 #ifdef CONFIG_PEERKEY
846 } else if (msg == SMK_M1) {
847 wpa_smk_m1(wpa_auth, sm, key);
848 #endif /* CONFIG_PEERKEY */
849 } else if (key_data_length > 0 &&
850 wpa_parse_kde_ies((const u8 *) (key + 1),
851 key_data_length, &kde) == 0 &&
854 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
855 "received EAPOL-Key Request for GTK "
857 /* FIX: why was this triggering PTK rekeying for the
858 * STA that requested Group Key rekeying?? */
859 /* wpa_request_new_ptk(sta->wpa_sm); */
860 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
861 wpa_rekey_gtk(wpa_auth, NULL);
864 /* Do not allow the same key replay counter to be reused. This
865 * does also invalidate all other pending replay counters if
866 * retransmissions were used, i.e., we will only process one of
867 * the pending replies and ignore rest if more than one is
869 sm->key_replay[0].valid = FALSE;
872 #ifdef CONFIG_PEERKEY
874 wpa_smk_m3(wpa_auth, sm, key);
877 #endif /* CONFIG_PEERKEY */
879 os_free(sm->last_rx_eapol_key);
880 sm->last_rx_eapol_key = os_malloc(data_len);
881 if (sm->last_rx_eapol_key == NULL)
883 os_memcpy(sm->last_rx_eapol_key, data, data_len);
884 sm->last_rx_eapol_key_len = data_len;
886 sm->EAPOLKeyReceived = TRUE;
887 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
888 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
889 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
894 static void wpa_gmk_to_gtk(const u8 *gmk, const u8 *addr, const u8 *gnonce,
895 u8 *gtk, size_t gtk_len)
897 u8 data[ETH_ALEN + WPA_NONCE_LEN];
899 /* GTK = PRF-X(GMK, "Group key expansion", AA || GNonce) */
900 os_memcpy(data, addr, ETH_ALEN);
901 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
903 #ifdef CONFIG_IEEE80211W
904 sha256_prf(gmk, WPA_GMK_LEN, "Group key expansion",
905 data, sizeof(data), gtk, gtk_len);
906 #else /* CONFIG_IEEE80211W */
907 sha1_prf(gmk, WPA_GMK_LEN, "Group key expansion",
908 data, sizeof(data), gtk, gtk_len);
909 #endif /* CONFIG_IEEE80211W */
911 wpa_hexdump_key(MSG_DEBUG, "GMK", gmk, WPA_GMK_LEN);
912 wpa_hexdump_key(MSG_DEBUG, "GTK", gtk, gtk_len);
916 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
918 struct wpa_authenticator *wpa_auth = eloop_ctx;
919 struct wpa_state_machine *sm = timeout_ctx;
921 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
922 sm->TimeoutEvt = TRUE;
927 void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
928 struct wpa_state_machine *sm, int key_info,
929 const u8 *key_rsc, const u8 *nonce,
930 const u8 *kde, size_t kde_len,
931 int keyidx, int encr, int force_version)
933 struct ieee802_1x_hdr *hdr;
934 struct wpa_eapol_key *key;
937 int key_data_len, pad_len = 0;
939 int version, pairwise;
942 len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
945 version = force_version;
946 else if (wpa_use_aes_cmac(sm))
947 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
948 else if (sm->pairwise == WPA_CIPHER_CCMP)
949 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
951 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
953 pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
955 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
956 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
959 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
960 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
961 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
962 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
963 pairwise, (unsigned long) kde_len, keyidx, encr);
965 key_data_len = kde_len;
967 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
968 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
969 pad_len = key_data_len % 8;
971 pad_len = 8 - pad_len;
972 key_data_len += pad_len + 8;
977 hdr = os_zalloc(len);
980 hdr->version = wpa_auth->conf.eapol_version;
981 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
982 hdr->length = host_to_be16(len - sizeof(*hdr));
983 key = (struct wpa_eapol_key *) (hdr + 1);
985 key->type = sm->wpa == WPA_VERSION_WPA2 ?
986 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
988 if (encr && sm->wpa == WPA_VERSION_WPA2)
989 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
990 if (sm->wpa != WPA_VERSION_WPA2)
991 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
992 WPA_PUT_BE16(key->key_info, key_info);
994 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
996 case WPA_CIPHER_CCMP:
997 WPA_PUT_BE16(key->key_length, 16);
999 case WPA_CIPHER_TKIP:
1000 WPA_PUT_BE16(key->key_length, 32);
1002 case WPA_CIPHER_WEP40:
1003 WPA_PUT_BE16(key->key_length, 5);
1005 case WPA_CIPHER_WEP104:
1006 WPA_PUT_BE16(key->key_length, 13);
1009 if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
1010 WPA_PUT_BE16(key->key_length, 0);
1012 /* FIX: STSL: what to use as key_replay_counter? */
1013 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1014 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1015 os_memcpy(sm->key_replay[i].counter,
1016 sm->key_replay[i - 1].counter,
1017 WPA_REPLAY_COUNTER_LEN);
1019 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1020 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1021 WPA_REPLAY_COUNTER_LEN);
1022 sm->key_replay[0].valid = TRUE;
1025 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1028 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1031 os_memcpy(key + 1, kde, kde_len);
1032 WPA_PUT_BE16(key->key_data_length, kde_len);
1033 } else if (encr && kde) {
1034 buf = os_zalloc(key_data_len);
1040 os_memcpy(pos, kde, kde_len);
1046 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1048 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1049 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1050 if (aes_wrap(sm->PTK.kek, (key_data_len - 8) / 8, buf,
1051 (u8 *) (key + 1))) {
1056 WPA_PUT_BE16(key->key_data_length, key_data_len);
1059 os_memcpy(key->key_iv,
1060 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1061 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1062 os_memcpy(ek, key->key_iv, 16);
1063 os_memcpy(ek + 16, sm->PTK.kek, 16);
1064 os_memcpy(key + 1, buf, key_data_len);
1065 rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
1066 WPA_PUT_BE16(key->key_data_length, key_data_len);
1071 if (key_info & WPA_KEY_INFO_MIC) {
1072 if (!sm->PTK_valid) {
1073 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1074 "PTK not valid when sending EAPOL-Key "
1079 wpa_eapol_key_mic(sm->PTK.kck, version, (u8 *) hdr, len,
1083 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1085 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1091 static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1092 struct wpa_state_machine *sm, int key_info,
1093 const u8 *key_rsc, const u8 *nonce,
1094 const u8 *kde, size_t kde_len,
1095 int keyidx, int encr)
1098 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1103 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1106 timeout_ms = pairwise ? dot11RSNAConfigPairwiseUpdateTimeOut :
1107 dot11RSNAConfigGroupUpdateTimeOut;
1108 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1109 wpa_send_eapol_timeout, wpa_auth, sm);
1113 static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len)
1115 struct ieee802_1x_hdr *hdr;
1116 struct wpa_eapol_key *key;
1121 if (data_len < sizeof(*hdr) + sizeof(*key))
1124 hdr = (struct ieee802_1x_hdr *) data;
1125 key = (struct wpa_eapol_key *) (hdr + 1);
1126 key_info = WPA_GET_BE16(key->key_info);
1127 os_memcpy(mic, key->key_mic, 16);
1128 os_memset(key->key_mic, 0, 16);
1129 if (wpa_eapol_key_mic(PTK->kck, key_info & WPA_KEY_INFO_TYPE_MASK,
1130 data, data_len, key->key_mic) ||
1131 os_memcmp(mic, key->key_mic, 16) != 0)
1133 os_memcpy(key->key_mic, mic, 16);
1138 void wpa_remove_ptk(struct wpa_state_machine *sm)
1140 sm->PTK_valid = FALSE;
1141 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1142 wpa_auth_set_key(sm->wpa_auth, 0, "none", sm->addr, 0, (u8 *) "", 0);
1143 sm->pairwise_set = FALSE;
1144 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1148 void wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
1155 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1156 "event %d notification", event);
1164 sm->DeauthenticationRequest = TRUE;
1167 case WPA_REAUTH_EAPOL:
1168 if (sm->GUpdateStationKeys) {
1170 * Reauthentication cancels the pending group key
1171 * update for this STA.
1173 sm->group->GKeyDoneStations--;
1174 sm->GUpdateStationKeys = FALSE;
1175 sm->PtkGroupInit = TRUE;
1177 sm->ReAuthenticationRequest = TRUE;
1180 #ifdef CONFIG_IEEE80211R
1181 /* Using FT protocol, not WPA auth state machine */
1182 sm->ft_completed = 1;
1184 #else /* CONFIG_IEEE80211R */
1186 #endif /* CONFIG_IEEE80211R */
1189 #ifdef CONFIG_IEEE80211R
1190 sm->ft_completed = 0;
1191 #endif /* CONFIG_IEEE80211R */
1193 #ifdef CONFIG_IEEE80211W
1194 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1196 #endif /* CONFIG_IEEE80211W */
1199 sm->PTK_valid = FALSE;
1200 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1202 if (event != WPA_REAUTH_EAPOL)
1210 static const char * wpa_alg_txt(int alg)
1213 case WPA_CIPHER_CCMP:
1215 case WPA_CIPHER_TKIP:
1217 case WPA_CIPHER_WEP104:
1218 case WPA_CIPHER_WEP40:
1226 SM_STATE(WPA_PTK, INITIALIZE)
1228 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1230 /* Init flag is not cleared here, so avoid busy
1231 * loop by claiming nothing changed. */
1232 sm->changed = FALSE;
1236 if (sm->GUpdateStationKeys)
1237 sm->group->GKeyDoneStations--;
1238 sm->GUpdateStationKeys = FALSE;
1239 if (sm->wpa == WPA_VERSION_WPA)
1240 sm->PInitAKeys = FALSE;
1241 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1242 * Local AA > Remote AA)) */) {
1245 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1247 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1249 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1250 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1251 WPA_EAPOL_authorized, 0);
1256 SM_STATE(WPA_PTK, DISCONNECT)
1258 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1259 sm->Disconnect = FALSE;
1260 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1264 SM_STATE(WPA_PTK, DISCONNECTED)
1266 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1267 sm->DeauthenticationRequest = FALSE;
1271 SM_STATE(WPA_PTK, AUTHENTICATION)
1273 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1274 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1275 sm->PTK_valid = FALSE;
1276 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1278 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1279 sm->AuthenticationRequest = FALSE;
1283 SM_STATE(WPA_PTK, AUTHENTICATION2)
1285 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1286 os_memcpy(sm->ANonce, sm->group->Counter, WPA_NONCE_LEN);
1287 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1288 sm->ReAuthenticationRequest = FALSE;
1289 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1290 * logical place than INITIALIZE since AUTHENTICATION2 can be
1291 * re-entered on ReAuthenticationRequest without going through
1297 SM_STATE(WPA_PTK, INITPMK)
1299 u8 msk[2 * PMK_LEN];
1300 size_t len = 2 * PMK_LEN;
1302 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1303 #ifdef CONFIG_IEEE80211R
1305 #endif /* CONFIG_IEEE80211R */
1307 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1308 os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
1309 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1310 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1311 "(len=%lu)", (unsigned long) len);
1312 os_memcpy(sm->PMK, msk, PMK_LEN);
1313 #ifdef CONFIG_IEEE80211R
1314 if (len >= 2 * PMK_LEN) {
1315 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1316 sm->xxkey_len = PMK_LEN;
1318 #endif /* CONFIG_IEEE80211R */
1320 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
1323 sm->req_replay_counter_used = 0;
1324 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1325 * will break reauthentication since EAPOL state machines may not be
1326 * get into AUTHENTICATING state that clears keyRun before WPA state
1327 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1328 * state and takes PMK from the previously used AAA Key. This will
1329 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1330 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1331 * be good workaround for this issue. */
1332 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1336 SM_STATE(WPA_PTK, INITPSK)
1339 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
1340 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL);
1342 os_memcpy(sm->PMK, psk, PMK_LEN);
1343 #ifdef CONFIG_IEEE80211R
1344 os_memcpy(sm->xxkey, psk, PMK_LEN);
1345 sm->xxkey_len = PMK_LEN;
1346 #endif /* CONFIG_IEEE80211R */
1348 sm->req_replay_counter_used = 0;
1352 SM_STATE(WPA_PTK, PTKSTART)
1354 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1355 size_t pmkid_len = 0;
1357 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
1358 sm->PTKRequest = FALSE;
1359 sm->TimeoutEvt = FALSE;
1360 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1361 "sending 1/4 msg of 4-Way Handshake");
1363 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1364 * one possible PSK for this STA.
1366 if (sm->wpa == WPA_VERSION_WPA2 &&
1367 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt)) {
1369 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
1370 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
1371 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
1372 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
1374 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
1375 sm->pmksa->pmkid, PMKID_LEN);
1378 * Calculate PMKID since no PMKSA cache entry was
1379 * available with pre-calculated PMKID.
1381 rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
1382 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
1383 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1386 wpa_send_eapol(sm->wpa_auth, sm,
1387 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
1388 sm->ANonce, pmkid, pmkid_len, 0, 0);
1393 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk,
1394 struct wpa_ptk *ptk)
1396 #ifdef CONFIG_IEEE80211R
1397 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
1398 return wpa_auth_derive_ptk_ft(sm, pmk, ptk);
1399 #endif /* CONFIG_IEEE80211R */
1401 wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
1402 sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce,
1403 (u8 *) ptk, sizeof(*ptk),
1404 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1410 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
1414 const u8 *pmk = NULL;
1416 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
1417 sm->EAPOLKeyReceived = FALSE;
1419 /* WPA with IEEE 802.1X: use the derived PMK from EAP
1420 * WPA-PSK: iterate through possible PSKs and select the one matching
1423 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1424 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, pmk);
1430 wpa_derive_ptk(sm, pmk, &PTK);
1432 if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key,
1433 sm->last_rx_eapol_key_len) == 0) {
1438 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
1443 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1444 "invalid MIC in msg 2/4 of 4-Way Handshake");
1448 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
1450 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1451 /* PSK may have changed from the previous choice, so update
1452 * state machine data based on whatever PSK was selected here.
1454 os_memcpy(sm->PMK, pmk, PMK_LEN);
1457 sm->MICVerified = TRUE;
1459 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
1460 sm->PTK_valid = TRUE;
1464 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
1466 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
1471 #ifdef CONFIG_IEEE80211W
1473 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1475 if (sm->mgmt_frame_prot) {
1476 return 2 + RSN_SELECTOR_LEN + sizeof(struct wpa_igtk_kde);
1483 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1485 struct wpa_igtk_kde igtk;
1486 struct wpa_group *gsm = sm->group;
1488 if (!sm->mgmt_frame_prot)
1491 igtk.keyid[0] = gsm->GN_igtk;
1493 if (wpa_auth_get_seqnum_igtk(sm->wpa_auth, NULL, gsm->GN_igtk, igtk.pn)
1495 os_memset(igtk.pn, 0, sizeof(igtk.pn));
1496 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
1497 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
1498 (const u8 *) &igtk, sizeof(igtk), NULL, 0);
1503 #else /* CONFIG_IEEE80211W */
1505 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1511 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1516 #endif /* CONFIG_IEEE80211W */
1519 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
1521 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
1522 size_t gtk_len, kde_len;
1523 struct wpa_group *gsm = sm->group;
1525 int wpa_ie_len, secure, keyidx, encr = 0;
1527 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
1528 sm->TimeoutEvt = FALSE;
1529 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, GTK[GN])
1531 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
1532 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
1533 wpa_ie = sm->wpa_auth->wpa_ie;
1534 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
1535 if (sm->wpa == WPA_VERSION_WPA &&
1536 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
1537 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
1538 /* WPA-only STA, remove RSN IE */
1539 wpa_ie = wpa_ie + wpa_ie[1] + 2;
1540 wpa_ie_len = wpa_ie[1] + 2;
1542 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1543 "sending 3/4 msg of 4-Way Handshake");
1544 if (sm->wpa == WPA_VERSION_WPA2) {
1545 /* WPA2 send GTK in the 4-way handshake */
1547 gtk = gsm->GTK[gsm->GN - 1];
1548 gtk_len = gsm->GTK_len;
1553 /* WPA does not include GTK in msg 3/4 */
1561 kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
1563 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
1564 kde = os_malloc(kde_len);
1569 os_memcpy(pos, wpa_ie, wpa_ie_len);
1573 hdr[0] = keyidx & 0x03;
1575 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
1578 pos = ieee80211w_kde_add(sm, pos);
1580 wpa_send_eapol(sm->wpa_auth, sm,
1581 (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
1582 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
1583 WPA_KEY_INFO_KEY_TYPE,
1584 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
1590 SM_STATE(WPA_PTK, PTKINITDONE)
1592 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
1593 sm->EAPOLKeyReceived = FALSE;
1597 if (sm->pairwise == WPA_CIPHER_TKIP) {
1604 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
1605 sm->PTK.tk1, klen)) {
1606 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1609 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
1610 sm->pairwise_set = TRUE;
1612 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
1613 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1614 eloop_register_timeout(sm->wpa_auth->conf.
1615 wpa_ptk_rekey, 0, wpa_rekey_ptk,
1619 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1620 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1621 WPA_EAPOL_authorized, 1);
1625 if (0 /* IBSS == TRUE */) {
1627 if (sm->keycount == 2) {
1628 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1629 WPA_EAPOL_portValid, 1);
1632 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
1635 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
1636 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
1637 if (sm->wpa == WPA_VERSION_WPA)
1638 sm->PInitAKeys = TRUE;
1641 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1642 "pairwise key handshake completed (%s)",
1643 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
1645 #ifdef CONFIG_IEEE80211R
1646 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
1647 #endif /* CONFIG_IEEE80211R */
1653 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
1656 SM_ENTER(WPA_PTK, INITIALIZE);
1657 else if (sm->Disconnect
1658 /* || FIX: dot11RSNAConfigSALifetime timeout */)
1659 SM_ENTER(WPA_PTK, DISCONNECT);
1660 else if (sm->DeauthenticationRequest)
1661 SM_ENTER(WPA_PTK, DISCONNECTED);
1662 else if (sm->AuthenticationRequest)
1663 SM_ENTER(WPA_PTK, AUTHENTICATION);
1664 else if (sm->ReAuthenticationRequest)
1665 SM_ENTER(WPA_PTK, AUTHENTICATION2);
1666 else if (sm->PTKRequest)
1667 SM_ENTER(WPA_PTK, PTKSTART);
1668 else switch (sm->wpa_ptk_state) {
1669 case WPA_PTK_INITIALIZE:
1671 case WPA_PTK_DISCONNECT:
1672 SM_ENTER(WPA_PTK, DISCONNECTED);
1674 case WPA_PTK_DISCONNECTED:
1675 SM_ENTER(WPA_PTK, INITIALIZE);
1677 case WPA_PTK_AUTHENTICATION:
1678 SM_ENTER(WPA_PTK, AUTHENTICATION2);
1680 case WPA_PTK_AUTHENTICATION2:
1681 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
1682 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
1683 WPA_EAPOL_keyRun) > 0)
1684 SM_ENTER(WPA_PTK, INITPMK);
1685 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
1686 /* FIX: && 802.1X::keyRun */)
1687 SM_ENTER(WPA_PTK, INITPSK);
1689 case WPA_PTK_INITPMK:
1690 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
1691 WPA_EAPOL_keyAvailable) > 0)
1692 SM_ENTER(WPA_PTK, PTKSTART);
1694 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1695 SM_ENTER(WPA_PTK, DISCONNECT);
1698 case WPA_PTK_INITPSK:
1699 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL))
1700 SM_ENTER(WPA_PTK, PTKSTART);
1702 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1703 "no PSK configured for the STA");
1704 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1705 SM_ENTER(WPA_PTK, DISCONNECT);
1708 case WPA_PTK_PTKSTART:
1709 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1710 sm->EAPOLKeyPairwise)
1711 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
1712 else if (sm->TimeoutCtr >
1713 (int) dot11RSNAConfigPairwiseUpdateCount) {
1714 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1715 SM_ENTER(WPA_PTK, DISCONNECT);
1716 } else if (sm->TimeoutEvt)
1717 SM_ENTER(WPA_PTK, PTKSTART);
1719 case WPA_PTK_PTKCALCNEGOTIATING:
1720 if (sm->MICVerified)
1721 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
1722 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1723 sm->EAPOLKeyPairwise)
1724 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
1725 else if (sm->TimeoutEvt)
1726 SM_ENTER(WPA_PTK, PTKSTART);
1728 case WPA_PTK_PTKCALCNEGOTIATING2:
1729 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
1731 case WPA_PTK_PTKINITNEGOTIATING:
1732 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1733 sm->EAPOLKeyPairwise && sm->MICVerified)
1734 SM_ENTER(WPA_PTK, PTKINITDONE);
1735 else if (sm->TimeoutCtr >
1736 (int) dot11RSNAConfigPairwiseUpdateCount) {
1737 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1738 SM_ENTER(WPA_PTK, DISCONNECT);
1739 } else if (sm->TimeoutEvt)
1740 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
1742 case WPA_PTK_PTKINITDONE:
1748 SM_STATE(WPA_PTK_GROUP, IDLE)
1750 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
1752 /* Init flag is not cleared here, so avoid busy
1753 * loop by claiming nothing changed. */
1754 sm->changed = FALSE;
1756 sm->GTimeoutCtr = 0;
1760 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
1762 u8 rsc[WPA_KEY_RSC_LEN];
1763 struct wpa_group *gsm = sm->group;
1764 u8 *kde, *pos, hdr[2];
1767 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
1768 if (sm->wpa == WPA_VERSION_WPA)
1769 sm->PInitAKeys = FALSE;
1770 sm->TimeoutEvt = FALSE;
1771 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
1772 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
1773 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
1774 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
1775 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1776 "sending 1/2 msg of Group Key Handshake");
1778 if (sm->wpa == WPA_VERSION_WPA2) {
1779 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
1780 ieee80211w_kde_len(sm);
1781 kde = os_malloc(kde_len);
1786 hdr[0] = gsm->GN & 0x03;
1788 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
1789 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
1790 pos = ieee80211w_kde_add(sm, pos);
1792 kde = gsm->GTK[gsm->GN - 1];
1793 pos = kde + gsm->GTK_len;
1796 wpa_send_eapol(sm->wpa_auth, sm,
1797 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
1799 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
1800 rsc, gsm->GNonce, kde, pos - kde, gsm->GN, 1);
1801 if (sm->wpa == WPA_VERSION_WPA2)
1807 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
1809 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
1810 sm->EAPOLKeyReceived = FALSE;
1811 if (sm->GUpdateStationKeys)
1812 sm->group->GKeyDoneStations--;
1813 sm->GUpdateStationKeys = FALSE;
1814 sm->GTimeoutCtr = 0;
1815 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
1816 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1817 "group key handshake completed (%s)",
1818 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
1823 SM_STATE(WPA_PTK_GROUP, KEYERROR)
1825 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
1826 if (sm->GUpdateStationKeys)
1827 sm->group->GKeyDoneStations--;
1828 sm->GUpdateStationKeys = FALSE;
1829 sm->Disconnect = TRUE;
1833 SM_STEP(WPA_PTK_GROUP)
1835 if (sm->Init || sm->PtkGroupInit) {
1836 SM_ENTER(WPA_PTK_GROUP, IDLE);
1837 sm->PtkGroupInit = FALSE;
1838 } else switch (sm->wpa_ptk_group_state) {
1839 case WPA_PTK_GROUP_IDLE:
1840 if (sm->GUpdateStationKeys ||
1841 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
1842 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
1844 case WPA_PTK_GROUP_REKEYNEGOTIATING:
1845 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1846 !sm->EAPOLKeyPairwise && sm->MICVerified)
1847 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
1848 else if (sm->GTimeoutCtr >
1849 (int) dot11RSNAConfigGroupUpdateCount)
1850 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
1851 else if (sm->TimeoutEvt)
1852 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
1854 case WPA_PTK_GROUP_KEYERROR:
1855 SM_ENTER(WPA_PTK_GROUP, IDLE);
1857 case WPA_PTK_GROUP_REKEYESTABLISHED:
1858 SM_ENTER(WPA_PTK_GROUP, IDLE);
1864 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
1865 struct wpa_group *group)
1869 /* FIX: is this the correct way of getting GNonce? */
1870 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
1871 inc_byte_array(group->Counter, WPA_NONCE_LEN);
1872 wpa_gmk_to_gtk(group->GMK, wpa_auth->addr, group->GNonce,
1873 group->GTK[group->GN - 1], group->GTK_len);
1875 #ifdef CONFIG_IEEE80211W
1876 if (wpa_auth->conf.ieee80211w != WPA_NO_IEEE80211W) {
1877 if (os_get_random(group->IGTK[group->GN_igtk - 4],
1878 WPA_IGTK_LEN) < 0) {
1879 wpa_printf(MSG_INFO, "RSN: Failed to get new random "
1883 wpa_hexdump_key(MSG_DEBUG, "IGTK",
1884 group->IGTK[group->GN_igtk - 4], WPA_IGTK_LEN);
1886 #endif /* CONFIG_IEEE80211W */
1892 static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
1893 struct wpa_group *group)
1895 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
1896 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
1897 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
1898 group->wpa_group_state = WPA_GROUP_GTK_INIT;
1901 os_memset(group->GTK, 0, sizeof(group->GTK));
1904 #ifdef CONFIG_IEEE80211W
1907 #endif /* CONFIG_IEEE80211W */
1908 /* GTK[GN] = CalcGTK() */
1909 wpa_gtk_update(wpa_auth, group);
1913 static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
1915 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
1916 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1917 "Not in PTKINITDONE; skip Group Key update");
1920 if (sm->GUpdateStationKeys) {
1922 * This should not really happen, but just in case, make sure
1923 * we do not count the same STA twice in GKeyDoneStations.
1925 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1926 "GUpdateStationKeys already set - do not "
1927 "increment GKeyDoneStations");
1929 sm->group->GKeyDoneStations++;
1930 sm->GUpdateStationKeys = TRUE;
1937 static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
1938 struct wpa_group *group)
1942 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
1943 "SETKEYS (VLAN-ID %d)", group->vlan_id);
1944 group->changed = TRUE;
1945 group->wpa_group_state = WPA_GROUP_SETKEYS;
1946 group->GTKReKey = FALSE;
1948 group->GM = group->GN;
1950 #ifdef CONFIG_IEEE80211W
1951 tmp = group->GM_igtk;
1952 group->GM_igtk = group->GN_igtk;
1953 group->GN_igtk = tmp;
1954 #endif /* CONFIG_IEEE80211W */
1955 /* "GKeyDoneStations = GNoStations" is done in more robust way by
1956 * counting the STAs that are marked with GUpdateStationKeys instead of
1957 * including all STAs that could be in not-yet-completed state. */
1958 wpa_gtk_update(wpa_auth, group);
1960 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, NULL);
1961 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
1962 group->GKeyDoneStations);
1966 static void wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
1967 struct wpa_group *group)
1969 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
1970 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
1971 group->changed = TRUE;
1972 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
1973 wpa_auth_set_key(wpa_auth, group->vlan_id,
1974 wpa_alg_txt(wpa_auth->conf.wpa_group),
1975 NULL, group->GN, group->GTK[group->GN - 1],
1978 #ifdef CONFIG_IEEE80211W
1979 if (wpa_auth->conf.ieee80211w != WPA_NO_IEEE80211W) {
1980 wpa_auth_set_key(wpa_auth, group->vlan_id, "IGTK",
1981 NULL, group->GN_igtk,
1982 group->IGTK[group->GN_igtk - 4],
1985 #endif /* CONFIG_IEEE80211W */
1989 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
1990 struct wpa_group *group)
1993 wpa_group_gtk_init(wpa_auth, group);
1994 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
1995 group->GTKAuthenticator) {
1996 wpa_group_setkeysdone(wpa_auth, group);
1997 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
1999 wpa_group_setkeys(wpa_auth, group);
2000 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
2001 if (group->GKeyDoneStations == 0)
2002 wpa_group_setkeysdone(wpa_auth, group);
2003 else if (group->GTKReKey)
2004 wpa_group_setkeys(wpa_auth, group);
2009 static void wpa_sm_step(struct wpa_state_machine *sm)
2014 if (sm->in_step_loop) {
2015 /* This should not happen, but if it does, make sure we do not
2016 * end up freeing the state machine too early by exiting the
2017 * recursive call. */
2018 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
2022 sm->in_step_loop = 1;
2024 if (sm->pending_deinit)
2027 sm->changed = FALSE;
2028 sm->wpa_auth->group->changed = FALSE;
2030 SM_STEP_RUN(WPA_PTK);
2031 if (sm->pending_deinit)
2033 SM_STEP_RUN(WPA_PTK_GROUP);
2034 if (sm->pending_deinit)
2036 wpa_group_sm_step(sm->wpa_auth, sm->group);
2037 } while (sm->changed || sm->wpa_auth->group->changed);
2038 sm->in_step_loop = 0;
2040 if (sm->pending_deinit) {
2041 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
2042 "machine deinit for " MACSTR, MAC2STR(sm->addr));
2043 wpa_free_sta_sm(sm);
2048 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
2050 struct wpa_state_machine *sm = eloop_ctx;
2055 void wpa_auth_sm_notify(struct wpa_state_machine *sm)
2059 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
2063 void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
2066 struct wpa_group *group;
2068 if (wpa_auth == NULL)
2071 group = wpa_auth->group;
2073 for (i = 0; i < 2; i++) {
2075 group->GM = group->GN;
2077 #ifdef CONFIG_IEEE80211W
2078 tmp = group->GM_igtk;
2079 group->GM_igtk = group->GN_igtk;
2080 group->GN_igtk = tmp;
2081 #endif /* CONFIG_IEEE80211W */
2082 wpa_gtk_update(wpa_auth, group);
2087 static const char * wpa_bool_txt(int bool)
2089 return bool ? "TRUE" : "FALSE";
2093 static int wpa_cipher_bits(int cipher)
2096 case WPA_CIPHER_CCMP:
2098 case WPA_CIPHER_TKIP:
2100 case WPA_CIPHER_WEP104:
2102 case WPA_CIPHER_WEP40:
2110 #define RSN_SUITE "%02x-%02x-%02x-%d"
2111 #define RSN_SUITE_ARG(s) \
2112 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2114 int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
2117 char pmkid_txt[PMKID_LEN * 2 + 1];
2119 if (wpa_auth == NULL)
2122 ret = os_snprintf(buf + len, buflen - len,
2123 "dot11RSNAOptionImplemented=TRUE\n"
2124 #ifdef CONFIG_RSN_PREAUTH
2125 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2126 #else /* CONFIG_RSN_PREAUTH */
2127 "dot11RSNAPreauthenticationImplemented=FALSE\n"
2128 #endif /* CONFIG_RSN_PREAUTH */
2129 "dot11RSNAEnabled=%s\n"
2130 "dot11RSNAPreauthenticationEnabled=%s\n",
2131 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
2132 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
2133 if (ret < 0 || (size_t) ret >= buflen - len)
2137 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2138 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
2141 buf + len, buflen - len,
2142 "dot11RSNAConfigVersion=%u\n"
2143 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
2144 /* FIX: dot11RSNAConfigGroupCipher */
2145 /* FIX: dot11RSNAConfigGroupRekeyMethod */
2146 /* FIX: dot11RSNAConfigGroupRekeyTime */
2147 /* FIX: dot11RSNAConfigGroupRekeyPackets */
2148 "dot11RSNAConfigGroupRekeyStrict=%u\n"
2149 "dot11RSNAConfigGroupUpdateCount=%u\n"
2150 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
2151 "dot11RSNAConfigGroupCipherSize=%u\n"
2152 "dot11RSNAConfigPMKLifetime=%u\n"
2153 "dot11RSNAConfigPMKReauthThreshold=%u\n"
2154 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
2155 "dot11RSNAConfigSATimeout=%u\n"
2156 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2157 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2158 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2159 "dot11RSNAPMKIDUsed=%s\n"
2160 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2161 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2162 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2163 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
2164 "dot11RSNA4WayHandshakeFailures=%u\n"
2165 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
2167 !!wpa_auth->conf.wpa_strict_rekey,
2168 dot11RSNAConfigGroupUpdateCount,
2169 dot11RSNAConfigPairwiseUpdateCount,
2170 wpa_cipher_bits(wpa_auth->conf.wpa_group),
2171 dot11RSNAConfigPMKLifetime,
2172 dot11RSNAConfigPMKReauthThreshold,
2173 dot11RSNAConfigSATimeout,
2174 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
2175 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
2176 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
2178 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
2179 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
2180 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
2181 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
2182 wpa_auth->dot11RSNA4WayHandshakeFailures);
2183 if (ret < 0 || (size_t) ret >= buflen - len)
2187 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
2188 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
2191 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
2192 wpa_auth->group->wpa_group_state);
2193 if (ret < 0 || (size_t) ret >= buflen - len)
2201 int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
2209 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
2211 /* dot11RSNAStatsEntry */
2213 if (sm->wpa == WPA_VERSION_WPA) {
2214 if (sm->pairwise == WPA_CIPHER_CCMP)
2215 pairwise = WPA_CIPHER_SUITE_CCMP;
2216 else if (sm->pairwise == WPA_CIPHER_TKIP)
2217 pairwise = WPA_CIPHER_SUITE_TKIP;
2218 else if (sm->pairwise == WPA_CIPHER_WEP104)
2219 pairwise = WPA_CIPHER_SUITE_WEP104;
2220 else if (sm->pairwise == WPA_CIPHER_WEP40)
2221 pairwise = WPA_CIPHER_SUITE_WEP40;
2222 else if (sm->pairwise == WPA_CIPHER_NONE)
2223 pairwise = WPA_CIPHER_SUITE_NONE;
2224 } else if (sm->wpa == WPA_VERSION_WPA2) {
2225 if (sm->pairwise == WPA_CIPHER_CCMP)
2226 pairwise = RSN_CIPHER_SUITE_CCMP;
2227 else if (sm->pairwise == WPA_CIPHER_TKIP)
2228 pairwise = RSN_CIPHER_SUITE_TKIP;
2229 else if (sm->pairwise == WPA_CIPHER_WEP104)
2230 pairwise = RSN_CIPHER_SUITE_WEP104;
2231 else if (sm->pairwise == WPA_CIPHER_WEP40)
2232 pairwise = RSN_CIPHER_SUITE_WEP40;
2233 else if (sm->pairwise == WPA_CIPHER_NONE)
2234 pairwise = RSN_CIPHER_SUITE_NONE;
2239 buf + len, buflen - len,
2240 /* TODO: dot11RSNAStatsIndex */
2241 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
2242 "dot11RSNAStatsVersion=1\n"
2243 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
2244 /* TODO: dot11RSNAStatsTKIPICVErrors */
2245 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
2246 "dot11RSNAStatsTKIPRemoveMICFailures=%u\n"
2247 /* TODO: dot11RSNAStatsCCMPReplays */
2248 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
2249 /* TODO: dot11RSNAStatsTKIPReplays */,
2251 RSN_SUITE_ARG(pairwise),
2252 sm->dot11RSNAStatsTKIPLocalMICFailures,
2253 sm->dot11RSNAStatsTKIPRemoteMICFailures);
2254 if (ret < 0 || (size_t) ret >= buflen - len)
2259 ret = os_snprintf(buf + len, buflen - len,
2260 "hostapdWPAPTKState=%d\n"
2261 "hostapdWPAPTKGroupState=%d\n",
2263 sm->wpa_ptk_group_state);
2264 if (ret < 0 || (size_t) ret >= buflen - len)
2272 void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
2275 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
2279 int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
2281 return sm && sm->pairwise_set;
2285 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
2289 return sm->wpa_key_mgmt;
2293 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
2301 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
2302 struct rsn_pmksa_cache_entry *entry)
2304 if (sm == NULL || sm->pmksa != entry)
2311 struct rsn_pmksa_cache_entry *
2312 wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
2314 return sm ? sm->pmksa : NULL;
2318 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
2321 sm->dot11RSNAStatsTKIPLocalMICFailures++;
2325 const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
2327 if (wpa_auth == NULL)
2329 *len = wpa_auth->wpa_ie_len;
2330 return wpa_auth->wpa_ie;
2334 int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
2335 int session_timeout, struct eapol_state_machine *eapol)
2337 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2)
2340 if (pmksa_cache_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
2341 sm->wpa_auth->addr, sm->addr, session_timeout,
2342 eapol, sm->wpa_key_mgmt))
2349 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
2350 const u8 *pmk, size_t len, const u8 *sta_addr,
2351 int session_timeout,
2352 struct eapol_state_machine *eapol)
2354 if (wpa_auth == NULL)
2357 if (pmksa_cache_add(wpa_auth->pmksa, pmk, len, wpa_auth->addr,
2358 sta_addr, session_timeout, eapol,
2359 WPA_KEY_MGMT_IEEE8021X))
2366 static struct wpa_group *
2367 wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
2369 struct wpa_group *group;
2371 if (wpa_auth == NULL || wpa_auth->group == NULL)
2374 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
2376 group = wpa_group_init(wpa_auth, vlan_id);
2380 group->next = wpa_auth->group->next;
2381 wpa_auth->group->next = group;
2387 int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
2389 struct wpa_group *group;
2391 if (sm == NULL || sm->wpa_auth == NULL)
2394 group = sm->wpa_auth->group;
2396 if (group->vlan_id == vlan_id)
2398 group = group->next;
2401 if (group == NULL) {
2402 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
2407 if (sm->group == group)
2410 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
2411 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
2417 #endif /* CONFIG_NATIVE_WINDOWS */