wpa_auth.c 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191
  1. /*
  2. * IEEE 802.11 RSN / WPA Authenticator
  3. * Copyright (c) 2004-2013, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "utils/state_machine.h"
  12. #include "utils/bitfield.h"
  13. #include "common/ieee802_11_defs.h"
  14. #include "crypto/aes_wrap.h"
  15. #include "crypto/crypto.h"
  16. #include "crypto/sha1.h"
  17. #include "crypto/sha256.h"
  18. #include "crypto/random.h"
  19. #include "eapol_auth/eapol_auth_sm.h"
  20. #include "ap_config.h"
  21. #include "ieee802_11.h"
  22. #include "wpa_auth.h"
  23. #include "pmksa_cache_auth.h"
  24. #include "wpa_auth_i.h"
  25. #include "wpa_auth_ie.h"
  26. #define STATE_MACHINE_DATA struct wpa_state_machine
  27. #define STATE_MACHINE_DEBUG_PREFIX "WPA"
  28. #define STATE_MACHINE_ADDR sm->addr
  29. static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
  30. static int wpa_sm_step(struct wpa_state_machine *sm);
  31. static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len);
  32. static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
  33. static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
  34. struct wpa_group *group);
  35. static void wpa_request_new_ptk(struct wpa_state_machine *sm);
  36. static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
  37. struct wpa_group *group);
  38. static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
  39. struct wpa_group *group);
  40. static const u32 dot11RSNAConfigGroupUpdateCount = 4;
  41. static const u32 dot11RSNAConfigPairwiseUpdateCount = 4;
  42. static const u32 eapol_key_timeout_first = 100; /* ms */
  43. static const u32 eapol_key_timeout_subseq = 1000; /* ms */
  44. static const u32 eapol_key_timeout_first_group = 500; /* ms */
  45. /* TODO: make these configurable */
  46. static const int dot11RSNAConfigPMKLifetime = 43200;
  47. static const int dot11RSNAConfigPMKReauthThreshold = 70;
  48. static const int dot11RSNAConfigSATimeout = 60;
  49. static inline int wpa_auth_mic_failure_report(
  50. struct wpa_authenticator *wpa_auth, const u8 *addr)
  51. {
  52. if (wpa_auth->cb.mic_failure_report)
  53. return wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr);
  54. return 0;
  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,
  72. const u8 *p2p_dev_addr,
  73. const u8 *prev_psk)
  74. {
  75. if (wpa_auth->cb.get_psk == NULL)
  76. return NULL;
  77. return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, p2p_dev_addr,
  78. prev_psk);
  79. }
  80. static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
  81. const u8 *addr, u8 *msk, size_t *len)
  82. {
  83. if (wpa_auth->cb.get_msk == NULL)
  84. return -1;
  85. return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len);
  86. }
  87. static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
  88. int vlan_id,
  89. enum wpa_alg alg, const u8 *addr, int idx,
  90. u8 *key, size_t key_len)
  91. {
  92. if (wpa_auth->cb.set_key == NULL)
  93. return -1;
  94. return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
  95. key, key_len);
  96. }
  97. static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
  98. const u8 *addr, int idx, u8 *seq)
  99. {
  100. if (wpa_auth->cb.get_seqnum == NULL)
  101. return -1;
  102. return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
  103. }
  104. static inline int
  105. wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
  106. const u8 *data, size_t data_len, int encrypt)
  107. {
  108. if (wpa_auth->cb.send_eapol == NULL)
  109. return -1;
  110. return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
  111. encrypt);
  112. }
  113. int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
  114. int (*cb)(struct wpa_state_machine *sm, void *ctx),
  115. void *cb_ctx)
  116. {
  117. if (wpa_auth->cb.for_each_sta == NULL)
  118. return 0;
  119. return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
  120. }
  121. int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
  122. int (*cb)(struct wpa_authenticator *a, void *ctx),
  123. void *cb_ctx)
  124. {
  125. if (wpa_auth->cb.for_each_auth == NULL)
  126. return 0;
  127. return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
  128. }
  129. void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
  130. logger_level level, const char *txt)
  131. {
  132. if (wpa_auth->cb.logger == NULL)
  133. return;
  134. wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
  135. }
  136. void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
  137. logger_level level, const char *fmt, ...)
  138. {
  139. char *format;
  140. int maxlen;
  141. va_list ap;
  142. if (wpa_auth->cb.logger == NULL)
  143. return;
  144. maxlen = os_strlen(fmt) + 100;
  145. format = os_malloc(maxlen);
  146. if (!format)
  147. return;
  148. va_start(ap, fmt);
  149. vsnprintf(format, maxlen, fmt, ap);
  150. va_end(ap);
  151. wpa_auth_logger(wpa_auth, addr, level, format);
  152. os_free(format);
  153. }
  154. static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
  155. const u8 *addr)
  156. {
  157. if (wpa_auth->cb.disconnect == NULL)
  158. return;
  159. wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR, MAC2STR(addr));
  160. wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
  161. WLAN_REASON_PREV_AUTH_NOT_VALID);
  162. }
  163. static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
  164. {
  165. int ret = 0;
  166. #ifdef CONFIG_IEEE80211R
  167. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
  168. ret = 1;
  169. #endif /* CONFIG_IEEE80211R */
  170. #ifdef CONFIG_IEEE80211W
  171. if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
  172. ret = 1;
  173. #endif /* CONFIG_IEEE80211W */
  174. if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN)
  175. ret = 1;
  176. return ret;
  177. }
  178. static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
  179. {
  180. struct wpa_authenticator *wpa_auth = eloop_ctx;
  181. if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
  182. wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
  183. "initialization.");
  184. } else {
  185. wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
  186. wpa_hexdump_key(MSG_DEBUG, "GMK",
  187. wpa_auth->group->GMK, WPA_GMK_LEN);
  188. }
  189. if (wpa_auth->conf.wpa_gmk_rekey) {
  190. eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
  191. wpa_rekey_gmk, wpa_auth, NULL);
  192. }
  193. }
  194. static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
  195. {
  196. struct wpa_authenticator *wpa_auth = eloop_ctx;
  197. struct wpa_group *group;
  198. wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
  199. for (group = wpa_auth->group; group; group = group->next) {
  200. group->GTKReKey = TRUE;
  201. do {
  202. group->changed = FALSE;
  203. wpa_group_sm_step(wpa_auth, group);
  204. } while (group->changed);
  205. }
  206. if (wpa_auth->conf.wpa_group_rekey) {
  207. eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
  208. 0, wpa_rekey_gtk, wpa_auth, NULL);
  209. }
  210. }
  211. static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
  212. {
  213. struct wpa_authenticator *wpa_auth = eloop_ctx;
  214. struct wpa_state_machine *sm = timeout_ctx;
  215. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
  216. wpa_request_new_ptk(sm);
  217. wpa_sm_step(sm);
  218. }
  219. static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
  220. {
  221. if (sm->pmksa == ctx)
  222. sm->pmksa = NULL;
  223. return 0;
  224. }
  225. static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
  226. void *ctx)
  227. {
  228. struct wpa_authenticator *wpa_auth = ctx;
  229. wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
  230. }
  231. static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
  232. struct wpa_group *group)
  233. {
  234. u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
  235. u8 rkey[32];
  236. unsigned long ptr;
  237. if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
  238. return -1;
  239. wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
  240. /*
  241. * Counter = PRF-256(Random number, "Init Counter",
  242. * Local MAC Address || Time)
  243. */
  244. os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
  245. wpa_get_ntp_timestamp(buf + ETH_ALEN);
  246. ptr = (unsigned long) group;
  247. os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
  248. if (random_get_bytes(rkey, sizeof(rkey)) < 0)
  249. return -1;
  250. if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
  251. group->Counter, WPA_NONCE_LEN) < 0)
  252. return -1;
  253. wpa_hexdump_key(MSG_DEBUG, "Key Counter",
  254. group->Counter, WPA_NONCE_LEN);
  255. return 0;
  256. }
  257. static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
  258. int vlan_id, int delay_init)
  259. {
  260. struct wpa_group *group;
  261. group = os_zalloc(sizeof(struct wpa_group));
  262. if (group == NULL)
  263. return NULL;
  264. group->GTKAuthenticator = TRUE;
  265. group->vlan_id = vlan_id;
  266. group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
  267. if (random_pool_ready() != 1) {
  268. wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
  269. "for secure operations - update keys later when "
  270. "the first station connects");
  271. }
  272. /*
  273. * Set initial GMK/Counter value here. The actual values that will be
  274. * used in negotiations will be set once the first station tries to
  275. * connect. This allows more time for collecting additional randomness
  276. * on embedded devices.
  277. */
  278. if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
  279. wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
  280. "initialization.");
  281. os_free(group);
  282. return NULL;
  283. }
  284. group->GInit = TRUE;
  285. if (delay_init) {
  286. wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start "
  287. "until Beacon frames have been configured");
  288. /* Initialization is completed in wpa_init_keys(). */
  289. } else {
  290. wpa_group_sm_step(wpa_auth, group);
  291. group->GInit = FALSE;
  292. wpa_group_sm_step(wpa_auth, group);
  293. }
  294. return group;
  295. }
  296. /**
  297. * wpa_init - Initialize WPA authenticator
  298. * @addr: Authenticator address
  299. * @conf: Configuration for WPA authenticator
  300. * @cb: Callback functions for WPA authenticator
  301. * Returns: Pointer to WPA authenticator data or %NULL on failure
  302. */
  303. struct wpa_authenticator * wpa_init(const u8 *addr,
  304. struct wpa_auth_config *conf,
  305. struct wpa_auth_callbacks *cb)
  306. {
  307. struct wpa_authenticator *wpa_auth;
  308. wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
  309. if (wpa_auth == NULL)
  310. return NULL;
  311. os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
  312. os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
  313. os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
  314. if (wpa_auth_gen_wpa_ie(wpa_auth)) {
  315. wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
  316. os_free(wpa_auth);
  317. return NULL;
  318. }
  319. wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
  320. if (wpa_auth->group == NULL) {
  321. os_free(wpa_auth->wpa_ie);
  322. os_free(wpa_auth);
  323. return NULL;
  324. }
  325. wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
  326. wpa_auth);
  327. if (wpa_auth->pmksa == NULL) {
  328. wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
  329. os_free(wpa_auth->wpa_ie);
  330. os_free(wpa_auth);
  331. return NULL;
  332. }
  333. #ifdef CONFIG_IEEE80211R
  334. wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
  335. if (wpa_auth->ft_pmk_cache == NULL) {
  336. wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
  337. os_free(wpa_auth->wpa_ie);
  338. pmksa_cache_auth_deinit(wpa_auth->pmksa);
  339. os_free(wpa_auth);
  340. return NULL;
  341. }
  342. #endif /* CONFIG_IEEE80211R */
  343. if (wpa_auth->conf.wpa_gmk_rekey) {
  344. eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
  345. wpa_rekey_gmk, wpa_auth, NULL);
  346. }
  347. if (wpa_auth->conf.wpa_group_rekey) {
  348. eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
  349. wpa_rekey_gtk, wpa_auth, NULL);
  350. }
  351. #ifdef CONFIG_P2P
  352. if (WPA_GET_BE32(conf->ip_addr_start)) {
  353. int count = WPA_GET_BE32(conf->ip_addr_end) -
  354. WPA_GET_BE32(conf->ip_addr_start) + 1;
  355. if (count > 1000)
  356. count = 1000;
  357. if (count > 0)
  358. wpa_auth->ip_pool = bitfield_alloc(count);
  359. }
  360. #endif /* CONFIG_P2P */
  361. return wpa_auth;
  362. }
  363. int wpa_init_keys(struct wpa_authenticator *wpa_auth)
  364. {
  365. struct wpa_group *group = wpa_auth->group;
  366. wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial "
  367. "keys");
  368. wpa_group_sm_step(wpa_auth, group);
  369. group->GInit = FALSE;
  370. wpa_group_sm_step(wpa_auth, group);
  371. if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
  372. return -1;
  373. return 0;
  374. }
  375. /**
  376. * wpa_deinit - Deinitialize WPA authenticator
  377. * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
  378. */
  379. void wpa_deinit(struct wpa_authenticator *wpa_auth)
  380. {
  381. struct wpa_group *group, *prev;
  382. eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
  383. eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
  384. #ifdef CONFIG_PEERKEY
  385. while (wpa_auth->stsl_negotiations)
  386. wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
  387. #endif /* CONFIG_PEERKEY */
  388. pmksa_cache_auth_deinit(wpa_auth->pmksa);
  389. #ifdef CONFIG_IEEE80211R
  390. wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
  391. wpa_auth->ft_pmk_cache = NULL;
  392. #endif /* CONFIG_IEEE80211R */
  393. #ifdef CONFIG_P2P
  394. bitfield_free(wpa_auth->ip_pool);
  395. #endif /* CONFIG_P2P */
  396. os_free(wpa_auth->wpa_ie);
  397. group = wpa_auth->group;
  398. while (group) {
  399. prev = group;
  400. group = group->next;
  401. os_free(prev);
  402. }
  403. os_free(wpa_auth);
  404. }
  405. /**
  406. * wpa_reconfig - Update WPA authenticator configuration
  407. * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
  408. * @conf: Configuration for WPA authenticator
  409. */
  410. int wpa_reconfig(struct wpa_authenticator *wpa_auth,
  411. struct wpa_auth_config *conf)
  412. {
  413. struct wpa_group *group;
  414. if (wpa_auth == NULL)
  415. return 0;
  416. os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
  417. if (wpa_auth_gen_wpa_ie(wpa_auth)) {
  418. wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
  419. return -1;
  420. }
  421. /*
  422. * Reinitialize GTK to make sure it is suitable for the new
  423. * configuration.
  424. */
  425. group = wpa_auth->group;
  426. group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
  427. group->GInit = TRUE;
  428. wpa_group_sm_step(wpa_auth, group);
  429. group->GInit = FALSE;
  430. wpa_group_sm_step(wpa_auth, group);
  431. return 0;
  432. }
  433. struct wpa_state_machine *
  434. wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
  435. const u8 *p2p_dev_addr)
  436. {
  437. struct wpa_state_machine *sm;
  438. if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
  439. return NULL;
  440. sm = os_zalloc(sizeof(struct wpa_state_machine));
  441. if (sm == NULL)
  442. return NULL;
  443. os_memcpy(sm->addr, addr, ETH_ALEN);
  444. if (p2p_dev_addr)
  445. os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
  446. sm->wpa_auth = wpa_auth;
  447. sm->group = wpa_auth->group;
  448. return sm;
  449. }
  450. int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
  451. struct wpa_state_machine *sm)
  452. {
  453. if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
  454. return -1;
  455. #ifdef CONFIG_IEEE80211R
  456. if (sm->ft_completed) {
  457. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  458. "FT authentication already completed - do not "
  459. "start 4-way handshake");
  460. return 0;
  461. }
  462. #endif /* CONFIG_IEEE80211R */
  463. if (sm->started) {
  464. os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
  465. sm->ReAuthenticationRequest = TRUE;
  466. return wpa_sm_step(sm);
  467. }
  468. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  469. "start authentication");
  470. sm->started = 1;
  471. sm->Init = TRUE;
  472. if (wpa_sm_step(sm) == 1)
  473. return 1; /* should not really happen */
  474. sm->Init = FALSE;
  475. sm->AuthenticationRequest = TRUE;
  476. return wpa_sm_step(sm);
  477. }
  478. void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
  479. {
  480. /* WPA/RSN was not used - clear WPA state. This is needed if the STA
  481. * reassociates back to the same AP while the previous entry for the
  482. * STA has not yet been removed. */
  483. if (sm == NULL)
  484. return;
  485. sm->wpa_key_mgmt = 0;
  486. }
  487. static void wpa_free_sta_sm(struct wpa_state_machine *sm)
  488. {
  489. #ifdef CONFIG_P2P
  490. if (WPA_GET_BE32(sm->ip_addr)) {
  491. u32 start;
  492. wpa_printf(MSG_DEBUG, "P2P: Free assigned IP "
  493. "address %u.%u.%u.%u from " MACSTR,
  494. sm->ip_addr[0], sm->ip_addr[1],
  495. sm->ip_addr[2], sm->ip_addr[3],
  496. MAC2STR(sm->addr));
  497. start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start);
  498. bitfield_clear(sm->wpa_auth->ip_pool,
  499. WPA_GET_BE32(sm->ip_addr) - start);
  500. }
  501. #endif /* CONFIG_P2P */
  502. if (sm->GUpdateStationKeys) {
  503. sm->group->GKeyDoneStations--;
  504. sm->GUpdateStationKeys = FALSE;
  505. }
  506. #ifdef CONFIG_IEEE80211R
  507. os_free(sm->assoc_resp_ftie);
  508. #endif /* CONFIG_IEEE80211R */
  509. os_free(sm->last_rx_eapol_key);
  510. os_free(sm->wpa_ie);
  511. os_free(sm);
  512. }
  513. void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
  514. {
  515. if (sm == NULL)
  516. return;
  517. if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
  518. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  519. "strict rekeying - force GTK rekey since STA "
  520. "is leaving");
  521. eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
  522. eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
  523. NULL);
  524. }
  525. eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
  526. sm->pending_1_of_4_timeout = 0;
  527. eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
  528. eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
  529. if (sm->in_step_loop) {
  530. /* Must not free state machine while wpa_sm_step() is running.
  531. * Freeing will be completed in the end of wpa_sm_step(). */
  532. wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
  533. "machine deinit for " MACSTR, MAC2STR(sm->addr));
  534. sm->pending_deinit = 1;
  535. } else
  536. wpa_free_sta_sm(sm);
  537. }
  538. static void wpa_request_new_ptk(struct wpa_state_machine *sm)
  539. {
  540. if (sm == NULL)
  541. return;
  542. sm->PTKRequest = TRUE;
  543. sm->PTK_valid = 0;
  544. }
  545. static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
  546. const u8 *replay_counter)
  547. {
  548. int i;
  549. for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
  550. if (!ctr[i].valid)
  551. break;
  552. if (os_memcmp(replay_counter, ctr[i].counter,
  553. WPA_REPLAY_COUNTER_LEN) == 0)
  554. return 1;
  555. }
  556. return 0;
  557. }
  558. static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
  559. const u8 *replay_counter)
  560. {
  561. int i;
  562. for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
  563. if (ctr[i].valid &&
  564. (replay_counter == NULL ||
  565. os_memcmp(replay_counter, ctr[i].counter,
  566. WPA_REPLAY_COUNTER_LEN) == 0))
  567. ctr[i].valid = FALSE;
  568. }
  569. }
  570. #ifdef CONFIG_IEEE80211R
  571. static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
  572. struct wpa_state_machine *sm,
  573. struct wpa_eapol_ie_parse *kde)
  574. {
  575. struct wpa_ie_data ie;
  576. struct rsn_mdie *mdie;
  577. if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
  578. ie.num_pmkid != 1 || ie.pmkid == NULL) {
  579. wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
  580. "FT 4-way handshake message 2/4");
  581. return -1;
  582. }
  583. os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
  584. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
  585. sm->sup_pmk_r1_name, PMKID_LEN);
  586. if (!kde->mdie || !kde->ftie) {
  587. wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
  588. "message 2/4", kde->mdie ? "FTIE" : "MDIE");
  589. return -1;
  590. }
  591. mdie = (struct rsn_mdie *) (kde->mdie + 2);
  592. if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
  593. os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
  594. MOBILITY_DOMAIN_ID_LEN) != 0) {
  595. wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
  596. return -1;
  597. }
  598. if (sm->assoc_resp_ftie &&
  599. (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
  600. os_memcmp(kde->ftie, sm->assoc_resp_ftie,
  601. 2 + sm->assoc_resp_ftie[1]) != 0)) {
  602. wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
  603. wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
  604. kde->ftie, kde->ftie_len);
  605. wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
  606. sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
  607. return -1;
  608. }
  609. return 0;
  610. }
  611. #endif /* CONFIG_IEEE80211R */
  612. static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
  613. struct wpa_state_machine *sm, int group)
  614. {
  615. /* Supplicant reported a Michael MIC error */
  616. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  617. "received EAPOL-Key Error Request "
  618. "(STA detected Michael MIC failure (group=%d))",
  619. group);
  620. if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
  621. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  622. "ignore Michael MIC failure report since "
  623. "group cipher is not TKIP");
  624. } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
  625. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  626. "ignore Michael MIC failure report since "
  627. "pairwise cipher is not TKIP");
  628. } else {
  629. if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
  630. return 1; /* STA entry was removed */
  631. sm->dot11RSNAStatsTKIPRemoteMICFailures++;
  632. wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
  633. }
  634. /*
  635. * Error report is not a request for a new key handshake, but since
  636. * Authenticator may do it, let's change the keys now anyway.
  637. */
  638. wpa_request_new_ptk(sm);
  639. return 0;
  640. }
  641. void wpa_receive(struct wpa_authenticator *wpa_auth,
  642. struct wpa_state_machine *sm,
  643. u8 *data, size_t data_len)
  644. {
  645. struct ieee802_1x_hdr *hdr;
  646. struct wpa_eapol_key *key;
  647. u16 key_info, key_data_length;
  648. enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
  649. SMK_M1, SMK_M3, SMK_ERROR } msg;
  650. char *msgtxt;
  651. struct wpa_eapol_ie_parse kde;
  652. int ft;
  653. const u8 *eapol_key_ie;
  654. size_t eapol_key_ie_len;
  655. if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
  656. return;
  657. if (data_len < sizeof(*hdr) + sizeof(*key))
  658. return;
  659. hdr = (struct ieee802_1x_hdr *) data;
  660. key = (struct wpa_eapol_key *) (hdr + 1);
  661. key_info = WPA_GET_BE16(key->key_info);
  662. key_data_length = WPA_GET_BE16(key->key_data_length);
  663. wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
  664. " key_info=0x%x type=%u key_data_length=%u",
  665. MAC2STR(sm->addr), key_info, key->type, key_data_length);
  666. if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) {
  667. wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
  668. "key_data overflow (%d > %lu)",
  669. key_data_length,
  670. (unsigned long) (data_len - sizeof(*hdr) -
  671. sizeof(*key)));
  672. return;
  673. }
  674. if (sm->wpa == WPA_VERSION_WPA2) {
  675. if (key->type == EAPOL_KEY_TYPE_WPA) {
  676. /*
  677. * Some deployed station implementations seem to send
  678. * msg 4/4 with incorrect type value in WPA2 mode.
  679. */
  680. wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
  681. "with unexpected WPA type in RSN mode");
  682. } else if (key->type != EAPOL_KEY_TYPE_RSN) {
  683. wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
  684. "unexpected type %d in RSN mode",
  685. key->type);
  686. return;
  687. }
  688. } else {
  689. if (key->type != EAPOL_KEY_TYPE_WPA) {
  690. wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
  691. "unexpected type %d in WPA mode",
  692. key->type);
  693. return;
  694. }
  695. }
  696. wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
  697. WPA_NONCE_LEN);
  698. wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
  699. key->replay_counter, WPA_REPLAY_COUNTER_LEN);
  700. /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
  701. * are set */
  702. if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
  703. (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
  704. if (key_info & WPA_KEY_INFO_ERROR) {
  705. msg = SMK_ERROR;
  706. msgtxt = "SMK Error";
  707. } else {
  708. msg = SMK_M1;
  709. msgtxt = "SMK M1";
  710. }
  711. } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
  712. msg = SMK_M3;
  713. msgtxt = "SMK M3";
  714. } else if (key_info & WPA_KEY_INFO_REQUEST) {
  715. msg = REQUEST;
  716. msgtxt = "Request";
  717. } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
  718. msg = GROUP_2;
  719. msgtxt = "2/2 Group";
  720. } else if (key_data_length == 0) {
  721. msg = PAIRWISE_4;
  722. msgtxt = "4/4 Pairwise";
  723. } else {
  724. msg = PAIRWISE_2;
  725. msgtxt = "2/4 Pairwise";
  726. }
  727. /* TODO: key_info type validation for PeerKey */
  728. if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
  729. msg == GROUP_2) {
  730. u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  731. if (sm->pairwise == WPA_CIPHER_CCMP ||
  732. sm->pairwise == WPA_CIPHER_GCMP) {
  733. if (wpa_use_aes_cmac(sm) &&
  734. sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN &&
  735. ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  736. wpa_auth_logger(wpa_auth, sm->addr,
  737. LOGGER_WARNING,
  738. "advertised support for "
  739. "AES-128-CMAC, but did not "
  740. "use it");
  741. return;
  742. }
  743. if (!wpa_use_aes_cmac(sm) &&
  744. ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
  745. wpa_auth_logger(wpa_auth, sm->addr,
  746. LOGGER_WARNING,
  747. "did not use HMAC-SHA1-AES "
  748. "with CCMP/GCMP");
  749. return;
  750. }
  751. }
  752. }
  753. if (key_info & WPA_KEY_INFO_REQUEST) {
  754. if (sm->req_replay_counter_used &&
  755. os_memcmp(key->replay_counter, sm->req_replay_counter,
  756. WPA_REPLAY_COUNTER_LEN) <= 0) {
  757. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
  758. "received EAPOL-Key request with "
  759. "replayed counter");
  760. return;
  761. }
  762. }
  763. if (!(key_info & WPA_KEY_INFO_REQUEST) &&
  764. !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
  765. int i;
  766. if (msg == PAIRWISE_2 &&
  767. wpa_replay_counter_valid(sm->prev_key_replay,
  768. key->replay_counter) &&
  769. sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
  770. os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
  771. {
  772. /*
  773. * Some supplicant implementations (e.g., Windows XP
  774. * WZC) update SNonce for each EAPOL-Key 2/4. This
  775. * breaks the workaround on accepting any of the
  776. * pending requests, so allow the SNonce to be updated
  777. * even if we have already sent out EAPOL-Key 3/4.
  778. */
  779. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  780. "Process SNonce update from STA "
  781. "based on retransmitted EAPOL-Key "
  782. "1/4");
  783. sm->update_snonce = 1;
  784. wpa_replay_counter_mark_invalid(sm->prev_key_replay,
  785. key->replay_counter);
  786. goto continue_processing;
  787. }
  788. if (msg == PAIRWISE_2 &&
  789. wpa_replay_counter_valid(sm->prev_key_replay,
  790. key->replay_counter) &&
  791. sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
  792. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  793. "ignore retransmitted EAPOL-Key %s - "
  794. "SNonce did not change", msgtxt);
  795. } else {
  796. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  797. "received EAPOL-Key %s with "
  798. "unexpected replay counter", msgtxt);
  799. }
  800. for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
  801. if (!sm->key_replay[i].valid)
  802. break;
  803. wpa_hexdump(MSG_DEBUG, "pending replay counter",
  804. sm->key_replay[i].counter,
  805. WPA_REPLAY_COUNTER_LEN);
  806. }
  807. wpa_hexdump(MSG_DEBUG, "received replay counter",
  808. key->replay_counter, WPA_REPLAY_COUNTER_LEN);
  809. return;
  810. }
  811. continue_processing:
  812. switch (msg) {
  813. case PAIRWISE_2:
  814. if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
  815. sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
  816. (!sm->update_snonce ||
  817. sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
  818. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  819. "received EAPOL-Key msg 2/4 in "
  820. "invalid state (%d) - dropped",
  821. sm->wpa_ptk_state);
  822. return;
  823. }
  824. random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
  825. if (sm->group->reject_4way_hs_for_entropy) {
  826. /*
  827. * The system did not have enough entropy to generate
  828. * strong random numbers. Reject the first 4-way
  829. * handshake(s) and collect some entropy based on the
  830. * information from it. Once enough entropy is
  831. * available, the next atempt will trigger GMK/Key
  832. * Counter update and the station will be allowed to
  833. * continue.
  834. */
  835. wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
  836. "collect more entropy for random number "
  837. "generation");
  838. random_mark_pool_ready();
  839. wpa_sta_disconnect(wpa_auth, sm->addr);
  840. return;
  841. }
  842. if (wpa_parse_kde_ies((u8 *) (key + 1), key_data_length,
  843. &kde) < 0) {
  844. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  845. "received EAPOL-Key msg 2/4 with "
  846. "invalid Key Data contents");
  847. return;
  848. }
  849. if (kde.rsn_ie) {
  850. eapol_key_ie = kde.rsn_ie;
  851. eapol_key_ie_len = kde.rsn_ie_len;
  852. } else if (kde.osen) {
  853. eapol_key_ie = kde.osen;
  854. eapol_key_ie_len = kde.osen_len;
  855. } else {
  856. eapol_key_ie = kde.wpa_ie;
  857. eapol_key_ie_len = kde.wpa_ie_len;
  858. }
  859. ft = sm->wpa == WPA_VERSION_WPA2 &&
  860. wpa_key_mgmt_ft(sm->wpa_key_mgmt);
  861. if (sm->wpa_ie == NULL ||
  862. wpa_compare_rsn_ie(ft,
  863. sm->wpa_ie, sm->wpa_ie_len,
  864. eapol_key_ie, eapol_key_ie_len)) {
  865. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  866. "WPA IE from (Re)AssocReq did not "
  867. "match with msg 2/4");
  868. if (sm->wpa_ie) {
  869. wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
  870. sm->wpa_ie, sm->wpa_ie_len);
  871. }
  872. wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
  873. eapol_key_ie, eapol_key_ie_len);
  874. /* MLME-DEAUTHENTICATE.request */
  875. wpa_sta_disconnect(wpa_auth, sm->addr);
  876. return;
  877. }
  878. #ifdef CONFIG_IEEE80211R
  879. if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
  880. wpa_sta_disconnect(wpa_auth, sm->addr);
  881. return;
  882. }
  883. #endif /* CONFIG_IEEE80211R */
  884. #ifdef CONFIG_P2P
  885. if (kde.ip_addr_req && kde.ip_addr_req[0] &&
  886. wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
  887. int idx;
  888. wpa_printf(MSG_DEBUG, "P2P: IP address requested in "
  889. "EAPOL-Key exchange");
  890. idx = bitfield_get_first_zero(wpa_auth->ip_pool);
  891. if (idx >= 0) {
  892. u32 start = WPA_GET_BE32(wpa_auth->conf.
  893. ip_addr_start);
  894. bitfield_set(wpa_auth->ip_pool, idx);
  895. WPA_PUT_BE32(sm->ip_addr, start + idx);
  896. wpa_printf(MSG_DEBUG, "P2P: Assigned IP "
  897. "address %u.%u.%u.%u to " MACSTR,
  898. sm->ip_addr[0], sm->ip_addr[1],
  899. sm->ip_addr[2], sm->ip_addr[3],
  900. MAC2STR(sm->addr));
  901. }
  902. }
  903. #endif /* CONFIG_P2P */
  904. break;
  905. case PAIRWISE_4:
  906. if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
  907. !sm->PTK_valid) {
  908. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  909. "received EAPOL-Key msg 4/4 in "
  910. "invalid state (%d) - dropped",
  911. sm->wpa_ptk_state);
  912. return;
  913. }
  914. break;
  915. case GROUP_2:
  916. if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
  917. || !sm->PTK_valid) {
  918. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  919. "received EAPOL-Key msg 2/2 in "
  920. "invalid state (%d) - dropped",
  921. sm->wpa_ptk_group_state);
  922. return;
  923. }
  924. break;
  925. #ifdef CONFIG_PEERKEY
  926. case SMK_M1:
  927. case SMK_M3:
  928. case SMK_ERROR:
  929. if (!wpa_auth->conf.peerkey) {
  930. wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
  931. "PeerKey use disabled - ignoring message");
  932. return;
  933. }
  934. if (!sm->PTK_valid) {
  935. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  936. "received EAPOL-Key msg SMK in "
  937. "invalid state - dropped");
  938. return;
  939. }
  940. break;
  941. #else /* CONFIG_PEERKEY */
  942. case SMK_M1:
  943. case SMK_M3:
  944. case SMK_ERROR:
  945. return; /* STSL disabled - ignore SMK messages */
  946. #endif /* CONFIG_PEERKEY */
  947. case REQUEST:
  948. break;
  949. }
  950. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  951. "received EAPOL-Key frame (%s)", msgtxt);
  952. if (key_info & WPA_KEY_INFO_ACK) {
  953. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  954. "received invalid EAPOL-Key: Key Ack set");
  955. return;
  956. }
  957. if (!(key_info & WPA_KEY_INFO_MIC)) {
  958. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  959. "received invalid EAPOL-Key: Key MIC not set");
  960. return;
  961. }
  962. sm->MICVerified = FALSE;
  963. if (sm->PTK_valid && !sm->update_snonce) {
  964. if (wpa_verify_key_mic(&sm->PTK, data, data_len)) {
  965. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  966. "received EAPOL-Key with invalid MIC");
  967. return;
  968. }
  969. sm->MICVerified = TRUE;
  970. eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
  971. sm->pending_1_of_4_timeout = 0;
  972. }
  973. if (key_info & WPA_KEY_INFO_REQUEST) {
  974. if (sm->MICVerified) {
  975. sm->req_replay_counter_used = 1;
  976. os_memcpy(sm->req_replay_counter, key->replay_counter,
  977. WPA_REPLAY_COUNTER_LEN);
  978. } else {
  979. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  980. "received EAPOL-Key request with "
  981. "invalid MIC");
  982. return;
  983. }
  984. /*
  985. * TODO: should decrypt key data field if encryption was used;
  986. * even though MAC address KDE is not normally encrypted,
  987. * supplicant is allowed to encrypt it.
  988. */
  989. if (msg == SMK_ERROR) {
  990. #ifdef CONFIG_PEERKEY
  991. wpa_smk_error(wpa_auth, sm, key);
  992. #endif /* CONFIG_PEERKEY */
  993. return;
  994. } else if (key_info & WPA_KEY_INFO_ERROR) {
  995. if (wpa_receive_error_report(
  996. wpa_auth, sm,
  997. !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
  998. return; /* STA entry was removed */
  999. } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
  1000. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1001. "received EAPOL-Key Request for new "
  1002. "4-Way Handshake");
  1003. wpa_request_new_ptk(sm);
  1004. #ifdef CONFIG_PEERKEY
  1005. } else if (msg == SMK_M1) {
  1006. wpa_smk_m1(wpa_auth, sm, key);
  1007. #endif /* CONFIG_PEERKEY */
  1008. } else if (key_data_length > 0 &&
  1009. wpa_parse_kde_ies((const u8 *) (key + 1),
  1010. key_data_length, &kde) == 0 &&
  1011. kde.mac_addr) {
  1012. } else {
  1013. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1014. "received EAPOL-Key Request for GTK "
  1015. "rekeying");
  1016. eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
  1017. wpa_rekey_gtk(wpa_auth, NULL);
  1018. }
  1019. } else {
  1020. /* Do not allow the same key replay counter to be reused. */
  1021. wpa_replay_counter_mark_invalid(sm->key_replay,
  1022. key->replay_counter);
  1023. if (msg == PAIRWISE_2) {
  1024. /*
  1025. * Maintain a copy of the pending EAPOL-Key frames in
  1026. * case the EAPOL-Key frame was retransmitted. This is
  1027. * needed to allow EAPOL-Key msg 2/4 reply to another
  1028. * pending msg 1/4 to update the SNonce to work around
  1029. * unexpected supplicant behavior.
  1030. */
  1031. os_memcpy(sm->prev_key_replay, sm->key_replay,
  1032. sizeof(sm->key_replay));
  1033. } else {
  1034. os_memset(sm->prev_key_replay, 0,
  1035. sizeof(sm->prev_key_replay));
  1036. }
  1037. /*
  1038. * Make sure old valid counters are not accepted anymore and
  1039. * do not get copied again.
  1040. */
  1041. wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
  1042. }
  1043. #ifdef CONFIG_PEERKEY
  1044. if (msg == SMK_M3) {
  1045. wpa_smk_m3(wpa_auth, sm, key);
  1046. return;
  1047. }
  1048. #endif /* CONFIG_PEERKEY */
  1049. os_free(sm->last_rx_eapol_key);
  1050. sm->last_rx_eapol_key = os_malloc(data_len);
  1051. if (sm->last_rx_eapol_key == NULL)
  1052. return;
  1053. os_memcpy(sm->last_rx_eapol_key, data, data_len);
  1054. sm->last_rx_eapol_key_len = data_len;
  1055. sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
  1056. sm->EAPOLKeyReceived = TRUE;
  1057. sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
  1058. sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
  1059. os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
  1060. wpa_sm_step(sm);
  1061. }
  1062. static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
  1063. const u8 *gnonce, u8 *gtk, size_t gtk_len)
  1064. {
  1065. u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + 16];
  1066. u8 *pos;
  1067. int ret = 0;
  1068. /* GTK = PRF-X(GMK, "Group key expansion",
  1069. * AA || GNonce || Time || random data)
  1070. * The example described in the IEEE 802.11 standard uses only AA and
  1071. * GNonce as inputs here. Add some more entropy since this derivation
  1072. * is done only at the Authenticator and as such, does not need to be
  1073. * exactly same.
  1074. */
  1075. os_memcpy(data, addr, ETH_ALEN);
  1076. os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
  1077. pos = data + ETH_ALEN + WPA_NONCE_LEN;
  1078. wpa_get_ntp_timestamp(pos);
  1079. pos += 8;
  1080. if (random_get_bytes(pos, 16) < 0)
  1081. ret = -1;
  1082. #ifdef CONFIG_IEEE80211W
  1083. sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len);
  1084. #else /* CONFIG_IEEE80211W */
  1085. if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len)
  1086. < 0)
  1087. ret = -1;
  1088. #endif /* CONFIG_IEEE80211W */
  1089. return ret;
  1090. }
  1091. static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
  1092. {
  1093. struct wpa_authenticator *wpa_auth = eloop_ctx;
  1094. struct wpa_state_machine *sm = timeout_ctx;
  1095. sm->pending_1_of_4_timeout = 0;
  1096. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
  1097. sm->TimeoutEvt = TRUE;
  1098. wpa_sm_step(sm);
  1099. }
  1100. void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
  1101. struct wpa_state_machine *sm, int key_info,
  1102. const u8 *key_rsc, const u8 *nonce,
  1103. const u8 *kde, size_t kde_len,
  1104. int keyidx, int encr, int force_version)
  1105. {
  1106. struct ieee802_1x_hdr *hdr;
  1107. struct wpa_eapol_key *key;
  1108. size_t len;
  1109. int alg;
  1110. int key_data_len, pad_len = 0;
  1111. u8 *buf, *pos;
  1112. int version, pairwise;
  1113. int i;
  1114. len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
  1115. if (force_version)
  1116. version = force_version;
  1117. else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN)
  1118. version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
  1119. else if (wpa_use_aes_cmac(sm))
  1120. version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
  1121. else if (sm->pairwise != WPA_CIPHER_TKIP)
  1122. version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
  1123. else
  1124. version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
  1125. pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
  1126. wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
  1127. "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
  1128. "encr=%d)",
  1129. version,
  1130. (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
  1131. (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
  1132. (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
  1133. (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
  1134. pairwise, (unsigned long) kde_len, keyidx, encr);
  1135. key_data_len = kde_len;
  1136. if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
  1137. sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
  1138. version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
  1139. pad_len = key_data_len % 8;
  1140. if (pad_len)
  1141. pad_len = 8 - pad_len;
  1142. key_data_len += pad_len + 8;
  1143. }
  1144. len += key_data_len;
  1145. hdr = os_zalloc(len);
  1146. if (hdr == NULL)
  1147. return;
  1148. hdr->version = wpa_auth->conf.eapol_version;
  1149. hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
  1150. hdr->length = host_to_be16(len - sizeof(*hdr));
  1151. key = (struct wpa_eapol_key *) (hdr + 1);
  1152. key->type = sm->wpa == WPA_VERSION_WPA2 ?
  1153. EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
  1154. key_info |= version;
  1155. if (encr && sm->wpa == WPA_VERSION_WPA2)
  1156. key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
  1157. if (sm->wpa != WPA_VERSION_WPA2)
  1158. key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
  1159. WPA_PUT_BE16(key->key_info, key_info);
  1160. alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
  1161. WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
  1162. if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
  1163. WPA_PUT_BE16(key->key_length, 0);
  1164. /* FIX: STSL: what to use as key_replay_counter? */
  1165. for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
  1166. sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
  1167. os_memcpy(sm->key_replay[i].counter,
  1168. sm->key_replay[i - 1].counter,
  1169. WPA_REPLAY_COUNTER_LEN);
  1170. }
  1171. inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
  1172. os_memcpy(key->replay_counter, sm->key_replay[0].counter,
  1173. WPA_REPLAY_COUNTER_LEN);
  1174. sm->key_replay[0].valid = TRUE;
  1175. if (nonce)
  1176. os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
  1177. if (key_rsc)
  1178. os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
  1179. if (kde && !encr) {
  1180. os_memcpy(key + 1, kde, kde_len);
  1181. WPA_PUT_BE16(key->key_data_length, kde_len);
  1182. } else if (encr && kde) {
  1183. buf = os_zalloc(key_data_len);
  1184. if (buf == NULL) {
  1185. os_free(hdr);
  1186. return;
  1187. }
  1188. pos = buf;
  1189. os_memcpy(pos, kde, kde_len);
  1190. pos += kde_len;
  1191. if (pad_len)
  1192. *pos++ = 0xdd;
  1193. wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
  1194. buf, key_data_len);
  1195. if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
  1196. sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
  1197. version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  1198. if (aes_wrap(sm->PTK.kek, (key_data_len - 8) / 8, buf,
  1199. (u8 *) (key + 1))) {
  1200. os_free(hdr);
  1201. os_free(buf);
  1202. return;
  1203. }
  1204. WPA_PUT_BE16(key->key_data_length, key_data_len);
  1205. } else {
  1206. u8 ek[32];
  1207. os_memcpy(key->key_iv,
  1208. sm->group->Counter + WPA_NONCE_LEN - 16, 16);
  1209. inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
  1210. os_memcpy(ek, key->key_iv, 16);
  1211. os_memcpy(ek + 16, sm->PTK.kek, 16);
  1212. os_memcpy(key + 1, buf, key_data_len);
  1213. rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
  1214. WPA_PUT_BE16(key->key_data_length, key_data_len);
  1215. }
  1216. os_free(buf);
  1217. }
  1218. if (key_info & WPA_KEY_INFO_MIC) {
  1219. if (!sm->PTK_valid) {
  1220. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  1221. "PTK not valid when sending EAPOL-Key "
  1222. "frame");
  1223. os_free(hdr);
  1224. return;
  1225. }
  1226. wpa_eapol_key_mic(sm->PTK.kck, version, (u8 *) hdr, len,
  1227. key->key_mic);
  1228. #ifdef CONFIG_TESTING_OPTIONS
  1229. if (!pairwise &&
  1230. wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0d &&
  1231. drand48() <
  1232. wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
  1233. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1234. "Corrupting group EAPOL-Key Key MIC");
  1235. key->key_mic[0]++;
  1236. }
  1237. #endif /* CONFIG_TESTING_OPTIONS */
  1238. }
  1239. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
  1240. 1);
  1241. wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
  1242. sm->pairwise_set);
  1243. os_free(hdr);
  1244. }
  1245. static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
  1246. struct wpa_state_machine *sm, int key_info,
  1247. const u8 *key_rsc, const u8 *nonce,
  1248. const u8 *kde, size_t kde_len,
  1249. int keyidx, int encr)
  1250. {
  1251. int timeout_ms;
  1252. int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
  1253. int ctr;
  1254. if (sm == NULL)
  1255. return;
  1256. __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
  1257. keyidx, encr, 0);
  1258. ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
  1259. if (ctr == 1 && wpa_auth->conf.tx_status)
  1260. timeout_ms = pairwise ? eapol_key_timeout_first :
  1261. eapol_key_timeout_first_group;
  1262. else
  1263. timeout_ms = eapol_key_timeout_subseq;
  1264. if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
  1265. sm->pending_1_of_4_timeout = 1;
  1266. wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
  1267. "counter %d)", timeout_ms, ctr);
  1268. eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
  1269. wpa_send_eapol_timeout, wpa_auth, sm);
  1270. }
  1271. static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len)
  1272. {
  1273. struct ieee802_1x_hdr *hdr;
  1274. struct wpa_eapol_key *key;
  1275. u16 key_info;
  1276. int ret = 0;
  1277. u8 mic[16];
  1278. if (data_len < sizeof(*hdr) + sizeof(*key))
  1279. return -1;
  1280. hdr = (struct ieee802_1x_hdr *) data;
  1281. key = (struct wpa_eapol_key *) (hdr + 1);
  1282. key_info = WPA_GET_BE16(key->key_info);
  1283. os_memcpy(mic, key->key_mic, 16);
  1284. os_memset(key->key_mic, 0, 16);
  1285. if (wpa_eapol_key_mic(PTK->kck, key_info & WPA_KEY_INFO_TYPE_MASK,
  1286. data, data_len, key->key_mic) ||
  1287. os_memcmp(mic, key->key_mic, 16) != 0)
  1288. ret = -1;
  1289. os_memcpy(key->key_mic, mic, 16);
  1290. return ret;
  1291. }
  1292. void wpa_remove_ptk(struct wpa_state_machine *sm)
  1293. {
  1294. sm->PTK_valid = FALSE;
  1295. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  1296. wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL, 0);
  1297. sm->pairwise_set = FALSE;
  1298. eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
  1299. }
  1300. int wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
  1301. {
  1302. int remove_ptk = 1;
  1303. if (sm == NULL)
  1304. return -1;
  1305. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1306. "event %d notification", event);
  1307. switch (event) {
  1308. case WPA_AUTH:
  1309. case WPA_ASSOC:
  1310. break;
  1311. case WPA_DEAUTH:
  1312. case WPA_DISASSOC:
  1313. sm->DeauthenticationRequest = TRUE;
  1314. break;
  1315. case WPA_REAUTH:
  1316. case WPA_REAUTH_EAPOL:
  1317. if (!sm->started) {
  1318. /*
  1319. * When using WPS, we may end up here if the STA
  1320. * manages to re-associate without the previous STA
  1321. * entry getting removed. Consequently, we need to make
  1322. * sure that the WPA state machines gets initialized
  1323. * properly at this point.
  1324. */
  1325. wpa_printf(MSG_DEBUG, "WPA state machine had not been "
  1326. "started - initialize now");
  1327. sm->started = 1;
  1328. sm->Init = TRUE;
  1329. if (wpa_sm_step(sm) == 1)
  1330. return 1; /* should not really happen */
  1331. sm->Init = FALSE;
  1332. sm->AuthenticationRequest = TRUE;
  1333. break;
  1334. }
  1335. if (sm->GUpdateStationKeys) {
  1336. /*
  1337. * Reauthentication cancels the pending group key
  1338. * update for this STA.
  1339. */
  1340. sm->group->GKeyDoneStations--;
  1341. sm->GUpdateStationKeys = FALSE;
  1342. sm->PtkGroupInit = TRUE;
  1343. }
  1344. sm->ReAuthenticationRequest = TRUE;
  1345. break;
  1346. case WPA_ASSOC_FT:
  1347. #ifdef CONFIG_IEEE80211R
  1348. wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
  1349. "after association");
  1350. wpa_ft_install_ptk(sm);
  1351. /* Using FT protocol, not WPA auth state machine */
  1352. sm->ft_completed = 1;
  1353. return 0;
  1354. #else /* CONFIG_IEEE80211R */
  1355. break;
  1356. #endif /* CONFIG_IEEE80211R */
  1357. }
  1358. #ifdef CONFIG_IEEE80211R
  1359. sm->ft_completed = 0;
  1360. #endif /* CONFIG_IEEE80211R */
  1361. #ifdef CONFIG_IEEE80211W
  1362. if (sm->mgmt_frame_prot && event == WPA_AUTH)
  1363. remove_ptk = 0;
  1364. #endif /* CONFIG_IEEE80211W */
  1365. if (remove_ptk) {
  1366. sm->PTK_valid = FALSE;
  1367. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  1368. if (event != WPA_REAUTH_EAPOL)
  1369. wpa_remove_ptk(sm);
  1370. }
  1371. return wpa_sm_step(sm);
  1372. }
  1373. SM_STATE(WPA_PTK, INITIALIZE)
  1374. {
  1375. SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
  1376. if (sm->Init) {
  1377. /* Init flag is not cleared here, so avoid busy
  1378. * loop by claiming nothing changed. */
  1379. sm->changed = FALSE;
  1380. }
  1381. sm->keycount = 0;
  1382. if (sm->GUpdateStationKeys)
  1383. sm->group->GKeyDoneStations--;
  1384. sm->GUpdateStationKeys = FALSE;
  1385. if (sm->wpa == WPA_VERSION_WPA)
  1386. sm->PInitAKeys = FALSE;
  1387. if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
  1388. * Local AA > Remote AA)) */) {
  1389. sm->Pair = TRUE;
  1390. }
  1391. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
  1392. wpa_remove_ptk(sm);
  1393. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
  1394. sm->TimeoutCtr = 0;
  1395. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1396. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  1397. WPA_EAPOL_authorized, 0);
  1398. }
  1399. }
  1400. SM_STATE(WPA_PTK, DISCONNECT)
  1401. {
  1402. SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
  1403. sm->Disconnect = FALSE;
  1404. wpa_sta_disconnect(sm->wpa_auth, sm->addr);
  1405. }
  1406. SM_STATE(WPA_PTK, DISCONNECTED)
  1407. {
  1408. SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
  1409. sm->DeauthenticationRequest = FALSE;
  1410. }
  1411. SM_STATE(WPA_PTK, AUTHENTICATION)
  1412. {
  1413. SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
  1414. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  1415. sm->PTK_valid = FALSE;
  1416. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
  1417. 1);
  1418. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
  1419. sm->AuthenticationRequest = FALSE;
  1420. }
  1421. static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
  1422. struct wpa_group *group)
  1423. {
  1424. if (group->first_sta_seen)
  1425. return;
  1426. /*
  1427. * System has run bit further than at the time hostapd was started
  1428. * potentially very early during boot up. This provides better chances
  1429. * of collecting more randomness on embedded systems. Re-initialize the
  1430. * GMK and Counter here to improve their strength if there was not
  1431. * enough entropy available immediately after system startup.
  1432. */
  1433. wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
  1434. "station");
  1435. if (random_pool_ready() != 1) {
  1436. wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
  1437. "to proceed - reject first 4-way handshake");
  1438. group->reject_4way_hs_for_entropy = TRUE;
  1439. } else {
  1440. group->first_sta_seen = TRUE;
  1441. group->reject_4way_hs_for_entropy = FALSE;
  1442. }
  1443. wpa_group_init_gmk_and_counter(wpa_auth, group);
  1444. wpa_gtk_update(wpa_auth, group);
  1445. wpa_group_config_group_keys(wpa_auth, group);
  1446. }
  1447. SM_STATE(WPA_PTK, AUTHENTICATION2)
  1448. {
  1449. SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
  1450. wpa_group_ensure_init(sm->wpa_auth, sm->group);
  1451. sm->ReAuthenticationRequest = FALSE;
  1452. /*
  1453. * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
  1454. * ambiguous. The Authenticator state machine uses a counter that is
  1455. * incremented by one for each 4-way handshake. However, the security
  1456. * analysis of 4-way handshake points out that unpredictable nonces
  1457. * help in preventing precomputation attacks. Instead of the state
  1458. * machine definition, use an unpredictable nonce value here to provide
  1459. * stronger protection against potential precomputation attacks.
  1460. */
  1461. if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
  1462. wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
  1463. "ANonce.");
  1464. sm->Disconnect = TRUE;
  1465. return;
  1466. }
  1467. wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
  1468. WPA_NONCE_LEN);
  1469. /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
  1470. * logical place than INITIALIZE since AUTHENTICATION2 can be
  1471. * re-entered on ReAuthenticationRequest without going through
  1472. * INITIALIZE. */
  1473. sm->TimeoutCtr = 0;
  1474. }
  1475. SM_STATE(WPA_PTK, INITPMK)
  1476. {
  1477. u8 msk[2 * PMK_LEN];
  1478. size_t len = 2 * PMK_LEN;
  1479. SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
  1480. #ifdef CONFIG_IEEE80211R
  1481. sm->xxkey_len = 0;
  1482. #endif /* CONFIG_IEEE80211R */
  1483. if (sm->pmksa) {
  1484. wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
  1485. os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
  1486. } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
  1487. wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
  1488. "(len=%lu)", (unsigned long) len);
  1489. os_memcpy(sm->PMK, msk, PMK_LEN);
  1490. #ifdef CONFIG_IEEE80211R
  1491. if (len >= 2 * PMK_LEN) {
  1492. os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
  1493. sm->xxkey_len = PMK_LEN;
  1494. }
  1495. #endif /* CONFIG_IEEE80211R */
  1496. } else {
  1497. wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
  1498. }
  1499. sm->req_replay_counter_used = 0;
  1500. /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
  1501. * will break reauthentication since EAPOL state machines may not be
  1502. * get into AUTHENTICATING state that clears keyRun before WPA state
  1503. * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
  1504. * state and takes PMK from the previously used AAA Key. This will
  1505. * eventually fail in 4-Way Handshake because Supplicant uses PMK
  1506. * derived from the new AAA Key. Setting keyRun = FALSE here seems to
  1507. * be good workaround for this issue. */
  1508. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
  1509. }
  1510. SM_STATE(WPA_PTK, INITPSK)
  1511. {
  1512. const u8 *psk;
  1513. SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
  1514. psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL);
  1515. if (psk) {
  1516. os_memcpy(sm->PMK, psk, PMK_LEN);
  1517. #ifdef CONFIG_IEEE80211R
  1518. os_memcpy(sm->xxkey, psk, PMK_LEN);
  1519. sm->xxkey_len = PMK_LEN;
  1520. #endif /* CONFIG_IEEE80211R */
  1521. }
  1522. sm->req_replay_counter_used = 0;
  1523. }
  1524. SM_STATE(WPA_PTK, PTKSTART)
  1525. {
  1526. u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
  1527. size_t pmkid_len = 0;
  1528. SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
  1529. sm->PTKRequest = FALSE;
  1530. sm->TimeoutEvt = FALSE;
  1531. sm->TimeoutCtr++;
  1532. if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
  1533. /* No point in sending the EAPOL-Key - we will disconnect
  1534. * immediately following this. */
  1535. return;
  1536. }
  1537. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1538. "sending 1/4 msg of 4-Way Handshake");
  1539. /*
  1540. * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
  1541. * one possible PSK for this STA.
  1542. */
  1543. if (sm->wpa == WPA_VERSION_WPA2 &&
  1544. wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
  1545. sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
  1546. pmkid = buf;
  1547. pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
  1548. pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
  1549. pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
  1550. RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
  1551. if (sm->pmksa)
  1552. os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
  1553. sm->pmksa->pmkid, PMKID_LEN);
  1554. else {
  1555. /*
  1556. * Calculate PMKID since no PMKSA cache entry was
  1557. * available with pre-calculated PMKID.
  1558. */
  1559. rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
  1560. sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
  1561. wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
  1562. }
  1563. }
  1564. wpa_send_eapol(sm->wpa_auth, sm,
  1565. WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
  1566. sm->ANonce, pmkid, pmkid_len, 0, 0);
  1567. }
  1568. static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk,
  1569. struct wpa_ptk *ptk)
  1570. {
  1571. size_t ptk_len = sm->pairwise != WPA_CIPHER_TKIP ? 48 : 64;
  1572. #ifdef CONFIG_IEEE80211R
  1573. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
  1574. return wpa_auth_derive_ptk_ft(sm, pmk, ptk, ptk_len);
  1575. #endif /* CONFIG_IEEE80211R */
  1576. wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
  1577. sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce,
  1578. (u8 *) ptk, ptk_len,
  1579. wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
  1580. return 0;
  1581. }
  1582. SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
  1583. {
  1584. struct wpa_ptk PTK;
  1585. int ok = 0;
  1586. const u8 *pmk = NULL;
  1587. SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
  1588. sm->EAPOLKeyReceived = FALSE;
  1589. sm->update_snonce = FALSE;
  1590. /* WPA with IEEE 802.1X: use the derived PMK from EAP
  1591. * WPA-PSK: iterate through possible PSKs and select the one matching
  1592. * the packet */
  1593. for (;;) {
  1594. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1595. pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
  1596. sm->p2p_dev_addr, pmk);
  1597. if (pmk == NULL)
  1598. break;
  1599. } else
  1600. pmk = sm->PMK;
  1601. wpa_derive_ptk(sm, pmk, &PTK);
  1602. if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key,
  1603. sm->last_rx_eapol_key_len) == 0) {
  1604. ok = 1;
  1605. break;
  1606. }
  1607. if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
  1608. break;
  1609. }
  1610. if (!ok) {
  1611. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1612. "invalid MIC in msg 2/4 of 4-Way Handshake");
  1613. return;
  1614. }
  1615. #ifdef CONFIG_IEEE80211R
  1616. if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
  1617. /*
  1618. * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
  1619. * with the value we derived.
  1620. */
  1621. if (os_memcmp(sm->sup_pmk_r1_name, sm->pmk_r1_name,
  1622. WPA_PMK_NAME_LEN) != 0) {
  1623. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1624. "PMKR1Name mismatch in FT 4-way "
  1625. "handshake");
  1626. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
  1627. "Supplicant",
  1628. sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
  1629. wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
  1630. sm->pmk_r1_name, WPA_PMK_NAME_LEN);
  1631. return;
  1632. }
  1633. }
  1634. #endif /* CONFIG_IEEE80211R */
  1635. sm->pending_1_of_4_timeout = 0;
  1636. eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
  1637. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1638. /* PSK may have changed from the previous choice, so update
  1639. * state machine data based on whatever PSK was selected here.
  1640. */
  1641. os_memcpy(sm->PMK, pmk, PMK_LEN);
  1642. }
  1643. sm->MICVerified = TRUE;
  1644. os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
  1645. sm->PTK_valid = TRUE;
  1646. }
  1647. SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
  1648. {
  1649. SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
  1650. sm->TimeoutCtr = 0;
  1651. }
  1652. #ifdef CONFIG_IEEE80211W
  1653. static int ieee80211w_kde_len(struct wpa_state_machine *sm)
  1654. {
  1655. if (sm->mgmt_frame_prot) {
  1656. return 2 + RSN_SELECTOR_LEN + sizeof(struct wpa_igtk_kde);
  1657. }
  1658. return 0;
  1659. }
  1660. static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
  1661. {
  1662. struct wpa_igtk_kde igtk;
  1663. struct wpa_group *gsm = sm->group;
  1664. u8 rsc[WPA_KEY_RSC_LEN];
  1665. if (!sm->mgmt_frame_prot)
  1666. return pos;
  1667. igtk.keyid[0] = gsm->GN_igtk;
  1668. igtk.keyid[1] = 0;
  1669. if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
  1670. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
  1671. os_memset(igtk.pn, 0, sizeof(igtk.pn));
  1672. else
  1673. os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
  1674. os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
  1675. if (sm->wpa_auth->conf.disable_gtk) {
  1676. /*
  1677. * Provide unique random IGTK to each STA to prevent use of
  1678. * IGTK in the BSS.
  1679. */
  1680. if (random_get_bytes(igtk.igtk, WPA_IGTK_LEN) < 0)
  1681. return pos;
  1682. }
  1683. pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
  1684. (const u8 *) &igtk, sizeof(igtk), NULL, 0);
  1685. return pos;
  1686. }
  1687. #else /* CONFIG_IEEE80211W */
  1688. static int ieee80211w_kde_len(struct wpa_state_machine *sm)
  1689. {
  1690. return 0;
  1691. }
  1692. static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
  1693. {
  1694. return pos;
  1695. }
  1696. #endif /* CONFIG_IEEE80211W */
  1697. SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
  1698. {
  1699. u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
  1700. size_t gtk_len, kde_len;
  1701. struct wpa_group *gsm = sm->group;
  1702. u8 *wpa_ie;
  1703. int wpa_ie_len, secure, keyidx, encr = 0;
  1704. SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
  1705. sm->TimeoutEvt = FALSE;
  1706. sm->TimeoutCtr++;
  1707. if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
  1708. /* No point in sending the EAPOL-Key - we will disconnect
  1709. * immediately following this. */
  1710. return;
  1711. }
  1712. /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
  1713. GTK[GN], IGTK, [FTIE], [TIE * 2])
  1714. */
  1715. os_memset(rsc, 0, WPA_KEY_RSC_LEN);
  1716. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
  1717. /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
  1718. wpa_ie = sm->wpa_auth->wpa_ie;
  1719. wpa_ie_len = sm->wpa_auth->wpa_ie_len;
  1720. if (sm->wpa == WPA_VERSION_WPA &&
  1721. (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
  1722. wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
  1723. /* WPA-only STA, remove RSN IE */
  1724. wpa_ie = wpa_ie + wpa_ie[1] + 2;
  1725. wpa_ie_len = wpa_ie[1] + 2;
  1726. }
  1727. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1728. "sending 3/4 msg of 4-Way Handshake");
  1729. if (sm->wpa == WPA_VERSION_WPA2) {
  1730. /* WPA2 send GTK in the 4-way handshake */
  1731. secure = 1;
  1732. gtk = gsm->GTK[gsm->GN - 1];
  1733. gtk_len = gsm->GTK_len;
  1734. if (sm->wpa_auth->conf.disable_gtk) {
  1735. /*
  1736. * Provide unique random GTK to each STA to prevent use
  1737. * of GTK in the BSS.
  1738. */
  1739. if (random_get_bytes(dummy_gtk, gtk_len) < 0)
  1740. return;
  1741. gtk = dummy_gtk;
  1742. }
  1743. keyidx = gsm->GN;
  1744. _rsc = rsc;
  1745. encr = 1;
  1746. } else {
  1747. /* WPA does not include GTK in msg 3/4 */
  1748. secure = 0;
  1749. gtk = NULL;
  1750. gtk_len = 0;
  1751. keyidx = 0;
  1752. _rsc = NULL;
  1753. if (sm->rx_eapol_key_secure) {
  1754. /*
  1755. * It looks like Windows 7 supplicant tries to use
  1756. * Secure bit in msg 2/4 after having reported Michael
  1757. * MIC failure and it then rejects the 4-way handshake
  1758. * if msg 3/4 does not set Secure bit. Work around this
  1759. * by setting the Secure bit here even in the case of
  1760. * WPA if the supplicant used it first.
  1761. */
  1762. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1763. "STA used Secure bit in WPA msg 2/4 - "
  1764. "set Secure for 3/4 as workaround");
  1765. secure = 1;
  1766. }
  1767. }
  1768. kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
  1769. if (gtk)
  1770. kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
  1771. #ifdef CONFIG_IEEE80211R
  1772. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
  1773. kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
  1774. kde_len += 300; /* FTIE + 2 * TIE */
  1775. }
  1776. #endif /* CONFIG_IEEE80211R */
  1777. #ifdef CONFIG_P2P
  1778. if (WPA_GET_BE32(sm->ip_addr) > 0)
  1779. kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
  1780. #endif /* CONFIG_P2P */
  1781. kde = os_malloc(kde_len);
  1782. if (kde == NULL)
  1783. return;
  1784. pos = kde;
  1785. os_memcpy(pos, wpa_ie, wpa_ie_len);
  1786. pos += wpa_ie_len;
  1787. #ifdef CONFIG_IEEE80211R
  1788. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
  1789. int res = wpa_insert_pmkid(kde, pos - kde, sm->pmk_r1_name);
  1790. if (res < 0) {
  1791. wpa_printf(MSG_ERROR, "FT: Failed to insert "
  1792. "PMKR1Name into RSN IE in EAPOL-Key data");
  1793. os_free(kde);
  1794. return;
  1795. }
  1796. pos += res;
  1797. }
  1798. #endif /* CONFIG_IEEE80211R */
  1799. if (gtk) {
  1800. u8 hdr[2];
  1801. hdr[0] = keyidx & 0x03;
  1802. hdr[1] = 0;
  1803. pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
  1804. gtk, gtk_len);
  1805. }
  1806. pos = ieee80211w_kde_add(sm, pos);
  1807. #ifdef CONFIG_IEEE80211R
  1808. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
  1809. int res;
  1810. struct wpa_auth_config *conf;
  1811. conf = &sm->wpa_auth->conf;
  1812. res = wpa_write_ftie(conf, conf->r0_key_holder,
  1813. conf->r0_key_holder_len,
  1814. NULL, NULL, pos, kde + kde_len - pos,
  1815. NULL, 0);
  1816. if (res < 0) {
  1817. wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
  1818. "into EAPOL-Key Key Data");
  1819. os_free(kde);
  1820. return;
  1821. }
  1822. pos += res;
  1823. /* TIE[ReassociationDeadline] (TU) */
  1824. *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
  1825. *pos++ = 5;
  1826. *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
  1827. WPA_PUT_LE32(pos, conf->reassociation_deadline);
  1828. pos += 4;
  1829. /* TIE[KeyLifetime] (seconds) */
  1830. *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
  1831. *pos++ = 5;
  1832. *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
  1833. WPA_PUT_LE32(pos, conf->r0_key_lifetime * 60);
  1834. pos += 4;
  1835. }
  1836. #endif /* CONFIG_IEEE80211R */
  1837. #ifdef CONFIG_P2P
  1838. if (WPA_GET_BE32(sm->ip_addr) > 0) {
  1839. u8 addr[3 * 4];
  1840. os_memcpy(addr, sm->ip_addr, 4);
  1841. os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
  1842. os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
  1843. pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
  1844. addr, sizeof(addr), NULL, 0);
  1845. }
  1846. #endif /* CONFIG_P2P */
  1847. wpa_send_eapol(sm->wpa_auth, sm,
  1848. (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
  1849. WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
  1850. WPA_KEY_INFO_KEY_TYPE,
  1851. _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
  1852. os_free(kde);
  1853. }
  1854. SM_STATE(WPA_PTK, PTKINITDONE)
  1855. {
  1856. SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
  1857. sm->EAPOLKeyReceived = FALSE;
  1858. if (sm->Pair) {
  1859. enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
  1860. int klen = wpa_cipher_key_len(sm->pairwise);
  1861. if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
  1862. sm->PTK.tk1, klen)) {
  1863. wpa_sta_disconnect(sm->wpa_auth, sm->addr);
  1864. return;
  1865. }
  1866. /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
  1867. sm->pairwise_set = TRUE;
  1868. if (sm->wpa_auth->conf.wpa_ptk_rekey) {
  1869. eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
  1870. eloop_register_timeout(sm->wpa_auth->conf.
  1871. wpa_ptk_rekey, 0, wpa_rekey_ptk,
  1872. sm->wpa_auth, sm);
  1873. }
  1874. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  1875. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  1876. WPA_EAPOL_authorized, 1);
  1877. }
  1878. }
  1879. if (0 /* IBSS == TRUE */) {
  1880. sm->keycount++;
  1881. if (sm->keycount == 2) {
  1882. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  1883. WPA_EAPOL_portValid, 1);
  1884. }
  1885. } else {
  1886. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
  1887. 1);
  1888. }
  1889. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
  1890. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
  1891. if (sm->wpa == WPA_VERSION_WPA)
  1892. sm->PInitAKeys = TRUE;
  1893. else
  1894. sm->has_GTK = TRUE;
  1895. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  1896. "pairwise key handshake completed (%s)",
  1897. sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
  1898. #ifdef CONFIG_IEEE80211R
  1899. wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
  1900. #endif /* CONFIG_IEEE80211R */
  1901. }
  1902. SM_STEP(WPA_PTK)
  1903. {
  1904. struct wpa_authenticator *wpa_auth = sm->wpa_auth;
  1905. if (sm->Init)
  1906. SM_ENTER(WPA_PTK, INITIALIZE);
  1907. else if (sm->Disconnect
  1908. /* || FIX: dot11RSNAConfigSALifetime timeout */) {
  1909. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  1910. "WPA_PTK: sm->Disconnect");
  1911. SM_ENTER(WPA_PTK, DISCONNECT);
  1912. }
  1913. else if (sm->DeauthenticationRequest)
  1914. SM_ENTER(WPA_PTK, DISCONNECTED);
  1915. else if (sm->AuthenticationRequest)
  1916. SM_ENTER(WPA_PTK, AUTHENTICATION);
  1917. else if (sm->ReAuthenticationRequest)
  1918. SM_ENTER(WPA_PTK, AUTHENTICATION2);
  1919. else if (sm->PTKRequest)
  1920. SM_ENTER(WPA_PTK, PTKSTART);
  1921. else switch (sm->wpa_ptk_state) {
  1922. case WPA_PTK_INITIALIZE:
  1923. break;
  1924. case WPA_PTK_DISCONNECT:
  1925. SM_ENTER(WPA_PTK, DISCONNECTED);
  1926. break;
  1927. case WPA_PTK_DISCONNECTED:
  1928. SM_ENTER(WPA_PTK, INITIALIZE);
  1929. break;
  1930. case WPA_PTK_AUTHENTICATION:
  1931. SM_ENTER(WPA_PTK, AUTHENTICATION2);
  1932. break;
  1933. case WPA_PTK_AUTHENTICATION2:
  1934. if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
  1935. wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
  1936. WPA_EAPOL_keyRun) > 0)
  1937. SM_ENTER(WPA_PTK, INITPMK);
  1938. else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
  1939. /* FIX: && 802.1X::keyRun */)
  1940. SM_ENTER(WPA_PTK, INITPSK);
  1941. break;
  1942. case WPA_PTK_INITPMK:
  1943. if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
  1944. WPA_EAPOL_keyAvailable) > 0)
  1945. SM_ENTER(WPA_PTK, PTKSTART);
  1946. else {
  1947. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1948. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  1949. "INITPMK - keyAvailable = false");
  1950. SM_ENTER(WPA_PTK, DISCONNECT);
  1951. }
  1952. break;
  1953. case WPA_PTK_INITPSK:
  1954. if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
  1955. NULL))
  1956. SM_ENTER(WPA_PTK, PTKSTART);
  1957. else {
  1958. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  1959. "no PSK configured for the STA");
  1960. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1961. SM_ENTER(WPA_PTK, DISCONNECT);
  1962. }
  1963. break;
  1964. case WPA_PTK_PTKSTART:
  1965. if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1966. sm->EAPOLKeyPairwise)
  1967. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
  1968. else if (sm->TimeoutCtr >
  1969. (int) dot11RSNAConfigPairwiseUpdateCount) {
  1970. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1971. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1972. "PTKSTART: Retry limit %d reached",
  1973. dot11RSNAConfigPairwiseUpdateCount);
  1974. SM_ENTER(WPA_PTK, DISCONNECT);
  1975. } else if (sm->TimeoutEvt)
  1976. SM_ENTER(WPA_PTK, PTKSTART);
  1977. break;
  1978. case WPA_PTK_PTKCALCNEGOTIATING:
  1979. if (sm->MICVerified)
  1980. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
  1981. else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1982. sm->EAPOLKeyPairwise)
  1983. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
  1984. else if (sm->TimeoutEvt)
  1985. SM_ENTER(WPA_PTK, PTKSTART);
  1986. break;
  1987. case WPA_PTK_PTKCALCNEGOTIATING2:
  1988. SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
  1989. break;
  1990. case WPA_PTK_PTKINITNEGOTIATING:
  1991. if (sm->update_snonce)
  1992. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
  1993. else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  1994. sm->EAPOLKeyPairwise && sm->MICVerified)
  1995. SM_ENTER(WPA_PTK, PTKINITDONE);
  1996. else if (sm->TimeoutCtr >
  1997. (int) dot11RSNAConfigPairwiseUpdateCount) {
  1998. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  1999. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  2000. "PTKINITNEGOTIATING: Retry limit %d "
  2001. "reached",
  2002. dot11RSNAConfigPairwiseUpdateCount);
  2003. SM_ENTER(WPA_PTK, DISCONNECT);
  2004. } else if (sm->TimeoutEvt)
  2005. SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
  2006. break;
  2007. case WPA_PTK_PTKINITDONE:
  2008. break;
  2009. }
  2010. }
  2011. SM_STATE(WPA_PTK_GROUP, IDLE)
  2012. {
  2013. SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
  2014. if (sm->Init) {
  2015. /* Init flag is not cleared here, so avoid busy
  2016. * loop by claiming nothing changed. */
  2017. sm->changed = FALSE;
  2018. }
  2019. sm->GTimeoutCtr = 0;
  2020. }
  2021. SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
  2022. {
  2023. u8 rsc[WPA_KEY_RSC_LEN];
  2024. struct wpa_group *gsm = sm->group;
  2025. u8 *kde, *pos, hdr[2];
  2026. size_t kde_len;
  2027. u8 *gtk, dummy_gtk[32];
  2028. SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
  2029. sm->GTimeoutCtr++;
  2030. if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
  2031. /* No point in sending the EAPOL-Key - we will disconnect
  2032. * immediately following this. */
  2033. return;
  2034. }
  2035. if (sm->wpa == WPA_VERSION_WPA)
  2036. sm->PInitAKeys = FALSE;
  2037. sm->TimeoutEvt = FALSE;
  2038. /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
  2039. os_memset(rsc, 0, WPA_KEY_RSC_LEN);
  2040. if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
  2041. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
  2042. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  2043. "sending 1/2 msg of Group Key Handshake");
  2044. gtk = gsm->GTK[gsm->GN - 1];
  2045. if (sm->wpa_auth->conf.disable_gtk) {
  2046. /*
  2047. * Provide unique random GTK to each STA to prevent use
  2048. * of GTK in the BSS.
  2049. */
  2050. if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
  2051. return;
  2052. gtk = dummy_gtk;
  2053. }
  2054. if (sm->wpa == WPA_VERSION_WPA2) {
  2055. kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
  2056. ieee80211w_kde_len(sm);
  2057. kde = os_malloc(kde_len);
  2058. if (kde == NULL)
  2059. return;
  2060. pos = kde;
  2061. hdr[0] = gsm->GN & 0x03;
  2062. hdr[1] = 0;
  2063. pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
  2064. gtk, gsm->GTK_len);
  2065. pos = ieee80211w_kde_add(sm, pos);
  2066. } else {
  2067. kde = gtk;
  2068. pos = kde + gsm->GTK_len;
  2069. }
  2070. wpa_send_eapol(sm->wpa_auth, sm,
  2071. WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
  2072. WPA_KEY_INFO_ACK |
  2073. (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
  2074. rsc, gsm->GNonce, kde, pos - kde, gsm->GN, 1);
  2075. if (sm->wpa == WPA_VERSION_WPA2)
  2076. os_free(kde);
  2077. }
  2078. SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
  2079. {
  2080. SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
  2081. sm->EAPOLKeyReceived = FALSE;
  2082. if (sm->GUpdateStationKeys)
  2083. sm->group->GKeyDoneStations--;
  2084. sm->GUpdateStationKeys = FALSE;
  2085. sm->GTimeoutCtr = 0;
  2086. /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
  2087. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  2088. "group key handshake completed (%s)",
  2089. sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
  2090. sm->has_GTK = TRUE;
  2091. }
  2092. SM_STATE(WPA_PTK_GROUP, KEYERROR)
  2093. {
  2094. SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
  2095. if (sm->GUpdateStationKeys)
  2096. sm->group->GKeyDoneStations--;
  2097. sm->GUpdateStationKeys = FALSE;
  2098. sm->Disconnect = TRUE;
  2099. }
  2100. SM_STEP(WPA_PTK_GROUP)
  2101. {
  2102. if (sm->Init || sm->PtkGroupInit) {
  2103. SM_ENTER(WPA_PTK_GROUP, IDLE);
  2104. sm->PtkGroupInit = FALSE;
  2105. } else switch (sm->wpa_ptk_group_state) {
  2106. case WPA_PTK_GROUP_IDLE:
  2107. if (sm->GUpdateStationKeys ||
  2108. (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
  2109. SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
  2110. break;
  2111. case WPA_PTK_GROUP_REKEYNEGOTIATING:
  2112. if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  2113. !sm->EAPOLKeyPairwise && sm->MICVerified)
  2114. SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
  2115. else if (sm->GTimeoutCtr >
  2116. (int) dot11RSNAConfigGroupUpdateCount)
  2117. SM_ENTER(WPA_PTK_GROUP, KEYERROR);
  2118. else if (sm->TimeoutEvt)
  2119. SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
  2120. break;
  2121. case WPA_PTK_GROUP_KEYERROR:
  2122. SM_ENTER(WPA_PTK_GROUP, IDLE);
  2123. break;
  2124. case WPA_PTK_GROUP_REKEYESTABLISHED:
  2125. SM_ENTER(WPA_PTK_GROUP, IDLE);
  2126. break;
  2127. }
  2128. }
  2129. static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
  2130. struct wpa_group *group)
  2131. {
  2132. int ret = 0;
  2133. os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
  2134. inc_byte_array(group->Counter, WPA_NONCE_LEN);
  2135. if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
  2136. wpa_auth->addr, group->GNonce,
  2137. group->GTK[group->GN - 1], group->GTK_len) < 0)
  2138. ret = -1;
  2139. wpa_hexdump_key(MSG_DEBUG, "GTK",
  2140. group->GTK[group->GN - 1], group->GTK_len);
  2141. #ifdef CONFIG_IEEE80211W
  2142. if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
  2143. os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
  2144. inc_byte_array(group->Counter, WPA_NONCE_LEN);
  2145. if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
  2146. wpa_auth->addr, group->GNonce,
  2147. group->IGTK[group->GN_igtk - 4],
  2148. WPA_IGTK_LEN) < 0)
  2149. ret = -1;
  2150. wpa_hexdump_key(MSG_DEBUG, "IGTK",
  2151. group->IGTK[group->GN_igtk - 4], WPA_IGTK_LEN);
  2152. }
  2153. #endif /* CONFIG_IEEE80211W */
  2154. return ret;
  2155. }
  2156. static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
  2157. struct wpa_group *group)
  2158. {
  2159. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  2160. "GTK_INIT (VLAN-ID %d)", group->vlan_id);
  2161. group->changed = FALSE; /* GInit is not cleared here; avoid loop */
  2162. group->wpa_group_state = WPA_GROUP_GTK_INIT;
  2163. /* GTK[0..N] = 0 */
  2164. os_memset(group->GTK, 0, sizeof(group->GTK));
  2165. group->GN = 1;
  2166. group->GM = 2;
  2167. #ifdef CONFIG_IEEE80211W
  2168. group->GN_igtk = 4;
  2169. group->GM_igtk = 5;
  2170. #endif /* CONFIG_IEEE80211W */
  2171. /* GTK[GN] = CalcGTK() */
  2172. wpa_gtk_update(wpa_auth, group);
  2173. }
  2174. static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
  2175. {
  2176. if (ctx != NULL && ctx != sm->group)
  2177. return 0;
  2178. if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
  2179. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  2180. "Not in PTKINITDONE; skip Group Key update");
  2181. sm->GUpdateStationKeys = FALSE;
  2182. return 0;
  2183. }
  2184. if (sm->GUpdateStationKeys) {
  2185. /*
  2186. * This should not really happen, so add a debug log entry.
  2187. * Since we clear the GKeyDoneStations before the loop, the
  2188. * station needs to be counted here anyway.
  2189. */
  2190. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  2191. "GUpdateStationKeys was already set when "
  2192. "marking station for GTK rekeying");
  2193. }
  2194. /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
  2195. if (sm->is_wnmsleep)
  2196. return 0;
  2197. sm->group->GKeyDoneStations++;
  2198. sm->GUpdateStationKeys = TRUE;
  2199. wpa_sm_step(sm);
  2200. return 0;
  2201. }
  2202. #ifdef CONFIG_WNM
  2203. /* update GTK when exiting WNM-Sleep Mode */
  2204. void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
  2205. {
  2206. if (sm == NULL || sm->is_wnmsleep)
  2207. return;
  2208. wpa_group_update_sta(sm, NULL);
  2209. }
  2210. void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
  2211. {
  2212. if (sm)
  2213. sm->is_wnmsleep = !!flag;
  2214. }
  2215. int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
  2216. {
  2217. struct wpa_group *gsm = sm->group;
  2218. u8 *start = pos;
  2219. /*
  2220. * GTK subelement:
  2221. * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
  2222. * Key[5..32]
  2223. */
  2224. *pos++ = WNM_SLEEP_SUBELEM_GTK;
  2225. *pos++ = 11 + gsm->GTK_len;
  2226. /* Key ID in B0-B1 of Key Info */
  2227. WPA_PUT_LE16(pos, gsm->GN & 0x03);
  2228. pos += 2;
  2229. *pos++ = gsm->GTK_len;
  2230. if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
  2231. return 0;
  2232. pos += 8;
  2233. os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
  2234. pos += gsm->GTK_len;
  2235. wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
  2236. gsm->GN);
  2237. wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
  2238. gsm->GTK[gsm->GN - 1], gsm->GTK_len);
  2239. return pos - start;
  2240. }
  2241. #ifdef CONFIG_IEEE80211W
  2242. int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
  2243. {
  2244. struct wpa_group *gsm = sm->group;
  2245. u8 *start = pos;
  2246. /*
  2247. * IGTK subelement:
  2248. * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
  2249. */
  2250. *pos++ = WNM_SLEEP_SUBELEM_IGTK;
  2251. *pos++ = 2 + 6 + WPA_IGTK_LEN;
  2252. WPA_PUT_LE16(pos, gsm->GN_igtk);
  2253. pos += 2;
  2254. if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
  2255. return 0;
  2256. pos += 6;
  2257. os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
  2258. pos += WPA_IGTK_LEN;
  2259. wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
  2260. gsm->GN_igtk);
  2261. wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
  2262. gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
  2263. return pos - start;
  2264. }
  2265. #endif /* CONFIG_IEEE80211W */
  2266. #endif /* CONFIG_WNM */
  2267. static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
  2268. struct wpa_group *group)
  2269. {
  2270. int tmp;
  2271. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  2272. "SETKEYS (VLAN-ID %d)", group->vlan_id);
  2273. group->changed = TRUE;
  2274. group->wpa_group_state = WPA_GROUP_SETKEYS;
  2275. group->GTKReKey = FALSE;
  2276. tmp = group->GM;
  2277. group->GM = group->GN;
  2278. group->GN = tmp;
  2279. #ifdef CONFIG_IEEE80211W
  2280. tmp = group->GM_igtk;
  2281. group->GM_igtk = group->GN_igtk;
  2282. group->GN_igtk = tmp;
  2283. #endif /* CONFIG_IEEE80211W */
  2284. /* "GKeyDoneStations = GNoStations" is done in more robust way by
  2285. * counting the STAs that are marked with GUpdateStationKeys instead of
  2286. * including all STAs that could be in not-yet-completed state. */
  2287. wpa_gtk_update(wpa_auth, group);
  2288. if (group->GKeyDoneStations) {
  2289. wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
  2290. "GKeyDoneStations=%d when starting new GTK rekey",
  2291. group->GKeyDoneStations);
  2292. group->GKeyDoneStations = 0;
  2293. }
  2294. wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
  2295. wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
  2296. group->GKeyDoneStations);
  2297. }
  2298. static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
  2299. struct wpa_group *group)
  2300. {
  2301. int ret = 0;
  2302. if (wpa_auth_set_key(wpa_auth, group->vlan_id,
  2303. wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
  2304. broadcast_ether_addr, group->GN,
  2305. group->GTK[group->GN - 1], group->GTK_len) < 0)
  2306. ret = -1;
  2307. #ifdef CONFIG_IEEE80211W
  2308. if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION &&
  2309. wpa_auth_set_key(wpa_auth, group->vlan_id, WPA_ALG_IGTK,
  2310. broadcast_ether_addr, group->GN_igtk,
  2311. group->IGTK[group->GN_igtk - 4],
  2312. WPA_IGTK_LEN) < 0)
  2313. ret = -1;
  2314. #endif /* CONFIG_IEEE80211W */
  2315. return ret;
  2316. }
  2317. static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
  2318. {
  2319. if (sm->group == ctx) {
  2320. wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
  2321. " for discconnection due to fatal failure",
  2322. MAC2STR(sm->addr));
  2323. sm->Disconnect = TRUE;
  2324. }
  2325. return 0;
  2326. }
  2327. static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
  2328. struct wpa_group *group)
  2329. {
  2330. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
  2331. group->changed = TRUE;
  2332. group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
  2333. wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
  2334. }
  2335. static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
  2336. struct wpa_group *group)
  2337. {
  2338. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  2339. "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
  2340. group->changed = TRUE;
  2341. group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
  2342. if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
  2343. wpa_group_fatal_failure(wpa_auth, group);
  2344. return -1;
  2345. }
  2346. return 0;
  2347. }
  2348. static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
  2349. struct wpa_group *group)
  2350. {
  2351. if (group->GInit) {
  2352. wpa_group_gtk_init(wpa_auth, group);
  2353. } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
  2354. /* Do not allow group operations */
  2355. } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
  2356. group->GTKAuthenticator) {
  2357. wpa_group_setkeysdone(wpa_auth, group);
  2358. } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
  2359. group->GTKReKey) {
  2360. wpa_group_setkeys(wpa_auth, group);
  2361. } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
  2362. if (group->GKeyDoneStations == 0)
  2363. wpa_group_setkeysdone(wpa_auth, group);
  2364. else if (group->GTKReKey)
  2365. wpa_group_setkeys(wpa_auth, group);
  2366. }
  2367. }
  2368. static int wpa_sm_step(struct wpa_state_machine *sm)
  2369. {
  2370. if (sm == NULL)
  2371. return 0;
  2372. if (sm->in_step_loop) {
  2373. /* This should not happen, but if it does, make sure we do not
  2374. * end up freeing the state machine too early by exiting the
  2375. * recursive call. */
  2376. wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
  2377. return 0;
  2378. }
  2379. sm->in_step_loop = 1;
  2380. do {
  2381. if (sm->pending_deinit)
  2382. break;
  2383. sm->changed = FALSE;
  2384. sm->wpa_auth->group->changed = FALSE;
  2385. SM_STEP_RUN(WPA_PTK);
  2386. if (sm->pending_deinit)
  2387. break;
  2388. SM_STEP_RUN(WPA_PTK_GROUP);
  2389. if (sm->pending_deinit)
  2390. break;
  2391. wpa_group_sm_step(sm->wpa_auth, sm->group);
  2392. } while (sm->changed || sm->wpa_auth->group->changed);
  2393. sm->in_step_loop = 0;
  2394. if (sm->pending_deinit) {
  2395. wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
  2396. "machine deinit for " MACSTR, MAC2STR(sm->addr));
  2397. wpa_free_sta_sm(sm);
  2398. return 1;
  2399. }
  2400. return 0;
  2401. }
  2402. static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
  2403. {
  2404. struct wpa_state_machine *sm = eloop_ctx;
  2405. wpa_sm_step(sm);
  2406. }
  2407. void wpa_auth_sm_notify(struct wpa_state_machine *sm)
  2408. {
  2409. if (sm == NULL)
  2410. return;
  2411. eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
  2412. }
  2413. void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
  2414. {
  2415. int tmp, i;
  2416. struct wpa_group *group;
  2417. if (wpa_auth == NULL)
  2418. return;
  2419. group = wpa_auth->group;
  2420. for (i = 0; i < 2; i++) {
  2421. tmp = group->GM;
  2422. group->GM = group->GN;
  2423. group->GN = tmp;
  2424. #ifdef CONFIG_IEEE80211W
  2425. tmp = group->GM_igtk;
  2426. group->GM_igtk = group->GN_igtk;
  2427. group->GN_igtk = tmp;
  2428. #endif /* CONFIG_IEEE80211W */
  2429. wpa_gtk_update(wpa_auth, group);
  2430. wpa_group_config_group_keys(wpa_auth, group);
  2431. }
  2432. }
  2433. static const char * wpa_bool_txt(int bool)
  2434. {
  2435. return bool ? "TRUE" : "FALSE";
  2436. }
  2437. #define RSN_SUITE "%02x-%02x-%02x-%d"
  2438. #define RSN_SUITE_ARG(s) \
  2439. ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
  2440. int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
  2441. {
  2442. int len = 0, ret;
  2443. char pmkid_txt[PMKID_LEN * 2 + 1];
  2444. #ifdef CONFIG_RSN_PREAUTH
  2445. const int preauth = 1;
  2446. #else /* CONFIG_RSN_PREAUTH */
  2447. const int preauth = 0;
  2448. #endif /* CONFIG_RSN_PREAUTH */
  2449. if (wpa_auth == NULL)
  2450. return len;
  2451. ret = os_snprintf(buf + len, buflen - len,
  2452. "dot11RSNAOptionImplemented=TRUE\n"
  2453. "dot11RSNAPreauthenticationImplemented=%s\n"
  2454. "dot11RSNAEnabled=%s\n"
  2455. "dot11RSNAPreauthenticationEnabled=%s\n",
  2456. wpa_bool_txt(preauth),
  2457. wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
  2458. wpa_bool_txt(wpa_auth->conf.rsn_preauth));
  2459. if (ret < 0 || (size_t) ret >= buflen - len)
  2460. return len;
  2461. len += ret;
  2462. wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
  2463. wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
  2464. ret = os_snprintf(
  2465. buf + len, buflen - len,
  2466. "dot11RSNAConfigVersion=%u\n"
  2467. "dot11RSNAConfigPairwiseKeysSupported=9999\n"
  2468. /* FIX: dot11RSNAConfigGroupCipher */
  2469. /* FIX: dot11RSNAConfigGroupRekeyMethod */
  2470. /* FIX: dot11RSNAConfigGroupRekeyTime */
  2471. /* FIX: dot11RSNAConfigGroupRekeyPackets */
  2472. "dot11RSNAConfigGroupRekeyStrict=%u\n"
  2473. "dot11RSNAConfigGroupUpdateCount=%u\n"
  2474. "dot11RSNAConfigPairwiseUpdateCount=%u\n"
  2475. "dot11RSNAConfigGroupCipherSize=%u\n"
  2476. "dot11RSNAConfigPMKLifetime=%u\n"
  2477. "dot11RSNAConfigPMKReauthThreshold=%u\n"
  2478. "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
  2479. "dot11RSNAConfigSATimeout=%u\n"
  2480. "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
  2481. "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
  2482. "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
  2483. "dot11RSNAPMKIDUsed=%s\n"
  2484. "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
  2485. "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
  2486. "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
  2487. "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
  2488. "dot11RSNA4WayHandshakeFailures=%u\n"
  2489. "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
  2490. RSN_VERSION,
  2491. !!wpa_auth->conf.wpa_strict_rekey,
  2492. dot11RSNAConfigGroupUpdateCount,
  2493. dot11RSNAConfigPairwiseUpdateCount,
  2494. wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
  2495. dot11RSNAConfigPMKLifetime,
  2496. dot11RSNAConfigPMKReauthThreshold,
  2497. dot11RSNAConfigSATimeout,
  2498. RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
  2499. RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
  2500. RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
  2501. pmkid_txt,
  2502. RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
  2503. RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
  2504. RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
  2505. wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
  2506. wpa_auth->dot11RSNA4WayHandshakeFailures);
  2507. if (ret < 0 || (size_t) ret >= buflen - len)
  2508. return len;
  2509. len += ret;
  2510. /* TODO: dot11RSNAConfigPairwiseCiphersTable */
  2511. /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
  2512. /* Private MIB */
  2513. ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
  2514. wpa_auth->group->wpa_group_state);
  2515. if (ret < 0 || (size_t) ret >= buflen - len)
  2516. return len;
  2517. len += ret;
  2518. return len;
  2519. }
  2520. int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
  2521. {
  2522. int len = 0, ret;
  2523. u32 pairwise = 0;
  2524. if (sm == NULL)
  2525. return 0;
  2526. /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
  2527. /* dot11RSNAStatsEntry */
  2528. pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
  2529. WPA_PROTO_RSN : WPA_PROTO_WPA,
  2530. sm->pairwise);
  2531. if (pairwise == 0)
  2532. return 0;
  2533. ret = os_snprintf(
  2534. buf + len, buflen - len,
  2535. /* TODO: dot11RSNAStatsIndex */
  2536. "dot11RSNAStatsSTAAddress=" MACSTR "\n"
  2537. "dot11RSNAStatsVersion=1\n"
  2538. "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
  2539. /* TODO: dot11RSNAStatsTKIPICVErrors */
  2540. "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
  2541. "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
  2542. /* TODO: dot11RSNAStatsCCMPReplays */
  2543. /* TODO: dot11RSNAStatsCCMPDecryptErrors */
  2544. /* TODO: dot11RSNAStatsTKIPReplays */,
  2545. MAC2STR(sm->addr),
  2546. RSN_SUITE_ARG(pairwise),
  2547. sm->dot11RSNAStatsTKIPLocalMICFailures,
  2548. sm->dot11RSNAStatsTKIPRemoteMICFailures);
  2549. if (ret < 0 || (size_t) ret >= buflen - len)
  2550. return len;
  2551. len += ret;
  2552. /* Private MIB */
  2553. ret = os_snprintf(buf + len, buflen - len,
  2554. "hostapdWPAPTKState=%d\n"
  2555. "hostapdWPAPTKGroupState=%d\n",
  2556. sm->wpa_ptk_state,
  2557. sm->wpa_ptk_group_state);
  2558. if (ret < 0 || (size_t) ret >= buflen - len)
  2559. return len;
  2560. len += ret;
  2561. return len;
  2562. }
  2563. void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
  2564. {
  2565. if (wpa_auth)
  2566. wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
  2567. }
  2568. int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
  2569. {
  2570. return sm && sm->pairwise_set;
  2571. }
  2572. int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
  2573. {
  2574. return sm->pairwise;
  2575. }
  2576. int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
  2577. {
  2578. if (sm == NULL)
  2579. return -1;
  2580. return sm->wpa_key_mgmt;
  2581. }
  2582. int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
  2583. {
  2584. if (sm == NULL)
  2585. return 0;
  2586. return sm->wpa;
  2587. }
  2588. int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
  2589. struct rsn_pmksa_cache_entry *entry)
  2590. {
  2591. if (sm == NULL || sm->pmksa != entry)
  2592. return -1;
  2593. sm->pmksa = NULL;
  2594. return 0;
  2595. }
  2596. struct rsn_pmksa_cache_entry *
  2597. wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
  2598. {
  2599. return sm ? sm->pmksa : NULL;
  2600. }
  2601. void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
  2602. {
  2603. if (sm)
  2604. sm->dot11RSNAStatsTKIPLocalMICFailures++;
  2605. }
  2606. const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
  2607. {
  2608. if (wpa_auth == NULL)
  2609. return NULL;
  2610. *len = wpa_auth->wpa_ie_len;
  2611. return wpa_auth->wpa_ie;
  2612. }
  2613. int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
  2614. int session_timeout, struct eapol_state_machine *eapol)
  2615. {
  2616. if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
  2617. sm->wpa_auth->conf.disable_pmksa_caching)
  2618. return -1;
  2619. if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
  2620. sm->wpa_auth->addr, sm->addr, session_timeout,
  2621. eapol, sm->wpa_key_mgmt))
  2622. return 0;
  2623. return -1;
  2624. }
  2625. int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
  2626. const u8 *pmk, size_t len, const u8 *sta_addr,
  2627. int session_timeout,
  2628. struct eapol_state_machine *eapol)
  2629. {
  2630. if (wpa_auth == NULL)
  2631. return -1;
  2632. if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, wpa_auth->addr,
  2633. sta_addr, session_timeout, eapol,
  2634. WPA_KEY_MGMT_IEEE8021X))
  2635. return 0;
  2636. return -1;
  2637. }
  2638. void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
  2639. const u8 *sta_addr)
  2640. {
  2641. struct rsn_pmksa_cache_entry *pmksa;
  2642. if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
  2643. return;
  2644. pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
  2645. if (pmksa) {
  2646. wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
  2647. MACSTR " based on request", MAC2STR(sta_addr));
  2648. pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
  2649. }
  2650. }
  2651. static struct wpa_group *
  2652. wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
  2653. {
  2654. struct wpa_group *group;
  2655. if (wpa_auth == NULL || wpa_auth->group == NULL)
  2656. return NULL;
  2657. wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
  2658. vlan_id);
  2659. group = wpa_group_init(wpa_auth, vlan_id, 0);
  2660. if (group == NULL)
  2661. return NULL;
  2662. group->next = wpa_auth->group->next;
  2663. wpa_auth->group->next = group;
  2664. return group;
  2665. }
  2666. int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
  2667. {
  2668. struct wpa_group *group;
  2669. if (sm == NULL || sm->wpa_auth == NULL)
  2670. return 0;
  2671. group = sm->wpa_auth->group;
  2672. while (group) {
  2673. if (group->vlan_id == vlan_id)
  2674. break;
  2675. group = group->next;
  2676. }
  2677. if (group == NULL) {
  2678. group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
  2679. if (group == NULL)
  2680. return -1;
  2681. }
  2682. if (sm->group == group)
  2683. return 0;
  2684. if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
  2685. return -1;
  2686. wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
  2687. "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
  2688. sm->group = group;
  2689. return 0;
  2690. }
  2691. void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
  2692. struct wpa_state_machine *sm, int ack)
  2693. {
  2694. if (wpa_auth == NULL || sm == NULL)
  2695. return;
  2696. wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
  2697. " ack=%d", MAC2STR(sm->addr), ack);
  2698. if (sm->pending_1_of_4_timeout && ack) {
  2699. /*
  2700. * Some deployed supplicant implementations update their SNonce
  2701. * for each EAPOL-Key 2/4 message even within the same 4-way
  2702. * handshake and then fail to use the first SNonce when
  2703. * deriving the PTK. This results in unsuccessful 4-way
  2704. * handshake whenever the relatively short initial timeout is
  2705. * reached and EAPOL-Key 1/4 is retransmitted. Try to work
  2706. * around this by increasing the timeout now that we know that
  2707. * the station has received the frame.
  2708. */
  2709. int timeout_ms = eapol_key_timeout_subseq;
  2710. wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
  2711. "timeout by %u ms because of acknowledged frame",
  2712. timeout_ms);
  2713. eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
  2714. eloop_register_timeout(timeout_ms / 1000,
  2715. (timeout_ms % 1000) * 1000,
  2716. wpa_send_eapol_timeout, wpa_auth, sm);
  2717. }
  2718. }
  2719. int wpa_auth_uses_sae(struct wpa_state_machine *sm)
  2720. {
  2721. if (sm == NULL)
  2722. return 0;
  2723. return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
  2724. }
  2725. int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
  2726. {
  2727. if (sm == NULL)
  2728. return 0;
  2729. return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
  2730. }
  2731. #ifdef CONFIG_P2P
  2732. int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
  2733. {
  2734. if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
  2735. return -1;
  2736. os_memcpy(addr, sm->ip_addr, 4);
  2737. return 0;
  2738. }
  2739. #endif /* CONFIG_P2P */