• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * hostapd - IEEE 802.11r - Fast BSS Transition
3  * Copyright (c) 2004-2018, 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 "common/ocv.h"
17 #include "common/wpa_ctrl.h"
18 #include "drivers/driver.h"
19 #include "crypto/aes.h"
20 #include "crypto/aes_siv.h"
21 #include "crypto/aes_wrap.h"
22 #include "crypto/sha384.h"
23 #include "crypto/sha512.h"
24 #include "crypto/random.h"
25 #include "ap_config.h"
26 #include "ieee802_11.h"
27 #include "wmm.h"
28 #include "wpa_auth.h"
29 #include "wpa_auth_i.h"
30 #include "pmksa_cache_auth.h"
31 
32 
33 #ifdef CONFIG_IEEE80211R_AP
34 
35 const unsigned int ftRRBseqTimeout = 10;
36 const unsigned int ftRRBmaxQueueLen = 100;
37 
38 /* TODO: make these configurable */
39 static const int dot11RSNAConfigPMKLifetime = 43200;
40 
41 
42 static int wpa_ft_send_rrb_auth_resp(struct wpa_state_machine *sm,
43 				     const u8 *current_ap, const u8 *sta_addr,
44 				     u16 status, const u8 *resp_ies,
45 				     size_t resp_ies_len);
46 static void ft_finish_pull(struct wpa_state_machine *sm);
47 static void wpa_ft_expire_pull(void *eloop_ctx, void *timeout_ctx);
48 static void wpa_ft_rrb_seq_timeout(void *eloop_ctx, void *timeout_ctx);
49 
50 struct tlv_list {
51 	u16 type;
52 	size_t len;
53 	const u8 *data;
54 };
55 
56 
57 /**
58  * wpa_ft_rrb_decrypt - Decrypt FT RRB message
59  * @key: AES-SIV key for AEAD
60  * @key_len: Length of key in octets
61  * @enc: Pointer to encrypted TLVs
62  * @enc_len: Length of encrypted TLVs in octets
63  * @auth: Pointer to authenticated TLVs
64  * @auth_len: Length of authenticated TLVs in octets
65  * @src_addr: MAC address of the frame sender
66  * @type: Vendor-specific subtype of the RRB frame (FT_PACKET_*)
67  * @plain: Pointer to return the pointer to the allocated plaintext buffer;
68  *	needs to be freed by the caller if not NULL;
69  *	will only be returned on success
70  * @plain_len: Pointer to return the length of the allocated plaintext buffer
71  *	in octets
72  * Returns: 0 on success, -1 on error
73  */
wpa_ft_rrb_decrypt(const u8 * key,const size_t key_len,const u8 * enc,size_t enc_len,const u8 * auth,const size_t auth_len,const u8 * src_addr,u8 type,u8 ** plain,size_t * plain_size)74 static int wpa_ft_rrb_decrypt(const u8 *key, const size_t key_len,
75 			      const u8 *enc, size_t enc_len,
76 			      const u8 *auth, const size_t auth_len,
77 			      const u8 *src_addr, u8 type,
78 			      u8 **plain, size_t *plain_size)
79 {
80 	const u8 *ad[3] = { src_addr, auth, &type };
81 	size_t ad_len[3] = { ETH_ALEN, auth_len, sizeof(type) };
82 
83 	wpa_printf(MSG_DEBUG, "FT(RRB): src_addr=" MACSTR " type=%u",
84 		   MAC2STR(src_addr), type);
85 	wpa_hexdump_key(MSG_DEBUG, "FT(RRB): decrypt using key", key, key_len);
86 	wpa_hexdump(MSG_DEBUG, "FT(RRB): encrypted TLVs", enc, enc_len);
87 	wpa_hexdump(MSG_DEBUG, "FT(RRB): authenticated TLVs", auth, auth_len);
88 
89 	if (!key) { /* skip decryption */
90 		*plain = os_memdup(enc, enc_len);
91 		if (enc_len > 0 && !*plain)
92 			goto err;
93 
94 		*plain_size = enc_len;
95 
96 		return 0;
97 	}
98 
99 	*plain = NULL;
100 
101 	/* SIV overhead */
102 	if (enc_len < AES_BLOCK_SIZE)
103 		goto err;
104 
105 	*plain = os_zalloc(enc_len - AES_BLOCK_SIZE);
106 	if (!*plain)
107 		goto err;
108 
109 	if (aes_siv_decrypt(key, key_len, enc, enc_len, 3, ad, ad_len,
110 			    *plain) < 0) {
111 		if (enc_len < AES_BLOCK_SIZE + 2)
112 			goto err;
113 
114 		/* Try to work around Ethernet devices that add extra
115 		 * two octet padding even if the frame is longer than
116 		 * the minimum Ethernet frame. */
117 		enc_len -= 2;
118 		if (aes_siv_decrypt(key, key_len, enc, enc_len, 3, ad, ad_len,
119 				    *plain) < 0)
120 			goto err;
121 	}
122 
123 	*plain_size = enc_len - AES_BLOCK_SIZE;
124 	wpa_hexdump_key(MSG_DEBUG, "FT(RRB): decrypted TLVs",
125 			*plain, *plain_size);
126 	return 0;
127 err:
128 	os_free(*plain);
129 	*plain = NULL;
130 	*plain_size = 0;
131 
132 	wpa_printf(MSG_ERROR, "FT(RRB): Failed to decrypt");
133 
134 	return -1;
135 }
136 
137 
138 /* get first tlv record in packet matching type
139  * @data (decrypted) packet
140  * @return 0 on success else -1
141  */
wpa_ft_rrb_get_tlv(const u8 * plain,size_t plain_len,u16 type,size_t * tlv_len,const u8 ** tlv_data)142 static int wpa_ft_rrb_get_tlv(const u8 *plain, size_t plain_len,
143 			      u16 type, size_t *tlv_len, const u8 **tlv_data)
144 {
145 	const struct ft_rrb_tlv *f;
146 	size_t left;
147 	le16 type16;
148 	size_t len;
149 
150 	left = plain_len;
151 	type16 = host_to_le16(type);
152 
153 	while (left >= sizeof(*f)) {
154 		f = (const struct ft_rrb_tlv *) plain;
155 
156 		left -= sizeof(*f);
157 		plain += sizeof(*f);
158 		len = le_to_host16(f->len);
159 
160 		if (left < len) {
161 			wpa_printf(MSG_DEBUG, "FT: RRB message truncated");
162 			break;
163 		}
164 
165 		if (f->type == type16) {
166 			*tlv_len = len;
167 			*tlv_data = plain;
168 			return 0;
169 		}
170 
171 		left -= len;
172 		plain += len;
173 	}
174 
175 	return -1;
176 }
177 
178 
wpa_ft_rrb_dump(const u8 * plain,const size_t plain_len)179 static void wpa_ft_rrb_dump(const u8 *plain, const size_t plain_len)
180 {
181 	const struct ft_rrb_tlv *f;
182 	size_t left;
183 	size_t len;
184 
185 	left = plain_len;
186 
187 	wpa_printf(MSG_DEBUG, "FT: RRB dump message");
188 	while (left >= sizeof(*f)) {
189 		f = (const struct ft_rrb_tlv *) plain;
190 
191 		left -= sizeof(*f);
192 		plain += sizeof(*f);
193 		len = le_to_host16(f->len);
194 
195 		wpa_printf(MSG_DEBUG, "FT: RRB TLV type = %d, len = %zu",
196 			   le_to_host16(f->type), len);
197 
198 		if (left < len) {
199 			wpa_printf(MSG_DEBUG,
200 				   "FT: RRB message truncated: left %zu bytes, need %zu",
201 				   left, len);
202 			break;
203 		}
204 
205 		wpa_hexdump(MSG_DEBUG, "FT: RRB TLV data", plain, len);
206 
207 		left -= len;
208 		plain += len;
209 	}
210 
211 	if (left > 0)
212 		wpa_hexdump(MSG_DEBUG, "FT: RRB TLV padding", plain, left);
213 
214 	wpa_printf(MSG_DEBUG, "FT: RRB dump message end");
215 }
216 
217 
cmp_int(const void * a,const void * b)218 static int cmp_int(const void *a, const void *b)
219 {
220 	int x, y;
221 
222 	x = *((int *) a);
223 	y = *((int *) b);
224 	return x - y;
225 }
226 
227 
wpa_ft_rrb_get_tlv_vlan(const u8 * plain,const size_t plain_len,struct vlan_description * vlan)228 static int wpa_ft_rrb_get_tlv_vlan(const u8 *plain, const size_t plain_len,
229 				   struct vlan_description *vlan)
230 {
231 	struct ft_rrb_tlv *f;
232 	size_t left;
233 	size_t len;
234 	int taggedidx;
235 	int vlan_id;
236 	int type;
237 
238 	left = plain_len;
239 	taggedidx = 0;
240 	os_memset(vlan, 0, sizeof(*vlan));
241 
242 	while (left >= sizeof(*f)) {
243 		f = (struct ft_rrb_tlv *) plain;
244 
245 		left -= sizeof(*f);
246 		plain += sizeof(*f);
247 
248 		len = le_to_host16(f->len);
249 		type = le_to_host16(f->type);
250 
251 		if (left < len) {
252 			wpa_printf(MSG_DEBUG, "FT: RRB message truncated");
253 			return -1;
254 		}
255 
256 		if (type != FT_RRB_VLAN_UNTAGGED && type != FT_RRB_VLAN_TAGGED)
257 			goto skip;
258 
259 		if (type == FT_RRB_VLAN_UNTAGGED && len != sizeof(le16)) {
260 			wpa_printf(MSG_DEBUG,
261 				   "FT: RRB VLAN_UNTAGGED invalid length");
262 			return -1;
263 		}
264 
265 		if (type == FT_RRB_VLAN_TAGGED && len % sizeof(le16) != 0) {
266 			wpa_printf(MSG_DEBUG,
267 				   "FT: RRB VLAN_TAGGED invalid length");
268 			return -1;
269 		}
270 
271 		while (len >= sizeof(le16)) {
272 			vlan_id = WPA_GET_LE16(plain);
273 			plain += sizeof(le16);
274 			left -= sizeof(le16);
275 			len -= sizeof(le16);
276 
277 			if (vlan_id <= 0 || vlan_id > MAX_VLAN_ID) {
278 				wpa_printf(MSG_DEBUG,
279 					   "FT: RRB VLAN ID invalid %d",
280 					   vlan_id);
281 				continue;
282 			}
283 
284 			if (type == FT_RRB_VLAN_UNTAGGED)
285 				vlan->untagged = vlan_id;
286 
287 			if (type == FT_RRB_VLAN_TAGGED &&
288 			    taggedidx < MAX_NUM_TAGGED_VLAN) {
289 				vlan->tagged[taggedidx] = vlan_id;
290 				taggedidx++;
291 			} else if (type == FT_RRB_VLAN_TAGGED) {
292 				wpa_printf(MSG_DEBUG, "FT: RRB too many VLANs");
293 			}
294 		}
295 
296 	skip:
297 		left -= len;
298 		plain += len;
299 	}
300 
301 	if (taggedidx)
302 		qsort(vlan->tagged, taggedidx, sizeof(int), cmp_int);
303 
304 	vlan->notempty = vlan->untagged || vlan->tagged[0];
305 
306 	return 0;
307 }
308 
309 
wpa_ft_tlv_len(const struct tlv_list * tlvs)310 static size_t wpa_ft_tlv_len(const struct tlv_list *tlvs)
311 {
312 	size_t tlv_len = 0;
313 	int i;
314 
315 	if (!tlvs)
316 		return 0;
317 
318 	for (i = 0; tlvs[i].type != FT_RRB_LAST_EMPTY; i++) {
319 		tlv_len += sizeof(struct ft_rrb_tlv);
320 		tlv_len += tlvs[i].len;
321 	}
322 
323 	return tlv_len;
324 }
325 
326 
wpa_ft_tlv_lin(const struct tlv_list * tlvs,u8 * start,u8 * endpos)327 static size_t wpa_ft_tlv_lin(const struct tlv_list *tlvs, u8 *start,
328 			     u8 *endpos)
329 {
330 	int i;
331 	size_t tlv_len;
332 	struct ft_rrb_tlv *hdr;
333 	u8 *pos;
334 
335 	if (!tlvs)
336 		return 0;
337 
338 	tlv_len = 0;
339 	pos = start;
340 	for (i = 0; tlvs[i].type != FT_RRB_LAST_EMPTY; i++) {
341 		if (tlv_len + sizeof(*hdr) > (size_t) (endpos - start))
342 			return tlv_len;
343 		tlv_len += sizeof(*hdr);
344 		hdr = (struct ft_rrb_tlv *) pos;
345 		hdr->type = host_to_le16(tlvs[i].type);
346 		hdr->len = host_to_le16(tlvs[i].len);
347 		pos = start + tlv_len;
348 
349 		if (tlv_len + tlvs[i].len > (size_t) (endpos - start))
350 			return tlv_len;
351 		if (tlvs[i].len == 0)
352 			continue;
353 		tlv_len += tlvs[i].len;
354 		os_memcpy(pos, tlvs[i].data, tlvs[i].len);
355 		pos = start + tlv_len;
356 	}
357 
358 	return tlv_len;
359 }
360 
361 
wpa_ft_vlan_len(const struct vlan_description * vlan)362 static size_t wpa_ft_vlan_len(const struct vlan_description *vlan)
363 {
364 	size_t tlv_len = 0;
365 	int i;
366 
367 	if (!vlan || !vlan->notempty)
368 		return 0;
369 
370 	if (vlan->untagged) {
371 		tlv_len += sizeof(struct ft_rrb_tlv);
372 		tlv_len += sizeof(le16);
373 	}
374 	if (vlan->tagged[0])
375 		tlv_len += sizeof(struct ft_rrb_tlv);
376 	for (i = 0; i < MAX_NUM_TAGGED_VLAN && vlan->tagged[i]; i++)
377 		tlv_len += sizeof(le16);
378 
379 	return tlv_len;
380 }
381 
382 
wpa_ft_vlan_lin(const struct vlan_description * vlan,u8 * start,u8 * endpos)383 static size_t wpa_ft_vlan_lin(const struct vlan_description *vlan,
384 			      u8 *start, u8 *endpos)
385 {
386 	size_t tlv_len;
387 	int i, len;
388 	struct ft_rrb_tlv *hdr;
389 	u8 *pos = start;
390 
391 	if (!vlan || !vlan->notempty)
392 		return 0;
393 
394 	tlv_len = 0;
395 	if (vlan->untagged) {
396 		tlv_len += sizeof(*hdr);
397 		if (start + tlv_len > endpos)
398 			return tlv_len;
399 		hdr = (struct ft_rrb_tlv *) pos;
400 		hdr->type = host_to_le16(FT_RRB_VLAN_UNTAGGED);
401 		hdr->len = host_to_le16(sizeof(le16));
402 		pos = start + tlv_len;
403 
404 		tlv_len += sizeof(le16);
405 		if (start + tlv_len > endpos)
406 			return tlv_len;
407 		WPA_PUT_LE16(pos, vlan->untagged);
408 		pos = start + tlv_len;
409 	}
410 
411 	if (!vlan->tagged[0])
412 		return tlv_len;
413 
414 	tlv_len += sizeof(*hdr);
415 	if (start + tlv_len > endpos)
416 		return tlv_len;
417 	hdr = (struct ft_rrb_tlv *) pos;
418 	hdr->type = host_to_le16(FT_RRB_VLAN_TAGGED);
419 	len = 0; /* len is computed below */
420 	pos = start + tlv_len;
421 
422 	for (i = 0; i < MAX_NUM_TAGGED_VLAN && vlan->tagged[i]; i++) {
423 		tlv_len += sizeof(le16);
424 		if (start + tlv_len > endpos)
425 			break;
426 		len += sizeof(le16);
427 		WPA_PUT_LE16(pos, vlan->tagged[i]);
428 		pos = start + tlv_len;
429 	}
430 
431 	hdr->len = host_to_le16(len);
432 
433 	return tlv_len;
434 }
435 
436 
wpa_ft_rrb_lin(const struct tlv_list * tlvs1,const struct tlv_list * tlvs2,const struct vlan_description * vlan,u8 ** plain,size_t * plain_len)437 static int wpa_ft_rrb_lin(const struct tlv_list *tlvs1,
438 			  const struct tlv_list *tlvs2,
439 			  const struct vlan_description *vlan,
440 			  u8 **plain, size_t *plain_len)
441 {
442 	u8 *pos, *endpos;
443 	size_t tlv_len;
444 
445 	tlv_len = wpa_ft_tlv_len(tlvs1);
446 	tlv_len += wpa_ft_tlv_len(tlvs2);
447 	tlv_len += wpa_ft_vlan_len(vlan);
448 
449 	*plain_len = tlv_len;
450 	*plain = os_zalloc(tlv_len);
451 	if (!*plain) {
452 		wpa_printf(MSG_ERROR, "FT: Failed to allocate plaintext");
453 		goto err;
454 	}
455 
456 	pos = *plain;
457 	endpos = *plain + tlv_len;
458 	pos += wpa_ft_tlv_lin(tlvs1, pos, endpos);
459 	pos += wpa_ft_tlv_lin(tlvs2, pos, endpos);
460 	pos += wpa_ft_vlan_lin(vlan, pos, endpos);
461 
462 	/* validity check */
463 	if (pos != endpos) {
464 		wpa_printf(MSG_ERROR, "FT: Length error building RRB");
465 		goto err;
466 	}
467 
468 	return 0;
469 
470 err:
471 	os_free(*plain);
472 	*plain = NULL;
473 	*plain_len = 0;
474 	return -1;
475 }
476 
477 
wpa_ft_rrb_encrypt(const u8 * key,const size_t key_len,const u8 * plain,const size_t plain_len,const u8 * auth,const size_t auth_len,const u8 * src_addr,u8 type,u8 * enc)478 static int wpa_ft_rrb_encrypt(const u8 *key, const size_t key_len,
479 			      const u8 *plain, const size_t plain_len,
480 			      const u8 *auth, const size_t auth_len,
481 			      const u8 *src_addr, u8 type, u8 *enc)
482 {
483 	const u8 *ad[3] = { src_addr, auth, &type };
484 	size_t ad_len[3] = { ETH_ALEN, auth_len, sizeof(type) };
485 
486 	wpa_printf(MSG_DEBUG, "FT(RRB): src_addr=" MACSTR " type=%u",
487 		   MAC2STR(src_addr), type);
488 	wpa_hexdump_key(MSG_DEBUG, "FT(RRB): plaintext message",
489 			plain, plain_len);
490 	wpa_hexdump_key(MSG_DEBUG, "FT(RRB): encrypt using key", key, key_len);
491 	wpa_hexdump(MSG_DEBUG, "FT(RRB): authenticated TLVs", auth, auth_len);
492 
493 	if (!key) {
494 		/* encryption not needed, return plaintext as packet */
495 		os_memcpy(enc, plain, plain_len);
496 	} else if (aes_siv_encrypt(key, key_len, plain, plain_len,
497 				   3, ad, ad_len, enc) < 0) {
498 		wpa_printf(MSG_ERROR, "FT: Failed to encrypt RRB-OUI message");
499 		return -1;
500 	}
501 	wpa_hexdump(MSG_DEBUG, "FT(RRB): encrypted TLVs",
502 		    enc, plain_len + AES_BLOCK_SIZE);
503 
504 	return 0;
505 }
506 
507 
508 /**
509  * wpa_ft_rrb_build - Build and encrypt an FT RRB message
510  * @key: AES-SIV key for AEAD
511  * @key_len: Length of key in octets
512  * @tlvs_enc0: First set of to-be-encrypted TLVs
513  * @tlvs_enc1: Second set of to-be-encrypted TLVs
514  * @tlvs_auth: Set of to-be-authenticated TLVs
515  * @src_addr: MAC address of the frame sender
516  * @type: Vendor-specific subtype of the RRB frame (FT_PACKET_*)
517  * @packet Pointer to return the pointer to the allocated packet buffer;
518  *         needs to be freed by the caller if not null;
519  *         will only be returned on success
520  * @packet_len: Pointer to return the length of the allocated buffer in octets
521  * Returns: 0 on success, -1 on error
522  */
wpa_ft_rrb_build(const u8 * key,const size_t key_len,const struct tlv_list * tlvs_enc0,const struct tlv_list * tlvs_enc1,const struct tlv_list * tlvs_auth,const struct vlan_description * vlan,const u8 * src_addr,u8 type,u8 ** packet,size_t * packet_len)523 static int wpa_ft_rrb_build(const u8 *key, const size_t key_len,
524 			    const struct tlv_list *tlvs_enc0,
525 			    const struct tlv_list *tlvs_enc1,
526 			    const struct tlv_list *tlvs_auth,
527 			    const struct vlan_description *vlan,
528 			    const u8 *src_addr, u8 type,
529 			    u8 **packet, size_t *packet_len)
530 {
531 	u8 *plain = NULL, *auth = NULL, *pos, *tmp;
532 	size_t plain_len = 0, auth_len = 0;
533 	int ret = -1;
534 	size_t pad_len = 0;
535 
536 	*packet = NULL;
537 	if (wpa_ft_rrb_lin(tlvs_enc0, tlvs_enc1, vlan, &plain, &plain_len) < 0)
538 		goto out;
539 
540 	if (wpa_ft_rrb_lin(tlvs_auth, NULL, NULL, &auth, &auth_len) < 0)
541 		goto out;
542 
543 	*packet_len = sizeof(u16) + auth_len + plain_len;
544 	if (key)
545 		*packet_len += AES_BLOCK_SIZE;
546 #define RRB_MIN_MSG_LEN 64
547 	if (*packet_len < RRB_MIN_MSG_LEN) {
548 		pad_len = RRB_MIN_MSG_LEN - *packet_len;
549 		if (pad_len < sizeof(struct ft_rrb_tlv))
550 			pad_len = sizeof(struct ft_rrb_tlv);
551 		wpa_printf(MSG_DEBUG,
552 			   "FT: Pad message to minimum Ethernet frame length (%d --> %d)",
553 			   (int) *packet_len, (int) (*packet_len + pad_len));
554 		*packet_len += pad_len;
555 		tmp = os_realloc(auth, auth_len + pad_len);
556 		if (!tmp)
557 			goto out;
558 		auth = tmp;
559 		pos = auth + auth_len;
560 		WPA_PUT_LE16(pos, FT_RRB_LAST_EMPTY);
561 		pos += 2;
562 		WPA_PUT_LE16(pos, pad_len - sizeof(struct ft_rrb_tlv));
563 		pos += 2;
564 		os_memset(pos, 0, pad_len - sizeof(struct ft_rrb_tlv));
565 		auth_len += pad_len;
566 
567 	}
568 	*packet = os_zalloc(*packet_len);
569 	if (!*packet)
570 		goto out;
571 
572 	pos = *packet;
573 	WPA_PUT_LE16(pos, auth_len);
574 	pos += 2;
575 	os_memcpy(pos, auth, auth_len);
576 	pos += auth_len;
577 	if (wpa_ft_rrb_encrypt(key, key_len, plain, plain_len, auth,
578 			       auth_len, src_addr, type, pos) < 0)
579 		goto out;
580 	wpa_hexdump(MSG_MSGDUMP, "FT: RRB frame payload", *packet, *packet_len);
581 
582 	ret = 0;
583 
584 out:
585 	bin_clear_free(plain, plain_len);
586 	os_free(auth);
587 
588 	if (ret) {
589 		wpa_printf(MSG_ERROR, "FT: Failed to build RRB-OUI message");
590 		os_free(*packet);
591 		*packet = NULL;
592 		*packet_len = 0;
593 	}
594 
595 	return ret;
596 }
597 
598 
599 #define RRB_GET_SRC(srcfield, type, field, txt, checklength) do { \
600 	if (wpa_ft_rrb_get_tlv(srcfield, srcfield##_len, type, \
601 				&f_##field##_len, &f_##field) < 0 || \
602 	    (checklength > 0 && ((size_t) checklength) != f_##field##_len)) { \
603 		wpa_printf(MSG_INFO, "FT: Missing required " #field \
604 			   " in %s from " MACSTR, txt, MAC2STR(src_addr)); \
605 		wpa_ft_rrb_dump(srcfield, srcfield##_len); \
606 		goto out; \
607 	} \
608 } while (0)
609 
610 #define RRB_GET(type, field, txt, checklength) \
611 	RRB_GET_SRC(plain, type, field, txt, checklength)
612 #define RRB_GET_AUTH(type, field, txt, checklength) \
613 	RRB_GET_SRC(auth, type, field, txt, checklength)
614 
615 #define RRB_GET_OPTIONAL_SRC(srcfield, type, field, txt, checklength) do { \
616 	if (wpa_ft_rrb_get_tlv(srcfield, srcfield##_len, type, \
617 				&f_##field##_len, &f_##field) < 0 || \
618 	    (checklength > 0 && ((size_t) checklength) != f_##field##_len)) { \
619 		wpa_printf(MSG_DEBUG, "FT: Missing optional " #field \
620 			   " in %s from " MACSTR, txt, MAC2STR(src_addr)); \
621 		f_##field##_len = 0; \
622 		f_##field = NULL; \
623 	} \
624 } while (0)
625 
626 #define RRB_GET_OPTIONAL(type, field, txt, checklength) \
627 	RRB_GET_OPTIONAL_SRC(plain, type, field, txt, checklength)
628 #define RRB_GET_OPTIONAL_AUTH(type, field, txt, checklength) \
629 	RRB_GET_OPTIONAL_SRC(auth, type, field, txt, checklength)
630 
wpa_ft_rrb_send(struct wpa_authenticator * wpa_auth,const u8 * dst,const u8 * data,size_t data_len)631 static int wpa_ft_rrb_send(struct wpa_authenticator *wpa_auth, const u8 *dst,
632 			   const u8 *data, size_t data_len)
633 {
634 	if (wpa_auth->cb->send_ether == NULL)
635 		return -1;
636 	wpa_printf(MSG_DEBUG, "FT: RRB send to " MACSTR, MAC2STR(dst));
637 	return wpa_auth->cb->send_ether(wpa_auth->cb_ctx, dst, ETH_P_RRB,
638 					data, data_len);
639 }
640 
641 
wpa_ft_rrb_oui_send(struct wpa_authenticator * wpa_auth,const u8 * dst,u8 oui_suffix,const u8 * data,size_t data_len)642 static int wpa_ft_rrb_oui_send(struct wpa_authenticator *wpa_auth,
643 			       const u8 *dst, u8 oui_suffix,
644 			       const u8 *data, size_t data_len)
645 {
646 	if (!wpa_auth->cb->send_oui)
647 		return -1;
648 	wpa_printf(MSG_DEBUG, "FT: RRB-OUI type %u send to " MACSTR " (len=%u)",
649 		   oui_suffix, MAC2STR(dst), (unsigned int) data_len);
650 	return wpa_auth->cb->send_oui(wpa_auth->cb_ctx, dst, oui_suffix, data,
651 				      data_len);
652 }
653 
654 
wpa_ft_action_send(struct wpa_authenticator * wpa_auth,const u8 * dst,const u8 * data,size_t data_len)655 static int wpa_ft_action_send(struct wpa_authenticator *wpa_auth,
656 			      const u8 *dst, const u8 *data, size_t data_len)
657 {
658 	if (wpa_auth->cb->send_ft_action == NULL)
659 		return -1;
660 	return wpa_auth->cb->send_ft_action(wpa_auth->cb_ctx, dst,
661 					    data, data_len);
662 }
663 
664 
wpa_ft_get_psk(struct wpa_authenticator * wpa_auth,const u8 * addr,const u8 * p2p_dev_addr,const u8 * prev_psk)665 static const u8 * wpa_ft_get_psk(struct wpa_authenticator *wpa_auth,
666 				 const u8 *addr, const u8 *p2p_dev_addr,
667 				 const u8 *prev_psk)
668 {
669 	if (wpa_auth->cb->get_psk == NULL)
670 		return NULL;
671 	return wpa_auth->cb->get_psk(wpa_auth->cb_ctx, addr, p2p_dev_addr,
672 				     prev_psk, NULL, NULL);
673 }
674 
675 
676 static struct wpa_state_machine *
wpa_ft_add_sta(struct wpa_authenticator * wpa_auth,const u8 * sta_addr)677 wpa_ft_add_sta(struct wpa_authenticator *wpa_auth, const u8 *sta_addr)
678 {
679 	if (wpa_auth->cb->add_sta == NULL)
680 		return NULL;
681 	return wpa_auth->cb->add_sta(wpa_auth->cb_ctx, sta_addr);
682 }
683 
684 
wpa_ft_set_vlan(struct wpa_authenticator * wpa_auth,const u8 * sta_addr,struct vlan_description * vlan)685 static int wpa_ft_set_vlan(struct wpa_authenticator *wpa_auth,
686 			   const u8 *sta_addr, struct vlan_description *vlan)
687 {
688 	if (!wpa_auth->cb->set_vlan)
689 		return -1;
690 	return wpa_auth->cb->set_vlan(wpa_auth->cb_ctx, sta_addr, vlan);
691 }
692 
693 
wpa_ft_get_vlan(struct wpa_authenticator * wpa_auth,const u8 * sta_addr,struct vlan_description * vlan)694 static int wpa_ft_get_vlan(struct wpa_authenticator *wpa_auth,
695 			   const u8 *sta_addr, struct vlan_description *vlan)
696 {
697 	if (!wpa_auth->cb->get_vlan)
698 		return -1;
699 	return wpa_auth->cb->get_vlan(wpa_auth->cb_ctx, sta_addr, vlan);
700 }
701 
702 
703 static int
wpa_ft_set_identity(struct wpa_authenticator * wpa_auth,const u8 * sta_addr,const u8 * identity,size_t identity_len)704 wpa_ft_set_identity(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
705 		    const u8 *identity, size_t identity_len)
706 {
707 	if (!wpa_auth->cb->set_identity)
708 		return -1;
709 	return wpa_auth->cb->set_identity(wpa_auth->cb_ctx, sta_addr, identity,
710 					  identity_len);
711 }
712 
713 
714 static size_t
wpa_ft_get_identity(struct wpa_authenticator * wpa_auth,const u8 * sta_addr,const u8 ** buf)715 wpa_ft_get_identity(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
716 		    const u8 **buf)
717 {
718 	*buf = NULL;
719 	if (!wpa_auth->cb->get_identity)
720 		return 0;
721 	return wpa_auth->cb->get_identity(wpa_auth->cb_ctx, sta_addr, buf);
722 }
723 
724 
725 static int
wpa_ft_set_radius_cui(struct wpa_authenticator * wpa_auth,const u8 * sta_addr,const u8 * radius_cui,size_t radius_cui_len)726 wpa_ft_set_radius_cui(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
727 		      const u8 *radius_cui, size_t radius_cui_len)
728 {
729 	if (!wpa_auth->cb->set_radius_cui)
730 		return -1;
731 	return wpa_auth->cb->set_radius_cui(wpa_auth->cb_ctx, sta_addr,
732 					    radius_cui, radius_cui_len);
733 }
734 
735 
736 static size_t
wpa_ft_get_radius_cui(struct wpa_authenticator * wpa_auth,const u8 * sta_addr,const u8 ** buf)737 wpa_ft_get_radius_cui(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
738 		      const u8 **buf)
739 {
740 	*buf = NULL;
741 	if (!wpa_auth->cb->get_radius_cui)
742 		return 0;
743 	return wpa_auth->cb->get_radius_cui(wpa_auth->cb_ctx, sta_addr, buf);
744 }
745 
746 
747 static void
wpa_ft_set_session_timeout(struct wpa_authenticator * wpa_auth,const u8 * sta_addr,int session_timeout)748 wpa_ft_set_session_timeout(struct wpa_authenticator *wpa_auth,
749 			    const u8 *sta_addr, int session_timeout)
750 {
751 	if (!wpa_auth->cb->set_session_timeout)
752 		return;
753 	wpa_auth->cb->set_session_timeout(wpa_auth->cb_ctx, sta_addr,
754 					  session_timeout);
755 }
756 
757 
758 static int
wpa_ft_get_session_timeout(struct wpa_authenticator * wpa_auth,const u8 * sta_addr)759 wpa_ft_get_session_timeout(struct wpa_authenticator *wpa_auth,
760 			    const u8 *sta_addr)
761 {
762 	if (!wpa_auth->cb->get_session_timeout)
763 		return 0;
764 	return wpa_auth->cb->get_session_timeout(wpa_auth->cb_ctx, sta_addr);
765 }
766 
767 
wpa_ft_add_tspec(struct wpa_authenticator * wpa_auth,const u8 * sta_addr,u8 * tspec_ie,size_t tspec_ielen)768 static int wpa_ft_add_tspec(struct wpa_authenticator *wpa_auth,
769 			    const u8 *sta_addr,
770 			    u8 *tspec_ie, size_t tspec_ielen)
771 {
772 	if (wpa_auth->cb->add_tspec == NULL) {
773 		wpa_printf(MSG_DEBUG, "FT: add_tspec is not initialized");
774 		return -1;
775 	}
776 	return wpa_auth->cb->add_tspec(wpa_auth->cb_ctx, sta_addr, tspec_ie,
777 				       tspec_ielen);
778 }
779 
780 
781 #ifdef CONFIG_OCV
wpa_channel_info(struct wpa_authenticator * wpa_auth,struct wpa_channel_info * ci)782 static int wpa_channel_info(struct wpa_authenticator *wpa_auth,
783 			       struct wpa_channel_info *ci)
784 {
785 	if (!wpa_auth->cb->channel_info)
786 		return -1;
787 	return wpa_auth->cb->channel_info(wpa_auth->cb_ctx, ci);
788 }
789 #endif /* CONFIG_OCV */
790 
791 
wpa_write_mdie(struct wpa_auth_config * conf,u8 * buf,size_t len)792 int wpa_write_mdie(struct wpa_auth_config *conf, u8 *buf, size_t len)
793 {
794 	u8 *pos = buf;
795 	u8 capab;
796 	if (len < 2 + sizeof(struct rsn_mdie))
797 		return -1;
798 
799 	*pos++ = WLAN_EID_MOBILITY_DOMAIN;
800 	*pos++ = MOBILITY_DOMAIN_ID_LEN + 1;
801 	os_memcpy(pos, conf->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
802 	pos += MOBILITY_DOMAIN_ID_LEN;
803 	capab = 0;
804 	if (conf->ft_over_ds)
805 		capab |= RSN_FT_CAPAB_FT_OVER_DS;
806 	*pos++ = capab;
807 
808 	return pos - buf;
809 }
810 
811 
wpa_write_ftie(struct wpa_auth_config * conf,int key_mgmt,size_t key_len,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,int rsnxe_used)812 int wpa_write_ftie(struct wpa_auth_config *conf, int key_mgmt, size_t key_len,
813 		   const u8 *r0kh_id, size_t r0kh_id_len,
814 		   const u8 *anonce, const u8 *snonce,
815 		   u8 *buf, size_t len, const u8 *subelem,
816 		   size_t subelem_len, int rsnxe_used)
817 {
818 	u8 *pos = buf, *ielen;
819 	size_t hdrlen;
820 	u16 mic_control = rsnxe_used ? FTE_MIC_CTRL_RSNXE_USED : 0;
821 
822 	if (key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
823 	    key_len == SHA256_MAC_LEN)
824 		hdrlen = sizeof(struct rsn_ftie);
825 	else if (key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
826 		 key_len == SHA384_MAC_LEN)
827 		hdrlen = sizeof(struct rsn_ftie_sha384);
828 	else if (key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
829 		 key_len == SHA512_MAC_LEN)
830 		hdrlen = sizeof(struct rsn_ftie_sha512);
831 	else if (wpa_key_mgmt_sha384(key_mgmt))
832 		hdrlen = sizeof(struct rsn_ftie_sha384);
833 	else
834 		hdrlen = sizeof(struct rsn_ftie);
835 
836 	if (len < 2 + hdrlen + 2 + FT_R1KH_ID_LEN + 2 + r0kh_id_len +
837 	    subelem_len)
838 		return -1;
839 
840 	*pos++ = WLAN_EID_FAST_BSS_TRANSITION;
841 	ielen = pos++;
842 
843 	if (key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
844 	    key_len == SHA512_MAC_LEN) {
845 		struct rsn_ftie_sha512 *hdr = (struct rsn_ftie_sha512 *) pos;
846 
847 		os_memset(hdr, 0, sizeof(*hdr));
848 		pos += sizeof(*hdr);
849 		mic_control |= FTE_MIC_LEN_32 << FTE_MIC_CTRL_MIC_LEN_SHIFT;
850 		WPA_PUT_LE16(hdr->mic_control, mic_control);
851 		if (anonce)
852 			os_memcpy(hdr->anonce, anonce, WPA_NONCE_LEN);
853 		if (snonce)
854 			os_memcpy(hdr->snonce, snonce, WPA_NONCE_LEN);
855 	} else if ((key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
856 		    key_len == SHA384_MAC_LEN) ||
857 		   wpa_key_mgmt_sha384(key_mgmt)) {
858 		struct rsn_ftie_sha384 *hdr = (struct rsn_ftie_sha384 *) pos;
859 
860 		os_memset(hdr, 0, sizeof(*hdr));
861 		pos += sizeof(*hdr);
862 		mic_control |= FTE_MIC_LEN_24 << FTE_MIC_CTRL_MIC_LEN_SHIFT;
863 		WPA_PUT_LE16(hdr->mic_control, mic_control);
864 		if (anonce)
865 			os_memcpy(hdr->anonce, anonce, WPA_NONCE_LEN);
866 		if (snonce)
867 			os_memcpy(hdr->snonce, snonce, WPA_NONCE_LEN);
868 	} else {
869 		struct rsn_ftie *hdr = (struct rsn_ftie *) pos;
870 
871 		os_memset(hdr, 0, sizeof(*hdr));
872 		pos += sizeof(*hdr);
873 		mic_control |= FTE_MIC_LEN_16 << FTE_MIC_CTRL_MIC_LEN_SHIFT;
874 		WPA_PUT_LE16(hdr->mic_control, mic_control);
875 		if (anonce)
876 			os_memcpy(hdr->anonce, anonce, WPA_NONCE_LEN);
877 		if (snonce)
878 			os_memcpy(hdr->snonce, snonce, WPA_NONCE_LEN);
879 	}
880 
881 	/* Optional Parameters */
882 	*pos++ = FTIE_SUBELEM_R1KH_ID;
883 	*pos++ = FT_R1KH_ID_LEN;
884 	os_memcpy(pos, conf->r1_key_holder, FT_R1KH_ID_LEN);
885 	pos += FT_R1KH_ID_LEN;
886 
887 	if (r0kh_id) {
888 		*pos++ = FTIE_SUBELEM_R0KH_ID;
889 		*pos++ = r0kh_id_len;
890 		os_memcpy(pos, r0kh_id, r0kh_id_len);
891 		pos += r0kh_id_len;
892 	}
893 
894 	if (subelem) {
895 		os_memcpy(pos, subelem, subelem_len);
896 		pos += subelem_len;
897 	}
898 
899 	*ielen = pos - buf - 2;
900 
901 	return pos - buf;
902 }
903 
904 
905 /* A packet to be handled after seq response */
906 struct ft_remote_item {
907 	struct dl_list list;
908 
909 	u8 nonce[FT_RRB_NONCE_LEN];
910 	struct os_reltime nonce_ts;
911 
912 	u8 src_addr[ETH_ALEN];
913 	u8 *enc;
914 	size_t enc_len;
915 	u8 *auth;
916 	size_t auth_len;
917 	int (*cb)(struct wpa_authenticator *wpa_auth,
918 		  const u8 *src_addr,
919 		  const u8 *enc, size_t enc_len,
920 		  const u8 *auth, size_t auth_len,
921 		  int no_defer);
922 };
923 
924 
wpa_ft_rrb_seq_free(struct ft_remote_item * item)925 static void wpa_ft_rrb_seq_free(struct ft_remote_item *item)
926 {
927 	eloop_cancel_timeout(wpa_ft_rrb_seq_timeout, ELOOP_ALL_CTX, item);
928 	dl_list_del(&item->list);
929 	bin_clear_free(item->enc, item->enc_len);
930 	os_free(item->auth);
931 	os_free(item);
932 }
933 
934 
wpa_ft_rrb_seq_flush(struct wpa_authenticator * wpa_auth,struct ft_remote_seq * rkh_seq,int cb)935 static void wpa_ft_rrb_seq_flush(struct wpa_authenticator *wpa_auth,
936 				 struct ft_remote_seq *rkh_seq, int cb)
937 {
938 	struct ft_remote_item *item, *n;
939 
940 	dl_list_for_each_safe(item, n, &rkh_seq->rx.queue,
941 			      struct ft_remote_item, list) {
942 		if (cb && item->cb)
943 			item->cb(wpa_auth, item->src_addr, item->enc,
944 				 item->enc_len, item->auth, item->auth_len, 1);
945 		wpa_ft_rrb_seq_free(item);
946 	}
947 }
948 
949 
wpa_ft_rrb_seq_timeout(void * eloop_ctx,void * timeout_ctx)950 static void wpa_ft_rrb_seq_timeout(void *eloop_ctx, void *timeout_ctx)
951 {
952 	struct ft_remote_item *item = timeout_ctx;
953 
954 	wpa_ft_rrb_seq_free(item);
955 }
956 
957 
958 static int
wpa_ft_rrb_seq_req(struct wpa_authenticator * wpa_auth,struct ft_remote_seq * rkh_seq,const u8 * src_addr,const u8 * f_r0kh_id,size_t f_r0kh_id_len,const u8 * f_r1kh_id,const u8 * key,size_t key_len,const u8 * enc,size_t enc_len,const u8 * auth,size_t auth_len,int (* cb)(struct wpa_authenticator * wpa_auth,const u8 * src_addr,const u8 * enc,size_t enc_len,const u8 * auth,size_t auth_len,int no_defer))959 wpa_ft_rrb_seq_req(struct wpa_authenticator *wpa_auth,
960 		   struct ft_remote_seq *rkh_seq, const u8 *src_addr,
961 		   const u8 *f_r0kh_id, size_t f_r0kh_id_len,
962 		   const u8 *f_r1kh_id, const u8 *key, size_t key_len,
963 		   const u8 *enc, size_t enc_len,
964 		   const u8 *auth, size_t auth_len,
965 		   int (*cb)(struct wpa_authenticator *wpa_auth,
966 			     const u8 *src_addr,
967 			     const u8 *enc, size_t enc_len,
968 			     const u8 *auth, size_t auth_len,
969 			     int no_defer))
970 {
971 	struct ft_remote_item *item = NULL;
972 	u8 *packet = NULL;
973 	size_t packet_len;
974 	struct tlv_list seq_req_auth[] = {
975 		{ .type = FT_RRB_NONCE, .len = FT_RRB_NONCE_LEN,
976 		  .data = NULL /* to be filled: item->nonce */ },
977 		{ .type = FT_RRB_R0KH_ID, .len = f_r0kh_id_len,
978 		  .data = f_r0kh_id },
979 		{ .type = FT_RRB_R1KH_ID, .len = FT_R1KH_ID_LEN,
980 		  .data = f_r1kh_id },
981 		{ .type = FT_RRB_LAST_EMPTY, .len = 0, .data = NULL },
982 	};
983 
984 	if (dl_list_len(&rkh_seq->rx.queue) >= ftRRBmaxQueueLen) {
985 		wpa_printf(MSG_DEBUG, "FT: Sequence number queue too long");
986 		goto err;
987 	}
988 
989 	wpa_printf(MSG_DEBUG, "FT: Send sequence number request from " MACSTR
990 		   " to " MACSTR,
991 		   MAC2STR(wpa_auth->addr), MAC2STR(src_addr));
992 	item = os_zalloc(sizeof(*item));
993 	if (!item)
994 		goto err;
995 
996 	os_memcpy(item->src_addr, src_addr, ETH_ALEN);
997 	item->cb = cb;
998 
999 	if (random_get_bytes(item->nonce, FT_RRB_NONCE_LEN) < 0) {
1000 		wpa_printf(MSG_DEBUG, "FT: Seq num nonce: out of random bytes");
1001 		goto err;
1002 	}
1003 
1004 	if (os_get_reltime(&item->nonce_ts) < 0)
1005 		goto err;
1006 
1007 	if (enc && enc_len > 0) {
1008 		item->enc = os_memdup(enc, enc_len);
1009 		item->enc_len = enc_len;
1010 		if (!item->enc)
1011 			goto err;
1012 	}
1013 
1014 	if (auth && auth_len > 0) {
1015 		item->auth = os_memdup(auth, auth_len);
1016 		item->auth_len = auth_len;
1017 		if (!item->auth)
1018 			goto err;
1019 	}
1020 
1021 	eloop_register_timeout(ftRRBseqTimeout, 0, wpa_ft_rrb_seq_timeout,
1022 			       wpa_auth, item);
1023 
1024 	seq_req_auth[0].data = item->nonce;
1025 
1026 	if (wpa_ft_rrb_build(key, key_len, NULL, NULL, seq_req_auth, NULL,
1027 			     wpa_auth->addr, FT_PACKET_R0KH_R1KH_SEQ_REQ,
1028 			     &packet, &packet_len) < 0) {
1029 		item = NULL; /* some other seq resp might still accept this */
1030 		goto err;
1031 	}
1032 
1033 	dl_list_add(&rkh_seq->rx.queue, &item->list);
1034 
1035 	wpa_ft_rrb_oui_send(wpa_auth, src_addr, FT_PACKET_R0KH_R1KH_SEQ_REQ,
1036 			    packet, packet_len);
1037 
1038 	os_free(packet);
1039 
1040 	return 0;
1041 err:
1042 	wpa_printf(MSG_DEBUG, "FT: Failed to send sequence number request");
1043 	if (item) {
1044 		os_free(item->auth);
1045 		bin_clear_free(item->enc, item->enc_len);
1046 		os_free(item);
1047 	}
1048 
1049 	return -1;
1050 }
1051 
1052 
1053 #define FT_RRB_SEQ_OK    0
1054 #define FT_RRB_SEQ_DROP  1
1055 #define FT_RRB_SEQ_DEFER 2
1056 
1057 static int
wpa_ft_rrb_seq_chk(struct ft_remote_seq * rkh_seq,const u8 * src_addr,const u8 * enc,size_t enc_len,const u8 * auth,size_t auth_len,const char * msgtype,int no_defer)1058 wpa_ft_rrb_seq_chk(struct ft_remote_seq *rkh_seq, const u8 *src_addr,
1059 		   const u8 *enc, size_t enc_len,
1060 		   const u8 *auth, size_t auth_len,
1061 		   const char *msgtype, int no_defer)
1062 {
1063 	const u8 *f_seq;
1064 	size_t f_seq_len;
1065 	const struct ft_rrb_seq *msg_both;
1066 	u32 msg_seq, msg_off, rkh_off;
1067 	struct os_reltime now;
1068 	unsigned int i;
1069 
1070 	RRB_GET_AUTH(FT_RRB_SEQ, seq, msgtype, sizeof(*msg_both));
1071 	wpa_hexdump(MSG_DEBUG, "FT: sequence number", f_seq, f_seq_len);
1072 	msg_both = (const struct ft_rrb_seq *) f_seq;
1073 
1074 	if (rkh_seq->rx.num_last == 0) {
1075 		/* first packet from remote */
1076 		goto defer;
1077 	}
1078 
1079 	if (le_to_host32(msg_both->dom) != rkh_seq->rx.dom) {
1080 		/* remote might have rebooted */
1081 		goto defer;
1082 	}
1083 
1084 	if (os_get_reltime(&now) == 0) {
1085 		u32 msg_ts_now_remote, msg_ts_off;
1086 		struct os_reltime now_remote;
1087 
1088 		os_reltime_sub(&now, &rkh_seq->rx.time_offset, &now_remote);
1089 		msg_ts_now_remote = now_remote.sec;
1090 		msg_ts_off = le_to_host32(msg_both->ts) -
1091 			(msg_ts_now_remote - ftRRBseqTimeout);
1092 		if (msg_ts_off > 2 * ftRRBseqTimeout)
1093 			goto defer;
1094 	}
1095 
1096 	msg_seq = le_to_host32(msg_both->seq);
1097 	rkh_off = rkh_seq->rx.last[rkh_seq->rx.offsetidx];
1098 	msg_off = msg_seq - rkh_off;
1099 	if (msg_off > 0xC0000000)
1100 		goto out; /* too old message, drop it */
1101 
1102 	if (msg_off <= 0x40000000) {
1103 		for (i = 0; i < rkh_seq->rx.num_last; i++) {
1104 			if (rkh_seq->rx.last[i] == msg_seq)
1105 				goto out; /* duplicate message, drop it */
1106 		}
1107 
1108 		return FT_RRB_SEQ_OK;
1109 	}
1110 
1111 defer:
1112 	if (no_defer)
1113 		goto out;
1114 
1115 	wpa_printf(MSG_DEBUG, "FT: Possibly invalid sequence number in %s from "
1116 		   MACSTR, msgtype, MAC2STR(src_addr));
1117 
1118 	return FT_RRB_SEQ_DEFER;
1119 out:
1120 	wpa_printf(MSG_DEBUG, "FT: Invalid sequence number in %s from " MACSTR,
1121 		   msgtype, MAC2STR(src_addr));
1122 
1123 	return FT_RRB_SEQ_DROP;
1124 }
1125 
1126 
1127 static void
wpa_ft_rrb_seq_accept(struct wpa_authenticator * wpa_auth,struct ft_remote_seq * rkh_seq,const u8 * src_addr,const u8 * auth,size_t auth_len,const char * msgtype)1128 wpa_ft_rrb_seq_accept(struct wpa_authenticator *wpa_auth,
1129 		      struct ft_remote_seq *rkh_seq, const u8 *src_addr,
1130 		      const u8 *auth, size_t auth_len,
1131 		      const char *msgtype)
1132 {
1133 	const u8 *f_seq;
1134 	size_t f_seq_len;
1135 	const struct ft_rrb_seq *msg_both;
1136 	u32 msg_seq, msg_off, min_off, rkh_off;
1137 	int minidx = 0;
1138 	unsigned int i;
1139 
1140 	RRB_GET_AUTH(FT_RRB_SEQ, seq, msgtype, sizeof(*msg_both));
1141 	msg_both = (const struct ft_rrb_seq *) f_seq;
1142 
1143 	msg_seq = le_to_host32(msg_both->seq);
1144 
1145 	if (rkh_seq->rx.num_last < FT_REMOTE_SEQ_BACKLOG) {
1146 		rkh_seq->rx.last[rkh_seq->rx.num_last] = msg_seq;
1147 		rkh_seq->rx.num_last++;
1148 		return;
1149 	}
1150 
1151 	rkh_off = rkh_seq->rx.last[rkh_seq->rx.offsetidx];
1152 	for (i = 0; i < rkh_seq->rx.num_last; i++) {
1153 		msg_off = rkh_seq->rx.last[i] - rkh_off;
1154 		min_off = rkh_seq->rx.last[minidx] - rkh_off;
1155 		if (msg_off < min_off && i != rkh_seq->rx.offsetidx)
1156 			minidx = i;
1157 	}
1158 	rkh_seq->rx.last[rkh_seq->rx.offsetidx] = msg_seq;
1159 	rkh_seq->rx.offsetidx = minidx;
1160 
1161 	return;
1162 out:
1163 	/* RRB_GET_AUTH should never fail here as
1164 	 * wpa_ft_rrb_seq_chk() verified FT_RRB_SEQ presence. */
1165 	wpa_printf(MSG_ERROR, "FT: %s() failed", __func__);
1166 }
1167 
1168 
wpa_ft_new_seq(struct ft_remote_seq * rkh_seq,struct ft_rrb_seq * f_seq)1169 static int wpa_ft_new_seq(struct ft_remote_seq *rkh_seq,
1170 			  struct ft_rrb_seq *f_seq)
1171 {
1172 	struct os_reltime now;
1173 
1174 	if (os_get_reltime(&now) < 0)
1175 		return -1;
1176 
1177 	if (!rkh_seq->tx.dom) {
1178 		if (random_get_bytes((u8 *) &rkh_seq->tx.seq,
1179 				     sizeof(rkh_seq->tx.seq))) {
1180 			wpa_printf(MSG_ERROR,
1181 				   "FT: Failed to get random data for sequence number initialization");
1182 			rkh_seq->tx.seq = now.usec;
1183 		}
1184 		if (random_get_bytes((u8 *) &rkh_seq->tx.dom,
1185 				     sizeof(rkh_seq->tx.dom))) {
1186 			wpa_printf(MSG_ERROR,
1187 				   "FT: Failed to get random data for sequence number initialization");
1188 			rkh_seq->tx.dom = now.usec;
1189 		}
1190 		rkh_seq->tx.dom |= 1;
1191 	}
1192 
1193 	f_seq->dom = host_to_le32(rkh_seq->tx.dom);
1194 	f_seq->seq = host_to_le32(rkh_seq->tx.seq);
1195 	f_seq->ts = host_to_le32(now.sec);
1196 
1197 	rkh_seq->tx.seq++;
1198 
1199 	return 0;
1200 }
1201 
1202 
1203 struct wpa_ft_pmk_r0_sa {
1204 	struct dl_list list;
1205 	u8 pmk_r0[PMK_LEN_MAX];
1206 	size_t pmk_r0_len;
1207 	u8 pmk_r0_name[WPA_PMK_NAME_LEN];
1208 	u8 spa[ETH_ALEN];
1209 	int pairwise; /* Pairwise cipher suite, WPA_CIPHER_* */
1210 	struct vlan_description *vlan;
1211 	os_time_t expiration; /* 0 for no expiration */
1212 	u8 *identity;
1213 	size_t identity_len;
1214 	u8 *radius_cui;
1215 	size_t radius_cui_len;
1216 	os_time_t session_timeout; /* 0 for no expiration */
1217 	/* TODO: radius_class, EAP type */
1218 	int pmk_r1_pushed;
1219 };
1220 
1221 struct wpa_ft_pmk_r1_sa {
1222 	struct dl_list list;
1223 	u8 pmk_r1[PMK_LEN_MAX];
1224 	size_t pmk_r1_len;
1225 	u8 pmk_r1_name[WPA_PMK_NAME_LEN];
1226 	u8 spa[ETH_ALEN];
1227 	int pairwise; /* Pairwise cipher suite, WPA_CIPHER_* */
1228 	struct vlan_description *vlan;
1229 	u8 *identity;
1230 	size_t identity_len;
1231 	u8 *radius_cui;
1232 	size_t radius_cui_len;
1233 	os_time_t session_timeout; /* 0 for no expiration */
1234 	/* TODO: radius_class, EAP type */
1235 };
1236 
1237 struct wpa_ft_pmk_cache {
1238 	struct dl_list pmk_r0; /* struct wpa_ft_pmk_r0_sa */
1239 	struct dl_list pmk_r1; /* struct wpa_ft_pmk_r1_sa */
1240 };
1241 
1242 
1243 static void wpa_ft_expire_pmk_r0(void *eloop_ctx, void *timeout_ctx);
1244 static void wpa_ft_expire_pmk_r1(void *eloop_ctx, void *timeout_ctx);
1245 
1246 
wpa_ft_free_pmk_r0(struct wpa_ft_pmk_r0_sa * r0)1247 static void wpa_ft_free_pmk_r0(struct wpa_ft_pmk_r0_sa *r0)
1248 {
1249 	if (!r0)
1250 		return;
1251 
1252 	dl_list_del(&r0->list);
1253 	eloop_cancel_timeout(wpa_ft_expire_pmk_r0, r0, NULL);
1254 
1255 	os_memset(r0->pmk_r0, 0, PMK_LEN_MAX);
1256 	os_free(r0->vlan);
1257 	os_free(r0->identity);
1258 	os_free(r0->radius_cui);
1259 	os_free(r0);
1260 }
1261 
1262 
wpa_ft_expire_pmk_r0(void * eloop_ctx,void * timeout_ctx)1263 static void wpa_ft_expire_pmk_r0(void *eloop_ctx, void *timeout_ctx)
1264 {
1265 	struct wpa_ft_pmk_r0_sa *r0 = eloop_ctx;
1266 	struct os_reltime now;
1267 	int expires_in;
1268 	int session_timeout;
1269 
1270 	os_get_reltime(&now);
1271 
1272 	if (!r0)
1273 		return;
1274 
1275 	expires_in = r0->expiration - now.sec;
1276 	session_timeout = r0->session_timeout - now.sec;
1277 	/* conditions to remove from cache:
1278 	 * a) r0->expiration is set and hit
1279 	 * -or-
1280 	 * b) r0->session_timeout is set and hit
1281 	 */
1282 	if ((!r0->expiration || expires_in > 0) &&
1283 	    (!r0->session_timeout || session_timeout > 0)) {
1284 		wpa_printf(MSG_ERROR,
1285 			   "FT: %s() called for non-expired entry %p",
1286 			   __func__, r0);
1287 		eloop_cancel_timeout(wpa_ft_expire_pmk_r0, r0, NULL);
1288 		if (r0->expiration && expires_in > 0)
1289 			eloop_register_timeout(expires_in + 1, 0,
1290 					       wpa_ft_expire_pmk_r0, r0, NULL);
1291 		if (r0->session_timeout && session_timeout > 0)
1292 			eloop_register_timeout(session_timeout + 1, 0,
1293 					       wpa_ft_expire_pmk_r0, r0, NULL);
1294 		return;
1295 	}
1296 
1297 	wpa_ft_free_pmk_r0(r0);
1298 }
1299 
1300 
wpa_ft_free_pmk_r1(struct wpa_ft_pmk_r1_sa * r1)1301 static void wpa_ft_free_pmk_r1(struct wpa_ft_pmk_r1_sa *r1)
1302 {
1303 	if (!r1)
1304 		return;
1305 
1306 	dl_list_del(&r1->list);
1307 	eloop_cancel_timeout(wpa_ft_expire_pmk_r1, r1, NULL);
1308 
1309 	os_memset(r1->pmk_r1, 0, PMK_LEN_MAX);
1310 	os_free(r1->vlan);
1311 	os_free(r1->identity);
1312 	os_free(r1->radius_cui);
1313 	os_free(r1);
1314 }
1315 
1316 
wpa_ft_expire_pmk_r1(void * eloop_ctx,void * timeout_ctx)1317 static void wpa_ft_expire_pmk_r1(void *eloop_ctx, void *timeout_ctx)
1318 {
1319 	struct wpa_ft_pmk_r1_sa *r1 = eloop_ctx;
1320 
1321 	wpa_ft_free_pmk_r1(r1);
1322 }
1323 
1324 
wpa_ft_pmk_cache_init(void)1325 struct wpa_ft_pmk_cache * wpa_ft_pmk_cache_init(void)
1326 {
1327 	struct wpa_ft_pmk_cache *cache;
1328 
1329 	cache = os_zalloc(sizeof(*cache));
1330 	if (cache) {
1331 		dl_list_init(&cache->pmk_r0);
1332 		dl_list_init(&cache->pmk_r1);
1333 	}
1334 
1335 	return cache;
1336 }
1337 
1338 
wpa_ft_pmk_cache_deinit(struct wpa_ft_pmk_cache * cache)1339 void wpa_ft_pmk_cache_deinit(struct wpa_ft_pmk_cache *cache)
1340 {
1341 	struct wpa_ft_pmk_r0_sa *r0, *r0prev;
1342 	struct wpa_ft_pmk_r1_sa *r1, *r1prev;
1343 
1344 	dl_list_for_each_safe(r0, r0prev, &cache->pmk_r0,
1345 			      struct wpa_ft_pmk_r0_sa, list)
1346 		wpa_ft_free_pmk_r0(r0);
1347 
1348 	dl_list_for_each_safe(r1, r1prev, &cache->pmk_r1,
1349 			      struct wpa_ft_pmk_r1_sa, list)
1350 		wpa_ft_free_pmk_r1(r1);
1351 
1352 	os_free(cache);
1353 }
1354 
1355 
wpa_ft_store_pmk_r0(struct wpa_authenticator * wpa_auth,const u8 * spa,const u8 * pmk_r0,size_t pmk_r0_len,const u8 * pmk_r0_name,int pairwise,const struct vlan_description * vlan,int expires_in,int session_timeout,const u8 * identity,size_t identity_len,const u8 * radius_cui,size_t radius_cui_len)1356 static int wpa_ft_store_pmk_r0(struct wpa_authenticator *wpa_auth,
1357 			       const u8 *spa, const u8 *pmk_r0,
1358 			       size_t pmk_r0_len,
1359 			       const u8 *pmk_r0_name, int pairwise,
1360 			       const struct vlan_description *vlan,
1361 			       int expires_in, int session_timeout,
1362 			       const u8 *identity, size_t identity_len,
1363 			       const u8 *radius_cui, size_t radius_cui_len)
1364 {
1365 	struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
1366 	struct wpa_ft_pmk_r0_sa *r0;
1367 	struct os_reltime now;
1368 
1369 	/* TODO: add limit on number of entries in cache */
1370 	os_get_reltime(&now);
1371 
1372 	r0 = os_zalloc(sizeof(*r0));
1373 	if (r0 == NULL)
1374 		return -1;
1375 
1376 	os_memcpy(r0->pmk_r0, pmk_r0, pmk_r0_len);
1377 	r0->pmk_r0_len = pmk_r0_len;
1378 	os_memcpy(r0->pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN);
1379 	os_memcpy(r0->spa, spa, ETH_ALEN);
1380 	r0->pairwise = pairwise;
1381 	if (expires_in > 0)
1382 		r0->expiration = now.sec + expires_in;
1383 	if (vlan && vlan->notempty) {
1384 		r0->vlan = os_zalloc(sizeof(*vlan));
1385 		if (!r0->vlan) {
1386 			bin_clear_free(r0, sizeof(*r0));
1387 			return -1;
1388 		}
1389 		*r0->vlan = *vlan;
1390 	}
1391 	if (identity) {
1392 		r0->identity = os_malloc(identity_len);
1393 		if (r0->identity) {
1394 			os_memcpy(r0->identity, identity, identity_len);
1395 			r0->identity_len = identity_len;
1396 		}
1397 	}
1398 	if (radius_cui) {
1399 		r0->radius_cui = os_malloc(radius_cui_len);
1400 		if (r0->radius_cui) {
1401 			os_memcpy(r0->radius_cui, radius_cui, radius_cui_len);
1402 			r0->radius_cui_len = radius_cui_len;
1403 		}
1404 	}
1405 	if (session_timeout > 0)
1406 		r0->session_timeout = now.sec + session_timeout;
1407 
1408 	dl_list_add(&cache->pmk_r0, &r0->list);
1409 	if (expires_in > 0)
1410 		eloop_register_timeout(expires_in + 1, 0, wpa_ft_expire_pmk_r0,
1411 				       r0, NULL);
1412 	if (session_timeout > 0)
1413 		eloop_register_timeout(session_timeout + 1, 0,
1414 				       wpa_ft_expire_pmk_r0, r0, NULL);
1415 
1416 	return 0;
1417 }
1418 
1419 
wpa_ft_fetch_pmk_r0(struct wpa_authenticator * wpa_auth,const u8 * spa,const u8 * pmk_r0_name,const struct wpa_ft_pmk_r0_sa ** r0_out)1420 static int wpa_ft_fetch_pmk_r0(struct wpa_authenticator *wpa_auth,
1421 			       const u8 *spa, const u8 *pmk_r0_name,
1422 			       const struct wpa_ft_pmk_r0_sa **r0_out)
1423 {
1424 	struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
1425 	struct wpa_ft_pmk_r0_sa *r0;
1426 	struct os_reltime now;
1427 
1428 	os_get_reltime(&now);
1429 	dl_list_for_each(r0, &cache->pmk_r0, struct wpa_ft_pmk_r0_sa, list) {
1430 		if (os_memcmp(r0->spa, spa, ETH_ALEN) == 0 &&
1431 		    os_memcmp_const(r0->pmk_r0_name, pmk_r0_name,
1432 				    WPA_PMK_NAME_LEN) == 0) {
1433 			*r0_out = r0;
1434 			return 0;
1435 		}
1436 	}
1437 
1438 	*r0_out = NULL;
1439 	return -1;
1440 }
1441 
1442 
wpa_ft_store_pmk_r1(struct wpa_authenticator * wpa_auth,const u8 * spa,const u8 * pmk_r1,size_t pmk_r1_len,const u8 * pmk_r1_name,int pairwise,const struct vlan_description * vlan,int expires_in,int session_timeout,const u8 * identity,size_t identity_len,const u8 * radius_cui,size_t radius_cui_len)1443 static int wpa_ft_store_pmk_r1(struct wpa_authenticator *wpa_auth,
1444 			       const u8 *spa, const u8 *pmk_r1,
1445 			       size_t pmk_r1_len,
1446 			       const u8 *pmk_r1_name, int pairwise,
1447 			       const struct vlan_description *vlan,
1448 			       int expires_in, int session_timeout,
1449 			       const u8 *identity, size_t identity_len,
1450 			       const u8 *radius_cui, size_t radius_cui_len)
1451 {
1452 	struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
1453 	int max_expires_in = wpa_auth->conf.r1_max_key_lifetime;
1454 	struct wpa_ft_pmk_r1_sa *r1;
1455 	struct os_reltime now;
1456 
1457 	/* TODO: limit on number of entries in cache */
1458 	os_get_reltime(&now);
1459 
1460 	if (max_expires_in && (max_expires_in < expires_in || expires_in == 0))
1461 		expires_in = max_expires_in;
1462 
1463 	r1 = os_zalloc(sizeof(*r1));
1464 	if (r1 == NULL)
1465 		return -1;
1466 
1467 	os_memcpy(r1->pmk_r1, pmk_r1, pmk_r1_len);
1468 	r1->pmk_r1_len = pmk_r1_len;
1469 	os_memcpy(r1->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN);
1470 	os_memcpy(r1->spa, spa, ETH_ALEN);
1471 	r1->pairwise = pairwise;
1472 	if (vlan && vlan->notempty) {
1473 		r1->vlan = os_zalloc(sizeof(*vlan));
1474 		if (!r1->vlan) {
1475 			bin_clear_free(r1, sizeof(*r1));
1476 			return -1;
1477 		}
1478 		*r1->vlan = *vlan;
1479 	}
1480 	if (identity) {
1481 		r1->identity = os_malloc(identity_len);
1482 		if (r1->identity) {
1483 			os_memcpy(r1->identity, identity, identity_len);
1484 			r1->identity_len = identity_len;
1485 		}
1486 	}
1487 	if (radius_cui) {
1488 		r1->radius_cui = os_malloc(radius_cui_len);
1489 		if (r1->radius_cui) {
1490 			os_memcpy(r1->radius_cui, radius_cui, radius_cui_len);
1491 			r1->radius_cui_len = radius_cui_len;
1492 		}
1493 	}
1494 	if (session_timeout > 0)
1495 		r1->session_timeout = now.sec + session_timeout;
1496 
1497 	dl_list_add(&cache->pmk_r1, &r1->list);
1498 
1499 	if (expires_in > 0)
1500 		eloop_register_timeout(expires_in + 1, 0, wpa_ft_expire_pmk_r1,
1501 				       r1, NULL);
1502 	if (session_timeout > 0)
1503 		eloop_register_timeout(session_timeout + 1, 0,
1504 				       wpa_ft_expire_pmk_r1, r1, NULL);
1505 
1506 	return 0;
1507 }
1508 
1509 
wpa_ft_fetch_pmk_r1(struct wpa_authenticator * wpa_auth,const u8 * spa,const u8 * pmk_r1_name,u8 * pmk_r1,size_t * pmk_r1_len,int * pairwise,struct vlan_description * vlan,const u8 ** identity,size_t * identity_len,const u8 ** radius_cui,size_t * radius_cui_len,int * session_timeout)1510 int wpa_ft_fetch_pmk_r1(struct wpa_authenticator *wpa_auth,
1511 			const u8 *spa, const u8 *pmk_r1_name,
1512 			u8 *pmk_r1, size_t *pmk_r1_len, int *pairwise,
1513 			struct vlan_description *vlan,
1514 			const u8 **identity, size_t *identity_len,
1515 			const u8 **radius_cui, size_t *radius_cui_len,
1516 			int *session_timeout)
1517 {
1518 	struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
1519 	struct wpa_ft_pmk_r1_sa *r1;
1520 	struct os_reltime now;
1521 
1522 	os_get_reltime(&now);
1523 
1524 	dl_list_for_each(r1, &cache->pmk_r1, struct wpa_ft_pmk_r1_sa, list) {
1525 		if (os_memcmp(r1->spa, spa, ETH_ALEN) == 0 &&
1526 		    os_memcmp_const(r1->pmk_r1_name, pmk_r1_name,
1527 				    WPA_PMK_NAME_LEN) == 0) {
1528 			os_memcpy(pmk_r1, r1->pmk_r1, r1->pmk_r1_len);
1529 			*pmk_r1_len = r1->pmk_r1_len;
1530 			if (pairwise)
1531 				*pairwise = r1->pairwise;
1532 			if (vlan && r1->vlan)
1533 				*vlan = *r1->vlan;
1534 			if (vlan && !r1->vlan)
1535 				os_memset(vlan, 0, sizeof(*vlan));
1536 			if (identity && identity_len) {
1537 				*identity = r1->identity;
1538 				*identity_len = r1->identity_len;
1539 			}
1540 			if (radius_cui && radius_cui_len) {
1541 				*radius_cui = r1->radius_cui;
1542 				*radius_cui_len = r1->radius_cui_len;
1543 			}
1544 			if (session_timeout && r1->session_timeout > now.sec)
1545 				*session_timeout = r1->session_timeout -
1546 					now.sec;
1547 			else if (session_timeout && r1->session_timeout)
1548 				*session_timeout = 1;
1549 			else if (session_timeout)
1550 				*session_timeout = 0;
1551 			return 0;
1552 		}
1553 	}
1554 
1555 	return -1;
1556 }
1557 
1558 
wpa_ft_rrb_init_r0kh_seq(struct ft_remote_r0kh * r0kh)1559 static int wpa_ft_rrb_init_r0kh_seq(struct ft_remote_r0kh *r0kh)
1560 {
1561 	if (r0kh->seq)
1562 		return 0;
1563 
1564 	r0kh->seq = os_zalloc(sizeof(*r0kh->seq));
1565 	if (!r0kh->seq) {
1566 		wpa_printf(MSG_DEBUG, "FT: Failed to allocate r0kh->seq");
1567 		return -1;
1568 	}
1569 
1570 	dl_list_init(&r0kh->seq->rx.queue);
1571 
1572 	return 0;
1573 }
1574 
1575 
wpa_ft_rrb_lookup_r0kh(struct wpa_authenticator * wpa_auth,const u8 * f_r0kh_id,size_t f_r0kh_id_len,struct ft_remote_r0kh ** r0kh_out,struct ft_remote_r0kh ** r0kh_wildcard)1576 static void wpa_ft_rrb_lookup_r0kh(struct wpa_authenticator *wpa_auth,
1577 				   const u8 *f_r0kh_id, size_t f_r0kh_id_len,
1578 				   struct ft_remote_r0kh **r0kh_out,
1579 				   struct ft_remote_r0kh **r0kh_wildcard)
1580 {
1581 	struct ft_remote_r0kh *r0kh;
1582 
1583 	*r0kh_wildcard = NULL;
1584 	*r0kh_out = NULL;
1585 
1586 	if (wpa_auth->conf.r0kh_list)
1587 		r0kh = *wpa_auth->conf.r0kh_list;
1588 	else
1589 		r0kh = NULL;
1590 	for (; r0kh; r0kh = r0kh->next) {
1591 		if (r0kh->id_len == 1 && r0kh->id[0] == '*')
1592 			*r0kh_wildcard = r0kh;
1593 		if (f_r0kh_id && r0kh->id_len == f_r0kh_id_len &&
1594 		    os_memcmp_const(f_r0kh_id, r0kh->id, f_r0kh_id_len) == 0)
1595 			*r0kh_out = r0kh;
1596 	}
1597 
1598 	if (!*r0kh_out && !*r0kh_wildcard)
1599 		wpa_printf(MSG_DEBUG, "FT: No matching R0KH found");
1600 
1601 	if (*r0kh_out && wpa_ft_rrb_init_r0kh_seq(*r0kh_out) < 0)
1602 		*r0kh_out = NULL;
1603 }
1604 
1605 
wpa_ft_rrb_init_r1kh_seq(struct ft_remote_r1kh * r1kh)1606 static int wpa_ft_rrb_init_r1kh_seq(struct ft_remote_r1kh *r1kh)
1607 {
1608 	if (r1kh->seq)
1609 		return 0;
1610 
1611 	r1kh->seq = os_zalloc(sizeof(*r1kh->seq));
1612 	if (!r1kh->seq) {
1613 		wpa_printf(MSG_DEBUG, "FT: Failed to allocate r1kh->seq");
1614 		return -1;
1615 	}
1616 
1617 	dl_list_init(&r1kh->seq->rx.queue);
1618 
1619 	return 0;
1620 }
1621 
1622 
wpa_ft_rrb_lookup_r1kh(struct wpa_authenticator * wpa_auth,const u8 * f_r1kh_id,struct ft_remote_r1kh ** r1kh_out,struct ft_remote_r1kh ** r1kh_wildcard)1623 static void wpa_ft_rrb_lookup_r1kh(struct wpa_authenticator *wpa_auth,
1624 				   const u8 *f_r1kh_id,
1625 				   struct ft_remote_r1kh **r1kh_out,
1626 				   struct ft_remote_r1kh **r1kh_wildcard)
1627 {
1628 	struct ft_remote_r1kh *r1kh;
1629 
1630 	*r1kh_wildcard = NULL;
1631 	*r1kh_out = NULL;
1632 
1633 	if (wpa_auth->conf.r1kh_list)
1634 		r1kh = *wpa_auth->conf.r1kh_list;
1635 	else
1636 		r1kh = NULL;
1637 	for (; r1kh; r1kh = r1kh->next) {
1638 		if (is_zero_ether_addr(r1kh->addr) &&
1639 		    is_zero_ether_addr(r1kh->id))
1640 			*r1kh_wildcard = r1kh;
1641 		if (f_r1kh_id &&
1642 		    os_memcmp_const(r1kh->id, f_r1kh_id, FT_R1KH_ID_LEN) == 0)
1643 			*r1kh_out = r1kh;
1644 	}
1645 
1646 	if (!*r1kh_out && !*r1kh_wildcard)
1647 		wpa_printf(MSG_DEBUG, "FT: No matching R1KH found");
1648 
1649 	if (*r1kh_out && wpa_ft_rrb_init_r1kh_seq(*r1kh_out) < 0)
1650 		*r1kh_out = NULL;
1651 }
1652 
1653 
wpa_ft_rrb_check_r0kh(struct wpa_authenticator * wpa_auth,const u8 * f_r0kh_id,size_t f_r0kh_id_len)1654 static int wpa_ft_rrb_check_r0kh(struct wpa_authenticator *wpa_auth,
1655 				 const u8 *f_r0kh_id, size_t f_r0kh_id_len)
1656 {
1657 	if (f_r0kh_id_len != wpa_auth->conf.r0_key_holder_len ||
1658 	    os_memcmp_const(f_r0kh_id, wpa_auth->conf.r0_key_holder,
1659 			    f_r0kh_id_len) != 0)
1660 		return -1;
1661 
1662 	return 0;
1663 }
1664 
1665 
wpa_ft_rrb_check_r1kh(struct wpa_authenticator * wpa_auth,const u8 * f_r1kh_id)1666 static int wpa_ft_rrb_check_r1kh(struct wpa_authenticator *wpa_auth,
1667 				 const u8 *f_r1kh_id)
1668 {
1669 	if (os_memcmp_const(f_r1kh_id, wpa_auth->conf.r1_key_holder,
1670 			    FT_R1KH_ID_LEN) != 0)
1671 		return -1;
1672 
1673 	return 0;
1674 }
1675 
1676 
wpa_ft_rrb_del_r0kh(void * eloop_ctx,void * timeout_ctx)1677 static void wpa_ft_rrb_del_r0kh(void *eloop_ctx, void *timeout_ctx)
1678 {
1679 	struct wpa_authenticator *wpa_auth = eloop_ctx;
1680 	struct ft_remote_r0kh *r0kh, *prev = NULL;
1681 
1682 	if (!wpa_auth->conf.r0kh_list)
1683 		return;
1684 
1685 	for (r0kh = *wpa_auth->conf.r0kh_list; r0kh; r0kh = r0kh->next) {
1686 		if (r0kh == timeout_ctx)
1687 			break;
1688 		prev = r0kh;
1689 	}
1690 	if (!r0kh)
1691 		return;
1692 	if (prev)
1693 		prev->next = r0kh->next;
1694 	else
1695 		*wpa_auth->conf.r0kh_list = r0kh->next;
1696 	if (r0kh->seq)
1697 		wpa_ft_rrb_seq_flush(wpa_auth, r0kh->seq, 0);
1698 	os_free(r0kh->seq);
1699 	os_free(r0kh);
1700 }
1701 
1702 
wpa_ft_rrb_r0kh_replenish(struct wpa_authenticator * wpa_auth,struct ft_remote_r0kh * r0kh,int timeout)1703 static void wpa_ft_rrb_r0kh_replenish(struct wpa_authenticator *wpa_auth,
1704 				      struct ft_remote_r0kh *r0kh, int timeout)
1705 {
1706 	if (timeout > 0)
1707 		eloop_replenish_timeout(timeout, 0, wpa_ft_rrb_del_r0kh,
1708 					wpa_auth, r0kh);
1709 }
1710 
1711 
wpa_ft_rrb_r0kh_timeout(struct wpa_authenticator * wpa_auth,struct ft_remote_r0kh * r0kh,int timeout)1712 static void wpa_ft_rrb_r0kh_timeout(struct wpa_authenticator *wpa_auth,
1713 				    struct ft_remote_r0kh *r0kh, int timeout)
1714 {
1715 	eloop_cancel_timeout(wpa_ft_rrb_del_r0kh, wpa_auth, r0kh);
1716 
1717 	if (timeout > 0)
1718 		eloop_register_timeout(timeout, 0, wpa_ft_rrb_del_r0kh,
1719 				       wpa_auth, r0kh);
1720 }
1721 
1722 
1723 static struct ft_remote_r0kh *
wpa_ft_rrb_add_r0kh(struct wpa_authenticator * wpa_auth,struct ft_remote_r0kh * r0kh_wildcard,const u8 * src_addr,const u8 * r0kh_id,size_t id_len,int timeout)1724 wpa_ft_rrb_add_r0kh(struct wpa_authenticator *wpa_auth,
1725 		    struct ft_remote_r0kh *r0kh_wildcard,
1726 		    const u8 *src_addr, const u8 *r0kh_id, size_t id_len,
1727 		    int timeout)
1728 {
1729 	struct ft_remote_r0kh *r0kh;
1730 
1731 	if (!wpa_auth->conf.r0kh_list)
1732 		return NULL;
1733 
1734 	r0kh = os_zalloc(sizeof(*r0kh));
1735 	if (!r0kh)
1736 		return NULL;
1737 
1738 	if (src_addr)
1739 		os_memcpy(r0kh->addr, src_addr, sizeof(r0kh->addr));
1740 
1741 	if (id_len > FT_R0KH_ID_MAX_LEN)
1742 		id_len = FT_R0KH_ID_MAX_LEN;
1743 	os_memcpy(r0kh->id, r0kh_id, id_len);
1744 	r0kh->id_len = id_len;
1745 
1746 	os_memcpy(r0kh->key, r0kh_wildcard->key, sizeof(r0kh->key));
1747 
1748 	r0kh->next = *wpa_auth->conf.r0kh_list;
1749 	*wpa_auth->conf.r0kh_list = r0kh;
1750 
1751 	if (timeout > 0)
1752 		eloop_register_timeout(timeout, 0, wpa_ft_rrb_del_r0kh,
1753 				       wpa_auth, r0kh);
1754 
1755 	if (wpa_ft_rrb_init_r0kh_seq(r0kh) < 0)
1756 		return NULL;
1757 
1758 	return r0kh;
1759 }
1760 
1761 
wpa_ft_rrb_del_r1kh(void * eloop_ctx,void * timeout_ctx)1762 static void wpa_ft_rrb_del_r1kh(void *eloop_ctx, void *timeout_ctx)
1763 {
1764 	struct wpa_authenticator *wpa_auth = eloop_ctx;
1765 	struct ft_remote_r1kh *r1kh, *prev = NULL;
1766 
1767 	if (!wpa_auth->conf.r1kh_list)
1768 		return;
1769 
1770 	for (r1kh = *wpa_auth->conf.r1kh_list; r1kh; r1kh = r1kh->next) {
1771 		if (r1kh == timeout_ctx)
1772 			break;
1773 		prev = r1kh;
1774 	}
1775 	if (!r1kh)
1776 		return;
1777 	if (prev)
1778 		prev->next = r1kh->next;
1779 	else
1780 		*wpa_auth->conf.r1kh_list = r1kh->next;
1781 	if (r1kh->seq)
1782 		wpa_ft_rrb_seq_flush(wpa_auth, r1kh->seq, 0);
1783 	os_free(r1kh->seq);
1784 	os_free(r1kh);
1785 }
1786 
1787 
wpa_ft_rrb_r1kh_replenish(struct wpa_authenticator * wpa_auth,struct ft_remote_r1kh * r1kh,int timeout)1788 static void wpa_ft_rrb_r1kh_replenish(struct wpa_authenticator *wpa_auth,
1789 				      struct ft_remote_r1kh *r1kh, int timeout)
1790 {
1791 	if (timeout > 0)
1792 		eloop_replenish_timeout(timeout, 0, wpa_ft_rrb_del_r1kh,
1793 					wpa_auth, r1kh);
1794 }
1795 
1796 
1797 static struct ft_remote_r1kh *
wpa_ft_rrb_add_r1kh(struct wpa_authenticator * wpa_auth,struct ft_remote_r1kh * r1kh_wildcard,const u8 * src_addr,const u8 * r1kh_id,int timeout)1798 wpa_ft_rrb_add_r1kh(struct wpa_authenticator *wpa_auth,
1799 		    struct ft_remote_r1kh *r1kh_wildcard,
1800 		    const u8 *src_addr, const u8 *r1kh_id, int timeout)
1801 {
1802 	struct ft_remote_r1kh *r1kh;
1803 
1804 	if (!wpa_auth->conf.r1kh_list)
1805 		return NULL;
1806 
1807 	r1kh = os_zalloc(sizeof(*r1kh));
1808 	if (!r1kh)
1809 		return NULL;
1810 
1811 	os_memcpy(r1kh->addr, src_addr, sizeof(r1kh->addr));
1812 	os_memcpy(r1kh->id, r1kh_id, sizeof(r1kh->id));
1813 	os_memcpy(r1kh->key, r1kh_wildcard->key, sizeof(r1kh->key));
1814 	r1kh->next = *wpa_auth->conf.r1kh_list;
1815 	*wpa_auth->conf.r1kh_list = r1kh;
1816 
1817 	if (timeout > 0)
1818 		eloop_register_timeout(timeout, 0, wpa_ft_rrb_del_r1kh,
1819 				       wpa_auth, r1kh);
1820 
1821 	if (wpa_ft_rrb_init_r1kh_seq(r1kh) < 0)
1822 		return NULL;
1823 
1824 	return r1kh;
1825 }
1826 
1827 
wpa_ft_sta_deinit(struct wpa_state_machine * sm)1828 void wpa_ft_sta_deinit(struct wpa_state_machine *sm)
1829 {
1830 	eloop_cancel_timeout(wpa_ft_expire_pull, sm, NULL);
1831 }
1832 
1833 
wpa_ft_deinit_seq(struct wpa_authenticator * wpa_auth)1834 static void wpa_ft_deinit_seq(struct wpa_authenticator *wpa_auth)
1835 {
1836 	struct ft_remote_r0kh *r0kh;
1837 	struct ft_remote_r1kh *r1kh;
1838 
1839 	eloop_cancel_timeout(wpa_ft_rrb_seq_timeout, wpa_auth, ELOOP_ALL_CTX);
1840 
1841 	if (wpa_auth->conf.r0kh_list)
1842 		r0kh = *wpa_auth->conf.r0kh_list;
1843 	else
1844 		r0kh = NULL;
1845 	for (; r0kh; r0kh = r0kh->next) {
1846 		if (!r0kh->seq)
1847 			continue;
1848 		wpa_ft_rrb_seq_flush(wpa_auth, r0kh->seq, 0);
1849 		os_free(r0kh->seq);
1850 		r0kh->seq = NULL;
1851 	}
1852 
1853 	if (wpa_auth->conf.r1kh_list)
1854 		r1kh = *wpa_auth->conf.r1kh_list;
1855 	else
1856 		r1kh = NULL;
1857 	for (; r1kh; r1kh = r1kh->next) {
1858 		if (!r1kh->seq)
1859 			continue;
1860 		wpa_ft_rrb_seq_flush(wpa_auth, r1kh->seq, 0);
1861 		os_free(r1kh->seq);
1862 		r1kh->seq = NULL;
1863 	}
1864 }
1865 
1866 
wpa_ft_deinit_rkh_tmp(struct wpa_authenticator * wpa_auth)1867 static void wpa_ft_deinit_rkh_tmp(struct wpa_authenticator *wpa_auth)
1868 {
1869 	struct ft_remote_r0kh *r0kh, *r0kh_next, *r0kh_prev = NULL;
1870 	struct ft_remote_r1kh *r1kh, *r1kh_next, *r1kh_prev = NULL;
1871 
1872 	if (wpa_auth->conf.r0kh_list)
1873 		r0kh = *wpa_auth->conf.r0kh_list;
1874 	else
1875 		r0kh = NULL;
1876 	while (r0kh) {
1877 		r0kh_next = r0kh->next;
1878 		if (eloop_cancel_timeout(wpa_ft_rrb_del_r0kh, wpa_auth,
1879 					 r0kh) > 0) {
1880 			if (r0kh_prev)
1881 				r0kh_prev->next = r0kh_next;
1882 			else
1883 				*wpa_auth->conf.r0kh_list = r0kh_next;
1884 			os_free(r0kh);
1885 		} else {
1886 			r0kh_prev = r0kh;
1887 		}
1888 		r0kh = r0kh_next;
1889 	}
1890 
1891 	if (wpa_auth->conf.r1kh_list)
1892 		r1kh = *wpa_auth->conf.r1kh_list;
1893 	else
1894 		r1kh = NULL;
1895 	while (r1kh) {
1896 		r1kh_next = r1kh->next;
1897 		if (eloop_cancel_timeout(wpa_ft_rrb_del_r1kh, wpa_auth,
1898 					 r1kh) > 0) {
1899 			if (r1kh_prev)
1900 				r1kh_prev->next = r1kh_next;
1901 			else
1902 				*wpa_auth->conf.r1kh_list = r1kh_next;
1903 			os_free(r1kh);
1904 		} else {
1905 			r1kh_prev = r1kh;
1906 		}
1907 		r1kh = r1kh_next;
1908 	}
1909 }
1910 
1911 
wpa_ft_deinit(struct wpa_authenticator * wpa_auth)1912 void wpa_ft_deinit(struct wpa_authenticator *wpa_auth)
1913 {
1914 	wpa_ft_deinit_seq(wpa_auth);
1915 	wpa_ft_deinit_rkh_tmp(wpa_auth);
1916 }
1917 
1918 
wpa_ft_block_r0kh(struct wpa_authenticator * wpa_auth,const u8 * f_r0kh_id,size_t f_r0kh_id_len)1919 static void wpa_ft_block_r0kh(struct wpa_authenticator *wpa_auth,
1920 			      const u8 *f_r0kh_id, size_t f_r0kh_id_len)
1921 {
1922 	struct ft_remote_r0kh *r0kh, *r0kh_wildcard;
1923 
1924 	if (!wpa_auth->conf.rkh_neg_timeout)
1925 		return;
1926 
1927 	wpa_ft_rrb_lookup_r0kh(wpa_auth, f_r0kh_id, f_r0kh_id_len,
1928 			       &r0kh, &r0kh_wildcard);
1929 
1930 	if (!r0kh_wildcard) {
1931 		/* r0kh removed after neg_timeout and might need re-adding */
1932 		return;
1933 	}
1934 
1935 	wpa_hexdump(MSG_DEBUG, "FT: Temporarily block R0KH-ID",
1936 		    f_r0kh_id, f_r0kh_id_len);
1937 
1938 	if (r0kh) {
1939 		wpa_ft_rrb_r0kh_timeout(wpa_auth, r0kh,
1940 					wpa_auth->conf.rkh_neg_timeout);
1941 		os_memset(r0kh->addr, 0, ETH_ALEN);
1942 	} else
1943 		wpa_ft_rrb_add_r0kh(wpa_auth, r0kh_wildcard, NULL, f_r0kh_id,
1944 				    f_r0kh_id_len,
1945 				    wpa_auth->conf.rkh_neg_timeout);
1946 }
1947 
1948 
wpa_ft_expire_pull(void * eloop_ctx,void * timeout_ctx)1949 static void wpa_ft_expire_pull(void *eloop_ctx, void *timeout_ctx)
1950 {
1951 	struct wpa_state_machine *sm = eloop_ctx;
1952 
1953 	wpa_printf(MSG_DEBUG, "FT: Timeout pending pull request for " MACSTR,
1954 		   MAC2STR(sm->addr));
1955 	if (sm->ft_pending_pull_left_retries <= 0)
1956 		wpa_ft_block_r0kh(sm->wpa_auth, sm->r0kh_id, sm->r0kh_id_len);
1957 
1958 	/* cancel multiple timeouts */
1959 	eloop_cancel_timeout(wpa_ft_expire_pull, sm, NULL);
1960 	ft_finish_pull(sm);
1961 }
1962 
1963 
wpa_ft_pull_pmk_r1(struct wpa_state_machine * sm,const u8 * ies,size_t ies_len,const u8 * pmk_r0_name)1964 static int wpa_ft_pull_pmk_r1(struct wpa_state_machine *sm,
1965 			      const u8 *ies, size_t ies_len,
1966 			      const u8 *pmk_r0_name)
1967 {
1968 	struct ft_remote_r0kh *r0kh, *r0kh_wildcard;
1969 	u8 *packet = NULL;
1970 	const u8 *key, *f_r1kh_id = sm->wpa_auth->conf.r1_key_holder;
1971 	size_t packet_len, key_len;
1972 	struct ft_rrb_seq f_seq;
1973 	int tsecs, tusecs, first;
1974 	struct wpabuf *ft_pending_req_ies;
1975 	int r0kh_timeout;
1976 	struct tlv_list req_enc[] = {
1977 		{ .type = FT_RRB_PMK_R0_NAME, .len = WPA_PMK_NAME_LEN,
1978 		  .data = pmk_r0_name },
1979 		{ .type = FT_RRB_S1KH_ID, .len = ETH_ALEN,
1980 		  .data = sm->addr },
1981 		{ .type = FT_RRB_LAST_EMPTY, .len = 0, .data = NULL },
1982 	};
1983 	struct tlv_list req_auth[] = {
1984 		{ .type = FT_RRB_NONCE, .len = FT_RRB_NONCE_LEN,
1985 		  .data = sm->ft_pending_pull_nonce },
1986 		{ .type = FT_RRB_SEQ, .len = sizeof(f_seq),
1987 		  .data = (u8 *) &f_seq },
1988 		{ .type = FT_RRB_R0KH_ID, .len = sm->r0kh_id_len,
1989 		  .data = sm->r0kh_id },
1990 		{ .type = FT_RRB_R1KH_ID, .len = FT_R1KH_ID_LEN,
1991 		  .data = f_r1kh_id },
1992 		{ .type = FT_RRB_LAST_EMPTY, .len = 0, .data = NULL },
1993 	};
1994 
1995 	if (sm->ft_pending_pull_left_retries <= 0)
1996 		return -1;
1997 	first = sm->ft_pending_pull_left_retries ==
1998 		sm->wpa_auth->conf.rkh_pull_retries;
1999 	sm->ft_pending_pull_left_retries--;
2000 
2001 	wpa_ft_rrb_lookup_r0kh(sm->wpa_auth, sm->r0kh_id, sm->r0kh_id_len,
2002 			       &r0kh, &r0kh_wildcard);
2003 
2004 	/* Keep r0kh sufficiently long in the list for seq num check */
2005 	r0kh_timeout = sm->wpa_auth->conf.rkh_pull_timeout / 1000 +
2006 		1 + ftRRBseqTimeout;
2007 	if (r0kh) {
2008 		wpa_ft_rrb_r0kh_replenish(sm->wpa_auth, r0kh, r0kh_timeout);
2009 	} else if (r0kh_wildcard) {
2010 		wpa_printf(MSG_DEBUG, "FT: Using wildcard R0KH-ID");
2011 		/* r0kh->addr: updated by SEQ_RESP and wpa_ft_expire_pull */
2012 		r0kh = wpa_ft_rrb_add_r0kh(sm->wpa_auth, r0kh_wildcard,
2013 					   r0kh_wildcard->addr,
2014 					   sm->r0kh_id, sm->r0kh_id_len,
2015 					   r0kh_timeout);
2016 	}
2017 	if (r0kh == NULL) {
2018 		wpa_hexdump(MSG_DEBUG, "FT: Did not find R0KH-ID",
2019 			    sm->r0kh_id, sm->r0kh_id_len);
2020 		return -1;
2021 	}
2022 	if (is_zero_ether_addr(r0kh->addr)) {
2023 		wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID is temporarily blocked",
2024 			    sm->r0kh_id, sm->r0kh_id_len);
2025 		return -1;
2026 	}
2027 	if (os_memcmp(r0kh->addr, sm->wpa_auth->addr, ETH_ALEN) == 0) {
2028 		wpa_printf(MSG_DEBUG,
2029 			   "FT: R0KH-ID points to self - no matching key available");
2030 		return -1;
2031 	}
2032 
2033 	key = r0kh->key;
2034 	key_len = sizeof(r0kh->key);
2035 
2036 	if (r0kh->seq->rx.num_last == 0) {
2037 		/* A sequence request will be sent out anyway when pull
2038 		 * response is received. Send it out now to avoid one RTT. */
2039 		wpa_ft_rrb_seq_req(sm->wpa_auth, r0kh->seq, r0kh->addr,
2040 				   r0kh->id, r0kh->id_len, f_r1kh_id, key,
2041 				   key_len, NULL, 0, NULL, 0, NULL);
2042 	}
2043 
2044 	wpa_printf(MSG_DEBUG, "FT: Send PMK-R1 pull request from " MACSTR
2045 		   " to remote R0KH address " MACSTR,
2046 		   MAC2STR(sm->wpa_auth->addr), MAC2STR(r0kh->addr));
2047 
2048 	if (first &&
2049 	    random_get_bytes(sm->ft_pending_pull_nonce, FT_RRB_NONCE_LEN) < 0) {
2050 		wpa_printf(MSG_DEBUG, "FT: Failed to get random data for "
2051 			   "nonce");
2052 		return -1;
2053 	}
2054 
2055 	if (wpa_ft_new_seq(r0kh->seq, &f_seq) < 0) {
2056 		wpa_printf(MSG_DEBUG, "FT: Failed to get seq num");
2057 		return -1;
2058 	}
2059 
2060 	if (wpa_ft_rrb_build(key, key_len, req_enc, NULL, req_auth, NULL,
2061 			     sm->wpa_auth->addr, FT_PACKET_R0KH_R1KH_PULL,
2062 			     &packet, &packet_len) < 0)
2063 		return -1;
2064 
2065 	ft_pending_req_ies = wpabuf_alloc_copy(ies, ies_len);
2066 	wpabuf_free(sm->ft_pending_req_ies);
2067 	sm->ft_pending_req_ies = ft_pending_req_ies;
2068 	if (!sm->ft_pending_req_ies) {
2069 		os_free(packet);
2070 		return -1;
2071 	}
2072 
2073 	tsecs = sm->wpa_auth->conf.rkh_pull_timeout / 1000;
2074 	tusecs = (sm->wpa_auth->conf.rkh_pull_timeout % 1000) * 1000;
2075 	eloop_register_timeout(tsecs, tusecs, wpa_ft_expire_pull, sm, NULL);
2076 
2077 	wpa_ft_rrb_oui_send(sm->wpa_auth, r0kh->addr, FT_PACKET_R0KH_R1KH_PULL,
2078 			    packet, packet_len);
2079 
2080 	os_free(packet);
2081 
2082 	return 0;
2083 }
2084 
2085 
wpa_ft_store_pmk_fils(struct wpa_state_machine * sm,const u8 * pmk_r0,const u8 * pmk_r0_name)2086 int wpa_ft_store_pmk_fils(struct wpa_state_machine *sm,
2087 			  const u8 *pmk_r0, const u8 *pmk_r0_name)
2088 {
2089 	int expires_in = sm->wpa_auth->conf.r0_key_lifetime;
2090 	struct vlan_description vlan;
2091 	const u8 *identity, *radius_cui;
2092 	size_t identity_len, radius_cui_len;
2093 	int session_timeout;
2094 	size_t pmk_r0_len = wpa_key_mgmt_sha384(sm->wpa_key_mgmt) ?
2095 		SHA384_MAC_LEN : PMK_LEN;
2096 
2097 	if (wpa_ft_get_vlan(sm->wpa_auth, sm->addr, &vlan) < 0) {
2098 		wpa_printf(MSG_DEBUG, "FT: vlan not available for STA " MACSTR,
2099 			   MAC2STR(sm->addr));
2100 		return -1;
2101 	}
2102 
2103 	identity_len = wpa_ft_get_identity(sm->wpa_auth, sm->addr, &identity);
2104 	radius_cui_len = wpa_ft_get_radius_cui(sm->wpa_auth, sm->addr,
2105 					       &radius_cui);
2106 	session_timeout = wpa_ft_get_session_timeout(sm->wpa_auth, sm->addr);
2107 
2108 	return wpa_ft_store_pmk_r0(sm->wpa_auth, sm->addr, pmk_r0, pmk_r0_len,
2109 				   pmk_r0_name, sm->pairwise, &vlan, expires_in,
2110 				   session_timeout, identity, identity_len,
2111 				   radius_cui, radius_cui_len);
2112 }
2113 
2114 
wpa_auth_derive_ptk_ft(struct wpa_state_machine * sm,struct wpa_ptk * ptk,u8 * pmk_r0,u8 * pmk_r1,u8 * pmk_r0_name,size_t * key_len,size_t kdk_len)2115 int wpa_auth_derive_ptk_ft(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
2116 			   u8 *pmk_r0, u8 *pmk_r1, u8 *pmk_r0_name,
2117 			   size_t *key_len, size_t kdk_len)
2118 {
2119 	size_t pmk_r0_len, pmk_r1_len;
2120 	u8 ptk_name[WPA_PMK_NAME_LEN];
2121 	const u8 *mdid = sm->wpa_auth->conf.mobility_domain;
2122 	const u8 *r0kh = sm->wpa_auth->conf.r0_key_holder;
2123 	size_t r0kh_len = sm->wpa_auth->conf.r0_key_holder_len;
2124 	const u8 *r1kh = sm->wpa_auth->conf.r1_key_holder;
2125 	const u8 *ssid = sm->wpa_auth->conf.ssid;
2126 	size_t ssid_len = sm->wpa_auth->conf.ssid_len;
2127 	const u8 *mpmk;
2128 	size_t mpmk_len;
2129 
2130 	if (sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
2131 	    (sm->xxkey_len == SHA256_MAC_LEN ||
2132 	     sm->xxkey_len == SHA384_MAC_LEN ||
2133 	     sm->xxkey_len == SHA512_MAC_LEN))
2134 		pmk_r0_len = sm->xxkey_len;
2135 	else if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt))
2136 		pmk_r0_len = SHA384_MAC_LEN;
2137 	else
2138 		pmk_r0_len = PMK_LEN;
2139 	*key_len = pmk_r1_len = pmk_r0_len;
2140 
2141 	if (sm->xxkey_len > 0) {
2142 		mpmk = sm->xxkey;
2143 		mpmk_len = sm->xxkey_len;
2144 	} else if (sm->pmksa) {
2145 		mpmk = sm->pmksa->pmk;
2146 		mpmk_len = sm->pmksa->pmk_len;
2147 	} else {
2148 		wpa_printf(MSG_DEBUG, "FT: XXKey not available for key "
2149 			   "derivation");
2150 		return -1;
2151 	}
2152 
2153 	if (wpa_derive_pmk_r0(mpmk, mpmk_len, ssid, ssid_len, mdid,
2154 			      r0kh, r0kh_len, sm->addr,
2155 			      pmk_r0, pmk_r0_name,
2156 			      sm->wpa_key_mgmt) < 0 ||
2157 	    wpa_derive_pmk_r1(pmk_r0, pmk_r0_len, pmk_r0_name, r1kh, sm->addr,
2158 			      pmk_r1, sm->pmk_r1_name) < 0)
2159 		return -1;
2160 
2161 	return wpa_pmk_r1_to_ptk(pmk_r1, pmk_r1_len, sm->SNonce, sm->ANonce,
2162 				 sm->addr, sm->wpa_auth->addr, sm->pmk_r1_name,
2163 				 ptk, ptk_name, sm->wpa_key_mgmt, sm->pairwise,
2164 				 kdk_len);
2165 }
2166 
2167 
wpa_auth_ft_store_keys(struct wpa_state_machine * sm,const u8 * pmk_r0,const u8 * pmk_r1,const u8 * pmk_r0_name,size_t key_len)2168 void wpa_auth_ft_store_keys(struct wpa_state_machine *sm, const u8 *pmk_r0,
2169 			    const u8 *pmk_r1, const u8 *pmk_r0_name,
2170 			    size_t key_len)
2171 {
2172 	int psk_local = sm->wpa_auth->conf.ft_psk_generate_local;
2173 	int expires_in = sm->wpa_auth->conf.r0_key_lifetime;
2174 	struct vlan_description vlan;
2175 	const u8 *identity, *radius_cui;
2176 	size_t identity_len, radius_cui_len;
2177 	int session_timeout;
2178 
2179 	if (psk_local && wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt))
2180 		return;
2181 
2182 	if (wpa_ft_get_vlan(sm->wpa_auth, sm->addr, &vlan) < 0) {
2183 		wpa_printf(MSG_DEBUG, "FT: vlan not available for STA " MACSTR,
2184 			   MAC2STR(sm->addr));
2185 		return;
2186 	}
2187 
2188 	identity_len = wpa_ft_get_identity(sm->wpa_auth, sm->addr, &identity);
2189 	radius_cui_len = wpa_ft_get_radius_cui(sm->wpa_auth, sm->addr,
2190 					       &radius_cui);
2191 	session_timeout = wpa_ft_get_session_timeout(sm->wpa_auth, sm->addr);
2192 
2193 
2194 	wpa_ft_store_pmk_r0(sm->wpa_auth, sm->addr, pmk_r0, key_len,
2195 			    pmk_r0_name,
2196 			    sm->pairwise, &vlan, expires_in,
2197 			    session_timeout, identity, identity_len,
2198 			    radius_cui, radius_cui_len);
2199 	wpa_ft_store_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1, key_len,
2200 			    sm->pmk_r1_name, sm->pairwise, &vlan,
2201 			    expires_in, session_timeout, identity,
2202 			    identity_len, radius_cui, radius_cui_len);
2203 }
2204 
2205 
wpa_auth_get_seqnum(struct wpa_authenticator * wpa_auth,const u8 * addr,int idx,u8 * seq)2206 static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
2207 				      const u8 *addr, int idx, u8 *seq)
2208 {
2209 	if (wpa_auth->cb->get_seqnum == NULL)
2210 		return -1;
2211 	return wpa_auth->cb->get_seqnum(wpa_auth->cb_ctx, addr, idx, seq);
2212 }
2213 
2214 
wpa_ft_gtk_subelem(struct wpa_state_machine * sm,size_t * len)2215 static u8 * wpa_ft_gtk_subelem(struct wpa_state_machine *sm, size_t *len)
2216 {
2217 	u8 *subelem;
2218 	struct wpa_auth_config *conf = &sm->wpa_auth->conf;
2219 	struct wpa_group *gsm = sm->group;
2220 	size_t subelem_len, pad_len;
2221 	const u8 *key;
2222 	size_t key_len;
2223 	u8 keybuf[WPA_GTK_MAX_LEN];
2224 	const u8 *kek;
2225 	size_t kek_len;
2226 
2227 	if (wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2228 		kek = sm->PTK.kek2;
2229 		kek_len = sm->PTK.kek2_len;
2230 	} else {
2231 		kek = sm->PTK.kek;
2232 		kek_len = sm->PTK.kek_len;
2233 	}
2234 
2235 	key_len = gsm->GTK_len;
2236 	if (key_len > sizeof(keybuf))
2237 		return NULL;
2238 
2239 	/*
2240 	 * Pad key for AES Key Wrap if it is not multiple of 8 bytes or is less
2241 	 * than 16 bytes.
2242 	 */
2243 	pad_len = key_len % 8;
2244 	if (pad_len)
2245 		pad_len = 8 - pad_len;
2246 	if (key_len + pad_len < 16)
2247 		pad_len += 8;
2248 	if (pad_len && key_len < sizeof(keybuf)) {
2249 		os_memcpy(keybuf, gsm->GTK[gsm->GN - 1], key_len);
2250 		if (conf->disable_gtk ||
2251 		    sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
2252 			/*
2253 			 * Provide unique random GTK to each STA to prevent use
2254 			 * of GTK in the BSS.
2255 			 */
2256 			if (random_get_bytes(keybuf, key_len) < 0)
2257 				return NULL;
2258 		}
2259 		os_memset(keybuf + key_len, 0, pad_len);
2260 		keybuf[key_len] = 0xdd;
2261 		key_len += pad_len;
2262 		key = keybuf;
2263 	} else if (conf->disable_gtk || sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
2264 		/*
2265 		 * Provide unique random GTK to each STA to prevent use of GTK
2266 		 * in the BSS.
2267 		 */
2268 		if (random_get_bytes(keybuf, key_len) < 0)
2269 			return NULL;
2270 		key = keybuf;
2271 	} else {
2272 		key = gsm->GTK[gsm->GN - 1];
2273 	}
2274 
2275 	/*
2276 	 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
2277 	 * Key[5..32].
2278 	 */
2279 	subelem_len = 13 + key_len + 8;
2280 	subelem = os_zalloc(subelem_len);
2281 	if (subelem == NULL)
2282 		return NULL;
2283 
2284 	subelem[0] = FTIE_SUBELEM_GTK;
2285 	subelem[1] = 11 + key_len + 8;
2286 	/* Key ID in B0-B1 of Key Info */
2287 	WPA_PUT_LE16(&subelem[2], gsm->GN & 0x03);
2288 	subelem[4] = gsm->GTK_len;
2289 	wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, subelem + 5);
2290 	if (aes_wrap(kek, kek_len, key_len / 8, key, subelem + 13)) {
2291 		wpa_printf(MSG_DEBUG,
2292 			   "FT: GTK subelem encryption failed: kek_len=%d",
2293 			   (int) kek_len);
2294 		forced_memzero(keybuf, sizeof(keybuf));
2295 		os_free(subelem);
2296 		return NULL;
2297 	}
2298 
2299 	forced_memzero(keybuf, sizeof(keybuf));
2300 	*len = subelem_len;
2301 	return subelem;
2302 }
2303 
2304 
wpa_ft_igtk_subelem(struct wpa_state_machine * sm,size_t * len)2305 static u8 * wpa_ft_igtk_subelem(struct wpa_state_machine *sm, size_t *len)
2306 {
2307 	u8 *subelem, *pos;
2308 	struct wpa_auth_config *conf = &sm->wpa_auth->conf;
2309 	struct wpa_group *gsm = sm->group;
2310 	size_t subelem_len;
2311 	const u8 *kek, *igtk;
2312 	size_t kek_len;
2313 	size_t igtk_len;
2314 	u8 stub_igtk[WPA_IGTK_MAX_LEN];
2315 
2316 	if (wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2317 		kek = sm->PTK.kek2;
2318 		kek_len = sm->PTK.kek2_len;
2319 	} else {
2320 		kek = sm->PTK.kek;
2321 		kek_len = sm->PTK.kek_len;
2322 	}
2323 
2324 	igtk_len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2325 
2326 	/* Sub-elem ID[1] | Length[1] | KeyID[2] | IPN[6] | Key Length[1] |
2327 	 * Key[16+8] */
2328 	subelem_len = 1 + 1 + 2 + 6 + 1 + igtk_len + 8;
2329 	subelem = os_zalloc(subelem_len);
2330 	if (subelem == NULL)
2331 		return NULL;
2332 
2333 	pos = subelem;
2334 	*pos++ = FTIE_SUBELEM_IGTK;
2335 	*pos++ = subelem_len - 2;
2336 	WPA_PUT_LE16(pos, gsm->GN_igtk);
2337 	pos += 2;
2338 	wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos);
2339 	pos += 6;
2340 	*pos++ = igtk_len;
2341 	igtk = gsm->IGTK[gsm->GN_igtk - 4];
2342 	if (conf->disable_gtk || sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
2343 		/*
2344 		 * Provide unique random IGTK to each STA to prevent use of
2345 		 * IGTK in the BSS.
2346 		 */
2347 		if (random_get_bytes(stub_igtk, igtk_len / 8) < 0) {
2348 			os_free(subelem);
2349 			return NULL;
2350 		}
2351 		igtk = stub_igtk;
2352 	}
2353 	if (aes_wrap(kek, kek_len, igtk_len / 8, igtk, pos)) {
2354 		wpa_printf(MSG_DEBUG,
2355 			   "FT: IGTK subelem encryption failed: kek_len=%d",
2356 			   (int) kek_len);
2357 		os_free(subelem);
2358 		return NULL;
2359 	}
2360 
2361 	*len = subelem_len;
2362 	return subelem;
2363 }
2364 
2365 
wpa_ft_bigtk_subelem(struct wpa_state_machine * sm,size_t * len)2366 static u8 * wpa_ft_bigtk_subelem(struct wpa_state_machine *sm, size_t *len)
2367 {
2368 	u8 *subelem, *pos;
2369 	struct wpa_group *gsm = sm->group;
2370 	size_t subelem_len;
2371 	const u8 *kek, *bigtk;
2372 	size_t kek_len;
2373 	size_t bigtk_len;
2374 	u8 stub_bigtk[WPA_IGTK_MAX_LEN];
2375 
2376 	if (wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2377 		kek = sm->PTK.kek2;
2378 		kek_len = sm->PTK.kek2_len;
2379 	} else {
2380 		kek = sm->PTK.kek;
2381 		kek_len = sm->PTK.kek_len;
2382 	}
2383 
2384 	bigtk_len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2385 
2386 	/* Sub-elem ID[1] | Length[1] | KeyID[2] | BIPN[6] | Key Length[1] |
2387 	 * Key[16+8] */
2388 	subelem_len = 1 + 1 + 2 + 6 + 1 + bigtk_len + 8;
2389 	subelem = os_zalloc(subelem_len);
2390 	if (subelem == NULL)
2391 		return NULL;
2392 
2393 	pos = subelem;
2394 	*pos++ = FTIE_SUBELEM_BIGTK;
2395 	*pos++ = subelem_len - 2;
2396 	WPA_PUT_LE16(pos, gsm->GN_bigtk);
2397 	pos += 2;
2398 	wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_bigtk, pos);
2399 	pos += 6;
2400 	*pos++ = bigtk_len;
2401 	bigtk = gsm->BIGTK[gsm->GN_bigtk - 6];
2402 	if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
2403 		/*
2404 		 * Provide unique random BIGTK to each OSEN STA to prevent use
2405 		 * of BIGTK in the BSS.
2406 		 */
2407 		if (random_get_bytes(stub_bigtk, bigtk_len / 8) < 0) {
2408 			os_free(subelem);
2409 			return NULL;
2410 		}
2411 		bigtk = stub_bigtk;
2412 	}
2413 	if (aes_wrap(kek, kek_len, bigtk_len / 8, bigtk, pos)) {
2414 		wpa_printf(MSG_DEBUG,
2415 			   "FT: BIGTK subelem encryption failed: kek_len=%d",
2416 			   (int) kek_len);
2417 		os_free(subelem);
2418 		return NULL;
2419 	}
2420 
2421 	*len = subelem_len;
2422 	return subelem;
2423 }
2424 
2425 
wpa_ft_process_rdie(struct wpa_state_machine * sm,u8 * pos,u8 * end,u8 id,u8 descr_count,const u8 * ies,size_t ies_len)2426 static u8 * wpa_ft_process_rdie(struct wpa_state_machine *sm,
2427 				u8 *pos, u8 *end, u8 id, u8 descr_count,
2428 				const u8 *ies, size_t ies_len)
2429 {
2430 	struct ieee802_11_elems parse;
2431 	struct rsn_rdie *rdie;
2432 
2433 	wpa_printf(MSG_DEBUG, "FT: Resource Request: id=%d descr_count=%d",
2434 		   id, descr_count);
2435 	wpa_hexdump(MSG_MSGDUMP, "FT: Resource descriptor IE(s)",
2436 		    ies, ies_len);
2437 
2438 	if (end - pos < (int) sizeof(*rdie)) {
2439 		wpa_printf(MSG_ERROR, "FT: Not enough room for response RDIE");
2440 		return pos;
2441 	}
2442 
2443 	*pos++ = WLAN_EID_RIC_DATA;
2444 	*pos++ = sizeof(*rdie);
2445 	rdie = (struct rsn_rdie *) pos;
2446 	rdie->id = id;
2447 	rdie->descr_count = 0;
2448 	rdie->status_code = host_to_le16(WLAN_STATUS_SUCCESS);
2449 	pos += sizeof(*rdie);
2450 
2451 	if (ieee802_11_parse_elems((u8 *) ies, ies_len, &parse, 1) ==
2452 	    ParseFailed) {
2453 		wpa_printf(MSG_DEBUG, "FT: Failed to parse request IEs");
2454 		rdie->status_code =
2455 			host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
2456 		return pos;
2457 	}
2458 
2459 	if (parse.wmm_tspec) {
2460 		struct wmm_tspec_element *tspec;
2461 
2462 		if (parse.wmm_tspec_len + 2 < (int) sizeof(*tspec)) {
2463 			wpa_printf(MSG_DEBUG, "FT: Too short WMM TSPEC IE "
2464 				   "(%d)", (int) parse.wmm_tspec_len);
2465 			rdie->status_code =
2466 				host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
2467 			return pos;
2468 		}
2469 		if (end - pos < (int) sizeof(*tspec)) {
2470 			wpa_printf(MSG_ERROR, "FT: Not enough room for "
2471 				   "response TSPEC");
2472 			rdie->status_code =
2473 				host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
2474 			return pos;
2475 		}
2476 		tspec = (struct wmm_tspec_element *) pos;
2477 		os_memcpy(tspec, parse.wmm_tspec - 2, sizeof(*tspec));
2478 	}
2479 
2480 #ifdef NEED_AP_MLME
2481 	if (parse.wmm_tspec && sm->wpa_auth->conf.ap_mlme) {
2482 		int res;
2483 
2484 		res = wmm_process_tspec((struct wmm_tspec_element *) pos);
2485 		wpa_printf(MSG_DEBUG, "FT: ADDTS processing result: %d", res);
2486 		if (res == WMM_ADDTS_STATUS_INVALID_PARAMETERS)
2487 			rdie->status_code =
2488 				host_to_le16(WLAN_STATUS_INVALID_PARAMETERS);
2489 		else if (res == WMM_ADDTS_STATUS_REFUSED)
2490 			rdie->status_code =
2491 				host_to_le16(WLAN_STATUS_REQUEST_DECLINED);
2492 		else {
2493 			/* TSPEC accepted; include updated TSPEC in response */
2494 			rdie->descr_count = 1;
2495 			pos += sizeof(struct wmm_tspec_element);
2496 		}
2497 		return pos;
2498 	}
2499 #endif /* NEED_AP_MLME */
2500 
2501 	if (parse.wmm_tspec && !sm->wpa_auth->conf.ap_mlme) {
2502 		int res;
2503 
2504 		res = wpa_ft_add_tspec(sm->wpa_auth, sm->addr, pos,
2505 				       sizeof(struct wmm_tspec_element));
2506 		if (res >= 0) {
2507 			if (res)
2508 				rdie->status_code = host_to_le16(res);
2509 			else {
2510 				/* TSPEC accepted; include updated TSPEC in
2511 				 * response */
2512 				rdie->descr_count = 1;
2513 				pos += sizeof(struct wmm_tspec_element);
2514 			}
2515 			return pos;
2516 		}
2517 	}
2518 
2519 	wpa_printf(MSG_DEBUG, "FT: No supported resource requested");
2520 	rdie->status_code = host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
2521 	return pos;
2522 }
2523 
2524 
wpa_ft_process_ric(struct wpa_state_machine * sm,u8 * pos,u8 * end,const u8 * ric,size_t ric_len)2525 static u8 * wpa_ft_process_ric(struct wpa_state_machine *sm, u8 *pos, u8 *end,
2526 			       const u8 *ric, size_t ric_len)
2527 {
2528 	const u8 *rpos, *start;
2529 	const struct rsn_rdie *rdie;
2530 
2531 	wpa_hexdump(MSG_MSGDUMP, "FT: RIC Request", ric, ric_len);
2532 
2533 	rpos = ric;
2534 	while (rpos + sizeof(*rdie) < ric + ric_len) {
2535 		if (rpos[0] != WLAN_EID_RIC_DATA || rpos[1] < sizeof(*rdie) ||
2536 		    rpos + 2 + rpos[1] > ric + ric_len)
2537 			break;
2538 		rdie = (const struct rsn_rdie *) (rpos + 2);
2539 		rpos += 2 + rpos[1];
2540 		start = rpos;
2541 
2542 		while (rpos + 2 <= ric + ric_len &&
2543 		       rpos + 2 + rpos[1] <= ric + ric_len) {
2544 			if (rpos[0] == WLAN_EID_RIC_DATA)
2545 				break;
2546 			rpos += 2 + rpos[1];
2547 		}
2548 		pos = wpa_ft_process_rdie(sm, pos, end, rdie->id,
2549 					  rdie->descr_count,
2550 					  start, rpos - start);
2551 	}
2552 
2553 	return pos;
2554 }
2555 
2556 
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,int omit_rsnxe)2557 u8 * wpa_sm_write_assoc_resp_ies(struct wpa_state_machine *sm, u8 *pos,
2558 				 size_t max_len, int auth_alg,
2559 				 const u8 *req_ies, size_t req_ies_len,
2560 				 int omit_rsnxe)
2561 {
2562 	u8 *end, *mdie, *ftie, *rsnie = NULL, *r0kh_id, *subelem = NULL;
2563 	u8 *fte_mic, *elem_count;
2564 	size_t mdie_len, ftie_len, rsnie_len = 0, r0kh_id_len, subelem_len = 0;
2565 	u8 rsnxe_buf[10], *rsnxe = rsnxe_buf;
2566 	size_t rsnxe_len;
2567 	int rsnxe_used;
2568 	int res;
2569 	struct wpa_auth_config *conf;
2570 	struct wpa_ft_ies parse;
2571 	u8 *ric_start;
2572 	u8 *anonce, *snonce;
2573 	const u8 *kck;
2574 	size_t kck_len;
2575 	size_t key_len;
2576 
2577 	if (sm == NULL)
2578 		return pos;
2579 
2580 	conf = &sm->wpa_auth->conf;
2581 
2582 	if (!wpa_key_mgmt_ft(sm->wpa_key_mgmt))
2583 		return pos;
2584 
2585 	end = pos + max_len;
2586 
2587 #ifdef CONFIG_TESTING_OPTIONS
2588 	if (auth_alg == WLAN_AUTH_FT &&
2589 	    sm->wpa_auth->conf.rsne_override_ft_set) {
2590 		wpa_printf(MSG_DEBUG,
2591 			   "TESTING: RSNE FT override for MIC calculation");
2592 		rsnie = sm->wpa_auth->conf.rsne_override_ft;
2593 		rsnie_len = sm->wpa_auth->conf.rsne_override_ft_len;
2594 		if (end - pos < (long int) rsnie_len)
2595 			return pos;
2596 		os_memcpy(pos, rsnie, rsnie_len);
2597 		rsnie = pos;
2598 		pos += rsnie_len;
2599 		if (rsnie_len > PMKID_LEN && sm->pmk_r1_name_valid) {
2600 			int idx;
2601 
2602 			/* Replace all 0xff PMKID with the valid PMKR1Name */
2603 			for (idx = 0; idx < PMKID_LEN; idx++) {
2604 				if (rsnie[rsnie_len - 1 - idx] != 0xff)
2605 					break;
2606 			}
2607 			if (idx == PMKID_LEN)
2608 				os_memcpy(&rsnie[rsnie_len - PMKID_LEN],
2609 					  sm->pmk_r1_name, WPA_PMK_NAME_LEN);
2610 		}
2611 	} else
2612 #endif /* CONFIG_TESTING_OPTIONS */
2613 	if (auth_alg == WLAN_AUTH_FT ||
2614 	    ((auth_alg == WLAN_AUTH_FILS_SK ||
2615 	      auth_alg == WLAN_AUTH_FILS_SK_PFS ||
2616 	      auth_alg == WLAN_AUTH_FILS_PK) &&
2617 	     (sm->wpa_key_mgmt & (WPA_KEY_MGMT_FT_FILS_SHA256 |
2618 				  WPA_KEY_MGMT_FT_FILS_SHA384)))) {
2619 		if (!sm->pmk_r1_name_valid) {
2620 			wpa_printf(MSG_ERROR,
2621 				   "FT: PMKR1Name is not valid for Assoc Resp RSNE");
2622 			return NULL;
2623 		}
2624 		wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name for Assoc Resp RSNE",
2625 			    sm->pmk_r1_name, WPA_PMK_NAME_LEN);
2626 		/*
2627 		 * RSN (only present if this is a Reassociation Response and
2628 		 * part of a fast BSS transition; or if this is a
2629 		 * (Re)Association Response frame during an FT initial mobility
2630 		 * domain association using FILS)
2631 		 */
2632 		res = wpa_write_rsn_ie(conf, pos, end - pos, sm->pmk_r1_name);
2633 		if (res < 0)
2634 			return NULL;
2635 		rsnie = pos;
2636 		rsnie_len = res;
2637 		pos += res;
2638 	}
2639 
2640 	/* Mobility Domain Information */
2641 	res = wpa_write_mdie(conf, pos, end - pos);
2642 	if (res < 0)
2643 		return NULL;
2644 	mdie = pos;
2645 	mdie_len = res;
2646 	pos += res;
2647 
2648 	/* Fast BSS Transition Information */
2649 	if (auth_alg == WLAN_AUTH_FT) {
2650 		subelem = wpa_ft_gtk_subelem(sm, &subelem_len);
2651 		if (!subelem) {
2652 			wpa_printf(MSG_DEBUG,
2653 				   "FT: Failed to add GTK subelement");
2654 			return NULL;
2655 		}
2656 		r0kh_id = sm->r0kh_id;
2657 		r0kh_id_len = sm->r0kh_id_len;
2658 		anonce = sm->ANonce;
2659 		snonce = sm->SNonce;
2660 		if (sm->mgmt_frame_prot) {
2661 			u8 *igtk;
2662 			size_t igtk_len;
2663 			u8 *nbuf;
2664 			igtk = wpa_ft_igtk_subelem(sm, &igtk_len);
2665 			if (igtk == NULL) {
2666 				wpa_printf(MSG_DEBUG,
2667 					   "FT: Failed to add IGTK subelement");
2668 				os_free(subelem);
2669 				return NULL;
2670 			}
2671 			nbuf = os_realloc(subelem, subelem_len + igtk_len);
2672 			if (nbuf == NULL) {
2673 				os_free(subelem);
2674 				os_free(igtk);
2675 				return NULL;
2676 			}
2677 			subelem = nbuf;
2678 			os_memcpy(subelem + subelem_len, igtk, igtk_len);
2679 			subelem_len += igtk_len;
2680 			os_free(igtk);
2681 		}
2682 		if (sm->mgmt_frame_prot && conf->beacon_prot) {
2683 			u8 *bigtk;
2684 			size_t bigtk_len;
2685 			u8 *nbuf;
2686 
2687 			bigtk = wpa_ft_bigtk_subelem(sm, &bigtk_len);
2688 			if (!bigtk) {
2689 				wpa_printf(MSG_DEBUG,
2690 					   "FT: Failed to add BIGTK subelement");
2691 				os_free(subelem);
2692 				return NULL;
2693 			}
2694 			nbuf = os_realloc(subelem, subelem_len + bigtk_len);
2695 			if (!nbuf) {
2696 				os_free(subelem);
2697 				os_free(bigtk);
2698 				return NULL;
2699 			}
2700 			subelem = nbuf;
2701 			os_memcpy(subelem + subelem_len, bigtk, bigtk_len);
2702 			subelem_len += bigtk_len;
2703 			os_free(bigtk);
2704 		}
2705 #ifdef CONFIG_OCV
2706 		if (wpa_auth_uses_ocv(sm)) {
2707 			struct wpa_channel_info ci;
2708 			u8 *nbuf, *ocipos;
2709 
2710 			if (wpa_channel_info(sm->wpa_auth, &ci) != 0) {
2711 				wpa_printf(MSG_WARNING,
2712 					   "Failed to get channel info for OCI element");
2713 				os_free(subelem);
2714 				return NULL;
2715 			}
2716 #ifdef CONFIG_TESTING_OPTIONS
2717 			if (conf->oci_freq_override_ft_assoc) {
2718 				wpa_printf(MSG_INFO,
2719 					   "TEST: Override OCI frequency %d -> %u MHz",
2720 					   ci.frequency,
2721 					   conf->oci_freq_override_ft_assoc);
2722 				ci.frequency = conf->oci_freq_override_ft_assoc;
2723 			}
2724 #endif /* CONFIG_TESTING_OPTIONS */
2725 
2726 			subelem_len += 2 + OCV_OCI_LEN;
2727 			nbuf = os_realloc(subelem, subelem_len);
2728 			if (!nbuf) {
2729 				os_free(subelem);
2730 				return NULL;
2731 			}
2732 			subelem = nbuf;
2733 
2734 			ocipos = subelem + subelem_len - 2 - OCV_OCI_LEN;
2735 			*ocipos++ = FTIE_SUBELEM_OCI;
2736 			*ocipos++ = OCV_OCI_LEN;
2737 			if (ocv_insert_oci(&ci, &ocipos) < 0) {
2738 				os_free(subelem);
2739 				return NULL;
2740 			}
2741 		}
2742 #endif /* CONFIG_OCV */
2743 	} else {
2744 		r0kh_id = conf->r0_key_holder;
2745 		r0kh_id_len = conf->r0_key_holder_len;
2746 		anonce = NULL;
2747 		snonce = NULL;
2748 	}
2749 	rsnxe_used = (auth_alg == WLAN_AUTH_FT) &&
2750 		(conf->sae_pwe == SAE_PWE_HASH_TO_ELEMENT ||
2751 		 conf->sae_pwe == SAE_PWE_BOTH);
2752 #ifdef CONFIG_TESTING_OPTIONS
2753 	if (sm->wpa_auth->conf.ft_rsnxe_used) {
2754 		rsnxe_used = sm->wpa_auth->conf.ft_rsnxe_used == 1;
2755 		wpa_printf(MSG_DEBUG, "TESTING: FT: Force RSNXE Used %d",
2756 			   rsnxe_used);
2757 	}
2758 #endif /* CONFIG_TESTING_OPTIONS */
2759 	key_len = sm->xxkey_len;
2760 	if (!key_len)
2761 		key_len = sm->pmk_r1_len;
2762 	if (!key_len && sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
2763 	    sm->wpa_auth->cb->get_psk) {
2764 		size_t psk_len;
2765 
2766 		if (sm->wpa_auth->cb->get_psk(sm->wpa_auth->cb_ctx,
2767 					      sm->addr, sm->p2p_dev_addr,
2768 					      NULL, &psk_len, NULL))
2769 			key_len = psk_len;
2770 	}
2771 	res = wpa_write_ftie(conf, sm->wpa_key_mgmt, key_len,
2772 			     r0kh_id, r0kh_id_len,
2773 			     anonce, snonce, pos, end - pos,
2774 			     subelem, subelem_len, rsnxe_used);
2775 	os_free(subelem);
2776 	if (res < 0)
2777 		return NULL;
2778 	ftie = pos;
2779 	ftie_len = res;
2780 	pos += res;
2781 
2782 	if (sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
2783 	    key_len == SHA512_MAC_LEN) {
2784 		struct rsn_ftie_sha512 *_ftie =
2785 			(struct rsn_ftie_sha512 *) (ftie + 2);
2786 
2787 		fte_mic = _ftie->mic;
2788 		elem_count = &_ftie->mic_control[1];
2789 	} else if ((sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
2790 		    key_len == SHA384_MAC_LEN) ||
2791 		   wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
2792 		struct rsn_ftie_sha384 *_ftie =
2793 			(struct rsn_ftie_sha384 *) (ftie + 2);
2794 
2795 		fte_mic = _ftie->mic;
2796 		elem_count = &_ftie->mic_control[1];
2797 	} else {
2798 		struct rsn_ftie *_ftie = (struct rsn_ftie *) (ftie + 2);
2799 
2800 		fte_mic = _ftie->mic;
2801 		elem_count = &_ftie->mic_control[1];
2802 	}
2803 	if (auth_alg == WLAN_AUTH_FT)
2804 		*elem_count = 3; /* Information element count */
2805 
2806 	ric_start = pos;
2807 	if (wpa_ft_parse_ies(req_ies, req_ies_len, &parse,
2808 			     sm->wpa_key_mgmt, false) == 0 && parse.ric) {
2809 		pos = wpa_ft_process_ric(sm, pos, end, parse.ric,
2810 					 parse.ric_len);
2811 		if (auth_alg == WLAN_AUTH_FT)
2812 			*elem_count +=
2813 				ieee802_11_ie_count(ric_start,
2814 						    pos - ric_start);
2815 	}
2816 	if (ric_start == pos)
2817 		ric_start = NULL;
2818 
2819 	if (omit_rsnxe) {
2820 		rsnxe_len = 0;
2821 	} else {
2822 		res = wpa_write_rsnxe(&sm->wpa_auth->conf, rsnxe,
2823 				      sizeof(rsnxe_buf));
2824 		if (res < 0) {
2825 			pos = NULL;
2826 			goto fail;
2827 		}
2828 		rsnxe_len = res;
2829 	}
2830 #ifdef CONFIG_TESTING_OPTIONS
2831 	if (auth_alg == WLAN_AUTH_FT &&
2832 	    sm->wpa_auth->conf.rsnxe_override_ft_set) {
2833 		wpa_printf(MSG_DEBUG,
2834 			   "TESTING: RSNXE FT override for MIC calculation");
2835 		rsnxe = sm->wpa_auth->conf.rsnxe_override_ft;
2836 		rsnxe_len = sm->wpa_auth->conf.rsnxe_override_ft_len;
2837 	}
2838 #endif /* CONFIG_TESTING_OPTIONS */
2839 	if (auth_alg == WLAN_AUTH_FT && rsnxe_len)
2840 		*elem_count += 1;
2841 
2842 	if (wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2843 		kck = sm->PTK.kck2;
2844 		kck_len = sm->PTK.kck2_len;
2845 	} else {
2846 		kck = sm->PTK.kck;
2847 		kck_len = sm->PTK.kck_len;
2848 	}
2849 	if (auth_alg == WLAN_AUTH_FT &&
2850 	    wpa_ft_mic(sm->wpa_key_mgmt, kck, kck_len,
2851 		       sm->addr, sm->wpa_auth->addr, 6,
2852 		       mdie, mdie_len, ftie, ftie_len,
2853 		       rsnie, rsnie_len,
2854 		       ric_start, ric_start ? pos - ric_start : 0,
2855 		       rsnxe_len ? rsnxe : NULL, rsnxe_len,
2856 		       NULL,
2857 		       fte_mic) < 0) {
2858 		wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
2859 		pos = NULL;
2860 		goto fail;
2861 	}
2862 
2863 	os_free(sm->assoc_resp_ftie);
2864 	sm->assoc_resp_ftie = os_malloc(ftie_len);
2865 	if (!sm->assoc_resp_ftie) {
2866 		pos = NULL;
2867 		goto fail;
2868 	}
2869 	os_memcpy(sm->assoc_resp_ftie, ftie, ftie_len);
2870 
2871 fail:
2872 	wpa_ft_parse_ies_free(&parse);
2873 	return pos;
2874 }
2875 
2876 
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,enum key_flag key_flag)2877 static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
2878 				   int vlan_id,
2879 				   enum wpa_alg alg, const u8 *addr, int idx,
2880 				   u8 *key, size_t key_len,
2881 				   enum key_flag key_flag)
2882 {
2883 	if (wpa_auth->cb->set_key == NULL)
2884 		return -1;
2885 	return wpa_auth->cb->set_key(wpa_auth->cb_ctx, vlan_id, alg, addr, idx,
2886 				     key, key_len, key_flag);
2887 }
2888 
2889 
2890 #ifdef CONFIG_PASN
wpa_auth_set_ltf_keyseed(struct wpa_authenticator * wpa_auth,const u8 * peer_addr,const u8 * ltf_keyseed,size_t ltf_keyseed_len)2891 static inline int wpa_auth_set_ltf_keyseed(struct wpa_authenticator *wpa_auth,
2892 					   const u8 *peer_addr,
2893 					   const u8 *ltf_keyseed,
2894 					   size_t ltf_keyseed_len)
2895 {
2896 	if (!wpa_auth->cb->set_ltf_keyseed)
2897 		return -1;
2898 	return wpa_auth->cb->set_ltf_keyseed(wpa_auth->cb_ctx, peer_addr,
2899 					     ltf_keyseed, ltf_keyseed_len);
2900 }
2901 #endif /* CONFIG_PASN */
2902 
2903 
wpa_auth_add_sta_ft(struct wpa_authenticator * wpa_auth,const u8 * addr)2904 static inline int wpa_auth_add_sta_ft(struct wpa_authenticator *wpa_auth,
2905 				      const u8 *addr)
2906 {
2907 	if (!wpa_auth->cb->add_sta_ft)
2908 		return -1;
2909 	return wpa_auth->cb->add_sta_ft(wpa_auth->cb_ctx, addr);
2910 }
2911 
2912 
wpa_ft_install_ptk(struct wpa_state_machine * sm,int retry)2913 void wpa_ft_install_ptk(struct wpa_state_machine *sm, int retry)
2914 {
2915 	enum wpa_alg alg;
2916 	int klen;
2917 
2918 	/* MLME-SETKEYS.request(PTK) */
2919 	alg = wpa_cipher_to_alg(sm->pairwise);
2920 	klen = wpa_cipher_key_len(sm->pairwise);
2921 	if (!wpa_cipher_valid_pairwise(sm->pairwise)) {
2922 		wpa_printf(MSG_DEBUG, "FT: Unknown pairwise alg 0x%x - skip "
2923 			   "PTK configuration", sm->pairwise);
2924 		return;
2925 	}
2926 
2927 	if (sm->tk_already_set) {
2928 		/* Must avoid TK reconfiguration to prevent clearing of TX/RX
2929 		 * PN in the driver */
2930 		wpa_printf(MSG_DEBUG,
2931 			   "FT: Do not re-install same PTK to the driver");
2932 		return;
2933 	}
2934 
2935 	if (!retry)
2936 		wpa_auth_add_sta_ft(sm->wpa_auth, sm->addr);
2937 
2938 	/* FIX: add STA entry to kernel/driver here? The set_key will fail
2939 	 * most likely without this.. At the moment, STA entry is added only
2940 	 * after association has been completed. This function will be called
2941 	 * again after association to get the PTK configured, but that could be
2942 	 * optimized by adding the STA entry earlier.
2943 	 */
2944 	if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, sm->keyidx_active,
2945 			     sm->PTK.tk, klen, KEY_FLAG_PAIRWISE_RX_TX))
2946 		return;
2947 
2948 #ifdef CONFIG_PASN
2949 	if (sm->wpa_auth->conf.secure_ltf &&
2950 	    ieee802_11_rsnx_capab(sm->rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF) &&
2951 	    wpa_auth_set_ltf_keyseed(sm->wpa_auth, sm->addr,
2952 				     sm->PTK.ltf_keyseed,
2953 				     sm->PTK.ltf_keyseed_len)) {
2954 		wpa_printf(MSG_ERROR,
2955 			   "FT: Failed to set LTF keyseed to driver");
2956 		return;
2957 	}
2958 #endif /* CONFIG_PASN */
2959 
2960 	/* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
2961 	sm->pairwise_set = true;
2962 	sm->tk_already_set = true;
2963 
2964 	wpa_auth_store_ptksa(sm->wpa_auth, sm->addr, sm->pairwise,
2965 			     dot11RSNAConfigPMKLifetime, &sm->PTK);
2966 }
2967 
2968 
2969 /* Derive PMK-R1 from PSK, check all available PSK */
wpa_ft_psk_pmk_r1(struct wpa_state_machine * sm,const u8 * req_pmk_r1_name,u8 * out_pmk_r1,int * out_pairwise,struct vlan_description * out_vlan,const u8 ** out_identity,size_t * out_identity_len,const u8 ** out_radius_cui,size_t * out_radius_cui_len,int * out_session_timeout)2970 static int wpa_ft_psk_pmk_r1(struct wpa_state_machine *sm,
2971 			     const u8 *req_pmk_r1_name,
2972 			     u8 *out_pmk_r1, int *out_pairwise,
2973 			     struct vlan_description *out_vlan,
2974 			     const u8 **out_identity, size_t *out_identity_len,
2975 			     const u8 **out_radius_cui,
2976 			     size_t *out_radius_cui_len,
2977 			     int *out_session_timeout)
2978 {
2979 	const u8 *pmk = NULL;
2980 	u8 pmk_r0[PMK_LEN], pmk_r0_name[WPA_PMK_NAME_LEN];
2981 	u8 pmk_r1[PMK_LEN], pmk_r1_name[WPA_PMK_NAME_LEN];
2982 	struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2983 	const u8 *mdid = wpa_auth->conf.mobility_domain;
2984 	const u8 *r0kh = sm->r0kh_id;
2985 	size_t r0kh_len = sm->r0kh_id_len;
2986 	const u8 *r1kh = wpa_auth->conf.r1_key_holder;
2987 	const u8 *ssid = wpa_auth->conf.ssid;
2988 	size_t ssid_len = wpa_auth->conf.ssid_len;
2989 	int pairwise;
2990 
2991 	pairwise = sm->pairwise;
2992 
2993 	for (;;) {
2994 		pmk = wpa_ft_get_psk(wpa_auth, sm->addr, sm->p2p_dev_addr,
2995 				     pmk);
2996 		if (pmk == NULL)
2997 			break;
2998 
2999 		if (wpa_derive_pmk_r0(pmk, PMK_LEN, ssid, ssid_len, mdid, r0kh,
3000 				      r0kh_len, sm->addr,
3001 				      pmk_r0, pmk_r0_name,
3002 				      WPA_KEY_MGMT_FT_PSK) < 0 ||
3003 		    wpa_derive_pmk_r1(pmk_r0, PMK_LEN, pmk_r0_name, r1kh,
3004 				      sm->addr, pmk_r1, pmk_r1_name) < 0 ||
3005 		    os_memcmp_const(pmk_r1_name, req_pmk_r1_name,
3006 				    WPA_PMK_NAME_LEN) != 0)
3007 			continue;
3008 
3009 		/* We found a PSK that matches the requested pmk_r1_name */
3010 		wpa_printf(MSG_DEBUG,
3011 			   "FT: Found PSK to generate PMK-R1 locally");
3012 		os_memcpy(out_pmk_r1, pmk_r1, PMK_LEN);
3013 		if (out_pairwise)
3014 			*out_pairwise = pairwise;
3015 		os_memcpy(sm->PMK, pmk, PMK_LEN);
3016 		sm->pmk_len = PMK_LEN;
3017 		if (out_vlan &&
3018 		    wpa_ft_get_vlan(sm->wpa_auth, sm->addr, out_vlan) < 0) {
3019 			wpa_printf(MSG_DEBUG, "FT: vlan not available for STA "
3020 				   MACSTR, MAC2STR(sm->addr));
3021 			return -1;
3022 		}
3023 
3024 		if (out_identity && out_identity_len) {
3025 			*out_identity_len = wpa_ft_get_identity(
3026 				sm->wpa_auth, sm->addr, out_identity);
3027 		}
3028 
3029 		if (out_radius_cui && out_radius_cui_len) {
3030 			*out_radius_cui_len = wpa_ft_get_radius_cui(
3031 				sm->wpa_auth, sm->addr, out_radius_cui);
3032 		}
3033 
3034 		if (out_session_timeout) {
3035 			*out_session_timeout = wpa_ft_get_session_timeout(
3036 				sm->wpa_auth, sm->addr);
3037 		}
3038 
3039 		return 0;
3040 	}
3041 
3042 	wpa_printf(MSG_DEBUG,
3043 		   "FT: Did not find PSK to generate PMK-R1 locally");
3044 	return -1;
3045 }
3046 
3047 
3048 /* Detect the configuration the station asked for.
3049  * Required to detect FT-PSK and pairwise cipher.
3050  */
wpa_ft_set_key_mgmt(struct wpa_state_machine * sm,struct wpa_ft_ies * parse)3051 static int wpa_ft_set_key_mgmt(struct wpa_state_machine *sm,
3052 			       struct wpa_ft_ies *parse)
3053 {
3054 	int key_mgmt, ciphers;
3055 
3056 	if (sm->wpa_key_mgmt)
3057 		return 0;
3058 
3059 	key_mgmt = parse->key_mgmt & sm->wpa_auth->conf.wpa_key_mgmt;
3060 	if (!key_mgmt) {
3061 		wpa_printf(MSG_DEBUG, "FT: Invalid key mgmt (0x%x) from "
3062 			   MACSTR, parse->key_mgmt, MAC2STR(sm->addr));
3063 		return -1;
3064 	}
3065 	if (key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
3066 		sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
3067 #ifdef CONFIG_SHA384
3068 	else if (key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X_SHA384)
3069 		sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X_SHA384;
3070 #endif /* CONFIG_SHA384 */
3071 	else if (key_mgmt & WPA_KEY_MGMT_FT_PSK)
3072 		sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_PSK;
3073 #ifdef CONFIG_FILS
3074 	else if (key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256)
3075 		sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_FILS_SHA256;
3076 	else if (key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384)
3077 		sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_FILS_SHA384;
3078 #endif /* CONFIG_FILS */
3079 	ciphers = parse->pairwise_cipher & sm->wpa_auth->conf.rsn_pairwise;
3080 	if (!ciphers) {
3081 		wpa_printf(MSG_DEBUG, "FT: Invalid pairwise cipher (0x%x) from "
3082 			   MACSTR,
3083 			   parse->pairwise_cipher, MAC2STR(sm->addr));
3084 		return -1;
3085 	}
3086 	sm->pairwise = wpa_pick_pairwise_cipher(ciphers, 0);
3087 
3088 	return 0;
3089 }
3090 
3091 
wpa_ft_local_derive_pmk_r1(struct wpa_authenticator * wpa_auth,struct wpa_state_machine * sm,const u8 * r0kh_id,size_t r0kh_id_len,const u8 * req_pmk_r0_name,u8 * out_pmk_r1_name,u8 * out_pmk_r1,int * out_pairwise,struct vlan_description * vlan,const u8 ** identity,size_t * identity_len,const u8 ** radius_cui,size_t * radius_cui_len,int * out_session_timeout,size_t * pmk_r1_len)3092 static int wpa_ft_local_derive_pmk_r1(struct wpa_authenticator *wpa_auth,
3093 				      struct wpa_state_machine *sm,
3094 				      const u8 *r0kh_id, size_t r0kh_id_len,
3095 				      const u8 *req_pmk_r0_name,
3096 				      u8 *out_pmk_r1_name,
3097 				      u8 *out_pmk_r1, int *out_pairwise,
3098 				      struct vlan_description *vlan,
3099 				      const u8 **identity, size_t *identity_len,
3100 				      const u8 **radius_cui,
3101 				      size_t *radius_cui_len,
3102 				      int *out_session_timeout,
3103 				      size_t *pmk_r1_len)
3104 {
3105 	struct wpa_auth_config *conf = &wpa_auth->conf;
3106 	const struct wpa_ft_pmk_r0_sa *r0;
3107 	int expires_in = 0;
3108 	int session_timeout = 0;
3109 	struct os_reltime now;
3110 
3111 	if (conf->r0_key_holder_len != r0kh_id_len ||
3112 	    os_memcmp(conf->r0_key_holder, r0kh_id, conf->r0_key_holder_len) !=
3113 	    0)
3114 		return -1; /* not our R0KH-ID */
3115 
3116 	wpa_printf(MSG_DEBUG, "FT: STA R0KH-ID matching local configuration");
3117 	if (wpa_ft_fetch_pmk_r0(sm->wpa_auth, sm->addr, req_pmk_r0_name, &r0) <
3118 	    0)
3119 		return -1; /* no matching PMKR0Name in local cache */
3120 
3121 	wpa_printf(MSG_DEBUG, "FT: Requested PMKR0Name found in local cache");
3122 
3123 	if (wpa_derive_pmk_r1(r0->pmk_r0, r0->pmk_r0_len, r0->pmk_r0_name,
3124 			      conf->r1_key_holder,
3125 			      sm->addr, out_pmk_r1, out_pmk_r1_name) < 0)
3126 		return -1;
3127 
3128 	os_get_reltime(&now);
3129 	if (r0->expiration)
3130 		expires_in = r0->expiration - now.sec;
3131 
3132 	if (r0->session_timeout)
3133 		session_timeout = r0->session_timeout - now.sec;
3134 
3135 	wpa_ft_store_pmk_r1(wpa_auth, sm->addr, out_pmk_r1, r0->pmk_r0_len,
3136 			    out_pmk_r1_name,
3137 			    sm->pairwise, r0->vlan, expires_in, session_timeout,
3138 			    r0->identity, r0->identity_len,
3139 			    r0->radius_cui, r0->radius_cui_len);
3140 
3141 	*out_pairwise = sm->pairwise;
3142 	if (vlan) {
3143 		if (r0->vlan)
3144 			*vlan = *r0->vlan;
3145 		else
3146 			os_memset(vlan, 0, sizeof(*vlan));
3147 	}
3148 
3149 	if (identity && identity_len) {
3150 		*identity = r0->identity;
3151 		*identity_len = r0->identity_len;
3152 	}
3153 
3154 	if (radius_cui && radius_cui_len) {
3155 		*radius_cui = r0->radius_cui;
3156 		*radius_cui_len = r0->radius_cui_len;
3157 	}
3158 
3159 	*out_session_timeout = session_timeout;
3160 
3161 	*pmk_r1_len = r0->pmk_r0_len;
3162 
3163 	return 0;
3164 }
3165 
3166 
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)3167 static int wpa_ft_process_auth_req(struct wpa_state_machine *sm,
3168 				   const u8 *ies, size_t ies_len,
3169 				   u8 **resp_ies, size_t *resp_ies_len)
3170 {
3171 	struct rsn_mdie *mdie;
3172 	u8 pmk_r1[PMK_LEN_MAX], pmk_r1_name[WPA_PMK_NAME_LEN];
3173 	u8 ptk_name[WPA_PMK_NAME_LEN];
3174 	struct wpa_auth_config *conf;
3175 	struct wpa_ft_ies parse;
3176 	size_t buflen;
3177 	int ret;
3178 	u8 *pos, *end;
3179 	int pairwise, session_timeout = 0;
3180 	struct vlan_description vlan;
3181 	const u8 *identity, *radius_cui;
3182 	size_t identity_len = 0, radius_cui_len = 0;
3183 	size_t pmk_r1_len, kdk_len, len;
3184 	int retval = WLAN_STATUS_UNSPECIFIED_FAILURE;
3185 
3186 	*resp_ies = NULL;
3187 	*resp_ies_len = 0;
3188 
3189 	sm->pmk_r1_name_valid = 0;
3190 	conf = &sm->wpa_auth->conf;
3191 
3192 	wpa_hexdump(MSG_DEBUG, "FT: Received authentication frame IEs",
3193 		    ies, ies_len);
3194 
3195 	if (wpa_ft_parse_ies(ies, ies_len, &parse, 0, false)) {
3196 		wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
3197 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
3198 	}
3199 
3200 	mdie = (struct rsn_mdie *) parse.mdie;
3201 	if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
3202 	    os_memcmp(mdie->mobility_domain,
3203 		      sm->wpa_auth->conf.mobility_domain,
3204 		      MOBILITY_DOMAIN_ID_LEN) != 0) {
3205 		wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
3206 		retval = WLAN_STATUS_INVALID_MDIE;
3207 		goto out;
3208 	}
3209 
3210 	if (!parse.ftie || parse.ftie_len < sizeof(struct rsn_ftie)) {
3211 		wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
3212 		retval = WLAN_STATUS_INVALID_FTIE;
3213 		goto out;
3214 	}
3215 
3216 	if (parse.r0kh_id == NULL) {
3217 		wpa_printf(MSG_DEBUG, "FT: Invalid FTIE - no R0KH-ID");
3218 		retval = WLAN_STATUS_INVALID_FTIE;
3219 		goto out;
3220 	}
3221 
3222 	wpa_hexdump(MSG_DEBUG, "FT: STA R0KH-ID",
3223 		    parse.r0kh_id, parse.r0kh_id_len);
3224 	os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
3225 	sm->r0kh_id_len = parse.r0kh_id_len;
3226 
3227 	if (parse.rsn_pmkid == NULL) {
3228 		wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
3229 		retval = WLAN_STATUS_INVALID_PMKID;
3230 		goto out;
3231 	}
3232 
3233 	if (wpa_ft_set_key_mgmt(sm, &parse) < 0)
3234 		goto out;
3235 
3236 	wpa_hexdump(MSG_DEBUG, "FT: Requested PMKR0Name",
3237 		    parse.rsn_pmkid, WPA_PMK_NAME_LEN);
3238 
3239 	if (conf->ft_psk_generate_local &&
3240 	    wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) {
3241 		if (wpa_derive_pmk_r1_name(parse.rsn_pmkid,
3242 					   sm->wpa_auth->conf.r1_key_holder,
3243 					   sm->addr, pmk_r1_name, PMK_LEN) < 0)
3244 			goto out;
3245 		if (wpa_ft_psk_pmk_r1(sm, pmk_r1_name, pmk_r1, &pairwise,
3246 				      &vlan, &identity, &identity_len,
3247 				      &radius_cui, &radius_cui_len,
3248 				      &session_timeout) < 0) {
3249 			retval = WLAN_STATUS_INVALID_PMKID;
3250 			goto out;
3251 		}
3252 		pmk_r1_len = PMK_LEN;
3253 		wpa_printf(MSG_DEBUG,
3254 			   "FT: Generated PMK-R1 for FT-PSK locally");
3255 		goto pmk_r1_derived;
3256 	}
3257 
3258 	/* Need to test all possible hash algorithms for FT-SAE-EXT-KEY since
3259 	 * the key length is not yet known. For other AKMs, only the length
3260 	 * identified by the AKM is used. */
3261 	for (len = SHA256_MAC_LEN; len <= SHA512_MAC_LEN; len += 16) {
3262 		if (parse.key_mgmt != WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
3263 		    ((wpa_key_mgmt_sha384(parse.key_mgmt) &&
3264 		      len != SHA384_MAC_LEN) ||
3265 		     (!wpa_key_mgmt_sha384(parse.key_mgmt) &&
3266 		      len != SHA256_MAC_LEN)))
3267 			continue;
3268 		if (wpa_derive_pmk_r1_name(parse.rsn_pmkid,
3269 					   sm->wpa_auth->conf.r1_key_holder,
3270 					   sm->addr, pmk_r1_name, len) < 0)
3271 			continue;
3272 
3273 		if (wpa_ft_fetch_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1_name,
3274 					pmk_r1, &pmk_r1_len, &pairwise, &vlan,
3275 					&identity, &identity_len, &radius_cui,
3276 					&radius_cui_len,
3277 					&session_timeout) == 0) {
3278 			wpa_printf(MSG_DEBUG,
3279 				   "FT: Found PMKR1Name (using SHA%zu) from local cache",
3280 				   pmk_r1_len * 8);
3281 			goto pmk_r1_derived;
3282 		}
3283 	}
3284 
3285 	wpa_printf(MSG_DEBUG,
3286 		   "FT: No PMK-R1 available in local cache for the requested PMKR1Name");
3287 	if (wpa_ft_local_derive_pmk_r1(sm->wpa_auth, sm,
3288 				       parse.r0kh_id, parse.r0kh_id_len,
3289 				       parse.rsn_pmkid,
3290 				       pmk_r1_name, pmk_r1, &pairwise,
3291 				       &vlan, &identity, &identity_len,
3292 				       &radius_cui, &radius_cui_len,
3293 				       &session_timeout, &pmk_r1_len) == 0) {
3294 		wpa_printf(MSG_DEBUG,
3295 			   "FT: Generated PMK-R1 based on local PMK-R0");
3296 		goto pmk_r1_derived;
3297 	}
3298 
3299 	if (wpa_ft_pull_pmk_r1(sm, ies, ies_len, parse.rsn_pmkid) < 0) {
3300 		wpa_printf(MSG_DEBUG,
3301 			   "FT: Did not have matching PMK-R1 and either unknown or blocked R0KH-ID or NAK from R0KH");
3302 		retval = WLAN_STATUS_INVALID_PMKID;
3303 		goto out;
3304 	}
3305 
3306 	retval = -1; /* Status pending */
3307 	goto out;
3308 
3309 pmk_r1_derived:
3310 	wpa_hexdump_key(MSG_DEBUG, "FT: Selected PMK-R1", pmk_r1, pmk_r1_len);
3311 	sm->pmk_r1_name_valid = 1;
3312 	os_memcpy(sm->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN);
3313 	os_memcpy(sm->pmk_r1, pmk_r1, pmk_r1_len);
3314 	sm->pmk_r1_len = pmk_r1_len;
3315 
3316 	if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
3317 		wpa_printf(MSG_DEBUG, "FT: Failed to get random data for "
3318 			   "ANonce");
3319 		goto out;
3320 	}
3321 
3322 	/* Now that we know the correct PMK-R1 length and as such, the length
3323 	 * of the MIC field, fetch the SNonce. */
3324 	if (pmk_r1_len == SHA512_MAC_LEN) {
3325 		const struct rsn_ftie_sha512 *ftie;
3326 
3327 		ftie = (const struct rsn_ftie_sha512 *) parse.ftie;
3328 		if (!ftie || parse.ftie_len < sizeof(*ftie)) {
3329 			wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
3330 			retval = WLAN_STATUS_INVALID_FTIE;
3331 			goto out;
3332 		}
3333 
3334 		os_memcpy(sm->SNonce, ftie->snonce, WPA_NONCE_LEN);
3335 	} else if (pmk_r1_len == SHA384_MAC_LEN) {
3336 		const struct rsn_ftie_sha384 *ftie;
3337 
3338 		ftie = (const struct rsn_ftie_sha384 *) parse.ftie;
3339 		if (!ftie || parse.ftie_len < sizeof(*ftie)) {
3340 			wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
3341 			retval = WLAN_STATUS_INVALID_FTIE;
3342 			goto out;
3343 		}
3344 
3345 		os_memcpy(sm->SNonce, ftie->snonce, WPA_NONCE_LEN);
3346 	} else {
3347 		const struct rsn_ftie *ftie;
3348 
3349 		ftie = (const struct rsn_ftie *) parse.ftie;
3350 		if (!ftie || parse.ftie_len < sizeof(*ftie)) {
3351 			wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
3352 			retval = WLAN_STATUS_INVALID_FTIE;
3353 			goto out;
3354 		}
3355 
3356 		os_memcpy(sm->SNonce, ftie->snonce, WPA_NONCE_LEN);
3357 	}
3358 
3359 	wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
3360 		    sm->SNonce, WPA_NONCE_LEN);
3361 	wpa_hexdump(MSG_DEBUG, "FT: Generated ANonce",
3362 		    sm->ANonce, WPA_NONCE_LEN);
3363 
3364 	if (sm->wpa_auth->conf.force_kdk_derivation ||
3365 	    (sm->wpa_auth->conf.secure_ltf &&
3366 	     ieee802_11_rsnx_capab(sm->rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
3367 		kdk_len = WPA_KDK_MAX_LEN;
3368 	else
3369 		kdk_len = 0;
3370 
3371 	if (wpa_pmk_r1_to_ptk(pmk_r1, pmk_r1_len, sm->SNonce, sm->ANonce,
3372 			      sm->addr, sm->wpa_auth->addr, pmk_r1_name,
3373 			      &sm->PTK, ptk_name, parse.key_mgmt,
3374 			      pairwise, kdk_len) < 0)
3375 		goto out;
3376 
3377 #ifdef CONFIG_PASN
3378 	if (sm->wpa_auth->conf.secure_ltf &&
3379 	    ieee802_11_rsnx_capab(sm->rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF) &&
3380 	    wpa_ltf_keyseed(&sm->PTK, parse.key_mgmt, pairwise)) {
3381 		wpa_printf(MSG_DEBUG, "FT: Failed to derive LTF keyseed");
3382 		goto out;
3383 	}
3384 #endif /* CONFIG_PASN */
3385 
3386 	sm->pairwise = pairwise;
3387 	sm->PTK_valid = true;
3388 	sm->tk_already_set = false;
3389 	wpa_ft_install_ptk(sm, 0);
3390 
3391 	if (wpa_ft_set_vlan(sm->wpa_auth, sm->addr, &vlan) < 0) {
3392 		wpa_printf(MSG_DEBUG, "FT: Failed to configure VLAN");
3393 		goto out;
3394 	}
3395 	if (wpa_ft_set_identity(sm->wpa_auth, sm->addr,
3396 				identity, identity_len) < 0 ||
3397 	    wpa_ft_set_radius_cui(sm->wpa_auth, sm->addr,
3398 				  radius_cui, radius_cui_len) < 0) {
3399 		wpa_printf(MSG_DEBUG, "FT: Failed to configure identity/CUI");
3400 		goto out;
3401 	}
3402 	wpa_ft_set_session_timeout(sm->wpa_auth, sm->addr, session_timeout);
3403 
3404 	buflen = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) +
3405 		2 + FT_R1KH_ID_LEN + 200;
3406 	*resp_ies = os_zalloc(buflen);
3407 	if (*resp_ies == NULL)
3408 		goto fail;
3409 
3410 	pos = *resp_ies;
3411 	end = *resp_ies + buflen;
3412 
3413 	ret = wpa_write_rsn_ie(conf, pos, end - pos, parse.rsn_pmkid);
3414 	if (ret < 0)
3415 		goto fail;
3416 	pos += ret;
3417 
3418 	ret = wpa_write_mdie(conf, pos, end - pos);
3419 	if (ret < 0)
3420 		goto fail;
3421 	pos += ret;
3422 
3423 	ret = wpa_write_ftie(conf, parse.key_mgmt, pmk_r1_len,
3424 			     parse.r0kh_id, parse.r0kh_id_len,
3425 			     sm->ANonce, sm->SNonce, pos, end - pos, NULL, 0,
3426 			     0);
3427 	if (ret < 0)
3428 		goto fail;
3429 	pos += ret;
3430 
3431 	*resp_ies_len = pos - *resp_ies;
3432 
3433 	retval = WLAN_STATUS_SUCCESS;
3434 	goto out;
3435 fail:
3436 	os_free(*resp_ies);
3437 	*resp_ies = NULL;
3438 out:
3439 	wpa_ft_parse_ies_free(&parse);
3440 	return retval;
3441 }
3442 
3443 
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)3444 void wpa_ft_process_auth(struct wpa_state_machine *sm, const u8 *bssid,
3445 			 u16 auth_transaction, const u8 *ies, size_t ies_len,
3446 			 void (*cb)(void *ctx, const u8 *dst, const u8 *bssid,
3447 				    u16 auth_transaction, u16 status,
3448 				    const u8 *ies, size_t ies_len),
3449 			 void *ctx)
3450 {
3451 	u16 status;
3452 	u8 *resp_ies;
3453 	size_t resp_ies_len;
3454 	int res;
3455 
3456 	if (sm == NULL) {
3457 		wpa_printf(MSG_DEBUG, "FT: Received authentication frame, but "
3458 			   "WPA SM not available");
3459 		return;
3460 	}
3461 
3462 	wpa_printf(MSG_DEBUG, "FT: Received authentication frame: STA=" MACSTR
3463 		   " BSSID=" MACSTR " transaction=%d",
3464 		   MAC2STR(sm->addr), MAC2STR(bssid), auth_transaction);
3465 	sm->ft_pending_cb = cb;
3466 	sm->ft_pending_cb_ctx = ctx;
3467 	sm->ft_pending_auth_transaction = auth_transaction;
3468 	sm->ft_pending_pull_left_retries = sm->wpa_auth->conf.rkh_pull_retries;
3469 	res = wpa_ft_process_auth_req(sm, ies, ies_len, &resp_ies,
3470 				      &resp_ies_len);
3471 	if (res < 0) {
3472 		wpa_printf(MSG_DEBUG, "FT: Callback postponed until response is available");
3473 		return;
3474 	}
3475 	status = res;
3476 
3477 	wpa_printf(MSG_DEBUG, "FT: FT authentication response: dst=" MACSTR
3478 		   " auth_transaction=%d status=%u (%s)",
3479 		   MAC2STR(sm->addr), auth_transaction + 1, status,
3480 		   status2str(status));
3481 	wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
3482 	cb(ctx, sm->addr, bssid, auth_transaction + 1, status,
3483 	   resp_ies, resp_ies_len);
3484 	os_free(resp_ies);
3485 }
3486 
3487 
wpa_ft_validate_reassoc(struct wpa_state_machine * sm,const u8 * ies,size_t ies_len)3488 int wpa_ft_validate_reassoc(struct wpa_state_machine *sm, const u8 *ies,
3489 			    size_t ies_len)
3490 {
3491 	struct wpa_ft_ies parse;
3492 	struct rsn_mdie *mdie;
3493 	u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
3494 	size_t mic_len;
3495 	unsigned int count;
3496 	const u8 *kck;
3497 	size_t kck_len;
3498 	struct wpa_auth_config *conf;
3499 	int retval = WLAN_STATUS_UNSPECIFIED_FAILURE;
3500 
3501 	if (sm == NULL)
3502 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
3503 
3504 	conf = &sm->wpa_auth->conf;
3505 
3506 	wpa_hexdump(MSG_DEBUG, "FT: Reassoc Req IEs", ies, ies_len);
3507 
3508 	if (wpa_ft_parse_ies(ies, ies_len, &parse, sm->wpa_key_mgmt,
3509 			     false) < 0) {
3510 		wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
3511 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
3512 	}
3513 
3514 	if (parse.rsn == NULL) {
3515 		wpa_printf(MSG_DEBUG, "FT: No RSNIE in Reassoc Req");
3516 		goto out;
3517 	}
3518 
3519 	if (parse.rsn_pmkid == NULL) {
3520 		wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
3521 		retval = WLAN_STATUS_INVALID_PMKID;
3522 		goto out;
3523 	}
3524 
3525 	if (os_memcmp_const(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN)
3526 	    != 0) {
3527 		wpa_printf(MSG_DEBUG, "FT: PMKID in Reassoc Req did not match "
3528 			   "with the PMKR1Name derived from auth request");
3529 		retval = WLAN_STATUS_INVALID_PMKID;
3530 		goto out;
3531 	}
3532 
3533 	mdie = (struct rsn_mdie *) parse.mdie;
3534 	if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
3535 	    os_memcmp(mdie->mobility_domain, conf->mobility_domain,
3536 		      MOBILITY_DOMAIN_ID_LEN) != 0) {
3537 		wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
3538 		retval = WLAN_STATUS_INVALID_MDIE;
3539 		goto out;
3540 	}
3541 
3542 	if (sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
3543 	    sm->pmk_r1_len == SHA512_MAC_LEN)
3544 		mic_len = 32;
3545 	else if ((sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY &&
3546 		  sm->pmk_r1_len == SHA384_MAC_LEN) ||
3547 		 wpa_key_mgmt_sha384(sm->wpa_key_mgmt))
3548 		mic_len = 24;
3549 	else
3550 		mic_len = 16;
3551 
3552 	if (!parse.ftie || !parse.fte_anonce || !parse.fte_snonce ||
3553 	    parse.fte_mic_len != mic_len) {
3554 		wpa_printf(MSG_DEBUG,
3555 			   "FT: Invalid FTE (fte_mic_len=%zu mic_len=%zu)",
3556 			   parse.fte_mic_len, mic_len);
3557 		retval = WLAN_STATUS_INVALID_FTIE;
3558 		goto out;
3559 	}
3560 
3561 	if (os_memcmp(parse.fte_snonce, sm->SNonce, WPA_NONCE_LEN) != 0) {
3562 		wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
3563 		wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
3564 			    parse.fte_snonce, WPA_NONCE_LEN);
3565 		wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
3566 			    sm->SNonce, WPA_NONCE_LEN);
3567 		retval = WLAN_STATUS_INVALID_FTIE;
3568 		goto out;
3569 	}
3570 
3571 	if (os_memcmp(parse.fte_anonce, sm->ANonce, WPA_NONCE_LEN) != 0) {
3572 		wpa_printf(MSG_DEBUG, "FT: ANonce mismatch in FTIE");
3573 		wpa_hexdump(MSG_DEBUG, "FT: Received ANonce",
3574 			    parse.fte_anonce, WPA_NONCE_LEN);
3575 		wpa_hexdump(MSG_DEBUG, "FT: Expected ANonce",
3576 			    sm->ANonce, WPA_NONCE_LEN);
3577 		retval = WLAN_STATUS_INVALID_FTIE;
3578 		goto out;
3579 	}
3580 
3581 	if (parse.r0kh_id == NULL) {
3582 		wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
3583 		retval = WLAN_STATUS_INVALID_FTIE;
3584 		goto out;
3585 	}
3586 
3587 	if (parse.r0kh_id_len != sm->r0kh_id_len ||
3588 	    os_memcmp_const(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0)
3589 	{
3590 		wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
3591 			   "the current R0KH-ID");
3592 		wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
3593 			    parse.r0kh_id, parse.r0kh_id_len);
3594 		wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
3595 			    sm->r0kh_id, sm->r0kh_id_len);
3596 		retval = WLAN_STATUS_INVALID_FTIE;
3597 		goto out;
3598 	}
3599 
3600 	if (parse.r1kh_id == NULL) {
3601 		wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
3602 		retval = WLAN_STATUS_INVALID_FTIE;
3603 		goto out;
3604 	}
3605 
3606 	if (os_memcmp_const(parse.r1kh_id, conf->r1_key_holder,
3607 			    FT_R1KH_ID_LEN) != 0) {
3608 		wpa_printf(MSG_DEBUG, "FT: Unknown R1KH-ID used in "
3609 			   "ReassocReq");
3610 		wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID in FTIE",
3611 			    parse.r1kh_id, FT_R1KH_ID_LEN);
3612 		wpa_hexdump(MSG_DEBUG, "FT: Expected R1KH-ID",
3613 			    conf->r1_key_holder, FT_R1KH_ID_LEN);
3614 		retval = WLAN_STATUS_INVALID_FTIE;
3615 		goto out;
3616 	}
3617 
3618 	if (parse.rsn_pmkid == NULL ||
3619 	    os_memcmp_const(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN))
3620 	{
3621 		wpa_printf(MSG_DEBUG, "FT: No matching PMKR1Name (PMKID) in "
3622 			   "RSNIE (pmkid=%d)", !!parse.rsn_pmkid);
3623 		retval = WLAN_STATUS_INVALID_PMKID;
3624 		goto out;
3625 	}
3626 
3627 	count = 3;
3628 	if (parse.ric)
3629 		count += ieee802_11_ie_count(parse.ric, parse.ric_len);
3630 	if (parse.rsnxe)
3631 		count++;
3632 	if (parse.fte_elem_count != count) {
3633 		wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in MIC "
3634 			   "Control: received %u expected %u",
3635 			   parse.fte_elem_count, count);
3636 		goto out;
3637 	}
3638 
3639 	if (wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
3640 		kck = sm->PTK.kck2;
3641 		kck_len = sm->PTK.kck2_len;
3642 	} else {
3643 		kck = sm->PTK.kck;
3644 		kck_len = sm->PTK.kck_len;
3645 	}
3646 	if (wpa_ft_mic(sm->wpa_key_mgmt, kck, kck_len,
3647 		       sm->addr, sm->wpa_auth->addr, 5,
3648 		       parse.mdie - 2, parse.mdie_len + 2,
3649 		       parse.ftie - 2, parse.ftie_len + 2,
3650 		       parse.rsn - 2, parse.rsn_len + 2,
3651 		       parse.ric, parse.ric_len,
3652 		       parse.rsnxe ? parse.rsnxe - 2 : NULL,
3653 		       parse.rsnxe ? parse.rsnxe_len + 2 : 0,
3654 		       NULL,
3655 		       mic) < 0) {
3656 		wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
3657 		goto out;
3658 	}
3659 
3660 	if (os_memcmp_const(mic, parse.fte_mic, mic_len) != 0) {
3661 		wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
3662 		wpa_printf(MSG_DEBUG, "FT: addr=" MACSTR " auth_addr=" MACSTR,
3663 			   MAC2STR(sm->addr), MAC2STR(sm->wpa_auth->addr));
3664 		wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC",
3665 			    parse.fte_mic, mic_len);
3666 		wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, mic_len);
3667 		wpa_hexdump(MSG_MSGDUMP, "FT: MDIE",
3668 			    parse.mdie - 2, parse.mdie_len + 2);
3669 		wpa_hexdump(MSG_MSGDUMP, "FT: FTIE",
3670 			    parse.ftie - 2, parse.ftie_len + 2);
3671 		wpa_hexdump(MSG_MSGDUMP, "FT: RSN",
3672 			    parse.rsn - 2, parse.rsn_len + 2);
3673 		wpa_hexdump(MSG_MSGDUMP, "FT: RSNXE",
3674 			    parse.rsnxe ? parse.rsnxe - 2 : NULL,
3675 			    parse.rsnxe ? parse.rsnxe_len + 2 : 0);
3676 		retval = WLAN_STATUS_INVALID_FTIE;
3677 		goto out;
3678 	}
3679 
3680 	if (parse.fte_rsnxe_used &&
3681 	    (conf->sae_pwe == SAE_PWE_HASH_TO_ELEMENT ||
3682 	     conf->sae_pwe == SAE_PWE_BOTH) &&
3683 	    !parse.rsnxe) {
3684 		wpa_printf(MSG_INFO,
3685 			   "FT: FTE indicated that STA uses RSNXE, but RSNXE was not included");
3686 		retval = -1; /* discard request */
3687 		goto out;
3688 	}
3689 
3690 #ifdef CONFIG_OCV
3691 	if (wpa_auth_uses_ocv(sm)) {
3692 		struct wpa_channel_info ci;
3693 		int tx_chanwidth;
3694 		int tx_seg1_idx;
3695 		enum oci_verify_result res;
3696 
3697 		if (wpa_channel_info(sm->wpa_auth, &ci) != 0) {
3698 			wpa_printf(MSG_WARNING,
3699 				   "Failed to get channel info to validate received OCI in (Re)Assoc Request");
3700 			goto out;
3701 		}
3702 
3703 		if (get_sta_tx_parameters(sm,
3704 					  channel_width_to_int(ci.chanwidth),
3705 					  ci.seg1_idx, &tx_chanwidth,
3706 					  &tx_seg1_idx) < 0)
3707 			goto out;
3708 
3709 		res = ocv_verify_tx_params(parse.oci, parse.oci_len, &ci,
3710 					   tx_chanwidth, tx_seg1_idx);
3711 		if (wpa_auth_uses_ocv(sm) == 2 && res == OCI_NOT_FOUND) {
3712 			/* Work around misbehaving STAs */
3713 			wpa_printf(MSG_INFO,
3714 				   "Disable OCV with a STA that does not send OCI");
3715 			wpa_auth_set_ocv(sm, 0);
3716 		} else if (res != OCI_SUCCESS) {
3717 			wpa_printf(MSG_WARNING, "OCV failed: %s", ocv_errorstr);
3718 			if (sm->wpa_auth->conf.msg_ctx)
3719 				wpa_msg(sm->wpa_auth->conf.msg_ctx, MSG_INFO,
3720 					OCV_FAILURE "addr=" MACSTR
3721 					" frame=ft-reassoc-req error=%s",
3722 					MAC2STR(sm->addr), ocv_errorstr);
3723 			retval = WLAN_STATUS_INVALID_FTIE;
3724 			goto out;
3725 		}
3726 	}
3727 #endif /* CONFIG_OCV */
3728 
3729 	retval = WLAN_STATUS_SUCCESS;
3730 out:
3731 	wpa_ft_parse_ies_free(&parse);
3732 	return retval;
3733 }
3734 
3735 
wpa_ft_action_rx(struct wpa_state_machine * sm,const u8 * data,size_t len)3736 int wpa_ft_action_rx(struct wpa_state_machine *sm, const u8 *data, size_t len)
3737 {
3738 	const u8 *sta_addr, *target_ap;
3739 	const u8 *ies;
3740 	size_t ies_len;
3741 	u8 action;
3742 	struct ft_rrb_frame *frame;
3743 
3744 	if (sm == NULL)
3745 		return -1;
3746 
3747 	/*
3748 	 * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
3749 	 * FT Request action frame body[variable]
3750 	 */
3751 
3752 	if (len < 14) {
3753 		wpa_printf(MSG_DEBUG, "FT: Too short FT Action frame "
3754 			   "(len=%lu)", (unsigned long) len);
3755 		return -1;
3756 	}
3757 
3758 	action = data[1];
3759 	sta_addr = data + 2;
3760 	target_ap = data + 8;
3761 	ies = data + 14;
3762 	ies_len = len - 14;
3763 
3764 	wpa_printf(MSG_DEBUG, "FT: Received FT Action frame (STA=" MACSTR
3765 		   " Target AP=" MACSTR " Action=%d)",
3766 		   MAC2STR(sta_addr), MAC2STR(target_ap), action);
3767 
3768 	if (os_memcmp(sta_addr, sm->addr, ETH_ALEN) != 0) {
3769 		wpa_printf(MSG_DEBUG, "FT: Mismatch in FT Action STA address: "
3770 			   "STA=" MACSTR " STA-Address=" MACSTR,
3771 			   MAC2STR(sm->addr), MAC2STR(sta_addr));
3772 		return -1;
3773 	}
3774 
3775 	/*
3776 	 * Do some validity checking on the target AP address (not own and not
3777 	 * broadcast. This could be extended to filter based on a list of known
3778 	 * APs in the MD (if such a list were configured).
3779 	 */
3780 	if ((target_ap[0] & 0x01) ||
3781 	    os_memcmp(target_ap, sm->wpa_auth->addr, ETH_ALEN) == 0) {
3782 		wpa_printf(MSG_DEBUG, "FT: Invalid Target AP in FT Action "
3783 			   "frame");
3784 		return -1;
3785 	}
3786 
3787 	wpa_hexdump(MSG_MSGDUMP, "FT: Action frame body", ies, ies_len);
3788 
3789 	if (!sm->wpa_auth->conf.ft_over_ds) {
3790 		wpa_printf(MSG_DEBUG, "FT: Over-DS option disabled - reject");
3791 		return -1;
3792 	}
3793 
3794 	/* RRB - Forward action frame to the target AP */
3795 	frame = os_malloc(sizeof(*frame) + len);
3796 	if (frame == NULL)
3797 		return -1;
3798 	frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
3799 	frame->packet_type = FT_PACKET_REQUEST;
3800 	frame->action_length = host_to_le16(len);
3801 	os_memcpy(frame->ap_address, sm->wpa_auth->addr, ETH_ALEN);
3802 	os_memcpy(frame + 1, data, len);
3803 
3804 	wpa_ft_rrb_send(sm->wpa_auth, target_ap, (u8 *) frame,
3805 			sizeof(*frame) + len);
3806 	os_free(frame);
3807 
3808 	return 0;
3809 }
3810 
3811 
wpa_ft_rrb_rx_request_cb(void * ctx,const u8 * dst,const u8 * bssid,u16 auth_transaction,u16 resp,const u8 * ies,size_t ies_len)3812 static void wpa_ft_rrb_rx_request_cb(void *ctx, const u8 *dst, const u8 *bssid,
3813 				     u16 auth_transaction, u16 resp,
3814 				     const u8 *ies, size_t ies_len)
3815 {
3816 	struct wpa_state_machine *sm = ctx;
3817 	wpa_printf(MSG_DEBUG, "FT: Over-the-DS RX request cb for " MACSTR,
3818 		   MAC2STR(sm->addr));
3819 	wpa_ft_send_rrb_auth_resp(sm, sm->ft_pending_current_ap, sm->addr,
3820 				  WLAN_STATUS_SUCCESS, ies, ies_len);
3821 }
3822 
3823 
wpa_ft_rrb_rx_request(struct wpa_authenticator * wpa_auth,const u8 * current_ap,const u8 * sta_addr,const u8 * body,size_t len)3824 static int wpa_ft_rrb_rx_request(struct wpa_authenticator *wpa_auth,
3825 				 const u8 *current_ap, const u8 *sta_addr,
3826 				 const u8 *body, size_t len)
3827 {
3828 	struct wpa_state_machine *sm;
3829 	u16 status;
3830 	u8 *resp_ies;
3831 	size_t resp_ies_len;
3832 	int res;
3833 
3834 	sm = wpa_ft_add_sta(wpa_auth, sta_addr);
3835 	if (sm == NULL) {
3836 		wpa_printf(MSG_DEBUG, "FT: Failed to add new STA based on "
3837 			   "RRB Request");
3838 		return -1;
3839 	}
3840 
3841 	wpa_hexdump(MSG_MSGDUMP, "FT: RRB Request Frame body", body, len);
3842 
3843 	sm->ft_pending_cb = wpa_ft_rrb_rx_request_cb;
3844 	sm->ft_pending_cb_ctx = sm;
3845 	os_memcpy(sm->ft_pending_current_ap, current_ap, ETH_ALEN);
3846 	sm->ft_pending_pull_left_retries = sm->wpa_auth->conf.rkh_pull_retries;
3847 	res = wpa_ft_process_auth_req(sm, body, len, &resp_ies,
3848 				      &resp_ies_len);
3849 	if (res < 0) {
3850 		wpa_printf(MSG_DEBUG, "FT: No immediate response available - wait for pull response");
3851 		return 0;
3852 	}
3853 	status = res;
3854 
3855 	res = wpa_ft_send_rrb_auth_resp(sm, current_ap, sta_addr, status,
3856 					resp_ies, resp_ies_len);
3857 	os_free(resp_ies);
3858 	return res;
3859 }
3860 
3861 
wpa_ft_send_rrb_auth_resp(struct wpa_state_machine * sm,const u8 * current_ap,const u8 * sta_addr,u16 status,const u8 * resp_ies,size_t resp_ies_len)3862 static int wpa_ft_send_rrb_auth_resp(struct wpa_state_machine *sm,
3863 				     const u8 *current_ap, const u8 *sta_addr,
3864 				     u16 status, const u8 *resp_ies,
3865 				     size_t resp_ies_len)
3866 {
3867 	struct wpa_authenticator *wpa_auth = sm->wpa_auth;
3868 	size_t rlen;
3869 	struct ft_rrb_frame *frame;
3870 	u8 *pos;
3871 
3872 	wpa_printf(MSG_DEBUG, "FT: RRB authentication response: STA=" MACSTR
3873 		   " CurrentAP=" MACSTR " status=%u (%s)",
3874 		   MAC2STR(sm->addr), MAC2STR(current_ap), status,
3875 		   status2str(status));
3876 	wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
3877 
3878 	/* RRB - Forward action frame response to the Current AP */
3879 
3880 	/*
3881 	 * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
3882 	 * Status_Code[2] FT Request action frame body[variable]
3883 	 */
3884 	rlen = 2 + 2 * ETH_ALEN + 2 + resp_ies_len;
3885 
3886 	frame = os_malloc(sizeof(*frame) + rlen);
3887 	if (frame == NULL)
3888 		return -1;
3889 	frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
3890 	frame->packet_type = FT_PACKET_RESPONSE;
3891 	frame->action_length = host_to_le16(rlen);
3892 	os_memcpy(frame->ap_address, wpa_auth->addr, ETH_ALEN);
3893 	pos = (u8 *) (frame + 1);
3894 	*pos++ = WLAN_ACTION_FT;
3895 	*pos++ = 2; /* Action: Response */
3896 	os_memcpy(pos, sta_addr, ETH_ALEN);
3897 	pos += ETH_ALEN;
3898 	os_memcpy(pos, wpa_auth->addr, ETH_ALEN);
3899 	pos += ETH_ALEN;
3900 	WPA_PUT_LE16(pos, status);
3901 	pos += 2;
3902 	if (resp_ies)
3903 		os_memcpy(pos, resp_ies, resp_ies_len);
3904 
3905 	wpa_ft_rrb_send(wpa_auth, current_ap, (u8 *) frame,
3906 			sizeof(*frame) + rlen);
3907 	os_free(frame);
3908 
3909 	return 0;
3910 }
3911 
3912 
wpa_ft_rrb_build_r0(const u8 * key,const size_t key_len,const struct tlv_list * tlvs,const struct wpa_ft_pmk_r0_sa * pmk_r0,const u8 * r1kh_id,const u8 * s1kh_id,const struct tlv_list * tlv_auth,const u8 * src_addr,u8 type,u8 ** packet,size_t * packet_len)3913 static int wpa_ft_rrb_build_r0(const u8 *key, const size_t key_len,
3914 			       const struct tlv_list *tlvs,
3915 			       const struct wpa_ft_pmk_r0_sa *pmk_r0,
3916 			       const u8 *r1kh_id, const u8 *s1kh_id,
3917 			       const struct tlv_list *tlv_auth,
3918 			       const u8 *src_addr, u8 type,
3919 			       u8 **packet, size_t *packet_len)
3920 {
3921 	u8 pmk_r1[PMK_LEN_MAX];
3922 	size_t pmk_r1_len = pmk_r0->pmk_r0_len;
3923 	u8 pmk_r1_name[WPA_PMK_NAME_LEN];
3924 	u8 f_pairwise[sizeof(le16)];
3925 	u8 f_expires_in[sizeof(le16)];
3926 	u8 f_session_timeout[sizeof(le32)];
3927 	int expires_in;
3928 	int session_timeout;
3929 	struct os_reltime now;
3930 	int ret;
3931 	struct tlv_list sess_tlv[] = {
3932 		{ .type = FT_RRB_PMK_R1, .len = pmk_r1_len,
3933 		  .data = pmk_r1 },
3934 		{ .type = FT_RRB_PMK_R1_NAME, .len = sizeof(pmk_r1_name),
3935 		  .data = pmk_r1_name },
3936 		{ .type = FT_RRB_PAIRWISE, .len = sizeof(f_pairwise),
3937 		  .data = f_pairwise },
3938 		{ .type = FT_RRB_EXPIRES_IN, .len = sizeof(f_expires_in),
3939 		  .data = f_expires_in },
3940 		{ .type = FT_RRB_IDENTITY, .len = pmk_r0->identity_len,
3941 		  .data = pmk_r0->identity },
3942 		{ .type = FT_RRB_RADIUS_CUI, .len = pmk_r0->radius_cui_len,
3943 		  .data = pmk_r0->radius_cui },
3944 		{ .type = FT_RRB_SESSION_TIMEOUT,
3945 		  .len = sizeof(f_session_timeout),
3946 		  .data = f_session_timeout },
3947 		{ .type = FT_RRB_LAST_EMPTY, .len = 0, .data = NULL },
3948 	};
3949 
3950 	wpa_printf(MSG_DEBUG, "FT: Derive PMK-R1 for peer AP");
3951 	if (wpa_derive_pmk_r1(pmk_r0->pmk_r0, pmk_r0->pmk_r0_len,
3952 			      pmk_r0->pmk_r0_name, r1kh_id,
3953 			      s1kh_id, pmk_r1, pmk_r1_name) < 0)
3954 		return -1;
3955 	WPA_PUT_LE16(f_pairwise, pmk_r0->pairwise);
3956 
3957 	os_get_reltime(&now);
3958 	if (pmk_r0->expiration > now.sec)
3959 		expires_in = pmk_r0->expiration - now.sec;
3960 	else if (pmk_r0->expiration)
3961 		expires_in = 1;
3962 	else
3963 		expires_in = 0;
3964 	WPA_PUT_LE16(f_expires_in, expires_in);
3965 
3966 	if (pmk_r0->session_timeout > now.sec)
3967 		session_timeout = pmk_r0->session_timeout - now.sec;
3968 	else if (pmk_r0->session_timeout)
3969 		session_timeout = 1;
3970 	else
3971 		session_timeout = 0;
3972 	WPA_PUT_LE32(f_session_timeout, session_timeout);
3973 
3974 	ret = wpa_ft_rrb_build(key, key_len, tlvs, sess_tlv, tlv_auth,
3975 			       pmk_r0->vlan, src_addr, type,
3976 			       packet, packet_len);
3977 
3978 	forced_memzero(pmk_r1, sizeof(pmk_r1));
3979 
3980 	return ret;
3981 }
3982 
3983 
wpa_ft_rrb_rx_pull(struct wpa_authenticator * wpa_auth,const u8 * src_addr,const u8 * enc,size_t enc_len,const u8 * auth,size_t auth_len,int no_defer)3984 static int wpa_ft_rrb_rx_pull(struct wpa_authenticator *wpa_auth,
3985 			      const u8 *src_addr,
3986 			      const u8 *enc, size_t enc_len,
3987 			      const u8 *auth, size_t auth_len,
3988 			      int no_defer)
3989 {
3990 	const char *msgtype = "pull request";
3991 	u8 *plain = NULL, *packet = NULL;
3992 	size_t plain_len = 0, packet_len = 0;
3993 	struct ft_remote_r1kh *r1kh, *r1kh_wildcard;
3994 	const u8 *key;
3995 	size_t key_len;
3996 	int seq_ret;
3997 	const u8 *f_nonce, *f_r0kh_id, *f_r1kh_id, *f_s1kh_id, *f_pmk_r0_name;
3998 	size_t f_nonce_len, f_r0kh_id_len, f_r1kh_id_len, f_s1kh_id_len;
3999 	size_t f_pmk_r0_name_len;
4000 	const struct wpa_ft_pmk_r0_sa *r0;
4001 	int ret;
4002 	struct tlv_list resp[2];
4003 	struct tlv_list resp_auth[5];
4004 	struct ft_rrb_seq f_seq;
4005 
4006 	wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull");
4007 
4008 	RRB_GET_AUTH(FT_RRB_R0KH_ID, r0kh_id, msgtype, -1);
4009 	wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID", f_r0kh_id, f_r0kh_id_len);
4010 
4011 	if (wpa_ft_rrb_check_r0kh(wpa_auth, f_r0kh_id, f_r0kh_id_len)) {
4012 		wpa_printf(MSG_DEBUG, "FT: R0KH-ID mismatch");
4013 		goto out;
4014 	}
4015 
4016 	RRB_GET_AUTH(FT_RRB_R1KH_ID, r1kh_id, msgtype, FT_R1KH_ID_LEN);
4017 	wpa_printf(MSG_DEBUG, "FT: R1KH-ID=" MACSTR, MAC2STR(f_r1kh_id));
4018 
4019 	wpa_ft_rrb_lookup_r1kh(wpa_auth, f_r1kh_id, &r1kh, &r1kh_wildcard);
4020 	if (r1kh) {
4021 		key = r1kh->key;
4022 		key_len = sizeof(r1kh->key);
4023 	} else if (r1kh_wildcard) {
4024 		wpa_printf(MSG_DEBUG, "FT: Using wildcard R1KH-ID");
4025 		key = r1kh_wildcard->key;
4026 		key_len = sizeof(r1kh_wildcard->key);
4027 	} else {
4028 		goto out;
4029 	}
4030 
4031 	RRB_GET_AUTH(FT_RRB_NONCE, nonce, "pull request", FT_RRB_NONCE_LEN);
4032 	wpa_hexdump(MSG_DEBUG, "FT: nonce", f_nonce, f_nonce_len);
4033 
4034 	seq_ret = FT_RRB_SEQ_DROP;
4035 	if (r1kh)
4036 		seq_ret = wpa_ft_rrb_seq_chk(r1kh->seq, src_addr, enc, enc_len,
4037 					     auth, auth_len, msgtype, no_defer);
4038 	if (!no_defer && r1kh_wildcard &&
4039 	    (!r1kh || os_memcmp(r1kh->addr, src_addr, ETH_ALEN) != 0)) {
4040 		/* wildcard: r1kh-id unknown or changed addr -> do a seq req */
4041 		seq_ret = FT_RRB_SEQ_DEFER;
4042 	}
4043 
4044 	if (seq_ret == FT_RRB_SEQ_DROP)
4045 		goto out;
4046 
4047 	if (wpa_ft_rrb_decrypt(key, key_len, enc, enc_len, auth, auth_len,
4048 			       src_addr, FT_PACKET_R0KH_R1KH_PULL,
4049 			       &plain, &plain_len) < 0)
4050 		goto out;
4051 
4052 	if (!r1kh)
4053 		r1kh = wpa_ft_rrb_add_r1kh(wpa_auth, r1kh_wildcard, src_addr,
4054 					   f_r1kh_id,
4055 					   wpa_auth->conf.rkh_pos_timeout);
4056 	if (!r1kh)
4057 		goto out;
4058 
4059 	if (seq_ret == FT_RRB_SEQ_DEFER) {
4060 		wpa_ft_rrb_seq_req(wpa_auth, r1kh->seq, src_addr, f_r0kh_id,
4061 				   f_r0kh_id_len, f_r1kh_id, key, key_len,
4062 				   enc, enc_len, auth, auth_len,
4063 				   &wpa_ft_rrb_rx_pull);
4064 		goto out;
4065 	}
4066 
4067 	wpa_ft_rrb_seq_accept(wpa_auth, r1kh->seq, src_addr, auth, auth_len,
4068 			      msgtype);
4069 	wpa_ft_rrb_r1kh_replenish(wpa_auth, r1kh,
4070 				  wpa_auth->conf.rkh_pos_timeout);
4071 
4072 	RRB_GET(FT_RRB_PMK_R0_NAME, pmk_r0_name, msgtype, WPA_PMK_NAME_LEN);
4073 	wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name", f_pmk_r0_name,
4074 		    f_pmk_r0_name_len);
4075 
4076 	RRB_GET(FT_RRB_S1KH_ID, s1kh_id, msgtype, ETH_ALEN);
4077 	wpa_printf(MSG_DEBUG, "FT: S1KH-ID=" MACSTR, MAC2STR(f_s1kh_id));
4078 
4079 	if (wpa_ft_new_seq(r1kh->seq, &f_seq) < 0) {
4080 		wpa_printf(MSG_DEBUG, "FT: Failed to get seq num");
4081 		goto out;
4082 	}
4083 
4084 	wpa_printf(MSG_DEBUG, "FT: Send PMK-R1 pull response from " MACSTR
4085 		   " to " MACSTR,
4086 		   MAC2STR(wpa_auth->addr), MAC2STR(src_addr));
4087 
4088 	resp[0].type = FT_RRB_S1KH_ID;
4089 	resp[0].len = f_s1kh_id_len;
4090 	resp[0].data = f_s1kh_id;
4091 	resp[1].type = FT_RRB_LAST_EMPTY;
4092 	resp[1].len = 0;
4093 	resp[1].data = NULL;
4094 
4095 	resp_auth[0].type = FT_RRB_NONCE;
4096 	resp_auth[0].len = f_nonce_len;
4097 	resp_auth[0].data = f_nonce;
4098 	resp_auth[1].type = FT_RRB_SEQ;
4099 	resp_auth[1].len = sizeof(f_seq);
4100 	resp_auth[1].data = (u8 *) &f_seq;
4101 	resp_auth[2].type = FT_RRB_R0KH_ID;
4102 	resp_auth[2].len = f_r0kh_id_len;
4103 	resp_auth[2].data = f_r0kh_id;
4104 	resp_auth[3].type = FT_RRB_R1KH_ID;
4105 	resp_auth[3].len = f_r1kh_id_len;
4106 	resp_auth[3].data = f_r1kh_id;
4107 	resp_auth[4].type = FT_RRB_LAST_EMPTY;
4108 	resp_auth[4].len = 0;
4109 	resp_auth[4].data = NULL;
4110 
4111 	if (wpa_ft_fetch_pmk_r0(wpa_auth, f_s1kh_id, f_pmk_r0_name, &r0) < 0) {
4112 		wpa_printf(MSG_DEBUG, "FT: No matching PMK-R0-Name found");
4113 		ret = wpa_ft_rrb_build(key, key_len, resp, NULL, resp_auth,
4114 				       NULL, wpa_auth->addr,
4115 				       FT_PACKET_R0KH_R1KH_RESP,
4116 				       &packet, &packet_len);
4117 	} else {
4118 		ret = wpa_ft_rrb_build_r0(key, key_len, resp, r0, f_r1kh_id,
4119 					  f_s1kh_id, resp_auth, wpa_auth->addr,
4120 					  FT_PACKET_R0KH_R1KH_RESP,
4121 					  &packet, &packet_len);
4122 	}
4123 
4124 	if (!ret)
4125 		wpa_ft_rrb_oui_send(wpa_auth, src_addr,
4126 				    FT_PACKET_R0KH_R1KH_RESP, packet,
4127 				    packet_len);
4128 
4129 out:
4130 	os_free(plain);
4131 	os_free(packet);
4132 
4133 	return 0;
4134 }
4135 
4136 
4137 /* @returns  0 on success
4138  *          -1 on error
4139  *          -2 if FR_RRB_PAIRWISE is missing
4140  */
wpa_ft_rrb_rx_r1(struct wpa_authenticator * wpa_auth,const u8 * src_addr,u8 type,const u8 * enc,size_t enc_len,const u8 * auth,size_t auth_len,const char * msgtype,u8 * s1kh_id_out,int (* cb)(struct wpa_authenticator * wpa_auth,const u8 * src_addr,const u8 * enc,size_t enc_len,const u8 * auth,size_t auth_len,int no_defer))4141 static int wpa_ft_rrb_rx_r1(struct wpa_authenticator *wpa_auth,
4142 			    const u8 *src_addr, u8 type,
4143 			    const u8 *enc, size_t enc_len,
4144 			    const u8 *auth, size_t auth_len,
4145 			    const char *msgtype, u8 *s1kh_id_out,
4146 			    int (*cb)(struct wpa_authenticator *wpa_auth,
4147 				      const u8 *src_addr,
4148 				      const u8 *enc, size_t enc_len,
4149 				      const u8 *auth, size_t auth_len,
4150 				      int no_defer))
4151 {
4152 	u8 *plain = NULL;
4153 	size_t plain_len = 0;
4154 	struct ft_remote_r0kh *r0kh, *r0kh_wildcard;
4155 	const u8 *key;
4156 	size_t key_len;
4157 	int seq_ret;
4158 	const u8 *f_r1kh_id, *f_s1kh_id, *f_r0kh_id;
4159 	const u8 *f_pmk_r1_name, *f_pairwise, *f_pmk_r1;
4160 	const u8 *f_expires_in;
4161 	size_t f_r1kh_id_len, f_s1kh_id_len, f_r0kh_id_len;
4162 	const u8 *f_identity, *f_radius_cui;
4163 	const u8 *f_session_timeout;
4164 	size_t f_pmk_r1_name_len, f_pairwise_len, f_pmk_r1_len;
4165 	size_t f_expires_in_len;
4166 	size_t f_identity_len, f_radius_cui_len;
4167 	size_t f_session_timeout_len;
4168 	int pairwise;
4169 	int ret = -1;
4170 	int expires_in;
4171 	int session_timeout;
4172 	struct vlan_description vlan;
4173 	size_t pmk_r1_len;
4174 
4175 	RRB_GET_AUTH(FT_RRB_R0KH_ID, r0kh_id, msgtype, -1);
4176 	wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID", f_r0kh_id, f_r0kh_id_len);
4177 
4178 	RRB_GET_AUTH(FT_RRB_R1KH_ID, r1kh_id, msgtype, FT_R1KH_ID_LEN);
4179 	wpa_printf(MSG_DEBUG, "FT: R1KH-ID=" MACSTR, MAC2STR(f_r1kh_id));
4180 
4181 	if (wpa_ft_rrb_check_r1kh(wpa_auth, f_r1kh_id)) {
4182 		wpa_printf(MSG_DEBUG, "FT: R1KH-ID mismatch");
4183 		goto out;
4184 	}
4185 
4186 	wpa_ft_rrb_lookup_r0kh(wpa_auth, f_r0kh_id, f_r0kh_id_len, &r0kh,
4187 			       &r0kh_wildcard);
4188 	if (r0kh) {
4189 		key = r0kh->key;
4190 		key_len = sizeof(r0kh->key);
4191 	} else if (r0kh_wildcard) {
4192 		wpa_printf(MSG_DEBUG, "FT: Using wildcard R0KH-ID");
4193 		key = r0kh_wildcard->key;
4194 		key_len = sizeof(r0kh_wildcard->key);
4195 	} else {
4196 		goto out;
4197 	}
4198 
4199 	seq_ret = FT_RRB_SEQ_DROP;
4200 	if (r0kh) {
4201 		seq_ret = wpa_ft_rrb_seq_chk(r0kh->seq, src_addr, enc, enc_len,
4202 					     auth, auth_len, msgtype,
4203 					     cb ? 0 : 1);
4204 	}
4205 	if (cb && r0kh_wildcard &&
4206 	    (!r0kh || os_memcmp(r0kh->addr, src_addr, ETH_ALEN) != 0)) {
4207 		/* wildcard: r0kh-id unknown or changed addr -> do a seq req */
4208 		seq_ret = FT_RRB_SEQ_DEFER;
4209 	}
4210 
4211 	if (seq_ret == FT_RRB_SEQ_DROP)
4212 		goto out;
4213 
4214 	if (wpa_ft_rrb_decrypt(key, key_len, enc, enc_len, auth, auth_len,
4215 			       src_addr, type, &plain, &plain_len) < 0)
4216 		goto out;
4217 
4218 	if (!r0kh)
4219 		r0kh = wpa_ft_rrb_add_r0kh(wpa_auth, r0kh_wildcard, src_addr,
4220 					   f_r0kh_id, f_r0kh_id_len,
4221 					   wpa_auth->conf.rkh_pos_timeout);
4222 	if (!r0kh)
4223 		goto out;
4224 
4225 	if (seq_ret == FT_RRB_SEQ_DEFER) {
4226 		wpa_ft_rrb_seq_req(wpa_auth, r0kh->seq, src_addr, f_r0kh_id,
4227 				   f_r0kh_id_len, f_r1kh_id, key, key_len,
4228 				   enc, enc_len, auth, auth_len, cb);
4229 		goto out;
4230 	}
4231 
4232 	wpa_ft_rrb_seq_accept(wpa_auth, r0kh->seq, src_addr, auth, auth_len,
4233 			      msgtype);
4234 	wpa_ft_rrb_r0kh_replenish(wpa_auth, r0kh,
4235 				  wpa_auth->conf.rkh_pos_timeout);
4236 
4237 	RRB_GET(FT_RRB_S1KH_ID, s1kh_id, msgtype, ETH_ALEN);
4238 	wpa_printf(MSG_DEBUG, "FT: S1KH-ID=" MACSTR, MAC2STR(f_s1kh_id));
4239 
4240 	if (s1kh_id_out)
4241 		os_memcpy(s1kh_id_out, f_s1kh_id, ETH_ALEN);
4242 
4243 	ret = -2;
4244 	RRB_GET(FT_RRB_PAIRWISE, pairwise, msgtype, sizeof(le16));
4245 	wpa_hexdump(MSG_DEBUG, "FT: pairwise", f_pairwise, f_pairwise_len);
4246 
4247 	ret = -1;
4248 	RRB_GET(FT_RRB_PMK_R1_NAME, pmk_r1_name, msgtype, WPA_PMK_NAME_LEN);
4249 	wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name",
4250 		    f_pmk_r1_name, WPA_PMK_NAME_LEN);
4251 
4252 	pmk_r1_len = PMK_LEN;
4253 	if (wpa_ft_rrb_get_tlv(plain, plain_len, FT_RRB_PMK_R1, &f_pmk_r1_len,
4254 			       &f_pmk_r1) == 0 &&
4255 	    (f_pmk_r1_len == PMK_LEN || f_pmk_r1_len == SHA384_MAC_LEN ||
4256 	     f_pmk_r1_len == SHA512_MAC_LEN))
4257 		pmk_r1_len = f_pmk_r1_len;
4258 	RRB_GET(FT_RRB_PMK_R1, pmk_r1, msgtype, pmk_r1_len);
4259 	wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", f_pmk_r1, pmk_r1_len);
4260 
4261 	pairwise = WPA_GET_LE16(f_pairwise);
4262 
4263 	RRB_GET_OPTIONAL(FT_RRB_EXPIRES_IN, expires_in, msgtype,
4264 			 sizeof(le16));
4265 	if (f_expires_in)
4266 		expires_in = WPA_GET_LE16(f_expires_in);
4267 	else
4268 		expires_in = 0;
4269 
4270 	wpa_printf(MSG_DEBUG, "FT: PMK-R1 %s - expires_in=%d", msgtype,
4271 		   expires_in);
4272 
4273 	if (wpa_ft_rrb_get_tlv_vlan(plain, plain_len, &vlan) < 0) {
4274 		wpa_printf(MSG_DEBUG, "FT: Cannot parse vlan");
4275 		wpa_ft_rrb_dump(plain, plain_len);
4276 		goto out;
4277 	}
4278 
4279 	wpa_printf(MSG_DEBUG, "FT: vlan %d%s",
4280 		   le_to_host16(vlan.untagged), vlan.tagged[0] ? "+" : "");
4281 
4282 	RRB_GET_OPTIONAL(FT_RRB_IDENTITY, identity, msgtype, -1);
4283 	if (f_identity)
4284 		wpa_hexdump_ascii(MSG_DEBUG, "FT: Identity", f_identity,
4285 				  f_identity_len);
4286 
4287 	RRB_GET_OPTIONAL(FT_RRB_RADIUS_CUI, radius_cui, msgtype, -1);
4288 	if (f_radius_cui)
4289 		wpa_hexdump_ascii(MSG_DEBUG, "FT: CUI", f_radius_cui,
4290 				  f_radius_cui_len);
4291 
4292 	RRB_GET_OPTIONAL(FT_RRB_SESSION_TIMEOUT, session_timeout, msgtype,
4293 			 sizeof(le32));
4294 	if (f_session_timeout)
4295 		session_timeout = WPA_GET_LE32(f_session_timeout);
4296 	else
4297 		session_timeout = 0;
4298 	wpa_printf(MSG_DEBUG, "FT: session_timeout %d", session_timeout);
4299 
4300 	if (wpa_ft_store_pmk_r1(wpa_auth, f_s1kh_id, f_pmk_r1, pmk_r1_len,
4301 				f_pmk_r1_name,
4302 				pairwise, &vlan, expires_in, session_timeout,
4303 				f_identity, f_identity_len, f_radius_cui,
4304 				f_radius_cui_len) < 0)
4305 		goto out;
4306 
4307 	ret = 0;
4308 out:
4309 	bin_clear_free(plain, plain_len);
4310 
4311 	return ret;
4312 
4313 }
4314 
4315 
ft_finish_pull(struct wpa_state_machine * sm)4316 static void ft_finish_pull(struct wpa_state_machine *sm)
4317 {
4318 	int res;
4319 	u8 *resp_ies;
4320 	size_t resp_ies_len;
4321 	u16 status;
4322 
4323 	if (!sm->ft_pending_cb || !sm->ft_pending_req_ies)
4324 		return;
4325 
4326 	res = wpa_ft_process_auth_req(sm, wpabuf_head(sm->ft_pending_req_ies),
4327 				      wpabuf_len(sm->ft_pending_req_ies),
4328 				      &resp_ies, &resp_ies_len);
4329 	if (res < 0) {
4330 		/* this loop is broken by ft_pending_pull_left_retries */
4331 		wpa_printf(MSG_DEBUG,
4332 			   "FT: Callback postponed until response is available");
4333 		return;
4334 	}
4335 	wpabuf_free(sm->ft_pending_req_ies);
4336 	sm->ft_pending_req_ies = NULL;
4337 	status = res;
4338 	wpa_printf(MSG_DEBUG, "FT: Postponed auth callback result for " MACSTR
4339 		   " - status %u", MAC2STR(sm->addr), status);
4340 
4341 	sm->ft_pending_cb(sm->ft_pending_cb_ctx, sm->addr, sm->wpa_auth->addr,
4342 			  sm->ft_pending_auth_transaction + 1, status,
4343 			  resp_ies, resp_ies_len);
4344 	os_free(resp_ies);
4345 }
4346 
4347 
4348 struct ft_get_sta_ctx {
4349 	const u8 *nonce;
4350 	const u8 *s1kh_id;
4351 	struct wpa_state_machine *sm;
4352 };
4353 
4354 
ft_get_sta_cb(struct wpa_state_machine * sm,void * ctx)4355 static int ft_get_sta_cb(struct wpa_state_machine *sm, void *ctx)
4356 {
4357 	struct ft_get_sta_ctx *info = ctx;
4358 
4359 	if ((info->s1kh_id &&
4360 	     os_memcmp(info->s1kh_id, sm->addr, ETH_ALEN) != 0) ||
4361 	    os_memcmp(info->nonce, sm->ft_pending_pull_nonce,
4362 		      FT_RRB_NONCE_LEN) != 0 ||
4363 	    sm->ft_pending_cb == NULL || sm->ft_pending_req_ies == NULL)
4364 		return 0;
4365 
4366 	info->sm = sm;
4367 
4368 	return 1;
4369 }
4370 
4371 
wpa_ft_rrb_rx_resp(struct wpa_authenticator * wpa_auth,const u8 * src_addr,const u8 * enc,size_t enc_len,const u8 * auth,size_t auth_len,int no_defer)4372 static int wpa_ft_rrb_rx_resp(struct wpa_authenticator *wpa_auth,
4373 			      const u8 *src_addr,
4374 			      const u8 *enc, size_t enc_len,
4375 			      const u8 *auth, size_t auth_len,
4376 			      int no_defer)
4377 {
4378 	const char *msgtype = "pull response";
4379 	int nak, ret = -1;
4380 	struct ft_get_sta_ctx ctx;
4381 	u8 s1kh_id[ETH_ALEN];
4382 	const u8 *f_nonce;
4383 	size_t f_nonce_len;
4384 
4385 	wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull response");
4386 
4387 	RRB_GET_AUTH(FT_RRB_NONCE, nonce, msgtype, FT_RRB_NONCE_LEN);
4388 	wpa_hexdump(MSG_DEBUG, "FT: nonce", f_nonce, f_nonce_len);
4389 
4390 	os_memset(&ctx, 0, sizeof(ctx));
4391 	ctx.nonce = f_nonce;
4392 	if (!wpa_auth_for_each_sta(wpa_auth, ft_get_sta_cb, &ctx)) {
4393 		/* nonce not found */
4394 		wpa_printf(MSG_DEBUG, "FT: Invalid nonce");
4395 		return -1;
4396 	}
4397 
4398 	ret = wpa_ft_rrb_rx_r1(wpa_auth, src_addr, FT_PACKET_R0KH_R1KH_RESP,
4399 			       enc, enc_len, auth, auth_len, msgtype, s1kh_id,
4400 			       no_defer ? NULL : &wpa_ft_rrb_rx_resp);
4401 	if (ret == -2) {
4402 		ret = 0;
4403 		nak = 1;
4404 	} else {
4405 		nak = 0;
4406 	}
4407 	if (ret < 0)
4408 		return -1;
4409 
4410 	ctx.s1kh_id = s1kh_id;
4411 	if (wpa_auth_for_each_sta(wpa_auth, ft_get_sta_cb, &ctx)) {
4412 		wpa_printf(MSG_DEBUG,
4413 			   "FT: Response to a pending pull request for " MACSTR,
4414 			   MAC2STR(ctx.sm->addr));
4415 		eloop_cancel_timeout(wpa_ft_expire_pull, ctx.sm, NULL);
4416 		if (nak)
4417 			ctx.sm->ft_pending_pull_left_retries = 0;
4418 		ft_finish_pull(ctx.sm);
4419 	}
4420 
4421 out:
4422 	return ret;
4423 }
4424 
4425 
wpa_ft_rrb_rx_push(struct wpa_authenticator * wpa_auth,const u8 * src_addr,const u8 * enc,size_t enc_len,const u8 * auth,size_t auth_len,int no_defer)4426 static int wpa_ft_rrb_rx_push(struct wpa_authenticator *wpa_auth,
4427 			      const u8 *src_addr,
4428 			      const u8 *enc, size_t enc_len,
4429 			      const u8 *auth, size_t auth_len, int no_defer)
4430 {
4431 	const char *msgtype = "push";
4432 
4433 	wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 push");
4434 
4435 	if (wpa_ft_rrb_rx_r1(wpa_auth, src_addr, FT_PACKET_R0KH_R1KH_PUSH,
4436 			     enc, enc_len, auth, auth_len, msgtype, NULL,
4437 			     no_defer ? NULL : wpa_ft_rrb_rx_push) < 0)
4438 		return -1;
4439 
4440 	return 0;
4441 }
4442 
4443 
wpa_ft_rrb_rx_seq(struct wpa_authenticator * wpa_auth,const u8 * src_addr,int type,const u8 * enc,size_t enc_len,const u8 * auth,size_t auth_len,struct ft_remote_seq ** rkh_seq,u8 ** key,size_t * key_len,struct ft_remote_r0kh ** r0kh_out,struct ft_remote_r1kh ** r1kh_out,struct ft_remote_r0kh ** r0kh_wildcard_out,struct ft_remote_r1kh ** r1kh_wildcard_out)4444 static int wpa_ft_rrb_rx_seq(struct wpa_authenticator *wpa_auth,
4445 			     const u8 *src_addr, int type,
4446 			     const u8 *enc, size_t enc_len,
4447 			     const u8 *auth, size_t auth_len,
4448 			     struct ft_remote_seq **rkh_seq,
4449 			     u8 **key, size_t *key_len,
4450 			     struct ft_remote_r0kh **r0kh_out,
4451 			     struct ft_remote_r1kh **r1kh_out,
4452 			     struct ft_remote_r0kh **r0kh_wildcard_out,
4453 			     struct ft_remote_r1kh **r1kh_wildcard_out)
4454 {
4455 	struct ft_remote_r0kh *r0kh = NULL;
4456 	struct ft_remote_r1kh *r1kh = NULL;
4457 	const u8 *f_r0kh_id, *f_r1kh_id;
4458 	size_t f_r0kh_id_len, f_r1kh_id_len;
4459 	int to_r0kh, to_r1kh;
4460 	u8 *plain = NULL;
4461 	size_t plain_len = 0;
4462 	struct ft_remote_r0kh *r0kh_wildcard;
4463 	struct ft_remote_r1kh *r1kh_wildcard;
4464 
4465 	RRB_GET_AUTH(FT_RRB_R0KH_ID, r0kh_id, "seq", -1);
4466 	RRB_GET_AUTH(FT_RRB_R1KH_ID, r1kh_id, "seq", FT_R1KH_ID_LEN);
4467 
4468 	to_r0kh = !wpa_ft_rrb_check_r0kh(wpa_auth, f_r0kh_id, f_r0kh_id_len);
4469 	to_r1kh = !wpa_ft_rrb_check_r1kh(wpa_auth, f_r1kh_id);
4470 
4471 	if (to_r0kh && to_r1kh) {
4472 		wpa_printf(MSG_DEBUG, "FT: seq - local R0KH-ID and R1KH-ID");
4473 		goto out;
4474 	}
4475 
4476 	if (!to_r0kh && !to_r1kh) {
4477 		wpa_printf(MSG_DEBUG, "FT: seq - remote R0KH-ID and R1KH-ID");
4478 		goto out;
4479 	}
4480 
4481 	if (!to_r0kh) {
4482 		wpa_ft_rrb_lookup_r0kh(wpa_auth, f_r0kh_id, f_r0kh_id_len,
4483 				       &r0kh, &r0kh_wildcard);
4484 		if (!r0kh_wildcard &&
4485 		    (!r0kh || os_memcmp(r0kh->addr, src_addr, ETH_ALEN) != 0)) {
4486 			wpa_hexdump(MSG_DEBUG, "FT: Did not find R0KH-ID",
4487 				    f_r0kh_id, f_r0kh_id_len);
4488 			goto out;
4489 		}
4490 		if (r0kh) {
4491 			*key = r0kh->key;
4492 			*key_len = sizeof(r0kh->key);
4493 		} else {
4494 			*key = r0kh_wildcard->key;
4495 			*key_len = sizeof(r0kh_wildcard->key);
4496 		}
4497 	}
4498 
4499 	if (!to_r1kh) {
4500 		wpa_ft_rrb_lookup_r1kh(wpa_auth, f_r1kh_id, &r1kh,
4501 				       &r1kh_wildcard);
4502 		if (!r1kh_wildcard &&
4503 		    (!r1kh || os_memcmp(r1kh->addr, src_addr, ETH_ALEN) != 0)) {
4504 			wpa_hexdump(MSG_DEBUG, "FT: Did not find R1KH-ID",
4505 				    f_r1kh_id, FT_R1KH_ID_LEN);
4506 			goto out;
4507 		}
4508 		if (r1kh) {
4509 			*key = r1kh->key;
4510 			*key_len = sizeof(r1kh->key);
4511 		} else {
4512 			*key = r1kh_wildcard->key;
4513 			*key_len = sizeof(r1kh_wildcard->key);
4514 		}
4515 	}
4516 
4517 	if (wpa_ft_rrb_decrypt(*key, *key_len, enc, enc_len, auth, auth_len,
4518 			       src_addr, type, &plain, &plain_len) < 0)
4519 		goto out;
4520 
4521 	os_free(plain);
4522 
4523 	if (!to_r0kh) {
4524 		if (!r0kh)
4525 			r0kh = wpa_ft_rrb_add_r0kh(wpa_auth, r0kh_wildcard,
4526 						   src_addr, f_r0kh_id,
4527 						   f_r0kh_id_len,
4528 						   ftRRBseqTimeout);
4529 		if (!r0kh)
4530 			goto out;
4531 
4532 		wpa_ft_rrb_r0kh_replenish(wpa_auth, r0kh, ftRRBseqTimeout);
4533 		*rkh_seq = r0kh->seq;
4534 		if (r0kh_out)
4535 			*r0kh_out = r0kh;
4536 		if (r0kh_wildcard_out)
4537 			*r0kh_wildcard_out = r0kh_wildcard;
4538 	}
4539 
4540 	if (!to_r1kh) {
4541 		if (!r1kh)
4542 			r1kh = wpa_ft_rrb_add_r1kh(wpa_auth, r1kh_wildcard,
4543 						   src_addr, f_r1kh_id,
4544 						   ftRRBseqTimeout);
4545 		if (!r1kh)
4546 			goto out;
4547 
4548 		wpa_ft_rrb_r1kh_replenish(wpa_auth, r1kh, ftRRBseqTimeout);
4549 		*rkh_seq = r1kh->seq;
4550 		if (r1kh_out)
4551 			*r1kh_out = r1kh;
4552 		if (r1kh_wildcard_out)
4553 			*r1kh_wildcard_out = r1kh_wildcard;
4554 	}
4555 
4556 	return 0;
4557 out:
4558 	return -1;
4559 }
4560 
4561 
wpa_ft_rrb_rx_seq_req(struct wpa_authenticator * wpa_auth,const u8 * src_addr,const u8 * enc,size_t enc_len,const u8 * auth,size_t auth_len,int no_defer)4562 static int wpa_ft_rrb_rx_seq_req(struct wpa_authenticator *wpa_auth,
4563 				 const u8 *src_addr,
4564 				 const u8 *enc, size_t enc_len,
4565 				 const u8 *auth, size_t auth_len,
4566 				 int no_defer)
4567 {
4568 	int ret = -1;
4569 	struct ft_rrb_seq f_seq;
4570 	const u8 *f_nonce, *f_r0kh_id, *f_r1kh_id;
4571 	size_t f_nonce_len, f_r0kh_id_len, f_r1kh_id_len;
4572 	struct ft_remote_seq *rkh_seq = NULL;
4573 	u8 *packet = NULL, *key = NULL;
4574 	size_t packet_len = 0, key_len = 0;
4575 	struct tlv_list seq_resp_auth[5];
4576 
4577 	wpa_printf(MSG_DEBUG, "FT: Received sequence number request");
4578 
4579 	if (wpa_ft_rrb_rx_seq(wpa_auth, src_addr, FT_PACKET_R0KH_R1KH_SEQ_REQ,
4580 			      enc, enc_len, auth, auth_len, &rkh_seq, &key,
4581 			      &key_len, NULL, NULL, NULL, NULL) < 0)
4582 		goto out;
4583 
4584 	RRB_GET_AUTH(FT_RRB_NONCE, nonce, "seq request", FT_RRB_NONCE_LEN);
4585 	wpa_hexdump(MSG_DEBUG, "FT: seq request - nonce", f_nonce, f_nonce_len);
4586 
4587 	RRB_GET_AUTH(FT_RRB_R0KH_ID, r0kh_id, "seq", -1);
4588 	RRB_GET_AUTH(FT_RRB_R1KH_ID, r1kh_id, "seq", FT_R1KH_ID_LEN);
4589 
4590 	if (wpa_ft_new_seq(rkh_seq, &f_seq) < 0) {
4591 		wpa_printf(MSG_DEBUG, "FT: Failed to get seq num");
4592 		goto out;
4593 	}
4594 
4595 	wpa_printf(MSG_DEBUG, "FT: Send sequence number response from " MACSTR
4596 		   " to " MACSTR,
4597 		   MAC2STR(wpa_auth->addr), MAC2STR(src_addr));
4598 
4599 	seq_resp_auth[0].type = FT_RRB_NONCE;
4600 	seq_resp_auth[0].len = f_nonce_len;
4601 	seq_resp_auth[0].data = f_nonce;
4602 	seq_resp_auth[1].type = FT_RRB_SEQ;
4603 	seq_resp_auth[1].len = sizeof(f_seq);
4604 	seq_resp_auth[1].data = (u8 *) &f_seq;
4605 	seq_resp_auth[2].type = FT_RRB_R0KH_ID;
4606 	seq_resp_auth[2].len = f_r0kh_id_len;
4607 	seq_resp_auth[2].data = f_r0kh_id;
4608 	seq_resp_auth[3].type = FT_RRB_R1KH_ID;
4609 	seq_resp_auth[3].len = FT_R1KH_ID_LEN;
4610 	seq_resp_auth[3].data = f_r1kh_id;
4611 	seq_resp_auth[4].type = FT_RRB_LAST_EMPTY;
4612 	seq_resp_auth[4].len = 0;
4613 	seq_resp_auth[4].data = NULL;
4614 
4615 	if (wpa_ft_rrb_build(key, key_len, NULL, NULL, seq_resp_auth, NULL,
4616 			     wpa_auth->addr, FT_PACKET_R0KH_R1KH_SEQ_RESP,
4617 			     &packet, &packet_len) < 0)
4618 		goto out;
4619 
4620 	wpa_ft_rrb_oui_send(wpa_auth, src_addr,
4621 			    FT_PACKET_R0KH_R1KH_SEQ_RESP, packet,
4622 			    packet_len);
4623 
4624 out:
4625 	os_free(packet);
4626 
4627 	return ret;
4628 }
4629 
4630 
wpa_ft_rrb_rx_seq_resp(struct wpa_authenticator * wpa_auth,const u8 * src_addr,const u8 * enc,size_t enc_len,const u8 * auth,size_t auth_len,int no_defer)4631 static int wpa_ft_rrb_rx_seq_resp(struct wpa_authenticator *wpa_auth,
4632 				  const u8 *src_addr,
4633 				  const u8 *enc, size_t enc_len,
4634 				  const u8 *auth, size_t auth_len,
4635 				  int no_defer)
4636 {
4637 	u8 *key = NULL;
4638 	size_t key_len = 0;
4639 	struct ft_remote_r0kh *r0kh = NULL, *r0kh_wildcard = NULL;
4640 	struct ft_remote_r1kh *r1kh = NULL, *r1kh_wildcard = NULL;
4641 	const u8 *f_nonce, *f_seq;
4642 	size_t f_nonce_len, f_seq_len;
4643 	struct ft_remote_seq *rkh_seq = NULL;
4644 	struct ft_remote_item *item;
4645 	struct os_reltime now, now_remote;
4646 	int seq_ret, found;
4647 	const struct ft_rrb_seq *msg_both;
4648 	u32 msg_dom, msg_seq;
4649 
4650 	wpa_printf(MSG_DEBUG, "FT: Received sequence number response");
4651 
4652 	if (wpa_ft_rrb_rx_seq(wpa_auth, src_addr, FT_PACKET_R0KH_R1KH_SEQ_RESP,
4653 			      enc, enc_len, auth, auth_len, &rkh_seq, &key,
4654 			      &key_len, &r0kh, &r1kh, &r0kh_wildcard,
4655 			      &r1kh_wildcard) < 0)
4656 		goto out;
4657 
4658 	RRB_GET_AUTH(FT_RRB_NONCE, nonce, "seq response", FT_RRB_NONCE_LEN);
4659 	wpa_hexdump(MSG_DEBUG, "FT: seq response - nonce", f_nonce,
4660 		    f_nonce_len);
4661 
4662 	found = 0;
4663 	dl_list_for_each(item, &rkh_seq->rx.queue, struct ft_remote_item,
4664 			 list) {
4665 		if (os_memcmp_const(f_nonce, item->nonce,
4666 				    FT_RRB_NONCE_LEN) != 0 ||
4667 		    os_get_reltime(&now) < 0 ||
4668 		    os_reltime_expired(&now, &item->nonce_ts, ftRRBseqTimeout))
4669 			continue;
4670 
4671 		found = 1;
4672 		break;
4673 	}
4674 	if (!found) {
4675 		wpa_printf(MSG_DEBUG, "FT: seq response - bad nonce");
4676 		goto out;
4677 	}
4678 
4679 	if (r0kh) {
4680 		wpa_ft_rrb_r0kh_replenish(wpa_auth, r0kh,
4681 					  wpa_auth->conf.rkh_pos_timeout);
4682 		if (r0kh_wildcard)
4683 			os_memcpy(r0kh->addr, src_addr, ETH_ALEN);
4684 	}
4685 
4686 	if (r1kh) {
4687 		wpa_ft_rrb_r1kh_replenish(wpa_auth, r1kh,
4688 					  wpa_auth->conf.rkh_pos_timeout);
4689 		if (r1kh_wildcard)
4690 			os_memcpy(r1kh->addr, src_addr, ETH_ALEN);
4691 	}
4692 
4693 	seq_ret = wpa_ft_rrb_seq_chk(rkh_seq, src_addr, enc, enc_len, auth,
4694 				     auth_len, "seq response", 1);
4695 	if (seq_ret == FT_RRB_SEQ_OK) {
4696 		wpa_printf(MSG_DEBUG, "FT: seq response - valid seq number");
4697 		wpa_ft_rrb_seq_accept(wpa_auth, rkh_seq, src_addr, auth,
4698 				      auth_len, "seq response");
4699 	} else {
4700 		wpa_printf(MSG_DEBUG, "FT: seq response - reset seq number");
4701 
4702 		RRB_GET_AUTH(FT_RRB_SEQ, seq, "seq response",
4703 			     sizeof(*msg_both));
4704 		msg_both = (const struct ft_rrb_seq *) f_seq;
4705 
4706 		msg_dom = le_to_host32(msg_both->dom);
4707 		msg_seq = le_to_host32(msg_both->seq);
4708 		now_remote.sec = le_to_host32(msg_both->ts);
4709 		now_remote.usec = 0;
4710 
4711 		rkh_seq->rx.num_last = 2;
4712 		rkh_seq->rx.dom = msg_dom;
4713 		rkh_seq->rx.offsetidx = 0;
4714 		/* Accept some older, possibly cached packets as well */
4715 		rkh_seq->rx.last[0] = msg_seq - FT_REMOTE_SEQ_BACKLOG -
4716 			dl_list_len(&rkh_seq->rx.queue);
4717 		rkh_seq->rx.last[1] = msg_seq;
4718 
4719 		/* local time - offset = remote time
4720 		 * <=> local time - remote time = offset */
4721 		os_reltime_sub(&now, &now_remote, &rkh_seq->rx.time_offset);
4722 	}
4723 
4724 	wpa_ft_rrb_seq_flush(wpa_auth, rkh_seq, 1);
4725 
4726 	return 0;
4727 out:
4728 	return -1;
4729 }
4730 
4731 
wpa_ft_rrb_rx(struct wpa_authenticator * wpa_auth,const u8 * src_addr,const u8 * data,size_t data_len)4732 int wpa_ft_rrb_rx(struct wpa_authenticator *wpa_auth, const u8 *src_addr,
4733 		  const u8 *data, size_t data_len)
4734 {
4735 	struct ft_rrb_frame *frame;
4736 	u16 alen;
4737 	const u8 *pos, *end, *start;
4738 	u8 action;
4739 	const u8 *sta_addr, *target_ap_addr;
4740 
4741 	wpa_printf(MSG_DEBUG, "FT: RRB received frame from remote AP " MACSTR,
4742 		   MAC2STR(src_addr));
4743 
4744 	if (data_len < sizeof(*frame)) {
4745 		wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (data_len=%lu)",
4746 			   (unsigned long) data_len);
4747 		return -1;
4748 	}
4749 
4750 	pos = data;
4751 	frame = (struct ft_rrb_frame *) pos;
4752 	pos += sizeof(*frame);
4753 
4754 	alen = le_to_host16(frame->action_length);
4755 	wpa_printf(MSG_DEBUG, "FT: RRB frame - frame_type=%d packet_type=%d "
4756 		   "action_length=%d ap_address=" MACSTR,
4757 		   frame->frame_type, frame->packet_type, alen,
4758 		   MAC2STR(frame->ap_address));
4759 
4760 	if (frame->frame_type != RSN_REMOTE_FRAME_TYPE_FT_RRB) {
4761 		/* Discard frame per IEEE Std 802.11r-2008, 11A.10.3 */
4762 		wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with "
4763 			   "unrecognized type %d", frame->frame_type);
4764 		return -1;
4765 	}
4766 
4767 	if (alen > data_len - sizeof(*frame)) {
4768 		wpa_printf(MSG_DEBUG, "FT: RRB frame too short for action "
4769 			   "frame");
4770 		return -1;
4771 	}
4772 
4773 	wpa_hexdump(MSG_MSGDUMP, "FT: RRB - FT Action frame", pos, alen);
4774 
4775 	if (alen < 1 + 1 + 2 * ETH_ALEN) {
4776 		wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (not enough "
4777 			   "room for Action Frame body); alen=%lu",
4778 			   (unsigned long) alen);
4779 		return -1;
4780 	}
4781 	start = pos;
4782 	end = pos + alen;
4783 
4784 	if (*pos != WLAN_ACTION_FT) {
4785 		wpa_printf(MSG_DEBUG, "FT: Unexpected Action frame category "
4786 			   "%d", *pos);
4787 		return -1;
4788 	}
4789 
4790 	pos++;
4791 	action = *pos++;
4792 	sta_addr = pos;
4793 	pos += ETH_ALEN;
4794 	target_ap_addr = pos;
4795 	pos += ETH_ALEN;
4796 	wpa_printf(MSG_DEBUG, "FT: RRB Action Frame: action=%d sta_addr="
4797 		   MACSTR " target_ap_addr=" MACSTR,
4798 		   action, MAC2STR(sta_addr), MAC2STR(target_ap_addr));
4799 
4800 	if (frame->packet_type == FT_PACKET_REQUEST) {
4801 		wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Request");
4802 
4803 		if (action != 1) {
4804 			wpa_printf(MSG_DEBUG, "FT: Unexpected Action %d in "
4805 				   "RRB Request", action);
4806 			return -1;
4807 		}
4808 
4809 		if (os_memcmp(target_ap_addr, wpa_auth->addr, ETH_ALEN) != 0) {
4810 			wpa_printf(MSG_DEBUG, "FT: Target AP address in the "
4811 				   "RRB Request does not match with own "
4812 				   "address");
4813 			return -1;
4814 		}
4815 
4816 		if (wpa_ft_rrb_rx_request(wpa_auth, frame->ap_address,
4817 					  sta_addr, pos, end - pos) < 0)
4818 			return -1;
4819 	} else if (frame->packet_type == FT_PACKET_RESPONSE) {
4820 		u16 status_code;
4821 
4822 		if (end - pos < 2) {
4823 			wpa_printf(MSG_DEBUG, "FT: Not enough room for status "
4824 				   "code in RRB Response");
4825 			return -1;
4826 		}
4827 		status_code = WPA_GET_LE16(pos);
4828 
4829 		wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Response "
4830 			   "(status_code=%d)", status_code);
4831 
4832 		if (wpa_ft_action_send(wpa_auth, sta_addr, start, alen) < 0)
4833 			return -1;
4834 	} else {
4835 		wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with unknown "
4836 			   "packet_type %d", frame->packet_type);
4837 		return -1;
4838 	}
4839 
4840 	return 0;
4841 }
4842 
4843 
wpa_ft_rrb_oui_rx(struct wpa_authenticator * wpa_auth,const u8 * src_addr,const u8 * dst_addr,u8 oui_suffix,const u8 * data,size_t data_len)4844 void wpa_ft_rrb_oui_rx(struct wpa_authenticator *wpa_auth, const u8 *src_addr,
4845 		       const u8 *dst_addr, u8 oui_suffix, const u8 *data,
4846 		       size_t data_len)
4847 {
4848 	const u8 *auth, *enc;
4849 	size_t alen, elen;
4850 	int no_defer = 0;
4851 
4852 	wpa_printf(MSG_DEBUG, "FT: RRB-OUI(" MACSTR
4853 		   ") received frame from remote AP "
4854 		   MACSTR " oui_suffix=%u dst=" MACSTR,
4855 		   MAC2STR(wpa_auth->addr), MAC2STR(src_addr), oui_suffix,
4856 		   MAC2STR(dst_addr));
4857 	wpa_hexdump(MSG_MSGDUMP, "FT: RRB frame payload", data, data_len);
4858 
4859 	if (is_multicast_ether_addr(src_addr)) {
4860 		wpa_printf(MSG_DEBUG,
4861 			   "FT: RRB-OUI received frame from multicast address "
4862 			   MACSTR, MAC2STR(src_addr));
4863 		return;
4864 	}
4865 
4866 	if (is_multicast_ether_addr(dst_addr))
4867 		no_defer = 1;
4868 
4869 	if (data_len < sizeof(u16)) {
4870 		wpa_printf(MSG_DEBUG, "FT: RRB-OUI frame too short");
4871 		return;
4872 	}
4873 
4874 	alen = WPA_GET_LE16(data);
4875 	if (data_len < sizeof(u16) + alen) {
4876 		wpa_printf(MSG_DEBUG, "FT: RRB-OUI frame too short");
4877 		return;
4878 	}
4879 
4880 	auth = data + sizeof(u16);
4881 	wpa_hexdump(MSG_MSGDUMP, "FT: Authenticated payload", auth, alen);
4882 	enc = data + sizeof(u16) + alen;
4883 	elen = data_len - sizeof(u16) - alen;
4884 	wpa_hexdump(MSG_MSGDUMP, "FT: Encrypted payload", enc, elen);
4885 
4886 	switch (oui_suffix) {
4887 	case FT_PACKET_R0KH_R1KH_PULL:
4888 		wpa_ft_rrb_rx_pull(wpa_auth, src_addr, enc, elen, auth, alen,
4889 				   no_defer);
4890 		break;
4891 	case FT_PACKET_R0KH_R1KH_RESP:
4892 		wpa_ft_rrb_rx_resp(wpa_auth, src_addr, enc, elen, auth, alen,
4893 				   no_defer);
4894 		break;
4895 	case FT_PACKET_R0KH_R1KH_PUSH:
4896 		wpa_ft_rrb_rx_push(wpa_auth, src_addr, enc, elen, auth, alen,
4897 				   no_defer);
4898 		break;
4899 	case FT_PACKET_R0KH_R1KH_SEQ_REQ:
4900 		wpa_ft_rrb_rx_seq_req(wpa_auth, src_addr, enc, elen, auth, alen,
4901 				      no_defer);
4902 		break;
4903 	case FT_PACKET_R0KH_R1KH_SEQ_RESP:
4904 		wpa_ft_rrb_rx_seq_resp(wpa_auth, src_addr, enc, elen, auth,
4905 				       alen, no_defer);
4906 		break;
4907 	}
4908 }
4909 
4910 
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)4911 static int wpa_ft_generate_pmk_r1(struct wpa_authenticator *wpa_auth,
4912 				  struct wpa_ft_pmk_r0_sa *pmk_r0,
4913 				  struct ft_remote_r1kh *r1kh,
4914 				  const u8 *s1kh_id)
4915 {
4916 	u8 *packet;
4917 	size_t packet_len;
4918 	struct ft_rrb_seq f_seq;
4919 	struct tlv_list push[] = {
4920 		{ .type = FT_RRB_S1KH_ID, .len = ETH_ALEN,
4921 		  .data = s1kh_id },
4922 		{ .type = FT_RRB_PMK_R0_NAME, .len = WPA_PMK_NAME_LEN,
4923 		  .data = pmk_r0->pmk_r0_name },
4924 		{ .type = FT_RRB_LAST_EMPTY, .len = 0, .data = NULL },
4925 	};
4926 	struct tlv_list push_auth[] = {
4927 		{ .type = FT_RRB_SEQ, .len = sizeof(f_seq),
4928 		  .data = (u8 *) &f_seq },
4929 		{ .type = FT_RRB_R0KH_ID,
4930 		  .len = wpa_auth->conf.r0_key_holder_len,
4931 		  .data = wpa_auth->conf.r0_key_holder },
4932 		{ .type = FT_RRB_R1KH_ID, .len = FT_R1KH_ID_LEN,
4933 		  .data = r1kh->id },
4934 		{ .type = FT_RRB_LAST_EMPTY, .len = 0, .data = NULL },
4935 	};
4936 
4937 	if (wpa_ft_new_seq(r1kh->seq, &f_seq) < 0) {
4938 		wpa_printf(MSG_DEBUG, "FT: Failed to get seq num");
4939 		return -1;
4940 	}
4941 
4942 	wpa_printf(MSG_DEBUG, "FT: Send PMK-R1 push from " MACSTR
4943 		   " to remote R0KH address " MACSTR,
4944 		   MAC2STR(wpa_auth->addr), MAC2STR(r1kh->addr));
4945 
4946 	if (wpa_ft_rrb_build_r0(r1kh->key, sizeof(r1kh->key), push, pmk_r0,
4947 				r1kh->id, s1kh_id, push_auth, wpa_auth->addr,
4948 				FT_PACKET_R0KH_R1KH_PUSH,
4949 				&packet, &packet_len) < 0)
4950 		return -1;
4951 
4952 	wpa_ft_rrb_oui_send(wpa_auth, r1kh->addr, FT_PACKET_R0KH_R1KH_PUSH,
4953 			    packet, packet_len);
4954 
4955 	os_free(packet);
4956 	return 0;
4957 }
4958 
4959 
wpa_ft_push_pmk_r1(struct wpa_authenticator * wpa_auth,const u8 * addr)4960 void wpa_ft_push_pmk_r1(struct wpa_authenticator *wpa_auth, const u8 *addr)
4961 {
4962 	struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
4963 	struct wpa_ft_pmk_r0_sa *r0, *r0found = NULL;
4964 	struct ft_remote_r1kh *r1kh;
4965 
4966 	if (!wpa_auth->conf.pmk_r1_push)
4967 		return;
4968 	if (!wpa_auth->conf.r1kh_list)
4969 		return;
4970 
4971 	dl_list_for_each(r0, &cache->pmk_r0, struct wpa_ft_pmk_r0_sa, list) {
4972 		if (os_memcmp(r0->spa, addr, ETH_ALEN) == 0) {
4973 			r0found = r0;
4974 			break;
4975 		}
4976 	}
4977 
4978 	r0 = r0found;
4979 	if (r0 == NULL || r0->pmk_r1_pushed)
4980 		return;
4981 	r0->pmk_r1_pushed = 1;
4982 
4983 	wpa_printf(MSG_DEBUG, "FT: Deriving and pushing PMK-R1 keys to R1KHs "
4984 		   "for STA " MACSTR, MAC2STR(addr));
4985 
4986 	for (r1kh = *wpa_auth->conf.r1kh_list; r1kh; r1kh = r1kh->next) {
4987 		if (is_zero_ether_addr(r1kh->addr) ||
4988 		    is_zero_ether_addr(r1kh->id))
4989 			continue;
4990 		if (wpa_ft_rrb_init_r1kh_seq(r1kh) < 0)
4991 			continue;
4992 		wpa_ft_generate_pmk_r1(wpa_auth, r0, r1kh, addr);
4993 	}
4994 }
4995 
4996 #endif /* CONFIG_IEEE80211R_AP */
4997