eapol_sm.c 33 KB

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