wpa.c 61 KB

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