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