wpa.c 60 KB

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