2 * hostapd - IEEE 802.11r - Fast BSS Transition
3 * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
17 #include "utils/common.h"
18 #include "common/ieee802_11_defs.h"
19 #include "common/ieee802_11_common.h"
20 #include "crypto/aes_wrap.h"
21 #include "crypto/random.h"
22 #include "ap_config.h"
23 #include "ieee802_11.h"
26 #include "wpa_auth_i.h"
27 #include "wpa_auth_ie.h"
30 #ifdef CONFIG_IEEE80211R
50 static int wpa_ft_parse_ies(const u8
*ies
, size_t ies_len
,
51 struct wpa_ft_ies
*parse
);
54 static int wpa_ft_rrb_send(struct wpa_authenticator
*wpa_auth
, const u8
*dst
,
55 const u8
*data
, size_t data_len
)
57 if (wpa_auth
->cb
.send_ether
== NULL
)
59 wpa_printf(MSG_DEBUG
, "FT: RRB send to " MACSTR
, MAC2STR(dst
));
60 return wpa_auth
->cb
.send_ether(wpa_auth
->cb
.ctx
, dst
, ETH_P_RRB
,
65 static int wpa_ft_action_send(struct wpa_authenticator
*wpa_auth
,
66 const u8
*dst
, const u8
*data
, size_t data_len
)
68 if (wpa_auth
->cb
.send_ft_action
== NULL
)
70 return wpa_auth
->cb
.send_ft_action(wpa_auth
->cb
.ctx
, dst
,
75 static struct wpa_state_machine
*
76 wpa_ft_add_sta(struct wpa_authenticator
*wpa_auth
, const u8
*sta_addr
)
78 if (wpa_auth
->cb
.add_sta
== NULL
)
80 return wpa_auth
->cb
.add_sta(wpa_auth
->cb
.ctx
, sta_addr
);
84 int wpa_write_mdie(struct wpa_auth_config
*conf
, u8
*buf
, size_t len
)
88 if (len
< 2 + sizeof(struct rsn_mdie
))
91 *pos
++ = WLAN_EID_MOBILITY_DOMAIN
;
92 *pos
++ = MOBILITY_DOMAIN_ID_LEN
+ 1;
93 os_memcpy(pos
, conf
->mobility_domain
, MOBILITY_DOMAIN_ID_LEN
);
94 pos
+= MOBILITY_DOMAIN_ID_LEN
;
97 capab
|= RSN_FT_CAPAB_FT_OVER_DS
;
104 int wpa_write_ftie(struct wpa_auth_config
*conf
, const u8
*r0kh_id
,
106 const u8
*anonce
, const u8
*snonce
,
107 u8
*buf
, size_t len
, const u8
*subelem
,
110 u8
*pos
= buf
, *ielen
;
111 struct rsn_ftie
*hdr
;
113 if (len
< 2 + sizeof(*hdr
) + 2 + FT_R1KH_ID_LEN
+ 2 + r0kh_id_len
+
117 *pos
++ = WLAN_EID_FAST_BSS_TRANSITION
;
120 hdr
= (struct rsn_ftie
*) pos
;
121 os_memset(hdr
, 0, sizeof(*hdr
));
123 WPA_PUT_LE16(hdr
->mic_control
, 0);
125 os_memcpy(hdr
->anonce
, anonce
, WPA_NONCE_LEN
);
127 os_memcpy(hdr
->snonce
, snonce
, WPA_NONCE_LEN
);
129 /* Optional Parameters */
130 *pos
++ = FTIE_SUBELEM_R1KH_ID
;
131 *pos
++ = FT_R1KH_ID_LEN
;
132 os_memcpy(pos
, conf
->r1_key_holder
, FT_R1KH_ID_LEN
);
133 pos
+= FT_R1KH_ID_LEN
;
136 *pos
++ = FTIE_SUBELEM_R0KH_ID
;
137 *pos
++ = r0kh_id_len
;
138 os_memcpy(pos
, r0kh_id
, r0kh_id_len
);
143 os_memcpy(pos
, subelem
, subelem_len
);
147 *ielen
= pos
- buf
- 2;
153 struct wpa_ft_pmk_r0_sa
{
154 struct wpa_ft_pmk_r0_sa
*next
;
156 u8 pmk_r0_name
[WPA_PMK_NAME_LEN
];
158 int pairwise
; /* Pairwise cipher suite, WPA_CIPHER_* */
159 /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */
163 struct wpa_ft_pmk_r1_sa
{
164 struct wpa_ft_pmk_r1_sa
*next
;
166 u8 pmk_r1_name
[WPA_PMK_NAME_LEN
];
168 int pairwise
; /* Pairwise cipher suite, WPA_CIPHER_* */
169 /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */
172 struct wpa_ft_pmk_cache
{
173 struct wpa_ft_pmk_r0_sa
*pmk_r0
;
174 struct wpa_ft_pmk_r1_sa
*pmk_r1
;
177 struct wpa_ft_pmk_cache
* wpa_ft_pmk_cache_init(void)
179 struct wpa_ft_pmk_cache
*cache
;
181 cache
= os_zalloc(sizeof(*cache
));
187 void wpa_ft_pmk_cache_deinit(struct wpa_ft_pmk_cache
*cache
)
189 struct wpa_ft_pmk_r0_sa
*r0
, *r0prev
;
190 struct wpa_ft_pmk_r1_sa
*r1
, *r1prev
;
196 os_memset(r0prev
->pmk_r0
, 0, PMK_LEN
);
204 os_memset(r1prev
->pmk_r1
, 0, PMK_LEN
);
212 static int wpa_ft_store_pmk_r0(struct wpa_authenticator
*wpa_auth
,
213 const u8
*spa
, const u8
*pmk_r0
,
214 const u8
*pmk_r0_name
, int pairwise
)
216 struct wpa_ft_pmk_cache
*cache
= wpa_auth
->ft_pmk_cache
;
217 struct wpa_ft_pmk_r0_sa
*r0
;
219 /* TODO: add expiration and limit on number of entries in cache */
221 r0
= os_zalloc(sizeof(*r0
));
225 os_memcpy(r0
->pmk_r0
, pmk_r0
, PMK_LEN
);
226 os_memcpy(r0
->pmk_r0_name
, pmk_r0_name
, WPA_PMK_NAME_LEN
);
227 os_memcpy(r0
->spa
, spa
, ETH_ALEN
);
228 r0
->pairwise
= pairwise
;
230 r0
->next
= cache
->pmk_r0
;
237 static int wpa_ft_fetch_pmk_r0(struct wpa_authenticator
*wpa_auth
,
238 const u8
*spa
, const u8
*pmk_r0_name
,
239 u8
*pmk_r0
, int *pairwise
)
241 struct wpa_ft_pmk_cache
*cache
= wpa_auth
->ft_pmk_cache
;
242 struct wpa_ft_pmk_r0_sa
*r0
;
246 if (os_memcmp(r0
->spa
, spa
, ETH_ALEN
) == 0 &&
247 os_memcmp(r0
->pmk_r0_name
, pmk_r0_name
, WPA_PMK_NAME_LEN
)
249 os_memcpy(pmk_r0
, r0
->pmk_r0
, PMK_LEN
);
251 *pairwise
= r0
->pairwise
;
262 static int wpa_ft_store_pmk_r1(struct wpa_authenticator
*wpa_auth
,
263 const u8
*spa
, const u8
*pmk_r1
,
264 const u8
*pmk_r1_name
, int pairwise
)
266 struct wpa_ft_pmk_cache
*cache
= wpa_auth
->ft_pmk_cache
;
267 struct wpa_ft_pmk_r1_sa
*r1
;
269 /* TODO: add expiration and limit on number of entries in cache */
271 r1
= os_zalloc(sizeof(*r1
));
275 os_memcpy(r1
->pmk_r1
, pmk_r1
, PMK_LEN
);
276 os_memcpy(r1
->pmk_r1_name
, pmk_r1_name
, WPA_PMK_NAME_LEN
);
277 os_memcpy(r1
->spa
, spa
, ETH_ALEN
);
278 r1
->pairwise
= pairwise
;
280 r1
->next
= cache
->pmk_r1
;
287 static int wpa_ft_fetch_pmk_r1(struct wpa_authenticator
*wpa_auth
,
288 const u8
*spa
, const u8
*pmk_r1_name
,
289 u8
*pmk_r1
, int *pairwise
)
291 struct wpa_ft_pmk_cache
*cache
= wpa_auth
->ft_pmk_cache
;
292 struct wpa_ft_pmk_r1_sa
*r1
;
296 if (os_memcmp(r1
->spa
, spa
, ETH_ALEN
) == 0 &&
297 os_memcmp(r1
->pmk_r1_name
, pmk_r1_name
, WPA_PMK_NAME_LEN
)
299 os_memcpy(pmk_r1
, r1
->pmk_r1
, PMK_LEN
);
301 *pairwise
= r1
->pairwise
;
312 static int wpa_ft_pull_pmk_r1(struct wpa_authenticator
*wpa_auth
,
313 const u8
*s1kh_id
, const u8
*r0kh_id
,
314 size_t r0kh_id_len
, const u8
*pmk_r0_name
)
316 struct ft_remote_r0kh
*r0kh
;
317 struct ft_r0kh_r1kh_pull_frame frame
, f
;
319 r0kh
= wpa_auth
->conf
.r0kh_list
;
321 if (r0kh
->id_len
== r0kh_id_len
&&
322 os_memcmp(r0kh
->id
, r0kh_id
, r0kh_id_len
) == 0)
329 wpa_printf(MSG_DEBUG
, "FT: Send PMK-R1 pull request to remote R0KH "
330 "address " MACSTR
, MAC2STR(r0kh
->addr
));
332 os_memset(&frame
, 0, sizeof(frame
));
333 frame
.frame_type
= RSN_REMOTE_FRAME_TYPE_FT_RRB
;
334 frame
.packet_type
= FT_PACKET_R0KH_R1KH_PULL
;
335 frame
.data_length
= host_to_le16(FT_R0KH_R1KH_PULL_DATA_LEN
);
336 os_memcpy(frame
.ap_address
, wpa_auth
->addr
, ETH_ALEN
);
338 /* aes_wrap() does not support inplace encryption, so use a temporary
339 * buffer for the data. */
340 if (random_get_bytes(f
.nonce
, sizeof(f
.nonce
))) {
341 wpa_printf(MSG_DEBUG
, "FT: Failed to get random data for "
345 os_memcpy(f
.pmk_r0_name
, pmk_r0_name
, WPA_PMK_NAME_LEN
);
346 os_memcpy(f
.r1kh_id
, wpa_auth
->conf
.r1_key_holder
, FT_R1KH_ID_LEN
);
347 os_memcpy(f
.s1kh_id
, s1kh_id
, ETH_ALEN
);
349 if (aes_wrap(r0kh
->key
, (FT_R0KH_R1KH_PULL_DATA_LEN
+ 7) / 8,
350 f
.nonce
, frame
.nonce
) < 0)
353 wpa_ft_rrb_send(wpa_auth
, r0kh
->addr
, (u8
*) &frame
, sizeof(frame
));
359 int wpa_auth_derive_ptk_ft(struct wpa_state_machine
*sm
, const u8
*pmk
,
360 struct wpa_ptk
*ptk
, size_t ptk_len
)
362 u8 pmk_r0
[PMK_LEN
], pmk_r0_name
[WPA_PMK_NAME_LEN
];
364 u8 ptk_name
[WPA_PMK_NAME_LEN
];
365 const u8
*mdid
= sm
->wpa_auth
->conf
.mobility_domain
;
366 const u8
*r0kh
= sm
->wpa_auth
->conf
.r0_key_holder
;
367 size_t r0kh_len
= sm
->wpa_auth
->conf
.r0_key_holder_len
;
368 const u8
*r1kh
= sm
->wpa_auth
->conf
.r1_key_holder
;
369 const u8
*ssid
= sm
->wpa_auth
->conf
.ssid
;
370 size_t ssid_len
= sm
->wpa_auth
->conf
.ssid_len
;
373 if (sm
->xxkey_len
== 0) {
374 wpa_printf(MSG_DEBUG
, "FT: XXKey not available for key "
379 wpa_derive_pmk_r0(sm
->xxkey
, sm
->xxkey_len
, ssid
, ssid_len
, mdid
,
380 r0kh
, r0kh_len
, sm
->addr
, pmk_r0
, pmk_r0_name
);
381 wpa_hexdump_key(MSG_DEBUG
, "FT: PMK-R0", pmk_r0
, PMK_LEN
);
382 wpa_hexdump(MSG_DEBUG
, "FT: PMKR0Name", pmk_r0_name
, WPA_PMK_NAME_LEN
);
383 wpa_ft_store_pmk_r0(sm
->wpa_auth
, sm
->addr
, pmk_r0
, pmk_r0_name
,
386 wpa_derive_pmk_r1(pmk_r0
, pmk_r0_name
, r1kh
, sm
->addr
,
387 pmk_r1
, sm
->pmk_r1_name
);
388 wpa_hexdump_key(MSG_DEBUG
, "FT: PMK-R1", pmk_r1
, PMK_LEN
);
389 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name", sm
->pmk_r1_name
,
391 wpa_ft_store_pmk_r1(sm
->wpa_auth
, sm
->addr
, pmk_r1
, sm
->pmk_r1_name
,
394 wpa_pmk_r1_to_ptk(pmk_r1
, sm
->SNonce
, sm
->ANonce
, sm
->addr
,
395 sm
->wpa_auth
->addr
, sm
->pmk_r1_name
,
396 (u8
*) ptk
, ptk_len
, ptk_name
);
397 wpa_hexdump_key(MSG_DEBUG
, "FT: PTK", (u8
*) ptk
, ptk_len
);
398 wpa_hexdump(MSG_DEBUG
, "FT: PTKName", ptk_name
, WPA_PMK_NAME_LEN
);
404 static inline int wpa_auth_get_seqnum(struct wpa_authenticator
*wpa_auth
,
405 const u8
*addr
, int idx
, u8
*seq
)
407 if (wpa_auth
->cb
.get_seqnum
== NULL
)
409 return wpa_auth
->cb
.get_seqnum(wpa_auth
->cb
.ctx
, addr
, idx
, seq
);
413 static u8
* wpa_ft_gtk_subelem(struct wpa_state_machine
*sm
, size_t *len
)
416 struct wpa_group
*gsm
= sm
->group
;
417 size_t subelem_len
, pad_len
;
422 key_len
= gsm
->GTK_len
;
423 if (key_len
> sizeof(keybuf
))
427 * Pad key for AES Key Wrap if it is not multiple of 8 bytes or is less
430 pad_len
= key_len
% 8;
432 pad_len
= 8 - pad_len
;
433 if (key_len
+ pad_len
< 16)
436 os_memcpy(keybuf
, gsm
->GTK
[gsm
->GN
- 1], key_len
);
437 os_memset(keybuf
+ key_len
, 0, pad_len
);
438 keybuf
[key_len
] = 0xdd;
442 key
= gsm
->GTK
[gsm
->GN
- 1];
445 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
448 subelem_len
= 13 + key_len
+ 8;
449 subelem
= os_zalloc(subelem_len
);
453 subelem
[0] = FTIE_SUBELEM_GTK
;
454 subelem
[1] = 11 + key_len
+ 8;
455 /* Key ID in B0-B1 of Key Info */
456 WPA_PUT_LE16(&subelem
[2], gsm
->GN
& 0x03);
457 subelem
[4] = gsm
->GTK_len
;
458 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN
, subelem
+ 5);
459 if (aes_wrap(sm
->PTK
.kek
, key_len
/ 8, key
, subelem
+ 13)) {
469 #ifdef CONFIG_IEEE80211W
470 static u8
* wpa_ft_igtk_subelem(struct wpa_state_machine
*sm
, size_t *len
)
473 struct wpa_group
*gsm
= sm
->group
;
476 /* Sub-elem ID[1] | Length[1] | KeyID[2] | IPN[6] | Key Length[1] |
478 subelem_len
= 1 + 1 + 2 + 6 + 1 + WPA_IGTK_LEN
+ 8;
479 subelem
= os_zalloc(subelem_len
);
484 *pos
++ = FTIE_SUBELEM_IGTK
;
485 *pos
++ = subelem_len
- 2;
486 WPA_PUT_LE16(pos
, gsm
->GN_igtk
);
488 wpa_auth_get_seqnum(sm
->wpa_auth
, NULL
, gsm
->GN_igtk
, pos
);
490 *pos
++ = WPA_IGTK_LEN
;
491 if (aes_wrap(sm
->PTK
.kek
, WPA_IGTK_LEN
/ 8,
492 gsm
->IGTK
[gsm
->GN_igtk
- 4], pos
)) {
500 #endif /* CONFIG_IEEE80211W */
503 static u8
* wpa_ft_process_rdie(u8
*pos
, u8
*end
, u8 id
, u8 descr_count
,
504 const u8
*ies
, size_t ies_len
)
506 struct ieee802_11_elems parse
;
507 struct rsn_rdie
*rdie
;
509 wpa_printf(MSG_DEBUG
, "FT: Resource Request: id=%d descr_count=%d",
511 wpa_hexdump(MSG_MSGDUMP
, "FT: Resource descriptor IE(s)",
514 if (end
- pos
< (int) sizeof(*rdie
)) {
515 wpa_printf(MSG_ERROR
, "FT: Not enough room for response RDIE");
519 *pos
++ = WLAN_EID_RIC_DATA
;
520 *pos
++ = sizeof(*rdie
);
521 rdie
= (struct rsn_rdie
*) pos
;
523 rdie
->descr_count
= 0;
524 rdie
->status_code
= host_to_le16(WLAN_STATUS_SUCCESS
);
525 pos
+= sizeof(*rdie
);
527 if (ieee802_11_parse_elems((u8
*) ies
, ies_len
, &parse
, 1) ==
529 wpa_printf(MSG_DEBUG
, "FT: Failed to parse request IEs");
531 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE
);
536 if (parse
.wmm_tspec
) {
537 struct wmm_tspec_element
*tspec
;
540 if (parse
.wmm_tspec_len
+ 2 < (int) sizeof(*tspec
)) {
541 wpa_printf(MSG_DEBUG
, "FT: Too short WMM TSPEC IE "
542 "(%d)", (int) parse
.wmm_tspec_len
);
544 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE
);
547 if (end
- pos
< (int) sizeof(*tspec
)) {
548 wpa_printf(MSG_ERROR
, "FT: Not enough room for "
551 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE
);
554 tspec
= (struct wmm_tspec_element
*) pos
;
555 os_memcpy(tspec
, parse
.wmm_tspec
- 2, sizeof(*tspec
));
556 res
= wmm_process_tspec(tspec
);
557 wpa_printf(MSG_DEBUG
, "FT: ADDTS processing result: %d", res
);
558 if (res
== WMM_ADDTS_STATUS_INVALID_PARAMETERS
)
560 host_to_le16(WLAN_STATUS_INVALID_PARAMETERS
);
561 else if (res
== WMM_ADDTS_STATUS_REFUSED
)
563 host_to_le16(WLAN_STATUS_REQUEST_DECLINED
);
565 /* TSPEC accepted; include updated TSPEC in response */
566 rdie
->descr_count
= 1;
567 pos
+= sizeof(*tspec
);
571 #endif /* NEED_AP_MLME */
573 wpa_printf(MSG_DEBUG
, "FT: No supported resource requested");
574 rdie
->status_code
= host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE
);
579 static u8
* wpa_ft_process_ric(u8
*pos
, u8
*end
, const u8
*ric
, size_t ric_len
)
581 const u8
*rpos
, *start
;
582 const struct rsn_rdie
*rdie
;
584 wpa_hexdump(MSG_MSGDUMP
, "FT: RIC Request", ric
, ric_len
);
587 while (rpos
+ sizeof(*rdie
) < ric
+ ric_len
) {
588 if (rpos
[0] != WLAN_EID_RIC_DATA
|| rpos
[1] < sizeof(*rdie
) ||
589 rpos
+ 2 + rpos
[1] > ric
+ ric_len
)
591 rdie
= (const struct rsn_rdie
*) (rpos
+ 2);
595 while (rpos
+ 2 <= ric
+ ric_len
&&
596 rpos
+ 2 + rpos
[1] <= ric
+ ric_len
) {
597 if (rpos
[0] == WLAN_EID_RIC_DATA
)
601 pos
= wpa_ft_process_rdie(pos
, end
, rdie
->id
,
603 start
, rpos
- start
);
610 u8
* wpa_sm_write_assoc_resp_ies(struct wpa_state_machine
*sm
, u8
*pos
,
611 size_t max_len
, int auth_alg
,
612 const u8
*req_ies
, size_t req_ies_len
)
614 u8
*end
, *mdie
, *ftie
, *rsnie
= NULL
, *r0kh_id
, *subelem
= NULL
;
615 size_t mdie_len
, ftie_len
, rsnie_len
= 0, r0kh_id_len
, subelem_len
= 0;
617 struct wpa_auth_config
*conf
;
618 struct rsn_ftie
*_ftie
;
619 struct wpa_ft_ies parse
;
626 conf
= &sm
->wpa_auth
->conf
;
628 if (sm
->wpa_key_mgmt
!= WPA_KEY_MGMT_FT_IEEE8021X
&&
629 sm
->wpa_key_mgmt
!= WPA_KEY_MGMT_FT_PSK
)
634 if (auth_alg
== WLAN_AUTH_FT
) {
636 * RSN (only present if this is a Reassociation Response and
637 * part of a fast BSS transition)
639 res
= wpa_write_rsn_ie(conf
, pos
, end
- pos
, sm
->pmk_r1_name
);
647 /* Mobility Domain Information */
648 res
= wpa_write_mdie(conf
, pos
, end
- pos
);
655 /* Fast BSS Transition Information */
656 if (auth_alg
== WLAN_AUTH_FT
) {
657 subelem
= wpa_ft_gtk_subelem(sm
, &subelem_len
);
658 r0kh_id
= sm
->r0kh_id
;
659 r0kh_id_len
= sm
->r0kh_id_len
;
662 #ifdef CONFIG_IEEE80211W
663 if (sm
->mgmt_frame_prot
) {
667 igtk
= wpa_ft_igtk_subelem(sm
, &igtk_len
);
672 nbuf
= os_realloc(subelem
, subelem_len
+ igtk_len
);
679 os_memcpy(subelem
+ subelem_len
, igtk
, igtk_len
);
680 subelem_len
+= igtk_len
;
683 #endif /* CONFIG_IEEE80211W */
685 r0kh_id
= conf
->r0_key_holder
;
686 r0kh_id_len
= conf
->r0_key_holder_len
;
690 res
= wpa_write_ftie(conf
, r0kh_id
, r0kh_id_len
, anonce
, snonce
, pos
,
691 end
- pos
, subelem
, subelem_len
);
699 os_free(sm
->assoc_resp_ftie
);
700 sm
->assoc_resp_ftie
= os_malloc(ftie_len
);
701 if (sm
->assoc_resp_ftie
)
702 os_memcpy(sm
->assoc_resp_ftie
, ftie
, ftie_len
);
704 _ftie
= (struct rsn_ftie
*) (ftie
+ 2);
705 if (auth_alg
== WLAN_AUTH_FT
)
706 _ftie
->mic_control
[1] = 3; /* Information element count */
709 if (wpa_ft_parse_ies(req_ies
, req_ies_len
, &parse
) == 0 && parse
.ric
) {
710 pos
= wpa_ft_process_ric(pos
, end
, parse
.ric
, parse
.ric_len
);
711 if (auth_alg
== WLAN_AUTH_FT
)
712 _ftie
->mic_control
[1] +=
713 ieee802_11_ie_count(ric_start
,
716 if (ric_start
== pos
)
719 if (auth_alg
== WLAN_AUTH_FT
&&
720 wpa_ft_mic(sm
->PTK
.kck
, sm
->addr
, sm
->wpa_auth
->addr
, 6,
721 mdie
, mdie_len
, ftie
, ftie_len
,
723 ric_start
, ric_start
? pos
- ric_start
: 0,
725 wpa_printf(MSG_DEBUG
, "FT: Failed to calculate MIC");
731 static int wpa_ft_parse_ftie(const u8
*ie
, size_t ie_len
,
732 struct wpa_ft_ies
*parse
)
737 parse
->ftie_len
= ie_len
;
739 pos
= ie
+ sizeof(struct rsn_ftie
);
742 while (pos
+ 2 <= end
&& pos
+ 2 + pos
[1] <= end
) {
744 case FTIE_SUBELEM_R1KH_ID
:
745 if (pos
[1] != FT_R1KH_ID_LEN
) {
746 wpa_printf(MSG_DEBUG
, "FT: Invalid R1KH-ID "
747 "length in FTIE: %d", pos
[1]);
750 parse
->r1kh_id
= pos
+ 2;
752 case FTIE_SUBELEM_GTK
:
753 parse
->gtk
= pos
+ 2;
754 parse
->gtk_len
= pos
[1];
756 case FTIE_SUBELEM_R0KH_ID
:
757 if (pos
[1] < 1 || pos
[1] > FT_R0KH_ID_MAX_LEN
) {
758 wpa_printf(MSG_DEBUG
, "FT: Invalid R0KH-ID "
759 "length in FTIE: %d", pos
[1]);
762 parse
->r0kh_id
= pos
+ 2;
763 parse
->r0kh_id_len
= pos
[1];
774 static int wpa_ft_parse_ies(const u8
*ies
, size_t ies_len
,
775 struct wpa_ft_ies
*parse
)
778 struct wpa_ie_data data
;
780 const struct rsn_ftie
*ftie
;
781 int prot_ie_count
= 0;
783 os_memset(parse
, 0, sizeof(*parse
));
789 while (pos
+ 2 <= end
&& pos
+ 2 + pos
[1] <= end
) {
792 parse
->rsn
= pos
+ 2;
793 parse
->rsn_len
= pos
[1];
794 ret
= wpa_parse_wpa_ie_rsn(parse
->rsn
- 2,
798 wpa_printf(MSG_DEBUG
, "FT: Failed to parse "
802 if (data
.num_pmkid
== 1 && data
.pmkid
)
803 parse
->rsn_pmkid
= data
.pmkid
;
805 case WLAN_EID_MOBILITY_DOMAIN
:
806 parse
->mdie
= pos
+ 2;
807 parse
->mdie_len
= pos
[1];
809 case WLAN_EID_FAST_BSS_TRANSITION
:
810 if (pos
[1] < sizeof(*ftie
))
812 ftie
= (const struct rsn_ftie
*) (pos
+ 2);
813 prot_ie_count
= ftie
->mic_control
[1];
814 if (wpa_ft_parse_ftie(pos
+ 2, pos
[1], parse
) < 0)
817 case WLAN_EID_RIC_DATA
:
818 if (parse
->ric
== NULL
)
825 if (prot_ie_count
== 0)
826 return 0; /* no MIC */
829 * Check that the protected IE count matches with IEs included in the
838 if (prot_ie_count
< 0) {
839 wpa_printf(MSG_DEBUG
, "FT: Some required IEs not included in "
840 "the protected IE count");
844 if (prot_ie_count
== 0 && parse
->ric
) {
845 wpa_printf(MSG_DEBUG
, "FT: RIC IE(s) in the frame, but not "
846 "included in protected IE count");
850 /* Determine the end of the RIC IE(s) */
852 while (pos
&& pos
+ 2 <= end
&& pos
+ 2 + pos
[1] <= end
&&
857 parse
->ric_len
= pos
- parse
->ric
;
859 wpa_printf(MSG_DEBUG
, "FT: %d protected IEs missing from "
860 "frame", (int) prot_ie_count
);
868 static inline int wpa_auth_set_key(struct wpa_authenticator
*wpa_auth
,
870 enum wpa_alg alg
, const u8
*addr
, int idx
,
871 u8
*key
, size_t key_len
)
873 if (wpa_auth
->cb
.set_key
== NULL
)
875 return wpa_auth
->cb
.set_key(wpa_auth
->cb
.ctx
, vlan_id
, alg
, addr
, idx
,
880 void wpa_ft_install_ptk(struct wpa_state_machine
*sm
)
885 /* MLME-SETKEYS.request(PTK) */
886 if (sm
->pairwise
== WPA_CIPHER_TKIP
) {
889 } else if (sm
->pairwise
== WPA_CIPHER_CCMP
) {
893 wpa_printf(MSG_DEBUG
, "FT: Unknown pairwise alg 0x%x - skip "
894 "PTK configuration", sm
->pairwise
);
898 /* FIX: add STA entry to kernel/driver here? The set_key will fail
899 * most likely without this.. At the moment, STA entry is added only
900 * after association has been completed. This function will be called
901 * again after association to get the PTK configured, but that could be
902 * optimized by adding the STA entry earlier.
904 if (wpa_auth_set_key(sm
->wpa_auth
, 0, alg
, sm
->addr
, 0,
908 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
909 sm
->pairwise_set
= TRUE
;
913 static u16
wpa_ft_process_auth_req(struct wpa_state_machine
*sm
,
914 const u8
*ies
, size_t ies_len
,
915 u8
**resp_ies
, size_t *resp_ies_len
)
917 struct rsn_mdie
*mdie
;
918 struct rsn_ftie
*ftie
;
919 u8 pmk_r1
[PMK_LEN
], pmk_r1_name
[WPA_PMK_NAME_LEN
];
920 u8 ptk_name
[WPA_PMK_NAME_LEN
];
921 struct wpa_auth_config
*conf
;
922 struct wpa_ft_ies parse
;
923 size_t buflen
, ptk_len
;
931 sm
->pmk_r1_name_valid
= 0;
932 conf
= &sm
->wpa_auth
->conf
;
934 wpa_hexdump(MSG_DEBUG
, "FT: Received authentication frame IEs",
937 if (wpa_ft_parse_ies(ies
, ies_len
, &parse
) < 0) {
938 wpa_printf(MSG_DEBUG
, "FT: Failed to parse FT IEs");
939 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
942 mdie
= (struct rsn_mdie
*) parse
.mdie
;
943 if (mdie
== NULL
|| parse
.mdie_len
< sizeof(*mdie
) ||
944 os_memcmp(mdie
->mobility_domain
,
945 sm
->wpa_auth
->conf
.mobility_domain
,
946 MOBILITY_DOMAIN_ID_LEN
) != 0) {
947 wpa_printf(MSG_DEBUG
, "FT: Invalid MDIE");
948 return WLAN_STATUS_INVALID_MDIE
;
951 ftie
= (struct rsn_ftie
*) parse
.ftie
;
952 if (ftie
== NULL
|| parse
.ftie_len
< sizeof(*ftie
)) {
953 wpa_printf(MSG_DEBUG
, "FT: Invalid FTIE");
954 return WLAN_STATUS_INVALID_FTIE
;
957 os_memcpy(sm
->SNonce
, ftie
->snonce
, WPA_NONCE_LEN
);
959 if (parse
.r0kh_id
== NULL
) {
960 wpa_printf(MSG_DEBUG
, "FT: Invalid FTIE - no R0KH-ID");
961 return WLAN_STATUS_INVALID_FTIE
;
964 wpa_hexdump(MSG_DEBUG
, "FT: STA R0KH-ID",
965 parse
.r0kh_id
, parse
.r0kh_id_len
);
966 os_memcpy(sm
->r0kh_id
, parse
.r0kh_id
, parse
.r0kh_id_len
);
967 sm
->r0kh_id_len
= parse
.r0kh_id_len
;
969 if (parse
.rsn_pmkid
== NULL
) {
970 wpa_printf(MSG_DEBUG
, "FT: No PMKID in RSNIE");
971 return WLAN_STATUS_INVALID_PMKID
;
974 wpa_hexdump(MSG_DEBUG
, "FT: Requested PMKR0Name",
975 parse
.rsn_pmkid
, WPA_PMK_NAME_LEN
);
976 wpa_derive_pmk_r1_name(parse
.rsn_pmkid
,
977 sm
->wpa_auth
->conf
.r1_key_holder
, sm
->addr
,
979 wpa_hexdump(MSG_DEBUG
, "FT: Derived requested PMKR1Name",
980 pmk_r1_name
, WPA_PMK_NAME_LEN
);
982 if (wpa_ft_fetch_pmk_r1(sm
->wpa_auth
, sm
->addr
, pmk_r1_name
, pmk_r1
,
984 if (wpa_ft_pull_pmk_r1(sm
->wpa_auth
, sm
->addr
, sm
->r0kh_id
,
985 sm
->r0kh_id_len
, parse
.rsn_pmkid
) < 0) {
986 wpa_printf(MSG_DEBUG
, "FT: Did not have matching "
987 "PMK-R1 and unknown R0KH-ID");
988 return WLAN_STATUS_INVALID_PMKID
;
992 * TODO: Should return "status pending" (and the caller should
993 * not send out response now). The real response will be sent
994 * once the response from R0KH is received.
996 return WLAN_STATUS_INVALID_PMKID
;
999 wpa_hexdump_key(MSG_DEBUG
, "FT: Selected PMK-R1", pmk_r1
, PMK_LEN
);
1000 sm
->pmk_r1_name_valid
= 1;
1001 os_memcpy(sm
->pmk_r1_name
, pmk_r1_name
, WPA_PMK_NAME_LEN
);
1003 if (random_get_bytes(sm
->ANonce
, WPA_NONCE_LEN
)) {
1004 wpa_printf(MSG_DEBUG
, "FT: Failed to get random data for "
1006 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
1009 wpa_hexdump(MSG_DEBUG
, "FT: Received SNonce",
1010 sm
->SNonce
, WPA_NONCE_LEN
);
1011 wpa_hexdump(MSG_DEBUG
, "FT: Generated ANonce",
1012 sm
->ANonce
, WPA_NONCE_LEN
);
1014 ptk_len
= pairwise
!= WPA_CIPHER_CCMP
? 64 : 48;
1015 wpa_pmk_r1_to_ptk(pmk_r1
, sm
->SNonce
, sm
->ANonce
, sm
->addr
,
1016 sm
->wpa_auth
->addr
, pmk_r1_name
,
1017 (u8
*) &sm
->PTK
, ptk_len
, ptk_name
);
1018 wpa_hexdump_key(MSG_DEBUG
, "FT: PTK",
1019 (u8
*) &sm
->PTK
, ptk_len
);
1020 wpa_hexdump(MSG_DEBUG
, "FT: PTKName", ptk_name
, WPA_PMK_NAME_LEN
);
1022 sm
->pairwise
= pairwise
;
1023 wpa_ft_install_ptk(sm
);
1025 buflen
= 2 + sizeof(struct rsn_mdie
) + 2 + sizeof(struct rsn_ftie
) +
1026 2 + FT_R1KH_ID_LEN
+ 200;
1027 *resp_ies
= os_zalloc(buflen
);
1028 if (*resp_ies
== NULL
) {
1029 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
1033 end
= *resp_ies
+ buflen
;
1035 ret
= wpa_write_rsn_ie(conf
, pos
, end
- pos
, parse
.rsn_pmkid
);
1039 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
1043 ret
= wpa_write_mdie(conf
, pos
, end
- pos
);
1047 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
1051 ret
= wpa_write_ftie(conf
, parse
.r0kh_id
, parse
.r0kh_id_len
,
1052 sm
->ANonce
, sm
->SNonce
, pos
, end
- pos
, NULL
, 0);
1056 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
1060 *resp_ies_len
= pos
- *resp_ies
;
1062 return WLAN_STATUS_SUCCESS
;
1066 void wpa_ft_process_auth(struct wpa_state_machine
*sm
, const u8
*bssid
,
1067 u16 auth_transaction
, const u8
*ies
, size_t ies_len
,
1068 void (*cb
)(void *ctx
, const u8
*dst
, const u8
*bssid
,
1069 u16 auth_transaction
, u16 status
,
1070 const u8
*ies
, size_t ies_len
),
1075 size_t resp_ies_len
;
1078 wpa_printf(MSG_DEBUG
, "FT: Received authentication frame, but "
1079 "WPA SM not available");
1083 wpa_printf(MSG_DEBUG
, "FT: Received authentication frame: STA=" MACSTR
1084 " BSSID=" MACSTR
" transaction=%d",
1085 MAC2STR(sm
->addr
), MAC2STR(bssid
), auth_transaction
);
1086 status
= wpa_ft_process_auth_req(sm
, ies
, ies_len
, &resp_ies
,
1089 wpa_printf(MSG_DEBUG
, "FT: FT authentication response: dst=" MACSTR
1090 " auth_transaction=%d status=%d",
1091 MAC2STR(sm
->addr
), auth_transaction
+ 1, status
);
1092 wpa_hexdump(MSG_DEBUG
, "FT: Response IEs", resp_ies
, resp_ies_len
);
1093 cb(ctx
, sm
->addr
, bssid
, auth_transaction
+ 1, status
,
1094 resp_ies
, resp_ies_len
);
1099 u16
wpa_ft_validate_reassoc(struct wpa_state_machine
*sm
, const u8
*ies
,
1102 struct wpa_ft_ies parse
;
1103 struct rsn_mdie
*mdie
;
1104 struct rsn_ftie
*ftie
;
1109 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
1111 wpa_hexdump(MSG_DEBUG
, "FT: Reassoc Req IEs", ies
, ies_len
);
1113 if (wpa_ft_parse_ies(ies
, ies_len
, &parse
) < 0) {
1114 wpa_printf(MSG_DEBUG
, "FT: Failed to parse FT IEs");
1115 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
1118 if (parse
.rsn
== NULL
) {
1119 wpa_printf(MSG_DEBUG
, "FT: No RSNIE in Reassoc Req");
1120 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
1123 if (parse
.rsn_pmkid
== NULL
) {
1124 wpa_printf(MSG_DEBUG
, "FT: No PMKID in RSNIE");
1125 return WLAN_STATUS_INVALID_PMKID
;
1128 if (os_memcmp(parse
.rsn_pmkid
, sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
) != 0)
1130 wpa_printf(MSG_DEBUG
, "FT: PMKID in Reassoc Req did not match "
1131 "with the PMKR1Name derived from auth request");
1132 return WLAN_STATUS_INVALID_PMKID
;
1135 mdie
= (struct rsn_mdie
*) parse
.mdie
;
1136 if (mdie
== NULL
|| parse
.mdie_len
< sizeof(*mdie
) ||
1137 os_memcmp(mdie
->mobility_domain
,
1138 sm
->wpa_auth
->conf
.mobility_domain
,
1139 MOBILITY_DOMAIN_ID_LEN
) != 0) {
1140 wpa_printf(MSG_DEBUG
, "FT: Invalid MDIE");
1141 return WLAN_STATUS_INVALID_MDIE
;
1144 ftie
= (struct rsn_ftie
*) parse
.ftie
;
1145 if (ftie
== NULL
|| parse
.ftie_len
< sizeof(*ftie
)) {
1146 wpa_printf(MSG_DEBUG
, "FT: Invalid FTIE");
1147 return WLAN_STATUS_INVALID_FTIE
;
1150 if (os_memcmp(ftie
->snonce
, sm
->SNonce
, WPA_NONCE_LEN
) != 0) {
1151 wpa_printf(MSG_DEBUG
, "FT: SNonce mismatch in FTIE");
1152 wpa_hexdump(MSG_DEBUG
, "FT: Received SNonce",
1153 ftie
->snonce
, WPA_NONCE_LEN
);
1154 wpa_hexdump(MSG_DEBUG
, "FT: Expected SNonce",
1155 sm
->SNonce
, WPA_NONCE_LEN
);
1159 if (os_memcmp(ftie
->anonce
, sm
->ANonce
, WPA_NONCE_LEN
) != 0) {
1160 wpa_printf(MSG_DEBUG
, "FT: ANonce mismatch in FTIE");
1161 wpa_hexdump(MSG_DEBUG
, "FT: Received ANonce",
1162 ftie
->anonce
, WPA_NONCE_LEN
);
1163 wpa_hexdump(MSG_DEBUG
, "FT: Expected ANonce",
1164 sm
->ANonce
, WPA_NONCE_LEN
);
1169 if (parse
.r0kh_id
== NULL
) {
1170 wpa_printf(MSG_DEBUG
, "FT: No R0KH-ID subelem in FTIE");
1174 if (parse
.r0kh_id_len
!= sm
->r0kh_id_len
||
1175 os_memcmp(parse
.r0kh_id
, sm
->r0kh_id
, parse
.r0kh_id_len
) != 0) {
1176 wpa_printf(MSG_DEBUG
, "FT: R0KH-ID in FTIE did not match with "
1177 "the current R0KH-ID");
1178 wpa_hexdump(MSG_DEBUG
, "FT: R0KH-ID in FTIE",
1179 parse
.r0kh_id
, parse
.r0kh_id_len
);
1180 wpa_hexdump(MSG_DEBUG
, "FT: The current R0KH-ID",
1181 sm
->r0kh_id
, sm
->r0kh_id_len
);
1185 if (parse
.r1kh_id
== NULL
) {
1186 wpa_printf(MSG_DEBUG
, "FT: No R1KH-ID subelem in FTIE");
1190 if (os_memcmp(parse
.r1kh_id
, sm
->wpa_auth
->conf
.r1_key_holder
,
1191 FT_R1KH_ID_LEN
) != 0) {
1192 wpa_printf(MSG_DEBUG
, "FT: Unknown R1KH-ID used in "
1194 wpa_hexdump(MSG_DEBUG
, "FT: R1KH-ID in FTIE",
1195 parse
.r1kh_id
, FT_R1KH_ID_LEN
);
1196 wpa_hexdump(MSG_DEBUG
, "FT: Expected R1KH-ID",
1197 sm
->wpa_auth
->conf
.r1_key_holder
, FT_R1KH_ID_LEN
);
1201 if (parse
.rsn_pmkid
== NULL
||
1202 os_memcmp(parse
.rsn_pmkid
, sm
->pmk_r1_name
, WPA_PMK_NAME_LEN
)) {
1203 wpa_printf(MSG_DEBUG
, "FT: No matching PMKR1Name (PMKID) in "
1204 "RSNIE (pmkid=%d)", !!parse
.rsn_pmkid
);
1211 if (ftie
->mic_control
[1] != count
) {
1212 wpa_printf(MSG_DEBUG
, "FT: Unexpected IE count in MIC "
1213 "Control: received %u expected %u",
1214 ftie
->mic_control
[1], count
);
1218 if (wpa_ft_mic(sm
->PTK
.kck
, sm
->addr
, sm
->wpa_auth
->addr
, 5,
1219 parse
.mdie
- 2, parse
.mdie_len
+ 2,
1220 parse
.ftie
- 2, parse
.ftie_len
+ 2,
1221 parse
.rsn
- 2, parse
.rsn_len
+ 2,
1222 parse
.ric
, parse
.ric_len
,
1224 wpa_printf(MSG_DEBUG
, "FT: Failed to calculate MIC");
1225 return WLAN_STATUS_UNSPECIFIED_FAILURE
;
1228 if (os_memcmp(mic
, ftie
->mic
, 16) != 0) {
1229 wpa_printf(MSG_DEBUG
, "FT: Invalid MIC in FTIE");
1230 wpa_hexdump(MSG_MSGDUMP
, "FT: Received MIC", ftie
->mic
, 16);
1231 wpa_hexdump(MSG_MSGDUMP
, "FT: Calculated MIC", mic
, 16);
1232 return WLAN_STATUS_INVALID_FTIE
;
1235 return WLAN_STATUS_SUCCESS
;
1239 int wpa_ft_action_rx(struct wpa_state_machine
*sm
, const u8
*data
, size_t len
)
1241 const u8
*sta_addr
, *target_ap
;
1245 struct ft_rrb_frame
*frame
;
1251 * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
1252 * FT Request action frame body[variable]
1256 wpa_printf(MSG_DEBUG
, "FT: Too short FT Action frame "
1257 "(len=%lu)", (unsigned long) len
);
1262 sta_addr
= data
+ 2;
1263 target_ap
= data
+ 8;
1267 wpa_printf(MSG_DEBUG
, "FT: Received FT Action frame (STA=" MACSTR
1268 " Target AP=" MACSTR
" Action=%d)",
1269 MAC2STR(sta_addr
), MAC2STR(target_ap
), action
);
1271 if (os_memcmp(sta_addr
, sm
->addr
, ETH_ALEN
) != 0) {
1272 wpa_printf(MSG_DEBUG
, "FT: Mismatch in FT Action STA address: "
1273 "STA=" MACSTR
" STA-Address=" MACSTR
,
1274 MAC2STR(sm
->addr
), MAC2STR(sta_addr
));
1279 * Do some sanity checking on the target AP address (not own and not
1280 * broadcast. This could be extended to filter based on a list of known
1281 * APs in the MD (if such a list were configured).
1283 if ((target_ap
[0] & 0x01) ||
1284 os_memcmp(target_ap
, sm
->wpa_auth
->addr
, ETH_ALEN
) == 0) {
1285 wpa_printf(MSG_DEBUG
, "FT: Invalid Target AP in FT Action "
1290 wpa_hexdump(MSG_MSGDUMP
, "FT: Action frame body", ies
, ies_len
);
1292 /* RRB - Forward action frame to the target AP */
1293 frame
= os_malloc(sizeof(*frame
) + len
);
1294 frame
->frame_type
= RSN_REMOTE_FRAME_TYPE_FT_RRB
;
1295 frame
->packet_type
= FT_PACKET_REQUEST
;
1296 frame
->action_length
= host_to_le16(len
);
1297 os_memcpy(frame
->ap_address
, sm
->wpa_auth
->addr
, ETH_ALEN
);
1298 os_memcpy(frame
+ 1, data
, len
);
1300 wpa_ft_rrb_send(sm
->wpa_auth
, target_ap
, (u8
*) frame
,
1301 sizeof(*frame
) + len
);
1308 static int wpa_ft_rrb_rx_request(struct wpa_authenticator
*wpa_auth
,
1309 const u8
*current_ap
, const u8
*sta_addr
,
1310 const u8
*body
, size_t len
)
1312 struct wpa_state_machine
*sm
;
1315 size_t resp_ies_len
, rlen
;
1316 struct ft_rrb_frame
*frame
;
1318 sm
= wpa_ft_add_sta(wpa_auth
, sta_addr
);
1320 wpa_printf(MSG_DEBUG
, "FT: Failed to add new STA based on "
1325 wpa_hexdump(MSG_MSGDUMP
, "FT: RRB Request Frame body", body
, len
);
1327 status
= wpa_ft_process_auth_req(sm
, body
, len
, &resp_ies
,
1330 wpa_printf(MSG_DEBUG
, "FT: RRB authentication response: STA=" MACSTR
1331 " CurrentAP=" MACSTR
" status=%d",
1332 MAC2STR(sm
->addr
), MAC2STR(current_ap
), status
);
1333 wpa_hexdump(MSG_DEBUG
, "FT: Response IEs", resp_ies
, resp_ies_len
);
1335 /* RRB - Forward action frame response to the Current AP */
1338 * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
1339 * Status_Code[2] FT Request action frame body[variable]
1341 rlen
= 2 + 2 * ETH_ALEN
+ 2 + resp_ies_len
;
1343 frame
= os_malloc(sizeof(*frame
) + rlen
);
1344 frame
->frame_type
= RSN_REMOTE_FRAME_TYPE_FT_RRB
;
1345 frame
->packet_type
= FT_PACKET_RESPONSE
;
1346 frame
->action_length
= host_to_le16(rlen
);
1347 os_memcpy(frame
->ap_address
, wpa_auth
->addr
, ETH_ALEN
);
1348 pos
= (u8
*) (frame
+ 1);
1349 *pos
++ = WLAN_ACTION_FT
;
1350 *pos
++ = 2; /* Action: Response */
1351 os_memcpy(pos
, sta_addr
, ETH_ALEN
);
1353 os_memcpy(pos
, wpa_auth
->addr
, ETH_ALEN
);
1355 WPA_PUT_LE16(pos
, status
);
1358 os_memcpy(pos
, resp_ies
, resp_ies_len
);
1362 wpa_ft_rrb_send(wpa_auth
, current_ap
, (u8
*) frame
,
1363 sizeof(*frame
) + rlen
);
1370 static int wpa_ft_rrb_rx_pull(struct wpa_authenticator
*wpa_auth
,
1372 const u8
*data
, size_t data_len
)
1374 struct ft_r0kh_r1kh_pull_frame
*frame
, f
;
1375 struct ft_remote_r1kh
*r1kh
;
1376 struct ft_r0kh_r1kh_resp_frame resp
, r
;
1380 wpa_printf(MSG_DEBUG
, "FT: Received PMK-R1 pull");
1382 if (data_len
< sizeof(*frame
))
1385 r1kh
= wpa_auth
->conf
.r1kh_list
;
1387 if (os_memcmp(r1kh
->addr
, src_addr
, ETH_ALEN
) == 0)
1392 wpa_printf(MSG_DEBUG
, "FT: No matching R1KH address found for "
1393 "PMK-R1 pull source address " MACSTR
,
1398 frame
= (struct ft_r0kh_r1kh_pull_frame
*) data
;
1399 /* aes_unwrap() does not support inplace decryption, so use a temporary
1400 * buffer for the data. */
1401 if (aes_unwrap(r1kh
->key
, (FT_R0KH_R1KH_PULL_DATA_LEN
+ 7) / 8,
1402 frame
->nonce
, f
.nonce
) < 0) {
1403 wpa_printf(MSG_DEBUG
, "FT: Failed to decrypt PMK-R1 pull "
1404 "request from " MACSTR
, MAC2STR(src_addr
));
1408 wpa_hexdump(MSG_DEBUG
, "FT: PMK-R1 pull - nonce",
1409 f
.nonce
, sizeof(f
.nonce
));
1410 wpa_hexdump(MSG_DEBUG
, "FT: PMK-R1 pull - PMKR0Name",
1411 f
.pmk_r0_name
, WPA_PMK_NAME_LEN
);
1412 wpa_printf(MSG_DEBUG
, "FT: PMK-R1 pull - R1KH-ID=" MACSTR
"S1KH-ID="
1413 MACSTR
, MAC2STR(f
.r1kh_id
), MAC2STR(f
.s1kh_id
));
1415 os_memset(&resp
, 0, sizeof(resp
));
1416 resp
.frame_type
= RSN_REMOTE_FRAME_TYPE_FT_RRB
;
1417 resp
.packet_type
= FT_PACKET_R0KH_R1KH_RESP
;
1418 resp
.data_length
= host_to_le16(FT_R0KH_R1KH_RESP_DATA_LEN
);
1419 os_memcpy(resp
.ap_address
, wpa_auth
->addr
, ETH_ALEN
);
1421 /* aes_wrap() does not support inplace encryption, so use a temporary
1422 * buffer for the data. */
1423 os_memcpy(r
.nonce
, f
.nonce
, sizeof(f
.nonce
));
1424 os_memcpy(r
.r1kh_id
, f
.r1kh_id
, FT_R1KH_ID_LEN
);
1425 os_memcpy(r
.s1kh_id
, f
.s1kh_id
, ETH_ALEN
);
1426 if (wpa_ft_fetch_pmk_r0(wpa_auth
, f
.s1kh_id
, f
.pmk_r0_name
, pmk_r0
,
1428 wpa_printf(MSG_DEBUG
, "FT: No matching PMKR0Name found for "
1433 wpa_derive_pmk_r1(pmk_r0
, f
.pmk_r0_name
, f
.r1kh_id
, f
.s1kh_id
,
1434 r
.pmk_r1
, r
.pmk_r1_name
);
1435 wpa_hexdump_key(MSG_DEBUG
, "FT: PMK-R1", r
.pmk_r1
, PMK_LEN
);
1436 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name", r
.pmk_r1_name
,
1438 r
.pairwise
= host_to_le16(pairwise
);
1440 if (aes_wrap(r1kh
->key
, (FT_R0KH_R1KH_RESP_DATA_LEN
+ 7) / 8,
1441 r
.nonce
, resp
.nonce
) < 0) {
1442 os_memset(pmk_r0
, 0, PMK_LEN
);
1446 os_memset(pmk_r0
, 0, PMK_LEN
);
1448 wpa_ft_rrb_send(wpa_auth
, src_addr
, (u8
*) &resp
, sizeof(resp
));
1454 static int wpa_ft_rrb_rx_resp(struct wpa_authenticator
*wpa_auth
,
1456 const u8
*data
, size_t data_len
)
1458 struct ft_r0kh_r1kh_resp_frame
*frame
, f
;
1459 struct ft_remote_r0kh
*r0kh
;
1462 wpa_printf(MSG_DEBUG
, "FT: Received PMK-R1 pull response");
1464 if (data_len
< sizeof(*frame
))
1467 r0kh
= wpa_auth
->conf
.r0kh_list
;
1469 if (os_memcmp(r0kh
->addr
, src_addr
, ETH_ALEN
) == 0)
1474 wpa_printf(MSG_DEBUG
, "FT: No matching R0KH address found for "
1475 "PMK-R0 pull response source address " MACSTR
,
1480 frame
= (struct ft_r0kh_r1kh_resp_frame
*) data
;
1481 /* aes_unwrap() does not support inplace decryption, so use a temporary
1482 * buffer for the data. */
1483 if (aes_unwrap(r0kh
->key
, (FT_R0KH_R1KH_RESP_DATA_LEN
+ 7) / 8,
1484 frame
->nonce
, f
.nonce
) < 0) {
1485 wpa_printf(MSG_DEBUG
, "FT: Failed to decrypt PMK-R1 pull "
1486 "response from " MACSTR
, MAC2STR(src_addr
));
1490 if (os_memcmp(f
.r1kh_id
, wpa_auth
->conf
.r1_key_holder
, FT_R1KH_ID_LEN
)
1492 wpa_printf(MSG_DEBUG
, "FT: PMK-R1 pull response did not use a "
1493 "matching R1KH-ID");
1497 /* TODO: verify that <nonce,s1kh_id> matches with a pending request
1498 * and call this requests callback function to finish request
1501 pairwise
= le_to_host16(f
.pairwise
);
1502 wpa_hexdump(MSG_DEBUG
, "FT: PMK-R1 pull - nonce",
1503 f
.nonce
, sizeof(f
.nonce
));
1504 wpa_printf(MSG_DEBUG
, "FT: PMK-R1 pull - R1KH-ID=" MACSTR
"S1KH-ID="
1505 MACSTR
" pairwise=0x%x",
1506 MAC2STR(f
.r1kh_id
), MAC2STR(f
.s1kh_id
), pairwise
);
1507 wpa_hexdump_key(MSG_DEBUG
, "FT: PMK-R1 pull - PMK-R1",
1509 wpa_hexdump(MSG_DEBUG
, "FT: PMK-R1 pull - PMKR1Name",
1510 f
.pmk_r1_name
, WPA_PMK_NAME_LEN
);
1512 wpa_ft_store_pmk_r1(wpa_auth
, f
.s1kh_id
, f
.pmk_r1
, f
.pmk_r1_name
,
1514 os_memset(f
.pmk_r1
, 0, PMK_LEN
);
1520 static int wpa_ft_rrb_rx_push(struct wpa_authenticator
*wpa_auth
,
1522 const u8
*data
, size_t data_len
)
1524 struct ft_r0kh_r1kh_push_frame
*frame
, f
;
1525 struct ft_remote_r0kh
*r0kh
;
1530 wpa_printf(MSG_DEBUG
, "FT: Received PMK-R1 push");
1532 if (data_len
< sizeof(*frame
))
1535 r0kh
= wpa_auth
->conf
.r0kh_list
;
1537 if (os_memcmp(r0kh
->addr
, src_addr
, ETH_ALEN
) == 0)
1542 wpa_printf(MSG_DEBUG
, "FT: No matching R0KH address found for "
1543 "PMK-R0 push source address " MACSTR
,
1548 frame
= (struct ft_r0kh_r1kh_push_frame
*) data
;
1549 /* aes_unwrap() does not support inplace decryption, so use a temporary
1550 * buffer for the data. */
1551 if (aes_unwrap(r0kh
->key
, (FT_R0KH_R1KH_PUSH_DATA_LEN
+ 7) / 8,
1552 frame
->timestamp
, f
.timestamp
) < 0) {
1553 wpa_printf(MSG_DEBUG
, "FT: Failed to decrypt PMK-R1 push from "
1554 MACSTR
, MAC2STR(src_addr
));
1559 tsend
= WPA_GET_LE32(f
.timestamp
);
1560 if ((now
.sec
> tsend
&& now
.sec
- tsend
> 60) ||
1561 (now
.sec
< tsend
&& tsend
- now
.sec
> 60)) {
1562 wpa_printf(MSG_DEBUG
, "FT: PMK-R1 push did not have a valid "
1563 "timestamp: sender time %d own time %d\n",
1564 (int) tsend
, (int) now
.sec
);
1568 if (os_memcmp(f
.r1kh_id
, wpa_auth
->conf
.r1_key_holder
, FT_R1KH_ID_LEN
)
1570 wpa_printf(MSG_DEBUG
, "FT: PMK-R1 push did not use a matching "
1571 "R1KH-ID (received " MACSTR
" own " MACSTR
")",
1573 MAC2STR(wpa_auth
->conf
.r1_key_holder
));
1577 pairwise
= le_to_host16(f
.pairwise
);
1578 wpa_printf(MSG_DEBUG
, "FT: PMK-R1 push - R1KH-ID=" MACSTR
" S1KH-ID="
1579 MACSTR
" pairwise=0x%x",
1580 MAC2STR(f
.r1kh_id
), MAC2STR(f
.s1kh_id
), pairwise
);
1581 wpa_hexdump_key(MSG_DEBUG
, "FT: PMK-R1 push - PMK-R1",
1583 wpa_hexdump(MSG_DEBUG
, "FT: PMK-R1 push - PMKR1Name",
1584 f
.pmk_r1_name
, WPA_PMK_NAME_LEN
);
1586 wpa_ft_store_pmk_r1(wpa_auth
, f
.s1kh_id
, f
.pmk_r1
, f
.pmk_r1_name
,
1588 os_memset(f
.pmk_r1
, 0, PMK_LEN
);
1594 int wpa_ft_rrb_rx(struct wpa_authenticator
*wpa_auth
, const u8
*src_addr
,
1595 const u8
*data
, size_t data_len
)
1597 struct ft_rrb_frame
*frame
;
1599 const u8
*pos
, *end
, *start
;
1601 const u8
*sta_addr
, *target_ap_addr
;
1603 wpa_printf(MSG_DEBUG
, "FT: RRB received frame from remote AP " MACSTR
,
1606 if (data_len
< sizeof(*frame
)) {
1607 wpa_printf(MSG_DEBUG
, "FT: Too short RRB frame (data_len=%lu)",
1608 (unsigned long) data_len
);
1613 frame
= (struct ft_rrb_frame
*) pos
;
1614 pos
+= sizeof(*frame
);
1616 alen
= le_to_host16(frame
->action_length
);
1617 wpa_printf(MSG_DEBUG
, "FT: RRB frame - frame_type=%d packet_type=%d "
1618 "action_length=%d ap_address=" MACSTR
,
1619 frame
->frame_type
, frame
->packet_type
, alen
,
1620 MAC2STR(frame
->ap_address
));
1622 if (frame
->frame_type
!= RSN_REMOTE_FRAME_TYPE_FT_RRB
) {
1623 /* Discard frame per IEEE Std 802.11r-2008, 11A.10.3 */
1624 wpa_printf(MSG_DEBUG
, "FT: RRB discarded frame with "
1625 "unrecognized type %d", frame
->frame_type
);
1629 if (alen
> data_len
- sizeof(*frame
)) {
1630 wpa_printf(MSG_DEBUG
, "FT: RRB frame too short for action "
1635 if (frame
->packet_type
== FT_PACKET_R0KH_R1KH_PULL
)
1636 return wpa_ft_rrb_rx_pull(wpa_auth
, src_addr
, data
, data_len
);
1637 if (frame
->packet_type
== FT_PACKET_R0KH_R1KH_RESP
)
1638 return wpa_ft_rrb_rx_resp(wpa_auth
, src_addr
, data
, data_len
);
1639 if (frame
->packet_type
== FT_PACKET_R0KH_R1KH_PUSH
)
1640 return wpa_ft_rrb_rx_push(wpa_auth
, src_addr
, data
, data_len
);
1642 wpa_hexdump(MSG_MSGDUMP
, "FT: RRB - FT Action frame", pos
, alen
);
1644 if (alen
< 1 + 1 + 2 * ETH_ALEN
) {
1645 wpa_printf(MSG_DEBUG
, "FT: Too short RRB frame (not enough "
1646 "room for Action Frame body); alen=%lu",
1647 (unsigned long) alen
);
1653 if (*pos
!= WLAN_ACTION_FT
) {
1654 wpa_printf(MSG_DEBUG
, "FT: Unexpected Action frame category "
1663 target_ap_addr
= pos
;
1665 wpa_printf(MSG_DEBUG
, "FT: RRB Action Frame: action=%d sta_addr="
1666 MACSTR
" target_ap_addr=" MACSTR
,
1667 action
, MAC2STR(sta_addr
), MAC2STR(target_ap_addr
));
1669 if (frame
->packet_type
== FT_PACKET_REQUEST
) {
1670 wpa_printf(MSG_DEBUG
, "FT: FT Packet Type - Request");
1673 wpa_printf(MSG_DEBUG
, "FT: Unexpected Action %d in "
1674 "RRB Request", action
);
1678 if (os_memcmp(target_ap_addr
, wpa_auth
->addr
, ETH_ALEN
) != 0) {
1679 wpa_printf(MSG_DEBUG
, "FT: Target AP address in the "
1680 "RRB Request does not match with own "
1685 if (wpa_ft_rrb_rx_request(wpa_auth
, frame
->ap_address
,
1686 sta_addr
, pos
, end
- pos
) < 0)
1688 } else if (frame
->packet_type
== FT_PACKET_RESPONSE
) {
1691 if (end
- pos
< 2) {
1692 wpa_printf(MSG_DEBUG
, "FT: Not enough room for status "
1693 "code in RRB Response");
1696 status_code
= WPA_GET_LE16(pos
);
1699 wpa_printf(MSG_DEBUG
, "FT: FT Packet Type - Response "
1700 "(status_code=%d)", status_code
);
1702 if (wpa_ft_action_send(wpa_auth
, sta_addr
, start
, alen
) < 0)
1705 wpa_printf(MSG_DEBUG
, "FT: RRB discarded frame with unknown "
1706 "packet_type %d", frame
->packet_type
);
1714 static void wpa_ft_generate_pmk_r1(struct wpa_authenticator
*wpa_auth
,
1715 struct wpa_ft_pmk_r0_sa
*pmk_r0
,
1716 struct ft_remote_r1kh
*r1kh
,
1717 const u8
*s1kh_id
, int pairwise
)
1719 struct ft_r0kh_r1kh_push_frame frame
, f
;
1722 os_memset(&frame
, 0, sizeof(frame
));
1723 frame
.frame_type
= RSN_REMOTE_FRAME_TYPE_FT_RRB
;
1724 frame
.packet_type
= FT_PACKET_R0KH_R1KH_PUSH
;
1725 frame
.data_length
= host_to_le16(FT_R0KH_R1KH_PUSH_DATA_LEN
);
1726 os_memcpy(frame
.ap_address
, wpa_auth
->addr
, ETH_ALEN
);
1728 /* aes_wrap() does not support inplace encryption, so use a temporary
1729 * buffer for the data. */
1730 os_memcpy(f
.r1kh_id
, r1kh
->id
, FT_R1KH_ID_LEN
);
1731 os_memcpy(f
.s1kh_id
, s1kh_id
, ETH_ALEN
);
1732 os_memcpy(f
.pmk_r0_name
, pmk_r0
->pmk_r0_name
, WPA_PMK_NAME_LEN
);
1733 wpa_derive_pmk_r1(pmk_r0
->pmk_r0
, pmk_r0
->pmk_r0_name
, r1kh
->id
,
1734 s1kh_id
, f
.pmk_r1
, f
.pmk_r1_name
);
1735 wpa_printf(MSG_DEBUG
, "FT: R1KH-ID " MACSTR
, MAC2STR(r1kh
->id
));
1736 wpa_hexdump_key(MSG_DEBUG
, "FT: PMK-R1", f
.pmk_r1
, PMK_LEN
);
1737 wpa_hexdump(MSG_DEBUG
, "FT: PMKR1Name", f
.pmk_r1_name
,
1740 WPA_PUT_LE32(f
.timestamp
, now
.sec
);
1741 f
.pairwise
= host_to_le16(pairwise
);
1742 if (aes_wrap(r1kh
->key
, (FT_R0KH_R1KH_PUSH_DATA_LEN
+ 7) / 8,
1743 f
.timestamp
, frame
.timestamp
) < 0)
1746 wpa_ft_rrb_send(wpa_auth
, r1kh
->addr
, (u8
*) &frame
, sizeof(frame
));
1750 void wpa_ft_push_pmk_r1(struct wpa_authenticator
*wpa_auth
, const u8
*addr
)
1752 struct wpa_ft_pmk_r0_sa
*r0
;
1753 struct ft_remote_r1kh
*r1kh
;
1755 if (!wpa_auth
->conf
.pmk_r1_push
)
1758 r0
= wpa_auth
->ft_pmk_cache
->pmk_r0
;
1760 if (os_memcmp(r0
->spa
, addr
, ETH_ALEN
) == 0)
1765 if (r0
== NULL
|| r0
->pmk_r1_pushed
)
1767 r0
->pmk_r1_pushed
= 1;
1769 wpa_printf(MSG_DEBUG
, "FT: Deriving and pushing PMK-R1 keys to R1KHs "
1770 "for STA " MACSTR
, MAC2STR(addr
));
1772 r1kh
= wpa_auth
->conf
.r1kh_list
;
1774 wpa_ft_generate_pmk_r1(wpa_auth
, r0
, r1kh
, addr
, r0
->pairwise
);
1779 #endif /* CONFIG_IEEE80211R */