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