eapol_sm.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  1. /*
  2. * IEEE 802.1X-2004 Authenticator - EAPOL state machine
  3. * Copyright (c) 2002-2009, 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 "eapol_sm.h"
  17. #include "eloop.h"
  18. #include "common/eapol_common.h"
  19. #include "sta_info.h"
  20. #include "eap_server/eap.h"
  21. #include "state_machine.h"
  22. #include "eap_common/eap_common.h"
  23. #define STATE_MACHINE_DATA struct eapol_state_machine
  24. #define STATE_MACHINE_DEBUG_PREFIX "IEEE 802.1X"
  25. #define STATE_MACHINE_ADDR sm->addr
  26. static struct eapol_callbacks eapol_cb;
  27. /* EAPOL state machines are described in IEEE Std 802.1X-2004, Chap. 8.2 */
  28. #define setPortAuthorized() \
  29. sm->eapol->cb.set_port_authorized(sm->hapd, sm->sta, 1)
  30. #define setPortUnauthorized() \
  31. sm->eapol->cb.set_port_authorized(sm->hapd, sm->sta, 0)
  32. /* procedures */
  33. #define txCannedFail() eapol_auth_tx_canned_eap(sm, 0)
  34. #define txCannedSuccess() eapol_auth_tx_canned_eap(sm, 1)
  35. #define txReq() eapol_auth_tx_req(sm)
  36. #define abortAuth() sm->eapol->cb.abort_auth(sm->hapd, sm->sta)
  37. #define txKey() sm->eapol->cb.tx_key(sm->hapd, sm->sta)
  38. #define processKey() do { } while (0)
  39. static void eapol_sm_step_run(struct eapol_state_machine *sm);
  40. static void eapol_sm_step_cb(void *eloop_ctx, void *timeout_ctx);
  41. static void eapol_auth_initialize(struct eapol_state_machine *sm);
  42. static void eapol_auth_logger(struct eapol_authenticator *eapol,
  43. const u8 *addr, eapol_logger_level level,
  44. const char *txt)
  45. {
  46. if (eapol->cb.logger == NULL)
  47. return;
  48. eapol->cb.logger(eapol->conf.hapd, addr, level, txt);
  49. }
  50. static void eapol_auth_vlogger(struct eapol_authenticator *eapol,
  51. const u8 *addr, eapol_logger_level level,
  52. const char *fmt, ...)
  53. {
  54. char *format;
  55. int maxlen;
  56. va_list ap;
  57. if (eapol->cb.logger == NULL)
  58. return;
  59. maxlen = os_strlen(fmt) + 100;
  60. format = os_malloc(maxlen);
  61. if (!format)
  62. return;
  63. va_start(ap, fmt);
  64. vsnprintf(format, maxlen, fmt, ap);
  65. va_end(ap);
  66. eapol_auth_logger(eapol, addr, level, format);
  67. os_free(format);
  68. }
  69. static void eapol_auth_tx_canned_eap(struct eapol_state_machine *sm,
  70. int success)
  71. {
  72. struct eap_hdr eap;
  73. os_memset(&eap, 0, sizeof(eap));
  74. eap.code = success ? EAP_CODE_SUCCESS : EAP_CODE_FAILURE;
  75. eap.identifier = ++sm->last_eap_id;
  76. eap.length = host_to_be16(sizeof(eap));
  77. eapol_auth_vlogger(sm->eapol, sm->addr, EAPOL_LOGGER_DEBUG,
  78. "Sending canned EAP packet %s (identifier %d)",
  79. success ? "SUCCESS" : "FAILURE", eap.identifier);
  80. sm->eapol->cb.eapol_send(sm->hapd, sm->sta, IEEE802_1X_TYPE_EAP_PACKET,
  81. (u8 *) &eap, sizeof(eap));
  82. sm->dot1xAuthEapolFramesTx++;
  83. }
  84. static void eapol_auth_tx_req(struct eapol_state_machine *sm)
  85. {
  86. if (sm->eap_if->eapReqData == NULL ||
  87. wpabuf_len(sm->eap_if->eapReqData) < sizeof(struct eap_hdr)) {
  88. eapol_auth_logger(sm->eapol, sm->addr,
  89. EAPOL_LOGGER_DEBUG,
  90. "TxReq called, but there is no EAP request "
  91. "from authentication server");
  92. return;
  93. }
  94. if (sm->flags & EAPOL_SM_WAIT_START) {
  95. wpa_printf(MSG_DEBUG, "EAPOL: Drop EAPOL TX to " MACSTR
  96. " while waiting for EAPOL-Start",
  97. MAC2STR(sm->addr));
  98. return;
  99. }
  100. sm->last_eap_id = eap_get_id(sm->eap_if->eapReqData);
  101. eapol_auth_vlogger(sm->eapol, sm->addr, EAPOL_LOGGER_DEBUG,
  102. "Sending EAP Packet (identifier %d)",
  103. sm->last_eap_id);
  104. sm->eapol->cb.eapol_send(sm->hapd, sm->sta, IEEE802_1X_TYPE_EAP_PACKET,
  105. wpabuf_head(sm->eap_if->eapReqData),
  106. wpabuf_len(sm->eap_if->eapReqData));
  107. sm->dot1xAuthEapolFramesTx++;
  108. if (eap_get_type(sm->eap_if->eapReqData) == EAP_TYPE_IDENTITY)
  109. sm->dot1xAuthEapolReqIdFramesTx++;
  110. else
  111. sm->dot1xAuthEapolReqFramesTx++;
  112. }
  113. /**
  114. * eapol_port_timers_tick - Port Timers state machine
  115. * @eloop_ctx: struct eapol_state_machine *
  116. * @timeout_ctx: Not used
  117. *
  118. * This statemachine is implemented as a function that will be called
  119. * once a second as a registered event loop timeout.
  120. */
  121. static void eapol_port_timers_tick(void *eloop_ctx, void *timeout_ctx)
  122. {
  123. struct eapol_state_machine *state = timeout_ctx;
  124. if (state->aWhile > 0) {
  125. state->aWhile--;
  126. if (state->aWhile == 0) {
  127. wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR
  128. " - aWhile --> 0",
  129. MAC2STR(state->addr));
  130. }
  131. }
  132. if (state->quietWhile > 0) {
  133. state->quietWhile--;
  134. if (state->quietWhile == 0) {
  135. wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR
  136. " - quietWhile --> 0",
  137. MAC2STR(state->addr));
  138. }
  139. }
  140. if (state->reAuthWhen > 0) {
  141. state->reAuthWhen--;
  142. if (state->reAuthWhen == 0) {
  143. wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR
  144. " - reAuthWhen --> 0",
  145. MAC2STR(state->addr));
  146. }
  147. }
  148. if (state->eap_if->retransWhile > 0) {
  149. state->eap_if->retransWhile--;
  150. if (state->eap_if->retransWhile == 0) {
  151. wpa_printf(MSG_DEBUG, "IEEE 802.1X: " MACSTR
  152. " - (EAP) retransWhile --> 0",
  153. MAC2STR(state->addr));
  154. }
  155. }
  156. eapol_sm_step_run(state);
  157. eloop_register_timeout(1, 0, eapol_port_timers_tick, eloop_ctx, state);
  158. }
  159. /* Authenticator PAE state machine */
  160. SM_STATE(AUTH_PAE, INITIALIZE)
  161. {
  162. SM_ENTRY_MA(AUTH_PAE, INITIALIZE, auth_pae);
  163. sm->portMode = Auto;
  164. }
  165. SM_STATE(AUTH_PAE, DISCONNECTED)
  166. {
  167. int from_initialize = sm->auth_pae_state == AUTH_PAE_INITIALIZE;
  168. if (sm->eapolLogoff) {
  169. if (sm->auth_pae_state == AUTH_PAE_CONNECTING)
  170. sm->authEapLogoffsWhileConnecting++;
  171. else if (sm->auth_pae_state == AUTH_PAE_AUTHENTICATED)
  172. sm->authAuthEapLogoffWhileAuthenticated++;
  173. }
  174. SM_ENTRY_MA(AUTH_PAE, DISCONNECTED, auth_pae);
  175. sm->authPortStatus = Unauthorized;
  176. setPortUnauthorized();
  177. sm->reAuthCount = 0;
  178. sm->eapolLogoff = FALSE;
  179. if (!from_initialize) {
  180. sm->eapol->cb.finished(sm->hapd, sm->sta, 0,
  181. sm->flags & EAPOL_SM_PREAUTH);
  182. }
  183. }
  184. SM_STATE(AUTH_PAE, RESTART)
  185. {
  186. if (sm->auth_pae_state == AUTH_PAE_AUTHENTICATED) {
  187. if (sm->reAuthenticate)
  188. sm->authAuthReauthsWhileAuthenticated++;
  189. if (sm->eapolStart)
  190. sm->authAuthEapStartsWhileAuthenticated++;
  191. if (sm->eapolLogoff)
  192. sm->authAuthEapLogoffWhileAuthenticated++;
  193. }
  194. SM_ENTRY_MA(AUTH_PAE, RESTART, auth_pae);
  195. sm->eap_if->eapRestart = TRUE;
  196. }
  197. SM_STATE(AUTH_PAE, CONNECTING)
  198. {
  199. if (sm->auth_pae_state != AUTH_PAE_CONNECTING)
  200. sm->authEntersConnecting++;
  201. SM_ENTRY_MA(AUTH_PAE, CONNECTING, auth_pae);
  202. sm->reAuthenticate = FALSE;
  203. sm->reAuthCount++;
  204. }
  205. SM_STATE(AUTH_PAE, HELD)
  206. {
  207. if (sm->auth_pae_state == AUTH_PAE_AUTHENTICATING && sm->authFail)
  208. sm->authAuthFailWhileAuthenticating++;
  209. SM_ENTRY_MA(AUTH_PAE, HELD, auth_pae);
  210. sm->authPortStatus = Unauthorized;
  211. setPortUnauthorized();
  212. sm->quietWhile = sm->quietPeriod;
  213. sm->eapolLogoff = FALSE;
  214. eapol_auth_vlogger(sm->eapol, sm->addr, EAPOL_LOGGER_WARNING,
  215. "authentication failed - EAP type: %d (%s)",
  216. sm->eap_type_authsrv,
  217. eap_server_get_name(0, sm->eap_type_authsrv));
  218. if (sm->eap_type_authsrv != sm->eap_type_supp) {
  219. eapol_auth_vlogger(sm->eapol, sm->addr, EAPOL_LOGGER_INFO,
  220. "Supplicant used different EAP type: "
  221. "%d (%s)", sm->eap_type_supp,
  222. eap_server_get_name(0, sm->eap_type_supp));
  223. }
  224. sm->eapol->cb.finished(sm->hapd, sm->sta, 0,
  225. sm->flags & EAPOL_SM_PREAUTH);
  226. }
  227. SM_STATE(AUTH_PAE, AUTHENTICATED)
  228. {
  229. char *extra = "";
  230. if (sm->auth_pae_state == AUTH_PAE_AUTHENTICATING && sm->authSuccess)
  231. sm->authAuthSuccessesWhileAuthenticating++;
  232. SM_ENTRY_MA(AUTH_PAE, AUTHENTICATED, auth_pae);
  233. sm->authPortStatus = Authorized;
  234. setPortAuthorized();
  235. sm->reAuthCount = 0;
  236. if (sm->flags & EAPOL_SM_PREAUTH)
  237. extra = " (pre-authentication)";
  238. else if (sm->flags & EAPOL_SM_FROM_PMKSA_CACHE)
  239. extra = " (PMKSA cache)";
  240. eapol_auth_vlogger(sm->eapol, sm->addr, EAPOL_LOGGER_INFO,
  241. "authenticated - EAP type: %d (%s)%s",
  242. sm->eap_type_authsrv,
  243. eap_server_get_name(0, sm->eap_type_authsrv),
  244. 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. sm->eapol->cb.eapol_event(sm->hapd, sm->sta,
  500. EAPOL_AUTH_REAUTHENTICATE);
  501. }
  502. SM_STEP(REAUTH_TIMER)
  503. {
  504. if (sm->portControl != Auto || sm->initialize ||
  505. sm->authPortStatus == Unauthorized || !sm->reAuthEnabled) {
  506. SM_ENTER_GLOBAL(REAUTH_TIMER, INITIALIZE);
  507. return;
  508. }
  509. switch (sm->reauth_timer_state) {
  510. case REAUTH_TIMER_INITIALIZE:
  511. if (sm->reAuthWhen == 0)
  512. SM_ENTER(REAUTH_TIMER, REAUTHENTICATE);
  513. break;
  514. case REAUTH_TIMER_REAUTHENTICATE:
  515. SM_ENTER(REAUTH_TIMER, INITIALIZE);
  516. break;
  517. }
  518. }
  519. /* Authenticator Key Transmit state machine */
  520. SM_STATE(AUTH_KEY_TX, NO_KEY_TRANSMIT)
  521. {
  522. SM_ENTRY_MA(AUTH_KEY_TX, NO_KEY_TRANSMIT, auth_key_tx);
  523. }
  524. SM_STATE(AUTH_KEY_TX, KEY_TRANSMIT)
  525. {
  526. SM_ENTRY_MA(AUTH_KEY_TX, KEY_TRANSMIT, auth_key_tx);
  527. txKey();
  528. sm->eap_if->eapKeyAvailable = FALSE;
  529. sm->keyDone = TRUE;
  530. }
  531. SM_STEP(AUTH_KEY_TX)
  532. {
  533. if (sm->initialize || sm->portControl != Auto) {
  534. SM_ENTER_GLOBAL(AUTH_KEY_TX, NO_KEY_TRANSMIT);
  535. return;
  536. }
  537. switch (sm->auth_key_tx_state) {
  538. case AUTH_KEY_TX_NO_KEY_TRANSMIT:
  539. if (sm->keyTxEnabled && sm->eap_if->eapKeyAvailable &&
  540. sm->keyRun && !(sm->flags & EAPOL_SM_USES_WPA))
  541. SM_ENTER(AUTH_KEY_TX, KEY_TRANSMIT);
  542. break;
  543. case AUTH_KEY_TX_KEY_TRANSMIT:
  544. if (!sm->keyTxEnabled || !sm->keyRun)
  545. SM_ENTER(AUTH_KEY_TX, NO_KEY_TRANSMIT);
  546. else if (sm->eap_if->eapKeyAvailable)
  547. SM_ENTER(AUTH_KEY_TX, KEY_TRANSMIT);
  548. break;
  549. }
  550. }
  551. /* Key Receive state machine */
  552. SM_STATE(KEY_RX, NO_KEY_RECEIVE)
  553. {
  554. SM_ENTRY_MA(KEY_RX, NO_KEY_RECEIVE, key_rx);
  555. }
  556. SM_STATE(KEY_RX, KEY_RECEIVE)
  557. {
  558. SM_ENTRY_MA(KEY_RX, KEY_RECEIVE, key_rx);
  559. processKey();
  560. sm->rxKey = FALSE;
  561. }
  562. SM_STEP(KEY_RX)
  563. {
  564. if (sm->initialize || !sm->eap_if->portEnabled) {
  565. SM_ENTER_GLOBAL(KEY_RX, NO_KEY_RECEIVE);
  566. return;
  567. }
  568. switch (sm->key_rx_state) {
  569. case KEY_RX_NO_KEY_RECEIVE:
  570. if (sm->rxKey)
  571. SM_ENTER(KEY_RX, KEY_RECEIVE);
  572. break;
  573. case KEY_RX_KEY_RECEIVE:
  574. if (sm->rxKey)
  575. SM_ENTER(KEY_RX, KEY_RECEIVE);
  576. break;
  577. }
  578. }
  579. /* Controlled Directions state machine */
  580. SM_STATE(CTRL_DIR, FORCE_BOTH)
  581. {
  582. SM_ENTRY_MA(CTRL_DIR, FORCE_BOTH, ctrl_dir);
  583. sm->operControlledDirections = Both;
  584. }
  585. SM_STATE(CTRL_DIR, IN_OR_BOTH)
  586. {
  587. SM_ENTRY_MA(CTRL_DIR, IN_OR_BOTH, ctrl_dir);
  588. sm->operControlledDirections = sm->adminControlledDirections;
  589. }
  590. SM_STEP(CTRL_DIR)
  591. {
  592. if (sm->initialize) {
  593. SM_ENTER_GLOBAL(CTRL_DIR, IN_OR_BOTH);
  594. return;
  595. }
  596. switch (sm->ctrl_dir_state) {
  597. case CTRL_DIR_FORCE_BOTH:
  598. if (sm->eap_if->portEnabled && sm->operEdge)
  599. SM_ENTER(CTRL_DIR, IN_OR_BOTH);
  600. break;
  601. case CTRL_DIR_IN_OR_BOTH:
  602. if (sm->operControlledDirections !=
  603. sm->adminControlledDirections)
  604. SM_ENTER(CTRL_DIR, IN_OR_BOTH);
  605. if (!sm->eap_if->portEnabled || !sm->operEdge)
  606. SM_ENTER(CTRL_DIR, FORCE_BOTH);
  607. break;
  608. }
  609. }
  610. struct eapol_state_machine *
  611. eapol_auth_alloc(struct eapol_authenticator *eapol, const u8 *addr,
  612. int flags, struct sta_info *sta)
  613. {
  614. struct eapol_state_machine *sm;
  615. struct hostapd_data *hapd; /* TODO: to be removed */
  616. struct eap_config eap_conf;
  617. if (eapol == NULL)
  618. return NULL;
  619. hapd = eapol->conf.hapd;
  620. sm = os_zalloc(sizeof(*sm));
  621. if (sm == NULL) {
  622. wpa_printf(MSG_DEBUG, "IEEE 802.1X state machine allocation "
  623. "failed");
  624. return NULL;
  625. }
  626. sm->radius_identifier = -1;
  627. os_memcpy(sm->addr, addr, ETH_ALEN);
  628. sm->flags = flags;
  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. eap_conf.peer_addr = addr;
  670. sm->eap = eap_server_sm_init(sm, &eapol_cb, &eap_conf);
  671. if (sm->eap == NULL) {
  672. eapol_auth_free(sm);
  673. return NULL;
  674. }
  675. sm->eap_if = eap_get_interface(sm->eap);
  676. eapol_auth_initialize(sm);
  677. return sm;
  678. }
  679. void eapol_auth_free(struct eapol_state_machine *sm)
  680. {
  681. if (sm == NULL)
  682. return;
  683. eloop_cancel_timeout(eapol_port_timers_tick, NULL, sm);
  684. eloop_cancel_timeout(eapol_sm_step_cb, sm, NULL);
  685. if (sm->eap)
  686. eap_server_sm_deinit(sm->eap);
  687. os_free(sm);
  688. }
  689. static int eapol_sm_sta_entry_alive(struct eapol_authenticator *eapol,
  690. const u8 *addr)
  691. {
  692. return eapol->cb.sta_entry_alive(eapol->conf.hapd, addr);
  693. }
  694. static void eapol_sm_step_run(struct eapol_state_machine *sm)
  695. {
  696. struct eapol_authenticator *eapol = sm->eapol;
  697. u8 addr[ETH_ALEN];
  698. unsigned int prev_auth_pae, prev_be_auth, prev_reauth_timer,
  699. prev_auth_key_tx, prev_key_rx, prev_ctrl_dir;
  700. int max_steps = 100;
  701. os_memcpy(addr, sm->addr, ETH_ALEN);
  702. /*
  703. * Allow EAPOL state machines to run as long as there are state
  704. * changes, but exit and return here through event loop if more than
  705. * 100 steps is needed as a precaution against infinite loops inside
  706. * eloop callback.
  707. */
  708. restart:
  709. 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. SM_STEP_RUN(AUTH_PAE);
  716. if (sm->initializing || eapol_sm_sta_entry_alive(eapol, addr))
  717. SM_STEP_RUN(BE_AUTH);
  718. if (sm->initializing || eapol_sm_sta_entry_alive(eapol, addr))
  719. SM_STEP_RUN(REAUTH_TIMER);
  720. if (sm->initializing || eapol_sm_sta_entry_alive(eapol, addr))
  721. SM_STEP_RUN(AUTH_KEY_TX);
  722. if (sm->initializing || eapol_sm_sta_entry_alive(eapol, addr))
  723. SM_STEP_RUN(KEY_RX);
  724. if (sm->initializing || eapol_sm_sta_entry_alive(eapol, addr))
  725. SM_STEP_RUN(CTRL_DIR);
  726. if (prev_auth_pae != sm->auth_pae_state ||
  727. prev_be_auth != sm->be_auth_state ||
  728. prev_reauth_timer != sm->reauth_timer_state ||
  729. prev_auth_key_tx != sm->auth_key_tx_state ||
  730. prev_key_rx != sm->key_rx_state ||
  731. prev_ctrl_dir != sm->ctrl_dir_state) {
  732. if (--max_steps > 0)
  733. goto restart;
  734. /* Re-run from eloop timeout */
  735. eapol_auth_step(sm);
  736. return;
  737. }
  738. if (eapol_sm_sta_entry_alive(eapol, addr) && sm->eap) {
  739. if (eap_server_sm_step(sm->eap)) {
  740. if (--max_steps > 0)
  741. goto restart;
  742. /* Re-run from eloop timeout */
  743. eapol_auth_step(sm);
  744. return;
  745. }
  746. /* TODO: find a better location for this */
  747. if (sm->eap_if->aaaEapResp) {
  748. sm->eap_if->aaaEapResp = FALSE;
  749. if (sm->eap_if->aaaEapRespData == NULL) {
  750. wpa_printf(MSG_DEBUG, "EAPOL: aaaEapResp set, "
  751. "but no aaaEapRespData available");
  752. return;
  753. }
  754. sm->eapol->cb.aaa_send(
  755. sm->hapd, sm->sta,
  756. wpabuf_head(sm->eap_if->aaaEapRespData),
  757. wpabuf_len(sm->eap_if->aaaEapRespData));
  758. }
  759. }
  760. if (eapol_sm_sta_entry_alive(eapol, addr))
  761. sm->eapol->cb.eapol_event(sm->hapd, sm->sta,
  762. EAPOL_AUTH_SM_CHANGE);
  763. }
  764. static void eapol_sm_step_cb(void *eloop_ctx, void *timeout_ctx)
  765. {
  766. struct eapol_state_machine *sm = eloop_ctx;
  767. eapol_sm_step_run(sm);
  768. }
  769. /**
  770. * eapol_auth_step - Advance EAPOL state machines
  771. * @sm: EAPOL state machine
  772. *
  773. * This function is called to advance EAPOL state machines after any change
  774. * that could affect their state.
  775. */
  776. void eapol_auth_step(struct eapol_state_machine *sm)
  777. {
  778. /*
  779. * Run eapol_sm_step_run from a registered timeout to make sure that
  780. * other possible timeouts/events are processed and to avoid long
  781. * function call chains.
  782. */
  783. eloop_register_timeout(0, 0, eapol_sm_step_cb, sm, NULL);
  784. }
  785. static void eapol_auth_initialize(struct eapol_state_machine *sm)
  786. {
  787. sm->initializing = TRUE;
  788. /* Initialize the state machines by asserting initialize and then
  789. * deasserting it after one step */
  790. sm->initialize = TRUE;
  791. eapol_sm_step_run(sm);
  792. sm->initialize = FALSE;
  793. eapol_sm_step_run(sm);
  794. sm->initializing = FALSE;
  795. /* Start one second tick for port timers state machine */
  796. eloop_cancel_timeout(eapol_port_timers_tick, NULL, sm);
  797. eloop_register_timeout(1, 0, eapol_port_timers_tick, NULL, sm);
  798. }
  799. #ifdef HOSTAPD_DUMP_STATE
  800. static inline const char * port_type_txt(PortTypes pt)
  801. {
  802. switch (pt) {
  803. case ForceUnauthorized: return "ForceUnauthorized";
  804. case ForceAuthorized: return "ForceAuthorized";
  805. case Auto: return "Auto";
  806. default: return "Unknown";
  807. }
  808. }
  809. static inline const char * port_state_txt(PortState ps)
  810. {
  811. switch (ps) {
  812. case Unauthorized: return "Unauthorized";
  813. case Authorized: return "Authorized";
  814. default: return "Unknown";
  815. }
  816. }
  817. static inline const char * ctrl_dir_txt(ControlledDirection dir)
  818. {
  819. switch (dir) {
  820. case Both: return "Both";
  821. case In: return "In";
  822. default: return "Unknown";
  823. }
  824. }
  825. static inline const char * auth_pae_state_txt(int s)
  826. {
  827. switch (s) {
  828. case AUTH_PAE_INITIALIZE: return "INITIALIZE";
  829. case AUTH_PAE_DISCONNECTED: return "DISCONNECTED";
  830. case AUTH_PAE_CONNECTING: return "CONNECTING";
  831. case AUTH_PAE_AUTHENTICATING: return "AUTHENTICATING";
  832. case AUTH_PAE_AUTHENTICATED: return "AUTHENTICATED";
  833. case AUTH_PAE_ABORTING: return "ABORTING";
  834. case AUTH_PAE_HELD: return "HELD";
  835. case AUTH_PAE_FORCE_AUTH: return "FORCE_AUTH";
  836. case AUTH_PAE_FORCE_UNAUTH: return "FORCE_UNAUTH";
  837. case AUTH_PAE_RESTART: return "RESTART";
  838. default: return "Unknown";
  839. }
  840. }
  841. static inline const char * be_auth_state_txt(int s)
  842. {
  843. switch (s) {
  844. case BE_AUTH_REQUEST: return "REQUEST";
  845. case BE_AUTH_RESPONSE: return "RESPONSE";
  846. case BE_AUTH_SUCCESS: return "SUCCESS";
  847. case BE_AUTH_FAIL: return "FAIL";
  848. case BE_AUTH_TIMEOUT: return "TIMEOUT";
  849. case BE_AUTH_IDLE: return "IDLE";
  850. case BE_AUTH_INITIALIZE: return "INITIALIZE";
  851. case BE_AUTH_IGNORE: return "IGNORE";
  852. default: return "Unknown";
  853. }
  854. }
  855. static inline const char * reauth_timer_state_txt(int s)
  856. {
  857. switch (s) {
  858. case REAUTH_TIMER_INITIALIZE: return "INITIALIZE";
  859. case REAUTH_TIMER_REAUTHENTICATE: return "REAUTHENTICATE";
  860. default: return "Unknown";
  861. }
  862. }
  863. static inline const char * auth_key_tx_state_txt(int s)
  864. {
  865. switch (s) {
  866. case AUTH_KEY_TX_NO_KEY_TRANSMIT: return "NO_KEY_TRANSMIT";
  867. case AUTH_KEY_TX_KEY_TRANSMIT: return "KEY_TRANSMIT";
  868. default: return "Unknown";
  869. }
  870. }
  871. static inline const char * key_rx_state_txt(int s)
  872. {
  873. switch (s) {
  874. case KEY_RX_NO_KEY_RECEIVE: return "NO_KEY_RECEIVE";
  875. case KEY_RX_KEY_RECEIVE: return "KEY_RECEIVE";
  876. default: return "Unknown";
  877. }
  878. }
  879. static inline const char * ctrl_dir_state_txt(int s)
  880. {
  881. switch (s) {
  882. case CTRL_DIR_FORCE_BOTH: return "FORCE_BOTH";
  883. case CTRL_DIR_IN_OR_BOTH: return "IN_OR_BOTH";
  884. default: return "Unknown";
  885. }
  886. }
  887. void eapol_auth_dump_state(FILE *f, const char *prefix,
  888. struct eapol_state_machine *sm)
  889. {
  890. fprintf(f, "%sEAPOL state machine:\n", prefix);
  891. fprintf(f, "%s aWhile=%d quietWhile=%d reAuthWhen=%d\n", prefix,
  892. sm->aWhile, sm->quietWhile, sm->reAuthWhen);
  893. #define _SB(b) ((b) ? "TRUE" : "FALSE")
  894. fprintf(f,
  895. "%s authAbort=%s authFail=%s authPortStatus=%s authStart=%s\n"
  896. "%s authTimeout=%s authSuccess=%s eapFail=%s eapolEap=%s\n"
  897. "%s eapSuccess=%s eapTimeout=%s initialize=%s "
  898. "keyAvailable=%s\n"
  899. "%s keyDone=%s keyRun=%s keyTxEnabled=%s portControl=%s\n"
  900. "%s portEnabled=%s portValid=%s reAuthenticate=%s\n",
  901. prefix, _SB(sm->authAbort), _SB(sm->authFail),
  902. port_state_txt(sm->authPortStatus), _SB(sm->authStart),
  903. prefix, _SB(sm->authTimeout), _SB(sm->authSuccess),
  904. _SB(sm->eap_if->eapFail), _SB(sm->eapolEap),
  905. prefix, _SB(sm->eap_if->eapSuccess),
  906. _SB(sm->eap_if->eapTimeout),
  907. _SB(sm->initialize), _SB(sm->eap_if->eapKeyAvailable),
  908. prefix, _SB(sm->keyDone), _SB(sm->keyRun),
  909. _SB(sm->keyTxEnabled), port_type_txt(sm->portControl),
  910. prefix, _SB(sm->eap_if->portEnabled), _SB(sm->portValid),
  911. _SB(sm->reAuthenticate));
  912. fprintf(f, "%s Authenticator PAE:\n"
  913. "%s state=%s\n"
  914. "%s eapolLogoff=%s eapolStart=%s eapRestart=%s\n"
  915. "%s portMode=%s reAuthCount=%d\n"
  916. "%s quietPeriod=%d reAuthMax=%d\n"
  917. "%s authEntersConnecting=%d\n"
  918. "%s authEapLogoffsWhileConnecting=%d\n"
  919. "%s authEntersAuthenticating=%d\n"
  920. "%s authAuthSuccessesWhileAuthenticating=%d\n"
  921. "%s authAuthTimeoutsWhileAuthenticating=%d\n"
  922. "%s authAuthFailWhileAuthenticating=%d\n"
  923. "%s authAuthEapStartsWhileAuthenticating=%d\n"
  924. "%s authAuthEapLogoffWhileAuthenticating=%d\n"
  925. "%s authAuthReauthsWhileAuthenticated=%d\n"
  926. "%s authAuthEapStartsWhileAuthenticated=%d\n"
  927. "%s authAuthEapLogoffWhileAuthenticated=%d\n",
  928. prefix, prefix, auth_pae_state_txt(sm->auth_pae_state), prefix,
  929. _SB(sm->eapolLogoff), _SB(sm->eapolStart),
  930. _SB(sm->eap_if->eapRestart),
  931. prefix, port_type_txt(sm->portMode), sm->reAuthCount,
  932. prefix, sm->quietPeriod, sm->reAuthMax,
  933. prefix, sm->authEntersConnecting,
  934. prefix, sm->authEapLogoffsWhileConnecting,
  935. prefix, sm->authEntersAuthenticating,
  936. prefix, sm->authAuthSuccessesWhileAuthenticating,
  937. prefix, sm->authAuthTimeoutsWhileAuthenticating,
  938. prefix, sm->authAuthFailWhileAuthenticating,
  939. prefix, sm->authAuthEapStartsWhileAuthenticating,
  940. prefix, sm->authAuthEapLogoffWhileAuthenticating,
  941. prefix, sm->authAuthReauthsWhileAuthenticated,
  942. prefix, sm->authAuthEapStartsWhileAuthenticated,
  943. prefix, sm->authAuthEapLogoffWhileAuthenticated);
  944. fprintf(f, "%s Backend Authentication:\n"
  945. "%s state=%s\n"
  946. "%s eapNoReq=%s eapReq=%s eapResp=%s\n"
  947. "%s serverTimeout=%d\n"
  948. "%s backendResponses=%d\n"
  949. "%s backendAccessChallenges=%d\n"
  950. "%s backendOtherRequestsToSupplicant=%d\n"
  951. "%s backendAuthSuccesses=%d\n"
  952. "%s backendAuthFails=%d\n",
  953. prefix, prefix,
  954. be_auth_state_txt(sm->be_auth_state),
  955. prefix, _SB(sm->eap_if->eapNoReq), _SB(sm->eap_if->eapReq),
  956. _SB(sm->eap_if->eapResp),
  957. prefix, sm->serverTimeout,
  958. prefix, sm->backendResponses,
  959. prefix, sm->backendAccessChallenges,
  960. prefix, sm->backendOtherRequestsToSupplicant,
  961. prefix, sm->backendAuthSuccesses,
  962. prefix, sm->backendAuthFails);
  963. fprintf(f, "%s Reauthentication Timer:\n"
  964. "%s state=%s\n"
  965. "%s reAuthPeriod=%d reAuthEnabled=%s\n", prefix, prefix,
  966. reauth_timer_state_txt(sm->reauth_timer_state), prefix,
  967. sm->reAuthPeriod, _SB(sm->reAuthEnabled));
  968. fprintf(f, "%s Authenticator Key Transmit:\n"
  969. "%s state=%s\n", prefix, prefix,
  970. auth_key_tx_state_txt(sm->auth_key_tx_state));
  971. fprintf(f, "%s Key Receive:\n"
  972. "%s state=%s\n"
  973. "%s rxKey=%s\n", prefix, prefix,
  974. key_rx_state_txt(sm->key_rx_state), prefix, _SB(sm->rxKey));
  975. fprintf(f, "%s Controlled Directions:\n"
  976. "%s state=%s\n"
  977. "%s adminControlledDirections=%s "
  978. "operControlledDirections=%s\n"
  979. "%s operEdge=%s\n", prefix, prefix,
  980. ctrl_dir_state_txt(sm->ctrl_dir_state),
  981. prefix, ctrl_dir_txt(sm->adminControlledDirections),
  982. ctrl_dir_txt(sm->operControlledDirections),
  983. prefix, _SB(sm->operEdge));
  984. #undef _SB
  985. }
  986. #endif /* HOSTAPD_DUMP_STATE */
  987. static int eapol_sm_get_eap_user(void *ctx, const u8 *identity,
  988. size_t identity_len, int phase2,
  989. struct eap_user *user)
  990. {
  991. struct eapol_state_machine *sm = ctx;
  992. return sm->eapol->cb.get_eap_user(sm->hapd, identity, identity_len,
  993. phase2, user);
  994. }
  995. static const char * eapol_sm_get_eap_req_id_text(void *ctx, size_t *len)
  996. {
  997. struct eapol_state_machine *sm = ctx;
  998. *len = sm->eapol->conf.eap_req_id_text_len;
  999. return sm->eapol->conf.eap_req_id_text;
  1000. }
  1001. static struct eapol_callbacks eapol_cb =
  1002. {
  1003. .get_eap_user = eapol_sm_get_eap_user,
  1004. .get_eap_req_id_text = eapol_sm_get_eap_req_id_text,
  1005. };
  1006. int eapol_auth_eap_pending_cb(struct eapol_state_machine *sm, void *ctx)
  1007. {
  1008. if (sm == NULL || ctx != sm->eap)
  1009. return -1;
  1010. eap_sm_pending_cb(sm->eap);
  1011. eapol_auth_step(sm);
  1012. return 0;
  1013. }
  1014. static int eapol_auth_conf_clone(struct eapol_auth_config *dst,
  1015. struct eapol_auth_config *src)
  1016. {
  1017. dst->hapd = src->hapd;
  1018. dst->eap_reauth_period = src->eap_reauth_period;
  1019. dst->wpa = src->wpa;
  1020. dst->individual_wep_key_len = src->individual_wep_key_len;
  1021. dst->eap_server = src->eap_server;
  1022. dst->ssl_ctx = src->ssl_ctx;
  1023. dst->eap_sim_db_priv = src->eap_sim_db_priv;
  1024. os_free(dst->eap_req_id_text);
  1025. if (src->eap_req_id_text) {
  1026. dst->eap_req_id_text = os_malloc(src->eap_req_id_text_len);
  1027. if (dst->eap_req_id_text == NULL)
  1028. return -1;
  1029. os_memcpy(dst->eap_req_id_text, src->eap_req_id_text,
  1030. src->eap_req_id_text_len);
  1031. dst->eap_req_id_text_len = src->eap_req_id_text_len;
  1032. } else {
  1033. dst->eap_req_id_text = NULL;
  1034. dst->eap_req_id_text_len = 0;
  1035. }
  1036. if (src->pac_opaque_encr_key) {
  1037. dst->pac_opaque_encr_key = os_malloc(16);
  1038. os_memcpy(dst->pac_opaque_encr_key, src->pac_opaque_encr_key,
  1039. 16);
  1040. } else
  1041. dst->pac_opaque_encr_key = NULL;
  1042. if (src->eap_fast_a_id) {
  1043. dst->eap_fast_a_id = os_malloc(src->eap_fast_a_id_len);
  1044. if (dst->eap_fast_a_id == NULL) {
  1045. os_free(dst->eap_req_id_text);
  1046. return -1;
  1047. }
  1048. os_memcpy(dst->eap_fast_a_id, src->eap_fast_a_id,
  1049. src->eap_fast_a_id_len);
  1050. dst->eap_fast_a_id_len = src->eap_fast_a_id_len;
  1051. } else
  1052. dst->eap_fast_a_id = NULL;
  1053. if (src->eap_fast_a_id_info) {
  1054. dst->eap_fast_a_id_info = os_strdup(src->eap_fast_a_id_info);
  1055. if (dst->eap_fast_a_id_info == NULL) {
  1056. os_free(dst->eap_req_id_text);
  1057. os_free(dst->eap_fast_a_id);
  1058. return -1;
  1059. }
  1060. } else
  1061. dst->eap_fast_a_id_info = NULL;
  1062. dst->eap_fast_prov = src->eap_fast_prov;
  1063. dst->pac_key_lifetime = src->pac_key_lifetime;
  1064. dst->pac_key_refresh_time = src->pac_key_refresh_time;
  1065. dst->eap_sim_aka_result_ind = src->eap_sim_aka_result_ind;
  1066. dst->tnc = src->tnc;
  1067. dst->wps = src->wps;
  1068. return 0;
  1069. }
  1070. static void eapol_auth_conf_free(struct eapol_auth_config *conf)
  1071. {
  1072. os_free(conf->eap_req_id_text);
  1073. conf->eap_req_id_text = NULL;
  1074. os_free(conf->pac_opaque_encr_key);
  1075. conf->pac_opaque_encr_key = NULL;
  1076. os_free(conf->eap_fast_a_id);
  1077. conf->eap_fast_a_id = NULL;
  1078. os_free(conf->eap_fast_a_id_info);
  1079. conf->eap_fast_a_id_info = NULL;
  1080. }
  1081. struct eapol_authenticator * eapol_auth_init(struct eapol_auth_config *conf,
  1082. struct eapol_auth_cb *cb)
  1083. {
  1084. struct eapol_authenticator *eapol;
  1085. eapol = os_zalloc(sizeof(*eapol));
  1086. if (eapol == NULL)
  1087. return NULL;
  1088. if (eapol_auth_conf_clone(&eapol->conf, conf) < 0) {
  1089. os_free(eapol);
  1090. return NULL;
  1091. }
  1092. if (conf->individual_wep_key_len > 0) {
  1093. /* use key0 in individual key and key1 in broadcast key */
  1094. eapol->default_wep_key_idx = 1;
  1095. }
  1096. eapol->cb.eapol_send = cb->eapol_send;
  1097. eapol->cb.aaa_send = cb->aaa_send;
  1098. eapol->cb.finished = cb->finished;
  1099. eapol->cb.get_eap_user = cb->get_eap_user;
  1100. eapol->cb.sta_entry_alive = cb->sta_entry_alive;
  1101. eapol->cb.logger = cb->logger;
  1102. eapol->cb.set_port_authorized = cb->set_port_authorized;
  1103. eapol->cb.abort_auth = cb->abort_auth;
  1104. eapol->cb.tx_key = cb->tx_key;
  1105. return eapol;
  1106. }
  1107. void eapol_auth_deinit(struct eapol_authenticator *eapol)
  1108. {
  1109. if (eapol == NULL)
  1110. return;
  1111. eapol_auth_conf_free(&eapol->conf);
  1112. os_free(eapol->default_wep_key);
  1113. os_free(eapol);
  1114. }