wpa_auth_ft.c 44 KB

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