eapol_sm.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  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_fast_a_id_len = eapol->conf.eap_fast_a_id_len;
  647. eap_conf.eap_fast_a_id_info = eapol->conf.eap_fast_a_id_info;
  648. eap_conf.eap_fast_prov = eapol->conf.eap_fast_prov;
  649. eap_conf.pac_key_lifetime = eapol->conf.pac_key_lifetime;
  650. eap_conf.pac_key_refresh_time = eapol->conf.pac_key_refresh_time;
  651. eap_conf.eap_sim_aka_result_ind = eapol->conf.eap_sim_aka_result_ind;
  652. eap_conf.tnc = eapol->conf.tnc;
  653. sm->eap = eap_server_sm_init(sm, &eapol_cb, &eap_conf);
  654. if (sm->eap == NULL) {
  655. eapol_auth_free(sm);
  656. return NULL;
  657. }
  658. sm->eap_if = eap_get_interface(sm->eap);
  659. eapol_auth_initialize(sm);
  660. return sm;
  661. }
  662. void eapol_auth_free(struct eapol_state_machine *sm)
  663. {
  664. if (sm == NULL)
  665. return;
  666. eloop_cancel_timeout(eapol_port_timers_tick, NULL, sm);
  667. eloop_cancel_timeout(eapol_sm_step_cb, sm, NULL);
  668. if (sm->eap)
  669. eap_server_sm_deinit(sm->eap);
  670. os_free(sm);
  671. }
  672. static int eapol_sm_sta_entry_alive(struct eapol_authenticator *eapol,
  673. const u8 *addr)
  674. {
  675. return eapol->cb.sta_entry_alive(eapol->conf.hapd, addr);
  676. }
  677. static void eapol_sm_step_run(struct eapol_state_machine *sm)
  678. {
  679. struct eapol_authenticator *eapol = sm->eapol;
  680. u8 addr[ETH_ALEN];
  681. unsigned int prev_auth_pae, prev_be_auth, prev_reauth_timer,
  682. prev_auth_key_tx, prev_key_rx, prev_ctrl_dir;
  683. int max_steps = 100;
  684. os_memcpy(addr, sm->addr, ETH_ALEN);
  685. /*
  686. * Allow EAPOL state machines to run as long as there are state
  687. * changes, but exit and return here through event loop if more than
  688. * 100 steps is needed as a precaution against infinite loops inside
  689. * eloop callback.
  690. */
  691. restart:
  692. prev_auth_pae = sm->auth_pae_state;
  693. prev_be_auth = sm->be_auth_state;
  694. prev_reauth_timer = sm->reauth_timer_state;
  695. prev_auth_key_tx = sm->auth_key_tx_state;
  696. prev_key_rx = sm->key_rx_state;
  697. prev_ctrl_dir = sm->ctrl_dir_state;
  698. SM_STEP_RUN(AUTH_PAE);
  699. if (sm->initializing || eapol_sm_sta_entry_alive(eapol, addr))
  700. SM_STEP_RUN(BE_AUTH);
  701. if (sm->initializing || eapol_sm_sta_entry_alive(eapol, addr))
  702. SM_STEP_RUN(REAUTH_TIMER);
  703. if (sm->initializing || eapol_sm_sta_entry_alive(eapol, addr))
  704. SM_STEP_RUN(AUTH_KEY_TX);
  705. if (sm->initializing || eapol_sm_sta_entry_alive(eapol, addr))
  706. SM_STEP_RUN(KEY_RX);
  707. if (sm->initializing || eapol_sm_sta_entry_alive(eapol, addr))
  708. SM_STEP_RUN(CTRL_DIR);
  709. if (prev_auth_pae != sm->auth_pae_state ||
  710. prev_be_auth != sm->be_auth_state ||
  711. prev_reauth_timer != sm->reauth_timer_state ||
  712. prev_auth_key_tx != sm->auth_key_tx_state ||
  713. prev_key_rx != sm->key_rx_state ||
  714. prev_ctrl_dir != sm->ctrl_dir_state) {
  715. if (--max_steps > 0)
  716. goto restart;
  717. /* Re-run from eloop timeout */
  718. eapol_auth_step(sm);
  719. return;
  720. }
  721. if (eapol_sm_sta_entry_alive(eapol, addr) && sm->eap) {
  722. if (eap_server_sm_step(sm->eap)) {
  723. if (--max_steps > 0)
  724. goto restart;
  725. /* Re-run from eloop timeout */
  726. eapol_auth_step(sm);
  727. return;
  728. }
  729. /* TODO: find a better location for this */
  730. if (sm->eap_if->aaaEapResp) {
  731. sm->eap_if->aaaEapResp = FALSE;
  732. if (sm->eap_if->aaaEapRespData == NULL) {
  733. wpa_printf(MSG_DEBUG, "EAPOL: aaaEapResp set, "
  734. "but no aaaEapRespData available");
  735. return;
  736. }
  737. sm->eapol->cb.aaa_send(
  738. sm->hapd, sm->sta,
  739. wpabuf_head(sm->eap_if->aaaEapRespData),
  740. wpabuf_len(sm->eap_if->aaaEapRespData));
  741. }
  742. }
  743. if (eapol_sm_sta_entry_alive(eapol, addr))
  744. wpa_auth_sm_notify(sm->sta->wpa_sm);
  745. }
  746. static void eapol_sm_step_cb(void *eloop_ctx, void *timeout_ctx)
  747. {
  748. struct eapol_state_machine *sm = eloop_ctx;
  749. eapol_sm_step_run(sm);
  750. }
  751. void eapol_auth_step(struct eapol_state_machine *sm)
  752. {
  753. /*
  754. * Run eapol_sm_step_run from a registered timeout to make sure that
  755. * other possible timeouts/events are processed and to avoid long
  756. * function call chains.
  757. */
  758. eloop_register_timeout(0, 0, eapol_sm_step_cb, sm, NULL);
  759. }
  760. void eapol_auth_initialize(struct eapol_state_machine *sm)
  761. {
  762. sm->initializing = TRUE;
  763. /* Initialize the state machines by asserting initialize and then
  764. * deasserting it after one step */
  765. sm->initialize = TRUE;
  766. eapol_sm_step_run(sm);
  767. sm->initialize = FALSE;
  768. eapol_sm_step_run(sm);
  769. sm->initializing = FALSE;
  770. /* Start one second tick for port timers state machine */
  771. eloop_cancel_timeout(eapol_port_timers_tick, NULL, sm);
  772. eloop_register_timeout(1, 0, eapol_port_timers_tick, NULL, sm);
  773. }
  774. #ifdef HOSTAPD_DUMP_STATE
  775. static inline const char * port_type_txt(PortTypes pt)
  776. {
  777. switch (pt) {
  778. case ForceUnauthorized: return "ForceUnauthorized";
  779. case ForceAuthorized: return "ForceAuthorized";
  780. case Auto: return "Auto";
  781. default: return "Unknown";
  782. }
  783. }
  784. static inline const char * port_state_txt(PortState ps)
  785. {
  786. switch (ps) {
  787. case Unauthorized: return "Unauthorized";
  788. case Authorized: return "Authorized";
  789. default: return "Unknown";
  790. }
  791. }
  792. static inline const char * ctrl_dir_txt(ControlledDirection dir)
  793. {
  794. switch (dir) {
  795. case Both: return "Both";
  796. case In: return "In";
  797. default: return "Unknown";
  798. }
  799. }
  800. static inline const char * auth_pae_state_txt(int s)
  801. {
  802. switch (s) {
  803. case AUTH_PAE_INITIALIZE: return "INITIALIZE";
  804. case AUTH_PAE_DISCONNECTED: return "DISCONNECTED";
  805. case AUTH_PAE_CONNECTING: return "CONNECTING";
  806. case AUTH_PAE_AUTHENTICATING: return "AUTHENTICATING";
  807. case AUTH_PAE_AUTHENTICATED: return "AUTHENTICATED";
  808. case AUTH_PAE_ABORTING: return "ABORTING";
  809. case AUTH_PAE_HELD: return "HELD";
  810. case AUTH_PAE_FORCE_AUTH: return "FORCE_AUTH";
  811. case AUTH_PAE_FORCE_UNAUTH: return "FORCE_UNAUTH";
  812. case AUTH_PAE_RESTART: return "RESTART";
  813. default: return "Unknown";
  814. }
  815. }
  816. static inline const char * be_auth_state_txt(int s)
  817. {
  818. switch (s) {
  819. case BE_AUTH_REQUEST: return "REQUEST";
  820. case BE_AUTH_RESPONSE: return "RESPONSE";
  821. case BE_AUTH_SUCCESS: return "SUCCESS";
  822. case BE_AUTH_FAIL: return "FAIL";
  823. case BE_AUTH_TIMEOUT: return "TIMEOUT";
  824. case BE_AUTH_IDLE: return "IDLE";
  825. case BE_AUTH_INITIALIZE: return "INITIALIZE";
  826. case BE_AUTH_IGNORE: return "IGNORE";
  827. default: return "Unknown";
  828. }
  829. }
  830. static inline const char * reauth_timer_state_txt(int s)
  831. {
  832. switch (s) {
  833. case REAUTH_TIMER_INITIALIZE: return "INITIALIZE";
  834. case REAUTH_TIMER_REAUTHENTICATE: return "REAUTHENTICATE";
  835. default: return "Unknown";
  836. }
  837. }
  838. static inline const char * auth_key_tx_state_txt(int s)
  839. {
  840. switch (s) {
  841. case AUTH_KEY_TX_NO_KEY_TRANSMIT: return "NO_KEY_TRANSMIT";
  842. case AUTH_KEY_TX_KEY_TRANSMIT: return "KEY_TRANSMIT";
  843. default: return "Unknown";
  844. }
  845. }
  846. static inline const char * key_rx_state_txt(int s)
  847. {
  848. switch (s) {
  849. case KEY_RX_NO_KEY_RECEIVE: return "NO_KEY_RECEIVE";
  850. case KEY_RX_KEY_RECEIVE: return "KEY_RECEIVE";
  851. default: return "Unknown";
  852. }
  853. }
  854. static inline const char * ctrl_dir_state_txt(int s)
  855. {
  856. switch (s) {
  857. case CTRL_DIR_FORCE_BOTH: return "FORCE_BOTH";
  858. case CTRL_DIR_IN_OR_BOTH: return "IN_OR_BOTH";
  859. default: return "Unknown";
  860. }
  861. }
  862. void eapol_auth_dump_state(FILE *f, const char *prefix,
  863. struct eapol_state_machine *sm)
  864. {
  865. fprintf(f, "%sEAPOL state machine:\n", prefix);
  866. fprintf(f, "%s aWhile=%d quietWhile=%d reAuthWhen=%d\n", prefix,
  867. sm->aWhile, sm->quietWhile, sm->reAuthWhen);
  868. #define _SB(b) ((b) ? "TRUE" : "FALSE")
  869. fprintf(f,
  870. "%s authAbort=%s authFail=%s authPortStatus=%s authStart=%s\n"
  871. "%s authTimeout=%s authSuccess=%s eapFail=%s eapolEap=%s\n"
  872. "%s eapSuccess=%s eapTimeout=%s initialize=%s "
  873. "keyAvailable=%s\n"
  874. "%s keyDone=%s keyRun=%s keyTxEnabled=%s portControl=%s\n"
  875. "%s portEnabled=%s portValid=%s reAuthenticate=%s\n",
  876. prefix, _SB(sm->authAbort), _SB(sm->authFail),
  877. port_state_txt(sm->authPortStatus), _SB(sm->authStart),
  878. prefix, _SB(sm->authTimeout), _SB(sm->authSuccess),
  879. _SB(sm->eap_if->eapFail), _SB(sm->eapolEap),
  880. prefix, _SB(sm->eap_if->eapSuccess),
  881. _SB(sm->eap_if->eapTimeout),
  882. _SB(sm->initialize), _SB(sm->eap_if->eapKeyAvailable),
  883. prefix, _SB(sm->keyDone), _SB(sm->keyRun),
  884. _SB(sm->keyTxEnabled), port_type_txt(sm->portControl),
  885. prefix, _SB(sm->eap_if->portEnabled), _SB(sm->portValid),
  886. _SB(sm->reAuthenticate));
  887. fprintf(f, "%s Authenticator PAE:\n"
  888. "%s state=%s\n"
  889. "%s eapolLogoff=%s eapolStart=%s eapRestart=%s\n"
  890. "%s portMode=%s reAuthCount=%d\n"
  891. "%s quietPeriod=%d reAuthMax=%d\n"
  892. "%s authEntersConnecting=%d\n"
  893. "%s authEapLogoffsWhileConnecting=%d\n"
  894. "%s authEntersAuthenticating=%d\n"
  895. "%s authAuthSuccessesWhileAuthenticating=%d\n"
  896. "%s authAuthTimeoutsWhileAuthenticating=%d\n"
  897. "%s authAuthFailWhileAuthenticating=%d\n"
  898. "%s authAuthEapStartsWhileAuthenticating=%d\n"
  899. "%s authAuthEapLogoffWhileAuthenticating=%d\n"
  900. "%s authAuthReauthsWhileAuthenticated=%d\n"
  901. "%s authAuthEapStartsWhileAuthenticated=%d\n"
  902. "%s authAuthEapLogoffWhileAuthenticated=%d\n",
  903. prefix, prefix, auth_pae_state_txt(sm->auth_pae_state), prefix,
  904. _SB(sm->eapolLogoff), _SB(sm->eapolStart),
  905. _SB(sm->eap_if->eapRestart),
  906. prefix, port_type_txt(sm->portMode), sm->reAuthCount,
  907. prefix, sm->quietPeriod, sm->reAuthMax,
  908. prefix, sm->authEntersConnecting,
  909. prefix, sm->authEapLogoffsWhileConnecting,
  910. prefix, sm->authEntersAuthenticating,
  911. prefix, sm->authAuthSuccessesWhileAuthenticating,
  912. prefix, sm->authAuthTimeoutsWhileAuthenticating,
  913. prefix, sm->authAuthFailWhileAuthenticating,
  914. prefix, sm->authAuthEapStartsWhileAuthenticating,
  915. prefix, sm->authAuthEapLogoffWhileAuthenticating,
  916. prefix, sm->authAuthReauthsWhileAuthenticated,
  917. prefix, sm->authAuthEapStartsWhileAuthenticated,
  918. prefix, sm->authAuthEapLogoffWhileAuthenticated);
  919. fprintf(f, "%s Backend Authentication:\n"
  920. "%s state=%s\n"
  921. "%s eapNoReq=%s eapReq=%s eapResp=%s\n"
  922. "%s serverTimeout=%d\n"
  923. "%s backendResponses=%d\n"
  924. "%s backendAccessChallenges=%d\n"
  925. "%s backendOtherRequestsToSupplicant=%d\n"
  926. "%s backendAuthSuccesses=%d\n"
  927. "%s backendAuthFails=%d\n",
  928. prefix, prefix,
  929. be_auth_state_txt(sm->be_auth_state),
  930. prefix, _SB(sm->eap_if->eapNoReq), _SB(sm->eap_if->eapReq),
  931. _SB(sm->eap_if->eapResp),
  932. prefix, sm->serverTimeout,
  933. prefix, sm->backendResponses,
  934. prefix, sm->backendAccessChallenges,
  935. prefix, sm->backendOtherRequestsToSupplicant,
  936. prefix, sm->backendAuthSuccesses,
  937. prefix, sm->backendAuthFails);
  938. fprintf(f, "%s Reauthentication Timer:\n"
  939. "%s state=%s\n"
  940. "%s reAuthPeriod=%d reAuthEnabled=%s\n", prefix, prefix,
  941. reauth_timer_state_txt(sm->reauth_timer_state), prefix,
  942. sm->reAuthPeriod, _SB(sm->reAuthEnabled));
  943. fprintf(f, "%s Authenticator Key Transmit:\n"
  944. "%s state=%s\n", prefix, prefix,
  945. auth_key_tx_state_txt(sm->auth_key_tx_state));
  946. fprintf(f, "%s Key Receive:\n"
  947. "%s state=%s\n"
  948. "%s rxKey=%s\n", prefix, prefix,
  949. key_rx_state_txt(sm->key_rx_state), prefix, _SB(sm->rxKey));
  950. fprintf(f, "%s Controlled Directions:\n"
  951. "%s state=%s\n"
  952. "%s adminControlledDirections=%s "
  953. "operControlledDirections=%s\n"
  954. "%s operEdge=%s\n", prefix, prefix,
  955. ctrl_dir_state_txt(sm->ctrl_dir_state),
  956. prefix, ctrl_dir_txt(sm->adminControlledDirections),
  957. ctrl_dir_txt(sm->operControlledDirections),
  958. prefix, _SB(sm->operEdge));
  959. #undef _SB
  960. }
  961. #endif /* HOSTAPD_DUMP_STATE */
  962. static int eapol_sm_get_eap_user(void *ctx, const u8 *identity,
  963. size_t identity_len, int phase2,
  964. struct eap_user *user)
  965. {
  966. struct eapol_state_machine *sm = ctx;
  967. return sm->eapol->cb.get_eap_user(sm->hapd, identity, identity_len,
  968. phase2, user);
  969. }
  970. static const char * eapol_sm_get_eap_req_id_text(void *ctx, size_t *len)
  971. {
  972. struct eapol_state_machine *sm = ctx;
  973. *len = sm->eapol->conf.eap_req_id_text_len;
  974. return sm->eapol->conf.eap_req_id_text;
  975. }
  976. static struct eapol_callbacks eapol_cb =
  977. {
  978. .get_eap_user = eapol_sm_get_eap_user,
  979. .get_eap_req_id_text = eapol_sm_get_eap_req_id_text,
  980. };
  981. int eapol_auth_eap_pending_cb(struct eapol_state_machine *sm, void *ctx)
  982. {
  983. if (sm == NULL || ctx != sm->eap)
  984. return -1;
  985. eap_sm_pending_cb(sm->eap);
  986. eapol_auth_step(sm);
  987. return 0;
  988. }
  989. static int eapol_auth_conf_clone(struct eapol_auth_config *dst,
  990. struct eapol_auth_config *src)
  991. {
  992. dst->hapd = src->hapd;
  993. dst->eap_reauth_period = src->eap_reauth_period;
  994. dst->wpa = src->wpa;
  995. dst->individual_wep_key_len = src->individual_wep_key_len;
  996. dst->eap_server = src->eap_server;
  997. dst->ssl_ctx = src->ssl_ctx;
  998. dst->eap_sim_db_priv = src->eap_sim_db_priv;
  999. os_free(dst->eap_req_id_text);
  1000. if (src->eap_req_id_text) {
  1001. dst->eap_req_id_text = os_malloc(src->eap_req_id_text_len);
  1002. if (dst->eap_req_id_text == NULL)
  1003. return -1;
  1004. os_memcpy(dst->eap_req_id_text, src->eap_req_id_text,
  1005. src->eap_req_id_text_len);
  1006. dst->eap_req_id_text_len = src->eap_req_id_text_len;
  1007. } else {
  1008. dst->eap_req_id_text = NULL;
  1009. dst->eap_req_id_text_len = 0;
  1010. }
  1011. if (src->pac_opaque_encr_key) {
  1012. dst->pac_opaque_encr_key = os_malloc(16);
  1013. os_memcpy(dst->pac_opaque_encr_key, src->pac_opaque_encr_key,
  1014. 16);
  1015. } else
  1016. dst->pac_opaque_encr_key = NULL;
  1017. if (src->eap_fast_a_id) {
  1018. dst->eap_fast_a_id = os_malloc(src->eap_fast_a_id_len);
  1019. if (dst->eap_fast_a_id == NULL) {
  1020. os_free(dst->eap_req_id_text);
  1021. return -1;
  1022. }
  1023. os_memcpy(dst->eap_fast_a_id, src->eap_fast_a_id,
  1024. src->eap_fast_a_id_len);
  1025. dst->eap_fast_a_id_len = src->eap_fast_a_id_len;
  1026. } else
  1027. dst->eap_fast_a_id = NULL;
  1028. if (src->eap_fast_a_id_info) {
  1029. dst->eap_fast_a_id_info = os_strdup(src->eap_fast_a_id_info);
  1030. if (dst->eap_fast_a_id_info == NULL) {
  1031. os_free(dst->eap_req_id_text);
  1032. os_free(dst->eap_fast_a_id);
  1033. return -1;
  1034. }
  1035. } else
  1036. dst->eap_fast_a_id_info = NULL;
  1037. dst->eap_fast_prov = src->eap_fast_prov;
  1038. dst->pac_key_lifetime = src->pac_key_lifetime;
  1039. dst->pac_key_refresh_time = src->pac_key_refresh_time;
  1040. dst->eap_sim_aka_result_ind = src->eap_sim_aka_result_ind;
  1041. dst->tnc = src->tnc;
  1042. return 0;
  1043. }
  1044. static void eapol_auth_conf_free(struct eapol_auth_config *conf)
  1045. {
  1046. os_free(conf->eap_req_id_text);
  1047. conf->eap_req_id_text = NULL;
  1048. os_free(conf->pac_opaque_encr_key);
  1049. conf->pac_opaque_encr_key = NULL;
  1050. os_free(conf->eap_fast_a_id);
  1051. conf->eap_fast_a_id = NULL;
  1052. os_free(conf->eap_fast_a_id_info);
  1053. conf->eap_fast_a_id_info = NULL;
  1054. }
  1055. struct eapol_authenticator * eapol_auth_init(struct eapol_auth_config *conf,
  1056. struct eapol_auth_cb *cb)
  1057. {
  1058. struct eapol_authenticator *eapol;
  1059. eapol = os_zalloc(sizeof(*eapol));
  1060. if (eapol == NULL)
  1061. return NULL;
  1062. if (eapol_auth_conf_clone(&eapol->conf, conf) < 0) {
  1063. os_free(eapol);
  1064. return NULL;
  1065. }
  1066. eapol->cb.eapol_send = cb->eapol_send;
  1067. eapol->cb.aaa_send = cb->aaa_send;
  1068. eapol->cb.finished = cb->finished;
  1069. eapol->cb.get_eap_user = cb->get_eap_user;
  1070. eapol->cb.sta_entry_alive = cb->sta_entry_alive;
  1071. eapol->cb.logger = cb->logger;
  1072. eapol->cb.set_port_authorized = cb->set_port_authorized;
  1073. eapol->cb.abort_auth = cb->abort_auth;
  1074. eapol->cb.tx_key = cb->tx_key;
  1075. return eapol;
  1076. }
  1077. void eapol_auth_deinit(struct eapol_authenticator *eapol)
  1078. {
  1079. if (eapol == NULL)
  1080. return;
  1081. eapol_auth_conf_free(&eapol->conf);
  1082. os_free(eapol);
  1083. }