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