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