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