2 * hostapd / EAP Full Authenticator state machine (RFC 4137)
3 * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
14 * This state machine is based on the full authenticator state machine defined
15 * in RFC 4137. However, to support backend authentication in RADIUS
16 * authentication server functionality, parts of backend authenticator (also
17 * from RFC 4137) are mixed in. This functionality is enabled by setting
18 * backend_auth configuration variable to TRUE.
25 #include "state_machine.h"
26 #include "common/wpa_ctrl.h"
28 #define STATE_MACHINE_DATA struct eap_sm
29 #define STATE_MACHINE_DEBUG_PREFIX "EAP"
31 #define EAP_MAX_AUTH_ROUNDS 50
33 static void eap_user_free(struct eap_user *user);
36 /* EAP state machines are described in RFC 4137 */
38 static int eap_sm_calculateTimeout(struct eap_sm *sm, int retransCount,
39 int eapSRTT, int eapRTTVAR,
41 static void eap_sm_parseEapResp(struct eap_sm *sm, const struct wpabuf *resp);
42 static int eap_sm_getId(const struct wpabuf *data);
43 static struct wpabuf * eap_sm_buildSuccess(struct eap_sm *sm, u8 id);
44 static struct wpabuf * eap_sm_buildFailure(struct eap_sm *sm, u8 id);
45 static int eap_sm_nextId(struct eap_sm *sm, int id);
46 static void eap_sm_Policy_update(struct eap_sm *sm, const u8 *nak_list,
48 static EapType eap_sm_Policy_getNextMethod(struct eap_sm *sm, int *vendor);
49 static int eap_sm_Policy_getDecision(struct eap_sm *sm);
50 static Boolean eap_sm_Policy_doPickUp(struct eap_sm *sm, EapType method);
53 static int eap_copy_buf(struct wpabuf **dst, const struct wpabuf *src)
59 *dst = wpabuf_dup(src);
64 static int eap_copy_data(u8 **dst, size_t *dst_len,
65 const u8 *src, size_t src_len)
71 *dst = os_malloc(src_len);
73 os_memcpy(*dst, src, src_len);
82 #define EAP_COPY(dst, src) \
83 eap_copy_data((dst), (dst ## Len), (src), (src ## Len))
87 * eap_user_get - Fetch user information from the database
88 * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
89 * @identity: Identity (User-Name) of the user
90 * @identity_len: Length of identity in bytes
91 * @phase2: 0 = EAP phase1 user, 1 = EAP phase2 (tunneled) user
92 * Returns: 0 on success, or -1 on failure
94 * This function is used to fetch user information for EAP. The user will be
95 * selected based on the specified identity. sm->user and
96 * sm->user_eap_method_index are updated for the new user when a matching user
97 * is found. sm->user can be used to get user information (e.g., password).
99 int eap_user_get(struct eap_sm *sm, const u8 *identity, size_t identity_len,
102 struct eap_user *user;
104 if (sm == NULL || sm->eapol_cb == NULL ||
105 sm->eapol_cb->get_eap_user == NULL)
108 eap_user_free(sm->user);
111 user = os_zalloc(sizeof(*user));
115 if (sm->eapol_cb->get_eap_user(sm->eapol_ctx, identity,
116 identity_len, phase2, user) != 0) {
122 sm->user_eap_method_index = 0;
128 SM_STATE(EAP, DISABLED)
130 SM_ENTRY(EAP, DISABLED);
135 SM_STATE(EAP, INITIALIZE)
137 SM_ENTRY(EAP, INITIALIZE);
140 sm->eap_if.eapSuccess = FALSE;
141 sm->eap_if.eapFail = FALSE;
142 sm->eap_if.eapTimeout = FALSE;
143 os_free(sm->eap_if.eapKeyData);
144 sm->eap_if.eapKeyData = NULL;
145 sm->eap_if.eapKeyDataLen = 0;
146 sm->eap_if.eapKeyAvailable = FALSE;
147 sm->eap_if.eapRestart = FALSE;
150 * Start reauthentication with identity request even if we know the
151 * previously used identity. This is needed to get reauthentication
154 sm->start_reauth = TRUE;
157 * This is not defined in RFC 4137, but method state needs to be
158 * reseted here so that it does not remain in success state when
159 * re-authentication starts.
161 if (sm->m && sm->eap_method_priv) {
162 sm->m->reset(sm, sm->eap_method_priv);
163 sm->eap_method_priv = NULL;
166 sm->user_eap_method_index = 0;
168 if (sm->backend_auth) {
169 sm->currentMethod = EAP_TYPE_NONE;
170 /* parse rxResp, respId, respMethod */
171 eap_sm_parseEapResp(sm, sm->eap_if.eapRespData);
173 sm->currentId = sm->respId;
177 sm->method_pending = METHOD_PENDING_NONE;
179 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_STARTED
180 MACSTR, MAC2STR(sm->peer_addr));
184 SM_STATE(EAP, PICK_UP_METHOD)
186 SM_ENTRY(EAP, PICK_UP_METHOD);
188 if (eap_sm_Policy_doPickUp(sm, sm->respMethod)) {
189 sm->currentMethod = sm->respMethod;
190 if (sm->m && sm->eap_method_priv) {
191 sm->m->reset(sm, sm->eap_method_priv);
192 sm->eap_method_priv = NULL;
194 sm->m = eap_server_get_eap_method(EAP_VENDOR_IETF,
196 if (sm->m && sm->m->initPickUp) {
197 sm->eap_method_priv = sm->m->initPickUp(sm);
198 if (sm->eap_method_priv == NULL) {
199 wpa_printf(MSG_DEBUG, "EAP: Failed to "
200 "initialize EAP method %d",
203 sm->currentMethod = EAP_TYPE_NONE;
207 sm->currentMethod = EAP_TYPE_NONE;
211 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_PROPOSED_METHOD
212 "method=%u", sm->currentMethod);
220 sm->eap_if.retransWhile = eap_sm_calculateTimeout(
221 sm, sm->retransCount, sm->eap_if.eapSRTT, sm->eap_if.eapRTTVAR,
226 SM_STATE(EAP, RETRANSMIT)
228 SM_ENTRY(EAP, RETRANSMIT);
231 if (sm->retransCount <= sm->MaxRetrans && sm->lastReqData) {
232 if (eap_copy_buf(&sm->eap_if.eapReqData, sm->lastReqData) == 0)
233 sm->eap_if.eapReq = TRUE;
238 SM_STATE(EAP, RECEIVED)
240 SM_ENTRY(EAP, RECEIVED);
242 /* parse rxResp, respId, respMethod */
243 eap_sm_parseEapResp(sm, sm->eap_if.eapRespData);
248 SM_STATE(EAP, DISCARD)
250 SM_ENTRY(EAP, DISCARD);
251 sm->eap_if.eapResp = FALSE;
252 sm->eap_if.eapNoReq = TRUE;
256 SM_STATE(EAP, SEND_REQUEST)
258 SM_ENTRY(EAP, SEND_REQUEST);
260 sm->retransCount = 0;
261 if (sm->eap_if.eapReqData) {
262 if (eap_copy_buf(&sm->lastReqData, sm->eap_if.eapReqData) == 0)
264 sm->eap_if.eapResp = FALSE;
265 sm->eap_if.eapReq = TRUE;
267 sm->eap_if.eapResp = FALSE;
268 sm->eap_if.eapReq = FALSE;
271 wpa_printf(MSG_INFO, "EAP: SEND_REQUEST - no eapReqData");
272 sm->eap_if.eapResp = FALSE;
273 sm->eap_if.eapReq = FALSE;
274 sm->eap_if.eapNoReq = TRUE;
279 SM_STATE(EAP, INTEGRITY_CHECK)
281 SM_ENTRY(EAP, INTEGRITY_CHECK);
284 sm->ignore = sm->m->check(sm, sm->eap_method_priv,
285 sm->eap_if.eapRespData);
290 SM_STATE(EAP, METHOD_REQUEST)
292 SM_ENTRY(EAP, METHOD_REQUEST);
295 wpa_printf(MSG_DEBUG, "EAP: method not initialized");
299 sm->currentId = eap_sm_nextId(sm, sm->currentId);
300 wpa_printf(MSG_DEBUG, "EAP: building EAP-Request: Identifier %d",
302 sm->lastId = sm->currentId;
303 wpabuf_free(sm->eap_if.eapReqData);
304 sm->eap_if.eapReqData = sm->m->buildReq(sm, sm->eap_method_priv,
306 if (sm->m->getTimeout)
307 sm->methodTimeout = sm->m->getTimeout(sm, sm->eap_method_priv);
309 sm->methodTimeout = 0;
313 SM_STATE(EAP, METHOD_RESPONSE)
315 SM_ENTRY(EAP, METHOD_RESPONSE);
317 sm->m->process(sm, sm->eap_method_priv, sm->eap_if.eapRespData);
318 if (sm->m->isDone(sm, sm->eap_method_priv)) {
319 eap_sm_Policy_update(sm, NULL, 0);
320 os_free(sm->eap_if.eapKeyData);
322 sm->eap_if.eapKeyData = sm->m->getKey(
323 sm, sm->eap_method_priv,
324 &sm->eap_if.eapKeyDataLen);
326 sm->eap_if.eapKeyData = NULL;
327 sm->eap_if.eapKeyDataLen = 0;
329 sm->methodState = METHOD_END;
331 sm->methodState = METHOD_CONTINUE;
336 SM_STATE(EAP, PROPOSE_METHOD)
341 SM_ENTRY(EAP, PROPOSE_METHOD);
343 type = eap_sm_Policy_getNextMethod(sm, &vendor);
344 if (vendor == EAP_VENDOR_IETF)
345 sm->currentMethod = type;
347 sm->currentMethod = EAP_TYPE_EXPANDED;
348 if (sm->m && sm->eap_method_priv) {
349 sm->m->reset(sm, sm->eap_method_priv);
350 sm->eap_method_priv = NULL;
352 sm->m = eap_server_get_eap_method(vendor, type);
354 sm->eap_method_priv = sm->m->init(sm);
355 if (sm->eap_method_priv == NULL) {
356 wpa_printf(MSG_DEBUG, "EAP: Failed to initialize EAP "
357 "method %d", sm->currentMethod);
359 sm->currentMethod = EAP_TYPE_NONE;
362 if (sm->currentMethod == EAP_TYPE_IDENTITY ||
363 sm->currentMethod == EAP_TYPE_NOTIFICATION)
364 sm->methodState = METHOD_CONTINUE;
366 sm->methodState = METHOD_PROPOSED;
368 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_PROPOSED_METHOD
369 "vendor=%u method=%u", vendor, sm->currentMethod);
375 const struct eap_hdr *nak;
378 const u8 *nak_list = NULL;
382 if (sm->eap_method_priv) {
383 sm->m->reset(sm, sm->eap_method_priv);
384 sm->eap_method_priv = NULL;
388 nak = wpabuf_head(sm->eap_if.eapRespData);
389 if (nak && wpabuf_len(sm->eap_if.eapRespData) > sizeof(*nak)) {
390 len = be_to_host16(nak->length);
391 if (len > wpabuf_len(sm->eap_if.eapRespData))
392 len = wpabuf_len(sm->eap_if.eapRespData);
393 pos = (const u8 *) (nak + 1);
395 if (*pos == EAP_TYPE_NAK) {
401 eap_sm_Policy_update(sm, nak_list, len);
405 SM_STATE(EAP, SELECT_ACTION)
407 SM_ENTRY(EAP, SELECT_ACTION);
409 sm->decision = eap_sm_Policy_getDecision(sm);
413 SM_STATE(EAP, TIMEOUT_FAILURE)
415 SM_ENTRY(EAP, TIMEOUT_FAILURE);
417 sm->eap_if.eapTimeout = TRUE;
421 SM_STATE(EAP, FAILURE)
423 SM_ENTRY(EAP, FAILURE);
425 wpabuf_free(sm->eap_if.eapReqData);
426 sm->eap_if.eapReqData = eap_sm_buildFailure(sm, sm->currentId);
427 wpabuf_free(sm->lastReqData);
428 sm->lastReqData = NULL;
429 sm->eap_if.eapFail = TRUE;
431 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_FAILURE
432 MACSTR, MAC2STR(sm->peer_addr));
436 SM_STATE(EAP, SUCCESS)
438 SM_ENTRY(EAP, SUCCESS);
440 wpabuf_free(sm->eap_if.eapReqData);
441 sm->eap_if.eapReqData = eap_sm_buildSuccess(sm, sm->currentId);
442 wpabuf_free(sm->lastReqData);
443 sm->lastReqData = NULL;
444 if (sm->eap_if.eapKeyData)
445 sm->eap_if.eapKeyAvailable = TRUE;
446 sm->eap_if.eapSuccess = TRUE;
448 wpa_msg(sm->msg_ctx, MSG_INFO, WPA_EVENT_EAP_SUCCESS
449 MACSTR, MAC2STR(sm->peer_addr));
453 SM_STATE(EAP, INITIALIZE_PASSTHROUGH)
455 SM_ENTRY(EAP, INITIALIZE_PASSTHROUGH);
457 wpabuf_free(sm->eap_if.aaaEapRespData);
458 sm->eap_if.aaaEapRespData = NULL;
464 SM_ENTRY(EAP, IDLE2);
466 sm->eap_if.retransWhile = eap_sm_calculateTimeout(
467 sm, sm->retransCount, sm->eap_if.eapSRTT, sm->eap_if.eapRTTVAR,
472 SM_STATE(EAP, RETRANSMIT2)
474 SM_ENTRY(EAP, RETRANSMIT2);
477 if (sm->retransCount <= sm->MaxRetrans && sm->lastReqData) {
478 if (eap_copy_buf(&sm->eap_if.eapReqData, sm->lastReqData) == 0)
479 sm->eap_if.eapReq = TRUE;
484 SM_STATE(EAP, RECEIVED2)
486 SM_ENTRY(EAP, RECEIVED2);
488 /* parse rxResp, respId, respMethod */
489 eap_sm_parseEapResp(sm, sm->eap_if.eapRespData);
493 SM_STATE(EAP, DISCARD2)
495 SM_ENTRY(EAP, DISCARD2);
496 sm->eap_if.eapResp = FALSE;
497 sm->eap_if.eapNoReq = TRUE;
501 SM_STATE(EAP, SEND_REQUEST2)
503 SM_ENTRY(EAP, SEND_REQUEST2);
505 sm->retransCount = 0;
506 if (sm->eap_if.eapReqData) {
507 if (eap_copy_buf(&sm->lastReqData, sm->eap_if.eapReqData) == 0)
509 sm->eap_if.eapResp = FALSE;
510 sm->eap_if.eapReq = TRUE;
512 sm->eap_if.eapResp = FALSE;
513 sm->eap_if.eapReq = FALSE;
516 wpa_printf(MSG_INFO, "EAP: SEND_REQUEST2 - no eapReqData");
517 sm->eap_if.eapResp = FALSE;
518 sm->eap_if.eapReq = FALSE;
519 sm->eap_if.eapNoReq = TRUE;
524 SM_STATE(EAP, AAA_REQUEST)
526 SM_ENTRY(EAP, AAA_REQUEST);
528 if (sm->eap_if.eapRespData == NULL) {
529 wpa_printf(MSG_INFO, "EAP: AAA_REQUEST - no eapRespData");
534 * if (respMethod == IDENTITY)
535 * aaaIdentity = eapRespData
536 * This is already taken care of by the EAP-Identity method which
537 * stores the identity into sm->identity.
540 eap_copy_buf(&sm->eap_if.aaaEapRespData, sm->eap_if.eapRespData);
544 SM_STATE(EAP, AAA_RESPONSE)
546 SM_ENTRY(EAP, AAA_RESPONSE);
548 eap_copy_buf(&sm->eap_if.eapReqData, sm->eap_if.aaaEapReqData);
549 sm->currentId = eap_sm_getId(sm->eap_if.eapReqData);
550 sm->methodTimeout = sm->eap_if.aaaMethodTimeout;
554 SM_STATE(EAP, AAA_IDLE)
556 SM_ENTRY(EAP, AAA_IDLE);
558 sm->eap_if.aaaFail = FALSE;
559 sm->eap_if.aaaSuccess = FALSE;
560 sm->eap_if.aaaEapReq = FALSE;
561 sm->eap_if.aaaEapNoReq = FALSE;
562 sm->eap_if.aaaEapResp = TRUE;
566 SM_STATE(EAP, TIMEOUT_FAILURE2)
568 SM_ENTRY(EAP, TIMEOUT_FAILURE2);
570 sm->eap_if.eapTimeout = TRUE;
574 SM_STATE(EAP, FAILURE2)
576 SM_ENTRY(EAP, FAILURE2);
578 eap_copy_buf(&sm->eap_if.eapReqData, sm->eap_if.aaaEapReqData);
579 sm->eap_if.eapFail = TRUE;
583 SM_STATE(EAP, SUCCESS2)
585 SM_ENTRY(EAP, SUCCESS2);
587 eap_copy_buf(&sm->eap_if.eapReqData, sm->eap_if.aaaEapReqData);
589 sm->eap_if.eapKeyAvailable = sm->eap_if.aaaEapKeyAvailable;
590 if (sm->eap_if.aaaEapKeyAvailable) {
591 EAP_COPY(&sm->eap_if.eapKeyData, sm->eap_if.aaaEapKeyData);
593 os_free(sm->eap_if.eapKeyData);
594 sm->eap_if.eapKeyData = NULL;
595 sm->eap_if.eapKeyDataLen = 0;
598 sm->eap_if.eapSuccess = TRUE;
601 * Start reauthentication with identity request even though we know the
602 * previously used identity. This is needed to get reauthentication
605 sm->start_reauth = TRUE;
611 if (sm->eap_if.eapRestart && sm->eap_if.portEnabled)
612 SM_ENTER_GLOBAL(EAP, INITIALIZE);
613 else if (!sm->eap_if.portEnabled)
614 SM_ENTER_GLOBAL(EAP, DISABLED);
615 else if (sm->num_rounds > EAP_MAX_AUTH_ROUNDS) {
616 if (sm->num_rounds == EAP_MAX_AUTH_ROUNDS + 1) {
617 wpa_printf(MSG_DEBUG, "EAP: more than %d "
618 "authentication rounds - abort",
619 EAP_MAX_AUTH_ROUNDS);
621 SM_ENTER_GLOBAL(EAP, FAILURE);
623 } else switch (sm->EAP_state) {
625 if (sm->backend_auth) {
627 SM_ENTER(EAP, SELECT_ACTION);
628 else if (sm->rxResp &&
629 (sm->respMethod == EAP_TYPE_NAK ||
630 (sm->respMethod == EAP_TYPE_EXPANDED &&
631 sm->respVendor == EAP_VENDOR_IETF &&
632 sm->respVendorMethod == EAP_TYPE_NAK)))
635 SM_ENTER(EAP, PICK_UP_METHOD);
637 SM_ENTER(EAP, SELECT_ACTION);
640 case EAP_PICK_UP_METHOD:
641 if (sm->currentMethod == EAP_TYPE_NONE) {
642 SM_ENTER(EAP, SELECT_ACTION);
644 SM_ENTER(EAP, METHOD_RESPONSE);
648 if (sm->eap_if.portEnabled)
649 SM_ENTER(EAP, INITIALIZE);
652 if (sm->eap_if.retransWhile == 0)
653 SM_ENTER(EAP, RETRANSMIT);
654 else if (sm->eap_if.eapResp)
655 SM_ENTER(EAP, RECEIVED);
658 if (sm->retransCount > sm->MaxRetrans)
659 SM_ENTER(EAP, TIMEOUT_FAILURE);
664 if (sm->rxResp && (sm->respId == sm->currentId) &&
665 (sm->respMethod == EAP_TYPE_NAK ||
666 (sm->respMethod == EAP_TYPE_EXPANDED &&
667 sm->respVendor == EAP_VENDOR_IETF &&
668 sm->respVendorMethod == EAP_TYPE_NAK))
669 && (sm->methodState == METHOD_PROPOSED))
671 else if (sm->rxResp && (sm->respId == sm->currentId) &&
672 ((sm->respMethod == sm->currentMethod) ||
673 (sm->respMethod == EAP_TYPE_EXPANDED &&
674 sm->respVendor == EAP_VENDOR_IETF &&
675 sm->respVendorMethod == sm->currentMethod)))
676 SM_ENTER(EAP, INTEGRITY_CHECK);
678 wpa_printf(MSG_DEBUG, "EAP: RECEIVED->DISCARD: "
679 "rxResp=%d respId=%d currentId=%d "
680 "respMethod=%d currentMethod=%d",
681 sm->rxResp, sm->respId, sm->currentId,
682 sm->respMethod, sm->currentMethod);
683 SM_ENTER(EAP, DISCARD);
689 case EAP_SEND_REQUEST:
692 case EAP_INTEGRITY_CHECK:
694 SM_ENTER(EAP, DISCARD);
696 SM_ENTER(EAP, METHOD_RESPONSE);
698 case EAP_METHOD_REQUEST:
699 SM_ENTER(EAP, SEND_REQUEST);
701 case EAP_METHOD_RESPONSE:
703 * Note: Mechanism to allow EAP methods to wait while going
704 * through pending processing is an extension to RFC 4137
705 * which only defines the transits to SELECT_ACTION and
706 * METHOD_REQUEST from this METHOD_RESPONSE state.
708 if (sm->methodState == METHOD_END)
709 SM_ENTER(EAP, SELECT_ACTION);
710 else if (sm->method_pending == METHOD_PENDING_WAIT) {
711 wpa_printf(MSG_DEBUG, "EAP: Method has pending "
712 "processing - wait before proceeding to "
713 "METHOD_REQUEST state");
714 } else if (sm->method_pending == METHOD_PENDING_CONT) {
715 wpa_printf(MSG_DEBUG, "EAP: Method has completed "
716 "pending processing - reprocess pending "
718 sm->method_pending = METHOD_PENDING_NONE;
719 SM_ENTER(EAP, METHOD_RESPONSE);
721 SM_ENTER(EAP, METHOD_REQUEST);
723 case EAP_PROPOSE_METHOD:
725 * Note: Mechanism to allow EAP methods to wait while going
726 * through pending processing is an extension to RFC 4137
727 * which only defines the transit to METHOD_REQUEST from this
728 * PROPOSE_METHOD state.
730 if (sm->method_pending == METHOD_PENDING_WAIT) {
731 wpa_printf(MSG_DEBUG, "EAP: Method has pending "
732 "processing - wait before proceeding to "
733 "METHOD_REQUEST state");
734 if (sm->user_eap_method_index > 0)
735 sm->user_eap_method_index--;
736 } else if (sm->method_pending == METHOD_PENDING_CONT) {
737 wpa_printf(MSG_DEBUG, "EAP: Method has completed "
738 "pending processing - reprocess pending "
740 sm->method_pending = METHOD_PENDING_NONE;
741 SM_ENTER(EAP, PROPOSE_METHOD);
743 SM_ENTER(EAP, METHOD_REQUEST);
746 SM_ENTER(EAP, SELECT_ACTION);
748 case EAP_SELECT_ACTION:
749 if (sm->decision == DECISION_FAILURE)
750 SM_ENTER(EAP, FAILURE);
751 else if (sm->decision == DECISION_SUCCESS)
752 SM_ENTER(EAP, SUCCESS);
753 else if (sm->decision == DECISION_PASSTHROUGH)
754 SM_ENTER(EAP, INITIALIZE_PASSTHROUGH);
756 SM_ENTER(EAP, PROPOSE_METHOD);
758 case EAP_TIMEOUT_FAILURE:
765 case EAP_INITIALIZE_PASSTHROUGH:
766 if (sm->currentId == -1)
767 SM_ENTER(EAP, AAA_IDLE);
769 SM_ENTER(EAP, AAA_REQUEST);
772 if (sm->eap_if.eapResp)
773 SM_ENTER(EAP, RECEIVED2);
774 else if (sm->eap_if.retransWhile == 0)
775 SM_ENTER(EAP, RETRANSMIT2);
777 case EAP_RETRANSMIT2:
778 if (sm->retransCount > sm->MaxRetrans)
779 SM_ENTER(EAP, TIMEOUT_FAILURE2);
781 SM_ENTER(EAP, IDLE2);
784 if (sm->rxResp && (sm->respId == sm->currentId))
785 SM_ENTER(EAP, AAA_REQUEST);
787 SM_ENTER(EAP, DISCARD2);
790 SM_ENTER(EAP, IDLE2);
792 case EAP_SEND_REQUEST2:
793 SM_ENTER(EAP, IDLE2);
795 case EAP_AAA_REQUEST:
796 SM_ENTER(EAP, AAA_IDLE);
798 case EAP_AAA_RESPONSE:
799 SM_ENTER(EAP, SEND_REQUEST2);
802 if (sm->eap_if.aaaFail)
803 SM_ENTER(EAP, FAILURE2);
804 else if (sm->eap_if.aaaSuccess)
805 SM_ENTER(EAP, SUCCESS2);
806 else if (sm->eap_if.aaaEapReq)
807 SM_ENTER(EAP, AAA_RESPONSE);
808 else if (sm->eap_if.aaaTimeout)
809 SM_ENTER(EAP, TIMEOUT_FAILURE2);
811 case EAP_TIMEOUT_FAILURE2:
821 static int eap_sm_calculateTimeout(struct eap_sm *sm, int retransCount,
822 int eapSRTT, int eapRTTVAR,
829 * EAP method (either internal or through AAA server, provided
830 * timeout hint. Use that as-is as a timeout for retransmitting
831 * the EAP request if no response is received.
833 wpa_printf(MSG_DEBUG, "EAP: retransmit timeout %d seconds "
834 "(from EAP method hint)", methodTimeout);
835 return methodTimeout;
839 * RFC 3748 recommends algorithms described in RFC 2988 for estimation
840 * of the retransmission timeout. This should be implemented once
841 * round-trip time measurements are available. For nowm a simple
842 * backoff mechanism is used instead if there are no EAP method
845 * SRTT = smoothed round-trip time
846 * RTTVAR = round-trip time variation
847 * RTO = retransmission timeout
851 * RFC 2988, 2.1: before RTT measurement, set RTO to 3 seconds for
852 * initial retransmission and then double the RTO to provide back off
853 * per 5.5. Limit the maximum RTO to 20 seconds per RFC 3748, 4.3
857 for (i = 0; i < retransCount; i++) {
865 wpa_printf(MSG_DEBUG, "EAP: retransmit timeout %d seconds "
866 "(from dynamic back off; retransCount=%d)",
873 static void eap_sm_parseEapResp(struct eap_sm *sm, const struct wpabuf *resp)
875 const struct eap_hdr *hdr;
878 /* parse rxResp, respId, respMethod */
881 sm->respMethod = EAP_TYPE_NONE;
882 sm->respVendor = EAP_VENDOR_IETF;
883 sm->respVendorMethod = EAP_TYPE_NONE;
885 if (resp == NULL || wpabuf_len(resp) < sizeof(*hdr)) {
886 wpa_printf(MSG_DEBUG, "EAP: parseEapResp: invalid resp=%p "
888 resp ? (unsigned long) wpabuf_len(resp) : 0);
892 hdr = wpabuf_head(resp);
893 plen = be_to_host16(hdr->length);
894 if (plen > wpabuf_len(resp)) {
895 wpa_printf(MSG_DEBUG, "EAP: Ignored truncated EAP-Packet "
896 "(len=%lu plen=%lu)",
897 (unsigned long) wpabuf_len(resp),
898 (unsigned long) plen);
902 sm->respId = hdr->identifier;
904 if (hdr->code == EAP_CODE_RESPONSE)
907 if (plen > sizeof(*hdr)) {
908 u8 *pos = (u8 *) (hdr + 1);
909 sm->respMethod = *pos++;
910 if (sm->respMethod == EAP_TYPE_EXPANDED) {
911 if (plen < sizeof(*hdr) + 8) {
912 wpa_printf(MSG_DEBUG, "EAP: Ignored truncated "
913 "expanded EAP-Packet (plen=%lu)",
914 (unsigned long) plen);
917 sm->respVendor = WPA_GET_BE24(pos);
919 sm->respVendorMethod = WPA_GET_BE32(pos);
923 wpa_printf(MSG_DEBUG, "EAP: parseEapResp: rxResp=%d respId=%d "
924 "respMethod=%u respVendor=%u respVendorMethod=%u",
925 sm->rxResp, sm->respId, sm->respMethod, sm->respVendor,
926 sm->respVendorMethod);
930 static int eap_sm_getId(const struct wpabuf *data)
932 const struct eap_hdr *hdr;
934 if (data == NULL || wpabuf_len(data) < sizeof(*hdr))
937 hdr = wpabuf_head(data);
938 wpa_printf(MSG_DEBUG, "EAP: getId: id=%d", hdr->identifier);
939 return hdr->identifier;
943 static struct wpabuf * eap_sm_buildSuccess(struct eap_sm *sm, u8 id)
946 struct eap_hdr *resp;
947 wpa_printf(MSG_DEBUG, "EAP: Building EAP-Success (id=%d)", id);
949 msg = wpabuf_alloc(sizeof(*resp));
952 resp = wpabuf_put(msg, sizeof(*resp));
953 resp->code = EAP_CODE_SUCCESS;
954 resp->identifier = id;
955 resp->length = host_to_be16(sizeof(*resp));
961 static struct wpabuf * eap_sm_buildFailure(struct eap_sm *sm, u8 id)
964 struct eap_hdr *resp;
965 wpa_printf(MSG_DEBUG, "EAP: Building EAP-Failure (id=%d)", id);
967 msg = wpabuf_alloc(sizeof(*resp));
970 resp = wpabuf_put(msg, sizeof(*resp));
971 resp->code = EAP_CODE_FAILURE;
972 resp->identifier = id;
973 resp->length = host_to_be16(sizeof(*resp));
979 static int eap_sm_nextId(struct eap_sm *sm, int id)
982 /* RFC 3748 Ch 4.1: recommended to initialize Identifier with a
985 if (id != sm->lastId)
988 return (id + 1) & 0xff;
993 * eap_sm_process_nak - Process EAP-Response/Nak
994 * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
995 * @nak_list: Nak list (allowed methods) from the supplicant
996 * @len: Length of nak_list in bytes
998 * This function is called when EAP-Response/Nak is received from the
999 * supplicant. This can happen for both phase 1 and phase 2 authentications.
1001 void eap_sm_process_nak(struct eap_sm *sm, const u8 *nak_list, size_t len)
1006 if (sm->user == NULL)
1009 wpa_printf(MSG_MSGDUMP, "EAP: processing NAK (current EAP method "
1010 "index %d)", sm->user_eap_method_index);
1012 wpa_hexdump(MSG_MSGDUMP, "EAP: configured methods",
1013 (u8 *) sm->user->methods,
1014 EAP_MAX_METHODS * sizeof(sm->user->methods[0]));
1015 wpa_hexdump(MSG_MSGDUMP, "EAP: list of methods supported by the peer",
1018 i = sm->user_eap_method_index;
1019 while (i < EAP_MAX_METHODS &&
1020 (sm->user->methods[i].vendor != EAP_VENDOR_IETF ||
1021 sm->user->methods[i].method != EAP_TYPE_NONE)) {
1022 if (sm->user->methods[i].vendor != EAP_VENDOR_IETF)
1024 for (j = 0; j < len; j++) {
1025 if (nak_list[j] == sm->user->methods[i].method) {
1037 /* not found - remove from the list */
1038 os_memmove(&sm->user->methods[i], &sm->user->methods[i + 1],
1039 (EAP_MAX_METHODS - i - 1) *
1040 sizeof(sm->user->methods[0]));
1041 sm->user->methods[EAP_MAX_METHODS - 1].vendor =
1043 sm->user->methods[EAP_MAX_METHODS - 1].method = EAP_TYPE_NONE;
1046 wpa_hexdump(MSG_MSGDUMP, "EAP: new list of configured methods",
1047 (u8 *) sm->user->methods, EAP_MAX_METHODS *
1048 sizeof(sm->user->methods[0]));
1052 static void eap_sm_Policy_update(struct eap_sm *sm, const u8 *nak_list,
1055 if (nak_list == NULL || sm == NULL || sm->user == NULL)
1058 if (sm->user->phase2) {
1059 wpa_printf(MSG_DEBUG, "EAP: EAP-Nak received after Phase2 user"
1060 " info was selected - reject");
1061 sm->decision = DECISION_FAILURE;
1065 eap_sm_process_nak(sm, nak_list, len);
1069 static EapType eap_sm_Policy_getNextMethod(struct eap_sm *sm, int *vendor)
1072 int idx = sm->user_eap_method_index;
1074 /* In theory, there should be no problems with starting
1075 * re-authentication with something else than EAP-Request/Identity and
1076 * this does indeed work with wpa_supplicant. However, at least Funk
1077 * Supplicant seemed to ignore re-auth if it skipped
1078 * EAP-Request/Identity.
1079 * Re-auth sets currentId == -1, so that can be used here to select
1080 * whether Identity needs to be requested again. */
1081 if (sm->identity == NULL || sm->currentId == -1) {
1082 *vendor = EAP_VENDOR_IETF;
1083 next = EAP_TYPE_IDENTITY;
1084 sm->update_user = TRUE;
1085 } else if (sm->user && idx < EAP_MAX_METHODS &&
1086 (sm->user->methods[idx].vendor != EAP_VENDOR_IETF ||
1087 sm->user->methods[idx].method != EAP_TYPE_NONE)) {
1088 *vendor = sm->user->methods[idx].vendor;
1089 next = sm->user->methods[idx].method;
1090 sm->user_eap_method_index++;
1092 *vendor = EAP_VENDOR_IETF;
1093 next = EAP_TYPE_NONE;
1095 wpa_printf(MSG_DEBUG, "EAP: getNextMethod: vendor %d type %d",
1101 static int eap_sm_Policy_getDecision(struct eap_sm *sm)
1103 if (!sm->eap_server && sm->identity && !sm->start_reauth) {
1104 wpa_printf(MSG_DEBUG, "EAP: getDecision: -> PASSTHROUGH");
1105 return DECISION_PASSTHROUGH;
1108 if (sm->m && sm->currentMethod != EAP_TYPE_IDENTITY &&
1109 sm->m->isSuccess(sm, sm->eap_method_priv)) {
1110 wpa_printf(MSG_DEBUG, "EAP: getDecision: method succeeded -> "
1112 sm->update_user = TRUE;
1113 return DECISION_SUCCESS;
1116 if (sm->m && sm->m->isDone(sm, sm->eap_method_priv) &&
1117 !sm->m->isSuccess(sm, sm->eap_method_priv)) {
1118 wpa_printf(MSG_DEBUG, "EAP: getDecision: method failed -> "
1120 sm->update_user = TRUE;
1121 return DECISION_FAILURE;
1124 if ((sm->user == NULL || sm->update_user) && sm->identity &&
1125 !sm->start_reauth) {
1127 * Allow Identity method to be started once to allow identity
1128 * selection hint to be sent from the authentication server,
1129 * but prevent a loop of Identity requests by only allowing
1130 * this to happen once.
1133 if (sm->user && sm->currentMethod == EAP_TYPE_IDENTITY &&
1134 sm->user->methods[0].vendor == EAP_VENDOR_IETF &&
1135 sm->user->methods[0].method == EAP_TYPE_IDENTITY)
1137 if (eap_user_get(sm, sm->identity, sm->identity_len, 0) != 0) {
1138 wpa_printf(MSG_DEBUG, "EAP: getDecision: user not "
1139 "found from database -> FAILURE");
1140 return DECISION_FAILURE;
1142 if (id_req && sm->user &&
1143 sm->user->methods[0].vendor == EAP_VENDOR_IETF &&
1144 sm->user->methods[0].method == EAP_TYPE_IDENTITY) {
1145 wpa_printf(MSG_DEBUG, "EAP: getDecision: stop "
1146 "identity request loop -> FAILURE");
1147 sm->update_user = TRUE;
1148 return DECISION_FAILURE;
1150 sm->update_user = FALSE;
1152 sm->start_reauth = FALSE;
1154 if (sm->user && sm->user_eap_method_index < EAP_MAX_METHODS &&
1155 (sm->user->methods[sm->user_eap_method_index].vendor !=
1157 sm->user->methods[sm->user_eap_method_index].method !=
1159 wpa_printf(MSG_DEBUG, "EAP: getDecision: another method "
1160 "available -> CONTINUE");
1161 return DECISION_CONTINUE;
1164 if (sm->identity == NULL || sm->currentId == -1) {
1165 wpa_printf(MSG_DEBUG, "EAP: getDecision: no identity known "
1167 return DECISION_CONTINUE;
1170 wpa_printf(MSG_DEBUG, "EAP: getDecision: no more methods available -> "
1172 return DECISION_FAILURE;
1176 static Boolean eap_sm_Policy_doPickUp(struct eap_sm *sm, EapType method)
1178 return method == EAP_TYPE_IDENTITY ? TRUE : FALSE;
1183 * eap_server_sm_step - Step EAP server state machine
1184 * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
1185 * Returns: 1 if EAP state was changed or 0 if not
1187 * This function advances EAP state machine to a new state to match with the
1188 * current variables. This should be called whenever variables used by the EAP
1189 * state machine have changed.
1191 int eap_server_sm_step(struct eap_sm *sm)
1195 sm->changed = FALSE;
1199 } while (sm->changed);
1204 static void eap_user_free(struct eap_user *user)
1208 os_free(user->password);
1209 user->password = NULL;
1215 * eap_server_sm_init - Allocate and initialize EAP server state machine
1216 * @eapol_ctx: Context data to be used with eapol_cb calls
1217 * @eapol_cb: Pointer to EAPOL callback functions
1218 * @conf: EAP configuration
1219 * Returns: Pointer to the allocated EAP state machine or %NULL on failure
1221 * This function allocates and initializes an EAP state machine.
1223 struct eap_sm * eap_server_sm_init(void *eapol_ctx,
1224 struct eapol_callbacks *eapol_cb,
1225 struct eap_config *conf)
1229 sm = os_zalloc(sizeof(*sm));
1232 sm->eapol_ctx = eapol_ctx;
1233 sm->eapol_cb = eapol_cb;
1234 sm->MaxRetrans = 5; /* RFC 3748: max 3-5 retransmissions suggested */
1235 sm->ssl_ctx = conf->ssl_ctx;
1236 sm->msg_ctx = conf->msg_ctx;
1237 sm->eap_sim_db_priv = conf->eap_sim_db_priv;
1238 sm->backend_auth = conf->backend_auth;
1239 sm->eap_server = conf->eap_server;
1240 if (conf->pac_opaque_encr_key) {
1241 sm->pac_opaque_encr_key = os_malloc(16);
1242 if (sm->pac_opaque_encr_key) {
1243 os_memcpy(sm->pac_opaque_encr_key,
1244 conf->pac_opaque_encr_key, 16);
1247 if (conf->eap_fast_a_id) {
1248 sm->eap_fast_a_id = os_malloc(conf->eap_fast_a_id_len);
1249 if (sm->eap_fast_a_id) {
1250 os_memcpy(sm->eap_fast_a_id, conf->eap_fast_a_id,
1251 conf->eap_fast_a_id_len);
1252 sm->eap_fast_a_id_len = conf->eap_fast_a_id_len;
1255 if (conf->eap_fast_a_id_info)
1256 sm->eap_fast_a_id_info = os_strdup(conf->eap_fast_a_id_info);
1257 sm->eap_fast_prov = conf->eap_fast_prov;
1258 sm->pac_key_lifetime = conf->pac_key_lifetime;
1259 sm->pac_key_refresh_time = conf->pac_key_refresh_time;
1260 sm->eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1261 sm->tnc = conf->tnc;
1262 sm->wps = conf->wps;
1263 if (conf->assoc_wps_ie)
1264 sm->assoc_wps_ie = wpabuf_dup(conf->assoc_wps_ie);
1265 if (conf->assoc_p2p_ie)
1266 sm->assoc_p2p_ie = wpabuf_dup(conf->assoc_p2p_ie);
1267 if (conf->peer_addr)
1268 os_memcpy(sm->peer_addr, conf->peer_addr, ETH_ALEN);
1269 sm->fragment_size = conf->fragment_size;
1270 sm->pwd_group = conf->pwd_group;
1272 wpa_printf(MSG_DEBUG, "EAP: Server state machine created");
1279 * eap_server_sm_deinit - Deinitialize and free an EAP server state machine
1280 * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
1282 * This function deinitializes EAP state machine and frees all allocated
1285 void eap_server_sm_deinit(struct eap_sm *sm)
1289 wpa_printf(MSG_DEBUG, "EAP: Server state machine removed");
1290 if (sm->m && sm->eap_method_priv)
1291 sm->m->reset(sm, sm->eap_method_priv);
1292 wpabuf_free(sm->eap_if.eapReqData);
1293 os_free(sm->eap_if.eapKeyData);
1294 wpabuf_free(sm->lastReqData);
1295 wpabuf_free(sm->eap_if.eapRespData);
1296 os_free(sm->identity);
1297 os_free(sm->pac_opaque_encr_key);
1298 os_free(sm->eap_fast_a_id);
1299 os_free(sm->eap_fast_a_id_info);
1300 wpabuf_free(sm->eap_if.aaaEapReqData);
1301 wpabuf_free(sm->eap_if.aaaEapRespData);
1302 os_free(sm->eap_if.aaaEapKeyData);
1303 eap_user_free(sm->user);
1304 wpabuf_free(sm->assoc_wps_ie);
1305 wpabuf_free(sm->assoc_p2p_ie);
1311 * eap_sm_notify_cached - Notify EAP state machine of cached PMK
1312 * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
1314 * This function is called when PMKSA caching is used to skip EAP
1317 void eap_sm_notify_cached(struct eap_sm *sm)
1322 sm->EAP_state = EAP_SUCCESS;
1327 * eap_sm_pending_cb - EAP state machine callback for a pending EAP request
1328 * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
1330 * This function is called when data for a pending EAP-Request is received.
1332 void eap_sm_pending_cb(struct eap_sm *sm)
1336 wpa_printf(MSG_DEBUG, "EAP: Callback for pending request received");
1337 if (sm->method_pending == METHOD_PENDING_WAIT)
1338 sm->method_pending = METHOD_PENDING_CONT;
1343 * eap_sm_method_pending - Query whether EAP method is waiting for pending data
1344 * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
1345 * Returns: 1 if method is waiting for pending data or 0 if not
1347 int eap_sm_method_pending(struct eap_sm *sm)
1351 return sm->method_pending == METHOD_PENDING_WAIT;
1356 * eap_get_identity - Get the user identity (from EAP-Response/Identity)
1357 * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
1358 * @len: Buffer for returning identity length
1359 * Returns: Pointer to the user identity or %NULL if not available
1361 const u8 * eap_get_identity(struct eap_sm *sm, size_t *len)
1363 *len = sm->identity_len;
1364 return sm->identity;
1369 * eap_get_interface - Get pointer to EAP-EAPOL interface data
1370 * @sm: Pointer to EAP state machine allocated with eap_server_sm_init()
1371 * Returns: Pointer to the EAP-EAPOL interface data
1373 struct eap_eapol_interface * eap_get_interface(struct eap_sm *sm)