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