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 "ap_config.h"
22 #include "ieee802_11.h"
25 #include "wpa_auth_i.h"
26 #include "wpa_auth_ie.h"
29 #ifdef CONFIG_IEEE80211R
49 static int wpa_ft_parse_ies(const u8 *ies, size_t ies_len,
50 struct wpa_ft_ies *parse);
53 static int wpa_ft_rrb_send(struct wpa_authenticator *wpa_auth, const u8 *dst,
54 const u8 *data, size_t data_len)
56 if (wpa_auth->cb.send_ether == NULL)
58 return wpa_auth->cb.send_ether(wpa_auth->cb.ctx, dst, ETH_P_RRB,
63 static int wpa_ft_action_send(struct wpa_authenticator *wpa_auth,
64 const u8 *dst, const u8 *data, size_t data_len)
66 if (wpa_auth->cb.send_ft_action == NULL)
68 return wpa_auth->cb.send_ft_action(wpa_auth->cb.ctx, dst,
73 static struct wpa_state_machine *
74 wpa_ft_add_sta(struct wpa_authenticator *wpa_auth, const u8 *sta_addr)
76 if (wpa_auth->cb.add_sta == NULL)
78 return wpa_auth->cb.add_sta(wpa_auth->cb.ctx, sta_addr);
82 int wpa_write_mdie(struct wpa_auth_config *conf, u8 *buf, size_t len)
86 if (len < 2 + sizeof(struct rsn_mdie))
89 *pos++ = WLAN_EID_MOBILITY_DOMAIN;
90 *pos++ = MOBILITY_DOMAIN_ID_LEN + 1;
91 os_memcpy(pos, conf->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
92 pos += MOBILITY_DOMAIN_ID_LEN;
93 capab = RSN_FT_CAPAB_FT_OVER_DS;
100 static int wpa_write_ftie(struct wpa_auth_config *conf, const u8 *r0kh_id,
102 const u8 *anonce, const u8 *snonce,
103 u8 *buf, size_t len, const u8 *subelem,
106 u8 *pos = buf, *ielen;
107 struct rsn_ftie *hdr;
109 if (len < 2 + sizeof(*hdr) + 2 + FT_R1KH_ID_LEN + 2 + r0kh_id_len +
113 *pos++ = WLAN_EID_FAST_BSS_TRANSITION;
116 hdr = (struct rsn_ftie *) pos;
117 os_memset(hdr, 0, sizeof(*hdr));
119 WPA_PUT_LE16(hdr->mic_control, 0);
121 os_memcpy(hdr->anonce, anonce, WPA_NONCE_LEN);
123 os_memcpy(hdr->snonce, snonce, WPA_NONCE_LEN);
125 /* Optional Parameters */
126 *pos++ = FTIE_SUBELEM_R1KH_ID;
127 *pos++ = FT_R1KH_ID_LEN;
128 os_memcpy(pos, conf->r1_key_holder, FT_R1KH_ID_LEN);
129 pos += FT_R1KH_ID_LEN;
132 *pos++ = FTIE_SUBELEM_R0KH_ID;
133 *pos++ = r0kh_id_len;
134 os_memcpy(pos, r0kh_id, r0kh_id_len);
139 os_memcpy(pos, subelem, subelem_len);
143 *ielen = pos - buf - 2;
149 struct wpa_ft_pmk_r0_sa {
150 struct wpa_ft_pmk_r0_sa *next;
152 u8 pmk_r0_name[WPA_PMK_NAME_LEN];
154 int pairwise; /* Pairwise cipher suite, WPA_CIPHER_* */
155 /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */
159 struct wpa_ft_pmk_r1_sa {
160 struct wpa_ft_pmk_r1_sa *next;
162 u8 pmk_r1_name[WPA_PMK_NAME_LEN];
164 int pairwise; /* Pairwise cipher suite, WPA_CIPHER_* */
165 /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */
168 struct wpa_ft_pmk_cache {
169 struct wpa_ft_pmk_r0_sa *pmk_r0;
170 struct wpa_ft_pmk_r1_sa *pmk_r1;
173 struct wpa_ft_pmk_cache * wpa_ft_pmk_cache_init(void)
175 struct wpa_ft_pmk_cache *cache;
177 cache = os_zalloc(sizeof(*cache));
183 void wpa_ft_pmk_cache_deinit(struct wpa_ft_pmk_cache *cache)
185 struct wpa_ft_pmk_r0_sa *r0, *r0prev;
186 struct wpa_ft_pmk_r1_sa *r1, *r1prev;
192 os_memset(r0prev->pmk_r0, 0, PMK_LEN);
200 os_memset(r1prev->pmk_r1, 0, PMK_LEN);
208 static int wpa_ft_store_pmk_r0(struct wpa_authenticator *wpa_auth,
209 const u8 *spa, const u8 *pmk_r0,
210 const u8 *pmk_r0_name, int pairwise)
212 struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
213 struct wpa_ft_pmk_r0_sa *r0;
215 /* TODO: add expiration and limit on number of entries in cache */
217 r0 = os_zalloc(sizeof(*r0));
221 os_memcpy(r0->pmk_r0, pmk_r0, PMK_LEN);
222 os_memcpy(r0->pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN);
223 os_memcpy(r0->spa, spa, ETH_ALEN);
224 r0->pairwise = pairwise;
226 r0->next = cache->pmk_r0;
233 static int wpa_ft_fetch_pmk_r0(struct wpa_authenticator *wpa_auth,
234 const u8 *spa, const u8 *pmk_r0_name,
235 u8 *pmk_r0, int *pairwise)
237 struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
238 struct wpa_ft_pmk_r0_sa *r0;
242 if (os_memcmp(r0->spa, spa, ETH_ALEN) == 0 &&
243 os_memcmp(r0->pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN)
245 os_memcpy(pmk_r0, r0->pmk_r0, PMK_LEN);
247 *pairwise = r0->pairwise;
258 static int wpa_ft_store_pmk_r1(struct wpa_authenticator *wpa_auth,
259 const u8 *spa, const u8 *pmk_r1,
260 const u8 *pmk_r1_name, int pairwise)
262 struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
263 struct wpa_ft_pmk_r1_sa *r1;
265 /* TODO: add expiration and limit on number of entries in cache */
267 r1 = os_zalloc(sizeof(*r1));
271 os_memcpy(r1->pmk_r1, pmk_r1, PMK_LEN);
272 os_memcpy(r1->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN);
273 os_memcpy(r1->spa, spa, ETH_ALEN);
274 r1->pairwise = pairwise;
276 r1->next = cache->pmk_r1;
283 static int wpa_ft_fetch_pmk_r1(struct wpa_authenticator *wpa_auth,
284 const u8 *spa, const u8 *pmk_r1_name,
285 u8 *pmk_r1, int *pairwise)
287 struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
288 struct wpa_ft_pmk_r1_sa *r1;
292 if (os_memcmp(r1->spa, spa, ETH_ALEN) == 0 &&
293 os_memcmp(r1->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN)
295 os_memcpy(pmk_r1, r1->pmk_r1, PMK_LEN);
297 *pairwise = r1->pairwise;
308 static int wpa_ft_pull_pmk_r1(struct wpa_authenticator *wpa_auth,
309 const u8 *s1kh_id, const u8 *r0kh_id,
310 size_t r0kh_id_len, const u8 *pmk_r0_name)
312 struct ft_remote_r0kh *r0kh;
313 struct ft_r0kh_r1kh_pull_frame frame, f;
315 r0kh = wpa_auth->conf.r0kh_list;
317 if (r0kh->id_len == r0kh_id_len &&
318 os_memcmp(r0kh->id, r0kh_id, r0kh_id_len) == 0)
325 wpa_printf(MSG_DEBUG, "FT: Send PMK-R1 pull request to remote R0KH "
326 "address " MACSTR, MAC2STR(r0kh->addr));
328 os_memset(&frame, 0, sizeof(frame));
329 frame.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
330 frame.packet_type = FT_PACKET_R0KH_R1KH_PULL;
331 frame.data_length = host_to_le16(FT_R0KH_R1KH_PULL_DATA_LEN);
332 os_memcpy(frame.ap_address, wpa_auth->addr, ETH_ALEN);
334 /* aes_wrap() does not support inplace encryption, so use a temporary
335 * buffer for the data. */
336 if (os_get_random(f.nonce, sizeof(f.nonce))) {
337 wpa_printf(MSG_DEBUG, "FT: Failed to get random data for "
341 os_memcpy(f.pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN);
342 os_memcpy(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN);
343 os_memcpy(f.s1kh_id, s1kh_id, ETH_ALEN);
345 if (aes_wrap(r0kh->key, (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8,
346 f.nonce, frame.nonce) < 0)
349 wpa_ft_rrb_send(wpa_auth, r0kh->addr, (u8 *) &frame, sizeof(frame));
355 int wpa_auth_derive_ptk_ft(struct wpa_state_machine *sm, const u8 *pmk,
356 struct wpa_ptk *ptk, size_t ptk_len)
358 u8 pmk_r0[PMK_LEN], pmk_r0_name[WPA_PMK_NAME_LEN];
360 u8 ptk_name[WPA_PMK_NAME_LEN];
361 const u8 *mdid = sm->wpa_auth->conf.mobility_domain;
362 const u8 *r0kh = sm->wpa_auth->conf.r0_key_holder;
363 size_t r0kh_len = sm->wpa_auth->conf.r0_key_holder_len;
364 const u8 *r1kh = sm->wpa_auth->conf.r1_key_holder;
365 const u8 *ssid = sm->wpa_auth->conf.ssid;
366 size_t ssid_len = sm->wpa_auth->conf.ssid_len;
369 if (sm->xxkey_len == 0) {
370 wpa_printf(MSG_DEBUG, "FT: XXKey not available for key "
375 wpa_derive_pmk_r0(sm->xxkey, sm->xxkey_len, ssid, ssid_len, mdid,
376 r0kh, r0kh_len, sm->addr, pmk_r0, pmk_r0_name);
377 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", pmk_r0, PMK_LEN);
378 wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name", pmk_r0_name, WPA_PMK_NAME_LEN);
379 wpa_ft_store_pmk_r0(sm->wpa_auth, sm->addr, pmk_r0, pmk_r0_name,
382 wpa_derive_pmk_r1(pmk_r0, pmk_r0_name, r1kh, sm->addr,
383 pmk_r1, sm->pmk_r1_name);
384 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", pmk_r1, PMK_LEN);
385 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", sm->pmk_r1_name,
387 wpa_ft_store_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1, sm->pmk_r1_name,
390 wpa_pmk_r1_to_ptk(pmk_r1, sm->SNonce, sm->ANonce, sm->addr,
391 sm->wpa_auth->addr, sm->pmk_r1_name,
392 (u8 *) ptk, ptk_len, ptk_name);
393 wpa_hexdump_key(MSG_DEBUG, "FT: PTK", (u8 *) ptk, ptk_len);
394 wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
400 static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
401 const u8 *addr, int idx, u8 *seq)
403 if (wpa_auth->cb.get_seqnum == NULL)
405 return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
409 static u8 * wpa_ft_gtk_subelem(struct wpa_state_machine *sm, size_t *len)
412 struct wpa_group *gsm = sm->group;
413 size_t subelem_len, pad_len;
418 key_len = gsm->GTK_len;
419 if (key_len > sizeof(keybuf))
423 * Pad key for AES Key Wrap if it is not multiple of 8 bytes or is less
426 pad_len = key_len % 8;
428 pad_len = 8 - pad_len;
429 if (key_len + pad_len < 16)
432 os_memcpy(keybuf, gsm->GTK[gsm->GN - 1], key_len);
433 os_memset(keybuf + key_len, 0, pad_len);
434 keybuf[key_len] = 0xdd;
438 key = gsm->GTK[gsm->GN - 1];
441 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
444 subelem_len = 13 + key_len + 8;
445 subelem = os_zalloc(subelem_len);
449 subelem[0] = FTIE_SUBELEM_GTK;
450 subelem[1] = 11 + key_len + 8;
451 /* Key ID in B0-B1 of Key Info */
452 WPA_PUT_LE16(&subelem[2], gsm->GN & 0x03);
453 subelem[4] = gsm->GTK_len;
454 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, subelem + 5);
455 if (aes_wrap(sm->PTK.kek, key_len / 8, key, subelem + 13)) {
465 #ifdef CONFIG_IEEE80211W
466 static u8 * wpa_ft_igtk_subelem(struct wpa_state_machine *sm, size_t *len)
469 struct wpa_group *gsm = sm->group;
472 /* Sub-elem ID[1] | Length[1] | KeyID[2] | IPN[6] | Key Length[1] |
474 subelem_len = 1 + 1 + 2 + 6 + 1 + WPA_IGTK_LEN + 8;
475 subelem = os_zalloc(subelem_len);
480 *pos++ = FTIE_SUBELEM_IGTK;
481 *pos++ = subelem_len - 2;
482 WPA_PUT_LE16(pos, gsm->GN_igtk);
484 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos);
486 *pos++ = WPA_IGTK_LEN;
487 if (aes_wrap(sm->PTK.kek, WPA_IGTK_LEN / 8,
488 gsm->IGTK[gsm->GN_igtk - 4], pos)) {
496 #endif /* CONFIG_IEEE80211W */
499 static u8 * wpa_ft_process_rdie(u8 *pos, u8 *end, u8 id, u8 descr_count,
500 const u8 *ies, size_t ies_len)
502 struct ieee802_11_elems parse;
503 struct rsn_rdie *rdie;
505 wpa_printf(MSG_DEBUG, "FT: Resource Request: id=%d descr_count=%d",
507 wpa_hexdump(MSG_MSGDUMP, "FT: Resource descriptor IE(s)",
510 if (end - pos < (int) sizeof(*rdie)) {
511 wpa_printf(MSG_ERROR, "FT: Not enough room for response RDIE");
515 *pos++ = WLAN_EID_RIC_DATA;
516 *pos++ = sizeof(*rdie);
517 rdie = (struct rsn_rdie *) pos;
519 rdie->descr_count = 0;
520 rdie->status_code = host_to_le16(WLAN_STATUS_SUCCESS);
521 pos += sizeof(*rdie);
523 if (ieee802_11_parse_elems((u8 *) ies, ies_len, &parse, 1) ==
525 wpa_printf(MSG_DEBUG, "FT: Failed to parse request IEs");
527 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
532 if (parse.wmm_tspec) {
533 struct wmm_tspec_element *tspec;
536 if (parse.wmm_tspec_len + 2 < (int) sizeof(*tspec)) {
537 wpa_printf(MSG_DEBUG, "FT: Too short WMM TSPEC IE "
538 "(%d)", (int) parse.wmm_tspec_len);
540 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
543 if (end - pos < (int) sizeof(*tspec)) {
544 wpa_printf(MSG_ERROR, "FT: Not enough room for "
547 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
550 tspec = (struct wmm_tspec_element *) pos;
551 os_memcpy(tspec, parse.wmm_tspec - 2, sizeof(*tspec));
552 res = wmm_process_tspec(tspec);
553 wpa_printf(MSG_DEBUG, "FT: ADDTS processing result: %d", res);
554 if (res == WMM_ADDTS_STATUS_INVALID_PARAMETERS)
556 host_to_le16(WLAN_STATUS_INVALID_PARAMETERS);
557 else if (res == WMM_ADDTS_STATUS_REFUSED)
559 host_to_le16(WLAN_STATUS_REQUEST_DECLINED);
561 /* TSPEC accepted; include updated TSPEC in response */
562 rdie->descr_count = 1;
563 pos += sizeof(*tspec);
567 #endif /* NEED_AP_MLME */
569 wpa_printf(MSG_DEBUG, "FT: No supported resource requested");
570 rdie->status_code = host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
575 static u8 * wpa_ft_process_ric(u8 *pos, u8 *end, const u8 *ric, size_t ric_len)
577 const u8 *rpos, *start;
578 const struct rsn_rdie *rdie;
580 wpa_hexdump(MSG_MSGDUMP, "FT: RIC Request", ric, ric_len);
583 while (rpos + sizeof(*rdie) < ric + ric_len) {
584 if (rpos[0] != WLAN_EID_RIC_DATA || rpos[1] < sizeof(*rdie) ||
585 rpos + 2 + rpos[1] > ric + ric_len)
587 rdie = (const struct rsn_rdie *) (rpos + 2);
591 while (rpos + 2 <= ric + ric_len &&
592 rpos + 2 + rpos[1] <= ric + ric_len) {
593 if (rpos[0] == WLAN_EID_RIC_DATA)
597 pos = wpa_ft_process_rdie(pos, end, rdie->id,
599 start, rpos - start);
606 u8 * wpa_sm_write_assoc_resp_ies(struct wpa_state_machine *sm, u8 *pos,
607 size_t max_len, int auth_alg,
608 const u8 *req_ies, size_t req_ies_len)
610 u8 *end, *mdie, *ftie, *rsnie = NULL, *r0kh_id, *subelem = NULL;
611 size_t mdie_len, ftie_len, rsnie_len = 0, r0kh_id_len, subelem_len = 0;
613 struct wpa_auth_config *conf;
614 struct rsn_ftie *_ftie;
615 struct wpa_ft_ies parse;
621 conf = &sm->wpa_auth->conf;
623 if (sm->wpa_key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
624 sm->wpa_key_mgmt != WPA_KEY_MGMT_FT_PSK)
629 if (auth_alg == WLAN_AUTH_FT) {
631 * RSN (only present if this is a Reassociation Response and
632 * part of a fast BSS transition)
634 res = wpa_write_rsn_ie(conf, pos, end - pos, sm->pmk_r1_name);
642 /* Mobility Domain Information */
643 res = wpa_write_mdie(conf, pos, end - pos);
650 /* Fast BSS Transition Information */
651 if (auth_alg == WLAN_AUTH_FT) {
652 subelem = wpa_ft_gtk_subelem(sm, &subelem_len);
653 r0kh_id = sm->r0kh_id;
654 r0kh_id_len = sm->r0kh_id_len;
655 #ifdef CONFIG_IEEE80211W
656 if (sm->mgmt_frame_prot) {
660 igtk = wpa_ft_igtk_subelem(sm, &igtk_len);
665 nbuf = os_realloc(subelem, subelem_len + igtk_len);
672 os_memcpy(subelem + subelem_len, igtk, igtk_len);
673 subelem_len += igtk_len;
676 #endif /* CONFIG_IEEE80211W */
678 r0kh_id = conf->r0_key_holder;
679 r0kh_id_len = conf->r0_key_holder_len;
681 res = wpa_write_ftie(conf, r0kh_id, r0kh_id_len, NULL, NULL, pos,
682 end - pos, subelem, subelem_len);
690 _ftie = (struct rsn_ftie *) (ftie + 2);
691 if (auth_alg == WLAN_AUTH_FT)
692 _ftie->mic_control[1] = 3; /* Information element count */
695 if (wpa_ft_parse_ies(req_ies, req_ies_len, &parse) == 0 && parse.ric) {
696 pos = wpa_ft_process_ric(pos, end, parse.ric, parse.ric_len);
697 if (auth_alg == WLAN_AUTH_FT)
698 _ftie->mic_control[1] +=
699 ieee802_11_ie_count(ric_start,
702 if (ric_start == pos)
705 if (auth_alg == WLAN_AUTH_FT &&
706 wpa_ft_mic(sm->PTK.kck, sm->addr, sm->wpa_auth->addr, 6,
707 mdie, mdie_len, ftie, ftie_len,
709 ric_start, ric_start ? pos - ric_start : 0,
711 wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
717 static int wpa_ft_parse_ftie(const u8 *ie, size_t ie_len,
718 struct wpa_ft_ies *parse)
723 parse->ftie_len = ie_len;
725 pos = ie + sizeof(struct rsn_ftie);
728 while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
730 case FTIE_SUBELEM_R1KH_ID:
731 if (pos[1] != FT_R1KH_ID_LEN) {
732 wpa_printf(MSG_DEBUG, "FT: Invalid R1KH-ID "
733 "length in FTIE: %d", pos[1]);
736 parse->r1kh_id = pos + 2;
738 case FTIE_SUBELEM_GTK:
739 parse->gtk = pos + 2;
740 parse->gtk_len = pos[1];
742 case FTIE_SUBELEM_R0KH_ID:
743 if (pos[1] < 1 || pos[1] > FT_R0KH_ID_MAX_LEN) {
744 wpa_printf(MSG_DEBUG, "FT: Invalid R0KH-ID "
745 "length in FTIE: %d", pos[1]);
748 parse->r0kh_id = pos + 2;
749 parse->r0kh_id_len = pos[1];
760 static int wpa_ft_parse_ies(const u8 *ies, size_t ies_len,
761 struct wpa_ft_ies *parse)
764 struct wpa_ie_data data;
766 const struct rsn_ftie *ftie;
767 int prot_ie_count = 0;
769 os_memset(parse, 0, sizeof(*parse));
775 while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
778 parse->rsn = pos + 2;
779 parse->rsn_len = pos[1];
780 ret = wpa_parse_wpa_ie_rsn(parse->rsn - 2,
784 wpa_printf(MSG_DEBUG, "FT: Failed to parse "
788 if (data.num_pmkid == 1 && data.pmkid)
789 parse->rsn_pmkid = data.pmkid;
791 case WLAN_EID_MOBILITY_DOMAIN:
792 parse->mdie = pos + 2;
793 parse->mdie_len = pos[1];
795 case WLAN_EID_FAST_BSS_TRANSITION:
796 if (pos[1] < sizeof(*ftie))
798 ftie = (const struct rsn_ftie *) (pos + 2);
799 prot_ie_count = ftie->mic_control[1];
800 if (wpa_ft_parse_ftie(pos + 2, pos[1], parse) < 0)
803 case WLAN_EID_RIC_DATA:
804 if (parse->ric == NULL)
811 if (prot_ie_count == 0)
812 return 0; /* no MIC */
815 * Check that the protected IE count matches with IEs included in the
824 if (prot_ie_count < 0) {
825 wpa_printf(MSG_DEBUG, "FT: Some required IEs not included in "
826 "the protected IE count");
830 if (prot_ie_count == 0 && parse->ric) {
831 wpa_printf(MSG_DEBUG, "FT: RIC IE(s) in the frame, but not "
832 "included in protected IE count");
836 /* Determine the end of the RIC IE(s) */
838 while (pos && pos + 2 <= end && pos + 2 + pos[1] <= end &&
843 parse->ric_len = pos - parse->ric;
845 wpa_printf(MSG_DEBUG, "FT: %d protected IEs missing from "
846 "frame", (int) prot_ie_count);
854 static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
856 enum wpa_alg alg, const u8 *addr, int idx,
857 u8 *key, size_t key_len)
859 if (wpa_auth->cb.set_key == NULL)
861 return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
866 void wpa_ft_install_ptk(struct wpa_state_machine *sm)
871 /* MLME-SETKEYS.request(PTK) */
872 if (sm->pairwise == WPA_CIPHER_TKIP) {
875 } else if (sm->pairwise == WPA_CIPHER_CCMP) {
879 wpa_printf(MSG_DEBUG, "FT: Unknown pairwise alg 0x%x - skip "
880 "PTK configuration", sm->pairwise);
884 /* FIX: add STA entry to kernel/driver here? The set_key will fail
885 * most likely without this.. At the moment, STA entry is added only
886 * after association has been completed. This function will be called
887 * again after association to get the PTK configured, but that could be
888 * optimized by adding the STA entry earlier.
890 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
894 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
895 sm->pairwise_set = TRUE;
899 static u16 wpa_ft_process_auth_req(struct wpa_state_machine *sm,
900 const u8 *ies, size_t ies_len,
901 u8 **resp_ies, size_t *resp_ies_len)
903 struct rsn_mdie *mdie;
904 struct rsn_ftie *ftie;
905 u8 pmk_r1[PMK_LEN], pmk_r1_name[WPA_PMK_NAME_LEN];
906 u8 ptk_name[WPA_PMK_NAME_LEN];
907 struct wpa_auth_config *conf;
908 struct wpa_ft_ies parse;
909 size_t buflen, ptk_len;
917 sm->pmk_r1_name_valid = 0;
918 conf = &sm->wpa_auth->conf;
920 wpa_hexdump(MSG_DEBUG, "FT: Received authentication frame IEs",
923 if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
924 wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
925 return WLAN_STATUS_UNSPECIFIED_FAILURE;
928 mdie = (struct rsn_mdie *) parse.mdie;
929 if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
930 os_memcmp(mdie->mobility_domain,
931 sm->wpa_auth->conf.mobility_domain,
932 MOBILITY_DOMAIN_ID_LEN) != 0) {
933 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
934 return WLAN_STATUS_INVALID_MDIE;
937 ftie = (struct rsn_ftie *) parse.ftie;
938 if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
939 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
940 return WLAN_STATUS_INVALID_FTIE;
943 os_memcpy(sm->SNonce, ftie->snonce, WPA_NONCE_LEN);
945 if (parse.r0kh_id == NULL) {
946 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE - no R0KH-ID");
947 return WLAN_STATUS_INVALID_FTIE;
950 wpa_hexdump(MSG_DEBUG, "FT: STA R0KH-ID",
951 parse.r0kh_id, parse.r0kh_id_len);
952 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
953 sm->r0kh_id_len = parse.r0kh_id_len;
955 if (parse.rsn_pmkid == NULL) {
956 wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
957 return WLAN_STATUS_INVALID_PMKID;
960 wpa_hexdump(MSG_DEBUG, "FT: Requested PMKR0Name",
961 parse.rsn_pmkid, WPA_PMK_NAME_LEN);
962 wpa_derive_pmk_r1_name(parse.rsn_pmkid,
963 sm->wpa_auth->conf.r1_key_holder, sm->addr,
965 wpa_hexdump(MSG_DEBUG, "FT: Derived requested PMKR1Name",
966 pmk_r1_name, WPA_PMK_NAME_LEN);
968 if (wpa_ft_fetch_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1_name, pmk_r1,
970 if (wpa_ft_pull_pmk_r1(sm->wpa_auth, sm->addr, sm->r0kh_id,
971 sm->r0kh_id_len, parse.rsn_pmkid) < 0) {
972 wpa_printf(MSG_DEBUG, "FT: Did not have matching "
973 "PMK-R1 and unknown R0KH-ID");
974 return WLAN_STATUS_INVALID_PMKID;
978 * TODO: Should return "status pending" (and the caller should
979 * not send out response now). The real response will be sent
980 * once the response from R0KH is received.
982 return WLAN_STATUS_INVALID_PMKID;
985 wpa_hexdump_key(MSG_DEBUG, "FT: Selected PMK-R1", pmk_r1, PMK_LEN);
986 sm->pmk_r1_name_valid = 1;
987 os_memcpy(sm->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN);
989 if (os_get_random(sm->ANonce, WPA_NONCE_LEN)) {
990 wpa_printf(MSG_DEBUG, "FT: Failed to get random data for "
992 return WLAN_STATUS_UNSPECIFIED_FAILURE;
995 wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
996 sm->SNonce, WPA_NONCE_LEN);
997 wpa_hexdump(MSG_DEBUG, "FT: Generated ANonce",
998 sm->ANonce, WPA_NONCE_LEN);
1000 ptk_len = pairwise != WPA_CIPHER_CCMP ? 64 : 48;
1001 wpa_pmk_r1_to_ptk(pmk_r1, sm->SNonce, sm->ANonce, sm->addr,
1002 sm->wpa_auth->addr, pmk_r1_name,
1003 (u8 *) &sm->PTK, ptk_len, ptk_name);
1004 wpa_hexdump_key(MSG_DEBUG, "FT: PTK",
1005 (u8 *) &sm->PTK, ptk_len);
1006 wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
1008 sm->pairwise = pairwise;
1009 wpa_ft_install_ptk(sm);
1011 buflen = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) +
1012 2 + FT_R1KH_ID_LEN + 200;
1013 *resp_ies = os_zalloc(buflen);
1014 if (*resp_ies == NULL) {
1015 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1019 end = *resp_ies + buflen;
1021 ret = wpa_write_rsn_ie(conf, pos, end - pos, parse.rsn_pmkid);
1025 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1029 ret = wpa_write_mdie(conf, pos, end - pos);
1033 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1037 ret = wpa_write_ftie(conf, parse.r0kh_id, parse.r0kh_id_len,
1038 sm->ANonce, sm->SNonce, pos, end - pos, NULL, 0);
1042 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1046 *resp_ies_len = pos - *resp_ies;
1048 return WLAN_STATUS_SUCCESS;
1052 void wpa_ft_process_auth(struct wpa_state_machine *sm, const u8 *bssid,
1053 u16 auth_transaction, const u8 *ies, size_t ies_len,
1054 void (*cb)(void *ctx, const u8 *dst, const u8 *bssid,
1055 u16 auth_transaction, u16 status,
1056 const u8 *ies, size_t ies_len),
1061 size_t resp_ies_len;
1064 wpa_printf(MSG_DEBUG, "FT: Received authentication frame, but "
1065 "WPA SM not available");
1069 wpa_printf(MSG_DEBUG, "FT: Received authentication frame: STA=" MACSTR
1070 " BSSID=" MACSTR " transaction=%d",
1071 MAC2STR(sm->addr), MAC2STR(bssid), auth_transaction);
1072 status = wpa_ft_process_auth_req(sm, ies, ies_len, &resp_ies,
1075 wpa_printf(MSG_DEBUG, "FT: FT authentication response: dst=" MACSTR
1076 " auth_transaction=%d status=%d",
1077 MAC2STR(sm->addr), auth_transaction + 1, status);
1078 wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
1079 cb(ctx, sm->addr, bssid, auth_transaction + 1, status,
1080 resp_ies, resp_ies_len);
1085 u16 wpa_ft_validate_reassoc(struct wpa_state_machine *sm, const u8 *ies,
1088 struct wpa_ft_ies parse;
1089 struct rsn_mdie *mdie;
1090 struct rsn_ftie *ftie;
1094 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1096 wpa_hexdump(MSG_DEBUG, "FT: Reassoc Req IEs", ies, ies_len);
1098 if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
1099 wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
1100 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1103 if (parse.rsn == NULL) {
1104 wpa_printf(MSG_DEBUG, "FT: No RSNIE in Reassoc Req");
1105 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1108 if (parse.rsn_pmkid == NULL) {
1109 wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
1110 return WLAN_STATUS_INVALID_PMKID;
1113 if (os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1115 wpa_printf(MSG_DEBUG, "FT: PMKID in Reassoc Req did not match "
1116 "with the PMKR1Name derived from auth request");
1117 return WLAN_STATUS_INVALID_PMKID;
1120 mdie = (struct rsn_mdie *) parse.mdie;
1121 if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
1122 os_memcmp(mdie->mobility_domain,
1123 sm->wpa_auth->conf.mobility_domain,
1124 MOBILITY_DOMAIN_ID_LEN) != 0) {
1125 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
1126 return WLAN_STATUS_INVALID_MDIE;
1129 ftie = (struct rsn_ftie *) parse.ftie;
1130 if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
1131 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
1132 return WLAN_STATUS_INVALID_FTIE;
1135 if (wpa_ft_mic(sm->PTK.kck, sm->addr, sm->wpa_auth->addr, 5,
1136 parse.mdie - 2, parse.mdie_len + 2,
1137 parse.ftie - 2, parse.ftie_len + 2,
1138 parse.rsn - 2, parse.rsn_len + 2,
1139 parse.ric, parse.ric_len,
1141 wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
1142 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1145 if (os_memcmp(mic, ftie->mic, 16) != 0) {
1146 wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
1147 wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", ftie->mic, 16);
1148 wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16);
1149 return WLAN_STATUS_INVALID_FTIE;
1152 return WLAN_STATUS_SUCCESS;
1156 int wpa_ft_action_rx(struct wpa_state_machine *sm, const u8 *data, size_t len)
1158 const u8 *sta_addr, *target_ap;
1162 struct ft_rrb_frame *frame;
1168 * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
1169 * FT Request action frame body[variable]
1173 wpa_printf(MSG_DEBUG, "FT: Too short FT Action frame "
1174 "(len=%lu)", (unsigned long) len);
1179 sta_addr = data + 2;
1180 target_ap = data + 8;
1184 wpa_printf(MSG_DEBUG, "FT: Received FT Action frame (STA=" MACSTR
1185 " Target AP=" MACSTR " Action=%d)",
1186 MAC2STR(sta_addr), MAC2STR(target_ap), action);
1188 if (os_memcmp(sta_addr, sm->addr, ETH_ALEN) != 0) {
1189 wpa_printf(MSG_DEBUG, "FT: Mismatch in FT Action STA address: "
1190 "STA=" MACSTR " STA-Address=" MACSTR,
1191 MAC2STR(sm->addr), MAC2STR(sta_addr));
1196 * Do some sanity checking on the target AP address (not own and not
1197 * broadcast. This could be extended to filter based on a list of known
1198 * APs in the MD (if such a list were configured).
1200 if ((target_ap[0] & 0x01) ||
1201 os_memcmp(target_ap, sm->wpa_auth->addr, ETH_ALEN) == 0) {
1202 wpa_printf(MSG_DEBUG, "FT: Invalid Target AP in FT Action "
1207 wpa_hexdump(MSG_MSGDUMP, "FT: Action frame body", ies, ies_len);
1209 /* RRB - Forward action frame to the target AP */
1210 frame = os_malloc(sizeof(*frame) + len);
1211 frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
1212 frame->packet_type = FT_PACKET_REQUEST;
1213 frame->action_length = host_to_le16(len);
1214 os_memcpy(frame->ap_address, sm->wpa_auth->addr, ETH_ALEN);
1215 os_memcpy(frame + 1, data, len);
1217 wpa_ft_rrb_send(sm->wpa_auth, target_ap, (u8 *) frame,
1218 sizeof(*frame) + len);
1225 static int wpa_ft_rrb_rx_request(struct wpa_authenticator *wpa_auth,
1226 const u8 *current_ap, const u8 *sta_addr,
1227 const u8 *body, size_t len)
1229 struct wpa_state_machine *sm;
1232 size_t resp_ies_len, rlen;
1233 struct ft_rrb_frame *frame;
1235 sm = wpa_ft_add_sta(wpa_auth, sta_addr);
1237 wpa_printf(MSG_DEBUG, "FT: Failed to add new STA based on "
1242 wpa_hexdump(MSG_MSGDUMP, "FT: RRB Request Frame body", body, len);
1244 status = wpa_ft_process_auth_req(sm, body, len, &resp_ies,
1247 wpa_printf(MSG_DEBUG, "FT: RRB authentication response: STA=" MACSTR
1248 " CurrentAP=" MACSTR " status=%d",
1249 MAC2STR(sm->addr), MAC2STR(current_ap), status);
1250 wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
1252 /* RRB - Forward action frame response to the Current AP */
1255 * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
1256 * Status_Code[2] FT Request action frame body[variable]
1258 rlen = 2 + 2 * ETH_ALEN + 2 + resp_ies_len;
1260 frame = os_malloc(sizeof(*frame) + rlen);
1261 frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
1262 frame->packet_type = FT_PACKET_RESPONSE;
1263 frame->action_length = host_to_le16(rlen);
1264 os_memcpy(frame->ap_address, wpa_auth->addr, ETH_ALEN);
1265 pos = (u8 *) (frame + 1);
1266 *pos++ = WLAN_ACTION_FT;
1267 *pos++ = 2; /* Action: Response */
1268 os_memcpy(pos, sta_addr, ETH_ALEN);
1270 os_memcpy(pos, wpa_auth->addr, ETH_ALEN);
1272 WPA_PUT_LE16(pos, status);
1275 os_memcpy(pos, resp_ies, resp_ies_len);
1279 wpa_ft_rrb_send(wpa_auth, current_ap, (u8 *) frame,
1280 sizeof(*frame) + rlen);
1287 static int wpa_ft_rrb_rx_pull(struct wpa_authenticator *wpa_auth,
1289 const u8 *data, size_t data_len)
1291 struct ft_r0kh_r1kh_pull_frame *frame, f;
1292 struct ft_remote_r1kh *r1kh;
1293 struct ft_r0kh_r1kh_resp_frame resp, r;
1297 wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull");
1299 if (data_len < sizeof(*frame))
1302 r1kh = wpa_auth->conf.r1kh_list;
1304 if (os_memcmp(r1kh->addr, src_addr, ETH_ALEN) == 0)
1309 wpa_printf(MSG_DEBUG, "FT: No matching R1KH address found for "
1310 "PMK-R1 pull source address " MACSTR,
1315 frame = (struct ft_r0kh_r1kh_pull_frame *) data;
1316 /* aes_unwrap() does not support inplace decryption, so use a temporary
1317 * buffer for the data. */
1318 if (aes_unwrap(r1kh->key, (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8,
1319 frame->nonce, f.nonce) < 0) {
1320 wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull "
1321 "request from " MACSTR, MAC2STR(src_addr));
1325 wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce",
1326 f.nonce, sizeof(f.nonce));
1327 wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR0Name",
1328 f.pmk_r0_name, WPA_PMK_NAME_LEN);
1329 wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID="
1330 MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));
1332 os_memset(&resp, 0, sizeof(resp));
1333 resp.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
1334 resp.packet_type = FT_PACKET_R0KH_R1KH_RESP;
1335 resp.data_length = host_to_le16(FT_R0KH_R1KH_RESP_DATA_LEN);
1336 os_memcpy(resp.ap_address, wpa_auth->addr, ETH_ALEN);
1338 /* aes_wrap() does not support inplace encryption, so use a temporary
1339 * buffer for the data. */
1340 os_memcpy(r.nonce, f.nonce, sizeof(f.nonce));
1341 os_memcpy(r.r1kh_id, f.r1kh_id, FT_R1KH_ID_LEN);
1342 os_memcpy(r.s1kh_id, f.s1kh_id, ETH_ALEN);
1343 if (wpa_ft_fetch_pmk_r0(wpa_auth, f.s1kh_id, f.pmk_r0_name, pmk_r0,
1345 wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name found for "
1350 wpa_derive_pmk_r1(pmk_r0, f.pmk_r0_name, f.r1kh_id, f.s1kh_id,
1351 r.pmk_r1, r.pmk_r1_name);
1352 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", r.pmk_r1, PMK_LEN);
1353 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", r.pmk_r1_name,
1355 r.pairwise = pairwise;
1357 if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8,
1358 r.nonce, resp.nonce) < 0) {
1359 os_memset(pmk_r0, 0, PMK_LEN);
1363 os_memset(pmk_r0, 0, PMK_LEN);
1365 wpa_ft_rrb_send(wpa_auth, src_addr, (u8 *) &resp, sizeof(resp));
1371 static int wpa_ft_rrb_rx_resp(struct wpa_authenticator *wpa_auth,
1373 const u8 *data, size_t data_len)
1375 struct ft_r0kh_r1kh_resp_frame *frame, f;
1376 struct ft_remote_r0kh *r0kh;
1379 wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull response");
1381 if (data_len < sizeof(*frame))
1384 r0kh = wpa_auth->conf.r0kh_list;
1386 if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0)
1391 wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for "
1392 "PMK-R0 pull response source address " MACSTR,
1397 frame = (struct ft_r0kh_r1kh_resp_frame *) data;
1398 /* aes_unwrap() does not support inplace decryption, so use a temporary
1399 * buffer for the data. */
1400 if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8,
1401 frame->nonce, f.nonce) < 0) {
1402 wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull "
1403 "response from " MACSTR, MAC2STR(src_addr));
1407 if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN)
1409 wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull response did not use a "
1410 "matching R1KH-ID");
1414 /* TODO: verify that <nonce,s1kh_id> matches with a pending request
1415 * and call this requests callback function to finish request
1418 pairwise = le_to_host16(f.pairwise);
1419 wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce",
1420 f.nonce, sizeof(f.nonce));
1421 wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID="
1422 MACSTR " pairwise=0x%x",
1423 MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id), pairwise);
1424 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 pull - PMK-R1",
1426 wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR1Name",
1427 f.pmk_r1_name, WPA_PMK_NAME_LEN);
1429 wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name,
1431 os_memset(f.pmk_r1, 0, PMK_LEN);
1437 static int wpa_ft_rrb_rx_push(struct wpa_authenticator *wpa_auth,
1439 const u8 *data, size_t data_len)
1441 struct ft_r0kh_r1kh_push_frame *frame, f;
1442 struct ft_remote_r0kh *r0kh;
1447 wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 push");
1449 if (data_len < sizeof(*frame))
1452 r0kh = wpa_auth->conf.r0kh_list;
1454 if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0)
1459 wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for "
1460 "PMK-R0 push source address " MACSTR,
1465 frame = (struct ft_r0kh_r1kh_push_frame *) data;
1466 /* aes_unwrap() does not support inplace decryption, so use a temporary
1467 * buffer for the data. */
1468 if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8,
1469 frame->timestamp, f.timestamp) < 0) {
1470 wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 push from "
1471 MACSTR, MAC2STR(src_addr));
1476 tsend = WPA_GET_LE32(f.timestamp);
1477 if ((now.sec > tsend && now.sec - tsend > 60) ||
1478 (now.sec < tsend && tsend - now.sec > 60)) {
1479 wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not have a valid "
1480 "timestamp: sender time %d own time %d\n",
1481 (int) tsend, (int) now.sec);
1485 if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN)
1487 wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not use a matching "
1488 "R1KH-ID (received " MACSTR " own " MACSTR ")",
1490 MAC2STR(wpa_auth->conf.r1_key_holder));
1494 pairwise = le_to_host16(f.pairwise);
1495 wpa_printf(MSG_DEBUG, "FT: PMK-R1 push - R1KH-ID=" MACSTR " S1KH-ID="
1496 MACSTR " pairwise=0x%x",
1497 MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id), pairwise);
1498 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 push - PMK-R1",
1500 wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 push - PMKR1Name",
1501 f.pmk_r1_name, WPA_PMK_NAME_LEN);
1503 wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name,
1505 os_memset(f.pmk_r1, 0, PMK_LEN);
1511 int wpa_ft_rrb_rx(struct wpa_authenticator *wpa_auth, const u8 *src_addr,
1512 const u8 *data, size_t data_len)
1514 struct ft_rrb_frame *frame;
1516 const u8 *pos, *end, *start;
1518 const u8 *sta_addr, *target_ap_addr;
1520 wpa_printf(MSG_DEBUG, "FT: RRB received frame from remote AP " MACSTR,
1523 if (data_len < sizeof(*frame)) {
1524 wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (data_len=%lu)",
1525 (unsigned long) data_len);
1530 frame = (struct ft_rrb_frame *) pos;
1531 pos += sizeof(*frame);
1533 alen = le_to_host16(frame->action_length);
1534 wpa_printf(MSG_DEBUG, "FT: RRB frame - frame_type=%d packet_type=%d "
1535 "action_length=%d ap_address=" MACSTR,
1536 frame->frame_type, frame->packet_type, alen,
1537 MAC2STR(frame->ap_address));
1539 if (frame->frame_type != RSN_REMOTE_FRAME_TYPE_FT_RRB) {
1540 /* Discard frame per IEEE Std 802.11r-2008, 11A.10.3 */
1541 wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with "
1542 "unrecognized type %d", frame->frame_type);
1546 if (alen > data_len - sizeof(*frame)) {
1547 wpa_printf(MSG_DEBUG, "FT: RRB frame too short for action "
1552 if (frame->packet_type == FT_PACKET_R0KH_R1KH_PULL)
1553 return wpa_ft_rrb_rx_pull(wpa_auth, src_addr, data, data_len);
1554 if (frame->packet_type == FT_PACKET_R0KH_R1KH_RESP)
1555 return wpa_ft_rrb_rx_resp(wpa_auth, src_addr, data, data_len);
1556 if (frame->packet_type == FT_PACKET_R0KH_R1KH_PUSH)
1557 return wpa_ft_rrb_rx_push(wpa_auth, src_addr, data, data_len);
1559 wpa_hexdump(MSG_MSGDUMP, "FT: RRB - FT Action frame", pos, alen);
1561 if (alen < 1 + 1 + 2 * ETH_ALEN) {
1562 wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (not enough "
1563 "room for Action Frame body); alen=%lu",
1564 (unsigned long) alen);
1570 if (*pos != WLAN_ACTION_FT) {
1571 wpa_printf(MSG_DEBUG, "FT: Unexpected Action frame category "
1580 target_ap_addr = pos;
1582 wpa_printf(MSG_DEBUG, "FT: RRB Action Frame: action=%d sta_addr="
1583 MACSTR " target_ap_addr=" MACSTR,
1584 action, MAC2STR(sta_addr), MAC2STR(target_ap_addr));
1586 if (frame->packet_type == FT_PACKET_REQUEST) {
1587 wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Request");
1590 wpa_printf(MSG_DEBUG, "FT: Unexpected Action %d in "
1591 "RRB Request", action);
1595 if (os_memcmp(target_ap_addr, wpa_auth->addr, ETH_ALEN) != 0) {
1596 wpa_printf(MSG_DEBUG, "FT: Target AP address in the "
1597 "RRB Request does not match with own "
1602 if (wpa_ft_rrb_rx_request(wpa_auth, frame->ap_address,
1603 sta_addr, pos, end - pos) < 0)
1605 } else if (frame->packet_type == FT_PACKET_RESPONSE) {
1608 if (end - pos < 2) {
1609 wpa_printf(MSG_DEBUG, "FT: Not enough room for status "
1610 "code in RRB Response");
1613 status_code = WPA_GET_LE16(pos);
1616 wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Response "
1617 "(status_code=%d)", status_code);
1619 if (wpa_ft_action_send(wpa_auth, sta_addr, start, alen) < 0)
1622 wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with unknown "
1623 "packet_type %d", frame->packet_type);
1631 static void wpa_ft_generate_pmk_r1(struct wpa_authenticator *wpa_auth,
1632 struct wpa_ft_pmk_r0_sa *pmk_r0,
1633 struct ft_remote_r1kh *r1kh,
1634 const u8 *s1kh_id, int pairwise)
1636 struct ft_r0kh_r1kh_push_frame frame, f;
1639 os_memset(&frame, 0, sizeof(frame));
1640 frame.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
1641 frame.packet_type = FT_PACKET_R0KH_R1KH_PUSH;
1642 frame.data_length = host_to_le16(FT_R0KH_R1KH_PUSH_DATA_LEN);
1643 os_memcpy(frame.ap_address, wpa_auth->addr, ETH_ALEN);
1645 /* aes_wrap() does not support inplace encryption, so use a temporary
1646 * buffer for the data. */
1647 os_memcpy(f.r1kh_id, r1kh->id, FT_R1KH_ID_LEN);
1648 os_memcpy(f.s1kh_id, s1kh_id, ETH_ALEN);
1649 os_memcpy(f.pmk_r0_name, pmk_r0->pmk_r0_name, WPA_PMK_NAME_LEN);
1650 wpa_derive_pmk_r1(pmk_r0->pmk_r0, pmk_r0->pmk_r0_name, r1kh->id,
1651 s1kh_id, f.pmk_r1, f.pmk_r1_name);
1652 wpa_printf(MSG_DEBUG, "FT: R1KH-ID " MACSTR, MAC2STR(r1kh->id));
1653 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", f.pmk_r1, PMK_LEN);
1654 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", f.pmk_r1_name,
1657 WPA_PUT_LE32(f.timestamp, now.sec);
1658 f.pairwise = pairwise;
1659 if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8,
1660 f.timestamp, frame.timestamp) < 0)
1663 wpa_ft_rrb_send(wpa_auth, r1kh->addr, (u8 *) &frame, sizeof(frame));
1667 void wpa_ft_push_pmk_r1(struct wpa_authenticator *wpa_auth, const u8 *addr)
1669 struct wpa_ft_pmk_r0_sa *r0;
1670 struct ft_remote_r1kh *r1kh;
1672 if (!wpa_auth->conf.pmk_r1_push)
1675 r0 = wpa_auth->ft_pmk_cache->pmk_r0;
1677 if (os_memcmp(r0->spa, addr, ETH_ALEN) == 0)
1682 if (r0 == NULL || r0->pmk_r1_pushed)
1684 r0->pmk_r1_pushed = 1;
1686 wpa_printf(MSG_DEBUG, "FT: Deriving and pushing PMK-R1 keys to R1KHs "
1687 "for STA " MACSTR, MAC2STR(addr));
1689 r1kh = wpa_auth->conf.r1kh_list;
1691 wpa_ft_generate_pmk_r1(wpa_auth, r0, r1kh, addr, r0->pairwise);
1696 #endif /* CONFIG_IEEE80211R */