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