2 * hostapd - IEEE 802.11r - Fast BSS Transition
3 * Copyright (c) 2004-2015, 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/random.h"
20 #include "ap_config.h"
21 #include "ieee802_11.h"
24 #include "wpa_auth_i.h"
27 #ifdef CONFIG_IEEE80211R_AP
29 const unsigned int ftRRBseqTimeout
= 10;
30 const unsigned int ftRRBmaxQueueLen
= 100;
33 static int wpa_ft_send_rrb_auth_resp(struct wpa_state_machine
*sm
,
34 const u8
*current_ap
, const u8
*sta_addr
,
35 u16 status
, const u8
*resp_ies
,
37 static void ft_finish_pull(struct wpa_state_machine
*sm
);
38 static void wpa_ft_expire_pull(void *eloop_ctx
, void *timeout_ctx
);
39 static void wpa_ft_rrb_seq_timeout(void *eloop_ctx
, void *timeout_ctx
);
49 * wpa_ft_rrb_decrypt - Decrypt FT RRB message
50 * @key: AES-SIV key for AEAD
51 * @key_len: Length of key in octets
52 * @enc: Pointer to encrypted TLVs
53 * @enc_len: Length of encrypted TLVs in octets
54 * @auth: Pointer to authenticated TLVs
55 * @auth_len: Length of authenticated TLVs in octets
56 * @src_addr: MAC address of the frame sender
57 * @type: Vendor-specific subtype of the RRB frame (FT_PACKET_*)
58 * @plain: Pointer to return the pointer to the allocated plaintext buffer;
59 * needs to be freed by the caller if not NULL;
60 * will only be returned on success
61 * @plain_len: Pointer to return the length of the allocated plaintext buffer
63 * Returns: 0 on success, -1 on error
65 static int wpa_ft_rrb_decrypt(const u8
*key
, const size_t key_len
,
66 const u8
*enc
, const size_t enc_len
,
67 const u8
*auth
, const size_t auth_len
,
68 const u8
*src_addr
, u8 type
,
69 u8
**plain
, size_t *plain_size
)
71 const u8
*ad
[3] = { src_addr
, auth
, &type
};
72 size_t ad_len
[3] = { ETH_ALEN
, auth_len
, sizeof(type
) };
74 wpa_hexdump_key(MSG_DEBUG
, "FT(RRB): decrypt using key", key
, key_len
);
76 if (!key
) { /* skip decryption */
77 *plain
= os_memdup(enc
, enc_len
);
78 if (enc_len
> 0 && !*plain
)
81 *plain_size
= enc_len
;
89 if (enc_len
< AES_BLOCK_SIZE
)
92 *plain
= os_zalloc(enc_len
- AES_BLOCK_SIZE
);
96 if (aes_siv_decrypt(key
, key_len
, enc
, enc_len
, 3, ad
, ad_len
,
100 *plain_size
= enc_len
- AES_BLOCK_SIZE
;
101 wpa_hexdump_key(MSG_DEBUG
, "FT(RRB): decrypted TLVs",
102 *plain
, *plain_size
);
109 wpa_printf(MSG_ERROR
, "FT(RRB): Failed to decrypt");
115 /* get first tlv record in packet matching type
116 * @data (decrypted) packet
117 * @return 0 on success else -1
119 static int wpa_ft_rrb_get_tlv(const u8
*plain
, size_t plain_len
,
120 u16 type
, size_t *tlv_len
, const u8
**tlv_data
)
122 const struct ft_rrb_tlv
*f
;
128 type16
= host_to_le16(type
);
130 while (left
>= sizeof(*f
)) {
131 f
= (const struct ft_rrb_tlv
*) plain
;
135 len
= le_to_host16(f
->len
);
138 wpa_printf(MSG_DEBUG
, "FT: RRB message truncated");
142 if (f
->type
== type16
) {
156 static void wpa_ft_rrb_dump(const u8
*plain
, const size_t plain_len
)
158 const struct ft_rrb_tlv
*f
;
164 wpa_printf(MSG_DEBUG
, "FT: RRB dump message");
165 while (left
>= sizeof(*f
)) {
166 f
= (const struct ft_rrb_tlv
*) plain
;
170 len
= le_to_host16(f
->len
);
172 wpa_printf(MSG_DEBUG
, "FT: RRB TLV type = %d, len = %zu",
173 le_to_host16(f
->type
), len
);
176 wpa_printf(MSG_DEBUG
,
177 "FT: RRB message truncated: left %zu bytes, need %zu",
182 wpa_hexdump(MSG_DEBUG
, "FT: RRB TLV data", plain
, len
);
189 wpa_hexdump(MSG_DEBUG
, "FT: RRB TLV padding", plain
, left
);
191 wpa_printf(MSG_DEBUG
, "FT: RRB dump message end");
195 static size_t wpa_ft_tlv_len(const struct tlv_list
*tlvs
)
203 for (i
= 0; tlvs
[i
].type
!= FT_RRB_LAST_EMPTY
; i
++) {
204 tlv_len
+= sizeof(struct ft_rrb_tlv
);
205 tlv_len
+= tlvs
[i
].len
;
212 static size_t wpa_ft_tlv_lin(const struct tlv_list
*tlvs
, u8
*start
,
217 struct ft_rrb_tlv
*hdr
;
225 for (i
= 0; tlvs
[i
].type
!= FT_RRB_LAST_EMPTY
; i
++) {
226 if (tlv_len
+ sizeof(*hdr
) > (size_t) (endpos
- start
))
228 tlv_len
+= sizeof(*hdr
);
229 hdr
= (struct ft_rrb_tlv
*) pos
;
230 hdr
->type
= host_to_le16(tlvs
[i
].type
);
231 hdr
->len
= host_to_le16(tlvs
[i
].len
);
232 pos
= start
+ tlv_len
;
234 if (tlv_len
+ tlvs
[i
].len
> (size_t) (endpos
- start
))
236 tlv_len
+= tlvs
[i
].len
;
237 os_memcpy(pos
, tlvs
[i
].data
, tlvs
[i
].len
);
238 pos
= start
+ tlv_len
;
245 static int wpa_ft_rrb_lin(const struct tlv_list
*tlvs1
,
246 const struct tlv_list
*tlvs2
,
247 u8
**plain
, size_t *plain_len
)
252 tlv_len
= wpa_ft_tlv_len(tlvs1
);
253 tlv_len
+= wpa_ft_tlv_len(tlvs2
);
255 *plain_len
= tlv_len
;
256 *plain
= os_zalloc(tlv_len
);
258 wpa_printf(MSG_ERROR
, "FT: Failed to allocate plaintext");
263 endpos
= *plain
+ tlv_len
;
264 pos
+= wpa_ft_tlv_lin(tlvs1
, pos
, endpos
);
265 pos
+= wpa_ft_tlv_lin(tlvs2
, pos
, endpos
);
269 wpa_printf(MSG_ERROR
, "FT: Length error building RRB");
283 static int wpa_ft_rrb_encrypt(const u8
*key
, const size_t key_len
,
284 const u8
*plain
, const size_t plain_len
,
285 const u8
*auth
, const size_t auth_len
,
286 const u8
*src_addr
, u8 type
, u8
*enc
)
288 const u8
*ad
[3] = { src_addr
, auth
, &type
};
289 size_t ad_len
[3] = { ETH_ALEN
, auth_len
, sizeof(type
) };
291 wpa_hexdump_key(MSG_DEBUG
, "FT(RRB): plaintext message",
293 wpa_hexdump_key(MSG_DEBUG
, "FT(RRB): encrypt using key", key
, key_len
);
296 /* encryption not needed, return plaintext as packet */
297 os_memcpy(enc
, plain
, plain_len
);
298 } else if (aes_siv_encrypt(key
, key_len
, plain
, plain_len
,
299 3, ad
, ad_len
, enc
) < 0) {
300 wpa_printf(MSG_ERROR
, "FT: Failed to encrypt RRB-OUI message");
309 * wpa_ft_rrb_build - Build and encrypt an FT RRB message
310 * @key: AES-SIV key for AEAD
311 * @key_len: Length of key in octets
312 * @tlvs_enc0: First set of to-be-encrypted TLVs
313 * @tlvs_enc1: Second set of to-be-encrypted TLVs
314 * @tlvs_auth: Set of to-be-authenticated TLVs
315 * @src_addr: MAC address of the frame sender
316 * @type: Vendor-specific subtype of the RRB frame (FT_PACKET_*)
317 * @packet Pointer to return the pointer to the allocated packet buffer;
318 * needs to be freed by the caller if not null;
319 * will only be returned on success
320 * @packet_len: Pointer to return the length of the allocated buffer in octets
321 * Returns: 0 on success, -1 on error
323 static int wpa_ft_rrb_build(const u8
*key
, const size_t key_len
,
324 const struct tlv_list
*tlvs_enc0
,
325 const struct tlv_list
*tlvs_enc1
,
326 const struct tlv_list
*tlvs_auth
,
327 const u8
*src_addr
, u8 type
,
328 u8
**packet
, size_t *packet_len
)
330 u8
*plain
= NULL
, *auth
= NULL
, *pos
;
331 size_t plain_len
= 0, auth_len
= 0;
334 if (wpa_ft_rrb_lin(tlvs_enc0
, tlvs_enc1
, &plain
, &plain_len
) < 0)
337 if (wpa_ft_rrb_lin(tlvs_auth
, NULL
, &auth
, &auth_len
) < 0)
340 *packet_len
= sizeof(u16
) + auth_len
+ plain_len
;
342 *packet_len
+= AES_BLOCK_SIZE
;
343 *packet
= os_zalloc(*packet_len
);
348 WPA_PUT_LE16(pos
, auth_len
);
350 os_memcpy(pos
, auth
, auth_len
);
352 if (wpa_ft_rrb_encrypt(key
, key_len
, plain
, plain_len
, auth
,
353 auth_len
, src_addr
, type
, pos
) < 0)
359 bin_clear_free(plain
, plain_len
);
363 wpa_printf(MSG_ERROR
, "FT: Failed to build RRB-OUI message");
373 #define RRB_GET_SRC(srcfield, type, field, txt, checklength) do { \
374 if (wpa_ft_rrb_get_tlv(srcfield, srcfield##_len, type, \
375 &f_##field##_len, &f_##field) < 0 || \
376 (checklength > 0 && ((size_t) checklength) != f_##field##_len)) { \
377 wpa_printf(MSG_INFO, "FT: Missing required " #field \
378 " in %s from " MACSTR, txt, MAC2STR(src_addr)); \
379 wpa_ft_rrb_dump(srcfield, srcfield##_len); \
384 #define RRB_GET(type, field, txt, checklength) \
385 RRB_GET_SRC(plain, type, field, txt, checklength)
386 #define RRB_GET_AUTH(type, field, txt, checklength) \
387 RRB_GET_SRC(auth, type, field, txt, checklength)
389 #define RRB_GET_OPTIONAL_SRC(srcfield, type, field, txt, checklength) do { \
390 if (wpa_ft_rrb_get_tlv(srcfield, srcfield##_len, type, \
391 &f_##field##_len, &f_##field) < 0 || \
392 (checklength > 0 && ((size_t) checklength) != f_##field##_len)) { \
393 wpa_printf(MSG_DEBUG, "FT: Missing optional " #field \
394 " in %s from " MACSTR, txt, MAC2STR(src_addr)); \
395 f_##field##_len = 0; \
400 #define RRB_GET_OPTIONAL(type, field, txt, checklength) \
401 RRB_GET_OPTIONAL_SRC(plain, type, field, txt, checklength)
402 #define RRB_GET_OPTIONAL_AUTH(type, field, txt, checklength) \
403 RRB_GET_OPTIONAL_SRC(auth, type, field, txt, checklength)
405 static int wpa_ft_rrb_send(struct wpa_authenticator
*wpa_auth
, const u8
*dst
,
406 const u8
*data
, size_t data_len
)
408 if (wpa_auth
->cb
->send_ether
== NULL
)
410 wpa_printf(MSG_DEBUG
, "FT: RRB send to " MACSTR
, MAC2STR(dst
));
411 return wpa_auth
->cb
->send_ether(wpa_auth
->cb_ctx
, dst
, ETH_P_RRB
,
416 static int wpa_ft_rrb_oui_send(struct wpa_authenticator
*wpa_auth
,
417 const u8
*dst
, u8 oui_suffix
,
418 const u8
*data
, size_t data_len
)
420 if (!wpa_auth
->cb
->send_oui
)
422 wpa_printf(MSG_DEBUG
, "FT: RRB-OUI type %u send to " MACSTR
,
423 oui_suffix
, MAC2STR(dst
));
424 return wpa_auth
->cb
->send_oui(wpa_auth
->cb_ctx
, dst
, oui_suffix
, data
,
429 static int wpa_ft_action_send(struct wpa_authenticator
*wpa_auth
,
430 const u8
*dst
, const u8
*data
, size_t data_len
)
432 if (wpa_auth
->cb
->send_ft_action
== NULL
)
434 return wpa_auth
->cb
->send_ft_action(wpa_auth
->cb_ctx
, dst
,
439 static const u8
* wpa_ft_get_psk(struct wpa_authenticator
*wpa_auth
,
440 const u8
*addr
, const u8
*p2p_dev_addr
,
443 if (wpa_auth
->cb
->get_psk
== NULL
)
445 return wpa_auth
->cb
->get_psk(wpa_auth
->cb_ctx
, addr
, p2p_dev_addr
,
450 static struct wpa_state_machine
*
451 wpa_ft_add_sta(struct wpa_authenticator
*wpa_auth
, const u8
*sta_addr
)
453 if (wpa_auth
->cb
->add_sta
== NULL
)
455 return wpa_auth
->cb
->add_sta(wpa_auth
->cb_ctx
, sta_addr
);
459 static int wpa_ft_add_tspec(struct wpa_authenticator
*wpa_auth
,
461 u8
*tspec_ie
, size_t tspec_ielen
)
463 if (wpa_auth
->cb
->add_tspec
== NULL
) {
464 wpa_printf(MSG_DEBUG
, "FT: add_tspec is not initialized");
467 return wpa_auth
->cb
->add_tspec(wpa_auth
->cb_ctx
, sta_addr
, tspec_ie
,
472 int wpa_write_mdie(struct wpa_auth_config
*conf
, u8
*buf
, size_t len
)
476 if (len
< 2 + sizeof(struct rsn_mdie
))
479 *pos
++ = WLAN_EID_MOBILITY_DOMAIN
;
480 *pos
++ = MOBILITY_DOMAIN_ID_LEN
+ 1;
481 os_memcpy(pos
, conf
->mobility_domain
, MOBILITY_DOMAIN_ID_LEN
);
482 pos
+= MOBILITY_DOMAIN_ID_LEN
;
484 if (conf
->ft_over_ds
)
485 capab
|= RSN_FT_CAPAB_FT_OVER_DS
;
492 int wpa_write_ftie(struct wpa_auth_config
*conf
, const u8
*r0kh_id
,
494 const u8
*anonce
, const u8
*snonce
,
495 u8
*buf
, size_t len
, const u8
*subelem
,
498 u8
*pos
= buf
, *ielen
;
499 struct rsn_ftie
*hdr
;
501 if (len
< 2 + sizeof(*hdr
) + 2 + FT_R1KH_ID_LEN
+ 2 + r0kh_id_len
+
505 *pos
++ = WLAN_EID_FAST_BSS_TRANSITION
;
508 hdr
= (struct rsn_ftie
*) pos
;
509 os_memset(hdr
, 0, sizeof(*hdr
));
511 WPA_PUT_LE16(hdr
->mic_control
, 0);
513 os_memcpy(hdr
->anonce
, anonce
, WPA_NONCE_LEN
);
515 os_memcpy(hdr
->snonce
, snonce
, WPA_NONCE_LEN
);
517 /* Optional Parameters */
518 *pos
++ = FTIE_SUBELEM_R1KH_ID
;
519 *pos
++ = FT_R1KH_ID_LEN
;
520 os_memcpy(pos
, conf
->r1_key_holder
, FT_R1KH_ID_LEN
);
521 pos
+= FT_R1KH_ID_LEN
;
524 *pos
++ = FTIE_SUBELEM_R0KH_ID
;
525 *pos
++ = r0kh_id_len
;
526 os_memcpy(pos
, r0kh_id
, r0kh_id_len
);
531 os_memcpy(pos
, subelem
, subelem_len
);
535 *ielen
= pos
- buf
- 2;
541 /* A packet to be handled after seq response */
542 struct ft_remote_item
{
545 u8 nonce
[FT_RRB_NONCE_LEN
];
546 struct os_reltime nonce_ts
;
548 u8 src_addr
[ETH_ALEN
];
553 int (*cb
)(struct wpa_authenticator
*wpa_auth
,
555 const u8
*enc
, size_t enc_len
,
556 const u8
*auth
, size_t auth_len
,
561 static void wpa_ft_rrb_seq_free(struct ft_remote_item
*item
)
563 eloop_cancel_timeout(wpa_ft_rrb_seq_timeout
, ELOOP_ALL_CTX
, item
);
564 dl_list_del(&item
->list
);
565 bin_clear_free(item
->enc
, item
->enc_len
);
571 static void wpa_ft_rrb_seq_flush(struct wpa_authenticator
*wpa_auth
,
572 struct ft_remote_seq
*rkh_seq
, int cb
)
574 struct ft_remote_item
*item
, *n
;
576 dl_list_for_each_safe(item
, n
, &rkh_seq
->rx
.queue
,
577 struct ft_remote_item
, list
) {
579 item
->cb(wpa_auth
, item
->src_addr
, item
->enc
,
580 item
->enc_len
, item
->auth
, item
->auth_len
, 1);
581 wpa_ft_rrb_seq_free(item
);
586 static void wpa_ft_rrb_seq_timeout(void *eloop_ctx
, void *timeout_ctx
)
588 struct ft_remote_item
*item
= timeout_ctx
;
590 wpa_ft_rrb_seq_free(item
);
595 wpa_ft_rrb_seq_req(struct wpa_authenticator
*wpa_auth
,
596 struct ft_remote_seq
*rkh_seq
, const u8
*src_addr
,
597 const u8
*f_r0kh_id
, size_t f_r0kh_id_len
,
598 const u8
*f_r1kh_id
, const u8
*key
, size_t key_len
,
599 const u8
*enc
, size_t enc_len
,
600 const u8
*auth
, size_t auth_len
,
601 int (*cb
)(struct wpa_authenticator
*wpa_auth
,
603 const u8
*enc
, size_t enc_len
,
604 const u8
*auth
, size_t auth_len
,
607 struct ft_remote_item
*item
= NULL
;
610 struct tlv_list seq_req_auth
[] = {
611 { .type
= FT_RRB_NONCE
, .len
= FT_RRB_NONCE_LEN
,
612 .data
= NULL
/* to be filled: item->nonce */ },
613 { .type
= FT_RRB_R0KH_ID
, .len
= f_r0kh_id_len
,
615 { .type
= FT_RRB_R1KH_ID
, .len
= FT_R1KH_ID_LEN
,
617 { .type
= FT_RRB_LAST_EMPTY
, .len
= 0, .data
= NULL
},
620 if (dl_list_len(&rkh_seq
->rx
.queue
) >= ftRRBmaxQueueLen
) {
621 wpa_printf(MSG_DEBUG
, "FT: Sequence number queue too long");
625 item
= os_zalloc(sizeof(*item
));
629 os_memcpy(item
->src_addr
, src_addr
, ETH_ALEN
);
632 if (random_get_bytes(item
->nonce
, FT_RRB_NONCE_LEN
) < 0) {
633 wpa_printf(MSG_DEBUG
, "FT: Seq num nonce: out of random bytes");
637 if (os_get_reltime(&item
->nonce_ts
) < 0)
640 if (enc
&& enc_len
> 0) {
641 item
->enc
= os_memdup(enc
, enc_len
);
642 item
->enc_len
= enc_len
;
647 if (auth
&& auth_len
> 0) {
648 item
->auth
= os_memdup(auth
, auth_len
);
649 item
->auth_len
= auth_len
;
654 eloop_register_timeout(ftRRBseqTimeout
, 0, wpa_ft_rrb_seq_timeout
,
657 seq_req_auth
[0].data
= item
->nonce
;
659 if (wpa_ft_rrb_build(key
, key_len
, NULL
, NULL
, seq_req_auth
,
660 wpa_auth
->addr
, FT_PACKET_R0KH_R1KH_SEQ_REQ
,
661 &packet
, &packet_len
) < 0) {
662 item
= NULL
; /* some other seq resp might still accept this */
666 dl_list_add(&rkh_seq
->rx
.queue
, &item
->list
);
668 wpa_ft_rrb_oui_send(wpa_auth
, src_addr
, FT_PACKET_R0KH_R1KH_SEQ_REQ
,
675 wpa_printf(MSG_DEBUG
, "FT: Failed to send sequence number request");
678 bin_clear_free(item
->enc
, item
->enc_len
);
686 #define FT_RRB_SEQ_OK 0
687 #define FT_RRB_SEQ_DROP 1
688 #define FT_RRB_SEQ_DEFER 2
691 wpa_ft_rrb_seq_chk(struct ft_remote_seq
*rkh_seq
, const u8
*src_addr
,
692 const u8
*enc
, size_t enc_len
,
693 const u8
*auth
, size_t auth_len
,
694 const char *msgtype
, int no_defer
)
698 const struct ft_rrb_seq
*msg_both
;
699 u32 msg_seq
, msg_off
, rkh_off
;
700 struct os_reltime now
;
703 RRB_GET_AUTH(FT_RRB_SEQ
, seq
, msgtype
, sizeof(*msg_both
));
704 wpa_hexdump(MSG_DEBUG
, "FT: sequence number", f_seq
, f_seq_len
);
705 msg_both
= (const struct ft_rrb_seq
*) f_seq
;
707 if (rkh_seq
->rx
.num_last
== 0) {
708 /* first packet from remote */
712 if (le_to_host32(msg_both
->dom
) != rkh_seq
->rx
.dom
) {
713 /* remote might have rebooted */
717 if (os_get_reltime(&now
) == 0) {
718 u32 msg_ts_now_remote
, msg_ts_off
;
719 struct os_reltime now_remote
;
721 os_reltime_sub(&now
, &rkh_seq
->rx
.time_offset
, &now_remote
);
722 msg_ts_now_remote
= now_remote
.sec
;
723 msg_ts_off
= le_to_host32(msg_both
->ts
) -
724 (msg_ts_now_remote
- ftRRBseqTimeout
);
725 if (msg_ts_off
> 2 * ftRRBseqTimeout
)
729 msg_seq
= le_to_host32(msg_both
->seq
);
730 rkh_off
= rkh_seq
->rx
.last
[rkh_seq
->rx
.offsetidx
];
731 msg_off
= msg_seq
- rkh_off
;
732 if (msg_off
> 0xC0000000)
733 goto out
; /* too old message, drop it */
735 if (msg_off
<= 0x40000000) {
736 for (i
= 0; i
< rkh_seq
->rx
.num_last
; i
++) {
737 if (rkh_seq
->rx
.last
[i
] == msg_seq
)
738 goto out
; /* duplicate message, drop it */
741 return FT_RRB_SEQ_OK
;
748 wpa_printf(MSG_DEBUG
, "FT: Possibly invalid sequence number in %s from "
749 MACSTR
, msgtype
, MAC2STR(src_addr
));
751 return FT_RRB_SEQ_DEFER
;
753 wpa_printf(MSG_DEBUG
, "FT: Invalid sequence number in %s from " MACSTR
,
754 msgtype
, MAC2STR(src_addr
));
756 return FT_RRB_SEQ_DROP
;
761 wpa_ft_rrb_seq_accept(struct wpa_authenticator
*wpa_auth
,
762 struct ft_remote_seq
*rkh_seq
, const u8
*src_addr
,
763 const u8
*auth
, size_t auth_len
,
768 const struct ft_rrb_seq
*msg_both
;
769 u32 msg_seq
, msg_off
, min_off
, rkh_off
;
773 RRB_GET_AUTH(FT_RRB_SEQ
, seq
, msgtype
, sizeof(*msg_both
));
774 msg_both
= (const struct ft_rrb_seq
*) f_seq
;
776 msg_seq
= le_to_host32(msg_both
->seq
);
778 if (rkh_seq
->rx
.num_last
< FT_REMOTE_SEQ_BACKLOG
) {
779 rkh_seq
->rx
.last
[rkh_seq
->rx
.num_last
] = msg_seq
;
780 rkh_seq
->rx
.num_last
++;
784 rkh_off
= rkh_seq
->rx
.last
[rkh_seq
->rx
.offsetidx
];
785 for (i
= 0; i
< rkh_seq
->rx
.num_last
; i
++) {
786 msg_off
= rkh_seq
->rx
.last
[i
] - rkh_off
;
787 min_off
= rkh_seq
->rx
.last
[minidx
] - rkh_off
;
788 if (msg_off
< min_off
&& i
!= rkh_seq
->rx
.offsetidx
)
791 rkh_seq
->rx
.last
[rkh_seq
->rx
.offsetidx
] = msg_seq
;
792 rkh_seq
->rx
.offsetidx
= minidx
;
796 /* RRB_GET_AUTH should never fail here as
797 * wpa_ft_rrb_seq_chk() verified FT_RRB_SEQ presence. */
798 wpa_printf(MSG_ERROR
, "FT: %s() failed", __func__
);
802 static int wpa_ft_new_seq(struct ft_remote_seq
*rkh_seq
,
803 struct ft_rrb_seq
*f_seq
)
805 struct os_reltime now
;
807 if (os_get_reltime(&now
) < 0)
810 if (!rkh_seq
->tx
.dom
) {
811 if (random_get_bytes((u8
*) &rkh_seq
->tx
.seq
,
812 sizeof(rkh_seq
->tx
.seq
))) {
813 wpa_printf(MSG_ERROR
,
814 "FT: Failed to get random data for sequence number initialization");
815 rkh_seq
->tx
.seq
= now
.usec
;
817 if (random_get_bytes((u8
*) &rkh_seq
->tx
.dom
,
818 sizeof(rkh_seq
->tx
.dom
))) {
819 wpa_printf(MSG_ERROR
,
820 "FT: Failed to get random data for sequence number initialization");
821 rkh_seq
->tx
.dom
= now
.usec
;
823 rkh_seq
->tx
.dom
|= 1;
826 f_seq
->dom
= host_to_le32(rkh_seq
->tx
.dom
);
827 f_seq
->seq
= host_to_le32(rkh_seq
->tx
.seq
);
828 f_seq
->ts
= host_to_le32(now
.sec
);
836 struct wpa_ft_pmk_r0_sa
{
837 struct wpa_ft_pmk_r0_sa
*next
;
839 u8 pmk_r0_name
[WPA_PMK_NAME_LEN
];
841 int pairwise
; /* Pairwise cipher suite, WPA_CIPHER_* */
842 /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */
846 struct wpa_ft_pmk_r1_sa
{
847 struct wpa_ft_pmk_r1_sa
*next
;
849 u8 pmk_r1_name
[WPA_PMK_NAME_LEN
];
851 int pairwise
; /* Pairwise cipher suite, WPA_CIPHER_* */
852 /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */
855 struct wpa_ft_pmk_cache
{
856 struct wpa_ft_pmk_r0_sa
*pmk_r0
;
857 struct wpa_ft_pmk_r1_sa
*pmk_r1
;
860 struct wpa_ft_pmk_cache
* wpa_ft_pmk_cache_init(void)
862 struct wpa_ft_pmk_cache
*cache
;
864 cache
= os_zalloc(sizeof(*cache
));
870 void wpa_ft_pmk_cache_deinit(struct wpa_ft_pmk_cache
*cache
)
872 struct wpa_ft_pmk_r0_sa
*r0
, *r0prev
;
873 struct wpa_ft_pmk_r1_sa
*r1
, *r1prev
;
879 os_memset(r0prev
->pmk_r0
, 0, PMK_LEN
);
887 os_memset(r1prev
->pmk_r1
, 0, PMK_LEN
);
895 int wpa_ft_store_pmk_r0(struct wpa_authenticator
*wpa_auth
,
896 const u8
*spa
, const u8
*pmk_r0
,
897 const u8
*pmk_r0_name
, int pairwise
)
899 struct wpa_ft_pmk_cache
*cache
= wpa_auth
->ft_pmk_cache
;
900 struct wpa_ft_pmk_r0_sa
*r0
;
902 /* TODO: add expiration and limit on number of entries in cache */
904 r0
= os_zalloc(sizeof(*r0
));
908 os_memcpy(r0
->pmk_r0
, pmk_r0
, PMK_LEN
);
909 os_memcpy(r0
->pmk_r0_name
, pmk_r0_name
, WPA_PMK_NAME_LEN
);
910 os_memcpy(r0
->spa
, spa
, ETH_ALEN
);
911 r0
->pairwise
= pairwise
;
913 r0
->next
= cache
->pmk_r0
;
920 static int wpa_ft_fetch_pmk_r0(struct wpa_authenticator
*wpa_auth
,
921 const u8
*spa
, const u8
*pmk_r0_name
,
922 const struct wpa_ft_pmk_r0_sa
**r0_out
)
924 struct wpa_ft_pmk_cache
*cache
= wpa_auth
->ft_pmk_cache
;
925 struct wpa_ft_pmk_r0_sa
*r0
;
929 if (os_memcmp(r0
->spa
, spa
, ETH_ALEN
) == 0 &&
930 os_memcmp_const(r0
->pmk_r0_name
, pmk_r0_name
,
931 WPA_PMK_NAME_LEN
) == 0) {
944 static int wpa_ft_store_pmk_r1(struct wpa_authenticator
*wpa_auth
,
945 const u8
*spa
, const u8
*pmk_r1
,
946 const u8
*pmk_r1_name
, int pairwise
)
948 struct wpa_ft_pmk_cache
*cache
= wpa_auth
->ft_pmk_cache
;
949 struct wpa_ft_pmk_r1_sa
*r1
;
951 /* TODO: add expiration and limit on number of entries in cache */
953 r1
= os_zalloc(sizeof(*r1
));
957 os_memcpy(r1
->pmk_r1
, pmk_r1
, PMK_LEN
);
958 os_memcpy(r1
->pmk_r1_name
, pmk_r1_name
, WPA_PMK_NAME_LEN
);
959 os_memcpy(r1
->spa
, spa
, ETH_ALEN
);
960 r1
->pairwise
= pairwise
;
962 r1
->next
= cache
->pmk_r1
;
969 static int wpa_ft_fetch_pmk_r1(struct wpa_authenticator
*wpa_auth
,
970 const u8
*spa
, const u8
*pmk_r1_name
,
971 u8
*pmk_r1
, int *pairwise
)
973 struct wpa_ft_pmk_cache
*cache
= wpa_auth
->ft_pmk_cache
;
974 struct wpa_ft_pmk_r1_sa
*r1
;
978 if (os_memcmp(r1
->spa
, spa
, ETH_ALEN
) == 0 &&
979 os_memcmp_const(r1
->pmk_r1_name
, pmk_r1_name
,
980 WPA_PMK_NAME_LEN
) == 0) {
981 os_memcpy(pmk_r1
, r1
->pmk_r1
, PMK_LEN
);
983 *pairwise
= r1
->pairwise
;
994 static int wpa_ft_rrb_init_r0kh_seq(struct ft_remote_r0kh
*r0kh
)
999 r0kh
->seq
= os_zalloc(sizeof(*r0kh
->seq
));
1001 wpa_printf(MSG_DEBUG
, "FT: Failed to allocate r0kh->seq");
1005 dl_list_init(&r0kh
->seq
->rx
.queue
);
1011 static void wpa_ft_rrb_lookup_r0kh(struct wpa_authenticator
*wpa_auth
,
1012 const u8
*f_r0kh_id
, size_t f_r0kh_id_len
,
1013 struct ft_remote_r0kh
**r0kh_out
,
1014 struct ft_remote_r0kh
**r0kh_wildcard
)
1016 struct ft_remote_r0kh
*r0kh
;
1018 *r0kh_wildcard
= NULL
;
1021 if (wpa_auth
->conf
.r0kh_list
)
1022 r0kh
= *wpa_auth
->conf
.r0kh_list
;
1025 for (; r0kh
; r0kh
= r0kh
->next
) {
1026 if (r0kh
->id_len
== 1 && r0kh
->id
[0] == '*')
1027 *r0kh_wildcard
= r0kh
;
1028 if (f_r0kh_id
&& r0kh
->id_len
== f_r0kh_id_len
&&
1029 os_memcmp_const(f_r0kh_id
, r0kh
->id
, f_r0kh_id_len
) == 0)
1033 if (!*r0kh_out
&& !*r0kh_wildcard
)
1034 wpa_printf(MSG_DEBUG
, "FT: No matching R0KH found");
1036 if (*r0kh_out
&& wpa_ft_rrb_init_r0kh_seq(*r0kh_out
) < 0)
1041 static int wpa_ft_rrb_init_r1kh_seq(struct ft_remote_r1kh
*r1kh
)
1046 r1kh
->seq
= os_zalloc(sizeof(*r1kh
->seq
));
1048 wpa_printf(MSG_DEBUG
, "FT: Failed to allocate r1kh->seq");
1052 dl_list_init(&r1kh
->seq
->rx
.queue
);
1058 static void wpa_ft_rrb_lookup_r1kh(struct wpa_authenticator
*wpa_auth
,
1059 const u8
*f_r1kh_id
,
1060 struct ft_remote_r1kh
**r1kh_out
,
1061 struct ft_remote_r1kh
**r1kh_wildcard
)
1063 struct ft_remote_r1kh
*r1kh
;
1065 *r1kh_wildcard
= NULL
;
1068 if (wpa_auth
->conf
.r1kh_list
)
1069 r1kh
= *wpa_auth
->conf
.r1kh_list
;
1072 for (; r1kh
; r1kh
= r1kh
->next
) {
1073 if (is_zero_ether_addr(r1kh
->addr
) &&
1074 is_zero_ether_addr(r1kh
->id
))
1075 *r1kh_wildcard
= r1kh
;
1077 os_memcmp_const(r1kh
->id
, f_r1kh_id
, FT_R1KH_ID_LEN
) == 0)
1081 if (!*r1kh_out
&& !*r1kh_wildcard
)
1082 wpa_printf(MSG_DEBUG
, "FT: No matching R1KH found");
1084 if (*r1kh_out
&& wpa_ft_rrb_init_r1kh_seq(*r1kh_out
) < 0)
1089 static int wpa_ft_rrb_check_r0kh(struct wpa_authenticator
*wpa_auth
,
1090 const u8
*f_r0kh_id
, size_t f_r0kh_id_len
)
1092 if (f_r0kh_id_len
!= wpa_auth
->conf
.r0_key_holder_len
||
1093 os_memcmp_const(f_r0kh_id
, wpa_auth
->conf
.r0_key_holder
,
1094 f_r0kh_id_len
) != 0)
1101 static int wpa_ft_rrb_check_r1kh(struct wpa_authenticator
*wpa_auth
,
1102 const u8
*f_r1kh_id
)
1104 if (os_memcmp_const(f_r1kh_id
, wpa_auth
->conf
.r1_key_holder
,
1105 FT_R1KH_ID_LEN
) != 0)
1112 static void wpa_ft_rrb_del_r0kh(void *eloop_ctx
, void *timeout_ctx
)
1114 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
1115 struct ft_remote_r0kh
*r0kh
, *prev
= NULL
;
1117 if (!wpa_auth
->conf
.r0kh_list
)
1120 for (r0kh
= *wpa_auth
->conf
.r0kh_list
; r0kh
; r0kh
= r0kh
->next
) {
1121 if (r0kh
== timeout_ctx
)
1128 prev
->next
= r0kh
->next
;
1130 *wpa_auth
->conf
.r0kh_list
= r0kh
->next
;
1132 wpa_ft_rrb_seq_flush(wpa_auth
, r0kh
->seq
, 0);
1138 static void wpa_ft_rrb_r0kh_replenish(struct wpa_authenticator
*wpa_auth
,
1139 struct ft_remote_r0kh
*r0kh
, int timeout
)
1142 eloop_replenish_timeout(timeout
, 0, wpa_ft_rrb_del_r0kh
,
1147 static void wpa_ft_rrb_r0kh_timeout(struct wpa_authenticator
*wpa_auth
,
1148 struct ft_remote_r0kh
*r0kh
, int timeout
)
1150 eloop_cancel_timeout(wpa_ft_rrb_del_r0kh
, wpa_auth
, r0kh
);
1153 eloop_register_timeout(timeout
, 0, wpa_ft_rrb_del_r0kh
,
1158 static struct ft_remote_r0kh
*
1159 wpa_ft_rrb_add_r0kh(struct wpa_authenticator
*wpa_auth
,
1160 struct ft_remote_r0kh
*r0kh_wildcard
,
1161 const u8
*src_addr
, const u8
*r0kh_id
, size_t id_len
,
1164 struct ft_remote_r0kh
*r0kh
;
1166 if (!wpa_auth
->conf
.r0kh_list
)
1169 r0kh
= os_zalloc(sizeof(*r0kh
));
1174 os_memcpy(r0kh
->addr
, src_addr
, sizeof(r0kh
->addr
));
1176 if (id_len
> FT_R0KH_ID_MAX_LEN
)
1177 id_len
= FT_R0KH_ID_MAX_LEN
;
1178 os_memcpy(r0kh
->id
, r0kh_id
, id_len
);
1179 r0kh
->id_len
= id_len
;
1181 os_memcpy(r0kh
->key
, r0kh_wildcard
->key
, sizeof(r0kh
->key
));
1183 r0kh
->next
= *wpa_auth
->conf
.r0kh_list
;
1184 *wpa_auth
->conf
.r0kh_list
= r0kh
;
1187 eloop_register_timeout(timeout
, 0, wpa_ft_rrb_del_r0kh
,
1190 if (wpa_ft_rrb_init_r0kh_seq(r0kh
) < 0)
1197 static void wpa_ft_rrb_del_r1kh(void *eloop_ctx
, void *timeout_ctx
)
1199 struct wpa_authenticator
*wpa_auth
= eloop_ctx
;
1200 struct ft_remote_r1kh
*r1kh
, *prev
= NULL
;
1202 if (!wpa_auth
->conf
.r1kh_list
)
1205 for (r1kh
= *wpa_auth
->conf
.r1kh_list
; r1kh
; r1kh
= r1kh
->next
) {
1206 if (r1kh
== timeout_ctx
)
1213 prev
->next
= r1kh
->next
;
1215 *wpa_auth
->conf
.r1kh_list
= r1kh
->next
;
1217 wpa_ft_rrb_seq_flush(wpa_auth
, r1kh
->seq
, 0);
1223 static void wpa_ft_rrb_r1kh_replenish(struct wpa_authenticator
*wpa_auth
,
1224 struct ft_remote_r1kh
*r1kh
, int timeout
)
1227 eloop_replenish_timeout(timeout
, 0, wpa_ft_rrb_del_r1kh
,
1232 static struct ft_remote_r1kh
*
1233 wpa_ft_rrb_add_r1kh(struct wpa_authenticator
*wpa_auth
,
1234 struct ft_remote_r1kh
*r1kh_wildcard
,
1235 const u8
*src_addr
, const u8
*r1kh_id
, int timeout
)
1237 struct ft_remote_r1kh
*r1kh
;
1239 if (!wpa_auth
->conf
.r1kh_list
)
1242 r1kh
= os_zalloc(sizeof(*r1kh
));
1246 os_memcpy(r1kh
->addr
, src_addr
, sizeof(r1kh
->addr
));
1247 os_memcpy(r1kh
->id
, r1kh_id
, sizeof(r1kh
->id
));
1248 os_memcpy(r1kh
->key
, r1kh_wildcard
->key
, sizeof(r1kh
->key
));
1249 r1kh
->next
= *wpa_auth
->conf
.r1kh_list
;
1250 *wpa_auth
->conf
.r1kh_list
= r1kh
;
1253 eloop_register_timeout(timeout
, 0, wpa_ft_rrb_del_r1kh
,
1256 if (wpa_ft_rrb_init_r1kh_seq(r1kh
) < 0)
1263 void wpa_ft_sta_deinit(struct wpa_state_machine
*sm
)
1265 eloop_cancel_timeout(wpa_ft_expire_pull
, sm
, NULL
);
1269 static void wpa_ft_deinit_seq(struct wpa_authenticator
*wpa_auth
)
1271 struct ft_remote_r0kh
*r0kh
;
1272 struct ft_remote_r1kh
*r1kh
;
1274 eloop_cancel_timeout(wpa_ft_rrb_seq_timeout
, wpa_auth
, ELOOP_ALL_CTX
);
1276 if (wpa_auth
->conf
.r0kh_list
)
1277 r0kh
= *wpa_auth
->conf
.r0kh_list
;
1280 for (; r0kh
; r0kh
= r0kh
->next
) {
1283 wpa_ft_rrb_seq_flush(wpa_auth
, r0kh
->seq
, 0);
1288 if (wpa_auth
->conf
.r1kh_list
)
1289 r1kh
= *wpa_auth
->conf
.r1kh_list
;
1292 for (; r1kh
; r1kh
= r1kh
->next
) {
1295 wpa_ft_rrb_seq_flush(wpa_auth
, r1kh
->seq
, 0);
1302 static void wpa_ft_deinit_rkh_tmp(struct wpa_authenticator
*wpa_auth
)
1304 struct ft_remote_r0kh
*r0kh
, *r0kh_next
, *r0kh_prev
= NULL
;
1305 struct ft_remote_r1kh
*r1kh
, *r1kh_next
, *r1kh_prev
= NULL
;
1307 if (wpa_auth
->conf
.r0kh_list
)
1308 r0kh
= *wpa_auth
->conf
.r0kh_list
;
1312 r0kh_next
= r0kh
->next
;
1313 if (eloop_cancel_timeout(wpa_ft_rrb_del_r0kh
, wpa_auth
,
1316 r0kh_prev
->next
= r0kh_next
;
1318 *wpa_auth
->conf
.r0kh_list
= r0kh_next
;
1326 if (wpa_auth
->conf
.r1kh_list
)
1327 r1kh
= *wpa_auth
->conf
.r1kh_list
;
1331 r1kh_next
= r1kh
->next
;
1332 if (eloop_cancel_timeout(wpa_ft_rrb_del_r1kh
, wpa_auth
,
1335 r1kh_prev
->next
= r1kh_next
;
1337 *wpa_auth
->conf
.r1kh_list
= r1kh_next
;
1347 void wpa_ft_deinit(struct wpa_authenticator
*wpa_auth
)
1349 wpa_ft_deinit_seq(wpa_auth
);
1350 wpa_ft_deinit_rkh_tmp(wpa_auth
);
1354 static void wpa_ft_block_r0kh(struct wpa_authenticator
*wpa_auth
,
1355 const u8
*f_r0kh_id
, size_t f_r0kh_id_len
)
1357 struct ft_remote_r0kh
*r0kh
, *r0kh_wildcard
;
1359 if (!wpa_auth
->conf
.rkh_neg_timeout
)
1362 wpa_ft_rrb_lookup_r0kh(wpa_auth
, f_r0kh_id
, f_r0kh_id_len
,
1363 &r0kh
, &r0kh_wildcard
);
1365 if (!r0kh_wildcard
) {
1366 /* r0kh removed after neg_timeout and might need re-adding */
1370 wpa_hexdump(MSG_DEBUG
, "FT: Blacklist R0KH-ID",
1371 f_r0kh_id
, f_r0kh_id_len
);
1374 wpa_ft_rrb_r0kh_timeout(wpa_auth
, r0kh
,
1375 wpa_auth
->conf
.rkh_neg_timeout
);
1376 os_memset(r0kh
->addr
, 0, ETH_ALEN
);
1378 wpa_ft_rrb_add_r0kh(wpa_auth
, r0kh_wildcard
, NULL
, f_r0kh_id
,
1380 wpa_auth
->conf
.rkh_neg_timeout
);
1384 static void wpa_ft_expire_pull(void *eloop_ctx
, void *timeout_ctx
)
1386 struct wpa_state_machine
*sm
= eloop_ctx
;
1388 wpa_printf(MSG_DEBUG
, "FT: Timeout pending pull request for " MACSTR
,
1390 if (sm
->ft_pending_pull_left_retries
<= 0)
1391 wpa_ft_block_r0kh(sm
->wpa_auth
, sm
->r0kh_id
, sm
->r0kh_id_len
);
1393 /* cancel multiple timeouts */
1394 eloop_cancel_timeout(wpa_ft_expire_pull
, sm
, NULL
);
1399 static int wpa_ft_pull_pmk_r1(struct wpa_state_machine
*sm
,
1400 const u8
*ies
, size_t ies_len
,
1401 const u8
*pmk_r0_name
)
1403 struct ft_remote_r0kh
*r0kh
, *r0kh_wildcard
;
1405 const u8
*key
, *f_r1kh_id
= sm
->wpa_auth
->conf
.r1_key_holder
;
1406 size_t packet_len
, key_len
;
1407 struct ft_rrb_seq f_seq
;
1408 int tsecs
, tusecs
, first
;
1409 struct wpabuf
*ft_pending_req_ies
;
1411 struct tlv_list req_enc
[] = {
1412 { .type
= FT_RRB_PMK_R0_NAME
, .len
= WPA_PMK_NAME_LEN
,
1413 .data
= pmk_r0_name
},
1414 { .type
= FT_RRB_S1KH_ID
, .len
= ETH_ALEN
,
1416 { .type
= FT_RRB_LAST_EMPTY
, .len
= 0, .data
= NULL
},
1418 struct tlv_list req_auth
[] = {
1419 { .type
= FT_RRB_NONCE
, .len
= FT_RRB_NONCE_LEN
,
1420 .data
= sm
->ft_pending_pull_nonce
},
1421 { .type
= FT_RRB_SEQ
, .len
= sizeof(f_seq
),
1422 .data
= (u8
*) &f_seq
},
1423 { .type
= FT_RRB_R0KH_ID
, .len
= sm
->r0kh_id_len
,
1424 .data
= sm
->r0kh_id
},
1425 { .type
= FT_RRB_R1KH_ID
, .len
= FT_R1KH_ID_LEN
,
1426 .data
= f_r1kh_id
},
1427 { .type
= FT_RRB_LAST_EMPTY
, .len
= 0, .data
= NULL
},
1430 if (sm
->ft_pending_pull_left_retries
<= 0)
1432 first
= sm
->ft_pending_pull_left_retries
==
1433 sm
->wpa_auth
->conf
.rkh_pull_retries
;
1434 sm
->ft_pending_pull_left_retries
--;
1436 wpa_ft_rrb_lookup_r0kh(sm
->wpa_auth
, sm
->r0kh_id
, sm
->r0kh_id_len
,
1437 &r0kh
, &r0kh_wildcard
);
1439 /* Keep r0kh sufficiently long in the list for seq num check */
1440 r0kh_timeout
= sm
->wpa_auth
->conf
.rkh_pull_timeout
/ 1000 +
1441 1 + ftRRBseqTimeout
;
1443 wpa_ft_rrb_r0kh_replenish(sm
->wpa_auth
, r0kh
, r0kh_timeout
);
1444 } else if (r0kh_wildcard
) {
1445 wpa_printf(MSG_DEBUG
, "FT: Using wildcard R0KH-ID");
1446 /* r0kh->addr: updated by SEQ_RESP and wpa_ft_expire_pull */
1447 r0kh
= wpa_ft_rrb_add_r0kh(sm
->wpa_auth
, r0kh_wildcard
,
1448 r0kh_wildcard
->addr
,
1449 sm
->r0kh_id
, sm
->r0kh_id_len
,
1453 wpa_hexdump(MSG_DEBUG
, "FT: Did not find R0KH-ID",
1454 sm
->r0kh_id
, sm
->r0kh_id_len
);
1457 if (is_zero_ether_addr(r0kh
->addr
)) {
1458 wpa_hexdump(MSG_DEBUG
, "FT: R0KH-ID is blacklisted",
1459 sm
->r0kh_id
, sm
->r0kh_id_len
);
1462 if (os_memcmp(r0kh
->addr
, sm
->wpa_auth
->addr
, ETH_ALEN
) == 0) {
1463 wpa_printf(MSG_DEBUG
,
1464 "FT: R0KH-ID points to self - no matching key available");
1469 key_len
= sizeof(r0kh
->key
);
1471 wpa_printf(MSG_DEBUG
, "FT: Send PMK-R1 pull request to remote R0KH "
1472 "address " MACSTR
, MAC2STR(r0kh
->addr
));
1474 if (r0kh
->seq
->rx
.num_last
== 0) {
1475 /* A sequence request will be sent out anyway when pull
1476 * response is received. Send it out now to avoid one RTT. */
1477 wpa_ft_rrb_seq_req(sm
->wpa_auth
, r0kh
->seq
, r0kh
->addr
,
1478 r0kh
->id
, r0kh
->id_len
, f_r1kh_id
, key
,
1479 key_len
, NULL
, 0, NULL
, 0, NULL
);
1483 random_get_bytes(sm
->ft_pending_pull_nonce
, FT_RRB_NONCE_LEN
) < 0) {
1484 wpa_printf(MSG_DEBUG
, "FT: Failed to get random data for "
1489 if (wpa_ft_new_seq(r0kh
->seq
, &f_seq
) < 0) {
1490 wpa_printf(MSG_DEBUG
, "FT: Failed to get seq num");
1494 if (wpa_ft_rrb_build(key
, key_len
, req_enc
, NULL
, req_auth
,
1495 sm
->wpa_auth
->addr
, FT_PACKET_R0KH_R1KH_PULL
,
1496 &packet
, &packet_len
) < 0)
1499 ft_pending_req_ies
= wpabuf_alloc_copy(ies
, ies_len
);
1500 wpabuf_free(sm
->ft_pending_req_ies
);
1501 sm
->ft_pending_req_ies
= ft_pending_req_ies
;
1502 if (!sm
->ft_pending_req_ies
) {
1507 tsecs
= sm
->wpa_auth
->conf
.rkh_pull_timeout
/ 1000;
1508 tusecs
= (sm
->wpa_auth
->conf
.rkh_pull_timeout
% 1000) * 1000;
1509 eloop_register_timeout(tsecs
, tusecs
, wpa_ft_expire_pull
, sm
, NULL
);
1511 wpa_ft_rrb_oui_send(sm
->wpa_auth
, r0kh
->addr
, FT_PACKET_R0KH_R1KH_PULL
,
1512 packet
, packet_len
);
1520 int wpa_auth_derive_ptk_ft(struct wpa_state_machine
*sm
, const u8
*pmk
,
1521 struct wpa_ptk
*ptk
)
1523 u8 pmk_r0
[PMK_LEN
], pmk_r0_name
[WPA_PMK_NAME_LEN
];
1525 u8 ptk_name
[WPA_PMK_NAME_LEN
];
1526 const u8
*mdid
= sm
->wpa_auth
->conf
.mobility_domain
;
1527 const u8
*r0kh
= sm
->wpa_auth
->conf
.r0_key_holder
;
1528 size_t r0kh_len
= sm
->wpa_auth
->conf
.r0_key_holder_len
;
1529 const u8
*r1kh
= sm
->wpa_auth
->conf
.r1_key_holder
;
1530 const u8
*ssid
= sm
->wpa_auth
->conf
.ssid
;
1531 size_t ssid_len
= sm
->wpa_auth
->conf
.ssid_len
;
1532 int psk_local
= sm
->wpa_auth
->conf
.ft_psk_generate_local
;
1534 if (sm
->xxkey_len
== 0) {
1535 wpa_printf(MSG_DEBUG
, "FT: XXKey not available for key "
1540 if (wpa_derive_pmk_r0(sm
->xxkey
, sm
->xxkey_len
, ssid
, ssid_len
, mdid
,
1541 r0kh
, r0kh_len
, sm
->addr
,
1542 pmk_r0
, pmk_r0_name
) < 0)
1544 wpa_hexdump_key(MSG_DEBUG
, "FT: PMK-R0", pmk_r0
, PMK_LEN
);
1545 wpa_hexdump(MSG_DEBUG
, "FT: PMKR0Name", pmk_r0_name
, WPA_PMK_NAME_LEN
);
1546 if (!psk_local
|| !wpa_key_mgmt_ft_psk(sm
->wpa_key_mgmt
))
1547 wpa_ft_store_pmk_r0(sm
->wpa_auth
, sm
->addr
, pmk_r0
, pmk_r0_name
,
1550 if (wpa_derive_pmk_r1(pmk_r0
, pmk_r0_name
, r1kh
, sm
->addr
,
1551 pmk_r1
, sm
->pmk_r1_name
) < 0)
1553 wpa_hexdump_key(MSG_DEBUG
, "FT: PMK-R1", pmk_r1
, PMK_LEN
);
1554 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name", sm
->pmk_r1_name
,
1556 if (!psk_local
|| !wpa_key_mgmt_ft_psk(sm
->wpa_key_mgmt
))
1557 wpa_ft_store_pmk_r1(sm
->wpa_auth
, sm
->addr
, pmk_r1
,
1558 sm
->pmk_r1_name
, sm
->pairwise
);
1560 return wpa_pmk_r1_to_ptk(pmk_r1
, sm
->SNonce
, sm
->ANonce
, sm
->addr
,
1561 sm
->wpa_auth
->addr
, sm
->pmk_r1_name
,
1562 ptk
, ptk_name
, sm
->wpa_key_mgmt
, sm
->pairwise
);
1566 static inline int wpa_auth_get_seqnum(struct wpa_authenticator
*wpa_auth
,
1567 const u8
*addr
, int idx
, u8
*seq
)
1569 if (wpa_auth
->cb
->get_seqnum
== NULL
)
1571 return wpa_auth
->cb
->get_seqnum(wpa_auth
->cb_ctx
, addr
, idx
, seq
);
1575 static u8
* wpa_ft_gtk_subelem(struct wpa_state_machine
*sm
, size_t *len
)
1578 struct wpa_group
*gsm
= sm
->group
;
1579 size_t subelem_len
, pad_len
;
1584 key_len
= gsm
->GTK_len
;
1585 if (key_len
> sizeof(keybuf
))
1589 * Pad key for AES Key Wrap if it is not multiple of 8 bytes or is less
1592 pad_len
= key_len
% 8;
1594 pad_len
= 8 - pad_len
;
1595 if (key_len
+ pad_len
< 16)
1597 if (pad_len
&& key_len
< sizeof(keybuf
)) {
1598 os_memcpy(keybuf
, gsm
->GTK
[gsm
->GN
- 1], key_len
);
1599 os_memset(keybuf
+ key_len
, 0, pad_len
);
1600 keybuf
[key_len
] = 0xdd;
1604 key
= gsm
->GTK
[gsm
->GN
- 1];
1607 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
1610 subelem_len
= 13 + key_len
+ 8;
1611 subelem
= os_zalloc(subelem_len
);
1612 if (subelem
== NULL
)
1615 subelem
[0] = FTIE_SUBELEM_GTK
;
1616 subelem
[1] = 11 + key_len
+ 8;
1617 /* Key ID in B0-B1 of Key Info */
1618 WPA_PUT_LE16(&subelem
[2], gsm
->GN
& 0x03);
1619 subelem
[4] = gsm
->GTK_len
;
1620 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, subelem
+ 5);
1621 if (aes_wrap(sm
->PTK
.kek
, sm
->PTK
.kek_len
, key_len
/ 8, key
,
1632 #ifdef CONFIG_IEEE80211W
1633 static u8
* wpa_ft_igtk_subelem(struct wpa_state_machine
*sm
, size_t *len
)
1636 struct wpa_group
*gsm
= sm
->group
;
1639 /* Sub-elem ID[1] | Length[1] | KeyID[2] | IPN[6] | Key Length[1] |
1641 subelem_len
= 1 + 1 + 2 + 6 + 1 + WPA_IGTK_LEN
+ 8;
1642 subelem
= os_zalloc(subelem_len
);
1643 if (subelem
== NULL
)
1647 *pos
++ = FTIE_SUBELEM_IGTK
;
1648 *pos
++ = subelem_len
- 2;
1649 WPA_PUT_LE16(pos
, gsm
->GN_igtk
);
1651 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, pos
);
1653 *pos
++ = WPA_IGTK_LEN
;
1654 if (aes_wrap(sm
->PTK
.kek
, sm
->PTK
.kek_len
, WPA_IGTK_LEN
/ 8,
1655 gsm
->IGTK
[gsm
->GN_igtk
- 4], pos
)) {
1663 #endif /* CONFIG_IEEE80211W */
1666 static u8
* wpa_ft_process_rdie(struct wpa_state_machine
*sm
,
1667 u8
*pos
, u8
*end
, u8 id
, u8 descr_count
,
1668 const u8
*ies
, size_t ies_len
)
1670 struct ieee802_11_elems parse
;
1671 struct rsn_rdie
*rdie
;
1673 wpa_printf(MSG_DEBUG
, "FT: Resource Request: id=%d descr_count=%d",
1675 wpa_hexdump(MSG_MSGDUMP
, "FT: Resource descriptor IE(s)",
1678 if (end
- pos
< (int) sizeof(*rdie
)) {
1679 wpa_printf(MSG_ERROR
, "FT: Not enough room for response RDIE");
1683 *pos
++ = WLAN_EID_RIC_DATA
;
1684 *pos
++ = sizeof(*rdie
);
1685 rdie
= (struct rsn_rdie
*) pos
;
1687 rdie
->descr_count
= 0;
1688 rdie
->status_code
= host_to_le16(WLAN_STATUS_SUCCESS
);
1689 pos
+= sizeof(*rdie
);
1691 if (ieee802_11_parse_elems((u8
*) ies
, ies_len
, &parse
, 1) ==
1693 wpa_printf(MSG_DEBUG
, "FT: Failed to parse request IEs");
1695 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE
);
1699 if (parse
.wmm_tspec
) {
1700 struct wmm_tspec_element
*tspec
;
1702 if (parse
.wmm_tspec_len
+ 2 < (int) sizeof(*tspec
)) {
1703 wpa_printf(MSG_DEBUG
, "FT: Too short WMM TSPEC IE "
1704 "(%d)", (int) parse
.wmm_tspec_len
);
1706 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE
);
1709 if (end
- pos
< (int) sizeof(*tspec
)) {
1710 wpa_printf(MSG_ERROR
, "FT: Not enough room for "
1713 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE
);
1716 tspec
= (struct wmm_tspec_element
*) pos
;
1717 os_memcpy(tspec
, parse
.wmm_tspec
- 2, sizeof(*tspec
));
1721 if (parse
.wmm_tspec
&& sm
->wpa_auth
->conf
.ap_mlme
) {
1724 res
= wmm_process_tspec((struct wmm_tspec_element
*) pos
);
1725 wpa_printf(MSG_DEBUG
, "FT: ADDTS processing result: %d", res
);
1726 if (res
== WMM_ADDTS_STATUS_INVALID_PARAMETERS
)
1728 host_to_le16(WLAN_STATUS_INVALID_PARAMETERS
);
1729 else if (res
== WMM_ADDTS_STATUS_REFUSED
)
1731 host_to_le16(WLAN_STATUS_REQUEST_DECLINED
);
1733 /* TSPEC accepted; include updated TSPEC in response */
1734 rdie
->descr_count
= 1;
1735 pos
+= sizeof(struct wmm_tspec_element
);
1739 #endif /* NEED_AP_MLME */
1741 if (parse
.wmm_tspec
&& !sm
->wpa_auth
->conf
.ap_mlme
) {
1744 res
= wpa_ft_add_tspec(sm
->wpa_auth
, sm
->addr
, pos
,
1745 sizeof(struct wmm_tspec_element
));
1748 rdie
->status_code
= host_to_le16(res
);
1750 /* TSPEC accepted; include updated TSPEC in
1752 rdie
->descr_count
= 1;
1753 pos
+= sizeof(struct wmm_tspec_element
);
1759 wpa_printf(MSG_DEBUG
, "FT: No supported resource requested");
1760 rdie
->status_code
= host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE
);
1765 static u8
* wpa_ft_process_ric(struct wpa_state_machine
*sm
, u8
*pos
, u8
*end
,
1766 const u8
*ric
, size_t ric_len
)
1768 const u8
*rpos
, *start
;
1769 const struct rsn_rdie
*rdie
;
1771 wpa_hexdump(MSG_MSGDUMP
, "FT: RIC Request", ric
, ric_len
);
1774 while (rpos
+ sizeof(*rdie
) < ric
+ ric_len
) {
1775 if (rpos
[0] != WLAN_EID_RIC_DATA
|| rpos
[1] < sizeof(*rdie
) ||
1776 rpos
+ 2 + rpos
[1] > ric
+ ric_len
)
1778 rdie
= (const struct rsn_rdie
*) (rpos
+ 2);
1779 rpos
+= 2 + rpos
[1];
1782 while (rpos
+ 2 <= ric
+ ric_len
&&
1783 rpos
+ 2 + rpos
[1] <= ric
+ ric_len
) {
1784 if (rpos
[0] == WLAN_EID_RIC_DATA
)
1786 rpos
+= 2 + rpos
[1];
1788 pos
= wpa_ft_process_rdie(sm
, pos
, end
, rdie
->id
,
1790 start
, rpos
- start
);
1797 u8
* wpa_sm_write_assoc_resp_ies(struct wpa_state_machine
*sm
, u8
*pos
,
1798 size_t max_len
, int auth_alg
,
1799 const u8
*req_ies
, size_t req_ies_len
)
1801 u8
*end
, *mdie
, *ftie
, *rsnie
= NULL
, *r0kh_id
, *subelem
= NULL
;
1802 size_t mdie_len
, ftie_len
, rsnie_len
= 0, r0kh_id_len
, subelem_len
= 0;
1804 struct wpa_auth_config
*conf
;
1805 struct rsn_ftie
*_ftie
;
1806 struct wpa_ft_ies parse
;
1808 u8
*anonce
, *snonce
;
1813 conf
= &sm
->wpa_auth
->conf
;
1815 if (!wpa_key_mgmt_ft(sm
->wpa_key_mgmt
))
1818 end
= pos
+ max_len
;
1820 if (auth_alg
== WLAN_AUTH_FT
) {
1822 * RSN (only present if this is a Reassociation Response and
1823 * part of a fast BSS transition)
1825 res
= wpa_write_rsn_ie(conf
, pos
, end
- pos
, sm
->pmk_r1_name
);
1833 /* Mobility Domain Information */
1834 res
= wpa_write_mdie(conf
, pos
, end
- pos
);
1841 /* Fast BSS Transition Information */
1842 if (auth_alg
== WLAN_AUTH_FT
) {
1843 subelem
= wpa_ft_gtk_subelem(sm
, &subelem_len
);
1844 r0kh_id
= sm
->r0kh_id
;
1845 r0kh_id_len
= sm
->r0kh_id_len
;
1846 anonce
= sm
->ANonce
;
1847 snonce
= sm
->SNonce
;
1848 #ifdef CONFIG_IEEE80211W
1849 if (sm
->mgmt_frame_prot
) {
1853 igtk
= wpa_ft_igtk_subelem(sm
, &igtk_len
);
1858 nbuf
= os_realloc(subelem
, subelem_len
+ igtk_len
);
1865 os_memcpy(subelem
+ subelem_len
, igtk
, igtk_len
);
1866 subelem_len
+= igtk_len
;
1869 #endif /* CONFIG_IEEE80211W */
1871 r0kh_id
= conf
->r0_key_holder
;
1872 r0kh_id_len
= conf
->r0_key_holder_len
;
1876 res
= wpa_write_ftie(conf
, r0kh_id
, r0kh_id_len
, anonce
, snonce
, pos
,
1877 end
- pos
, subelem
, subelem_len
);
1885 _ftie
= (struct rsn_ftie
*) (ftie
+ 2);
1886 if (auth_alg
== WLAN_AUTH_FT
)
1887 _ftie
->mic_control
[1] = 3; /* Information element count */
1890 if (wpa_ft_parse_ies(req_ies
, req_ies_len
, &parse
) == 0 && parse
.ric
) {
1891 pos
= wpa_ft_process_ric(sm
, pos
, end
, parse
.ric
,
1893 if (auth_alg
== WLAN_AUTH_FT
)
1894 _ftie
->mic_control
[1] +=
1895 ieee802_11_ie_count(ric_start
,
1898 if (ric_start
== pos
)
1901 if (auth_alg
== WLAN_AUTH_FT
&&
1902 wpa_ft_mic(sm
->PTK
.kck
, sm
->PTK
.kck_len
, sm
->addr
,
1903 sm
->wpa_auth
->addr
, 6,
1904 mdie
, mdie_len
, ftie
, ftie_len
,
1906 ric_start
, ric_start
? pos
- ric_start
: 0,
1908 wpa_printf(MSG_DEBUG
, "FT: Failed to calculate MIC");
1910 os_free(sm
->assoc_resp_ftie
);
1911 sm
->assoc_resp_ftie
= os_malloc(ftie_len
);
1912 if (sm
->assoc_resp_ftie
)
1913 os_memcpy(sm
->assoc_resp_ftie
, ftie
, ftie_len
);
1919 static inline int wpa_auth_set_key(struct wpa_authenticator
*wpa_auth
,
1921 enum wpa_alg alg
, const u8
*addr
, int idx
,
1922 u8
*key
, size_t key_len
)
1924 if (wpa_auth
->cb
->set_key
== NULL
)
1926 return wpa_auth
->cb
->set_key(wpa_auth
->cb_ctx
, vlan_id
, alg
, addr
, idx
,
1931 void wpa_ft_install_ptk(struct wpa_state_machine
*sm
)
1936 /* MLME-SETKEYS.request(PTK) */
1937 alg
= wpa_cipher_to_alg(sm
->pairwise
);
1938 klen
= wpa_cipher_key_len(sm
->pairwise
);
1939 if (!wpa_cipher_valid_pairwise(sm
->pairwise
)) {
1940 wpa_printf(MSG_DEBUG
, "FT: Unknown pairwise alg 0x%x - skip "
1941 "PTK configuration", sm
->pairwise
);
1945 if (sm
->tk_already_set
) {
1946 /* Must avoid TK reconfiguration to prevent clearing of TX/RX
1947 * PN in the driver */
1948 wpa_printf(MSG_DEBUG
,
1949 "FT: Do not re-install same PTK to the driver");
1953 /* FIX: add STA entry to kernel/driver here? The set_key will fail
1954 * most likely without this.. At the moment, STA entry is added only
1955 * after association has been completed. This function will be called
1956 * again after association to get the PTK configured, but that could be
1957 * optimized by adding the STA entry earlier.
1959 if (wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
, 0,
1963 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
1964 sm
->pairwise_set
= TRUE
;
1965 sm
->tk_already_set
= TRUE
;
1969 /* Derive PMK-R1 from PSK, check all available PSK */
1970 static int wpa_ft_psk_pmk_r1(struct wpa_state_machine
*sm
,
1971 const u8
*req_pmk_r1_name
,
1972 u8
*out_pmk_r1
, int *out_pairwise
)
1974 const u8
*pmk
= NULL
;
1975 u8 pmk_r0
[PMK_LEN
], pmk_r0_name
[WPA_PMK_NAME_LEN
];
1976 u8 pmk_r1
[PMK_LEN
], pmk_r1_name
[WPA_PMK_NAME_LEN
];
1977 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
1978 const u8
*mdid
= wpa_auth
->conf
.mobility_domain
;
1979 const u8
*r0kh
= sm
->r0kh_id
;
1980 size_t r0kh_len
= sm
->r0kh_id_len
;
1981 const u8
*r1kh
= wpa_auth
->conf
.r1_key_holder
;
1982 const u8
*ssid
= wpa_auth
->conf
.ssid
;
1983 size_t ssid_len
= wpa_auth
->conf
.ssid_len
;
1986 pairwise
= sm
->pairwise
;
1989 pmk
= wpa_ft_get_psk(wpa_auth
, sm
->addr
, sm
->p2p_dev_addr
,
1994 if (wpa_derive_pmk_r0(pmk
, PMK_LEN
, ssid
, ssid_len
, mdid
, r0kh
,
1996 pmk_r0
, pmk_r0_name
) < 0 ||
1997 wpa_derive_pmk_r1(pmk_r0
, pmk_r0_name
, r1kh
, sm
->addr
,
1998 pmk_r1
, pmk_r1_name
) < 0 ||
1999 os_memcmp_const(pmk_r1_name
, req_pmk_r1_name
,
2000 WPA_PMK_NAME_LEN
) != 0)
2003 /* We found a PSK that matches the requested pmk_r1_name */
2004 wpa_printf(MSG_DEBUG
,
2005 "FT: Found PSK to generate PMK-R1 locally");
2006 os_memcpy(out_pmk_r1
, pmk_r1
, PMK_LEN
);
2008 *out_pairwise
= pairwise
;
2012 wpa_printf(MSG_DEBUG
,
2013 "FT: Did not find PSK to generate PMK-R1 locally");
2018 /* Detect the configuration the station asked for.
2019 * Required to detect FT-PSK and pairwise cipher.
2021 static int wpa_ft_set_key_mgmt(struct wpa_state_machine
*sm
,
2022 struct wpa_ft_ies
*parse
)
2024 int key_mgmt
, ciphers
;
2026 if (sm
->wpa_key_mgmt
)
2029 key_mgmt
= parse
->key_mgmt
& sm
->wpa_auth
->conf
.wpa_key_mgmt
;
2031 wpa_printf(MSG_DEBUG
, "FT: Invalid key mgmt (0x%x) from "
2032 MACSTR
, parse
->key_mgmt
, MAC2STR(sm
->addr
));
2035 if (key_mgmt
& WPA_KEY_MGMT_FT_IEEE8021X
)
2036 sm
->wpa_key_mgmt
= WPA_KEY_MGMT_FT_IEEE8021X
;
2037 else if (key_mgmt
& WPA_KEY_MGMT_FT_PSK
)
2038 sm
->wpa_key_mgmt
= WPA_KEY_MGMT_FT_PSK
;
2040 else if (key_mgmt
& WPA_KEY_MGMT_FT_FILS_SHA256
)
2041 sm
->wpa_key_mgmt
= WPA_KEY_MGMT_FT_FILS_SHA256
;
2042 else if (key_mgmt
& WPA_KEY_MGMT_FT_FILS_SHA384
)
2043 sm
->wpa_key_mgmt
= WPA_KEY_MGMT_FT_FILS_SHA384
;
2044 #endif /* CONFIG_FILS */
2045 ciphers
= parse
->pairwise_cipher
& sm
->wpa_auth
->conf
.rsn_pairwise
;
2047 wpa_printf(MSG_DEBUG
, "FT: Invalid pairwise cipher (0x%x) from "
2049 parse
->pairwise_cipher
, MAC2STR(sm
->addr
));
2052 sm
->pairwise
= wpa_pick_pairwise_cipher(ciphers
, 0);
2058 static int wpa_ft_process_auth_req(struct wpa_state_machine
*sm
,
2059 const u8
*ies
, size_t ies_len
,
2060 u8
**resp_ies
, size_t *resp_ies_len
)
2062 struct rsn_mdie
*mdie
;
2063 struct rsn_ftie
*ftie
;
2064 u8 pmk_r1
[PMK_LEN
], pmk_r1_name
[WPA_PMK_NAME_LEN
];
2065 u8 ptk_name
[WPA_PMK_NAME_LEN
];
2066 struct wpa_auth_config
*conf
;
2067 struct wpa_ft_ies parse
;
2076 sm
->pmk_r1_name_valid
= 0;
2077 conf
= &sm
->wpa_auth
->conf
;
2079 wpa_hexdump(MSG_DEBUG
, "FT: Received authentication frame IEs",
2082 if (wpa_ft_parse_ies(ies
, ies_len
, &parse
) < 0) {
2083 wpa_printf(MSG_DEBUG
, "FT: Failed to parse FT IEs");
2084 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
2087 mdie
= (struct rsn_mdie
*) parse
.mdie
;
2088 if (mdie
== NULL
|| parse
.mdie_len
< sizeof(*mdie
) ||
2089 os_memcmp(mdie
->mobility_domain
,
2090 sm
->wpa_auth
->conf
.mobility_domain
,
2091 MOBILITY_DOMAIN_ID_LEN
) != 0) {
2092 wpa_printf(MSG_DEBUG
, "FT: Invalid MDIE");
2093 return WLAN_STATUS_INVALID_MDIE
;
2096 ftie
= (struct rsn_ftie
*) parse
.ftie
;
2097 if (ftie
== NULL
|| parse
.ftie_len
< sizeof(*ftie
)) {
2098 wpa_printf(MSG_DEBUG
, "FT: Invalid FTIE");
2099 return WLAN_STATUS_INVALID_FTIE
;
2102 os_memcpy(sm
->SNonce
, ftie
->snonce
, WPA_NONCE_LEN
);
2104 if (parse
.r0kh_id
== NULL
) {
2105 wpa_printf(MSG_DEBUG
, "FT: Invalid FTIE - no R0KH-ID");
2106 return WLAN_STATUS_INVALID_FTIE
;
2109 wpa_hexdump(MSG_DEBUG
, "FT: STA R0KH-ID",
2110 parse
.r0kh_id
, parse
.r0kh_id_len
);
2111 os_memcpy(sm
->r0kh_id
, parse
.r0kh_id
, parse
.r0kh_id_len
);
2112 sm
->r0kh_id_len
= parse
.r0kh_id_len
;
2114 if (parse
.rsn_pmkid
== NULL
) {
2115 wpa_printf(MSG_DEBUG
, "FT: No PMKID in RSNIE");
2116 return WLAN_STATUS_INVALID_PMKID
;
2119 if (wpa_ft_set_key_mgmt(sm
, &parse
) < 0)
2120 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
2122 wpa_hexdump(MSG_DEBUG
, "FT: Requested PMKR0Name",
2123 parse
.rsn_pmkid
, WPA_PMK_NAME_LEN
);
2124 if (wpa_derive_pmk_r1_name(parse
.rsn_pmkid
,
2125 sm
->wpa_auth
->conf
.r1_key_holder
, sm
->addr
,
2127 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
2128 wpa_hexdump(MSG_DEBUG
, "FT: Derived requested PMKR1Name",
2129 pmk_r1_name
, WPA_PMK_NAME_LEN
);
2131 if (conf
->ft_psk_generate_local
&&
2132 wpa_key_mgmt_ft_psk(sm
->wpa_key_mgmt
)) {
2133 if (wpa_ft_psk_pmk_r1(sm
, pmk_r1_name
, pmk_r1
, &pairwise
) < 0)
2134 return WLAN_STATUS_INVALID_PMKID
;
2135 } else if (wpa_ft_fetch_pmk_r1(sm
->wpa_auth
, sm
->addr
, pmk_r1_name
,
2136 pmk_r1
, &pairwise
) < 0) {
2137 if (wpa_ft_pull_pmk_r1(sm
, ies
, ies_len
, parse
.rsn_pmkid
) < 0) {
2138 wpa_printf(MSG_DEBUG
,
2139 "FT: Did not have matching PMK-R1 and either unknown or blocked R0KH-ID or NAK from R0KH");
2140 return WLAN_STATUS_INVALID_PMKID
;
2143 return -1; /* Status pending */
2146 wpa_hexdump_key(MSG_DEBUG
, "FT: Selected PMK-R1", pmk_r1
, PMK_LEN
);
2147 sm
->pmk_r1_name_valid
= 1;
2148 os_memcpy(sm
->pmk_r1_name
, pmk_r1_name
, WPA_PMK_NAME_LEN
);
2150 if (random_get_bytes(sm
->ANonce
, WPA_NONCE_LEN
)) {
2151 wpa_printf(MSG_DEBUG
, "FT: Failed to get random data for "
2153 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
2156 wpa_hexdump(MSG_DEBUG
, "FT: Received SNonce",
2157 sm
->SNonce
, WPA_NONCE_LEN
);
2158 wpa_hexdump(MSG_DEBUG
, "FT: Generated ANonce",
2159 sm
->ANonce
, WPA_NONCE_LEN
);
2161 if (wpa_pmk_r1_to_ptk(pmk_r1
, sm
->SNonce
, sm
->ANonce
, sm
->addr
,
2162 sm
->wpa_auth
->addr
, pmk_r1_name
,
2163 &sm
->PTK
, ptk_name
, sm
->wpa_key_mgmt
,
2165 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
2167 sm
->pairwise
= pairwise
;
2168 sm
->PTK_valid
= TRUE
;
2169 sm
->tk_already_set
= FALSE
;
2170 wpa_ft_install_ptk(sm
);
2172 buflen
= 2 + sizeof(struct rsn_mdie
) + 2 + sizeof(struct rsn_ftie
) +
2173 2 + FT_R1KH_ID_LEN
+ 200;
2174 *resp_ies
= os_zalloc(buflen
);
2175 if (*resp_ies
== NULL
)
2179 end
= *resp_ies
+ buflen
;
2181 ret
= wpa_write_rsn_ie(conf
, pos
, end
- pos
, parse
.rsn_pmkid
);
2186 ret
= wpa_write_mdie(conf
, pos
, end
- pos
);
2191 ret
= wpa_write_ftie(conf
, parse
.r0kh_id
, parse
.r0kh_id_len
,
2192 sm
->ANonce
, sm
->SNonce
, pos
, end
- pos
, NULL
, 0);
2197 *resp_ies_len
= pos
- *resp_ies
;
2199 return WLAN_STATUS_SUCCESS
;
2203 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
2207 void wpa_ft_process_auth(struct wpa_state_machine
*sm
, const u8
*bssid
,
2208 u16 auth_transaction
, const u8
*ies
, size_t ies_len
,
2209 void (*cb
)(void *ctx
, const u8
*dst
, const u8
*bssid
,
2210 u16 auth_transaction
, u16 status
,
2211 const u8
*ies
, size_t ies_len
),
2216 size_t resp_ies_len
;
2220 wpa_printf(MSG_DEBUG
, "FT: Received authentication frame, but "
2221 "WPA SM not available");
2225 wpa_printf(MSG_DEBUG
, "FT: Received authentication frame: STA=" MACSTR
2226 " BSSID=" MACSTR
" transaction=%d",
2227 MAC2STR(sm
->addr
), MAC2STR(bssid
), auth_transaction
);
2228 sm
->ft_pending_cb
= cb
;
2229 sm
->ft_pending_cb_ctx
= ctx
;
2230 sm
->ft_pending_auth_transaction
= auth_transaction
;
2231 sm
->ft_pending_pull_left_retries
= sm
->wpa_auth
->conf
.rkh_pull_retries
;
2232 res
= wpa_ft_process_auth_req(sm
, ies
, ies_len
, &resp_ies
,
2235 wpa_printf(MSG_DEBUG
, "FT: Callback postponed until response is available");
2240 wpa_printf(MSG_DEBUG
, "FT: FT authentication response: dst=" MACSTR
2241 " auth_transaction=%d status=%d",
2242 MAC2STR(sm
->addr
), auth_transaction
+ 1, status
);
2243 wpa_hexdump(MSG_DEBUG
, "FT: Response IEs", resp_ies
, resp_ies_len
);
2244 cb(ctx
, sm
->addr
, bssid
, auth_transaction
+ 1, status
,
2245 resp_ies
, resp_ies_len
);
2250 u16
wpa_ft_validate_reassoc(struct wpa_state_machine
*sm
, const u8
*ies
,
2253 struct wpa_ft_ies parse
;
2254 struct rsn_mdie
*mdie
;
2255 struct rsn_ftie
*ftie
;
2256 u8 mic
[WPA_EAPOL_KEY_MIC_MAX_LEN
];
2257 size_t mic_len
= 16;
2261 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
2263 wpa_hexdump(MSG_DEBUG
, "FT: Reassoc Req IEs", ies
, ies_len
);
2265 if (wpa_ft_parse_ies(ies
, ies_len
, &parse
) < 0) {
2266 wpa_printf(MSG_DEBUG
, "FT: Failed to parse FT IEs");
2267 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
2270 if (parse
.rsn
== NULL
) {
2271 wpa_printf(MSG_DEBUG
, "FT: No RSNIE in Reassoc Req");
2272 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
2275 if (parse
.rsn_pmkid
== NULL
) {
2276 wpa_printf(MSG_DEBUG
, "FT: No PMKID in RSNIE");
2277 return WLAN_STATUS_INVALID_PMKID
;
2280 if (os_memcmp_const(parse
.rsn_pmkid
, sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
)
2282 wpa_printf(MSG_DEBUG
, "FT: PMKID in Reassoc Req did not match "
2283 "with the PMKR1Name derived from auth request");
2284 return WLAN_STATUS_INVALID_PMKID
;
2287 mdie
= (struct rsn_mdie
*) parse
.mdie
;
2288 if (mdie
== NULL
|| parse
.mdie_len
< sizeof(*mdie
) ||
2289 os_memcmp(mdie
->mobility_domain
,
2290 sm
->wpa_auth
->conf
.mobility_domain
,
2291 MOBILITY_DOMAIN_ID_LEN
) != 0) {
2292 wpa_printf(MSG_DEBUG
, "FT: Invalid MDIE");
2293 return WLAN_STATUS_INVALID_MDIE
;
2296 ftie
= (struct rsn_ftie
*) parse
.ftie
;
2297 if (ftie
== NULL
|| parse
.ftie_len
< sizeof(*ftie
)) {
2298 wpa_printf(MSG_DEBUG
, "FT: Invalid FTIE");
2299 return WLAN_STATUS_INVALID_FTIE
;
2302 if (os_memcmp(ftie
->snonce
, sm
->SNonce
, WPA_NONCE_LEN
) != 0) {
2303 wpa_printf(MSG_DEBUG
, "FT: SNonce mismatch in FTIE");
2304 wpa_hexdump(MSG_DEBUG
, "FT: Received SNonce",
2305 ftie
->snonce
, WPA_NONCE_LEN
);
2306 wpa_hexdump(MSG_DEBUG
, "FT: Expected SNonce",
2307 sm
->SNonce
, WPA_NONCE_LEN
);
2308 return WLAN_STATUS_INVALID_FTIE
;
2311 if (os_memcmp(ftie
->anonce
, sm
->ANonce
, WPA_NONCE_LEN
) != 0) {
2312 wpa_printf(MSG_DEBUG
, "FT: ANonce mismatch in FTIE");
2313 wpa_hexdump(MSG_DEBUG
, "FT: Received ANonce",
2314 ftie
->anonce
, WPA_NONCE_LEN
);
2315 wpa_hexdump(MSG_DEBUG
, "FT: Expected ANonce",
2316 sm
->ANonce
, WPA_NONCE_LEN
);
2317 return WLAN_STATUS_INVALID_FTIE
;
2321 if (parse
.r0kh_id
== NULL
) {
2322 wpa_printf(MSG_DEBUG
, "FT: No R0KH-ID subelem in FTIE");
2323 return WLAN_STATUS_INVALID_FTIE
;
2326 if (parse
.r0kh_id_len
!= sm
->r0kh_id_len
||
2327 os_memcmp_const(parse
.r0kh_id
, sm
->r0kh_id
, parse
.r0kh_id_len
) != 0)
2329 wpa_printf(MSG_DEBUG
, "FT: R0KH-ID in FTIE did not match with "
2330 "the current R0KH-ID");
2331 wpa_hexdump(MSG_DEBUG
, "FT: R0KH-ID in FTIE",
2332 parse
.r0kh_id
, parse
.r0kh_id_len
);
2333 wpa_hexdump(MSG_DEBUG
, "FT: The current R0KH-ID",
2334 sm
->r0kh_id
, sm
->r0kh_id_len
);
2335 return WLAN_STATUS_INVALID_FTIE
;
2338 if (parse
.r1kh_id
== NULL
) {
2339 wpa_printf(MSG_DEBUG
, "FT: No R1KH-ID subelem in FTIE");
2340 return WLAN_STATUS_INVALID_FTIE
;
2343 if (os_memcmp_const(parse
.r1kh_id
, sm
->wpa_auth
->conf
.r1_key_holder
,
2344 FT_R1KH_ID_LEN
) != 0) {
2345 wpa_printf(MSG_DEBUG
, "FT: Unknown R1KH-ID used in "
2347 wpa_hexdump(MSG_DEBUG
, "FT: R1KH-ID in FTIE",
2348 parse
.r1kh_id
, FT_R1KH_ID_LEN
);
2349 wpa_hexdump(MSG_DEBUG
, "FT: Expected R1KH-ID",
2350 sm
->wpa_auth
->conf
.r1_key_holder
, FT_R1KH_ID_LEN
);
2351 return WLAN_STATUS_INVALID_FTIE
;
2354 if (parse
.rsn_pmkid
== NULL
||
2355 os_memcmp_const(parse
.rsn_pmkid
, sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
))
2357 wpa_printf(MSG_DEBUG
, "FT: No matching PMKR1Name (PMKID) in "
2358 "RSNIE (pmkid=%d)", !!parse
.rsn_pmkid
);
2359 return WLAN_STATUS_INVALID_PMKID
;
2364 count
+= ieee802_11_ie_count(parse
.ric
, parse
.ric_len
);
2365 if (ftie
->mic_control
[1] != count
) {
2366 wpa_printf(MSG_DEBUG
, "FT: Unexpected IE count in MIC "
2367 "Control: received %u expected %u",
2368 ftie
->mic_control
[1], count
);
2369 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
2372 if (wpa_ft_mic(sm
->PTK
.kck
, sm
->PTK
.kck_len
, sm
->addr
,
2373 sm
->wpa_auth
->addr
, 5,
2374 parse
.mdie
- 2, parse
.mdie_len
+ 2,
2375 parse
.ftie
- 2, parse
.ftie_len
+ 2,
2376 parse
.rsn
- 2, parse
.rsn_len
+ 2,
2377 parse
.ric
, parse
.ric_len
,
2379 wpa_printf(MSG_DEBUG
, "FT: Failed to calculate MIC");
2380 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
2383 if (os_memcmp_const(mic
, ftie
->mic
, mic_len
) != 0) {
2384 wpa_printf(MSG_DEBUG
, "FT: Invalid MIC in FTIE");
2385 wpa_printf(MSG_DEBUG
, "FT: addr=" MACSTR
" auth_addr=" MACSTR
,
2386 MAC2STR(sm
->addr
), MAC2STR(sm
->wpa_auth
->addr
));
2387 wpa_hexdump(MSG_MSGDUMP
, "FT: Received MIC",
2388 ftie
->mic
, mic_len
);
2389 wpa_hexdump(MSG_MSGDUMP
, "FT: Calculated MIC", mic
, mic_len
);
2390 wpa_hexdump(MSG_MSGDUMP
, "FT: MDIE",
2391 parse
.mdie
- 2, parse
.mdie_len
+ 2);
2392 wpa_hexdump(MSG_MSGDUMP
, "FT: FTIE",
2393 parse
.ftie
- 2, parse
.ftie_len
+ 2);
2394 wpa_hexdump(MSG_MSGDUMP
, "FT: RSN",
2395 parse
.rsn
- 2, parse
.rsn_len
+ 2);
2396 return WLAN_STATUS_INVALID_FTIE
;
2399 return WLAN_STATUS_SUCCESS
;
2403 int wpa_ft_action_rx(struct wpa_state_machine
*sm
, const u8
*data
, size_t len
)
2405 const u8
*sta_addr
, *target_ap
;
2409 struct ft_rrb_frame
*frame
;
2415 * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
2416 * FT Request action frame body[variable]
2420 wpa_printf(MSG_DEBUG
, "FT: Too short FT Action frame "
2421 "(len=%lu)", (unsigned long) len
);
2426 sta_addr
= data
+ 2;
2427 target_ap
= data
+ 8;
2431 wpa_printf(MSG_DEBUG
, "FT: Received FT Action frame (STA=" MACSTR
2432 " Target AP=" MACSTR
" Action=%d)",
2433 MAC2STR(sta_addr
), MAC2STR(target_ap
), action
);
2435 if (os_memcmp(sta_addr
, sm
->addr
, ETH_ALEN
) != 0) {
2436 wpa_printf(MSG_DEBUG
, "FT: Mismatch in FT Action STA address: "
2437 "STA=" MACSTR
" STA-Address=" MACSTR
,
2438 MAC2STR(sm
->addr
), MAC2STR(sta_addr
));
2443 * Do some sanity checking on the target AP address (not own and not
2444 * broadcast. This could be extended to filter based on a list of known
2445 * APs in the MD (if such a list were configured).
2447 if ((target_ap
[0] & 0x01) ||
2448 os_memcmp(target_ap
, sm
->wpa_auth
->addr
, ETH_ALEN
) == 0) {
2449 wpa_printf(MSG_DEBUG
, "FT: Invalid Target AP in FT Action "
2454 wpa_hexdump(MSG_MSGDUMP
, "FT: Action frame body", ies
, ies_len
);
2456 if (!sm
->wpa_auth
->conf
.ft_over_ds
) {
2457 wpa_printf(MSG_DEBUG
, "FT: Over-DS option disabled - reject");
2461 /* RRB - Forward action frame to the target AP */
2462 frame
= os_malloc(sizeof(*frame
) + len
);
2465 frame
->frame_type
= RSN_REMOTE_FRAME_TYPE_FT_RRB
;
2466 frame
->packet_type
= FT_PACKET_REQUEST
;
2467 frame
->action_length
= host_to_le16(len
);
2468 os_memcpy(frame
->ap_address
, sm
->wpa_auth
->addr
, ETH_ALEN
);
2469 os_memcpy(frame
+ 1, data
, len
);
2471 wpa_ft_rrb_send(sm
->wpa_auth
, target_ap
, (u8
*) frame
,
2472 sizeof(*frame
) + len
);
2479 static void wpa_ft_rrb_rx_request_cb(void *ctx
, const u8
*dst
, const u8
*bssid
,
2480 u16 auth_transaction
, u16 resp
,
2481 const u8
*ies
, size_t ies_len
)
2483 struct wpa_state_machine
*sm
= ctx
;
2484 wpa_printf(MSG_DEBUG
, "FT: Over-the-DS RX request cb for " MACSTR
,
2486 wpa_ft_send_rrb_auth_resp(sm
, sm
->ft_pending_current_ap
, sm
->addr
,
2487 WLAN_STATUS_SUCCESS
, ies
, ies_len
);
2491 static int wpa_ft_rrb_rx_request(struct wpa_authenticator
*wpa_auth
,
2492 const u8
*current_ap
, const u8
*sta_addr
,
2493 const u8
*body
, size_t len
)
2495 struct wpa_state_machine
*sm
;
2498 size_t resp_ies_len
;
2501 sm
= wpa_ft_add_sta(wpa_auth
, sta_addr
);
2503 wpa_printf(MSG_DEBUG
, "FT: Failed to add new STA based on "
2508 wpa_hexdump(MSG_MSGDUMP
, "FT: RRB Request Frame body", body
, len
);
2510 sm
->ft_pending_cb
= wpa_ft_rrb_rx_request_cb
;
2511 sm
->ft_pending_cb_ctx
= sm
;
2512 os_memcpy(sm
->ft_pending_current_ap
, current_ap
, ETH_ALEN
);
2513 sm
->ft_pending_pull_left_retries
= sm
->wpa_auth
->conf
.rkh_pull_retries
;
2514 res
= wpa_ft_process_auth_req(sm
, body
, len
, &resp_ies
,
2517 wpa_printf(MSG_DEBUG
, "FT: No immediate response available - wait for pull response");
2522 res
= wpa_ft_send_rrb_auth_resp(sm
, current_ap
, sta_addr
, status
,
2523 resp_ies
, resp_ies_len
);
2529 static int wpa_ft_send_rrb_auth_resp(struct wpa_state_machine
*sm
,
2530 const u8
*current_ap
, const u8
*sta_addr
,
2531 u16 status
, const u8
*resp_ies
,
2532 size_t resp_ies_len
)
2534 struct wpa_authenticator
*wpa_auth
= sm
->wpa_auth
;
2536 struct ft_rrb_frame
*frame
;
2539 wpa_printf(MSG_DEBUG
, "FT: RRB authentication response: STA=" MACSTR
2540 " CurrentAP=" MACSTR
" status=%d",
2541 MAC2STR(sm
->addr
), MAC2STR(current_ap
), status
);
2542 wpa_hexdump(MSG_DEBUG
, "FT: Response IEs", resp_ies
, resp_ies_len
);
2544 /* RRB - Forward action frame response to the Current AP */
2547 * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
2548 * Status_Code[2] FT Request action frame body[variable]
2550 rlen
= 2 + 2 * ETH_ALEN
+ 2 + resp_ies_len
;
2552 frame
= os_malloc(sizeof(*frame
) + rlen
);
2555 frame
->frame_type
= RSN_REMOTE_FRAME_TYPE_FT_RRB
;
2556 frame
->packet_type
= FT_PACKET_RESPONSE
;
2557 frame
->action_length
= host_to_le16(rlen
);
2558 os_memcpy(frame
->ap_address
, wpa_auth
->addr
, ETH_ALEN
);
2559 pos
= (u8
*) (frame
+ 1);
2560 *pos
++ = WLAN_ACTION_FT
;
2561 *pos
++ = 2; /* Action: Response */
2562 os_memcpy(pos
, sta_addr
, ETH_ALEN
);
2564 os_memcpy(pos
, wpa_auth
->addr
, ETH_ALEN
);
2566 WPA_PUT_LE16(pos
, status
);
2569 os_memcpy(pos
, resp_ies
, resp_ies_len
);
2571 wpa_ft_rrb_send(wpa_auth
, current_ap
, (u8
*) frame
,
2572 sizeof(*frame
) + rlen
);
2579 static int wpa_ft_rrb_build_r0(const u8
*key
, const size_t key_len
,
2580 const struct tlv_list
*tlvs
,
2581 const struct wpa_ft_pmk_r0_sa
*pmk_r0
,
2582 const u8
*r1kh_id
, const u8
*s1kh_id
,
2583 const struct tlv_list
*tlv_auth
,
2584 const u8
*src_addr
, u8 type
,
2585 u8
**packet
, size_t *packet_len
)
2588 u8 pmk_r1_name
[WPA_PMK_NAME_LEN
];
2589 u8 f_pairwise
[sizeof(le16
)];
2591 struct tlv_list sess_tlv
[] = {
2592 { .type
= FT_RRB_PMK_R1
, .len
= sizeof(pmk_r1
),
2594 { .type
= FT_RRB_PMK_R1_NAME
, .len
= sizeof(pmk_r1_name
),
2595 .data
= pmk_r1_name
},
2596 { .type
= FT_RRB_PAIRWISE
, .len
= sizeof(f_pairwise
),
2597 .data
= f_pairwise
},
2598 { .type
= FT_RRB_LAST_EMPTY
, .len
= 0, .data
= NULL
},
2602 return wpa_ft_rrb_build(key
, key_len
, tlvs
, NULL
, tlv_auth
,
2603 src_addr
, type
, packet
, packet_len
);
2605 if (wpa_derive_pmk_r1(pmk_r0
->pmk_r0
, pmk_r0
->pmk_r0_name
, r1kh_id
,
2606 s1kh_id
, pmk_r1
, pmk_r1_name
) < 0)
2608 wpa_hexdump_key(MSG_DEBUG
, "FT: PMK-R1", pmk_r1
, PMK_LEN
);
2609 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name", pmk_r1_name
, WPA_PMK_NAME_LEN
);
2610 WPA_PUT_LE16(f_pairwise
, pmk_r0
->pairwise
);
2612 ret
= wpa_ft_rrb_build(key
, key_len
, tlvs
, sess_tlv
, tlv_auth
,
2613 src_addr
, type
, packet
, packet_len
);
2615 os_memset(pmk_r1
, 0, sizeof(pmk_r1
));
2621 static int wpa_ft_rrb_rx_pull(struct wpa_authenticator
*wpa_auth
,
2623 const u8
*enc
, size_t enc_len
,
2624 const u8
*auth
, size_t auth_len
,
2627 const char *msgtype
= "pull request";
2628 u8
*plain
= NULL
, *packet
= NULL
;
2629 size_t plain_len
= 0, packet_len
= 0;
2630 struct ft_remote_r1kh
*r1kh
, *r1kh_wildcard
;
2634 const u8
*f_nonce
, *f_r0kh_id
, *f_r1kh_id
, *f_s1kh_id
, *f_pmk_r0_name
;
2635 size_t f_nonce_len
, f_r0kh_id_len
, f_r1kh_id_len
, f_s1kh_id_len
;
2636 size_t f_pmk_r0_name_len
;
2637 const struct wpa_ft_pmk_r0_sa
*r0
;
2639 struct tlv_list resp
[2];
2640 struct tlv_list resp_auth
[5];
2641 struct ft_rrb_seq f_seq
;
2643 wpa_printf(MSG_DEBUG
, "FT: Received PMK-R1 pull");
2645 RRB_GET_AUTH(FT_RRB_R0KH_ID
, r0kh_id
, msgtype
, -1);
2646 wpa_hexdump(MSG_DEBUG
, "FT: R0KH-ID", f_r0kh_id
, f_r0kh_id_len
);
2648 if (wpa_ft_rrb_check_r0kh(wpa_auth
, f_r0kh_id
, f_r0kh_id_len
)) {
2649 wpa_printf(MSG_DEBUG
, "FT: R0KH-ID mismatch");
2653 RRB_GET_AUTH(FT_RRB_R1KH_ID
, r1kh_id
, msgtype
, FT_R1KH_ID_LEN
);
2654 wpa_printf(MSG_DEBUG
, "FT: R1KH-ID=" MACSTR
, MAC2STR(f_r1kh_id
));
2656 wpa_ft_rrb_lookup_r1kh(wpa_auth
, f_r1kh_id
, &r1kh
, &r1kh_wildcard
);
2659 key_len
= sizeof(r1kh
->key
);
2660 } else if (r1kh_wildcard
) {
2661 wpa_printf(MSG_DEBUG
, "FT: Using wildcard R1KH-ID");
2662 key
= r1kh_wildcard
->key
;
2663 key_len
= sizeof(r1kh_wildcard
->key
);
2668 RRB_GET_AUTH(FT_RRB_NONCE
, nonce
, "pull request", FT_RRB_NONCE_LEN
);
2669 wpa_hexdump(MSG_DEBUG
, "FT: nonce", f_nonce
, f_nonce_len
);
2671 seq_ret
= FT_RRB_SEQ_DROP
;
2673 seq_ret
= wpa_ft_rrb_seq_chk(r1kh
->seq
, src_addr
, enc
, enc_len
,
2674 auth
, auth_len
, msgtype
, no_defer
);
2675 if (!no_defer
&& r1kh_wildcard
&&
2676 (!r1kh
|| os_memcmp(r1kh
->addr
, src_addr
, ETH_ALEN
) != 0)) {
2677 /* wildcard: r1kh-id unknown or changed addr -> do a seq req */
2678 seq_ret
= FT_RRB_SEQ_DEFER
;
2681 if (seq_ret
== FT_RRB_SEQ_DROP
)
2684 if (wpa_ft_rrb_decrypt(key
, key_len
, enc
, enc_len
, auth
, auth_len
,
2685 src_addr
, FT_PACKET_R0KH_R1KH_PULL
,
2686 &plain
, &plain_len
) < 0)
2690 r1kh
= wpa_ft_rrb_add_r1kh(wpa_auth
, r1kh_wildcard
, src_addr
,
2692 wpa_auth
->conf
.rkh_pos_timeout
);
2696 if (seq_ret
== FT_RRB_SEQ_DEFER
) {
2697 wpa_ft_rrb_seq_req(wpa_auth
, r1kh
->seq
, src_addr
, f_r0kh_id
,
2698 f_r0kh_id_len
, f_r1kh_id
, key
, key_len
,
2699 enc
, enc_len
, auth
, auth_len
,
2700 &wpa_ft_rrb_rx_pull
);
2704 wpa_ft_rrb_seq_accept(wpa_auth
, r1kh
->seq
, src_addr
, auth
, auth_len
,
2706 wpa_ft_rrb_r1kh_replenish(wpa_auth
, r1kh
,
2707 wpa_auth
->conf
.rkh_pos_timeout
);
2709 RRB_GET(FT_RRB_PMK_R0_NAME
, pmk_r0_name
, msgtype
, WPA_PMK_NAME_LEN
);
2710 wpa_hexdump(MSG_DEBUG
, "FT: PMKR0Name", f_pmk_r0_name
,
2713 RRB_GET(FT_RRB_S1KH_ID
, s1kh_id
, msgtype
, ETH_ALEN
);
2714 wpa_printf(MSG_DEBUG
, "FT: S1KH-ID=" MACSTR
, MAC2STR(f_s1kh_id
));
2716 if (wpa_ft_new_seq(r1kh
->seq
, &f_seq
) < 0) {
2717 wpa_printf(MSG_DEBUG
, "FT: Failed to get seq num");
2721 resp
[0].type
= FT_RRB_S1KH_ID
;
2722 resp
[0].len
= f_s1kh_id_len
;
2723 resp
[0].data
= f_s1kh_id
;
2724 resp
[1].type
= FT_RRB_LAST_EMPTY
;
2726 resp
[1].data
= NULL
;
2728 resp_auth
[0].type
= FT_RRB_NONCE
;
2729 resp_auth
[0].len
= f_nonce_len
;
2730 resp_auth
[0].data
= f_nonce
;
2731 resp_auth
[1].type
= FT_RRB_SEQ
;
2732 resp_auth
[1].len
= sizeof(f_seq
);
2733 resp_auth
[1].data
= (u8
*) &f_seq
;
2734 resp_auth
[2].type
= FT_RRB_R0KH_ID
;
2735 resp_auth
[2].len
= f_r0kh_id_len
;
2736 resp_auth
[2].data
= f_r0kh_id
;
2737 resp_auth
[3].type
= FT_RRB_R1KH_ID
;
2738 resp_auth
[3].len
= f_r1kh_id_len
;
2739 resp_auth
[3].data
= f_r1kh_id
;
2740 resp_auth
[4].type
= FT_RRB_LAST_EMPTY
;
2741 resp_auth
[4].len
= 0;
2742 resp_auth
[4].data
= NULL
;
2744 if (wpa_ft_fetch_pmk_r0(wpa_auth
, f_s1kh_id
, f_pmk_r0_name
, &r0
) < 0)
2745 wpa_printf(MSG_DEBUG
, "FT: No matching PMK-R0-Name found");
2747 ret
= wpa_ft_rrb_build_r0(key
, key_len
, resp
, r0
, f_r1kh_id
, f_s1kh_id
,
2748 resp_auth
, wpa_auth
->addr
,
2749 FT_PACKET_R0KH_R1KH_RESP
,
2750 &packet
, &packet_len
);
2753 wpa_ft_rrb_oui_send(wpa_auth
, src_addr
,
2754 FT_PACKET_R0KH_R1KH_RESP
, packet
,
2765 /* @returns 0 on success
2767 * -2 if FR_RRB_PAIRWISE is missing
2769 static int wpa_ft_rrb_rx_r1(struct wpa_authenticator
*wpa_auth
,
2770 const u8
*src_addr
, u8 type
,
2771 const u8
*enc
, size_t enc_len
,
2772 const u8
*auth
, size_t auth_len
,
2773 const char *msgtype
, u8
*s1kh_id_out
,
2774 int (*cb
)(struct wpa_authenticator
*wpa_auth
,
2776 const u8
*enc
, size_t enc_len
,
2777 const u8
*auth
, size_t auth_len
,
2781 size_t plain_len
= 0;
2782 struct ft_remote_r0kh
*r0kh
, *r0kh_wildcard
;
2786 const u8
*f_r1kh_id
, *f_s1kh_id
, *f_r0kh_id
;
2787 const u8
*f_pmk_r1_name
, *f_pairwise
, *f_pmk_r1
;
2788 size_t f_r1kh_id_len
, f_s1kh_id_len
, f_r0kh_id_len
;
2789 size_t f_pmk_r1_name_len
, f_pairwise_len
, f_pmk_r1_len
;
2793 RRB_GET_AUTH(FT_RRB_R0KH_ID
, r0kh_id
, msgtype
, -1);
2794 wpa_hexdump(MSG_DEBUG
, "FT: R0KH-ID", f_r0kh_id
, f_r0kh_id_len
);
2796 RRB_GET_AUTH(FT_RRB_R1KH_ID
, r1kh_id
, msgtype
, FT_R1KH_ID_LEN
);
2797 wpa_printf(MSG_DEBUG
, "FT: R1KH-ID=" MACSTR
, MAC2STR(f_r1kh_id
));
2799 if (wpa_ft_rrb_check_r1kh(wpa_auth
, f_r1kh_id
)) {
2800 wpa_printf(MSG_DEBUG
, "FT: R1KH-ID mismatch");
2804 wpa_ft_rrb_lookup_r0kh(wpa_auth
, f_r0kh_id
, f_r0kh_id_len
, &r0kh
,
2808 key_len
= sizeof(r0kh
->key
);
2809 } else if (r0kh_wildcard
) {
2810 wpa_printf(MSG_DEBUG
, "FT: Using wildcard R0KH-ID");
2811 key
= r0kh_wildcard
->key
;
2812 key_len
= sizeof(r0kh_wildcard
->key
);
2817 seq_ret
= FT_RRB_SEQ_DROP
;
2819 seq_ret
= wpa_ft_rrb_seq_chk(r0kh
->seq
, src_addr
, enc
, enc_len
,
2820 auth
, auth_len
, msgtype
,
2823 if (cb
&& r0kh_wildcard
&&
2824 (!r0kh
|| os_memcmp(r0kh
->addr
, src_addr
, ETH_ALEN
) != 0)) {
2825 /* wildcard: r0kh-id unknown or changed addr -> do a seq req */
2826 seq_ret
= FT_RRB_SEQ_DEFER
;
2829 if (seq_ret
== FT_RRB_SEQ_DROP
)
2832 if (wpa_ft_rrb_decrypt(key
, key_len
, enc
, enc_len
, auth
, auth_len
,
2833 src_addr
, type
, &plain
, &plain_len
) < 0)
2837 r0kh
= wpa_ft_rrb_add_r0kh(wpa_auth
, r0kh_wildcard
, src_addr
,
2838 f_r0kh_id
, f_r0kh_id_len
,
2839 wpa_auth
->conf
.rkh_pos_timeout
);
2843 if (seq_ret
== FT_RRB_SEQ_DEFER
) {
2844 wpa_ft_rrb_seq_req(wpa_auth
, r0kh
->seq
, src_addr
, f_r0kh_id
,
2845 f_r0kh_id_len
, f_r1kh_id
, key
, key_len
,
2846 enc
, enc_len
, auth
, auth_len
, cb
);
2850 wpa_ft_rrb_seq_accept(wpa_auth
, r0kh
->seq
, src_addr
, auth
, auth_len
,
2852 wpa_ft_rrb_r0kh_replenish(wpa_auth
, r0kh
,
2853 wpa_auth
->conf
.rkh_pos_timeout
);
2855 RRB_GET(FT_RRB_S1KH_ID
, s1kh_id
, msgtype
, ETH_ALEN
);
2856 wpa_printf(MSG_DEBUG
, "FT: S1KH-ID=" MACSTR
, MAC2STR(f_s1kh_id
));
2859 os_memcpy(s1kh_id_out
, f_s1kh_id
, ETH_ALEN
);
2862 RRB_GET(FT_RRB_PAIRWISE
, pairwise
, msgtype
, sizeof(le16
));
2863 wpa_hexdump(MSG_DEBUG
, "FT: pairwise", f_pairwise
, f_pairwise_len
);
2866 RRB_GET(FT_RRB_PMK_R1_NAME
, pmk_r1_name
, msgtype
, WPA_PMK_NAME_LEN
);
2867 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name",
2868 f_pmk_r1_name
, WPA_PMK_NAME_LEN
);
2870 RRB_GET(FT_RRB_PMK_R1
, pmk_r1
, msgtype
, PMK_LEN
);
2871 wpa_hexdump_key(MSG_DEBUG
, "FT: PMK-R1", f_pmk_r1
, PMK_LEN
);
2873 pairwise
= WPA_GET_LE16(f_pairwise
);
2875 if (wpa_ft_store_pmk_r1(wpa_auth
, f_s1kh_id
, f_pmk_r1
, f_pmk_r1_name
,
2882 os_memset(plain
, 0, plain_len
);
2891 static void ft_finish_pull(struct wpa_state_machine
*sm
)
2895 size_t resp_ies_len
;
2898 if (!sm
->ft_pending_cb
|| !sm
->ft_pending_req_ies
)
2901 res
= wpa_ft_process_auth_req(sm
, wpabuf_head(sm
->ft_pending_req_ies
),
2902 wpabuf_len(sm
->ft_pending_req_ies
),
2903 &resp_ies
, &resp_ies_len
);
2905 /* this loop is broken by ft_pending_pull_left_retries */
2906 wpa_printf(MSG_DEBUG
,
2907 "FT: Callback postponed until response is available");
2910 wpabuf_free(sm
->ft_pending_req_ies
);
2911 sm
->ft_pending_req_ies
= NULL
;
2913 wpa_printf(MSG_DEBUG
, "FT: Postponed auth callback result for " MACSTR
2914 " - status %u", MAC2STR(sm
->addr
), status
);
2916 sm
->ft_pending_cb(sm
->ft_pending_cb_ctx
, sm
->addr
, sm
->wpa_auth
->addr
,
2917 sm
->ft_pending_auth_transaction
+ 1, status
,
2918 resp_ies
, resp_ies_len
);
2923 struct ft_get_sta_ctx
{
2926 struct wpa_state_machine
*sm
;
2930 static int ft_get_sta_cb(struct wpa_state_machine
*sm
, void *ctx
)
2932 struct ft_get_sta_ctx
*info
= ctx
;
2934 if ((info
->s1kh_id
&&
2935 os_memcmp(info
->s1kh_id
, sm
->addr
, ETH_ALEN
) != 0) ||
2936 os_memcmp(info
->nonce
, sm
->ft_pending_pull_nonce
,
2937 FT_RRB_NONCE_LEN
) != 0 ||
2938 sm
->ft_pending_cb
== NULL
|| sm
->ft_pending_req_ies
== NULL
)
2947 static int wpa_ft_rrb_rx_resp(struct wpa_authenticator
*wpa_auth
,
2949 const u8
*enc
, size_t enc_len
,
2950 const u8
*auth
, size_t auth_len
,
2953 const char *msgtype
= "pull response";
2955 struct ft_get_sta_ctx ctx
;
2956 u8 s1kh_id
[ETH_ALEN
];
2960 wpa_printf(MSG_DEBUG
, "FT: Received PMK-R1 pull response");
2962 RRB_GET_AUTH(FT_RRB_NONCE
, nonce
, msgtype
, FT_RRB_NONCE_LEN
);
2963 wpa_hexdump(MSG_DEBUG
, "FT: nonce", f_nonce
, f_nonce_len
);
2965 os_memset(&ctx
, 0, sizeof(ctx
));
2966 ctx
.nonce
= f_nonce
;
2967 if (!wpa_auth_for_each_sta(wpa_auth
, ft_get_sta_cb
, &ctx
)) {
2968 /* nonce not found */
2969 wpa_printf(MSG_DEBUG
, "FT: Invalid nonce");
2973 ret
= wpa_ft_rrb_rx_r1(wpa_auth
, src_addr
, FT_PACKET_R0KH_R1KH_RESP
,
2974 enc
, enc_len
, auth
, auth_len
, msgtype
, s1kh_id
,
2975 no_defer
? NULL
: &wpa_ft_rrb_rx_resp
);
2985 ctx
.s1kh_id
= s1kh_id
;
2986 if (wpa_auth_for_each_sta(wpa_auth
, ft_get_sta_cb
, &ctx
)) {
2987 wpa_printf(MSG_DEBUG
,
2988 "FT: Response to a pending pull request for " MACSTR
,
2989 MAC2STR(ctx
.sm
->addr
));
2990 eloop_cancel_timeout(wpa_ft_expire_pull
, ctx
.sm
, NULL
);
2992 ctx
.sm
->ft_pending_pull_left_retries
= 0;
2993 ft_finish_pull(ctx
.sm
);
3001 static int wpa_ft_rrb_rx_push(struct wpa_authenticator
*wpa_auth
,
3003 const u8
*enc
, size_t enc_len
,
3004 const u8
*auth
, size_t auth_len
, int no_defer
)
3006 const char *msgtype
= "push";
3008 wpa_printf(MSG_DEBUG
, "FT: Received PMK-R1 push");
3010 if (wpa_ft_rrb_rx_r1(wpa_auth
, src_addr
, FT_PACKET_R0KH_R1KH_PUSH
,
3011 enc
, enc_len
, auth
, auth_len
, msgtype
, NULL
,
3012 no_defer
? NULL
: wpa_ft_rrb_rx_push
) < 0)
3019 static int wpa_ft_rrb_rx_seq(struct wpa_authenticator
*wpa_auth
,
3020 const u8
*src_addr
, int type
,
3021 const u8
*enc
, size_t enc_len
,
3022 const u8
*auth
, size_t auth_len
,
3023 struct ft_remote_seq
**rkh_seq
,
3024 u8
**key
, size_t *key_len
,
3025 struct ft_remote_r0kh
**r0kh_out
,
3026 struct ft_remote_r1kh
**r1kh_out
,
3027 struct ft_remote_r0kh
**r0kh_wildcard_out
,
3028 struct ft_remote_r1kh
**r1kh_wildcard_out
)
3030 struct ft_remote_r0kh
*r0kh
= NULL
;
3031 struct ft_remote_r1kh
*r1kh
= NULL
;
3032 const u8
*f_r0kh_id
, *f_r1kh_id
;
3033 size_t f_r0kh_id_len
, f_r1kh_id_len
;
3034 int to_r0kh
, to_r1kh
;
3036 size_t plain_len
= 0;
3037 struct ft_remote_r0kh
*r0kh_wildcard
;
3038 struct ft_remote_r1kh
*r1kh_wildcard
;
3040 RRB_GET_AUTH(FT_RRB_R0KH_ID
, r0kh_id
, "seq", -1);
3041 RRB_GET_AUTH(FT_RRB_R1KH_ID
, r1kh_id
, "seq", FT_R1KH_ID_LEN
);
3043 to_r0kh
= !wpa_ft_rrb_check_r0kh(wpa_auth
, f_r0kh_id
, f_r0kh_id_len
);
3044 to_r1kh
= !wpa_ft_rrb_check_r1kh(wpa_auth
, f_r1kh_id
);
3046 if (to_r0kh
&& to_r1kh
) {
3047 wpa_printf(MSG_DEBUG
, "FT: seq - local R0KH-ID and R1KH-ID");
3051 if (!to_r0kh
&& !to_r1kh
) {
3052 wpa_printf(MSG_DEBUG
, "FT: seq - remote R0KH-ID and R1KH-ID");
3057 wpa_ft_rrb_lookup_r0kh(wpa_auth
, f_r0kh_id
, f_r0kh_id_len
,
3058 &r0kh
, &r0kh_wildcard
);
3059 if (!r0kh_wildcard
&&
3060 (!r0kh
|| os_memcmp(r0kh
->addr
, src_addr
, ETH_ALEN
) != 0)) {
3061 wpa_hexdump(MSG_DEBUG
, "FT: Did not find R0KH-ID",
3062 f_r0kh_id
, f_r0kh_id_len
);
3067 *key_len
= sizeof(r0kh
->key
);
3069 *key
= r0kh_wildcard
->key
;
3070 *key_len
= sizeof(r0kh_wildcard
->key
);
3075 wpa_ft_rrb_lookup_r1kh(wpa_auth
, f_r1kh_id
, &r1kh
,
3077 if (!r1kh_wildcard
&&
3078 (!r1kh
|| os_memcmp(r1kh
->addr
, src_addr
, ETH_ALEN
) != 0)) {
3079 wpa_hexdump(MSG_DEBUG
, "FT: Did not find R1KH-ID",
3080 f_r1kh_id
, FT_R1KH_ID_LEN
);
3085 *key_len
= sizeof(r1kh
->key
);
3087 *key
= r1kh_wildcard
->key
;
3088 *key_len
= sizeof(r1kh_wildcard
->key
);
3092 if (wpa_ft_rrb_decrypt(*key
, *key_len
, enc
, enc_len
, auth
, auth_len
,
3093 src_addr
, type
, &plain
, &plain_len
) < 0)
3100 r0kh
= wpa_ft_rrb_add_r0kh(wpa_auth
, r0kh_wildcard
,
3101 src_addr
, f_r0kh_id
,
3107 wpa_ft_rrb_r0kh_replenish(wpa_auth
, r0kh
, ftRRBseqTimeout
);
3108 *rkh_seq
= r0kh
->seq
;
3111 if (r0kh_wildcard_out
)
3112 *r0kh_wildcard_out
= r0kh_wildcard
;
3117 r1kh
= wpa_ft_rrb_add_r1kh(wpa_auth
, r1kh_wildcard
,
3118 src_addr
, f_r1kh_id
,
3123 wpa_ft_rrb_r1kh_replenish(wpa_auth
, r1kh
, ftRRBseqTimeout
);
3124 *rkh_seq
= r1kh
->seq
;
3127 if (r1kh_wildcard_out
)
3128 *r1kh_wildcard_out
= r1kh_wildcard
;
3137 static int wpa_ft_rrb_rx_seq_req(struct wpa_authenticator
*wpa_auth
,
3139 const u8
*enc
, size_t enc_len
,
3140 const u8
*auth
, size_t auth_len
,
3144 struct ft_rrb_seq f_seq
;
3145 const u8
*f_nonce
, *f_r0kh_id
, *f_r1kh_id
;
3146 size_t f_nonce_len
, f_r0kh_id_len
, f_r1kh_id_len
;
3147 struct ft_remote_seq
*rkh_seq
= NULL
;
3148 u8
*packet
= NULL
, *key
= NULL
;
3149 size_t packet_len
= 0, key_len
= 0;
3150 struct tlv_list seq_resp_auth
[5];
3152 wpa_printf(MSG_DEBUG
, "FT: Received sequence number request");
3154 if (wpa_ft_rrb_rx_seq(wpa_auth
, src_addr
, FT_PACKET_R0KH_R1KH_SEQ_REQ
,
3155 enc
, enc_len
, auth
, auth_len
, &rkh_seq
, &key
,
3156 &key_len
, NULL
, NULL
, NULL
, NULL
) < 0)
3159 RRB_GET_AUTH(FT_RRB_NONCE
, nonce
, "seq request", FT_RRB_NONCE_LEN
);
3160 wpa_hexdump(MSG_DEBUG
, "FT: seq request - nonce", f_nonce
, f_nonce_len
);
3162 RRB_GET_AUTH(FT_RRB_R0KH_ID
, r0kh_id
, "seq", -1);
3163 RRB_GET_AUTH(FT_RRB_R1KH_ID
, r1kh_id
, "seq", FT_R1KH_ID_LEN
);
3165 if (wpa_ft_new_seq(rkh_seq
, &f_seq
) < 0) {
3166 wpa_printf(MSG_DEBUG
, "FT: Failed to get seq num");
3170 seq_resp_auth
[0].type
= FT_RRB_NONCE
;
3171 seq_resp_auth
[0].len
= f_nonce_len
;
3172 seq_resp_auth
[0].data
= f_nonce
;
3173 seq_resp_auth
[1].type
= FT_RRB_SEQ
;
3174 seq_resp_auth
[1].len
= sizeof(f_seq
);
3175 seq_resp_auth
[1].data
= (u8
*) &f_seq
;
3176 seq_resp_auth
[2].type
= FT_RRB_R0KH_ID
;
3177 seq_resp_auth
[2].len
= f_r0kh_id_len
;
3178 seq_resp_auth
[2].data
= f_r0kh_id
;
3179 seq_resp_auth
[3].type
= FT_RRB_R1KH_ID
;
3180 seq_resp_auth
[3].len
= FT_R1KH_ID_LEN
;
3181 seq_resp_auth
[3].data
= f_r1kh_id
;
3182 seq_resp_auth
[4].type
= FT_RRB_LAST_EMPTY
;
3183 seq_resp_auth
[4].len
= 0;
3184 seq_resp_auth
[4].data
= NULL
;
3186 if (wpa_ft_rrb_build(key
, key_len
, NULL
, NULL
, seq_resp_auth
,
3187 wpa_auth
->addr
, FT_PACKET_R0KH_R1KH_SEQ_RESP
,
3188 &packet
, &packet_len
) < 0)
3191 wpa_ft_rrb_oui_send(wpa_auth
, src_addr
,
3192 FT_PACKET_R0KH_R1KH_SEQ_RESP
, packet
,
3202 static int wpa_ft_rrb_rx_seq_resp(struct wpa_authenticator
*wpa_auth
,
3204 const u8
*enc
, size_t enc_len
,
3205 const u8
*auth
, size_t auth_len
,
3210 struct ft_remote_r0kh
*r0kh
= NULL
, *r0kh_wildcard
= NULL
;
3211 struct ft_remote_r1kh
*r1kh
= NULL
, *r1kh_wildcard
= NULL
;
3212 const u8
*f_nonce
, *f_seq
;
3213 size_t f_nonce_len
, f_seq_len
;
3214 struct ft_remote_seq
*rkh_seq
= NULL
;
3215 struct ft_remote_item
*item
;
3216 struct os_reltime now
, now_remote
;
3218 const struct ft_rrb_seq
*msg_both
;
3219 u32 msg_dom
, msg_seq
;
3221 wpa_printf(MSG_DEBUG
, "FT: Received sequence number response");
3223 if (wpa_ft_rrb_rx_seq(wpa_auth
, src_addr
, FT_PACKET_R0KH_R1KH_SEQ_RESP
,
3224 enc
, enc_len
, auth
, auth_len
, &rkh_seq
, &key
,
3225 &key_len
, &r0kh
, &r1kh
, &r0kh_wildcard
,
3226 &r1kh_wildcard
) < 0)
3229 RRB_GET_AUTH(FT_RRB_NONCE
, nonce
, "seq response", FT_RRB_NONCE_LEN
);
3230 wpa_hexdump(MSG_DEBUG
, "FT: seq response - nonce", f_nonce
,
3234 dl_list_for_each(item
, &rkh_seq
->rx
.queue
, struct ft_remote_item
,
3236 if (os_memcmp_const(f_nonce
, item
->nonce
,
3237 FT_RRB_NONCE_LEN
) != 0 ||
3238 os_get_reltime(&now
) < 0 ||
3239 os_reltime_expired(&now
, &item
->nonce_ts
, ftRRBseqTimeout
))
3246 wpa_printf(MSG_DEBUG
, "FT: seq response - bad nonce");
3251 wpa_ft_rrb_r0kh_replenish(wpa_auth
, r0kh
,
3252 wpa_auth
->conf
.rkh_pos_timeout
);
3254 os_memcpy(r0kh
->addr
, src_addr
, ETH_ALEN
);
3258 wpa_ft_rrb_r1kh_replenish(wpa_auth
, r1kh
,
3259 wpa_auth
->conf
.rkh_pos_timeout
);
3261 os_memcpy(r1kh
->addr
, src_addr
, ETH_ALEN
);
3264 seq_ret
= wpa_ft_rrb_seq_chk(rkh_seq
, src_addr
, enc
, enc_len
, auth
,
3265 auth_len
, "seq response", 1);
3266 if (seq_ret
== FT_RRB_SEQ_OK
) {
3267 wpa_printf(MSG_DEBUG
, "FT: seq response - valid seq number");
3268 wpa_ft_rrb_seq_accept(wpa_auth
, rkh_seq
, src_addr
, auth
,
3269 auth_len
, "seq response");
3271 wpa_printf(MSG_DEBUG
, "FT: seq response - reset seq number");
3273 RRB_GET_AUTH(FT_RRB_SEQ
, seq
, "seq response",
3275 msg_both
= (const struct ft_rrb_seq
*) f_seq
;
3277 msg_dom
= le_to_host32(msg_both
->dom
);
3278 msg_seq
= le_to_host32(msg_both
->seq
);
3279 now_remote
.sec
= le_to_host32(msg_both
->ts
);
3280 now_remote
.usec
= 0;
3282 rkh_seq
->rx
.num_last
= 2;
3283 rkh_seq
->rx
.dom
= msg_dom
;
3284 rkh_seq
->rx
.offsetidx
= 0;
3285 /* Accept some older, possibly cached packets as well */
3286 rkh_seq
->rx
.last
[0] = msg_seq
- FT_REMOTE_SEQ_BACKLOG
-
3287 dl_list_len(&rkh_seq
->rx
.queue
);
3288 rkh_seq
->rx
.last
[1] = msg_seq
;
3290 /* local time - offset = remote time
3291 * <=> local time - remote time = offset */
3292 os_reltime_sub(&now
, &now_remote
, &rkh_seq
->rx
.time_offset
);
3295 wpa_ft_rrb_seq_flush(wpa_auth
, rkh_seq
, 1);
3303 int wpa_ft_rrb_rx(struct wpa_authenticator
*wpa_auth
, const u8
*src_addr
,
3304 const u8
*data
, size_t data_len
)
3306 struct ft_rrb_frame
*frame
;
3308 const u8
*pos
, *end
, *start
;
3310 const u8
*sta_addr
, *target_ap_addr
;
3312 wpa_printf(MSG_DEBUG
, "FT: RRB received frame from remote AP " MACSTR
,
3315 if (data_len
< sizeof(*frame
)) {
3316 wpa_printf(MSG_DEBUG
, "FT: Too short RRB frame (data_len=%lu)",
3317 (unsigned long) data_len
);
3322 frame
= (struct ft_rrb_frame
*) pos
;
3323 pos
+= sizeof(*frame
);
3325 alen
= le_to_host16(frame
->action_length
);
3326 wpa_printf(MSG_DEBUG
, "FT: RRB frame - frame_type=%d packet_type=%d "
3327 "action_length=%d ap_address=" MACSTR
,
3328 frame
->frame_type
, frame
->packet_type
, alen
,
3329 MAC2STR(frame
->ap_address
));
3331 if (frame
->frame_type
!= RSN_REMOTE_FRAME_TYPE_FT_RRB
) {
3332 /* Discard frame per IEEE Std 802.11r-2008, 11A.10.3 */
3333 wpa_printf(MSG_DEBUG
, "FT: RRB discarded frame with "
3334 "unrecognized type %d", frame
->frame_type
);
3338 if (alen
> data_len
- sizeof(*frame
)) {
3339 wpa_printf(MSG_DEBUG
, "FT: RRB frame too short for action "
3344 wpa_hexdump(MSG_MSGDUMP
, "FT: RRB - FT Action frame", pos
, alen
);
3346 if (alen
< 1 + 1 + 2 * ETH_ALEN
) {
3347 wpa_printf(MSG_DEBUG
, "FT: Too short RRB frame (not enough "
3348 "room for Action Frame body); alen=%lu",
3349 (unsigned long) alen
);
3355 if (*pos
!= WLAN_ACTION_FT
) {
3356 wpa_printf(MSG_DEBUG
, "FT: Unexpected Action frame category "
3365 target_ap_addr
= pos
;
3367 wpa_printf(MSG_DEBUG
, "FT: RRB Action Frame: action=%d sta_addr="
3368 MACSTR
" target_ap_addr=" MACSTR
,
3369 action
, MAC2STR(sta_addr
), MAC2STR(target_ap_addr
));
3371 if (frame
->packet_type
== FT_PACKET_REQUEST
) {
3372 wpa_printf(MSG_DEBUG
, "FT: FT Packet Type - Request");
3375 wpa_printf(MSG_DEBUG
, "FT: Unexpected Action %d in "
3376 "RRB Request", action
);
3380 if (os_memcmp(target_ap_addr
, wpa_auth
->addr
, ETH_ALEN
) != 0) {
3381 wpa_printf(MSG_DEBUG
, "FT: Target AP address in the "
3382 "RRB Request does not match with own "
3387 if (wpa_ft_rrb_rx_request(wpa_auth
, frame
->ap_address
,
3388 sta_addr
, pos
, end
- pos
) < 0)
3390 } else if (frame
->packet_type
== FT_PACKET_RESPONSE
) {
3393 if (end
- pos
< 2) {
3394 wpa_printf(MSG_DEBUG
, "FT: Not enough room for status "
3395 "code in RRB Response");
3398 status_code
= WPA_GET_LE16(pos
);
3401 wpa_printf(MSG_DEBUG
, "FT: FT Packet Type - Response "
3402 "(status_code=%d)", status_code
);
3404 if (wpa_ft_action_send(wpa_auth
, sta_addr
, start
, alen
) < 0)
3407 wpa_printf(MSG_DEBUG
, "FT: RRB discarded frame with unknown "
3408 "packet_type %d", frame
->packet_type
);
3413 wpa_hexdump(MSG_DEBUG
, "FT: Ignore extra data in end",
3421 void wpa_ft_rrb_oui_rx(struct wpa_authenticator
*wpa_auth
, const u8
*src_addr
,
3422 const u8
*dst_addr
, u8 oui_suffix
, const u8
*data
,
3425 const u8
*auth
, *enc
;
3429 wpa_printf(MSG_DEBUG
, "FT: RRB-OUI received frame from remote AP "
3430 MACSTR
, MAC2STR(src_addr
));
3431 wpa_printf(MSG_DEBUG
, "FT: RRB-OUI frame - oui_suffix=%d", oui_suffix
);
3433 if (is_multicast_ether_addr(src_addr
)) {
3434 wpa_printf(MSG_DEBUG
,
3435 "FT: RRB-OUI received frame from multicast address "
3436 MACSTR
, MAC2STR(src_addr
));
3440 if (is_multicast_ether_addr(dst_addr
)) {
3441 wpa_printf(MSG_DEBUG
,
3442 "FT: RRB-OUI received frame from remote AP " MACSTR
3443 " to multicast address " MACSTR
,
3444 MAC2STR(src_addr
), MAC2STR(dst_addr
));
3448 if (data_len
< sizeof(u16
)) {
3449 wpa_printf(MSG_DEBUG
, "FT: RRB-OUI frame too short");
3453 alen
= WPA_GET_LE16(data
);
3454 if (data_len
< sizeof(u16
) + alen
) {
3455 wpa_printf(MSG_DEBUG
, "FT: RRB-OUI frame too short");
3459 auth
= data
+ sizeof(u16
);
3460 enc
= data
+ sizeof(u16
) + alen
;
3461 elen
= data_len
- sizeof(u16
) - alen
;
3463 switch (oui_suffix
) {
3464 case FT_PACKET_R0KH_R1KH_PULL
:
3465 wpa_ft_rrb_rx_pull(wpa_auth
, src_addr
, enc
, elen
, auth
, alen
,
3468 case FT_PACKET_R0KH_R1KH_RESP
:
3469 wpa_ft_rrb_rx_resp(wpa_auth
, src_addr
, enc
, elen
, auth
, alen
,
3472 case FT_PACKET_R0KH_R1KH_PUSH
:
3473 wpa_ft_rrb_rx_push(wpa_auth
, src_addr
, enc
, elen
, auth
, alen
,
3476 case FT_PACKET_R0KH_R1KH_SEQ_REQ
:
3477 wpa_ft_rrb_rx_seq_req(wpa_auth
, src_addr
, enc
, elen
, auth
, alen
,
3480 case FT_PACKET_R0KH_R1KH_SEQ_RESP
:
3481 wpa_ft_rrb_rx_seq_resp(wpa_auth
, src_addr
, enc
, elen
, auth
,
3488 static int wpa_ft_generate_pmk_r1(struct wpa_authenticator
*wpa_auth
,
3489 struct wpa_ft_pmk_r0_sa
*pmk_r0
,
3490 struct ft_remote_r1kh
*r1kh
,
3495 struct ft_rrb_seq f_seq
;
3496 struct tlv_list push
[] = {
3497 { .type
= FT_RRB_S1KH_ID
, .len
= ETH_ALEN
,
3499 { .type
= FT_RRB_PMK_R0_NAME
, .len
= WPA_PMK_NAME_LEN
,
3500 .data
= pmk_r0
->pmk_r0_name
},
3501 { .type
= FT_RRB_LAST_EMPTY
, .len
= 0, .data
= NULL
},
3503 struct tlv_list push_auth
[] = {
3504 { .type
= FT_RRB_SEQ
, .len
= sizeof(f_seq
),
3505 .data
= (u8
*) &f_seq
},
3506 { .type
= FT_RRB_R0KH_ID
,
3507 .len
= wpa_auth
->conf
.r0_key_holder_len
,
3508 .data
= wpa_auth
->conf
.r0_key_holder
},
3509 { .type
= FT_RRB_R1KH_ID
, .len
= FT_R1KH_ID_LEN
,
3511 { .type
= FT_RRB_LAST_EMPTY
, .len
= 0, .data
= NULL
},
3514 if (wpa_ft_new_seq(r1kh
->seq
, &f_seq
) < 0) {
3515 wpa_printf(MSG_DEBUG
, "FT: Failed to get seq num");
3519 if (wpa_ft_rrb_build_r0(r1kh
->key
, sizeof(r1kh
->key
), push
, pmk_r0
,
3520 r1kh
->id
, s1kh_id
, push_auth
, wpa_auth
->addr
,
3521 FT_PACKET_R0KH_R1KH_PUSH
,
3522 &packet
, &packet_len
) < 0)
3525 wpa_ft_rrb_oui_send(wpa_auth
, r1kh
->addr
, FT_PACKET_R0KH_R1KH_PUSH
,
3526 packet
, packet_len
);
3533 void wpa_ft_push_pmk_r1(struct wpa_authenticator
*wpa_auth
, const u8
*addr
)
3535 struct wpa_ft_pmk_r0_sa
*r0
;
3536 struct ft_remote_r1kh
*r1kh
;
3538 if (!wpa_auth
->conf
.pmk_r1_push
)
3540 if (!wpa_auth
->conf
.r1kh_list
)
3543 r0
= wpa_auth
->ft_pmk_cache
->pmk_r0
;
3545 if (os_memcmp(r0
->spa
, addr
, ETH_ALEN
) == 0)
3550 if (r0
== NULL
|| r0
->pmk_r1_pushed
)
3552 r0
->pmk_r1_pushed
= 1;
3554 wpa_printf(MSG_DEBUG
, "FT: Deriving and pushing PMK-R1 keys to R1KHs "
3555 "for STA " MACSTR
, MAC2STR(addr
));
3557 for (r1kh
= *wpa_auth
->conf
.r1kh_list
; r1kh
; r1kh
= r1kh
->next
) {
3558 if (is_zero_ether_addr(r1kh
->addr
) ||
3559 is_zero_ether_addr(r1kh
->id
))
3561 if (wpa_ft_rrb_init_r1kh_seq(r1kh
) < 0)
3563 wpa_ft_generate_pmk_r1(wpa_auth
, r0
, r1kh
, addr
);
3567 #endif /* CONFIG_IEEE80211R_AP */