wpa.c 63 KB

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