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_wrap.h"
17 #include "crypto/random.h"
18 #include "ap_config.h"
19 #include "ieee802_11.h"
22 #include "wpa_auth_i.h"
25 #ifdef CONFIG_IEEE80211R
27 static int wpa_ft_send_rrb_auth_resp(struct wpa_state_machine *sm,
28 const u8 *current_ap, const u8 *sta_addr,
29 u16 status, const u8 *resp_ies,
33 static int wpa_ft_rrb_send(struct wpa_authenticator *wpa_auth, const u8 *dst,
34 const u8 *data, size_t data_len)
36 if (wpa_auth->cb.send_ether == NULL)
38 wpa_printf(MSG_DEBUG, "FT: RRB send to " MACSTR, MAC2STR(dst));
39 return wpa_auth->cb.send_ether(wpa_auth->cb.ctx, dst, ETH_P_RRB,
44 static int wpa_ft_action_send(struct wpa_authenticator *wpa_auth,
45 const u8 *dst, const u8 *data, size_t data_len)
47 if (wpa_auth->cb.send_ft_action == NULL)
49 return wpa_auth->cb.send_ft_action(wpa_auth->cb.ctx, dst,
54 static struct wpa_state_machine *
55 wpa_ft_add_sta(struct wpa_authenticator *wpa_auth, const u8 *sta_addr)
57 if (wpa_auth->cb.add_sta == NULL)
59 return wpa_auth->cb.add_sta(wpa_auth->cb.ctx, sta_addr);
63 static int wpa_ft_add_tspec(struct wpa_authenticator *wpa_auth,
65 u8 *tspec_ie, size_t tspec_ielen)
67 if (wpa_auth->cb.add_tspec == NULL) {
68 wpa_printf(MSG_DEBUG, "FT: add_tspec is not initialized");
71 return wpa_auth->cb.add_tspec(wpa_auth->cb.ctx, sta_addr, tspec_ie,
76 int wpa_write_mdie(struct wpa_auth_config *conf, u8 *buf, size_t len)
80 if (len < 2 + sizeof(struct rsn_mdie))
83 *pos++ = WLAN_EID_MOBILITY_DOMAIN;
84 *pos++ = MOBILITY_DOMAIN_ID_LEN + 1;
85 os_memcpy(pos, conf->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
86 pos += MOBILITY_DOMAIN_ID_LEN;
89 capab |= RSN_FT_CAPAB_FT_OVER_DS;
96 int wpa_write_ftie(struct wpa_auth_config *conf, const u8 *r0kh_id,
98 const u8 *anonce, const u8 *snonce,
99 u8 *buf, size_t len, const u8 *subelem,
102 u8 *pos = buf, *ielen;
103 struct rsn_ftie *hdr;
105 if (len < 2 + sizeof(*hdr) + 2 + FT_R1KH_ID_LEN + 2 + r0kh_id_len +
109 *pos++ = WLAN_EID_FAST_BSS_TRANSITION;
112 hdr = (struct rsn_ftie *) pos;
113 os_memset(hdr, 0, sizeof(*hdr));
115 WPA_PUT_LE16(hdr->mic_control, 0);
117 os_memcpy(hdr->anonce, anonce, WPA_NONCE_LEN);
119 os_memcpy(hdr->snonce, snonce, WPA_NONCE_LEN);
121 /* Optional Parameters */
122 *pos++ = FTIE_SUBELEM_R1KH_ID;
123 *pos++ = FT_R1KH_ID_LEN;
124 os_memcpy(pos, conf->r1_key_holder, FT_R1KH_ID_LEN);
125 pos += FT_R1KH_ID_LEN;
128 *pos++ = FTIE_SUBELEM_R0KH_ID;
129 *pos++ = r0kh_id_len;
130 os_memcpy(pos, r0kh_id, r0kh_id_len);
135 os_memcpy(pos, subelem, subelem_len);
139 *ielen = pos - buf - 2;
145 struct wpa_ft_pmk_r0_sa {
146 struct wpa_ft_pmk_r0_sa *next;
148 u8 pmk_r0_name[WPA_PMK_NAME_LEN];
150 int pairwise; /* Pairwise cipher suite, WPA_CIPHER_* */
151 /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */
155 struct wpa_ft_pmk_r1_sa {
156 struct wpa_ft_pmk_r1_sa *next;
158 u8 pmk_r1_name[WPA_PMK_NAME_LEN];
160 int pairwise; /* Pairwise cipher suite, WPA_CIPHER_* */
161 /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */
164 struct wpa_ft_pmk_cache {
165 struct wpa_ft_pmk_r0_sa *pmk_r0;
166 struct wpa_ft_pmk_r1_sa *pmk_r1;
169 struct wpa_ft_pmk_cache * wpa_ft_pmk_cache_init(void)
171 struct wpa_ft_pmk_cache *cache;
173 cache = os_zalloc(sizeof(*cache));
179 void wpa_ft_pmk_cache_deinit(struct wpa_ft_pmk_cache *cache)
181 struct wpa_ft_pmk_r0_sa *r0, *r0prev;
182 struct wpa_ft_pmk_r1_sa *r1, *r1prev;
188 os_memset(r0prev->pmk_r0, 0, PMK_LEN);
196 os_memset(r1prev->pmk_r1, 0, PMK_LEN);
204 static int wpa_ft_store_pmk_r0(struct wpa_authenticator *wpa_auth,
205 const u8 *spa, const u8 *pmk_r0,
206 const u8 *pmk_r0_name, int pairwise)
208 struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
209 struct wpa_ft_pmk_r0_sa *r0;
211 /* TODO: add expiration and limit on number of entries in cache */
213 r0 = os_zalloc(sizeof(*r0));
217 os_memcpy(r0->pmk_r0, pmk_r0, PMK_LEN);
218 os_memcpy(r0->pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN);
219 os_memcpy(r0->spa, spa, ETH_ALEN);
220 r0->pairwise = pairwise;
222 r0->next = cache->pmk_r0;
229 static int wpa_ft_fetch_pmk_r0(struct wpa_authenticator *wpa_auth,
230 const u8 *spa, const u8 *pmk_r0_name,
231 u8 *pmk_r0, int *pairwise)
233 struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
234 struct wpa_ft_pmk_r0_sa *r0;
238 if (os_memcmp(r0->spa, spa, ETH_ALEN) == 0 &&
239 os_memcmp_const(r0->pmk_r0_name, pmk_r0_name,
240 WPA_PMK_NAME_LEN) == 0) {
241 os_memcpy(pmk_r0, r0->pmk_r0, PMK_LEN);
243 *pairwise = r0->pairwise;
254 static int wpa_ft_store_pmk_r1(struct wpa_authenticator *wpa_auth,
255 const u8 *spa, const u8 *pmk_r1,
256 const u8 *pmk_r1_name, int pairwise)
258 struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
259 struct wpa_ft_pmk_r1_sa *r1;
261 /* TODO: add expiration and limit on number of entries in cache */
263 r1 = os_zalloc(sizeof(*r1));
267 os_memcpy(r1->pmk_r1, pmk_r1, PMK_LEN);
268 os_memcpy(r1->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN);
269 os_memcpy(r1->spa, spa, ETH_ALEN);
270 r1->pairwise = pairwise;
272 r1->next = cache->pmk_r1;
279 static int wpa_ft_fetch_pmk_r1(struct wpa_authenticator *wpa_auth,
280 const u8 *spa, const u8 *pmk_r1_name,
281 u8 *pmk_r1, int *pairwise)
283 struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
284 struct wpa_ft_pmk_r1_sa *r1;
288 if (os_memcmp(r1->spa, spa, ETH_ALEN) == 0 &&
289 os_memcmp_const(r1->pmk_r1_name, pmk_r1_name,
290 WPA_PMK_NAME_LEN) == 0) {
291 os_memcpy(pmk_r1, r1->pmk_r1, PMK_LEN);
293 *pairwise = r1->pairwise;
304 static int wpa_ft_pull_pmk_r1(struct wpa_state_machine *sm,
305 const u8 *ies, size_t ies_len,
306 const u8 *pmk_r0_name)
308 struct ft_remote_r0kh *r0kh;
309 struct ft_r0kh_r1kh_pull_frame frame, f;
311 r0kh = sm->wpa_auth->conf.r0kh_list;
313 if (r0kh->id_len == sm->r0kh_id_len &&
314 os_memcmp_const(r0kh->id, sm->r0kh_id, sm->r0kh_id_len) ==
320 wpa_hexdump(MSG_DEBUG, "FT: Did not find R0KH-ID",
321 sm->r0kh_id, sm->r0kh_id_len);
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, sm->wpa_auth->addr, ETH_ALEN);
334 /* aes_wrap() does not support inplace encryption, so use a temporary
335 * buffer for the data. */
336 if (random_get_bytes(f.nonce, FT_R0KH_R1KH_PULL_NONCE_LEN)) {
337 wpa_printf(MSG_DEBUG, "FT: Failed to get random data for "
341 os_memcpy(sm->ft_pending_pull_nonce, f.nonce,
342 FT_R0KH_R1KH_PULL_NONCE_LEN);
343 os_memcpy(f.pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN);
344 os_memcpy(f.r1kh_id, sm->wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN);
345 os_memcpy(f.s1kh_id, sm->addr, ETH_ALEN);
346 os_memset(f.pad, 0, sizeof(f.pad));
348 if (aes_wrap(r0kh->key, sizeof(r0kh->key),
349 (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8,
350 f.nonce, frame.nonce) < 0)
353 wpabuf_free(sm->ft_pending_req_ies);
354 sm->ft_pending_req_ies = wpabuf_alloc_copy(ies, ies_len);
355 if (sm->ft_pending_req_ies == NULL)
358 wpa_ft_rrb_send(sm->wpa_auth, r0kh->addr, (u8 *) &frame, sizeof(frame));
364 int wpa_auth_derive_ptk_ft(struct wpa_state_machine *sm, const u8 *pmk,
367 u8 pmk_r0[PMK_LEN], pmk_r0_name[WPA_PMK_NAME_LEN];
369 u8 ptk_name[WPA_PMK_NAME_LEN];
370 const u8 *mdid = sm->wpa_auth->conf.mobility_domain;
371 const u8 *r0kh = sm->wpa_auth->conf.r0_key_holder;
372 size_t r0kh_len = sm->wpa_auth->conf.r0_key_holder_len;
373 const u8 *r1kh = sm->wpa_auth->conf.r1_key_holder;
374 const u8 *ssid = sm->wpa_auth->conf.ssid;
375 size_t ssid_len = sm->wpa_auth->conf.ssid_len;
377 if (sm->xxkey_len == 0) {
378 wpa_printf(MSG_DEBUG, "FT: XXKey not available for key "
383 wpa_derive_pmk_r0(sm->xxkey, sm->xxkey_len, ssid, ssid_len, mdid,
384 r0kh, r0kh_len, sm->addr, pmk_r0, pmk_r0_name);
385 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", pmk_r0, PMK_LEN);
386 wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name", pmk_r0_name, WPA_PMK_NAME_LEN);
387 wpa_ft_store_pmk_r0(sm->wpa_auth, sm->addr, pmk_r0, pmk_r0_name,
390 wpa_derive_pmk_r1(pmk_r0, pmk_r0_name, r1kh, sm->addr,
391 pmk_r1, sm->pmk_r1_name);
392 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", pmk_r1, PMK_LEN);
393 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", sm->pmk_r1_name,
395 wpa_ft_store_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1, sm->pmk_r1_name,
398 return wpa_pmk_r1_to_ptk(pmk_r1, sm->SNonce, sm->ANonce, sm->addr,
399 sm->wpa_auth->addr, sm->pmk_r1_name,
400 ptk, ptk_name, sm->wpa_key_mgmt, sm->pairwise);
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)
435 if (pad_len && key_len < sizeof(keybuf)) {
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, sm->PTK.kek_len, key_len / 8, key,
470 #ifdef CONFIG_IEEE80211W
471 static u8 * wpa_ft_igtk_subelem(struct wpa_state_machine *sm, size_t *len)
474 struct wpa_group *gsm = sm->group;
477 /* Sub-elem ID[1] | Length[1] | KeyID[2] | IPN[6] | Key Length[1] |
479 subelem_len = 1 + 1 + 2 + 6 + 1 + WPA_IGTK_LEN + 8;
480 subelem = os_zalloc(subelem_len);
485 *pos++ = FTIE_SUBELEM_IGTK;
486 *pos++ = subelem_len - 2;
487 WPA_PUT_LE16(pos, gsm->GN_igtk);
489 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos);
491 *pos++ = WPA_IGTK_LEN;
492 if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len, WPA_IGTK_LEN / 8,
493 gsm->IGTK[gsm->GN_igtk - 4], pos)) {
501 #endif /* CONFIG_IEEE80211W */
504 static u8 * wpa_ft_process_rdie(struct wpa_state_machine *sm,
505 u8 *pos, u8 *end, u8 id, u8 descr_count,
506 const u8 *ies, size_t ies_len)
508 struct ieee802_11_elems parse;
509 struct rsn_rdie *rdie;
511 wpa_printf(MSG_DEBUG, "FT: Resource Request: id=%d descr_count=%d",
513 wpa_hexdump(MSG_MSGDUMP, "FT: Resource descriptor IE(s)",
516 if (end - pos < (int) sizeof(*rdie)) {
517 wpa_printf(MSG_ERROR, "FT: Not enough room for response RDIE");
521 *pos++ = WLAN_EID_RIC_DATA;
522 *pos++ = sizeof(*rdie);
523 rdie = (struct rsn_rdie *) pos;
525 rdie->descr_count = 0;
526 rdie->status_code = host_to_le16(WLAN_STATUS_SUCCESS);
527 pos += sizeof(*rdie);
529 if (ieee802_11_parse_elems((u8 *) ies, ies_len, &parse, 1) ==
531 wpa_printf(MSG_DEBUG, "FT: Failed to parse request IEs");
533 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
538 if (parse.wmm_tspec && sm->wpa_auth->conf.ap_mlme) {
539 struct wmm_tspec_element *tspec;
542 if (parse.wmm_tspec_len + 2 < (int) sizeof(*tspec)) {
543 wpa_printf(MSG_DEBUG, "FT: Too short WMM TSPEC IE "
544 "(%d)", (int) parse.wmm_tspec_len);
546 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
549 if (end - pos < (int) sizeof(*tspec)) {
550 wpa_printf(MSG_ERROR, "FT: Not enough room for "
553 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
556 tspec = (struct wmm_tspec_element *) pos;
557 os_memcpy(tspec, parse.wmm_tspec - 2, sizeof(*tspec));
558 res = wmm_process_tspec(tspec);
559 wpa_printf(MSG_DEBUG, "FT: ADDTS processing result: %d", res);
560 if (res == WMM_ADDTS_STATUS_INVALID_PARAMETERS)
562 host_to_le16(WLAN_STATUS_INVALID_PARAMETERS);
563 else if (res == WMM_ADDTS_STATUS_REFUSED)
565 host_to_le16(WLAN_STATUS_REQUEST_DECLINED);
567 /* TSPEC accepted; include updated TSPEC in response */
568 rdie->descr_count = 1;
569 pos += sizeof(*tspec);
573 #endif /* NEED_AP_MLME */
575 if (parse.wmm_tspec && !sm->wpa_auth->conf.ap_mlme) {
576 struct wmm_tspec_element *tspec;
579 tspec = (struct wmm_tspec_element *) pos;
580 os_memcpy(tspec, parse.wmm_tspec - 2, sizeof(*tspec));
581 res = wpa_ft_add_tspec(sm->wpa_auth, sm->addr, pos,
585 rdie->status_code = host_to_le16(res);
587 /* TSPEC accepted; include updated TSPEC in
589 rdie->descr_count = 1;
590 pos += sizeof(*tspec);
596 wpa_printf(MSG_DEBUG, "FT: No supported resource requested");
597 rdie->status_code = host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
602 static u8 * wpa_ft_process_ric(struct wpa_state_machine *sm, u8 *pos, u8 *end,
603 const u8 *ric, size_t ric_len)
605 const u8 *rpos, *start;
606 const struct rsn_rdie *rdie;
608 wpa_hexdump(MSG_MSGDUMP, "FT: RIC Request", ric, ric_len);
611 while (rpos + sizeof(*rdie) < ric + ric_len) {
612 if (rpos[0] != WLAN_EID_RIC_DATA || rpos[1] < sizeof(*rdie) ||
613 rpos + 2 + rpos[1] > ric + ric_len)
615 rdie = (const struct rsn_rdie *) (rpos + 2);
619 while (rpos + 2 <= ric + ric_len &&
620 rpos + 2 + rpos[1] <= ric + ric_len) {
621 if (rpos[0] == WLAN_EID_RIC_DATA)
625 pos = wpa_ft_process_rdie(sm, pos, end, rdie->id,
627 start, rpos - start);
634 u8 * wpa_sm_write_assoc_resp_ies(struct wpa_state_machine *sm, u8 *pos,
635 size_t max_len, int auth_alg,
636 const u8 *req_ies, size_t req_ies_len)
638 u8 *end, *mdie, *ftie, *rsnie = NULL, *r0kh_id, *subelem = NULL;
639 size_t mdie_len, ftie_len, rsnie_len = 0, r0kh_id_len, subelem_len = 0;
641 struct wpa_auth_config *conf;
642 struct rsn_ftie *_ftie;
643 struct wpa_ft_ies parse;
650 conf = &sm->wpa_auth->conf;
652 if (!wpa_key_mgmt_ft(sm->wpa_key_mgmt))
657 if (auth_alg == WLAN_AUTH_FT) {
659 * RSN (only present if this is a Reassociation Response and
660 * part of a fast BSS transition)
662 res = wpa_write_rsn_ie(conf, pos, end - pos, sm->pmk_r1_name);
670 /* Mobility Domain Information */
671 res = wpa_write_mdie(conf, pos, end - pos);
678 /* Fast BSS Transition Information */
679 if (auth_alg == WLAN_AUTH_FT) {
680 subelem = wpa_ft_gtk_subelem(sm, &subelem_len);
681 r0kh_id = sm->r0kh_id;
682 r0kh_id_len = sm->r0kh_id_len;
685 #ifdef CONFIG_IEEE80211W
686 if (sm->mgmt_frame_prot) {
690 igtk = wpa_ft_igtk_subelem(sm, &igtk_len);
695 nbuf = os_realloc(subelem, subelem_len + igtk_len);
702 os_memcpy(subelem + subelem_len, igtk, igtk_len);
703 subelem_len += igtk_len;
706 #endif /* CONFIG_IEEE80211W */
708 r0kh_id = conf->r0_key_holder;
709 r0kh_id_len = conf->r0_key_holder_len;
713 res = wpa_write_ftie(conf, r0kh_id, r0kh_id_len, anonce, snonce, pos,
714 end - pos, subelem, subelem_len);
722 os_free(sm->assoc_resp_ftie);
723 sm->assoc_resp_ftie = os_malloc(ftie_len);
724 if (sm->assoc_resp_ftie)
725 os_memcpy(sm->assoc_resp_ftie, ftie, ftie_len);
727 _ftie = (struct rsn_ftie *) (ftie + 2);
728 if (auth_alg == WLAN_AUTH_FT)
729 _ftie->mic_control[1] = 3; /* Information element count */
732 if (wpa_ft_parse_ies(req_ies, req_ies_len, &parse) == 0 && parse.ric) {
733 pos = wpa_ft_process_ric(sm, pos, end, parse.ric,
735 if (auth_alg == WLAN_AUTH_FT)
736 _ftie->mic_control[1] +=
737 ieee802_11_ie_count(ric_start,
740 if (ric_start == pos)
743 if (auth_alg == WLAN_AUTH_FT &&
744 wpa_ft_mic(sm->PTK.kck, sm->PTK.kck_len, sm->addr,
745 sm->wpa_auth->addr, 6,
746 mdie, mdie_len, ftie, ftie_len,
748 ric_start, ric_start ? pos - ric_start : 0,
750 wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
756 static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
758 enum wpa_alg alg, const u8 *addr, int idx,
759 u8 *key, size_t key_len)
761 if (wpa_auth->cb.set_key == NULL)
763 return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
768 void wpa_ft_install_ptk(struct wpa_state_machine *sm)
773 /* MLME-SETKEYS.request(PTK) */
774 alg = wpa_cipher_to_alg(sm->pairwise);
775 klen = wpa_cipher_key_len(sm->pairwise);
776 if (!wpa_cipher_valid_pairwise(sm->pairwise)) {
777 wpa_printf(MSG_DEBUG, "FT: Unknown pairwise alg 0x%x - skip "
778 "PTK configuration", sm->pairwise);
782 /* FIX: add STA entry to kernel/driver here? The set_key will fail
783 * most likely without this.. At the moment, STA entry is added only
784 * after association has been completed. This function will be called
785 * again after association to get the PTK configured, but that could be
786 * optimized by adding the STA entry earlier.
788 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
792 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
793 sm->pairwise_set = TRUE;
797 static int wpa_ft_process_auth_req(struct wpa_state_machine *sm,
798 const u8 *ies, size_t ies_len,
799 u8 **resp_ies, size_t *resp_ies_len)
801 struct rsn_mdie *mdie;
802 struct rsn_ftie *ftie;
803 u8 pmk_r1[PMK_LEN], pmk_r1_name[WPA_PMK_NAME_LEN];
804 u8 ptk_name[WPA_PMK_NAME_LEN];
805 struct wpa_auth_config *conf;
806 struct wpa_ft_ies parse;
815 sm->pmk_r1_name_valid = 0;
816 conf = &sm->wpa_auth->conf;
818 wpa_hexdump(MSG_DEBUG, "FT: Received authentication frame IEs",
821 if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
822 wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
823 return WLAN_STATUS_UNSPECIFIED_FAILURE;
826 mdie = (struct rsn_mdie *) parse.mdie;
827 if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
828 os_memcmp(mdie->mobility_domain,
829 sm->wpa_auth->conf.mobility_domain,
830 MOBILITY_DOMAIN_ID_LEN) != 0) {
831 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
832 return WLAN_STATUS_INVALID_MDIE;
835 ftie = (struct rsn_ftie *) parse.ftie;
836 if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
837 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
838 return WLAN_STATUS_INVALID_FTIE;
841 os_memcpy(sm->SNonce, ftie->snonce, WPA_NONCE_LEN);
843 if (parse.r0kh_id == NULL) {
844 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE - no R0KH-ID");
845 return WLAN_STATUS_INVALID_FTIE;
848 wpa_hexdump(MSG_DEBUG, "FT: STA R0KH-ID",
849 parse.r0kh_id, parse.r0kh_id_len);
850 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
851 sm->r0kh_id_len = parse.r0kh_id_len;
853 if (parse.rsn_pmkid == NULL) {
854 wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
855 return WLAN_STATUS_INVALID_PMKID;
858 wpa_hexdump(MSG_DEBUG, "FT: Requested PMKR0Name",
859 parse.rsn_pmkid, WPA_PMK_NAME_LEN);
860 wpa_derive_pmk_r1_name(parse.rsn_pmkid,
861 sm->wpa_auth->conf.r1_key_holder, sm->addr,
863 wpa_hexdump(MSG_DEBUG, "FT: Derived requested PMKR1Name",
864 pmk_r1_name, WPA_PMK_NAME_LEN);
866 if (wpa_ft_fetch_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1_name, pmk_r1,
868 if (wpa_ft_pull_pmk_r1(sm, ies, ies_len, parse.rsn_pmkid) < 0) {
869 wpa_printf(MSG_DEBUG, "FT: Did not have matching "
870 "PMK-R1 and unknown R0KH-ID");
871 return WLAN_STATUS_INVALID_PMKID;
874 return -1; /* Status pending */
877 wpa_hexdump_key(MSG_DEBUG, "FT: Selected PMK-R1", pmk_r1, PMK_LEN);
878 sm->pmk_r1_name_valid = 1;
879 os_memcpy(sm->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN);
881 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
882 wpa_printf(MSG_DEBUG, "FT: Failed to get random data for "
884 return WLAN_STATUS_UNSPECIFIED_FAILURE;
887 wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
888 sm->SNonce, WPA_NONCE_LEN);
889 wpa_hexdump(MSG_DEBUG, "FT: Generated ANonce",
890 sm->ANonce, WPA_NONCE_LEN);
892 if (wpa_pmk_r1_to_ptk(pmk_r1, sm->SNonce, sm->ANonce, sm->addr,
893 sm->wpa_auth->addr, pmk_r1_name,
894 &sm->PTK, ptk_name, sm->wpa_key_mgmt,
896 return WLAN_STATUS_UNSPECIFIED_FAILURE;
898 sm->pairwise = pairwise;
899 sm->PTK_valid = TRUE;
900 wpa_ft_install_ptk(sm);
902 buflen = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) +
903 2 + FT_R1KH_ID_LEN + 200;
904 *resp_ies = os_zalloc(buflen);
905 if (*resp_ies == NULL) {
906 return WLAN_STATUS_UNSPECIFIED_FAILURE;
910 end = *resp_ies + buflen;
912 ret = wpa_write_rsn_ie(conf, pos, end - pos, parse.rsn_pmkid);
916 return WLAN_STATUS_UNSPECIFIED_FAILURE;
920 ret = wpa_write_mdie(conf, pos, end - pos);
924 return WLAN_STATUS_UNSPECIFIED_FAILURE;
928 ret = wpa_write_ftie(conf, parse.r0kh_id, parse.r0kh_id_len,
929 sm->ANonce, sm->SNonce, pos, end - pos, NULL, 0);
933 return WLAN_STATUS_UNSPECIFIED_FAILURE;
937 *resp_ies_len = pos - *resp_ies;
939 return WLAN_STATUS_SUCCESS;
943 void wpa_ft_process_auth(struct wpa_state_machine *sm, const u8 *bssid,
944 u16 auth_transaction, const u8 *ies, size_t ies_len,
945 void (*cb)(void *ctx, const u8 *dst, const u8 *bssid,
946 u16 auth_transaction, u16 status,
947 const u8 *ies, size_t ies_len),
956 wpa_printf(MSG_DEBUG, "FT: Received authentication frame, but "
957 "WPA SM not available");
961 wpa_printf(MSG_DEBUG, "FT: Received authentication frame: STA=" MACSTR
962 " BSSID=" MACSTR " transaction=%d",
963 MAC2STR(sm->addr), MAC2STR(bssid), auth_transaction);
964 sm->ft_pending_cb = cb;
965 sm->ft_pending_cb_ctx = ctx;
966 sm->ft_pending_auth_transaction = auth_transaction;
967 res = wpa_ft_process_auth_req(sm, ies, ies_len, &resp_ies,
970 wpa_printf(MSG_DEBUG, "FT: Callback postponed until response is available");
975 wpa_printf(MSG_DEBUG, "FT: FT authentication response: dst=" MACSTR
976 " auth_transaction=%d status=%d",
977 MAC2STR(sm->addr), auth_transaction + 1, status);
978 wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
979 cb(ctx, sm->addr, bssid, auth_transaction + 1, status,
980 resp_ies, resp_ies_len);
985 u16 wpa_ft_validate_reassoc(struct wpa_state_machine *sm, const u8 *ies,
988 struct wpa_ft_ies parse;
989 struct rsn_mdie *mdie;
990 struct rsn_ftie *ftie;
991 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
996 return WLAN_STATUS_UNSPECIFIED_FAILURE;
998 wpa_hexdump(MSG_DEBUG, "FT: Reassoc Req IEs", ies, ies_len);
1000 if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
1001 wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
1002 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1005 if (parse.rsn == NULL) {
1006 wpa_printf(MSG_DEBUG, "FT: No RSNIE in Reassoc Req");
1007 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1010 if (parse.rsn_pmkid == NULL) {
1011 wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
1012 return WLAN_STATUS_INVALID_PMKID;
1015 if (os_memcmp_const(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN)
1017 wpa_printf(MSG_DEBUG, "FT: PMKID in Reassoc Req did not match "
1018 "with the PMKR1Name derived from auth request");
1019 return WLAN_STATUS_INVALID_PMKID;
1022 mdie = (struct rsn_mdie *) parse.mdie;
1023 if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
1024 os_memcmp(mdie->mobility_domain,
1025 sm->wpa_auth->conf.mobility_domain,
1026 MOBILITY_DOMAIN_ID_LEN) != 0) {
1027 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
1028 return WLAN_STATUS_INVALID_MDIE;
1031 ftie = (struct rsn_ftie *) parse.ftie;
1032 if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
1033 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
1034 return WLAN_STATUS_INVALID_FTIE;
1037 if (os_memcmp(ftie->snonce, sm->SNonce, WPA_NONCE_LEN) != 0) {
1038 wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
1039 wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
1040 ftie->snonce, WPA_NONCE_LEN);
1041 wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
1042 sm->SNonce, WPA_NONCE_LEN);
1046 if (os_memcmp(ftie->anonce, sm->ANonce, WPA_NONCE_LEN) != 0) {
1047 wpa_printf(MSG_DEBUG, "FT: ANonce mismatch in FTIE");
1048 wpa_hexdump(MSG_DEBUG, "FT: Received ANonce",
1049 ftie->anonce, WPA_NONCE_LEN);
1050 wpa_hexdump(MSG_DEBUG, "FT: Expected ANonce",
1051 sm->ANonce, WPA_NONCE_LEN);
1056 if (parse.r0kh_id == NULL) {
1057 wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
1061 if (parse.r0kh_id_len != sm->r0kh_id_len ||
1062 os_memcmp_const(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0)
1064 wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
1065 "the current R0KH-ID");
1066 wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
1067 parse.r0kh_id, parse.r0kh_id_len);
1068 wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
1069 sm->r0kh_id, sm->r0kh_id_len);
1073 if (parse.r1kh_id == NULL) {
1074 wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
1078 if (os_memcmp_const(parse.r1kh_id, sm->wpa_auth->conf.r1_key_holder,
1079 FT_R1KH_ID_LEN) != 0) {
1080 wpa_printf(MSG_DEBUG, "FT: Unknown R1KH-ID used in "
1082 wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID in FTIE",
1083 parse.r1kh_id, FT_R1KH_ID_LEN);
1084 wpa_hexdump(MSG_DEBUG, "FT: Expected R1KH-ID",
1085 sm->wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN);
1089 if (parse.rsn_pmkid == NULL ||
1090 os_memcmp_const(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN))
1092 wpa_printf(MSG_DEBUG, "FT: No matching PMKR1Name (PMKID) in "
1093 "RSNIE (pmkid=%d)", !!parse.rsn_pmkid);
1099 count += ieee802_11_ie_count(parse.ric, parse.ric_len);
1100 if (ftie->mic_control[1] != count) {
1101 wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in MIC "
1102 "Control: received %u expected %u",
1103 ftie->mic_control[1], count);
1107 if (wpa_ft_mic(sm->PTK.kck, sm->PTK.kck_len, sm->addr,
1108 sm->wpa_auth->addr, 5,
1109 parse.mdie - 2, parse.mdie_len + 2,
1110 parse.ftie - 2, parse.ftie_len + 2,
1111 parse.rsn - 2, parse.rsn_len + 2,
1112 parse.ric, parse.ric_len,
1114 wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
1115 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1118 if (os_memcmp_const(mic, ftie->mic, mic_len) != 0) {
1119 wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
1120 wpa_printf(MSG_DEBUG, "FT: addr=" MACSTR " auth_addr=" MACSTR,
1121 MAC2STR(sm->addr), MAC2STR(sm->wpa_auth->addr));
1122 wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC",
1123 ftie->mic, mic_len);
1124 wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, mic_len);
1125 wpa_hexdump(MSG_MSGDUMP, "FT: MDIE",
1126 parse.mdie - 2, parse.mdie_len + 2);
1127 wpa_hexdump(MSG_MSGDUMP, "FT: FTIE",
1128 parse.ftie - 2, parse.ftie_len + 2);
1129 wpa_hexdump(MSG_MSGDUMP, "FT: RSN",
1130 parse.rsn - 2, parse.rsn_len + 2);
1131 return WLAN_STATUS_INVALID_FTIE;
1134 return WLAN_STATUS_SUCCESS;
1138 int wpa_ft_action_rx(struct wpa_state_machine *sm, const u8 *data, size_t len)
1140 const u8 *sta_addr, *target_ap;
1144 struct ft_rrb_frame *frame;
1150 * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
1151 * FT Request action frame body[variable]
1155 wpa_printf(MSG_DEBUG, "FT: Too short FT Action frame "
1156 "(len=%lu)", (unsigned long) len);
1161 sta_addr = data + 2;
1162 target_ap = data + 8;
1166 wpa_printf(MSG_DEBUG, "FT: Received FT Action frame (STA=" MACSTR
1167 " Target AP=" MACSTR " Action=%d)",
1168 MAC2STR(sta_addr), MAC2STR(target_ap), action);
1170 if (os_memcmp(sta_addr, sm->addr, ETH_ALEN) != 0) {
1171 wpa_printf(MSG_DEBUG, "FT: Mismatch in FT Action STA address: "
1172 "STA=" MACSTR " STA-Address=" MACSTR,
1173 MAC2STR(sm->addr), MAC2STR(sta_addr));
1178 * Do some sanity checking on the target AP address (not own and not
1179 * broadcast. This could be extended to filter based on a list of known
1180 * APs in the MD (if such a list were configured).
1182 if ((target_ap[0] & 0x01) ||
1183 os_memcmp(target_ap, sm->wpa_auth->addr, ETH_ALEN) == 0) {
1184 wpa_printf(MSG_DEBUG, "FT: Invalid Target AP in FT Action "
1189 wpa_hexdump(MSG_MSGDUMP, "FT: Action frame body", ies, ies_len);
1191 /* RRB - Forward action frame to the target AP */
1192 frame = os_malloc(sizeof(*frame) + len);
1195 frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
1196 frame->packet_type = FT_PACKET_REQUEST;
1197 frame->action_length = host_to_le16(len);
1198 os_memcpy(frame->ap_address, sm->wpa_auth->addr, ETH_ALEN);
1199 os_memcpy(frame + 1, data, len);
1201 wpa_ft_rrb_send(sm->wpa_auth, target_ap, (u8 *) frame,
1202 sizeof(*frame) + len);
1209 static void wpa_ft_rrb_rx_request_cb(void *ctx, const u8 *dst, const u8 *bssid,
1210 u16 auth_transaction, u16 resp,
1211 const u8 *ies, size_t ies_len)
1213 struct wpa_state_machine *sm = ctx;
1214 wpa_printf(MSG_DEBUG, "FT: Over-the-DS RX request cb for " MACSTR,
1216 wpa_ft_send_rrb_auth_resp(sm, sm->ft_pending_current_ap, sm->addr,
1217 WLAN_STATUS_SUCCESS, ies, ies_len);
1221 static int wpa_ft_rrb_rx_request(struct wpa_authenticator *wpa_auth,
1222 const u8 *current_ap, const u8 *sta_addr,
1223 const u8 *body, size_t len)
1225 struct wpa_state_machine *sm;
1228 size_t resp_ies_len;
1231 sm = wpa_ft_add_sta(wpa_auth, sta_addr);
1233 wpa_printf(MSG_DEBUG, "FT: Failed to add new STA based on "
1238 wpa_hexdump(MSG_MSGDUMP, "FT: RRB Request Frame body", body, len);
1240 sm->ft_pending_cb = wpa_ft_rrb_rx_request_cb;
1241 sm->ft_pending_cb_ctx = sm;
1242 os_memcpy(sm->ft_pending_current_ap, current_ap, ETH_ALEN);
1243 res = wpa_ft_process_auth_req(sm, body, len, &resp_ies,
1246 wpa_printf(MSG_DEBUG, "FT: No immediate response available - wait for pull response");
1251 res = wpa_ft_send_rrb_auth_resp(sm, current_ap, sta_addr, status,
1252 resp_ies, resp_ies_len);
1258 static int wpa_ft_send_rrb_auth_resp(struct wpa_state_machine *sm,
1259 const u8 *current_ap, const u8 *sta_addr,
1260 u16 status, const u8 *resp_ies,
1261 size_t resp_ies_len)
1263 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
1265 struct ft_rrb_frame *frame;
1268 wpa_printf(MSG_DEBUG, "FT: RRB authentication response: STA=" MACSTR
1269 " CurrentAP=" MACSTR " status=%d",
1270 MAC2STR(sm->addr), MAC2STR(current_ap), status);
1271 wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
1273 /* RRB - Forward action frame response to the Current AP */
1276 * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
1277 * Status_Code[2] FT Request action frame body[variable]
1279 rlen = 2 + 2 * ETH_ALEN + 2 + resp_ies_len;
1281 frame = os_malloc(sizeof(*frame) + rlen);
1284 frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
1285 frame->packet_type = FT_PACKET_RESPONSE;
1286 frame->action_length = host_to_le16(rlen);
1287 os_memcpy(frame->ap_address, wpa_auth->addr, ETH_ALEN);
1288 pos = (u8 *) (frame + 1);
1289 *pos++ = WLAN_ACTION_FT;
1290 *pos++ = 2; /* Action: Response */
1291 os_memcpy(pos, sta_addr, ETH_ALEN);
1293 os_memcpy(pos, wpa_auth->addr, ETH_ALEN);
1295 WPA_PUT_LE16(pos, status);
1298 os_memcpy(pos, resp_ies, resp_ies_len);
1300 wpa_ft_rrb_send(wpa_auth, current_ap, (u8 *) frame,
1301 sizeof(*frame) + rlen);
1308 static int wpa_ft_rrb_rx_pull(struct wpa_authenticator *wpa_auth,
1310 const u8 *data, size_t data_len)
1312 struct ft_r0kh_r1kh_pull_frame f;
1315 struct ft_remote_r1kh *r1kh;
1316 struct ft_r0kh_r1kh_resp_frame resp, r;
1320 wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull");
1322 if (data_len < sizeof(f))
1325 r1kh = wpa_auth->conf.r1kh_list;
1327 if (os_memcmp(r1kh->addr, src_addr, ETH_ALEN) == 0)
1332 wpa_printf(MSG_DEBUG, "FT: No matching R1KH address found for "
1333 "PMK-R1 pull source address " MACSTR,
1338 crypt = data + offsetof(struct ft_r0kh_r1kh_pull_frame, nonce);
1339 os_memset(&f, 0, sizeof(f));
1340 plain = ((u8 *) &f) + offsetof(struct ft_r0kh_r1kh_pull_frame, nonce);
1341 /* aes_unwrap() does not support inplace decryption, so use a temporary
1342 * buffer for the data. */
1343 if (aes_unwrap(r1kh->key, sizeof(r1kh->key),
1344 (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8,
1345 crypt, plain) < 0) {
1346 wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull "
1347 "request from " MACSTR, MAC2STR(src_addr));
1351 wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce",
1352 f.nonce, sizeof(f.nonce));
1353 wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR0Name",
1354 f.pmk_r0_name, WPA_PMK_NAME_LEN);
1355 wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR " S1KH-ID="
1356 MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));
1358 os_memset(&resp, 0, sizeof(resp));
1359 resp.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
1360 resp.packet_type = FT_PACKET_R0KH_R1KH_RESP;
1361 resp.data_length = host_to_le16(FT_R0KH_R1KH_RESP_DATA_LEN);
1362 os_memcpy(resp.ap_address, wpa_auth->addr, ETH_ALEN);
1364 /* aes_wrap() does not support inplace encryption, so use a temporary
1365 * buffer for the data. */
1366 os_memcpy(r.nonce, f.nonce, sizeof(f.nonce));
1367 os_memcpy(r.r1kh_id, f.r1kh_id, FT_R1KH_ID_LEN);
1368 os_memcpy(r.s1kh_id, f.s1kh_id, ETH_ALEN);
1369 if (wpa_ft_fetch_pmk_r0(wpa_auth, f.s1kh_id, f.pmk_r0_name, pmk_r0,
1371 wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name found for "
1376 wpa_derive_pmk_r1(pmk_r0, f.pmk_r0_name, f.r1kh_id, f.s1kh_id,
1377 r.pmk_r1, r.pmk_r1_name);
1378 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", r.pmk_r1, PMK_LEN);
1379 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", r.pmk_r1_name,
1381 r.pairwise = host_to_le16(pairwise);
1382 os_memset(r.pad, 0, sizeof(r.pad));
1384 if (aes_wrap(r1kh->key, sizeof(r1kh->key),
1385 (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8,
1386 r.nonce, resp.nonce) < 0) {
1387 os_memset(pmk_r0, 0, PMK_LEN);
1391 os_memset(pmk_r0, 0, PMK_LEN);
1393 wpa_ft_rrb_send(wpa_auth, src_addr, (u8 *) &resp, sizeof(resp));
1399 static void ft_pull_resp_cb_finish(void *eloop_ctx, void *timeout_ctx)
1401 struct wpa_state_machine *sm = eloop_ctx;
1404 size_t resp_ies_len;
1407 res = wpa_ft_process_auth_req(sm, wpabuf_head(sm->ft_pending_req_ies),
1408 wpabuf_len(sm->ft_pending_req_ies),
1409 &resp_ies, &resp_ies_len);
1410 wpabuf_free(sm->ft_pending_req_ies);
1411 sm->ft_pending_req_ies = NULL;
1413 res = WLAN_STATUS_UNSPECIFIED_FAILURE;
1415 wpa_printf(MSG_DEBUG, "FT: Postponed auth callback result for " MACSTR
1416 " - status %u", MAC2STR(sm->addr), status);
1418 sm->ft_pending_cb(sm->ft_pending_cb_ctx, sm->addr, sm->wpa_auth->addr,
1419 sm->ft_pending_auth_transaction + 1, status,
1420 resp_ies, resp_ies_len);
1425 static int ft_pull_resp_cb(struct wpa_state_machine *sm, void *ctx)
1427 struct ft_r0kh_r1kh_resp_frame *frame = ctx;
1429 if (os_memcmp(frame->s1kh_id, sm->addr, ETH_ALEN) != 0)
1431 if (os_memcmp(frame->nonce, sm->ft_pending_pull_nonce,
1432 FT_R0KH_R1KH_PULL_NONCE_LEN) != 0)
1434 if (sm->ft_pending_cb == NULL || sm->ft_pending_req_ies == NULL)
1437 wpa_printf(MSG_DEBUG, "FT: Response to a pending pull request for "
1438 MACSTR " - process from timeout", MAC2STR(sm->addr));
1439 eloop_register_timeout(0, 0, ft_pull_resp_cb_finish, sm, NULL);
1444 static int wpa_ft_rrb_rx_resp(struct wpa_authenticator *wpa_auth,
1446 const u8 *data, size_t data_len)
1448 struct ft_r0kh_r1kh_resp_frame f;
1451 struct ft_remote_r0kh *r0kh;
1454 wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull response");
1456 if (data_len < sizeof(f))
1459 r0kh = wpa_auth->conf.r0kh_list;
1461 if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0)
1466 wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for "
1467 "PMK-R0 pull response source address " MACSTR,
1472 crypt = data + offsetof(struct ft_r0kh_r1kh_resp_frame, nonce);
1473 os_memset(&f, 0, sizeof(f));
1474 plain = ((u8 *) &f) + offsetof(struct ft_r0kh_r1kh_resp_frame, nonce);
1475 /* aes_unwrap() does not support inplace decryption, so use a temporary
1476 * buffer for the data. */
1477 if (aes_unwrap(r0kh->key, sizeof(r0kh->key),
1478 (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8,
1479 crypt, plain) < 0) {
1480 wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull "
1481 "response from " MACSTR, MAC2STR(src_addr));
1485 if (os_memcmp_const(f.r1kh_id, wpa_auth->conf.r1_key_holder,
1486 FT_R1KH_ID_LEN) != 0) {
1487 wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull response did not use a "
1488 "matching R1KH-ID");
1492 pairwise = le_to_host16(f.pairwise);
1493 wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce",
1494 f.nonce, sizeof(f.nonce));
1495 wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - 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 pull - PMK-R1",
1500 wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR1Name",
1501 f.pmk_r1_name, WPA_PMK_NAME_LEN);
1503 res = wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name,
1505 wpa_printf(MSG_DEBUG, "FT: Look for pending pull request");
1506 wpa_auth_for_each_sta(wpa_auth, ft_pull_resp_cb, &f);
1507 os_memset(f.pmk_r1, 0, PMK_LEN);
1509 return res ? 0 : -1;
1513 static int wpa_ft_rrb_rx_push(struct wpa_authenticator *wpa_auth,
1515 const u8 *data, size_t data_len)
1517 struct ft_r0kh_r1kh_push_frame f;
1520 struct ft_remote_r0kh *r0kh;
1525 wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 push");
1527 if (data_len < sizeof(f))
1530 r0kh = wpa_auth->conf.r0kh_list;
1532 if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0)
1537 wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for "
1538 "PMK-R0 push source address " MACSTR,
1543 crypt = data + offsetof(struct ft_r0kh_r1kh_push_frame, timestamp);
1544 os_memset(&f, 0, sizeof(f));
1545 plain = ((u8 *) &f) + offsetof(struct ft_r0kh_r1kh_push_frame,
1547 /* aes_unwrap() does not support inplace decryption, so use a temporary
1548 * buffer for the data. */
1549 if (aes_unwrap(r0kh->key, sizeof(r0kh->key),
1550 (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8,
1551 crypt, plain) < 0) {
1552 wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 push from "
1553 MACSTR, MAC2STR(src_addr));
1558 tsend = WPA_GET_LE32(f.timestamp);
1559 if ((now.sec > tsend && now.sec - tsend > 60) ||
1560 (now.sec < tsend && tsend - now.sec > 60)) {
1561 wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not have a valid "
1562 "timestamp: sender time %d own time %d\n",
1563 (int) tsend, (int) now.sec);
1567 if (os_memcmp_const(f.r1kh_id, wpa_auth->conf.r1_key_holder,
1568 FT_R1KH_ID_LEN) != 0) {
1569 wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not use a matching "
1570 "R1KH-ID (received " MACSTR " own " MACSTR ")",
1572 MAC2STR(wpa_auth->conf.r1_key_holder));
1576 pairwise = le_to_host16(f.pairwise);
1577 wpa_printf(MSG_DEBUG, "FT: PMK-R1 push - R1KH-ID=" MACSTR " S1KH-ID="
1578 MACSTR " pairwise=0x%x",
1579 MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id), pairwise);
1580 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 push - PMK-R1",
1582 wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 push - PMKR1Name",
1583 f.pmk_r1_name, WPA_PMK_NAME_LEN);
1585 wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name,
1587 os_memset(f.pmk_r1, 0, PMK_LEN);
1593 int wpa_ft_rrb_rx(struct wpa_authenticator *wpa_auth, const u8 *src_addr,
1594 const u8 *data, size_t data_len)
1596 struct ft_rrb_frame *frame;
1598 const u8 *pos, *end, *start;
1600 const u8 *sta_addr, *target_ap_addr;
1602 wpa_printf(MSG_DEBUG, "FT: RRB received frame from remote AP " MACSTR,
1605 if (data_len < sizeof(*frame)) {
1606 wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (data_len=%lu)",
1607 (unsigned long) data_len);
1612 frame = (struct ft_rrb_frame *) pos;
1613 pos += sizeof(*frame);
1615 alen = le_to_host16(frame->action_length);
1616 wpa_printf(MSG_DEBUG, "FT: RRB frame - frame_type=%d packet_type=%d "
1617 "action_length=%d ap_address=" MACSTR,
1618 frame->frame_type, frame->packet_type, alen,
1619 MAC2STR(frame->ap_address));
1621 if (frame->frame_type != RSN_REMOTE_FRAME_TYPE_FT_RRB) {
1622 /* Discard frame per IEEE Std 802.11r-2008, 11A.10.3 */
1623 wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with "
1624 "unrecognized type %d", frame->frame_type);
1628 if (alen > data_len - sizeof(*frame)) {
1629 wpa_printf(MSG_DEBUG, "FT: RRB frame too short for action "
1634 if (frame->packet_type == FT_PACKET_R0KH_R1KH_PULL)
1635 return wpa_ft_rrb_rx_pull(wpa_auth, src_addr, data, data_len);
1636 if (frame->packet_type == FT_PACKET_R0KH_R1KH_RESP)
1637 return wpa_ft_rrb_rx_resp(wpa_auth, src_addr, data, data_len);
1638 if (frame->packet_type == FT_PACKET_R0KH_R1KH_PUSH)
1639 return wpa_ft_rrb_rx_push(wpa_auth, src_addr, data, data_len);
1641 wpa_hexdump(MSG_MSGDUMP, "FT: RRB - FT Action frame", pos, alen);
1643 if (alen < 1 + 1 + 2 * ETH_ALEN) {
1644 wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (not enough "
1645 "room for Action Frame body); alen=%lu",
1646 (unsigned long) alen);
1652 if (*pos != WLAN_ACTION_FT) {
1653 wpa_printf(MSG_DEBUG, "FT: Unexpected Action frame category "
1662 target_ap_addr = pos;
1664 wpa_printf(MSG_DEBUG, "FT: RRB Action Frame: action=%d sta_addr="
1665 MACSTR " target_ap_addr=" MACSTR,
1666 action, MAC2STR(sta_addr), MAC2STR(target_ap_addr));
1668 if (frame->packet_type == FT_PACKET_REQUEST) {
1669 wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Request");
1672 wpa_printf(MSG_DEBUG, "FT: Unexpected Action %d in "
1673 "RRB Request", action);
1677 if (os_memcmp(target_ap_addr, wpa_auth->addr, ETH_ALEN) != 0) {
1678 wpa_printf(MSG_DEBUG, "FT: Target AP address in the "
1679 "RRB Request does not match with own "
1684 if (wpa_ft_rrb_rx_request(wpa_auth, frame->ap_address,
1685 sta_addr, pos, end - pos) < 0)
1687 } else if (frame->packet_type == FT_PACKET_RESPONSE) {
1690 if (end - pos < 2) {
1691 wpa_printf(MSG_DEBUG, "FT: Not enough room for status "
1692 "code in RRB Response");
1695 status_code = WPA_GET_LE16(pos);
1698 wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Response "
1699 "(status_code=%d)", status_code);
1701 if (wpa_ft_action_send(wpa_auth, sta_addr, start, alen) < 0)
1704 wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with unknown "
1705 "packet_type %d", frame->packet_type);
1710 wpa_hexdump(MSG_DEBUG, "FT: Ignore extra data in end",
1718 static void wpa_ft_generate_pmk_r1(struct wpa_authenticator *wpa_auth,
1719 struct wpa_ft_pmk_r0_sa *pmk_r0,
1720 struct ft_remote_r1kh *r1kh,
1721 const u8 *s1kh_id, int pairwise)
1723 struct ft_r0kh_r1kh_push_frame frame, f;
1728 os_memset(&frame, 0, sizeof(frame));
1729 frame.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
1730 frame.packet_type = FT_PACKET_R0KH_R1KH_PUSH;
1731 frame.data_length = host_to_le16(FT_R0KH_R1KH_PUSH_DATA_LEN);
1732 os_memcpy(frame.ap_address, wpa_auth->addr, ETH_ALEN);
1734 /* aes_wrap() does not support inplace encryption, so use a temporary
1735 * buffer for the data. */
1736 os_memcpy(f.r1kh_id, r1kh->id, FT_R1KH_ID_LEN);
1737 os_memcpy(f.s1kh_id, s1kh_id, ETH_ALEN);
1738 os_memcpy(f.pmk_r0_name, pmk_r0->pmk_r0_name, WPA_PMK_NAME_LEN);
1739 wpa_derive_pmk_r1(pmk_r0->pmk_r0, pmk_r0->pmk_r0_name, r1kh->id,
1740 s1kh_id, f.pmk_r1, f.pmk_r1_name);
1741 wpa_printf(MSG_DEBUG, "FT: R1KH-ID " MACSTR, MAC2STR(r1kh->id));
1742 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", f.pmk_r1, PMK_LEN);
1743 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", f.pmk_r1_name,
1746 WPA_PUT_LE32(f.timestamp, now.sec);
1747 f.pairwise = host_to_le16(pairwise);
1748 os_memset(f.pad, 0, sizeof(f.pad));
1749 plain = ((const u8 *) &f) + offsetof(struct ft_r0kh_r1kh_push_frame,
1751 crypt = ((u8 *) &frame) + offsetof(struct ft_r0kh_r1kh_push_frame,
1753 if (aes_wrap(r1kh->key, sizeof(r1kh->key),
1754 (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8,
1758 wpa_ft_rrb_send(wpa_auth, r1kh->addr, (u8 *) &frame, sizeof(frame));
1762 void wpa_ft_push_pmk_r1(struct wpa_authenticator *wpa_auth, const u8 *addr)
1764 struct wpa_ft_pmk_r0_sa *r0;
1765 struct ft_remote_r1kh *r1kh;
1767 if (!wpa_auth->conf.pmk_r1_push)
1770 r0 = wpa_auth->ft_pmk_cache->pmk_r0;
1772 if (os_memcmp(r0->spa, addr, ETH_ALEN) == 0)
1777 if (r0 == NULL || r0->pmk_r1_pushed)
1779 r0->pmk_r1_pushed = 1;
1781 wpa_printf(MSG_DEBUG, "FT: Deriving and pushing PMK-R1 keys to R1KHs "
1782 "for STA " MACSTR, MAC2STR(addr));
1784 r1kh = wpa_auth->conf.r1kh_list;
1786 wpa_ft_generate_pmk_r1(wpa_auth, r0, r1kh, addr, r0->pairwise);
1791 #endif /* CONFIG_IEEE80211R */