eapol_sm.c 33 KB

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