2 * hostapd - IEEE 802.11r - Fast BSS Transition
3 * Copyright (c) 2004-2018, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
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 "crypto/aes.h"
17 #include "crypto/aes_siv.h"
18 #include "crypto/aes_wrap.h"
19 #include "crypto/sha384.h"
20 #include "crypto/random.h"
21 #include "ap_config.h"
22 #include "ieee802_11.h"
25 #include "wpa_auth_i.h"
28 #ifdef CONFIG_IEEE80211R_AP
30 const unsigned int ftRRBseqTimeout
= 10;
31 const unsigned int ftRRBmaxQueueLen
= 100;
34 static int wpa_ft_send_rrb_auth_resp(struct wpa_state_machine
*sm
,
35 const u8
*current_ap
, const u8
*sta_addr
,
36 u16 status
, const u8
*resp_ies
,
38 static void ft_finish_pull(struct wpa_state_machine
*sm
);
39 static void wpa_ft_expire_pull(void *eloop_ctx
, void *timeout_ctx
);
40 static void wpa_ft_rrb_seq_timeout(void *eloop_ctx
, void *timeout_ctx
);
50 * wpa_ft_rrb_decrypt - Decrypt FT RRB message
51 * @key: AES-SIV key for AEAD
52 * @key_len: Length of key in octets
53 * @enc: Pointer to encrypted TLVs
54 * @enc_len: Length of encrypted TLVs in octets
55 * @auth: Pointer to authenticated TLVs
56 * @auth_len: Length of authenticated TLVs in octets
57 * @src_addr: MAC address of the frame sender
58 * @type: Vendor-specific subtype of the RRB frame (FT_PACKET_*)
59 * @plain: Pointer to return the pointer to the allocated plaintext buffer;
60 * needs to be freed by the caller if not NULL;
61 * will only be returned on success
62 * @plain_len: Pointer to return the length of the allocated plaintext buffer
64 * Returns: 0 on success, -1 on error
66 static int wpa_ft_rrb_decrypt(const u8
*key
, const size_t key_len
,
67 const u8
*enc
, const size_t enc_len
,
68 const u8
*auth
, const size_t auth_len
,
69 const u8
*src_addr
, u8 type
,
70 u8
**plain
, size_t *plain_size
)
72 const u8
*ad
[3] = { src_addr
, auth
, &type
};
73 size_t ad_len
[3] = { ETH_ALEN
, auth_len
, sizeof(type
) };
75 wpa_hexdump_key(MSG_DEBUG
, "FT(RRB): decrypt using key", key
, key_len
);
77 if (!key
) { /* skip decryption */
78 *plain
= os_memdup(enc
, enc_len
);
79 if (enc_len
> 0 && !*plain
)
82 *plain_size
= enc_len
;
90 if (enc_len
< AES_BLOCK_SIZE
)
93 *plain
= os_zalloc(enc_len
- AES_BLOCK_SIZE
);
97 if (aes_siv_decrypt(key
, key_len
, enc
, enc_len
, 3, ad
, ad_len
,
101 *plain_size
= enc_len
- AES_BLOCK_SIZE
;
102 wpa_hexdump_key(MSG_DEBUG
, "FT(RRB): decrypted TLVs",
103 *plain
, *plain_size
);
110 wpa_printf(MSG_ERROR
, "FT(RRB): Failed to decrypt");
116 /* get first tlv record in packet matching type
117 * @data (decrypted) packet
118 * @return 0 on success else -1
120 static int wpa_ft_rrb_get_tlv(const u8
*plain
, size_t plain_len
,
121 u16 type
, size_t *tlv_len
, const u8
**tlv_data
)
123 const struct ft_rrb_tlv
*f
;
129 type16
= host_to_le16(type
);
131 while (left
>= sizeof(*f
)) {
132 f
= (const struct ft_rrb_tlv
*) plain
;
136 len
= le_to_host16(f
->len
);
139 wpa_printf(MSG_DEBUG
, "FT: RRB message truncated");
143 if (f
->type
== type16
) {
157 static void wpa_ft_rrb_dump(const u8
*plain
, const size_t plain_len
)
159 const struct ft_rrb_tlv
*f
;
165 wpa_printf(MSG_DEBUG
, "FT: RRB dump message");
166 while (left
>= sizeof(*f
)) {
167 f
= (const struct ft_rrb_tlv
*) plain
;
171 len
= le_to_host16(f
->len
);
173 wpa_printf(MSG_DEBUG
, "FT: RRB TLV type = %d, len = %zu",
174 le_to_host16(f
->type
), len
);
177 wpa_printf(MSG_DEBUG
,
178 "FT: RRB message truncated: left %zu bytes, need %zu",
183 wpa_hexdump(MSG_DEBUG
, "FT: RRB TLV data", plain
, len
);
190 wpa_hexdump(MSG_DEBUG
, "FT: RRB TLV padding", plain
, left
);
192 wpa_printf(MSG_DEBUG
, "FT: RRB dump message end");
196 static int cmp_int(const void *a
, const void *b
)
206 static int wpa_ft_rrb_get_tlv_vlan(const u8
*plain
, const size_t plain_len
,
207 struct vlan_description
*vlan
)
209 struct ft_rrb_tlv
*f
;
218 os_memset(vlan
, 0, sizeof(*vlan
));
220 while (left
>= sizeof(*f
)) {
221 f
= (struct ft_rrb_tlv
*) plain
;
226 len
= le_to_host16(f
->len
);
227 type
= le_to_host16(f
->type
);
230 wpa_printf(MSG_DEBUG
, "FT: RRB message truncated");
234 if (type
!= FT_RRB_VLAN_UNTAGGED
&& type
!= FT_RRB_VLAN_TAGGED
)
237 if (type
== FT_RRB_VLAN_UNTAGGED
&& len
!= sizeof(le16
)) {
238 wpa_printf(MSG_DEBUG
,
239 "FT: RRB VLAN_UNTAGGED invalid length");
243 if (type
== FT_RRB_VLAN_TAGGED
&& len
% sizeof(le16
) != 0) {
244 wpa_printf(MSG_DEBUG
,
245 "FT: RRB VLAN_TAGGED invalid length");
249 while (len
>= sizeof(le16
)) {
250 vlan_id
= WPA_GET_LE16(plain
);
251 plain
+= sizeof(le16
);
252 left
-= sizeof(le16
);
255 if (vlan_id
<= 0 || vlan_id
> MAX_VLAN_ID
) {
256 wpa_printf(MSG_DEBUG
,
257 "FT: RRB VLAN ID invalid %d",
262 if (type
== FT_RRB_VLAN_UNTAGGED
)
263 vlan
->untagged
= vlan_id
;
265 if (type
== FT_RRB_VLAN_TAGGED
&&
266 taggedidx
< MAX_NUM_TAGGED_VLAN
) {
267 vlan
->tagged
[taggedidx
] = vlan_id
;
269 } else if (type
== FT_RRB_VLAN_TAGGED
) {
270 wpa_printf(MSG_DEBUG
, "FT: RRB too many VLANs");
280 qsort(vlan
->tagged
, taggedidx
, sizeof(int), cmp_int
);
282 vlan
->notempty
= vlan
->untagged
|| vlan
->tagged
[0];
288 static size_t wpa_ft_tlv_len(const struct tlv_list
*tlvs
)
296 for (i
= 0; tlvs
[i
].type
!= FT_RRB_LAST_EMPTY
; i
++) {
297 tlv_len
+= sizeof(struct ft_rrb_tlv
);
298 tlv_len
+= tlvs
[i
].len
;
305 static size_t wpa_ft_tlv_lin(const struct tlv_list
*tlvs
, u8
*start
,
310 struct ft_rrb_tlv
*hdr
;
318 for (i
= 0; tlvs
[i
].type
!= FT_RRB_LAST_EMPTY
; i
++) {
319 if (tlv_len
+ sizeof(*hdr
) > (size_t) (endpos
- start
))
321 tlv_len
+= sizeof(*hdr
);
322 hdr
= (struct ft_rrb_tlv
*) pos
;
323 hdr
->type
= host_to_le16(tlvs
[i
].type
);
324 hdr
->len
= host_to_le16(tlvs
[i
].len
);
325 pos
= start
+ tlv_len
;
327 if (tlv_len
+ tlvs
[i
].len
> (size_t) (endpos
- start
))
329 if (tlvs
[i
].len
== 0)
331 tlv_len
+= tlvs
[i
].len
;
332 os_memcpy(pos
, tlvs
[i
].data
, tlvs
[i
].len
);
333 pos
= start
+ tlv_len
;
340 static size_t wpa_ft_vlan_len(const struct vlan_description
*vlan
)
345 if (!vlan
|| !vlan
->notempty
)
348 if (vlan
->untagged
) {
349 tlv_len
+= sizeof(struct ft_rrb_tlv
);
350 tlv_len
+= sizeof(le16
);
353 tlv_len
+= sizeof(struct ft_rrb_tlv
);
354 for (i
= 0; i
< MAX_NUM_TAGGED_VLAN
&& vlan
->tagged
[i
]; i
++)
355 tlv_len
+= sizeof(le16
);
361 static size_t wpa_ft_vlan_lin(const struct vlan_description
*vlan
,
362 u8
*start
, u8
*endpos
)
366 struct ft_rrb_tlv
*hdr
;
369 if (!vlan
|| !vlan
->notempty
)
373 if (vlan
->untagged
) {
374 tlv_len
+= sizeof(*hdr
);
375 if (start
+ tlv_len
> endpos
)
377 hdr
= (struct ft_rrb_tlv
*) pos
;
378 hdr
->type
= host_to_le16(FT_RRB_VLAN_UNTAGGED
);
379 hdr
->len
= host_to_le16(sizeof(le16
));
380 pos
= start
+ tlv_len
;
382 tlv_len
+= sizeof(le16
);
383 if (start
+ tlv_len
> endpos
)
385 WPA_PUT_LE16(pos
, vlan
->untagged
);
386 pos
= start
+ tlv_len
;
389 if (!vlan
->tagged
[0])
392 tlv_len
+= sizeof(*hdr
);
393 if (start
+ tlv_len
> endpos
)
395 hdr
= (struct ft_rrb_tlv
*) pos
;
396 hdr
->type
= host_to_le16(FT_RRB_VLAN_TAGGED
);
397 len
= 0; /* len is computed below */
398 pos
= start
+ tlv_len
;
400 for (i
= 0; i
< MAX_NUM_TAGGED_VLAN
&& vlan
->tagged
[i
]; i
++) {
401 tlv_len
+= sizeof(le16
);
402 if (start
+ tlv_len
> endpos
)
405 WPA_PUT_LE16(pos
, vlan
->tagged
[i
]);
406 pos
= start
+ tlv_len
;
409 hdr
->len
= host_to_le16(len
);
415 static int wpa_ft_rrb_lin(const struct tlv_list
*tlvs1
,
416 const struct tlv_list
*tlvs2
,
417 const struct vlan_description
*vlan
,
418 u8
**plain
, size_t *plain_len
)
423 tlv_len
= wpa_ft_tlv_len(tlvs1
);
424 tlv_len
+= wpa_ft_tlv_len(tlvs2
);
425 tlv_len
+= wpa_ft_vlan_len(vlan
);
427 *plain_len
= tlv_len
;
428 *plain
= os_zalloc(tlv_len
);
430 wpa_printf(MSG_ERROR
, "FT: Failed to allocate plaintext");
435 endpos
= *plain
+ tlv_len
;
436 pos
+= wpa_ft_tlv_lin(tlvs1
, pos
, endpos
);
437 pos
+= wpa_ft_tlv_lin(tlvs2
, pos
, endpos
);
438 pos
+= wpa_ft_vlan_lin(vlan
, pos
, endpos
);
442 wpa_printf(MSG_ERROR
, "FT: Length error building RRB");
456 static int wpa_ft_rrb_encrypt(const u8
*key
, const size_t key_len
,
457 const u8
*plain
, const size_t plain_len
,
458 const u8
*auth
, const size_t auth_len
,
459 const u8
*src_addr
, u8 type
, u8
*enc
)
461 const u8
*ad
[3] = { src_addr
, auth
, &type
};
462 size_t ad_len
[3] = { ETH_ALEN
, auth_len
, sizeof(type
) };
464 wpa_hexdump_key(MSG_DEBUG
, "FT(RRB): plaintext message",
466 wpa_hexdump_key(MSG_DEBUG
, "FT(RRB): encrypt using key", key
, key_len
);
469 /* encryption not needed, return plaintext as packet */
470 os_memcpy(enc
, plain
, plain_len
);
471 } else if (aes_siv_encrypt(key
, key_len
, plain
, plain_len
,
472 3, ad
, ad_len
, enc
) < 0) {
473 wpa_printf(MSG_ERROR
, "FT: Failed to encrypt RRB-OUI message");
482 * wpa_ft_rrb_build - Build and encrypt an FT RRB message
483 * @key: AES-SIV key for AEAD
484 * @key_len: Length of key in octets
485 * @tlvs_enc0: First set of to-be-encrypted TLVs
486 * @tlvs_enc1: Second set of to-be-encrypted TLVs
487 * @tlvs_auth: Set of to-be-authenticated TLVs
488 * @src_addr: MAC address of the frame sender
489 * @type: Vendor-specific subtype of the RRB frame (FT_PACKET_*)
490 * @packet Pointer to return the pointer to the allocated packet buffer;
491 * needs to be freed by the caller if not null;
492 * will only be returned on success
493 * @packet_len: Pointer to return the length of the allocated buffer in octets
494 * Returns: 0 on success, -1 on error
496 static int wpa_ft_rrb_build(const u8
*key
, const size_t key_len
,
497 const struct tlv_list
*tlvs_enc0
,
498 const struct tlv_list
*tlvs_enc1
,
499 const struct tlv_list
*tlvs_auth
,
500 const struct vlan_description
*vlan
,
501 const u8
*src_addr
, u8 type
,
502 u8
**packet
, size_t *packet_len
)
504 u8
*plain
= NULL
, *auth
= NULL
, *pos
;
505 size_t plain_len
= 0, auth_len
= 0;
509 if (wpa_ft_rrb_lin(tlvs_enc0
, tlvs_enc1
, vlan
, &plain
, &plain_len
) < 0)
512 if (wpa_ft_rrb_lin(tlvs_auth
, NULL
, NULL
, &auth
, &auth_len
) < 0)
515 *packet_len
= sizeof(u16
) + auth_len
+ plain_len
;
517 *packet_len
+= AES_BLOCK_SIZE
;
518 *packet
= os_zalloc(*packet_len
);
523 WPA_PUT_LE16(pos
, auth_len
);
525 os_memcpy(pos
, auth
, auth_len
);
527 if (wpa_ft_rrb_encrypt(key
, key_len
, plain
, plain_len
, auth
,
528 auth_len
, src_addr
, type
, pos
) < 0)
534 bin_clear_free(plain
, plain_len
);
538 wpa_printf(MSG_ERROR
, "FT: Failed to build RRB-OUI message");
548 #define RRB_GET_SRC(srcfield, type, field, txt, checklength) do { \
549 if (wpa_ft_rrb_get_tlv(srcfield, srcfield##_len, type, \
550 &f_##field##_len, &f_##field) < 0 || \
551 (checklength > 0 && ((size_t) checklength) != f_##field##_len)) { \
552 wpa_printf(MSG_INFO, "FT: Missing required " #field \
553 " in %s from " MACSTR, txt, MAC2STR(src_addr)); \
554 wpa_ft_rrb_dump(srcfield, srcfield##_len); \
559 #define RRB_GET(type, field, txt, checklength) \
560 RRB_GET_SRC(plain, type, field, txt, checklength)
561 #define RRB_GET_AUTH(type, field, txt, checklength) \
562 RRB_GET_SRC(auth, type, field, txt, checklength)
564 #define RRB_GET_OPTIONAL_SRC(srcfield, type, field, txt, checklength) do { \
565 if (wpa_ft_rrb_get_tlv(srcfield, srcfield##_len, type, \
566 &f_##field##_len, &f_##field) < 0 || \
567 (checklength > 0 && ((size_t) checklength) != f_##field##_len)) { \
568 wpa_printf(MSG_DEBUG, "FT: Missing optional " #field \
569 " in %s from " MACSTR, txt, MAC2STR(src_addr)); \
570 f_##field##_len = 0; \
575 #define RRB_GET_OPTIONAL(type, field, txt, checklength) \
576 RRB_GET_OPTIONAL_SRC(plain, type, field, txt, checklength)
577 #define RRB_GET_OPTIONAL_AUTH(type, field, txt, checklength) \
578 RRB_GET_OPTIONAL_SRC(auth, type, field, txt, checklength)
580 static int wpa_ft_rrb_send(struct wpa_authenticator
*wpa_auth
, const u8
*dst
,
581 const u8
*data
, size_t data_len
)
583 if (wpa_auth
->cb
->send_ether
== NULL
)
585 wpa_printf(MSG_DEBUG
, "FT: RRB send to " MACSTR
, MAC2STR(dst
));
586 return wpa_auth
->cb
->send_ether(wpa_auth
->cb_ctx
, dst
, ETH_P_RRB
,
591 static int wpa_ft_rrb_oui_send(struct wpa_authenticator
*wpa_auth
,
592 const u8
*dst
, u8 oui_suffix
,
593 const u8
*data
, size_t data_len
)
595 if (!wpa_auth
->cb
->send_oui
)
597 wpa_printf(MSG_DEBUG
, "FT: RRB-OUI type %u send to " MACSTR
,
598 oui_suffix
, MAC2STR(dst
));
599 return wpa_auth
->cb
->send_oui(wpa_auth
->cb_ctx
, dst
, oui_suffix
, data
,
604 static int wpa_ft_action_send(struct wpa_authenticator
*wpa_auth
,
605 const u8
*dst
, const u8
*data
, size_t data_len
)
607 if (wpa_auth
->cb
->send_ft_action
== NULL
)
609 return wpa_auth
->cb
->send_ft_action(wpa_auth
->cb_ctx
, dst
,
614 static const u8
* wpa_ft_get_psk(struct wpa_authenticator
*wpa_auth
,
615 const u8
*addr
, const u8
*p2p_dev_addr
,
618 if (wpa_auth
->cb
->get_psk
== NULL
)
620 return wpa_auth
->cb
->get_psk(wpa_auth
->cb_ctx
, addr
, p2p_dev_addr
,
625 static struct wpa_state_machine
*
626 wpa_ft_add_sta(struct wpa_authenticator
*wpa_auth
, const u8
*sta_addr
)
628 if (wpa_auth
->cb
->add_sta
== NULL
)
630 return wpa_auth
->cb
->add_sta(wpa_auth
->cb_ctx
, sta_addr
);
634 static int wpa_ft_set_vlan(struct wpa_authenticator
*wpa_auth
,
635 const u8
*sta_addr
, struct vlan_description
*vlan
)
637 if (!wpa_auth
->cb
->set_vlan
)
639 return wpa_auth
->cb
->set_vlan(wpa_auth
->cb_ctx
, sta_addr
, vlan
);
643 static int wpa_ft_get_vlan(struct wpa_authenticator
*wpa_auth
,
644 const u8
*sta_addr
, struct vlan_description
*vlan
)
646 if (!wpa_auth
->cb
->get_vlan
)
648 return wpa_auth
->cb
->get_vlan(wpa_auth
->cb_ctx
, sta_addr
, vlan
);
653 wpa_ft_set_identity(struct wpa_authenticator
*wpa_auth
, const u8
*sta_addr
,
654 const u8
*identity
, size_t identity_len
)
656 if (!wpa_auth
->cb
->set_identity
)
658 return wpa_auth
->cb
->set_identity(wpa_auth
->cb_ctx
, sta_addr
, identity
,
664 wpa_ft_get_identity(struct wpa_authenticator
*wpa_auth
, const u8
*sta_addr
,
668 if (!wpa_auth
->cb
->get_identity
)
670 return wpa_auth
->cb
->get_identity(wpa_auth
->cb_ctx
, sta_addr
, buf
);
675 wpa_ft_set_radius_cui(struct wpa_authenticator
*wpa_auth
, const u8
*sta_addr
,
676 const u8
*radius_cui
, size_t radius_cui_len
)
678 if (!wpa_auth
->cb
->set_radius_cui
)
680 return wpa_auth
->cb
->set_radius_cui(wpa_auth
->cb_ctx
, sta_addr
,
681 radius_cui
, radius_cui_len
);
686 wpa_ft_get_radius_cui(struct wpa_authenticator
*wpa_auth
, const u8
*sta_addr
,
690 if (!wpa_auth
->cb
->get_radius_cui
)
692 return wpa_auth
->cb
->get_radius_cui(wpa_auth
->cb_ctx
, sta_addr
, buf
);
697 wpa_ft_set_session_timeout(struct wpa_authenticator
*wpa_auth
,
698 const u8
*sta_addr
, int session_timeout
)
700 if (!wpa_auth
->cb
->set_session_timeout
)
702 wpa_auth
->cb
->set_session_timeout(wpa_auth
->cb_ctx
, sta_addr
,
708 wpa_ft_get_session_timeout(struct wpa_authenticator
*wpa_auth
,
711 if (!wpa_auth
->cb
->get_session_timeout
)
713 return wpa_auth
->cb
->get_session_timeout(wpa_auth
->cb_ctx
, sta_addr
);
717 static int wpa_ft_add_tspec(struct wpa_authenticator
*wpa_auth
,
719 u8
*tspec_ie
, size_t tspec_ielen
)
721 if (wpa_auth
->cb
->add_tspec
== NULL
) {
722 wpa_printf(MSG_DEBUG
, "FT: add_tspec is not initialized");
725 return wpa_auth
->cb
->add_tspec(wpa_auth
->cb_ctx
, sta_addr
, tspec_ie
,
730 int wpa_write_mdie(struct wpa_auth_config
*conf
, u8
*buf
, size_t len
)
734 if (len
< 2 + sizeof(struct rsn_mdie
))
737 *pos
++ = WLAN_EID_MOBILITY_DOMAIN
;
738 *pos
++ = MOBILITY_DOMAIN_ID_LEN
+ 1;
739 os_memcpy(pos
, conf
->mobility_domain
, MOBILITY_DOMAIN_ID_LEN
);
740 pos
+= MOBILITY_DOMAIN_ID_LEN
;
742 if (conf
->ft_over_ds
)
743 capab
|= RSN_FT_CAPAB_FT_OVER_DS
;
750 int wpa_write_ftie(struct wpa_auth_config
*conf
, int use_sha384
,
751 const u8
*r0kh_id
, size_t r0kh_id_len
,
752 const u8
*anonce
, const u8
*snonce
,
753 u8
*buf
, size_t len
, const u8
*subelem
,
756 u8
*pos
= buf
, *ielen
;
757 size_t hdrlen
= use_sha384
? sizeof(struct rsn_ftie_sha384
) :
758 sizeof(struct rsn_ftie
);
760 if (len
< 2 + hdrlen
+ 2 + FT_R1KH_ID_LEN
+ 2 + r0kh_id_len
+
764 *pos
++ = WLAN_EID_FAST_BSS_TRANSITION
;
768 struct rsn_ftie_sha384
*hdr
= (struct rsn_ftie_sha384
*) pos
;
770 os_memset(hdr
, 0, sizeof(*hdr
));
772 WPA_PUT_LE16(hdr
->mic_control
, 0);
774 os_memcpy(hdr
->anonce
, anonce
, WPA_NONCE_LEN
);
776 os_memcpy(hdr
->snonce
, snonce
, WPA_NONCE_LEN
);
778 struct rsn_ftie
*hdr
= (struct rsn_ftie
*) pos
;
780 os_memset(hdr
, 0, sizeof(*hdr
));
782 WPA_PUT_LE16(hdr
->mic_control
, 0);
784 os_memcpy(hdr
->anonce
, anonce
, WPA_NONCE_LEN
);
786 os_memcpy(hdr
->snonce
, snonce
, WPA_NONCE_LEN
);
789 /* Optional Parameters */
790 *pos
++ = FTIE_SUBELEM_R1KH_ID
;
791 *pos
++ = FT_R1KH_ID_LEN
;
792 os_memcpy(pos
, conf
->r1_key_holder
, FT_R1KH_ID_LEN
);
793 pos
+= FT_R1KH_ID_LEN
;
796 *pos
++ = FTIE_SUBELEM_R0KH_ID
;
797 *pos
++ = r0kh_id_len
;
798 os_memcpy(pos
, r0kh_id
, r0kh_id_len
);
803 os_memcpy(pos
, subelem
, subelem_len
);
807 *ielen
= pos
- buf
- 2;
813 /* A packet to be handled after seq response */
814 struct ft_remote_item
{
817 u8 nonce
[FT_RRB_NONCE_LEN
];
818 struct os_reltime nonce_ts
;
820 u8 src_addr
[ETH_ALEN
];
825 int (*cb
)(struct wpa_authenticator
*wpa_auth
,
827 const u8
*enc
, size_t enc_len
,
828 const u8
*auth
, size_t auth_len
,
833 static void wpa_ft_rrb_seq_free(struct ft_remote_item
*item
)
835 eloop_cancel_timeout(wpa_ft_rrb_seq_timeout
, ELOOP_ALL_CTX
, item
);
836 dl_list_del(&item
->list
);
837 bin_clear_free(item
->enc
, item
->enc_len
);
843 static void wpa_ft_rrb_seq_flush(struct wpa_authenticator
*wpa_auth
,
844 struct ft_remote_seq
*rkh_seq
, int cb
)
846 struct ft_remote_item
*item
, *n
;
848 dl_list_for_each_safe(item
, n
, &rkh_seq
->rx
.queue
,
849 struct ft_remote_item
, list
) {
851 item
->cb(wpa_auth
, item
->src_addr
, item
->enc
,
852 item
->enc_len
, item
->auth
, item
->auth_len
, 1);
853 wpa_ft_rrb_seq_free(item
);
858 static void wpa_ft_rrb_seq_timeout(void *eloop_ctx
, void *timeout_ctx
)
860 struct ft_remote_item
*item
= timeout_ctx
;
862 wpa_ft_rrb_seq_free(item
);
867 wpa_ft_rrb_seq_req(struct wpa_authenticator
*wpa_auth
,
868 struct ft_remote_seq
*rkh_seq
, const u8
*src_addr
,
869 const u8
*f_r0kh_id
, size_t f_r0kh_id_len
,
870 const u8
*f_r1kh_id
, const u8
*key
, size_t key_len
,
871 const u8
*enc
, size_t enc_len
,
872 const u8
*auth
, size_t auth_len
,
873 int (*cb
)(struct wpa_authenticator
*wpa_auth
,
875 const u8
*enc
, size_t enc_len
,
876 const u8
*auth
, size_t auth_len
,
879 struct ft_remote_item
*item
= NULL
;
882 struct tlv_list seq_req_auth
[] = {
883 { .type
= FT_RRB_NONCE
, .len
= FT_RRB_NONCE_LEN
,
884 .data
= NULL
/* to be filled: item->nonce */ },
885 { .type
= FT_RRB_R0KH_ID
, .len
= f_r0kh_id_len
,
887 { .type
= FT_RRB_R1KH_ID
, .len
= FT_R1KH_ID_LEN
,
889 { .type
= FT_RRB_LAST_EMPTY
, .len
= 0, .data
= NULL
},
892 if (dl_list_len(&rkh_seq
->rx
.queue
) >= ftRRBmaxQueueLen
) {
893 wpa_printf(MSG_DEBUG
, "FT: Sequence number queue too long");
897 item
= os_zalloc(sizeof(*item
));
901 os_memcpy(item
->src_addr
, src_addr
, ETH_ALEN
);
904 if (random_get_bytes(item
->nonce
, FT_RRB_NONCE_LEN
) < 0) {
905 wpa_printf(MSG_DEBUG
, "FT: Seq num nonce: out of random bytes");
909 if (os_get_reltime(&item
->nonce_ts
) < 0)
912 if (enc
&& enc_len
> 0) {
913 item
->enc
= os_memdup(enc
, enc_len
);
914 item
->enc_len
= enc_len
;
919 if (auth
&& auth_len
> 0) {
920 item
->auth
= os_memdup(auth
, auth_len
);
921 item
->auth_len
= auth_len
;
926 eloop_register_timeout(ftRRBseqTimeout
, 0, wpa_ft_rrb_seq_timeout
,
929 seq_req_auth
[0].data
= item
->nonce
;
931 if (wpa_ft_rrb_build(key
, key_len
, NULL
, NULL
, seq_req_auth
, NULL
,
932 wpa_auth
->addr
, FT_PACKET_R0KH_R1KH_SEQ_REQ
,
933 &packet
, &packet_len
) < 0) {
934 item
= NULL
; /* some other seq resp might still accept this */
938 dl_list_add(&rkh_seq
->rx
.queue
, &item
->list
);
940 wpa_ft_rrb_oui_send(wpa_auth
, src_addr
, FT_PACKET_R0KH_R1KH_SEQ_REQ
,
947 wpa_printf(MSG_DEBUG
, "FT: Failed to send sequence number request");
950 bin_clear_free(item
->enc
, item
->enc_len
);
958 #define FT_RRB_SEQ_OK 0
959 #define FT_RRB_SEQ_DROP 1
960 #define FT_RRB_SEQ_DEFER 2
963 wpa_ft_rrb_seq_chk(struct ft_remote_seq
*rkh_seq
, const u8
*src_addr
,
964 const u8
*enc
, size_t enc_len
,
965 const u8
*auth
, size_t auth_len
,
966 const char *msgtype
, int no_defer
)
970 const struct ft_rrb_seq
*msg_both
;
971 u32 msg_seq
, msg_off
, rkh_off
;
972 struct os_reltime now
;
975 RRB_GET_AUTH(FT_RRB_SEQ
, seq
, msgtype
, sizeof(*msg_both
));
976 wpa_hexdump(MSG_DEBUG
, "FT: sequence number", f_seq
, f_seq_len
);
977 msg_both
= (const struct ft_rrb_seq
*) f_seq
;
979 if (rkh_seq
->rx
.num_last
== 0) {
980 /* first packet from remote */
984 if (le_to_host32(msg_both
->dom
) != rkh_seq
->rx
.dom
) {
985 /* remote might have rebooted */
989 if (os_get_reltime(&now
) == 0) {
990 u32 msg_ts_now_remote
, msg_ts_off
;
991 struct os_reltime now_remote
;
993 os_reltime_sub(&now
, &rkh_seq
->rx
.time_offset
, &now_remote
);
994 msg_ts_now_remote
= now_remote
.sec
;
995 msg_ts_off
= le_to_host32(msg_both
->ts
) -
996 (msg_ts_now_remote
- ftRRBseqTimeout
);
997 if (msg_ts_off
> 2 * ftRRBseqTimeout
)
1001 msg_seq
= le_to_host32(msg_both
->seq
);
1002 rkh_off
= rkh_seq
->rx
.last
[rkh_seq
->rx
.offsetidx
];
1003 msg_off
= msg_seq
- rkh_off
;
1004 if (msg_off
> 0xC0000000)
1005 goto out
; /* too old message, drop it */
1007 if (msg_off
<= 0x40000000) {
1008 for (i
= 0; i
< rkh_seq
->rx
.num_last
; i
++) {
1009 if (rkh_seq
->rx
.last
[i
] == msg_seq
)
1010 goto out
; /* duplicate message, drop it */
1013 return FT_RRB_SEQ_OK
;
1020 wpa_printf(MSG_DEBUG
, "FT: Possibly invalid sequence number in %s from "
1021 MACSTR
, msgtype
, MAC2STR(src_addr
));
1023 return FT_RRB_SEQ_DEFER
;
1025 wpa_printf(MSG_DEBUG
, "FT: Invalid sequence number in %s from " MACSTR
,
1026 msgtype
, MAC2STR(src_addr
));
1028 return FT_RRB_SEQ_DROP
;
1033 wpa_ft_rrb_seq_accept(struct wpa_authenticator
*wpa_auth
,
1034 struct ft_remote_seq
*rkh_seq
, const u8
*src_addr
,
1035 const u8
*auth
, size_t auth_len
,
1036 const char *msgtype
)
1040 const struct ft_rrb_seq
*msg_both
;
1041 u32 msg_seq
, msg_off
, min_off
, rkh_off
;
1045 RRB_GET_AUTH(FT_RRB_SEQ
, seq
, msgtype
, sizeof(*msg_both
));
1046 msg_both
= (const struct ft_rrb_seq
*) f_seq
;
1048 msg_seq
= le_to_host32(msg_both
->seq
);
1050 if (rkh_seq
->rx
.num_last
< FT_REMOTE_SEQ_BACKLOG
) {
1051 rkh_seq
->rx
.last
[rkh_seq
->rx
.num_last
] = msg_seq
;
1052 rkh_seq
->rx
.num_last
++;
1056 rkh_off
= rkh_seq
->rx
.last
[rkh_seq
->rx
.offsetidx
];
1057 for (i
= 0; i
< rkh_seq
->rx
.num_last
; i
++) {
1058 msg_off
= rkh_seq
->rx
.last
[i
] - rkh_off
;
1059 min_off
= rkh_seq
->rx
.last
[minidx
] - rkh_off
;
1060 if (msg_off
< min_off
&& i
!= rkh_seq
->rx
.offsetidx
)
1063 rkh_seq
->rx
.last
[rkh_seq
->rx
.offsetidx
] = msg_seq
;
1064 rkh_seq
->rx
.offsetidx
= minidx
;
1068 /* RRB_GET_AUTH should never fail here as
1069 * wpa_ft_rrb_seq_chk() verified FT_RRB_SEQ presence. */
1070 wpa_printf(MSG_ERROR
, "FT: %s() failed", __func__
);
1074 static int wpa_ft_new_seq(struct ft_remote_seq
*rkh_seq
,
1075 struct ft_rrb_seq
*f_seq
)
1077 struct os_reltime now
;
1079 if (os_get_reltime(&now
) < 0)
1082 if (!rkh_seq
->tx
.dom
) {
1083 if (random_get_bytes((u8
*) &rkh_seq
->tx
.seq
,
1084 sizeof(rkh_seq
->tx
.seq
))) {
1085 wpa_printf(MSG_ERROR
,
1086 "FT: Failed to get random data for sequence number initialization");
1087 rkh_seq
->tx
.seq
= now
.usec
;
1089 if (random_get_bytes((u8
*) &rkh_seq
->tx
.dom
,
1090 sizeof(rkh_seq
->tx
.dom
))) {
1091 wpa_printf(MSG_ERROR
,
1092 "FT: Failed to get random data for sequence number initialization");
1093 rkh_seq
->tx
.dom
= now
.usec
;
1095 rkh_seq
->tx
.dom
|= 1;
1098 f_seq
->dom
= host_to_le32(rkh_seq
->tx
.dom
);
1099 f_seq
->seq
= host_to_le32(rkh_seq
->tx
.seq
);
1100 f_seq
->ts
= host_to_le32(now
.sec
);
1108 struct wpa_ft_pmk_r0_sa
{
1109 struct dl_list list
;
1110 u8 pmk_r0
[PMK_LEN_MAX
];
1112 u8 pmk_r0_name
[WPA_PMK_NAME_LEN
];
1114 int pairwise
; /* Pairwise cipher suite, WPA_CIPHER_* */
1115 struct vlan_description
*vlan
;
1116 os_time_t expiration
; /* 0 for no expiration */
1118 size_t identity_len
;
1120 size_t radius_cui_len
;
1121 os_time_t session_timeout
; /* 0 for no expiration */
1122 /* TODO: radius_class, EAP type */
1126 struct wpa_ft_pmk_r1_sa
{
1127 struct dl_list list
;
1128 u8 pmk_r1
[PMK_LEN_MAX
];
1130 u8 pmk_r1_name
[WPA_PMK_NAME_LEN
];
1132 int pairwise
; /* Pairwise cipher suite, WPA_CIPHER_* */
1133 struct vlan_description
*vlan
;
1135 size_t identity_len
;
1137 size_t radius_cui_len
;
1138 os_time_t session_timeout
; /* 0 for no expiration */
1139 /* TODO: radius_class, EAP type */
1142 struct wpa_ft_pmk_cache
{
1143 struct dl_list pmk_r0
; /* struct wpa_ft_pmk_r0_sa */
1144 struct dl_list pmk_r1
; /* struct wpa_ft_pmk_r1_sa */
1148 static void wpa_ft_expire_pmk_r0(void *eloop_ctx
, void *timeout_ctx
);
1149 static void wpa_ft_expire_pmk_r1(void *eloop_ctx
, void *timeout_ctx
);
1152 static void wpa_ft_free_pmk_r0(struct wpa_ft_pmk_r0_sa
*r0
)
1157 dl_list_del(&r0
->list
);
1158 eloop_cancel_timeout(wpa_ft_expire_pmk_r0
, r0
, NULL
);
1160 os_memset(r0
->pmk_r0
, 0, PMK_LEN_MAX
);
1162 os_free(r0
->identity
);
1163 os_free(r0
->radius_cui
);
1168 static void wpa_ft_expire_pmk_r0(void *eloop_ctx
, void *timeout_ctx
)
1170 struct wpa_ft_pmk_r0_sa
*r0
= eloop_ctx
;
1171 struct os_reltime now
;
1173 int session_timeout
;
1175 os_get_reltime(&now
);
1180 expires_in
= r0
->expiration
- now
.sec
;
1181 session_timeout
= r0
->session_timeout
- now
.sec
;
1182 /* conditions to remove from cache:
1183 * a) r0->expiration is set and hit
1185 * b) r0->session_timeout is set and hit
1187 if ((!r0
->expiration
|| expires_in
> 0) &&
1188 (!r0
->session_timeout
|| session_timeout
> 0)) {
1189 wpa_printf(MSG_ERROR
,
1190 "FT: %s() called for non-expired entry %p",
1192 eloop_cancel_timeout(wpa_ft_expire_pmk_r0
, r0
, NULL
);
1193 if (r0
->expiration
&& expires_in
> 0)
1194 eloop_register_timeout(expires_in
+ 1, 0,
1195 wpa_ft_expire_pmk_r0
, r0
, NULL
);
1196 if (r0
->session_timeout
&& session_timeout
> 0)
1197 eloop_register_timeout(session_timeout
+ 1, 0,
1198 wpa_ft_expire_pmk_r0
, r0
, NULL
);
1202 wpa_ft_free_pmk_r0(r0
);
1206 static void wpa_ft_free_pmk_r1(struct wpa_ft_pmk_r1_sa
*r1
)
1211 dl_list_del(&r1
->list
);
1212 eloop_cancel_timeout(wpa_ft_expire_pmk_r1
, r1
, NULL
);
1214 os_memset(r1
->pmk_r1
, 0, PMK_LEN_MAX
);
1216 os_free(r1
->identity
);
1217 os_free(r1
->radius_cui
);
1222 static void wpa_ft_expire_pmk_r1(void *eloop_ctx
, void *timeout_ctx
)
1224 struct wpa_ft_pmk_r1_sa
*r1
= eloop_ctx
;
1226 wpa_ft_free_pmk_r1(r1
);
1230 struct wpa_ft_pmk_cache
* wpa_ft_pmk_cache_init(void)
1232 struct wpa_ft_pmk_cache
*cache
;
1234 cache
= os_zalloc(sizeof(*cache
));
1236 dl_list_init(&cache
->pmk_r0
);
1237 dl_list_init(&cache
->pmk_r1
);
1244 void wpa_ft_pmk_cache_deinit(struct wpa_ft_pmk_cache
*cache
)
1246 struct wpa_ft_pmk_r0_sa
*r0
, *r0prev
;
1247 struct wpa_ft_pmk_r1_sa
*r1
, *r1prev
;
1249 dl_list_for_each_safe(r0
, r0prev
, &cache
->pmk_r0
,
1250 struct wpa_ft_pmk_r0_sa
, list
)
1251 wpa_ft_free_pmk_r0(r0
);
1253 dl_list_for_each_safe(r1
, r1prev
, &cache
->pmk_r1
,
1254 struct wpa_ft_pmk_r1_sa
, list
)
1255 wpa_ft_free_pmk_r1(r1
);
1261 static int wpa_ft_store_pmk_r0(struct wpa_authenticator
*wpa_auth
,
1262 const u8
*spa
, const u8
*pmk_r0
,
1264 const u8
*pmk_r0_name
, int pairwise
,
1265 const struct vlan_description
*vlan
,
1266 int expires_in
, int session_timeout
,
1267 const u8
*identity
, size_t identity_len
,
1268 const u8
*radius_cui
, size_t radius_cui_len
)
1270 struct wpa_ft_pmk_cache
*cache
= wpa_auth
->ft_pmk_cache
;
1271 struct wpa_ft_pmk_r0_sa
*r0
;
1272 struct os_reltime now
;
1274 /* TODO: add limit on number of entries in cache */
1275 os_get_reltime(&now
);
1277 r0
= os_zalloc(sizeof(*r0
));
1281 os_memcpy(r0
->pmk_r0
, pmk_r0
, pmk_r0_len
);
1282 r0
->pmk_r0_len
= pmk_r0_len
;
1283 os_memcpy(r0
->pmk_r0_name
, pmk_r0_name
, WPA_PMK_NAME_LEN
);
1284 os_memcpy(r0
->spa
, spa
, ETH_ALEN
);
1285 r0
->pairwise
= pairwise
;
1287 r0
->expiration
= now
.sec
+ expires_in
;
1288 if (vlan
&& vlan
->notempty
) {
1289 r0
->vlan
= os_zalloc(sizeof(*vlan
));
1291 bin_clear_free(r0
, sizeof(*r0
));
1297 r0
->identity
= os_malloc(identity_len
);
1299 os_memcpy(r0
->identity
, identity
, identity_len
);
1300 r0
->identity_len
= identity_len
;
1304 r0
->radius_cui
= os_malloc(radius_cui_len
);
1305 if (r0
->radius_cui
) {
1306 os_memcpy(r0
->radius_cui
, radius_cui
, radius_cui_len
);
1307 r0
->radius_cui_len
= radius_cui_len
;
1310 if (session_timeout
> 0)
1311 r0
->session_timeout
= now
.sec
+ session_timeout
;
1313 dl_list_add(&cache
->pmk_r0
, &r0
->list
);
1315 eloop_register_timeout(expires_in
+ 1, 0, wpa_ft_expire_pmk_r0
,
1317 if (session_timeout
> 0)
1318 eloop_register_timeout(session_timeout
+ 1, 0,
1319 wpa_ft_expire_pmk_r0
, r0
, NULL
);
1325 static int wpa_ft_fetch_pmk_r0(struct wpa_authenticator
*wpa_auth
,
1326 const u8
*spa
, const u8
*pmk_r0_name
,
1327 const struct wpa_ft_pmk_r0_sa
**r0_out
)
1329 struct wpa_ft_pmk_cache
*cache
= wpa_auth
->ft_pmk_cache
;
1330 struct wpa_ft_pmk_r0_sa
*r0
;
1331 struct os_reltime now
;
1333 os_get_reltime(&now
);
1334 dl_list_for_each(r0
, &cache
->pmk_r0
, struct wpa_ft_pmk_r0_sa
, list
) {
1335 if (os_memcmp(r0
->spa
, spa
, ETH_ALEN
) == 0 &&
1336 os_memcmp_const(r0
->pmk_r0_name
, pmk_r0_name
,
1337 WPA_PMK_NAME_LEN
) == 0) {
1348 static int wpa_ft_store_pmk_r1(struct wpa_authenticator
*wpa_auth
,
1349 const u8
*spa
, const u8
*pmk_r1
,
1351 const u8
*pmk_r1_name
, int pairwise
,
1352 const struct vlan_description
*vlan
,
1353 int expires_in
, int session_timeout
,
1354 const u8
*identity
, size_t identity_len
,
1355 const u8
*radius_cui
, size_t radius_cui_len
)
1357 struct wpa_ft_pmk_cache
*cache
= wpa_auth
->ft_pmk_cache
;
1358 int max_expires_in
= wpa_auth
->conf
.r1_max_key_lifetime
;
1359 struct wpa_ft_pmk_r1_sa
*r1
;
1360 struct os_reltime now
;
1362 /* TODO: limit on number of entries in cache */
1363 os_get_reltime(&now
);
1365 if (max_expires_in
&& (max_expires_in
< expires_in
|| expires_in
== 0))
1366 expires_in
= max_expires_in
;
1368 r1
= os_zalloc(sizeof(*r1
));
1372 os_memcpy(r1
->pmk_r1
, pmk_r1
, pmk_r1_len
);
1373 r1
->pmk_r1_len
= pmk_r1_len
;
1374 os_memcpy(r1
->pmk_r1_name
, pmk_r1_name
, WPA_PMK_NAME_LEN
);
1375 os_memcpy(r1
->spa
, spa
, ETH_ALEN
);
1376 r1
->pairwise
= pairwise
;
1377 if (vlan
&& vlan
->notempty
) {
1378 r1
->vlan
= os_zalloc(sizeof(*vlan
));
1380 bin_clear_free(r1
, sizeof(*r1
));
1386 r1
->identity
= os_malloc(identity_len
);
1388 os_memcpy(r1
->identity
, identity
, identity_len
);
1389 r1
->identity_len
= identity_len
;
1393 r1
->radius_cui
= os_malloc(radius_cui_len
);
1394 if (r1
->radius_cui
) {
1395 os_memcpy(r1
->radius_cui
, radius_cui
, radius_cui_len
);
1396 r1
->radius_cui_len
= radius_cui_len
;
1399 if (session_timeout
> 0)
1400 r1
->session_timeout
= now
.sec
+ session_timeout
;
1402 dl_list_add(&cache
->pmk_r1
, &r1
->list
);
1405 eloop_register_timeout(expires_in
+ 1, 0, wpa_ft_expire_pmk_r1
,
1407 if (session_timeout
> 0)
1408 eloop_register_timeout(session_timeout
+ 1, 0,
1409 wpa_ft_expire_pmk_r1
, r1
, NULL
);
1415 static int wpa_ft_fetch_pmk_r1(struct wpa_authenticator
*wpa_auth
,
1416 const u8
*spa
, const u8
*pmk_r1_name
,
1417 u8
*pmk_r1
, size_t *pmk_r1_len
, int *pairwise
,
1418 struct vlan_description
*vlan
,
1419 const u8
**identity
, size_t *identity_len
,
1420 const u8
**radius_cui
, size_t *radius_cui_len
,
1421 int *session_timeout
)
1423 struct wpa_ft_pmk_cache
*cache
= wpa_auth
->ft_pmk_cache
;
1424 struct wpa_ft_pmk_r1_sa
*r1
;
1425 struct os_reltime now
;
1427 os_get_reltime(&now
);
1429 dl_list_for_each(r1
, &cache
->pmk_r1
, struct wpa_ft_pmk_r1_sa
, list
) {
1430 if (os_memcmp(r1
->spa
, spa
, ETH_ALEN
) == 0 &&
1431 os_memcmp_const(r1
->pmk_r1_name
, pmk_r1_name
,
1432 WPA_PMK_NAME_LEN
) == 0) {
1433 os_memcpy(pmk_r1
, r1
->pmk_r1
, r1
->pmk_r1_len
);
1434 *pmk_r1_len
= r1
->pmk_r1_len
;
1436 *pairwise
= r1
->pairwise
;
1437 if (vlan
&& r1
->vlan
)
1439 if (vlan
&& !r1
->vlan
)
1440 os_memset(vlan
, 0, sizeof(*vlan
));
1441 if (identity
&& identity_len
) {
1442 *identity
= r1
->identity
;
1443 *identity_len
= r1
->identity_len
;
1445 if (radius_cui
&& radius_cui_len
) {
1446 *radius_cui
= r1
->radius_cui
;
1447 *radius_cui_len
= r1
->radius_cui_len
;
1449 if (session_timeout
&& r1
->session_timeout
> now
.sec
)
1450 *session_timeout
= r1
->session_timeout
-
1452 else if (session_timeout
&& r1
->session_timeout
)
1453 *session_timeout
= 1;
1455 *session_timeout
= 0;
1464 static int wpa_ft_rrb_init_r0kh_seq(struct ft_remote_r0kh
*r0kh
)
1469 r0kh
->seq
= os_zalloc(sizeof(*r0kh
->seq
));
1471 wpa_printf(MSG_DEBUG
, "FT: Failed to allocate r0kh->seq");
1475 dl_list_init(&r0kh
->seq
->rx
.queue
);
1481 static void wpa_ft_rrb_lookup_r0kh(struct wpa_authenticator
*wpa_auth
,
1482 const u8
*f_r0kh_id
, size_t f_r0kh_id_len
,
1483 struct ft_remote_r0kh
**r0kh_out
,
1484 struct ft_remote_r0kh
**r0kh_wildcard
)
1486 struct ft_remote_r0kh
*r0kh
;
1488 *r0kh_wildcard
= NULL
;
1491 if (wpa_auth
->conf
.r0kh_list
)
1492 r0kh
= *wpa_auth
->conf
.r0kh_list
;
1495 for (; r0kh
; r0kh
= r0kh
->next
) {
1496 if (r0kh
->id_len
== 1 && r0kh
->id
[0] == '*')
1497 *r0kh_wildcard
= r0kh
;
1498 if (f_r0kh_id
&& r0kh
->id_len
== f_r0kh_id_len
&&
1499 os_memcmp_const(f_r0kh_id
, r0kh
->id
, f_r0kh_id_len
) == 0)
1503 if (!*r0kh_out
&& !*r0kh_wildcard
)
1504 wpa_printf(MSG_DEBUG
, "FT: No matching R0KH found");
1506 if (*r0kh_out
&& wpa_ft_rrb_init_r0kh_seq(*r0kh_out
) < 0)
1511 static int wpa_ft_rrb_init_r1kh_seq(struct ft_remote_r1kh
*r1kh
)
1516 r1kh
->seq
= os_zalloc(sizeof(*r1kh
->seq
));
1518 wpa_printf(MSG_DEBUG
, "FT: Failed to allocate r1kh->seq");
1522 dl_list_init(&r1kh
->seq
->rx
.queue
);
1528 static void wpa_ft_rrb_lookup_r1kh(struct wpa_authenticator
*wpa_auth
,
1529 const u8
*f_r1kh_id
,
1530 struct ft_remote_r1kh
**r1kh_out
,
1531 struct ft_remote_r1kh
**r1kh_wildcard
)
1533 struct ft_remote_r1kh
*r1kh
;
1535 *r1kh_wildcard
= NULL
;
1538 if (wpa_auth
->conf
.r1kh_list
)
1539 r1kh
= *wpa_auth
->conf
.r1kh_list
;
1542 for (; r1kh
; r1kh
= r1kh
->next
) {
1543 if (is_zero_ether_addr(r1kh
->addr
) &&
1544 is_zero_ether_addr(r1kh
->id
))
1545 *r1kh_wildcard
= r1kh
;
1547 os_memcmp_const(r1kh
->id
, f_r1kh_id
, FT_R1KH_ID_LEN
) == 0)
1551 if (!*r1kh_out
&& !*r1kh_wildcard
)
1552 wpa_printf(MSG_DEBUG
, "FT: No matching R1KH found");
1554 if (*r1kh_out
&& wpa_ft_rrb_init_r1kh_seq(*r1kh_out
) < 0)
1559 static int wpa_ft_rrb_check_r0kh(struct wpa_authenticator
*wpa_auth
,
1560 const u8
*f_r0kh_id
, size_t f_r0kh_id_len
)
1562 if (f_r0kh_id_len
!= wpa_auth
->conf
.r0_key_holder_len
||
1563 os_memcmp_const(f_r0kh_id
, wpa_auth
->conf
.r0_key_holder
,
1564 f_r0kh_id_len
) != 0)
1571 static int wpa_ft_rrb_check_r1kh(struct wpa_authenticator
*wpa_auth
,
1572 const u8
*f_r1kh_id
)
1574 if (os_memcmp_const(f_r1kh_id
, wpa_auth
->conf
.r1_key_holder
,
1575 FT_R1KH_ID_LEN
) != 0)
1582 static void wpa_ft_rrb_del_r0kh(void *eloop_ctx
, void *timeout_ctx
)
1584 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
1585 struct ft_remote_r0kh
*r0kh
, *prev
= NULL
;
1587 if (!wpa_auth
->conf
.r0kh_list
)
1590 for (r0kh
= *wpa_auth
->conf
.r0kh_list
; r0kh
; r0kh
= r0kh
->next
) {
1591 if (r0kh
== timeout_ctx
)
1598 prev
->next
= r0kh
->next
;
1600 *wpa_auth
->conf
.r0kh_list
= r0kh
->next
;
1602 wpa_ft_rrb_seq_flush(wpa_auth
, r0kh
->seq
, 0);
1608 static void wpa_ft_rrb_r0kh_replenish(struct wpa_authenticator
*wpa_auth
,
1609 struct ft_remote_r0kh
*r0kh
, int timeout
)
1612 eloop_replenish_timeout(timeout
, 0, wpa_ft_rrb_del_r0kh
,
1617 static void wpa_ft_rrb_r0kh_timeout(struct wpa_authenticator
*wpa_auth
,
1618 struct ft_remote_r0kh
*r0kh
, int timeout
)
1620 eloop_cancel_timeout(wpa_ft_rrb_del_r0kh
, wpa_auth
, r0kh
);
1623 eloop_register_timeout(timeout
, 0, wpa_ft_rrb_del_r0kh
,
1628 static struct ft_remote_r0kh
*
1629 wpa_ft_rrb_add_r0kh(struct wpa_authenticator
*wpa_auth
,
1630 struct ft_remote_r0kh
*r0kh_wildcard
,
1631 const u8
*src_addr
, const u8
*r0kh_id
, size_t id_len
,
1634 struct ft_remote_r0kh
*r0kh
;
1636 if (!wpa_auth
->conf
.r0kh_list
)
1639 r0kh
= os_zalloc(sizeof(*r0kh
));
1644 os_memcpy(r0kh
->addr
, src_addr
, sizeof(r0kh
->addr
));
1646 if (id_len
> FT_R0KH_ID_MAX_LEN
)
1647 id_len
= FT_R0KH_ID_MAX_LEN
;
1648 os_memcpy(r0kh
->id
, r0kh_id
, id_len
);
1649 r0kh
->id_len
= id_len
;
1651 os_memcpy(r0kh
->key
, r0kh_wildcard
->key
, sizeof(r0kh
->key
));
1653 r0kh
->next
= *wpa_auth
->conf
.r0kh_list
;
1654 *wpa_auth
->conf
.r0kh_list
= r0kh
;
1657 eloop_register_timeout(timeout
, 0, wpa_ft_rrb_del_r0kh
,
1660 if (wpa_ft_rrb_init_r0kh_seq(r0kh
) < 0)
1667 static void wpa_ft_rrb_del_r1kh(void *eloop_ctx
, void *timeout_ctx
)
1669 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
1670 struct ft_remote_r1kh
*r1kh
, *prev
= NULL
;
1672 if (!wpa_auth
->conf
.r1kh_list
)
1675 for (r1kh
= *wpa_auth
->conf
.r1kh_list
; r1kh
; r1kh
= r1kh
->next
) {
1676 if (r1kh
== timeout_ctx
)
1683 prev
->next
= r1kh
->next
;
1685 *wpa_auth
->conf
.r1kh_list
= r1kh
->next
;
1687 wpa_ft_rrb_seq_flush(wpa_auth
, r1kh
->seq
, 0);
1693 static void wpa_ft_rrb_r1kh_replenish(struct wpa_authenticator
*wpa_auth
,
1694 struct ft_remote_r1kh
*r1kh
, int timeout
)
1697 eloop_replenish_timeout(timeout
, 0, wpa_ft_rrb_del_r1kh
,
1702 static struct ft_remote_r1kh
*
1703 wpa_ft_rrb_add_r1kh(struct wpa_authenticator
*wpa_auth
,
1704 struct ft_remote_r1kh
*r1kh_wildcard
,
1705 const u8
*src_addr
, const u8
*r1kh_id
, int timeout
)
1707 struct ft_remote_r1kh
*r1kh
;
1709 if (!wpa_auth
->conf
.r1kh_list
)
1712 r1kh
= os_zalloc(sizeof(*r1kh
));
1716 os_memcpy(r1kh
->addr
, src_addr
, sizeof(r1kh
->addr
));
1717 os_memcpy(r1kh
->id
, r1kh_id
, sizeof(r1kh
->id
));
1718 os_memcpy(r1kh
->key
, r1kh_wildcard
->key
, sizeof(r1kh
->key
));
1719 r1kh
->next
= *wpa_auth
->conf
.r1kh_list
;
1720 *wpa_auth
->conf
.r1kh_list
= r1kh
;
1723 eloop_register_timeout(timeout
, 0, wpa_ft_rrb_del_r1kh
,
1726 if (wpa_ft_rrb_init_r1kh_seq(r1kh
) < 0)
1733 void wpa_ft_sta_deinit(struct wpa_state_machine
*sm
)
1735 eloop_cancel_timeout(wpa_ft_expire_pull
, sm
, NULL
);
1739 static void wpa_ft_deinit_seq(struct wpa_authenticator
*wpa_auth
)
1741 struct ft_remote_r0kh
*r0kh
;
1742 struct ft_remote_r1kh
*r1kh
;
1744 eloop_cancel_timeout(wpa_ft_rrb_seq_timeout
, wpa_auth
, ELOOP_ALL_CTX
);
1746 if (wpa_auth
->conf
.r0kh_list
)
1747 r0kh
= *wpa_auth
->conf
.r0kh_list
;
1750 for (; r0kh
; r0kh
= r0kh
->next
) {
1753 wpa_ft_rrb_seq_flush(wpa_auth
, r0kh
->seq
, 0);
1758 if (wpa_auth
->conf
.r1kh_list
)
1759 r1kh
= *wpa_auth
->conf
.r1kh_list
;
1762 for (; r1kh
; r1kh
= r1kh
->next
) {
1765 wpa_ft_rrb_seq_flush(wpa_auth
, r1kh
->seq
, 0);
1772 static void wpa_ft_deinit_rkh_tmp(struct wpa_authenticator
*wpa_auth
)
1774 struct ft_remote_r0kh
*r0kh
, *r0kh_next
, *r0kh_prev
= NULL
;
1775 struct ft_remote_r1kh
*r1kh
, *r1kh_next
, *r1kh_prev
= NULL
;
1777 if (wpa_auth
->conf
.r0kh_list
)
1778 r0kh
= *wpa_auth
->conf
.r0kh_list
;
1782 r0kh_next
= r0kh
->next
;
1783 if (eloop_cancel_timeout(wpa_ft_rrb_del_r0kh
, wpa_auth
,
1786 r0kh_prev
->next
= r0kh_next
;
1788 *wpa_auth
->conf
.r0kh_list
= r0kh_next
;
1796 if (wpa_auth
->conf
.r1kh_list
)
1797 r1kh
= *wpa_auth
->conf
.r1kh_list
;
1801 r1kh_next
= r1kh
->next
;
1802 if (eloop_cancel_timeout(wpa_ft_rrb_del_r1kh
, wpa_auth
,
1805 r1kh_prev
->next
= r1kh_next
;
1807 *wpa_auth
->conf
.r1kh_list
= r1kh_next
;
1817 void wpa_ft_deinit(struct wpa_authenticator
*wpa_auth
)
1819 wpa_ft_deinit_seq(wpa_auth
);
1820 wpa_ft_deinit_rkh_tmp(wpa_auth
);
1824 static void wpa_ft_block_r0kh(struct wpa_authenticator
*wpa_auth
,
1825 const u8
*f_r0kh_id
, size_t f_r0kh_id_len
)
1827 struct ft_remote_r0kh
*r0kh
, *r0kh_wildcard
;
1829 if (!wpa_auth
->conf
.rkh_neg_timeout
)
1832 wpa_ft_rrb_lookup_r0kh(wpa_auth
, f_r0kh_id
, f_r0kh_id_len
,
1833 &r0kh
, &r0kh_wildcard
);
1835 if (!r0kh_wildcard
) {
1836 /* r0kh removed after neg_timeout and might need re-adding */
1840 wpa_hexdump(MSG_DEBUG
, "FT: Blacklist R0KH-ID",
1841 f_r0kh_id
, f_r0kh_id_len
);
1844 wpa_ft_rrb_r0kh_timeout(wpa_auth
, r0kh
,
1845 wpa_auth
->conf
.rkh_neg_timeout
);
1846 os_memset(r0kh
->addr
, 0, ETH_ALEN
);
1848 wpa_ft_rrb_add_r0kh(wpa_auth
, r0kh_wildcard
, NULL
, f_r0kh_id
,
1850 wpa_auth
->conf
.rkh_neg_timeout
);
1854 static void wpa_ft_expire_pull(void *eloop_ctx
, void *timeout_ctx
)
1856 struct wpa_state_machine
*sm
= eloop_ctx
;
1858 wpa_printf(MSG_DEBUG
, "FT: Timeout pending pull request for " MACSTR
,
1860 if (sm
->ft_pending_pull_left_retries
<= 0)
1861 wpa_ft_block_r0kh(sm
->wpa_auth
, sm
->r0kh_id
, sm
->r0kh_id_len
);
1863 /* cancel multiple timeouts */
1864 eloop_cancel_timeout(wpa_ft_expire_pull
, sm
, NULL
);
1869 static int wpa_ft_pull_pmk_r1(struct wpa_state_machine
*sm
,
1870 const u8
*ies
, size_t ies_len
,
1871 const u8
*pmk_r0_name
)
1873 struct ft_remote_r0kh
*r0kh
, *r0kh_wildcard
;
1875 const u8
*key
, *f_r1kh_id
= sm
->wpa_auth
->conf
.r1_key_holder
;
1876 size_t packet_len
, key_len
;
1877 struct ft_rrb_seq f_seq
;
1878 int tsecs
, tusecs
, first
;
1879 struct wpabuf
*ft_pending_req_ies
;
1881 struct tlv_list req_enc
[] = {
1882 { .type
= FT_RRB_PMK_R0_NAME
, .len
= WPA_PMK_NAME_LEN
,
1883 .data
= pmk_r0_name
},
1884 { .type
= FT_RRB_S1KH_ID
, .len
= ETH_ALEN
,
1886 { .type
= FT_RRB_LAST_EMPTY
, .len
= 0, .data
= NULL
},
1888 struct tlv_list req_auth
[] = {
1889 { .type
= FT_RRB_NONCE
, .len
= FT_RRB_NONCE_LEN
,
1890 .data
= sm
->ft_pending_pull_nonce
},
1891 { .type
= FT_RRB_SEQ
, .len
= sizeof(f_seq
),
1892 .data
= (u8
*) &f_seq
},
1893 { .type
= FT_RRB_R0KH_ID
, .len
= sm
->r0kh_id_len
,
1894 .data
= sm
->r0kh_id
},
1895 { .type
= FT_RRB_R1KH_ID
, .len
= FT_R1KH_ID_LEN
,
1896 .data
= f_r1kh_id
},
1897 { .type
= FT_RRB_LAST_EMPTY
, .len
= 0, .data
= NULL
},
1900 if (sm
->ft_pending_pull_left_retries
<= 0)
1902 first
= sm
->ft_pending_pull_left_retries
==
1903 sm
->wpa_auth
->conf
.rkh_pull_retries
;
1904 sm
->ft_pending_pull_left_retries
--;
1906 wpa_ft_rrb_lookup_r0kh(sm
->wpa_auth
, sm
->r0kh_id
, sm
->r0kh_id_len
,
1907 &r0kh
, &r0kh_wildcard
);
1909 /* Keep r0kh sufficiently long in the list for seq num check */
1910 r0kh_timeout
= sm
->wpa_auth
->conf
.rkh_pull_timeout
/ 1000 +
1911 1 + ftRRBseqTimeout
;
1913 wpa_ft_rrb_r0kh_replenish(sm
->wpa_auth
, r0kh
, r0kh_timeout
);
1914 } else if (r0kh_wildcard
) {
1915 wpa_printf(MSG_DEBUG
, "FT: Using wildcard R0KH-ID");
1916 /* r0kh->addr: updated by SEQ_RESP and wpa_ft_expire_pull */
1917 r0kh
= wpa_ft_rrb_add_r0kh(sm
->wpa_auth
, r0kh_wildcard
,
1918 r0kh_wildcard
->addr
,
1919 sm
->r0kh_id
, sm
->r0kh_id_len
,
1923 wpa_hexdump(MSG_DEBUG
, "FT: Did not find R0KH-ID",
1924 sm
->r0kh_id
, sm
->r0kh_id_len
);
1927 if (is_zero_ether_addr(r0kh
->addr
)) {
1928 wpa_hexdump(MSG_DEBUG
, "FT: R0KH-ID is blacklisted",
1929 sm
->r0kh_id
, sm
->r0kh_id_len
);
1932 if (os_memcmp(r0kh
->addr
, sm
->wpa_auth
->addr
, ETH_ALEN
) == 0) {
1933 wpa_printf(MSG_DEBUG
,
1934 "FT: R0KH-ID points to self - no matching key available");
1939 key_len
= sizeof(r0kh
->key
);
1941 wpa_printf(MSG_DEBUG
, "FT: Send PMK-R1 pull request to remote R0KH "
1942 "address " MACSTR
, MAC2STR(r0kh
->addr
));
1944 if (r0kh
->seq
->rx
.num_last
== 0) {
1945 /* A sequence request will be sent out anyway when pull
1946 * response is received. Send it out now to avoid one RTT. */
1947 wpa_ft_rrb_seq_req(sm
->wpa_auth
, r0kh
->seq
, r0kh
->addr
,
1948 r0kh
->id
, r0kh
->id_len
, f_r1kh_id
, key
,
1949 key_len
, NULL
, 0, NULL
, 0, NULL
);
1953 random_get_bytes(sm
->ft_pending_pull_nonce
, FT_RRB_NONCE_LEN
) < 0) {
1954 wpa_printf(MSG_DEBUG
, "FT: Failed to get random data for "
1959 if (wpa_ft_new_seq(r0kh
->seq
, &f_seq
) < 0) {
1960 wpa_printf(MSG_DEBUG
, "FT: Failed to get seq num");
1964 if (wpa_ft_rrb_build(key
, key_len
, req_enc
, NULL
, req_auth
, NULL
,
1965 sm
->wpa_auth
->addr
, FT_PACKET_R0KH_R1KH_PULL
,
1966 &packet
, &packet_len
) < 0)
1969 ft_pending_req_ies
= wpabuf_alloc_copy(ies
, ies_len
);
1970 wpabuf_free(sm
->ft_pending_req_ies
);
1971 sm
->ft_pending_req_ies
= ft_pending_req_ies
;
1972 if (!sm
->ft_pending_req_ies
) {
1977 tsecs
= sm
->wpa_auth
->conf
.rkh_pull_timeout
/ 1000;
1978 tusecs
= (sm
->wpa_auth
->conf
.rkh_pull_timeout
% 1000) * 1000;
1979 eloop_register_timeout(tsecs
, tusecs
, wpa_ft_expire_pull
, sm
, NULL
);
1981 wpa_ft_rrb_oui_send(sm
->wpa_auth
, r0kh
->addr
, FT_PACKET_R0KH_R1KH_PULL
,
1982 packet
, packet_len
);
1990 int wpa_ft_store_pmk_fils(struct wpa_state_machine
*sm
,
1991 const u8
*pmk_r0
, const u8
*pmk_r0_name
)
1993 int expires_in
= sm
->wpa_auth
->conf
.r0_key_lifetime
;
1994 struct vlan_description vlan
;
1995 const u8
*identity
, *radius_cui
;
1996 size_t identity_len
, radius_cui_len
;
1997 int session_timeout
;
1998 size_t pmk_r0_len
= wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
) ?
1999 SHA384_MAC_LEN
: PMK_LEN
;
2001 if (wpa_ft_get_vlan(sm
->wpa_auth
, sm
->addr
, &vlan
) < 0) {
2002 wpa_printf(MSG_DEBUG
, "FT: vlan not available for STA " MACSTR
,
2007 identity_len
= wpa_ft_get_identity(sm
->wpa_auth
, sm
->addr
, &identity
);
2008 radius_cui_len
= wpa_ft_get_radius_cui(sm
->wpa_auth
, sm
->addr
,
2010 session_timeout
= wpa_ft_get_session_timeout(sm
->wpa_auth
, sm
->addr
);
2012 return wpa_ft_store_pmk_r0(sm
->wpa_auth
, sm
->addr
, pmk_r0
, pmk_r0_len
,
2013 pmk_r0_name
, sm
->pairwise
, &vlan
, expires_in
,
2014 session_timeout
, identity
, identity_len
,
2015 radius_cui
, radius_cui_len
);
2019 int wpa_auth_derive_ptk_ft(struct wpa_state_machine
*sm
, const u8
*pmk
,
2020 struct wpa_ptk
*ptk
)
2022 u8 pmk_r0
[PMK_LEN_MAX
], pmk_r0_name
[WPA_PMK_NAME_LEN
];
2023 size_t pmk_r0_len
= wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
) ?
2024 SHA384_MAC_LEN
: PMK_LEN
;
2025 size_t pmk_r1_len
= pmk_r0_len
;
2026 u8 pmk_r1
[PMK_LEN_MAX
];
2027 u8 ptk_name
[WPA_PMK_NAME_LEN
];
2028 const u8
*mdid
= sm
->wpa_auth
->conf
.mobility_domain
;
2029 const u8
*r0kh
= sm
->wpa_auth
->conf
.r0_key_holder
;
2030 size_t r0kh_len
= sm
->wpa_auth
->conf
.r0_key_holder_len
;
2031 const u8
*r1kh
= sm
->wpa_auth
->conf
.r1_key_holder
;
2032 const u8
*ssid
= sm
->wpa_auth
->conf
.ssid
;
2033 size_t ssid_len
= sm
->wpa_auth
->conf
.ssid_len
;
2034 int psk_local
= sm
->wpa_auth
->conf
.ft_psk_generate_local
;
2035 int expires_in
= sm
->wpa_auth
->conf
.r0_key_lifetime
;
2036 struct vlan_description vlan
;
2037 const u8
*identity
, *radius_cui
;
2038 size_t identity_len
, radius_cui_len
;
2039 int session_timeout
;
2041 if (sm
->xxkey_len
== 0) {
2042 wpa_printf(MSG_DEBUG
, "FT: XXKey not available for key "
2047 if (wpa_ft_get_vlan(sm
->wpa_auth
, sm
->addr
, &vlan
) < 0) {
2048 wpa_printf(MSG_DEBUG
, "FT: vlan not available for STA " MACSTR
,
2053 identity_len
= wpa_ft_get_identity(sm
->wpa_auth
, sm
->addr
, &identity
);
2054 radius_cui_len
= wpa_ft_get_radius_cui(sm
->wpa_auth
, sm
->addr
,
2056 session_timeout
= wpa_ft_get_session_timeout(sm
->wpa_auth
, sm
->addr
);
2058 if (wpa_derive_pmk_r0(sm
->xxkey
, sm
->xxkey_len
, ssid
, ssid_len
, mdid
,
2059 r0kh
, r0kh_len
, sm
->addr
,
2060 pmk_r0
, pmk_r0_name
,
2061 wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
)) < 0)
2063 wpa_hexdump_key(MSG_DEBUG
, "FT: PMK-R0", pmk_r0
, pmk_r0_len
);
2064 wpa_hexdump(MSG_DEBUG
, "FT: PMKR0Name", pmk_r0_name
, WPA_PMK_NAME_LEN
);
2065 if (!psk_local
|| !wpa_key_mgmt_ft_psk(sm
->wpa_key_mgmt
))
2066 wpa_ft_store_pmk_r0(sm
->wpa_auth
, sm
->addr
, pmk_r0
, pmk_r0_len
,
2068 sm
->pairwise
, &vlan
, expires_in
,
2069 session_timeout
, identity
, identity_len
,
2070 radius_cui
, radius_cui_len
);
2072 if (wpa_derive_pmk_r1(pmk_r0
, pmk_r0_len
, pmk_r0_name
, r1kh
, sm
->addr
,
2073 pmk_r1
, sm
->pmk_r1_name
) < 0)
2075 wpa_hexdump_key(MSG_DEBUG
, "FT: PMK-R1", pmk_r1
, pmk_r1_len
);
2076 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name", sm
->pmk_r1_name
,
2078 if (!psk_local
|| !wpa_key_mgmt_ft_psk(sm
->wpa_key_mgmt
))
2079 wpa_ft_store_pmk_r1(sm
->wpa_auth
, sm
->addr
, pmk_r1
, pmk_r1_len
,
2080 sm
->pmk_r1_name
, sm
->pairwise
, &vlan
,
2081 expires_in
, session_timeout
, identity
,
2082 identity_len
, radius_cui
, radius_cui_len
);
2084 return wpa_pmk_r1_to_ptk(pmk_r1
, pmk_r1_len
, sm
->SNonce
, sm
->ANonce
,
2085 sm
->addr
, sm
->wpa_auth
->addr
, sm
->pmk_r1_name
,
2086 ptk
, ptk_name
, sm
->wpa_key_mgmt
, sm
->pairwise
);
2090 static inline int wpa_auth_get_seqnum(struct wpa_authenticator
*wpa_auth
,
2091 const u8
*addr
, int idx
, u8
*seq
)
2093 if (wpa_auth
->cb
->get_seqnum
== NULL
)
2095 return wpa_auth
->cb
->get_seqnum(wpa_auth
->cb_ctx
, addr
, idx
, seq
);
2099 static u8
* wpa_ft_gtk_subelem(struct wpa_state_machine
*sm
, size_t *len
)
2102 struct wpa_group
*gsm
= sm
->group
;
2103 size_t subelem_len
, pad_len
;
2110 if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) {
2112 kek_len
= sm
->PTK
.kek2_len
;
2115 kek_len
= sm
->PTK
.kek_len
;
2118 key_len
= gsm
->GTK_len
;
2119 if (key_len
> sizeof(keybuf
))
2123 * Pad key for AES Key Wrap if it is not multiple of 8 bytes or is less
2126 pad_len
= key_len
% 8;
2128 pad_len
= 8 - pad_len
;
2129 if (key_len
+ pad_len
< 16)
2131 if (pad_len
&& key_len
< sizeof(keybuf
)) {
2132 os_memcpy(keybuf
, gsm
->GTK
[gsm
->GN
- 1], key_len
);
2133 os_memset(keybuf
+ key_len
, 0, pad_len
);
2134 keybuf
[key_len
] = 0xdd;
2138 key
= gsm
->GTK
[gsm
->GN
- 1];
2141 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
2144 subelem_len
= 13 + key_len
+ 8;
2145 subelem
= os_zalloc(subelem_len
);
2146 if (subelem
== NULL
)
2149 subelem
[0] = FTIE_SUBELEM_GTK
;
2150 subelem
[1] = 11 + key_len
+ 8;
2151 /* Key ID in B0-B1 of Key Info */
2152 WPA_PUT_LE16(&subelem
[2], gsm
->GN
& 0x03);
2153 subelem
[4] = gsm
->GTK_len
;
2154 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, subelem
+ 5);
2155 if (aes_wrap(kek
, kek_len
, key_len
/ 8, key
, subelem
+ 13)) {
2156 wpa_printf(MSG_DEBUG
,
2157 "FT: GTK subelem encryption failed: kek_len=%d",
2168 #ifdef CONFIG_IEEE80211W
2169 static u8
* wpa_ft_igtk_subelem(struct wpa_state_machine
*sm
, size_t *len
)
2172 struct wpa_group
*gsm
= sm
->group
;
2177 if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) {
2179 kek_len
= sm
->PTK
.kek2_len
;
2182 kek_len
= sm
->PTK
.kek_len
;
2185 /* Sub-elem ID[1] | Length[1] | KeyID[2] | IPN[6] | Key Length[1] |
2187 subelem_len
= 1 + 1 + 2 + 6 + 1 + WPA_IGTK_LEN
+ 8;
2188 subelem
= os_zalloc(subelem_len
);
2189 if (subelem
== NULL
)
2193 *pos
++ = FTIE_SUBELEM_IGTK
;
2194 *pos
++ = subelem_len
- 2;
2195 WPA_PUT_LE16(pos
, gsm
->GN_igtk
);
2197 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, pos
);
2199 *pos
++ = WPA_IGTK_LEN
;
2200 if (aes_wrap(kek
, kek_len
, WPA_IGTK_LEN
/ 8,
2201 gsm
->IGTK
[gsm
->GN_igtk
- 4], pos
)) {
2202 wpa_printf(MSG_DEBUG
,
2203 "FT: IGTK subelem encryption failed: kek_len=%d",
2212 #endif /* CONFIG_IEEE80211W */
2215 static u8
* wpa_ft_process_rdie(struct wpa_state_machine
*sm
,
2216 u8
*pos
, u8
*end
, u8 id
, u8 descr_count
,
2217 const u8
*ies
, size_t ies_len
)
2219 struct ieee802_11_elems parse
;
2220 struct rsn_rdie
*rdie
;
2222 wpa_printf(MSG_DEBUG
, "FT: Resource Request: id=%d descr_count=%d",
2224 wpa_hexdump(MSG_MSGDUMP
, "FT: Resource descriptor IE(s)",
2227 if (end
- pos
< (int) sizeof(*rdie
)) {
2228 wpa_printf(MSG_ERROR
, "FT: Not enough room for response RDIE");
2232 *pos
++ = WLAN_EID_RIC_DATA
;
2233 *pos
++ = sizeof(*rdie
);
2234 rdie
= (struct rsn_rdie
*) pos
;
2236 rdie
->descr_count
= 0;
2237 rdie
->status_code
= host_to_le16(WLAN_STATUS_SUCCESS
);
2238 pos
+= sizeof(*rdie
);
2240 if (ieee802_11_parse_elems((u8
*) ies
, ies_len
, &parse
, 1) ==
2242 wpa_printf(MSG_DEBUG
, "FT: Failed to parse request IEs");
2244 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE
);
2248 if (parse
.wmm_tspec
) {
2249 struct wmm_tspec_element
*tspec
;
2251 if (parse
.wmm_tspec_len
+ 2 < (int) sizeof(*tspec
)) {
2252 wpa_printf(MSG_DEBUG
, "FT: Too short WMM TSPEC IE "
2253 "(%d)", (int) parse
.wmm_tspec_len
);
2255 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE
);
2258 if (end
- pos
< (int) sizeof(*tspec
)) {
2259 wpa_printf(MSG_ERROR
, "FT: Not enough room for "
2262 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE
);
2265 tspec
= (struct wmm_tspec_element
*) pos
;
2266 os_memcpy(tspec
, parse
.wmm_tspec
- 2, sizeof(*tspec
));
2270 if (parse
.wmm_tspec
&& sm
->wpa_auth
->conf
.ap_mlme
) {
2273 res
= wmm_process_tspec((struct wmm_tspec_element
*) pos
);
2274 wpa_printf(MSG_DEBUG
, "FT: ADDTS processing result: %d", res
);
2275 if (res
== WMM_ADDTS_STATUS_INVALID_PARAMETERS
)
2277 host_to_le16(WLAN_STATUS_INVALID_PARAMETERS
);
2278 else if (res
== WMM_ADDTS_STATUS_REFUSED
)
2280 host_to_le16(WLAN_STATUS_REQUEST_DECLINED
);
2282 /* TSPEC accepted; include updated TSPEC in response */
2283 rdie
->descr_count
= 1;
2284 pos
+= sizeof(struct wmm_tspec_element
);
2288 #endif /* NEED_AP_MLME */
2290 if (parse
.wmm_tspec
&& !sm
->wpa_auth
->conf
.ap_mlme
) {
2293 res
= wpa_ft_add_tspec(sm
->wpa_auth
, sm
->addr
, pos
,
2294 sizeof(struct wmm_tspec_element
));
2297 rdie
->status_code
= host_to_le16(res
);
2299 /* TSPEC accepted; include updated TSPEC in
2301 rdie
->descr_count
= 1;
2302 pos
+= sizeof(struct wmm_tspec_element
);
2308 wpa_printf(MSG_DEBUG
, "FT: No supported resource requested");
2309 rdie
->status_code
= host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE
);
2314 static u8
* wpa_ft_process_ric(struct wpa_state_machine
*sm
, u8
*pos
, u8
*end
,
2315 const u8
*ric
, size_t ric_len
)
2317 const u8
*rpos
, *start
;
2318 const struct rsn_rdie
*rdie
;
2320 wpa_hexdump(MSG_MSGDUMP
, "FT: RIC Request", ric
, ric_len
);
2323 while (rpos
+ sizeof(*rdie
) < ric
+ ric_len
) {
2324 if (rpos
[0] != WLAN_EID_RIC_DATA
|| rpos
[1] < sizeof(*rdie
) ||
2325 rpos
+ 2 + rpos
[1] > ric
+ ric_len
)
2327 rdie
= (const struct rsn_rdie
*) (rpos
+ 2);
2328 rpos
+= 2 + rpos
[1];
2331 while (rpos
+ 2 <= ric
+ ric_len
&&
2332 rpos
+ 2 + rpos
[1] <= ric
+ ric_len
) {
2333 if (rpos
[0] == WLAN_EID_RIC_DATA
)
2335 rpos
+= 2 + rpos
[1];
2337 pos
= wpa_ft_process_rdie(sm
, pos
, end
, rdie
->id
,
2339 start
, rpos
- start
);
2346 u8
* wpa_sm_write_assoc_resp_ies(struct wpa_state_machine
*sm
, u8
*pos
,
2347 size_t max_len
, int auth_alg
,
2348 const u8
*req_ies
, size_t req_ies_len
)
2350 u8
*end
, *mdie
, *ftie
, *rsnie
= NULL
, *r0kh_id
, *subelem
= NULL
;
2351 u8
*fte_mic
, *elem_count
;
2352 size_t mdie_len
, ftie_len
, rsnie_len
= 0, r0kh_id_len
, subelem_len
= 0;
2354 struct wpa_auth_config
*conf
;
2355 struct wpa_ft_ies parse
;
2357 u8
*anonce
, *snonce
;
2365 use_sha384
= wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
);
2366 conf
= &sm
->wpa_auth
->conf
;
2368 if (!wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
2371 end
= pos
+ max_len
;
2373 if (auth_alg
== WLAN_AUTH_FT
) {
2375 * RSN (only present if this is a Reassociation Response and
2376 * part of a fast BSS transition)
2378 res
= wpa_write_rsn_ie(conf
, pos
, end
- pos
, sm
->pmk_r1_name
);
2386 /* Mobility Domain Information */
2387 res
= wpa_write_mdie(conf
, pos
, end
- pos
);
2394 /* Fast BSS Transition Information */
2395 if (auth_alg
== WLAN_AUTH_FT
) {
2396 subelem
= wpa_ft_gtk_subelem(sm
, &subelem_len
);
2398 wpa_printf(MSG_DEBUG
,
2399 "FT: Failed to add GTK subelement");
2402 r0kh_id
= sm
->r0kh_id
;
2403 r0kh_id_len
= sm
->r0kh_id_len
;
2404 anonce
= sm
->ANonce
;
2405 snonce
= sm
->SNonce
;
2406 #ifdef CONFIG_IEEE80211W
2407 if (sm
->mgmt_frame_prot
) {
2411 igtk
= wpa_ft_igtk_subelem(sm
, &igtk_len
);
2413 wpa_printf(MSG_DEBUG
,
2414 "FT: Failed to add IGTK subelement");
2418 nbuf
= os_realloc(subelem
, subelem_len
+ igtk_len
);
2425 os_memcpy(subelem
+ subelem_len
, igtk
, igtk_len
);
2426 subelem_len
+= igtk_len
;
2429 #endif /* CONFIG_IEEE80211W */
2431 r0kh_id
= conf
->r0_key_holder
;
2432 r0kh_id_len
= conf
->r0_key_holder_len
;
2436 res
= wpa_write_ftie(conf
, use_sha384
, r0kh_id
, r0kh_id_len
,
2437 anonce
, snonce
, pos
, end
- pos
,
2438 subelem
, subelem_len
);
2447 struct rsn_ftie_sha384
*_ftie
=
2448 (struct rsn_ftie_sha384
*) (ftie
+ 2);
2450 fte_mic
= _ftie
->mic
;
2451 elem_count
= &_ftie
->mic_control
[1];
2453 struct rsn_ftie
*_ftie
= (struct rsn_ftie
*) (ftie
+ 2);
2455 fte_mic
= _ftie
->mic
;
2456 elem_count
= &_ftie
->mic_control
[1];
2458 if (auth_alg
== WLAN_AUTH_FT
)
2459 *elem_count
= 3; /* Information element count */
2462 if (wpa_ft_parse_ies(req_ies
, req_ies_len
, &parse
, use_sha384
) == 0
2464 pos
= wpa_ft_process_ric(sm
, pos
, end
, parse
.ric
,
2466 if (auth_alg
== WLAN_AUTH_FT
)
2468 ieee802_11_ie_count(ric_start
,
2471 if (ric_start
== pos
)
2474 if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) {
2476 kck_len
= sm
->PTK
.kck2_len
;
2479 kck_len
= sm
->PTK
.kck_len
;
2481 if (auth_alg
== WLAN_AUTH_FT
&&
2482 wpa_ft_mic(kck
, kck_len
, sm
->addr
, sm
->wpa_auth
->addr
, 6,
2483 mdie
, mdie_len
, ftie
, ftie_len
,
2485 ric_start
, ric_start
? pos
- ric_start
: 0,
2487 wpa_printf(MSG_DEBUG
, "FT: Failed to calculate MIC");
2491 os_free(sm
->assoc_resp_ftie
);
2492 sm
->assoc_resp_ftie
= os_malloc(ftie_len
);
2493 if (!sm
->assoc_resp_ftie
)
2495 os_memcpy(sm
->assoc_resp_ftie
, ftie
, ftie_len
);
2501 static inline int wpa_auth_set_key(struct wpa_authenticator
*wpa_auth
,
2503 enum wpa_alg alg
, const u8
*addr
, int idx
,
2504 u8
*key
, size_t key_len
)
2506 if (wpa_auth
->cb
->set_key
== NULL
)
2508 return wpa_auth
->cb
->set_key(wpa_auth
->cb_ctx
, vlan_id
, alg
, addr
, idx
,
2513 void wpa_ft_install_ptk(struct wpa_state_machine
*sm
)
2518 /* MLME-SETKEYS.request(PTK) */
2519 alg
= wpa_cipher_to_alg(sm
->pairwise
);
2520 klen
= wpa_cipher_key_len(sm
->pairwise
);
2521 if (!wpa_cipher_valid_pairwise(sm
->pairwise
)) {
2522 wpa_printf(MSG_DEBUG
, "FT: Unknown pairwise alg 0x%x - skip "
2523 "PTK configuration", sm
->pairwise
);
2527 if (sm
->tk_already_set
) {
2528 /* Must avoid TK reconfiguration to prevent clearing of TX/RX
2529 * PN in the driver */
2530 wpa_printf(MSG_DEBUG
,
2531 "FT: Do not re-install same PTK to the driver");
2535 /* FIX: add STA entry to kernel/driver here? The set_key will fail
2536 * most likely without this.. At the moment, STA entry is added only
2537 * after association has been completed. This function will be called
2538 * again after association to get the PTK configured, but that could be
2539 * optimized by adding the STA entry earlier.
2541 if (wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
, 0,
2545 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
2546 sm
->pairwise_set
= TRUE
;
2547 sm
->tk_already_set
= TRUE
;
2551 /* Derive PMK-R1 from PSK, check all available PSK */
2552 static int wpa_ft_psk_pmk_r1(struct wpa_state_machine
*sm
,
2553 const u8
*req_pmk_r1_name
,
2554 u8
*out_pmk_r1
, int *out_pairwise
,
2555 struct vlan_description
*out_vlan
,
2556 const u8
**out_identity
, size_t *out_identity_len
,
2557 const u8
**out_radius_cui
,
2558 size_t *out_radius_cui_len
,
2559 int *out_session_timeout
)
2561 const u8
*pmk
= NULL
;
2562 u8 pmk_r0
[PMK_LEN
], pmk_r0_name
[WPA_PMK_NAME_LEN
];
2563 u8 pmk_r1
[PMK_LEN
], pmk_r1_name
[WPA_PMK_NAME_LEN
];
2564 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2565 const u8
*mdid
= wpa_auth
->conf
.mobility_domain
;
2566 const u8
*r0kh
= sm
->r0kh_id
;
2567 size_t r0kh_len
= sm
->r0kh_id_len
;
2568 const u8
*r1kh
= wpa_auth
->conf
.r1_key_holder
;
2569 const u8
*ssid
= wpa_auth
->conf
.ssid
;
2570 size_t ssid_len
= wpa_auth
->conf
.ssid_len
;
2573 pairwise
= sm
->pairwise
;
2576 pmk
= wpa_ft_get_psk(wpa_auth
, sm
->addr
, sm
->p2p_dev_addr
,
2581 if (wpa_derive_pmk_r0(pmk
, PMK_LEN
, ssid
, ssid_len
, mdid
, r0kh
,
2583 pmk_r0
, pmk_r0_name
, 0) < 0 ||
2584 wpa_derive_pmk_r1(pmk_r0
, PMK_LEN
, pmk_r0_name
, r1kh
,
2585 sm
->addr
, pmk_r1
, pmk_r1_name
) < 0 ||
2586 os_memcmp_const(pmk_r1_name
, req_pmk_r1_name
,
2587 WPA_PMK_NAME_LEN
) != 0)
2590 /* We found a PSK that matches the requested pmk_r1_name */
2591 wpa_printf(MSG_DEBUG
,
2592 "FT: Found PSK to generate PMK-R1 locally");
2593 os_memcpy(out_pmk_r1
, pmk_r1
, PMK_LEN
);
2595 *out_pairwise
= pairwise
;
2597 wpa_ft_get_vlan(sm
->wpa_auth
, sm
->addr
, out_vlan
) < 0) {
2598 wpa_printf(MSG_DEBUG
, "FT: vlan not available for STA "
2599 MACSTR
, MAC2STR(sm
->addr
));
2603 if (out_identity
&& out_identity_len
) {
2604 *out_identity_len
= wpa_ft_get_identity(
2605 sm
->wpa_auth
, sm
->addr
, out_identity
);
2608 if (out_radius_cui
&& out_radius_cui_len
) {
2609 *out_radius_cui_len
= wpa_ft_get_radius_cui(
2610 sm
->wpa_auth
, sm
->addr
, out_radius_cui
);
2613 if (out_session_timeout
) {
2614 *out_session_timeout
= wpa_ft_get_session_timeout(
2615 sm
->wpa_auth
, sm
->addr
);
2621 wpa_printf(MSG_DEBUG
,
2622 "FT: Did not find PSK to generate PMK-R1 locally");
2627 /* Detect the configuration the station asked for.
2628 * Required to detect FT-PSK and pairwise cipher.
2630 static int wpa_ft_set_key_mgmt(struct wpa_state_machine
*sm
,
2631 struct wpa_ft_ies
*parse
)
2633 int key_mgmt
, ciphers
;
2635 if (sm
->wpa_key_mgmt
)
2638 key_mgmt
= parse
->key_mgmt
& sm
->wpa_auth
->conf
.wpa_key_mgmt
;
2640 wpa_printf(MSG_DEBUG
, "FT: Invalid key mgmt (0x%x) from "
2641 MACSTR
, parse
->key_mgmt
, MAC2STR(sm
->addr
));
2644 if (key_mgmt
& WPA_KEY_MGMT_FT_IEEE8021X
)
2645 sm
->wpa_key_mgmt
= WPA_KEY_MGMT_FT_IEEE8021X
;
2646 #ifdef CONFIG_SHA384
2647 else if (key_mgmt
& WPA_KEY_MGMT_FT_IEEE8021X_SHA384
)
2648 sm
->wpa_key_mgmt
= WPA_KEY_MGMT_FT_IEEE8021X_SHA384
;
2649 #endif /* CONFIG_SHA384 */
2650 else if (key_mgmt
& WPA_KEY_MGMT_FT_PSK
)
2651 sm
->wpa_key_mgmt
= WPA_KEY_MGMT_FT_PSK
;
2653 else if (key_mgmt
& WPA_KEY_MGMT_FT_FILS_SHA256
)
2654 sm
->wpa_key_mgmt
= WPA_KEY_MGMT_FT_FILS_SHA256
;
2655 else if (key_mgmt
& WPA_KEY_MGMT_FT_FILS_SHA384
)
2656 sm
->wpa_key_mgmt
= WPA_KEY_MGMT_FT_FILS_SHA384
;
2657 #endif /* CONFIG_FILS */
2658 ciphers
= parse
->pairwise_cipher
& sm
->wpa_auth
->conf
.rsn_pairwise
;
2660 wpa_printf(MSG_DEBUG
, "FT: Invalid pairwise cipher (0x%x) from "
2662 parse
->pairwise_cipher
, MAC2STR(sm
->addr
));
2665 sm
->pairwise
= wpa_pick_pairwise_cipher(ciphers
, 0);
2671 static int wpa_ft_local_derive_pmk_r1(struct wpa_authenticator
*wpa_auth
,
2672 struct wpa_state_machine
*sm
,
2673 const u8
*r0kh_id
, size_t r0kh_id_len
,
2674 const u8
*req_pmk_r0_name
,
2675 const u8
*req_pmk_r1_name
,
2676 u8
*out_pmk_r1
, int *out_pairwise
,
2677 struct vlan_description
*vlan
,
2678 const u8
**identity
, size_t *identity_len
,
2679 const u8
**radius_cui
,
2680 size_t *radius_cui_len
,
2681 int *out_session_timeout
)
2683 struct wpa_auth_config
*conf
= &wpa_auth
->conf
;
2684 const struct wpa_ft_pmk_r0_sa
*r0
;
2685 u8 pmk_r1_name
[WPA_PMK_NAME_LEN
];
2687 int session_timeout
= 0;
2688 struct os_reltime now
;
2690 if (conf
->r0_key_holder_len
!= r0kh_id_len
||
2691 os_memcmp(conf
->r0_key_holder
, r0kh_id
, conf
->r0_key_holder_len
) !=
2693 return -1; /* not our R0KH-ID */
2695 wpa_printf(MSG_DEBUG
, "FT: STA R0KH-ID matching local configuration");
2696 if (wpa_ft_fetch_pmk_r0(sm
->wpa_auth
, sm
->addr
, req_pmk_r0_name
, &r0
) <
2698 return -1; /* no matching PMKR0Name in local cache */
2700 wpa_printf(MSG_DEBUG
, "FT: Requested PMKR0Name found in local cache");
2702 if (wpa_derive_pmk_r1(r0
->pmk_r0
, r0
->pmk_r0_len
, r0
->pmk_r0_name
,
2703 conf
->r1_key_holder
,
2704 sm
->addr
, out_pmk_r1
, pmk_r1_name
) < 0)
2706 wpa_hexdump_key(MSG_DEBUG
, "FT: PMK-R1", out_pmk_r1
, r0
->pmk_r0_len
);
2707 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name", pmk_r1_name
, WPA_PMK_NAME_LEN
);
2709 os_get_reltime(&now
);
2711 expires_in
= r0
->expiration
- now
.sec
;
2713 if (r0
->session_timeout
)
2714 session_timeout
= r0
->session_timeout
- now
.sec
;
2716 wpa_ft_store_pmk_r1(wpa_auth
, sm
->addr
, out_pmk_r1
, r0
->pmk_r0_len
,
2718 sm
->pairwise
, r0
->vlan
, expires_in
, session_timeout
,
2719 r0
->identity
, r0
->identity_len
,
2720 r0
->radius_cui
, r0
->radius_cui_len
);
2722 *out_pairwise
= sm
->pairwise
;
2727 os_memset(vlan
, 0, sizeof(*vlan
));
2730 if (identity
&& identity_len
) {
2731 *identity
= r0
->identity
;
2732 *identity_len
= r0
->identity_len
;
2735 if (radius_cui
&& radius_cui_len
) {
2736 *radius_cui
= r0
->radius_cui
;
2737 *radius_cui_len
= r0
->radius_cui_len
;
2740 *out_session_timeout
= session_timeout
;
2746 static int wpa_ft_process_auth_req(struct wpa_state_machine
*sm
,
2747 const u8
*ies
, size_t ies_len
,
2748 u8
**resp_ies
, size_t *resp_ies_len
)
2750 struct rsn_mdie
*mdie
;
2751 u8 pmk_r1
[PMK_LEN_MAX
], pmk_r1_name
[WPA_PMK_NAME_LEN
];
2752 u8 ptk_name
[WPA_PMK_NAME_LEN
];
2753 struct wpa_auth_config
*conf
;
2754 struct wpa_ft_ies parse
;
2758 int pairwise
, session_timeout
= 0;
2759 struct vlan_description vlan
;
2760 const u8
*identity
, *radius_cui
;
2761 size_t identity_len
= 0, radius_cui_len
= 0;
2768 sm
->pmk_r1_name_valid
= 0;
2769 conf
= &sm
->wpa_auth
->conf
;
2771 wpa_hexdump(MSG_DEBUG
, "FT: Received authentication frame IEs",
2774 if (wpa_ft_parse_ies(ies
, ies_len
, &parse
, -1)) {
2775 wpa_printf(MSG_DEBUG
, "FT: Failed to parse FT IEs");
2776 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
2778 use_sha384
= wpa_key_mgmt_sha384(parse
.key_mgmt
);
2779 pmk_r1_len
= use_sha384
? SHA384_MAC_LEN
: PMK_LEN
;
2781 mdie
= (struct rsn_mdie
*) parse
.mdie
;
2782 if (mdie
== NULL
|| parse
.mdie_len
< sizeof(*mdie
) ||
2783 os_memcmp(mdie
->mobility_domain
,
2784 sm
->wpa_auth
->conf
.mobility_domain
,
2785 MOBILITY_DOMAIN_ID_LEN
) != 0) {
2786 wpa_printf(MSG_DEBUG
, "FT: Invalid MDIE");
2787 return WLAN_STATUS_INVALID_MDIE
;
2791 struct rsn_ftie_sha384
*ftie
;
2793 ftie
= (struct rsn_ftie_sha384
*) parse
.ftie
;
2794 if (!ftie
|| parse
.ftie_len
< sizeof(*ftie
)) {
2795 wpa_printf(MSG_DEBUG
, "FT: Invalid FTIE");
2796 return WLAN_STATUS_INVALID_FTIE
;
2799 os_memcpy(sm
->SNonce
, ftie
->snonce
, WPA_NONCE_LEN
);
2801 struct rsn_ftie
*ftie
;
2803 ftie
= (struct rsn_ftie
*) parse
.ftie
;
2804 if (!ftie
|| parse
.ftie_len
< sizeof(*ftie
)) {
2805 wpa_printf(MSG_DEBUG
, "FT: Invalid FTIE");
2806 return WLAN_STATUS_INVALID_FTIE
;
2809 os_memcpy(sm
->SNonce
, ftie
->snonce
, WPA_NONCE_LEN
);
2812 if (parse
.r0kh_id
== NULL
) {
2813 wpa_printf(MSG_DEBUG
, "FT: Invalid FTIE - no R0KH-ID");
2814 return WLAN_STATUS_INVALID_FTIE
;
2817 wpa_hexdump(MSG_DEBUG
, "FT: STA R0KH-ID",
2818 parse
.r0kh_id
, parse
.r0kh_id_len
);
2819 os_memcpy(sm
->r0kh_id
, parse
.r0kh_id
, parse
.r0kh_id_len
);
2820 sm
->r0kh_id_len
= parse
.r0kh_id_len
;
2822 if (parse
.rsn_pmkid
== NULL
) {
2823 wpa_printf(MSG_DEBUG
, "FT: No PMKID in RSNIE");
2824 return WLAN_STATUS_INVALID_PMKID
;
2827 if (wpa_ft_set_key_mgmt(sm
, &parse
) < 0)
2828 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
2830 wpa_hexdump(MSG_DEBUG
, "FT: Requested PMKR0Name",
2831 parse
.rsn_pmkid
, WPA_PMK_NAME_LEN
);
2832 if (wpa_derive_pmk_r1_name(parse
.rsn_pmkid
,
2833 sm
->wpa_auth
->conf
.r1_key_holder
, sm
->addr
,
2835 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
2836 wpa_hexdump(MSG_DEBUG
, "FT: Derived requested PMKR1Name",
2837 pmk_r1_name
, WPA_PMK_NAME_LEN
);
2839 if (conf
->ft_psk_generate_local
&&
2840 wpa_key_mgmt_ft_psk(sm
->wpa_key_mgmt
)) {
2841 if (wpa_ft_psk_pmk_r1(sm
, pmk_r1_name
, pmk_r1
, &pairwise
,
2842 &vlan
, &identity
, &identity_len
,
2843 &radius_cui
, &radius_cui_len
,
2844 &session_timeout
) < 0)
2845 return WLAN_STATUS_INVALID_PMKID
;
2846 wpa_printf(MSG_DEBUG
,
2847 "FT: Generated PMK-R1 for FT-PSK locally");
2848 } else if (wpa_ft_fetch_pmk_r1(sm
->wpa_auth
, sm
->addr
, pmk_r1_name
,
2849 pmk_r1
, &pmk_r1_len
, &pairwise
, &vlan
,
2850 &identity
, &identity_len
, &radius_cui
,
2851 &radius_cui_len
, &session_timeout
) < 0) {
2852 wpa_printf(MSG_DEBUG
,
2853 "FT: No PMK-R1 available in local cache for the requested PMKR1Name");
2854 if (wpa_ft_local_derive_pmk_r1(sm
->wpa_auth
, sm
,
2855 parse
.r0kh_id
, parse
.r0kh_id_len
,
2857 pmk_r1_name
, pmk_r1
, &pairwise
,
2858 &vlan
, &identity
, &identity_len
,
2859 &radius_cui
, &radius_cui_len
,
2860 &session_timeout
) == 0) {
2861 wpa_printf(MSG_DEBUG
,
2862 "FT: Generated PMK-R1 based on local PMK-R0");
2863 goto pmk_r1_derived
;
2866 if (wpa_ft_pull_pmk_r1(sm
, ies
, ies_len
, parse
.rsn_pmkid
) < 0) {
2867 wpa_printf(MSG_DEBUG
,
2868 "FT: Did not have matching PMK-R1 and either unknown or blocked R0KH-ID or NAK from R0KH");
2869 return WLAN_STATUS_INVALID_PMKID
;
2872 return -1; /* Status pending */
2874 wpa_printf(MSG_DEBUG
, "FT: Found PMKR1Name from local cache");
2878 wpa_hexdump_key(MSG_DEBUG
, "FT: Selected PMK-R1", pmk_r1
, pmk_r1_len
);
2879 sm
->pmk_r1_name_valid
= 1;
2880 os_memcpy(sm
->pmk_r1_name
, pmk_r1_name
, WPA_PMK_NAME_LEN
);
2882 if (random_get_bytes(sm
->ANonce
, WPA_NONCE_LEN
)) {
2883 wpa_printf(MSG_DEBUG
, "FT: Failed to get random data for "
2885 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
2888 wpa_hexdump(MSG_DEBUG
, "FT: Received SNonce",
2889 sm
->SNonce
, WPA_NONCE_LEN
);
2890 wpa_hexdump(MSG_DEBUG
, "FT: Generated ANonce",
2891 sm
->ANonce
, WPA_NONCE_LEN
);
2893 if (wpa_pmk_r1_to_ptk(pmk_r1
, pmk_r1_len
, sm
->SNonce
, sm
->ANonce
,
2894 sm
->addr
, sm
->wpa_auth
->addr
, pmk_r1_name
,
2895 &sm
->PTK
, ptk_name
, sm
->wpa_key_mgmt
,
2897 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
2899 sm
->pairwise
= pairwise
;
2900 sm
->PTK_valid
= TRUE
;
2901 sm
->tk_already_set
= FALSE
;
2902 wpa_ft_install_ptk(sm
);
2904 if (wpa_ft_set_vlan(sm
->wpa_auth
, sm
->addr
, &vlan
) < 0) {
2905 wpa_printf(MSG_DEBUG
, "FT: Failed to configure VLAN");
2906 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
2908 if (wpa_ft_set_identity(sm
->wpa_auth
, sm
->addr
,
2909 identity
, identity_len
) < 0 ||
2910 wpa_ft_set_radius_cui(sm
->wpa_auth
, sm
->addr
,
2911 radius_cui
, radius_cui_len
) < 0) {
2912 wpa_printf(MSG_DEBUG
, "FT: Failed to configure identity/CUI");
2913 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
2915 wpa_ft_set_session_timeout(sm
->wpa_auth
, sm
->addr
, session_timeout
);
2917 buflen
= 2 + sizeof(struct rsn_mdie
) + 2 + sizeof(struct rsn_ftie
) +
2918 2 + FT_R1KH_ID_LEN
+ 200;
2919 *resp_ies
= os_zalloc(buflen
);
2920 if (*resp_ies
== NULL
)
2924 end
= *resp_ies
+ buflen
;
2926 ret
= wpa_write_rsn_ie(conf
, pos
, end
- pos
, parse
.rsn_pmkid
);
2931 ret
= wpa_write_mdie(conf
, pos
, end
- pos
);
2936 ret
= wpa_write_ftie(conf
, use_sha384
, parse
.r0kh_id
, parse
.r0kh_id_len
,
2937 sm
->ANonce
, sm
->SNonce
, pos
, end
- pos
, NULL
, 0);
2942 *resp_ies_len
= pos
- *resp_ies
;
2944 return WLAN_STATUS_SUCCESS
;
2948 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
2952 void wpa_ft_process_auth(struct wpa_state_machine
*sm
, const u8
*bssid
,
2953 u16 auth_transaction
, const u8
*ies
, size_t ies_len
,
2954 void (*cb
)(void *ctx
, const u8
*dst
, const u8
*bssid
,
2955 u16 auth_transaction
, u16 status
,
2956 const u8
*ies
, size_t ies_len
),
2961 size_t resp_ies_len
;
2965 wpa_printf(MSG_DEBUG
, "FT: Received authentication frame, but "
2966 "WPA SM not available");
2970 wpa_printf(MSG_DEBUG
, "FT: Received authentication frame: STA=" MACSTR
2971 " BSSID=" MACSTR
" transaction=%d",
2972 MAC2STR(sm
->addr
), MAC2STR(bssid
), auth_transaction
);
2973 sm
->ft_pending_cb
= cb
;
2974 sm
->ft_pending_cb_ctx
= ctx
;
2975 sm
->ft_pending_auth_transaction
= auth_transaction
;
2976 sm
->ft_pending_pull_left_retries
= sm
->wpa_auth
->conf
.rkh_pull_retries
;
2977 res
= wpa_ft_process_auth_req(sm
, ies
, ies_len
, &resp_ies
,
2980 wpa_printf(MSG_DEBUG
, "FT: Callback postponed until response is available");
2985 wpa_printf(MSG_DEBUG
, "FT: FT authentication response: dst=" MACSTR
2986 " auth_transaction=%d status=%d",
2987 MAC2STR(sm
->addr
), auth_transaction
+ 1, status
);
2988 wpa_hexdump(MSG_DEBUG
, "FT: Response IEs", resp_ies
, resp_ies_len
);
2989 cb(ctx
, sm
->addr
, bssid
, auth_transaction
+ 1, status
,
2990 resp_ies
, resp_ies_len
);
2995 u16
wpa_ft_validate_reassoc(struct wpa_state_machine
*sm
, const u8
*ies
,
2998 struct wpa_ft_ies parse
;
2999 struct rsn_mdie
*mdie
;
3000 u8 mic
[WPA_EAPOL_KEY_MIC_MAX_LEN
];
3001 size_t mic_len
= 16;
3006 const u8
*anonce
, *snonce
, *fte_mic
;
3010 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
3012 use_sha384
= wpa_key_mgmt_sha384(sm
->wpa_key_mgmt
);
3014 wpa_hexdump(MSG_DEBUG
, "FT: Reassoc Req IEs", ies
, ies_len
);
3016 if (wpa_ft_parse_ies(ies
, ies_len
, &parse
, use_sha384
) < 0) {
3017 wpa_printf(MSG_DEBUG
, "FT: Failed to parse FT IEs");
3018 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
3021 if (parse
.rsn
== NULL
) {
3022 wpa_printf(MSG_DEBUG
, "FT: No RSNIE in Reassoc Req");
3023 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
3026 if (parse
.rsn_pmkid
== NULL
) {
3027 wpa_printf(MSG_DEBUG
, "FT: No PMKID in RSNIE");
3028 return WLAN_STATUS_INVALID_PMKID
;
3031 if (os_memcmp_const(parse
.rsn_pmkid
, sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
)
3033 wpa_printf(MSG_DEBUG
, "FT: PMKID in Reassoc Req did not match "
3034 "with the PMKR1Name derived from auth request");
3035 return WLAN_STATUS_INVALID_PMKID
;
3038 mdie
= (struct rsn_mdie
*) parse
.mdie
;
3039 if (mdie
== NULL
|| parse
.mdie_len
< sizeof(*mdie
) ||
3040 os_memcmp(mdie
->mobility_domain
,
3041 sm
->wpa_auth
->conf
.mobility_domain
,
3042 MOBILITY_DOMAIN_ID_LEN
) != 0) {
3043 wpa_printf(MSG_DEBUG
, "FT: Invalid MDIE");
3044 return WLAN_STATUS_INVALID_MDIE
;
3048 struct rsn_ftie_sha384
*ftie
;
3050 ftie
= (struct rsn_ftie_sha384
*) parse
.ftie
;
3051 if (ftie
== NULL
|| parse
.ftie_len
< sizeof(*ftie
)) {
3052 wpa_printf(MSG_DEBUG
, "FT: Invalid FTIE");
3053 return WLAN_STATUS_INVALID_FTIE
;
3056 anonce
= ftie
->anonce
;
3057 snonce
= ftie
->snonce
;
3058 fte_elem_count
= ftie
->mic_control
[1];
3059 fte_mic
= ftie
->mic
;
3061 struct rsn_ftie
*ftie
;
3063 ftie
= (struct rsn_ftie
*) parse
.ftie
;
3064 if (ftie
== NULL
|| parse
.ftie_len
< sizeof(*ftie
)) {
3065 wpa_printf(MSG_DEBUG
, "FT: Invalid FTIE");
3066 return WLAN_STATUS_INVALID_FTIE
;
3069 anonce
= ftie
->anonce
;
3070 snonce
= ftie
->snonce
;
3071 fte_elem_count
= ftie
->mic_control
[1];
3072 fte_mic
= ftie
->mic
;
3075 if (os_memcmp(snonce
, sm
->SNonce
, WPA_NONCE_LEN
) != 0) {
3076 wpa_printf(MSG_DEBUG
, "FT: SNonce mismatch in FTIE");
3077 wpa_hexdump(MSG_DEBUG
, "FT: Received SNonce",
3078 snonce
, WPA_NONCE_LEN
);
3079 wpa_hexdump(MSG_DEBUG
, "FT: Expected SNonce",
3080 sm
->SNonce
, WPA_NONCE_LEN
);
3081 return WLAN_STATUS_INVALID_FTIE
;
3084 if (os_memcmp(anonce
, sm
->ANonce
, WPA_NONCE_LEN
) != 0) {
3085 wpa_printf(MSG_DEBUG
, "FT: ANonce mismatch in FTIE");
3086 wpa_hexdump(MSG_DEBUG
, "FT: Received ANonce",
3087 anonce
, WPA_NONCE_LEN
);
3088 wpa_hexdump(MSG_DEBUG
, "FT: Expected ANonce",
3089 sm
->ANonce
, WPA_NONCE_LEN
);
3090 return WLAN_STATUS_INVALID_FTIE
;
3094 if (parse
.r0kh_id
== NULL
) {
3095 wpa_printf(MSG_DEBUG
, "FT: No R0KH-ID subelem in FTIE");
3096 return WLAN_STATUS_INVALID_FTIE
;
3099 if (parse
.r0kh_id_len
!= sm
->r0kh_id_len
||
3100 os_memcmp_const(parse
.r0kh_id
, sm
->r0kh_id
, parse
.r0kh_id_len
) != 0)
3102 wpa_printf(MSG_DEBUG
, "FT: R0KH-ID in FTIE did not match with "
3103 "the current R0KH-ID");
3104 wpa_hexdump(MSG_DEBUG
, "FT: R0KH-ID in FTIE",
3105 parse
.r0kh_id
, parse
.r0kh_id_len
);
3106 wpa_hexdump(MSG_DEBUG
, "FT: The current R0KH-ID",
3107 sm
->r0kh_id
, sm
->r0kh_id_len
);
3108 return WLAN_STATUS_INVALID_FTIE
;
3111 if (parse
.r1kh_id
== NULL
) {
3112 wpa_printf(MSG_DEBUG
, "FT: No R1KH-ID subelem in FTIE");
3113 return WLAN_STATUS_INVALID_FTIE
;
3116 if (os_memcmp_const(parse
.r1kh_id
, sm
->wpa_auth
->conf
.r1_key_holder
,
3117 FT_R1KH_ID_LEN
) != 0) {
3118 wpa_printf(MSG_DEBUG
, "FT: Unknown R1KH-ID used in "
3120 wpa_hexdump(MSG_DEBUG
, "FT: R1KH-ID in FTIE",
3121 parse
.r1kh_id
, FT_R1KH_ID_LEN
);
3122 wpa_hexdump(MSG_DEBUG
, "FT: Expected R1KH-ID",
3123 sm
->wpa_auth
->conf
.r1_key_holder
, FT_R1KH_ID_LEN
);
3124 return WLAN_STATUS_INVALID_FTIE
;
3127 if (parse
.rsn_pmkid
== NULL
||
3128 os_memcmp_const(parse
.rsn_pmkid
, sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
))
3130 wpa_printf(MSG_DEBUG
, "FT: No matching PMKR1Name (PMKID) in "
3131 "RSNIE (pmkid=%d)", !!parse
.rsn_pmkid
);
3132 return WLAN_STATUS_INVALID_PMKID
;
3137 count
+= ieee802_11_ie_count(parse
.ric
, parse
.ric_len
);
3138 if (fte_elem_count
!= count
) {
3139 wpa_printf(MSG_DEBUG
, "FT: Unexpected IE count in MIC "
3140 "Control: received %u expected %u",
3141 fte_elem_count
, count
);
3142 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
3145 if (wpa_key_mgmt_fils(sm
->wpa_key_mgmt
)) {
3147 kck_len
= sm
->PTK
.kck2_len
;
3150 kck_len
= sm
->PTK
.kck_len
;
3152 if (wpa_ft_mic(kck
, kck_len
, sm
->addr
, sm
->wpa_auth
->addr
, 5,
3153 parse
.mdie
- 2, parse
.mdie_len
+ 2,
3154 parse
.ftie
- 2, parse
.ftie_len
+ 2,
3155 parse
.rsn
- 2, parse
.rsn_len
+ 2,
3156 parse
.ric
, parse
.ric_len
,
3158 wpa_printf(MSG_DEBUG
, "FT: Failed to calculate MIC");
3159 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
3162 if (os_memcmp_const(mic
, fte_mic
, mic_len
) != 0) {
3163 wpa_printf(MSG_DEBUG
, "FT: Invalid MIC in FTIE");
3164 wpa_printf(MSG_DEBUG
, "FT: addr=" MACSTR
" auth_addr=" MACSTR
,
3165 MAC2STR(sm
->addr
), MAC2STR(sm
->wpa_auth
->addr
));
3166 wpa_hexdump(MSG_MSGDUMP
, "FT: Received MIC",
3168 wpa_hexdump(MSG_MSGDUMP
, "FT: Calculated MIC", mic
, mic_len
);
3169 wpa_hexdump(MSG_MSGDUMP
, "FT: MDIE",
3170 parse
.mdie
- 2, parse
.mdie_len
+ 2);
3171 wpa_hexdump(MSG_MSGDUMP
, "FT: FTIE",
3172 parse
.ftie
- 2, parse
.ftie_len
+ 2);
3173 wpa_hexdump(MSG_MSGDUMP
, "FT: RSN",
3174 parse
.rsn
- 2, parse
.rsn_len
+ 2);
3175 return WLAN_STATUS_INVALID_FTIE
;
3178 return WLAN_STATUS_SUCCESS
;
3182 int wpa_ft_action_rx(struct wpa_state_machine
*sm
, const u8
*data
, size_t len
)
3184 const u8
*sta_addr
, *target_ap
;
3188 struct ft_rrb_frame
*frame
;
3194 * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
3195 * FT Request action frame body[variable]
3199 wpa_printf(MSG_DEBUG
, "FT: Too short FT Action frame "
3200 "(len=%lu)", (unsigned long) len
);
3205 sta_addr
= data
+ 2;
3206 target_ap
= data
+ 8;
3210 wpa_printf(MSG_DEBUG
, "FT: Received FT Action frame (STA=" MACSTR
3211 " Target AP=" MACSTR
" Action=%d)",
3212 MAC2STR(sta_addr
), MAC2STR(target_ap
), action
);
3214 if (os_memcmp(sta_addr
, sm
->addr
, ETH_ALEN
) != 0) {
3215 wpa_printf(MSG_DEBUG
, "FT: Mismatch in FT Action STA address: "
3216 "STA=" MACSTR
" STA-Address=" MACSTR
,
3217 MAC2STR(sm
->addr
), MAC2STR(sta_addr
));
3222 * Do some sanity checking on the target AP address (not own and not
3223 * broadcast. This could be extended to filter based on a list of known
3224 * APs in the MD (if such a list were configured).
3226 if ((target_ap
[0] & 0x01) ||
3227 os_memcmp(target_ap
, sm
->wpa_auth
->addr
, ETH_ALEN
) == 0) {
3228 wpa_printf(MSG_DEBUG
, "FT: Invalid Target AP in FT Action "
3233 wpa_hexdump(MSG_MSGDUMP
, "FT: Action frame body", ies
, ies_len
);
3235 if (!sm
->wpa_auth
->conf
.ft_over_ds
) {
3236 wpa_printf(MSG_DEBUG
, "FT: Over-DS option disabled - reject");
3240 /* RRB - Forward action frame to the target AP */
3241 frame
= os_malloc(sizeof(*frame
) + len
);
3244 frame
->frame_type
= RSN_REMOTE_FRAME_TYPE_FT_RRB
;
3245 frame
->packet_type
= FT_PACKET_REQUEST
;
3246 frame
->action_length
= host_to_le16(len
);
3247 os_memcpy(frame
->ap_address
, sm
->wpa_auth
->addr
, ETH_ALEN
);
3248 os_memcpy(frame
+ 1, data
, len
);
3250 wpa_ft_rrb_send(sm
->wpa_auth
, target_ap
, (u8
*) frame
,
3251 sizeof(*frame
) + len
);
3258 static void wpa_ft_rrb_rx_request_cb(void *ctx
, const u8
*dst
, const u8
*bssid
,
3259 u16 auth_transaction
, u16 resp
,
3260 const u8
*ies
, size_t ies_len
)
3262 struct wpa_state_machine
*sm
= ctx
;
3263 wpa_printf(MSG_DEBUG
, "FT: Over-the-DS RX request cb for " MACSTR
,
3265 wpa_ft_send_rrb_auth_resp(sm
, sm
->ft_pending_current_ap
, sm
->addr
,
3266 WLAN_STATUS_SUCCESS
, ies
, ies_len
);
3270 static int wpa_ft_rrb_rx_request(struct wpa_authenticator
*wpa_auth
,
3271 const u8
*current_ap
, const u8
*sta_addr
,
3272 const u8
*body
, size_t len
)
3274 struct wpa_state_machine
*sm
;
3277 size_t resp_ies_len
;
3280 sm
= wpa_ft_add_sta(wpa_auth
, sta_addr
);
3282 wpa_printf(MSG_DEBUG
, "FT: Failed to add new STA based on "
3287 wpa_hexdump(MSG_MSGDUMP
, "FT: RRB Request Frame body", body
, len
);
3289 sm
->ft_pending_cb
= wpa_ft_rrb_rx_request_cb
;
3290 sm
->ft_pending_cb_ctx
= sm
;
3291 os_memcpy(sm
->ft_pending_current_ap
, current_ap
, ETH_ALEN
);
3292 sm
->ft_pending_pull_left_retries
= sm
->wpa_auth
->conf
.rkh_pull_retries
;
3293 res
= wpa_ft_process_auth_req(sm
, body
, len
, &resp_ies
,
3296 wpa_printf(MSG_DEBUG
, "FT: No immediate response available - wait for pull response");
3301 res
= wpa_ft_send_rrb_auth_resp(sm
, current_ap
, sta_addr
, status
,
3302 resp_ies
, resp_ies_len
);
3308 static int wpa_ft_send_rrb_auth_resp(struct wpa_state_machine
*sm
,
3309 const u8
*current_ap
, const u8
*sta_addr
,
3310 u16 status
, const u8
*resp_ies
,
3311 size_t resp_ies_len
)
3313 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
3315 struct ft_rrb_frame
*frame
;
3318 wpa_printf(MSG_DEBUG
, "FT: RRB authentication response: STA=" MACSTR
3319 " CurrentAP=" MACSTR
" status=%d",
3320 MAC2STR(sm
->addr
), MAC2STR(current_ap
), status
);
3321 wpa_hexdump(MSG_DEBUG
, "FT: Response IEs", resp_ies
, resp_ies_len
);
3323 /* RRB - Forward action frame response to the Current AP */
3326 * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
3327 * Status_Code[2] FT Request action frame body[variable]
3329 rlen
= 2 + 2 * ETH_ALEN
+ 2 + resp_ies_len
;
3331 frame
= os_malloc(sizeof(*frame
) + rlen
);
3334 frame
->frame_type
= RSN_REMOTE_FRAME_TYPE_FT_RRB
;
3335 frame
->packet_type
= FT_PACKET_RESPONSE
;
3336 frame
->action_length
= host_to_le16(rlen
);
3337 os_memcpy(frame
->ap_address
, wpa_auth
->addr
, ETH_ALEN
);
3338 pos
= (u8
*) (frame
+ 1);
3339 *pos
++ = WLAN_ACTION_FT
;
3340 *pos
++ = 2; /* Action: Response */
3341 os_memcpy(pos
, sta_addr
, ETH_ALEN
);
3343 os_memcpy(pos
, wpa_auth
->addr
, ETH_ALEN
);
3345 WPA_PUT_LE16(pos
, status
);
3348 os_memcpy(pos
, resp_ies
, resp_ies_len
);
3350 wpa_ft_rrb_send(wpa_auth
, current_ap
, (u8
*) frame
,
3351 sizeof(*frame
) + rlen
);
3358 static int wpa_ft_rrb_build_r0(const u8
*key
, const size_t key_len
,
3359 const struct tlv_list
*tlvs
,
3360 const struct wpa_ft_pmk_r0_sa
*pmk_r0
,
3361 const u8
*r1kh_id
, const u8
*s1kh_id
,
3362 const struct tlv_list
*tlv_auth
,
3363 const u8
*src_addr
, u8 type
,
3364 u8
**packet
, size_t *packet_len
)
3366 u8 pmk_r1
[PMK_LEN_MAX
];
3367 size_t pmk_r1_len
= pmk_r0
->pmk_r0_len
;
3368 u8 pmk_r1_name
[WPA_PMK_NAME_LEN
];
3369 u8 f_pairwise
[sizeof(le16
)];
3370 u8 f_expires_in
[sizeof(le16
)];
3371 u8 f_session_timeout
[sizeof(le32
)];
3373 int session_timeout
;
3374 struct os_reltime now
;
3376 struct tlv_list sess_tlv
[] = {
3377 { .type
= FT_RRB_PMK_R1
, .len
= pmk_r1_len
,
3379 { .type
= FT_RRB_PMK_R1_NAME
, .len
= sizeof(pmk_r1_name
),
3380 .data
= pmk_r1_name
},
3381 { .type
= FT_RRB_PAIRWISE
, .len
= sizeof(f_pairwise
),
3382 .data
= f_pairwise
},
3383 { .type
= FT_RRB_EXPIRES_IN
, .len
= sizeof(f_expires_in
),
3384 .data
= f_expires_in
},
3385 { .type
= FT_RRB_IDENTITY
, .len
= pmk_r0
->identity_len
,
3386 .data
= pmk_r0
->identity
},
3387 { .type
= FT_RRB_RADIUS_CUI
, .len
= pmk_r0
->radius_cui_len
,
3388 .data
= pmk_r0
->radius_cui
},
3389 { .type
= FT_RRB_SESSION_TIMEOUT
,
3390 .len
= sizeof(f_session_timeout
),
3391 .data
= f_session_timeout
},
3392 { .type
= FT_RRB_LAST_EMPTY
, .len
= 0, .data
= NULL
},
3395 if (wpa_derive_pmk_r1(pmk_r0
->pmk_r0
, pmk_r0
->pmk_r0_len
,
3396 pmk_r0
->pmk_r0_name
, r1kh_id
,
3397 s1kh_id
, pmk_r1
, pmk_r1_name
) < 0)
3399 wpa_hexdump_key(MSG_DEBUG
, "FT: PMK-R1 (for peer AP)",
3400 pmk_r1
, pmk_r1_len
);
3401 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name (for peer AP)",
3402 pmk_r1_name
, WPA_PMK_NAME_LEN
);
3403 WPA_PUT_LE16(f_pairwise
, pmk_r0
->pairwise
);
3405 os_get_reltime(&now
);
3406 if (pmk_r0
->expiration
> now
.sec
)
3407 expires_in
= pmk_r0
->expiration
- now
.sec
;
3408 else if (pmk_r0
->expiration
)
3412 WPA_PUT_LE16(f_expires_in
, expires_in
);
3414 if (pmk_r0
->session_timeout
> now
.sec
)
3415 session_timeout
= pmk_r0
->session_timeout
- now
.sec
;
3416 else if (pmk_r0
->session_timeout
)
3417 session_timeout
= 1;
3419 session_timeout
= 0;
3420 WPA_PUT_LE32(f_session_timeout
, session_timeout
);
3422 ret
= wpa_ft_rrb_build(key
, key_len
, tlvs
, sess_tlv
, tlv_auth
,
3423 pmk_r0
->vlan
, src_addr
, type
,
3424 packet
, packet_len
);
3426 os_memset(pmk_r1
, 0, sizeof(pmk_r1
));
3432 static int wpa_ft_rrb_rx_pull(struct wpa_authenticator
*wpa_auth
,
3434 const u8
*enc
, size_t enc_len
,
3435 const u8
*auth
, size_t auth_len
,
3438 const char *msgtype
= "pull request";
3439 u8
*plain
= NULL
, *packet
= NULL
;
3440 size_t plain_len
= 0, packet_len
= 0;
3441 struct ft_remote_r1kh
*r1kh
, *r1kh_wildcard
;
3445 const u8
*f_nonce
, *f_r0kh_id
, *f_r1kh_id
, *f_s1kh_id
, *f_pmk_r0_name
;
3446 size_t f_nonce_len
, f_r0kh_id_len
, f_r1kh_id_len
, f_s1kh_id_len
;
3447 size_t f_pmk_r0_name_len
;
3448 const struct wpa_ft_pmk_r0_sa
*r0
;
3450 struct tlv_list resp
[2];
3451 struct tlv_list resp_auth
[5];
3452 struct ft_rrb_seq f_seq
;
3454 wpa_printf(MSG_DEBUG
, "FT: Received PMK-R1 pull");
3456 RRB_GET_AUTH(FT_RRB_R0KH_ID
, r0kh_id
, msgtype
, -1);
3457 wpa_hexdump(MSG_DEBUG
, "FT: R0KH-ID", f_r0kh_id
, f_r0kh_id_len
);
3459 if (wpa_ft_rrb_check_r0kh(wpa_auth
, f_r0kh_id
, f_r0kh_id_len
)) {
3460 wpa_printf(MSG_DEBUG
, "FT: R0KH-ID mismatch");
3464 RRB_GET_AUTH(FT_RRB_R1KH_ID
, r1kh_id
, msgtype
, FT_R1KH_ID_LEN
);
3465 wpa_printf(MSG_DEBUG
, "FT: R1KH-ID=" MACSTR
, MAC2STR(f_r1kh_id
));
3467 wpa_ft_rrb_lookup_r1kh(wpa_auth
, f_r1kh_id
, &r1kh
, &r1kh_wildcard
);
3470 key_len
= sizeof(r1kh
->key
);
3471 } else if (r1kh_wildcard
) {
3472 wpa_printf(MSG_DEBUG
, "FT: Using wildcard R1KH-ID");
3473 key
= r1kh_wildcard
->key
;
3474 key_len
= sizeof(r1kh_wildcard
->key
);
3479 RRB_GET_AUTH(FT_RRB_NONCE
, nonce
, "pull request", FT_RRB_NONCE_LEN
);
3480 wpa_hexdump(MSG_DEBUG
, "FT: nonce", f_nonce
, f_nonce_len
);
3482 seq_ret
= FT_RRB_SEQ_DROP
;
3484 seq_ret
= wpa_ft_rrb_seq_chk(r1kh
->seq
, src_addr
, enc
, enc_len
,
3485 auth
, auth_len
, msgtype
, no_defer
);
3486 if (!no_defer
&& r1kh_wildcard
&&
3487 (!r1kh
|| os_memcmp(r1kh
->addr
, src_addr
, ETH_ALEN
) != 0)) {
3488 /* wildcard: r1kh-id unknown or changed addr -> do a seq req */
3489 seq_ret
= FT_RRB_SEQ_DEFER
;
3492 if (seq_ret
== FT_RRB_SEQ_DROP
)
3495 if (wpa_ft_rrb_decrypt(key
, key_len
, enc
, enc_len
, auth
, auth_len
,
3496 src_addr
, FT_PACKET_R0KH_R1KH_PULL
,
3497 &plain
, &plain_len
) < 0)
3501 r1kh
= wpa_ft_rrb_add_r1kh(wpa_auth
, r1kh_wildcard
, src_addr
,
3503 wpa_auth
->conf
.rkh_pos_timeout
);
3507 if (seq_ret
== FT_RRB_SEQ_DEFER
) {
3508 wpa_ft_rrb_seq_req(wpa_auth
, r1kh
->seq
, src_addr
, f_r0kh_id
,
3509 f_r0kh_id_len
, f_r1kh_id
, key
, key_len
,
3510 enc
, enc_len
, auth
, auth_len
,
3511 &wpa_ft_rrb_rx_pull
);
3515 wpa_ft_rrb_seq_accept(wpa_auth
, r1kh
->seq
, src_addr
, auth
, auth_len
,
3517 wpa_ft_rrb_r1kh_replenish(wpa_auth
, r1kh
,
3518 wpa_auth
->conf
.rkh_pos_timeout
);
3520 RRB_GET(FT_RRB_PMK_R0_NAME
, pmk_r0_name
, msgtype
, WPA_PMK_NAME_LEN
);
3521 wpa_hexdump(MSG_DEBUG
, "FT: PMKR0Name", f_pmk_r0_name
,
3524 RRB_GET(FT_RRB_S1KH_ID
, s1kh_id
, msgtype
, ETH_ALEN
);
3525 wpa_printf(MSG_DEBUG
, "FT: S1KH-ID=" MACSTR
, MAC2STR(f_s1kh_id
));
3527 if (wpa_ft_new_seq(r1kh
->seq
, &f_seq
) < 0) {
3528 wpa_printf(MSG_DEBUG
, "FT: Failed to get seq num");
3532 resp
[0].type
= FT_RRB_S1KH_ID
;
3533 resp
[0].len
= f_s1kh_id_len
;
3534 resp
[0].data
= f_s1kh_id
;
3535 resp
[1].type
= FT_RRB_LAST_EMPTY
;
3537 resp
[1].data
= NULL
;
3539 resp_auth
[0].type
= FT_RRB_NONCE
;
3540 resp_auth
[0].len
= f_nonce_len
;
3541 resp_auth
[0].data
= f_nonce
;
3542 resp_auth
[1].type
= FT_RRB_SEQ
;
3543 resp_auth
[1].len
= sizeof(f_seq
);
3544 resp_auth
[1].data
= (u8
*) &f_seq
;
3545 resp_auth
[2].type
= FT_RRB_R0KH_ID
;
3546 resp_auth
[2].len
= f_r0kh_id_len
;
3547 resp_auth
[2].data
= f_r0kh_id
;
3548 resp_auth
[3].type
= FT_RRB_R1KH_ID
;
3549 resp_auth
[3].len
= f_r1kh_id_len
;
3550 resp_auth
[3].data
= f_r1kh_id
;
3551 resp_auth
[4].type
= FT_RRB_LAST_EMPTY
;
3552 resp_auth
[4].len
= 0;
3553 resp_auth
[4].data
= NULL
;
3555 if (wpa_ft_fetch_pmk_r0(wpa_auth
, f_s1kh_id
, f_pmk_r0_name
, &r0
) < 0) {
3556 wpa_printf(MSG_DEBUG
, "FT: No matching PMK-R0-Name found");
3557 ret
= wpa_ft_rrb_build(key
, key_len
, resp
, NULL
, resp_auth
,
3558 NULL
, wpa_auth
->addr
,
3559 FT_PACKET_R0KH_R1KH_RESP
,
3560 &packet
, &packet_len
);
3562 ret
= wpa_ft_rrb_build_r0(key
, key_len
, resp
, r0
, f_r1kh_id
,
3563 f_s1kh_id
, resp_auth
, wpa_auth
->addr
,
3564 FT_PACKET_R0KH_R1KH_RESP
,
3565 &packet
, &packet_len
);
3569 wpa_ft_rrb_oui_send(wpa_auth
, src_addr
,
3570 FT_PACKET_R0KH_R1KH_RESP
, packet
,
3581 /* @returns 0 on success
3583 * -2 if FR_RRB_PAIRWISE is missing
3585 static int wpa_ft_rrb_rx_r1(struct wpa_authenticator
*wpa_auth
,
3586 const u8
*src_addr
, u8 type
,
3587 const u8
*enc
, size_t enc_len
,
3588 const u8
*auth
, size_t auth_len
,
3589 const char *msgtype
, u8
*s1kh_id_out
,
3590 int (*cb
)(struct wpa_authenticator
*wpa_auth
,
3592 const u8
*enc
, size_t enc_len
,
3593 const u8
*auth
, size_t auth_len
,
3597 size_t plain_len
= 0;
3598 struct ft_remote_r0kh
*r0kh
, *r0kh_wildcard
;
3602 const u8
*f_r1kh_id
, *f_s1kh_id
, *f_r0kh_id
;
3603 const u8
*f_pmk_r1_name
, *f_pairwise
, *f_pmk_r1
;
3604 const u8
*f_expires_in
;
3605 size_t f_r1kh_id_len
, f_s1kh_id_len
, f_r0kh_id_len
;
3606 const u8
*f_identity
, *f_radius_cui
;
3607 const u8
*f_session_timeout
;
3608 size_t f_pmk_r1_name_len
, f_pairwise_len
, f_pmk_r1_len
;
3609 size_t f_expires_in_len
;
3610 size_t f_identity_len
, f_radius_cui_len
;
3611 size_t f_session_timeout_len
;
3615 int session_timeout
;
3616 struct vlan_description vlan
;
3619 RRB_GET_AUTH(FT_RRB_R0KH_ID
, r0kh_id
, msgtype
, -1);
3620 wpa_hexdump(MSG_DEBUG
, "FT: R0KH-ID", f_r0kh_id
, f_r0kh_id_len
);
3622 RRB_GET_AUTH(FT_RRB_R1KH_ID
, r1kh_id
, msgtype
, FT_R1KH_ID_LEN
);
3623 wpa_printf(MSG_DEBUG
, "FT: R1KH-ID=" MACSTR
, MAC2STR(f_r1kh_id
));
3625 if (wpa_ft_rrb_check_r1kh(wpa_auth
, f_r1kh_id
)) {
3626 wpa_printf(MSG_DEBUG
, "FT: R1KH-ID mismatch");
3630 wpa_ft_rrb_lookup_r0kh(wpa_auth
, f_r0kh_id
, f_r0kh_id_len
, &r0kh
,
3634 key_len
= sizeof(r0kh
->key
);
3635 } else if (r0kh_wildcard
) {
3636 wpa_printf(MSG_DEBUG
, "FT: Using wildcard R0KH-ID");
3637 key
= r0kh_wildcard
->key
;
3638 key_len
= sizeof(r0kh_wildcard
->key
);
3643 seq_ret
= FT_RRB_SEQ_DROP
;
3645 seq_ret
= wpa_ft_rrb_seq_chk(r0kh
->seq
, src_addr
, enc
, enc_len
,
3646 auth
, auth_len
, msgtype
,
3649 if (cb
&& r0kh_wildcard
&&
3650 (!r0kh
|| os_memcmp(r0kh
->addr
, src_addr
, ETH_ALEN
) != 0)) {
3651 /* wildcard: r0kh-id unknown or changed addr -> do a seq req */
3652 seq_ret
= FT_RRB_SEQ_DEFER
;
3655 if (seq_ret
== FT_RRB_SEQ_DROP
)
3658 if (wpa_ft_rrb_decrypt(key
, key_len
, enc
, enc_len
, auth
, auth_len
,
3659 src_addr
, type
, &plain
, &plain_len
) < 0)
3663 r0kh
= wpa_ft_rrb_add_r0kh(wpa_auth
, r0kh_wildcard
, src_addr
,
3664 f_r0kh_id
, f_r0kh_id_len
,
3665 wpa_auth
->conf
.rkh_pos_timeout
);
3669 if (seq_ret
== FT_RRB_SEQ_DEFER
) {
3670 wpa_ft_rrb_seq_req(wpa_auth
, r0kh
->seq
, src_addr
, f_r0kh_id
,
3671 f_r0kh_id_len
, f_r1kh_id
, key
, key_len
,
3672 enc
, enc_len
, auth
, auth_len
, cb
);
3676 wpa_ft_rrb_seq_accept(wpa_auth
, r0kh
->seq
, src_addr
, auth
, auth_len
,
3678 wpa_ft_rrb_r0kh_replenish(wpa_auth
, r0kh
,
3679 wpa_auth
->conf
.rkh_pos_timeout
);
3681 RRB_GET(FT_RRB_S1KH_ID
, s1kh_id
, msgtype
, ETH_ALEN
);
3682 wpa_printf(MSG_DEBUG
, "FT: S1KH-ID=" MACSTR
, MAC2STR(f_s1kh_id
));
3685 os_memcpy(s1kh_id_out
, f_s1kh_id
, ETH_ALEN
);
3688 RRB_GET(FT_RRB_PAIRWISE
, pairwise
, msgtype
, sizeof(le16
));
3689 wpa_hexdump(MSG_DEBUG
, "FT: pairwise", f_pairwise
, f_pairwise_len
);
3692 RRB_GET(FT_RRB_PMK_R1_NAME
, pmk_r1_name
, msgtype
, WPA_PMK_NAME_LEN
);
3693 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name",
3694 f_pmk_r1_name
, WPA_PMK_NAME_LEN
);
3696 pmk_r1_len
= PMK_LEN
;
3697 if (wpa_ft_rrb_get_tlv(plain
, plain_len
, FT_RRB_PMK_R1
, &f_pmk_r1_len
,
3699 (f_pmk_r1_len
== PMK_LEN
|| f_pmk_r1_len
== SHA384_MAC_LEN
))
3700 pmk_r1_len
= f_pmk_r1_len
;
3701 RRB_GET(FT_RRB_PMK_R1
, pmk_r1
, msgtype
, pmk_r1_len
);
3702 wpa_hexdump_key(MSG_DEBUG
, "FT: PMK-R1", f_pmk_r1
, pmk_r1_len
);
3704 pairwise
= WPA_GET_LE16(f_pairwise
);
3706 RRB_GET_OPTIONAL(FT_RRB_EXPIRES_IN
, expires_in
, msgtype
,
3709 expires_in
= WPA_GET_LE16(f_expires_in
);
3713 wpa_printf(MSG_DEBUG
, "FT: PMK-R1 %s - expires_in=%d", msgtype
,
3716 if (wpa_ft_rrb_get_tlv_vlan(plain
, plain_len
, &vlan
) < 0) {
3717 wpa_printf(MSG_DEBUG
, "FT: Cannot parse vlan");
3718 wpa_ft_rrb_dump(plain
, plain_len
);
3722 wpa_printf(MSG_DEBUG
, "FT: vlan %d%s",
3723 le_to_host16(vlan
.untagged
), vlan
.tagged
[0] ? "+" : "");
3725 RRB_GET_OPTIONAL(FT_RRB_IDENTITY
, identity
, msgtype
, -1);
3727 wpa_hexdump_ascii(MSG_DEBUG
, "FT: Identity", f_identity
,
3730 RRB_GET_OPTIONAL(FT_RRB_RADIUS_CUI
, radius_cui
, msgtype
, -1);
3732 wpa_hexdump_ascii(MSG_DEBUG
, "FT: CUI", f_radius_cui
,
3735 RRB_GET_OPTIONAL(FT_RRB_SESSION_TIMEOUT
, session_timeout
, msgtype
,
3737 if (f_session_timeout
)
3738 session_timeout
= WPA_GET_LE32(f_session_timeout
);
3740 session_timeout
= 0;
3741 wpa_printf(MSG_DEBUG
, "FT: session_timeout %d", session_timeout
);
3743 if (wpa_ft_store_pmk_r1(wpa_auth
, f_s1kh_id
, f_pmk_r1
, pmk_r1_len
,
3745 pairwise
, &vlan
, expires_in
, session_timeout
,
3746 f_identity
, f_identity_len
, f_radius_cui
,
3747 f_radius_cui_len
) < 0)
3753 os_memset(plain
, 0, plain_len
);
3762 static void ft_finish_pull(struct wpa_state_machine
*sm
)
3766 size_t resp_ies_len
;
3769 if (!sm
->ft_pending_cb
|| !sm
->ft_pending_req_ies
)
3772 res
= wpa_ft_process_auth_req(sm
, wpabuf_head(sm
->ft_pending_req_ies
),
3773 wpabuf_len(sm
->ft_pending_req_ies
),
3774 &resp_ies
, &resp_ies_len
);
3776 /* this loop is broken by ft_pending_pull_left_retries */
3777 wpa_printf(MSG_DEBUG
,
3778 "FT: Callback postponed until response is available");
3781 wpabuf_free(sm
->ft_pending_req_ies
);
3782 sm
->ft_pending_req_ies
= NULL
;
3784 wpa_printf(MSG_DEBUG
, "FT: Postponed auth callback result for " MACSTR
3785 " - status %u", MAC2STR(sm
->addr
), status
);
3787 sm
->ft_pending_cb(sm
->ft_pending_cb_ctx
, sm
->addr
, sm
->wpa_auth
->addr
,
3788 sm
->ft_pending_auth_transaction
+ 1, status
,
3789 resp_ies
, resp_ies_len
);
3794 struct ft_get_sta_ctx
{
3797 struct wpa_state_machine
*sm
;
3801 static int ft_get_sta_cb(struct wpa_state_machine
*sm
, void *ctx
)
3803 struct ft_get_sta_ctx
*info
= ctx
;
3805 if ((info
->s1kh_id
&&
3806 os_memcmp(info
->s1kh_id
, sm
->addr
, ETH_ALEN
) != 0) ||
3807 os_memcmp(info
->nonce
, sm
->ft_pending_pull_nonce
,
3808 FT_RRB_NONCE_LEN
) != 0 ||
3809 sm
->ft_pending_cb
== NULL
|| sm
->ft_pending_req_ies
== NULL
)
3818 static int wpa_ft_rrb_rx_resp(struct wpa_authenticator
*wpa_auth
,
3820 const u8
*enc
, size_t enc_len
,
3821 const u8
*auth
, size_t auth_len
,
3824 const char *msgtype
= "pull response";
3826 struct ft_get_sta_ctx ctx
;
3827 u8 s1kh_id
[ETH_ALEN
];
3831 wpa_printf(MSG_DEBUG
, "FT: Received PMK-R1 pull response");
3833 RRB_GET_AUTH(FT_RRB_NONCE
, nonce
, msgtype
, FT_RRB_NONCE_LEN
);
3834 wpa_hexdump(MSG_DEBUG
, "FT: nonce", f_nonce
, f_nonce_len
);
3836 os_memset(&ctx
, 0, sizeof(ctx
));
3837 ctx
.nonce
= f_nonce
;
3838 if (!wpa_auth_for_each_sta(wpa_auth
, ft_get_sta_cb
, &ctx
)) {
3839 /* nonce not found */
3840 wpa_printf(MSG_DEBUG
, "FT: Invalid nonce");
3844 ret
= wpa_ft_rrb_rx_r1(wpa_auth
, src_addr
, FT_PACKET_R0KH_R1KH_RESP
,
3845 enc
, enc_len
, auth
, auth_len
, msgtype
, s1kh_id
,
3846 no_defer
? NULL
: &wpa_ft_rrb_rx_resp
);
3856 ctx
.s1kh_id
= s1kh_id
;
3857 if (wpa_auth_for_each_sta(wpa_auth
, ft_get_sta_cb
, &ctx
)) {
3858 wpa_printf(MSG_DEBUG
,
3859 "FT: Response to a pending pull request for " MACSTR
,
3860 MAC2STR(ctx
.sm
->addr
));
3861 eloop_cancel_timeout(wpa_ft_expire_pull
, ctx
.sm
, NULL
);
3863 ctx
.sm
->ft_pending_pull_left_retries
= 0;
3864 ft_finish_pull(ctx
.sm
);
3872 static int wpa_ft_rrb_rx_push(struct wpa_authenticator
*wpa_auth
,
3874 const u8
*enc
, size_t enc_len
,
3875 const u8
*auth
, size_t auth_len
, int no_defer
)
3877 const char *msgtype
= "push";
3879 wpa_printf(MSG_DEBUG
, "FT: Received PMK-R1 push");
3881 if (wpa_ft_rrb_rx_r1(wpa_auth
, src_addr
, FT_PACKET_R0KH_R1KH_PUSH
,
3882 enc
, enc_len
, auth
, auth_len
, msgtype
, NULL
,
3883 no_defer
? NULL
: wpa_ft_rrb_rx_push
) < 0)
3890 static int wpa_ft_rrb_rx_seq(struct wpa_authenticator
*wpa_auth
,
3891 const u8
*src_addr
, int type
,
3892 const u8
*enc
, size_t enc_len
,
3893 const u8
*auth
, size_t auth_len
,
3894 struct ft_remote_seq
**rkh_seq
,
3895 u8
**key
, size_t *key_len
,
3896 struct ft_remote_r0kh
**r0kh_out
,
3897 struct ft_remote_r1kh
**r1kh_out
,
3898 struct ft_remote_r0kh
**r0kh_wildcard_out
,
3899 struct ft_remote_r1kh
**r1kh_wildcard_out
)
3901 struct ft_remote_r0kh
*r0kh
= NULL
;
3902 struct ft_remote_r1kh
*r1kh
= NULL
;
3903 const u8
*f_r0kh_id
, *f_r1kh_id
;
3904 size_t f_r0kh_id_len
, f_r1kh_id_len
;
3905 int to_r0kh
, to_r1kh
;
3907 size_t plain_len
= 0;
3908 struct ft_remote_r0kh
*r0kh_wildcard
;
3909 struct ft_remote_r1kh
*r1kh_wildcard
;
3911 RRB_GET_AUTH(FT_RRB_R0KH_ID
, r0kh_id
, "seq", -1);
3912 RRB_GET_AUTH(FT_RRB_R1KH_ID
, r1kh_id
, "seq", FT_R1KH_ID_LEN
);
3914 to_r0kh
= !wpa_ft_rrb_check_r0kh(wpa_auth
, f_r0kh_id
, f_r0kh_id_len
);
3915 to_r1kh
= !wpa_ft_rrb_check_r1kh(wpa_auth
, f_r1kh_id
);
3917 if (to_r0kh
&& to_r1kh
) {
3918 wpa_printf(MSG_DEBUG
, "FT: seq - local R0KH-ID and R1KH-ID");
3922 if (!to_r0kh
&& !to_r1kh
) {
3923 wpa_printf(MSG_DEBUG
, "FT: seq - remote R0KH-ID and R1KH-ID");
3928 wpa_ft_rrb_lookup_r0kh(wpa_auth
, f_r0kh_id
, f_r0kh_id_len
,
3929 &r0kh
, &r0kh_wildcard
);
3930 if (!r0kh_wildcard
&&
3931 (!r0kh
|| os_memcmp(r0kh
->addr
, src_addr
, ETH_ALEN
) != 0)) {
3932 wpa_hexdump(MSG_DEBUG
, "FT: Did not find R0KH-ID",
3933 f_r0kh_id
, f_r0kh_id_len
);
3938 *key_len
= sizeof(r0kh
->key
);
3940 *key
= r0kh_wildcard
->key
;
3941 *key_len
= sizeof(r0kh_wildcard
->key
);
3946 wpa_ft_rrb_lookup_r1kh(wpa_auth
, f_r1kh_id
, &r1kh
,
3948 if (!r1kh_wildcard
&&
3949 (!r1kh
|| os_memcmp(r1kh
->addr
, src_addr
, ETH_ALEN
) != 0)) {
3950 wpa_hexdump(MSG_DEBUG
, "FT: Did not find R1KH-ID",
3951 f_r1kh_id
, FT_R1KH_ID_LEN
);
3956 *key_len
= sizeof(r1kh
->key
);
3958 *key
= r1kh_wildcard
->key
;
3959 *key_len
= sizeof(r1kh_wildcard
->key
);
3963 if (wpa_ft_rrb_decrypt(*key
, *key_len
, enc
, enc_len
, auth
, auth_len
,
3964 src_addr
, type
, &plain
, &plain_len
) < 0)
3971 r0kh
= wpa_ft_rrb_add_r0kh(wpa_auth
, r0kh_wildcard
,
3972 src_addr
, f_r0kh_id
,
3978 wpa_ft_rrb_r0kh_replenish(wpa_auth
, r0kh
, ftRRBseqTimeout
);
3979 *rkh_seq
= r0kh
->seq
;
3982 if (r0kh_wildcard_out
)
3983 *r0kh_wildcard_out
= r0kh_wildcard
;
3988 r1kh
= wpa_ft_rrb_add_r1kh(wpa_auth
, r1kh_wildcard
,
3989 src_addr
, f_r1kh_id
,
3994 wpa_ft_rrb_r1kh_replenish(wpa_auth
, r1kh
, ftRRBseqTimeout
);
3995 *rkh_seq
= r1kh
->seq
;
3998 if (r1kh_wildcard_out
)
3999 *r1kh_wildcard_out
= r1kh_wildcard
;
4008 static int wpa_ft_rrb_rx_seq_req(struct wpa_authenticator
*wpa_auth
,
4010 const u8
*enc
, size_t enc_len
,
4011 const u8
*auth
, size_t auth_len
,
4015 struct ft_rrb_seq f_seq
;
4016 const u8
*f_nonce
, *f_r0kh_id
, *f_r1kh_id
;
4017 size_t f_nonce_len
, f_r0kh_id_len
, f_r1kh_id_len
;
4018 struct ft_remote_seq
*rkh_seq
= NULL
;
4019 u8
*packet
= NULL
, *key
= NULL
;
4020 size_t packet_len
= 0, key_len
= 0;
4021 struct tlv_list seq_resp_auth
[5];
4023 wpa_printf(MSG_DEBUG
, "FT: Received sequence number request");
4025 if (wpa_ft_rrb_rx_seq(wpa_auth
, src_addr
, FT_PACKET_R0KH_R1KH_SEQ_REQ
,
4026 enc
, enc_len
, auth
, auth_len
, &rkh_seq
, &key
,
4027 &key_len
, NULL
, NULL
, NULL
, NULL
) < 0)
4030 RRB_GET_AUTH(FT_RRB_NONCE
, nonce
, "seq request", FT_RRB_NONCE_LEN
);
4031 wpa_hexdump(MSG_DEBUG
, "FT: seq request - nonce", f_nonce
, f_nonce_len
);
4033 RRB_GET_AUTH(FT_RRB_R0KH_ID
, r0kh_id
, "seq", -1);
4034 RRB_GET_AUTH(FT_RRB_R1KH_ID
, r1kh_id
, "seq", FT_R1KH_ID_LEN
);
4036 if (wpa_ft_new_seq(rkh_seq
, &f_seq
) < 0) {
4037 wpa_printf(MSG_DEBUG
, "FT: Failed to get seq num");
4041 seq_resp_auth
[0].type
= FT_RRB_NONCE
;
4042 seq_resp_auth
[0].len
= f_nonce_len
;
4043 seq_resp_auth
[0].data
= f_nonce
;
4044 seq_resp_auth
[1].type
= FT_RRB_SEQ
;
4045 seq_resp_auth
[1].len
= sizeof(f_seq
);
4046 seq_resp_auth
[1].data
= (u8
*) &f_seq
;
4047 seq_resp_auth
[2].type
= FT_RRB_R0KH_ID
;
4048 seq_resp_auth
[2].len
= f_r0kh_id_len
;
4049 seq_resp_auth
[2].data
= f_r0kh_id
;
4050 seq_resp_auth
[3].type
= FT_RRB_R1KH_ID
;
4051 seq_resp_auth
[3].len
= FT_R1KH_ID_LEN
;
4052 seq_resp_auth
[3].data
= f_r1kh_id
;
4053 seq_resp_auth
[4].type
= FT_RRB_LAST_EMPTY
;
4054 seq_resp_auth
[4].len
= 0;
4055 seq_resp_auth
[4].data
= NULL
;
4057 if (wpa_ft_rrb_build(key
, key_len
, NULL
, NULL
, seq_resp_auth
, NULL
,
4058 wpa_auth
->addr
, FT_PACKET_R0KH_R1KH_SEQ_RESP
,
4059 &packet
, &packet_len
) < 0)
4062 wpa_ft_rrb_oui_send(wpa_auth
, src_addr
,
4063 FT_PACKET_R0KH_R1KH_SEQ_RESP
, packet
,
4073 static int wpa_ft_rrb_rx_seq_resp(struct wpa_authenticator
*wpa_auth
,
4075 const u8
*enc
, size_t enc_len
,
4076 const u8
*auth
, size_t auth_len
,
4081 struct ft_remote_r0kh
*r0kh
= NULL
, *r0kh_wildcard
= NULL
;
4082 struct ft_remote_r1kh
*r1kh
= NULL
, *r1kh_wildcard
= NULL
;
4083 const u8
*f_nonce
, *f_seq
;
4084 size_t f_nonce_len
, f_seq_len
;
4085 struct ft_remote_seq
*rkh_seq
= NULL
;
4086 struct ft_remote_item
*item
;
4087 struct os_reltime now
, now_remote
;
4089 const struct ft_rrb_seq
*msg_both
;
4090 u32 msg_dom
, msg_seq
;
4092 wpa_printf(MSG_DEBUG
, "FT: Received sequence number response");
4094 if (wpa_ft_rrb_rx_seq(wpa_auth
, src_addr
, FT_PACKET_R0KH_R1KH_SEQ_RESP
,
4095 enc
, enc_len
, auth
, auth_len
, &rkh_seq
, &key
,
4096 &key_len
, &r0kh
, &r1kh
, &r0kh_wildcard
,
4097 &r1kh_wildcard
) < 0)
4100 RRB_GET_AUTH(FT_RRB_NONCE
, nonce
, "seq response", FT_RRB_NONCE_LEN
);
4101 wpa_hexdump(MSG_DEBUG
, "FT: seq response - nonce", f_nonce
,
4105 dl_list_for_each(item
, &rkh_seq
->rx
.queue
, struct ft_remote_item
,
4107 if (os_memcmp_const(f_nonce
, item
->nonce
,
4108 FT_RRB_NONCE_LEN
) != 0 ||
4109 os_get_reltime(&now
) < 0 ||
4110 os_reltime_expired(&now
, &item
->nonce_ts
, ftRRBseqTimeout
))
4117 wpa_printf(MSG_DEBUG
, "FT: seq response - bad nonce");
4122 wpa_ft_rrb_r0kh_replenish(wpa_auth
, r0kh
,
4123 wpa_auth
->conf
.rkh_pos_timeout
);
4125 os_memcpy(r0kh
->addr
, src_addr
, ETH_ALEN
);
4129 wpa_ft_rrb_r1kh_replenish(wpa_auth
, r1kh
,
4130 wpa_auth
->conf
.rkh_pos_timeout
);
4132 os_memcpy(r1kh
->addr
, src_addr
, ETH_ALEN
);
4135 seq_ret
= wpa_ft_rrb_seq_chk(rkh_seq
, src_addr
, enc
, enc_len
, auth
,
4136 auth_len
, "seq response", 1);
4137 if (seq_ret
== FT_RRB_SEQ_OK
) {
4138 wpa_printf(MSG_DEBUG
, "FT: seq response - valid seq number");
4139 wpa_ft_rrb_seq_accept(wpa_auth
, rkh_seq
, src_addr
, auth
,
4140 auth_len
, "seq response");
4142 wpa_printf(MSG_DEBUG
, "FT: seq response - reset seq number");
4144 RRB_GET_AUTH(FT_RRB_SEQ
, seq
, "seq response",
4146 msg_both
= (const struct ft_rrb_seq
*) f_seq
;
4148 msg_dom
= le_to_host32(msg_both
->dom
);
4149 msg_seq
= le_to_host32(msg_both
->seq
);
4150 now_remote
.sec
= le_to_host32(msg_both
->ts
);
4151 now_remote
.usec
= 0;
4153 rkh_seq
->rx
.num_last
= 2;
4154 rkh_seq
->rx
.dom
= msg_dom
;
4155 rkh_seq
->rx
.offsetidx
= 0;
4156 /* Accept some older, possibly cached packets as well */
4157 rkh_seq
->rx
.last
[0] = msg_seq
- FT_REMOTE_SEQ_BACKLOG
-
4158 dl_list_len(&rkh_seq
->rx
.queue
);
4159 rkh_seq
->rx
.last
[1] = msg_seq
;
4161 /* local time - offset = remote time
4162 * <=> local time - remote time = offset */
4163 os_reltime_sub(&now
, &now_remote
, &rkh_seq
->rx
.time_offset
);
4166 wpa_ft_rrb_seq_flush(wpa_auth
, rkh_seq
, 1);
4174 int wpa_ft_rrb_rx(struct wpa_authenticator
*wpa_auth
, const u8
*src_addr
,
4175 const u8
*data
, size_t data_len
)
4177 struct ft_rrb_frame
*frame
;
4179 const u8
*pos
, *end
, *start
;
4181 const u8
*sta_addr
, *target_ap_addr
;
4183 wpa_printf(MSG_DEBUG
, "FT: RRB received frame from remote AP " MACSTR
,
4186 if (data_len
< sizeof(*frame
)) {
4187 wpa_printf(MSG_DEBUG
, "FT: Too short RRB frame (data_len=%lu)",
4188 (unsigned long) data_len
);
4193 frame
= (struct ft_rrb_frame
*) pos
;
4194 pos
+= sizeof(*frame
);
4196 alen
= le_to_host16(frame
->action_length
);
4197 wpa_printf(MSG_DEBUG
, "FT: RRB frame - frame_type=%d packet_type=%d "
4198 "action_length=%d ap_address=" MACSTR
,
4199 frame
->frame_type
, frame
->packet_type
, alen
,
4200 MAC2STR(frame
->ap_address
));
4202 if (frame
->frame_type
!= RSN_REMOTE_FRAME_TYPE_FT_RRB
) {
4203 /* Discard frame per IEEE Std 802.11r-2008, 11A.10.3 */
4204 wpa_printf(MSG_DEBUG
, "FT: RRB discarded frame with "
4205 "unrecognized type %d", frame
->frame_type
);
4209 if (alen
> data_len
- sizeof(*frame
)) {
4210 wpa_printf(MSG_DEBUG
, "FT: RRB frame too short for action "
4215 wpa_hexdump(MSG_MSGDUMP
, "FT: RRB - FT Action frame", pos
, alen
);
4217 if (alen
< 1 + 1 + 2 * ETH_ALEN
) {
4218 wpa_printf(MSG_DEBUG
, "FT: Too short RRB frame (not enough "
4219 "room for Action Frame body); alen=%lu",
4220 (unsigned long) alen
);
4226 if (*pos
!= WLAN_ACTION_FT
) {
4227 wpa_printf(MSG_DEBUG
, "FT: Unexpected Action frame category "
4236 target_ap_addr
= pos
;
4238 wpa_printf(MSG_DEBUG
, "FT: RRB Action Frame: action=%d sta_addr="
4239 MACSTR
" target_ap_addr=" MACSTR
,
4240 action
, MAC2STR(sta_addr
), MAC2STR(target_ap_addr
));
4242 if (frame
->packet_type
== FT_PACKET_REQUEST
) {
4243 wpa_printf(MSG_DEBUG
, "FT: FT Packet Type - Request");
4246 wpa_printf(MSG_DEBUG
, "FT: Unexpected Action %d in "
4247 "RRB Request", action
);
4251 if (os_memcmp(target_ap_addr
, wpa_auth
->addr
, ETH_ALEN
) != 0) {
4252 wpa_printf(MSG_DEBUG
, "FT: Target AP address in the "
4253 "RRB Request does not match with own "
4258 if (wpa_ft_rrb_rx_request(wpa_auth
, frame
->ap_address
,
4259 sta_addr
, pos
, end
- pos
) < 0)
4261 } else if (frame
->packet_type
== FT_PACKET_RESPONSE
) {
4264 if (end
- pos
< 2) {
4265 wpa_printf(MSG_DEBUG
, "FT: Not enough room for status "
4266 "code in RRB Response");
4269 status_code
= WPA_GET_LE16(pos
);
4272 wpa_printf(MSG_DEBUG
, "FT: FT Packet Type - Response "
4273 "(status_code=%d)", status_code
);
4275 if (wpa_ft_action_send(wpa_auth
, sta_addr
, start
, alen
) < 0)
4278 wpa_printf(MSG_DEBUG
, "FT: RRB discarded frame with unknown "
4279 "packet_type %d", frame
->packet_type
);
4284 wpa_hexdump(MSG_DEBUG
, "FT: Ignore extra data in end",
4292 void wpa_ft_rrb_oui_rx(struct wpa_authenticator
*wpa_auth
, const u8
*src_addr
,
4293 const u8
*dst_addr
, u8 oui_suffix
, const u8
*data
,
4296 const u8
*auth
, *enc
;
4300 wpa_printf(MSG_DEBUG
, "FT: RRB-OUI received frame from remote AP "
4301 MACSTR
, MAC2STR(src_addr
));
4302 wpa_printf(MSG_DEBUG
, "FT: RRB-OUI frame - oui_suffix=%d", oui_suffix
);
4304 if (is_multicast_ether_addr(src_addr
)) {
4305 wpa_printf(MSG_DEBUG
,
4306 "FT: RRB-OUI received frame from multicast address "
4307 MACSTR
, MAC2STR(src_addr
));
4311 if (is_multicast_ether_addr(dst_addr
)) {
4312 wpa_printf(MSG_DEBUG
,
4313 "FT: RRB-OUI received frame from remote AP " MACSTR
4314 " to multicast address " MACSTR
,
4315 MAC2STR(src_addr
), MAC2STR(dst_addr
));
4319 if (data_len
< sizeof(u16
)) {
4320 wpa_printf(MSG_DEBUG
, "FT: RRB-OUI frame too short");
4324 alen
= WPA_GET_LE16(data
);
4325 if (data_len
< sizeof(u16
) + alen
) {
4326 wpa_printf(MSG_DEBUG
, "FT: RRB-OUI frame too short");
4330 auth
= data
+ sizeof(u16
);
4331 enc
= data
+ sizeof(u16
) + alen
;
4332 elen
= data_len
- sizeof(u16
) - alen
;
4334 switch (oui_suffix
) {
4335 case FT_PACKET_R0KH_R1KH_PULL
:
4336 wpa_ft_rrb_rx_pull(wpa_auth
, src_addr
, enc
, elen
, auth
, alen
,
4339 case FT_PACKET_R0KH_R1KH_RESP
:
4340 wpa_ft_rrb_rx_resp(wpa_auth
, src_addr
, enc
, elen
, auth
, alen
,
4343 case FT_PACKET_R0KH_R1KH_PUSH
:
4344 wpa_ft_rrb_rx_push(wpa_auth
, src_addr
, enc
, elen
, auth
, alen
,
4347 case FT_PACKET_R0KH_R1KH_SEQ_REQ
:
4348 wpa_ft_rrb_rx_seq_req(wpa_auth
, src_addr
, enc
, elen
, auth
, alen
,
4351 case FT_PACKET_R0KH_R1KH_SEQ_RESP
:
4352 wpa_ft_rrb_rx_seq_resp(wpa_auth
, src_addr
, enc
, elen
, auth
,
4359 static int wpa_ft_generate_pmk_r1(struct wpa_authenticator
*wpa_auth
,
4360 struct wpa_ft_pmk_r0_sa
*pmk_r0
,
4361 struct ft_remote_r1kh
*r1kh
,
4366 struct ft_rrb_seq f_seq
;
4367 struct tlv_list push
[] = {
4368 { .type
= FT_RRB_S1KH_ID
, .len
= ETH_ALEN
,
4370 { .type
= FT_RRB_PMK_R0_NAME
, .len
= WPA_PMK_NAME_LEN
,
4371 .data
= pmk_r0
->pmk_r0_name
},
4372 { .type
= FT_RRB_LAST_EMPTY
, .len
= 0, .data
= NULL
},
4374 struct tlv_list push_auth
[] = {
4375 { .type
= FT_RRB_SEQ
, .len
= sizeof(f_seq
),
4376 .data
= (u8
*) &f_seq
},
4377 { .type
= FT_RRB_R0KH_ID
,
4378 .len
= wpa_auth
->conf
.r0_key_holder_len
,
4379 .data
= wpa_auth
->conf
.r0_key_holder
},
4380 { .type
= FT_RRB_R1KH_ID
, .len
= FT_R1KH_ID_LEN
,
4382 { .type
= FT_RRB_LAST_EMPTY
, .len
= 0, .data
= NULL
},
4385 if (wpa_ft_new_seq(r1kh
->seq
, &f_seq
) < 0) {
4386 wpa_printf(MSG_DEBUG
, "FT: Failed to get seq num");
4390 if (wpa_ft_rrb_build_r0(r1kh
->key
, sizeof(r1kh
->key
), push
, pmk_r0
,
4391 r1kh
->id
, s1kh_id
, push_auth
, wpa_auth
->addr
,
4392 FT_PACKET_R0KH_R1KH_PUSH
,
4393 &packet
, &packet_len
) < 0)
4396 wpa_ft_rrb_oui_send(wpa_auth
, r1kh
->addr
, FT_PACKET_R0KH_R1KH_PUSH
,
4397 packet
, packet_len
);
4404 void wpa_ft_push_pmk_r1(struct wpa_authenticator
*wpa_auth
, const u8
*addr
)
4406 struct wpa_ft_pmk_cache
*cache
= wpa_auth
->ft_pmk_cache
;
4407 struct wpa_ft_pmk_r0_sa
*r0
, *r0found
= NULL
;
4408 struct ft_remote_r1kh
*r1kh
;
4410 if (!wpa_auth
->conf
.pmk_r1_push
)
4412 if (!wpa_auth
->conf
.r1kh_list
)
4415 dl_list_for_each(r0
, &cache
->pmk_r0
, struct wpa_ft_pmk_r0_sa
, list
) {
4416 if (os_memcmp(r0
->spa
, addr
, ETH_ALEN
) == 0) {
4423 if (r0
== NULL
|| r0
->pmk_r1_pushed
)
4425 r0
->pmk_r1_pushed
= 1;
4427 wpa_printf(MSG_DEBUG
, "FT: Deriving and pushing PMK-R1 keys to R1KHs "
4428 "for STA " MACSTR
, MAC2STR(addr
));
4430 for (r1kh
= *wpa_auth
->conf
.r1kh_list
; r1kh
; r1kh
= r1kh
->next
) {
4431 if (is_zero_ether_addr(r1kh
->addr
) ||
4432 is_zero_ether_addr(r1kh
->id
))
4434 if (wpa_ft_rrb_init_r1kh_seq(r1kh
) < 0)
4436 wpa_ft_generate_pmk_r1(wpa_auth
, r0
, r1kh
, addr
);
4440 #endif /* CONFIG_IEEE80211R_AP */