2 * IEEE 802.11 RSN / WPA Authenticator
3 * Copyright (c) 2004-2013, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "utils/state_machine.h"
14 #include "utils/bitfield.h"
15 #include "common/ieee802_11_defs.h"
16 #include "crypto/aes_wrap.h"
17 #include "crypto/crypto.h"
18 #include "crypto/sha1.h"
19 #include "crypto/sha256.h"
20 #include "crypto/random.h"
21 #include "eapol_auth/eapol_auth_sm.h"
22 #include "ap_config.h"
23 #include "ieee802_11.h"
25 #include "pmksa_cache_auth.h"
26 #include "wpa_auth_i.h"
27 #include "wpa_auth_ie.h"
29 #define STATE_MACHINE_DATA struct wpa_state_machine
30 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
31 #define STATE_MACHINE_ADDR sm->addr
34 static void wpa_send_eapol_timeout(void *eloop_ctx
, void *timeout_ctx
);
35 static int wpa_sm_step(struct wpa_state_machine
*sm
);
36 static int wpa_verify_key_mic(struct wpa_ptk
*PTK
, u8
*data
, size_t data_len
);
37 static void wpa_sm_call_step(void *eloop_ctx
, void *timeout_ctx
);
38 static void wpa_group_sm_step(struct wpa_authenticator
*wpa_auth
,
39 struct wpa_group
*group
);
40 static void wpa_request_new_ptk(struct wpa_state_machine
*sm
);
41 static int wpa_gtk_update(struct wpa_authenticator
*wpa_auth
,
42 struct wpa_group
*group
);
43 static int wpa_group_config_group_keys(struct wpa_authenticator
*wpa_auth
,
44 struct wpa_group
*group
);
46 static const u32 dot11RSNAConfigGroupUpdateCount
= 4;
47 static const u32 dot11RSNAConfigPairwiseUpdateCount
= 4;
48 static const u32 eapol_key_timeout_first
= 100; /* ms */
49 static const u32 eapol_key_timeout_subseq
= 1000; /* ms */
50 static const u32 eapol_key_timeout_first_group
= 500; /* ms */
52 /* TODO: make these configurable */
53 static const int dot11RSNAConfigPMKLifetime
= 43200;
54 static const int dot11RSNAConfigPMKReauthThreshold
= 70;
55 static const int dot11RSNAConfigSATimeout
= 60;
58 static inline int wpa_auth_mic_failure_report(
59 struct wpa_authenticator
*wpa_auth
, const u8
*addr
)
61 if (wpa_auth
->cb
.mic_failure_report
)
62 return 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
,
87 const u8
*p2p_dev_addr
,
90 if (wpa_auth
->cb
.get_psk
== NULL
)
92 return wpa_auth
->cb
.get_psk(wpa_auth
->cb
.ctx
, addr
, p2p_dev_addr
,
97 static inline int wpa_auth_get_msk(struct wpa_authenticator
*wpa_auth
,
98 const u8
*addr
, u8
*msk
, size_t *len
)
100 if (wpa_auth
->cb
.get_msk
== NULL
)
102 return wpa_auth
->cb
.get_msk(wpa_auth
->cb
.ctx
, addr
, msk
, len
);
106 static inline int wpa_auth_set_key(struct wpa_authenticator
*wpa_auth
,
108 enum wpa_alg alg
, const u8
*addr
, int idx
,
109 u8
*key
, size_t key_len
)
111 if (wpa_auth
->cb
.set_key
== NULL
)
113 return wpa_auth
->cb
.set_key(wpa_auth
->cb
.ctx
, vlan_id
, alg
, addr
, idx
,
118 static inline int wpa_auth_get_seqnum(struct wpa_authenticator
*wpa_auth
,
119 const u8
*addr
, int idx
, u8
*seq
)
121 if (wpa_auth
->cb
.get_seqnum
== NULL
)
123 return wpa_auth
->cb
.get_seqnum(wpa_auth
->cb
.ctx
, addr
, idx
, seq
);
128 wpa_auth_send_eapol(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
129 const u8
*data
, size_t data_len
, int encrypt
)
131 if (wpa_auth
->cb
.send_eapol
== NULL
)
133 return wpa_auth
->cb
.send_eapol(wpa_auth
->cb
.ctx
, addr
, data
, data_len
,
138 int wpa_auth_for_each_sta(struct wpa_authenticator
*wpa_auth
,
139 int (*cb
)(struct wpa_state_machine
*sm
, void *ctx
),
142 if (wpa_auth
->cb
.for_each_sta
== NULL
)
144 return wpa_auth
->cb
.for_each_sta(wpa_auth
->cb
.ctx
, cb
, cb_ctx
);
148 int wpa_auth_for_each_auth(struct wpa_authenticator
*wpa_auth
,
149 int (*cb
)(struct wpa_authenticator
*a
, void *ctx
),
152 if (wpa_auth
->cb
.for_each_auth
== NULL
)
154 return wpa_auth
->cb
.for_each_auth(wpa_auth
->cb
.ctx
, cb
, cb_ctx
);
158 void wpa_auth_logger(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
159 logger_level level
, const char *txt
)
161 if (wpa_auth
->cb
.logger
== NULL
)
163 wpa_auth
->cb
.logger(wpa_auth
->cb
.ctx
, addr
, level
, txt
);
167 void wpa_auth_vlogger(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
168 logger_level level
, const char *fmt
, ...)
174 if (wpa_auth
->cb
.logger
== NULL
)
177 maxlen
= os_strlen(fmt
) + 100;
178 format
= os_malloc(maxlen
);
183 vsnprintf(format
, maxlen
, fmt
, ap
);
186 wpa_auth_logger(wpa_auth
, addr
, level
, format
);
192 static void wpa_sta_disconnect(struct wpa_authenticator
*wpa_auth
,
195 if (wpa_auth
->cb
.disconnect
== NULL
)
197 wpa_printf(MSG_DEBUG
, "wpa_sta_disconnect STA " MACSTR
, MAC2STR(addr
));
198 wpa_auth
->cb
.disconnect(wpa_auth
->cb
.ctx
, addr
,
199 WLAN_REASON_PREV_AUTH_NOT_VALID
);
203 static int wpa_use_aes_cmac(struct wpa_state_machine
*sm
)
206 #ifdef CONFIG_IEEE80211R
207 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
209 #endif /* CONFIG_IEEE80211R */
210 #ifdef CONFIG_IEEE80211W
211 if (wpa_key_mgmt_sha256(sm
->wpa_key_mgmt
))
213 #endif /* CONFIG_IEEE80211W */
214 if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
)
220 static void wpa_rekey_gmk(void *eloop_ctx
, void *timeout_ctx
)
222 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
224 if (random_get_bytes(wpa_auth
->group
->GMK
, WPA_GMK_LEN
)) {
225 wpa_printf(MSG_ERROR
, "Failed to get random data for WPA "
228 wpa_auth_logger(wpa_auth
, NULL
, LOGGER_DEBUG
, "GMK rekeyd");
229 wpa_hexdump_key(MSG_DEBUG
, "GMK",
230 wpa_auth
->group
->GMK
, WPA_GMK_LEN
);
233 if (wpa_auth
->conf
.wpa_gmk_rekey
) {
234 eloop_register_timeout(wpa_auth
->conf
.wpa_gmk_rekey
, 0,
235 wpa_rekey_gmk
, wpa_auth
, NULL
);
240 static void wpa_rekey_gtk(void *eloop_ctx
, void *timeout_ctx
)
242 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
243 struct wpa_group
*group
;
245 wpa_auth_logger(wpa_auth
, NULL
, LOGGER_DEBUG
, "rekeying GTK");
246 for (group
= wpa_auth
->group
; group
; group
= group
->next
) {
247 group
->GTKReKey
= TRUE
;
249 group
->changed
= FALSE
;
250 wpa_group_sm_step(wpa_auth
, group
);
251 } while (group
->changed
);
254 if (wpa_auth
->conf
.wpa_group_rekey
) {
255 eloop_register_timeout(wpa_auth
->conf
.wpa_group_rekey
,
256 0, wpa_rekey_gtk
, wpa_auth
, NULL
);
261 static void wpa_rekey_ptk(void *eloop_ctx
, void *timeout_ctx
)
263 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
264 struct wpa_state_machine
*sm
= timeout_ctx
;
266 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
, "rekeying PTK");
267 wpa_request_new_ptk(sm
);
272 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine
*sm
, void *ctx
)
274 if (sm
->pmksa
== ctx
)
280 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry
*entry
,
283 struct wpa_authenticator
*wpa_auth
= ctx
;
284 wpa_auth_for_each_sta(wpa_auth
, wpa_auth_pmksa_clear_cb
, entry
);
288 static int wpa_group_init_gmk_and_counter(struct wpa_authenticator
*wpa_auth
,
289 struct wpa_group
*group
)
291 u8 buf
[ETH_ALEN
+ 8 + sizeof(unsigned long)];
295 if (random_get_bytes(group
->GMK
, WPA_GMK_LEN
) < 0)
297 wpa_hexdump_key(MSG_DEBUG
, "GMK", group
->GMK
, WPA_GMK_LEN
);
300 * Counter = PRF-256(Random number, "Init Counter",
301 * Local MAC Address || Time)
303 os_memcpy(buf
, wpa_auth
->addr
, ETH_ALEN
);
304 wpa_get_ntp_timestamp(buf
+ ETH_ALEN
);
305 ptr
= (unsigned long) group
;
306 os_memcpy(buf
+ ETH_ALEN
+ 8, &ptr
, sizeof(ptr
));
307 if (random_get_bytes(rkey
, sizeof(rkey
)) < 0)
310 if (sha1_prf(rkey
, sizeof(rkey
), "Init Counter", buf
, sizeof(buf
),
311 group
->Counter
, WPA_NONCE_LEN
) < 0)
313 wpa_hexdump_key(MSG_DEBUG
, "Key Counter",
314 group
->Counter
, WPA_NONCE_LEN
);
320 static struct wpa_group
* wpa_group_init(struct wpa_authenticator
*wpa_auth
,
321 int vlan_id
, int delay_init
)
323 struct wpa_group
*group
;
325 group
= os_zalloc(sizeof(struct wpa_group
));
329 group
->GTKAuthenticator
= TRUE
;
330 group
->vlan_id
= vlan_id
;
331 group
->GTK_len
= wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
);
333 if (random_pool_ready() != 1) {
334 wpa_printf(MSG_INFO
, "WPA: Not enough entropy in random pool "
335 "for secure operations - update keys later when "
336 "the first station connects");
340 * Set initial GMK/Counter value here. The actual values that will be
341 * used in negotiations will be set once the first station tries to
342 * connect. This allows more time for collecting additional randomness
343 * on embedded devices.
345 if (wpa_group_init_gmk_and_counter(wpa_auth
, group
) < 0) {
346 wpa_printf(MSG_ERROR
, "Failed to get random data for WPA "
354 wpa_printf(MSG_DEBUG
, "WPA: Delay group state machine start "
355 "until Beacon frames have been configured");
356 /* Initialization is completed in wpa_init_keys(). */
358 wpa_group_sm_step(wpa_auth
, group
);
359 group
->GInit
= FALSE
;
360 wpa_group_sm_step(wpa_auth
, group
);
368 * wpa_init - Initialize WPA authenticator
369 * @addr: Authenticator address
370 * @conf: Configuration for WPA authenticator
371 * @cb: Callback functions for WPA authenticator
372 * Returns: Pointer to WPA authenticator data or %NULL on failure
374 struct wpa_authenticator
* wpa_init(const u8
*addr
,
375 struct wpa_auth_config
*conf
,
376 struct wpa_auth_callbacks
*cb
)
378 struct wpa_authenticator
*wpa_auth
;
380 wpa_auth
= os_zalloc(sizeof(struct wpa_authenticator
));
381 if (wpa_auth
== NULL
)
383 os_memcpy(wpa_auth
->addr
, addr
, ETH_ALEN
);
384 os_memcpy(&wpa_auth
->conf
, conf
, sizeof(*conf
));
385 os_memcpy(&wpa_auth
->cb
, cb
, sizeof(*cb
));
387 if (wpa_auth_gen_wpa_ie(wpa_auth
)) {
388 wpa_printf(MSG_ERROR
, "Could not generate WPA IE.");
393 wpa_auth
->group
= wpa_group_init(wpa_auth
, 0, 1);
394 if (wpa_auth
->group
== NULL
) {
395 os_free(wpa_auth
->wpa_ie
);
400 wpa_auth
->pmksa
= pmksa_cache_auth_init(wpa_auth_pmksa_free_cb
,
402 if (wpa_auth
->pmksa
== NULL
) {
403 wpa_printf(MSG_ERROR
, "PMKSA cache initialization failed.");
404 os_free(wpa_auth
->wpa_ie
);
409 #ifdef CONFIG_IEEE80211R
410 wpa_auth
->ft_pmk_cache
= wpa_ft_pmk_cache_init();
411 if (wpa_auth
->ft_pmk_cache
== NULL
) {
412 wpa_printf(MSG_ERROR
, "FT PMK cache initialization failed.");
413 os_free(wpa_auth
->wpa_ie
);
414 pmksa_cache_auth_deinit(wpa_auth
->pmksa
);
418 #endif /* CONFIG_IEEE80211R */
420 if (wpa_auth
->conf
.wpa_gmk_rekey
) {
421 eloop_register_timeout(wpa_auth
->conf
.wpa_gmk_rekey
, 0,
422 wpa_rekey_gmk
, wpa_auth
, NULL
);
425 if (wpa_auth
->conf
.wpa_group_rekey
) {
426 eloop_register_timeout(wpa_auth
->conf
.wpa_group_rekey
, 0,
427 wpa_rekey_gtk
, wpa_auth
, NULL
);
431 if (WPA_GET_BE32(conf
->ip_addr_start
)) {
432 int count
= WPA_GET_BE32(conf
->ip_addr_end
) -
433 WPA_GET_BE32(conf
->ip_addr_start
) + 1;
437 wpa_auth
->ip_pool
= bitfield_alloc(count
);
439 #endif /* CONFIG_P2P */
445 int wpa_init_keys(struct wpa_authenticator
*wpa_auth
)
447 struct wpa_group
*group
= wpa_auth
->group
;
449 wpa_printf(MSG_DEBUG
, "WPA: Start group state machine to set initial "
451 wpa_group_sm_step(wpa_auth
, group
);
452 group
->GInit
= FALSE
;
453 wpa_group_sm_step(wpa_auth
, group
);
454 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
461 * wpa_deinit - Deinitialize WPA authenticator
462 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
464 void wpa_deinit(struct wpa_authenticator
*wpa_auth
)
466 struct wpa_group
*group
, *prev
;
468 eloop_cancel_timeout(wpa_rekey_gmk
, wpa_auth
, NULL
);
469 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
471 #ifdef CONFIG_PEERKEY
472 while (wpa_auth
->stsl_negotiations
)
473 wpa_stsl_remove(wpa_auth
, wpa_auth
->stsl_negotiations
);
474 #endif /* CONFIG_PEERKEY */
476 pmksa_cache_auth_deinit(wpa_auth
->pmksa
);
478 #ifdef CONFIG_IEEE80211R
479 wpa_ft_pmk_cache_deinit(wpa_auth
->ft_pmk_cache
);
480 wpa_auth
->ft_pmk_cache
= NULL
;
481 #endif /* CONFIG_IEEE80211R */
484 bitfield_free(wpa_auth
->ip_pool
);
485 #endif /* CONFIG_P2P */
488 os_free(wpa_auth
->wpa_ie
);
490 group
= wpa_auth
->group
;
502 * wpa_reconfig - Update WPA authenticator configuration
503 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
504 * @conf: Configuration for WPA authenticator
506 int wpa_reconfig(struct wpa_authenticator
*wpa_auth
,
507 struct wpa_auth_config
*conf
)
509 struct wpa_group
*group
;
510 if (wpa_auth
== NULL
)
513 os_memcpy(&wpa_auth
->conf
, conf
, sizeof(*conf
));
514 if (wpa_auth_gen_wpa_ie(wpa_auth
)) {
515 wpa_printf(MSG_ERROR
, "Could not generate WPA IE.");
520 * Reinitialize GTK to make sure it is suitable for the new
523 group
= wpa_auth
->group
;
524 group
->GTK_len
= wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
);
526 wpa_group_sm_step(wpa_auth
, group
);
527 group
->GInit
= FALSE
;
528 wpa_group_sm_step(wpa_auth
, group
);
534 struct wpa_state_machine
*
535 wpa_auth_sta_init(struct wpa_authenticator
*wpa_auth
, const u8
*addr
,
536 const u8
*p2p_dev_addr
)
538 struct wpa_state_machine
*sm
;
540 if (wpa_auth
->group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
543 sm
= os_zalloc(sizeof(struct wpa_state_machine
));
546 os_memcpy(sm
->addr
, addr
, ETH_ALEN
);
548 os_memcpy(sm
->p2p_dev_addr
, p2p_dev_addr
, ETH_ALEN
);
550 sm
->wpa_auth
= wpa_auth
;
551 sm
->group
= wpa_auth
->group
;
557 int wpa_auth_sta_associated(struct wpa_authenticator
*wpa_auth
,
558 struct wpa_state_machine
*sm
)
560 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
563 #ifdef CONFIG_IEEE80211R
564 if (sm
->ft_completed
) {
565 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
566 "FT authentication already completed - do not "
567 "start 4-way handshake");
570 #endif /* CONFIG_IEEE80211R */
573 os_memset(&sm
->key_replay
, 0, sizeof(sm
->key_replay
));
574 sm
->ReAuthenticationRequest
= TRUE
;
575 return wpa_sm_step(sm
);
578 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
579 "start authentication");
583 if (wpa_sm_step(sm
) == 1)
584 return 1; /* should not really happen */
586 sm
->AuthenticationRequest
= TRUE
;
587 return wpa_sm_step(sm
);
591 void wpa_auth_sta_no_wpa(struct wpa_state_machine
*sm
)
593 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
594 * reassociates back to the same AP while the previous entry for the
595 * STA has not yet been removed. */
599 sm
->wpa_key_mgmt
= 0;
603 static void wpa_free_sta_sm(struct wpa_state_machine
*sm
)
606 if (WPA_GET_BE32(sm
->ip_addr
)) {
608 wpa_printf(MSG_DEBUG
, "P2P: Free assigned IP "
609 "address %u.%u.%u.%u from " MACSTR
,
610 sm
->ip_addr
[0], sm
->ip_addr
[1],
611 sm
->ip_addr
[2], sm
->ip_addr
[3],
613 start
= WPA_GET_BE32(sm
->wpa_auth
->conf
.ip_addr_start
);
614 bitfield_clear(sm
->wpa_auth
->ip_pool
,
615 WPA_GET_BE32(sm
->ip_addr
) - start
);
617 #endif /* CONFIG_P2P */
618 if (sm
->GUpdateStationKeys
) {
619 sm
->group
->GKeyDoneStations
--;
620 sm
->GUpdateStationKeys
= FALSE
;
622 #ifdef CONFIG_IEEE80211R
623 os_free(sm
->assoc_resp_ftie
);
624 #endif /* CONFIG_IEEE80211R */
625 os_free(sm
->last_rx_eapol_key
);
631 void wpa_auth_sta_deinit(struct wpa_state_machine
*sm
)
636 if (sm
->wpa_auth
->conf
.wpa_strict_rekey
&& sm
->has_GTK
) {
637 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
638 "strict rekeying - force GTK rekey since STA "
640 eloop_cancel_timeout(wpa_rekey_gtk
, sm
->wpa_auth
, NULL
);
641 eloop_register_timeout(0, 500000, wpa_rekey_gtk
, sm
->wpa_auth
,
645 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
646 sm
->pending_1_of_4_timeout
= 0;
647 eloop_cancel_timeout(wpa_sm_call_step
, sm
, NULL
);
648 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
649 if (sm
->in_step_loop
) {
650 /* Must not free state machine while wpa_sm_step() is running.
651 * Freeing will be completed in the end of wpa_sm_step(). */
652 wpa_printf(MSG_DEBUG
, "WPA: Registering pending STA state "
653 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
654 sm
->pending_deinit
= 1;
660 static void wpa_request_new_ptk(struct wpa_state_machine
*sm
)
665 sm
->PTKRequest
= TRUE
;
670 static int wpa_replay_counter_valid(struct wpa_key_replay_counter
*ctr
,
671 const u8
*replay_counter
)
674 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
677 if (os_memcmp(replay_counter
, ctr
[i
].counter
,
678 WPA_REPLAY_COUNTER_LEN
) == 0)
685 static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter
*ctr
,
686 const u8
*replay_counter
)
689 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
691 (replay_counter
== NULL
||
692 os_memcmp(replay_counter
, ctr
[i
].counter
,
693 WPA_REPLAY_COUNTER_LEN
) == 0))
694 ctr
[i
].valid
= FALSE
;
699 #ifdef CONFIG_IEEE80211R
700 static int ft_check_msg_2_of_4(struct wpa_authenticator
*wpa_auth
,
701 struct wpa_state_machine
*sm
,
702 struct wpa_eapol_ie_parse
*kde
)
704 struct wpa_ie_data ie
;
705 struct rsn_mdie
*mdie
;
707 if (wpa_parse_wpa_ie_rsn(kde
->rsn_ie
, kde
->rsn_ie_len
, &ie
) < 0 ||
708 ie
.num_pmkid
!= 1 || ie
.pmkid
== NULL
) {
709 wpa_printf(MSG_DEBUG
, "FT: No PMKR1Name in "
710 "FT 4-way handshake message 2/4");
714 os_memcpy(sm
->sup_pmk_r1_name
, ie
.pmkid
, PMKID_LEN
);
715 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name from Supplicant",
716 sm
->sup_pmk_r1_name
, PMKID_LEN
);
718 if (!kde
->mdie
|| !kde
->ftie
) {
719 wpa_printf(MSG_DEBUG
, "FT: No %s in FT 4-way handshake "
720 "message 2/4", kde
->mdie
? "FTIE" : "MDIE");
724 mdie
= (struct rsn_mdie
*) (kde
->mdie
+ 2);
725 if (kde
->mdie
[1] < sizeof(struct rsn_mdie
) ||
726 os_memcmp(wpa_auth
->conf
.mobility_domain
, mdie
->mobility_domain
,
727 MOBILITY_DOMAIN_ID_LEN
) != 0) {
728 wpa_printf(MSG_DEBUG
, "FT: MDIE mismatch");
732 if (sm
->assoc_resp_ftie
&&
733 (kde
->ftie
[1] != sm
->assoc_resp_ftie
[1] ||
734 os_memcmp(kde
->ftie
, sm
->assoc_resp_ftie
,
735 2 + sm
->assoc_resp_ftie
[1]) != 0)) {
736 wpa_printf(MSG_DEBUG
, "FT: FTIE mismatch");
737 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in EAPOL-Key msg 2/4",
738 kde
->ftie
, kde
->ftie_len
);
739 wpa_hexdump(MSG_DEBUG
, "FT: FTIE in (Re)AssocResp",
740 sm
->assoc_resp_ftie
, 2 + sm
->assoc_resp_ftie
[1]);
746 #endif /* CONFIG_IEEE80211R */
749 static int wpa_receive_error_report(struct wpa_authenticator
*wpa_auth
,
750 struct wpa_state_machine
*sm
, int group
)
752 /* Supplicant reported a Michael MIC error */
753 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
754 "received EAPOL-Key Error Request "
755 "(STA detected Michael MIC failure (group=%d))",
758 if (group
&& wpa_auth
->conf
.wpa_group
!= WPA_CIPHER_TKIP
) {
759 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
760 "ignore Michael MIC failure report since "
761 "group cipher is not TKIP");
762 } else if (!group
&& sm
->pairwise
!= WPA_CIPHER_TKIP
) {
763 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
764 "ignore Michael MIC failure report since "
765 "pairwise cipher is not TKIP");
767 if (wpa_auth_mic_failure_report(wpa_auth
, sm
->addr
) > 0)
768 return 1; /* STA entry was removed */
769 sm
->dot11RSNAStatsTKIPRemoteMICFailures
++;
770 wpa_auth
->dot11RSNAStatsTKIPRemoteMICFailures
++;
774 * Error report is not a request for a new key handshake, but since
775 * Authenticator may do it, let's change the keys now anyway.
777 wpa_request_new_ptk(sm
);
782 void wpa_receive(struct wpa_authenticator
*wpa_auth
,
783 struct wpa_state_machine
*sm
,
784 u8
*data
, size_t data_len
)
786 struct ieee802_1x_hdr
*hdr
;
787 struct wpa_eapol_key
*key
;
788 u16 key_info
, key_data_length
;
789 enum { PAIRWISE_2
, PAIRWISE_4
, GROUP_2
, REQUEST
,
790 SMK_M1
, SMK_M3
, SMK_ERROR
} msg
;
792 struct wpa_eapol_ie_parse kde
;
794 const u8
*eapol_key_ie
;
795 size_t eapol_key_ie_len
;
797 if (wpa_auth
== NULL
|| !wpa_auth
->conf
.wpa
|| sm
== NULL
)
800 if (data_len
< sizeof(*hdr
) + sizeof(*key
))
803 hdr
= (struct ieee802_1x_hdr
*) data
;
804 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
805 key_info
= WPA_GET_BE16(key
->key_info
);
806 key_data_length
= WPA_GET_BE16(key
->key_data_length
);
807 wpa_printf(MSG_DEBUG
, "WPA: Received EAPOL-Key from " MACSTR
808 " key_info=0x%x type=%u key_data_length=%u",
809 MAC2STR(sm
->addr
), key_info
, key
->type
, key_data_length
);
810 if (key_data_length
> data_len
- sizeof(*hdr
) - sizeof(*key
)) {
811 wpa_printf(MSG_INFO
, "WPA: Invalid EAPOL-Key frame - "
812 "key_data overflow (%d > %lu)",
814 (unsigned long) (data_len
- sizeof(*hdr
) -
819 if (sm
->wpa
== WPA_VERSION_WPA2
) {
820 if (key
->type
== EAPOL_KEY_TYPE_WPA
) {
822 * Some deployed station implementations seem to send
823 * msg 4/4 with incorrect type value in WPA2 mode.
825 wpa_printf(MSG_DEBUG
, "Workaround: Allow EAPOL-Key "
826 "with unexpected WPA type in RSN mode");
827 } else if (key
->type
!= EAPOL_KEY_TYPE_RSN
) {
828 wpa_printf(MSG_DEBUG
, "Ignore EAPOL-Key with "
829 "unexpected type %d in RSN mode",
834 if (key
->type
!= EAPOL_KEY_TYPE_WPA
) {
835 wpa_printf(MSG_DEBUG
, "Ignore EAPOL-Key with "
836 "unexpected type %d in WPA mode",
842 wpa_hexdump(MSG_DEBUG
, "WPA: Received Key Nonce", key
->key_nonce
,
844 wpa_hexdump(MSG_DEBUG
, "WPA: Received Replay Counter",
845 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
847 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
850 if ((key_info
& (WPA_KEY_INFO_SMK_MESSAGE
| WPA_KEY_INFO_REQUEST
)) ==
851 (WPA_KEY_INFO_SMK_MESSAGE
| WPA_KEY_INFO_REQUEST
)) {
852 if (key_info
& WPA_KEY_INFO_ERROR
) {
854 msgtxt
= "SMK Error";
859 } else if (key_info
& WPA_KEY_INFO_SMK_MESSAGE
) {
862 } else if (key_info
& WPA_KEY_INFO_REQUEST
) {
865 } else if (!(key_info
& WPA_KEY_INFO_KEY_TYPE
)) {
867 msgtxt
= "2/2 Group";
868 } else if (key_data_length
== 0) {
870 msgtxt
= "4/4 Pairwise";
873 msgtxt
= "2/4 Pairwise";
876 /* TODO: key_info type validation for PeerKey */
877 if (msg
== REQUEST
|| msg
== PAIRWISE_2
|| msg
== PAIRWISE_4
||
879 u16 ver
= key_info
& WPA_KEY_INFO_TYPE_MASK
;
880 if (sm
->pairwise
== WPA_CIPHER_CCMP
||
881 sm
->pairwise
== WPA_CIPHER_GCMP
) {
882 if (wpa_use_aes_cmac(sm
) &&
883 sm
->wpa_key_mgmt
!= WPA_KEY_MGMT_OSEN
&&
884 ver
!= WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
885 wpa_auth_logger(wpa_auth
, sm
->addr
,
887 "advertised support for "
888 "AES-128-CMAC, but did not "
893 if (!wpa_use_aes_cmac(sm
) &&
894 ver
!= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
) {
895 wpa_auth_logger(wpa_auth
, sm
->addr
,
897 "did not use HMAC-SHA1-AES "
904 if (key_info
& WPA_KEY_INFO_REQUEST
) {
905 if (sm
->req_replay_counter_used
&&
906 os_memcmp(key
->replay_counter
, sm
->req_replay_counter
,
907 WPA_REPLAY_COUNTER_LEN
) <= 0) {
908 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_WARNING
,
909 "received EAPOL-Key request with "
915 if (!(key_info
& WPA_KEY_INFO_REQUEST
) &&
916 !wpa_replay_counter_valid(sm
->key_replay
, key
->replay_counter
)) {
919 if (msg
== PAIRWISE_2
&&
920 wpa_replay_counter_valid(sm
->prev_key_replay
,
921 key
->replay_counter
) &&
922 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
&&
923 os_memcmp(sm
->SNonce
, key
->key_nonce
, WPA_NONCE_LEN
) != 0)
926 * Some supplicant implementations (e.g., Windows XP
927 * WZC) update SNonce for each EAPOL-Key 2/4. This
928 * breaks the workaround on accepting any of the
929 * pending requests, so allow the SNonce to be updated
930 * even if we have already sent out EAPOL-Key 3/4.
932 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
933 "Process SNonce update from STA "
934 "based on retransmitted EAPOL-Key "
936 sm
->update_snonce
= 1;
937 wpa_replay_counter_mark_invalid(sm
->prev_key_replay
,
938 key
->replay_counter
);
939 goto continue_processing
;
942 if (msg
== PAIRWISE_2
&&
943 wpa_replay_counter_valid(sm
->prev_key_replay
,
944 key
->replay_counter
) &&
945 sm
->wpa_ptk_state
== WPA_PTK_PTKINITNEGOTIATING
) {
946 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
947 "ignore retransmitted EAPOL-Key %s - "
948 "SNonce did not change", msgtxt
);
950 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
951 "received EAPOL-Key %s with "
952 "unexpected replay counter", msgtxt
);
954 for (i
= 0; i
< RSNA_MAX_EAPOL_RETRIES
; i
++) {
955 if (!sm
->key_replay
[i
].valid
)
957 wpa_hexdump(MSG_DEBUG
, "pending replay counter",
958 sm
->key_replay
[i
].counter
,
959 WPA_REPLAY_COUNTER_LEN
);
961 wpa_hexdump(MSG_DEBUG
, "received replay counter",
962 key
->replay_counter
, WPA_REPLAY_COUNTER_LEN
);
969 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKSTART
&&
970 sm
->wpa_ptk_state
!= WPA_PTK_PTKCALCNEGOTIATING
&&
971 (!sm
->update_snonce
||
972 sm
->wpa_ptk_state
!= WPA_PTK_PTKINITNEGOTIATING
)) {
973 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
974 "received EAPOL-Key msg 2/4 in "
975 "invalid state (%d) - dropped",
979 random_add_randomness(key
->key_nonce
, WPA_NONCE_LEN
);
980 if (sm
->group
->reject_4way_hs_for_entropy
) {
982 * The system did not have enough entropy to generate
983 * strong random numbers. Reject the first 4-way
984 * handshake(s) and collect some entropy based on the
985 * information from it. Once enough entropy is
986 * available, the next atempt will trigger GMK/Key
987 * Counter update and the station will be allowed to
990 wpa_printf(MSG_DEBUG
, "WPA: Reject 4-way handshake to "
991 "collect more entropy for random number "
993 random_mark_pool_ready();
994 wpa_sta_disconnect(wpa_auth
, sm
->addr
);
997 if (wpa_parse_kde_ies((u8
*) (key
+ 1), key_data_length
,
999 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1000 "received EAPOL-Key msg 2/4 with "
1001 "invalid Key Data contents");
1005 eapol_key_ie
= kde
.rsn_ie
;
1006 eapol_key_ie_len
= kde
.rsn_ie_len
;
1007 } else if (kde
.osen
) {
1008 eapol_key_ie
= kde
.osen
;
1009 eapol_key_ie_len
= kde
.osen_len
;
1011 eapol_key_ie
= kde
.wpa_ie
;
1012 eapol_key_ie_len
= kde
.wpa_ie_len
;
1014 ft
= sm
->wpa
== WPA_VERSION_WPA2
&&
1015 wpa_key_mgmt_ft(sm
->wpa_key_mgmt
);
1016 if (sm
->wpa_ie
== NULL
||
1017 wpa_compare_rsn_ie(ft
,
1018 sm
->wpa_ie
, sm
->wpa_ie_len
,
1019 eapol_key_ie
, eapol_key_ie_len
)) {
1020 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1021 "WPA IE from (Re)AssocReq did not "
1022 "match with msg 2/4");
1024 wpa_hexdump(MSG_DEBUG
, "WPA IE in AssocReq",
1025 sm
->wpa_ie
, sm
->wpa_ie_len
);
1027 wpa_hexdump(MSG_DEBUG
, "WPA IE in msg 2/4",
1028 eapol_key_ie
, eapol_key_ie_len
);
1029 /* MLME-DEAUTHENTICATE.request */
1030 wpa_sta_disconnect(wpa_auth
, sm
->addr
);
1033 #ifdef CONFIG_IEEE80211R
1034 if (ft
&& ft_check_msg_2_of_4(wpa_auth
, sm
, &kde
) < 0) {
1035 wpa_sta_disconnect(wpa_auth
, sm
->addr
);
1038 #endif /* CONFIG_IEEE80211R */
1040 if (kde
.ip_addr_req
&& kde
.ip_addr_req
[0] &&
1041 wpa_auth
->ip_pool
&& WPA_GET_BE32(sm
->ip_addr
) == 0) {
1043 wpa_printf(MSG_DEBUG
, "P2P: IP address requested in "
1044 "EAPOL-Key exchange");
1045 idx
= bitfield_get_first_zero(wpa_auth
->ip_pool
);
1047 u32 start
= WPA_GET_BE32(wpa_auth
->conf
.
1049 bitfield_set(wpa_auth
->ip_pool
, idx
);
1050 WPA_PUT_BE32(sm
->ip_addr
, start
+ idx
);
1051 wpa_printf(MSG_DEBUG
, "P2P: Assigned IP "
1052 "address %u.%u.%u.%u to " MACSTR
,
1053 sm
->ip_addr
[0], sm
->ip_addr
[1],
1054 sm
->ip_addr
[2], sm
->ip_addr
[3],
1058 #endif /* CONFIG_P2P */
1061 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITNEGOTIATING
||
1063 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1064 "received EAPOL-Key msg 4/4 in "
1065 "invalid state (%d) - dropped",
1071 if (sm
->wpa_ptk_group_state
!= WPA_PTK_GROUP_REKEYNEGOTIATING
1072 || !sm
->PTK_valid
) {
1073 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1074 "received EAPOL-Key msg 2/2 in "
1075 "invalid state (%d) - dropped",
1076 sm
->wpa_ptk_group_state
);
1080 #ifdef CONFIG_PEERKEY
1084 if (!wpa_auth
->conf
.peerkey
) {
1085 wpa_printf(MSG_DEBUG
, "RSN: SMK M1/M3/Error, but "
1086 "PeerKey use disabled - ignoring message");
1089 if (!sm
->PTK_valid
) {
1090 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1091 "received EAPOL-Key msg SMK in "
1092 "invalid state - dropped");
1096 #else /* CONFIG_PEERKEY */
1100 return; /* STSL disabled - ignore SMK messages */
1101 #endif /* CONFIG_PEERKEY */
1106 wpa_auth_vlogger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1107 "received EAPOL-Key frame (%s)", msgtxt
);
1109 if (key_info
& WPA_KEY_INFO_ACK
) {
1110 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1111 "received invalid EAPOL-Key: Key Ack set");
1115 if (!(key_info
& WPA_KEY_INFO_MIC
)) {
1116 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1117 "received invalid EAPOL-Key: Key MIC not set");
1121 sm
->MICVerified
= FALSE
;
1122 if (sm
->PTK_valid
&& !sm
->update_snonce
) {
1123 if (wpa_verify_key_mic(&sm
->PTK
, data
, data_len
)) {
1124 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1125 "received EAPOL-Key with invalid MIC");
1128 sm
->MICVerified
= TRUE
;
1129 eloop_cancel_timeout(wpa_send_eapol_timeout
, wpa_auth
, sm
);
1130 sm
->pending_1_of_4_timeout
= 0;
1133 if (key_info
& WPA_KEY_INFO_REQUEST
) {
1134 if (sm
->MICVerified
) {
1135 sm
->req_replay_counter_used
= 1;
1136 os_memcpy(sm
->req_replay_counter
, key
->replay_counter
,
1137 WPA_REPLAY_COUNTER_LEN
);
1139 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1140 "received EAPOL-Key request with "
1146 * TODO: should decrypt key data field if encryption was used;
1147 * even though MAC address KDE is not normally encrypted,
1148 * supplicant is allowed to encrypt it.
1150 if (msg
== SMK_ERROR
) {
1151 #ifdef CONFIG_PEERKEY
1152 wpa_smk_error(wpa_auth
, sm
, key
);
1153 #endif /* CONFIG_PEERKEY */
1155 } else if (key_info
& WPA_KEY_INFO_ERROR
) {
1156 if (wpa_receive_error_report(
1158 !(key_info
& WPA_KEY_INFO_KEY_TYPE
)) > 0)
1159 return; /* STA entry was removed */
1160 } else if (key_info
& WPA_KEY_INFO_KEY_TYPE
) {
1161 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1162 "received EAPOL-Key Request for new "
1164 wpa_request_new_ptk(sm
);
1165 #ifdef CONFIG_PEERKEY
1166 } else if (msg
== SMK_M1
) {
1167 wpa_smk_m1(wpa_auth
, sm
, key
);
1168 #endif /* CONFIG_PEERKEY */
1169 } else if (key_data_length
> 0 &&
1170 wpa_parse_kde_ies((const u8
*) (key
+ 1),
1171 key_data_length
, &kde
) == 0 &&
1174 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1175 "received EAPOL-Key Request for GTK "
1177 eloop_cancel_timeout(wpa_rekey_gtk
, wpa_auth
, NULL
);
1178 wpa_rekey_gtk(wpa_auth
, NULL
);
1181 /* Do not allow the same key replay counter to be reused. */
1182 wpa_replay_counter_mark_invalid(sm
->key_replay
,
1183 key
->replay_counter
);
1185 if (msg
== PAIRWISE_2
) {
1187 * Maintain a copy of the pending EAPOL-Key frames in
1188 * case the EAPOL-Key frame was retransmitted. This is
1189 * needed to allow EAPOL-Key msg 2/4 reply to another
1190 * pending msg 1/4 to update the SNonce to work around
1191 * unexpected supplicant behavior.
1193 os_memcpy(sm
->prev_key_replay
, sm
->key_replay
,
1194 sizeof(sm
->key_replay
));
1196 os_memset(sm
->prev_key_replay
, 0,
1197 sizeof(sm
->prev_key_replay
));
1201 * Make sure old valid counters are not accepted anymore and
1202 * do not get copied again.
1204 wpa_replay_counter_mark_invalid(sm
->key_replay
, NULL
);
1207 #ifdef CONFIG_PEERKEY
1208 if (msg
== SMK_M3
) {
1209 wpa_smk_m3(wpa_auth
, sm
, key
);
1212 #endif /* CONFIG_PEERKEY */
1214 os_free(sm
->last_rx_eapol_key
);
1215 sm
->last_rx_eapol_key
= os_malloc(data_len
);
1216 if (sm
->last_rx_eapol_key
== NULL
)
1218 os_memcpy(sm
->last_rx_eapol_key
, data
, data_len
);
1219 sm
->last_rx_eapol_key_len
= data_len
;
1221 sm
->rx_eapol_key_secure
= !!(key_info
& WPA_KEY_INFO_SECURE
);
1222 sm
->EAPOLKeyReceived
= TRUE
;
1223 sm
->EAPOLKeyPairwise
= !!(key_info
& WPA_KEY_INFO_KEY_TYPE
);
1224 sm
->EAPOLKeyRequest
= !!(key_info
& WPA_KEY_INFO_REQUEST
);
1225 os_memcpy(sm
->SNonce
, key
->key_nonce
, WPA_NONCE_LEN
);
1230 static int wpa_gmk_to_gtk(const u8
*gmk
, const char *label
, const u8
*addr
,
1231 const u8
*gnonce
, u8
*gtk
, size_t gtk_len
)
1233 u8 data
[ETH_ALEN
+ WPA_NONCE_LEN
+ 8 + 16];
1237 /* GTK = PRF-X(GMK, "Group key expansion",
1238 * AA || GNonce || Time || random data)
1239 * The example described in the IEEE 802.11 standard uses only AA and
1240 * GNonce as inputs here. Add some more entropy since this derivation
1241 * is done only at the Authenticator and as such, does not need to be
1244 os_memcpy(data
, addr
, ETH_ALEN
);
1245 os_memcpy(data
+ ETH_ALEN
, gnonce
, WPA_NONCE_LEN
);
1246 pos
= data
+ ETH_ALEN
+ WPA_NONCE_LEN
;
1247 wpa_get_ntp_timestamp(pos
);
1249 if (random_get_bytes(pos
, 16) < 0)
1252 #ifdef CONFIG_IEEE80211W
1253 sha256_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
), gtk
, gtk_len
);
1254 #else /* CONFIG_IEEE80211W */
1255 if (sha1_prf(gmk
, WPA_GMK_LEN
, label
, data
, sizeof(data
), gtk
, gtk_len
)
1258 #endif /* CONFIG_IEEE80211W */
1264 static void wpa_send_eapol_timeout(void *eloop_ctx
, void *timeout_ctx
)
1266 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
1267 struct wpa_state_machine
*sm
= timeout_ctx
;
1269 sm
->pending_1_of_4_timeout
= 0;
1270 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
, "EAPOL-Key timeout");
1271 sm
->TimeoutEvt
= TRUE
;
1276 void __wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
1277 struct wpa_state_machine
*sm
, int key_info
,
1278 const u8
*key_rsc
, const u8
*nonce
,
1279 const u8
*kde
, size_t kde_len
,
1280 int keyidx
, int encr
, int force_version
)
1282 struct ieee802_1x_hdr
*hdr
;
1283 struct wpa_eapol_key
*key
;
1286 int key_data_len
, pad_len
= 0;
1288 int version
, pairwise
;
1291 len
= sizeof(struct ieee802_1x_hdr
) + sizeof(struct wpa_eapol_key
);
1294 version
= force_version
;
1295 else if (sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
)
1296 version
= WPA_KEY_INFO_TYPE_AKM_DEFINED
;
1297 else if (wpa_use_aes_cmac(sm
))
1298 version
= WPA_KEY_INFO_TYPE_AES_128_CMAC
;
1299 else if (sm
->pairwise
!= WPA_CIPHER_TKIP
)
1300 version
= WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
;
1302 version
= WPA_KEY_INFO_TYPE_HMAC_MD5_RC4
;
1304 pairwise
= !!(key_info
& WPA_KEY_INFO_KEY_TYPE
);
1306 wpa_printf(MSG_DEBUG
, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1307 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1310 (key_info
& WPA_KEY_INFO_SECURE
) ? 1 : 0,
1311 (key_info
& WPA_KEY_INFO_MIC
) ? 1 : 0,
1312 (key_info
& WPA_KEY_INFO_ACK
) ? 1 : 0,
1313 (key_info
& WPA_KEY_INFO_INSTALL
) ? 1 : 0,
1314 pairwise
, (unsigned long) kde_len
, keyidx
, encr
);
1316 key_data_len
= kde_len
;
1318 if ((version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1319 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
||
1320 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) && encr
) {
1321 pad_len
= key_data_len
% 8;
1323 pad_len
= 8 - pad_len
;
1324 key_data_len
+= pad_len
+ 8;
1327 len
+= key_data_len
;
1329 hdr
= os_zalloc(len
);
1332 hdr
->version
= wpa_auth
->conf
.eapol_version
;
1333 hdr
->type
= IEEE802_1X_TYPE_EAPOL_KEY
;
1334 hdr
->length
= host_to_be16(len
- sizeof(*hdr
));
1335 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1337 key
->type
= sm
->wpa
== WPA_VERSION_WPA2
?
1338 EAPOL_KEY_TYPE_RSN
: EAPOL_KEY_TYPE_WPA
;
1339 key_info
|= version
;
1340 if (encr
&& sm
->wpa
== WPA_VERSION_WPA2
)
1341 key_info
|= WPA_KEY_INFO_ENCR_KEY_DATA
;
1342 if (sm
->wpa
!= WPA_VERSION_WPA2
)
1343 key_info
|= keyidx
<< WPA_KEY_INFO_KEY_INDEX_SHIFT
;
1344 WPA_PUT_BE16(key
->key_info
, key_info
);
1346 alg
= pairwise
? sm
->pairwise
: wpa_auth
->conf
.wpa_group
;
1347 WPA_PUT_BE16(key
->key_length
, wpa_cipher_key_len(alg
));
1348 if (key_info
& WPA_KEY_INFO_SMK_MESSAGE
)
1349 WPA_PUT_BE16(key
->key_length
, 0);
1351 /* FIX: STSL: what to use as key_replay_counter? */
1352 for (i
= RSNA_MAX_EAPOL_RETRIES
- 1; i
> 0; i
--) {
1353 sm
->key_replay
[i
].valid
= sm
->key_replay
[i
- 1].valid
;
1354 os_memcpy(sm
->key_replay
[i
].counter
,
1355 sm
->key_replay
[i
- 1].counter
,
1356 WPA_REPLAY_COUNTER_LEN
);
1358 inc_byte_array(sm
->key_replay
[0].counter
, WPA_REPLAY_COUNTER_LEN
);
1359 os_memcpy(key
->replay_counter
, sm
->key_replay
[0].counter
,
1360 WPA_REPLAY_COUNTER_LEN
);
1361 sm
->key_replay
[0].valid
= TRUE
;
1364 os_memcpy(key
->key_nonce
, nonce
, WPA_NONCE_LEN
);
1367 os_memcpy(key
->key_rsc
, key_rsc
, WPA_KEY_RSC_LEN
);
1370 os_memcpy(key
+ 1, kde
, kde_len
);
1371 WPA_PUT_BE16(key
->key_data_length
, kde_len
);
1372 } else if (encr
&& kde
) {
1373 buf
= os_zalloc(key_data_len
);
1379 os_memcpy(pos
, kde
, kde_len
);
1385 wpa_hexdump_key(MSG_DEBUG
, "Plaintext EAPOL-Key Key Data",
1387 if (version
== WPA_KEY_INFO_TYPE_HMAC_SHA1_AES
||
1388 sm
->wpa_key_mgmt
== WPA_KEY_MGMT_OSEN
||
1389 version
== WPA_KEY_INFO_TYPE_AES_128_CMAC
) {
1390 if (aes_wrap(sm
->PTK
.kek
, (key_data_len
- 8) / 8, buf
,
1391 (u8
*) (key
+ 1))) {
1396 WPA_PUT_BE16(key
->key_data_length
, key_data_len
);
1399 os_memcpy(key
->key_iv
,
1400 sm
->group
->Counter
+ WPA_NONCE_LEN
- 16, 16);
1401 inc_byte_array(sm
->group
->Counter
, WPA_NONCE_LEN
);
1402 os_memcpy(ek
, key
->key_iv
, 16);
1403 os_memcpy(ek
+ 16, sm
->PTK
.kek
, 16);
1404 os_memcpy(key
+ 1, buf
, key_data_len
);
1405 rc4_skip(ek
, 32, 256, (u8
*) (key
+ 1), key_data_len
);
1406 WPA_PUT_BE16(key
->key_data_length
, key_data_len
);
1411 if (key_info
& WPA_KEY_INFO_MIC
) {
1412 if (!sm
->PTK_valid
) {
1413 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1414 "PTK not valid when sending EAPOL-Key "
1419 wpa_eapol_key_mic(sm
->PTK
.kck
, version
, (u8
*) hdr
, len
,
1421 #ifdef CONFIG_TESTING_OPTIONS
1423 wpa_auth
->conf
.corrupt_gtk_rekey_mic_probability
> 0.0d
&&
1425 wpa_auth
->conf
.corrupt_gtk_rekey_mic_probability
) {
1426 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_INFO
,
1427 "Corrupting group EAPOL-Key Key MIC");
1430 #endif /* CONFIG_TESTING_OPTIONS */
1433 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_inc_EapolFramesTx
,
1435 wpa_auth_send_eapol(wpa_auth
, sm
->addr
, (u8
*) hdr
, len
,
1441 static void wpa_send_eapol(struct wpa_authenticator
*wpa_auth
,
1442 struct wpa_state_machine
*sm
, int key_info
,
1443 const u8
*key_rsc
, const u8
*nonce
,
1444 const u8
*kde
, size_t kde_len
,
1445 int keyidx
, int encr
)
1448 int pairwise
= key_info
& WPA_KEY_INFO_KEY_TYPE
;
1454 __wpa_send_eapol(wpa_auth
, sm
, key_info
, key_rsc
, nonce
, kde
, kde_len
,
1457 ctr
= pairwise
? sm
->TimeoutCtr
: sm
->GTimeoutCtr
;
1458 if (ctr
== 1 && wpa_auth
->conf
.tx_status
)
1459 timeout_ms
= pairwise
? eapol_key_timeout_first
:
1460 eapol_key_timeout_first_group
;
1462 timeout_ms
= eapol_key_timeout_subseq
;
1463 if (pairwise
&& ctr
== 1 && !(key_info
& WPA_KEY_INFO_MIC
))
1464 sm
->pending_1_of_4_timeout
= 1;
1465 wpa_printf(MSG_DEBUG
, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1466 "counter %d)", timeout_ms
, ctr
);
1467 eloop_register_timeout(timeout_ms
/ 1000, (timeout_ms
% 1000) * 1000,
1468 wpa_send_eapol_timeout
, wpa_auth
, sm
);
1472 static int wpa_verify_key_mic(struct wpa_ptk
*PTK
, u8
*data
, size_t data_len
)
1474 struct ieee802_1x_hdr
*hdr
;
1475 struct wpa_eapol_key
*key
;
1480 if (data_len
< sizeof(*hdr
) + sizeof(*key
))
1483 hdr
= (struct ieee802_1x_hdr
*) data
;
1484 key
= (struct wpa_eapol_key
*) (hdr
+ 1);
1485 key_info
= WPA_GET_BE16(key
->key_info
);
1486 os_memcpy(mic
, key
->key_mic
, 16);
1487 os_memset(key
->key_mic
, 0, 16);
1488 if (wpa_eapol_key_mic(PTK
->kck
, key_info
& WPA_KEY_INFO_TYPE_MASK
,
1489 data
, data_len
, key
->key_mic
) ||
1490 os_memcmp(mic
, key
->key_mic
, 16) != 0)
1492 os_memcpy(key
->key_mic
, mic
, 16);
1497 void wpa_remove_ptk(struct wpa_state_machine
*sm
)
1499 sm
->PTK_valid
= FALSE
;
1500 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1501 wpa_auth_set_key(sm
->wpa_auth
, 0, WPA_ALG_NONE
, sm
->addr
, 0, NULL
, 0);
1502 sm
->pairwise_set
= FALSE
;
1503 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
1507 int wpa_auth_sm_event(struct wpa_state_machine
*sm
, wpa_event event
)
1514 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1515 "event %d notification", event
);
1523 sm
->DeauthenticationRequest
= TRUE
;
1526 case WPA_REAUTH_EAPOL
:
1529 * When using WPS, we may end up here if the STA
1530 * manages to re-associate without the previous STA
1531 * entry getting removed. Consequently, we need to make
1532 * sure that the WPA state machines gets initialized
1533 * properly at this point.
1535 wpa_printf(MSG_DEBUG
, "WPA state machine had not been "
1536 "started - initialize now");
1539 if (wpa_sm_step(sm
) == 1)
1540 return 1; /* should not really happen */
1542 sm
->AuthenticationRequest
= TRUE
;
1545 if (sm
->GUpdateStationKeys
) {
1547 * Reauthentication cancels the pending group key
1548 * update for this STA.
1550 sm
->group
->GKeyDoneStations
--;
1551 sm
->GUpdateStationKeys
= FALSE
;
1552 sm
->PtkGroupInit
= TRUE
;
1554 sm
->ReAuthenticationRequest
= TRUE
;
1557 #ifdef CONFIG_IEEE80211R
1558 wpa_printf(MSG_DEBUG
, "FT: Retry PTK configuration "
1559 "after association");
1560 wpa_ft_install_ptk(sm
);
1562 /* Using FT protocol, not WPA auth state machine */
1563 sm
->ft_completed
= 1;
1565 #else /* CONFIG_IEEE80211R */
1567 #endif /* CONFIG_IEEE80211R */
1570 #ifdef CONFIG_IEEE80211R
1571 sm
->ft_completed
= 0;
1572 #endif /* CONFIG_IEEE80211R */
1574 #ifdef CONFIG_IEEE80211W
1575 if (sm
->mgmt_frame_prot
&& event
== WPA_AUTH
)
1577 #endif /* CONFIG_IEEE80211W */
1580 sm
->PTK_valid
= FALSE
;
1581 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1583 if (event
!= WPA_REAUTH_EAPOL
)
1587 return wpa_sm_step(sm
);
1591 SM_STATE(WPA_PTK
, INITIALIZE
)
1593 SM_ENTRY_MA(WPA_PTK
, INITIALIZE
, wpa_ptk
);
1595 /* Init flag is not cleared here, so avoid busy
1596 * loop by claiming nothing changed. */
1597 sm
->changed
= FALSE
;
1601 if (sm
->GUpdateStationKeys
)
1602 sm
->group
->GKeyDoneStations
--;
1603 sm
->GUpdateStationKeys
= FALSE
;
1604 if (sm
->wpa
== WPA_VERSION_WPA
)
1605 sm
->PInitAKeys
= FALSE
;
1606 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1607 * Local AA > Remote AA)) */) {
1610 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 0);
1612 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
, 0);
1614 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
1615 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
1616 WPA_EAPOL_authorized
, 0);
1621 SM_STATE(WPA_PTK
, DISCONNECT
)
1623 SM_ENTRY_MA(WPA_PTK
, DISCONNECT
, wpa_ptk
);
1624 sm
->Disconnect
= FALSE
;
1625 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
);
1629 SM_STATE(WPA_PTK
, DISCONNECTED
)
1631 SM_ENTRY_MA(WPA_PTK
, DISCONNECTED
, wpa_ptk
);
1632 sm
->DeauthenticationRequest
= FALSE
;
1636 SM_STATE(WPA_PTK
, AUTHENTICATION
)
1638 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION
, wpa_ptk
);
1639 os_memset(&sm
->PTK
, 0, sizeof(sm
->PTK
));
1640 sm
->PTK_valid
= FALSE
;
1641 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portControl_Auto
,
1643 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portEnabled
, 1);
1644 sm
->AuthenticationRequest
= FALSE
;
1648 static void wpa_group_ensure_init(struct wpa_authenticator
*wpa_auth
,
1649 struct wpa_group
*group
)
1651 if (group
->first_sta_seen
)
1654 * System has run bit further than at the time hostapd was started
1655 * potentially very early during boot up. This provides better chances
1656 * of collecting more randomness on embedded systems. Re-initialize the
1657 * GMK and Counter here to improve their strength if there was not
1658 * enough entropy available immediately after system startup.
1660 wpa_printf(MSG_DEBUG
, "WPA: Re-initialize GMK/Counter on first "
1662 if (random_pool_ready() != 1) {
1663 wpa_printf(MSG_INFO
, "WPA: Not enough entropy in random pool "
1664 "to proceed - reject first 4-way handshake");
1665 group
->reject_4way_hs_for_entropy
= TRUE
;
1667 group
->first_sta_seen
= TRUE
;
1668 group
->reject_4way_hs_for_entropy
= FALSE
;
1671 wpa_group_init_gmk_and_counter(wpa_auth
, group
);
1672 wpa_gtk_update(wpa_auth
, group
);
1673 wpa_group_config_group_keys(wpa_auth
, group
);
1677 SM_STATE(WPA_PTK
, AUTHENTICATION2
)
1679 SM_ENTRY_MA(WPA_PTK
, AUTHENTICATION2
, wpa_ptk
);
1681 wpa_group_ensure_init(sm
->wpa_auth
, sm
->group
);
1682 sm
->ReAuthenticationRequest
= FALSE
;
1685 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1686 * ambiguous. The Authenticator state machine uses a counter that is
1687 * incremented by one for each 4-way handshake. However, the security
1688 * analysis of 4-way handshake points out that unpredictable nonces
1689 * help in preventing precomputation attacks. Instead of the state
1690 * machine definition, use an unpredictable nonce value here to provide
1691 * stronger protection against potential precomputation attacks.
1693 if (random_get_bytes(sm
->ANonce
, WPA_NONCE_LEN
)) {
1694 wpa_printf(MSG_ERROR
, "WPA: Failed to get random data for "
1696 sm
->Disconnect
= TRUE
;
1699 wpa_hexdump(MSG_DEBUG
, "WPA: Assign ANonce", sm
->ANonce
,
1701 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1702 * logical place than INITIALIZE since AUTHENTICATION2 can be
1703 * re-entered on ReAuthenticationRequest without going through
1709 SM_STATE(WPA_PTK
, INITPMK
)
1711 u8 msk
[2 * PMK_LEN
];
1712 size_t len
= 2 * PMK_LEN
;
1714 SM_ENTRY_MA(WPA_PTK
, INITPMK
, wpa_ptk
);
1715 #ifdef CONFIG_IEEE80211R
1717 #endif /* CONFIG_IEEE80211R */
1719 wpa_printf(MSG_DEBUG
, "WPA: PMK from PMKSA cache");
1720 os_memcpy(sm
->PMK
, sm
->pmksa
->pmk
, PMK_LEN
);
1721 } else if (wpa_auth_get_msk(sm
->wpa_auth
, sm
->addr
, msk
, &len
) == 0) {
1722 wpa_printf(MSG_DEBUG
, "WPA: PMK from EAPOL state machine "
1723 "(len=%lu)", (unsigned long) len
);
1724 os_memcpy(sm
->PMK
, msk
, PMK_LEN
);
1725 #ifdef CONFIG_IEEE80211R
1726 if (len
>= 2 * PMK_LEN
) {
1727 os_memcpy(sm
->xxkey
, msk
+ PMK_LEN
, PMK_LEN
);
1728 sm
->xxkey_len
= PMK_LEN
;
1730 #endif /* CONFIG_IEEE80211R */
1732 wpa_printf(MSG_DEBUG
, "WPA: Could not get PMK");
1735 sm
->req_replay_counter_used
= 0;
1736 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1737 * will break reauthentication since EAPOL state machines may not be
1738 * get into AUTHENTICATING state that clears keyRun before WPA state
1739 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1740 * state and takes PMK from the previously used AAA Key. This will
1741 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1742 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1743 * be good workaround for this issue. */
1744 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyRun
, 0);
1748 SM_STATE(WPA_PTK
, INITPSK
)
1751 SM_ENTRY_MA(WPA_PTK
, INITPSK
, wpa_ptk
);
1752 psk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, sm
->p2p_dev_addr
, NULL
);
1754 os_memcpy(sm
->PMK
, psk
, PMK_LEN
);
1755 #ifdef CONFIG_IEEE80211R
1756 os_memcpy(sm
->xxkey
, psk
, PMK_LEN
);
1757 sm
->xxkey_len
= PMK_LEN
;
1758 #endif /* CONFIG_IEEE80211R */
1760 sm
->req_replay_counter_used
= 0;
1764 SM_STATE(WPA_PTK
, PTKSTART
)
1766 u8 buf
[2 + RSN_SELECTOR_LEN
+ PMKID_LEN
], *pmkid
= NULL
;
1767 size_t pmkid_len
= 0;
1769 SM_ENTRY_MA(WPA_PTK
, PTKSTART
, wpa_ptk
);
1770 sm
->PTKRequest
= FALSE
;
1771 sm
->TimeoutEvt
= FALSE
;
1774 if (sm
->TimeoutCtr
> (int) dot11RSNAConfigPairwiseUpdateCount
) {
1775 /* No point in sending the EAPOL-Key - we will disconnect
1776 * immediately following this. */
1780 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1781 "sending 1/4 msg of 4-Way Handshake");
1783 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1784 * one possible PSK for this STA.
1786 if (sm
->wpa
== WPA_VERSION_WPA2
&&
1787 wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) &&
1788 sm
->wpa_key_mgmt
!= WPA_KEY_MGMT_OSEN
) {
1790 pmkid_len
= 2 + RSN_SELECTOR_LEN
+ PMKID_LEN
;
1791 pmkid
[0] = WLAN_EID_VENDOR_SPECIFIC
;
1792 pmkid
[1] = RSN_SELECTOR_LEN
+ PMKID_LEN
;
1793 RSN_SELECTOR_PUT(&pmkid
[2], RSN_KEY_DATA_PMKID
);
1795 os_memcpy(&pmkid
[2 + RSN_SELECTOR_LEN
],
1796 sm
->pmksa
->pmkid
, PMKID_LEN
);
1799 * Calculate PMKID since no PMKSA cache entry was
1800 * available with pre-calculated PMKID.
1802 rsn_pmkid(sm
->PMK
, PMK_LEN
, sm
->wpa_auth
->addr
,
1803 sm
->addr
, &pmkid
[2 + RSN_SELECTOR_LEN
],
1804 wpa_key_mgmt_sha256(sm
->wpa_key_mgmt
));
1807 wpa_send_eapol(sm
->wpa_auth
, sm
,
1808 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_KEY_TYPE
, NULL
,
1809 sm
->ANonce
, pmkid
, pmkid_len
, 0, 0);
1813 static int wpa_derive_ptk(struct wpa_state_machine
*sm
, const u8
*pmk
,
1814 struct wpa_ptk
*ptk
)
1816 size_t ptk_len
= wpa_cipher_key_len(sm
->pairwise
) + 32;
1817 #ifdef CONFIG_IEEE80211R
1818 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
1819 return wpa_auth_derive_ptk_ft(sm
, pmk
, ptk
, ptk_len
);
1820 #endif /* CONFIG_IEEE80211R */
1822 wpa_pmk_to_ptk(pmk
, PMK_LEN
, "Pairwise key expansion",
1823 sm
->wpa_auth
->addr
, sm
->addr
, sm
->ANonce
, sm
->SNonce
,
1824 (u8
*) ptk
, ptk_len
,
1825 wpa_key_mgmt_sha256(sm
->wpa_key_mgmt
));
1831 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING
)
1835 const u8
*pmk
= NULL
;
1837 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING
, wpa_ptk
);
1838 sm
->EAPOLKeyReceived
= FALSE
;
1839 sm
->update_snonce
= FALSE
;
1841 /* WPA with IEEE 802.1X: use the derived PMK from EAP
1842 * WPA-PSK: iterate through possible PSKs and select the one matching
1845 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
1846 pmk
= wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
,
1847 sm
->p2p_dev_addr
, pmk
);
1853 wpa_derive_ptk(sm
, pmk
, &PTK
);
1855 if (wpa_verify_key_mic(&PTK
, sm
->last_rx_eapol_key
,
1856 sm
->last_rx_eapol_key_len
) == 0) {
1861 if (!wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
))
1866 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1867 "invalid MIC in msg 2/4 of 4-Way Handshake");
1871 #ifdef CONFIG_IEEE80211R
1872 if (sm
->wpa
== WPA_VERSION_WPA2
&& wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
1874 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
1875 * with the value we derived.
1877 if (os_memcmp(sm
->sup_pmk_r1_name
, sm
->pmk_r1_name
,
1878 WPA_PMK_NAME_LEN
) != 0) {
1879 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
1880 "PMKR1Name mismatch in FT 4-way "
1882 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name from "
1884 sm
->sup_pmk_r1_name
, WPA_PMK_NAME_LEN
);
1885 wpa_hexdump(MSG_DEBUG
, "FT: Derived PMKR1Name",
1886 sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
);
1890 #endif /* CONFIG_IEEE80211R */
1892 sm
->pending_1_of_4_timeout
= 0;
1893 eloop_cancel_timeout(wpa_send_eapol_timeout
, sm
->wpa_auth
, sm
);
1895 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
1896 /* PSK may have changed from the previous choice, so update
1897 * state machine data based on whatever PSK was selected here.
1899 os_memcpy(sm
->PMK
, pmk
, PMK_LEN
);
1902 sm
->MICVerified
= TRUE
;
1904 os_memcpy(&sm
->PTK
, &PTK
, sizeof(PTK
));
1905 sm
->PTK_valid
= TRUE
;
1909 SM_STATE(WPA_PTK
, PTKCALCNEGOTIATING2
)
1911 SM_ENTRY_MA(WPA_PTK
, PTKCALCNEGOTIATING2
, wpa_ptk
);
1916 #ifdef CONFIG_IEEE80211W
1918 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
)
1920 if (sm
->mgmt_frame_prot
) {
1921 return 2 + RSN_SELECTOR_LEN
+ sizeof(struct wpa_igtk_kde
);
1928 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
)
1930 struct wpa_igtk_kde igtk
;
1931 struct wpa_group
*gsm
= sm
->group
;
1932 u8 rsc
[WPA_KEY_RSC_LEN
];
1934 if (!sm
->mgmt_frame_prot
)
1937 igtk
.keyid
[0] = gsm
->GN_igtk
;
1939 if (gsm
->wpa_group_state
!= WPA_GROUP_SETKEYSDONE
||
1940 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, rsc
) < 0)
1941 os_memset(igtk
.pn
, 0, sizeof(igtk
.pn
));
1943 os_memcpy(igtk
.pn
, rsc
, sizeof(igtk
.pn
));
1944 os_memcpy(igtk
.igtk
, gsm
->IGTK
[gsm
->GN_igtk
- 4], WPA_IGTK_LEN
);
1945 if (sm
->wpa_auth
->conf
.disable_gtk
) {
1947 * Provide unique random IGTK to each STA to prevent use of
1950 if (random_get_bytes(igtk
.igtk
, WPA_IGTK_LEN
) < 0)
1953 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_IGTK
,
1954 (const u8
*) &igtk
, sizeof(igtk
), NULL
, 0);
1959 #else /* CONFIG_IEEE80211W */
1961 static int ieee80211w_kde_len(struct wpa_state_machine
*sm
)
1967 static u8
* ieee80211w_kde_add(struct wpa_state_machine
*sm
, u8
*pos
)
1972 #endif /* CONFIG_IEEE80211W */
1975 SM_STATE(WPA_PTK
, PTKINITNEGOTIATING
)
1977 u8 rsc
[WPA_KEY_RSC_LEN
], *_rsc
, *gtk
, *kde
, *pos
, dummy_gtk
[32];
1978 size_t gtk_len
, kde_len
;
1979 struct wpa_group
*gsm
= sm
->group
;
1981 int wpa_ie_len
, secure
, keyidx
, encr
= 0;
1983 SM_ENTRY_MA(WPA_PTK
, PTKINITNEGOTIATING
, wpa_ptk
);
1984 sm
->TimeoutEvt
= FALSE
;
1987 if (sm
->TimeoutCtr
> (int) dot11RSNAConfigPairwiseUpdateCount
) {
1988 /* No point in sending the EAPOL-Key - we will disconnect
1989 * immediately following this. */
1993 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
1994 GTK[GN], IGTK, [FTIE], [TIE * 2])
1996 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
1997 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
1998 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
1999 wpa_ie
= sm
->wpa_auth
->wpa_ie
;
2000 wpa_ie_len
= sm
->wpa_auth
->wpa_ie_len
;
2001 if (sm
->wpa
== WPA_VERSION_WPA
&&
2002 (sm
->wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
) &&
2003 wpa_ie_len
> wpa_ie
[1] + 2 && wpa_ie
[0] == WLAN_EID_RSN
) {
2004 /* WPA-only STA, remove RSN IE */
2005 wpa_ie
= wpa_ie
+ wpa_ie
[1] + 2;
2006 wpa_ie_len
= wpa_ie
[1] + 2;
2008 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2009 "sending 3/4 msg of 4-Way Handshake");
2010 if (sm
->wpa
== WPA_VERSION_WPA2
) {
2011 /* WPA2 send GTK in the 4-way handshake */
2013 gtk
= gsm
->GTK
[gsm
->GN
- 1];
2014 gtk_len
= gsm
->GTK_len
;
2015 if (sm
->wpa_auth
->conf
.disable_gtk
) {
2017 * Provide unique random GTK to each STA to prevent use
2018 * of GTK in the BSS.
2020 if (random_get_bytes(dummy_gtk
, gtk_len
) < 0)
2028 /* WPA does not include GTK in msg 3/4 */
2034 if (sm
->rx_eapol_key_secure
) {
2036 * It looks like Windows 7 supplicant tries to use
2037 * Secure bit in msg 2/4 after having reported Michael
2038 * MIC failure and it then rejects the 4-way handshake
2039 * if msg 3/4 does not set Secure bit. Work around this
2040 * by setting the Secure bit here even in the case of
2041 * WPA if the supplicant used it first.
2043 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2044 "STA used Secure bit in WPA msg 2/4 - "
2045 "set Secure for 3/4 as workaround");
2050 kde_len
= wpa_ie_len
+ ieee80211w_kde_len(sm
);
2052 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 2 + gtk_len
;
2053 #ifdef CONFIG_IEEE80211R
2054 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
2055 kde_len
+= 2 + PMKID_LEN
; /* PMKR1Name into RSN IE */
2056 kde_len
+= 300; /* FTIE + 2 * TIE */
2058 #endif /* CONFIG_IEEE80211R */
2060 if (WPA_GET_BE32(sm
->ip_addr
) > 0)
2061 kde_len
+= 2 + RSN_SELECTOR_LEN
+ 3 * 4;
2062 #endif /* CONFIG_P2P */
2063 kde
= os_malloc(kde_len
);
2068 os_memcpy(pos
, wpa_ie
, wpa_ie_len
);
2070 #ifdef CONFIG_IEEE80211R
2071 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
2072 int res
= wpa_insert_pmkid(kde
, pos
- kde
, sm
->pmk_r1_name
);
2074 wpa_printf(MSG_ERROR
, "FT: Failed to insert "
2075 "PMKR1Name into RSN IE in EAPOL-Key data");
2081 #endif /* CONFIG_IEEE80211R */
2084 hdr
[0] = keyidx
& 0x03;
2086 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
2089 pos
= ieee80211w_kde_add(sm
, pos
);
2091 #ifdef CONFIG_IEEE80211R
2092 if (wpa_key_mgmt_ft(sm
->wpa_key_mgmt
)) {
2094 struct wpa_auth_config
*conf
;
2096 conf
= &sm
->wpa_auth
->conf
;
2097 res
= wpa_write_ftie(conf
, conf
->r0_key_holder
,
2098 conf
->r0_key_holder_len
,
2099 NULL
, NULL
, pos
, kde
+ kde_len
- pos
,
2102 wpa_printf(MSG_ERROR
, "FT: Failed to insert FTIE "
2103 "into EAPOL-Key Key Data");
2109 /* TIE[ReassociationDeadline] (TU) */
2110 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
2112 *pos
++ = WLAN_TIMEOUT_REASSOC_DEADLINE
;
2113 WPA_PUT_LE32(pos
, conf
->reassociation_deadline
);
2116 /* TIE[KeyLifetime] (seconds) */
2117 *pos
++ = WLAN_EID_TIMEOUT_INTERVAL
;
2119 *pos
++ = WLAN_TIMEOUT_KEY_LIFETIME
;
2120 WPA_PUT_LE32(pos
, conf
->r0_key_lifetime
* 60);
2123 #endif /* CONFIG_IEEE80211R */
2125 if (WPA_GET_BE32(sm
->ip_addr
) > 0) {
2127 os_memcpy(addr
, sm
->ip_addr
, 4);
2128 os_memcpy(addr
+ 4, sm
->wpa_auth
->conf
.ip_addr_mask
, 4);
2129 os_memcpy(addr
+ 8, sm
->wpa_auth
->conf
.ip_addr_go
, 4);
2130 pos
= wpa_add_kde(pos
, WFA_KEY_DATA_IP_ADDR_ALLOC
,
2131 addr
, sizeof(addr
), NULL
, 0);
2133 #endif /* CONFIG_P2P */
2135 wpa_send_eapol(sm
->wpa_auth
, sm
,
2136 (secure
? WPA_KEY_INFO_SECURE
: 0) | WPA_KEY_INFO_MIC
|
2137 WPA_KEY_INFO_ACK
| WPA_KEY_INFO_INSTALL
|
2138 WPA_KEY_INFO_KEY_TYPE
,
2139 _rsc
, sm
->ANonce
, kde
, pos
- kde
, keyidx
, encr
);
2144 SM_STATE(WPA_PTK
, PTKINITDONE
)
2146 SM_ENTRY_MA(WPA_PTK
, PTKINITDONE
, wpa_ptk
);
2147 sm
->EAPOLKeyReceived
= FALSE
;
2149 enum wpa_alg alg
= wpa_cipher_to_alg(sm
->pairwise
);
2150 int klen
= wpa_cipher_key_len(sm
->pairwise
);
2151 if (wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
, 0,
2152 sm
->PTK
.tk1
, klen
)) {
2153 wpa_sta_disconnect(sm
->wpa_auth
, sm
->addr
);
2156 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
2157 sm
->pairwise_set
= TRUE
;
2159 if (sm
->wpa_auth
->conf
.wpa_ptk_rekey
) {
2160 eloop_cancel_timeout(wpa_rekey_ptk
, sm
->wpa_auth
, sm
);
2161 eloop_register_timeout(sm
->wpa_auth
->conf
.
2162 wpa_ptk_rekey
, 0, wpa_rekey_ptk
,
2166 if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)) {
2167 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
2168 WPA_EAPOL_authorized
, 1);
2172 if (0 /* IBSS == TRUE */) {
2174 if (sm
->keycount
== 2) {
2175 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
,
2176 WPA_EAPOL_portValid
, 1);
2179 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_portValid
,
2182 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyAvailable
, 0);
2183 wpa_auth_set_eapol(sm
->wpa_auth
, sm
->addr
, WPA_EAPOL_keyDone
, 1);
2184 if (sm
->wpa
== WPA_VERSION_WPA
)
2185 sm
->PInitAKeys
= TRUE
;
2188 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2189 "pairwise key handshake completed (%s)",
2190 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
2192 #ifdef CONFIG_IEEE80211R
2193 wpa_ft_push_pmk_r1(sm
->wpa_auth
, sm
->addr
);
2194 #endif /* CONFIG_IEEE80211R */
2200 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2203 SM_ENTER(WPA_PTK
, INITIALIZE
);
2204 else if (sm
->Disconnect
2205 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
2206 wpa_auth_logger(wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2207 "WPA_PTK: sm->Disconnect");
2208 SM_ENTER(WPA_PTK
, DISCONNECT
);
2210 else if (sm
->DeauthenticationRequest
)
2211 SM_ENTER(WPA_PTK
, DISCONNECTED
);
2212 else if (sm
->AuthenticationRequest
)
2213 SM_ENTER(WPA_PTK
, AUTHENTICATION
);
2214 else if (sm
->ReAuthenticationRequest
)
2215 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
2216 else if (sm
->PTKRequest
)
2217 SM_ENTER(WPA_PTK
, PTKSTART
);
2218 else switch (sm
->wpa_ptk_state
) {
2219 case WPA_PTK_INITIALIZE
:
2221 case WPA_PTK_DISCONNECT
:
2222 SM_ENTER(WPA_PTK
, DISCONNECTED
);
2224 case WPA_PTK_DISCONNECTED
:
2225 SM_ENTER(WPA_PTK
, INITIALIZE
);
2227 case WPA_PTK_AUTHENTICATION
:
2228 SM_ENTER(WPA_PTK
, AUTHENTICATION2
);
2230 case WPA_PTK_AUTHENTICATION2
:
2231 if (wpa_key_mgmt_wpa_ieee8021x(sm
->wpa_key_mgmt
) &&
2232 wpa_auth_get_eapol(sm
->wpa_auth
, sm
->addr
,
2233 WPA_EAPOL_keyRun
) > 0)
2234 SM_ENTER(WPA_PTK
, INITPMK
);
2235 else if (wpa_key_mgmt_wpa_psk(sm
->wpa_key_mgmt
)
2236 /* FIX: && 802.1X::keyRun */)
2237 SM_ENTER(WPA_PTK
, INITPSK
);
2239 case WPA_PTK_INITPMK
:
2240 if (wpa_auth_get_eapol(sm
->wpa_auth
, sm
->addr
,
2241 WPA_EAPOL_keyAvailable
) > 0)
2242 SM_ENTER(WPA_PTK
, PTKSTART
);
2244 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
2245 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2246 "INITPMK - keyAvailable = false");
2247 SM_ENTER(WPA_PTK
, DISCONNECT
);
2250 case WPA_PTK_INITPSK
:
2251 if (wpa_auth_get_psk(sm
->wpa_auth
, sm
->addr
, sm
->p2p_dev_addr
,
2253 SM_ENTER(WPA_PTK
, PTKSTART
);
2255 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2256 "no PSK configured for the STA");
2257 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
2258 SM_ENTER(WPA_PTK
, DISCONNECT
);
2261 case WPA_PTK_PTKSTART
:
2262 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
2263 sm
->EAPOLKeyPairwise
)
2264 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
2265 else if (sm
->TimeoutCtr
>
2266 (int) dot11RSNAConfigPairwiseUpdateCount
) {
2267 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
2268 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2269 "PTKSTART: Retry limit %d reached",
2270 dot11RSNAConfigPairwiseUpdateCount
);
2271 SM_ENTER(WPA_PTK
, DISCONNECT
);
2272 } else if (sm
->TimeoutEvt
)
2273 SM_ENTER(WPA_PTK
, PTKSTART
);
2275 case WPA_PTK_PTKCALCNEGOTIATING
:
2276 if (sm
->MICVerified
)
2277 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING2
);
2278 else if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
2279 sm
->EAPOLKeyPairwise
)
2280 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
2281 else if (sm
->TimeoutEvt
)
2282 SM_ENTER(WPA_PTK
, PTKSTART
);
2284 case WPA_PTK_PTKCALCNEGOTIATING2
:
2285 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
2287 case WPA_PTK_PTKINITNEGOTIATING
:
2288 if (sm
->update_snonce
)
2289 SM_ENTER(WPA_PTK
, PTKCALCNEGOTIATING
);
2290 else if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
2291 sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
2292 SM_ENTER(WPA_PTK
, PTKINITDONE
);
2293 else if (sm
->TimeoutCtr
>
2294 (int) dot11RSNAConfigPairwiseUpdateCount
) {
2295 wpa_auth
->dot11RSNA4WayHandshakeFailures
++;
2296 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2297 "PTKINITNEGOTIATING: Retry limit %d "
2299 dot11RSNAConfigPairwiseUpdateCount
);
2300 SM_ENTER(WPA_PTK
, DISCONNECT
);
2301 } else if (sm
->TimeoutEvt
)
2302 SM_ENTER(WPA_PTK
, PTKINITNEGOTIATING
);
2304 case WPA_PTK_PTKINITDONE
:
2310 SM_STATE(WPA_PTK_GROUP
, IDLE
)
2312 SM_ENTRY_MA(WPA_PTK_GROUP
, IDLE
, wpa_ptk_group
);
2314 /* Init flag is not cleared here, so avoid busy
2315 * loop by claiming nothing changed. */
2316 sm
->changed
= FALSE
;
2318 sm
->GTimeoutCtr
= 0;
2322 SM_STATE(WPA_PTK_GROUP
, REKEYNEGOTIATING
)
2324 u8 rsc
[WPA_KEY_RSC_LEN
];
2325 struct wpa_group
*gsm
= sm
->group
;
2326 u8
*kde
, *pos
, hdr
[2];
2328 u8
*gtk
, dummy_gtk
[32];
2330 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYNEGOTIATING
, wpa_ptk_group
);
2333 if (sm
->GTimeoutCtr
> (int) dot11RSNAConfigGroupUpdateCount
) {
2334 /* No point in sending the EAPOL-Key - we will disconnect
2335 * immediately following this. */
2339 if (sm
->wpa
== WPA_VERSION_WPA
)
2340 sm
->PInitAKeys
= FALSE
;
2341 sm
->TimeoutEvt
= FALSE
;
2342 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
2343 os_memset(rsc
, 0, WPA_KEY_RSC_LEN
);
2344 if (gsm
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
)
2345 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, rsc
);
2346 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2347 "sending 1/2 msg of Group Key Handshake");
2349 gtk
= gsm
->GTK
[gsm
->GN
- 1];
2350 if (sm
->wpa_auth
->conf
.disable_gtk
) {
2352 * Provide unique random GTK to each STA to prevent use
2353 * of GTK in the BSS.
2355 if (random_get_bytes(dummy_gtk
, gsm
->GTK_len
) < 0)
2359 if (sm
->wpa
== WPA_VERSION_WPA2
) {
2360 kde_len
= 2 + RSN_SELECTOR_LEN
+ 2 + gsm
->GTK_len
+
2361 ieee80211w_kde_len(sm
);
2362 kde
= os_malloc(kde_len
);
2367 hdr
[0] = gsm
->GN
& 0x03;
2369 pos
= wpa_add_kde(pos
, RSN_KEY_DATA_GROUPKEY
, hdr
, 2,
2371 pos
= ieee80211w_kde_add(sm
, pos
);
2374 pos
= kde
+ gsm
->GTK_len
;
2377 wpa_send_eapol(sm
->wpa_auth
, sm
,
2378 WPA_KEY_INFO_SECURE
| WPA_KEY_INFO_MIC
|
2380 (!sm
->Pair
? WPA_KEY_INFO_INSTALL
: 0),
2381 rsc
, gsm
->GNonce
, kde
, pos
- kde
, gsm
->GN
, 1);
2382 if (sm
->wpa
== WPA_VERSION_WPA2
)
2387 SM_STATE(WPA_PTK_GROUP
, REKEYESTABLISHED
)
2389 SM_ENTRY_MA(WPA_PTK_GROUP
, REKEYESTABLISHED
, wpa_ptk_group
);
2390 sm
->EAPOLKeyReceived
= FALSE
;
2391 if (sm
->GUpdateStationKeys
)
2392 sm
->group
->GKeyDoneStations
--;
2393 sm
->GUpdateStationKeys
= FALSE
;
2394 sm
->GTimeoutCtr
= 0;
2395 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
2396 wpa_auth_vlogger(sm
->wpa_auth
, sm
->addr
, LOGGER_INFO
,
2397 "group key handshake completed (%s)",
2398 sm
->wpa
== WPA_VERSION_WPA
? "WPA" : "RSN");
2403 SM_STATE(WPA_PTK_GROUP
, KEYERROR
)
2405 SM_ENTRY_MA(WPA_PTK_GROUP
, KEYERROR
, wpa_ptk_group
);
2406 if (sm
->GUpdateStationKeys
)
2407 sm
->group
->GKeyDoneStations
--;
2408 sm
->GUpdateStationKeys
= FALSE
;
2409 sm
->Disconnect
= TRUE
;
2413 SM_STEP(WPA_PTK_GROUP
)
2415 if (sm
->Init
|| sm
->PtkGroupInit
) {
2416 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
2417 sm
->PtkGroupInit
= FALSE
;
2418 } else switch (sm
->wpa_ptk_group_state
) {
2419 case WPA_PTK_GROUP_IDLE
:
2420 if (sm
->GUpdateStationKeys
||
2421 (sm
->wpa
== WPA_VERSION_WPA
&& sm
->PInitAKeys
))
2422 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
2424 case WPA_PTK_GROUP_REKEYNEGOTIATING
:
2425 if (sm
->EAPOLKeyReceived
&& !sm
->EAPOLKeyRequest
&&
2426 !sm
->EAPOLKeyPairwise
&& sm
->MICVerified
)
2427 SM_ENTER(WPA_PTK_GROUP
, REKEYESTABLISHED
);
2428 else if (sm
->GTimeoutCtr
>
2429 (int) dot11RSNAConfigGroupUpdateCount
)
2430 SM_ENTER(WPA_PTK_GROUP
, KEYERROR
);
2431 else if (sm
->TimeoutEvt
)
2432 SM_ENTER(WPA_PTK_GROUP
, REKEYNEGOTIATING
);
2434 case WPA_PTK_GROUP_KEYERROR
:
2435 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
2437 case WPA_PTK_GROUP_REKEYESTABLISHED
:
2438 SM_ENTER(WPA_PTK_GROUP
, IDLE
);
2444 static int wpa_gtk_update(struct wpa_authenticator
*wpa_auth
,
2445 struct wpa_group
*group
)
2449 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
2450 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
2451 if (wpa_gmk_to_gtk(group
->GMK
, "Group key expansion",
2452 wpa_auth
->addr
, group
->GNonce
,
2453 group
->GTK
[group
->GN
- 1], group
->GTK_len
) < 0)
2455 wpa_hexdump_key(MSG_DEBUG
, "GTK",
2456 group
->GTK
[group
->GN
- 1], group
->GTK_len
);
2458 #ifdef CONFIG_IEEE80211W
2459 if (wpa_auth
->conf
.ieee80211w
!= NO_MGMT_FRAME_PROTECTION
) {
2460 os_memcpy(group
->GNonce
, group
->Counter
, WPA_NONCE_LEN
);
2461 inc_byte_array(group
->Counter
, WPA_NONCE_LEN
);
2462 if (wpa_gmk_to_gtk(group
->GMK
, "IGTK key expansion",
2463 wpa_auth
->addr
, group
->GNonce
,
2464 group
->IGTK
[group
->GN_igtk
- 4],
2467 wpa_hexdump_key(MSG_DEBUG
, "IGTK",
2468 group
->IGTK
[group
->GN_igtk
- 4], WPA_IGTK_LEN
);
2470 #endif /* CONFIG_IEEE80211W */
2476 static void wpa_group_gtk_init(struct wpa_authenticator
*wpa_auth
,
2477 struct wpa_group
*group
)
2479 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
2480 "GTK_INIT (VLAN-ID %d)", group
->vlan_id
);
2481 group
->changed
= FALSE
; /* GInit is not cleared here; avoid loop */
2482 group
->wpa_group_state
= WPA_GROUP_GTK_INIT
;
2485 os_memset(group
->GTK
, 0, sizeof(group
->GTK
));
2488 #ifdef CONFIG_IEEE80211W
2491 #endif /* CONFIG_IEEE80211W */
2492 /* GTK[GN] = CalcGTK() */
2493 wpa_gtk_update(wpa_auth
, group
);
2497 static int wpa_group_update_sta(struct wpa_state_machine
*sm
, void *ctx
)
2499 if (ctx
!= NULL
&& ctx
!= sm
->group
)
2502 if (sm
->wpa_ptk_state
!= WPA_PTK_PTKINITDONE
) {
2503 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2504 "Not in PTKINITDONE; skip Group Key update");
2505 sm
->GUpdateStationKeys
= FALSE
;
2508 if (sm
->GUpdateStationKeys
) {
2510 * This should not really happen, so add a debug log entry.
2511 * Since we clear the GKeyDoneStations before the loop, the
2512 * station needs to be counted here anyway.
2514 wpa_auth_logger(sm
->wpa_auth
, sm
->addr
, LOGGER_DEBUG
,
2515 "GUpdateStationKeys was already set when "
2516 "marking station for GTK rekeying");
2519 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
2520 if (sm
->is_wnmsleep
)
2523 sm
->group
->GKeyDoneStations
++;
2524 sm
->GUpdateStationKeys
= TRUE
;
2532 /* update GTK when exiting WNM-Sleep Mode */
2533 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine
*sm
)
2535 if (sm
== NULL
|| sm
->is_wnmsleep
)
2538 wpa_group_update_sta(sm
, NULL
);
2542 void wpa_set_wnmsleep(struct wpa_state_machine
*sm
, int flag
)
2545 sm
->is_wnmsleep
= !!flag
;
2549 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine
*sm
, u8
*pos
)
2551 struct wpa_group
*gsm
= sm
->group
;
2556 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
2559 *pos
++ = WNM_SLEEP_SUBELEM_GTK
;
2560 *pos
++ = 11 + gsm
->GTK_len
;
2561 /* Key ID in B0-B1 of Key Info */
2562 WPA_PUT_LE16(pos
, gsm
->GN
& 0x03);
2564 *pos
++ = gsm
->GTK_len
;
2565 if (wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, pos
) != 0)
2568 os_memcpy(pos
, gsm
->GTK
[gsm
->GN
- 1], gsm
->GTK_len
);
2569 pos
+= gsm
->GTK_len
;
2571 wpa_printf(MSG_DEBUG
, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
2573 wpa_hexdump_key(MSG_DEBUG
, "WNM: GTK in WNM-Sleep Mode exit",
2574 gsm
->GTK
[gsm
->GN
- 1], gsm
->GTK_len
);
2580 #ifdef CONFIG_IEEE80211W
2581 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine
*sm
, u8
*pos
)
2583 struct wpa_group
*gsm
= sm
->group
;
2588 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
2590 *pos
++ = WNM_SLEEP_SUBELEM_IGTK
;
2591 *pos
++ = 2 + 6 + WPA_IGTK_LEN
;
2592 WPA_PUT_LE16(pos
, gsm
->GN_igtk
);
2594 if (wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, pos
) != 0)
2598 os_memcpy(pos
, gsm
->IGTK
[gsm
->GN_igtk
- 4], WPA_IGTK_LEN
);
2599 pos
+= WPA_IGTK_LEN
;
2601 wpa_printf(MSG_DEBUG
, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
2603 wpa_hexdump_key(MSG_DEBUG
, "WNM: IGTK in WNM-Sleep Mode exit",
2604 gsm
->IGTK
[gsm
->GN_igtk
- 4], WPA_IGTK_LEN
);
2608 #endif /* CONFIG_IEEE80211W */
2609 #endif /* CONFIG_WNM */
2612 static void wpa_group_setkeys(struct wpa_authenticator
*wpa_auth
,
2613 struct wpa_group
*group
)
2617 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
2618 "SETKEYS (VLAN-ID %d)", group
->vlan_id
);
2619 group
->changed
= TRUE
;
2620 group
->wpa_group_state
= WPA_GROUP_SETKEYS
;
2621 group
->GTKReKey
= FALSE
;
2623 group
->GM
= group
->GN
;
2625 #ifdef CONFIG_IEEE80211W
2626 tmp
= group
->GM_igtk
;
2627 group
->GM_igtk
= group
->GN_igtk
;
2628 group
->GN_igtk
= tmp
;
2629 #endif /* CONFIG_IEEE80211W */
2630 /* "GKeyDoneStations = GNoStations" is done in more robust way by
2631 * counting the STAs that are marked with GUpdateStationKeys instead of
2632 * including all STAs that could be in not-yet-completed state. */
2633 wpa_gtk_update(wpa_auth
, group
);
2635 if (group
->GKeyDoneStations
) {
2636 wpa_printf(MSG_DEBUG
, "wpa_group_setkeys: Unexpected "
2637 "GKeyDoneStations=%d when starting new GTK rekey",
2638 group
->GKeyDoneStations
);
2639 group
->GKeyDoneStations
= 0;
2641 wpa_auth_for_each_sta(wpa_auth
, wpa_group_update_sta
, group
);
2642 wpa_printf(MSG_DEBUG
, "wpa_group_setkeys: GKeyDoneStations=%d",
2643 group
->GKeyDoneStations
);
2647 static int wpa_group_config_group_keys(struct wpa_authenticator
*wpa_auth
,
2648 struct wpa_group
*group
)
2652 if (wpa_auth_set_key(wpa_auth
, group
->vlan_id
,
2653 wpa_cipher_to_alg(wpa_auth
->conf
.wpa_group
),
2654 broadcast_ether_addr
, group
->GN
,
2655 group
->GTK
[group
->GN
- 1], group
->GTK_len
) < 0)
2658 #ifdef CONFIG_IEEE80211W
2659 if (wpa_auth
->conf
.ieee80211w
!= NO_MGMT_FRAME_PROTECTION
&&
2660 wpa_auth_set_key(wpa_auth
, group
->vlan_id
, WPA_ALG_IGTK
,
2661 broadcast_ether_addr
, group
->GN_igtk
,
2662 group
->IGTK
[group
->GN_igtk
- 4],
2665 #endif /* CONFIG_IEEE80211W */
2671 static int wpa_group_disconnect_cb(struct wpa_state_machine
*sm
, void *ctx
)
2673 if (sm
->group
== ctx
) {
2674 wpa_printf(MSG_DEBUG
, "WPA: Mark STA " MACSTR
2675 " for discconnection due to fatal failure",
2677 sm
->Disconnect
= TRUE
;
2684 static void wpa_group_fatal_failure(struct wpa_authenticator
*wpa_auth
,
2685 struct wpa_group
*group
)
2687 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state FATAL_FAILURE");
2688 group
->changed
= TRUE
;
2689 group
->wpa_group_state
= WPA_GROUP_FATAL_FAILURE
;
2690 wpa_auth_for_each_sta(wpa_auth
, wpa_group_disconnect_cb
, group
);
2694 static int wpa_group_setkeysdone(struct wpa_authenticator
*wpa_auth
,
2695 struct wpa_group
*group
)
2697 wpa_printf(MSG_DEBUG
, "WPA: group state machine entering state "
2698 "SETKEYSDONE (VLAN-ID %d)", group
->vlan_id
);
2699 group
->changed
= TRUE
;
2700 group
->wpa_group_state
= WPA_GROUP_SETKEYSDONE
;
2702 if (wpa_group_config_group_keys(wpa_auth
, group
) < 0) {
2703 wpa_group_fatal_failure(wpa_auth
, group
);
2711 static void wpa_group_sm_step(struct wpa_authenticator
*wpa_auth
,
2712 struct wpa_group
*group
)
2715 wpa_group_gtk_init(wpa_auth
, group
);
2716 } else if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
) {
2717 /* Do not allow group operations */
2718 } else if (group
->wpa_group_state
== WPA_GROUP_GTK_INIT
&&
2719 group
->GTKAuthenticator
) {
2720 wpa_group_setkeysdone(wpa_auth
, group
);
2721 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYSDONE
&&
2723 wpa_group_setkeys(wpa_auth
, group
);
2724 } else if (group
->wpa_group_state
== WPA_GROUP_SETKEYS
) {
2725 if (group
->GKeyDoneStations
== 0)
2726 wpa_group_setkeysdone(wpa_auth
, group
);
2727 else if (group
->GTKReKey
)
2728 wpa_group_setkeys(wpa_auth
, group
);
2733 static int wpa_sm_step(struct wpa_state_machine
*sm
)
2738 if (sm
->in_step_loop
) {
2739 /* This should not happen, but if it does, make sure we do not
2740 * end up freeing the state machine too early by exiting the
2741 * recursive call. */
2742 wpa_printf(MSG_ERROR
, "WPA: wpa_sm_step() called recursively");
2746 sm
->in_step_loop
= 1;
2748 if (sm
->pending_deinit
)
2751 sm
->changed
= FALSE
;
2752 sm
->wpa_auth
->group
->changed
= FALSE
;
2754 SM_STEP_RUN(WPA_PTK
);
2755 if (sm
->pending_deinit
)
2757 SM_STEP_RUN(WPA_PTK_GROUP
);
2758 if (sm
->pending_deinit
)
2760 wpa_group_sm_step(sm
->wpa_auth
, sm
->group
);
2761 } while (sm
->changed
|| sm
->wpa_auth
->group
->changed
);
2762 sm
->in_step_loop
= 0;
2764 if (sm
->pending_deinit
) {
2765 wpa_printf(MSG_DEBUG
, "WPA: Completing pending STA state "
2766 "machine deinit for " MACSTR
, MAC2STR(sm
->addr
));
2767 wpa_free_sta_sm(sm
);
2774 static void wpa_sm_call_step(void *eloop_ctx
, void *timeout_ctx
)
2776 struct wpa_state_machine
*sm
= eloop_ctx
;
2781 void wpa_auth_sm_notify(struct wpa_state_machine
*sm
)
2785 eloop_register_timeout(0, 0, wpa_sm_call_step
, sm
, NULL
);
2789 void wpa_gtk_rekey(struct wpa_authenticator
*wpa_auth
)
2792 struct wpa_group
*group
;
2794 if (wpa_auth
== NULL
)
2797 group
= wpa_auth
->group
;
2799 for (i
= 0; i
< 2; i
++) {
2801 group
->GM
= group
->GN
;
2803 #ifdef CONFIG_IEEE80211W
2804 tmp
= group
->GM_igtk
;
2805 group
->GM_igtk
= group
->GN_igtk
;
2806 group
->GN_igtk
= tmp
;
2807 #endif /* CONFIG_IEEE80211W */
2808 wpa_gtk_update(wpa_auth
, group
);
2809 wpa_group_config_group_keys(wpa_auth
, group
);
2814 static const char * wpa_bool_txt(int bool)
2816 return bool ? "TRUE" : "FALSE";
2820 #define RSN_SUITE "%02x-%02x-%02x-%d"
2821 #define RSN_SUITE_ARG(s) \
2822 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2824 int wpa_get_mib(struct wpa_authenticator
*wpa_auth
, char *buf
, size_t buflen
)
2827 char pmkid_txt
[PMKID_LEN
* 2 + 1];
2828 #ifdef CONFIG_RSN_PREAUTH
2829 const int preauth
= 1;
2830 #else /* CONFIG_RSN_PREAUTH */
2831 const int preauth
= 0;
2832 #endif /* CONFIG_RSN_PREAUTH */
2834 if (wpa_auth
== NULL
)
2837 ret
= os_snprintf(buf
+ len
, buflen
- len
,
2838 "dot11RSNAOptionImplemented=TRUE\n"
2839 "dot11RSNAPreauthenticationImplemented=%s\n"
2840 "dot11RSNAEnabled=%s\n"
2841 "dot11RSNAPreauthenticationEnabled=%s\n",
2842 wpa_bool_txt(preauth
),
2843 wpa_bool_txt(wpa_auth
->conf
.wpa
& WPA_PROTO_RSN
),
2844 wpa_bool_txt(wpa_auth
->conf
.rsn_preauth
));
2845 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
2849 wpa_snprintf_hex(pmkid_txt
, sizeof(pmkid_txt
),
2850 wpa_auth
->dot11RSNAPMKIDUsed
, PMKID_LEN
);
2853 buf
+ len
, buflen
- len
,
2854 "dot11RSNAConfigVersion=%u\n"
2855 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
2856 /* FIX: dot11RSNAConfigGroupCipher */
2857 /* FIX: dot11RSNAConfigGroupRekeyMethod */
2858 /* FIX: dot11RSNAConfigGroupRekeyTime */
2859 /* FIX: dot11RSNAConfigGroupRekeyPackets */
2860 "dot11RSNAConfigGroupRekeyStrict=%u\n"
2861 "dot11RSNAConfigGroupUpdateCount=%u\n"
2862 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
2863 "dot11RSNAConfigGroupCipherSize=%u\n"
2864 "dot11RSNAConfigPMKLifetime=%u\n"
2865 "dot11RSNAConfigPMKReauthThreshold=%u\n"
2866 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
2867 "dot11RSNAConfigSATimeout=%u\n"
2868 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE
"\n"
2869 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE
"\n"
2870 "dot11RSNAGroupCipherSelected=" RSN_SUITE
"\n"
2871 "dot11RSNAPMKIDUsed=%s\n"
2872 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE
"\n"
2873 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE
"\n"
2874 "dot11RSNAGroupCipherRequested=" RSN_SUITE
"\n"
2875 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
2876 "dot11RSNA4WayHandshakeFailures=%u\n"
2877 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
2879 !!wpa_auth
->conf
.wpa_strict_rekey
,
2880 dot11RSNAConfigGroupUpdateCount
,
2881 dot11RSNAConfigPairwiseUpdateCount
,
2882 wpa_cipher_key_len(wpa_auth
->conf
.wpa_group
) * 8,
2883 dot11RSNAConfigPMKLifetime
,
2884 dot11RSNAConfigPMKReauthThreshold
,
2885 dot11RSNAConfigSATimeout
,
2886 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteSelected
),
2887 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherSelected
),
2888 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherSelected
),
2890 RSN_SUITE_ARG(wpa_auth
->dot11RSNAAuthenticationSuiteRequested
),
2891 RSN_SUITE_ARG(wpa_auth
->dot11RSNAPairwiseCipherRequested
),
2892 RSN_SUITE_ARG(wpa_auth
->dot11RSNAGroupCipherRequested
),
2893 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
,
2894 wpa_auth
->dot11RSNA4WayHandshakeFailures
);
2895 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
2899 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
2900 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
2903 ret
= os_snprintf(buf
+ len
, buflen
- len
, "hostapdWPAGroupState=%d\n",
2904 wpa_auth
->group
->wpa_group_state
);
2905 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
2913 int wpa_get_mib_sta(struct wpa_state_machine
*sm
, char *buf
, size_t buflen
)
2921 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
2923 /* dot11RSNAStatsEntry */
2925 pairwise
= wpa_cipher_to_suite(sm
->wpa
== WPA_VERSION_WPA2
?
2926 WPA_PROTO_RSN
: WPA_PROTO_WPA
,
2932 buf
+ len
, buflen
- len
,
2933 /* TODO: dot11RSNAStatsIndex */
2934 "dot11RSNAStatsSTAAddress=" MACSTR
"\n"
2935 "dot11RSNAStatsVersion=1\n"
2936 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE
"\n"
2937 /* TODO: dot11RSNAStatsTKIPICVErrors */
2938 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
2939 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
2940 /* TODO: dot11RSNAStatsCCMPReplays */
2941 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
2942 /* TODO: dot11RSNAStatsTKIPReplays */,
2944 RSN_SUITE_ARG(pairwise
),
2945 sm
->dot11RSNAStatsTKIPLocalMICFailures
,
2946 sm
->dot11RSNAStatsTKIPRemoteMICFailures
);
2947 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
2952 ret
= os_snprintf(buf
+ len
, buflen
- len
,
2953 "hostapdWPAPTKState=%d\n"
2954 "hostapdWPAPTKGroupState=%d\n",
2956 sm
->wpa_ptk_group_state
);
2957 if (ret
< 0 || (size_t) ret
>= buflen
- len
)
2965 void wpa_auth_countermeasures_start(struct wpa_authenticator
*wpa_auth
)
2968 wpa_auth
->dot11RSNATKIPCounterMeasuresInvoked
++;
2972 int wpa_auth_pairwise_set(struct wpa_state_machine
*sm
)
2974 return sm
&& sm
->pairwise_set
;
2978 int wpa_auth_get_pairwise(struct wpa_state_machine
*sm
)
2980 return sm
->pairwise
;
2984 int wpa_auth_sta_key_mgmt(struct wpa_state_machine
*sm
)
2988 return sm
->wpa_key_mgmt
;
2992 int wpa_auth_sta_wpa_version(struct wpa_state_machine
*sm
)
3000 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine
*sm
,
3001 struct rsn_pmksa_cache_entry
*entry
)
3003 if (sm
== NULL
|| sm
->pmksa
!= entry
)
3010 struct rsn_pmksa_cache_entry
*
3011 wpa_auth_sta_get_pmksa(struct wpa_state_machine
*sm
)
3013 return sm
? sm
->pmksa
: NULL
;
3017 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine
*sm
)
3020 sm
->dot11RSNAStatsTKIPLocalMICFailures
++;
3024 const u8
* wpa_auth_get_wpa_ie(struct wpa_authenticator
*wpa_auth
, size_t *len
)
3026 if (wpa_auth
== NULL
)
3028 *len
= wpa_auth
->wpa_ie_len
;
3029 return wpa_auth
->wpa_ie
;
3033 int wpa_auth_pmksa_add(struct wpa_state_machine
*sm
, const u8
*pmk
,
3034 int session_timeout
, struct eapol_state_machine
*eapol
)
3036 if (sm
== NULL
|| sm
->wpa
!= WPA_VERSION_WPA2
||
3037 sm
->wpa_auth
->conf
.disable_pmksa_caching
)
3040 if (pmksa_cache_auth_add(sm
->wpa_auth
->pmksa
, pmk
, PMK_LEN
,
3041 sm
->wpa_auth
->addr
, sm
->addr
, session_timeout
,
3042 eapol
, sm
->wpa_key_mgmt
))
3049 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator
*wpa_auth
,
3050 const u8
*pmk
, size_t len
, const u8
*sta_addr
,
3051 int session_timeout
,
3052 struct eapol_state_machine
*eapol
)
3054 if (wpa_auth
== NULL
)
3057 if (pmksa_cache_auth_add(wpa_auth
->pmksa
, pmk
, len
, wpa_auth
->addr
,
3058 sta_addr
, session_timeout
, eapol
,
3059 WPA_KEY_MGMT_IEEE8021X
))
3066 void wpa_auth_pmksa_remove(struct wpa_authenticator
*wpa_auth
,
3069 struct rsn_pmksa_cache_entry
*pmksa
;
3071 if (wpa_auth
== NULL
|| wpa_auth
->pmksa
== NULL
)
3073 pmksa
= pmksa_cache_auth_get(wpa_auth
->pmksa
, sta_addr
, NULL
);
3075 wpa_printf(MSG_DEBUG
, "WPA: Remove PMKSA cache entry for "
3076 MACSTR
" based on request", MAC2STR(sta_addr
));
3077 pmksa_cache_free_entry(wpa_auth
->pmksa
, pmksa
);
3082 static struct wpa_group
*
3083 wpa_auth_add_group(struct wpa_authenticator
*wpa_auth
, int vlan_id
)
3085 struct wpa_group
*group
;
3087 if (wpa_auth
== NULL
|| wpa_auth
->group
== NULL
)
3090 wpa_printf(MSG_DEBUG
, "WPA: Add group state machine for VLAN-ID %d",
3092 group
= wpa_group_init(wpa_auth
, vlan_id
, 0);
3096 group
->next
= wpa_auth
->group
->next
;
3097 wpa_auth
->group
->next
= group
;
3103 int wpa_auth_sta_set_vlan(struct wpa_state_machine
*sm
, int vlan_id
)
3105 struct wpa_group
*group
;
3107 if (sm
== NULL
|| sm
->wpa_auth
== NULL
)
3110 group
= sm
->wpa_auth
->group
;
3112 if (group
->vlan_id
== vlan_id
)
3114 group
= group
->next
;
3117 if (group
== NULL
) {
3118 group
= wpa_auth_add_group(sm
->wpa_auth
, vlan_id
);
3123 if (sm
->group
== group
)
3126 if (group
->wpa_group_state
== WPA_GROUP_FATAL_FAILURE
)
3129 wpa_printf(MSG_DEBUG
, "WPA: Moving STA " MACSTR
" to use group state "
3130 "machine for VLAN ID %d", MAC2STR(sm
->addr
), vlan_id
);
3137 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator
*wpa_auth
,
3138 struct wpa_state_machine
*sm
, int ack
)
3140 if (wpa_auth
== NULL
|| sm
== NULL
)
3142 wpa_printf(MSG_DEBUG
, "WPA: EAPOL-Key TX status for STA " MACSTR
3143 " ack=%d", MAC2STR(sm
->addr
), ack
);
3144 if (sm
->pending_1_of_4_timeout
&& ack
) {
3146 * Some deployed supplicant implementations update their SNonce
3147 * for each EAPOL-Key 2/4 message even within the same 4-way
3148 * handshake and then fail to use the first SNonce when
3149 * deriving the PTK. This results in unsuccessful 4-way
3150 * handshake whenever the relatively short initial timeout is
3151 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
3152 * around this by increasing the timeout now that we know that
3153 * the station has received the frame.
3155 int timeout_ms
= eapol_key_timeout_subseq
;
3156 wpa_printf(MSG_DEBUG
, "WPA: Increase initial EAPOL-Key 1/4 "
3157 "timeout by %u ms because of acknowledged frame",
3159 eloop_cancel_timeout(wpa_send_eapol_timeout
, wpa_auth
, sm
);
3160 eloop_register_timeout(timeout_ms
/ 1000,
3161 (timeout_ms
% 1000) * 1000,
3162 wpa_send_eapol_timeout
, wpa_auth
, sm
);
3167 int wpa_auth_uses_sae(struct wpa_state_machine
*sm
)
3171 return wpa_key_mgmt_sae(sm
->wpa_key_mgmt
);
3175 int wpa_auth_uses_ft_sae(struct wpa_state_machine
*sm
)
3179 return sm
->wpa_key_mgmt
== WPA_KEY_MGMT_FT_SAE
;
3184 int wpa_auth_get_ip_addr(struct wpa_state_machine
*sm
, u8
*addr
)
3186 if (sm
== NULL
|| WPA_GET_BE32(sm
->ip_addr
) == 0)
3188 os_memcpy(addr
, sm
->ip_addr
, 4);
3191 #endif /* CONFIG_P2P */