2 * hostapd - IEEE 802.11i-2004 / WPA Authenticator
3 * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
17 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "utils/state_machine.h"
20 #include "common/ieee802_11_defs.h"
21 #include "crypto/aes_wrap.h"
22 #include "crypto/crypto.h"
23 #include "crypto/sha1.h"
24 #include "crypto/sha256.h"
25 #include "eapol_auth/eapol_auth_sm.h"
26 #include "ap_config.h"
27 #include "ieee802_11.h"
29 #include "pmksa_cache_auth.h"
30 #include "wpa_auth_i.h"
31 #include "wpa_auth_ie.h"
33 #define STATE_MACHINE_DATA struct wpa_state_machine
34 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
35 #define STATE_MACHINE_ADDR sm->addr
38 static void wpa_send_eapol_timeout(void *eloop_ctx
, void *timeout_ctx
);
39 static int wpa_sm_step(struct wpa_state_machine
*sm
);
40 static int wpa_verify_key_mic(struct wpa_ptk
*PTK
, u8
*data
, size_t data_len
);
41 static void wpa_sm_call_step(void *eloop_ctx
, void *timeout_ctx
);
42 static void wpa_group_sm_step(struct wpa_authenticator
*wpa_auth
,
43 struct wpa_group
*group
);
44 static void wpa_request_new_ptk(struct wpa_state_machine
*sm
);
45 static int wpa_gtk_update(struct wpa_authenticator
*wpa_auth
,
46 struct wpa_group
*group
);
48 static const u32 dot11RSNAConfigGroupUpdateCount
= 4;
49 static const u32 dot11RSNAConfigPairwiseUpdateCount
= 4;
50 static const u32 eapol_key_timeout_first
= 100; /* ms */
51 static const u32 eapol_key_timeout_subseq
= 1000; /* ms */
53 /* TODO: make these configurable */
54 static const int dot11RSNAConfigPMKLifetime
= 43200;
55 static const int dot11RSNAConfigPMKReauthThreshold
= 70;
56 static const int dot11RSNAConfigSATimeout
= 60;
59 static inline void wpa_auth_mic_failure_report(
60 struct wpa_authenticator
*wpa_auth
, const u8
*addr
)
62 if (wpa_auth
->cb
.mic_failure_report
)
63 wpa_auth
->cb
.mic_failure_report(wpa_auth
->cb
.ctx
, addr
);
67 static inline void wpa_auth_set_eapol(struct wpa_authenticator
*wpa_auth
,
68 const u8
*addr
, wpa_eapol_variable var
,
71 if (wpa_auth
->cb
.set_eapol
)
72 wpa_auth
->cb
.set_eapol(wpa_auth
->cb
.ctx
, addr
, var
, value
);
76 static inline int wpa_auth_get_eapol(struct wpa_authenticator
*wpa_auth
,
77 const u8
*addr
, wpa_eapol_variable var
)
79 if (wpa_auth
->cb
.get_eapol
== NULL
)
81 return wpa_auth
->cb
.get_eapol(wpa_auth
->cb
.ctx
, addr
, var
);
85 static inline const u8
* wpa_auth_get_psk(struct wpa_authenticator
*wpa_auth
,
86 const u8
*addr
, const u8
*prev_psk
)
88 if (wpa_auth
->cb
.get_psk
== NULL
)
90 return wpa_auth
->cb
.get_psk(wpa_auth
->cb
.ctx
, addr
, prev_psk
);
94 static inline int wpa_auth_get_msk(struct wpa_authenticator
*wpa_auth
,
95 const u8
*addr
, u8
*msk
, size_t *len
)
97 if (wpa_auth
->cb
.get_msk
== NULL
)
99 return wpa_auth
->cb
.get_msk(wpa_auth
->cb
.ctx
, addr
, msk
, len
);
103 static inline int wpa_auth_set_key(struct wpa_authenticator
*wpa_auth
,
105 enum wpa_alg alg
, const u8
*addr
, int idx
,
106 u8
*key
, size_t key_len
)
108 if (wpa_auth
->cb
.set_key
== NULL
)
110 return wpa_auth
->cb
.set_key(wpa_auth
->cb
.ctx
, vlan_id
, alg
, addr
, idx
,
115 static inline int wpa_auth_get_seqnum(struct wpa_authenticator
*wpa_auth
,
116 const u8
*addr
, int idx
, u8
*seq
)
118 if (wpa_auth
->cb
.get_seqnum
== NULL
)
120 return wpa_auth
->cb
.get_seqnum(wpa_auth
->cb
.ctx
, addr
, idx
, seq
);
125 wpa_auth_send_eapol(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
126 const u8
*data
, size_t data_len
, int encrypt
)
128 if (wpa_auth
->cb
.send_eapol
== NULL
)
130 return wpa_auth
->cb
.send_eapol(wpa_auth
->cb
.ctx
, addr
, data
, data_len
,
135 int wpa_auth_for_each_sta(struct wpa_authenticator
*wpa_auth
,
136 int (*cb
)(struct wpa_state_machine
*sm
, void *ctx
),
139 if (wpa_auth
->cb
.for_each_sta
== NULL
)
141 return wpa_auth
->cb
.for_each_sta(wpa_auth
->cb
.ctx
, cb
, cb_ctx
);
145 int wpa_auth_for_each_auth(struct wpa_authenticator
*wpa_auth
,
146 int (*cb
)(struct wpa_authenticator
*a
, void *ctx
),
149 if (wpa_auth
->cb
.for_each_auth
== NULL
)
151 return wpa_auth
->cb
.for_each_auth(wpa_auth
->cb
.ctx
, cb
, cb_ctx
);
155 void wpa_auth_logger(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
156 logger_level level
, const char *txt
)
158 if (wpa_auth
->cb
.logger
== NULL
)
160 wpa_auth
->cb
.logger(wpa_auth
->cb
.ctx
, addr
, level
, txt
);
164 void wpa_auth_vlogger(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
165 logger_level level
, const char *fmt
, ...)
171 if (wpa_auth
->cb
.logger
== NULL
)
174 maxlen
= os_strlen(fmt
) + 100;
175 format
= os_malloc(maxlen
);
180 vsnprintf(format
, maxlen
, fmt
, ap
);
183 wpa_auth_logger(wpa_auth
, addr
, level
, format
);
189 static void wpa_sta_disconnect(struct wpa_authenticator
*wpa_auth
,
192 if (wpa_auth
->cb
.disconnect
== NULL
)
194 wpa_auth
->cb
.disconnect(wpa_auth
->cb
.ctx
, addr
,
195 WLAN_REASON_PREV_AUTH_NOT_VALID
);
199 static int wpa_use_aes_cmac(struct wpa_state_machine
*sm
)
202 #ifdef CONFIG_IEEE80211R
203 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
205 #endif /* CONFIG_IEEE80211R */
206 #ifdef CONFIG_IEEE80211W
207 if (wpa_key_mgmt_sha256(sm
->wpa_key_mgmt
))
209 #endif /* CONFIG_IEEE80211W */
214 static void wpa_rekey_gmk(void *eloop_ctx
, void *timeout_ctx
)
216 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
218 if (os_get_random(wpa_auth
->group
->GMK
, WPA_GMK_LEN
)) {
219 wpa_printf(MSG_ERROR
, "Failed to get random data for WPA "
222 wpa_auth_logger(wpa_auth
, NULL
, LOGGER_DEBUG
, "GMK rekeyd");
225 if (wpa_auth
->conf
.wpa_gmk_rekey
) {
226 eloop_register_timeout(wpa_auth
->conf
.wpa_gmk_rekey
, 0,
227 wpa_rekey_gmk
, wpa_auth
, NULL
);
232 static void wpa_rekey_gtk(void *eloop_ctx
, void *timeout_ctx
)
234 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
235 struct wpa_group
*group
;
237 wpa_auth_logger(wpa_auth
, NULL
, LOGGER_DEBUG
, "rekeying GTK");
238 for (group
= wpa_auth
->group
; group
; group
= group
->next
) {
239 group
->GTKReKey
= TRUE
;
241 group
->changed
= FALSE
;
242 wpa_group_sm_step(wpa_auth
, group
);
243 } while (group
->changed
);
246 if (wpa_auth
->conf
.wpa_group_rekey
) {
247 eloop_register_timeout(wpa_auth
->conf
.wpa_group_rekey
,
248 0, wpa_rekey_gtk
, wpa_auth
, NULL
);
253 static void wpa_rekey_ptk(void *eloop_ctx
, void *timeout_ctx
)
255 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
256 struct wpa_state_machine
*sm
= timeout_ctx
;
258 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
, "rekeying PTK");
259 wpa_request_new_ptk(sm
);
264 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine
*sm
, void *ctx
)
266 if (sm
->pmksa
== ctx
)
272 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry
*entry
,
275 struct wpa_authenticator
*wpa_auth
= ctx
;
276 wpa_auth_for_each_sta(wpa_auth
, wpa_auth_pmksa_clear_cb
, entry
);
280 static void wpa_group_set_key_len(struct wpa_group
*group
, int cipher
)
283 case WPA_CIPHER_CCMP
:
286 case WPA_CIPHER_TKIP
:
289 case WPA_CIPHER_WEP104
:
292 case WPA_CIPHER_WEP40
:
299 static struct wpa_group
* wpa_group_init(struct wpa_authenticator
*wpa_auth
,
302 struct wpa_group
*group
;
303 u8 buf
[ETH_ALEN
+ 8 + sizeof(group
)];
306 group
= os_zalloc(sizeof(struct wpa_group
));
310 group
->GTKAuthenticator
= TRUE
;
311 group
->vlan_id
= vlan_id
;
313 wpa_group_set_key_len(group
, wpa_auth
->conf
.wpa_group
);
315 /* Counter = PRF-256(Random number, "Init Counter",
316 * Local MAC Address || Time)
318 os_memcpy(buf
, wpa_auth
->addr
, ETH_ALEN
);
319 wpa_get_ntp_timestamp(buf
+ ETH_ALEN
);
320 os_memcpy(buf
+ ETH_ALEN
+ 8, &group
, sizeof(group
));
321 if (os_get_random(rkey
, sizeof(rkey
)) ||
322 os_get_random(group
->GMK
, WPA_GMK_LEN
)) {
323 wpa_printf(MSG_ERROR
, "Failed to get random data for WPA "
329 sha1_prf(rkey
, sizeof(rkey
), "Init Counter", buf
, sizeof(buf
),
330 group
->Counter
, WPA_NONCE_LEN
);
333 wpa_group_sm_step(wpa_auth
, group
);
334 group
->GInit
= FALSE
;
335 wpa_group_sm_step(wpa_auth
, group
);
342 * wpa_init - Initialize WPA authenticator
343 * @addr: Authenticator address
344 * @conf: Configuration for WPA authenticator
345 * @cb: Callback functions for WPA authenticator
346 * Returns: Pointer to WPA authenticator data or %NULL on failure
348 struct wpa_authenticator
* wpa_init(const u8
*addr
,
349 struct wpa_auth_config
*conf
,
350 struct wpa_auth_callbacks
*cb
)
352 struct wpa_authenticator
*wpa_auth
;
354 wpa_auth
= os_zalloc(sizeof(struct wpa_authenticator
));
355 if (wpa_auth
== NULL
)
357 os_memcpy(wpa_auth
->addr
, addr
, ETH_ALEN
);
358 os_memcpy(&wpa_auth
->conf
, conf
, sizeof(*conf
));
359 os_memcpy(&wpa_auth
->cb
, cb
, sizeof(*cb
));
361 if (wpa_auth_gen_wpa_ie(wpa_auth
)) {
362 wpa_printf(MSG_ERROR
, "Could not generate WPA IE.");
367 wpa_auth
->group
= wpa_group_init(wpa_auth
, 0);
368 if (wpa_auth
->group
== NULL
) {
369 os_free(wpa_auth
->wpa_ie
);
374 wpa_auth
->pmksa
= pmksa_cache_auth_init(wpa_auth_pmksa_free_cb
,
376 if (wpa_auth
->pmksa
== NULL
) {
377 wpa_printf(MSG_ERROR
, "PMKSA cache initialization failed.");
378 os_free(wpa_auth
->wpa_ie
);
383 #ifdef CONFIG_IEEE80211R
384 wpa_auth
->ft_pmk_cache
= wpa_ft_pmk_cache_init();
385 if (wpa_auth
->ft_pmk_cache
== NULL
) {
386 wpa_printf(MSG_ERROR
, "FT PMK cache initialization failed.");
387 os_free(wpa_auth
->wpa_ie
);
388 pmksa_cache_auth_deinit(wpa_auth
->pmksa
);
392 #endif /* CONFIG_IEEE80211R */
394 if (wpa_auth
->conf
.wpa_gmk_rekey
) {
395 eloop_register_timeout(wpa_auth
->conf
.wpa_gmk_rekey
, 0,
396 wpa_rekey_gmk
, wpa_auth
, NULL
);
399 if (wpa_auth
->conf
.wpa_group_rekey
) {
400 eloop_register_timeout(wpa_auth
->conf
.wpa_group_rekey
, 0,
401 wpa_rekey_gtk
, wpa_auth
, NULL
);
409 * wpa_deinit - Deinitialize WPA authenticator
410 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
412 void wpa_deinit(struct wpa_authenticator
*wpa_auth
)
414 struct wpa_group
*group
, *prev
;
416 eloop_cancel_timeout(wpa_rekey_gmk
, wpa_auth
, NULL
);
417 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
419 #ifdef CONFIG_PEERKEY
420 while (wpa_auth
->stsl_negotiations
)
421 wpa_stsl_remove(wpa_auth
, wpa_auth
->stsl_negotiations
);
422 #endif /* CONFIG_PEERKEY */
424 pmksa_cache_auth_deinit(wpa_auth
->pmksa
);
426 #ifdef CONFIG_IEEE80211R
427 wpa_ft_pmk_cache_deinit(wpa_auth
->ft_pmk_cache
);
428 wpa_auth
->ft_pmk_cache
= NULL
;
429 #endif /* CONFIG_IEEE80211R */
431 os_free(wpa_auth
->wpa_ie
);
433 group
= wpa_auth
->group
;
445 * wpa_reconfig - Update WPA authenticator configuration
446 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
447 * @conf: Configuration for WPA authenticator
449 int wpa_reconfig(struct wpa_authenticator
*wpa_auth
,
450 struct wpa_auth_config
*conf
)
452 struct wpa_group
*group
;
453 if (wpa_auth
== NULL
)
456 os_memcpy(&wpa_auth
->conf
, conf
, sizeof(*conf
));
457 if (wpa_auth_gen_wpa_ie(wpa_auth
)) {
458 wpa_printf(MSG_ERROR
, "Could not generate WPA IE.");
463 * Reinitialize GTK to make sure it is suitable for the new
466 group
= wpa_auth
->group
;
467 wpa_group_set_key_len(group
, wpa_auth
->conf
.wpa_group
);
469 wpa_group_sm_step(wpa_auth
, group
);
470 group
->GInit
= FALSE
;
471 wpa_group_sm_step(wpa_auth
, group
);
477 struct wpa_state_machine
*
478 wpa_auth_sta_init(struct wpa_authenticator
*wpa_auth
, const u8
*addr
)
480 struct wpa_state_machine
*sm
;
482 sm
= os_zalloc(sizeof(struct wpa_state_machine
));
485 os_memcpy(sm
->addr
, addr
, ETH_ALEN
);
487 sm
->wpa_auth
= wpa_auth
;
488 sm
->group
= wpa_auth
->group
;
494 int wpa_auth_sta_associated(struct wpa_authenticator
*wpa_auth
,
495 struct wpa_state_machine
*sm
)
497 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
500 #ifdef CONFIG_IEEE80211R
501 if (sm
->ft_completed
) {
502 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
503 "FT authentication already completed - do not "
504 "start 4-way handshake");
507 #endif /* CONFIG_IEEE80211R */
510 os_memset(&sm
->key_replay
, 0, sizeof(sm
->key_replay
));
511 sm
->ReAuthenticationRequest
= TRUE
;
512 return wpa_sm_step(sm
);
515 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
516 "start authentication");
520 if (wpa_sm_step(sm
) == 1)
521 return 1; /* should not really happen */
523 sm
->AuthenticationRequest
= TRUE
;
524 return wpa_sm_step(sm
);
528 void wpa_auth_sta_no_wpa(struct wpa_state_machine
*sm
)
530 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
531 * reassociates back to the same AP while the previous entry for the
532 * STA has not yet been removed. */
536 sm
->wpa_key_mgmt
= 0;
540 static void wpa_free_sta_sm(struct wpa_state_machine
*sm
)
542 os_free(sm
->last_rx_eapol_key
);
548 void wpa_auth_sta_deinit(struct wpa_state_machine
*sm
)
553 if (sm
->wpa_auth
->conf
.wpa_strict_rekey
&& sm
->has_GTK
) {
554 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
555 "strict rekeying - force GTK rekey since STA "
557 eloop_cancel_timeout(wpa_rekey_gtk
, sm
->wpa_auth
, NULL
);
558 eloop_register_timeout(0, 500000, wpa_rekey_gtk
, sm
->wpa_auth
,
562 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
563 eloop_cancel_timeout(wpa_sm_call_step
, sm
, NULL
);
564 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
565 if (sm
->in_step_loop
) {
566 /* Must not free state machine while wpa_sm_step() is running.
567 * Freeing will be completed in the end of wpa_sm_step(). */
568 wpa_printf(MSG_DEBUG
, "WPA: Registering pending STA state "
569 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
570 sm
->pending_deinit
= 1;
576 static void wpa_request_new_ptk(struct wpa_state_machine
*sm
)
581 sm
->PTKRequest
= TRUE
;
586 static int wpa_replay_counter_valid(struct wpa_state_machine
*sm
,
587 const u8
*replay_counter
)
590 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
591 if (!sm
->key_replay
[i
].valid
)
593 if (os_memcmp(replay_counter
, sm
->key_replay
[i
].counter
,
594 WPA_REPLAY_COUNTER_LEN
) == 0)
601 void wpa_receive(struct wpa_authenticator
*wpa_auth
,
602 struct wpa_state_machine
*sm
,
603 u8
*data
, size_t data_len
)
605 struct ieee802_1x_hdr
*hdr
;
606 struct wpa_eapol_key
*key
;
607 u16 key_info
, key_data_length
;
608 enum { PAIRWISE_2
, PAIRWISE_4
, GROUP_2
, REQUEST
,
609 SMK_M1
, SMK_M3
, SMK_ERROR
} msg
;
611 struct wpa_eapol_ie_parse kde
;
613 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
616 if (data_len
< sizeof(*hdr
) + sizeof(*key
))
619 hdr
= (struct ieee802_1x_hdr
*) data
;
620 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
621 key_info
= WPA_GET_BE16(key
->key_info
);
622 key_data_length
= WPA_GET_BE16(key
->key_data_length
);
623 if (key_data_length
> data_len
- sizeof(*hdr
) - sizeof(*key
)) {
624 wpa_printf(MSG_INFO
, "WPA: Invalid EAPOL-Key frame - "
625 "key_data overflow (%d > %lu)",
627 (unsigned long) (data_len
- sizeof(*hdr
) -
632 if (sm
->wpa
== WPA_VERSION_WPA2
) {
633 if (key
->type
!= EAPOL_KEY_TYPE_RSN
) {
634 wpa_printf(MSG_DEBUG
, "Ignore EAPOL-Key with "
635 "unexpected type %d in RSN mode",
640 if (key
->type
!= EAPOL_KEY_TYPE_WPA
) {
641 wpa_printf(MSG_DEBUG
, "Ignore EAPOL-Key with "
642 "unexpected type %d in WPA mode",
648 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
651 if ((key_info
& (WPA_KEY_INFO_SMK_MESSAGE
| WPA_KEY_INFO_REQUEST
)) ==
652 (WPA_KEY_INFO_SMK_MESSAGE
| WPA_KEY_INFO_REQUEST
)) {
653 if (key_info
& WPA_KEY_INFO_ERROR
) {
655 msgtxt
= "SMK Error";
660 } else if (key_info
& WPA_KEY_INFO_SMK_MESSAGE
) {
663 } else if (key_info
& WPA_KEY_INFO_REQUEST
) {
666 } else if (!(key_info
& WPA_KEY_INFO_KEY_TYPE
)) {
668 msgtxt
= "2/2 Group";
669 } else if (key_data_length
== 0) {
671 msgtxt
= "4/4 Pairwise";
674 msgtxt
= "2/4 Pairwise";
677 /* TODO: key_info type validation for PeerKey */
678 if (msg
== REQUEST
|| msg
== PAIRWISE_2
|| msg
== PAIRWISE_4
||
680 u16 ver
= key_info
& WPA_KEY_INFO_TYPE_MASK
;
681 if (sm
->pairwise
== WPA_CIPHER_CCMP
) {
682 if (wpa_use_aes_cmac(sm
) &&
683 ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
684 wpa_auth_logger(wpa_auth
, sm
->addr
,
686 "advertised support for "
687 "AES-128-CMAC, but did not "
692 if (!wpa_use_aes_cmac(sm
) &&
693 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
694 wpa_auth_logger(wpa_auth
, sm
->addr
,
696 "did not use HMAC-SHA1-AES "
703 if (key_info
& WPA_KEY_INFO_REQUEST
) {
704 if (sm
->req_replay_counter_used
&&
705 os_memcmp(key
->replay_counter
, sm
->req_replay_counter
,
706 WPA_REPLAY_COUNTER_LEN
) <= 0) {
707 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_WARNING
,
708 "received EAPOL-Key request with "
714 if (!(key_info
& WPA_KEY_INFO_REQUEST
) &&
715 !wpa_replay_counter_valid(sm
, key
->replay_counter
)) {
717 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
718 "received EAPOL-Key %s with unexpected "
719 "replay counter", msgtxt
);
720 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
721 if (!sm
->key_replay
[i
].valid
)
723 wpa_hexdump(MSG_DEBUG
, "pending replay counter",
724 sm
->key_replay
[i
].counter
,
725 WPA_REPLAY_COUNTER_LEN
);
727 wpa_hexdump(MSG_DEBUG
, "received replay counter",
728 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
734 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKSTART
&&
735 sm
->wpa_ptk_state
!= WPA_PTK_PTKCALCNEGOTIATING
) {
736 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
737 "received EAPOL-Key msg 2/4 in "
738 "invalid state (%d) - dropped",
742 if (sm
->wpa_ie
== NULL
||
743 sm
->wpa_ie_len
!= key_data_length
||
744 os_memcmp(sm
->wpa_ie
, key
+ 1, key_data_length
) != 0) {
745 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
746 "WPA IE from (Re)AssocReq did not "
747 "match with msg 2/4");
749 wpa_hexdump(MSG_DEBUG
, "WPA IE in AssocReq",
750 sm
->wpa_ie
, sm
->wpa_ie_len
);
752 wpa_hexdump(MSG_DEBUG
, "WPA IE in msg 2/4",
753 (u8
*) (key
+ 1), key_data_length
);
754 /* MLME-DEAUTHENTICATE.request */
755 wpa_sta_disconnect(wpa_auth
, sm
->addr
);
760 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITNEGOTIATING
||
762 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
763 "received EAPOL-Key msg 4/4 in "
764 "invalid state (%d) - dropped",
770 if (sm
->wpa_ptk_group_state
!= WPA_PTK_GROUP_REKEYNEGOTIATING
772 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
773 "received EAPOL-Key msg 2/2 in "
774 "invalid state (%d) - dropped",
775 sm
->wpa_ptk_group_state
);
779 #ifdef CONFIG_PEERKEY
783 if (!wpa_auth
->conf
.peerkey
) {
784 wpa_printf(MSG_DEBUG
, "RSN: SMK M1/M3/Error, but "
785 "PeerKey use disabled - ignoring message");
788 if (!sm
->PTK_valid
) {
789 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
790 "received EAPOL-Key msg SMK in "
791 "invalid state - dropped");
795 #else /* CONFIG_PEERKEY */
799 return; /* STSL disabled - ignore SMK messages */
800 #endif /* CONFIG_PEERKEY */
805 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
806 "received EAPOL-Key frame (%s)", msgtxt
);
808 if (key_info
& WPA_KEY_INFO_ACK
) {
809 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
810 "received invalid EAPOL-Key: Key Ack set");
814 if (!(key_info
& WPA_KEY_INFO_MIC
)) {
815 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
816 "received invalid EAPOL-Key: Key MIC not set");
820 sm
->MICVerified
= FALSE
;
822 if (wpa_verify_key_mic(&sm
->PTK
, data
, data_len
)) {
823 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
824 "received EAPOL-Key with invalid MIC");
827 sm
->MICVerified
= TRUE
;
828 eloop_cancel_timeout(wpa_send_eapol_timeout
, wpa_auth
, sm
);
831 if (key_info
& WPA_KEY_INFO_REQUEST
) {
832 if (sm
->MICVerified
) {
833 sm
->req_replay_counter_used
= 1;
834 os_memcpy(sm
->req_replay_counter
, key
->replay_counter
,
835 WPA_REPLAY_COUNTER_LEN
);
837 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
838 "received EAPOL-Key request with "
844 * TODO: should decrypt key data field if encryption was used;
845 * even though MAC address KDE is not normally encrypted,
846 * supplicant is allowed to encrypt it.
848 if (msg
== SMK_ERROR
) {
849 #ifdef CONFIG_PEERKEY
850 wpa_smk_error(wpa_auth
, sm
, key
);
851 #endif /* CONFIG_PEERKEY */
853 } else if (key_info
& WPA_KEY_INFO_ERROR
) {
854 /* Supplicant reported a Michael MIC error */
855 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
856 "received EAPOL-Key Error Request "
857 "(STA detected Michael MIC failure)");
858 wpa_auth_mic_failure_report(wpa_auth
, sm
->addr
);
859 sm
->dot11RSNAStatsTKIPRemoteMICFailures
++;
860 wpa_auth
->dot11RSNAStatsTKIPRemoteMICFailures
++;
861 /* Error report is not a request for a new key
862 * handshake, but since Authenticator may do it, let's
863 * change the keys now anyway. */
864 wpa_request_new_ptk(sm
);
865 } else if (key_info
& WPA_KEY_INFO_KEY_TYPE
) {
866 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
867 "received EAPOL-Key Request for new "
869 wpa_request_new_ptk(sm
);
870 #ifdef CONFIG_PEERKEY
871 } else if (msg
== SMK_M1
) {
872 wpa_smk_m1(wpa_auth
, sm
, key
);
873 #endif /* CONFIG_PEERKEY */
874 } else if (key_data_length
> 0 &&
875 wpa_parse_kde_ies((const u8
*) (key
+ 1),
876 key_data_length
, &kde
) == 0 &&
879 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
880 "received EAPOL-Key Request for GTK "
882 /* FIX: why was this triggering PTK rekeying for the
883 * STA that requested Group Key rekeying?? */
884 /* wpa_request_new_ptk(sta->wpa_sm); */
885 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
886 wpa_rekey_gtk(wpa_auth
, NULL
);
889 /* Do not allow the same key replay counter to be reused. This
890 * does also invalidate all other pending replay counters if
891 * retransmissions were used, i.e., we will only process one of
892 * the pending replies and ignore rest if more than one is
894 sm
->key_replay
[0].valid
= FALSE
;
897 #ifdef CONFIG_PEERKEY
899 wpa_smk_m3(wpa_auth
, sm
, key
);
902 #endif /* CONFIG_PEERKEY */
904 os_free(sm
->last_rx_eapol_key
);
905 sm
->last_rx_eapol_key
= os_malloc(data_len
);
906 if (sm
->last_rx_eapol_key
== NULL
)
908 os_memcpy(sm
->last_rx_eapol_key
, data
, data_len
);
909 sm
->last_rx_eapol_key_len
= data_len
;
911 sm
->EAPOLKeyReceived
= TRUE
;
912 sm
->EAPOLKeyPairwise
= !!(key_info
& WPA_KEY_INFO_KEY_TYPE
);
913 sm
->EAPOLKeyRequest
= !!(key_info
& WPA_KEY_INFO_REQUEST
);
914 os_memcpy(sm
->SNonce
, key
->key_nonce
, WPA_NONCE_LEN
);
919 static void wpa_gmk_to_gtk(const u8
*gmk
, const u8
*addr
, const u8
*gnonce
,
920 u8
*gtk
, size_t gtk_len
)
922 u8 data
[ETH_ALEN
+ WPA_NONCE_LEN
];
924 /* GTK = PRF-X(GMK, "Group key expansion", AA || GNonce) */
925 os_memcpy(data
, addr
, ETH_ALEN
);
926 os_memcpy(data
+ ETH_ALEN
, gnonce
, WPA_NONCE_LEN
);
928 #ifdef CONFIG_IEEE80211W
929 sha256_prf(gmk
, WPA_GMK_LEN
, "Group key expansion",
930 data
, sizeof(data
), gtk
, gtk_len
);
931 #else /* CONFIG_IEEE80211W */
932 sha1_prf(gmk
, WPA_GMK_LEN
, "Group key expansion",
933 data
, sizeof(data
), gtk
, gtk_len
);
934 #endif /* CONFIG_IEEE80211W */
936 wpa_hexdump_key(MSG_DEBUG
, "GMK", gmk
, WPA_GMK_LEN
);
937 wpa_hexdump_key(MSG_DEBUG
, "GTK", gtk
, gtk_len
);
941 static void wpa_send_eapol_timeout(void *eloop_ctx
, void *timeout_ctx
)
943 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
944 struct wpa_state_machine
*sm
= timeout_ctx
;
946 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
, "EAPOL-Key timeout");
947 sm
->TimeoutEvt
= TRUE
;
952 void __wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
953 struct wpa_state_machine
*sm
, int key_info
,
954 const u8
*key_rsc
, const u8
*nonce
,
955 const u8
*kde
, size_t kde_len
,
956 int keyidx
, int encr
, int force_version
)
958 struct ieee802_1x_hdr
*hdr
;
959 struct wpa_eapol_key
*key
;
962 int key_data_len
, pad_len
= 0;
964 int version
, pairwise
;
967 len
= sizeof(struct ieee802_1x_hdr
) + sizeof(struct wpa_eapol_key
);
970 version
= force_version
;
971 else if (wpa_use_aes_cmac(sm
))
972 version
= WPA_KEY_INFO_TYPE_AES_128_CMAC
;
973 else if (sm
->pairwise
== WPA_CIPHER_CCMP
)
974 version
= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
;
976 version
= WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
;
978 pairwise
= key_info
& WPA_KEY_INFO_KEY_TYPE
;
980 wpa_printf(MSG_DEBUG
, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
981 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
984 (key_info
& WPA_KEY_INFO_SECURE
) ? 1 : 0,
985 (key_info
& WPA_KEY_INFO_MIC
) ? 1 : 0,
986 (key_info
& WPA_KEY_INFO_ACK
) ? 1 : 0,
987 (key_info
& WPA_KEY_INFO_INSTALL
) ? 1 : 0,
988 pairwise
, (unsigned long) kde_len
, keyidx
, encr
);
990 key_data_len
= kde_len
;
992 if ((version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
993 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) && encr
) {
994 pad_len
= key_data_len
% 8;
996 pad_len
= 8 - pad_len
;
997 key_data_len
+= pad_len
+ 8;
1000 len
+= key_data_len
;
1002 hdr
= os_zalloc(len
);
1005 hdr
->version
= wpa_auth
->conf
.eapol_version
;
1006 hdr
->type
= IEEE802_1X_TYPE_EAPOL_KEY
;
1007 hdr
->length
= host_to_be16(len
- sizeof(*hdr
));
1008 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1010 key
->type
= sm
->wpa
== WPA_VERSION_WPA2
?
1011 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
1012 key_info
|= version
;
1013 if (encr
&& sm
->wpa
== WPA_VERSION_WPA2
)
1014 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
1015 if (sm
->wpa
!= WPA_VERSION_WPA2
)
1016 key_info
|= keyidx
<< WPA_KEY_INFO_KEY_INDEX_SHIFT
;
1017 WPA_PUT_BE16(key
->key_info
, key_info
);
1019 alg
= pairwise
? sm
->pairwise
: wpa_auth
->conf
.wpa_group
;
1021 case WPA_CIPHER_CCMP
:
1022 WPA_PUT_BE16(key
->key_length
, 16);
1024 case WPA_CIPHER_TKIP
:
1025 WPA_PUT_BE16(key
->key_length
, 32);
1027 case WPA_CIPHER_WEP40
:
1028 WPA_PUT_BE16(key
->key_length
, 5);
1030 case WPA_CIPHER_WEP104
:
1031 WPA_PUT_BE16(key
->key_length
, 13);
1034 if (key_info
& WPA_KEY_INFO_SMK_MESSAGE
)
1035 WPA_PUT_BE16(key
->key_length
, 0);
1037 /* FIX: STSL: what to use as key_replay_counter? */
1038 for (i
= RSNA_MAX_EAPOL_RETRIES
- 1; i
> 0; i
--) {
1039 sm
->key_replay
[i
].valid
= sm
->key_replay
[i
- 1].valid
;
1040 os_memcpy(sm
->key_replay
[i
].counter
,
1041 sm
->key_replay
[i
- 1].counter
,
1042 WPA_REPLAY_COUNTER_LEN
);
1044 inc_byte_array(sm
->key_replay
[0].counter
, WPA_REPLAY_COUNTER_LEN
);
1045 os_memcpy(key
->replay_counter
, sm
->key_replay
[0].counter
,
1046 WPA_REPLAY_COUNTER_LEN
);
1047 sm
->key_replay
[0].valid
= TRUE
;
1050 os_memcpy(key
->key_nonce
, nonce
, WPA_NONCE_LEN
);
1053 os_memcpy(key
->key_rsc
, key_rsc
, WPA_KEY_RSC_LEN
);
1056 os_memcpy(key
+ 1, kde
, kde_len
);
1057 WPA_PUT_BE16(key
->key_data_length
, kde_len
);
1058 } else if (encr
&& kde
) {
1059 buf
= os_zalloc(key_data_len
);
1065 os_memcpy(pos
, kde
, kde_len
);
1071 wpa_hexdump_key(MSG_DEBUG
, "Plaintext EAPOL-Key Key Data",
1073 if (version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1074 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
1075 if (aes_wrap(sm
->PTK
.kek
, (key_data_len
- 8) / 8, buf
,
1076 (u8
*) (key
+ 1))) {
1081 WPA_PUT_BE16(key
->key_data_length
, key_data_len
);
1084 os_memcpy(key
->key_iv
,
1085 sm
->group
->Counter
+ WPA_NONCE_LEN
- 16, 16);
1086 inc_byte_array(sm
->group
->Counter
, WPA_NONCE_LEN
);
1087 os_memcpy(ek
, key
->key_iv
, 16);
1088 os_memcpy(ek
+ 16, sm
->PTK
.kek
, 16);
1089 os_memcpy(key
+ 1, buf
, key_data_len
);
1090 rc4_skip(ek
, 32, 256, (u8
*) (key
+ 1), key_data_len
);
1091 WPA_PUT_BE16(key
->key_data_length
, key_data_len
);
1096 if (key_info
& WPA_KEY_INFO_MIC
) {
1097 if (!sm
->PTK_valid
) {
1098 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1099 "PTK not valid when sending EAPOL-Key "
1104 wpa_eapol_key_mic(sm
->PTK
.kck
, version
, (u8
*) hdr
, len
,
1108 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_inc_EapolFramesTx
,
1110 wpa_auth_send_eapol(wpa_auth
, sm
->addr
, (u8
*) hdr
, len
,
1116 static void wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
1117 struct wpa_state_machine
*sm
, int key_info
,
1118 const u8
*key_rsc
, const u8
*nonce
,
1119 const u8
*kde
, size_t kde_len
,
1120 int keyidx
, int encr
)
1123 int pairwise
= key_info
& WPA_KEY_INFO_KEY_TYPE
;
1129 __wpa_send_eapol(wpa_auth
, sm
, key_info
, key_rsc
, nonce
, kde
, kde_len
,
1132 ctr
= pairwise
? sm
->TimeoutCtr
: sm
->GTimeoutCtr
;
1134 timeout_ms
= eapol_key_timeout_first
;
1136 timeout_ms
= eapol_key_timeout_subseq
;
1137 eloop_register_timeout(timeout_ms
/ 1000, (timeout_ms
% 1000) * 1000,
1138 wpa_send_eapol_timeout
, wpa_auth
, sm
);
1142 static int wpa_verify_key_mic(struct wpa_ptk
*PTK
, u8
*data
, size_t data_len
)
1144 struct ieee802_1x_hdr
*hdr
;
1145 struct wpa_eapol_key
*key
;
1150 if (data_len
< sizeof(*hdr
) + sizeof(*key
))
1153 hdr
= (struct ieee802_1x_hdr
*) data
;
1154 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1155 key_info
= WPA_GET_BE16(key
->key_info
);
1156 os_memcpy(mic
, key
->key_mic
, 16);
1157 os_memset(key
->key_mic
, 0, 16);
1158 if (wpa_eapol_key_mic(PTK
->kck
, key_info
& WPA_KEY_INFO_TYPE_MASK
,
1159 data
, data_len
, key
->key_mic
) ||
1160 os_memcmp(mic
, key
->key_mic
, 16) != 0)
1162 os_memcpy(key
->key_mic
, mic
, 16);
1167 void wpa_remove_ptk(struct wpa_state_machine
*sm
)
1169 sm
->PTK_valid
= FALSE
;
1170 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1171 wpa_auth_set_key(sm
->wpa_auth
, 0, WPA_ALG_NONE
, sm
->addr
, 0, (u8
*) "",
1173 sm
->pairwise_set
= FALSE
;
1174 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
1178 int wpa_auth_sm_event(struct wpa_state_machine
*sm
, wpa_event event
)
1185 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1186 "event %d notification", event
);
1194 sm
->DeauthenticationRequest
= TRUE
;
1197 case WPA_REAUTH_EAPOL
:
1198 if (sm
->GUpdateStationKeys
) {
1200 * Reauthentication cancels the pending group key
1201 * update for this STA.
1203 sm
->group
->GKeyDoneStations
--;
1204 sm
->GUpdateStationKeys
= FALSE
;
1205 sm
->PtkGroupInit
= TRUE
;
1207 sm
->ReAuthenticationRequest
= TRUE
;
1210 #ifdef CONFIG_IEEE80211R
1211 wpa_printf(MSG_DEBUG
, "FT: Retry PTK configuration "
1212 "after association");
1213 wpa_ft_install_ptk(sm
);
1215 /* Using FT protocol, not WPA auth state machine */
1216 sm
->ft_completed
= 1;
1218 #else /* CONFIG_IEEE80211R */
1220 #endif /* CONFIG_IEEE80211R */
1223 #ifdef CONFIG_IEEE80211R
1224 sm
->ft_completed
= 0;
1225 #endif /* CONFIG_IEEE80211R */
1227 #ifdef CONFIG_IEEE80211W
1228 if (sm
->mgmt_frame_prot
&& event
== WPA_AUTH
)
1230 #endif /* CONFIG_IEEE80211W */
1233 sm
->PTK_valid
= FALSE
;
1234 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1236 if (event
!= WPA_REAUTH_EAPOL
)
1240 return wpa_sm_step(sm
);
1244 static enum wpa_alg
wpa_alg_enum(int alg
)
1247 case WPA_CIPHER_CCMP
:
1248 return WPA_ALG_CCMP
;
1249 case WPA_CIPHER_TKIP
:
1250 return WPA_ALG_TKIP
;
1251 case WPA_CIPHER_WEP104
:
1252 case WPA_CIPHER_WEP40
:
1255 return WPA_ALG_NONE
;
1260 SM_STATE(WPA_PTK
, INITIALIZE
)
1262 SM_ENTRY_MA(WPA_PTK
, INITIALIZE
, wpa_ptk
);
1264 /* Init flag is not cleared here, so avoid busy
1265 * loop by claiming nothing changed. */
1266 sm
->changed
= FALSE
;
1270 if (sm
->GUpdateStationKeys
)
1271 sm
->group
->GKeyDoneStations
--;
1272 sm
->GUpdateStationKeys
= FALSE
;
1273 if (sm
->wpa
== WPA_VERSION_WPA
)
1274 sm
->PInitAKeys
= FALSE
;
1275 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1276 * Local AA > Remote AA)) */) {
1279 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 0);
1281 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
, 0);
1283 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
1284 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
1285 WPA_EAPOL_authorized
, 0);
1290 SM_STATE(WPA_PTK
, DISCONNECT
)
1292 SM_ENTRY_MA(WPA_PTK
, DISCONNECT
, wpa_ptk
);
1293 sm
->Disconnect
= FALSE
;
1294 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
);
1298 SM_STATE(WPA_PTK
, DISCONNECTED
)
1300 SM_ENTRY_MA(WPA_PTK
, DISCONNECTED
, wpa_ptk
);
1301 sm
->DeauthenticationRequest
= FALSE
;
1305 SM_STATE(WPA_PTK
, AUTHENTICATION
)
1307 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION
, wpa_ptk
);
1308 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1309 sm
->PTK_valid
= FALSE
;
1310 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portControl_Auto
,
1312 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 1);
1313 sm
->AuthenticationRequest
= FALSE
;
1317 SM_STATE(WPA_PTK
, AUTHENTICATION2
)
1319 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION2
, wpa_ptk
);
1320 os_memcpy(sm
->ANonce
, sm
->group
->Counter
, WPA_NONCE_LEN
);
1321 inc_byte_array(sm
->group
->Counter
, WPA_NONCE_LEN
);
1322 sm
->ReAuthenticationRequest
= FALSE
;
1323 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1324 * logical place than INITIALIZE since AUTHENTICATION2 can be
1325 * re-entered on ReAuthenticationRequest without going through
1331 SM_STATE(WPA_PTK
, INITPMK
)
1333 u8 msk
[2 * PMK_LEN
];
1334 size_t len
= 2 * PMK_LEN
;
1336 SM_ENTRY_MA(WPA_PTK
, INITPMK
, wpa_ptk
);
1337 #ifdef CONFIG_IEEE80211R
1339 #endif /* CONFIG_IEEE80211R */
1341 wpa_printf(MSG_DEBUG
, "WPA: PMK from PMKSA cache");
1342 os_memcpy(sm
->PMK
, sm
->pmksa
->pmk
, PMK_LEN
);
1343 } else if (wpa_auth_get_msk(sm
->wpa_auth
, sm
->addr
, msk
, &len
) == 0) {
1344 wpa_printf(MSG_DEBUG
, "WPA: PMK from EAPOL state machine "
1345 "(len=%lu)", (unsigned long) len
);
1346 os_memcpy(sm
->PMK
, msk
, PMK_LEN
);
1347 #ifdef CONFIG_IEEE80211R
1348 if (len
>= 2 * PMK_LEN
) {
1349 os_memcpy(sm
->xxkey
, msk
+ PMK_LEN
, PMK_LEN
);
1350 sm
->xxkey_len
= PMK_LEN
;
1352 #endif /* CONFIG_IEEE80211R */
1354 wpa_printf(MSG_DEBUG
, "WPA: Could not get PMK");
1357 sm
->req_replay_counter_used
= 0;
1358 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1359 * will break reauthentication since EAPOL state machines may not be
1360 * get into AUTHENTICATING state that clears keyRun before WPA state
1361 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1362 * state and takes PMK from the previously used AAA Key. This will
1363 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1364 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1365 * be good workaround for this issue. */
1366 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyRun
, 0);
1370 SM_STATE(WPA_PTK
, INITPSK
)
1373 SM_ENTRY_MA(WPA_PTK
, INITPSK
, wpa_ptk
);
1374 psk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, NULL
);
1376 os_memcpy(sm
->PMK
, psk
, PMK_LEN
);
1377 #ifdef CONFIG_IEEE80211R
1378 os_memcpy(sm
->xxkey
, psk
, PMK_LEN
);
1379 sm
->xxkey_len
= PMK_LEN
;
1380 #endif /* CONFIG_IEEE80211R */
1382 sm
->req_replay_counter_used
= 0;
1386 SM_STATE(WPA_PTK
, PTKSTART
)
1388 u8 buf
[2 + RSN_SELECTOR_LEN
+ PMKID_LEN
], *pmkid
= NULL
;
1389 size_t pmkid_len
= 0;
1391 SM_ENTRY_MA(WPA_PTK
, PTKSTART
, wpa_ptk
);
1392 sm
->PTKRequest
= FALSE
;
1393 sm
->TimeoutEvt
= FALSE
;
1396 if (sm
->TimeoutCtr
> (int) dot11RSNAConfigPairwiseUpdateCount
) {
1397 /* No point in sending the EAPOL-Key - we will disconnect
1398 * immediately following this. */
1402 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1403 "sending 1/4 msg of 4-Way Handshake");
1405 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1406 * one possible PSK for this STA.
1408 if (sm
->wpa
== WPA_VERSION_WPA2
&&
1409 wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
)) {
1411 pmkid_len
= 2 + RSN_SELECTOR_LEN
+ PMKID_LEN
;
1412 pmkid
[0] = WLAN_EID_VENDOR_SPECIFIC
;
1413 pmkid
[1] = RSN_SELECTOR_LEN
+ PMKID_LEN
;
1414 RSN_SELECTOR_PUT(&pmkid
[2], RSN_KEY_DATA_PMKID
);
1416 os_memcpy(&pmkid
[2 + RSN_SELECTOR_LEN
],
1417 sm
->pmksa
->pmkid
, PMKID_LEN
);
1420 * Calculate PMKID since no PMKSA cache entry was
1421 * available with pre-calculated PMKID.
1423 rsn_pmkid(sm
->PMK
, PMK_LEN
, sm
->wpa_auth
->addr
,
1424 sm
->addr
, &pmkid
[2 + RSN_SELECTOR_LEN
],
1425 wpa_key_mgmt_sha256(sm
->wpa_key_mgmt
));
1428 wpa_send_eapol(sm
->wpa_auth
, sm
,
1429 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_KEY_TYPE
, NULL
,
1430 sm
->ANonce
, pmkid
, pmkid_len
, 0, 0);
1434 static int wpa_derive_ptk(struct wpa_state_machine
*sm
, const u8
*pmk
,
1435 struct wpa_ptk
*ptk
)
1437 size_t ptk_len
= sm
->pairwise
== WPA_CIPHER_CCMP
? 48 : 64;
1438 #ifdef CONFIG_IEEE80211R
1439 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
1440 return wpa_auth_derive_ptk_ft(sm
, pmk
, ptk
, ptk_len
);
1441 #endif /* CONFIG_IEEE80211R */
1443 wpa_pmk_to_ptk(pmk
, PMK_LEN
, "Pairwise key expansion",
1444 sm
->wpa_auth
->addr
, sm
->addr
, sm
->ANonce
, sm
->SNonce
,
1445 (u8
*) ptk
, ptk_len
,
1446 wpa_key_mgmt_sha256(sm
->wpa_key_mgmt
));
1452 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING
)
1456 const u8
*pmk
= NULL
;
1458 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING
, wpa_ptk
);
1459 sm
->EAPOLKeyReceived
= FALSE
;
1461 /* WPA with IEEE 802.1X: use the derived PMK from EAP
1462 * WPA-PSK: iterate through possible PSKs and select the one matching
1465 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
1466 pmk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, pmk
);
1472 wpa_derive_ptk(sm
, pmk
, &PTK
);
1474 if (wpa_verify_key_mic(&PTK
, sm
->last_rx_eapol_key
,
1475 sm
->last_rx_eapol_key_len
) == 0) {
1480 if (!wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
))
1485 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1486 "invalid MIC in msg 2/4 of 4-Way Handshake");
1490 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
1492 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
1493 /* PSK may have changed from the previous choice, so update
1494 * state machine data based on whatever PSK was selected here.
1496 os_memcpy(sm
->PMK
, pmk
, PMK_LEN
);
1499 sm
->MICVerified
= TRUE
;
1501 os_memcpy(&sm
->PTK
, &PTK
, sizeof(PTK
));
1502 sm
->PTK_valid
= TRUE
;
1506 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING2
)
1508 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING2
, wpa_ptk
);
1513 #ifdef CONFIG_IEEE80211W
1515 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
)
1517 if (sm
->mgmt_frame_prot
) {
1518 return 2 + RSN_SELECTOR_LEN
+ sizeof(struct wpa_igtk_kde
);
1525 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
)
1527 struct wpa_igtk_kde igtk
;
1528 struct wpa_group
*gsm
= sm
->group
;
1530 if (!sm
->mgmt_frame_prot
)
1533 igtk
.keyid
[0] = gsm
->GN_igtk
;
1535 if (gsm
->wpa_group_state
!= WPA_GROUP_SETKEYSDONE
||
1536 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, igtk
.pn
) < 0)
1537 os_memset(igtk
.pn
, 0, sizeof(igtk
.pn
));
1538 os_memcpy(igtk
.igtk
, gsm
->IGTK
[gsm
->GN_igtk
- 4], WPA_IGTK_LEN
);
1539 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_IGTK
,
1540 (const u8
*) &igtk
, sizeof(igtk
), NULL
, 0);
1545 #else /* CONFIG_IEEE80211W */
1547 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
)
1553 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
)
1558 #endif /* CONFIG_IEEE80211W */
1561 SM_STATE(WPA_PTK
, PTKINITNEGOTIATING
)
1563 u8 rsc
[WPA_KEY_RSC_LEN
], *_rsc
, *gtk
, *kde
, *pos
;
1564 size_t gtk_len
, kde_len
;
1565 struct wpa_group
*gsm
= sm
->group
;
1567 int wpa_ie_len
, secure
, keyidx
, encr
= 0;
1569 SM_ENTRY_MA(WPA_PTK
, PTKINITNEGOTIATING
, wpa_ptk
);
1570 sm
->TimeoutEvt
= FALSE
;
1573 if (sm
->TimeoutCtr
> (int) dot11RSNAConfigPairwiseUpdateCount
) {
1574 /* No point in sending the EAPOL-Key - we will disconnect
1575 * immediately following this. */
1579 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, GTK[GN])
1581 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
1582 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
1583 wpa_ie
= sm
->wpa_auth
->wpa_ie
;
1584 wpa_ie_len
= sm
->wpa_auth
->wpa_ie_len
;
1585 if (sm
->wpa
== WPA_VERSION_WPA
&&
1586 (sm
->wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
) &&
1587 wpa_ie_len
> wpa_ie
[1] + 2 && wpa_ie
[0] == WLAN_EID_RSN
) {
1588 /* WPA-only STA, remove RSN IE */
1589 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
1590 wpa_ie_len
= wpa_ie
[1] + 2;
1592 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1593 "sending 3/4 msg of 4-Way Handshake");
1594 if (sm
->wpa
== WPA_VERSION_WPA2
) {
1595 /* WPA2 send GTK in the 4-way handshake */
1597 gtk
= gsm
->GTK
[gsm
->GN
- 1];
1598 gtk_len
= gsm
->GTK_len
;
1603 /* WPA does not include GTK in msg 3/4 */
1611 kde_len
= wpa_ie_len
+ ieee80211w_kde_len(sm
);
1613 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 2 + gtk_len
;
1614 kde
= os_malloc(kde_len
);
1619 os_memcpy(pos
, wpa_ie
, wpa_ie_len
);
1623 hdr
[0] = keyidx
& 0x03;
1625 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
1628 pos
= ieee80211w_kde_add(sm
, pos
);
1630 wpa_send_eapol(sm
->wpa_auth
, sm
,
1631 (secure
? WPA_KEY_INFO_SECURE
: 0) | WPA_KEY_INFO_MIC
|
1632 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_INSTALL
|
1633 WPA_KEY_INFO_KEY_TYPE
,
1634 _rsc
, sm
->ANonce
, kde
, pos
- kde
, keyidx
, encr
);
1639 SM_STATE(WPA_PTK
, PTKINITDONE
)
1641 SM_ENTRY_MA(WPA_PTK
, PTKINITDONE
, wpa_ptk
);
1642 sm
->EAPOLKeyReceived
= FALSE
;
1646 if (sm
->pairwise
== WPA_CIPHER_TKIP
) {
1653 if (wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
, 0,
1654 sm
->PTK
.tk1
, klen
)) {
1655 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
);
1658 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
1659 sm
->pairwise_set
= TRUE
;
1661 if (sm
->wpa_auth
->conf
.wpa_ptk_rekey
) {
1662 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
1663 eloop_register_timeout(sm
->wpa_auth
->conf
.
1664 wpa_ptk_rekey
, 0, wpa_rekey_ptk
,
1668 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
1669 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
1670 WPA_EAPOL_authorized
, 1);
1674 if (0 /* IBSS == TRUE */) {
1676 if (sm
->keycount
== 2) {
1677 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
1678 WPA_EAPOL_portValid
, 1);
1681 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
,
1684 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyAvailable
, 0);
1685 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyDone
, 1);
1686 if (sm
->wpa
== WPA_VERSION_WPA
)
1687 sm
->PInitAKeys
= TRUE
;
1690 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
1691 "pairwise key handshake completed (%s)",
1692 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
1694 #ifdef CONFIG_IEEE80211R
1695 wpa_ft_push_pmk_r1(sm
->wpa_auth
, sm
->addr
);
1696 #endif /* CONFIG_IEEE80211R */
1702 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
1705 SM_ENTER(WPA_PTK
, INITIALIZE
);
1706 else if (sm
->Disconnect
1707 /* || FIX: dot11RSNAConfigSALifetime timeout */)
1708 SM_ENTER(WPA_PTK
, DISCONNECT
);
1709 else if (sm
->DeauthenticationRequest
)
1710 SM_ENTER(WPA_PTK
, DISCONNECTED
);
1711 else if (sm
->AuthenticationRequest
)
1712 SM_ENTER(WPA_PTK
, AUTHENTICATION
);
1713 else if (sm
->ReAuthenticationRequest
)
1714 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
1715 else if (sm
->PTKRequest
)
1716 SM_ENTER(WPA_PTK
, PTKSTART
);
1717 else switch (sm
->wpa_ptk_state
) {
1718 case WPA_PTK_INITIALIZE
:
1720 case WPA_PTK_DISCONNECT
:
1721 SM_ENTER(WPA_PTK
, DISCONNECTED
);
1723 case WPA_PTK_DISCONNECTED
:
1724 SM_ENTER(WPA_PTK
, INITIALIZE
);
1726 case WPA_PTK_AUTHENTICATION
:
1727 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
1729 case WPA_PTK_AUTHENTICATION2
:
1730 if (wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) &&
1731 wpa_auth_get_eapol(sm
->wpa_auth
, sm
->addr
,
1732 WPA_EAPOL_keyRun
) > 0)
1733 SM_ENTER(WPA_PTK
, INITPMK
);
1734 else if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)
1735 /* FIX: && 802.1X::keyRun */)
1736 SM_ENTER(WPA_PTK
, INITPSK
);
1738 case WPA_PTK_INITPMK
:
1739 if (wpa_auth_get_eapol(sm
->wpa_auth
, sm
->addr
,
1740 WPA_EAPOL_keyAvailable
) > 0)
1741 SM_ENTER(WPA_PTK
, PTKSTART
);
1743 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
1744 SM_ENTER(WPA_PTK
, DISCONNECT
);
1747 case WPA_PTK_INITPSK
:
1748 if (wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, NULL
))
1749 SM_ENTER(WPA_PTK
, PTKSTART
);
1751 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
1752 "no PSK configured for the STA");
1753 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
1754 SM_ENTER(WPA_PTK
, DISCONNECT
);
1757 case WPA_PTK_PTKSTART
:
1758 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
1759 sm
->EAPOLKeyPairwise
)
1760 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
1761 else if (sm
->TimeoutCtr
>
1762 (int) dot11RSNAConfigPairwiseUpdateCount
) {
1763 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
1764 SM_ENTER(WPA_PTK
, DISCONNECT
);
1765 } else if (sm
->TimeoutEvt
)
1766 SM_ENTER(WPA_PTK
, PTKSTART
);
1768 case WPA_PTK_PTKCALCNEGOTIATING
:
1769 if (sm
->MICVerified
)
1770 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING2
);
1771 else if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
1772 sm
->EAPOLKeyPairwise
)
1773 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
1774 else if (sm
->TimeoutEvt
)
1775 SM_ENTER(WPA_PTK
, PTKSTART
);
1777 case WPA_PTK_PTKCALCNEGOTIATING2
:
1778 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
1780 case WPA_PTK_PTKINITNEGOTIATING
:
1781 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
1782 sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
1783 SM_ENTER(WPA_PTK
, PTKINITDONE
);
1784 else if (sm
->TimeoutCtr
>
1785 (int) dot11RSNAConfigPairwiseUpdateCount
) {
1786 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
1787 SM_ENTER(WPA_PTK
, DISCONNECT
);
1788 } else if (sm
->TimeoutEvt
)
1789 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
1791 case WPA_PTK_PTKINITDONE
:
1797 SM_STATE(WPA_PTK_GROUP
, IDLE
)
1799 SM_ENTRY_MA(WPA_PTK_GROUP
, IDLE
, wpa_ptk_group
);
1801 /* Init flag is not cleared here, so avoid busy
1802 * loop by claiming nothing changed. */
1803 sm
->changed
= FALSE
;
1805 sm
->GTimeoutCtr
= 0;
1809 SM_STATE(WPA_PTK_GROUP
, REKEYNEGOTIATING
)
1811 u8 rsc
[WPA_KEY_RSC_LEN
];
1812 struct wpa_group
*gsm
= sm
->group
;
1813 u8
*kde
, *pos
, hdr
[2];
1816 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYNEGOTIATING
, wpa_ptk_group
);
1819 if (sm
->GTimeoutCtr
> (int) dot11RSNAConfigGroupUpdateCount
) {
1820 /* No point in sending the EAPOL-Key - we will disconnect
1821 * immediately following this. */
1825 if (sm
->wpa
== WPA_VERSION_WPA
)
1826 sm
->PInitAKeys
= FALSE
;
1827 sm
->TimeoutEvt
= FALSE
;
1828 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
1829 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
1830 if (gsm
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
)
1831 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
1832 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1833 "sending 1/2 msg of Group Key Handshake");
1835 if (sm
->wpa
== WPA_VERSION_WPA2
) {
1836 kde_len
= 2 + RSN_SELECTOR_LEN
+ 2 + gsm
->GTK_len
+
1837 ieee80211w_kde_len(sm
);
1838 kde
= os_malloc(kde_len
);
1843 hdr
[0] = gsm
->GN
& 0x03;
1845 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
1846 gsm
->GTK
[gsm
->GN
- 1], gsm
->GTK_len
);
1847 pos
= ieee80211w_kde_add(sm
, pos
);
1849 kde
= gsm
->GTK
[gsm
->GN
- 1];
1850 pos
= kde
+ gsm
->GTK_len
;
1853 wpa_send_eapol(sm
->wpa_auth
, sm
,
1854 WPA_KEY_INFO_SECURE
| WPA_KEY_INFO_MIC
|
1856 (!sm
->Pair
? WPA_KEY_INFO_INSTALL
: 0),
1857 rsc
, gsm
->GNonce
, kde
, pos
- kde
, gsm
->GN
, 1);
1858 if (sm
->wpa
== WPA_VERSION_WPA2
)
1863 SM_STATE(WPA_PTK_GROUP
, REKEYESTABLISHED
)
1865 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYESTABLISHED
, wpa_ptk_group
);
1866 sm
->EAPOLKeyReceived
= FALSE
;
1867 if (sm
->GUpdateStationKeys
)
1868 sm
->group
->GKeyDoneStations
--;
1869 sm
->GUpdateStationKeys
= FALSE
;
1870 sm
->GTimeoutCtr
= 0;
1871 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
1872 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
1873 "group key handshake completed (%s)",
1874 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
1879 SM_STATE(WPA_PTK_GROUP
, KEYERROR
)
1881 SM_ENTRY_MA(WPA_PTK_GROUP
, KEYERROR
, wpa_ptk_group
);
1882 if (sm
->GUpdateStationKeys
)
1883 sm
->group
->GKeyDoneStations
--;
1884 sm
->GUpdateStationKeys
= FALSE
;
1885 sm
->Disconnect
= TRUE
;
1889 SM_STEP(WPA_PTK_GROUP
)
1891 if (sm
->Init
|| sm
->PtkGroupInit
) {
1892 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
1893 sm
->PtkGroupInit
= FALSE
;
1894 } else switch (sm
->wpa_ptk_group_state
) {
1895 case WPA_PTK_GROUP_IDLE
:
1896 if (sm
->GUpdateStationKeys
||
1897 (sm
->wpa
== WPA_VERSION_WPA
&& sm
->PInitAKeys
))
1898 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
1900 case WPA_PTK_GROUP_REKEYNEGOTIATING
:
1901 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
1902 !sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
1903 SM_ENTER(WPA_PTK_GROUP
, REKEYESTABLISHED
);
1904 else if (sm
->GTimeoutCtr
>
1905 (int) dot11RSNAConfigGroupUpdateCount
)
1906 SM_ENTER(WPA_PTK_GROUP
, KEYERROR
);
1907 else if (sm
->TimeoutEvt
)
1908 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
1910 case WPA_PTK_GROUP_KEYERROR
:
1911 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
1913 case WPA_PTK_GROUP_REKEYESTABLISHED
:
1914 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
1920 static int wpa_gtk_update(struct wpa_authenticator
*wpa_auth
,
1921 struct wpa_group
*group
)
1925 /* FIX: is this the correct way of getting GNonce? */
1926 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
1927 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
1928 wpa_gmk_to_gtk(group
->GMK
, wpa_auth
->addr
, group
->GNonce
,
1929 group
->GTK
[group
->GN
- 1], group
->GTK_len
);
1931 #ifdef CONFIG_IEEE80211W
1932 if (wpa_auth
->conf
.ieee80211w
!= NO_MGMT_FRAME_PROTECTION
) {
1933 if (os_get_random(group
->IGTK
[group
->GN_igtk
- 4],
1934 WPA_IGTK_LEN
) < 0) {
1935 wpa_printf(MSG_INFO
, "RSN: Failed to get new random "
1939 wpa_hexdump_key(MSG_DEBUG
, "IGTK",
1940 group
->IGTK
[group
->GN_igtk
- 4], WPA_IGTK_LEN
);
1942 #endif /* CONFIG_IEEE80211W */
1948 static void wpa_group_gtk_init(struct wpa_authenticator
*wpa_auth
,
1949 struct wpa_group
*group
)
1951 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
1952 "GTK_INIT (VLAN-ID %d)", group
->vlan_id
);
1953 group
->changed
= FALSE
; /* GInit is not cleared here; avoid loop */
1954 group
->wpa_group_state
= WPA_GROUP_GTK_INIT
;
1957 os_memset(group
->GTK
, 0, sizeof(group
->GTK
));
1960 #ifdef CONFIG_IEEE80211W
1963 #endif /* CONFIG_IEEE80211W */
1964 /* GTK[GN] = CalcGTK() */
1965 wpa_gtk_update(wpa_auth
, group
);
1969 static int wpa_group_update_sta(struct wpa_state_machine
*sm
, void *ctx
)
1971 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITDONE
) {
1972 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1973 "Not in PTKINITDONE; skip Group Key update");
1976 if (sm
->GUpdateStationKeys
) {
1978 * This should not really happen, but just in case, make sure
1979 * we do not count the same STA twice in GKeyDoneStations.
1981 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1982 "GUpdateStationKeys already set - do not "
1983 "increment GKeyDoneStations");
1985 sm
->group
->GKeyDoneStations
++;
1986 sm
->GUpdateStationKeys
= TRUE
;
1993 static void wpa_group_setkeys(struct wpa_authenticator
*wpa_auth
,
1994 struct wpa_group
*group
)
1998 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
1999 "SETKEYS (VLAN-ID %d)", group
->vlan_id
);
2000 group
->changed
= TRUE
;
2001 group
->wpa_group_state
= WPA_GROUP_SETKEYS
;
2002 group
->GTKReKey
= FALSE
;
2004 group
->GM
= group
->GN
;
2006 #ifdef CONFIG_IEEE80211W
2007 tmp
= group
->GM_igtk
;
2008 group
->GM_igtk
= group
->GN_igtk
;
2009 group
->GN_igtk
= tmp
;
2010 #endif /* CONFIG_IEEE80211W */
2011 /* "GKeyDoneStations = GNoStations" is done in more robust way by
2012 * counting the STAs that are marked with GUpdateStationKeys instead of
2013 * including all STAs that could be in not-yet-completed state. */
2014 wpa_gtk_update(wpa_auth
, group
);
2016 wpa_auth_for_each_sta(wpa_auth
, wpa_group_update_sta
, NULL
);
2017 wpa_printf(MSG_DEBUG
, "wpa_group_setkeys: GKeyDoneStations=%d",
2018 group
->GKeyDoneStations
);
2022 static void wpa_group_setkeysdone(struct wpa_authenticator
*wpa_auth
,
2023 struct wpa_group
*group
)
2025 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
2026 "SETKEYSDONE (VLAN-ID %d)", group
->vlan_id
);
2027 group
->changed
= TRUE
;
2028 group
->wpa_group_state
= WPA_GROUP_SETKEYSDONE
;
2029 wpa_auth_set_key(wpa_auth
, group
->vlan_id
,
2030 wpa_alg_enum(wpa_auth
->conf
.wpa_group
),
2031 NULL
, group
->GN
, group
->GTK
[group
->GN
- 1],
2034 #ifdef CONFIG_IEEE80211W
2035 if (wpa_auth
->conf
.ieee80211w
!= NO_MGMT_FRAME_PROTECTION
) {
2036 wpa_auth_set_key(wpa_auth
, group
->vlan_id
, WPA_ALG_IGTK
,
2037 NULL
, group
->GN_igtk
,
2038 group
->IGTK
[group
->GN_igtk
- 4],
2041 #endif /* CONFIG_IEEE80211W */
2045 static void wpa_group_sm_step(struct wpa_authenticator
*wpa_auth
,
2046 struct wpa_group
*group
)
2049 wpa_group_gtk_init(wpa_auth
, group
);
2050 } else if (group
->wpa_group_state
== WPA_GROUP_GTK_INIT
&&
2051 group
->GTKAuthenticator
) {
2052 wpa_group_setkeysdone(wpa_auth
, group
);
2053 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
&&
2055 wpa_group_setkeys(wpa_auth
, group
);
2056 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYS
) {
2057 if (group
->GKeyDoneStations
== 0)
2058 wpa_group_setkeysdone(wpa_auth
, group
);
2059 else if (group
->GTKReKey
)
2060 wpa_group_setkeys(wpa_auth
, group
);
2065 static int wpa_sm_step(struct wpa_state_machine
*sm
)
2070 if (sm
->in_step_loop
) {
2071 /* This should not happen, but if it does, make sure we do not
2072 * end up freeing the state machine too early by exiting the
2073 * recursive call. */
2074 wpa_printf(MSG_ERROR
, "WPA: wpa_sm_step() called recursively");
2078 sm
->in_step_loop
= 1;
2080 if (sm
->pending_deinit
)
2083 sm
->changed
= FALSE
;
2084 sm
->wpa_auth
->group
->changed
= FALSE
;
2086 SM_STEP_RUN(WPA_PTK
);
2087 if (sm
->pending_deinit
)
2089 SM_STEP_RUN(WPA_PTK_GROUP
);
2090 if (sm
->pending_deinit
)
2092 wpa_group_sm_step(sm
->wpa_auth
, sm
->group
);
2093 } while (sm
->changed
|| sm
->wpa_auth
->group
->changed
);
2094 sm
->in_step_loop
= 0;
2096 if (sm
->pending_deinit
) {
2097 wpa_printf(MSG_DEBUG
, "WPA: Completing pending STA state "
2098 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
2099 wpa_free_sta_sm(sm
);
2106 static void wpa_sm_call_step(void *eloop_ctx
, void *timeout_ctx
)
2108 struct wpa_state_machine
*sm
= eloop_ctx
;
2113 void wpa_auth_sm_notify(struct wpa_state_machine
*sm
)
2117 eloop_register_timeout(0, 0, wpa_sm_call_step
, sm
, NULL
);
2121 void wpa_gtk_rekey(struct wpa_authenticator
*wpa_auth
)
2124 struct wpa_group
*group
;
2126 if (wpa_auth
== NULL
)
2129 group
= wpa_auth
->group
;
2131 for (i
= 0; i
< 2; i
++) {
2133 group
->GM
= group
->GN
;
2135 #ifdef CONFIG_IEEE80211W
2136 tmp
= group
->GM_igtk
;
2137 group
->GM_igtk
= group
->GN_igtk
;
2138 group
->GN_igtk
= tmp
;
2139 #endif /* CONFIG_IEEE80211W */
2140 wpa_gtk_update(wpa_auth
, group
);
2145 static const char * wpa_bool_txt(int bool)
2147 return bool ? "TRUE" : "FALSE";
2151 static int wpa_cipher_bits(int cipher
)
2154 case WPA_CIPHER_CCMP
:
2156 case WPA_CIPHER_TKIP
:
2158 case WPA_CIPHER_WEP104
:
2160 case WPA_CIPHER_WEP40
:
2168 #define RSN_SUITE "%02x-%02x-%02x-%d"
2169 #define RSN_SUITE_ARG(s) \
2170 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2172 int wpa_get_mib(struct wpa_authenticator
*wpa_auth
, char *buf
, size_t buflen
)
2175 char pmkid_txt
[PMKID_LEN
* 2 + 1];
2177 if (wpa_auth
== NULL
)
2180 ret
= os_snprintf(buf
+ len
, buflen
- len
,
2181 "dot11RSNAOptionImplemented=TRUE\n"
2182 #ifdef CONFIG_RSN_PREAUTH
2183 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2184 #else /* CONFIG_RSN_PREAUTH */
2185 "dot11RSNAPreauthenticationImplemented=FALSE\n"
2186 #endif /* CONFIG_RSN_PREAUTH */
2187 "dot11RSNAEnabled=%s\n"
2188 "dot11RSNAPreauthenticationEnabled=%s\n",
2189 wpa_bool_txt(wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
),
2190 wpa_bool_txt(wpa_auth
->conf
.rsn_preauth
));
2191 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
2195 wpa_snprintf_hex(pmkid_txt
, sizeof(pmkid_txt
),
2196 wpa_auth
->dot11RSNAPMKIDUsed
, PMKID_LEN
);
2199 buf
+ len
, buflen
- len
,
2200 "dot11RSNAConfigVersion=%u\n"
2201 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
2202 /* FIX: dot11RSNAConfigGroupCipher */
2203 /* FIX: dot11RSNAConfigGroupRekeyMethod */
2204 /* FIX: dot11RSNAConfigGroupRekeyTime */
2205 /* FIX: dot11RSNAConfigGroupRekeyPackets */
2206 "dot11RSNAConfigGroupRekeyStrict=%u\n"
2207 "dot11RSNAConfigGroupUpdateCount=%u\n"
2208 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
2209 "dot11RSNAConfigGroupCipherSize=%u\n"
2210 "dot11RSNAConfigPMKLifetime=%u\n"
2211 "dot11RSNAConfigPMKReauthThreshold=%u\n"
2212 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
2213 "dot11RSNAConfigSATimeout=%u\n"
2214 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE
"\n"
2215 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE
"\n"
2216 "dot11RSNAGroupCipherSelected=" RSN_SUITE
"\n"
2217 "dot11RSNAPMKIDUsed=%s\n"
2218 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE
"\n"
2219 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE
"\n"
2220 "dot11RSNAGroupCipherRequested=" RSN_SUITE
"\n"
2221 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
2222 "dot11RSNA4WayHandshakeFailures=%u\n"
2223 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
2225 !!wpa_auth
->conf
.wpa_strict_rekey
,
2226 dot11RSNAConfigGroupUpdateCount
,
2227 dot11RSNAConfigPairwiseUpdateCount
,
2228 wpa_cipher_bits(wpa_auth
->conf
.wpa_group
),
2229 dot11RSNAConfigPMKLifetime
,
2230 dot11RSNAConfigPMKReauthThreshold
,
2231 dot11RSNAConfigSATimeout
,
2232 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteSelected
),
2233 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherSelected
),
2234 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherSelected
),
2236 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteRequested
),
2237 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherRequested
),
2238 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherRequested
),
2239 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
,
2240 wpa_auth
->dot11RSNA4WayHandshakeFailures
);
2241 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
2245 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
2246 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
2249 ret
= os_snprintf(buf
+ len
, buflen
- len
, "hostapdWPAGroupState=%d\n",
2250 wpa_auth
->group
->wpa_group_state
);
2251 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
2259 int wpa_get_mib_sta(struct wpa_state_machine
*sm
, char *buf
, size_t buflen
)
2267 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
2269 /* dot11RSNAStatsEntry */
2271 if (sm
->wpa
== WPA_VERSION_WPA
) {
2272 if (sm
->pairwise
== WPA_CIPHER_CCMP
)
2273 pairwise
= WPA_CIPHER_SUITE_CCMP
;
2274 else if (sm
->pairwise
== WPA_CIPHER_TKIP
)
2275 pairwise
= WPA_CIPHER_SUITE_TKIP
;
2276 else if (sm
->pairwise
== WPA_CIPHER_WEP104
)
2277 pairwise
= WPA_CIPHER_SUITE_WEP104
;
2278 else if (sm
->pairwise
== WPA_CIPHER_WEP40
)
2279 pairwise
= WPA_CIPHER_SUITE_WEP40
;
2280 else if (sm
->pairwise
== WPA_CIPHER_NONE
)
2281 pairwise
= WPA_CIPHER_SUITE_NONE
;
2282 } else if (sm
->wpa
== WPA_VERSION_WPA2
) {
2283 if (sm
->pairwise
== WPA_CIPHER_CCMP
)
2284 pairwise
= RSN_CIPHER_SUITE_CCMP
;
2285 else if (sm
->pairwise
== WPA_CIPHER_TKIP
)
2286 pairwise
= RSN_CIPHER_SUITE_TKIP
;
2287 else if (sm
->pairwise
== WPA_CIPHER_WEP104
)
2288 pairwise
= RSN_CIPHER_SUITE_WEP104
;
2289 else if (sm
->pairwise
== WPA_CIPHER_WEP40
)
2290 pairwise
= RSN_CIPHER_SUITE_WEP40
;
2291 else if (sm
->pairwise
== WPA_CIPHER_NONE
)
2292 pairwise
= RSN_CIPHER_SUITE_NONE
;
2297 buf
+ len
, buflen
- len
,
2298 /* TODO: dot11RSNAStatsIndex */
2299 "dot11RSNAStatsSTAAddress=" MACSTR
"\n"
2300 "dot11RSNAStatsVersion=1\n"
2301 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE
"\n"
2302 /* TODO: dot11RSNAStatsTKIPICVErrors */
2303 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
2304 "dot11RSNAStatsTKIPRemoveMICFailures=%u\n"
2305 /* TODO: dot11RSNAStatsCCMPReplays */
2306 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
2307 /* TODO: dot11RSNAStatsTKIPReplays */,
2309 RSN_SUITE_ARG(pairwise
),
2310 sm
->dot11RSNAStatsTKIPLocalMICFailures
,
2311 sm
->dot11RSNAStatsTKIPRemoteMICFailures
);
2312 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
2317 ret
= os_snprintf(buf
+ len
, buflen
- len
,
2318 "hostapdWPAPTKState=%d\n"
2319 "hostapdWPAPTKGroupState=%d\n",
2321 sm
->wpa_ptk_group_state
);
2322 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
2330 void wpa_auth_countermeasures_start(struct wpa_authenticator
*wpa_auth
)
2333 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
++;
2337 int wpa_auth_pairwise_set(struct wpa_state_machine
*sm
)
2339 return sm
&& sm
->pairwise_set
;
2343 int wpa_auth_get_pairwise(struct wpa_state_machine
*sm
)
2345 return sm
->pairwise
;
2349 int wpa_auth_sta_key_mgmt(struct wpa_state_machine
*sm
)
2353 return sm
->wpa_key_mgmt
;
2357 int wpa_auth_sta_wpa_version(struct wpa_state_machine
*sm
)
2365 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine
*sm
,
2366 struct rsn_pmksa_cache_entry
*entry
)
2368 if (sm
== NULL
|| sm
->pmksa
!= entry
)
2375 struct rsn_pmksa_cache_entry
*
2376 wpa_auth_sta_get_pmksa(struct wpa_state_machine
*sm
)
2378 return sm
? sm
->pmksa
: NULL
;
2382 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine
*sm
)
2385 sm
->dot11RSNAStatsTKIPLocalMICFailures
++;
2389 const u8
* wpa_auth_get_wpa_ie(struct wpa_authenticator
*wpa_auth
, size_t *len
)
2391 if (wpa_auth
== NULL
)
2393 *len
= wpa_auth
->wpa_ie_len
;
2394 return wpa_auth
->wpa_ie
;
2398 int wpa_auth_pmksa_add(struct wpa_state_machine
*sm
, const u8
*pmk
,
2399 int session_timeout
, struct eapol_state_machine
*eapol
)
2401 if (sm
== NULL
|| sm
->wpa
!= WPA_VERSION_WPA2
)
2404 if (pmksa_cache_auth_add(sm
->wpa_auth
->pmksa
, pmk
, PMK_LEN
,
2405 sm
->wpa_auth
->addr
, sm
->addr
, session_timeout
,
2406 eapol
, sm
->wpa_key_mgmt
))
2413 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator
*wpa_auth
,
2414 const u8
*pmk
, size_t len
, const u8
*sta_addr
,
2415 int session_timeout
,
2416 struct eapol_state_machine
*eapol
)
2418 if (wpa_auth
== NULL
)
2421 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, len
, wpa_auth
->addr
,
2422 sta_addr
, session_timeout
, eapol
,
2423 WPA_KEY_MGMT_IEEE8021X
))
2430 static struct wpa_group
*
2431 wpa_auth_add_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
2433 struct wpa_group
*group
;
2435 if (wpa_auth
== NULL
|| wpa_auth
->group
== NULL
)
2438 wpa_printf(MSG_DEBUG
, "WPA: Add group state machine for VLAN-ID %d",
2440 group
= wpa_group_init(wpa_auth
, vlan_id
);
2444 group
->next
= wpa_auth
->group
->next
;
2445 wpa_auth
->group
->next
= group
;
2451 int wpa_auth_sta_set_vlan(struct wpa_state_machine
*sm
, int vlan_id
)
2453 struct wpa_group
*group
;
2455 if (sm
== NULL
|| sm
->wpa_auth
== NULL
)
2458 group
= sm
->wpa_auth
->group
;
2460 if (group
->vlan_id
== vlan_id
)
2462 group
= group
->next
;
2465 if (group
== NULL
) {
2466 group
= wpa_auth_add_group(sm
->wpa_auth
, vlan_id
);
2471 if (sm
->group
== group
)
2474 wpa_printf(MSG_DEBUG
, "WPA: Moving STA " MACSTR
" to use group state "
2475 "machine for VLAN ID %d", MAC2STR(sm
->addr
), vlan_id
);