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