eapol_sm.c 34 KB

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