eapol_sm.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  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. 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. wpa_auth_sm_notify(sm->sta->wpa_sm);
  762. }
  763. static void eapol_sm_step_cb(void *eloop_ctx, void *timeout_ctx)
  764. {
  765. struct eapol_state_machine *sm = eloop_ctx;
  766. eapol_sm_step_run(sm);
  767. }
  768. /**
  769. * eapol_auth_step - Advance EAPOL state machines
  770. * @sm: EAPOL state machine
  771. *
  772. * This function is called to advance EAPOL state machines after any change
  773. * that could affect their state.
  774. */
  775. void eapol_auth_step(struct eapol_state_machine *sm)
  776. {
  777. /*
  778. * Run eapol_sm_step_run from a registered timeout to make sure that
  779. * other possible timeouts/events are processed and to avoid long
  780. * function call chains.
  781. */
  782. eloop_register_timeout(0, 0, eapol_sm_step_cb, sm, NULL);
  783. }
  784. void eapol_auth_initialize(struct eapol_state_machine *sm)
  785. {
  786. sm->initializing = TRUE;
  787. /* Initialize the state machines by asserting initialize and then
  788. * deasserting it after one step */
  789. sm->initialize = TRUE;
  790. eapol_sm_step_run(sm);
  791. sm->initialize = FALSE;
  792. eapol_sm_step_run(sm);
  793. sm->initializing = FALSE;
  794. /* Start one second tick for port timers state machine */
  795. eloop_cancel_timeout(eapol_port_timers_tick, NULL, sm);
  796. eloop_register_timeout(1, 0, eapol_port_timers_tick, NULL, sm);
  797. }
  798. #ifdef HOSTAPD_DUMP_STATE
  799. static inline const char * port_type_txt(PortTypes pt)
  800. {
  801. switch (pt) {
  802. case ForceUnauthorized: return "ForceUnauthorized";
  803. case ForceAuthorized: return "ForceAuthorized";
  804. case Auto: return "Auto";
  805. default: return "Unknown";
  806. }
  807. }
  808. static inline const char * port_state_txt(PortState ps)
  809. {
  810. switch (ps) {
  811. case Unauthorized: return "Unauthorized";
  812. case Authorized: return "Authorized";
  813. default: return "Unknown";
  814. }
  815. }
  816. static inline const char * ctrl_dir_txt(ControlledDirection dir)
  817. {
  818. switch (dir) {
  819. case Both: return "Both";
  820. case In: return "In";
  821. default: return "Unknown";
  822. }
  823. }
  824. static inline const char * auth_pae_state_txt(int s)
  825. {
  826. switch (s) {
  827. case AUTH_PAE_INITIALIZE: return "INITIALIZE";
  828. case AUTH_PAE_DISCONNECTED: return "DISCONNECTED";
  829. case AUTH_PAE_CONNECTING: return "CONNECTING";
  830. case AUTH_PAE_AUTHENTICATING: return "AUTHENTICATING";
  831. case AUTH_PAE_AUTHENTICATED: return "AUTHENTICATED";
  832. case AUTH_PAE_ABORTING: return "ABORTING";
  833. case AUTH_PAE_HELD: return "HELD";
  834. case AUTH_PAE_FORCE_AUTH: return "FORCE_AUTH";
  835. case AUTH_PAE_FORCE_UNAUTH: return "FORCE_UNAUTH";
  836. case AUTH_PAE_RESTART: return "RESTART";
  837. default: return "Unknown";
  838. }
  839. }
  840. static inline const char * be_auth_state_txt(int s)
  841. {
  842. switch (s) {
  843. case BE_AUTH_REQUEST: return "REQUEST";
  844. case BE_AUTH_RESPONSE: return "RESPONSE";
  845. case BE_AUTH_SUCCESS: return "SUCCESS";
  846. case BE_AUTH_FAIL: return "FAIL";
  847. case BE_AUTH_TIMEOUT: return "TIMEOUT";
  848. case BE_AUTH_IDLE: return "IDLE";
  849. case BE_AUTH_INITIALIZE: return "INITIALIZE";
  850. case BE_AUTH_IGNORE: return "IGNORE";
  851. default: return "Unknown";
  852. }
  853. }
  854. static inline const char * reauth_timer_state_txt(int s)
  855. {
  856. switch (s) {
  857. case REAUTH_TIMER_INITIALIZE: return "INITIALIZE";
  858. case REAUTH_TIMER_REAUTHENTICATE: return "REAUTHENTICATE";
  859. default: return "Unknown";
  860. }
  861. }
  862. static inline const char * auth_key_tx_state_txt(int s)
  863. {
  864. switch (s) {
  865. case AUTH_KEY_TX_NO_KEY_TRANSMIT: return "NO_KEY_TRANSMIT";
  866. case AUTH_KEY_TX_KEY_TRANSMIT: return "KEY_TRANSMIT";
  867. default: return "Unknown";
  868. }
  869. }
  870. static inline const char * key_rx_state_txt(int s)
  871. {
  872. switch (s) {
  873. case KEY_RX_NO_KEY_RECEIVE: return "NO_KEY_RECEIVE";
  874. case KEY_RX_KEY_RECEIVE: return "KEY_RECEIVE";
  875. default: return "Unknown";
  876. }
  877. }
  878. static inline const char * ctrl_dir_state_txt(int s)
  879. {
  880. switch (s) {
  881. case CTRL_DIR_FORCE_BOTH: return "FORCE_BOTH";
  882. case CTRL_DIR_IN_OR_BOTH: return "IN_OR_BOTH";
  883. default: return "Unknown";
  884. }
  885. }
  886. void eapol_auth_dump_state(FILE *f, const char *prefix,
  887. struct eapol_state_machine *sm)
  888. {
  889. fprintf(f, "%sEAPOL state machine:\n", prefix);
  890. fprintf(f, "%s aWhile=%d quietWhile=%d reAuthWhen=%d\n", prefix,
  891. sm->aWhile, sm->quietWhile, sm->reAuthWhen);
  892. #define _SB(b) ((b) ? "TRUE" : "FALSE")
  893. fprintf(f,
  894. "%s authAbort=%s authFail=%s authPortStatus=%s authStart=%s\n"
  895. "%s authTimeout=%s authSuccess=%s eapFail=%s eapolEap=%s\n"
  896. "%s eapSuccess=%s eapTimeout=%s initialize=%s "
  897. "keyAvailable=%s\n"
  898. "%s keyDone=%s keyRun=%s keyTxEnabled=%s portControl=%s\n"
  899. "%s portEnabled=%s portValid=%s reAuthenticate=%s\n",
  900. prefix, _SB(sm->authAbort), _SB(sm->authFail),
  901. port_state_txt(sm->authPortStatus), _SB(sm->authStart),
  902. prefix, _SB(sm->authTimeout), _SB(sm->authSuccess),
  903. _SB(sm->eap_if->eapFail), _SB(sm->eapolEap),
  904. prefix, _SB(sm->eap_if->eapSuccess),
  905. _SB(sm->eap_if->eapTimeout),
  906. _SB(sm->initialize), _SB(sm->eap_if->eapKeyAvailable),
  907. prefix, _SB(sm->keyDone), _SB(sm->keyRun),
  908. _SB(sm->keyTxEnabled), port_type_txt(sm->portControl),
  909. prefix, _SB(sm->eap_if->portEnabled), _SB(sm->portValid),
  910. _SB(sm->reAuthenticate));
  911. fprintf(f, "%s Authenticator PAE:\n"
  912. "%s state=%s\n"
  913. "%s eapolLogoff=%s eapolStart=%s eapRestart=%s\n"
  914. "%s portMode=%s reAuthCount=%d\n"
  915. "%s quietPeriod=%d reAuthMax=%d\n"
  916. "%s authEntersConnecting=%d\n"
  917. "%s authEapLogoffsWhileConnecting=%d\n"
  918. "%s authEntersAuthenticating=%d\n"
  919. "%s authAuthSuccessesWhileAuthenticating=%d\n"
  920. "%s authAuthTimeoutsWhileAuthenticating=%d\n"
  921. "%s authAuthFailWhileAuthenticating=%d\n"
  922. "%s authAuthEapStartsWhileAuthenticating=%d\n"
  923. "%s authAuthEapLogoffWhileAuthenticating=%d\n"
  924. "%s authAuthReauthsWhileAuthenticated=%d\n"
  925. "%s authAuthEapStartsWhileAuthenticated=%d\n"
  926. "%s authAuthEapLogoffWhileAuthenticated=%d\n",
  927. prefix, prefix, auth_pae_state_txt(sm->auth_pae_state), prefix,
  928. _SB(sm->eapolLogoff), _SB(sm->eapolStart),
  929. _SB(sm->eap_if->eapRestart),
  930. prefix, port_type_txt(sm->portMode), sm->reAuthCount,
  931. prefix, sm->quietPeriod, sm->reAuthMax,
  932. prefix, sm->authEntersConnecting,
  933. prefix, sm->authEapLogoffsWhileConnecting,
  934. prefix, sm->authEntersAuthenticating,
  935. prefix, sm->authAuthSuccessesWhileAuthenticating,
  936. prefix, sm->authAuthTimeoutsWhileAuthenticating,
  937. prefix, sm->authAuthFailWhileAuthenticating,
  938. prefix, sm->authAuthEapStartsWhileAuthenticating,
  939. prefix, sm->authAuthEapLogoffWhileAuthenticating,
  940. prefix, sm->authAuthReauthsWhileAuthenticated,
  941. prefix, sm->authAuthEapStartsWhileAuthenticated,
  942. prefix, sm->authAuthEapLogoffWhileAuthenticated);
  943. fprintf(f, "%s Backend Authentication:\n"
  944. "%s state=%s\n"
  945. "%s eapNoReq=%s eapReq=%s eapResp=%s\n"
  946. "%s serverTimeout=%d\n"
  947. "%s backendResponses=%d\n"
  948. "%s backendAccessChallenges=%d\n"
  949. "%s backendOtherRequestsToSupplicant=%d\n"
  950. "%s backendAuthSuccesses=%d\n"
  951. "%s backendAuthFails=%d\n",
  952. prefix, prefix,
  953. be_auth_state_txt(sm->be_auth_state),
  954. prefix, _SB(sm->eap_if->eapNoReq), _SB(sm->eap_if->eapReq),
  955. _SB(sm->eap_if->eapResp),
  956. prefix, sm->serverTimeout,
  957. prefix, sm->backendResponses,
  958. prefix, sm->backendAccessChallenges,
  959. prefix, sm->backendOtherRequestsToSupplicant,
  960. prefix, sm->backendAuthSuccesses,
  961. prefix, sm->backendAuthFails);
  962. fprintf(f, "%s Reauthentication Timer:\n"
  963. "%s state=%s\n"
  964. "%s reAuthPeriod=%d reAuthEnabled=%s\n", prefix, prefix,
  965. reauth_timer_state_txt(sm->reauth_timer_state), prefix,
  966. sm->reAuthPeriod, _SB(sm->reAuthEnabled));
  967. fprintf(f, "%s Authenticator Key Transmit:\n"
  968. "%s state=%s\n", prefix, prefix,
  969. auth_key_tx_state_txt(sm->auth_key_tx_state));
  970. fprintf(f, "%s Key Receive:\n"
  971. "%s state=%s\n"
  972. "%s rxKey=%s\n", prefix, prefix,
  973. key_rx_state_txt(sm->key_rx_state), prefix, _SB(sm->rxKey));
  974. fprintf(f, "%s Controlled Directions:\n"
  975. "%s state=%s\n"
  976. "%s adminControlledDirections=%s "
  977. "operControlledDirections=%s\n"
  978. "%s operEdge=%s\n", prefix, prefix,
  979. ctrl_dir_state_txt(sm->ctrl_dir_state),
  980. prefix, ctrl_dir_txt(sm->adminControlledDirections),
  981. ctrl_dir_txt(sm->operControlledDirections),
  982. prefix, _SB(sm->operEdge));
  983. #undef _SB
  984. }
  985. #endif /* HOSTAPD_DUMP_STATE */
  986. static int eapol_sm_get_eap_user(void *ctx, const u8 *identity,
  987. size_t identity_len, int phase2,
  988. struct eap_user *user)
  989. {
  990. struct eapol_state_machine *sm = ctx;
  991. return sm->eapol->cb.get_eap_user(sm->hapd, identity, identity_len,
  992. phase2, user);
  993. }
  994. static const char * eapol_sm_get_eap_req_id_text(void *ctx, size_t *len)
  995. {
  996. struct eapol_state_machine *sm = ctx;
  997. *len = sm->eapol->conf.eap_req_id_text_len;
  998. return sm->eapol->conf.eap_req_id_text;
  999. }
  1000. static struct eapol_callbacks eapol_cb =
  1001. {
  1002. .get_eap_user = eapol_sm_get_eap_user,
  1003. .get_eap_req_id_text = eapol_sm_get_eap_req_id_text,
  1004. };
  1005. int eapol_auth_eap_pending_cb(struct eapol_state_machine *sm, void *ctx)
  1006. {
  1007. if (sm == NULL || ctx != sm->eap)
  1008. return -1;
  1009. eap_sm_pending_cb(sm->eap);
  1010. eapol_auth_step(sm);
  1011. return 0;
  1012. }
  1013. static int eapol_auth_conf_clone(struct eapol_auth_config *dst,
  1014. struct eapol_auth_config *src)
  1015. {
  1016. dst->hapd = src->hapd;
  1017. dst->eap_reauth_period = src->eap_reauth_period;
  1018. dst->wpa = src->wpa;
  1019. dst->individual_wep_key_len = src->individual_wep_key_len;
  1020. dst->eap_server = src->eap_server;
  1021. dst->ssl_ctx = src->ssl_ctx;
  1022. dst->eap_sim_db_priv = src->eap_sim_db_priv;
  1023. os_free(dst->eap_req_id_text);
  1024. if (src->eap_req_id_text) {
  1025. dst->eap_req_id_text = os_malloc(src->eap_req_id_text_len);
  1026. if (dst->eap_req_id_text == NULL)
  1027. return -1;
  1028. os_memcpy(dst->eap_req_id_text, src->eap_req_id_text,
  1029. src->eap_req_id_text_len);
  1030. dst->eap_req_id_text_len = src->eap_req_id_text_len;
  1031. } else {
  1032. dst->eap_req_id_text = NULL;
  1033. dst->eap_req_id_text_len = 0;
  1034. }
  1035. if (src->pac_opaque_encr_key) {
  1036. dst->pac_opaque_encr_key = os_malloc(16);
  1037. os_memcpy(dst->pac_opaque_encr_key, src->pac_opaque_encr_key,
  1038. 16);
  1039. } else
  1040. dst->pac_opaque_encr_key = NULL;
  1041. if (src->eap_fast_a_id) {
  1042. dst->eap_fast_a_id = os_malloc(src->eap_fast_a_id_len);
  1043. if (dst->eap_fast_a_id == NULL) {
  1044. os_free(dst->eap_req_id_text);
  1045. return -1;
  1046. }
  1047. os_memcpy(dst->eap_fast_a_id, src->eap_fast_a_id,
  1048. src->eap_fast_a_id_len);
  1049. dst->eap_fast_a_id_len = src->eap_fast_a_id_len;
  1050. } else
  1051. dst->eap_fast_a_id = NULL;
  1052. if (src->eap_fast_a_id_info) {
  1053. dst->eap_fast_a_id_info = os_strdup(src->eap_fast_a_id_info);
  1054. if (dst->eap_fast_a_id_info == NULL) {
  1055. os_free(dst->eap_req_id_text);
  1056. os_free(dst->eap_fast_a_id);
  1057. return -1;
  1058. }
  1059. } else
  1060. dst->eap_fast_a_id_info = NULL;
  1061. dst->eap_fast_prov = src->eap_fast_prov;
  1062. dst->pac_key_lifetime = src->pac_key_lifetime;
  1063. dst->pac_key_refresh_time = src->pac_key_refresh_time;
  1064. dst->eap_sim_aka_result_ind = src->eap_sim_aka_result_ind;
  1065. dst->tnc = src->tnc;
  1066. dst->wps = src->wps;
  1067. return 0;
  1068. }
  1069. static void eapol_auth_conf_free(struct eapol_auth_config *conf)
  1070. {
  1071. os_free(conf->eap_req_id_text);
  1072. conf->eap_req_id_text = NULL;
  1073. os_free(conf->pac_opaque_encr_key);
  1074. conf->pac_opaque_encr_key = NULL;
  1075. os_free(conf->eap_fast_a_id);
  1076. conf->eap_fast_a_id = NULL;
  1077. os_free(conf->eap_fast_a_id_info);
  1078. conf->eap_fast_a_id_info = NULL;
  1079. }
  1080. struct eapol_authenticator * eapol_auth_init(struct eapol_auth_config *conf,
  1081. struct eapol_auth_cb *cb)
  1082. {
  1083. struct eapol_authenticator *eapol;
  1084. eapol = os_zalloc(sizeof(*eapol));
  1085. if (eapol == NULL)
  1086. return NULL;
  1087. if (eapol_auth_conf_clone(&eapol->conf, conf) < 0) {
  1088. os_free(eapol);
  1089. return NULL;
  1090. }
  1091. if (conf->individual_wep_key_len > 0) {
  1092. /* use key0 in individual key and key1 in broadcast key */
  1093. eapol->default_wep_key_idx = 1;
  1094. }
  1095. eapol->cb.eapol_send = cb->eapol_send;
  1096. eapol->cb.aaa_send = cb->aaa_send;
  1097. eapol->cb.finished = cb->finished;
  1098. eapol->cb.get_eap_user = cb->get_eap_user;
  1099. eapol->cb.sta_entry_alive = cb->sta_entry_alive;
  1100. eapol->cb.logger = cb->logger;
  1101. eapol->cb.set_port_authorized = cb->set_port_authorized;
  1102. eapol->cb.abort_auth = cb->abort_auth;
  1103. eapol->cb.tx_key = cb->tx_key;
  1104. return eapol;
  1105. }
  1106. void eapol_auth_deinit(struct eapol_authenticator *eapol)
  1107. {
  1108. if (eapol == NULL)
  1109. return;
  1110. eapol_auth_conf_free(&eapol->conf);
  1111. os_free(eapol->default_wep_key);
  1112. os_free(eapol);
  1113. }