wpa.c 61 KB

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