wpa.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334
  1. /*
  2. * hostapd - IEEE 802.11i-2004 / WPA Authenticator
  3. * Copyright (c) 2004-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. #ifndef CONFIG_NATIVE_WINDOWS
  16. #include "common.h"
  17. #include "config.h"
  18. #include "eapol_sm.h"
  19. #include "wpa.h"
  20. #include "sha1.h"
  21. #include "sha256.h"
  22. #include "rc4.h"
  23. #include "aes_wrap.h"
  24. #include "crypto.h"
  25. #include "eloop.h"
  26. #include "ieee802_11.h"
  27. #include "pmksa_cache.h"
  28. #include "state_machine.h"
  29. #include "wpa_auth_i.h"
  30. #include "wpa_auth_ie.h"
  31. #define STATE_MACHINE_DATA struct wpa_state_machine
  32. #define STATE_MACHINE_DEBUG_PREFIX "WPA"
  33. #define STATE_MACHINE_ADDR sm->addr
  34. static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
  35. static void wpa_sm_step(struct wpa_state_machine *sm);
  36. static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len);
  37. static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
  38. static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
  39. struct wpa_group *group);
  40. /* Default timeouts are 100 ms, but this seems to be a bit too fast for most
  41. * WPA Supplicants, so use a bit longer timeout. */
  42. static const u32 dot11RSNAConfigGroupUpdateTimeOut = 1000; /* ms */
  43. static const u32 dot11RSNAConfigGroupUpdateCount = 3;
  44. static const u32 dot11RSNAConfigPairwiseUpdateTimeOut = 1000; /* ms */
  45. static const u32 dot11RSNAConfigPairwiseUpdateCount = 3;
  46. /* TODO: make these configurable */
  47. static const int dot11RSNAConfigPMKLifetime = 43200;
  48. static const int dot11RSNAConfigPMKReauthThreshold = 70;
  49. static const int dot11RSNAConfigSATimeout = 60;
  50. static inline void wpa_auth_mic_failure_report(
  51. struct wpa_authenticator *wpa_auth, const u8 *addr)
  52. {
  53. if (wpa_auth->cb.mic_failure_report)
  54. wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr);
  55. }
  56. static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
  57. const u8 *addr, wpa_eapol_variable var,
  58. int value)
  59. {
  60. if (wpa_auth->cb.set_eapol)
  61. wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value);
  62. }
  63. static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
  64. const u8 *addr, wpa_eapol_variable var)
  65. {
  66. if (wpa_auth->cb.get_eapol == NULL)
  67. return -1;
  68. return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var);
  69. }
  70. static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
  71. const u8 *addr, const u8 *prev_psk)
  72. {
  73. if (wpa_auth->cb.get_psk == NULL)
  74. return NULL;
  75. return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, prev_psk);
  76. }
  77. static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
  78. const u8 *addr, u8 *msk, size_t *len)
  79. {
  80. if (wpa_auth->cb.get_msk == NULL)
  81. return -1;
  82. return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len);
  83. }
  84. static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
  85. int vlan_id,
  86. const char *alg, const u8 *addr, int idx,
  87. u8 *key, size_t key_len)
  88. {
  89. if (wpa_auth->cb.set_key == NULL)
  90. return -1;
  91. return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
  92. key, key_len);
  93. }
  94. static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
  95. const u8 *addr, int idx, u8 *seq)
  96. {
  97. if (wpa_auth->cb.get_seqnum == NULL)
  98. return -1;
  99. return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
  100. }
  101. static inline int wpa_auth_get_seqnum_igtk(struct wpa_authenticator *wpa_auth,
  102. const u8 *addr, int idx, u8 *seq)
  103. {
  104. if (wpa_auth->cb.get_seqnum_igtk == NULL)
  105. return -1;
  106. return wpa_auth->cb.get_seqnum_igtk(wpa_auth->cb.ctx, addr, idx, seq);
  107. }
  108. static inline int
  109. wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
  110. const u8 *data, size_t data_len, int encrypt)
  111. {
  112. if (wpa_auth->cb.send_eapol == NULL)
  113. return -1;
  114. return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
  115. encrypt);
  116. }
  117. int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
  118. int (*cb)(struct wpa_state_machine *sm, void *ctx),
  119. void *cb_ctx)
  120. {
  121. if (wpa_auth->cb.for_each_sta == NULL)
  122. return 0;
  123. return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
  124. }
  125. int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
  126. int (*cb)(struct wpa_authenticator *a, void *ctx),
  127. void *cb_ctx)
  128. {
  129. if (wpa_auth->cb.for_each_auth == NULL)
  130. return 0;
  131. return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
  132. }
  133. void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
  134. logger_level level, const char *txt)
  135. {
  136. if (wpa_auth->cb.logger == NULL)
  137. return;
  138. wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
  139. }
  140. void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
  141. logger_level level, const char *fmt, ...)
  142. {
  143. char *format;
  144. int maxlen;
  145. va_list ap;
  146. if (wpa_auth->cb.logger == NULL)
  147. return;
  148. maxlen = os_strlen(fmt) + 100;
  149. format = os_malloc(maxlen);
  150. if (!format)
  151. return;
  152. va_start(ap, fmt);
  153. vsnprintf(format, maxlen, fmt, ap);
  154. va_end(ap);
  155. wpa_auth_logger(wpa_auth, addr, level, format);
  156. os_free(format);
  157. }
  158. static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
  159. const u8 *addr)
  160. {
  161. if (wpa_auth->cb.disconnect == NULL)
  162. return;
  163. wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
  164. WLAN_REASON_PREV_AUTH_NOT_VALID);
  165. }
  166. static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
  167. {
  168. int ret = 0;
  169. #ifdef CONFIG_IEEE80211R
  170. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
  171. ret = 1;
  172. #endif /* CONFIG_IEEE80211R */
  173. #ifdef CONFIG_IEEE80211W
  174. if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
  175. ret = 1;
  176. #endif /* CONFIG_IEEE80211W */
  177. return ret;
  178. }
  179. static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
  180. {
  181. struct wpa_authenticator *wpa_auth = eloop_ctx;
  182. if (os_get_random(wpa_auth->group->GMK, WPA_GMK_LEN)) {
  183. wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
  184. "initialization.");
  185. } else {
  186. wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
  187. }
  188. if (wpa_auth->conf.wpa_gmk_rekey) {
  189. eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
  190. wpa_rekey_gmk, wpa_auth, NULL);
  191. }
  192. }
  193. static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
  194. {
  195. struct wpa_authenticator *wpa_auth = eloop_ctx;
  196. struct wpa_group *group;
  197. wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
  198. for (group = wpa_auth->group; group; group = group->next) {
  199. group->GTKReKey = TRUE;
  200. do {
  201. group->changed = FALSE;
  202. wpa_group_sm_step(wpa_auth, group);
  203. } while (group->changed);
  204. }
  205. if (wpa_auth->conf.wpa_group_rekey) {
  206. eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
  207. 0, wpa_rekey_gtk, wpa_auth, NULL);
  208. }
  209. }
  210. static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
  211. {
  212. if (sm->pmksa == ctx)
  213. sm->pmksa = NULL;
  214. return 0;
  215. }
  216. static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
  217. void *ctx)
  218. {
  219. struct wpa_authenticator *wpa_auth = ctx;
  220. wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
  221. }
  222. static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
  223. int vlan_id)
  224. {
  225. struct wpa_group *group;
  226. u8 buf[ETH_ALEN + 8 + sizeof(group)];
  227. u8 rkey[32];
  228. group = os_zalloc(sizeof(struct wpa_group));
  229. if (group == NULL)
  230. return NULL;
  231. group->GTKAuthenticator = TRUE;
  232. group->vlan_id = vlan_id;
  233. switch (wpa_auth->conf.wpa_group) {
  234. case WPA_CIPHER_CCMP:
  235. group->GTK_len = 16;
  236. break;
  237. case WPA_CIPHER_TKIP:
  238. group->GTK_len = 32;
  239. break;
  240. case WPA_CIPHER_WEP104:
  241. group->GTK_len = 13;
  242. break;
  243. case WPA_CIPHER_WEP40:
  244. group->GTK_len = 5;
  245. break;
  246. }
  247. /* Counter = PRF-256(Random number, "Init Counter",
  248. * Local MAC Address || Time)
  249. */
  250. os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
  251. wpa_get_ntp_timestamp(buf + ETH_ALEN);
  252. os_memcpy(buf + ETH_ALEN + 8, &group, sizeof(group));
  253. if (os_get_random(rkey, sizeof(rkey)) ||
  254. os_get_random(group->GMK, WPA_GMK_LEN)) {
  255. wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
  256. "initialization.");
  257. os_free(group);
  258. return NULL;
  259. }
  260. sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
  261. group->Counter, WPA_NONCE_LEN);
  262. group->GInit = TRUE;
  263. wpa_group_sm_step(wpa_auth, group);
  264. group->GInit = FALSE;
  265. wpa_group_sm_step(wpa_auth, group);
  266. return group;
  267. }
  268. /**
  269. * wpa_init - Initialize WPA authenticator
  270. * @addr: Authenticator address
  271. * @conf: Configuration for WPA authenticator
  272. * Returns: Pointer to WPA authenticator data or %NULL on failure
  273. */
  274. struct wpa_authenticator * wpa_init(const u8 *addr,
  275. struct wpa_auth_config *conf,
  276. struct wpa_auth_callbacks *cb)
  277. {
  278. struct wpa_authenticator *wpa_auth;
  279. wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
  280. if (wpa_auth == NULL)
  281. return NULL;
  282. os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
  283. os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
  284. os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
  285. if (wpa_auth_gen_wpa_ie(wpa_auth)) {
  286. wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
  287. os_free(wpa_auth);
  288. return NULL;
  289. }
  290. wpa_auth->group = wpa_group_init(wpa_auth, 0);
  291. if (wpa_auth->group == NULL) {
  292. os_free(wpa_auth->wpa_ie);
  293. os_free(wpa_auth);
  294. return NULL;
  295. }
  296. wpa_auth->pmksa = pmksa_cache_init(wpa_auth_pmksa_free_cb, wpa_auth);
  297. if (wpa_auth->pmksa == NULL) {
  298. wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
  299. os_free(wpa_auth->wpa_ie);
  300. os_free(wpa_auth);
  301. return NULL;
  302. }
  303. #ifdef CONFIG_IEEE80211R
  304. wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
  305. if (wpa_auth->ft_pmk_cache == NULL) {
  306. wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
  307. os_free(wpa_auth->wpa_ie);
  308. pmksa_cache_deinit(wpa_auth->pmksa);
  309. os_free(wpa_auth);
  310. return NULL;
  311. }
  312. #endif /* CONFIG_IEEE80211R */
  313. if (wpa_auth->conf.wpa_gmk_rekey) {
  314. eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
  315. wpa_rekey_gmk, wpa_auth, NULL);
  316. }
  317. if (wpa_auth->conf.wpa_group_rekey) {
  318. eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
  319. wpa_rekey_gtk, wpa_auth, NULL);
  320. }
  321. return wpa_auth;
  322. }
  323. /**
  324. * wpa_deinit - Deinitialize WPA authenticator
  325. * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
  326. */
  327. void wpa_deinit(struct wpa_authenticator *wpa_auth)
  328. {
  329. struct wpa_group *group, *prev;
  330. eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
  331. eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
  332. #ifdef CONFIG_PEERKEY
  333. while (wpa_auth->stsl_negotiations)
  334. wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
  335. #endif /* CONFIG_PEERKEY */
  336. pmksa_cache_deinit(wpa_auth->pmksa);
  337. #ifdef CONFIG_IEEE80211R
  338. wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
  339. wpa_auth->ft_pmk_cache = NULL;
  340. #endif /* CONFIG_IEEE80211R */
  341. os_free(wpa_auth->wpa_ie);
  342. group = wpa_auth->group;
  343. while (group) {
  344. prev = group;
  345. group = group->next;
  346. os_free(prev);
  347. }
  348. os_free(wpa_auth);
  349. }
  350. /**
  351. * wpa_reconfig - Update WPA authenticator configuration
  352. * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
  353. * @conf: Configuration for WPA authenticator
  354. */
  355. int wpa_reconfig(struct wpa_authenticator *wpa_auth,
  356. struct wpa_auth_config *conf)
  357. {
  358. if (wpa_auth == NULL)
  359. return 0;
  360. os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
  361. /*
  362. * TODO:
  363. * Disassociate stations if configuration changed
  364. * Update WPA/RSN IE
  365. */
  366. return 0;
  367. }
  368. struct wpa_state_machine *
  369. wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr)
  370. {
  371. struct wpa_state_machine *sm;
  372. sm = os_zalloc(sizeof(struct wpa_state_machine));
  373. if (sm == NULL)
  374. return NULL;
  375. os_memcpy(sm->addr, addr, ETH_ALEN);
  376. sm->wpa_auth = wpa_auth;
  377. sm->group = wpa_auth->group;
  378. return sm;
  379. }
  380. void wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
  381. struct wpa_state_machine *sm)
  382. {
  383. if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
  384. return;
  385. #ifdef CONFIG_IEEE80211R
  386. if (sm->ft_completed) {
  387. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  388. "FT authentication already completed - do not "
  389. "start 4-way handshake");
  390. return;
  391. }
  392. #endif /* CONFIG_IEEE80211R */
  393. if (sm->started) {
  394. os_memset(sm->key_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
  395. sm->ReAuthenticationRequest = TRUE;
  396. wpa_sm_step(sm);
  397. return;
  398. }
  399. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  400. "start authentication");
  401. sm->started = 1;
  402. sm->Init = TRUE;
  403. wpa_sm_step(sm);
  404. sm->Init = FALSE;
  405. sm->AuthenticationRequest = TRUE;
  406. wpa_sm_step(sm);
  407. }
  408. static void wpa_free_sta_sm(struct wpa_state_machine *sm)
  409. {
  410. os_free(sm->last_rx_eapol_key);
  411. os_free(sm->wpa_ie);
  412. os_free(sm);
  413. }
  414. void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
  415. {
  416. if (sm == NULL)
  417. return;
  418. if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
  419. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  420. "strict rekeying - force GTK rekey since STA "
  421. "is leaving");
  422. eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
  423. eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
  424. NULL);
  425. }
  426. eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
  427. eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
  428. if (sm->in_step_loop) {
  429. /* Must not free state machine while wpa_sm_step() is running.
  430. * Freeing will be completed in the end of wpa_sm_step(). */
  431. wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
  432. "machine deinit for " MACSTR, MAC2STR(sm->addr));
  433. sm->pending_deinit = 1;
  434. } else
  435. wpa_free_sta_sm(sm);
  436. }
  437. static void wpa_request_new_ptk(struct wpa_state_machine *sm)
  438. {
  439. if (sm == NULL)
  440. return;
  441. sm->PTKRequest = TRUE;
  442. sm->PTK_valid = 0;
  443. }
  444. void wpa_receive(struct wpa_authenticator *wpa_auth,
  445. struct wpa_state_machine *sm,
  446. u8 *data, size_t data_len)
  447. {
  448. struct ieee802_1x_hdr *hdr;
  449. struct wpa_eapol_key *key;
  450. u16 key_info, key_data_length;
  451. enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
  452. SMK_M1, SMK_M3, SMK_ERROR } msg;
  453. char *msgtxt;
  454. struct wpa_eapol_ie_parse kde;
  455. if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
  456. return;
  457. if (data_len < sizeof(*hdr) + sizeof(*key))
  458. return;
  459. hdr = (struct ieee802_1x_hdr *) data;
  460. key = (struct wpa_eapol_key *) (hdr + 1);
  461. key_info = WPA_GET_BE16(key->key_info);
  462. key_data_length = WPA_GET_BE16(key->key_data_length);
  463. if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) {
  464. wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
  465. "key_data overflow (%d > %lu)",
  466. key_data_length,
  467. (unsigned long) (data_len - sizeof(*hdr) -
  468. sizeof(*key)));
  469. return;
  470. }
  471. /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
  472. * are set */
  473. if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
  474. (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
  475. if (key_info & WPA_KEY_INFO_ERROR) {
  476. msg = SMK_ERROR;
  477. msgtxt = "SMK Error";
  478. } else {
  479. msg = SMK_M1;
  480. msgtxt = "SMK M1";
  481. }
  482. } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
  483. msg = SMK_M3;
  484. msgtxt = "SMK M3";
  485. } else if (key_info & WPA_KEY_INFO_REQUEST) {
  486. msg = REQUEST;
  487. msgtxt = "Request";
  488. } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
  489. msg = GROUP_2;
  490. msgtxt = "2/2 Group";
  491. } else if (key_data_length == 0) {
  492. msg = PAIRWISE_4;
  493. msgtxt = "4/4 Pairwise";
  494. } else {
  495. msg = PAIRWISE_2;
  496. msgtxt = "2/4 Pairwise";
  497. }
  498. /* TODO: key_info type validation for PeerKey */
  499. if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
  500. msg == GROUP_2) {
  501. u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  502. if (sm->pairwise == WPA_CIPHER_CCMP) {
  503. if (wpa_use_aes_cmac(sm) &&
  504. ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  505. wpa_auth_logger(wpa_auth, sm->addr,
  506. LOGGER_WARNING,
  507. "advertised support for "
  508. "AES-128-CMAC, but did not "
  509. "use it");
  510. return;
  511. }
  512. if (!wpa_use_aes_cmac(sm) &&
  513. ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
  514. wpa_auth_logger(wpa_auth, sm->addr,
  515. LOGGER_WARNING,
  516. "did not use HMAC-SHA1-AES "
  517. "with CCMP");
  518. return;
  519. }
  520. }
  521. }
  522. if (key_info & WPA_KEY_INFO_REQUEST) {
  523. if (sm->req_replay_counter_used &&
  524. os_memcmp(key->replay_counter, sm->req_replay_counter,
  525. WPA_REPLAY_COUNTER_LEN) <= 0) {
  526. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
  527. "received EAPOL-Key request with "
  528. "replayed counter");
  529. return;
  530. }
  531. }
  532. if (!(key_info & WPA_KEY_INFO_REQUEST) &&
  533. (!sm->key_replay_counter_valid ||
  534. os_memcmp(key->replay_counter, sm->key_replay_counter,
  535. WPA_REPLAY_COUNTER_LEN) != 0)) {
  536. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  537. "received EAPOL-Key %s with unexpected "
  538. "replay counter", msgtxt);
  539. wpa_hexdump(MSG_DEBUG, "expected replay counter",
  540. sm->key_replay_counter, WPA_REPLAY_COUNTER_LEN);
  541. wpa_hexdump(MSG_DEBUG, "received replay counter",
  542. key->replay_counter, WPA_REPLAY_COUNTER_LEN);
  543. return;
  544. }
  545. switch (msg) {
  546. case PAIRWISE_2:
  547. if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
  548. sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING) {
  549. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  550. "received EAPOL-Key msg 2/4 in "
  551. "invalid state (%d) - dropped",
  552. sm->wpa_ptk_state);
  553. return;
  554. }
  555. if (sm->wpa_ie == NULL ||
  556. sm->wpa_ie_len != key_data_length ||
  557. os_memcmp(sm->wpa_ie, key + 1, key_data_length) != 0) {
  558. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  559. "WPA IE from (Re)AssocReq did not "
  560. "match with msg 2/4");
  561. if (sm->wpa_ie) {
  562. wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
  563. sm->wpa_ie, sm->wpa_ie_len);
  564. }
  565. wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
  566. (u8 *) (key + 1), key_data_length);
  567. /* MLME-DEAUTHENTICATE.request */
  568. wpa_sta_disconnect(wpa_auth, sm->addr);
  569. return;
  570. }
  571. break;
  572. case PAIRWISE_4:
  573. if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
  574. !sm->PTK_valid) {
  575. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  576. "received EAPOL-Key msg 4/4 in "
  577. "invalid state (%d) - dropped",
  578. sm->wpa_ptk_state);
  579. return;
  580. }
  581. break;
  582. case GROUP_2:
  583. if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
  584. || !sm->PTK_valid) {
  585. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  586. "received EAPOL-Key msg 2/2 in "
  587. "invalid state (%d) - dropped",
  588. sm->wpa_ptk_group_state);
  589. return;
  590. }
  591. break;
  592. #ifdef CONFIG_PEERKEY
  593. case SMK_M1:
  594. case SMK_M3:
  595. case SMK_ERROR:
  596. if (!wpa_auth->conf.peerkey) {
  597. wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
  598. "PeerKey use disabled - ignoring message");
  599. return;
  600. }
  601. if (!sm->PTK_valid) {
  602. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  603. "received EAPOL-Key msg SMK in "
  604. "invalid state - dropped");
  605. return;
  606. }
  607. break;
  608. #else /* CONFIG_PEERKEY */
  609. case SMK_M1:
  610. case SMK_M3:
  611. case SMK_ERROR:
  612. return; /* STSL disabled - ignore SMK messages */
  613. #endif /* CONFIG_PEERKEY */
  614. case REQUEST:
  615. break;
  616. }
  617. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  618. "received EAPOL-Key frame (%s)", msgtxt);
  619. if (key_info & WPA_KEY_INFO_ACK) {
  620. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  621. "received invalid EAPOL-Key: Key Ack set");
  622. return;
  623. }
  624. if (!(key_info & WPA_KEY_INFO_MIC)) {
  625. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  626. "received invalid EAPOL-Key: Key MIC not set");
  627. return;
  628. }
  629. sm->MICVerified = FALSE;
  630. if (sm->PTK_valid) {
  631. if (wpa_verify_key_mic(&sm->PTK, data, data_len)) {
  632. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  633. "received EAPOL-Key with invalid MIC");
  634. return;
  635. }
  636. sm->MICVerified = TRUE;
  637. eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
  638. }
  639. if (key_info & WPA_KEY_INFO_REQUEST) {
  640. if (sm->MICVerified) {
  641. sm->req_replay_counter_used = 1;
  642. os_memcpy(sm->req_replay_counter, key->replay_counter,
  643. WPA_REPLAY_COUNTER_LEN);
  644. } else {
  645. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  646. "received EAPOL-Key request with "
  647. "invalid MIC");
  648. return;
  649. }
  650. /*
  651. * TODO: should decrypt key data field if encryption was used;
  652. * even though MAC address KDE is not normally encrypted,
  653. * supplicant is allowed to encrypt it.
  654. */
  655. if (msg == SMK_ERROR) {
  656. #ifdef CONFIG_PEERKEY
  657. wpa_smk_error(wpa_auth, sm, key);
  658. #endif /* CONFIG_PEERKEY */
  659. return;
  660. } else if (key_info & WPA_KEY_INFO_ERROR) {
  661. /* Supplicant reported a Michael MIC error */
  662. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  663. "received EAPOL-Key Error Request "
  664. "(STA detected Michael MIC failure)");
  665. wpa_auth_mic_failure_report(wpa_auth, sm->addr);
  666. sm->dot11RSNAStatsTKIPRemoteMICFailures++;
  667. wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
  668. /* Error report is not a request for a new key
  669. * handshake, but since Authenticator may do it, let's
  670. * change the keys now anyway. */
  671. wpa_request_new_ptk(sm);
  672. } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
  673. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  674. "received EAPOL-Key Request for new "
  675. "4-Way Handshake");
  676. wpa_request_new_ptk(sm);
  677. #ifdef CONFIG_PEERKEY
  678. } else if (msg == SMK_M1) {
  679. wpa_smk_m1(wpa_auth, sm, key);
  680. #endif /* CONFIG_PEERKEY */
  681. } else if (key_data_length > 0 &&
  682. wpa_parse_kde_ies((const u8 *) (key + 1),
  683. key_data_length, &kde) == 0 &&
  684. kde.mac_addr) {
  685. } else {
  686. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  687. "received EAPOL-Key Request for GTK "
  688. "rekeying");
  689. /* FIX: why was this triggering PTK rekeying for the
  690. * STA that requested Group Key rekeying?? */
  691. /* wpa_request_new_ptk(sta->wpa_sm); */
  692. eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
  693. wpa_rekey_gtk(wpa_auth, NULL);
  694. }
  695. } else {
  696. /* Do not allow the same key replay counter to be reused. */
  697. sm->key_replay_counter_valid = FALSE;
  698. }
  699. #ifdef CONFIG_PEERKEY
  700. if (msg == SMK_M3) {
  701. wpa_smk_m3(wpa_auth, sm, key);
  702. return;
  703. }
  704. #endif /* CONFIG_PEERKEY */
  705. os_free(sm->last_rx_eapol_key);
  706. sm->last_rx_eapol_key = os_malloc(data_len);
  707. if (sm->last_rx_eapol_key == NULL)
  708. return;
  709. os_memcpy(sm->last_rx_eapol_key, data, data_len);
  710. sm->last_rx_eapol_key_len = data_len;
  711. sm->EAPOLKeyReceived = TRUE;
  712. sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
  713. sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
  714. os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
  715. wpa_sm_step(sm);
  716. }
  717. static void wpa_gmk_to_gtk(const u8 *gmk, const u8 *addr, const u8 *gnonce,
  718. u8 *gtk, size_t gtk_len)
  719. {
  720. u8 data[ETH_ALEN + WPA_NONCE_LEN];
  721. /* GTK = PRF-X(GMK, "Group key expansion", AA || GNonce) */
  722. os_memcpy(data, addr, ETH_ALEN);
  723. os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
  724. #ifdef CONFIG_IEEE80211W
  725. sha256_prf(gmk, WPA_GMK_LEN, "Group key expansion",
  726. data, sizeof(data), gtk, gtk_len);
  727. #else /* CONFIG_IEEE80211W */
  728. sha1_prf(gmk, WPA_GMK_LEN, "Group key expansion",
  729. data, sizeof(data), gtk, gtk_len);
  730. #endif /* CONFIG_IEEE80211W */
  731. wpa_hexdump_key(MSG_DEBUG, "GMK", gmk, WPA_GMK_LEN);
  732. wpa_hexdump_key(MSG_DEBUG, "GTK", gtk, gtk_len);
  733. }
  734. static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
  735. {
  736. struct wpa_authenticator *wpa_auth = eloop_ctx;
  737. struct wpa_state_machine *sm = timeout_ctx;
  738. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
  739. sm->TimeoutEvt = TRUE;
  740. wpa_sm_step(sm);
  741. }
  742. void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
  743. struct wpa_state_machine *sm, int key_info,
  744. const u8 *key_rsc, const u8 *nonce,
  745. const u8 *kde, size_t kde_len,
  746. int keyidx, int encr, int force_version)
  747. {
  748. struct ieee802_1x_hdr *hdr;
  749. struct wpa_eapol_key *key;
  750. size_t len;
  751. int alg;
  752. int key_data_len, pad_len = 0;
  753. u8 *buf, *pos;
  754. int version, pairwise;
  755. len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
  756. if (force_version)
  757. version = force_version;
  758. else if (wpa_use_aes_cmac(sm))
  759. version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
  760. else if (sm->pairwise == WPA_CIPHER_CCMP)
  761. version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
  762. else
  763. version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
  764. pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
  765. wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
  766. "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
  767. "encr=%d)",
  768. version,
  769. (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
  770. (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
  771. (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
  772. (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
  773. pairwise, (unsigned long) kde_len, keyidx, encr);
  774. key_data_len = kde_len;
  775. if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
  776. version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
  777. pad_len = key_data_len % 8;
  778. if (pad_len)
  779. pad_len = 8 - pad_len;
  780. key_data_len += pad_len + 8;
  781. }
  782. len += key_data_len;
  783. hdr = os_zalloc(len);
  784. if (hdr == NULL)
  785. return;
  786. hdr->version = wpa_auth->conf.eapol_version;
  787. hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
  788. hdr->length = host_to_be16(len - sizeof(*hdr));
  789. key = (struct wpa_eapol_key *) (hdr + 1);
  790. key->type = sm->wpa == WPA_VERSION_WPA2 ?
  791. EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
  792. key_info |= version;
  793. if (encr && sm->wpa == WPA_VERSION_WPA2)
  794. key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
  795. if (sm->wpa != WPA_VERSION_WPA2)
  796. key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
  797. WPA_PUT_BE16(key->key_info, key_info);
  798. alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
  799. switch (alg) {
  800. case WPA_CIPHER_CCMP:
  801. WPA_PUT_BE16(key->key_length, 16);
  802. break;
  803. case WPA_CIPHER_TKIP:
  804. WPA_PUT_BE16(key->key_length, 32);
  805. break;
  806. case WPA_CIPHER_WEP40:
  807. WPA_PUT_BE16(key->key_length, 5);
  808. break;
  809. case WPA_CIPHER_WEP104:
  810. WPA_PUT_BE16(key->key_length, 13);
  811. break;
  812. }
  813. if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
  814. WPA_PUT_BE16(key->key_length, 0);
  815. /* FIX: STSL: what to use as key_replay_counter? */
  816. inc_byte_array(sm->key_replay_counter, WPA_REPLAY_COUNTER_LEN);
  817. os_memcpy(key->replay_counter, sm->key_replay_counter,
  818. WPA_REPLAY_COUNTER_LEN);
  819. sm->key_replay_counter_valid = TRUE;
  820. if (nonce)
  821. os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
  822. if (key_rsc)
  823. os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
  824. if (kde && !encr) {
  825. os_memcpy(key + 1, kde, kde_len);
  826. WPA_PUT_BE16(key->key_data_length, kde_len);
  827. } else if (encr && kde) {
  828. buf = os_zalloc(key_data_len);
  829. if (buf == NULL) {
  830. os_free(hdr);
  831. return;
  832. }
  833. pos = buf;
  834. os_memcpy(pos, kde, kde_len);
  835. pos += kde_len;
  836. if (pad_len)
  837. *pos++ = 0xdd;
  838. wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
  839. buf, key_data_len);
  840. if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
  841. version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  842. if (aes_wrap(sm->PTK.kek, (key_data_len - 8) / 8, buf,
  843. (u8 *) (key + 1))) {
  844. os_free(hdr);
  845. os_free(buf);
  846. return;
  847. }
  848. WPA_PUT_BE16(key->key_data_length, key_data_len);
  849. } else {
  850. u8 ek[32];
  851. os_memcpy(key->key_iv,
  852. sm->group->Counter + WPA_NONCE_LEN - 16, 16);
  853. inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
  854. os_memcpy(ek, key->key_iv, 16);
  855. os_memcpy(ek + 16, sm->PTK.kek, 16);
  856. os_memcpy(key + 1, buf, key_data_len);
  857. rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
  858. WPA_PUT_BE16(key->key_data_length, key_data_len);
  859. }
  860. os_free(buf);
  861. }
  862. if (key_info & WPA_KEY_INFO_MIC) {
  863. if (!sm->PTK_valid) {
  864. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  865. "PTK not valid when sending EAPOL-Key "
  866. "frame");
  867. os_free(hdr);
  868. return;
  869. }
  870. wpa_eapol_key_mic(sm->PTK.kck, version, (u8 *) hdr, len,
  871. key->key_mic);
  872. }
  873. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
  874. 1);
  875. wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
  876. sm->pairwise_set);
  877. os_free(hdr);
  878. }
  879. static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
  880. struct wpa_state_machine *sm, int key_info,
  881. const u8 *key_rsc, const u8 *nonce,
  882. const u8 *kde, size_t kde_len,
  883. int keyidx, int encr)
  884. {
  885. int timeout_ms;
  886. int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
  887. if (sm == NULL)
  888. return;
  889. __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
  890. keyidx, encr, 0);
  891. timeout_ms = pairwise ? dot11RSNAConfigPairwiseUpdateTimeOut :
  892. dot11RSNAConfigGroupUpdateTimeOut;
  893. eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
  894. wpa_send_eapol_timeout, wpa_auth, sm);
  895. }
  896. static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len)
  897. {
  898. struct ieee802_1x_hdr *hdr;
  899. struct wpa_eapol_key *key;
  900. u16 key_info;
  901. int ret = 0;
  902. u8 mic[16];
  903. if (data_len < sizeof(*hdr) + sizeof(*key))
  904. return -1;
  905. hdr = (struct ieee802_1x_hdr *) data;
  906. key = (struct wpa_eapol_key *) (hdr + 1);
  907. key_info = WPA_GET_BE16(key->key_info);
  908. os_memcpy(mic, key->key_mic, 16);
  909. os_memset(key->key_mic, 0, 16);
  910. if (wpa_eapol_key_mic(PTK->kck, key_info & WPA_KEY_INFO_TYPE_MASK,
  911. data, data_len, key->key_mic) ||
  912. os_memcmp(mic, key->key_mic, 16) != 0)
  913. ret = -1;
  914. os_memcpy(key->key_mic, mic, 16);
  915. return ret;
  916. }
  917. void wpa_remove_ptk(struct wpa_state_machine *sm)
  918. {
  919. sm->PTK_valid = FALSE;
  920. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  921. wpa_auth_set_key(sm->wpa_auth, 0, "none", sm->addr, 0, (u8 *) "", 0);
  922. sm->pairwise_set = FALSE;
  923. }
  924. void wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
  925. {
  926. int remove_ptk = 1;
  927. if (sm == NULL)
  928. return;
  929. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  930. "event %d notification", event);
  931. switch (event) {
  932. case WPA_AUTH:
  933. case WPA_ASSOC:
  934. break;
  935. case WPA_DEAUTH:
  936. case WPA_DISASSOC:
  937. sm->DeauthenticationRequest = TRUE;
  938. break;
  939. case WPA_REAUTH:
  940. case WPA_REAUTH_EAPOL:
  941. sm->ReAuthenticationRequest = TRUE;
  942. break;
  943. case WPA_ASSOC_FT:
  944. #ifdef CONFIG_IEEE80211R
  945. /* Using FT protocol, not WPA auth state machine */
  946. sm->ft_completed = 1;
  947. return;
  948. #else /* CONFIG_IEEE80211R */
  949. break;
  950. #endif /* CONFIG_IEEE80211R */
  951. }
  952. #ifdef CONFIG_IEEE80211R
  953. sm->ft_completed = 0;
  954. #endif /* CONFIG_IEEE80211R */
  955. #ifdef CONFIG_IEEE80211W
  956. if (sm->mgmt_frame_prot && event == WPA_AUTH)
  957. remove_ptk = 0;
  958. #endif /* CONFIG_IEEE80211W */
  959. if (remove_ptk) {
  960. sm->PTK_valid = FALSE;
  961. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  962. if (event != WPA_REAUTH_EAPOL)
  963. wpa_remove_ptk(sm);
  964. }
  965. wpa_sm_step(sm);
  966. }
  967. static const char * wpa_alg_txt(int alg)
  968. {
  969. switch (alg) {
  970. case WPA_CIPHER_CCMP:
  971. return "CCMP";
  972. case WPA_CIPHER_TKIP:
  973. return "TKIP";
  974. case WPA_CIPHER_WEP104:
  975. case WPA_CIPHER_WEP40:
  976. return "WEP";
  977. default:
  978. return "";
  979. }
  980. }
  981. SM_STATE(WPA_PTK, INITIALIZE)
  982. {
  983. SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
  984. if (sm->Init) {
  985. /* Init flag is not cleared here, so avoid busy
  986. * loop by claiming nothing changed. */
  987. sm->changed = FALSE;
  988. }
  989. sm->keycount = 0;
  990. if (sm->GUpdateStationKeys)
  991. sm->group->GKeyDoneStations--;
  992. sm->GUpdateStationKeys = FALSE;
  993. if (sm->wpa == WPA_VERSION_WPA)
  994. sm->PInitAKeys = FALSE;
  995. if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
  996. * Local AA > Remote AA)) */) {
  997. sm->Pair = TRUE;
  998. }
  999. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
  1000. wpa_remove_ptk(sm);
  1001. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
  1002. sm->TimeoutCtr = 0;
  1003. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1004. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  1005. WPA_EAPOL_authorized, 0);
  1006. }
  1007. }
  1008. SM_STATE(WPA_PTK, DISCONNECT)
  1009. {
  1010. SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
  1011. sm->Disconnect = FALSE;
  1012. wpa_sta_disconnect(sm->wpa_auth, sm->addr);
  1013. }
  1014. SM_STATE(WPA_PTK, DISCONNECTED)
  1015. {
  1016. SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
  1017. sm->DeauthenticationRequest = FALSE;
  1018. }
  1019. SM_STATE(WPA_PTK, AUTHENTICATION)
  1020. {
  1021. SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
  1022. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  1023. sm->PTK_valid = FALSE;
  1024. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
  1025. 1);
  1026. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
  1027. sm->AuthenticationRequest = FALSE;
  1028. }
  1029. SM_STATE(WPA_PTK, AUTHENTICATION2)
  1030. {
  1031. SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
  1032. os_memcpy(sm->ANonce, sm->group->Counter, WPA_NONCE_LEN);
  1033. inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
  1034. sm->ReAuthenticationRequest = FALSE;
  1035. /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
  1036. * logical place than INITIALIZE since AUTHENTICATION2 can be
  1037. * re-entered on ReAuthenticationRequest without going through
  1038. * INITIALIZE. */
  1039. sm->TimeoutCtr = 0;
  1040. }
  1041. SM_STATE(WPA_PTK, INITPMK)
  1042. {
  1043. u8 msk[2 * PMK_LEN];
  1044. size_t len = 2 * PMK_LEN;
  1045. SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
  1046. #ifdef CONFIG_IEEE80211R
  1047. sm->xxkey_len = 0;
  1048. #endif /* CONFIG_IEEE80211R */
  1049. if (sm->pmksa) {
  1050. wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
  1051. os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
  1052. } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
  1053. wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
  1054. "(len=%lu)", (unsigned long) len);
  1055. os_memcpy(sm->PMK, msk, PMK_LEN);
  1056. #ifdef CONFIG_IEEE80211R
  1057. if (len >= 2 * PMK_LEN) {
  1058. os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
  1059. sm->xxkey_len = PMK_LEN;
  1060. }
  1061. #endif /* CONFIG_IEEE80211R */
  1062. } else {
  1063. wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
  1064. }
  1065. sm->req_replay_counter_used = 0;
  1066. /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
  1067. * will break reauthentication since EAPOL state machines may not be
  1068. * get into AUTHENTICATING state that clears keyRun before WPA state
  1069. * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
  1070. * state and takes PMK from the previously used AAA Key. This will
  1071. * eventually fail in 4-Way Handshake because Supplicant uses PMK
  1072. * derived from the new AAA Key. Setting keyRun = FALSE here seems to
  1073. * be good workaround for this issue. */
  1074. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
  1075. }
  1076. SM_STATE(WPA_PTK, INITPSK)
  1077. {
  1078. const u8 *psk;
  1079. SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
  1080. psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL);
  1081. if (psk) {
  1082. os_memcpy(sm->PMK, psk, PMK_LEN);
  1083. #ifdef CONFIG_IEEE80211R
  1084. os_memcpy(sm->xxkey, psk, PMK_LEN);
  1085. sm->xxkey_len = PMK_LEN;
  1086. #endif /* CONFIG_IEEE80211R */
  1087. }
  1088. sm->req_replay_counter_used = 0;
  1089. }
  1090. SM_STATE(WPA_PTK, PTKSTART)
  1091. {
  1092. u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
  1093. size_t pmkid_len = 0;
  1094. SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
  1095. sm->PTKRequest = FALSE;
  1096. sm->TimeoutEvt = FALSE;
  1097. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1098. "sending 1/4 msg of 4-Way Handshake");
  1099. /*
  1100. * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
  1101. * one possible PSK for this STA.
  1102. */
  1103. if (sm->wpa == WPA_VERSION_WPA2 &&
  1104. wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt)) {
  1105. pmkid = buf;
  1106. pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
  1107. pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
  1108. pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
  1109. RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
  1110. if (sm->pmksa)
  1111. os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
  1112. sm->pmksa->pmkid, PMKID_LEN);
  1113. else {
  1114. /*
  1115. * Calculate PMKID since no PMKSA cache entry was
  1116. * available with pre-calculated PMKID.
  1117. */
  1118. rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
  1119. sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
  1120. wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
  1121. }
  1122. }
  1123. wpa_send_eapol(sm->wpa_auth, sm,
  1124. WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
  1125. sm->ANonce, pmkid, pmkid_len, 0, 0);
  1126. sm->TimeoutCtr++;
  1127. }
  1128. static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk,
  1129. struct wpa_ptk *ptk)
  1130. {
  1131. #ifdef CONFIG_IEEE80211R
  1132. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
  1133. return wpa_auth_derive_ptk_ft(sm, pmk, ptk);
  1134. #endif /* CONFIG_IEEE80211R */
  1135. wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
  1136. sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce,
  1137. (u8 *) ptk, sizeof(*ptk),
  1138. wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
  1139. return 0;
  1140. }
  1141. SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
  1142. {
  1143. struct wpa_ptk PTK;
  1144. int ok = 0;
  1145. const u8 *pmk = NULL;
  1146. SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
  1147. sm->EAPOLKeyReceived = FALSE;
  1148. /* WPA with IEEE 802.1X: use the derived PMK from EAP
  1149. * WPA-PSK: iterate through possible PSKs and select the one matching
  1150. * the packet */
  1151. for (;;) {
  1152. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1153. pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, pmk);
  1154. if (pmk == NULL)
  1155. break;
  1156. } else
  1157. pmk = sm->PMK;
  1158. wpa_derive_ptk(sm, pmk, &PTK);
  1159. if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key,
  1160. sm->last_rx_eapol_key_len) == 0) {
  1161. ok = 1;
  1162. break;
  1163. }
  1164. if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
  1165. break;
  1166. }
  1167. if (!ok) {
  1168. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1169. "invalid MIC in msg 2/4 of 4-Way Handshake");
  1170. return;
  1171. }
  1172. eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
  1173. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1174. /* PSK may have changed from the previous choice, so update
  1175. * state machine data based on whatever PSK was selected here.
  1176. */
  1177. os_memcpy(sm->PMK, pmk, PMK_LEN);
  1178. }
  1179. sm->MICVerified = TRUE;
  1180. os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
  1181. sm->PTK_valid = TRUE;
  1182. }
  1183. SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
  1184. {
  1185. SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
  1186. sm->TimeoutCtr = 0;
  1187. }
  1188. #ifdef CONFIG_IEEE80211W
  1189. static int ieee80211w_kde_len(struct wpa_state_machine *sm)
  1190. {
  1191. if (sm->mgmt_frame_prot) {
  1192. return 2 + RSN_SELECTOR_LEN + sizeof(struct wpa_igtk_kde);
  1193. }
  1194. return 0;
  1195. }
  1196. static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
  1197. {
  1198. struct wpa_igtk_kde igtk;
  1199. struct wpa_group *gsm = sm->group;
  1200. if (!sm->mgmt_frame_prot)
  1201. return pos;
  1202. igtk.keyid[0] = gsm->GN_igtk;
  1203. igtk.keyid[1] = 0;
  1204. if (wpa_auth_get_seqnum_igtk(sm->wpa_auth, NULL, gsm->GN_igtk, igtk.pn)
  1205. < 0)
  1206. os_memset(igtk.pn, 0, sizeof(igtk.pn));
  1207. os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
  1208. pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
  1209. (const u8 *) &igtk, sizeof(igtk), NULL, 0);
  1210. return pos;
  1211. }
  1212. #else /* CONFIG_IEEE80211W */
  1213. static int ieee80211w_kde_len(struct wpa_state_machine *sm)
  1214. {
  1215. return 0;
  1216. }
  1217. static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
  1218. {
  1219. return pos;
  1220. }
  1221. #endif /* CONFIG_IEEE80211W */
  1222. SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
  1223. {
  1224. u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
  1225. size_t gtk_len, kde_len;
  1226. struct wpa_group *gsm = sm->group;
  1227. u8 *wpa_ie;
  1228. int wpa_ie_len, secure, keyidx, encr = 0;
  1229. SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
  1230. sm->TimeoutEvt = FALSE;
  1231. /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, GTK[GN])
  1232. */
  1233. os_memset(rsc, 0, WPA_KEY_RSC_LEN);
  1234. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
  1235. wpa_ie = sm->wpa_auth->wpa_ie;
  1236. wpa_ie_len = sm->wpa_auth->wpa_ie_len;
  1237. if (sm->wpa == WPA_VERSION_WPA &&
  1238. (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
  1239. wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
  1240. /* WPA-only STA, remove RSN IE */
  1241. wpa_ie = wpa_ie + wpa_ie[1] + 2;
  1242. wpa_ie_len = wpa_ie[1] + 2;
  1243. }
  1244. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1245. "sending 3/4 msg of 4-Way Handshake");
  1246. if (sm->wpa == WPA_VERSION_WPA2) {
  1247. /* WPA2 send GTK in the 4-way handshake */
  1248. secure = 1;
  1249. gtk = gsm->GTK[gsm->GN - 1];
  1250. gtk_len = gsm->GTK_len;
  1251. keyidx = gsm->GN;
  1252. _rsc = rsc;
  1253. encr = 1;
  1254. } else {
  1255. /* WPA does not include GTK in msg 3/4 */
  1256. secure = 0;
  1257. gtk = NULL;
  1258. gtk_len = 0;
  1259. keyidx = 0;
  1260. _rsc = NULL;
  1261. }
  1262. kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
  1263. if (gtk)
  1264. kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
  1265. kde = os_malloc(kde_len);
  1266. if (kde == NULL)
  1267. return;
  1268. pos = kde;
  1269. os_memcpy(pos, wpa_ie, wpa_ie_len);
  1270. pos += wpa_ie_len;
  1271. if (gtk) {
  1272. u8 hdr[2];
  1273. hdr[0] = keyidx & 0x03;
  1274. hdr[1] = 0;
  1275. pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
  1276. gtk, gtk_len);
  1277. }
  1278. pos = ieee80211w_kde_add(sm, pos);
  1279. wpa_send_eapol(sm->wpa_auth, sm,
  1280. (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
  1281. WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
  1282. WPA_KEY_INFO_KEY_TYPE,
  1283. _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
  1284. os_free(kde);
  1285. sm->TimeoutCtr++;
  1286. }
  1287. SM_STATE(WPA_PTK, PTKINITDONE)
  1288. {
  1289. SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
  1290. sm->EAPOLKeyReceived = FALSE;
  1291. if (sm->Pair) {
  1292. char *alg;
  1293. int klen;
  1294. if (sm->pairwise == WPA_CIPHER_TKIP) {
  1295. alg = "TKIP";
  1296. klen = 32;
  1297. } else {
  1298. alg = "CCMP";
  1299. klen = 16;
  1300. }
  1301. if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
  1302. sm->PTK.tk1, klen)) {
  1303. wpa_sta_disconnect(sm->wpa_auth, sm->addr);
  1304. return;
  1305. }
  1306. /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
  1307. sm->pairwise_set = TRUE;
  1308. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1309. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  1310. WPA_EAPOL_authorized, 1);
  1311. }
  1312. }
  1313. if (0 /* IBSS == TRUE */) {
  1314. sm->keycount++;
  1315. if (sm->keycount == 2) {
  1316. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  1317. WPA_EAPOL_portValid, 1);
  1318. }
  1319. } else {
  1320. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
  1321. 1);
  1322. }
  1323. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
  1324. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
  1325. if (sm->wpa == WPA_VERSION_WPA)
  1326. sm->PInitAKeys = TRUE;
  1327. else
  1328. sm->has_GTK = TRUE;
  1329. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  1330. "pairwise key handshake completed (%s)",
  1331. sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
  1332. #ifdef CONFIG_IEEE80211R
  1333. wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
  1334. #endif /* CONFIG_IEEE80211R */
  1335. }
  1336. SM_STEP(WPA_PTK)
  1337. {
  1338. struct wpa_authenticator *wpa_auth = sm->wpa_auth;
  1339. if (sm->Init)
  1340. SM_ENTER(WPA_PTK, INITIALIZE);
  1341. else if (sm->Disconnect
  1342. /* || FIX: dot11RSNAConfigSALifetime timeout */)
  1343. SM_ENTER(WPA_PTK, DISCONNECT);
  1344. else if (sm->DeauthenticationRequest)
  1345. SM_ENTER(WPA_PTK, DISCONNECTED);
  1346. else if (sm->AuthenticationRequest)
  1347. SM_ENTER(WPA_PTK, AUTHENTICATION);
  1348. else if (sm->ReAuthenticationRequest)
  1349. SM_ENTER(WPA_PTK, AUTHENTICATION2);
  1350. else if (sm->PTKRequest)
  1351. SM_ENTER(WPA_PTK, PTKSTART);
  1352. else switch (sm->wpa_ptk_state) {
  1353. case WPA_PTK_INITIALIZE:
  1354. break;
  1355. case WPA_PTK_DISCONNECT:
  1356. SM_ENTER(WPA_PTK, DISCONNECTED);
  1357. break;
  1358. case WPA_PTK_DISCONNECTED:
  1359. SM_ENTER(WPA_PTK, INITIALIZE);
  1360. break;
  1361. case WPA_PTK_AUTHENTICATION:
  1362. SM_ENTER(WPA_PTK, AUTHENTICATION2);
  1363. break;
  1364. case WPA_PTK_AUTHENTICATION2:
  1365. if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
  1366. wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
  1367. WPA_EAPOL_keyRun) > 0)
  1368. SM_ENTER(WPA_PTK, INITPMK);
  1369. else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
  1370. /* FIX: && 802.1X::keyRun */)
  1371. SM_ENTER(WPA_PTK, INITPSK);
  1372. break;
  1373. case WPA_PTK_INITPMK:
  1374. if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
  1375. WPA_EAPOL_keyAvailable) > 0)
  1376. SM_ENTER(WPA_PTK, PTKSTART);
  1377. else {
  1378. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1379. SM_ENTER(WPA_PTK, DISCONNECT);
  1380. }
  1381. break;
  1382. case WPA_PTK_INITPSK:
  1383. if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL))
  1384. SM_ENTER(WPA_PTK, PTKSTART);
  1385. else {
  1386. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  1387. "no PSK configured for the STA");
  1388. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1389. SM_ENTER(WPA_PTK, DISCONNECT);
  1390. }
  1391. break;
  1392. case WPA_PTK_PTKSTART:
  1393. if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1394. sm->EAPOLKeyPairwise)
  1395. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
  1396. else if (sm->TimeoutCtr >
  1397. (int) dot11RSNAConfigPairwiseUpdateCount) {
  1398. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1399. SM_ENTER(WPA_PTK, DISCONNECT);
  1400. } else if (sm->TimeoutEvt)
  1401. SM_ENTER(WPA_PTK, PTKSTART);
  1402. break;
  1403. case WPA_PTK_PTKCALCNEGOTIATING:
  1404. if (sm->MICVerified)
  1405. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
  1406. else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1407. sm->EAPOLKeyPairwise)
  1408. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
  1409. else if (sm->TimeoutEvt)
  1410. SM_ENTER(WPA_PTK, PTKSTART);
  1411. break;
  1412. case WPA_PTK_PTKCALCNEGOTIATING2:
  1413. SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
  1414. break;
  1415. case WPA_PTK_PTKINITNEGOTIATING:
  1416. if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1417. sm->EAPOLKeyPairwise && sm->MICVerified)
  1418. SM_ENTER(WPA_PTK, PTKINITDONE);
  1419. else if (sm->TimeoutCtr >
  1420. (int) dot11RSNAConfigPairwiseUpdateCount) {
  1421. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1422. SM_ENTER(WPA_PTK, DISCONNECT);
  1423. } else if (sm->TimeoutEvt)
  1424. SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
  1425. break;
  1426. case WPA_PTK_PTKINITDONE:
  1427. break;
  1428. }
  1429. }
  1430. SM_STATE(WPA_PTK_GROUP, IDLE)
  1431. {
  1432. SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
  1433. if (sm->Init) {
  1434. /* Init flag is not cleared here, so avoid busy
  1435. * loop by claiming nothing changed. */
  1436. sm->changed = FALSE;
  1437. }
  1438. sm->GTimeoutCtr = 0;
  1439. }
  1440. SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
  1441. {
  1442. u8 rsc[WPA_KEY_RSC_LEN];
  1443. struct wpa_group *gsm = sm->group;
  1444. u8 *kde, *pos, hdr[2];
  1445. size_t kde_len;
  1446. SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
  1447. if (sm->wpa == WPA_VERSION_WPA)
  1448. sm->PInitAKeys = FALSE;
  1449. sm->TimeoutEvt = FALSE;
  1450. /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
  1451. os_memset(rsc, 0, WPA_KEY_RSC_LEN);
  1452. if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
  1453. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
  1454. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1455. "sending 1/2 msg of Group Key Handshake");
  1456. if (sm->wpa == WPA_VERSION_WPA2) {
  1457. kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
  1458. ieee80211w_kde_len(sm);
  1459. kde = os_malloc(kde_len);
  1460. if (kde == NULL)
  1461. return;
  1462. pos = kde;
  1463. hdr[0] = gsm->GN & 0x03;
  1464. hdr[1] = 0;
  1465. pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
  1466. gsm->GTK[gsm->GN - 1], gsm->GTK_len);
  1467. pos = ieee80211w_kde_add(sm, pos);
  1468. } else {
  1469. kde = gsm->GTK[gsm->GN - 1];
  1470. pos = kde + gsm->GTK_len;
  1471. }
  1472. wpa_send_eapol(sm->wpa_auth, sm,
  1473. WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
  1474. WPA_KEY_INFO_ACK |
  1475. (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
  1476. rsc, gsm->GNonce, kde, pos - kde, gsm->GN, 1);
  1477. if (sm->wpa == WPA_VERSION_WPA2)
  1478. os_free(kde);
  1479. sm->GTimeoutCtr++;
  1480. }
  1481. SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
  1482. {
  1483. SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
  1484. sm->EAPOLKeyReceived = FALSE;
  1485. if (sm->GUpdateStationKeys)
  1486. sm->group->GKeyDoneStations--;
  1487. sm->GUpdateStationKeys = FALSE;
  1488. sm->GTimeoutCtr = 0;
  1489. /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
  1490. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  1491. "group key handshake completed (%s)",
  1492. sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
  1493. sm->has_GTK = TRUE;
  1494. }
  1495. SM_STATE(WPA_PTK_GROUP, KEYERROR)
  1496. {
  1497. SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
  1498. if (sm->GUpdateStationKeys)
  1499. sm->group->GKeyDoneStations--;
  1500. sm->GUpdateStationKeys = FALSE;
  1501. sm->Disconnect = TRUE;
  1502. }
  1503. SM_STEP(WPA_PTK_GROUP)
  1504. {
  1505. if (sm->Init)
  1506. SM_ENTER(WPA_PTK_GROUP, IDLE);
  1507. else switch (sm->wpa_ptk_group_state) {
  1508. case WPA_PTK_GROUP_IDLE:
  1509. if (sm->GUpdateStationKeys ||
  1510. (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
  1511. SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
  1512. break;
  1513. case WPA_PTK_GROUP_REKEYNEGOTIATING:
  1514. if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1515. !sm->EAPOLKeyPairwise && sm->MICVerified)
  1516. SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
  1517. else if (sm->GTimeoutCtr >
  1518. (int) dot11RSNAConfigGroupUpdateCount)
  1519. SM_ENTER(WPA_PTK_GROUP, KEYERROR);
  1520. else if (sm->TimeoutEvt)
  1521. SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
  1522. break;
  1523. case WPA_PTK_GROUP_KEYERROR:
  1524. SM_ENTER(WPA_PTK_GROUP, IDLE);
  1525. break;
  1526. case WPA_PTK_GROUP_REKEYESTABLISHED:
  1527. SM_ENTER(WPA_PTK_GROUP, IDLE);
  1528. break;
  1529. }
  1530. }
  1531. static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
  1532. struct wpa_group *group)
  1533. {
  1534. int ret = 0;
  1535. /* FIX: is this the correct way of getting GNonce? */
  1536. os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
  1537. inc_byte_array(group->Counter, WPA_NONCE_LEN);
  1538. wpa_gmk_to_gtk(group->GMK, wpa_auth->addr, group->GNonce,
  1539. group->GTK[group->GN - 1], group->GTK_len);
  1540. #ifdef CONFIG_IEEE80211W
  1541. if (wpa_auth->conf.ieee80211w != WPA_NO_IEEE80211W) {
  1542. if (os_get_random(group->IGTK[group->GN_igtk - 4],
  1543. WPA_IGTK_LEN) < 0) {
  1544. wpa_printf(MSG_INFO, "RSN: Failed to get new random "
  1545. "IGTK");
  1546. ret = -1;
  1547. }
  1548. wpa_hexdump_key(MSG_DEBUG, "IGTK",
  1549. group->IGTK[group->GN_igtk - 4], WPA_IGTK_LEN);
  1550. }
  1551. #endif /* CONFIG_IEEE80211W */
  1552. return ret;
  1553. }
  1554. static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
  1555. struct wpa_group *group)
  1556. {
  1557. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  1558. "GTK_INIT (VLAN-ID %d)", group->vlan_id);
  1559. group->changed = FALSE; /* GInit is not cleared here; avoid loop */
  1560. group->wpa_group_state = WPA_GROUP_GTK_INIT;
  1561. /* GTK[0..N] = 0 */
  1562. os_memset(group->GTK, 0, sizeof(group->GTK));
  1563. group->GN = 1;
  1564. group->GM = 2;
  1565. #ifdef CONFIG_IEEE80211W
  1566. group->GN_igtk = 4;
  1567. group->GM_igtk = 5;
  1568. #endif /* CONFIG_IEEE80211W */
  1569. /* GTK[GN] = CalcGTK() */
  1570. wpa_gtk_update(wpa_auth, group);
  1571. }
  1572. static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
  1573. {
  1574. if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
  1575. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1576. "Not in PTKINITDONE; skip Group Key update");
  1577. return 0;
  1578. }
  1579. sm->group->GKeyDoneStations++;
  1580. sm->GUpdateStationKeys = TRUE;
  1581. wpa_sm_step(sm);
  1582. return 0;
  1583. }
  1584. static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
  1585. struct wpa_group *group)
  1586. {
  1587. int tmp;
  1588. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  1589. "SETKEYS (VLAN-ID %d)", group->vlan_id);
  1590. group->changed = TRUE;
  1591. group->wpa_group_state = WPA_GROUP_SETKEYS;
  1592. group->GTKReKey = FALSE;
  1593. tmp = group->GM;
  1594. group->GM = group->GN;
  1595. group->GN = tmp;
  1596. #ifdef CONFIG_IEEE80211W
  1597. tmp = group->GM_igtk;
  1598. group->GM_igtk = group->GN_igtk;
  1599. group->GN_igtk = tmp;
  1600. #endif /* CONFIG_IEEE80211W */
  1601. /* "GKeyDoneStations = GNoStations" is done in more robust way by
  1602. * counting the STAs that are marked with GUpdateStationKeys instead of
  1603. * including all STAs that could be in not-yet-completed state. */
  1604. wpa_gtk_update(wpa_auth, group);
  1605. wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, NULL);
  1606. wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
  1607. group->GKeyDoneStations);
  1608. }
  1609. static void wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
  1610. struct wpa_group *group)
  1611. {
  1612. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  1613. "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
  1614. group->changed = TRUE;
  1615. group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
  1616. wpa_auth_set_key(wpa_auth, group->vlan_id,
  1617. wpa_alg_txt(wpa_auth->conf.wpa_group),
  1618. NULL, group->GN, group->GTK[group->GN - 1],
  1619. group->GTK_len);
  1620. #ifdef CONFIG_IEEE80211W
  1621. if (wpa_auth->conf.ieee80211w != WPA_NO_IEEE80211W) {
  1622. wpa_auth_set_key(wpa_auth, group->vlan_id, "IGTK",
  1623. NULL, group->GN_igtk,
  1624. group->IGTK[group->GN_igtk - 4],
  1625. WPA_IGTK_LEN);
  1626. }
  1627. #endif /* CONFIG_IEEE80211W */
  1628. }
  1629. static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
  1630. struct wpa_group *group)
  1631. {
  1632. if (group->GInit) {
  1633. wpa_group_gtk_init(wpa_auth, group);
  1634. } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
  1635. group->GTKAuthenticator) {
  1636. wpa_group_setkeysdone(wpa_auth, group);
  1637. } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
  1638. group->GTKReKey) {
  1639. wpa_group_setkeys(wpa_auth, group);
  1640. } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
  1641. if (group->GKeyDoneStations == 0)
  1642. wpa_group_setkeysdone(wpa_auth, group);
  1643. else if (group->GTKReKey)
  1644. wpa_group_setkeys(wpa_auth, group);
  1645. }
  1646. }
  1647. static void wpa_sm_step(struct wpa_state_machine *sm)
  1648. {
  1649. if (sm == NULL)
  1650. return;
  1651. if (sm->in_step_loop) {
  1652. /* This should not happen, but if it does, make sure we do not
  1653. * end up freeing the state machine too early by exiting the
  1654. * recursive call. */
  1655. wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
  1656. return;
  1657. }
  1658. sm->in_step_loop = 1;
  1659. do {
  1660. if (sm->pending_deinit)
  1661. break;
  1662. sm->changed = FALSE;
  1663. sm->wpa_auth->group->changed = FALSE;
  1664. SM_STEP_RUN(WPA_PTK);
  1665. if (sm->pending_deinit)
  1666. break;
  1667. SM_STEP_RUN(WPA_PTK_GROUP);
  1668. if (sm->pending_deinit)
  1669. break;
  1670. wpa_group_sm_step(sm->wpa_auth, sm->group);
  1671. } while (sm->changed || sm->wpa_auth->group->changed);
  1672. sm->in_step_loop = 0;
  1673. if (sm->pending_deinit) {
  1674. wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
  1675. "machine deinit for " MACSTR, MAC2STR(sm->addr));
  1676. wpa_free_sta_sm(sm);
  1677. }
  1678. }
  1679. static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
  1680. {
  1681. struct wpa_state_machine *sm = eloop_ctx;
  1682. wpa_sm_step(sm);
  1683. }
  1684. void wpa_auth_sm_notify(struct wpa_state_machine *sm)
  1685. {
  1686. if (sm == NULL)
  1687. return;
  1688. eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
  1689. }
  1690. void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
  1691. {
  1692. int tmp, i;
  1693. struct wpa_group *group;
  1694. if (wpa_auth == NULL)
  1695. return;
  1696. group = wpa_auth->group;
  1697. for (i = 0; i < 2; i++) {
  1698. tmp = group->GM;
  1699. group->GM = group->GN;
  1700. group->GN = tmp;
  1701. #ifdef CONFIG_IEEE80211W
  1702. tmp = group->GM_igtk;
  1703. group->GM_igtk = group->GN_igtk;
  1704. group->GN_igtk = tmp;
  1705. #endif /* CONFIG_IEEE80211W */
  1706. wpa_gtk_update(wpa_auth, group);
  1707. }
  1708. }
  1709. static const char * wpa_bool_txt(int bool)
  1710. {
  1711. return bool ? "TRUE" : "FALSE";
  1712. }
  1713. static int wpa_cipher_bits(int cipher)
  1714. {
  1715. switch (cipher) {
  1716. case WPA_CIPHER_CCMP:
  1717. return 128;
  1718. case WPA_CIPHER_TKIP:
  1719. return 256;
  1720. case WPA_CIPHER_WEP104:
  1721. return 104;
  1722. case WPA_CIPHER_WEP40:
  1723. return 40;
  1724. default:
  1725. return 0;
  1726. }
  1727. }
  1728. #define RSN_SUITE "%02x-%02x-%02x-%d"
  1729. #define RSN_SUITE_ARG(s) \
  1730. ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
  1731. int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
  1732. {
  1733. int len = 0, ret;
  1734. char pmkid_txt[PMKID_LEN * 2 + 1];
  1735. if (wpa_auth == NULL)
  1736. return len;
  1737. ret = os_snprintf(buf + len, buflen - len,
  1738. "dot11RSNAOptionImplemented=TRUE\n"
  1739. #ifdef CONFIG_RSN_PREAUTH
  1740. "dot11RSNAPreauthenticationImplemented=TRUE\n"
  1741. #else /* CONFIG_RSN_PREAUTH */
  1742. "dot11RSNAPreauthenticationImplemented=FALSE\n"
  1743. #endif /* CONFIG_RSN_PREAUTH */
  1744. "dot11RSNAEnabled=%s\n"
  1745. "dot11RSNAPreauthenticationEnabled=%s\n",
  1746. wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
  1747. wpa_bool_txt(wpa_auth->conf.rsn_preauth));
  1748. if (ret < 0 || (size_t) ret >= buflen - len)
  1749. return len;
  1750. len += ret;
  1751. wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
  1752. wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
  1753. ret = os_snprintf(
  1754. buf + len, buflen - len,
  1755. "dot11RSNAConfigVersion=%u\n"
  1756. "dot11RSNAConfigPairwiseKeysSupported=9999\n"
  1757. /* FIX: dot11RSNAConfigGroupCipher */
  1758. /* FIX: dot11RSNAConfigGroupRekeyMethod */
  1759. /* FIX: dot11RSNAConfigGroupRekeyTime */
  1760. /* FIX: dot11RSNAConfigGroupRekeyPackets */
  1761. "dot11RSNAConfigGroupRekeyStrict=%u\n"
  1762. "dot11RSNAConfigGroupUpdateCount=%u\n"
  1763. "dot11RSNAConfigPairwiseUpdateCount=%u\n"
  1764. "dot11RSNAConfigGroupCipherSize=%u\n"
  1765. "dot11RSNAConfigPMKLifetime=%u\n"
  1766. "dot11RSNAConfigPMKReauthThreshold=%u\n"
  1767. "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
  1768. "dot11RSNAConfigSATimeout=%u\n"
  1769. "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
  1770. "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
  1771. "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
  1772. "dot11RSNAPMKIDUsed=%s\n"
  1773. "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
  1774. "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
  1775. "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
  1776. "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
  1777. "dot11RSNA4WayHandshakeFailures=%u\n"
  1778. "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
  1779. RSN_VERSION,
  1780. !!wpa_auth->conf.wpa_strict_rekey,
  1781. dot11RSNAConfigGroupUpdateCount,
  1782. dot11RSNAConfigPairwiseUpdateCount,
  1783. wpa_cipher_bits(wpa_auth->conf.wpa_group),
  1784. dot11RSNAConfigPMKLifetime,
  1785. dot11RSNAConfigPMKReauthThreshold,
  1786. dot11RSNAConfigSATimeout,
  1787. RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
  1788. RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
  1789. RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
  1790. pmkid_txt,
  1791. RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
  1792. RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
  1793. RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
  1794. wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
  1795. wpa_auth->dot11RSNA4WayHandshakeFailures);
  1796. if (ret < 0 || (size_t) ret >= buflen - len)
  1797. return len;
  1798. len += ret;
  1799. /* TODO: dot11RSNAConfigPairwiseCiphersTable */
  1800. /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
  1801. /* Private MIB */
  1802. ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
  1803. wpa_auth->group->wpa_group_state);
  1804. if (ret < 0 || (size_t) ret >= buflen - len)
  1805. return len;
  1806. len += ret;
  1807. return len;
  1808. }
  1809. int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
  1810. {
  1811. int len = 0, ret;
  1812. u32 pairwise = 0;
  1813. if (sm == NULL)
  1814. return 0;
  1815. /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
  1816. /* dot11RSNAStatsEntry */
  1817. if (sm->wpa == WPA_VERSION_WPA) {
  1818. if (sm->pairwise == WPA_CIPHER_CCMP)
  1819. pairwise = WPA_CIPHER_SUITE_CCMP;
  1820. else if (sm->pairwise == WPA_CIPHER_TKIP)
  1821. pairwise = WPA_CIPHER_SUITE_TKIP;
  1822. else if (sm->pairwise == WPA_CIPHER_WEP104)
  1823. pairwise = WPA_CIPHER_SUITE_WEP104;
  1824. else if (sm->pairwise == WPA_CIPHER_WEP40)
  1825. pairwise = WPA_CIPHER_SUITE_WEP40;
  1826. else if (sm->pairwise == WPA_CIPHER_NONE)
  1827. pairwise = WPA_CIPHER_SUITE_NONE;
  1828. } else if (sm->wpa == WPA_VERSION_WPA2) {
  1829. if (sm->pairwise == WPA_CIPHER_CCMP)
  1830. pairwise = RSN_CIPHER_SUITE_CCMP;
  1831. else if (sm->pairwise == WPA_CIPHER_TKIP)
  1832. pairwise = RSN_CIPHER_SUITE_TKIP;
  1833. else if (sm->pairwise == WPA_CIPHER_WEP104)
  1834. pairwise = RSN_CIPHER_SUITE_WEP104;
  1835. else if (sm->pairwise == WPA_CIPHER_WEP40)
  1836. pairwise = RSN_CIPHER_SUITE_WEP40;
  1837. else if (sm->pairwise == WPA_CIPHER_NONE)
  1838. pairwise = RSN_CIPHER_SUITE_NONE;
  1839. } else
  1840. return 0;
  1841. ret = os_snprintf(
  1842. buf + len, buflen - len,
  1843. /* TODO: dot11RSNAStatsIndex */
  1844. "dot11RSNAStatsSTAAddress=" MACSTR "\n"
  1845. "dot11RSNAStatsVersion=1\n"
  1846. "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
  1847. /* TODO: dot11RSNAStatsTKIPICVErrors */
  1848. "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
  1849. "dot11RSNAStatsTKIPRemoveMICFailures=%u\n"
  1850. /* TODO: dot11RSNAStatsCCMPReplays */
  1851. /* TODO: dot11RSNAStatsCCMPDecryptErrors */
  1852. /* TODO: dot11RSNAStatsTKIPReplays */,
  1853. MAC2STR(sm->addr),
  1854. RSN_SUITE_ARG(pairwise),
  1855. sm->dot11RSNAStatsTKIPLocalMICFailures,
  1856. sm->dot11RSNAStatsTKIPRemoteMICFailures);
  1857. if (ret < 0 || (size_t) ret >= buflen - len)
  1858. return len;
  1859. len += ret;
  1860. /* Private MIB */
  1861. ret = os_snprintf(buf + len, buflen - len,
  1862. "hostapdWPAPTKState=%d\n"
  1863. "hostapdWPAPTKGroupState=%d\n",
  1864. sm->wpa_ptk_state,
  1865. sm->wpa_ptk_group_state);
  1866. if (ret < 0 || (size_t) ret >= buflen - len)
  1867. return len;
  1868. len += ret;
  1869. return len;
  1870. }
  1871. void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
  1872. {
  1873. if (wpa_auth)
  1874. wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
  1875. }
  1876. int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
  1877. {
  1878. return sm && sm->pairwise_set;
  1879. }
  1880. int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
  1881. {
  1882. if (sm == NULL)
  1883. return -1;
  1884. return sm->wpa_key_mgmt;
  1885. }
  1886. int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
  1887. {
  1888. if (sm == NULL)
  1889. return 0;
  1890. return sm->wpa;
  1891. }
  1892. int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
  1893. struct rsn_pmksa_cache_entry *entry)
  1894. {
  1895. if (sm == NULL || sm->pmksa != entry)
  1896. return -1;
  1897. sm->pmksa = NULL;
  1898. return 0;
  1899. }
  1900. struct rsn_pmksa_cache_entry *
  1901. wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
  1902. {
  1903. return sm ? sm->pmksa : NULL;
  1904. }
  1905. void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
  1906. {
  1907. if (sm)
  1908. sm->dot11RSNAStatsTKIPLocalMICFailures++;
  1909. }
  1910. const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
  1911. {
  1912. if (wpa_auth == NULL)
  1913. return NULL;
  1914. *len = wpa_auth->wpa_ie_len;
  1915. return wpa_auth->wpa_ie;
  1916. }
  1917. int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
  1918. int session_timeout, struct eapol_state_machine *eapol)
  1919. {
  1920. if (sm == NULL || sm->wpa != WPA_VERSION_WPA2)
  1921. return -1;
  1922. if (pmksa_cache_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
  1923. sm->wpa_auth->addr, sm->addr, session_timeout,
  1924. eapol, sm->wpa_key_mgmt))
  1925. return 0;
  1926. return -1;
  1927. }
  1928. int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
  1929. const u8 *pmk, size_t len, const u8 *sta_addr,
  1930. int session_timeout,
  1931. struct eapol_state_machine *eapol)
  1932. {
  1933. if (wpa_auth == NULL)
  1934. return -1;
  1935. if (pmksa_cache_add(wpa_auth->pmksa, pmk, len, wpa_auth->addr,
  1936. sta_addr, session_timeout, eapol,
  1937. WPA_KEY_MGMT_IEEE8021X))
  1938. return 0;
  1939. return -1;
  1940. }
  1941. static struct wpa_group *
  1942. wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
  1943. {
  1944. struct wpa_group *group;
  1945. if (wpa_auth == NULL || wpa_auth->group == NULL)
  1946. return NULL;
  1947. wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
  1948. vlan_id);
  1949. group = wpa_group_init(wpa_auth, vlan_id);
  1950. if (group == NULL)
  1951. return NULL;
  1952. group->next = wpa_auth->group->next;
  1953. wpa_auth->group->next = group;
  1954. return group;
  1955. }
  1956. int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
  1957. {
  1958. struct wpa_group *group;
  1959. if (sm == NULL || sm->wpa_auth == NULL)
  1960. return 0;
  1961. group = sm->wpa_auth->group;
  1962. while (group) {
  1963. if (group->vlan_id == vlan_id)
  1964. break;
  1965. group = group->next;
  1966. }
  1967. if (group == NULL) {
  1968. group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
  1969. if (group == NULL)
  1970. return -1;
  1971. }
  1972. if (sm->group == group)
  1973. return 0;
  1974. wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
  1975. "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
  1976. sm->group = group;
  1977. return 0;
  1978. }
  1979. #endif /* CONFIG_NATIVE_WINDOWS */