wpa.c 60 KB

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