2 * EAPOL supplicant state machines
3 * Copyright (c) 2004-2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "state_machine.h"
15 #include "crypto/crypto.h"
16 #include "crypto/md5.h"
17 #include "common/eapol_common.h"
18 #include "eap_peer/eap.h"
19 #include "eap_peer/eap_config.h"
20 #include "eap_peer/eap_proxy.h"
21 #include "eapol_supp_sm.h"
23 #define STATE_MACHINE_DATA struct eapol_sm
24 #define STATE_MACHINE_DEBUG_PREFIX "EAPOL"
27 /* IEEE 802.1X-2004 - Supplicant - EAPOL state machines */
30 * struct eapol_sm - Internal data for EAPOL state machines
34 unsigned int authWhile;
35 unsigned int heldWhile;
36 unsigned int startWhen;
37 unsigned int idleWhile; /* for EAP state machine */
38 int timer_tick_enabled;
40 /* Global variables */
47 PortControl portControl;
49 PortStatus suppPortStatus; /* dot1xSuppControlledPortStatus */
57 /* Supplicant PAE state machine */
60 SUPP_PAE_DISCONNECTED = 1,
62 SUPP_PAE_CONNECTING = 3,
63 SUPP_PAE_AUTHENTICATING = 4,
64 SUPP_PAE_AUTHENTICATED = 5,
68 SUPP_PAE_S_FORCE_AUTH = 9,
69 SUPP_PAE_S_FORCE_UNAUTH = 10
70 } SUPP_PAE_state; /* dot1xSuppPaeState */
74 unsigned int startCount;
76 PortControl sPortMode;
78 unsigned int heldPeriod; /* dot1xSuppHeldPeriod */
79 unsigned int startPeriod; /* dot1xSuppStartPeriod */
80 unsigned int maxStart; /* dot1xSuppMaxStart */
82 /* Key Receive state machine */
85 KEY_RX_NO_KEY_RECEIVE, KEY_RX_KEY_RECEIVE
90 /* Supplicant Backend state machine */
93 SUPP_BE_INITIALIZE = 1,
101 } SUPP_BE_state; /* dot1xSuppBackendPaeState */
107 unsigned int authPeriod; /* dot1xSuppAuthPeriod */
110 unsigned int dot1xSuppEapolFramesRx;
111 unsigned int dot1xSuppEapolFramesTx;
112 unsigned int dot1xSuppEapolStartFramesTx;
113 unsigned int dot1xSuppEapolLogoffFramesTx;
114 unsigned int dot1xSuppEapolRespFramesTx;
115 unsigned int dot1xSuppEapolReqIdFramesRx;
116 unsigned int dot1xSuppEapolReqFramesRx;
117 unsigned int dot1xSuppInvalidEapolFramesRx;
118 unsigned int dot1xSuppEapLengthErrorFramesRx;
119 unsigned int dot1xSuppLastEapolFrameVersion;
120 unsigned char dot1xSuppLastEapolFrameSource[6];
122 /* Miscellaneous variables (not defined in IEEE 802.1X-2004) */
125 struct eap_peer_config *config;
128 size_t last_rx_key_len;
129 struct wpabuf *eapReqData; /* for EAP */
130 Boolean altAccept; /* for EAP */
131 Boolean altReject; /* for EAP */
132 Boolean eapTriggerStart;
133 Boolean replay_counter_valid;
134 u8 last_replay_counter[16];
135 struct eapol_config conf;
136 struct eapol_ctx *ctx;
137 enum { EAPOL_CB_IN_PROGRESS = 0, EAPOL_CB_SUCCESS, EAPOL_CB_FAILURE }
141 Boolean unicast_key_received, broadcast_key_received;
143 Boolean force_authorized_update;
145 #ifdef CONFIG_EAP_PROXY
146 Boolean use_eap_proxy;
147 struct eap_proxy_sm *eap_proxy;
148 #endif /* CONFIG_EAP_PROXY */
152 static void eapol_sm_txLogoff(struct eapol_sm *sm);
153 static void eapol_sm_txStart(struct eapol_sm *sm);
154 static void eapol_sm_processKey(struct eapol_sm *sm);
155 static void eapol_sm_getSuppRsp(struct eapol_sm *sm);
156 static void eapol_sm_txSuppRsp(struct eapol_sm *sm);
157 static void eapol_sm_abortSupp(struct eapol_sm *sm);
158 static void eapol_sm_abort_cached(struct eapol_sm *sm);
159 static void eapol_sm_step_timeout(void *eloop_ctx, void *timeout_ctx);
160 static void eapol_sm_set_port_authorized(struct eapol_sm *sm);
161 static void eapol_sm_set_port_unauthorized(struct eapol_sm *sm);
164 /* Port Timers state machine - implemented as a function that will be called
165 * once a second as a registered event loop timeout */
166 static void eapol_port_timers_tick(void *eloop_ctx, void *timeout_ctx)
168 struct eapol_sm *sm = timeout_ctx;
170 if (sm->authWhile > 0) {
172 if (sm->authWhile == 0)
173 wpa_printf(MSG_DEBUG, "EAPOL: authWhile --> 0");
175 if (sm->heldWhile > 0) {
177 if (sm->heldWhile == 0)
178 wpa_printf(MSG_DEBUG, "EAPOL: heldWhile --> 0");
180 if (sm->startWhen > 0) {
182 if (sm->startWhen == 0)
183 wpa_printf(MSG_DEBUG, "EAPOL: startWhen --> 0");
185 if (sm->idleWhile > 0) {
187 if (sm->idleWhile == 0)
188 wpa_printf(MSG_DEBUG, "EAPOL: idleWhile --> 0");
191 if (sm->authWhile | sm->heldWhile | sm->startWhen | sm->idleWhile) {
192 if (eloop_register_timeout(1, 0, eapol_port_timers_tick,
194 sm->timer_tick_enabled = 0;
196 wpa_printf(MSG_DEBUG, "EAPOL: disable timer tick");
197 sm->timer_tick_enabled = 0;
203 static void eapol_enable_timer_tick(struct eapol_sm *sm)
205 if (sm->timer_tick_enabled)
207 wpa_printf(MSG_DEBUG, "EAPOL: enable timer tick");
208 eloop_cancel_timeout(eapol_port_timers_tick, NULL, sm);
209 if (eloop_register_timeout(1, 0, eapol_port_timers_tick, NULL, sm) == 0)
210 sm->timer_tick_enabled = 1;
214 SM_STATE(SUPP_PAE, LOGOFF)
216 SM_ENTRY(SUPP_PAE, LOGOFF);
217 eapol_sm_txLogoff(sm);
218 sm->logoffSent = TRUE;
219 eapol_sm_set_port_unauthorized(sm);
223 SM_STATE(SUPP_PAE, DISCONNECTED)
225 SM_ENTRY(SUPP_PAE, DISCONNECTED);
226 sm->sPortMode = Auto;
228 sm->eapTriggerStart = FALSE;
229 sm->logoffSent = FALSE;
230 eapol_sm_set_port_unauthorized(sm);
231 sm->suppAbort = TRUE;
233 sm->unicast_key_received = FALSE;
234 sm->broadcast_key_received = FALSE;
237 * IEEE Std 802.1X-2004 does not clear heldWhile here, but doing so
238 * allows the timer tick to be stopped more quickly when the port is
239 * not enabled. Since this variable is used only within HELD state,
240 * clearing it on initialization does not change actual state machine
247 SM_STATE(SUPP_PAE, CONNECTING)
249 int send_start = sm->SUPP_PAE_state == SUPP_PAE_CONNECTING ||
250 sm->SUPP_PAE_state == SUPP_PAE_HELD;
251 SM_ENTRY(SUPP_PAE, CONNECTING);
253 if (sm->eapTriggerStart)
255 if (sm->ctx->preauth)
257 sm->eapTriggerStart = FALSE;
260 sm->startWhen = sm->startPeriod;
264 * Do not send EAPOL-Start immediately since in most cases,
265 * Authenticator is going to start authentication immediately
266 * after association and an extra EAPOL-Start is just going to
267 * delay authentication. Use a short timeout to send the first
268 * EAPOL-Start if Authenticator does not start authentication.
270 if (sm->conf.wps && !(sm->conf.wps & EAPOL_PEER_IS_WPS20_AP)) {
271 /* Reduce latency on starting WPS negotiation. */
272 wpa_printf(MSG_DEBUG,
273 "EAPOL: Using shorter startWhen for WPS");
279 eapol_enable_timer_tick(sm);
280 sm->eapolEap = FALSE;
282 eapol_sm_txStart(sm);
286 SM_STATE(SUPP_PAE, AUTHENTICATING)
288 SM_ENTRY(SUPP_PAE, AUTHENTICATING);
290 sm->suppSuccess = FALSE;
291 sm->suppFail = FALSE;
292 sm->suppTimeout = FALSE;
295 sm->suppStart = TRUE;
299 SM_STATE(SUPP_PAE, HELD)
301 SM_ENTRY(SUPP_PAE, HELD);
302 sm->heldWhile = sm->heldPeriod;
303 eapol_enable_timer_tick(sm);
304 eapol_sm_set_port_unauthorized(sm);
305 sm->cb_status = EAPOL_CB_FAILURE;
309 SM_STATE(SUPP_PAE, AUTHENTICATED)
311 SM_ENTRY(SUPP_PAE, AUTHENTICATED);
312 eapol_sm_set_port_authorized(sm);
313 sm->cb_status = EAPOL_CB_SUCCESS;
317 SM_STATE(SUPP_PAE, RESTART)
319 SM_ENTRY(SUPP_PAE, RESTART);
320 sm->eapRestart = TRUE;
323 * Prevent EAP peer state machine from failing due to prior
324 * external EAP success notification (altSuccess=TRUE in the
325 * IDLE state could result in a transition to the FAILURE state.
327 wpa_printf(MSG_DEBUG, "EAPOL: Clearing prior altAccept TRUE");
328 sm->eapSuccess = FALSE;
329 sm->altAccept = FALSE;
334 SM_STATE(SUPP_PAE, S_FORCE_AUTH)
336 SM_ENTRY(SUPP_PAE, S_FORCE_AUTH);
337 eapol_sm_set_port_authorized(sm);
338 sm->sPortMode = ForceAuthorized;
342 SM_STATE(SUPP_PAE, S_FORCE_UNAUTH)
344 SM_ENTRY(SUPP_PAE, S_FORCE_UNAUTH);
345 eapol_sm_set_port_unauthorized(sm);
346 sm->sPortMode = ForceUnauthorized;
347 eapol_sm_txLogoff(sm);
353 if ((sm->userLogoff && !sm->logoffSent) &&
354 !(sm->initialize || !sm->portEnabled))
355 SM_ENTER_GLOBAL(SUPP_PAE, LOGOFF);
356 else if (((sm->portControl == Auto) &&
357 (sm->sPortMode != sm->portControl)) ||
358 sm->initialize || !sm->portEnabled)
359 SM_ENTER_GLOBAL(SUPP_PAE, DISCONNECTED);
360 else if ((sm->portControl == ForceAuthorized) &&
361 (sm->sPortMode != sm->portControl) &&
362 !(sm->initialize || !sm->portEnabled))
363 SM_ENTER_GLOBAL(SUPP_PAE, S_FORCE_AUTH);
364 else if ((sm->portControl == ForceUnauthorized) &&
365 (sm->sPortMode != sm->portControl) &&
366 !(sm->initialize || !sm->portEnabled))
367 SM_ENTER_GLOBAL(SUPP_PAE, S_FORCE_UNAUTH);
368 else switch (sm->SUPP_PAE_state) {
369 case SUPP_PAE_UNKNOWN:
371 case SUPP_PAE_LOGOFF:
373 SM_ENTER(SUPP_PAE, DISCONNECTED);
375 case SUPP_PAE_DISCONNECTED:
376 SM_ENTER(SUPP_PAE, CONNECTING);
378 case SUPP_PAE_CONNECTING:
379 if (sm->startWhen == 0 && sm->startCount < sm->maxStart)
380 SM_ENTER(SUPP_PAE, CONNECTING);
381 else if (sm->startWhen == 0 &&
382 sm->startCount >= sm->maxStart &&
384 SM_ENTER(SUPP_PAE, AUTHENTICATED);
385 else if (sm->eapSuccess || sm->eapFail)
386 SM_ENTER(SUPP_PAE, AUTHENTICATING);
387 else if (sm->eapolEap)
388 SM_ENTER(SUPP_PAE, RESTART);
389 else if (sm->startWhen == 0 &&
390 sm->startCount >= sm->maxStart &&
392 SM_ENTER(SUPP_PAE, HELD);
394 case SUPP_PAE_AUTHENTICATING:
395 if (sm->eapSuccess && !sm->portValid &&
396 sm->conf.accept_802_1x_keys &&
397 sm->conf.required_keys == 0) {
398 wpa_printf(MSG_DEBUG, "EAPOL: IEEE 802.1X for "
399 "plaintext connection; no EAPOL-Key frames "
401 sm->portValid = TRUE;
402 if (sm->ctx->eapol_done_cb)
403 sm->ctx->eapol_done_cb(sm->ctx->ctx);
405 if (sm->eapSuccess && sm->portValid)
406 SM_ENTER(SUPP_PAE, AUTHENTICATED);
407 else if (sm->eapFail || (sm->keyDone && !sm->portValid))
408 SM_ENTER(SUPP_PAE, HELD);
409 else if (sm->suppTimeout)
410 SM_ENTER(SUPP_PAE, CONNECTING);
411 else if (sm->eapTriggerStart)
412 SM_ENTER(SUPP_PAE, CONNECTING);
415 if (sm->heldWhile == 0)
416 SM_ENTER(SUPP_PAE, CONNECTING);
417 else if (sm->eapolEap)
418 SM_ENTER(SUPP_PAE, RESTART);
420 case SUPP_PAE_AUTHENTICATED:
421 if (sm->eapolEap && sm->portValid)
422 SM_ENTER(SUPP_PAE, RESTART);
423 else if (!sm->portValid)
424 SM_ENTER(SUPP_PAE, DISCONNECTED);
426 case SUPP_PAE_RESTART:
428 SM_ENTER(SUPP_PAE, AUTHENTICATING);
430 case SUPP_PAE_S_FORCE_AUTH:
432 case SUPP_PAE_S_FORCE_UNAUTH:
438 SM_STATE(KEY_RX, NO_KEY_RECEIVE)
440 SM_ENTRY(KEY_RX, NO_KEY_RECEIVE);
444 SM_STATE(KEY_RX, KEY_RECEIVE)
446 SM_ENTRY(KEY_RX, KEY_RECEIVE);
447 eapol_sm_processKey(sm);
454 if (sm->initialize || !sm->portEnabled)
455 SM_ENTER_GLOBAL(KEY_RX, NO_KEY_RECEIVE);
456 switch (sm->KEY_RX_state) {
459 case KEY_RX_NO_KEY_RECEIVE:
461 SM_ENTER(KEY_RX, KEY_RECEIVE);
463 case KEY_RX_KEY_RECEIVE:
465 SM_ENTER(KEY_RX, KEY_RECEIVE);
471 SM_STATE(SUPP_BE, REQUEST)
473 SM_ENTRY(SUPP_BE, REQUEST);
476 eapol_sm_getSuppRsp(sm);
480 SM_STATE(SUPP_BE, RESPONSE)
482 SM_ENTRY(SUPP_BE, RESPONSE);
483 eapol_sm_txSuppRsp(sm);
488 SM_STATE(SUPP_BE, SUCCESS)
490 SM_ENTRY(SUPP_BE, SUCCESS);
492 sm->suppSuccess = TRUE;
494 #ifdef CONFIG_EAP_PROXY
495 if (sm->use_eap_proxy) {
496 if (eap_proxy_key_available(sm->eap_proxy)) {
497 u8 *session_id, *emsk;
498 size_t session_id_len, emsk_len;
500 /* New key received - clear IEEE 802.1X EAPOL-Key replay
502 sm->replay_counter_valid = FALSE;
504 session_id = eap_proxy_get_eap_session_id(
505 sm->eap_proxy, &session_id_len);
506 emsk = eap_proxy_get_emsk(sm->eap_proxy, &emsk_len);
507 if (sm->config->erp && session_id && emsk) {
508 eap_peer_erp_init(sm->eap, session_id,
509 session_id_len, emsk,
513 bin_clear_free(emsk, emsk_len);
518 #endif /* CONFIG_EAP_PROXY */
520 if (eap_key_available(sm->eap)) {
521 /* New key received - clear IEEE 802.1X EAPOL-Key replay
523 sm->replay_counter_valid = FALSE;
528 SM_STATE(SUPP_BE, FAIL)
530 SM_ENTRY(SUPP_BE, FAIL);
535 SM_STATE(SUPP_BE, TIMEOUT)
537 SM_ENTRY(SUPP_BE, TIMEOUT);
538 sm->suppTimeout = TRUE;
542 SM_STATE(SUPP_BE, IDLE)
544 SM_ENTRY(SUPP_BE, IDLE);
545 sm->suppStart = FALSE;
546 sm->initial_req = TRUE;
550 SM_STATE(SUPP_BE, INITIALIZE)
552 SM_ENTRY(SUPP_BE, INITIALIZE);
553 eapol_sm_abortSupp(sm);
554 sm->suppAbort = FALSE;
557 * IEEE Std 802.1X-2004 does not clear authWhile here, but doing so
558 * allows the timer tick to be stopped more quickly when the port is
559 * not enabled. Since this variable is used only within RECEIVE state,
560 * clearing it on initialization does not change actual state machine
567 SM_STATE(SUPP_BE, RECEIVE)
569 SM_ENTRY(SUPP_BE, RECEIVE);
570 sm->authWhile = sm->authPeriod;
571 eapol_enable_timer_tick(sm);
572 sm->eapolEap = FALSE;
573 sm->eapNoResp = FALSE;
574 sm->initial_req = FALSE;
580 if (sm->initialize || sm->suppAbort)
581 SM_ENTER_GLOBAL(SUPP_BE, INITIALIZE);
582 else switch (sm->SUPP_BE_state) {
583 case SUPP_BE_UNKNOWN:
585 case SUPP_BE_REQUEST:
587 * IEEE Std 802.1X-2004 has transitions from REQUEST to FAIL
588 * and SUCCESS based on eapFail and eapSuccess, respectively.
589 * However, IEEE Std 802.1X-2004 is also specifying that
590 * eapNoResp should be set in conjunction with eapSuccess and
591 * eapFail which would mean that more than one of the
592 * transitions here would be activated at the same time.
593 * Skipping RESPONSE and/or RECEIVE states in these cases can
594 * cause problems and the direct transitions to do not seem
595 * correct. Because of this, the conditions for these
596 * transitions are verified only after eapNoResp. They are
597 * unlikely to be used since eapNoResp should always be set if
598 * either of eapSuccess or eapFail is set.
600 if (sm->eapResp && sm->eapNoResp) {
601 wpa_printf(MSG_DEBUG, "EAPOL: SUPP_BE REQUEST: both "
602 "eapResp and eapNoResp set?!");
605 SM_ENTER(SUPP_BE, RESPONSE);
606 else if (sm->eapNoResp)
607 SM_ENTER(SUPP_BE, RECEIVE);
608 else if (sm->eapFail)
609 SM_ENTER(SUPP_BE, FAIL);
610 else if (sm->eapSuccess)
611 SM_ENTER(SUPP_BE, SUCCESS);
613 case SUPP_BE_RESPONSE:
614 SM_ENTER(SUPP_BE, RECEIVE);
616 case SUPP_BE_SUCCESS:
617 SM_ENTER(SUPP_BE, IDLE);
620 SM_ENTER(SUPP_BE, IDLE);
622 case SUPP_BE_TIMEOUT:
623 SM_ENTER(SUPP_BE, IDLE);
626 if (sm->eapFail && sm->suppStart)
627 SM_ENTER(SUPP_BE, FAIL);
628 else if (sm->eapolEap && sm->suppStart)
629 SM_ENTER(SUPP_BE, REQUEST);
630 else if (sm->eapSuccess && sm->suppStart)
631 SM_ENTER(SUPP_BE, SUCCESS);
633 case SUPP_BE_INITIALIZE:
634 SM_ENTER(SUPP_BE, IDLE);
636 case SUPP_BE_RECEIVE:
638 SM_ENTER(SUPP_BE, REQUEST);
639 else if (sm->eapFail)
640 SM_ENTER(SUPP_BE, FAIL);
641 else if (sm->authWhile == 0)
642 SM_ENTER(SUPP_BE, TIMEOUT);
643 else if (sm->eapSuccess)
644 SM_ENTER(SUPP_BE, SUCCESS);
650 static void eapol_sm_txLogoff(struct eapol_sm *sm)
652 wpa_printf(MSG_DEBUG, "EAPOL: txLogoff");
653 sm->ctx->eapol_send(sm->ctx->eapol_send_ctx,
654 IEEE802_1X_TYPE_EAPOL_LOGOFF, (u8 *) "", 0);
655 sm->dot1xSuppEapolLogoffFramesTx++;
656 sm->dot1xSuppEapolFramesTx++;
660 static void eapol_sm_txStart(struct eapol_sm *sm)
662 wpa_printf(MSG_DEBUG, "EAPOL: txStart");
663 sm->ctx->eapol_send(sm->ctx->eapol_send_ctx,
664 IEEE802_1X_TYPE_EAPOL_START, (u8 *) "", 0);
665 sm->dot1xSuppEapolStartFramesTx++;
666 sm->dot1xSuppEapolFramesTx++;
670 #define IEEE8021X_ENCR_KEY_LEN 32
671 #define IEEE8021X_SIGN_KEY_LEN 32
673 struct eap_key_data {
674 u8 encr_key[IEEE8021X_ENCR_KEY_LEN];
675 u8 sign_key[IEEE8021X_SIGN_KEY_LEN];
679 static void eapol_sm_processKey(struct eapol_sm *sm)
682 struct ieee802_1x_hdr *hdr;
683 struct ieee802_1x_eapol_key *key;
684 struct eap_key_data keydata;
685 u8 orig_key_sign[IEEE8021X_KEY_SIGN_LEN], datakey[32];
686 #ifndef CONFIG_NO_RC4
687 u8 ekey[IEEE8021X_KEY_IV_LEN + IEEE8021X_ENCR_KEY_LEN];
688 #endif /* CONFIG_NO_RC4 */
689 int key_len, res, sign_key_len, encr_key_len;
693 wpa_printf(MSG_DEBUG, "EAPOL: processKey");
694 if (sm->last_rx_key == NULL)
697 if (!sm->conf.accept_802_1x_keys) {
698 wpa_printf(MSG_WARNING, "EAPOL: Received IEEE 802.1X EAPOL-Key"
699 " even though this was not accepted - "
700 "ignoring this packet");
704 if (sm->last_rx_key_len < sizeof(*hdr) + sizeof(*key))
706 hdr = (struct ieee802_1x_hdr *) sm->last_rx_key;
707 key = (struct ieee802_1x_eapol_key *) (hdr + 1);
708 plen = be_to_host16(hdr->length);
709 if (sizeof(*hdr) + plen > sm->last_rx_key_len || plen < sizeof(*key)) {
710 wpa_printf(MSG_WARNING, "EAPOL: Too short EAPOL-Key frame");
713 rx_key_length = WPA_GET_BE16(key->key_length);
714 wpa_printf(MSG_DEBUG, "EAPOL: RX IEEE 802.1X ver=%d type=%d len=%d "
715 "EAPOL-Key: type=%d key_length=%d key_index=0x%x",
716 hdr->version, hdr->type, be_to_host16(hdr->length),
717 key->type, rx_key_length, key->key_index);
719 eapol_sm_notify_lower_layer_success(sm, 1);
720 sign_key_len = IEEE8021X_SIGN_KEY_LEN;
721 encr_key_len = IEEE8021X_ENCR_KEY_LEN;
722 res = eapol_sm_get_key(sm, (u8 *) &keydata, sizeof(keydata));
724 wpa_printf(MSG_DEBUG, "EAPOL: Could not get master key for "
725 "decrypting EAPOL-Key keys");
729 /* LEAP derives only 16 bytes of keying material. */
730 res = eapol_sm_get_key(sm, (u8 *) &keydata, 16);
732 wpa_printf(MSG_DEBUG, "EAPOL: Could not get LEAP "
733 "master key for decrypting EAPOL-Key keys");
738 os_memcpy(keydata.sign_key, keydata.encr_key, 16);
740 wpa_printf(MSG_DEBUG, "EAPOL: Could not get enough master key "
741 "data for decrypting EAPOL-Key keys (res=%d)", res);
745 /* The key replay_counter must increase when same master key */
746 if (sm->replay_counter_valid &&
747 os_memcmp(sm->last_replay_counter, key->replay_counter,
748 IEEE8021X_REPLAY_COUNTER_LEN) >= 0) {
749 wpa_printf(MSG_WARNING, "EAPOL: EAPOL-Key replay counter did "
750 "not increase - ignoring key");
751 wpa_hexdump(MSG_DEBUG, "EAPOL: last replay counter",
752 sm->last_replay_counter,
753 IEEE8021X_REPLAY_COUNTER_LEN);
754 wpa_hexdump(MSG_DEBUG, "EAPOL: received replay counter",
755 key->replay_counter, IEEE8021X_REPLAY_COUNTER_LEN);
759 /* Verify key signature (HMAC-MD5) */
760 os_memcpy(orig_key_sign, key->key_signature, IEEE8021X_KEY_SIGN_LEN);
761 os_memset(key->key_signature, 0, IEEE8021X_KEY_SIGN_LEN);
762 hmac_md5(keydata.sign_key, sign_key_len,
763 sm->last_rx_key, sizeof(*hdr) + be_to_host16(hdr->length),
765 if (os_memcmp_const(orig_key_sign, key->key_signature,
766 IEEE8021X_KEY_SIGN_LEN) != 0) {
767 wpa_printf(MSG_DEBUG, "EAPOL: Invalid key signature in "
769 os_memcpy(key->key_signature, orig_key_sign,
770 IEEE8021X_KEY_SIGN_LEN);
773 wpa_printf(MSG_DEBUG, "EAPOL: EAPOL-Key key signature verified");
775 key_len = plen - sizeof(*key);
776 if (key_len > 32 || rx_key_length > 32) {
777 wpa_printf(MSG_WARNING, "EAPOL: Too long key data length %d",
778 key_len ? key_len : rx_key_length);
781 if (key_len == rx_key_length) {
784 /* otherwise unused */
786 wpa_printf(MSG_ERROR, "EAPOL: RC4 not supported in the build");
788 #else /* CONFIG_NO_RC4 */
789 os_memcpy(ekey, key->key_iv, IEEE8021X_KEY_IV_LEN);
790 os_memcpy(ekey + IEEE8021X_KEY_IV_LEN, keydata.encr_key,
792 os_memcpy(datakey, key + 1, key_len);
793 rc4_skip(ekey, IEEE8021X_KEY_IV_LEN + encr_key_len, 0,
795 wpa_hexdump_key(MSG_DEBUG, "EAPOL: Decrypted(RC4) key",
797 #endif /* CONFIG_NO_RC4 */
798 } else if (key_len == 0) {
800 * IEEE 802.1X-2004 specifies that least significant Key Length
801 * octets from MS-MPPE-Send-Key are used as the key if the key
802 * data is not present. This seems to be meaning the beginning
803 * of the MS-MPPE-Send-Key. In addition, MS-MPPE-Send-Key in
804 * Supplicant corresponds to MS-MPPE-Recv-Key in Authenticator.
805 * Anyway, taking the beginning of the keying material from EAP
806 * seems to interoperate with Authenticators.
808 key_len = rx_key_length;
809 os_memcpy(datakey, keydata.encr_key, key_len);
810 wpa_hexdump_key(MSG_DEBUG, "EAPOL: using part of EAP keying "
811 "material data encryption key",
814 wpa_printf(MSG_DEBUG, "EAPOL: Invalid key data length %d "
815 "(key_length=%d)", key_len, rx_key_length);
819 sm->replay_counter_valid = TRUE;
820 os_memcpy(sm->last_replay_counter, key->replay_counter,
821 IEEE8021X_REPLAY_COUNTER_LEN);
823 wpa_printf(MSG_DEBUG, "EAPOL: Setting dynamic WEP key: %s keyidx %d "
825 key->key_index & IEEE8021X_KEY_INDEX_FLAG ?
826 "unicast" : "broadcast",
827 key->key_index & IEEE8021X_KEY_INDEX_MASK, key_len);
829 if (sm->ctx->set_wep_key &&
830 sm->ctx->set_wep_key(sm->ctx->ctx,
831 key->key_index & IEEE8021X_KEY_INDEX_FLAG,
832 key->key_index & IEEE8021X_KEY_INDEX_MASK,
833 datakey, key_len) < 0) {
834 wpa_printf(MSG_WARNING, "EAPOL: Failed to set WEP key to the "
837 if (key->key_index & IEEE8021X_KEY_INDEX_FLAG)
838 sm->unicast_key_received = TRUE;
840 sm->broadcast_key_received = TRUE;
842 if ((sm->unicast_key_received ||
843 !(sm->conf.required_keys & EAPOL_REQUIRE_KEY_UNICAST)) &&
844 (sm->broadcast_key_received ||
845 !(sm->conf.required_keys & EAPOL_REQUIRE_KEY_BROADCAST)))
847 wpa_printf(MSG_DEBUG, "EAPOL: all required EAPOL-Key "
849 sm->portValid = TRUE;
850 if (sm->ctx->eapol_done_cb)
851 sm->ctx->eapol_done_cb(sm->ctx->ctx);
854 #endif /* CONFIG_FIPS */
858 static void eapol_sm_getSuppRsp(struct eapol_sm *sm)
860 wpa_printf(MSG_DEBUG, "EAPOL: getSuppRsp");
861 /* EAP layer processing; no special code is needed, since Supplicant
862 * Backend state machine is waiting for eapNoResp or eapResp to be set
863 * and these are only set in the EAP state machine when the processing
868 static void eapol_sm_txSuppRsp(struct eapol_sm *sm)
872 wpa_printf(MSG_DEBUG, "EAPOL: txSuppRsp");
874 #ifdef CONFIG_EAP_PROXY
875 if (sm->use_eap_proxy) {
876 /* Get EAP Response from EAP Proxy */
877 resp = eap_proxy_get_eapRespData(sm->eap_proxy);
879 wpa_printf(MSG_WARNING, "EAPOL: txSuppRsp - EAP Proxy "
880 "response data not available");
884 #endif /* CONFIG_EAP_PROXY */
886 resp = eap_get_eapRespData(sm->eap);
888 wpa_printf(MSG_WARNING, "EAPOL: txSuppRsp - EAP response data "
893 /* Send EAP-Packet from the EAP layer to the Authenticator */
894 sm->ctx->eapol_send(sm->ctx->eapol_send_ctx,
895 IEEE802_1X_TYPE_EAP_PACKET, wpabuf_head(resp),
898 /* eapRespData is not used anymore, so free it here */
902 sm->dot1xSuppEapolReqIdFramesRx++;
904 sm->dot1xSuppEapolReqFramesRx++;
905 sm->dot1xSuppEapolRespFramesTx++;
906 sm->dot1xSuppEapolFramesTx++;
910 static void eapol_sm_abortSupp(struct eapol_sm *sm)
912 /* release system resources that may have been allocated for the
913 * authentication session */
914 os_free(sm->last_rx_key);
915 sm->last_rx_key = NULL;
916 wpabuf_free(sm->eapReqData);
917 sm->eapReqData = NULL;
918 eap_sm_abort(sm->eap);
919 #ifdef CONFIG_EAP_PROXY
920 eap_proxy_sm_abort(sm->eap_proxy);
921 #endif /* CONFIG_EAP_PROXY */
925 static void eapol_sm_step_timeout(void *eloop_ctx, void *timeout_ctx)
927 eapol_sm_step(timeout_ctx);
931 static void eapol_sm_set_port_authorized(struct eapol_sm *sm)
935 cb = sm->suppPortStatus != Authorized || sm->force_authorized_update;
936 sm->force_authorized_update = FALSE;
937 sm->suppPortStatus = Authorized;
938 if (cb && sm->ctx->port_cb)
939 sm->ctx->port_cb(sm->ctx->ctx, 1);
943 static void eapol_sm_set_port_unauthorized(struct eapol_sm *sm)
947 cb = sm->suppPortStatus != Unauthorized || sm->force_authorized_update;
948 sm->force_authorized_update = FALSE;
949 sm->suppPortStatus = Unauthorized;
950 if (cb && sm->ctx->port_cb)
951 sm->ctx->port_cb(sm->ctx->ctx, 0);
956 * eapol_sm_step - EAPOL state machine step function
957 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
959 * This function is called to notify the state machine about changed external
960 * variables. It will step through the EAPOL state machines in loop to process
961 * all triggered state changes.
963 void eapol_sm_step(struct eapol_sm *sm)
967 /* In theory, it should be ok to run this in loop until !changed.
968 * However, it is better to use a limit on number of iterations to
969 * allow events (e.g., SIGTERM) to stop the program cleanly if the
970 * state machine were to generate a busy loop. */
971 for (i = 0; i < 100; i++) {
973 SM_STEP_RUN(SUPP_PAE);
975 SM_STEP_RUN(SUPP_BE);
976 #ifdef CONFIG_EAP_PROXY
977 if (sm->use_eap_proxy) {
978 /* Drive the EAP proxy state machine */
979 if (eap_proxy_sm_step(sm->eap_proxy, sm->eap))
982 #endif /* CONFIG_EAP_PROXY */
983 if (eap_peer_sm_step(sm->eap))
990 /* restart EAPOL state machine step from timeout call in order
991 * to allow other events to be processed. */
992 eloop_cancel_timeout(eapol_sm_step_timeout, NULL, sm);
993 eloop_register_timeout(0, 0, eapol_sm_step_timeout, NULL, sm);
996 if (sm->ctx->cb && sm->cb_status != EAPOL_CB_IN_PROGRESS) {
997 enum eapol_supp_result result;
998 if (sm->cb_status == EAPOL_CB_SUCCESS)
999 result = EAPOL_SUPP_RESULT_SUCCESS;
1000 else if (eap_peer_was_failure_expected(sm->eap))
1001 result = EAPOL_SUPP_RESULT_EXPECTED_FAILURE;
1003 result = EAPOL_SUPP_RESULT_FAILURE;
1004 sm->cb_status = EAPOL_CB_IN_PROGRESS;
1005 sm->ctx->cb(sm, result, sm->ctx->cb_ctx);
1010 #ifdef CONFIG_CTRL_IFACE
1011 static const char *eapol_supp_pae_state(int state)
1014 case SUPP_PAE_LOGOFF:
1016 case SUPP_PAE_DISCONNECTED:
1017 return "DISCONNECTED";
1018 case SUPP_PAE_CONNECTING:
1019 return "CONNECTING";
1020 case SUPP_PAE_AUTHENTICATING:
1021 return "AUTHENTICATING";
1024 case SUPP_PAE_AUTHENTICATED:
1025 return "AUTHENTICATED";
1026 case SUPP_PAE_RESTART:
1034 static const char *eapol_supp_be_state(int state)
1037 case SUPP_BE_REQUEST:
1039 case SUPP_BE_RESPONSE:
1041 case SUPP_BE_SUCCESS:
1045 case SUPP_BE_TIMEOUT:
1049 case SUPP_BE_INITIALIZE:
1050 return "INITIALIZE";
1051 case SUPP_BE_RECEIVE:
1059 static const char * eapol_port_status(PortStatus status)
1061 if (status == Authorized)
1062 return "Authorized";
1064 return "Unauthorized";
1066 #endif /* CONFIG_CTRL_IFACE */
1069 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
1070 static const char * eapol_port_control(PortControl ctrl)
1075 case ForceUnauthorized:
1076 return "ForceUnauthorized";
1077 case ForceAuthorized:
1078 return "ForceAuthorized";
1083 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1087 * eapol_sm_configure - Set EAPOL variables
1088 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1089 * @heldPeriod: dot1xSuppHeldPeriod
1090 * @authPeriod: dot1xSuppAuthPeriod
1091 * @startPeriod: dot1xSuppStartPeriod
1092 * @maxStart: dot1xSuppMaxStart
1094 * Set configurable EAPOL state machine variables. Each variable can be set to
1095 * the given value or ignored if set to -1 (to set only some of the variables).
1097 void eapol_sm_configure(struct eapol_sm *sm, int heldPeriod, int authPeriod,
1098 int startPeriod, int maxStart)
1102 if (heldPeriod >= 0)
1103 sm->heldPeriod = heldPeriod;
1104 if (authPeriod >= 0)
1105 sm->authPeriod = authPeriod;
1106 if (startPeriod >= 0)
1107 sm->startPeriod = startPeriod;
1109 sm->maxStart = maxStart;
1114 * eapol_sm_get_method_name - Get EAPOL method name
1115 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1116 * Returns: Static string containing name of current eap method or NULL
1118 const char * eapol_sm_get_method_name(struct eapol_sm *sm)
1120 if (sm->SUPP_PAE_state != SUPP_PAE_AUTHENTICATED ||
1121 sm->suppPortStatus != Authorized)
1124 return eap_sm_get_method_name(sm->eap);
1128 #ifdef CONFIG_CTRL_IFACE
1130 * eapol_sm_get_status - Get EAPOL state machine status
1131 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1132 * @buf: Buffer for status information
1133 * @buflen: Maximum buffer length
1134 * @verbose: Whether to include verbose status information
1135 * Returns: Number of bytes written to buf.
1137 * Query EAPOL state machine for status information. This function fills in a
1138 * text area with current status information from the EAPOL state machine. If
1139 * the buffer (buf) is not large enough, status information will be truncated
1140 * to fit the buffer.
1142 int eapol_sm_get_status(struct eapol_sm *sm, char *buf, size_t buflen,
1149 len = os_snprintf(buf, buflen,
1150 "Supplicant PAE state=%s\n"
1151 "suppPortStatus=%s\n",
1152 eapol_supp_pae_state(sm->SUPP_PAE_state),
1153 eapol_port_status(sm->suppPortStatus));
1154 if (os_snprintf_error(buflen, len))
1158 ret = os_snprintf(buf + len, buflen - len,
1164 "Supplicant Backend state=%s\n",
1169 eapol_port_control(sm->portControl),
1170 eapol_supp_be_state(sm->SUPP_BE_state));
1171 if (os_snprintf_error(buflen - len, ret))
1176 #ifdef CONFIG_EAP_PROXY
1177 if (sm->use_eap_proxy)
1178 len += eap_proxy_sm_get_status(sm->eap_proxy,
1179 buf + len, buflen - len,
1182 #endif /* CONFIG_EAP_PROXY */
1183 len += eap_sm_get_status(sm->eap, buf + len, buflen - len, verbose);
1190 * eapol_sm_get_mib - Get EAPOL state machine MIBs
1191 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1192 * @buf: Buffer for MIB information
1193 * @buflen: Maximum buffer length
1194 * Returns: Number of bytes written to buf.
1196 * Query EAPOL state machine for MIB information. This function fills in a
1197 * text area with current MIB information from the EAPOL state machine. If
1198 * the buffer (buf) is not large enough, MIB information will be truncated to
1201 int eapol_sm_get_mib(struct eapol_sm *sm, char *buf, size_t buflen)
1208 ret = os_snprintf(buf, buflen,
1209 "dot1xSuppPaeState=%d\n"
1210 "dot1xSuppHeldPeriod=%u\n"
1211 "dot1xSuppAuthPeriod=%u\n"
1212 "dot1xSuppStartPeriod=%u\n"
1213 "dot1xSuppMaxStart=%u\n"
1214 "dot1xSuppSuppControlledPortStatus=%s\n"
1215 "dot1xSuppBackendPaeState=%d\n",
1221 sm->suppPortStatus == Authorized ?
1222 "Authorized" : "Unauthorized",
1225 if (os_snprintf_error(buflen, ret))
1229 ret = os_snprintf(buf + len, buflen - len,
1230 "dot1xSuppEapolFramesRx=%u\n"
1231 "dot1xSuppEapolFramesTx=%u\n"
1232 "dot1xSuppEapolStartFramesTx=%u\n"
1233 "dot1xSuppEapolLogoffFramesTx=%u\n"
1234 "dot1xSuppEapolRespFramesTx=%u\n"
1235 "dot1xSuppEapolReqIdFramesRx=%u\n"
1236 "dot1xSuppEapolReqFramesRx=%u\n"
1237 "dot1xSuppInvalidEapolFramesRx=%u\n"
1238 "dot1xSuppEapLengthErrorFramesRx=%u\n"
1239 "dot1xSuppLastEapolFrameVersion=%u\n"
1240 "dot1xSuppLastEapolFrameSource=" MACSTR "\n",
1241 sm->dot1xSuppEapolFramesRx,
1242 sm->dot1xSuppEapolFramesTx,
1243 sm->dot1xSuppEapolStartFramesTx,
1244 sm->dot1xSuppEapolLogoffFramesTx,
1245 sm->dot1xSuppEapolRespFramesTx,
1246 sm->dot1xSuppEapolReqIdFramesRx,
1247 sm->dot1xSuppEapolReqFramesRx,
1248 sm->dot1xSuppInvalidEapolFramesRx,
1249 sm->dot1xSuppEapLengthErrorFramesRx,
1250 sm->dot1xSuppLastEapolFrameVersion,
1251 MAC2STR(sm->dot1xSuppLastEapolFrameSource));
1253 if (os_snprintf_error(buflen - len, ret))
1259 #endif /* CONFIG_CTRL_IFACE */
1263 * eapol_sm_rx_eapol - Process received EAPOL frames
1264 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1265 * @src: Source MAC address of the EAPOL packet
1266 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1267 * @len: Length of the EAPOL frame
1268 * Returns: 1 = EAPOL frame processed, 0 = not for EAPOL state machine,
1271 int eapol_sm_rx_eapol(struct eapol_sm *sm, const u8 *src, const u8 *buf,
1274 const struct ieee802_1x_hdr *hdr;
1275 const struct ieee802_1x_eapol_key *key;
1282 sm->dot1xSuppEapolFramesRx++;
1283 if (len < sizeof(*hdr)) {
1284 sm->dot1xSuppInvalidEapolFramesRx++;
1287 hdr = (const struct ieee802_1x_hdr *) buf;
1288 sm->dot1xSuppLastEapolFrameVersion = hdr->version;
1289 os_memcpy(sm->dot1xSuppLastEapolFrameSource, src, ETH_ALEN);
1290 if (hdr->version < EAPOL_VERSION) {
1291 /* TODO: backwards compatibility */
1293 plen = be_to_host16(hdr->length);
1294 if (plen > len - sizeof(*hdr)) {
1295 sm->dot1xSuppEapLengthErrorFramesRx++;
1299 if (sm->conf.wps && sm->conf.workaround &&
1300 plen < len - sizeof(*hdr) &&
1301 hdr->type == IEEE802_1X_TYPE_EAP_PACKET &&
1302 len - sizeof(*hdr) > sizeof(struct eap_hdr)) {
1303 const struct eap_hdr *ehdr =
1304 (const struct eap_hdr *) (hdr + 1);
1307 elen = be_to_host16(ehdr->length);
1308 if (elen > plen && elen <= len - sizeof(*hdr)) {
1310 * Buffalo WHR-G125 Ver.1.47 seems to send EAP-WPS
1311 * packets with too short EAPOL header length field
1312 * (14 octets). This is fixed in firmware Ver.1.49.
1313 * As a workaround, fix the EAPOL header based on the
1314 * correct length in the EAP packet.
1316 wpa_printf(MSG_DEBUG, "EAPOL: Workaround - fix EAPOL "
1317 "payload length based on EAP header: "
1318 "%d -> %d", (int) plen, elen);
1322 #endif /* CONFIG_WPS */
1323 data_len = plen + sizeof(*hdr);
1325 switch (hdr->type) {
1326 case IEEE802_1X_TYPE_EAP_PACKET:
1327 if (sm->conf.workaround) {
1329 * An AP has been reported to send out EAP message with
1330 * undocumented code 10 at some point near the
1331 * completion of EAP authentication. This can result in
1332 * issues with the unexpected EAP message triggering
1333 * restart of EAPOL authentication. Avoid this by
1334 * skipping the message without advancing the state
1337 const struct eap_hdr *ehdr =
1338 (const struct eap_hdr *) (hdr + 1);
1339 if (plen >= sizeof(*ehdr) && ehdr->code == 10) {
1340 wpa_printf(MSG_DEBUG, "EAPOL: Ignore EAP packet with unknown code 10");
1345 if (sm->cached_pmk) {
1346 /* Trying to use PMKSA caching, but Authenticator did
1347 * not seem to have a matching entry. Need to restart
1348 * EAPOL state machines.
1350 eapol_sm_abort_cached(sm);
1352 wpabuf_free(sm->eapReqData);
1353 sm->eapReqData = wpabuf_alloc_copy(hdr + 1, plen);
1354 if (sm->eapReqData) {
1355 wpa_printf(MSG_DEBUG, "EAPOL: Received EAP-Packet "
1357 sm->eapolEap = TRUE;
1358 #ifdef CONFIG_EAP_PROXY
1359 if (sm->use_eap_proxy) {
1360 eap_proxy_packet_update(
1362 wpabuf_mhead_u8(sm->eapReqData),
1363 wpabuf_len(sm->eapReqData));
1364 wpa_printf(MSG_DEBUG, "EAPOL: eap_proxy "
1367 #endif /* CONFIG_EAP_PROXY */
1371 case IEEE802_1X_TYPE_EAPOL_KEY:
1372 if (plen < sizeof(*key)) {
1373 wpa_printf(MSG_DEBUG, "EAPOL: Too short EAPOL-Key "
1377 key = (const struct ieee802_1x_eapol_key *) (hdr + 1);
1378 if (key->type == EAPOL_KEY_TYPE_WPA ||
1379 key->type == EAPOL_KEY_TYPE_RSN) {
1380 /* WPA Supplicant takes care of this frame. */
1381 wpa_printf(MSG_DEBUG, "EAPOL: Ignoring WPA EAPOL-Key "
1382 "frame in EAPOL state machines");
1386 if (key->type != EAPOL_KEY_TYPE_RC4) {
1387 wpa_printf(MSG_DEBUG, "EAPOL: Ignored unknown "
1388 "EAPOL-Key type %d", key->type);
1391 os_free(sm->last_rx_key);
1392 sm->last_rx_key = os_malloc(data_len);
1393 if (sm->last_rx_key) {
1394 wpa_printf(MSG_DEBUG, "EAPOL: Received EAPOL-Key "
1396 os_memcpy(sm->last_rx_key, buf, data_len);
1397 sm->last_rx_key_len = data_len;
1402 #ifdef CONFIG_MACSEC
1403 case IEEE802_1X_TYPE_EAPOL_MKA:
1404 wpa_printf(MSG_EXCESSIVE,
1405 "EAPOL type %d will be handled by MKA",
1408 #endif /* CONFIG_MACSEC */
1410 wpa_printf(MSG_DEBUG, "EAPOL: Received unknown EAPOL type %d",
1412 sm->dot1xSuppInvalidEapolFramesRx++;
1421 * eapol_sm_notify_tx_eapol_key - Notification about transmitted EAPOL packet
1422 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1424 * Notify EAPOL state machine about transmitted EAPOL packet from an external
1425 * component, e.g., WPA. This will update the statistics.
1427 void eapol_sm_notify_tx_eapol_key(struct eapol_sm *sm)
1430 sm->dot1xSuppEapolFramesTx++;
1435 * eapol_sm_notify_portEnabled - Notification about portEnabled change
1436 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1437 * @enabled: New portEnabled value
1439 * Notify EAPOL state machine about new portEnabled value.
1441 void eapol_sm_notify_portEnabled(struct eapol_sm *sm, Boolean enabled)
1445 wpa_printf(MSG_DEBUG, "EAPOL: External notification - "
1446 "portEnabled=%d", enabled);
1447 if (sm->portEnabled != enabled)
1448 sm->force_authorized_update = TRUE;
1449 sm->portEnabled = enabled;
1455 * eapol_sm_notify_portValid - Notification about portValid change
1456 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1457 * @valid: New portValid value
1459 * Notify EAPOL state machine about new portValid value.
1461 void eapol_sm_notify_portValid(struct eapol_sm *sm, Boolean valid)
1465 wpa_printf(MSG_DEBUG, "EAPOL: External notification - "
1466 "portValid=%d", valid);
1467 sm->portValid = valid;
1473 * eapol_sm_notify_eap_success - Notification of external EAP success trigger
1474 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1475 * @success: %TRUE = set success, %FALSE = clear success
1477 * Notify the EAPOL state machine that external event has forced EAP state to
1478 * success (success = %TRUE). This can be cleared by setting success = %FALSE.
1480 * This function is called to update EAP state when WPA-PSK key handshake has
1481 * been completed successfully since WPA-PSK does not use EAP state machine.
1483 void eapol_sm_notify_eap_success(struct eapol_sm *sm, Boolean success)
1487 wpa_printf(MSG_DEBUG, "EAPOL: External notification - "
1488 "EAP success=%d", success);
1489 sm->eapSuccess = success;
1490 sm->altAccept = success;
1492 eap_notify_success(sm->eap);
1498 * eapol_sm_notify_eap_fail - Notification of external EAP failure trigger
1499 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1500 * @fail: %TRUE = set failure, %FALSE = clear failure
1502 * Notify EAPOL state machine that external event has forced EAP state to
1503 * failure (fail = %TRUE). This can be cleared by setting fail = %FALSE.
1505 void eapol_sm_notify_eap_fail(struct eapol_sm *sm, Boolean fail)
1509 wpa_printf(MSG_DEBUG, "EAPOL: External notification - "
1510 "EAP fail=%d", fail);
1512 sm->altReject = fail;
1518 * eapol_sm_notify_config - Notification of EAPOL configuration change
1519 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1520 * @config: Pointer to current network EAP configuration
1521 * @conf: Pointer to EAPOL configuration data
1523 * Notify EAPOL state machine that configuration has changed. config will be
1524 * stored as a backpointer to network configuration. This can be %NULL to clear
1525 * the stored pointed. conf will be copied to local EAPOL/EAP configuration
1526 * data. If conf is %NULL, this part of the configuration change will be
1529 void eapol_sm_notify_config(struct eapol_sm *sm,
1530 struct eap_peer_config *config,
1531 const struct eapol_config *conf)
1536 sm->config = config;
1537 #ifdef CONFIG_EAP_PROXY
1538 sm->use_eap_proxy = eap_proxy_notify_config(sm->eap_proxy, config) > 0;
1539 #endif /* CONFIG_EAP_PROXY */
1544 sm->conf.accept_802_1x_keys = conf->accept_802_1x_keys;
1545 sm->conf.required_keys = conf->required_keys;
1546 sm->conf.fast_reauth = conf->fast_reauth;
1547 sm->conf.workaround = conf->workaround;
1548 sm->conf.wps = conf->wps;
1549 #ifdef CONFIG_EAP_PROXY
1550 if (sm->use_eap_proxy) {
1551 /* Using EAP Proxy, so skip EAP state machine update */
1554 #endif /* CONFIG_EAP_PROXY */
1556 eap_set_fast_reauth(sm->eap, conf->fast_reauth);
1557 eap_set_workaround(sm->eap, conf->workaround);
1558 eap_set_force_disabled(sm->eap, conf->eap_disabled);
1559 eap_set_external_sim(sm->eap, conf->external_sim);
1565 * eapol_sm_get_key - Get master session key (MSK) from EAP
1566 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1567 * @key: Pointer for key buffer
1568 * @len: Number of bytes to copy to key
1569 * Returns: 0 on success (len of key available), maximum available key len
1570 * (>0) if key is available but it is shorter than len, or -1 on failure.
1572 * Fetch EAP keying material (MSK, eapKeyData) from EAP state machine. The key
1573 * is available only after a successful authentication.
1575 int eapol_sm_get_key(struct eapol_sm *sm, u8 *key, size_t len)
1580 #ifdef CONFIG_EAP_PROXY
1581 if (sm && sm->use_eap_proxy) {
1582 /* Get key from EAP proxy */
1583 if (sm == NULL || !eap_proxy_key_available(sm->eap_proxy)) {
1584 wpa_printf(MSG_DEBUG, "EAPOL: EAP key not available");
1587 eap_key = eap_proxy_get_eapKeyData(sm->eap_proxy, &eap_len);
1588 if (eap_key == NULL) {
1589 wpa_printf(MSG_DEBUG, "EAPOL: Failed to get "
1595 #endif /* CONFIG_EAP_PROXY */
1596 if (sm == NULL || !eap_key_available(sm->eap)) {
1597 wpa_printf(MSG_DEBUG, "EAPOL: EAP key not available");
1600 eap_key = eap_get_eapKeyData(sm->eap, &eap_len);
1601 if (eap_key == NULL) {
1602 wpa_printf(MSG_DEBUG, "EAPOL: Failed to get eapKeyData");
1605 #ifdef CONFIG_EAP_PROXY
1607 #endif /* CONFIG_EAP_PROXY */
1608 if (len > eap_len) {
1609 wpa_printf(MSG_DEBUG, "EAPOL: Requested key length (%lu) not "
1610 "available (len=%lu)",
1611 (unsigned long) len, (unsigned long) eap_len);
1614 os_memcpy(key, eap_key, len);
1615 wpa_printf(MSG_DEBUG, "EAPOL: Successfully fetched key (len=%lu)",
1616 (unsigned long) len);
1622 * eapol_sm_get_session_id - Get EAP Session-Id
1623 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1624 * @len: Pointer to variable that will be set to number of bytes in the session
1625 * Returns: Pointer to the EAP Session-Id or %NULL on failure
1627 * The Session-Id is available only after a successful authentication.
1629 const u8 * eapol_sm_get_session_id(struct eapol_sm *sm, size_t *len)
1631 if (sm == NULL || !eap_key_available(sm->eap)) {
1632 wpa_printf(MSG_DEBUG, "EAPOL: EAP Session-Id not available");
1635 return eap_get_eapSessionId(sm->eap, len);
1640 * eapol_sm_notify_logoff - Notification of logon/logoff commands
1641 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1642 * @logoff: Whether command was logoff
1644 * Notify EAPOL state machines that user requested logon/logoff.
1646 void eapol_sm_notify_logoff(struct eapol_sm *sm, Boolean logoff)
1649 sm->userLogoff = logoff;
1651 /* If there is a delayed txStart queued, start now. */
1660 * eapol_sm_notify_pmkid_attempt - Notification of successful PMKSA caching
1661 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1663 * Notify EAPOL state machines that PMKSA caching was successful. This is used
1664 * to move EAPOL and EAP state machines into authenticated/successful state.
1666 void eapol_sm_notify_cached(struct eapol_sm *sm)
1670 wpa_printf(MSG_DEBUG, "EAPOL: PMKSA caching was used - skip EAPOL");
1671 sm->eapSuccess = TRUE;
1672 eap_notify_success(sm->eap);
1678 * eapol_sm_notify_pmkid_attempt - Notification of PMKSA caching
1679 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1681 * Notify EAPOL state machines if PMKSA caching is used.
1683 void eapol_sm_notify_pmkid_attempt(struct eapol_sm *sm)
1687 wpa_printf(MSG_DEBUG, "RSN: Trying to use cached PMKSA");
1688 sm->cached_pmk = TRUE;
1692 static void eapol_sm_abort_cached(struct eapol_sm *sm)
1694 wpa_printf(MSG_DEBUG, "RSN: Authenticator did not accept PMKID, "
1695 "doing full EAP authentication");
1698 sm->cached_pmk = FALSE;
1699 sm->SUPP_PAE_state = SUPP_PAE_CONNECTING;
1700 eapol_sm_set_port_unauthorized(sm);
1702 /* Make sure we do not start sending EAPOL-Start frames first, but
1703 * instead move to RESTART state to start EAPOL authentication. */
1705 eapol_enable_timer_tick(sm);
1707 if (sm->ctx->aborted_cached)
1708 sm->ctx->aborted_cached(sm->ctx->ctx);
1713 * eapol_sm_register_scard_ctx - Notification of smart card context
1714 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1715 * @ctx: Context data for smart card operations
1717 * Notify EAPOL state machines of context data for smart card operations. This
1718 * context data will be used as a parameter for scard_*() functions.
1720 void eapol_sm_register_scard_ctx(struct eapol_sm *sm, void *ctx)
1723 sm->ctx->scard_ctx = ctx;
1724 eap_register_scard_ctx(sm->eap, ctx);
1730 * eapol_sm_notify_portControl - Notification of portControl changes
1731 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1732 * @portControl: New value for portControl variable
1734 * Notify EAPOL state machines that portControl variable has changed.
1736 void eapol_sm_notify_portControl(struct eapol_sm *sm, PortControl portControl)
1740 wpa_printf(MSG_DEBUG, "EAPOL: External notification - "
1741 "portControl=%s", eapol_port_control(portControl));
1742 sm->portControl = portControl;
1748 * eapol_sm_notify_ctrl_attached - Notification of attached monitor
1749 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1751 * Notify EAPOL state machines that a monitor was attached to the control
1752 * interface to trigger re-sending of pending requests for user input.
1754 void eapol_sm_notify_ctrl_attached(struct eapol_sm *sm)
1758 eap_sm_notify_ctrl_attached(sm->eap);
1763 * eapol_sm_notify_ctrl_response - Notification of received user input
1764 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1766 * Notify EAPOL state machines that a control response, i.e., user
1767 * input, was received in order to trigger retrying of a pending EAP request.
1769 void eapol_sm_notify_ctrl_response(struct eapol_sm *sm)
1773 if (sm->eapReqData && !sm->eapReq) {
1774 wpa_printf(MSG_DEBUG, "EAPOL: received control response (user "
1775 "input) notification - retrying pending EAP "
1777 sm->eapolEap = TRUE;
1785 * eapol_sm_request_reauth - Request reauthentication
1786 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1788 * This function can be used to request EAPOL reauthentication, e.g., when the
1789 * current PMKSA entry is nearing expiration.
1791 void eapol_sm_request_reauth(struct eapol_sm *sm)
1793 if (sm == NULL || sm->SUPP_PAE_state != SUPP_PAE_AUTHENTICATED)
1795 eapol_sm_txStart(sm);
1800 * eapol_sm_notify_lower_layer_success - Notification of lower layer success
1801 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1802 * @in_eapol_sm: Whether the caller is already running inside EAPOL state
1803 * machine loop (eapol_sm_step())
1805 * Notify EAPOL (and EAP) state machines that a lower layer has detected a
1806 * successful authentication. This is used to recover from dropped EAP-Success
1809 void eapol_sm_notify_lower_layer_success(struct eapol_sm *sm, int in_eapol_sm)
1813 eap_notify_lower_layer_success(sm->eap);
1820 * eapol_sm_invalidate_cached_session - Mark cached EAP session data invalid
1821 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
1823 void eapol_sm_invalidate_cached_session(struct eapol_sm *sm)
1826 eap_invalidate_cached_session(sm->eap);
1830 static struct eap_peer_config * eapol_sm_get_config(void *ctx)
1832 struct eapol_sm *sm = ctx;
1833 return sm ? sm->config : NULL;
1837 static struct wpabuf * eapol_sm_get_eapReqData(void *ctx)
1839 struct eapol_sm *sm = ctx;
1840 if (sm == NULL || sm->eapReqData == NULL)
1843 return sm->eapReqData;
1847 static Boolean eapol_sm_get_bool(void *ctx, enum eapol_bool_var variable)
1849 struct eapol_sm *sm = ctx;
1853 case EAPOL_eapSuccess:
1854 return sm->eapSuccess;
1855 case EAPOL_eapRestart:
1856 return sm->eapRestart;
1861 case EAPOL_eapNoResp:
1862 return sm->eapNoResp;
1865 case EAPOL_portEnabled:
1866 return sm->portEnabled;
1867 case EAPOL_altAccept:
1868 return sm->altAccept;
1869 case EAPOL_altReject:
1870 return sm->altReject;
1871 case EAPOL_eapTriggerStart:
1872 return sm->eapTriggerStart;
1878 static void eapol_sm_set_bool(void *ctx, enum eapol_bool_var variable,
1881 struct eapol_sm *sm = ctx;
1885 case EAPOL_eapSuccess:
1886 sm->eapSuccess = value;
1888 case EAPOL_eapRestart:
1889 sm->eapRestart = value;
1892 sm->eapFail = value;
1895 sm->eapResp = value;
1897 case EAPOL_eapNoResp:
1898 sm->eapNoResp = value;
1903 case EAPOL_portEnabled:
1904 sm->portEnabled = value;
1906 case EAPOL_altAccept:
1907 sm->altAccept = value;
1909 case EAPOL_altReject:
1910 sm->altReject = value;
1912 case EAPOL_eapTriggerStart:
1913 sm->eapTriggerStart = value;
1919 static unsigned int eapol_sm_get_int(void *ctx, enum eapol_int_var variable)
1921 struct eapol_sm *sm = ctx;
1925 case EAPOL_idleWhile:
1926 return sm->idleWhile;
1932 static void eapol_sm_set_int(void *ctx, enum eapol_int_var variable,
1935 struct eapol_sm *sm = ctx;
1939 case EAPOL_idleWhile:
1940 sm->idleWhile = value;
1941 if (sm->idleWhile > 0)
1942 eapol_enable_timer_tick(sm);
1948 static void eapol_sm_set_config_blob(void *ctx, struct wpa_config_blob *blob)
1950 #ifndef CONFIG_NO_CONFIG_BLOBS
1951 struct eapol_sm *sm = ctx;
1952 if (sm && sm->ctx && sm->ctx->set_config_blob)
1953 sm->ctx->set_config_blob(sm->ctx->ctx, blob);
1954 #endif /* CONFIG_NO_CONFIG_BLOBS */
1958 static const struct wpa_config_blob *
1959 eapol_sm_get_config_blob(void *ctx, const char *name)
1961 #ifndef CONFIG_NO_CONFIG_BLOBS
1962 struct eapol_sm *sm = ctx;
1963 if (sm && sm->ctx && sm->ctx->get_config_blob)
1964 return sm->ctx->get_config_blob(sm->ctx->ctx, name);
1967 #else /* CONFIG_NO_CONFIG_BLOBS */
1969 #endif /* CONFIG_NO_CONFIG_BLOBS */
1973 static void eapol_sm_notify_pending(void *ctx)
1975 struct eapol_sm *sm = ctx;
1978 if (sm->eapReqData && !sm->eapReq) {
1979 wpa_printf(MSG_DEBUG, "EAPOL: received notification from EAP "
1980 "state machine - retrying pending EAP Request");
1981 sm->eapolEap = TRUE;
1988 #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
1989 static void eapol_sm_eap_param_needed(void *ctx, enum wpa_ctrl_req_type field,
1992 struct eapol_sm *sm = ctx;
1993 wpa_printf(MSG_DEBUG, "EAPOL: EAP parameter needed");
1994 if (sm->ctx->eap_param_needed)
1995 sm->ctx->eap_param_needed(sm->ctx->ctx, field, txt);
1997 #else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
1998 #define eapol_sm_eap_param_needed NULL
1999 #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
2001 static void eapol_sm_notify_cert(void *ctx, int depth, const char *subject,
2002 const char *altsubject[],
2003 int num_altsubject, const char *cert_hash,
2004 const struct wpabuf *cert)
2006 struct eapol_sm *sm = ctx;
2007 if (sm->ctx->cert_cb)
2008 sm->ctx->cert_cb(sm->ctx->ctx, depth, subject, altsubject,
2009 num_altsubject, cert_hash, cert);
2013 static void eapol_sm_notify_status(void *ctx, const char *status,
2014 const char *parameter)
2016 struct eapol_sm *sm = ctx;
2018 if (sm->ctx->status_cb)
2019 sm->ctx->status_cb(sm->ctx->ctx, status, parameter);
2023 static void eapol_sm_notify_eap_error(void *ctx, int error_code)
2025 struct eapol_sm *sm = ctx;
2027 if (sm->ctx->eap_error_cb)
2028 sm->ctx->eap_error_cb(sm->ctx->ctx, error_code);
2032 #ifdef CONFIG_EAP_PROXY
2034 static void eapol_sm_eap_proxy_cb(void *ctx)
2036 struct eapol_sm *sm = ctx;
2038 if (sm->ctx->eap_proxy_cb)
2039 sm->ctx->eap_proxy_cb(sm->ctx->ctx);
2044 eapol_sm_eap_proxy_notify_sim_status(void *ctx,
2045 enum eap_proxy_sim_state sim_state)
2047 struct eapol_sm *sm = ctx;
2049 if (sm->ctx->eap_proxy_notify_sim_status)
2050 sm->ctx->eap_proxy_notify_sim_status(sm->ctx->ctx, sim_state);
2053 #endif /* CONFIG_EAP_PROXY */
2056 static void eapol_sm_set_anon_id(void *ctx, const u8 *id, size_t len)
2058 struct eapol_sm *sm = ctx;
2060 if (sm->ctx->set_anon_id)
2061 sm->ctx->set_anon_id(sm->ctx->ctx, id, len);
2065 static const struct eapol_callbacks eapol_cb =
2067 eapol_sm_get_config,
2072 eapol_sm_get_eapReqData,
2073 eapol_sm_set_config_blob,
2074 eapol_sm_get_config_blob,
2075 eapol_sm_notify_pending,
2076 eapol_sm_eap_param_needed,
2077 eapol_sm_notify_cert,
2078 eapol_sm_notify_status,
2079 eapol_sm_notify_eap_error,
2080 #ifdef CONFIG_EAP_PROXY
2081 eapol_sm_eap_proxy_cb,
2082 eapol_sm_eap_proxy_notify_sim_status,
2083 eapol_sm_get_eap_proxy_imsi,
2084 #endif /* CONFIG_EAP_PROXY */
2085 eapol_sm_set_anon_id
2090 * eapol_sm_init - Initialize EAPOL state machine
2091 * @ctx: Pointer to EAPOL context data; this needs to be an allocated buffer
2092 * and EAPOL state machine will free it in eapol_sm_deinit()
2093 * Returns: Pointer to the allocated EAPOL state machine or %NULL on failure
2095 * Allocate and initialize an EAPOL state machine.
2097 struct eapol_sm *eapol_sm_init(struct eapol_ctx *ctx)
2099 struct eapol_sm *sm;
2100 struct eap_config conf;
2101 sm = os_zalloc(sizeof(*sm));
2106 sm->portControl = Auto;
2108 /* Supplicant PAE state machine */
2109 sm->heldPeriod = 60;
2110 sm->startPeriod = 30;
2113 /* Supplicant Backend state machine */
2114 sm->authPeriod = 30;
2116 os_memset(&conf, 0, sizeof(conf));
2117 conf.opensc_engine_path = ctx->opensc_engine_path;
2118 conf.pkcs11_engine_path = ctx->pkcs11_engine_path;
2119 conf.pkcs11_module_path = ctx->pkcs11_module_path;
2120 conf.openssl_ciphers = ctx->openssl_ciphers;
2121 conf.wps = ctx->wps;
2122 conf.cert_in_cb = ctx->cert_in_cb;
2124 sm->eap = eap_peer_sm_init(sm, &eapol_cb, sm->ctx->msg_ctx, &conf);
2125 if (sm->eap == NULL) {
2130 #ifdef CONFIG_EAP_PROXY
2131 sm->use_eap_proxy = FALSE;
2132 sm->eap_proxy = eap_proxy_init(sm, &eapol_cb, sm->ctx->msg_ctx);
2133 if (sm->eap_proxy == NULL) {
2134 wpa_printf(MSG_ERROR, "Unable to initialize EAP Proxy");
2136 #endif /* CONFIG_EAP_PROXY */
2138 /* Initialize EAPOL state machines */
2139 sm->force_authorized_update = TRUE;
2140 sm->initialize = TRUE;
2142 sm->initialize = FALSE;
2145 if (eloop_register_timeout(1, 0, eapol_port_timers_tick, NULL, sm) == 0)
2146 sm->timer_tick_enabled = 1;
2153 * eapol_sm_deinit - Deinitialize EAPOL state machine
2154 * @sm: Pointer to EAPOL state machine allocated with eapol_sm_init()
2156 * Deinitialize and free EAPOL state machine.
2158 void eapol_sm_deinit(struct eapol_sm *sm)
2162 eloop_cancel_timeout(eapol_sm_step_timeout, NULL, sm);
2163 eloop_cancel_timeout(eapol_port_timers_tick, NULL, sm);
2164 eap_peer_sm_deinit(sm->eap);
2165 #ifdef CONFIG_EAP_PROXY
2166 eap_proxy_deinit(sm->eap_proxy);
2167 #endif /* CONFIG_EAP_PROXY */
2168 os_free(sm->last_rx_key);
2169 wpabuf_free(sm->eapReqData);
2175 void eapol_sm_set_ext_pw_ctx(struct eapol_sm *sm,
2176 struct ext_password_data *ext)
2179 eap_sm_set_ext_pw_ctx(sm->eap, ext);
2183 int eapol_sm_failed(struct eapol_sm *sm)
2187 return !sm->eapSuccess && sm->eapFail;
2191 #ifdef CONFIG_EAP_PROXY
2192 int eapol_sm_get_eap_proxy_imsi(void *ctx, int sim_num, char *imsi, size_t *len)
2194 struct eapol_sm *sm = ctx;
2196 if (sm->eap_proxy == NULL)
2198 return eap_proxy_get_imsi(sm->eap_proxy, sim_num, imsi, len);
2200 #endif /* CONFIG_EAP_PROXY */
2203 void eapol_sm_erp_flush(struct eapol_sm *sm)
2206 eap_peer_erp_free_keys(sm->eap);
2210 struct wpabuf * eapol_sm_build_erp_reauth_start(struct eapol_sm *sm)
2215 return eap_peer_build_erp_reauth_start(sm->eap, 0);
2216 #else /* CONFIG_ERP */
2218 #endif /* CONFIG_ERP */
2222 void eapol_sm_process_erp_finish(struct eapol_sm *sm, const u8 *buf,
2228 eap_peer_finish(sm->eap, (const struct eap_hdr *) buf, len);
2229 #endif /* CONFIG_ERP */
2233 int eapol_sm_update_erp_next_seq_num(struct eapol_sm *sm, u16 next_seq_num)
2238 return eap_peer_update_erp_next_seq_num(sm->eap, next_seq_num);
2239 #else /* CONFIG_ERP */
2241 #endif /* CONFIG_ERP */
2245 int eapol_sm_get_erp_info(struct eapol_sm *sm, struct eap_peer_config *config,
2246 const u8 **username, size_t *username_len,
2247 const u8 **realm, size_t *realm_len,
2248 u16 *erp_next_seq_num, const u8 **rrk,
2254 return eap_peer_get_erp_info(sm->eap, config, username, username_len,
2255 realm, realm_len, erp_next_seq_num, rrk,
2257 #else /* CONFIG_ERP */
2259 #endif /* CONFIG_ERP */