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
);
47 /* Default timeouts are 100 ms, but this seems to be a bit too fast for most
48 * WPA Supplicants, so use a bit longer timeout. */
49 static const u32 dot11RSNAConfigGroupUpdateTimeOut
= 1000; /* ms */
50 static const u32 dot11RSNAConfigGroupUpdateCount
= 3;
51 static const u32 dot11RSNAConfigPairwiseUpdateTimeOut
= 1000; /* ms */
52 static const u32 dot11RSNAConfigPairwiseUpdateCount
= 3;
54 /* TODO: make these configurable */
55 static const int dot11RSNAConfigPMKLifetime
= 43200;
56 static const int dot11RSNAConfigPMKReauthThreshold
= 70;
57 static const int dot11RSNAConfigSATimeout
= 60;
60 static inline void wpa_auth_mic_failure_report(
61 struct wpa_authenticator
*wpa_auth
, const u8
*addr
)
63 if (wpa_auth
->cb
.mic_failure_report
)
64 wpa_auth
->cb
.mic_failure_report(wpa_auth
->cb
.ctx
, addr
);
68 static inline void wpa_auth_set_eapol(struct wpa_authenticator
*wpa_auth
,
69 const u8
*addr
, wpa_eapol_variable var
,
72 if (wpa_auth
->cb
.set_eapol
)
73 wpa_auth
->cb
.set_eapol(wpa_auth
->cb
.ctx
, addr
, var
, value
);
77 static inline int wpa_auth_get_eapol(struct wpa_authenticator
*wpa_auth
,
78 const u8
*addr
, wpa_eapol_variable var
)
80 if (wpa_auth
->cb
.get_eapol
== NULL
)
82 return wpa_auth
->cb
.get_eapol(wpa_auth
->cb
.ctx
, addr
, var
);
86 static inline const u8
* wpa_auth_get_psk(struct wpa_authenticator
*wpa_auth
,
87 const u8
*addr
, const u8
*prev_psk
)
89 if (wpa_auth
->cb
.get_psk
== NULL
)
91 return wpa_auth
->cb
.get_psk(wpa_auth
->cb
.ctx
, addr
, prev_psk
);
95 static inline int wpa_auth_get_msk(struct wpa_authenticator
*wpa_auth
,
96 const u8
*addr
, u8
*msk
, size_t *len
)
98 if (wpa_auth
->cb
.get_msk
== NULL
)
100 return wpa_auth
->cb
.get_msk(wpa_auth
->cb
.ctx
, addr
, msk
, len
);
104 static inline int wpa_auth_set_key(struct wpa_authenticator
*wpa_auth
,
106 const char *alg
, const u8
*addr
, int idx
,
107 u8
*key
, size_t key_len
)
109 if (wpa_auth
->cb
.set_key
== NULL
)
111 return wpa_auth
->cb
.set_key(wpa_auth
->cb
.ctx
, vlan_id
, alg
, addr
, idx
,
116 static inline int wpa_auth_get_seqnum(struct wpa_authenticator
*wpa_auth
,
117 const u8
*addr
, int idx
, u8
*seq
)
119 if (wpa_auth
->cb
.get_seqnum
== NULL
)
121 return wpa_auth
->cb
.get_seqnum(wpa_auth
->cb
.ctx
, addr
, idx
, seq
);
125 static inline int wpa_auth_get_seqnum_igtk(struct wpa_authenticator
*wpa_auth
,
126 const u8
*addr
, int idx
, u8
*seq
)
128 if (wpa_auth
->cb
.get_seqnum_igtk
== NULL
)
130 return wpa_auth
->cb
.get_seqnum_igtk(wpa_auth
->cb
.ctx
, addr
, idx
, seq
);
135 wpa_auth_send_eapol(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
136 const u8
*data
, size_t data_len
, int encrypt
)
138 if (wpa_auth
->cb
.send_eapol
== NULL
)
140 return wpa_auth
->cb
.send_eapol(wpa_auth
->cb
.ctx
, addr
, data
, data_len
,
145 int wpa_auth_for_each_sta(struct wpa_authenticator
*wpa_auth
,
146 int (*cb
)(struct wpa_state_machine
*sm
, void *ctx
),
149 if (wpa_auth
->cb
.for_each_sta
== NULL
)
151 return wpa_auth
->cb
.for_each_sta(wpa_auth
->cb
.ctx
, cb
, cb_ctx
);
155 int wpa_auth_for_each_auth(struct wpa_authenticator
*wpa_auth
,
156 int (*cb
)(struct wpa_authenticator
*a
, void *ctx
),
159 if (wpa_auth
->cb
.for_each_auth
== NULL
)
161 return wpa_auth
->cb
.for_each_auth(wpa_auth
->cb
.ctx
, cb
, cb_ctx
);
165 void wpa_auth_logger(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
166 logger_level level
, const char *txt
)
168 if (wpa_auth
->cb
.logger
== NULL
)
170 wpa_auth
->cb
.logger(wpa_auth
->cb
.ctx
, addr
, level
, txt
);
174 void wpa_auth_vlogger(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
175 logger_level level
, const char *fmt
, ...)
181 if (wpa_auth
->cb
.logger
== NULL
)
184 maxlen
= os_strlen(fmt
) + 100;
185 format
= os_malloc(maxlen
);
190 vsnprintf(format
, maxlen
, fmt
, ap
);
193 wpa_auth_logger(wpa_auth
, addr
, level
, format
);
199 static void wpa_sta_disconnect(struct wpa_authenticator
*wpa_auth
,
202 if (wpa_auth
->cb
.disconnect
== NULL
)
204 wpa_auth
->cb
.disconnect(wpa_auth
->cb
.ctx
, addr
,
205 WLAN_REASON_PREV_AUTH_NOT_VALID
);
209 static int wpa_use_aes_cmac(struct wpa_state_machine
*sm
)
212 #ifdef CONFIG_IEEE80211R
213 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
215 #endif /* CONFIG_IEEE80211R */
216 #ifdef CONFIG_IEEE80211W
217 if (wpa_key_mgmt_sha256(sm
->wpa_key_mgmt
))
219 #endif /* CONFIG_IEEE80211W */
224 static void wpa_rekey_gmk(void *eloop_ctx
, void *timeout_ctx
)
226 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
228 if (os_get_random(wpa_auth
->group
->GMK
, WPA_GMK_LEN
)) {
229 wpa_printf(MSG_ERROR
, "Failed to get random data for WPA "
232 wpa_auth_logger(wpa_auth
, NULL
, LOGGER_DEBUG
, "GMK rekeyd");
235 if (wpa_auth
->conf
.wpa_gmk_rekey
) {
236 eloop_register_timeout(wpa_auth
->conf
.wpa_gmk_rekey
, 0,
237 wpa_rekey_gmk
, wpa_auth
, NULL
);
242 static void wpa_rekey_gtk(void *eloop_ctx
, void *timeout_ctx
)
244 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
245 struct wpa_group
*group
;
247 wpa_auth_logger(wpa_auth
, NULL
, LOGGER_DEBUG
, "rekeying GTK");
248 for (group
= wpa_auth
->group
; group
; group
= group
->next
) {
249 group
->GTKReKey
= TRUE
;
251 group
->changed
= FALSE
;
252 wpa_group_sm_step(wpa_auth
, group
);
253 } while (group
->changed
);
256 if (wpa_auth
->conf
.wpa_group_rekey
) {
257 eloop_register_timeout(wpa_auth
->conf
.wpa_group_rekey
,
258 0, wpa_rekey_gtk
, wpa_auth
, NULL
);
263 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine
*sm
, void *ctx
)
265 if (sm
->pmksa
== ctx
)
271 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry
*entry
,
274 struct wpa_authenticator
*wpa_auth
= ctx
;
275 wpa_auth_for_each_sta(wpa_auth
, wpa_auth_pmksa_clear_cb
, entry
);
279 static struct wpa_group
* wpa_group_init(struct wpa_authenticator
*wpa_auth
,
282 struct wpa_group
*group
;
283 u8 buf
[ETH_ALEN
+ 8 + sizeof(group
)];
286 group
= os_zalloc(sizeof(struct wpa_group
));
290 group
->GTKAuthenticator
= TRUE
;
291 group
->vlan_id
= vlan_id
;
293 switch (wpa_auth
->conf
.wpa_group
) {
294 case WPA_CIPHER_CCMP
:
297 case WPA_CIPHER_TKIP
:
300 case WPA_CIPHER_WEP104
:
303 case WPA_CIPHER_WEP40
:
308 /* Counter = PRF-256(Random number, "Init Counter",
309 * Local MAC Address || Time)
311 os_memcpy(buf
, wpa_auth
->addr
, ETH_ALEN
);
312 wpa_get_ntp_timestamp(buf
+ ETH_ALEN
);
313 os_memcpy(buf
+ ETH_ALEN
+ 8, &group
, sizeof(group
));
314 if (os_get_random(rkey
, sizeof(rkey
)) ||
315 os_get_random(group
->GMK
, WPA_GMK_LEN
)) {
316 wpa_printf(MSG_ERROR
, "Failed to get random data for WPA "
322 sha1_prf(rkey
, sizeof(rkey
), "Init Counter", buf
, sizeof(buf
),
323 group
->Counter
, WPA_NONCE_LEN
);
326 wpa_group_sm_step(wpa_auth
, group
);
327 group
->GInit
= FALSE
;
328 wpa_group_sm_step(wpa_auth
, group
);
335 * wpa_init - Initialize WPA authenticator
336 * @addr: Authenticator address
337 * @conf: Configuration for WPA authenticator
338 * Returns: Pointer to WPA authenticator data or %NULL on failure
340 struct wpa_authenticator
* wpa_init(const u8
*addr
,
341 struct wpa_auth_config
*conf
,
342 struct wpa_auth_callbacks
*cb
)
344 struct wpa_authenticator
*wpa_auth
;
346 wpa_auth
= os_zalloc(sizeof(struct wpa_authenticator
));
347 if (wpa_auth
== NULL
)
349 os_memcpy(wpa_auth
->addr
, addr
, ETH_ALEN
);
350 os_memcpy(&wpa_auth
->conf
, conf
, sizeof(*conf
));
351 os_memcpy(&wpa_auth
->cb
, cb
, sizeof(*cb
));
353 if (wpa_auth_gen_wpa_ie(wpa_auth
)) {
354 wpa_printf(MSG_ERROR
, "Could not generate WPA IE.");
359 wpa_auth
->group
= wpa_group_init(wpa_auth
, 0);
360 if (wpa_auth
->group
== NULL
) {
361 os_free(wpa_auth
->wpa_ie
);
366 wpa_auth
->pmksa
= pmksa_cache_init(wpa_auth_pmksa_free_cb
, wpa_auth
);
367 if (wpa_auth
->pmksa
== NULL
) {
368 wpa_printf(MSG_ERROR
, "PMKSA cache initialization failed.");
369 os_free(wpa_auth
->wpa_ie
);
374 #ifdef CONFIG_IEEE80211R
375 wpa_auth
->ft_pmk_cache
= wpa_ft_pmk_cache_init();
376 if (wpa_auth
->ft_pmk_cache
== NULL
) {
377 wpa_printf(MSG_ERROR
, "FT PMK cache initialization failed.");
378 os_free(wpa_auth
->wpa_ie
);
379 pmksa_cache_deinit(wpa_auth
->pmksa
);
383 #endif /* CONFIG_IEEE80211R */
385 if (wpa_auth
->conf
.wpa_gmk_rekey
) {
386 eloop_register_timeout(wpa_auth
->conf
.wpa_gmk_rekey
, 0,
387 wpa_rekey_gmk
, wpa_auth
, NULL
);
390 if (wpa_auth
->conf
.wpa_group_rekey
) {
391 eloop_register_timeout(wpa_auth
->conf
.wpa_group_rekey
, 0,
392 wpa_rekey_gtk
, wpa_auth
, NULL
);
400 * wpa_deinit - Deinitialize WPA authenticator
401 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
403 void wpa_deinit(struct wpa_authenticator
*wpa_auth
)
405 struct wpa_group
*group
, *prev
;
407 eloop_cancel_timeout(wpa_rekey_gmk
, wpa_auth
, NULL
);
408 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
410 #ifdef CONFIG_PEERKEY
411 while (wpa_auth
->stsl_negotiations
)
412 wpa_stsl_remove(wpa_auth
, wpa_auth
->stsl_negotiations
);
413 #endif /* CONFIG_PEERKEY */
415 pmksa_cache_deinit(wpa_auth
->pmksa
);
417 #ifdef CONFIG_IEEE80211R
418 wpa_ft_pmk_cache_deinit(wpa_auth
->ft_pmk_cache
);
419 wpa_auth
->ft_pmk_cache
= NULL
;
420 #endif /* CONFIG_IEEE80211R */
422 os_free(wpa_auth
->wpa_ie
);
424 group
= wpa_auth
->group
;
436 * wpa_reconfig - Update WPA authenticator configuration
437 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
438 * @conf: Configuration for WPA authenticator
440 int wpa_reconfig(struct wpa_authenticator
*wpa_auth
,
441 struct wpa_auth_config
*conf
)
443 if (wpa_auth
== NULL
)
446 os_memcpy(&wpa_auth
->conf
, conf
, sizeof(*conf
));
449 * Disassociate stations if configuration changed
456 struct wpa_state_machine
*
457 wpa_auth_sta_init(struct wpa_authenticator
*wpa_auth
, const u8
*addr
)
459 struct wpa_state_machine
*sm
;
461 sm
= os_zalloc(sizeof(struct wpa_state_machine
));
464 os_memcpy(sm
->addr
, addr
, ETH_ALEN
);
466 sm
->wpa_auth
= wpa_auth
;
467 sm
->group
= wpa_auth
->group
;
473 void wpa_auth_sta_associated(struct wpa_authenticator
*wpa_auth
,
474 struct wpa_state_machine
*sm
)
476 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
479 #ifdef CONFIG_IEEE80211R
480 if (sm
->ft_completed
) {
481 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
482 "FT authentication already completed - do not "
483 "start 4-way handshake");
486 #endif /* CONFIG_IEEE80211R */
489 os_memset(sm
->key_replay_counter
, 0, WPA_REPLAY_COUNTER_LEN
);
490 sm
->ReAuthenticationRequest
= TRUE
;
495 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
496 "start authentication");
502 sm
->AuthenticationRequest
= TRUE
;
507 static void wpa_free_sta_sm(struct wpa_state_machine
*sm
)
509 os_free(sm
->last_rx_eapol_key
);
515 void wpa_auth_sta_deinit(struct wpa_state_machine
*sm
)
520 if (sm
->wpa_auth
->conf
.wpa_strict_rekey
&& sm
->has_GTK
) {
521 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
522 "strict rekeying - force GTK rekey since STA "
524 eloop_cancel_timeout(wpa_rekey_gtk
, sm
->wpa_auth
, NULL
);
525 eloop_register_timeout(0, 500000, wpa_rekey_gtk
, sm
->wpa_auth
,
529 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
530 eloop_cancel_timeout(wpa_sm_call_step
, sm
, NULL
);
531 if (sm
->in_step_loop
) {
532 /* Must not free state machine while wpa_sm_step() is running.
533 * Freeing will be completed in the end of wpa_sm_step(). */
534 wpa_printf(MSG_DEBUG
, "WPA: Registering pending STA state "
535 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
536 sm
->pending_deinit
= 1;
542 static void wpa_request_new_ptk(struct wpa_state_machine
*sm
)
547 sm
->PTKRequest
= TRUE
;
552 void wpa_receive(struct wpa_authenticator
*wpa_auth
,
553 struct wpa_state_machine
*sm
,
554 u8
*data
, size_t data_len
)
556 struct ieee802_1x_hdr
*hdr
;
557 struct wpa_eapol_key
*key
;
558 u16 key_info
, key_data_length
;
559 enum { PAIRWISE_2
, PAIRWISE_4
, GROUP_2
, REQUEST
,
560 SMK_M1
, SMK_M3
, SMK_ERROR
} msg
;
562 struct wpa_eapol_ie_parse kde
;
564 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
567 if (data_len
< sizeof(*hdr
) + sizeof(*key
))
570 hdr
= (struct ieee802_1x_hdr
*) data
;
571 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
572 key_info
= WPA_GET_BE16(key
->key_info
);
573 key_data_length
= WPA_GET_BE16(key
->key_data_length
);
574 if (key_data_length
> data_len
- sizeof(*hdr
) - sizeof(*key
)) {
575 wpa_printf(MSG_INFO
, "WPA: Invalid EAPOL-Key frame - "
576 "key_data overflow (%d > %lu)",
578 (unsigned long) (data_len
- sizeof(*hdr
) -
583 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
586 if ((key_info
& (WPA_KEY_INFO_SMK_MESSAGE
| WPA_KEY_INFO_REQUEST
)) ==
587 (WPA_KEY_INFO_SMK_MESSAGE
| WPA_KEY_INFO_REQUEST
)) {
588 if (key_info
& WPA_KEY_INFO_ERROR
) {
590 msgtxt
= "SMK Error";
595 } else if (key_info
& WPA_KEY_INFO_SMK_MESSAGE
) {
598 } else if (key_info
& WPA_KEY_INFO_REQUEST
) {
601 } else if (!(key_info
& WPA_KEY_INFO_KEY_TYPE
)) {
603 msgtxt
= "2/2 Group";
604 } else if (key_data_length
== 0) {
606 msgtxt
= "4/4 Pairwise";
609 msgtxt
= "2/4 Pairwise";
612 /* TODO: key_info type validation for PeerKey */
613 if (msg
== REQUEST
|| msg
== PAIRWISE_2
|| msg
== PAIRWISE_4
||
615 u16 ver
= key_info
& WPA_KEY_INFO_TYPE_MASK
;
616 if (sm
->pairwise
== WPA_CIPHER_CCMP
) {
617 if (wpa_use_aes_cmac(sm
) &&
618 ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
619 wpa_auth_logger(wpa_auth
, sm
->addr
,
621 "advertised support for "
622 "AES-128-CMAC, but did not "
627 if (!wpa_use_aes_cmac(sm
) &&
628 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
629 wpa_auth_logger(wpa_auth
, sm
->addr
,
631 "did not use HMAC-SHA1-AES "
638 if (key_info
& WPA_KEY_INFO_REQUEST
) {
639 if (sm
->req_replay_counter_used
&&
640 os_memcmp(key
->replay_counter
, sm
->req_replay_counter
,
641 WPA_REPLAY_COUNTER_LEN
) <= 0) {
642 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_WARNING
,
643 "received EAPOL-Key request with "
649 if (!(key_info
& WPA_KEY_INFO_REQUEST
) &&
650 (!sm
->key_replay_counter_valid
||
651 os_memcmp(key
->replay_counter
, sm
->key_replay_counter
,
652 WPA_REPLAY_COUNTER_LEN
) != 0)) {
653 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
654 "received EAPOL-Key %s with unexpected "
655 "replay counter", msgtxt
);
656 wpa_hexdump(MSG_DEBUG
, "expected replay counter",
657 sm
->key_replay_counter
, WPA_REPLAY_COUNTER_LEN
);
658 wpa_hexdump(MSG_DEBUG
, "received replay counter",
659 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
665 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKSTART
&&
666 sm
->wpa_ptk_state
!= WPA_PTK_PTKCALCNEGOTIATING
) {
667 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
668 "received EAPOL-Key msg 2/4 in "
669 "invalid state (%d) - dropped",
673 if (sm
->wpa_ie
== NULL
||
674 sm
->wpa_ie_len
!= key_data_length
||
675 os_memcmp(sm
->wpa_ie
, key
+ 1, key_data_length
) != 0) {
676 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
677 "WPA IE from (Re)AssocReq did not "
678 "match with msg 2/4");
680 wpa_hexdump(MSG_DEBUG
, "WPA IE in AssocReq",
681 sm
->wpa_ie
, sm
->wpa_ie_len
);
683 wpa_hexdump(MSG_DEBUG
, "WPA IE in msg 2/4",
684 (u8
*) (key
+ 1), key_data_length
);
685 /* MLME-DEAUTHENTICATE.request */
686 wpa_sta_disconnect(wpa_auth
, sm
->addr
);
691 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITNEGOTIATING
||
693 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
694 "received EAPOL-Key msg 4/4 in "
695 "invalid state (%d) - dropped",
701 if (sm
->wpa_ptk_group_state
!= WPA_PTK_GROUP_REKEYNEGOTIATING
703 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
704 "received EAPOL-Key msg 2/2 in "
705 "invalid state (%d) - dropped",
706 sm
->wpa_ptk_group_state
);
710 #ifdef CONFIG_PEERKEY
714 if (!wpa_auth
->conf
.peerkey
) {
715 wpa_printf(MSG_DEBUG
, "RSN: SMK M1/M3/Error, but "
716 "PeerKey use disabled - ignoring message");
719 if (!sm
->PTK_valid
) {
720 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
721 "received EAPOL-Key msg SMK in "
722 "invalid state - dropped");
726 #else /* CONFIG_PEERKEY */
730 return; /* STSL disabled - ignore SMK messages */
731 #endif /* CONFIG_PEERKEY */
736 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
737 "received EAPOL-Key frame (%s)", msgtxt
);
739 if (key_info
& WPA_KEY_INFO_ACK
) {
740 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
741 "received invalid EAPOL-Key: Key Ack set");
745 if (!(key_info
& WPA_KEY_INFO_MIC
)) {
746 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
747 "received invalid EAPOL-Key: Key MIC not set");
751 sm
->MICVerified
= FALSE
;
753 if (wpa_verify_key_mic(&sm
->PTK
, data
, data_len
)) {
754 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
755 "received EAPOL-Key with invalid MIC");
758 sm
->MICVerified
= TRUE
;
759 eloop_cancel_timeout(wpa_send_eapol_timeout
, wpa_auth
, sm
);
762 if (key_info
& WPA_KEY_INFO_REQUEST
) {
763 if (sm
->MICVerified
) {
764 sm
->req_replay_counter_used
= 1;
765 os_memcpy(sm
->req_replay_counter
, key
->replay_counter
,
766 WPA_REPLAY_COUNTER_LEN
);
768 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
769 "received EAPOL-Key request with "
775 * TODO: should decrypt key data field if encryption was used;
776 * even though MAC address KDE is not normally encrypted,
777 * supplicant is allowed to encrypt it.
779 if (msg
== SMK_ERROR
) {
780 #ifdef CONFIG_PEERKEY
781 wpa_smk_error(wpa_auth
, sm
, key
);
782 #endif /* CONFIG_PEERKEY */
784 } else if (key_info
& WPA_KEY_INFO_ERROR
) {
785 /* Supplicant reported a Michael MIC error */
786 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
787 "received EAPOL-Key Error Request "
788 "(STA detected Michael MIC failure)");
789 wpa_auth_mic_failure_report(wpa_auth
, sm
->addr
);
790 sm
->dot11RSNAStatsTKIPRemoteMICFailures
++;
791 wpa_auth
->dot11RSNAStatsTKIPRemoteMICFailures
++;
792 /* Error report is not a request for a new key
793 * handshake, but since Authenticator may do it, let's
794 * change the keys now anyway. */
795 wpa_request_new_ptk(sm
);
796 } else if (key_info
& WPA_KEY_INFO_KEY_TYPE
) {
797 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
798 "received EAPOL-Key Request for new "
800 wpa_request_new_ptk(sm
);
801 #ifdef CONFIG_PEERKEY
802 } else if (msg
== SMK_M1
) {
803 wpa_smk_m1(wpa_auth
, sm
, key
);
804 #endif /* CONFIG_PEERKEY */
805 } else if (key_data_length
> 0 &&
806 wpa_parse_kde_ies((const u8
*) (key
+ 1),
807 key_data_length
, &kde
) == 0 &&
810 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
811 "received EAPOL-Key Request for GTK "
813 /* FIX: why was this triggering PTK rekeying for the
814 * STA that requested Group Key rekeying?? */
815 /* wpa_request_new_ptk(sta->wpa_sm); */
816 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
817 wpa_rekey_gtk(wpa_auth
, NULL
);
820 /* Do not allow the same key replay counter to be reused. */
821 sm
->key_replay_counter_valid
= FALSE
;
824 #ifdef CONFIG_PEERKEY
826 wpa_smk_m3(wpa_auth
, sm
, key
);
829 #endif /* CONFIG_PEERKEY */
831 os_free(sm
->last_rx_eapol_key
);
832 sm
->last_rx_eapol_key
= os_malloc(data_len
);
833 if (sm
->last_rx_eapol_key
== NULL
)
835 os_memcpy(sm
->last_rx_eapol_key
, data
, data_len
);
836 sm
->last_rx_eapol_key_len
= data_len
;
838 sm
->EAPOLKeyReceived
= TRUE
;
839 sm
->EAPOLKeyPairwise
= !!(key_info
& WPA_KEY_INFO_KEY_TYPE
);
840 sm
->EAPOLKeyRequest
= !!(key_info
& WPA_KEY_INFO_REQUEST
);
841 os_memcpy(sm
->SNonce
, key
->key_nonce
, WPA_NONCE_LEN
);
846 static void wpa_gmk_to_gtk(const u8
*gmk
, const u8
*addr
, const u8
*gnonce
,
847 u8
*gtk
, size_t gtk_len
)
849 u8 data
[ETH_ALEN
+ WPA_NONCE_LEN
];
851 /* GTK = PRF-X(GMK, "Group key expansion", AA || GNonce) */
852 os_memcpy(data
, addr
, ETH_ALEN
);
853 os_memcpy(data
+ ETH_ALEN
, gnonce
, WPA_NONCE_LEN
);
855 #ifdef CONFIG_IEEE80211W
856 sha256_prf(gmk
, WPA_GMK_LEN
, "Group key expansion",
857 data
, sizeof(data
), gtk
, gtk_len
);
858 #else /* CONFIG_IEEE80211W */
859 sha1_prf(gmk
, WPA_GMK_LEN
, "Group key expansion",
860 data
, sizeof(data
), gtk
, gtk_len
);
861 #endif /* CONFIG_IEEE80211W */
863 wpa_hexdump_key(MSG_DEBUG
, "GMK", gmk
, WPA_GMK_LEN
);
864 wpa_hexdump_key(MSG_DEBUG
, "GTK", gtk
, gtk_len
);
868 static void wpa_send_eapol_timeout(void *eloop_ctx
, void *timeout_ctx
)
870 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
871 struct wpa_state_machine
*sm
= timeout_ctx
;
873 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
, "EAPOL-Key timeout");
874 sm
->TimeoutEvt
= TRUE
;
879 void __wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
880 struct wpa_state_machine
*sm
, int key_info
,
881 const u8
*key_rsc
, const u8
*nonce
,
882 const u8
*kde
, size_t kde_len
,
883 int keyidx
, int encr
, int force_version
)
885 struct ieee802_1x_hdr
*hdr
;
886 struct wpa_eapol_key
*key
;
889 int key_data_len
, pad_len
= 0;
891 int version
, pairwise
;
893 len
= sizeof(struct ieee802_1x_hdr
) + sizeof(struct wpa_eapol_key
);
896 version
= force_version
;
897 else if (wpa_use_aes_cmac(sm
))
898 version
= WPA_KEY_INFO_TYPE_AES_128_CMAC
;
899 else if (sm
->pairwise
== WPA_CIPHER_CCMP
)
900 version
= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
;
902 version
= WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
;
904 pairwise
= key_info
& WPA_KEY_INFO_KEY_TYPE
;
906 wpa_printf(MSG_DEBUG
, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
907 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
910 (key_info
& WPA_KEY_INFO_SECURE
) ? 1 : 0,
911 (key_info
& WPA_KEY_INFO_MIC
) ? 1 : 0,
912 (key_info
& WPA_KEY_INFO_ACK
) ? 1 : 0,
913 (key_info
& WPA_KEY_INFO_INSTALL
) ? 1 : 0,
914 pairwise
, (unsigned long) kde_len
, keyidx
, encr
);
916 key_data_len
= kde_len
;
918 if ((version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
919 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) && encr
) {
920 pad_len
= key_data_len
% 8;
922 pad_len
= 8 - pad_len
;
923 key_data_len
+= pad_len
+ 8;
928 hdr
= os_zalloc(len
);
931 hdr
->version
= wpa_auth
->conf
.eapol_version
;
932 hdr
->type
= IEEE802_1X_TYPE_EAPOL_KEY
;
933 hdr
->length
= host_to_be16(len
- sizeof(*hdr
));
934 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
936 key
->type
= sm
->wpa
== WPA_VERSION_WPA2
?
937 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
939 if (encr
&& sm
->wpa
== WPA_VERSION_WPA2
)
940 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
941 if (sm
->wpa
!= WPA_VERSION_WPA2
)
942 key_info
|= keyidx
<< WPA_KEY_INFO_KEY_INDEX_SHIFT
;
943 WPA_PUT_BE16(key
->key_info
, key_info
);
945 alg
= pairwise
? sm
->pairwise
: wpa_auth
->conf
.wpa_group
;
947 case WPA_CIPHER_CCMP
:
948 WPA_PUT_BE16(key
->key_length
, 16);
950 case WPA_CIPHER_TKIP
:
951 WPA_PUT_BE16(key
->key_length
, 32);
953 case WPA_CIPHER_WEP40
:
954 WPA_PUT_BE16(key
->key_length
, 5);
956 case WPA_CIPHER_WEP104
:
957 WPA_PUT_BE16(key
->key_length
, 13);
960 if (key_info
& WPA_KEY_INFO_SMK_MESSAGE
)
961 WPA_PUT_BE16(key
->key_length
, 0);
963 /* FIX: STSL: what to use as key_replay_counter? */
964 inc_byte_array(sm
->key_replay_counter
, WPA_REPLAY_COUNTER_LEN
);
965 os_memcpy(key
->replay_counter
, sm
->key_replay_counter
,
966 WPA_REPLAY_COUNTER_LEN
);
967 sm
->key_replay_counter_valid
= TRUE
;
970 os_memcpy(key
->key_nonce
, nonce
, WPA_NONCE_LEN
);
973 os_memcpy(key
->key_rsc
, key_rsc
, WPA_KEY_RSC_LEN
);
976 os_memcpy(key
+ 1, kde
, kde_len
);
977 WPA_PUT_BE16(key
->key_data_length
, kde_len
);
978 } else if (encr
&& kde
) {
979 buf
= os_zalloc(key_data_len
);
985 os_memcpy(pos
, kde
, kde_len
);
991 wpa_hexdump_key(MSG_DEBUG
, "Plaintext EAPOL-Key Key Data",
993 if (version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
994 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
995 if (aes_wrap(sm
->PTK
.kek
, (key_data_len
- 8) / 8, buf
,
1001 WPA_PUT_BE16(key
->key_data_length
, key_data_len
);
1004 os_memcpy(key
->key_iv
,
1005 sm
->group
->Counter
+ WPA_NONCE_LEN
- 16, 16);
1006 inc_byte_array(sm
->group
->Counter
, WPA_NONCE_LEN
);
1007 os_memcpy(ek
, key
->key_iv
, 16);
1008 os_memcpy(ek
+ 16, sm
->PTK
.kek
, 16);
1009 os_memcpy(key
+ 1, buf
, key_data_len
);
1010 rc4_skip(ek
, 32, 256, (u8
*) (key
+ 1), key_data_len
);
1011 WPA_PUT_BE16(key
->key_data_length
, key_data_len
);
1016 if (key_info
& WPA_KEY_INFO_MIC
) {
1017 if (!sm
->PTK_valid
) {
1018 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1019 "PTK not valid when sending EAPOL-Key "
1024 wpa_eapol_key_mic(sm
->PTK
.kck
, version
, (u8
*) hdr
, len
,
1028 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_inc_EapolFramesTx
,
1030 wpa_auth_send_eapol(wpa_auth
, sm
->addr
, (u8
*) hdr
, len
,
1036 static void wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
1037 struct wpa_state_machine
*sm
, int key_info
,
1038 const u8
*key_rsc
, const u8
*nonce
,
1039 const u8
*kde
, size_t kde_len
,
1040 int keyidx
, int encr
)
1043 int pairwise
= key_info
& WPA_KEY_INFO_KEY_TYPE
;
1048 __wpa_send_eapol(wpa_auth
, sm
, key_info
, key_rsc
, nonce
, kde
, kde_len
,
1051 timeout_ms
= pairwise
? dot11RSNAConfigPairwiseUpdateTimeOut
:
1052 dot11RSNAConfigGroupUpdateTimeOut
;
1053 eloop_register_timeout(timeout_ms
/ 1000, (timeout_ms
% 1000) * 1000,
1054 wpa_send_eapol_timeout
, wpa_auth
, sm
);
1058 static int wpa_verify_key_mic(struct wpa_ptk
*PTK
, u8
*data
, size_t data_len
)
1060 struct ieee802_1x_hdr
*hdr
;
1061 struct wpa_eapol_key
*key
;
1066 if (data_len
< sizeof(*hdr
) + sizeof(*key
))
1069 hdr
= (struct ieee802_1x_hdr
*) data
;
1070 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1071 key_info
= WPA_GET_BE16(key
->key_info
);
1072 os_memcpy(mic
, key
->key_mic
, 16);
1073 os_memset(key
->key_mic
, 0, 16);
1074 if (wpa_eapol_key_mic(PTK
->kck
, key_info
& WPA_KEY_INFO_TYPE_MASK
,
1075 data
, data_len
, key
->key_mic
) ||
1076 os_memcmp(mic
, key
->key_mic
, 16) != 0)
1078 os_memcpy(key
->key_mic
, mic
, 16);
1083 void wpa_remove_ptk(struct wpa_state_machine
*sm
)
1085 sm
->PTK_valid
= FALSE
;
1086 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1087 wpa_auth_set_key(sm
->wpa_auth
, 0, "none", sm
->addr
, 0, (u8
*) "", 0);
1088 sm
->pairwise_set
= FALSE
;
1092 void wpa_auth_sm_event(struct wpa_state_machine
*sm
, wpa_event event
)
1099 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1100 "event %d notification", event
);
1108 sm
->DeauthenticationRequest
= TRUE
;
1111 case WPA_REAUTH_EAPOL
:
1112 sm
->ReAuthenticationRequest
= TRUE
;
1115 #ifdef CONFIG_IEEE80211R
1116 /* Using FT protocol, not WPA auth state machine */
1117 sm
->ft_completed
= 1;
1119 #else /* CONFIG_IEEE80211R */
1121 #endif /* CONFIG_IEEE80211R */
1124 #ifdef CONFIG_IEEE80211R
1125 sm
->ft_completed
= 0;
1126 #endif /* CONFIG_IEEE80211R */
1128 #ifdef CONFIG_IEEE80211W
1129 if (sm
->mgmt_frame_prot
&& event
== WPA_AUTH
)
1131 #endif /* CONFIG_IEEE80211W */
1134 sm
->PTK_valid
= FALSE
;
1135 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1137 if (event
!= WPA_REAUTH_EAPOL
)
1145 static const char * wpa_alg_txt(int alg
)
1148 case WPA_CIPHER_CCMP
:
1150 case WPA_CIPHER_TKIP
:
1152 case WPA_CIPHER_WEP104
:
1153 case WPA_CIPHER_WEP40
:
1161 SM_STATE(WPA_PTK
, INITIALIZE
)
1163 SM_ENTRY_MA(WPA_PTK
, INITIALIZE
, wpa_ptk
);
1165 /* Init flag is not cleared here, so avoid busy
1166 * loop by claiming nothing changed. */
1167 sm
->changed
= FALSE
;
1171 if (sm
->GUpdateStationKeys
)
1172 sm
->group
->GKeyDoneStations
--;
1173 sm
->GUpdateStationKeys
= FALSE
;
1174 if (sm
->wpa
== WPA_VERSION_WPA
)
1175 sm
->PInitAKeys
= FALSE
;
1176 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1177 * Local AA > Remote AA)) */) {
1180 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 0);
1182 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
, 0);
1184 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
1185 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
1186 WPA_EAPOL_authorized
, 0);
1191 SM_STATE(WPA_PTK
, DISCONNECT
)
1193 SM_ENTRY_MA(WPA_PTK
, DISCONNECT
, wpa_ptk
);
1194 sm
->Disconnect
= FALSE
;
1195 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
);
1199 SM_STATE(WPA_PTK
, DISCONNECTED
)
1201 SM_ENTRY_MA(WPA_PTK
, DISCONNECTED
, wpa_ptk
);
1202 sm
->DeauthenticationRequest
= FALSE
;
1206 SM_STATE(WPA_PTK
, AUTHENTICATION
)
1208 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION
, wpa_ptk
);
1209 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1210 sm
->PTK_valid
= FALSE
;
1211 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portControl_Auto
,
1213 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 1);
1214 sm
->AuthenticationRequest
= FALSE
;
1218 SM_STATE(WPA_PTK
, AUTHENTICATION2
)
1220 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION2
, wpa_ptk
);
1221 os_memcpy(sm
->ANonce
, sm
->group
->Counter
, WPA_NONCE_LEN
);
1222 inc_byte_array(sm
->group
->Counter
, WPA_NONCE_LEN
);
1223 sm
->ReAuthenticationRequest
= FALSE
;
1224 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1225 * logical place than INITIALIZE since AUTHENTICATION2 can be
1226 * re-entered on ReAuthenticationRequest without going through
1232 SM_STATE(WPA_PTK
, INITPMK
)
1234 u8 msk
[2 * PMK_LEN
];
1235 size_t len
= 2 * PMK_LEN
;
1237 SM_ENTRY_MA(WPA_PTK
, INITPMK
, wpa_ptk
);
1238 #ifdef CONFIG_IEEE80211R
1240 #endif /* CONFIG_IEEE80211R */
1242 wpa_printf(MSG_DEBUG
, "WPA: PMK from PMKSA cache");
1243 os_memcpy(sm
->PMK
, sm
->pmksa
->pmk
, PMK_LEN
);
1244 } else if (wpa_auth_get_msk(sm
->wpa_auth
, sm
->addr
, msk
, &len
) == 0) {
1245 wpa_printf(MSG_DEBUG
, "WPA: PMK from EAPOL state machine "
1246 "(len=%lu)", (unsigned long) len
);
1247 os_memcpy(sm
->PMK
, msk
, PMK_LEN
);
1248 #ifdef CONFIG_IEEE80211R
1249 if (len
>= 2 * PMK_LEN
) {
1250 os_memcpy(sm
->xxkey
, msk
+ PMK_LEN
, PMK_LEN
);
1251 sm
->xxkey_len
= PMK_LEN
;
1253 #endif /* CONFIG_IEEE80211R */
1255 wpa_printf(MSG_DEBUG
, "WPA: Could not get PMK");
1258 sm
->req_replay_counter_used
= 0;
1259 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1260 * will break reauthentication since EAPOL state machines may not be
1261 * get into AUTHENTICATING state that clears keyRun before WPA state
1262 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1263 * state and takes PMK from the previously used AAA Key. This will
1264 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1265 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1266 * be good workaround for this issue. */
1267 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyRun
, 0);
1271 SM_STATE(WPA_PTK
, INITPSK
)
1274 SM_ENTRY_MA(WPA_PTK
, INITPSK
, wpa_ptk
);
1275 psk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, NULL
);
1277 os_memcpy(sm
->PMK
, psk
, PMK_LEN
);
1278 #ifdef CONFIG_IEEE80211R
1279 os_memcpy(sm
->xxkey
, psk
, PMK_LEN
);
1280 sm
->xxkey_len
= PMK_LEN
;
1281 #endif /* CONFIG_IEEE80211R */
1283 sm
->req_replay_counter_used
= 0;
1287 SM_STATE(WPA_PTK
, PTKSTART
)
1289 u8 buf
[2 + RSN_SELECTOR_LEN
+ PMKID_LEN
], *pmkid
= NULL
;
1290 size_t pmkid_len
= 0;
1292 SM_ENTRY_MA(WPA_PTK
, PTKSTART
, wpa_ptk
);
1293 sm
->PTKRequest
= FALSE
;
1294 sm
->TimeoutEvt
= FALSE
;
1295 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1296 "sending 1/4 msg of 4-Way Handshake");
1298 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1299 * one possible PSK for this STA.
1301 if (sm
->wpa
== WPA_VERSION_WPA2
&&
1302 wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
)) {
1304 pmkid_len
= 2 + RSN_SELECTOR_LEN
+ PMKID_LEN
;
1305 pmkid
[0] = WLAN_EID_VENDOR_SPECIFIC
;
1306 pmkid
[1] = RSN_SELECTOR_LEN
+ PMKID_LEN
;
1307 RSN_SELECTOR_PUT(&pmkid
[2], RSN_KEY_DATA_PMKID
);
1309 os_memcpy(&pmkid
[2 + RSN_SELECTOR_LEN
],
1310 sm
->pmksa
->pmkid
, PMKID_LEN
);
1313 * Calculate PMKID since no PMKSA cache entry was
1314 * available with pre-calculated PMKID.
1316 rsn_pmkid(sm
->PMK
, PMK_LEN
, sm
->wpa_auth
->addr
,
1317 sm
->addr
, &pmkid
[2 + RSN_SELECTOR_LEN
],
1318 wpa_key_mgmt_sha256(sm
->wpa_key_mgmt
));
1321 wpa_send_eapol(sm
->wpa_auth
, sm
,
1322 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_KEY_TYPE
, NULL
,
1323 sm
->ANonce
, pmkid
, pmkid_len
, 0, 0);
1328 static int wpa_derive_ptk(struct wpa_state_machine
*sm
, const u8
*pmk
,
1329 struct wpa_ptk
*ptk
)
1331 #ifdef CONFIG_IEEE80211R
1332 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
1333 return wpa_auth_derive_ptk_ft(sm
, pmk
, ptk
);
1334 #endif /* CONFIG_IEEE80211R */
1336 wpa_pmk_to_ptk(pmk
, PMK_LEN
, "Pairwise key expansion",
1337 sm
->wpa_auth
->addr
, sm
->addr
, sm
->ANonce
, sm
->SNonce
,
1338 (u8
*) ptk
, sizeof(*ptk
),
1339 wpa_key_mgmt_sha256(sm
->wpa_key_mgmt
));
1345 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING
)
1349 const u8
*pmk
= NULL
;
1351 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING
, wpa_ptk
);
1352 sm
->EAPOLKeyReceived
= FALSE
;
1354 /* WPA with IEEE 802.1X: use the derived PMK from EAP
1355 * WPA-PSK: iterate through possible PSKs and select the one matching
1358 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
1359 pmk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, pmk
);
1365 wpa_derive_ptk(sm
, pmk
, &PTK
);
1367 if (wpa_verify_key_mic(&PTK
, sm
->last_rx_eapol_key
,
1368 sm
->last_rx_eapol_key_len
) == 0) {
1373 if (!wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
))
1378 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1379 "invalid MIC in msg 2/4 of 4-Way Handshake");
1383 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
1385 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
1386 /* PSK may have changed from the previous choice, so update
1387 * state machine data based on whatever PSK was selected here.
1389 os_memcpy(sm
->PMK
, pmk
, PMK_LEN
);
1392 sm
->MICVerified
= TRUE
;
1394 os_memcpy(&sm
->PTK
, &PTK
, sizeof(PTK
));
1395 sm
->PTK_valid
= TRUE
;
1399 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING2
)
1401 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING2
, wpa_ptk
);
1406 #ifdef CONFIG_IEEE80211W
1408 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
)
1410 if (sm
->mgmt_frame_prot
) {
1411 return 2 + RSN_SELECTOR_LEN
+ sizeof(struct wpa_igtk_kde
);
1418 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
)
1420 struct wpa_igtk_kde igtk
;
1421 struct wpa_group
*gsm
= sm
->group
;
1423 if (!sm
->mgmt_frame_prot
)
1426 igtk
.keyid
[0] = gsm
->GN_igtk
;
1428 if (wpa_auth_get_seqnum_igtk(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, igtk
.pn
)
1430 os_memset(igtk
.pn
, 0, sizeof(igtk
.pn
));
1431 os_memcpy(igtk
.igtk
, gsm
->IGTK
[gsm
->GN_igtk
- 4], WPA_IGTK_LEN
);
1432 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_IGTK
,
1433 (const u8
*) &igtk
, sizeof(igtk
), NULL
, 0);
1438 #else /* CONFIG_IEEE80211W */
1440 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
)
1446 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
)
1451 #endif /* CONFIG_IEEE80211W */
1454 SM_STATE(WPA_PTK
, PTKINITNEGOTIATING
)
1456 u8 rsc
[WPA_KEY_RSC_LEN
], *_rsc
, *gtk
, *kde
, *pos
;
1457 size_t gtk_len
, kde_len
;
1458 struct wpa_group
*gsm
= sm
->group
;
1460 int wpa_ie_len
, secure
, keyidx
, encr
= 0;
1462 SM_ENTRY_MA(WPA_PTK
, PTKINITNEGOTIATING
, wpa_ptk
);
1463 sm
->TimeoutEvt
= FALSE
;
1464 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, GTK[GN])
1466 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
1467 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
1468 wpa_ie
= sm
->wpa_auth
->wpa_ie
;
1469 wpa_ie_len
= sm
->wpa_auth
->wpa_ie_len
;
1470 if (sm
->wpa
== WPA_VERSION_WPA
&&
1471 (sm
->wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
) &&
1472 wpa_ie_len
> wpa_ie
[1] + 2 && wpa_ie
[0] == WLAN_EID_RSN
) {
1473 /* WPA-only STA, remove RSN IE */
1474 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
1475 wpa_ie_len
= wpa_ie
[1] + 2;
1477 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1478 "sending 3/4 msg of 4-Way Handshake");
1479 if (sm
->wpa
== WPA_VERSION_WPA2
) {
1480 /* WPA2 send GTK in the 4-way handshake */
1482 gtk
= gsm
->GTK
[gsm
->GN
- 1];
1483 gtk_len
= gsm
->GTK_len
;
1488 /* WPA does not include GTK in msg 3/4 */
1496 kde_len
= wpa_ie_len
+ ieee80211w_kde_len(sm
);
1498 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 2 + gtk_len
;
1499 kde
= os_malloc(kde_len
);
1504 os_memcpy(pos
, wpa_ie
, wpa_ie_len
);
1508 hdr
[0] = keyidx
& 0x03;
1510 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
1513 pos
= ieee80211w_kde_add(sm
, pos
);
1515 wpa_send_eapol(sm
->wpa_auth
, sm
,
1516 (secure
? WPA_KEY_INFO_SECURE
: 0) | WPA_KEY_INFO_MIC
|
1517 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_INSTALL
|
1518 WPA_KEY_INFO_KEY_TYPE
,
1519 _rsc
, sm
->ANonce
, kde
, pos
- kde
, keyidx
, encr
);
1525 SM_STATE(WPA_PTK
, PTKINITDONE
)
1527 SM_ENTRY_MA(WPA_PTK
, PTKINITDONE
, wpa_ptk
);
1528 sm
->EAPOLKeyReceived
= FALSE
;
1532 if (sm
->pairwise
== WPA_CIPHER_TKIP
) {
1539 if (wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
, 0,
1540 sm
->PTK
.tk1
, klen
)) {
1541 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
);
1544 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
1545 sm
->pairwise_set
= TRUE
;
1547 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
1548 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
1549 WPA_EAPOL_authorized
, 1);
1553 if (0 /* IBSS == TRUE */) {
1555 if (sm
->keycount
== 2) {
1556 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
1557 WPA_EAPOL_portValid
, 1);
1560 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
,
1563 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyAvailable
, 0);
1564 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyDone
, 1);
1565 if (sm
->wpa
== WPA_VERSION_WPA
)
1566 sm
->PInitAKeys
= TRUE
;
1569 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
1570 "pairwise key handshake completed (%s)",
1571 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
1573 #ifdef CONFIG_IEEE80211R
1574 wpa_ft_push_pmk_r1(sm
->wpa_auth
, sm
->addr
);
1575 #endif /* CONFIG_IEEE80211R */
1581 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
1584 SM_ENTER(WPA_PTK
, INITIALIZE
);
1585 else if (sm
->Disconnect
1586 /* || FIX: dot11RSNAConfigSALifetime timeout */)
1587 SM_ENTER(WPA_PTK
, DISCONNECT
);
1588 else if (sm
->DeauthenticationRequest
)
1589 SM_ENTER(WPA_PTK
, DISCONNECTED
);
1590 else if (sm
->AuthenticationRequest
)
1591 SM_ENTER(WPA_PTK
, AUTHENTICATION
);
1592 else if (sm
->ReAuthenticationRequest
)
1593 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
1594 else if (sm
->PTKRequest
)
1595 SM_ENTER(WPA_PTK
, PTKSTART
);
1596 else switch (sm
->wpa_ptk_state
) {
1597 case WPA_PTK_INITIALIZE
:
1599 case WPA_PTK_DISCONNECT
:
1600 SM_ENTER(WPA_PTK
, DISCONNECTED
);
1602 case WPA_PTK_DISCONNECTED
:
1603 SM_ENTER(WPA_PTK
, INITIALIZE
);
1605 case WPA_PTK_AUTHENTICATION
:
1606 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
1608 case WPA_PTK_AUTHENTICATION2
:
1609 if (wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) &&
1610 wpa_auth_get_eapol(sm
->wpa_auth
, sm
->addr
,
1611 WPA_EAPOL_keyRun
) > 0)
1612 SM_ENTER(WPA_PTK
, INITPMK
);
1613 else if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)
1614 /* FIX: && 802.1X::keyRun */)
1615 SM_ENTER(WPA_PTK
, INITPSK
);
1617 case WPA_PTK_INITPMK
:
1618 if (wpa_auth_get_eapol(sm
->wpa_auth
, sm
->addr
,
1619 WPA_EAPOL_keyAvailable
) > 0)
1620 SM_ENTER(WPA_PTK
, PTKSTART
);
1622 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
1623 SM_ENTER(WPA_PTK
, DISCONNECT
);
1626 case WPA_PTK_INITPSK
:
1627 if (wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, NULL
))
1628 SM_ENTER(WPA_PTK
, PTKSTART
);
1630 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
1631 "no PSK configured for the STA");
1632 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
1633 SM_ENTER(WPA_PTK
, DISCONNECT
);
1636 case WPA_PTK_PTKSTART
:
1637 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
1638 sm
->EAPOLKeyPairwise
)
1639 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
1640 else if (sm
->TimeoutCtr
>
1641 (int) dot11RSNAConfigPairwiseUpdateCount
) {
1642 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
1643 SM_ENTER(WPA_PTK
, DISCONNECT
);
1644 } else if (sm
->TimeoutEvt
)
1645 SM_ENTER(WPA_PTK
, PTKSTART
);
1647 case WPA_PTK_PTKCALCNEGOTIATING
:
1648 if (sm
->MICVerified
)
1649 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING2
);
1650 else if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
1651 sm
->EAPOLKeyPairwise
)
1652 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
1653 else if (sm
->TimeoutEvt
)
1654 SM_ENTER(WPA_PTK
, PTKSTART
);
1656 case WPA_PTK_PTKCALCNEGOTIATING2
:
1657 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
1659 case WPA_PTK_PTKINITNEGOTIATING
:
1660 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
1661 sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
1662 SM_ENTER(WPA_PTK
, PTKINITDONE
);
1663 else if (sm
->TimeoutCtr
>
1664 (int) dot11RSNAConfigPairwiseUpdateCount
) {
1665 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
1666 SM_ENTER(WPA_PTK
, DISCONNECT
);
1667 } else if (sm
->TimeoutEvt
)
1668 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
1670 case WPA_PTK_PTKINITDONE
:
1676 SM_STATE(WPA_PTK_GROUP
, IDLE
)
1678 SM_ENTRY_MA(WPA_PTK_GROUP
, IDLE
, wpa_ptk_group
);
1680 /* Init flag is not cleared here, so avoid busy
1681 * loop by claiming nothing changed. */
1682 sm
->changed
= FALSE
;
1684 sm
->GTimeoutCtr
= 0;
1688 SM_STATE(WPA_PTK_GROUP
, REKEYNEGOTIATING
)
1690 u8 rsc
[WPA_KEY_RSC_LEN
];
1691 struct wpa_group
*gsm
= sm
->group
;
1692 u8
*kde
, *pos
, hdr
[2];
1695 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYNEGOTIATING
, wpa_ptk_group
);
1696 if (sm
->wpa
== WPA_VERSION_WPA
)
1697 sm
->PInitAKeys
= FALSE
;
1698 sm
->TimeoutEvt
= FALSE
;
1699 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
1700 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
1701 if (gsm
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
)
1702 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
1703 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1704 "sending 1/2 msg of Group Key Handshake");
1706 if (sm
->wpa
== WPA_VERSION_WPA2
) {
1707 kde_len
= 2 + RSN_SELECTOR_LEN
+ 2 + gsm
->GTK_len
+
1708 ieee80211w_kde_len(sm
);
1709 kde
= os_malloc(kde_len
);
1714 hdr
[0] = gsm
->GN
& 0x03;
1716 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
1717 gsm
->GTK
[gsm
->GN
- 1], gsm
->GTK_len
);
1718 pos
= ieee80211w_kde_add(sm
, pos
);
1720 kde
= gsm
->GTK
[gsm
->GN
- 1];
1721 pos
= kde
+ gsm
->GTK_len
;
1724 wpa_send_eapol(sm
->wpa_auth
, sm
,
1725 WPA_KEY_INFO_SECURE
| WPA_KEY_INFO_MIC
|
1727 (!sm
->Pair
? WPA_KEY_INFO_INSTALL
: 0),
1728 rsc
, gsm
->GNonce
, kde
, pos
- kde
, gsm
->GN
, 1);
1729 if (sm
->wpa
== WPA_VERSION_WPA2
)
1735 SM_STATE(WPA_PTK_GROUP
, REKEYESTABLISHED
)
1737 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYESTABLISHED
, wpa_ptk_group
);
1738 sm
->EAPOLKeyReceived
= FALSE
;
1739 if (sm
->GUpdateStationKeys
)
1740 sm
->group
->GKeyDoneStations
--;
1741 sm
->GUpdateStationKeys
= FALSE
;
1742 sm
->GTimeoutCtr
= 0;
1743 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
1744 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
1745 "group key handshake completed (%s)",
1746 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
1751 SM_STATE(WPA_PTK_GROUP
, KEYERROR
)
1753 SM_ENTRY_MA(WPA_PTK_GROUP
, KEYERROR
, wpa_ptk_group
);
1754 if (sm
->GUpdateStationKeys
)
1755 sm
->group
->GKeyDoneStations
--;
1756 sm
->GUpdateStationKeys
= FALSE
;
1757 sm
->Disconnect
= TRUE
;
1761 SM_STEP(WPA_PTK_GROUP
)
1764 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
1765 else switch (sm
->wpa_ptk_group_state
) {
1766 case WPA_PTK_GROUP_IDLE
:
1767 if (sm
->GUpdateStationKeys
||
1768 (sm
->wpa
== WPA_VERSION_WPA
&& sm
->PInitAKeys
))
1769 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
1771 case WPA_PTK_GROUP_REKEYNEGOTIATING
:
1772 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
1773 !sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
1774 SM_ENTER(WPA_PTK_GROUP
, REKEYESTABLISHED
);
1775 else if (sm
->GTimeoutCtr
>
1776 (int) dot11RSNAConfigGroupUpdateCount
)
1777 SM_ENTER(WPA_PTK_GROUP
, KEYERROR
);
1778 else if (sm
->TimeoutEvt
)
1779 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
1781 case WPA_PTK_GROUP_KEYERROR
:
1782 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
1784 case WPA_PTK_GROUP_REKEYESTABLISHED
:
1785 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
1791 static int wpa_gtk_update(struct wpa_authenticator
*wpa_auth
,
1792 struct wpa_group
*group
)
1796 /* FIX: is this the correct way of getting GNonce? */
1797 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
1798 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
1799 wpa_gmk_to_gtk(group
->GMK
, wpa_auth
->addr
, group
->GNonce
,
1800 group
->GTK
[group
->GN
- 1], group
->GTK_len
);
1802 #ifdef CONFIG_IEEE80211W
1803 if (wpa_auth
->conf
.ieee80211w
!= WPA_NO_IEEE80211W
) {
1804 if (os_get_random(group
->IGTK
[group
->GN_igtk
- 4],
1805 WPA_IGTK_LEN
) < 0) {
1806 wpa_printf(MSG_INFO
, "RSN: Failed to get new random "
1810 wpa_hexdump_key(MSG_DEBUG
, "IGTK",
1811 group
->IGTK
[group
->GN_igtk
- 4], WPA_IGTK_LEN
);
1813 #endif /* CONFIG_IEEE80211W */
1819 static void wpa_group_gtk_init(struct wpa_authenticator
*wpa_auth
,
1820 struct wpa_group
*group
)
1822 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
1823 "GTK_INIT (VLAN-ID %d)", group
->vlan_id
);
1824 group
->changed
= FALSE
; /* GInit is not cleared here; avoid loop */
1825 group
->wpa_group_state
= WPA_GROUP_GTK_INIT
;
1828 os_memset(group
->GTK
, 0, sizeof(group
->GTK
));
1831 #ifdef CONFIG_IEEE80211W
1834 #endif /* CONFIG_IEEE80211W */
1835 /* GTK[GN] = CalcGTK() */
1836 wpa_gtk_update(wpa_auth
, group
);
1840 static int wpa_group_update_sta(struct wpa_state_machine
*sm
, void *ctx
)
1842 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITDONE
) {
1843 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1844 "Not in PTKINITDONE; skip Group Key update");
1847 sm
->group
->GKeyDoneStations
++;
1848 sm
->GUpdateStationKeys
= TRUE
;
1854 static void wpa_group_setkeys(struct wpa_authenticator
*wpa_auth
,
1855 struct wpa_group
*group
)
1859 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
1860 "SETKEYS (VLAN-ID %d)", group
->vlan_id
);
1861 group
->changed
= TRUE
;
1862 group
->wpa_group_state
= WPA_GROUP_SETKEYS
;
1863 group
->GTKReKey
= FALSE
;
1865 group
->GM
= group
->GN
;
1867 #ifdef CONFIG_IEEE80211W
1868 tmp
= group
->GM_igtk
;
1869 group
->GM_igtk
= group
->GN_igtk
;
1870 group
->GN_igtk
= tmp
;
1871 #endif /* CONFIG_IEEE80211W */
1872 /* "GKeyDoneStations = GNoStations" is done in more robust way by
1873 * counting the STAs that are marked with GUpdateStationKeys instead of
1874 * including all STAs that could be in not-yet-completed state. */
1875 wpa_gtk_update(wpa_auth
, group
);
1877 wpa_auth_for_each_sta(wpa_auth
, wpa_group_update_sta
, NULL
);
1878 wpa_printf(MSG_DEBUG
, "wpa_group_setkeys: GKeyDoneStations=%d",
1879 group
->GKeyDoneStations
);
1883 static void wpa_group_setkeysdone(struct wpa_authenticator
*wpa_auth
,
1884 struct wpa_group
*group
)
1886 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
1887 "SETKEYSDONE (VLAN-ID %d)", group
->vlan_id
);
1888 group
->changed
= TRUE
;
1889 group
->wpa_group_state
= WPA_GROUP_SETKEYSDONE
;
1890 wpa_auth_set_key(wpa_auth
, group
->vlan_id
,
1891 wpa_alg_txt(wpa_auth
->conf
.wpa_group
),
1892 NULL
, group
->GN
, group
->GTK
[group
->GN
- 1],
1895 #ifdef CONFIG_IEEE80211W
1896 if (wpa_auth
->conf
.ieee80211w
!= WPA_NO_IEEE80211W
) {
1897 wpa_auth_set_key(wpa_auth
, group
->vlan_id
, "IGTK",
1898 NULL
, group
->GN_igtk
,
1899 group
->IGTK
[group
->GN_igtk
- 4],
1902 #endif /* CONFIG_IEEE80211W */
1906 static void wpa_group_sm_step(struct wpa_authenticator
*wpa_auth
,
1907 struct wpa_group
*group
)
1910 wpa_group_gtk_init(wpa_auth
, group
);
1911 } else if (group
->wpa_group_state
== WPA_GROUP_GTK_INIT
&&
1912 group
->GTKAuthenticator
) {
1913 wpa_group_setkeysdone(wpa_auth
, group
);
1914 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
&&
1916 wpa_group_setkeys(wpa_auth
, group
);
1917 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYS
) {
1918 if (group
->GKeyDoneStations
== 0)
1919 wpa_group_setkeysdone(wpa_auth
, group
);
1920 else if (group
->GTKReKey
)
1921 wpa_group_setkeys(wpa_auth
, group
);
1926 static void wpa_sm_step(struct wpa_state_machine
*sm
)
1931 if (sm
->in_step_loop
) {
1932 /* This should not happen, but if it does, make sure we do not
1933 * end up freeing the state machine too early by exiting the
1934 * recursive call. */
1935 wpa_printf(MSG_ERROR
, "WPA: wpa_sm_step() called recursively");
1939 sm
->in_step_loop
= 1;
1941 if (sm
->pending_deinit
)
1944 sm
->changed
= FALSE
;
1945 sm
->wpa_auth
->group
->changed
= FALSE
;
1947 SM_STEP_RUN(WPA_PTK
);
1948 if (sm
->pending_deinit
)
1950 SM_STEP_RUN(WPA_PTK_GROUP
);
1951 if (sm
->pending_deinit
)
1953 wpa_group_sm_step(sm
->wpa_auth
, sm
->group
);
1954 } while (sm
->changed
|| sm
->wpa_auth
->group
->changed
);
1955 sm
->in_step_loop
= 0;
1957 if (sm
->pending_deinit
) {
1958 wpa_printf(MSG_DEBUG
, "WPA: Completing pending STA state "
1959 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
1960 wpa_free_sta_sm(sm
);
1965 static void wpa_sm_call_step(void *eloop_ctx
, void *timeout_ctx
)
1967 struct wpa_state_machine
*sm
= eloop_ctx
;
1972 void wpa_auth_sm_notify(struct wpa_state_machine
*sm
)
1976 eloop_register_timeout(0, 0, wpa_sm_call_step
, sm
, NULL
);
1980 void wpa_gtk_rekey(struct wpa_authenticator
*wpa_auth
)
1983 struct wpa_group
*group
;
1985 if (wpa_auth
== NULL
)
1988 group
= wpa_auth
->group
;
1990 for (i
= 0; i
< 2; i
++) {
1992 group
->GM
= group
->GN
;
1994 #ifdef CONFIG_IEEE80211W
1995 tmp
= group
->GM_igtk
;
1996 group
->GM_igtk
= group
->GN_igtk
;
1997 group
->GN_igtk
= tmp
;
1998 #endif /* CONFIG_IEEE80211W */
1999 wpa_gtk_update(wpa_auth
, group
);
2004 static const char * wpa_bool_txt(int bool)
2006 return bool ? "TRUE" : "FALSE";
2010 static int wpa_cipher_bits(int cipher
)
2013 case WPA_CIPHER_CCMP
:
2015 case WPA_CIPHER_TKIP
:
2017 case WPA_CIPHER_WEP104
:
2019 case WPA_CIPHER_WEP40
:
2027 #define RSN_SUITE "%02x-%02x-%02x-%d"
2028 #define RSN_SUITE_ARG(s) \
2029 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2031 int wpa_get_mib(struct wpa_authenticator
*wpa_auth
, char *buf
, size_t buflen
)
2034 char pmkid_txt
[PMKID_LEN
* 2 + 1];
2036 if (wpa_auth
== NULL
)
2039 ret
= os_snprintf(buf
+ len
, buflen
- len
,
2040 "dot11RSNAOptionImplemented=TRUE\n"
2041 #ifdef CONFIG_RSN_PREAUTH
2042 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2043 #else /* CONFIG_RSN_PREAUTH */
2044 "dot11RSNAPreauthenticationImplemented=FALSE\n"
2045 #endif /* CONFIG_RSN_PREAUTH */
2046 "dot11RSNAEnabled=%s\n"
2047 "dot11RSNAPreauthenticationEnabled=%s\n",
2048 wpa_bool_txt(wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
),
2049 wpa_bool_txt(wpa_auth
->conf
.rsn_preauth
));
2050 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
2054 wpa_snprintf_hex(pmkid_txt
, sizeof(pmkid_txt
),
2055 wpa_auth
->dot11RSNAPMKIDUsed
, PMKID_LEN
);
2058 buf
+ len
, buflen
- len
,
2059 "dot11RSNAConfigVersion=%u\n"
2060 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
2061 /* FIX: dot11RSNAConfigGroupCipher */
2062 /* FIX: dot11RSNAConfigGroupRekeyMethod */
2063 /* FIX: dot11RSNAConfigGroupRekeyTime */
2064 /* FIX: dot11RSNAConfigGroupRekeyPackets */
2065 "dot11RSNAConfigGroupRekeyStrict=%u\n"
2066 "dot11RSNAConfigGroupUpdateCount=%u\n"
2067 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
2068 "dot11RSNAConfigGroupCipherSize=%u\n"
2069 "dot11RSNAConfigPMKLifetime=%u\n"
2070 "dot11RSNAConfigPMKReauthThreshold=%u\n"
2071 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
2072 "dot11RSNAConfigSATimeout=%u\n"
2073 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE
"\n"
2074 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE
"\n"
2075 "dot11RSNAGroupCipherSelected=" RSN_SUITE
"\n"
2076 "dot11RSNAPMKIDUsed=%s\n"
2077 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE
"\n"
2078 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE
"\n"
2079 "dot11RSNAGroupCipherRequested=" RSN_SUITE
"\n"
2080 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
2081 "dot11RSNA4WayHandshakeFailures=%u\n"
2082 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
2084 !!wpa_auth
->conf
.wpa_strict_rekey
,
2085 dot11RSNAConfigGroupUpdateCount
,
2086 dot11RSNAConfigPairwiseUpdateCount
,
2087 wpa_cipher_bits(wpa_auth
->conf
.wpa_group
),
2088 dot11RSNAConfigPMKLifetime
,
2089 dot11RSNAConfigPMKReauthThreshold
,
2090 dot11RSNAConfigSATimeout
,
2091 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteSelected
),
2092 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherSelected
),
2093 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherSelected
),
2095 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteRequested
),
2096 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherRequested
),
2097 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherRequested
),
2098 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
,
2099 wpa_auth
->dot11RSNA4WayHandshakeFailures
);
2100 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
2104 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
2105 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
2108 ret
= os_snprintf(buf
+ len
, buflen
- len
, "hostapdWPAGroupState=%d\n",
2109 wpa_auth
->group
->wpa_group_state
);
2110 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
2118 int wpa_get_mib_sta(struct wpa_state_machine
*sm
, char *buf
, size_t buflen
)
2126 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
2128 /* dot11RSNAStatsEntry */
2130 if (sm
->wpa
== WPA_VERSION_WPA
) {
2131 if (sm
->pairwise
== WPA_CIPHER_CCMP
)
2132 pairwise
= WPA_CIPHER_SUITE_CCMP
;
2133 else if (sm
->pairwise
== WPA_CIPHER_TKIP
)
2134 pairwise
= WPA_CIPHER_SUITE_TKIP
;
2135 else if (sm
->pairwise
== WPA_CIPHER_WEP104
)
2136 pairwise
= WPA_CIPHER_SUITE_WEP104
;
2137 else if (sm
->pairwise
== WPA_CIPHER_WEP40
)
2138 pairwise
= WPA_CIPHER_SUITE_WEP40
;
2139 else if (sm
->pairwise
== WPA_CIPHER_NONE
)
2140 pairwise
= WPA_CIPHER_SUITE_NONE
;
2141 } else if (sm
->wpa
== WPA_VERSION_WPA2
) {
2142 if (sm
->pairwise
== WPA_CIPHER_CCMP
)
2143 pairwise
= RSN_CIPHER_SUITE_CCMP
;
2144 else if (sm
->pairwise
== WPA_CIPHER_TKIP
)
2145 pairwise
= RSN_CIPHER_SUITE_TKIP
;
2146 else if (sm
->pairwise
== WPA_CIPHER_WEP104
)
2147 pairwise
= RSN_CIPHER_SUITE_WEP104
;
2148 else if (sm
->pairwise
== WPA_CIPHER_WEP40
)
2149 pairwise
= RSN_CIPHER_SUITE_WEP40
;
2150 else if (sm
->pairwise
== WPA_CIPHER_NONE
)
2151 pairwise
= RSN_CIPHER_SUITE_NONE
;
2156 buf
+ len
, buflen
- len
,
2157 /* TODO: dot11RSNAStatsIndex */
2158 "dot11RSNAStatsSTAAddress=" MACSTR
"\n"
2159 "dot11RSNAStatsVersion=1\n"
2160 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE
"\n"
2161 /* TODO: dot11RSNAStatsTKIPICVErrors */
2162 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
2163 "dot11RSNAStatsTKIPRemoveMICFailures=%u\n"
2164 /* TODO: dot11RSNAStatsCCMPReplays */
2165 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
2166 /* TODO: dot11RSNAStatsTKIPReplays */,
2168 RSN_SUITE_ARG(pairwise
),
2169 sm
->dot11RSNAStatsTKIPLocalMICFailures
,
2170 sm
->dot11RSNAStatsTKIPRemoteMICFailures
);
2171 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
2176 ret
= os_snprintf(buf
+ len
, buflen
- len
,
2177 "hostapdWPAPTKState=%d\n"
2178 "hostapdWPAPTKGroupState=%d\n",
2180 sm
->wpa_ptk_group_state
);
2181 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
2189 void wpa_auth_countermeasures_start(struct wpa_authenticator
*wpa_auth
)
2192 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
++;
2196 int wpa_auth_pairwise_set(struct wpa_state_machine
*sm
)
2198 return sm
&& sm
->pairwise_set
;
2202 int wpa_auth_sta_key_mgmt(struct wpa_state_machine
*sm
)
2206 return sm
->wpa_key_mgmt
;
2210 int wpa_auth_sta_wpa_version(struct wpa_state_machine
*sm
)
2218 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine
*sm
,
2219 struct rsn_pmksa_cache_entry
*entry
)
2221 if (sm
== NULL
|| sm
->pmksa
!= entry
)
2228 struct rsn_pmksa_cache_entry
*
2229 wpa_auth_sta_get_pmksa(struct wpa_state_machine
*sm
)
2231 return sm
? sm
->pmksa
: NULL
;
2235 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine
*sm
)
2238 sm
->dot11RSNAStatsTKIPLocalMICFailures
++;
2242 const u8
* wpa_auth_get_wpa_ie(struct wpa_authenticator
*wpa_auth
, size_t *len
)
2244 if (wpa_auth
== NULL
)
2246 *len
= wpa_auth
->wpa_ie_len
;
2247 return wpa_auth
->wpa_ie
;
2251 int wpa_auth_pmksa_add(struct wpa_state_machine
*sm
, const u8
*pmk
,
2252 int session_timeout
, struct eapol_state_machine
*eapol
)
2254 if (sm
== NULL
|| sm
->wpa
!= WPA_VERSION_WPA2
)
2257 if (pmksa_cache_add(sm
->wpa_auth
->pmksa
, pmk
, PMK_LEN
,
2258 sm
->wpa_auth
->addr
, sm
->addr
, session_timeout
,
2259 eapol
, sm
->wpa_key_mgmt
))
2266 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator
*wpa_auth
,
2267 const u8
*pmk
, size_t len
, const u8
*sta_addr
,
2268 int session_timeout
,
2269 struct eapol_state_machine
*eapol
)
2271 if (wpa_auth
== NULL
)
2274 if (pmksa_cache_add(wpa_auth
->pmksa
, pmk
, len
, wpa_auth
->addr
,
2275 sta_addr
, session_timeout
, eapol
,
2276 WPA_KEY_MGMT_IEEE8021X
))
2283 static struct wpa_group
*
2284 wpa_auth_add_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
2286 struct wpa_group
*group
;
2288 if (wpa_auth
== NULL
|| wpa_auth
->group
== NULL
)
2291 wpa_printf(MSG_DEBUG
, "WPA: Add group state machine for VLAN-ID %d",
2293 group
= wpa_group_init(wpa_auth
, vlan_id
);
2297 group
->next
= wpa_auth
->group
->next
;
2298 wpa_auth
->group
->next
= group
;
2304 int wpa_auth_sta_set_vlan(struct wpa_state_machine
*sm
, int vlan_id
)
2306 struct wpa_group
*group
;
2308 if (sm
== NULL
|| sm
->wpa_auth
== NULL
)
2311 group
= sm
->wpa_auth
->group
;
2313 if (group
->vlan_id
== vlan_id
)
2315 group
= group
->next
;
2318 if (group
== NULL
) {
2319 group
= wpa_auth_add_group(sm
->wpa_auth
, vlan_id
);
2324 if (sm
->group
== group
)
2327 wpa_printf(MSG_DEBUG
, "WPA: Moving STA " MACSTR
" to use group state "
2328 "machine for VLAN ID %d", MAC2STR(sm
->addr
), vlan_id
);
2334 #endif /* CONFIG_NATIVE_WINDOWS */