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