1 /*
2 * IEEE 802.1X-2004 Authenticator - EAPOL state machine
3 * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "includes.h"
10
11 #include "common.h"
12 #include "eloop.h"
13 #include "state_machine.h"
14 #include "common/eapol_common.h"
15 #include "eap_common/eap_defs.h"
16 #include "eap_common/eap_common.h"
17 #include "eap_server/eap.h"
18 #include "eapol_auth_sm.h"
19 #include "eapol_auth_sm_i.h"
20
21 #define STATE_MACHINE_DATA struct eapol_state_machine
22 #define STATE_MACHINE_DEBUG_PREFIX "IEEE 802.1X"
23 #define STATE_MACHINE_ADDR sm->addr
24
25 static struct eapol_callbacks eapol_cb;
26
27 /* EAPOL state machines are described in IEEE Std 802.1X-2004, Chap. 8.2 */
28
29 #define setPortAuthorized() \
30 sm->eapol->cb.set_port_authorized(sm->eapol->conf.ctx, sm->sta, 1)
31 #define setPortUnauthorized() \
32 sm->eapol->cb.set_port_authorized(sm->eapol->conf.ctx, sm->sta, 0)
33
34 /* procedures */
35 #define txCannedFail() eapol_auth_tx_canned_eap(sm, 0)
36 #define txCannedSuccess() eapol_auth_tx_canned_eap(sm, 1)
37 #define txReq() eapol_auth_tx_req(sm)
38 #define abortAuth() sm->eapol->cb.abort_auth(sm->eapol->conf.ctx, sm->sta)
39 #define txKey() sm->eapol->cb.tx_key(sm->eapol->conf.ctx, sm->sta)
40 #define processKey() do { } while (0)
41
42
43 static void eapol_sm_step_run(struct eapol_state_machine *sm);
44 static void eapol_sm_step_cb(void *eloop_ctx, void *timeout_ctx);
45 static void eapol_auth_initialize(struct eapol_state_machine *sm);
46
47
eapol_auth_logger(struct eapol_authenticator * eapol,const u8 * addr,eapol_logger_level level,const char * txt)48 static void eapol_auth_logger(struct eapol_authenticator *eapol,
49 const u8 *addr, eapol_logger_level level,
50 const char *txt)
51 {
52 if (eapol->cb.logger == NULL)
53 return;
54 eapol->cb.logger(eapol->conf.ctx, addr, level, txt);
55 }
56
57
eapol_auth_vlogger(struct eapol_authenticator * eapol,const u8 * addr,eapol_logger_level level,const char * fmt,...)58 static void eapol_auth_vlogger(struct eapol_authenticator *eapol,
59 const u8 *addr, eapol_logger_level level,
60 const char *fmt, ...)
61 {
62 char *format;
63 int maxlen;
64 va_list ap;
65
66 if (eapol->cb.logger == NULL)
67 return;
68
69 maxlen = os_strlen(fmt) + 100;
70 format = os_malloc(maxlen);
71 if (!format)
72 return;
73
74 va_start(ap, fmt);
75 vsnprintf(format, maxlen, fmt, ap);
76 va_end(ap);
77
78 eapol_auth_logger(eapol, addr, level, format);
79
80 os_free(format);
81 }
82
83
eapol_auth_tx_canned_eap(struct eapol_state_machine * sm,int success)84 static void eapol_auth_tx_canned_eap(struct eapol_state_machine *sm,
85 int success)
86 {
87 struct eap_hdr eap;
88
89 os_memset(&eap, 0, sizeof(eap));
90
91 eap.code = success ? EAP_CODE_SUCCESS : EAP_CODE_FAILURE;
92 eap.identifier = ++sm->last_eap_id;
93 eap.length = host_to_be16(sizeof(eap));
94
95 eapol_auth_vlogger(sm->eapol, sm->addr, EAPOL_LOGGER_DEBUG,
96 "Sending canned EAP packet %s (identifier %d)",
97 success ? "SUCCESS" : "FAILURE", eap.identifier);
98 sm->eapol->cb.eapol_send(sm->eapol->conf.ctx, sm->sta,
99 IEEE802_1X_TYPE_EAP_PACKET,
100 (u8 *) &eap, sizeof(eap));
101 sm->dot1xAuthEapolFramesTx++;
102 }
103
104
eapol_auth_tx_req(struct eapol_state_machine * sm)105 static void eapol_auth_tx_req(struct eapol_state_machine *sm)
106 {
107 if (sm->eap_if->eapReqData == NULL ||
108 wpabuf_len(sm->eap_if->eapReqData) < sizeof(struct eap_hdr)) {
109 eapol_auth_logger(sm->eapol, sm->addr,
110 EAPOL_LOGGER_DEBUG,
111 "TxReq called, but there is no EAP request "
112 "from authentication server");
113 return;
114 }
115
116 if (sm->flags & EAPOL_SM_WAIT_START) {
117 wpa_printf(MSG_DEBUG, "EAPOL: Drop EAPOL TX to " MACSTR
118 " while waiting for EAPOL-Start",
119 MAC2STR(sm->addr));
120 return;
121 }
122
123 sm->last_eap_id = eap_get_id(sm->eap_if->eapReqData);
124 eapol_auth_vlogger(sm->eapol, sm->addr, EAPOL_LOGGER_DEBUG,
125 "Sending EAP Packet (identifier %d)",
126 sm->last_eap_id);
127 sm->eapol->cb.eapol_send(sm->eapol->conf.ctx, sm->sta,
128 IEEE802_1X_TYPE_EAP_PACKET,
129 wpabuf_head(sm->eap_if->eapReqData),
130 wpabuf_len(sm->eap_if->eapReqData));
131 sm->dot1xAuthEapolFramesTx++;
132 if (eap_get_type(sm->eap_if->eapReqData) == EAP_TYPE_IDENTITY)
133 sm->dot1xAuthEapolReqIdFramesTx++;
134 else
135 sm->dot1xAuthEapolReqFramesTx++;
136 }
137
138
139 /**
140 * eapol_port_timers_tick - Port Timers state machine
141 * @eloop_ctx: struct eapol_state_machine *
142 * @timeout_ctx: Not used
143 *
144 * This statemachine is implemented as a function that will be called
145 * once a second as a registered event loop timeout.
146 */
eapol_port_timers_tick(void * eloop_ctx,void * timeout_ctx)147 static void eapol_port_timers_tick(void *eloop_ctx, void *timeout_ctx)
148 {
149 struct eapol_state_machine *state = timeout_ctx;
150
151 if (state->aWhile > 0) {
152 state->aWhile--;
153 if (state->aWhile == 0) {
154 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR
155 " - aWhile --> 0",
156 MAC2STR(state->addr));
157 }
158 }
159
160 if (state->quietWhile > 0) {
161 state->quietWhile--;
162 if (state->quietWhile == 0) {
163 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR
164 " - quietWhile --> 0",
165 MAC2STR(state->addr));
166 }
167 }
168
169 if (state->reAuthWhen > 0) {
170 state->reAuthWhen--;
171 if (state->reAuthWhen == 0) {
172 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR
173 " - reAuthWhen --> 0",
174 MAC2STR(state->addr));
175 }
176 }
177
178 if (state->eap_if->retransWhile > 0) {
179 state->eap_if->retransWhile--;
180 if (state->eap_if->retransWhile == 0) {
181 wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR
182 " - (EAP) retransWhile --> 0",
183 MAC2STR(state->addr));
184 }
185 }
186
187 eapol_sm_step_run(state);
188
189 eloop_register_timeout(1, 0, eapol_port_timers_tick, eloop_ctx, state);
190 }
191
192
193
194 /* Authenticator PAE state machine */
195
SM_STATE(AUTH_PAE,INITIALIZE)196 SM_STATE(AUTH_PAE, INITIALIZE)
197 {
198 SM_ENTRY_MA(AUTH_PAE, INITIALIZE, auth_pae);
199 sm->portMode = Auto;
200 }
201
202
SM_STATE(AUTH_PAE,DISCONNECTED)203 SM_STATE(AUTH_PAE, DISCONNECTED)
204 {
205 int from_initialize = sm->auth_pae_state == AUTH_PAE_INITIALIZE;
206
207 if (sm->eapolLogoff) {
208 if (sm->auth_pae_state == AUTH_PAE_CONNECTING)
209 sm->authEapLogoffsWhileConnecting++;
210 else if (sm->auth_pae_state == AUTH_PAE_AUTHENTICATED)
211 sm->authAuthEapLogoffWhileAuthenticated++;
212 }
213
214 SM_ENTRY_MA(AUTH_PAE, DISCONNECTED, auth_pae);
215
216 sm->authPortStatus = Unauthorized;
217 setPortUnauthorized();
218 sm->reAuthCount = 0;
219 sm->eapolLogoff = FALSE;
220 if (!from_initialize) {
221 sm->eapol->cb.finished(sm->eapol->conf.ctx, sm->sta, 0,
222 sm->flags & EAPOL_SM_PREAUTH);
223 }
224 }
225
226
SM_STATE(AUTH_PAE,RESTART)227 SM_STATE(AUTH_PAE, RESTART)
228 {
229 if (sm->auth_pae_state == AUTH_PAE_AUTHENTICATED) {
230 if (sm->reAuthenticate)
231 sm->authAuthReauthsWhileAuthenticated++;
232 if (sm->eapolStart)
233 sm->authAuthEapStartsWhileAuthenticated++;
234 if (sm->eapolLogoff)
235 sm->authAuthEapLogoffWhileAuthenticated++;
236 }
237
238 SM_ENTRY_MA(AUTH_PAE, RESTART, auth_pae);
239
240 sm->eap_if->eapRestart = TRUE;
241 }
242
243
SM_STATE(AUTH_PAE,CONNECTING)244 SM_STATE(AUTH_PAE, CONNECTING)
245 {
246 if (sm->auth_pae_state != AUTH_PAE_CONNECTING)
247 sm->authEntersConnecting++;
248
249 SM_ENTRY_MA(AUTH_PAE, CONNECTING, auth_pae);
250
251 sm->reAuthenticate = FALSE;
252 sm->reAuthCount++;
253 }
254
255
SM_STATE(AUTH_PAE,HELD)256 SM_STATE(AUTH_PAE, HELD)
257 {
258 if (sm->auth_pae_state == AUTH_PAE_AUTHENTICATING && sm->authFail)
259 sm->authAuthFailWhileAuthenticating++;
260
261 SM_ENTRY_MA(AUTH_PAE, HELD, auth_pae);
262
263 sm->authPortStatus = Unauthorized;
264 setPortUnauthorized();
265 sm->quietWhile = sm->quietPeriod;
266 sm->eapolLogoff = FALSE;
267
268 eapol_auth_vlogger(sm->eapol, sm->addr, EAPOL_LOGGER_WARNING,
269 "authentication failed - EAP type: %d (%s)",
270 sm->eap_type_authsrv,
271 eap_server_get_name(0, sm->eap_type_authsrv));
272 if (sm->eap_type_authsrv != sm->eap_type_supp) {
273 eapol_auth_vlogger(sm->eapol, sm->addr, EAPOL_LOGGER_INFO,
274 "Supplicant used different EAP type: "
275 "%d (%s)", sm->eap_type_supp,
276 eap_server_get_name(0, sm->eap_type_supp));
277 }
278 sm->eapol->cb.finished(sm->eapol->conf.ctx, sm->sta, 0,
279 sm->flags & EAPOL_SM_PREAUTH);
280 }
281
282
SM_STATE(AUTH_PAE,AUTHENTICATED)283 SM_STATE(AUTH_PAE, AUTHENTICATED)
284 {
285 char *extra = "";
286
287 if (sm->auth_pae_state == AUTH_PAE_AUTHENTICATING && sm->authSuccess)
288 sm->authAuthSuccessesWhileAuthenticating++;
289
290 SM_ENTRY_MA(AUTH_PAE, AUTHENTICATED, auth_pae);
291
292 sm->authPortStatus = Authorized;
293 setPortAuthorized();
294 sm->reAuthCount = 0;
295 if (sm->flags & EAPOL_SM_PREAUTH)
296 extra = " (pre-authentication)";
297 else if (sm->flags & EAPOL_SM_FROM_PMKSA_CACHE)
298 extra = " (PMKSA cache)";
299 eapol_auth_vlogger(sm->eapol, sm->addr, EAPOL_LOGGER_INFO,
300 "authenticated - EAP type: %d (%s)%s",
301 sm->eap_type_authsrv,
302 eap_server_get_name(0, sm->eap_type_authsrv),
303 extra);
304 sm->eapol->cb.finished(sm->eapol->conf.ctx, sm->sta, 1,
305 sm->flags & EAPOL_SM_PREAUTH);
306 }
307
308
SM_STATE(AUTH_PAE,AUTHENTICATING)309 SM_STATE(AUTH_PAE, AUTHENTICATING)
310 {
311 SM_ENTRY_MA(AUTH_PAE, AUTHENTICATING, auth_pae);
312
313 sm->eapolStart = FALSE;
314 sm->authSuccess = FALSE;
315 sm->authFail = FALSE;
316 sm->authTimeout = FALSE;
317 sm->authStart = TRUE;
318 sm->keyRun = FALSE;
319 sm->keyDone = FALSE;
320 }
321
322
SM_STATE(AUTH_PAE,ABORTING)323 SM_STATE(AUTH_PAE, ABORTING)
324 {
325 if (sm->auth_pae_state == AUTH_PAE_AUTHENTICATING) {
326 if (sm->authTimeout)
327 sm->authAuthTimeoutsWhileAuthenticating++;
328 if (sm->eapolStart)
329 sm->authAuthEapStartsWhileAuthenticating++;
330 if (sm->eapolLogoff)
331 sm->authAuthEapLogoffWhileAuthenticating++;
332 }
333
334 SM_ENTRY_MA(AUTH_PAE, ABORTING, auth_pae);
335
336 sm->authAbort = TRUE;
337 sm->keyRun = FALSE;
338 sm->keyDone = FALSE;
339 }
340
341
SM_STATE(AUTH_PAE,FORCE_AUTH)342 SM_STATE(AUTH_PAE, FORCE_AUTH)
343 {
344 SM_ENTRY_MA(AUTH_PAE, FORCE_AUTH, auth_pae);
345
346 sm->authPortStatus = Authorized;
347 setPortAuthorized();
348 sm->portMode = ForceAuthorized;
349 sm->eapolStart = FALSE;
350 txCannedSuccess();
351 }
352
353
SM_STATE(AUTH_PAE,FORCE_UNAUTH)354 SM_STATE(AUTH_PAE, FORCE_UNAUTH)
355 {
356 SM_ENTRY_MA(AUTH_PAE, FORCE_UNAUTH, auth_pae);
357
358 sm->authPortStatus = Unauthorized;
359 setPortUnauthorized();
360 sm->portMode = ForceUnauthorized;
361 sm->eapolStart = FALSE;
362 txCannedFail();
363 }
364
365
SM_STEP(AUTH_PAE)366 SM_STEP(AUTH_PAE)
367 {
368 if ((sm->portControl == Auto && sm->portMode != sm->portControl) ||
369 sm->initialize || !sm->eap_if->portEnabled)
370 SM_ENTER_GLOBAL(AUTH_PAE, INITIALIZE);
371 else if (sm->portControl == ForceAuthorized &&
372 sm->portMode != sm->portControl &&
373 !(sm->initialize || !sm->eap_if->portEnabled))
374 SM_ENTER_GLOBAL(AUTH_PAE, FORCE_AUTH);
375 else if (sm->portControl == ForceUnauthorized &&
376 sm->portMode != sm->portControl &&
377 !(sm->initialize || !sm->eap_if->portEnabled))
378 SM_ENTER_GLOBAL(AUTH_PAE, FORCE_UNAUTH);
379 else {
380 switch (sm->auth_pae_state) {
381 case AUTH_PAE_INITIALIZE:
382 SM_ENTER(AUTH_PAE, DISCONNECTED);
383 break;
384 case AUTH_PAE_DISCONNECTED:
385 SM_ENTER(AUTH_PAE, RESTART);
386 break;
387 case AUTH_PAE_RESTART:
388 if (!sm->eap_if->eapRestart)
389 SM_ENTER(AUTH_PAE, CONNECTING);
390 break;
391 case AUTH_PAE_HELD:
392 if (sm->quietWhile == 0)
393 SM_ENTER(AUTH_PAE, RESTART);
394 break;
395 case AUTH_PAE_CONNECTING:
396 if (sm->eapolLogoff || sm->reAuthCount > sm->reAuthMax)
397 SM_ENTER(AUTH_PAE, DISCONNECTED);
398 else if ((sm->eap_if->eapReq &&
399 sm->reAuthCount <= sm->reAuthMax) ||
400 sm->eap_if->eapSuccess || sm->eap_if->eapFail)
401 SM_ENTER(AUTH_PAE, AUTHENTICATING);
402 break;
403 case AUTH_PAE_AUTHENTICATED:
404 if (sm->eapolStart || sm->reAuthenticate)
405 SM_ENTER(AUTH_PAE, RESTART);
406 else if (sm->eapolLogoff || !sm->portValid)
407 SM_ENTER(AUTH_PAE, DISCONNECTED);
408 break;
409 case AUTH_PAE_AUTHENTICATING:
410 if (sm->authSuccess && sm->portValid)
411 SM_ENTER(AUTH_PAE, AUTHENTICATED);
412 else if (sm->authFail ||
413 (sm->keyDone && !sm->portValid))
414 SM_ENTER(AUTH_PAE, HELD);
415 else if (sm->eapolStart || sm->eapolLogoff ||
416 sm->authTimeout)
417 SM_ENTER(AUTH_PAE, ABORTING);
418 break;
419 case AUTH_PAE_ABORTING:
420 if (sm->eapolLogoff && !sm->authAbort)
421 SM_ENTER(AUTH_PAE, DISCONNECTED);
422 else if (!sm->eapolLogoff && !sm->authAbort)
423 SM_ENTER(AUTH_PAE, RESTART);
424 break;
425 case AUTH_PAE_FORCE_AUTH:
426 if (sm->eapolStart)
427 SM_ENTER(AUTH_PAE, FORCE_AUTH);
428 break;
429 case AUTH_PAE_FORCE_UNAUTH:
430 if (sm->eapolStart)
431 SM_ENTER(AUTH_PAE, FORCE_UNAUTH);
432 break;
433 }
434 }
435 }
436
437
438
439 /* Backend Authentication state machine */
440
SM_STATE(BE_AUTH,INITIALIZE)441 SM_STATE(BE_AUTH, INITIALIZE)
442 {
443 SM_ENTRY_MA(BE_AUTH, INITIALIZE, be_auth);
444
445 abortAuth();
446 sm->eap_if->eapNoReq = FALSE;
447 sm->authAbort = FALSE;
448 }
449
450
SM_STATE(BE_AUTH,REQUEST)451 SM_STATE(BE_AUTH, REQUEST)
452 {
453 SM_ENTRY_MA(BE_AUTH, REQUEST, be_auth);
454
455 txReq();
456 sm->eap_if->eapReq = FALSE;
457 sm->backendOtherRequestsToSupplicant++;
458
459 /*
460 * Clearing eapolEap here is not specified in IEEE Std 802.1X-2004, but
461 * it looks like this would be logical thing to do there since the old
462 * EAP response would not be valid anymore after the new EAP request
463 * was sent out.
464 *
465 * A race condition has been reported, in which hostapd ended up
466 * sending out EAP-Response/Identity as a response to the first
467 * EAP-Request from the main EAP method. This can be avoided by
468 * clearing eapolEap here.
469 */
470 sm->eapolEap = FALSE;
471 }
472
473
SM_STATE(BE_AUTH,RESPONSE)474 SM_STATE(BE_AUTH, RESPONSE)
475 {
476 SM_ENTRY_MA(BE_AUTH, RESPONSE, be_auth);
477
478 sm->authTimeout = FALSE;
479 sm->eapolEap = FALSE;
480 sm->eap_if->eapNoReq = FALSE;
481 sm->aWhile = sm->serverTimeout;
482 sm->eap_if->eapResp = TRUE;
483 /* sendRespToServer(); */
484 sm->backendResponses++;
485 }
486
487
SM_STATE(BE_AUTH,SUCCESS)488 SM_STATE(BE_AUTH, SUCCESS)
489 {
490 SM_ENTRY_MA(BE_AUTH, SUCCESS, be_auth);
491
492 txReq();
493 sm->authSuccess = TRUE;
494 sm->keyRun = TRUE;
495 }
496
497
SM_STATE(BE_AUTH,FAIL)498 SM_STATE(BE_AUTH, FAIL)
499 {
500 SM_ENTRY_MA(BE_AUTH, FAIL, be_auth);
501
502 txReq();
503 sm->authFail = TRUE;
504 }
505
506
SM_STATE(BE_AUTH,TIMEOUT)507 SM_STATE(BE_AUTH, TIMEOUT)
508 {
509 SM_ENTRY_MA(BE_AUTH, TIMEOUT, be_auth);
510
511 sm->authTimeout = TRUE;
512 }
513
514
SM_STATE(BE_AUTH,IDLE)515 SM_STATE(BE_AUTH, IDLE)
516 {
517 SM_ENTRY_MA(BE_AUTH, IDLE, be_auth);
518
519 sm->authStart = FALSE;
520 }
521
522
SM_STATE(BE_AUTH,IGNORE)523 SM_STATE(BE_AUTH, IGNORE)
524 {
525 SM_ENTRY_MA(BE_AUTH, IGNORE, be_auth);
526
527 sm->eap_if->eapNoReq = FALSE;
528 }
529
530
SM_STEP(BE_AUTH)531 SM_STEP(BE_AUTH)
532 {
533 if (sm->portControl != Auto || sm->initialize || sm->authAbort) {
534 SM_ENTER_GLOBAL(BE_AUTH, INITIALIZE);
535 return;
536 }
537
538 switch (sm->be_auth_state) {
539 case BE_AUTH_INITIALIZE:
540 SM_ENTER(BE_AUTH, IDLE);
541 break;
542 case BE_AUTH_REQUEST:
543 if (sm->eapolEap)
544 SM_ENTER(BE_AUTH, RESPONSE);
545 else if (sm->eap_if->eapReq)
546 SM_ENTER(BE_AUTH, REQUEST);
547 else if (sm->eap_if->eapTimeout)
548 SM_ENTER(BE_AUTH, TIMEOUT);
549 break;
550 case BE_AUTH_RESPONSE:
551 if (sm->eap_if->eapNoReq)
552 SM_ENTER(BE_AUTH, IGNORE);
553 if (sm->eap_if->eapReq) {
554 sm->backendAccessChallenges++;
555 SM_ENTER(BE_AUTH, REQUEST);
556 } else if (sm->aWhile == 0)
557 SM_ENTER(BE_AUTH, TIMEOUT);
558 else if (sm->eap_if->eapFail) {
559 sm->backendAuthFails++;
560 SM_ENTER(BE_AUTH, FAIL);
561 } else if (sm->eap_if->eapSuccess) {
562 sm->backendAuthSuccesses++;
563 SM_ENTER(BE_AUTH, SUCCESS);
564 }
565 break;
566 case BE_AUTH_SUCCESS:
567 SM_ENTER(BE_AUTH, IDLE);
568 break;
569 case BE_AUTH_FAIL:
570 SM_ENTER(BE_AUTH, IDLE);
571 break;
572 case BE_AUTH_TIMEOUT:
573 SM_ENTER(BE_AUTH, IDLE);
574 break;
575 case BE_AUTH_IDLE:
576 if (sm->eap_if->eapFail && sm->authStart)
577 SM_ENTER(BE_AUTH, FAIL);
578 else if (sm->eap_if->eapReq && sm->authStart)
579 SM_ENTER(BE_AUTH, REQUEST);
580 else if (sm->eap_if->eapSuccess && sm->authStart)
581 SM_ENTER(BE_AUTH, SUCCESS);
582 break;
583 case BE_AUTH_IGNORE:
584 if (sm->eapolEap)
585 SM_ENTER(BE_AUTH, RESPONSE);
586 else if (sm->eap_if->eapReq)
587 SM_ENTER(BE_AUTH, REQUEST);
588 else if (sm->eap_if->eapTimeout)
589 SM_ENTER(BE_AUTH, TIMEOUT);
590 break;
591 }
592 }
593
594
595
596 /* Reauthentication Timer state machine */
597
SM_STATE(REAUTH_TIMER,INITIALIZE)598 SM_STATE(REAUTH_TIMER, INITIALIZE)
599 {
600 SM_ENTRY_MA(REAUTH_TIMER, INITIALIZE, reauth_timer);
601
602 sm->reAuthWhen = sm->reAuthPeriod;
603 }
604
605
SM_STATE(REAUTH_TIMER,REAUTHENTICATE)606 SM_STATE(REAUTH_TIMER, REAUTHENTICATE)
607 {
608 SM_ENTRY_MA(REAUTH_TIMER, REAUTHENTICATE, reauth_timer);
609
610 sm->reAuthenticate = TRUE;
611 sm->eapol->cb.eapol_event(sm->eapol->conf.ctx, sm->sta,
612 EAPOL_AUTH_REAUTHENTICATE);
613 }
614
615
SM_STEP(REAUTH_TIMER)616 SM_STEP(REAUTH_TIMER)
617 {
618 if (sm->portControl != Auto || sm->initialize ||
619 sm->authPortStatus == Unauthorized || !sm->reAuthEnabled) {
620 SM_ENTER_GLOBAL(REAUTH_TIMER, INITIALIZE);
621 return;
622 }
623
624 switch (sm->reauth_timer_state) {
625 case REAUTH_TIMER_INITIALIZE:
626 if (sm->reAuthWhen == 0)
627 SM_ENTER(REAUTH_TIMER, REAUTHENTICATE);
628 break;
629 case REAUTH_TIMER_REAUTHENTICATE:
630 SM_ENTER(REAUTH_TIMER, INITIALIZE);
631 break;
632 }
633 }
634
635
636
637 /* Authenticator Key Transmit state machine */
638
SM_STATE(AUTH_KEY_TX,NO_KEY_TRANSMIT)639 SM_STATE(AUTH_KEY_TX, NO_KEY_TRANSMIT)
640 {
641 SM_ENTRY_MA(AUTH_KEY_TX, NO_KEY_TRANSMIT, auth_key_tx);
642 }
643
644
SM_STATE(AUTH_KEY_TX,KEY_TRANSMIT)645 SM_STATE(AUTH_KEY_TX, KEY_TRANSMIT)
646 {
647 SM_ENTRY_MA(AUTH_KEY_TX, KEY_TRANSMIT, auth_key_tx);
648
649 txKey();
650 sm->eap_if->eapKeyAvailable = FALSE;
651 sm->keyDone = TRUE;
652 }
653
654
SM_STEP(AUTH_KEY_TX)655 SM_STEP(AUTH_KEY_TX)
656 {
657 if (sm->initialize || sm->portControl != Auto) {
658 SM_ENTER_GLOBAL(AUTH_KEY_TX, NO_KEY_TRANSMIT);
659 return;
660 }
661
662 switch (sm->auth_key_tx_state) {
663 case AUTH_KEY_TX_NO_KEY_TRANSMIT:
664 if (sm->keyTxEnabled && sm->eap_if->eapKeyAvailable &&
665 sm->keyRun && !(sm->flags & EAPOL_SM_USES_WPA))
666 SM_ENTER(AUTH_KEY_TX, KEY_TRANSMIT);
667 break;
668 case AUTH_KEY_TX_KEY_TRANSMIT:
669 if (!sm->keyTxEnabled || !sm->keyRun)
670 SM_ENTER(AUTH_KEY_TX, NO_KEY_TRANSMIT);
671 else if (sm->eap_if->eapKeyAvailable)
672 SM_ENTER(AUTH_KEY_TX, KEY_TRANSMIT);
673 break;
674 }
675 }
676
677
678
679 /* Key Receive state machine */
680
SM_STATE(KEY_RX,NO_KEY_RECEIVE)681 SM_STATE(KEY_RX, NO_KEY_RECEIVE)
682 {
683 SM_ENTRY_MA(KEY_RX, NO_KEY_RECEIVE, key_rx);
684 }
685
686
SM_STATE(KEY_RX,KEY_RECEIVE)687 SM_STATE(KEY_RX, KEY_RECEIVE)
688 {
689 SM_ENTRY_MA(KEY_RX, KEY_RECEIVE, key_rx);
690
691 processKey();
692 sm->rxKey = FALSE;
693 }
694
695
SM_STEP(KEY_RX)696 SM_STEP(KEY_RX)
697 {
698 if (sm->initialize || !sm->eap_if->portEnabled) {
699 SM_ENTER_GLOBAL(KEY_RX, NO_KEY_RECEIVE);
700 return;
701 }
702
703 switch (sm->key_rx_state) {
704 case KEY_RX_NO_KEY_RECEIVE:
705 if (sm->rxKey)
706 SM_ENTER(KEY_RX, KEY_RECEIVE);
707 break;
708 case KEY_RX_KEY_RECEIVE:
709 if (sm->rxKey)
710 SM_ENTER(KEY_RX, KEY_RECEIVE);
711 break;
712 }
713 }
714
715
716
717 /* Controlled Directions state machine */
718
SM_STATE(CTRL_DIR,FORCE_BOTH)719 SM_STATE(CTRL_DIR, FORCE_BOTH)
720 {
721 SM_ENTRY_MA(CTRL_DIR, FORCE_BOTH, ctrl_dir);
722 sm->operControlledDirections = Both;
723 }
724
725
SM_STATE(CTRL_DIR,IN_OR_BOTH)726 SM_STATE(CTRL_DIR, IN_OR_BOTH)
727 {
728 SM_ENTRY_MA(CTRL_DIR, IN_OR_BOTH, ctrl_dir);
729 sm->operControlledDirections = sm->adminControlledDirections;
730 }
731
732
SM_STEP(CTRL_DIR)733 SM_STEP(CTRL_DIR)
734 {
735 if (sm->initialize) {
736 SM_ENTER_GLOBAL(CTRL_DIR, IN_OR_BOTH);
737 return;
738 }
739
740 switch (sm->ctrl_dir_state) {
741 case CTRL_DIR_FORCE_BOTH:
742 if (sm->eap_if->portEnabled && sm->operEdge)
743 SM_ENTER(CTRL_DIR, IN_OR_BOTH);
744 break;
745 case CTRL_DIR_IN_OR_BOTH:
746 if (sm->operControlledDirections !=
747 sm->adminControlledDirections)
748 SM_ENTER(CTRL_DIR, IN_OR_BOTH);
749 if (!sm->eap_if->portEnabled || !sm->operEdge)
750 SM_ENTER(CTRL_DIR, FORCE_BOTH);
751 break;
752 }
753 }
754
755
756
757 struct eapol_state_machine *
eapol_auth_alloc(struct eapol_authenticator * eapol,const u8 * addr,int flags,const struct wpabuf * assoc_wps_ie,const struct wpabuf * assoc_p2p_ie,void * sta_ctx)758 eapol_auth_alloc(struct eapol_authenticator *eapol, const u8 *addr,
759 int flags, const struct wpabuf *assoc_wps_ie,
760 const struct wpabuf *assoc_p2p_ie, void *sta_ctx)
761 {
762 struct eapol_state_machine *sm;
763 struct eap_config eap_conf;
764
765 if (eapol == NULL)
766 return NULL;
767
768 sm = os_zalloc(sizeof(*sm));
769 if (sm == NULL) {
770 wpa_printf(MSG_DEBUG, "IEEE 802.1X state machine allocation "
771 "failed");
772 return NULL;
773 }
774 sm->radius_identifier = -1;
775 os_memcpy(sm->addr, addr, ETH_ALEN);
776 sm->flags = flags;
777
778 sm->eapol = eapol;
779 sm->sta = sta_ctx;
780
781 /* Set default values for state machine constants */
782 sm->auth_pae_state = AUTH_PAE_INITIALIZE;
783 sm->quietPeriod = AUTH_PAE_DEFAULT_quietPeriod;
784 sm->reAuthMax = AUTH_PAE_DEFAULT_reAuthMax;
785
786 sm->be_auth_state = BE_AUTH_INITIALIZE;
787 sm->serverTimeout = BE_AUTH_DEFAULT_serverTimeout;
788
789 sm->reauth_timer_state = REAUTH_TIMER_INITIALIZE;
790 sm->reAuthPeriod = eapol->conf.eap_reauth_period;
791 sm->reAuthEnabled = eapol->conf.eap_reauth_period > 0 ? TRUE : FALSE;
792
793 sm->auth_key_tx_state = AUTH_KEY_TX_NO_KEY_TRANSMIT;
794
795 sm->key_rx_state = KEY_RX_NO_KEY_RECEIVE;
796
797 sm->ctrl_dir_state = CTRL_DIR_IN_OR_BOTH;
798
799 sm->portControl = Auto;
800
801 if (!eapol->conf.wpa &&
802 (eapol->default_wep_key || eapol->conf.individual_wep_key_len > 0))
803 sm->keyTxEnabled = TRUE;
804 else
805 sm->keyTxEnabled = FALSE;
806 if (eapol->conf.wpa)
807 sm->portValid = FALSE;
808 else
809 sm->portValid = TRUE;
810
811 os_memset(&eap_conf, 0, sizeof(eap_conf));
812 eap_conf.eap_server = eapol->conf.eap_server;
813 eap_conf.ssl_ctx = eapol->conf.ssl_ctx;
814 eap_conf.msg_ctx = eapol->conf.msg_ctx;
815 eap_conf.eap_sim_db_priv = eapol->conf.eap_sim_db_priv;
816 eap_conf.pac_opaque_encr_key = eapol->conf.pac_opaque_encr_key;
817 eap_conf.eap_fast_a_id = eapol->conf.eap_fast_a_id;
818 eap_conf.eap_fast_a_id_len = eapol->conf.eap_fast_a_id_len;
819 eap_conf.eap_fast_a_id_info = eapol->conf.eap_fast_a_id_info;
820 eap_conf.eap_fast_prov = eapol->conf.eap_fast_prov;
821 eap_conf.pac_key_lifetime = eapol->conf.pac_key_lifetime;
822 eap_conf.pac_key_refresh_time = eapol->conf.pac_key_refresh_time;
823 eap_conf.eap_sim_aka_result_ind = eapol->conf.eap_sim_aka_result_ind;
824 eap_conf.tnc = eapol->conf.tnc;
825 eap_conf.wps = eapol->conf.wps;
826 eap_conf.assoc_wps_ie = assoc_wps_ie;
827 eap_conf.assoc_p2p_ie = assoc_p2p_ie;
828 eap_conf.peer_addr = addr;
829 eap_conf.fragment_size = eapol->conf.fragment_size;
830 eap_conf.pwd_group = eapol->conf.pwd_group;
831 eap_conf.pbc_in_m1 = eapol->conf.pbc_in_m1;
832 sm->eap = eap_server_sm_init(sm, &eapol_cb, &eap_conf);
833 if (sm->eap == NULL) {
834 eapol_auth_free(sm);
835 return NULL;
836 }
837 sm->eap_if = eap_get_interface(sm->eap);
838
839 eapol_auth_initialize(sm);
840
841 return sm;
842 }
843
844
eapol_auth_free(struct eapol_state_machine * sm)845 void eapol_auth_free(struct eapol_state_machine *sm)
846 {
847 if (sm == NULL)
848 return;
849
850 eloop_cancel_timeout(eapol_port_timers_tick, NULL, sm);
851 eloop_cancel_timeout(eapol_sm_step_cb, sm, NULL);
852 if (sm->eap)
853 eap_server_sm_deinit(sm->eap);
854 os_free(sm);
855 }
856
857
eapol_sm_sta_entry_alive(struct eapol_authenticator * eapol,const u8 * addr)858 static int eapol_sm_sta_entry_alive(struct eapol_authenticator *eapol,
859 const u8 *addr)
860 {
861 return eapol->cb.sta_entry_alive(eapol->conf.ctx, addr);
862 }
863
864
eapol_sm_step_run(struct eapol_state_machine * sm)865 static void eapol_sm_step_run(struct eapol_state_machine *sm)
866 {
867 struct eapol_authenticator *eapol = sm->eapol;
868 u8 addr[ETH_ALEN];
869 unsigned int prev_auth_pae, prev_be_auth, prev_reauth_timer,
870 prev_auth_key_tx, prev_key_rx, prev_ctrl_dir;
871 int max_steps = 100;
872
873 os_memcpy(addr, sm->addr, ETH_ALEN);
874
875 /*
876 * Allow EAPOL state machines to run as long as there are state
877 * changes, but exit and return here through event loop if more than
878 * 100 steps is needed as a precaution against infinite loops inside
879 * eloop callback.
880 */
881 restart:
882 prev_auth_pae = sm->auth_pae_state;
883 prev_be_auth = sm->be_auth_state;
884 prev_reauth_timer = sm->reauth_timer_state;
885 prev_auth_key_tx = sm->auth_key_tx_state;
886 prev_key_rx = sm->key_rx_state;
887 prev_ctrl_dir = sm->ctrl_dir_state;
888
889 SM_STEP_RUN(AUTH_PAE);
890 if (sm->initializing || eapol_sm_sta_entry_alive(eapol, addr))
891 SM_STEP_RUN(BE_AUTH);
892 if (sm->initializing || eapol_sm_sta_entry_alive(eapol, addr))
893 SM_STEP_RUN(REAUTH_TIMER);
894 if (sm->initializing || eapol_sm_sta_entry_alive(eapol, addr))
895 SM_STEP_RUN(AUTH_KEY_TX);
896 if (sm->initializing || eapol_sm_sta_entry_alive(eapol, addr))
897 SM_STEP_RUN(KEY_RX);
898 if (sm->initializing || eapol_sm_sta_entry_alive(eapol, addr))
899 SM_STEP_RUN(CTRL_DIR);
900
901 if (prev_auth_pae != sm->auth_pae_state ||
902 prev_be_auth != sm->be_auth_state ||
903 prev_reauth_timer != sm->reauth_timer_state ||
904 prev_auth_key_tx != sm->auth_key_tx_state ||
905 prev_key_rx != sm->key_rx_state ||
906 prev_ctrl_dir != sm->ctrl_dir_state) {
907 if (--max_steps > 0)
908 goto restart;
909 /* Re-run from eloop timeout */
910 eapol_auth_step(sm);
911 return;
912 }
913
914 if (eapol_sm_sta_entry_alive(eapol, addr) && sm->eap) {
915 if (eap_server_sm_step(sm->eap)) {
916 if (--max_steps > 0)
917 goto restart;
918 /* Re-run from eloop timeout */
919 eapol_auth_step(sm);
920 return;
921 }
922
923 /* TODO: find a better location for this */
924 if (sm->eap_if->aaaEapResp) {
925 sm->eap_if->aaaEapResp = FALSE;
926 if (sm->eap_if->aaaEapRespData == NULL) {
927 wpa_printf(MSG_DEBUG, "EAPOL: aaaEapResp set, "
928 "but no aaaEapRespData available");
929 return;
930 }
931 sm->eapol->cb.aaa_send(
932 sm->eapol->conf.ctx, sm->sta,
933 wpabuf_head(sm->eap_if->aaaEapRespData),
934 wpabuf_len(sm->eap_if->aaaEapRespData));
935 }
936 }
937
938 if (eapol_sm_sta_entry_alive(eapol, addr))
939 sm->eapol->cb.eapol_event(sm->eapol->conf.ctx, sm->sta,
940 EAPOL_AUTH_SM_CHANGE);
941 }
942
943
eapol_sm_step_cb(void * eloop_ctx,void * timeout_ctx)944 static void eapol_sm_step_cb(void *eloop_ctx, void *timeout_ctx)
945 {
946 struct eapol_state_machine *sm = eloop_ctx;
947 eapol_sm_step_run(sm);
948 }
949
950
951 /**
952 * eapol_auth_step - Advance EAPOL state machines
953 * @sm: EAPOL state machine
954 *
955 * This function is called to advance EAPOL state machines after any change
956 * that could affect their state.
957 */
eapol_auth_step(struct eapol_state_machine * sm)958 void eapol_auth_step(struct eapol_state_machine *sm)
959 {
960 /*
961 * Run eapol_sm_step_run from a registered timeout to make sure that
962 * other possible timeouts/events are processed and to avoid long
963 * function call chains.
964 */
965
966 eloop_register_timeout(0, 0, eapol_sm_step_cb, sm, NULL);
967 }
968
969
eapol_auth_initialize(struct eapol_state_machine * sm)970 static void eapol_auth_initialize(struct eapol_state_machine *sm)
971 {
972 sm->initializing = TRUE;
973 /* Initialize the state machines by asserting initialize and then
974 * deasserting it after one step */
975 sm->initialize = TRUE;
976 eapol_sm_step_run(sm);
977 sm->initialize = FALSE;
978 eapol_sm_step_run(sm);
979 sm->initializing = FALSE;
980
981 /* Start one second tick for port timers state machine */
982 eloop_cancel_timeout(eapol_port_timers_tick, NULL, sm);
983 eloop_register_timeout(1, 0, eapol_port_timers_tick, NULL, sm);
984 }
985
986
eapol_sm_get_eap_user(void * ctx,const u8 * identity,size_t identity_len,int phase2,struct eap_user * user)987 static int eapol_sm_get_eap_user(void *ctx, const u8 *identity,
988 size_t identity_len, int phase2,
989 struct eap_user *user)
990 {
991 struct eapol_state_machine *sm = ctx;
992 return sm->eapol->cb.get_eap_user(sm->eapol->conf.ctx, identity,
993 identity_len, phase2, user);
994 }
995
996
eapol_sm_get_eap_req_id_text(void * ctx,size_t * len)997 static const char * eapol_sm_get_eap_req_id_text(void *ctx, size_t *len)
998 {
999 struct eapol_state_machine *sm = ctx;
1000 *len = sm->eapol->conf.eap_req_id_text_len;
1001 return sm->eapol->conf.eap_req_id_text;
1002 }
1003
1004
1005 static struct eapol_callbacks eapol_cb =
1006 {
1007 eapol_sm_get_eap_user,
1008 eapol_sm_get_eap_req_id_text
1009 };
1010
1011
eapol_auth_eap_pending_cb(struct eapol_state_machine * sm,void * ctx)1012 int eapol_auth_eap_pending_cb(struct eapol_state_machine *sm, void *ctx)
1013 {
1014 if (sm == NULL || ctx == NULL || ctx != sm->eap)
1015 return -1;
1016
1017 eap_sm_pending_cb(sm->eap);
1018 eapol_auth_step(sm);
1019
1020 return 0;
1021 }
1022
1023
eapol_auth_conf_clone(struct eapol_auth_config * dst,struct eapol_auth_config * src)1024 static int eapol_auth_conf_clone(struct eapol_auth_config *dst,
1025 struct eapol_auth_config *src)
1026 {
1027 dst->ctx = src->ctx;
1028 dst->eap_reauth_period = src->eap_reauth_period;
1029 dst->wpa = src->wpa;
1030 dst->individual_wep_key_len = src->individual_wep_key_len;
1031 dst->eap_server = src->eap_server;
1032 dst->ssl_ctx = src->ssl_ctx;
1033 dst->msg_ctx = src->msg_ctx;
1034 dst->eap_sim_db_priv = src->eap_sim_db_priv;
1035 os_free(dst->eap_req_id_text);
1036 dst->pwd_group = src->pwd_group;
1037 dst->pbc_in_m1 = src->pbc_in_m1;
1038 if (src->eap_req_id_text) {
1039 dst->eap_req_id_text = os_malloc(src->eap_req_id_text_len);
1040 if (dst->eap_req_id_text == NULL)
1041 return -1;
1042 os_memcpy(dst->eap_req_id_text, src->eap_req_id_text,
1043 src->eap_req_id_text_len);
1044 dst->eap_req_id_text_len = src->eap_req_id_text_len;
1045 } else {
1046 dst->eap_req_id_text = NULL;
1047 dst->eap_req_id_text_len = 0;
1048 }
1049 if (src->pac_opaque_encr_key) {
1050 dst->pac_opaque_encr_key = os_malloc(16);
1051 os_memcpy(dst->pac_opaque_encr_key, src->pac_opaque_encr_key,
1052 16);
1053 } else
1054 dst->pac_opaque_encr_key = NULL;
1055 if (src->eap_fast_a_id) {
1056 dst->eap_fast_a_id = os_malloc(src->eap_fast_a_id_len);
1057 if (dst->eap_fast_a_id == NULL) {
1058 os_free(dst->eap_req_id_text);
1059 return -1;
1060 }
1061 os_memcpy(dst->eap_fast_a_id, src->eap_fast_a_id,
1062 src->eap_fast_a_id_len);
1063 dst->eap_fast_a_id_len = src->eap_fast_a_id_len;
1064 } else
1065 dst->eap_fast_a_id = NULL;
1066 if (src->eap_fast_a_id_info) {
1067 dst->eap_fast_a_id_info = os_strdup(src->eap_fast_a_id_info);
1068 if (dst->eap_fast_a_id_info == NULL) {
1069 os_free(dst->eap_req_id_text);
1070 os_free(dst->eap_fast_a_id);
1071 return -1;
1072 }
1073 } else
1074 dst->eap_fast_a_id_info = NULL;
1075 dst->eap_fast_prov = src->eap_fast_prov;
1076 dst->pac_key_lifetime = src->pac_key_lifetime;
1077 dst->pac_key_refresh_time = src->pac_key_refresh_time;
1078 dst->eap_sim_aka_result_ind = src->eap_sim_aka_result_ind;
1079 dst->tnc = src->tnc;
1080 dst->wps = src->wps;
1081 dst->fragment_size = src->fragment_size;
1082 return 0;
1083 }
1084
1085
eapol_auth_conf_free(struct eapol_auth_config * conf)1086 static void eapol_auth_conf_free(struct eapol_auth_config *conf)
1087 {
1088 os_free(conf->eap_req_id_text);
1089 conf->eap_req_id_text = NULL;
1090 os_free(conf->pac_opaque_encr_key);
1091 conf->pac_opaque_encr_key = NULL;
1092 os_free(conf->eap_fast_a_id);
1093 conf->eap_fast_a_id = NULL;
1094 os_free(conf->eap_fast_a_id_info);
1095 conf->eap_fast_a_id_info = NULL;
1096 }
1097
1098
eapol_auth_init(struct eapol_auth_config * conf,struct eapol_auth_cb * cb)1099 struct eapol_authenticator * eapol_auth_init(struct eapol_auth_config *conf,
1100 struct eapol_auth_cb *cb)
1101 {
1102 struct eapol_authenticator *eapol;
1103
1104 eapol = os_zalloc(sizeof(*eapol));
1105 if (eapol == NULL)
1106 return NULL;
1107
1108 if (eapol_auth_conf_clone(&eapol->conf, conf) < 0) {
1109 os_free(eapol);
1110 return NULL;
1111 }
1112
1113 if (conf->individual_wep_key_len > 0) {
1114 /* use key0 in individual key and key1 in broadcast key */
1115 eapol->default_wep_key_idx = 1;
1116 }
1117
1118 eapol->cb.eapol_send = cb->eapol_send;
1119 eapol->cb.aaa_send = cb->aaa_send;
1120 eapol->cb.finished = cb->finished;
1121 eapol->cb.get_eap_user = cb->get_eap_user;
1122 eapol->cb.sta_entry_alive = cb->sta_entry_alive;
1123 eapol->cb.logger = cb->logger;
1124 eapol->cb.set_port_authorized = cb->set_port_authorized;
1125 eapol->cb.abort_auth = cb->abort_auth;
1126 eapol->cb.tx_key = cb->tx_key;
1127 eapol->cb.eapol_event = cb->eapol_event;
1128
1129 return eapol;
1130 }
1131
1132
eapol_auth_deinit(struct eapol_authenticator * eapol)1133 void eapol_auth_deinit(struct eapol_authenticator *eapol)
1134 {
1135 if (eapol == NULL)
1136 return;
1137
1138 eapol_auth_conf_free(&eapol->conf);
1139 os_free(eapol->default_wep_key);
1140 os_free(eapol);
1141 }
1142