eapol_sm.c 32 KB

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