wpa_ft.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668
  1. /*
  2. * hostapd - IEEE 802.11r - Fast BSS Transition
  3. * Copyright (c) 2004-2007, 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. #include "common.h"
  16. #include "config.h"
  17. #include "wpa.h"
  18. #include "aes_wrap.h"
  19. #include "ieee802_11.h"
  20. #include "wme.h"
  21. #include "defs.h"
  22. #include "wpa_auth_i.h"
  23. #include "wpa_auth_ie.h"
  24. #ifdef CONFIG_IEEE80211R
  25. struct wpa_ft_ies {
  26. const u8 *mdie;
  27. size_t mdie_len;
  28. const u8 *ftie;
  29. size_t ftie_len;
  30. const u8 *r1kh_id;
  31. const u8 *gtk;
  32. size_t gtk_len;
  33. const u8 *r0kh_id;
  34. size_t r0kh_id_len;
  35. const u8 *rsn;
  36. size_t rsn_len;
  37. const u8 *rsn_pmkid;
  38. const u8 *ric;
  39. size_t ric_len;
  40. };
  41. static int wpa_ft_parse_ies(const u8 *ies, size_t ies_len,
  42. struct wpa_ft_ies *parse);
  43. static int wpa_ft_rrb_send(struct wpa_authenticator *wpa_auth, const u8 *dst,
  44. const u8 *data, size_t data_len)
  45. {
  46. if (wpa_auth->cb.send_ether == NULL)
  47. return -1;
  48. return wpa_auth->cb.send_ether(wpa_auth->cb.ctx, dst, ETH_P_RRB,
  49. data, data_len);
  50. }
  51. static int wpa_ft_action_send(struct wpa_authenticator *wpa_auth,
  52. const u8 *dst, const u8 *data, size_t data_len)
  53. {
  54. if (wpa_auth->cb.send_ft_action == NULL)
  55. return -1;
  56. return wpa_auth->cb.send_ft_action(wpa_auth->cb.ctx, dst,
  57. data, data_len);
  58. }
  59. static struct wpa_state_machine *
  60. wpa_ft_add_sta(struct wpa_authenticator *wpa_auth, const u8 *sta_addr)
  61. {
  62. if (wpa_auth->cb.add_sta == NULL)
  63. return NULL;
  64. return wpa_auth->cb.add_sta(wpa_auth->cb.ctx, sta_addr);
  65. }
  66. int wpa_write_mdie(struct wpa_auth_config *conf, u8 *buf, size_t len)
  67. {
  68. u8 *pos = buf;
  69. u8 capab;
  70. if (len < 2 + sizeof(struct rsn_mdie))
  71. return -1;
  72. *pos++ = WLAN_EID_MOBILITY_DOMAIN;
  73. *pos++ = MOBILITY_DOMAIN_ID_LEN + 1;
  74. os_memcpy(pos, conf->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
  75. pos += MOBILITY_DOMAIN_ID_LEN;
  76. capab = RSN_FT_CAPAB_FT_OVER_DS;
  77. *pos++ = capab;
  78. return pos - buf;
  79. }
  80. static int wpa_write_ftie(struct wpa_auth_config *conf, const u8 *r0kh_id,
  81. size_t r0kh_id_len,
  82. const u8 *anonce, const u8 *snonce,
  83. u8 *buf, size_t len, const u8 *subelem,
  84. size_t subelem_len)
  85. {
  86. u8 *pos = buf, *ielen;
  87. struct rsn_ftie *hdr;
  88. if (len < 2 + sizeof(*hdr) + 2 + FT_R1KH_ID_LEN + 2 + r0kh_id_len +
  89. subelem_len)
  90. return -1;
  91. *pos++ = WLAN_EID_FAST_BSS_TRANSITION;
  92. ielen = pos++;
  93. hdr = (struct rsn_ftie *) pos;
  94. os_memset(hdr, 0, sizeof(*hdr));
  95. pos += sizeof(*hdr);
  96. WPA_PUT_LE16(hdr->mic_control, 0);
  97. if (anonce)
  98. os_memcpy(hdr->anonce, anonce, WPA_NONCE_LEN);
  99. if (snonce)
  100. os_memcpy(hdr->snonce, snonce, WPA_NONCE_LEN);
  101. /* Optional Parameters */
  102. *pos++ = FTIE_SUBELEM_R1KH_ID;
  103. *pos++ = FT_R1KH_ID_LEN;
  104. os_memcpy(pos, conf->r1_key_holder, FT_R1KH_ID_LEN);
  105. pos += FT_R1KH_ID_LEN;
  106. if (r0kh_id) {
  107. *pos++ = FTIE_SUBELEM_R0KH_ID;
  108. *pos++ = r0kh_id_len;
  109. os_memcpy(pos, r0kh_id, r0kh_id_len);
  110. pos += r0kh_id_len;
  111. }
  112. if (subelem) {
  113. os_memcpy(pos, subelem, subelem_len);
  114. pos += subelem_len;
  115. }
  116. *ielen = pos - buf - 2;
  117. return pos - buf;
  118. }
  119. struct wpa_ft_pmk_r0_sa {
  120. struct wpa_ft_pmk_r0_sa *next;
  121. u8 pmk_r0[PMK_LEN];
  122. u8 pmk_r0_name[WPA_PMK_NAME_LEN];
  123. u8 spa[ETH_ALEN];
  124. /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */
  125. int pmk_r1_pushed;
  126. };
  127. struct wpa_ft_pmk_r1_sa {
  128. struct wpa_ft_pmk_r1_sa *next;
  129. u8 pmk_r1[PMK_LEN];
  130. u8 pmk_r1_name[WPA_PMK_NAME_LEN];
  131. u8 spa[ETH_ALEN];
  132. /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */
  133. };
  134. struct wpa_ft_pmk_cache {
  135. struct wpa_ft_pmk_r0_sa *pmk_r0;
  136. struct wpa_ft_pmk_r1_sa *pmk_r1;
  137. };
  138. struct wpa_ft_pmk_cache * wpa_ft_pmk_cache_init(void)
  139. {
  140. struct wpa_ft_pmk_cache *cache;
  141. cache = os_zalloc(sizeof(*cache));
  142. return cache;
  143. }
  144. void wpa_ft_pmk_cache_deinit(struct wpa_ft_pmk_cache *cache)
  145. {
  146. struct wpa_ft_pmk_r0_sa *r0, *r0prev;
  147. struct wpa_ft_pmk_r1_sa *r1, *r1prev;
  148. r0 = cache->pmk_r0;
  149. while (r0) {
  150. r0prev = r0;
  151. r0 = r0->next;
  152. os_memset(r0prev->pmk_r0, 0, PMK_LEN);
  153. os_free(r0prev);
  154. }
  155. r1 = cache->pmk_r1;
  156. while (r1) {
  157. r1prev = r1;
  158. r1 = r1->next;
  159. os_memset(r1prev->pmk_r1, 0, PMK_LEN);
  160. os_free(r1prev);
  161. }
  162. os_free(cache);
  163. }
  164. static int wpa_ft_store_pmk_r0(struct wpa_authenticator *wpa_auth,
  165. const u8 *spa, const u8 *pmk_r0,
  166. const u8 *pmk_r0_name)
  167. {
  168. struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
  169. struct wpa_ft_pmk_r0_sa *r0;
  170. /* TODO: add expiration and limit on number of entries in cache */
  171. r0 = os_zalloc(sizeof(*r0));
  172. if (r0 == NULL)
  173. return -1;
  174. os_memcpy(r0->pmk_r0, pmk_r0, PMK_LEN);
  175. os_memcpy(r0->pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN);
  176. os_memcpy(r0->spa, spa, ETH_ALEN);
  177. r0->next = cache->pmk_r0;
  178. cache->pmk_r0 = r0;
  179. return 0;
  180. }
  181. static int wpa_ft_fetch_pmk_r0(struct wpa_authenticator *wpa_auth,
  182. const u8 *spa, const u8 *pmk_r0_name,
  183. u8 *pmk_r0)
  184. {
  185. struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
  186. struct wpa_ft_pmk_r0_sa *r0;
  187. r0 = cache->pmk_r0;
  188. while (r0) {
  189. if (os_memcmp(r0->spa, spa, ETH_ALEN) == 0 &&
  190. os_memcmp(r0->pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN)
  191. == 0) {
  192. os_memcpy(pmk_r0, r0->pmk_r0, PMK_LEN);
  193. return 0;
  194. }
  195. r0 = r0->next;
  196. }
  197. return -1;
  198. }
  199. static int wpa_ft_store_pmk_r1(struct wpa_authenticator *wpa_auth,
  200. const u8 *spa, const u8 *pmk_r1,
  201. const u8 *pmk_r1_name)
  202. {
  203. struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
  204. struct wpa_ft_pmk_r1_sa *r1;
  205. /* TODO: add expiration and limit on number of entries in cache */
  206. r1 = os_zalloc(sizeof(*r1));
  207. if (r1 == NULL)
  208. return -1;
  209. os_memcpy(r1->pmk_r1, pmk_r1, PMK_LEN);
  210. os_memcpy(r1->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN);
  211. os_memcpy(r1->spa, spa, ETH_ALEN);
  212. r1->next = cache->pmk_r1;
  213. cache->pmk_r1 = r1;
  214. return 0;
  215. }
  216. static int wpa_ft_fetch_pmk_r1(struct wpa_authenticator *wpa_auth,
  217. const u8 *spa, const u8 *pmk_r1_name,
  218. u8 *pmk_r1)
  219. {
  220. struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
  221. struct wpa_ft_pmk_r1_sa *r1;
  222. r1 = cache->pmk_r1;
  223. while (r1) {
  224. if (os_memcmp(r1->spa, spa, ETH_ALEN) == 0 &&
  225. os_memcmp(r1->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN)
  226. == 0) {
  227. os_memcpy(pmk_r1, r1->pmk_r1, PMK_LEN);
  228. return 0;
  229. }
  230. r1 = r1->next;
  231. }
  232. return -1;
  233. }
  234. static int wpa_ft_pull_pmk_r1(struct wpa_authenticator *wpa_auth,
  235. const u8 *s1kh_id, const u8 *r0kh_id,
  236. size_t r0kh_id_len, const u8 *pmk_r0_name)
  237. {
  238. struct ft_remote_r0kh *r0kh;
  239. struct ft_r0kh_r1kh_pull_frame frame, f;
  240. r0kh = wpa_auth->conf.r0kh_list;
  241. while (r0kh) {
  242. if (r0kh->id_len == r0kh_id_len &&
  243. os_memcmp(r0kh->id, r0kh_id, r0kh_id_len) == 0)
  244. break;
  245. r0kh = r0kh->next;
  246. }
  247. if (r0kh == NULL)
  248. return -1;
  249. wpa_printf(MSG_DEBUG, "FT: Send PMK-R1 pull request to remote R0KH "
  250. "address " MACSTR, MAC2STR(r0kh->addr));
  251. os_memset(&frame, 0, sizeof(frame));
  252. frame.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  253. frame.packet_type = FT_PACKET_R0KH_R1KH_PULL;
  254. frame.data_length = host_to_le16(FT_R0KH_R1KH_PULL_DATA_LEN);
  255. os_memcpy(frame.ap_address, wpa_auth->addr, ETH_ALEN);
  256. /* aes_wrap() does not support inplace encryption, so use a temporary
  257. * buffer for the data. */
  258. if (os_get_random(f.nonce, sizeof(f.nonce))) {
  259. wpa_printf(MSG_DEBUG, "FT: Failed to get random data for "
  260. "nonce");
  261. return -1;
  262. }
  263. os_memcpy(f.pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN);
  264. os_memcpy(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN);
  265. os_memcpy(f.s1kh_id, s1kh_id, ETH_ALEN);
  266. if (aes_wrap(r0kh->key, (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8,
  267. f.nonce, frame.nonce) < 0)
  268. return -1;
  269. wpa_ft_rrb_send(wpa_auth, r0kh->addr, (u8 *) &frame, sizeof(frame));
  270. return 0;
  271. }
  272. int wpa_auth_derive_ptk_ft(struct wpa_state_machine *sm, const u8 *pmk,
  273. struct wpa_ptk *ptk, size_t ptk_len)
  274. {
  275. u8 pmk_r0[PMK_LEN], pmk_r0_name[WPA_PMK_NAME_LEN];
  276. u8 pmk_r1[PMK_LEN], pmk_r1_name[WPA_PMK_NAME_LEN];
  277. u8 ptk_name[WPA_PMK_NAME_LEN];
  278. const u8 *mdid = sm->wpa_auth->conf.mobility_domain;
  279. const u8 *r0kh = sm->wpa_auth->conf.r0_key_holder;
  280. size_t r0kh_len = sm->wpa_auth->conf.r0_key_holder_len;
  281. const u8 *r1kh = sm->wpa_auth->conf.r1_key_holder;
  282. const u8 *ssid = sm->wpa_auth->conf.ssid;
  283. size_t ssid_len = sm->wpa_auth->conf.ssid_len;
  284. if (sm->xxkey_len == 0) {
  285. wpa_printf(MSG_DEBUG, "FT: XXKey not available for key "
  286. "derivation");
  287. return -1;
  288. }
  289. wpa_derive_pmk_r0(sm->xxkey, sm->xxkey_len, ssid, ssid_len, mdid,
  290. r0kh, r0kh_len, sm->addr, pmk_r0, pmk_r0_name);
  291. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", pmk_r0, PMK_LEN);
  292. wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name", pmk_r0_name, WPA_PMK_NAME_LEN);
  293. wpa_ft_store_pmk_r0(sm->wpa_auth, sm->addr, pmk_r0, pmk_r0_name);
  294. wpa_derive_pmk_r1(pmk_r0, pmk_r0_name, r1kh, sm->addr,
  295. pmk_r1, pmk_r1_name);
  296. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", pmk_r1, PMK_LEN);
  297. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", pmk_r1_name, WPA_PMK_NAME_LEN);
  298. wpa_ft_store_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1, pmk_r1_name);
  299. wpa_pmk_r1_to_ptk(pmk_r1, sm->SNonce, sm->ANonce, sm->addr,
  300. sm->wpa_auth->addr, pmk_r1_name,
  301. (u8 *) ptk, ptk_len, ptk_name);
  302. wpa_hexdump_key(MSG_DEBUG, "FT: PTK", (u8 *) ptk, ptk_len);
  303. wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
  304. return 0;
  305. }
  306. static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
  307. const u8 *addr, int idx, u8 *seq)
  308. {
  309. if (wpa_auth->cb.get_seqnum == NULL)
  310. return -1;
  311. return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
  312. }
  313. #ifdef CONFIG_IEEE80211W
  314. static inline int wpa_auth_get_seqnum_igtk(struct wpa_authenticator *wpa_auth,
  315. const u8 *addr, int idx, u8 *seq)
  316. {
  317. if (wpa_auth->cb.get_seqnum_igtk == NULL)
  318. return -1;
  319. return wpa_auth->cb.get_seqnum_igtk(wpa_auth->cb.ctx, addr, idx, seq);
  320. }
  321. #endif /* CONFIG_IEEE80211W */
  322. static u8 * wpa_ft_gtk_subelem(struct wpa_state_machine *sm, size_t *len)
  323. {
  324. u8 *subelem;
  325. struct wpa_group *gsm = sm->group;
  326. size_t subelem_len, pad_len;
  327. const u8 *key;
  328. size_t key_len;
  329. u8 keybuf[32];
  330. key_len = gsm->GTK_len;
  331. if (key_len > sizeof(keybuf))
  332. return NULL;
  333. /*
  334. * Pad key for AES Key Wrap if it is not multiple of 8 bytes or is less
  335. * than 16 bytes.
  336. */
  337. pad_len = key_len % 8;
  338. if (pad_len)
  339. pad_len = 8 - pad_len;
  340. if (key_len + pad_len < 16)
  341. pad_len += 8;
  342. if (pad_len) {
  343. os_memcpy(keybuf, gsm->GTK[gsm->GN - 1], key_len);
  344. os_memset(keybuf + key_len, 0, pad_len);
  345. keybuf[key_len] = 0xdd;
  346. key_len += pad_len;
  347. key = keybuf;
  348. } else
  349. key = gsm->GTK[gsm->GN - 1];
  350. /*
  351. * Sub-elem ID[1] | Length[1] | Key Info[1] | Key Length[1] | RSC[8] |
  352. * Key[5..32].
  353. */
  354. subelem_len = 12 + key_len + 8;
  355. subelem = os_zalloc(subelem_len);
  356. if (subelem == NULL)
  357. return NULL;
  358. subelem[0] = FTIE_SUBELEM_GTK;
  359. subelem[1] = 10 + key_len + 8;
  360. subelem[2] = gsm->GN & 0x03; /* Key ID in B0-B1 of Key Info */
  361. subelem[3] = gsm->GTK_len;
  362. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, subelem + 4);
  363. if (aes_wrap(sm->PTK.kek, key_len / 8, key, subelem + 12)) {
  364. os_free(subelem);
  365. return NULL;
  366. }
  367. *len = subelem_len;
  368. return subelem;
  369. }
  370. #ifdef CONFIG_IEEE80211W
  371. static u8 * wpa_ft_igtk_subelem(struct wpa_state_machine *sm, size_t *len)
  372. {
  373. u8 *subelem, *pos;
  374. struct wpa_group *gsm = sm->group;
  375. size_t subelem_len;
  376. /* Sub-elem ID[1] | Length[1] | KeyID[2] | IPN[6] | Key Length[1] |
  377. * Key[16+8] */
  378. subelem_len = 1 + 1 + 2 + 6 + 1 + WPA_IGTK_LEN + 8;
  379. subelem = os_zalloc(subelem_len);
  380. if (subelem == NULL)
  381. return NULL;
  382. pos = subelem;
  383. *pos++ = FTIE_SUBELEM_IGTK;
  384. *pos++ = subelem_len - 2;
  385. WPA_PUT_LE16(pos, gsm->GN_igtk);
  386. pos += 2;
  387. wpa_auth_get_seqnum_igtk(sm->wpa_auth, NULL, gsm->GN_igtk, pos);
  388. pos += 6;
  389. *pos++ = WPA_IGTK_LEN;
  390. if (aes_wrap(sm->PTK.kek, WPA_IGTK_LEN / 8,
  391. gsm->IGTK[gsm->GN_igtk - 4], pos)) {
  392. os_free(subelem);
  393. return NULL;
  394. }
  395. *len = subelem_len;
  396. return subelem;
  397. }
  398. #endif /* CONFIG_IEEE80211W */
  399. static u8 * wpa_ft_process_rdie(u8 *pos, u8 *end, u8 id, u8 descr_count,
  400. const u8 *ies, size_t ies_len)
  401. {
  402. struct ieee802_11_elems parse;
  403. struct rsn_rdie *rdie;
  404. wpa_printf(MSG_DEBUG, "FT: Resource Request: id=%d descr_count=%d",
  405. id, descr_count);
  406. wpa_hexdump(MSG_MSGDUMP, "FT: Resource descriptor IE(s)",
  407. ies, ies_len);
  408. if (end - pos < (int) sizeof(*rdie)) {
  409. wpa_printf(MSG_ERROR, "FT: Not enough room for response RDIE");
  410. return pos;
  411. }
  412. *pos++ = WLAN_EID_RIC_DATA;
  413. *pos++ = sizeof(*rdie);
  414. rdie = (struct rsn_rdie *) pos;
  415. rdie->id = id;
  416. rdie->descr_count = 0;
  417. rdie->status_code = host_to_le16(WLAN_STATUS_SUCCESS);
  418. pos += sizeof(*rdie);
  419. if (ieee802_11_parse_elems((u8 *) ies, ies_len, &parse, 1) ==
  420. ParseFailed) {
  421. wpa_printf(MSG_DEBUG, "FT: Failed to parse request IEs");
  422. rdie->status_code =
  423. host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
  424. return pos;
  425. }
  426. #ifdef NEED_AP_MLME
  427. if (parse.wmm_tspec) {
  428. struct wmm_tspec_element *tspec;
  429. int res;
  430. if (parse.wmm_tspec_len + 2 < (int) sizeof(*tspec)) {
  431. wpa_printf(MSG_DEBUG, "FT: Too short WMM TSPEC IE "
  432. "(%d)", (int) parse.wmm_tspec_len);
  433. rdie->status_code =
  434. host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
  435. return pos;
  436. }
  437. if (end - pos < (int) sizeof(*tspec)) {
  438. wpa_printf(MSG_ERROR, "FT: Not enough room for "
  439. "response TSPEC");
  440. rdie->status_code =
  441. host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
  442. return pos;
  443. }
  444. tspec = (struct wmm_tspec_element *) pos;
  445. os_memcpy(tspec, parse.wmm_tspec - 2, sizeof(*tspec));
  446. res = wmm_process_tspec(tspec);
  447. wpa_printf(MSG_DEBUG, "FT: ADDTS processing result: %d", res);
  448. if (res == WMM_ADDTS_STATUS_INVALID_PARAMETERS)
  449. rdie->status_code =
  450. host_to_le16(WLAN_STATUS_INVALID_PARAMETERS);
  451. else if (res == WMM_ADDTS_STATUS_REFUSED)
  452. rdie->status_code =
  453. host_to_le16(WLAN_STATUS_REQUEST_DECLINED);
  454. else {
  455. /* TSPEC accepted; include updated TSPEC in response */
  456. rdie->descr_count = 1;
  457. pos += sizeof(*tspec);
  458. }
  459. return pos;
  460. }
  461. #endif /* NEED_AP_MLME */
  462. wpa_printf(MSG_DEBUG, "FT: No supported resource requested");
  463. rdie->status_code = host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
  464. return pos;
  465. }
  466. static u8 * wpa_ft_process_ric(u8 *pos, u8 *end, const u8 *ric, size_t ric_len)
  467. {
  468. const u8 *rpos, *start;
  469. const struct rsn_rdie *rdie;
  470. wpa_hexdump(MSG_MSGDUMP, "FT: RIC Request", ric, ric_len);
  471. rpos = ric;
  472. while (rpos + sizeof(*rdie) < ric + ric_len) {
  473. if (rpos[0] != WLAN_EID_RIC_DATA || rpos[1] < sizeof(*rdie) ||
  474. rpos + 2 + rpos[1] > ric + ric_len)
  475. break;
  476. rdie = (const struct rsn_rdie *) (rpos + 2);
  477. rpos += 2 + rpos[1];
  478. start = rpos;
  479. while (rpos + 2 <= ric + ric_len &&
  480. rpos + 2 + rpos[1] <= ric + ric_len) {
  481. if (rpos[0] == WLAN_EID_RIC_DATA)
  482. break;
  483. rpos += 2 + rpos[1];
  484. }
  485. pos = wpa_ft_process_rdie(pos, end, rdie->id,
  486. rdie->descr_count,
  487. start, rpos - start);
  488. }
  489. return pos;
  490. }
  491. u8 * wpa_sm_write_assoc_resp_ies(struct wpa_state_machine *sm, u8 *pos,
  492. size_t max_len, int auth_alg,
  493. const u8 *req_ies, size_t req_ies_len)
  494. {
  495. u8 *end, *mdie, *ftie, *rsnie, *r0kh_id, *subelem = NULL;
  496. size_t mdie_len, ftie_len, rsnie_len, r0kh_id_len, subelem_len = 0;
  497. int res;
  498. struct wpa_auth_config *conf;
  499. struct rsn_ftie *_ftie;
  500. struct wpa_ft_ies parse;
  501. u8 *ric_start;
  502. if (sm == NULL)
  503. return pos;
  504. conf = &sm->wpa_auth->conf;
  505. if (sm->wpa_key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
  506. sm->wpa_key_mgmt != WPA_KEY_MGMT_FT_PSK)
  507. return pos;
  508. end = pos + max_len;
  509. /* RSN */
  510. res = wpa_write_rsn_ie(conf, pos, end - pos, sm->pmk_r1_name);
  511. if (res < 0)
  512. return pos;
  513. rsnie = pos;
  514. rsnie_len = res;
  515. pos += res;
  516. /* Mobility Domain Information */
  517. res = wpa_write_mdie(conf, pos, end - pos);
  518. if (res < 0)
  519. return pos;
  520. mdie = pos;
  521. mdie_len = res;
  522. pos += res;
  523. /* Fast BSS Transition Information */
  524. if (auth_alg == WLAN_AUTH_FT) {
  525. subelem = wpa_ft_gtk_subelem(sm, &subelem_len);
  526. r0kh_id = sm->r0kh_id;
  527. r0kh_id_len = sm->r0kh_id_len;
  528. #ifdef CONFIG_IEEE80211W
  529. if (sm->mgmt_frame_prot) {
  530. u8 *igtk;
  531. size_t igtk_len;
  532. u8 *nbuf;
  533. igtk = wpa_ft_igtk_subelem(sm, &igtk_len);
  534. if (igtk == NULL) {
  535. os_free(subelem);
  536. return pos;
  537. }
  538. nbuf = os_realloc(subelem, subelem_len + igtk_len);
  539. if (nbuf == NULL) {
  540. os_free(subelem);
  541. os_free(igtk);
  542. return pos;
  543. }
  544. subelem = nbuf;
  545. os_memcpy(subelem + subelem_len, igtk, igtk_len);
  546. subelem_len += igtk_len;
  547. os_free(igtk);
  548. }
  549. #endif /* CONFIG_IEEE80211W */
  550. } else {
  551. r0kh_id = conf->r0_key_holder;
  552. r0kh_id_len = conf->r0_key_holder_len;
  553. }
  554. res = wpa_write_ftie(conf, r0kh_id, r0kh_id_len, NULL, NULL, pos,
  555. end - pos, subelem, subelem_len);
  556. os_free(subelem);
  557. if (res < 0)
  558. return pos;
  559. ftie = pos;
  560. ftie_len = res;
  561. pos += res;
  562. _ftie = (struct rsn_ftie *) (ftie + 2);
  563. _ftie->mic_control[1] = 3; /* Information element count */
  564. ric_start = pos;
  565. if (wpa_ft_parse_ies(req_ies, req_ies_len, &parse) == 0 && parse.ric) {
  566. pos = wpa_ft_process_ric(pos, end, parse.ric, parse.ric_len);
  567. _ftie->mic_control[1] += ieee802_11_ie_count(ric_start,
  568. pos - ric_start);
  569. }
  570. if (ric_start == pos)
  571. ric_start = NULL;
  572. if (wpa_ft_mic(sm->PTK.kck, sm->addr, sm->wpa_auth->addr, 6,
  573. mdie, mdie_len, ftie, ftie_len,
  574. rsnie, rsnie_len,
  575. ric_start, ric_start ? pos - ric_start : 0,
  576. _ftie->mic) < 0)
  577. wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
  578. return pos;
  579. }
  580. static int wpa_ft_parse_ftie(const u8 *ie, size_t ie_len,
  581. struct wpa_ft_ies *parse)
  582. {
  583. const u8 *end, *pos;
  584. parse->ftie = ie;
  585. parse->ftie_len = ie_len;
  586. pos = ie + sizeof(struct rsn_ftie);
  587. end = ie + ie_len;
  588. while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
  589. switch (pos[0]) {
  590. case FTIE_SUBELEM_R1KH_ID:
  591. if (pos[1] != FT_R1KH_ID_LEN) {
  592. wpa_printf(MSG_DEBUG, "FT: Invalid R1KH-ID "
  593. "length in FTIE: %d", pos[1]);
  594. return -1;
  595. }
  596. parse->r1kh_id = pos + 2;
  597. break;
  598. case FTIE_SUBELEM_GTK:
  599. parse->gtk = pos + 2;
  600. parse->gtk_len = pos[1];
  601. break;
  602. case FTIE_SUBELEM_R0KH_ID:
  603. if (pos[1] < 1 || pos[1] > FT_R0KH_ID_MAX_LEN) {
  604. wpa_printf(MSG_DEBUG, "FT: Invalid R0KH-ID "
  605. "length in FTIE: %d", pos[1]);
  606. return -1;
  607. }
  608. parse->r0kh_id = pos + 2;
  609. parse->r0kh_id_len = pos[1];
  610. break;
  611. }
  612. pos += 2 + pos[1];
  613. }
  614. return 0;
  615. }
  616. static int wpa_ft_parse_ies(const u8 *ies, size_t ies_len,
  617. struct wpa_ft_ies *parse)
  618. {
  619. const u8 *end, *pos;
  620. struct wpa_ie_data data;
  621. int ret;
  622. const struct rsn_ftie *ftie;
  623. int prot_ie_count = 0;
  624. os_memset(parse, 0, sizeof(*parse));
  625. if (ies == NULL)
  626. return 0;
  627. pos = ies;
  628. end = ies + ies_len;
  629. while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
  630. switch (pos[0]) {
  631. case WLAN_EID_RSN:
  632. parse->rsn = pos + 2;
  633. parse->rsn_len = pos[1];
  634. ret = wpa_parse_wpa_ie_rsn(parse->rsn - 2,
  635. parse->rsn_len + 2,
  636. &data);
  637. if (ret < 0) {
  638. wpa_printf(MSG_DEBUG, "FT: Failed to parse "
  639. "RSN IE: %d", ret);
  640. return -1;
  641. }
  642. if (data.num_pmkid == 1 && data.pmkid)
  643. parse->rsn_pmkid = data.pmkid;
  644. break;
  645. case WLAN_EID_MOBILITY_DOMAIN:
  646. parse->mdie = pos + 2;
  647. parse->mdie_len = pos[1];
  648. break;
  649. case WLAN_EID_FAST_BSS_TRANSITION:
  650. if (pos[1] < sizeof(*ftie))
  651. return -1;
  652. ftie = (const struct rsn_ftie *) (pos + 2);
  653. prot_ie_count = ftie->mic_control[1];
  654. if (wpa_ft_parse_ftie(pos + 2, pos[1], parse) < 0)
  655. return -1;
  656. break;
  657. case WLAN_EID_RIC_DATA:
  658. if (parse->ric == NULL)
  659. parse->ric = pos;
  660. }
  661. pos += 2 + pos[1];
  662. }
  663. if (prot_ie_count == 0)
  664. return 0; /* no MIC */
  665. /*
  666. * Check that the protected IE count matches with IEs included in the
  667. * frame.
  668. */
  669. if (parse->rsn)
  670. prot_ie_count--;
  671. if (parse->mdie)
  672. prot_ie_count--;
  673. if (parse->ftie)
  674. prot_ie_count--;
  675. if (prot_ie_count < 0) {
  676. wpa_printf(MSG_DEBUG, "FT: Some required IEs not included in "
  677. "the protected IE count");
  678. return -1;
  679. }
  680. if (prot_ie_count == 0 && parse->ric) {
  681. wpa_printf(MSG_DEBUG, "FT: RIC IE(s) in the frame, but not "
  682. "included in protected IE count");
  683. return -1;
  684. }
  685. /* Determine the end of the RIC IE(s) */
  686. pos = parse->ric;
  687. while (pos && pos + 2 <= end && pos + 2 + pos[1] <= end &&
  688. prot_ie_count) {
  689. prot_ie_count--;
  690. pos += 2 + pos[1];
  691. }
  692. parse->ric_len = pos - parse->ric;
  693. if (prot_ie_count) {
  694. wpa_printf(MSG_DEBUG, "FT: %d protected IEs missing from "
  695. "frame", (int) prot_ie_count);
  696. return -1;
  697. }
  698. return 0;
  699. }
  700. static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
  701. int vlan_id,
  702. wpa_alg alg, const u8 *addr, int idx,
  703. u8 *key, size_t key_len)
  704. {
  705. if (wpa_auth->cb.set_key == NULL)
  706. return -1;
  707. return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
  708. key, key_len);
  709. }
  710. static void wpa_ft_install_ptk(struct wpa_state_machine *sm)
  711. {
  712. wpa_alg alg;
  713. int klen;
  714. /* MLME-SETKEYS.request(PTK) */
  715. if (sm->pairwise == WPA_CIPHER_TKIP) {
  716. alg = WPA_ALG_TKIP;
  717. klen = 32;
  718. } else if (sm->pairwise == WPA_CIPHER_CCMP) {
  719. alg = WPA_ALG_CCMP;
  720. klen = 16;
  721. } else
  722. return;
  723. /* FIX: add STA entry to kernel/driver here? The set_key will fail
  724. * most likely without this.. At the moment, STA entry is added only
  725. * after association has been completed. Alternatively, could
  726. * re-configure PTK at that point(?).
  727. */
  728. if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
  729. sm->PTK.tk1, klen))
  730. return;
  731. /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
  732. sm->pairwise_set = TRUE;
  733. }
  734. static u16 wpa_ft_process_auth_req(struct wpa_state_machine *sm,
  735. const u8 *ies, size_t ies_len,
  736. u8 **resp_ies, size_t *resp_ies_len)
  737. {
  738. struct rsn_mdie *mdie;
  739. struct rsn_ftie *ftie;
  740. u8 pmk_r1[PMK_LEN], pmk_r1_name[WPA_PMK_NAME_LEN];
  741. u8 ptk_name[WPA_PMK_NAME_LEN];
  742. struct wpa_auth_config *conf;
  743. struct wpa_ft_ies parse;
  744. size_t buflen, ptk_len;
  745. int ret;
  746. u8 *pos, *end;
  747. *resp_ies = NULL;
  748. *resp_ies_len = 0;
  749. sm->pmk_r1_name_valid = 0;
  750. conf = &sm->wpa_auth->conf;
  751. wpa_hexdump(MSG_DEBUG, "FT: Received authentication frame IEs",
  752. ies, ies_len);
  753. if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
  754. wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
  755. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  756. }
  757. mdie = (struct rsn_mdie *) parse.mdie;
  758. if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
  759. os_memcmp(mdie->mobility_domain,
  760. sm->wpa_auth->conf.mobility_domain,
  761. MOBILITY_DOMAIN_ID_LEN) != 0) {
  762. wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
  763. return WLAN_STATUS_INVALID_MDIE;
  764. }
  765. ftie = (struct rsn_ftie *) parse.ftie;
  766. if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
  767. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
  768. return WLAN_STATUS_INVALID_FTIE;
  769. }
  770. os_memcpy(sm->SNonce, ftie->snonce, WPA_NONCE_LEN);
  771. if (parse.r0kh_id == NULL) {
  772. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE - no R0KH-ID");
  773. return WLAN_STATUS_INVALID_FTIE;
  774. }
  775. wpa_hexdump(MSG_DEBUG, "FT: STA R0KH-ID",
  776. parse.r0kh_id, parse.r0kh_id_len);
  777. os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
  778. sm->r0kh_id_len = parse.r0kh_id_len;
  779. if (parse.rsn_pmkid == NULL) {
  780. wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
  781. return WLAN_STATUS_INVALID_PMKID;
  782. }
  783. wpa_hexdump(MSG_DEBUG, "FT: Requested PMKR0Name",
  784. parse.rsn_pmkid, WPA_PMK_NAME_LEN);
  785. wpa_derive_pmk_r1_name(parse.rsn_pmkid,
  786. sm->wpa_auth->conf.r1_key_holder, sm->addr,
  787. pmk_r1_name);
  788. wpa_hexdump(MSG_DEBUG, "FT: Derived requested PMKR1Name",
  789. pmk_r1_name, WPA_PMK_NAME_LEN);
  790. if (wpa_ft_fetch_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1_name, pmk_r1) <
  791. 0) {
  792. if (wpa_ft_pull_pmk_r1(sm->wpa_auth, sm->addr, sm->r0kh_id,
  793. sm->r0kh_id_len, parse.rsn_pmkid) < 0) {
  794. wpa_printf(MSG_DEBUG, "FT: Did not have matching "
  795. "PMK-R1 and unknown R0KH-ID");
  796. return WLAN_STATUS_INVALID_PMKID;
  797. }
  798. /*
  799. * TODO: Should return "status pending" (and the caller should
  800. * not send out response now). The real response will be sent
  801. * once the response from R0KH is received.
  802. */
  803. return WLAN_STATUS_INVALID_PMKID;
  804. }
  805. wpa_hexdump_key(MSG_DEBUG, "FT: Selected PMK-R1", pmk_r1, PMK_LEN);
  806. sm->pmk_r1_name_valid = 1;
  807. os_memcpy(sm->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN);
  808. if (os_get_random(sm->ANonce, WPA_NONCE_LEN)) {
  809. wpa_printf(MSG_DEBUG, "FT: Failed to get random data for "
  810. "ANonce");
  811. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  812. }
  813. wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
  814. sm->SNonce, WPA_NONCE_LEN);
  815. wpa_hexdump(MSG_DEBUG, "FT: Generated ANonce",
  816. sm->ANonce, WPA_NONCE_LEN);
  817. ptk_len = sm->pairwise == WPA_CIPHER_CCMP ? 48 : 64;
  818. wpa_pmk_r1_to_ptk(pmk_r1, sm->SNonce, sm->ANonce, sm->addr,
  819. sm->wpa_auth->addr, pmk_r1_name,
  820. (u8 *) &sm->PTK, ptk_len, ptk_name);
  821. wpa_hexdump_key(MSG_DEBUG, "FT: PTK",
  822. (u8 *) &sm->PTK, ptk_len);
  823. wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
  824. wpa_ft_install_ptk(sm);
  825. buflen = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) +
  826. 2 + FT_R1KH_ID_LEN + 200;
  827. *resp_ies = os_zalloc(buflen);
  828. if (*resp_ies == NULL) {
  829. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  830. }
  831. pos = *resp_ies;
  832. end = *resp_ies + buflen;
  833. ret = wpa_write_rsn_ie(conf, pos, end - pos, parse.rsn_pmkid);
  834. if (ret < 0) {
  835. os_free(*resp_ies);
  836. *resp_ies = NULL;
  837. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  838. }
  839. pos += ret;
  840. ret = wpa_write_mdie(conf, pos, end - pos);
  841. if (ret < 0) {
  842. os_free(*resp_ies);
  843. *resp_ies = NULL;
  844. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  845. }
  846. pos += ret;
  847. ret = wpa_write_ftie(conf, parse.r0kh_id, parse.r0kh_id_len,
  848. sm->ANonce, sm->SNonce, pos, end - pos, NULL, 0);
  849. if (ret < 0) {
  850. os_free(*resp_ies);
  851. *resp_ies = NULL;
  852. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  853. }
  854. pos += ret;
  855. *resp_ies_len = pos - *resp_ies;
  856. return WLAN_STATUS_SUCCESS;
  857. }
  858. void wpa_ft_process_auth(struct wpa_state_machine *sm, const u8 *bssid,
  859. u16 auth_transaction, const u8 *ies, size_t ies_len,
  860. void (*cb)(void *ctx, const u8 *dst, const u8 *bssid,
  861. u16 auth_transaction, u16 status,
  862. const u8 *ies, size_t ies_len),
  863. void *ctx)
  864. {
  865. u16 status;
  866. u8 *resp_ies;
  867. size_t resp_ies_len;
  868. if (sm == NULL) {
  869. wpa_printf(MSG_DEBUG, "FT: Received authentication frame, but "
  870. "WPA SM not available");
  871. return;
  872. }
  873. wpa_printf(MSG_DEBUG, "FT: Received authentication frame: STA=" MACSTR
  874. " BSSID=" MACSTR " transaction=%d",
  875. MAC2STR(sm->addr), MAC2STR(bssid), auth_transaction);
  876. status = wpa_ft_process_auth_req(sm, ies, ies_len, &resp_ies,
  877. &resp_ies_len);
  878. wpa_printf(MSG_DEBUG, "FT: FT authentication response: dst=" MACSTR
  879. " auth_transaction=%d status=%d",
  880. MAC2STR(sm->addr), auth_transaction + 1, status);
  881. wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
  882. cb(ctx, sm->addr, bssid, auth_transaction + 1, status,
  883. resp_ies, resp_ies_len);
  884. os_free(resp_ies);
  885. }
  886. u16 wpa_ft_validate_reassoc(struct wpa_state_machine *sm, const u8 *ies,
  887. size_t ies_len)
  888. {
  889. struct wpa_ft_ies parse;
  890. struct rsn_mdie *mdie;
  891. struct rsn_ftie *ftie;
  892. u8 mic[16];
  893. if (sm == NULL)
  894. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  895. wpa_hexdump(MSG_DEBUG, "FT: Reassoc Req IEs", ies, ies_len);
  896. if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
  897. wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
  898. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  899. }
  900. if (parse.rsn == NULL) {
  901. wpa_printf(MSG_DEBUG, "FT: No RSNIE in Reassoc Req");
  902. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  903. }
  904. if (parse.rsn_pmkid == NULL) {
  905. wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
  906. return WLAN_STATUS_INVALID_PMKID;
  907. }
  908. if (os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
  909. {
  910. wpa_printf(MSG_DEBUG, "FT: PMKID in Reassoc Req did not match "
  911. "with the PMKR1Name derived from auth request");
  912. return WLAN_STATUS_INVALID_PMKID;
  913. }
  914. mdie = (struct rsn_mdie *) parse.mdie;
  915. if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
  916. os_memcmp(mdie->mobility_domain,
  917. sm->wpa_auth->conf.mobility_domain,
  918. MOBILITY_DOMAIN_ID_LEN) != 0) {
  919. wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
  920. return WLAN_STATUS_INVALID_MDIE;
  921. }
  922. ftie = (struct rsn_ftie *) parse.ftie;
  923. if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
  924. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
  925. return WLAN_STATUS_INVALID_FTIE;
  926. }
  927. if (wpa_ft_mic(sm->PTK.kck, sm->addr, sm->wpa_auth->addr, 5,
  928. parse.mdie - 2, parse.mdie_len + 2,
  929. parse.ftie - 2, parse.ftie_len + 2,
  930. parse.rsn - 2, parse.rsn_len + 2,
  931. parse.ric, parse.ric_len,
  932. mic) < 0) {
  933. wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
  934. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  935. }
  936. if (os_memcmp(mic, ftie->mic, 16) != 0) {
  937. wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
  938. wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", ftie->mic, 16);
  939. wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16);
  940. return WLAN_STATUS_INVALID_FTIE;
  941. }
  942. return WLAN_STATUS_SUCCESS;
  943. }
  944. int wpa_ft_action_rx(struct wpa_state_machine *sm, const u8 *data, size_t len)
  945. {
  946. const u8 *sta_addr, *target_ap;
  947. const u8 *ies;
  948. size_t ies_len;
  949. u8 action;
  950. struct ft_rrb_frame *frame;
  951. if (sm == NULL)
  952. return -1;
  953. /*
  954. * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
  955. * FT Request action frame body[variable]
  956. */
  957. if (len < 14) {
  958. wpa_printf(MSG_DEBUG, "FT: Too short FT Action frame "
  959. "(len=%lu)", (unsigned long) len);
  960. return -1;
  961. }
  962. action = data[1];
  963. sta_addr = data + 2;
  964. target_ap = data + 8;
  965. ies = data + 14;
  966. ies_len = len - 14;
  967. wpa_printf(MSG_DEBUG, "FT: Received FT Action frame (STA=" MACSTR
  968. " Target AP=" MACSTR " Action=%d)",
  969. MAC2STR(sta_addr), MAC2STR(target_ap), action);
  970. if (os_memcmp(sta_addr, sm->addr, ETH_ALEN) != 0) {
  971. wpa_printf(MSG_DEBUG, "FT: Mismatch in FT Action STA address: "
  972. "STA=" MACSTR " STA-Address=" MACSTR,
  973. MAC2STR(sm->addr), MAC2STR(sta_addr));
  974. return -1;
  975. }
  976. /*
  977. * Do some sanity checking on the target AP address (not own and not
  978. * broadcast. This could be extended to filter based on a list of known
  979. * APs in the MD (if such a list were configured).
  980. */
  981. if ((target_ap[0] & 0x01) ||
  982. os_memcmp(target_ap, sm->wpa_auth->addr, ETH_ALEN) == 0) {
  983. wpa_printf(MSG_DEBUG, "FT: Invalid Target AP in FT Action "
  984. "frame");
  985. return -1;
  986. }
  987. wpa_hexdump(MSG_MSGDUMP, "FT: Action frame body", ies, ies_len);
  988. /* RRB - Forward action frame to the target AP */
  989. frame = os_malloc(sizeof(*frame) + len);
  990. frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  991. frame->packet_type = FT_PACKET_REQUEST;
  992. frame->action_length = host_to_le16(len);
  993. os_memcpy(frame->ap_address, sm->wpa_auth->addr, ETH_ALEN);
  994. os_memcpy(frame + 1, data, len);
  995. wpa_ft_rrb_send(sm->wpa_auth, target_ap, (u8 *) frame,
  996. sizeof(*frame) + len);
  997. os_free(frame);
  998. return 0;
  999. }
  1000. static int wpa_ft_rrb_rx_request(struct wpa_authenticator *wpa_auth,
  1001. const u8 *current_ap, const u8 *sta_addr,
  1002. const u8 *body, size_t len)
  1003. {
  1004. struct wpa_state_machine *sm;
  1005. u16 status;
  1006. u8 *resp_ies, *pos;
  1007. size_t resp_ies_len, rlen;
  1008. struct ft_rrb_frame *frame;
  1009. sm = wpa_ft_add_sta(wpa_auth, sta_addr);
  1010. if (sm == NULL) {
  1011. wpa_printf(MSG_DEBUG, "FT: Failed to add new STA based on "
  1012. "RRB Request");
  1013. return -1;
  1014. }
  1015. wpa_hexdump(MSG_MSGDUMP, "FT: RRB Request Frame body", body, len);
  1016. status = wpa_ft_process_auth_req(sm, body, len, &resp_ies,
  1017. &resp_ies_len);
  1018. wpa_printf(MSG_DEBUG, "FT: RRB authentication response: STA=" MACSTR
  1019. " CurrentAP=" MACSTR " status=%d",
  1020. MAC2STR(sm->addr), MAC2STR(current_ap), status);
  1021. wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
  1022. /* RRB - Forward action frame response to the Current AP */
  1023. /*
  1024. * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
  1025. * Status_Code[2] FT Request action frame body[variable]
  1026. */
  1027. rlen = 2 + 2 * ETH_ALEN + 2 + resp_ies_len;
  1028. frame = os_malloc(sizeof(*frame) + rlen);
  1029. frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  1030. frame->packet_type = FT_PACKET_RESPONSE;
  1031. frame->action_length = host_to_le16(rlen);
  1032. os_memcpy(frame->ap_address, wpa_auth->addr, ETH_ALEN);
  1033. pos = (u8 *) (frame + 1);
  1034. *pos++ = WLAN_ACTION_FT;
  1035. *pos++ = 2; /* Action: Response */
  1036. os_memcpy(pos, sta_addr, ETH_ALEN);
  1037. pos += ETH_ALEN;
  1038. os_memcpy(pos, wpa_auth->addr, ETH_ALEN);
  1039. pos += ETH_ALEN;
  1040. WPA_PUT_LE16(pos, status);
  1041. pos += 2;
  1042. if (resp_ies) {
  1043. os_memcpy(pos, resp_ies, resp_ies_len);
  1044. os_free(resp_ies);
  1045. }
  1046. wpa_ft_rrb_send(wpa_auth, current_ap, (u8 *) frame,
  1047. sizeof(*frame) + rlen);
  1048. os_free(frame);
  1049. return 0;
  1050. }
  1051. static int wpa_ft_rrb_rx_pull(struct wpa_authenticator *wpa_auth,
  1052. const u8 *src_addr,
  1053. const u8 *data, size_t data_len)
  1054. {
  1055. struct ft_r0kh_r1kh_pull_frame *frame, f;
  1056. struct ft_remote_r1kh *r1kh;
  1057. struct ft_r0kh_r1kh_resp_frame resp, r;
  1058. u8 pmk_r0[PMK_LEN];
  1059. wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull");
  1060. if (data_len < sizeof(*frame))
  1061. return -1;
  1062. r1kh = wpa_auth->conf.r1kh_list;
  1063. while (r1kh) {
  1064. if (os_memcmp(r1kh->addr, src_addr, ETH_ALEN) == 0)
  1065. break;
  1066. r1kh = r1kh->next;
  1067. }
  1068. if (r1kh == NULL) {
  1069. wpa_printf(MSG_DEBUG, "FT: No matching R1KH address found for "
  1070. "PMK-R1 pull source address " MACSTR,
  1071. MAC2STR(src_addr));
  1072. return -1;
  1073. }
  1074. frame = (struct ft_r0kh_r1kh_pull_frame *) data;
  1075. /* aes_unwrap() does not support inplace decryption, so use a temporary
  1076. * buffer for the data. */
  1077. if (aes_unwrap(r1kh->key, (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8,
  1078. frame->nonce, f.nonce) < 0) {
  1079. wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull "
  1080. "request from " MACSTR, MAC2STR(src_addr));
  1081. return -1;
  1082. }
  1083. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce",
  1084. f.nonce, sizeof(f.nonce));
  1085. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR0Name",
  1086. f.pmk_r0_name, WPA_PMK_NAME_LEN);
  1087. wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID="
  1088. MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));
  1089. os_memset(&resp, 0, sizeof(resp));
  1090. resp.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  1091. resp.packet_type = FT_PACKET_R0KH_R1KH_RESP;
  1092. resp.data_length = host_to_le16(FT_R0KH_R1KH_RESP_DATA_LEN);
  1093. os_memcpy(resp.ap_address, wpa_auth->addr, ETH_ALEN);
  1094. /* aes_wrap() does not support inplace encryption, so use a temporary
  1095. * buffer for the data. */
  1096. os_memcpy(r.nonce, f.nonce, sizeof(f.nonce));
  1097. os_memcpy(r.r1kh_id, f.r1kh_id, FT_R1KH_ID_LEN);
  1098. os_memcpy(r.s1kh_id, f.s1kh_id, ETH_ALEN);
  1099. if (wpa_ft_fetch_pmk_r0(wpa_auth, f.s1kh_id, f.pmk_r0_name, pmk_r0) <
  1100. 0) {
  1101. wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name found for "
  1102. "PMK-R1 pull");
  1103. return -1;
  1104. }
  1105. wpa_derive_pmk_r1(pmk_r0, f.pmk_r0_name, f.r1kh_id, f.s1kh_id,
  1106. r.pmk_r1, r.pmk_r1_name);
  1107. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", r.pmk_r1, PMK_LEN);
  1108. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", r.pmk_r1_name,
  1109. WPA_PMK_NAME_LEN);
  1110. if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8,
  1111. r.nonce, resp.nonce) < 0) {
  1112. os_memset(pmk_r0, 0, PMK_LEN);
  1113. return -1;
  1114. }
  1115. os_memset(pmk_r0, 0, PMK_LEN);
  1116. wpa_ft_rrb_send(wpa_auth, src_addr, (u8 *) &resp, sizeof(resp));
  1117. return 0;
  1118. }
  1119. static int wpa_ft_rrb_rx_resp(struct wpa_authenticator *wpa_auth,
  1120. const u8 *src_addr,
  1121. const u8 *data, size_t data_len)
  1122. {
  1123. struct ft_r0kh_r1kh_resp_frame *frame, f;
  1124. struct ft_remote_r0kh *r0kh;
  1125. wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull response");
  1126. if (data_len < sizeof(*frame))
  1127. return -1;
  1128. r0kh = wpa_auth->conf.r0kh_list;
  1129. while (r0kh) {
  1130. if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0)
  1131. break;
  1132. r0kh = r0kh->next;
  1133. }
  1134. if (r0kh == NULL) {
  1135. wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for "
  1136. "PMK-R0 pull response source address " MACSTR,
  1137. MAC2STR(src_addr));
  1138. return -1;
  1139. }
  1140. frame = (struct ft_r0kh_r1kh_resp_frame *) data;
  1141. /* aes_unwrap() does not support inplace decryption, so use a temporary
  1142. * buffer for the data. */
  1143. if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8,
  1144. frame->nonce, f.nonce) < 0) {
  1145. wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull "
  1146. "response from " MACSTR, MAC2STR(src_addr));
  1147. return -1;
  1148. }
  1149. if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN)
  1150. != 0) {
  1151. wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull response did not use a "
  1152. "matching R1KH-ID");
  1153. return -1;
  1154. }
  1155. /* TODO: verify that <nonce,s1kh_id> matches with a pending request
  1156. * and call this requests callback function to finish request
  1157. * processing */
  1158. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce",
  1159. f.nonce, sizeof(f.nonce));
  1160. wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID="
  1161. MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));
  1162. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 pull - PMK-R1",
  1163. f.pmk_r1, PMK_LEN);
  1164. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR1Name",
  1165. f.pmk_r1_name, WPA_PMK_NAME_LEN);
  1166. wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name);
  1167. os_memset(f.pmk_r1, 0, PMK_LEN);
  1168. return 0;
  1169. }
  1170. static int wpa_ft_rrb_rx_push(struct wpa_authenticator *wpa_auth,
  1171. const u8 *src_addr,
  1172. const u8 *data, size_t data_len)
  1173. {
  1174. struct ft_r0kh_r1kh_push_frame *frame, f;
  1175. struct ft_remote_r0kh *r0kh;
  1176. struct os_time now;
  1177. os_time_t tsend;
  1178. wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 push");
  1179. if (data_len < sizeof(*frame))
  1180. return -1;
  1181. r0kh = wpa_auth->conf.r0kh_list;
  1182. while (r0kh) {
  1183. if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0)
  1184. break;
  1185. r0kh = r0kh->next;
  1186. }
  1187. if (r0kh == NULL) {
  1188. wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for "
  1189. "PMK-R0 push source address " MACSTR,
  1190. MAC2STR(src_addr));
  1191. return -1;
  1192. }
  1193. frame = (struct ft_r0kh_r1kh_push_frame *) data;
  1194. /* aes_unwrap() does not support inplace decryption, so use a temporary
  1195. * buffer for the data. */
  1196. if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8,
  1197. frame->timestamp, f.timestamp) < 0) {
  1198. wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 push from "
  1199. MACSTR, MAC2STR(src_addr));
  1200. return -1;
  1201. }
  1202. os_get_time(&now);
  1203. tsend = WPA_GET_LE32(f.timestamp);
  1204. if ((now.sec > tsend && now.sec - tsend > 60) ||
  1205. (now.sec < tsend && tsend - now.sec > 60)) {
  1206. wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not have a valid "
  1207. "timestamp: sender time %d own time %d\n",
  1208. (int) tsend, (int) now.sec);
  1209. return -1;
  1210. }
  1211. if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN)
  1212. != 0) {
  1213. wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not use a matching "
  1214. "R1KH-ID (received " MACSTR " own " MACSTR ")",
  1215. MAC2STR(f.r1kh_id),
  1216. MAC2STR(wpa_auth->conf.r1_key_holder));
  1217. return -1;
  1218. }
  1219. wpa_printf(MSG_DEBUG, "FT: PMK-R1 push - R1KH-ID=" MACSTR " S1KH-ID="
  1220. MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));
  1221. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 push - PMK-R1",
  1222. f.pmk_r1, PMK_LEN);
  1223. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 push - PMKR1Name",
  1224. f.pmk_r1_name, WPA_PMK_NAME_LEN);
  1225. wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name);
  1226. os_memset(f.pmk_r1, 0, PMK_LEN);
  1227. return 0;
  1228. }
  1229. int wpa_ft_rrb_rx(struct wpa_authenticator *wpa_auth, const u8 *src_addr,
  1230. const u8 *data, size_t data_len)
  1231. {
  1232. struct ft_rrb_frame *frame;
  1233. u16 alen;
  1234. const u8 *pos, *end, *start;
  1235. u8 action;
  1236. const u8 *sta_addr, *target_ap_addr;
  1237. wpa_printf(MSG_DEBUG, "FT: RRB received frame from remote AP " MACSTR,
  1238. MAC2STR(src_addr));
  1239. if (data_len < sizeof(*frame)) {
  1240. wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (data_len=%lu)",
  1241. (unsigned long) data_len);
  1242. return -1;
  1243. }
  1244. pos = data;
  1245. frame = (struct ft_rrb_frame *) pos;
  1246. pos += sizeof(*frame);
  1247. alen = le_to_host16(frame->action_length);
  1248. wpa_printf(MSG_DEBUG, "FT: RRB frame - frame_type=%d packet_type=%d "
  1249. "action_length=%d ap_address=" MACSTR,
  1250. frame->frame_type, frame->packet_type, alen,
  1251. MAC2STR(frame->ap_address));
  1252. if (frame->frame_type != RSN_REMOTE_FRAME_TYPE_FT_RRB) {
  1253. /* Discard frame per IEEE Std 802.11r-2008, 11A.10.3 */
  1254. wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with "
  1255. "unrecognized type %d", frame->frame_type);
  1256. return -1;
  1257. }
  1258. if (alen > data_len - sizeof(*frame)) {
  1259. wpa_printf(MSG_DEBUG, "FT: RRB frame too short for action "
  1260. "frame");
  1261. return -1;
  1262. }
  1263. if (frame->packet_type == FT_PACKET_R0KH_R1KH_PULL)
  1264. return wpa_ft_rrb_rx_pull(wpa_auth, src_addr, data, data_len);
  1265. if (frame->packet_type == FT_PACKET_R0KH_R1KH_RESP)
  1266. return wpa_ft_rrb_rx_resp(wpa_auth, src_addr, data, data_len);
  1267. if (frame->packet_type == FT_PACKET_R0KH_R1KH_PUSH)
  1268. return wpa_ft_rrb_rx_push(wpa_auth, src_addr, data, data_len);
  1269. wpa_hexdump(MSG_MSGDUMP, "FT: RRB - FT Action frame", pos, alen);
  1270. if (alen < 1 + 1 + 2 * ETH_ALEN) {
  1271. wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (not enough "
  1272. "room for Action Frame body); alen=%lu",
  1273. (unsigned long) alen);
  1274. return -1;
  1275. }
  1276. start = pos;
  1277. end = pos + alen;
  1278. if (*pos != WLAN_ACTION_FT) {
  1279. wpa_printf(MSG_DEBUG, "FT: Unexpected Action frame category "
  1280. "%d", *pos);
  1281. return -1;
  1282. }
  1283. pos++;
  1284. action = *pos++;
  1285. sta_addr = pos;
  1286. pos += ETH_ALEN;
  1287. target_ap_addr = pos;
  1288. pos += ETH_ALEN;
  1289. wpa_printf(MSG_DEBUG, "FT: RRB Action Frame: action=%d sta_addr="
  1290. MACSTR " target_ap_addr=" MACSTR,
  1291. action, MAC2STR(sta_addr), MAC2STR(target_ap_addr));
  1292. if (frame->packet_type == FT_PACKET_REQUEST) {
  1293. wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Request");
  1294. if (action != 1) {
  1295. wpa_printf(MSG_DEBUG, "FT: Unexpected Action %d in "
  1296. "RRB Request", action);
  1297. return -1;
  1298. }
  1299. if (os_memcmp(target_ap_addr, wpa_auth->addr, ETH_ALEN) != 0) {
  1300. wpa_printf(MSG_DEBUG, "FT: Target AP address in the "
  1301. "RRB Request does not match with own "
  1302. "address");
  1303. return -1;
  1304. }
  1305. if (wpa_ft_rrb_rx_request(wpa_auth, frame->ap_address,
  1306. sta_addr, pos, end - pos) < 0)
  1307. return -1;
  1308. } else if (frame->packet_type == FT_PACKET_RESPONSE) {
  1309. u16 status_code;
  1310. if (end - pos < 2) {
  1311. wpa_printf(MSG_DEBUG, "FT: Not enough room for status "
  1312. "code in RRB Response");
  1313. return -1;
  1314. }
  1315. status_code = WPA_GET_LE16(pos);
  1316. pos += 2;
  1317. wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Response "
  1318. "(status_code=%d)", status_code);
  1319. if (wpa_ft_action_send(wpa_auth, sta_addr, start, alen) < 0)
  1320. return -1;
  1321. } else {
  1322. wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with unknown "
  1323. "packet_type %d", frame->packet_type);
  1324. return -1;
  1325. }
  1326. return 0;
  1327. }
  1328. static void wpa_ft_generate_pmk_r1(struct wpa_authenticator *wpa_auth,
  1329. struct wpa_ft_pmk_r0_sa *pmk_r0,
  1330. struct ft_remote_r1kh *r1kh,
  1331. const u8 *s1kh_id)
  1332. {
  1333. struct ft_r0kh_r1kh_push_frame frame, f;
  1334. struct os_time now;
  1335. os_memset(&frame, 0, sizeof(frame));
  1336. frame.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  1337. frame.packet_type = FT_PACKET_R0KH_R1KH_PUSH;
  1338. frame.data_length = host_to_le16(FT_R0KH_R1KH_PUSH_DATA_LEN);
  1339. os_memcpy(frame.ap_address, wpa_auth->addr, ETH_ALEN);
  1340. /* aes_wrap() does not support inplace encryption, so use a temporary
  1341. * buffer for the data. */
  1342. os_memcpy(f.r1kh_id, r1kh->id, FT_R1KH_ID_LEN);
  1343. os_memcpy(f.s1kh_id, s1kh_id, ETH_ALEN);
  1344. os_memcpy(f.pmk_r0_name, pmk_r0->pmk_r0_name, WPA_PMK_NAME_LEN);
  1345. wpa_derive_pmk_r1(pmk_r0->pmk_r0, pmk_r0->pmk_r0_name, r1kh->id,
  1346. s1kh_id, f.pmk_r1, f.pmk_r1_name);
  1347. wpa_printf(MSG_DEBUG, "FT: R1KH-ID " MACSTR, MAC2STR(r1kh->id));
  1348. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", f.pmk_r1, PMK_LEN);
  1349. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", f.pmk_r1_name,
  1350. WPA_PMK_NAME_LEN);
  1351. os_get_time(&now);
  1352. WPA_PUT_LE32(f.timestamp, now.sec);
  1353. if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8,
  1354. f.timestamp, frame.timestamp) < 0)
  1355. return;
  1356. wpa_ft_rrb_send(wpa_auth, r1kh->addr, (u8 *) &frame, sizeof(frame));
  1357. }
  1358. void wpa_ft_push_pmk_r1(struct wpa_authenticator *wpa_auth, const u8 *addr)
  1359. {
  1360. struct wpa_ft_pmk_r0_sa *r0;
  1361. struct ft_remote_r1kh *r1kh;
  1362. if (!wpa_auth->conf.pmk_r1_push)
  1363. return;
  1364. r0 = wpa_auth->ft_pmk_cache->pmk_r0;
  1365. while (r0) {
  1366. if (os_memcmp(r0->spa, addr, ETH_ALEN) == 0)
  1367. break;
  1368. r0 = r0->next;
  1369. }
  1370. if (r0 == NULL || r0->pmk_r1_pushed)
  1371. return;
  1372. r0->pmk_r1_pushed = 1;
  1373. wpa_printf(MSG_DEBUG, "FT: Deriving and pushing PMK-R1 keys to R1KHs "
  1374. "for STA " MACSTR, MAC2STR(addr));
  1375. r1kh = wpa_auth->conf.r1kh_list;
  1376. while (r1kh) {
  1377. wpa_ft_generate_pmk_r1(wpa_auth, r0, r1kh, addr);
  1378. r1kh = r1kh->next;
  1379. }
  1380. }
  1381. #endif /* CONFIG_IEEE80211R */