2 * hostapd - IEEE 802.11i-2004 / WPA Authenticator
3 * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
17 #ifndef CONFIG_NATIVE_WINDOWS
19 #include "utils/common.h"
20 #include "utils/eloop.h"
21 #include "utils/state_machine.h"
22 #include "common/ieee802_11_defs.h"
23 #include "crypto/aes_wrap.h"
24 #include "crypto/crypto.h"
25 #include "crypto/sha1.h"
26 #include "crypto/sha256.h"
27 #include "eapol_auth/eapol_auth_sm.h"
28 #include "ap_config.h"
29 #include "ieee802_11.h"
31 #include "pmksa_cache_auth.h"
32 #include "wpa_auth_i.h"
33 #include "wpa_auth_ie.h"
35 #define STATE_MACHINE_DATA struct wpa_state_machine
36 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
37 #define STATE_MACHINE_ADDR sm->addr
40 static void wpa_send_eapol_timeout(void *eloop_ctx
, void *timeout_ctx
);
41 static 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
);
47 static int wpa_gtk_update(struct wpa_authenticator
*wpa_auth
,
48 struct wpa_group
*group
);
50 static const u32 dot11RSNAConfigGroupUpdateCount
= 4;
51 static const u32 dot11RSNAConfigPairwiseUpdateCount
= 4;
52 static const u32 eapol_key_timeout_first
= 100; /* ms */
53 static const u32 eapol_key_timeout_subseq
= 1000; /* ms */
55 /* TODO: make these configurable */
56 static const int dot11RSNAConfigPMKLifetime
= 43200;
57 static const int dot11RSNAConfigPMKReauthThreshold
= 70;
58 static const int dot11RSNAConfigSATimeout
= 60;
61 static inline void wpa_auth_mic_failure_report(
62 struct wpa_authenticator
*wpa_auth
, const u8
*addr
)
64 if (wpa_auth
->cb
.mic_failure_report
)
65 wpa_auth
->cb
.mic_failure_report(wpa_auth
->cb
.ctx
, addr
);
69 static inline void wpa_auth_set_eapol(struct wpa_authenticator
*wpa_auth
,
70 const u8
*addr
, wpa_eapol_variable var
,
73 if (wpa_auth
->cb
.set_eapol
)
74 wpa_auth
->cb
.set_eapol(wpa_auth
->cb
.ctx
, addr
, var
, value
);
78 static inline int wpa_auth_get_eapol(struct wpa_authenticator
*wpa_auth
,
79 const u8
*addr
, wpa_eapol_variable var
)
81 if (wpa_auth
->cb
.get_eapol
== NULL
)
83 return wpa_auth
->cb
.get_eapol(wpa_auth
->cb
.ctx
, addr
, var
);
87 static inline const u8
* wpa_auth_get_psk(struct wpa_authenticator
*wpa_auth
,
88 const u8
*addr
, const u8
*prev_psk
)
90 if (wpa_auth
->cb
.get_psk
== NULL
)
92 return wpa_auth
->cb
.get_psk(wpa_auth
->cb
.ctx
, addr
, prev_psk
);
96 static inline int wpa_auth_get_msk(struct wpa_authenticator
*wpa_auth
,
97 const u8
*addr
, u8
*msk
, size_t *len
)
99 if (wpa_auth
->cb
.get_msk
== NULL
)
101 return wpa_auth
->cb
.get_msk(wpa_auth
->cb
.ctx
, addr
, msk
, len
);
105 static inline int wpa_auth_set_key(struct wpa_authenticator
*wpa_auth
,
107 enum wpa_alg alg
, const u8
*addr
, int idx
,
108 u8
*key
, size_t key_len
)
110 if (wpa_auth
->cb
.set_key
== NULL
)
112 return wpa_auth
->cb
.set_key(wpa_auth
->cb
.ctx
, vlan_id
, alg
, addr
, idx
,
117 static inline int wpa_auth_get_seqnum(struct wpa_authenticator
*wpa_auth
,
118 const u8
*addr
, int idx
, u8
*seq
)
120 if (wpa_auth
->cb
.get_seqnum
== NULL
)
122 return wpa_auth
->cb
.get_seqnum(wpa_auth
->cb
.ctx
, addr
, idx
, seq
);
127 wpa_auth_send_eapol(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
128 const u8
*data
, size_t data_len
, int encrypt
)
130 if (wpa_auth
->cb
.send_eapol
== NULL
)
132 return wpa_auth
->cb
.send_eapol(wpa_auth
->cb
.ctx
, addr
, data
, data_len
,
137 int wpa_auth_for_each_sta(struct wpa_authenticator
*wpa_auth
,
138 int (*cb
)(struct wpa_state_machine
*sm
, void *ctx
),
141 if (wpa_auth
->cb
.for_each_sta
== NULL
)
143 return wpa_auth
->cb
.for_each_sta(wpa_auth
->cb
.ctx
, cb
, cb_ctx
);
147 int wpa_auth_for_each_auth(struct wpa_authenticator
*wpa_auth
,
148 int (*cb
)(struct wpa_authenticator
*a
, void *ctx
),
151 if (wpa_auth
->cb
.for_each_auth
== NULL
)
153 return wpa_auth
->cb
.for_each_auth(wpa_auth
->cb
.ctx
, cb
, cb_ctx
);
157 void wpa_auth_logger(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
158 logger_level level
, const char *txt
)
160 if (wpa_auth
->cb
.logger
== NULL
)
162 wpa_auth
->cb
.logger(wpa_auth
->cb
.ctx
, addr
, level
, txt
);
166 void wpa_auth_vlogger(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
167 logger_level level
, const char *fmt
, ...)
173 if (wpa_auth
->cb
.logger
== NULL
)
176 maxlen
= os_strlen(fmt
) + 100;
177 format
= os_malloc(maxlen
);
182 vsnprintf(format
, maxlen
, fmt
, ap
);
185 wpa_auth_logger(wpa_auth
, addr
, level
, format
);
191 static void wpa_sta_disconnect(struct wpa_authenticator
*wpa_auth
,
194 if (wpa_auth
->cb
.disconnect
== NULL
)
196 wpa_auth
->cb
.disconnect(wpa_auth
->cb
.ctx
, addr
,
197 WLAN_REASON_PREV_AUTH_NOT_VALID
);
201 static int wpa_use_aes_cmac(struct wpa_state_machine
*sm
)
204 #ifdef CONFIG_IEEE80211R
205 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
207 #endif /* CONFIG_IEEE80211R */
208 #ifdef CONFIG_IEEE80211W
209 if (wpa_key_mgmt_sha256(sm
->wpa_key_mgmt
))
211 #endif /* CONFIG_IEEE80211W */
216 static void wpa_rekey_gmk(void *eloop_ctx
, void *timeout_ctx
)
218 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
220 if (os_get_random(wpa_auth
->group
->GMK
, WPA_GMK_LEN
)) {
221 wpa_printf(MSG_ERROR
, "Failed to get random data for WPA "
224 wpa_auth_logger(wpa_auth
, NULL
, LOGGER_DEBUG
, "GMK rekeyd");
227 if (wpa_auth
->conf
.wpa_gmk_rekey
) {
228 eloop_register_timeout(wpa_auth
->conf
.wpa_gmk_rekey
, 0,
229 wpa_rekey_gmk
, wpa_auth
, NULL
);
234 static void wpa_rekey_gtk(void *eloop_ctx
, void *timeout_ctx
)
236 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
237 struct wpa_group
*group
;
239 wpa_auth_logger(wpa_auth
, NULL
, LOGGER_DEBUG
, "rekeying GTK");
240 for (group
= wpa_auth
->group
; group
; group
= group
->next
) {
241 group
->GTKReKey
= TRUE
;
243 group
->changed
= FALSE
;
244 wpa_group_sm_step(wpa_auth
, group
);
245 } while (group
->changed
);
248 if (wpa_auth
->conf
.wpa_group_rekey
) {
249 eloop_register_timeout(wpa_auth
->conf
.wpa_group_rekey
,
250 0, wpa_rekey_gtk
, wpa_auth
, NULL
);
255 static void wpa_rekey_ptk(void *eloop_ctx
, void *timeout_ctx
)
257 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
258 struct wpa_state_machine
*sm
= timeout_ctx
;
260 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
, "rekeying PTK");
261 wpa_request_new_ptk(sm
);
266 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine
*sm
, void *ctx
)
268 if (sm
->pmksa
== ctx
)
274 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry
*entry
,
277 struct wpa_authenticator
*wpa_auth
= ctx
;
278 wpa_auth_for_each_sta(wpa_auth
, wpa_auth_pmksa_clear_cb
, entry
);
282 static void wpa_group_set_key_len(struct wpa_group
*group
, int cipher
)
285 case WPA_CIPHER_CCMP
:
288 case WPA_CIPHER_TKIP
:
291 case WPA_CIPHER_WEP104
:
294 case WPA_CIPHER_WEP40
:
301 static struct wpa_group
* wpa_group_init(struct wpa_authenticator
*wpa_auth
,
304 struct wpa_group
*group
;
305 u8 buf
[ETH_ALEN
+ 8 + sizeof(group
)];
308 group
= os_zalloc(sizeof(struct wpa_group
));
312 group
->GTKAuthenticator
= TRUE
;
313 group
->vlan_id
= vlan_id
;
315 wpa_group_set_key_len(group
, wpa_auth
->conf
.wpa_group
);
317 /* Counter = PRF-256(Random number, "Init Counter",
318 * Local MAC Address || Time)
320 os_memcpy(buf
, wpa_auth
->addr
, ETH_ALEN
);
321 wpa_get_ntp_timestamp(buf
+ ETH_ALEN
);
322 os_memcpy(buf
+ ETH_ALEN
+ 8, &group
, sizeof(group
));
323 if (os_get_random(rkey
, sizeof(rkey
)) ||
324 os_get_random(group
->GMK
, WPA_GMK_LEN
)) {
325 wpa_printf(MSG_ERROR
, "Failed to get random data for WPA "
331 sha1_prf(rkey
, sizeof(rkey
), "Init Counter", buf
, sizeof(buf
),
332 group
->Counter
, WPA_NONCE_LEN
);
335 wpa_group_sm_step(wpa_auth
, group
);
336 group
->GInit
= FALSE
;
337 wpa_group_sm_step(wpa_auth
, group
);
344 * wpa_init - Initialize WPA authenticator
345 * @addr: Authenticator address
346 * @conf: Configuration for WPA authenticator
347 * @cb: Callback functions for WPA authenticator
348 * Returns: Pointer to WPA authenticator data or %NULL on failure
350 struct wpa_authenticator
* wpa_init(const u8
*addr
,
351 struct wpa_auth_config
*conf
,
352 struct wpa_auth_callbacks
*cb
)
354 struct wpa_authenticator
*wpa_auth
;
356 wpa_auth
= os_zalloc(sizeof(struct wpa_authenticator
));
357 if (wpa_auth
== NULL
)
359 os_memcpy(wpa_auth
->addr
, addr
, ETH_ALEN
);
360 os_memcpy(&wpa_auth
->conf
, conf
, sizeof(*conf
));
361 os_memcpy(&wpa_auth
->cb
, cb
, sizeof(*cb
));
363 if (wpa_auth_gen_wpa_ie(wpa_auth
)) {
364 wpa_printf(MSG_ERROR
, "Could not generate WPA IE.");
369 wpa_auth
->group
= wpa_group_init(wpa_auth
, 0);
370 if (wpa_auth
->group
== NULL
) {
371 os_free(wpa_auth
->wpa_ie
);
376 wpa_auth
->pmksa
= pmksa_cache_auth_init(wpa_auth_pmksa_free_cb
,
378 if (wpa_auth
->pmksa
== NULL
) {
379 wpa_printf(MSG_ERROR
, "PMKSA cache initialization failed.");
380 os_free(wpa_auth
->wpa_ie
);
385 #ifdef CONFIG_IEEE80211R
386 wpa_auth
->ft_pmk_cache
= wpa_ft_pmk_cache_init();
387 if (wpa_auth
->ft_pmk_cache
== NULL
) {
388 wpa_printf(MSG_ERROR
, "FT PMK cache initialization failed.");
389 os_free(wpa_auth
->wpa_ie
);
390 pmksa_cache_auth_deinit(wpa_auth
->pmksa
);
394 #endif /* CONFIG_IEEE80211R */
396 if (wpa_auth
->conf
.wpa_gmk_rekey
) {
397 eloop_register_timeout(wpa_auth
->conf
.wpa_gmk_rekey
, 0,
398 wpa_rekey_gmk
, wpa_auth
, NULL
);
401 if (wpa_auth
->conf
.wpa_group_rekey
) {
402 eloop_register_timeout(wpa_auth
->conf
.wpa_group_rekey
, 0,
403 wpa_rekey_gtk
, wpa_auth
, NULL
);
411 * wpa_deinit - Deinitialize WPA authenticator
412 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
414 void wpa_deinit(struct wpa_authenticator
*wpa_auth
)
416 struct wpa_group
*group
, *prev
;
418 eloop_cancel_timeout(wpa_rekey_gmk
, wpa_auth
, NULL
);
419 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
421 #ifdef CONFIG_PEERKEY
422 while (wpa_auth
->stsl_negotiations
)
423 wpa_stsl_remove(wpa_auth
, wpa_auth
->stsl_negotiations
);
424 #endif /* CONFIG_PEERKEY */
426 pmksa_cache_auth_deinit(wpa_auth
->pmksa
);
428 #ifdef CONFIG_IEEE80211R
429 wpa_ft_pmk_cache_deinit(wpa_auth
->ft_pmk_cache
);
430 wpa_auth
->ft_pmk_cache
= NULL
;
431 #endif /* CONFIG_IEEE80211R */
433 os_free(wpa_auth
->wpa_ie
);
435 group
= wpa_auth
->group
;
447 * wpa_reconfig - Update WPA authenticator configuration
448 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
449 * @conf: Configuration for WPA authenticator
451 int wpa_reconfig(struct wpa_authenticator
*wpa_auth
,
452 struct wpa_auth_config
*conf
)
454 struct wpa_group
*group
;
455 if (wpa_auth
== NULL
)
458 os_memcpy(&wpa_auth
->conf
, conf
, sizeof(*conf
));
459 if (wpa_auth_gen_wpa_ie(wpa_auth
)) {
460 wpa_printf(MSG_ERROR
, "Could not generate WPA IE.");
465 * Reinitialize GTK to make sure it is suitable for the new
468 group
= wpa_auth
->group
;
469 wpa_group_set_key_len(group
, wpa_auth
->conf
.wpa_group
);
471 wpa_group_sm_step(wpa_auth
, group
);
472 group
->GInit
= FALSE
;
473 wpa_group_sm_step(wpa_auth
, group
);
479 struct wpa_state_machine
*
480 wpa_auth_sta_init(struct wpa_authenticator
*wpa_auth
, const u8
*addr
)
482 struct wpa_state_machine
*sm
;
484 sm
= os_zalloc(sizeof(struct wpa_state_machine
));
487 os_memcpy(sm
->addr
, addr
, ETH_ALEN
);
489 sm
->wpa_auth
= wpa_auth
;
490 sm
->group
= wpa_auth
->group
;
496 void wpa_auth_sta_associated(struct wpa_authenticator
*wpa_auth
,
497 struct wpa_state_machine
*sm
)
499 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
502 #ifdef CONFIG_IEEE80211R
503 if (sm
->ft_completed
) {
504 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
505 "FT authentication already completed - do not "
506 "start 4-way handshake");
509 #endif /* CONFIG_IEEE80211R */
512 os_memset(&sm
->key_replay
, 0, sizeof(sm
->key_replay
));
513 sm
->ReAuthenticationRequest
= TRUE
;
518 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
519 "start authentication");
525 sm
->AuthenticationRequest
= TRUE
;
530 void wpa_auth_sta_no_wpa(struct wpa_state_machine
*sm
)
532 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
533 * reassociates back to the same AP while the previous entry for the
534 * STA has not yet been removed. */
538 sm
->wpa_key_mgmt
= 0;
542 static void wpa_free_sta_sm(struct wpa_state_machine
*sm
)
544 os_free(sm
->last_rx_eapol_key
);
550 void wpa_auth_sta_deinit(struct wpa_state_machine
*sm
)
555 if (sm
->wpa_auth
->conf
.wpa_strict_rekey
&& sm
->has_GTK
) {
556 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
557 "strict rekeying - force GTK rekey since STA "
559 eloop_cancel_timeout(wpa_rekey_gtk
, sm
->wpa_auth
, NULL
);
560 eloop_register_timeout(0, 500000, wpa_rekey_gtk
, sm
->wpa_auth
,
564 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
565 eloop_cancel_timeout(wpa_sm_call_step
, sm
, NULL
);
566 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
567 if (sm
->in_step_loop
) {
568 /* Must not free state machine while wpa_sm_step() is running.
569 * Freeing will be completed in the end of wpa_sm_step(). */
570 wpa_printf(MSG_DEBUG
, "WPA: Registering pending STA state "
571 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
572 sm
->pending_deinit
= 1;
578 static void wpa_request_new_ptk(struct wpa_state_machine
*sm
)
583 sm
->PTKRequest
= TRUE
;
588 static int wpa_replay_counter_valid(struct wpa_state_machine
*sm
,
589 const u8
*replay_counter
)
592 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
593 if (!sm
->key_replay
[i
].valid
)
595 if (os_memcmp(replay_counter
, sm
->key_replay
[i
].counter
,
596 WPA_REPLAY_COUNTER_LEN
) == 0)
603 void wpa_receive(struct wpa_authenticator
*wpa_auth
,
604 struct wpa_state_machine
*sm
,
605 u8
*data
, size_t data_len
)
607 struct ieee802_1x_hdr
*hdr
;
608 struct wpa_eapol_key
*key
;
609 u16 key_info
, key_data_length
;
610 enum { PAIRWISE_2
, PAIRWISE_4
, GROUP_2
, REQUEST
,
611 SMK_M1
, SMK_M3
, SMK_ERROR
} msg
;
613 struct wpa_eapol_ie_parse kde
;
615 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
618 if (data_len
< sizeof(*hdr
) + sizeof(*key
))
621 hdr
= (struct ieee802_1x_hdr
*) data
;
622 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
623 key_info
= WPA_GET_BE16(key
->key_info
);
624 key_data_length
= WPA_GET_BE16(key
->key_data_length
);
625 if (key_data_length
> data_len
- sizeof(*hdr
) - sizeof(*key
)) {
626 wpa_printf(MSG_INFO
, "WPA: Invalid EAPOL-Key frame - "
627 "key_data overflow (%d > %lu)",
629 (unsigned long) (data_len
- sizeof(*hdr
) -
634 if (sm
->wpa
== WPA_VERSION_WPA2
) {
635 if (key
->type
!= EAPOL_KEY_TYPE_RSN
) {
636 wpa_printf(MSG_DEBUG
, "Ignore EAPOL-Key with "
637 "unexpected type %d in RSN mode",
642 if (key
->type
!= EAPOL_KEY_TYPE_WPA
) {
643 wpa_printf(MSG_DEBUG
, "Ignore EAPOL-Key with "
644 "unexpected type %d in WPA mode",
650 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
653 if ((key_info
& (WPA_KEY_INFO_SMK_MESSAGE
| WPA_KEY_INFO_REQUEST
)) ==
654 (WPA_KEY_INFO_SMK_MESSAGE
| WPA_KEY_INFO_REQUEST
)) {
655 if (key_info
& WPA_KEY_INFO_ERROR
) {
657 msgtxt
= "SMK Error";
662 } else if (key_info
& WPA_KEY_INFO_SMK_MESSAGE
) {
665 } else if (key_info
& WPA_KEY_INFO_REQUEST
) {
668 } else if (!(key_info
& WPA_KEY_INFO_KEY_TYPE
)) {
670 msgtxt
= "2/2 Group";
671 } else if (key_data_length
== 0) {
673 msgtxt
= "4/4 Pairwise";
676 msgtxt
= "2/4 Pairwise";
679 /* TODO: key_info type validation for PeerKey */
680 if (msg
== REQUEST
|| msg
== PAIRWISE_2
|| msg
== PAIRWISE_4
||
682 u16 ver
= key_info
& WPA_KEY_INFO_TYPE_MASK
;
683 if (sm
->pairwise
== WPA_CIPHER_CCMP
) {
684 if (wpa_use_aes_cmac(sm
) &&
685 ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
686 wpa_auth_logger(wpa_auth
, sm
->addr
,
688 "advertised support for "
689 "AES-128-CMAC, but did not "
694 if (!wpa_use_aes_cmac(sm
) &&
695 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
696 wpa_auth_logger(wpa_auth
, sm
->addr
,
698 "did not use HMAC-SHA1-AES "
705 if (key_info
& WPA_KEY_INFO_REQUEST
) {
706 if (sm
->req_replay_counter_used
&&
707 os_memcmp(key
->replay_counter
, sm
->req_replay_counter
,
708 WPA_REPLAY_COUNTER_LEN
) <= 0) {
709 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_WARNING
,
710 "received EAPOL-Key request with "
716 if (!(key_info
& WPA_KEY_INFO_REQUEST
) &&
717 !wpa_replay_counter_valid(sm
, key
->replay_counter
)) {
719 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
720 "received EAPOL-Key %s with unexpected "
721 "replay counter", msgtxt
);
722 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
723 if (!sm
->key_replay
[i
].valid
)
725 wpa_hexdump(MSG_DEBUG
, "pending replay counter",
726 sm
->key_replay
[i
].counter
,
727 WPA_REPLAY_COUNTER_LEN
);
729 wpa_hexdump(MSG_DEBUG
, "received replay counter",
730 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
736 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKSTART
&&
737 sm
->wpa_ptk_state
!= WPA_PTK_PTKCALCNEGOTIATING
) {
738 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
739 "received EAPOL-Key msg 2/4 in "
740 "invalid state (%d) - dropped",
744 if (sm
->wpa_ie
== NULL
||
745 sm
->wpa_ie_len
!= key_data_length
||
746 os_memcmp(sm
->wpa_ie
, key
+ 1, key_data_length
) != 0) {
747 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
748 "WPA IE from (Re)AssocReq did not "
749 "match with msg 2/4");
751 wpa_hexdump(MSG_DEBUG
, "WPA IE in AssocReq",
752 sm
->wpa_ie
, sm
->wpa_ie_len
);
754 wpa_hexdump(MSG_DEBUG
, "WPA IE in msg 2/4",
755 (u8
*) (key
+ 1), key_data_length
);
756 /* MLME-DEAUTHENTICATE.request */
757 wpa_sta_disconnect(wpa_auth
, sm
->addr
);
762 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITNEGOTIATING
||
764 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
765 "received EAPOL-Key msg 4/4 in "
766 "invalid state (%d) - dropped",
772 if (sm
->wpa_ptk_group_state
!= WPA_PTK_GROUP_REKEYNEGOTIATING
774 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
775 "received EAPOL-Key msg 2/2 in "
776 "invalid state (%d) - dropped",
777 sm
->wpa_ptk_group_state
);
781 #ifdef CONFIG_PEERKEY
785 if (!wpa_auth
->conf
.peerkey
) {
786 wpa_printf(MSG_DEBUG
, "RSN: SMK M1/M3/Error, but "
787 "PeerKey use disabled - ignoring message");
790 if (!sm
->PTK_valid
) {
791 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
792 "received EAPOL-Key msg SMK in "
793 "invalid state - dropped");
797 #else /* CONFIG_PEERKEY */
801 return; /* STSL disabled - ignore SMK messages */
802 #endif /* CONFIG_PEERKEY */
807 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
808 "received EAPOL-Key frame (%s)", msgtxt
);
810 if (key_info
& WPA_KEY_INFO_ACK
) {
811 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
812 "received invalid EAPOL-Key: Key Ack set");
816 if (!(key_info
& WPA_KEY_INFO_MIC
)) {
817 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
818 "received invalid EAPOL-Key: Key MIC not set");
822 sm
->MICVerified
= FALSE
;
824 if (wpa_verify_key_mic(&sm
->PTK
, data
, data_len
)) {
825 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
826 "received EAPOL-Key with invalid MIC");
829 sm
->MICVerified
= TRUE
;
830 eloop_cancel_timeout(wpa_send_eapol_timeout
, wpa_auth
, sm
);
833 if (key_info
& WPA_KEY_INFO_REQUEST
) {
834 if (sm
->MICVerified
) {
835 sm
->req_replay_counter_used
= 1;
836 os_memcpy(sm
->req_replay_counter
, key
->replay_counter
,
837 WPA_REPLAY_COUNTER_LEN
);
839 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
840 "received EAPOL-Key request with "
846 * TODO: should decrypt key data field if encryption was used;
847 * even though MAC address KDE is not normally encrypted,
848 * supplicant is allowed to encrypt it.
850 if (msg
== SMK_ERROR
) {
851 #ifdef CONFIG_PEERKEY
852 wpa_smk_error(wpa_auth
, sm
, key
);
853 #endif /* CONFIG_PEERKEY */
855 } else if (key_info
& WPA_KEY_INFO_ERROR
) {
856 /* Supplicant reported a Michael MIC error */
857 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
858 "received EAPOL-Key Error Request "
859 "(STA detected Michael MIC failure)");
860 wpa_auth_mic_failure_report(wpa_auth
, sm
->addr
);
861 sm
->dot11RSNAStatsTKIPRemoteMICFailures
++;
862 wpa_auth
->dot11RSNAStatsTKIPRemoteMICFailures
++;
863 /* Error report is not a request for a new key
864 * handshake, but since Authenticator may do it, let's
865 * change the keys now anyway. */
866 wpa_request_new_ptk(sm
);
867 } else if (key_info
& WPA_KEY_INFO_KEY_TYPE
) {
868 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
869 "received EAPOL-Key Request for new "
871 wpa_request_new_ptk(sm
);
872 #ifdef CONFIG_PEERKEY
873 } else if (msg
== SMK_M1
) {
874 wpa_smk_m1(wpa_auth
, sm
, key
);
875 #endif /* CONFIG_PEERKEY */
876 } else if (key_data_length
> 0 &&
877 wpa_parse_kde_ies((const u8
*) (key
+ 1),
878 key_data_length
, &kde
) == 0 &&
881 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
882 "received EAPOL-Key Request for GTK "
884 /* FIX: why was this triggering PTK rekeying for the
885 * STA that requested Group Key rekeying?? */
886 /* wpa_request_new_ptk(sta->wpa_sm); */
887 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
888 wpa_rekey_gtk(wpa_auth
, NULL
);
891 /* Do not allow the same key replay counter to be reused. This
892 * does also invalidate all other pending replay counters if
893 * retransmissions were used, i.e., we will only process one of
894 * the pending replies and ignore rest if more than one is
896 sm
->key_replay
[0].valid
= FALSE
;
899 #ifdef CONFIG_PEERKEY
901 wpa_smk_m3(wpa_auth
, sm
, key
);
904 #endif /* CONFIG_PEERKEY */
906 os_free(sm
->last_rx_eapol_key
);
907 sm
->last_rx_eapol_key
= os_malloc(data_len
);
908 if (sm
->last_rx_eapol_key
== NULL
)
910 os_memcpy(sm
->last_rx_eapol_key
, data
, data_len
);
911 sm
->last_rx_eapol_key_len
= data_len
;
913 sm
->EAPOLKeyReceived
= TRUE
;
914 sm
->EAPOLKeyPairwise
= !!(key_info
& WPA_KEY_INFO_KEY_TYPE
);
915 sm
->EAPOLKeyRequest
= !!(key_info
& WPA_KEY_INFO_REQUEST
);
916 os_memcpy(sm
->SNonce
, key
->key_nonce
, WPA_NONCE_LEN
);
921 static void wpa_gmk_to_gtk(const u8
*gmk
, const u8
*addr
, const u8
*gnonce
,
922 u8
*gtk
, size_t gtk_len
)
924 u8 data
[ETH_ALEN
+ WPA_NONCE_LEN
];
926 /* GTK = PRF-X(GMK, "Group key expansion", AA || GNonce) */
927 os_memcpy(data
, addr
, ETH_ALEN
);
928 os_memcpy(data
+ ETH_ALEN
, gnonce
, WPA_NONCE_LEN
);
930 #ifdef CONFIG_IEEE80211W
931 sha256_prf(gmk
, WPA_GMK_LEN
, "Group key expansion",
932 data
, sizeof(data
), gtk
, gtk_len
);
933 #else /* CONFIG_IEEE80211W */
934 sha1_prf(gmk
, WPA_GMK_LEN
, "Group key expansion",
935 data
, sizeof(data
), gtk
, gtk_len
);
936 #endif /* CONFIG_IEEE80211W */
938 wpa_hexdump_key(MSG_DEBUG
, "GMK", gmk
, WPA_GMK_LEN
);
939 wpa_hexdump_key(MSG_DEBUG
, "GTK", gtk
, gtk_len
);
943 static void wpa_send_eapol_timeout(void *eloop_ctx
, void *timeout_ctx
)
945 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
946 struct wpa_state_machine
*sm
= timeout_ctx
;
948 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
, "EAPOL-Key timeout");
949 sm
->TimeoutEvt
= TRUE
;
954 void __wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
955 struct wpa_state_machine
*sm
, int key_info
,
956 const u8
*key_rsc
, const u8
*nonce
,
957 const u8
*kde
, size_t kde_len
,
958 int keyidx
, int encr
, int force_version
)
960 struct ieee802_1x_hdr
*hdr
;
961 struct wpa_eapol_key
*key
;
964 int key_data_len
, pad_len
= 0;
966 int version
, pairwise
;
969 len
= sizeof(struct ieee802_1x_hdr
) + sizeof(struct wpa_eapol_key
);
972 version
= force_version
;
973 else if (wpa_use_aes_cmac(sm
))
974 version
= WPA_KEY_INFO_TYPE_AES_128_CMAC
;
975 else if (sm
->pairwise
== WPA_CIPHER_CCMP
)
976 version
= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
;
978 version
= WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
;
980 pairwise
= key_info
& WPA_KEY_INFO_KEY_TYPE
;
982 wpa_printf(MSG_DEBUG
, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
983 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
986 (key_info
& WPA_KEY_INFO_SECURE
) ? 1 : 0,
987 (key_info
& WPA_KEY_INFO_MIC
) ? 1 : 0,
988 (key_info
& WPA_KEY_INFO_ACK
) ? 1 : 0,
989 (key_info
& WPA_KEY_INFO_INSTALL
) ? 1 : 0,
990 pairwise
, (unsigned long) kde_len
, keyidx
, encr
);
992 key_data_len
= kde_len
;
994 if ((version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
995 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) && encr
) {
996 pad_len
= key_data_len
% 8;
998 pad_len
= 8 - pad_len
;
999 key_data_len
+= pad_len
+ 8;
1002 len
+= key_data_len
;
1004 hdr
= os_zalloc(len
);
1007 hdr
->version
= wpa_auth
->conf
.eapol_version
;
1008 hdr
->type
= IEEE802_1X_TYPE_EAPOL_KEY
;
1009 hdr
->length
= host_to_be16(len
- sizeof(*hdr
));
1010 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1012 key
->type
= sm
->wpa
== WPA_VERSION_WPA2
?
1013 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
1014 key_info
|= version
;
1015 if (encr
&& sm
->wpa
== WPA_VERSION_WPA2
)
1016 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
1017 if (sm
->wpa
!= WPA_VERSION_WPA2
)
1018 key_info
|= keyidx
<< WPA_KEY_INFO_KEY_INDEX_SHIFT
;
1019 WPA_PUT_BE16(key
->key_info
, key_info
);
1021 alg
= pairwise
? sm
->pairwise
: wpa_auth
->conf
.wpa_group
;
1023 case WPA_CIPHER_CCMP
:
1024 WPA_PUT_BE16(key
->key_length
, 16);
1026 case WPA_CIPHER_TKIP
:
1027 WPA_PUT_BE16(key
->key_length
, 32);
1029 case WPA_CIPHER_WEP40
:
1030 WPA_PUT_BE16(key
->key_length
, 5);
1032 case WPA_CIPHER_WEP104
:
1033 WPA_PUT_BE16(key
->key_length
, 13);
1036 if (key_info
& WPA_KEY_INFO_SMK_MESSAGE
)
1037 WPA_PUT_BE16(key
->key_length
, 0);
1039 /* FIX: STSL: what to use as key_replay_counter? */
1040 for (i
= RSNA_MAX_EAPOL_RETRIES
- 1; i
> 0; i
--) {
1041 sm
->key_replay
[i
].valid
= sm
->key_replay
[i
- 1].valid
;
1042 os_memcpy(sm
->key_replay
[i
].counter
,
1043 sm
->key_replay
[i
- 1].counter
,
1044 WPA_REPLAY_COUNTER_LEN
);
1046 inc_byte_array(sm
->key_replay
[0].counter
, WPA_REPLAY_COUNTER_LEN
);
1047 os_memcpy(key
->replay_counter
, sm
->key_replay
[0].counter
,
1048 WPA_REPLAY_COUNTER_LEN
);
1049 sm
->key_replay
[0].valid
= TRUE
;
1052 os_memcpy(key
->key_nonce
, nonce
, WPA_NONCE_LEN
);
1055 os_memcpy(key
->key_rsc
, key_rsc
, WPA_KEY_RSC_LEN
);
1058 os_memcpy(key
+ 1, kde
, kde_len
);
1059 WPA_PUT_BE16(key
->key_data_length
, kde_len
);
1060 } else if (encr
&& kde
) {
1061 buf
= os_zalloc(key_data_len
);
1067 os_memcpy(pos
, kde
, kde_len
);
1073 wpa_hexdump_key(MSG_DEBUG
, "Plaintext EAPOL-Key Key Data",
1075 if (version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1076 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
1077 if (aes_wrap(sm
->PTK
.kek
, (key_data_len
- 8) / 8, buf
,
1078 (u8
*) (key
+ 1))) {
1083 WPA_PUT_BE16(key
->key_data_length
, key_data_len
);
1086 os_memcpy(key
->key_iv
,
1087 sm
->group
->Counter
+ WPA_NONCE_LEN
- 16, 16);
1088 inc_byte_array(sm
->group
->Counter
, WPA_NONCE_LEN
);
1089 os_memcpy(ek
, key
->key_iv
, 16);
1090 os_memcpy(ek
+ 16, sm
->PTK
.kek
, 16);
1091 os_memcpy(key
+ 1, buf
, key_data_len
);
1092 rc4_skip(ek
, 32, 256, (u8
*) (key
+ 1), key_data_len
);
1093 WPA_PUT_BE16(key
->key_data_length
, key_data_len
);
1098 if (key_info
& WPA_KEY_INFO_MIC
) {
1099 if (!sm
->PTK_valid
) {
1100 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1101 "PTK not valid when sending EAPOL-Key "
1106 wpa_eapol_key_mic(sm
->PTK
.kck
, version
, (u8
*) hdr
, len
,
1110 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_inc_EapolFramesTx
,
1112 wpa_auth_send_eapol(wpa_auth
, sm
->addr
, (u8
*) hdr
, len
,
1118 static void wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
1119 struct wpa_state_machine
*sm
, int key_info
,
1120 const u8
*key_rsc
, const u8
*nonce
,
1121 const u8
*kde
, size_t kde_len
,
1122 int keyidx
, int encr
)
1125 int pairwise
= key_info
& WPA_KEY_INFO_KEY_TYPE
;
1131 __wpa_send_eapol(wpa_auth
, sm
, key_info
, key_rsc
, nonce
, kde
, kde_len
,
1134 ctr
= pairwise
? sm
->TimeoutCtr
: sm
->GTimeoutCtr
;
1136 timeout_ms
= eapol_key_timeout_first
;
1138 timeout_ms
= eapol_key_timeout_subseq
;
1139 eloop_register_timeout(timeout_ms
/ 1000, (timeout_ms
% 1000) * 1000,
1140 wpa_send_eapol_timeout
, wpa_auth
, sm
);
1144 static int wpa_verify_key_mic(struct wpa_ptk
*PTK
, u8
*data
, size_t data_len
)
1146 struct ieee802_1x_hdr
*hdr
;
1147 struct wpa_eapol_key
*key
;
1152 if (data_len
< sizeof(*hdr
) + sizeof(*key
))
1155 hdr
= (struct ieee802_1x_hdr
*) data
;
1156 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1157 key_info
= WPA_GET_BE16(key
->key_info
);
1158 os_memcpy(mic
, key
->key_mic
, 16);
1159 os_memset(key
->key_mic
, 0, 16);
1160 if (wpa_eapol_key_mic(PTK
->kck
, key_info
& WPA_KEY_INFO_TYPE_MASK
,
1161 data
, data_len
, key
->key_mic
) ||
1162 os_memcmp(mic
, key
->key_mic
, 16) != 0)
1164 os_memcpy(key
->key_mic
, mic
, 16);
1169 void wpa_remove_ptk(struct wpa_state_machine
*sm
)
1171 sm
->PTK_valid
= FALSE
;
1172 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1173 wpa_auth_set_key(sm
->wpa_auth
, 0, WPA_ALG_NONE
, sm
->addr
, 0, (u8
*) "",
1175 sm
->pairwise_set
= FALSE
;
1176 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
1180 void wpa_auth_sm_event(struct wpa_state_machine
*sm
, wpa_event event
)
1187 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1188 "event %d notification", event
);
1196 sm
->DeauthenticationRequest
= TRUE
;
1199 case WPA_REAUTH_EAPOL
:
1200 if (sm
->GUpdateStationKeys
) {
1202 * Reauthentication cancels the pending group key
1203 * update for this STA.
1205 sm
->group
->GKeyDoneStations
--;
1206 sm
->GUpdateStationKeys
= FALSE
;
1207 sm
->PtkGroupInit
= TRUE
;
1209 sm
->ReAuthenticationRequest
= TRUE
;
1212 #ifdef CONFIG_IEEE80211R
1213 /* Using FT protocol, not WPA auth state machine */
1214 sm
->ft_completed
= 1;
1216 #else /* CONFIG_IEEE80211R */
1218 #endif /* CONFIG_IEEE80211R */
1221 #ifdef CONFIG_IEEE80211R
1222 sm
->ft_completed
= 0;
1223 #endif /* CONFIG_IEEE80211R */
1225 #ifdef CONFIG_IEEE80211W
1226 if (sm
->mgmt_frame_prot
&& event
== WPA_AUTH
)
1228 #endif /* CONFIG_IEEE80211W */
1231 sm
->PTK_valid
= FALSE
;
1232 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1234 if (event
!= WPA_REAUTH_EAPOL
)
1242 static enum wpa_alg
wpa_alg_enum(int alg
)
1245 case WPA_CIPHER_CCMP
:
1246 return WPA_ALG_CCMP
;
1247 case WPA_CIPHER_TKIP
:
1248 return WPA_ALG_TKIP
;
1249 case WPA_CIPHER_WEP104
:
1250 case WPA_CIPHER_WEP40
:
1253 return WPA_ALG_NONE
;
1258 SM_STATE(WPA_PTK
, INITIALIZE
)
1260 SM_ENTRY_MA(WPA_PTK
, INITIALIZE
, wpa_ptk
);
1262 /* Init flag is not cleared here, so avoid busy
1263 * loop by claiming nothing changed. */
1264 sm
->changed
= FALSE
;
1268 if (sm
->GUpdateStationKeys
)
1269 sm
->group
->GKeyDoneStations
--;
1270 sm
->GUpdateStationKeys
= FALSE
;
1271 if (sm
->wpa
== WPA_VERSION_WPA
)
1272 sm
->PInitAKeys
= FALSE
;
1273 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1274 * Local AA > Remote AA)) */) {
1277 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 0);
1279 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
, 0);
1281 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
1282 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
1283 WPA_EAPOL_authorized
, 0);
1288 SM_STATE(WPA_PTK
, DISCONNECT
)
1290 SM_ENTRY_MA(WPA_PTK
, DISCONNECT
, wpa_ptk
);
1291 sm
->Disconnect
= FALSE
;
1292 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
);
1296 SM_STATE(WPA_PTK
, DISCONNECTED
)
1298 SM_ENTRY_MA(WPA_PTK
, DISCONNECTED
, wpa_ptk
);
1299 sm
->DeauthenticationRequest
= FALSE
;
1303 SM_STATE(WPA_PTK
, AUTHENTICATION
)
1305 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION
, wpa_ptk
);
1306 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1307 sm
->PTK_valid
= FALSE
;
1308 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portControl_Auto
,
1310 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 1);
1311 sm
->AuthenticationRequest
= FALSE
;
1315 SM_STATE(WPA_PTK
, AUTHENTICATION2
)
1317 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION2
, wpa_ptk
);
1318 os_memcpy(sm
->ANonce
, sm
->group
->Counter
, WPA_NONCE_LEN
);
1319 inc_byte_array(sm
->group
->Counter
, WPA_NONCE_LEN
);
1320 sm
->ReAuthenticationRequest
= FALSE
;
1321 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1322 * logical place than INITIALIZE since AUTHENTICATION2 can be
1323 * re-entered on ReAuthenticationRequest without going through
1329 SM_STATE(WPA_PTK
, INITPMK
)
1331 u8 msk
[2 * PMK_LEN
];
1332 size_t len
= 2 * PMK_LEN
;
1334 SM_ENTRY_MA(WPA_PTK
, INITPMK
, wpa_ptk
);
1335 #ifdef CONFIG_IEEE80211R
1337 #endif /* CONFIG_IEEE80211R */
1339 wpa_printf(MSG_DEBUG
, "WPA: PMK from PMKSA cache");
1340 os_memcpy(sm
->PMK
, sm
->pmksa
->pmk
, PMK_LEN
);
1341 } else if (wpa_auth_get_msk(sm
->wpa_auth
, sm
->addr
, msk
, &len
) == 0) {
1342 wpa_printf(MSG_DEBUG
, "WPA: PMK from EAPOL state machine "
1343 "(len=%lu)", (unsigned long) len
);
1344 os_memcpy(sm
->PMK
, msk
, PMK_LEN
);
1345 #ifdef CONFIG_IEEE80211R
1346 if (len
>= 2 * PMK_LEN
) {
1347 os_memcpy(sm
->xxkey
, msk
+ PMK_LEN
, PMK_LEN
);
1348 sm
->xxkey_len
= PMK_LEN
;
1350 #endif /* CONFIG_IEEE80211R */
1352 wpa_printf(MSG_DEBUG
, "WPA: Could not get PMK");
1355 sm
->req_replay_counter_used
= 0;
1356 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1357 * will break reauthentication since EAPOL state machines may not be
1358 * get into AUTHENTICATING state that clears keyRun before WPA state
1359 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1360 * state and takes PMK from the previously used AAA Key. This will
1361 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1362 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1363 * be good workaround for this issue. */
1364 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyRun
, 0);
1368 SM_STATE(WPA_PTK
, INITPSK
)
1371 SM_ENTRY_MA(WPA_PTK
, INITPSK
, wpa_ptk
);
1372 psk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, NULL
);
1374 os_memcpy(sm
->PMK
, psk
, PMK_LEN
);
1375 #ifdef CONFIG_IEEE80211R
1376 os_memcpy(sm
->xxkey
, psk
, PMK_LEN
);
1377 sm
->xxkey_len
= PMK_LEN
;
1378 #endif /* CONFIG_IEEE80211R */
1380 sm
->req_replay_counter_used
= 0;
1384 SM_STATE(WPA_PTK
, PTKSTART
)
1386 u8 buf
[2 + RSN_SELECTOR_LEN
+ PMKID_LEN
], *pmkid
= NULL
;
1387 size_t pmkid_len
= 0;
1389 SM_ENTRY_MA(WPA_PTK
, PTKSTART
, wpa_ptk
);
1390 sm
->PTKRequest
= FALSE
;
1391 sm
->TimeoutEvt
= FALSE
;
1394 if (sm
->TimeoutCtr
> (int) dot11RSNAConfigPairwiseUpdateCount
) {
1395 /* No point in sending the EAPOL-Key - we will disconnect
1396 * immediately following this. */
1400 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1401 "sending 1/4 msg of 4-Way Handshake");
1403 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1404 * one possible PSK for this STA.
1406 if (sm
->wpa
== WPA_VERSION_WPA2
&&
1407 wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
)) {
1409 pmkid_len
= 2 + RSN_SELECTOR_LEN
+ PMKID_LEN
;
1410 pmkid
[0] = WLAN_EID_VENDOR_SPECIFIC
;
1411 pmkid
[1] = RSN_SELECTOR_LEN
+ PMKID_LEN
;
1412 RSN_SELECTOR_PUT(&pmkid
[2], RSN_KEY_DATA_PMKID
);
1414 os_memcpy(&pmkid
[2 + RSN_SELECTOR_LEN
],
1415 sm
->pmksa
->pmkid
, PMKID_LEN
);
1418 * Calculate PMKID since no PMKSA cache entry was
1419 * available with pre-calculated PMKID.
1421 rsn_pmkid(sm
->PMK
, PMK_LEN
, sm
->wpa_auth
->addr
,
1422 sm
->addr
, &pmkid
[2 + RSN_SELECTOR_LEN
],
1423 wpa_key_mgmt_sha256(sm
->wpa_key_mgmt
));
1426 wpa_send_eapol(sm
->wpa_auth
, sm
,
1427 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_KEY_TYPE
, NULL
,
1428 sm
->ANonce
, pmkid
, pmkid_len
, 0, 0);
1432 static int wpa_derive_ptk(struct wpa_state_machine
*sm
, const u8
*pmk
,
1433 struct wpa_ptk
*ptk
)
1435 size_t ptk_len
= sm
->pairwise
== WPA_CIPHER_CCMP
? 48 : 64;
1436 #ifdef CONFIG_IEEE80211R
1437 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
1438 return wpa_auth_derive_ptk_ft(sm
, pmk
, ptk
, ptk_len
);
1439 #endif /* CONFIG_IEEE80211R */
1441 wpa_pmk_to_ptk(pmk
, PMK_LEN
, "Pairwise key expansion",
1442 sm
->wpa_auth
->addr
, sm
->addr
, sm
->ANonce
, sm
->SNonce
,
1443 (u8
*) ptk
, ptk_len
,
1444 wpa_key_mgmt_sha256(sm
->wpa_key_mgmt
));
1450 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING
)
1454 const u8
*pmk
= NULL
;
1456 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING
, wpa_ptk
);
1457 sm
->EAPOLKeyReceived
= FALSE
;
1459 /* WPA with IEEE 802.1X: use the derived PMK from EAP
1460 * WPA-PSK: iterate through possible PSKs and select the one matching
1463 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
1464 pmk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, pmk
);
1470 wpa_derive_ptk(sm
, pmk
, &PTK
);
1472 if (wpa_verify_key_mic(&PTK
, sm
->last_rx_eapol_key
,
1473 sm
->last_rx_eapol_key_len
) == 0) {
1478 if (!wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
))
1483 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1484 "invalid MIC in msg 2/4 of 4-Way Handshake");
1488 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
1490 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
1491 /* PSK may have changed from the previous choice, so update
1492 * state machine data based on whatever PSK was selected here.
1494 os_memcpy(sm
->PMK
, pmk
, PMK_LEN
);
1497 sm
->MICVerified
= TRUE
;
1499 os_memcpy(&sm
->PTK
, &PTK
, sizeof(PTK
));
1500 sm
->PTK_valid
= TRUE
;
1504 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING2
)
1506 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING2
, wpa_ptk
);
1511 #ifdef CONFIG_IEEE80211W
1513 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
)
1515 if (sm
->mgmt_frame_prot
) {
1516 return 2 + RSN_SELECTOR_LEN
+ sizeof(struct wpa_igtk_kde
);
1523 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
)
1525 struct wpa_igtk_kde igtk
;
1526 struct wpa_group
*gsm
= sm
->group
;
1528 if (!sm
->mgmt_frame_prot
)
1531 igtk
.keyid
[0] = gsm
->GN_igtk
;
1533 if (wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, igtk
.pn
) < 0)
1534 os_memset(igtk
.pn
, 0, sizeof(igtk
.pn
));
1535 os_memcpy(igtk
.igtk
, gsm
->IGTK
[gsm
->GN_igtk
- 4], WPA_IGTK_LEN
);
1536 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_IGTK
,
1537 (const u8
*) &igtk
, sizeof(igtk
), NULL
, 0);
1542 #else /* CONFIG_IEEE80211W */
1544 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
)
1550 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
)
1555 #endif /* CONFIG_IEEE80211W */
1558 SM_STATE(WPA_PTK
, PTKINITNEGOTIATING
)
1560 u8 rsc
[WPA_KEY_RSC_LEN
], *_rsc
, *gtk
, *kde
, *pos
;
1561 size_t gtk_len
, kde_len
;
1562 struct wpa_group
*gsm
= sm
->group
;
1564 int wpa_ie_len
, secure
, keyidx
, encr
= 0;
1566 SM_ENTRY_MA(WPA_PTK
, PTKINITNEGOTIATING
, wpa_ptk
);
1567 sm
->TimeoutEvt
= FALSE
;
1570 if (sm
->TimeoutCtr
> (int) dot11RSNAConfigPairwiseUpdateCount
) {
1571 /* No point in sending the EAPOL-Key - we will disconnect
1572 * immediately following this. */
1576 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, GTK[GN])
1578 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
1579 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
1580 wpa_ie
= sm
->wpa_auth
->wpa_ie
;
1581 wpa_ie_len
= sm
->wpa_auth
->wpa_ie_len
;
1582 if (sm
->wpa
== WPA_VERSION_WPA
&&
1583 (sm
->wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
) &&
1584 wpa_ie_len
> wpa_ie
[1] + 2 && wpa_ie
[0] == WLAN_EID_RSN
) {
1585 /* WPA-only STA, remove RSN IE */
1586 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
1587 wpa_ie_len
= wpa_ie
[1] + 2;
1589 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1590 "sending 3/4 msg of 4-Way Handshake");
1591 if (sm
->wpa
== WPA_VERSION_WPA2
) {
1592 /* WPA2 send GTK in the 4-way handshake */
1594 gtk
= gsm
->GTK
[gsm
->GN
- 1];
1595 gtk_len
= gsm
->GTK_len
;
1600 /* WPA does not include GTK in msg 3/4 */
1608 kde_len
= wpa_ie_len
+ ieee80211w_kde_len(sm
);
1610 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 2 + gtk_len
;
1611 kde
= os_malloc(kde_len
);
1616 os_memcpy(pos
, wpa_ie
, wpa_ie_len
);
1620 hdr
[0] = keyidx
& 0x03;
1622 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
1625 pos
= ieee80211w_kde_add(sm
, pos
);
1627 wpa_send_eapol(sm
->wpa_auth
, sm
,
1628 (secure
? WPA_KEY_INFO_SECURE
: 0) | WPA_KEY_INFO_MIC
|
1629 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_INSTALL
|
1630 WPA_KEY_INFO_KEY_TYPE
,
1631 _rsc
, sm
->ANonce
, kde
, pos
- kde
, keyidx
, encr
);
1636 SM_STATE(WPA_PTK
, PTKINITDONE
)
1638 SM_ENTRY_MA(WPA_PTK
, PTKINITDONE
, wpa_ptk
);
1639 sm
->EAPOLKeyReceived
= FALSE
;
1643 if (sm
->pairwise
== WPA_CIPHER_TKIP
) {
1650 if (wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
, 0,
1651 sm
->PTK
.tk1
, klen
)) {
1652 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
);
1655 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
1656 sm
->pairwise_set
= TRUE
;
1658 if (sm
->wpa_auth
->conf
.wpa_ptk_rekey
) {
1659 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
1660 eloop_register_timeout(sm
->wpa_auth
->conf
.
1661 wpa_ptk_rekey
, 0, wpa_rekey_ptk
,
1665 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
1666 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
1667 WPA_EAPOL_authorized
, 1);
1671 if (0 /* IBSS == TRUE */) {
1673 if (sm
->keycount
== 2) {
1674 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
1675 WPA_EAPOL_portValid
, 1);
1678 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
,
1681 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyAvailable
, 0);
1682 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyDone
, 1);
1683 if (sm
->wpa
== WPA_VERSION_WPA
)
1684 sm
->PInitAKeys
= TRUE
;
1687 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
1688 "pairwise key handshake completed (%s)",
1689 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
1691 #ifdef CONFIG_IEEE80211R
1692 wpa_ft_push_pmk_r1(sm
->wpa_auth
, sm
->addr
);
1693 #endif /* CONFIG_IEEE80211R */
1699 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
1702 SM_ENTER(WPA_PTK
, INITIALIZE
);
1703 else if (sm
->Disconnect
1704 /* || FIX: dot11RSNAConfigSALifetime timeout */)
1705 SM_ENTER(WPA_PTK
, DISCONNECT
);
1706 else if (sm
->DeauthenticationRequest
)
1707 SM_ENTER(WPA_PTK
, DISCONNECTED
);
1708 else if (sm
->AuthenticationRequest
)
1709 SM_ENTER(WPA_PTK
, AUTHENTICATION
);
1710 else if (sm
->ReAuthenticationRequest
)
1711 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
1712 else if (sm
->PTKRequest
)
1713 SM_ENTER(WPA_PTK
, PTKSTART
);
1714 else switch (sm
->wpa_ptk_state
) {
1715 case WPA_PTK_INITIALIZE
:
1717 case WPA_PTK_DISCONNECT
:
1718 SM_ENTER(WPA_PTK
, DISCONNECTED
);
1720 case WPA_PTK_DISCONNECTED
:
1721 SM_ENTER(WPA_PTK
, INITIALIZE
);
1723 case WPA_PTK_AUTHENTICATION
:
1724 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
1726 case WPA_PTK_AUTHENTICATION2
:
1727 if (wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) &&
1728 wpa_auth_get_eapol(sm
->wpa_auth
, sm
->addr
,
1729 WPA_EAPOL_keyRun
) > 0)
1730 SM_ENTER(WPA_PTK
, INITPMK
);
1731 else if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)
1732 /* FIX: && 802.1X::keyRun */)
1733 SM_ENTER(WPA_PTK
, INITPSK
);
1735 case WPA_PTK_INITPMK
:
1736 if (wpa_auth_get_eapol(sm
->wpa_auth
, sm
->addr
,
1737 WPA_EAPOL_keyAvailable
) > 0)
1738 SM_ENTER(WPA_PTK
, PTKSTART
);
1740 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
1741 SM_ENTER(WPA_PTK
, DISCONNECT
);
1744 case WPA_PTK_INITPSK
:
1745 if (wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, NULL
))
1746 SM_ENTER(WPA_PTK
, PTKSTART
);
1748 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
1749 "no PSK configured for the STA");
1750 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
1751 SM_ENTER(WPA_PTK
, DISCONNECT
);
1754 case WPA_PTK_PTKSTART
:
1755 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
1756 sm
->EAPOLKeyPairwise
)
1757 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
1758 else if (sm
->TimeoutCtr
>
1759 (int) dot11RSNAConfigPairwiseUpdateCount
) {
1760 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
1761 SM_ENTER(WPA_PTK
, DISCONNECT
);
1762 } else if (sm
->TimeoutEvt
)
1763 SM_ENTER(WPA_PTK
, PTKSTART
);
1765 case WPA_PTK_PTKCALCNEGOTIATING
:
1766 if (sm
->MICVerified
)
1767 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING2
);
1768 else if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
1769 sm
->EAPOLKeyPairwise
)
1770 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
1771 else if (sm
->TimeoutEvt
)
1772 SM_ENTER(WPA_PTK
, PTKSTART
);
1774 case WPA_PTK_PTKCALCNEGOTIATING2
:
1775 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
1777 case WPA_PTK_PTKINITNEGOTIATING
:
1778 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
1779 sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
1780 SM_ENTER(WPA_PTK
, PTKINITDONE
);
1781 else if (sm
->TimeoutCtr
>
1782 (int) dot11RSNAConfigPairwiseUpdateCount
) {
1783 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
1784 SM_ENTER(WPA_PTK
, DISCONNECT
);
1785 } else if (sm
->TimeoutEvt
)
1786 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
1788 case WPA_PTK_PTKINITDONE
:
1794 SM_STATE(WPA_PTK_GROUP
, IDLE
)
1796 SM_ENTRY_MA(WPA_PTK_GROUP
, IDLE
, wpa_ptk_group
);
1798 /* Init flag is not cleared here, so avoid busy
1799 * loop by claiming nothing changed. */
1800 sm
->changed
= FALSE
;
1802 sm
->GTimeoutCtr
= 0;
1806 SM_STATE(WPA_PTK_GROUP
, REKEYNEGOTIATING
)
1808 u8 rsc
[WPA_KEY_RSC_LEN
];
1809 struct wpa_group
*gsm
= sm
->group
;
1810 u8
*kde
, *pos
, hdr
[2];
1813 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYNEGOTIATING
, wpa_ptk_group
);
1816 if (sm
->GTimeoutCtr
> (int) dot11RSNAConfigGroupUpdateCount
) {
1817 /* No point in sending the EAPOL-Key - we will disconnect
1818 * immediately following this. */
1822 if (sm
->wpa
== WPA_VERSION_WPA
)
1823 sm
->PInitAKeys
= FALSE
;
1824 sm
->TimeoutEvt
= FALSE
;
1825 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
1826 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
1827 if (gsm
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
)
1828 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
1829 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1830 "sending 1/2 msg of Group Key Handshake");
1832 if (sm
->wpa
== WPA_VERSION_WPA2
) {
1833 kde_len
= 2 + RSN_SELECTOR_LEN
+ 2 + gsm
->GTK_len
+
1834 ieee80211w_kde_len(sm
);
1835 kde
= os_malloc(kde_len
);
1840 hdr
[0] = gsm
->GN
& 0x03;
1842 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
1843 gsm
->GTK
[gsm
->GN
- 1], gsm
->GTK_len
);
1844 pos
= ieee80211w_kde_add(sm
, pos
);
1846 kde
= gsm
->GTK
[gsm
->GN
- 1];
1847 pos
= kde
+ gsm
->GTK_len
;
1850 wpa_send_eapol(sm
->wpa_auth
, sm
,
1851 WPA_KEY_INFO_SECURE
| WPA_KEY_INFO_MIC
|
1853 (!sm
->Pair
? WPA_KEY_INFO_INSTALL
: 0),
1854 rsc
, gsm
->GNonce
, kde
, pos
- kde
, gsm
->GN
, 1);
1855 if (sm
->wpa
== WPA_VERSION_WPA2
)
1860 SM_STATE(WPA_PTK_GROUP
, REKEYESTABLISHED
)
1862 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYESTABLISHED
, wpa_ptk_group
);
1863 sm
->EAPOLKeyReceived
= FALSE
;
1864 if (sm
->GUpdateStationKeys
)
1865 sm
->group
->GKeyDoneStations
--;
1866 sm
->GUpdateStationKeys
= FALSE
;
1867 sm
->GTimeoutCtr
= 0;
1868 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
1869 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
1870 "group key handshake completed (%s)",
1871 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
1876 SM_STATE(WPA_PTK_GROUP
, KEYERROR
)
1878 SM_ENTRY_MA(WPA_PTK_GROUP
, KEYERROR
, wpa_ptk_group
);
1879 if (sm
->GUpdateStationKeys
)
1880 sm
->group
->GKeyDoneStations
--;
1881 sm
->GUpdateStationKeys
= FALSE
;
1882 sm
->Disconnect
= TRUE
;
1886 SM_STEP(WPA_PTK_GROUP
)
1888 if (sm
->Init
|| sm
->PtkGroupInit
) {
1889 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
1890 sm
->PtkGroupInit
= FALSE
;
1891 } else switch (sm
->wpa_ptk_group_state
) {
1892 case WPA_PTK_GROUP_IDLE
:
1893 if (sm
->GUpdateStationKeys
||
1894 (sm
->wpa
== WPA_VERSION_WPA
&& sm
->PInitAKeys
))
1895 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
1897 case WPA_PTK_GROUP_REKEYNEGOTIATING
:
1898 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
1899 !sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
1900 SM_ENTER(WPA_PTK_GROUP
, REKEYESTABLISHED
);
1901 else if (sm
->GTimeoutCtr
>
1902 (int) dot11RSNAConfigGroupUpdateCount
)
1903 SM_ENTER(WPA_PTK_GROUP
, KEYERROR
);
1904 else if (sm
->TimeoutEvt
)
1905 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
1907 case WPA_PTK_GROUP_KEYERROR
:
1908 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
1910 case WPA_PTK_GROUP_REKEYESTABLISHED
:
1911 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
1917 static int wpa_gtk_update(struct wpa_authenticator
*wpa_auth
,
1918 struct wpa_group
*group
)
1922 /* FIX: is this the correct way of getting GNonce? */
1923 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
1924 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
1925 wpa_gmk_to_gtk(group
->GMK
, wpa_auth
->addr
, group
->GNonce
,
1926 group
->GTK
[group
->GN
- 1], group
->GTK_len
);
1928 #ifdef CONFIG_IEEE80211W
1929 if (wpa_auth
->conf
.ieee80211w
!= NO_MGMT_FRAME_PROTECTION
) {
1930 if (os_get_random(group
->IGTK
[group
->GN_igtk
- 4],
1931 WPA_IGTK_LEN
) < 0) {
1932 wpa_printf(MSG_INFO
, "RSN: Failed to get new random "
1936 wpa_hexdump_key(MSG_DEBUG
, "IGTK",
1937 group
->IGTK
[group
->GN_igtk
- 4], WPA_IGTK_LEN
);
1939 #endif /* CONFIG_IEEE80211W */
1945 static void wpa_group_gtk_init(struct wpa_authenticator
*wpa_auth
,
1946 struct wpa_group
*group
)
1948 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
1949 "GTK_INIT (VLAN-ID %d)", group
->vlan_id
);
1950 group
->changed
= FALSE
; /* GInit is not cleared here; avoid loop */
1951 group
->wpa_group_state
= WPA_GROUP_GTK_INIT
;
1954 os_memset(group
->GTK
, 0, sizeof(group
->GTK
));
1957 #ifdef CONFIG_IEEE80211W
1960 #endif /* CONFIG_IEEE80211W */
1961 /* GTK[GN] = CalcGTK() */
1962 wpa_gtk_update(wpa_auth
, group
);
1966 static int wpa_group_update_sta(struct wpa_state_machine
*sm
, void *ctx
)
1968 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITDONE
) {
1969 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1970 "Not in PTKINITDONE; skip Group Key update");
1973 if (sm
->GUpdateStationKeys
) {
1975 * This should not really happen, but just in case, make sure
1976 * we do not count the same STA twice in GKeyDoneStations.
1978 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1979 "GUpdateStationKeys already set - do not "
1980 "increment GKeyDoneStations");
1982 sm
->group
->GKeyDoneStations
++;
1983 sm
->GUpdateStationKeys
= TRUE
;
1990 static void wpa_group_setkeys(struct wpa_authenticator
*wpa_auth
,
1991 struct wpa_group
*group
)
1995 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
1996 "SETKEYS (VLAN-ID %d)", group
->vlan_id
);
1997 group
->changed
= TRUE
;
1998 group
->wpa_group_state
= WPA_GROUP_SETKEYS
;
1999 group
->GTKReKey
= FALSE
;
2001 group
->GM
= group
->GN
;
2003 #ifdef CONFIG_IEEE80211W
2004 tmp
= group
->GM_igtk
;
2005 group
->GM_igtk
= group
->GN_igtk
;
2006 group
->GN_igtk
= tmp
;
2007 #endif /* CONFIG_IEEE80211W */
2008 /* "GKeyDoneStations = GNoStations" is done in more robust way by
2009 * counting the STAs that are marked with GUpdateStationKeys instead of
2010 * including all STAs that could be in not-yet-completed state. */
2011 wpa_gtk_update(wpa_auth
, group
);
2013 wpa_auth_for_each_sta(wpa_auth
, wpa_group_update_sta
, NULL
);
2014 wpa_printf(MSG_DEBUG
, "wpa_group_setkeys: GKeyDoneStations=%d",
2015 group
->GKeyDoneStations
);
2019 static void wpa_group_setkeysdone(struct wpa_authenticator
*wpa_auth
,
2020 struct wpa_group
*group
)
2022 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
2023 "SETKEYSDONE (VLAN-ID %d)", group
->vlan_id
);
2024 group
->changed
= TRUE
;
2025 group
->wpa_group_state
= WPA_GROUP_SETKEYSDONE
;
2026 wpa_auth_set_key(wpa_auth
, group
->vlan_id
,
2027 wpa_alg_enum(wpa_auth
->conf
.wpa_group
),
2028 NULL
, group
->GN
, group
->GTK
[group
->GN
- 1],
2031 #ifdef CONFIG_IEEE80211W
2032 if (wpa_auth
->conf
.ieee80211w
!= NO_MGMT_FRAME_PROTECTION
) {
2033 wpa_auth_set_key(wpa_auth
, group
->vlan_id
, WPA_ALG_IGTK
,
2034 NULL
, group
->GN_igtk
,
2035 group
->IGTK
[group
->GN_igtk
- 4],
2038 #endif /* CONFIG_IEEE80211W */
2042 static void wpa_group_sm_step(struct wpa_authenticator
*wpa_auth
,
2043 struct wpa_group
*group
)
2046 wpa_group_gtk_init(wpa_auth
, group
);
2047 } else if (group
->wpa_group_state
== WPA_GROUP_GTK_INIT
&&
2048 group
->GTKAuthenticator
) {
2049 wpa_group_setkeysdone(wpa_auth
, group
);
2050 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
&&
2052 wpa_group_setkeys(wpa_auth
, group
);
2053 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYS
) {
2054 if (group
->GKeyDoneStations
== 0)
2055 wpa_group_setkeysdone(wpa_auth
, group
);
2056 else if (group
->GTKReKey
)
2057 wpa_group_setkeys(wpa_auth
, group
);
2062 static void wpa_sm_step(struct wpa_state_machine
*sm
)
2067 if (sm
->in_step_loop
) {
2068 /* This should not happen, but if it does, make sure we do not
2069 * end up freeing the state machine too early by exiting the
2070 * recursive call. */
2071 wpa_printf(MSG_ERROR
, "WPA: wpa_sm_step() called recursively");
2075 sm
->in_step_loop
= 1;
2077 if (sm
->pending_deinit
)
2080 sm
->changed
= FALSE
;
2081 sm
->wpa_auth
->group
->changed
= FALSE
;
2083 SM_STEP_RUN(WPA_PTK
);
2084 if (sm
->pending_deinit
)
2086 SM_STEP_RUN(WPA_PTK_GROUP
);
2087 if (sm
->pending_deinit
)
2089 wpa_group_sm_step(sm
->wpa_auth
, sm
->group
);
2090 } while (sm
->changed
|| sm
->wpa_auth
->group
->changed
);
2091 sm
->in_step_loop
= 0;
2093 if (sm
->pending_deinit
) {
2094 wpa_printf(MSG_DEBUG
, "WPA: Completing pending STA state "
2095 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
2096 wpa_free_sta_sm(sm
);
2101 static void wpa_sm_call_step(void *eloop_ctx
, void *timeout_ctx
)
2103 struct wpa_state_machine
*sm
= eloop_ctx
;
2108 void wpa_auth_sm_notify(struct wpa_state_machine
*sm
)
2112 eloop_register_timeout(0, 0, wpa_sm_call_step
, sm
, NULL
);
2116 void wpa_gtk_rekey(struct wpa_authenticator
*wpa_auth
)
2119 struct wpa_group
*group
;
2121 if (wpa_auth
== NULL
)
2124 group
= wpa_auth
->group
;
2126 for (i
= 0; i
< 2; i
++) {
2128 group
->GM
= group
->GN
;
2130 #ifdef CONFIG_IEEE80211W
2131 tmp
= group
->GM_igtk
;
2132 group
->GM_igtk
= group
->GN_igtk
;
2133 group
->GN_igtk
= tmp
;
2134 #endif /* CONFIG_IEEE80211W */
2135 wpa_gtk_update(wpa_auth
, group
);
2140 static const char * wpa_bool_txt(int bool)
2142 return bool ? "TRUE" : "FALSE";
2146 static int wpa_cipher_bits(int cipher
)
2149 case WPA_CIPHER_CCMP
:
2151 case WPA_CIPHER_TKIP
:
2153 case WPA_CIPHER_WEP104
:
2155 case WPA_CIPHER_WEP40
:
2163 #define RSN_SUITE "%02x-%02x-%02x-%d"
2164 #define RSN_SUITE_ARG(s) \
2165 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2167 int wpa_get_mib(struct wpa_authenticator
*wpa_auth
, char *buf
, size_t buflen
)
2170 char pmkid_txt
[PMKID_LEN
* 2 + 1];
2172 if (wpa_auth
== NULL
)
2175 ret
= os_snprintf(buf
+ len
, buflen
- len
,
2176 "dot11RSNAOptionImplemented=TRUE\n"
2177 #ifdef CONFIG_RSN_PREAUTH
2178 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2179 #else /* CONFIG_RSN_PREAUTH */
2180 "dot11RSNAPreauthenticationImplemented=FALSE\n"
2181 #endif /* CONFIG_RSN_PREAUTH */
2182 "dot11RSNAEnabled=%s\n"
2183 "dot11RSNAPreauthenticationEnabled=%s\n",
2184 wpa_bool_txt(wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
),
2185 wpa_bool_txt(wpa_auth
->conf
.rsn_preauth
));
2186 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
2190 wpa_snprintf_hex(pmkid_txt
, sizeof(pmkid_txt
),
2191 wpa_auth
->dot11RSNAPMKIDUsed
, PMKID_LEN
);
2194 buf
+ len
, buflen
- len
,
2195 "dot11RSNAConfigVersion=%u\n"
2196 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
2197 /* FIX: dot11RSNAConfigGroupCipher */
2198 /* FIX: dot11RSNAConfigGroupRekeyMethod */
2199 /* FIX: dot11RSNAConfigGroupRekeyTime */
2200 /* FIX: dot11RSNAConfigGroupRekeyPackets */
2201 "dot11RSNAConfigGroupRekeyStrict=%u\n"
2202 "dot11RSNAConfigGroupUpdateCount=%u\n"
2203 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
2204 "dot11RSNAConfigGroupCipherSize=%u\n"
2205 "dot11RSNAConfigPMKLifetime=%u\n"
2206 "dot11RSNAConfigPMKReauthThreshold=%u\n"
2207 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
2208 "dot11RSNAConfigSATimeout=%u\n"
2209 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE
"\n"
2210 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE
"\n"
2211 "dot11RSNAGroupCipherSelected=" RSN_SUITE
"\n"
2212 "dot11RSNAPMKIDUsed=%s\n"
2213 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE
"\n"
2214 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE
"\n"
2215 "dot11RSNAGroupCipherRequested=" RSN_SUITE
"\n"
2216 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
2217 "dot11RSNA4WayHandshakeFailures=%u\n"
2218 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
2220 !!wpa_auth
->conf
.wpa_strict_rekey
,
2221 dot11RSNAConfigGroupUpdateCount
,
2222 dot11RSNAConfigPairwiseUpdateCount
,
2223 wpa_cipher_bits(wpa_auth
->conf
.wpa_group
),
2224 dot11RSNAConfigPMKLifetime
,
2225 dot11RSNAConfigPMKReauthThreshold
,
2226 dot11RSNAConfigSATimeout
,
2227 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteSelected
),
2228 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherSelected
),
2229 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherSelected
),
2231 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteRequested
),
2232 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherRequested
),
2233 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherRequested
),
2234 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
,
2235 wpa_auth
->dot11RSNA4WayHandshakeFailures
);
2236 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
2240 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
2241 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
2244 ret
= os_snprintf(buf
+ len
, buflen
- len
, "hostapdWPAGroupState=%d\n",
2245 wpa_auth
->group
->wpa_group_state
);
2246 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
2254 int wpa_get_mib_sta(struct wpa_state_machine
*sm
, char *buf
, size_t buflen
)
2262 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
2264 /* dot11RSNAStatsEntry */
2266 if (sm
->wpa
== WPA_VERSION_WPA
) {
2267 if (sm
->pairwise
== WPA_CIPHER_CCMP
)
2268 pairwise
= WPA_CIPHER_SUITE_CCMP
;
2269 else if (sm
->pairwise
== WPA_CIPHER_TKIP
)
2270 pairwise
= WPA_CIPHER_SUITE_TKIP
;
2271 else if (sm
->pairwise
== WPA_CIPHER_WEP104
)
2272 pairwise
= WPA_CIPHER_SUITE_WEP104
;
2273 else if (sm
->pairwise
== WPA_CIPHER_WEP40
)
2274 pairwise
= WPA_CIPHER_SUITE_WEP40
;
2275 else if (sm
->pairwise
== WPA_CIPHER_NONE
)
2276 pairwise
= WPA_CIPHER_SUITE_NONE
;
2277 } else if (sm
->wpa
== WPA_VERSION_WPA2
) {
2278 if (sm
->pairwise
== WPA_CIPHER_CCMP
)
2279 pairwise
= RSN_CIPHER_SUITE_CCMP
;
2280 else if (sm
->pairwise
== WPA_CIPHER_TKIP
)
2281 pairwise
= RSN_CIPHER_SUITE_TKIP
;
2282 else if (sm
->pairwise
== WPA_CIPHER_WEP104
)
2283 pairwise
= RSN_CIPHER_SUITE_WEP104
;
2284 else if (sm
->pairwise
== WPA_CIPHER_WEP40
)
2285 pairwise
= RSN_CIPHER_SUITE_WEP40
;
2286 else if (sm
->pairwise
== WPA_CIPHER_NONE
)
2287 pairwise
= RSN_CIPHER_SUITE_NONE
;
2292 buf
+ len
, buflen
- len
,
2293 /* TODO: dot11RSNAStatsIndex */
2294 "dot11RSNAStatsSTAAddress=" MACSTR
"\n"
2295 "dot11RSNAStatsVersion=1\n"
2296 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE
"\n"
2297 /* TODO: dot11RSNAStatsTKIPICVErrors */
2298 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
2299 "dot11RSNAStatsTKIPRemoveMICFailures=%u\n"
2300 /* TODO: dot11RSNAStatsCCMPReplays */
2301 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
2302 /* TODO: dot11RSNAStatsTKIPReplays */,
2304 RSN_SUITE_ARG(pairwise
),
2305 sm
->dot11RSNAStatsTKIPLocalMICFailures
,
2306 sm
->dot11RSNAStatsTKIPRemoteMICFailures
);
2307 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
2312 ret
= os_snprintf(buf
+ len
, buflen
- len
,
2313 "hostapdWPAPTKState=%d\n"
2314 "hostapdWPAPTKGroupState=%d\n",
2316 sm
->wpa_ptk_group_state
);
2317 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
2325 void wpa_auth_countermeasures_start(struct wpa_authenticator
*wpa_auth
)
2328 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
++;
2332 int wpa_auth_pairwise_set(struct wpa_state_machine
*sm
)
2334 return sm
&& sm
->pairwise_set
;
2338 int wpa_auth_get_pairwise(struct wpa_state_machine
*sm
)
2340 return sm
->pairwise
;
2344 int wpa_auth_sta_key_mgmt(struct wpa_state_machine
*sm
)
2348 return sm
->wpa_key_mgmt
;
2352 int wpa_auth_sta_wpa_version(struct wpa_state_machine
*sm
)
2360 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine
*sm
,
2361 struct rsn_pmksa_cache_entry
*entry
)
2363 if (sm
== NULL
|| sm
->pmksa
!= entry
)
2370 struct rsn_pmksa_cache_entry
*
2371 wpa_auth_sta_get_pmksa(struct wpa_state_machine
*sm
)
2373 return sm
? sm
->pmksa
: NULL
;
2377 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine
*sm
)
2380 sm
->dot11RSNAStatsTKIPLocalMICFailures
++;
2384 const u8
* wpa_auth_get_wpa_ie(struct wpa_authenticator
*wpa_auth
, size_t *len
)
2386 if (wpa_auth
== NULL
)
2388 *len
= wpa_auth
->wpa_ie_len
;
2389 return wpa_auth
->wpa_ie
;
2393 int wpa_auth_pmksa_add(struct wpa_state_machine
*sm
, const u8
*pmk
,
2394 int session_timeout
, struct eapol_state_machine
*eapol
)
2396 if (sm
== NULL
|| sm
->wpa
!= WPA_VERSION_WPA2
)
2399 if (pmksa_cache_auth_add(sm
->wpa_auth
->pmksa
, pmk
, PMK_LEN
,
2400 sm
->wpa_auth
->addr
, sm
->addr
, session_timeout
,
2401 eapol
, sm
->wpa_key_mgmt
))
2408 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator
*wpa_auth
,
2409 const u8
*pmk
, size_t len
, const u8
*sta_addr
,
2410 int session_timeout
,
2411 struct eapol_state_machine
*eapol
)
2413 if (wpa_auth
== NULL
)
2416 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, len
, wpa_auth
->addr
,
2417 sta_addr
, session_timeout
, eapol
,
2418 WPA_KEY_MGMT_IEEE8021X
))
2425 static struct wpa_group
*
2426 wpa_auth_add_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
2428 struct wpa_group
*group
;
2430 if (wpa_auth
== NULL
|| wpa_auth
->group
== NULL
)
2433 wpa_printf(MSG_DEBUG
, "WPA: Add group state machine for VLAN-ID %d",
2435 group
= wpa_group_init(wpa_auth
, vlan_id
);
2439 group
->next
= wpa_auth
->group
->next
;
2440 wpa_auth
->group
->next
= group
;
2446 int wpa_auth_sta_set_vlan(struct wpa_state_machine
*sm
, int vlan_id
)
2448 struct wpa_group
*group
;
2450 if (sm
== NULL
|| sm
->wpa_auth
== NULL
)
2453 group
= sm
->wpa_auth
->group
;
2455 if (group
->vlan_id
== vlan_id
)
2457 group
= group
->next
;
2460 if (group
== NULL
) {
2461 group
= wpa_auth_add_group(sm
->wpa_auth
, vlan_id
);
2466 if (sm
->group
== group
)
2469 wpa_printf(MSG_DEBUG
, "WPA: Moving STA " MACSTR
" to use group state "
2470 "machine for VLAN ID %d", MAC2STR(sm
->addr
), vlan_id
);
2476 #endif /* CONFIG_NATIVE_WINDOWS */