wpa_auth.c 69 KB

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