eapol_sm.c 34 KB

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