wpa.c 64 KB

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