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