1/* 2 * hostapd - IEEE 802.11r - Fast BSS Transition 3 * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi> 4 * 5 * This software may be distributed under the terms of the BSD license. 6 * See README for more details. 7 */ 8 9#include "utils/includes.h" 10 11#include "utils/common.h" 12#include "common/ieee802_11_defs.h" 13#include "common/ieee802_11_common.h" 14#include "crypto/aes_wrap.h" 15#include "crypto/random.h" 16#include "ap_config.h" 17#include "ieee802_11.h" 18#include "wmm.h" 19#include "wpa_auth.h" 20#include "wpa_auth_i.h" 21 22 23#ifdef CONFIG_IEEE80211R 24 25static int wpa_ft_rrb_send(struct wpa_authenticator *wpa_auth, const u8 *dst, 26 const u8 *data, size_t data_len) 27{ 28 if (wpa_auth->cb.send_ether == NULL) 29 return -1; 30 wpa_printf(MSG_DEBUG, "FT: RRB send to " MACSTR, MAC2STR(dst)); 31 return wpa_auth->cb.send_ether(wpa_auth->cb.ctx, dst, ETH_P_RRB, 32 data, data_len); 33} 34 35 36static int wpa_ft_action_send(struct wpa_authenticator *wpa_auth, 37 const u8 *dst, const u8 *data, size_t data_len) 38{ 39 if (wpa_auth->cb.send_ft_action == NULL) 40 return -1; 41 return wpa_auth->cb.send_ft_action(wpa_auth->cb.ctx, dst, 42 data, data_len); 43} 44 45 46static struct wpa_state_machine * 47wpa_ft_add_sta(struct wpa_authenticator *wpa_auth, const u8 *sta_addr) 48{ 49 if (wpa_auth->cb.add_sta == NULL) 50 return NULL; 51 return wpa_auth->cb.add_sta(wpa_auth->cb.ctx, sta_addr); 52} 53 54 55static int wpa_ft_add_tspec(struct wpa_authenticator *wpa_auth, 56 const u8 *sta_addr, 57 u8 *tspec_ie, size_t tspec_ielen) 58{ 59 if (wpa_auth->cb.add_tspec == NULL) { 60 wpa_printf(MSG_DEBUG, "FT: add_tspec is not initialized"); 61 return -1; 62 } 63 return wpa_auth->cb.add_tspec(wpa_auth->cb.ctx, sta_addr, tspec_ie, 64 tspec_ielen); 65} 66 67 68int wpa_write_mdie(struct wpa_auth_config *conf, u8 *buf, size_t len) 69{ 70 u8 *pos = buf; 71 u8 capab; 72 if (len < 2 + sizeof(struct rsn_mdie)) 73 return -1; 74 75 *pos++ = WLAN_EID_MOBILITY_DOMAIN; 76 *pos++ = MOBILITY_DOMAIN_ID_LEN + 1; 77 os_memcpy(pos, conf->mobility_domain, MOBILITY_DOMAIN_ID_LEN); 78 pos += MOBILITY_DOMAIN_ID_LEN; 79 capab = 0; 80 if (conf->ft_over_ds) 81 capab |= RSN_FT_CAPAB_FT_OVER_DS; 82 *pos++ = capab; 83 84 return pos - buf; 85} 86 87 88int wpa_write_ftie(struct wpa_auth_config *conf, const u8 *r0kh_id, 89 size_t r0kh_id_len, 90 const u8 *anonce, const u8 *snonce, 91 u8 *buf, size_t len, const u8 *subelem, 92 size_t subelem_len) 93{ 94 u8 *pos = buf, *ielen; 95 struct rsn_ftie *hdr; 96 97 if (len < 2 + sizeof(*hdr) + 2 + FT_R1KH_ID_LEN + 2 + r0kh_id_len + 98 subelem_len) 99 return -1; 100 101 *pos++ = WLAN_EID_FAST_BSS_TRANSITION; 102 ielen = pos++; 103 104 hdr = (struct rsn_ftie *) pos; 105 os_memset(hdr, 0, sizeof(*hdr)); 106 pos += sizeof(*hdr); 107 WPA_PUT_LE16(hdr->mic_control, 0); 108 if (anonce) 109 os_memcpy(hdr->anonce, anonce, WPA_NONCE_LEN); 110 if (snonce) 111 os_memcpy(hdr->snonce, snonce, WPA_NONCE_LEN); 112 113 /* Optional Parameters */ 114 *pos++ = FTIE_SUBELEM_R1KH_ID; 115 *pos++ = FT_R1KH_ID_LEN; 116 os_memcpy(pos, conf->r1_key_holder, FT_R1KH_ID_LEN); 117 pos += FT_R1KH_ID_LEN; 118 119 if (r0kh_id) { 120 *pos++ = FTIE_SUBELEM_R0KH_ID; 121 *pos++ = r0kh_id_len; 122 os_memcpy(pos, r0kh_id, r0kh_id_len); 123 pos += r0kh_id_len; 124 } 125 126 if (subelem) { 127 os_memcpy(pos, subelem, subelem_len); 128 pos += subelem_len; 129 } 130 131 *ielen = pos - buf - 2; 132 133 return pos - buf; 134} 135 136 137struct wpa_ft_pmk_r0_sa { 138 struct wpa_ft_pmk_r0_sa *next; 139 u8 pmk_r0[PMK_LEN]; 140 u8 pmk_r0_name[WPA_PMK_NAME_LEN]; 141 u8 spa[ETH_ALEN]; 142 int pairwise; /* Pairwise cipher suite, WPA_CIPHER_* */ 143 /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */ 144 int pmk_r1_pushed; 145}; 146 147struct wpa_ft_pmk_r1_sa { 148 struct wpa_ft_pmk_r1_sa *next; 149 u8 pmk_r1[PMK_LEN]; 150 u8 pmk_r1_name[WPA_PMK_NAME_LEN]; 151 u8 spa[ETH_ALEN]; 152 int pairwise; /* Pairwise cipher suite, WPA_CIPHER_* */ 153 /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */ 154}; 155 156struct wpa_ft_pmk_cache { 157 struct wpa_ft_pmk_r0_sa *pmk_r0; 158 struct wpa_ft_pmk_r1_sa *pmk_r1; 159}; 160 161struct wpa_ft_pmk_cache * wpa_ft_pmk_cache_init(void) 162{ 163 struct wpa_ft_pmk_cache *cache; 164 165 cache = os_zalloc(sizeof(*cache)); 166 167 return cache; 168} 169 170 171void wpa_ft_pmk_cache_deinit(struct wpa_ft_pmk_cache *cache) 172{ 173 struct wpa_ft_pmk_r0_sa *r0, *r0prev; 174 struct wpa_ft_pmk_r1_sa *r1, *r1prev; 175 176 r0 = cache->pmk_r0; 177 while (r0) { 178 r0prev = r0; 179 r0 = r0->next; 180 os_memset(r0prev->pmk_r0, 0, PMK_LEN); 181 os_free(r0prev); 182 } 183 184 r1 = cache->pmk_r1; 185 while (r1) { 186 r1prev = r1; 187 r1 = r1->next; 188 os_memset(r1prev->pmk_r1, 0, PMK_LEN); 189 os_free(r1prev); 190 } 191 192 os_free(cache); 193} 194 195 196static int wpa_ft_store_pmk_r0(struct wpa_authenticator *wpa_auth, 197 const u8 *spa, const u8 *pmk_r0, 198 const u8 *pmk_r0_name, int pairwise) 199{ 200 struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache; 201 struct wpa_ft_pmk_r0_sa *r0; 202 203 /* TODO: add expiration and limit on number of entries in cache */ 204 205 r0 = os_zalloc(sizeof(*r0)); 206 if (r0 == NULL) 207 return -1; 208 209 os_memcpy(r0->pmk_r0, pmk_r0, PMK_LEN); 210 os_memcpy(r0->pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN); 211 os_memcpy(r0->spa, spa, ETH_ALEN); 212 r0->pairwise = pairwise; 213 214 r0->next = cache->pmk_r0; 215 cache->pmk_r0 = r0; 216 217 return 0; 218} 219 220 221static int wpa_ft_fetch_pmk_r0(struct wpa_authenticator *wpa_auth, 222 const u8 *spa, const u8 *pmk_r0_name, 223 u8 *pmk_r0, int *pairwise) 224{ 225 struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache; 226 struct wpa_ft_pmk_r0_sa *r0; 227 228 r0 = cache->pmk_r0; 229 while (r0) { 230 if (os_memcmp(r0->spa, spa, ETH_ALEN) == 0 && 231 os_memcmp(r0->pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN) 232 == 0) { 233 os_memcpy(pmk_r0, r0->pmk_r0, PMK_LEN); 234 if (pairwise) 235 *pairwise = r0->pairwise; 236 return 0; 237 } 238 239 r0 = r0->next; 240 } 241 242 return -1; 243} 244 245 246static int wpa_ft_store_pmk_r1(struct wpa_authenticator *wpa_auth, 247 const u8 *spa, const u8 *pmk_r1, 248 const u8 *pmk_r1_name, int pairwise) 249{ 250 struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache; 251 struct wpa_ft_pmk_r1_sa *r1; 252 253 /* TODO: add expiration and limit on number of entries in cache */ 254 255 r1 = os_zalloc(sizeof(*r1)); 256 if (r1 == NULL) 257 return -1; 258 259 os_memcpy(r1->pmk_r1, pmk_r1, PMK_LEN); 260 os_memcpy(r1->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN); 261 os_memcpy(r1->spa, spa, ETH_ALEN); 262 r1->pairwise = pairwise; 263 264 r1->next = cache->pmk_r1; 265 cache->pmk_r1 = r1; 266 267 return 0; 268} 269 270 271static int wpa_ft_fetch_pmk_r1(struct wpa_authenticator *wpa_auth, 272 const u8 *spa, const u8 *pmk_r1_name, 273 u8 *pmk_r1, int *pairwise) 274{ 275 struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache; 276 struct wpa_ft_pmk_r1_sa *r1; 277 278 r1 = cache->pmk_r1; 279 while (r1) { 280 if (os_memcmp(r1->spa, spa, ETH_ALEN) == 0 && 281 os_memcmp(r1->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN) 282 == 0) { 283 os_memcpy(pmk_r1, r1->pmk_r1, PMK_LEN); 284 if (pairwise) 285 *pairwise = r1->pairwise; 286 return 0; 287 } 288 289 r1 = r1->next; 290 } 291 292 return -1; 293} 294 295 296static int wpa_ft_pull_pmk_r1(struct wpa_authenticator *wpa_auth, 297 const u8 *s1kh_id, const u8 *r0kh_id, 298 size_t r0kh_id_len, const u8 *pmk_r0_name) 299{ 300 struct ft_remote_r0kh *r0kh; 301 struct ft_r0kh_r1kh_pull_frame frame, f; 302 303 r0kh = wpa_auth->conf.r0kh_list; 304 while (r0kh) { 305 if (r0kh->id_len == r0kh_id_len && 306 os_memcmp(r0kh->id, r0kh_id, r0kh_id_len) == 0) 307 break; 308 r0kh = r0kh->next; 309 } 310 if (r0kh == NULL) 311 return -1; 312 313 wpa_printf(MSG_DEBUG, "FT: Send PMK-R1 pull request to remote R0KH " 314 "address " MACSTR, MAC2STR(r0kh->addr)); 315 316 os_memset(&frame, 0, sizeof(frame)); 317 frame.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB; 318 frame.packet_type = FT_PACKET_R0KH_R1KH_PULL; 319 frame.data_length = host_to_le16(FT_R0KH_R1KH_PULL_DATA_LEN); 320 os_memcpy(frame.ap_address, wpa_auth->addr, ETH_ALEN); 321 322 /* aes_wrap() does not support inplace encryption, so use a temporary 323 * buffer for the data. */ 324 if (random_get_bytes(f.nonce, sizeof(f.nonce))) { 325 wpa_printf(MSG_DEBUG, "FT: Failed to get random data for " 326 "nonce"); 327 return -1; 328 } 329 os_memcpy(f.pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN); 330 os_memcpy(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN); 331 os_memcpy(f.s1kh_id, s1kh_id, ETH_ALEN); 332 333 if (aes_wrap(r0kh->key, (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8, 334 f.nonce, frame.nonce) < 0) 335 return -1; 336 337 wpa_ft_rrb_send(wpa_auth, r0kh->addr, (u8 *) &frame, sizeof(frame)); 338 339 return 0; 340} 341 342 343int wpa_auth_derive_ptk_ft(struct wpa_state_machine *sm, const u8 *pmk, 344 struct wpa_ptk *ptk, size_t ptk_len) 345{ 346 u8 pmk_r0[PMK_LEN], pmk_r0_name[WPA_PMK_NAME_LEN]; 347 u8 pmk_r1[PMK_LEN]; 348 u8 ptk_name[WPA_PMK_NAME_LEN]; 349 const u8 *mdid = sm->wpa_auth->conf.mobility_domain; 350 const u8 *r0kh = sm->wpa_auth->conf.r0_key_holder; 351 size_t r0kh_len = sm->wpa_auth->conf.r0_key_holder_len; 352 const u8 *r1kh = sm->wpa_auth->conf.r1_key_holder; 353 const u8 *ssid = sm->wpa_auth->conf.ssid; 354 size_t ssid_len = sm->wpa_auth->conf.ssid_len; 355 356 357 if (sm->xxkey_len == 0) { 358 wpa_printf(MSG_DEBUG, "FT: XXKey not available for key " 359 "derivation"); 360 return -1; 361 } 362 363 wpa_derive_pmk_r0(sm->xxkey, sm->xxkey_len, ssid, ssid_len, mdid, 364 r0kh, r0kh_len, sm->addr, pmk_r0, pmk_r0_name); 365 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", pmk_r0, PMK_LEN); 366 wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name", pmk_r0_name, WPA_PMK_NAME_LEN); 367 wpa_ft_store_pmk_r0(sm->wpa_auth, sm->addr, pmk_r0, pmk_r0_name, 368 sm->pairwise); 369 370 wpa_derive_pmk_r1(pmk_r0, pmk_r0_name, r1kh, sm->addr, 371 pmk_r1, sm->pmk_r1_name); 372 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", pmk_r1, PMK_LEN); 373 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", sm->pmk_r1_name, 374 WPA_PMK_NAME_LEN); 375 wpa_ft_store_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1, sm->pmk_r1_name, 376 sm->pairwise); 377 378 wpa_pmk_r1_to_ptk(pmk_r1, sm->SNonce, sm->ANonce, sm->addr, 379 sm->wpa_auth->addr, sm->pmk_r1_name, 380 (u8 *) ptk, ptk_len, ptk_name); 381 wpa_hexdump_key(MSG_DEBUG, "FT: PTK", (u8 *) ptk, ptk_len); 382 wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN); 383 384 return 0; 385} 386 387 388static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth, 389 const u8 *addr, int idx, u8 *seq) 390{ 391 if (wpa_auth->cb.get_seqnum == NULL) 392 return -1; 393 return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq); 394} 395 396 397static u8 * wpa_ft_gtk_subelem(struct wpa_state_machine *sm, size_t *len) 398{ 399 u8 *subelem; 400 struct wpa_group *gsm = sm->group; 401 size_t subelem_len, pad_len; 402 const u8 *key; 403 size_t key_len; 404 u8 keybuf[32]; 405 406 key_len = gsm->GTK_len; 407 if (key_len > sizeof(keybuf)) 408 return NULL; 409 410 /* 411 * Pad key for AES Key Wrap if it is not multiple of 8 bytes or is less 412 * than 16 bytes. 413 */ 414 pad_len = key_len % 8; 415 if (pad_len) 416 pad_len = 8 - pad_len; 417 if (key_len + pad_len < 16) 418 pad_len += 8; 419 if (pad_len) { 420 os_memcpy(keybuf, gsm->GTK[gsm->GN - 1], key_len); 421 os_memset(keybuf + key_len, 0, pad_len); 422 keybuf[key_len] = 0xdd; 423 key_len += pad_len; 424 key = keybuf; 425 } else 426 key = gsm->GTK[gsm->GN - 1]; 427 428 /* 429 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] | 430 * Key[5..32]. 431 */ 432 subelem_len = 13 + key_len + 8; 433 subelem = os_zalloc(subelem_len); 434 if (subelem == NULL) 435 return NULL; 436 437 subelem[0] = FTIE_SUBELEM_GTK; 438 subelem[1] = 11 + key_len + 8; 439 /* Key ID in B0-B1 of Key Info */ 440 WPA_PUT_LE16(&subelem[2], gsm->GN & 0x03); 441 subelem[4] = gsm->GTK_len; 442 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, subelem + 5); 443 if (aes_wrap(sm->PTK.kek, key_len / 8, key, subelem + 13)) { 444 os_free(subelem); 445 return NULL; 446 } 447 448 *len = subelem_len; 449 return subelem; 450} 451 452 453#ifdef CONFIG_IEEE80211W 454static u8 * wpa_ft_igtk_subelem(struct wpa_state_machine *sm, size_t *len) 455{ 456 u8 *subelem, *pos; 457 struct wpa_group *gsm = sm->group; 458 size_t subelem_len; 459 460 /* Sub-elem ID[1] | Length[1] | KeyID[2] | IPN[6] | Key Length[1] | 461 * Key[16+8] */ 462 subelem_len = 1 + 1 + 2 + 6 + 1 + WPA_IGTK_LEN + 8; 463 subelem = os_zalloc(subelem_len); 464 if (subelem == NULL) 465 return NULL; 466 467 pos = subelem; 468 *pos++ = FTIE_SUBELEM_IGTK; 469 *pos++ = subelem_len - 2; 470 WPA_PUT_LE16(pos, gsm->GN_igtk); 471 pos += 2; 472 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos); 473 pos += 6; 474 *pos++ = WPA_IGTK_LEN; 475 if (aes_wrap(sm->PTK.kek, WPA_IGTK_LEN / 8, 476 gsm->IGTK[gsm->GN_igtk - 4], pos)) { 477 os_free(subelem); 478 return NULL; 479 } 480 481 *len = subelem_len; 482 return subelem; 483} 484#endif /* CONFIG_IEEE80211W */ 485 486 487static u8 * wpa_ft_process_rdie(struct wpa_state_machine *sm, 488 u8 *pos, u8 *end, u8 id, u8 descr_count, 489 const u8 *ies, size_t ies_len) 490{ 491 struct ieee802_11_elems parse; 492 struct rsn_rdie *rdie; 493 494 wpa_printf(MSG_DEBUG, "FT: Resource Request: id=%d descr_count=%d", 495 id, descr_count); 496 wpa_hexdump(MSG_MSGDUMP, "FT: Resource descriptor IE(s)", 497 ies, ies_len); 498 499 if (end - pos < (int) sizeof(*rdie)) { 500 wpa_printf(MSG_ERROR, "FT: Not enough room for response RDIE"); 501 return pos; 502 } 503 504 *pos++ = WLAN_EID_RIC_DATA; 505 *pos++ = sizeof(*rdie); 506 rdie = (struct rsn_rdie *) pos; 507 rdie->id = id; 508 rdie->descr_count = 0; 509 rdie->status_code = host_to_le16(WLAN_STATUS_SUCCESS); 510 pos += sizeof(*rdie); 511 512 if (ieee802_11_parse_elems((u8 *) ies, ies_len, &parse, 1) == 513 ParseFailed) { 514 wpa_printf(MSG_DEBUG, "FT: Failed to parse request IEs"); 515 rdie->status_code = 516 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE); 517 return pos; 518 } 519 520#ifdef NEED_AP_MLME 521 if (parse.wmm_tspec && sm->wpa_auth->conf.ap_mlme) { 522 struct wmm_tspec_element *tspec; 523 int res; 524 525 if (parse.wmm_tspec_len + 2 < (int) sizeof(*tspec)) { 526 wpa_printf(MSG_DEBUG, "FT: Too short WMM TSPEC IE " 527 "(%d)", (int) parse.wmm_tspec_len); 528 rdie->status_code = 529 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE); 530 return pos; 531 } 532 if (end - pos < (int) sizeof(*tspec)) { 533 wpa_printf(MSG_ERROR, "FT: Not enough room for " 534 "response TSPEC"); 535 rdie->status_code = 536 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE); 537 return pos; 538 } 539 tspec = (struct wmm_tspec_element *) pos; 540 os_memcpy(tspec, parse.wmm_tspec - 2, sizeof(*tspec)); 541 res = wmm_process_tspec(tspec); 542 wpa_printf(MSG_DEBUG, "FT: ADDTS processing result: %d", res); 543 if (res == WMM_ADDTS_STATUS_INVALID_PARAMETERS) 544 rdie->status_code = 545 host_to_le16(WLAN_STATUS_INVALID_PARAMETERS); 546 else if (res == WMM_ADDTS_STATUS_REFUSED) 547 rdie->status_code = 548 host_to_le16(WLAN_STATUS_REQUEST_DECLINED); 549 else { 550 /* TSPEC accepted; include updated TSPEC in response */ 551 rdie->descr_count = 1; 552 pos += sizeof(*tspec); 553 } 554 return pos; 555 } 556#endif /* NEED_AP_MLME */ 557 558 if (parse.wmm_tspec && !sm->wpa_auth->conf.ap_mlme) { 559 struct wmm_tspec_element *tspec; 560 int res; 561 562 tspec = (struct wmm_tspec_element *) pos; 563 os_memcpy(tspec, parse.wmm_tspec - 2, sizeof(*tspec)); 564 res = wpa_ft_add_tspec(sm->wpa_auth, sm->addr, pos, 565 sizeof(*tspec)); 566 if (res >= 0) { 567 if (res) 568 rdie->status_code = host_to_le16(res); 569 else { 570 /* TSPEC accepted; include updated TSPEC in 571 * response */ 572 rdie->descr_count = 1; 573 pos += sizeof(*tspec); 574 } 575 return pos; 576 } 577 } 578 579 wpa_printf(MSG_DEBUG, "FT: No supported resource requested"); 580 rdie->status_code = host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE); 581 return pos; 582} 583 584 585static u8 * wpa_ft_process_ric(struct wpa_state_machine *sm, u8 *pos, u8 *end, 586 const u8 *ric, size_t ric_len) 587{ 588 const u8 *rpos, *start; 589 const struct rsn_rdie *rdie; 590 591 wpa_hexdump(MSG_MSGDUMP, "FT: RIC Request", ric, ric_len); 592 593 rpos = ric; 594 while (rpos + sizeof(*rdie) < ric + ric_len) { 595 if (rpos[0] != WLAN_EID_RIC_DATA || rpos[1] < sizeof(*rdie) || 596 rpos + 2 + rpos[1] > ric + ric_len) 597 break; 598 rdie = (const struct rsn_rdie *) (rpos + 2); 599 rpos += 2 + rpos[1]; 600 start = rpos; 601 602 while (rpos + 2 <= ric + ric_len && 603 rpos + 2 + rpos[1] <= ric + ric_len) { 604 if (rpos[0] == WLAN_EID_RIC_DATA) 605 break; 606 rpos += 2 + rpos[1]; 607 } 608 pos = wpa_ft_process_rdie(sm, pos, end, rdie->id, 609 rdie->descr_count, 610 start, rpos - start); 611 } 612 613 return pos; 614} 615 616 617u8 * wpa_sm_write_assoc_resp_ies(struct wpa_state_machine *sm, u8 *pos, 618 size_t max_len, int auth_alg, 619 const u8 *req_ies, size_t req_ies_len) 620{ 621 u8 *end, *mdie, *ftie, *rsnie = NULL, *r0kh_id, *subelem = NULL; 622 size_t mdie_len, ftie_len, rsnie_len = 0, r0kh_id_len, subelem_len = 0; 623 int res; 624 struct wpa_auth_config *conf; 625 struct rsn_ftie *_ftie; 626 struct wpa_ft_ies parse; 627 u8 *ric_start; 628 u8 *anonce, *snonce; 629 630 if (sm == NULL) 631 return pos; 632 633 conf = &sm->wpa_auth->conf; 634 635 if (sm->wpa_key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X && 636 sm->wpa_key_mgmt != WPA_KEY_MGMT_FT_PSK) 637 return pos; 638 639 end = pos + max_len; 640 641 if (auth_alg == WLAN_AUTH_FT) { 642 /* 643 * RSN (only present if this is a Reassociation Response and 644 * part of a fast BSS transition) 645 */ 646 res = wpa_write_rsn_ie(conf, pos, end - pos, sm->pmk_r1_name); 647 if (res < 0) 648 return pos; 649 rsnie = pos; 650 rsnie_len = res; 651 pos += res; 652 } 653 654 /* Mobility Domain Information */ 655 res = wpa_write_mdie(conf, pos, end - pos); 656 if (res < 0) 657 return pos; 658 mdie = pos; 659 mdie_len = res; 660 pos += res; 661 662 /* Fast BSS Transition Information */ 663 if (auth_alg == WLAN_AUTH_FT) { 664 subelem = wpa_ft_gtk_subelem(sm, &subelem_len); 665 r0kh_id = sm->r0kh_id; 666 r0kh_id_len = sm->r0kh_id_len; 667 anonce = sm->ANonce; 668 snonce = sm->SNonce; 669#ifdef CONFIG_IEEE80211W 670 if (sm->mgmt_frame_prot) { 671 u8 *igtk; 672 size_t igtk_len; 673 u8 *nbuf; 674 igtk = wpa_ft_igtk_subelem(sm, &igtk_len); 675 if (igtk == NULL) { 676 os_free(subelem); 677 return pos; 678 } 679 nbuf = os_realloc(subelem, subelem_len + igtk_len); 680 if (nbuf == NULL) { 681 os_free(subelem); 682 os_free(igtk); 683 return pos; 684 } 685 subelem = nbuf; 686 os_memcpy(subelem + subelem_len, igtk, igtk_len); 687 subelem_len += igtk_len; 688 os_free(igtk); 689 } 690#endif /* CONFIG_IEEE80211W */ 691 } else { 692 r0kh_id = conf->r0_key_holder; 693 r0kh_id_len = conf->r0_key_holder_len; 694 anonce = NULL; 695 snonce = NULL; 696 } 697 res = wpa_write_ftie(conf, r0kh_id, r0kh_id_len, anonce, snonce, pos, 698 end - pos, subelem, subelem_len); 699 os_free(subelem); 700 if (res < 0) 701 return pos; 702 ftie = pos; 703 ftie_len = res; 704 pos += res; 705 706 os_free(sm->assoc_resp_ftie); 707 sm->assoc_resp_ftie = os_malloc(ftie_len); 708 if (sm->assoc_resp_ftie) 709 os_memcpy(sm->assoc_resp_ftie, ftie, ftie_len); 710 711 _ftie = (struct rsn_ftie *) (ftie + 2); 712 if (auth_alg == WLAN_AUTH_FT) 713 _ftie->mic_control[1] = 3; /* Information element count */ 714 715 ric_start = pos; 716 if (wpa_ft_parse_ies(req_ies, req_ies_len, &parse) == 0 && parse.ric) { 717 pos = wpa_ft_process_ric(sm, pos, end, parse.ric, 718 parse.ric_len); 719 if (auth_alg == WLAN_AUTH_FT) 720 _ftie->mic_control[1] += 721 ieee802_11_ie_count(ric_start, 722 pos - ric_start); 723 } 724 if (ric_start == pos) 725 ric_start = NULL; 726 727 if (auth_alg == WLAN_AUTH_FT && 728 wpa_ft_mic(sm->PTK.kck, sm->addr, sm->wpa_auth->addr, 6, 729 mdie, mdie_len, ftie, ftie_len, 730 rsnie, rsnie_len, 731 ric_start, ric_start ? pos - ric_start : 0, 732 _ftie->mic) < 0) 733 wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC"); 734 735 return pos; 736} 737 738 739static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth, 740 int vlan_id, 741 enum wpa_alg alg, const u8 *addr, int idx, 742 u8 *key, size_t key_len) 743{ 744 if (wpa_auth->cb.set_key == NULL) 745 return -1; 746 return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx, 747 key, key_len); 748} 749 750 751void wpa_ft_install_ptk(struct wpa_state_machine *sm) 752{ 753 enum wpa_alg alg; 754 int klen; 755 756 /* MLME-SETKEYS.request(PTK) */ 757 alg = wpa_cipher_to_alg(sm->pairwise); 758 klen = wpa_cipher_key_len(sm->pairwise); 759 if (!wpa_cipher_valid_pairwise(sm->pairwise)) { 760 wpa_printf(MSG_DEBUG, "FT: Unknown pairwise alg 0x%x - skip " 761 "PTK configuration", sm->pairwise); 762 return; 763 } 764 765 if (sm->tk_already_set) { 766 /* Must avoid TK reconfiguration to prevent clearing of TX/RX 767 * PN in the driver */ 768 wpa_printf(MSG_DEBUG, 769 "FT: Do not re-install same PTK to the driver"); 770 return; 771 } 772 773 /* FIX: add STA entry to kernel/driver here? The set_key will fail 774 * most likely without this.. At the moment, STA entry is added only 775 * after association has been completed. This function will be called 776 * again after association to get the PTK configured, but that could be 777 * optimized by adding the STA entry earlier. 778 */ 779 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0, 780 sm->PTK.tk1, klen)) 781 return; 782 783 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */ 784 sm->pairwise_set = TRUE; 785 sm->tk_already_set = TRUE; 786} 787 788 789static u16 wpa_ft_process_auth_req(struct wpa_state_machine *sm, 790 const u8 *ies, size_t ies_len, 791 u8 **resp_ies, size_t *resp_ies_len) 792{ 793 struct rsn_mdie *mdie; 794 struct rsn_ftie *ftie; 795 u8 pmk_r1[PMK_LEN], pmk_r1_name[WPA_PMK_NAME_LEN]; 796 u8 ptk_name[WPA_PMK_NAME_LEN]; 797 struct wpa_auth_config *conf; 798 struct wpa_ft_ies parse; 799 size_t buflen, ptk_len; 800 int ret; 801 u8 *pos, *end; 802 int pairwise; 803 804 *resp_ies = NULL; 805 *resp_ies_len = 0; 806 807 sm->pmk_r1_name_valid = 0; 808 conf = &sm->wpa_auth->conf; 809 810 wpa_hexdump(MSG_DEBUG, "FT: Received authentication frame IEs", 811 ies, ies_len); 812 813 if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) { 814 wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs"); 815 return WLAN_STATUS_UNSPECIFIED_FAILURE; 816 } 817 818 mdie = (struct rsn_mdie *) parse.mdie; 819 if (mdie == NULL || parse.mdie_len < sizeof(*mdie) || 820 os_memcmp(mdie->mobility_domain, 821 sm->wpa_auth->conf.mobility_domain, 822 MOBILITY_DOMAIN_ID_LEN) != 0) { 823 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE"); 824 return WLAN_STATUS_INVALID_MDIE; 825 } 826 827 ftie = (struct rsn_ftie *) parse.ftie; 828 if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) { 829 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE"); 830 return WLAN_STATUS_INVALID_FTIE; 831 } 832 833 os_memcpy(sm->SNonce, ftie->snonce, WPA_NONCE_LEN); 834 835 if (parse.r0kh_id == NULL) { 836 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE - no R0KH-ID"); 837 return WLAN_STATUS_INVALID_FTIE; 838 } 839 840 wpa_hexdump(MSG_DEBUG, "FT: STA R0KH-ID", 841 parse.r0kh_id, parse.r0kh_id_len); 842 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len); 843 sm->r0kh_id_len = parse.r0kh_id_len; 844 845 if (parse.rsn_pmkid == NULL) { 846 wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE"); 847 return WLAN_STATUS_INVALID_PMKID; 848 } 849 850 wpa_hexdump(MSG_DEBUG, "FT: Requested PMKR0Name", 851 parse.rsn_pmkid, WPA_PMK_NAME_LEN); 852 wpa_derive_pmk_r1_name(parse.rsn_pmkid, 853 sm->wpa_auth->conf.r1_key_holder, sm->addr, 854 pmk_r1_name); 855 wpa_hexdump(MSG_DEBUG, "FT: Derived requested PMKR1Name", 856 pmk_r1_name, WPA_PMK_NAME_LEN); 857 858 if (wpa_ft_fetch_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1_name, pmk_r1, 859 &pairwise) < 0) { 860 if (wpa_ft_pull_pmk_r1(sm->wpa_auth, sm->addr, sm->r0kh_id, 861 sm->r0kh_id_len, parse.rsn_pmkid) < 0) { 862 wpa_printf(MSG_DEBUG, "FT: Did not have matching " 863 "PMK-R1 and unknown R0KH-ID"); 864 return WLAN_STATUS_INVALID_PMKID; 865 } 866 867 /* 868 * TODO: Should return "status pending" (and the caller should 869 * not send out response now). The real response will be sent 870 * once the response from R0KH is received. 871 */ 872 return WLAN_STATUS_INVALID_PMKID; 873 } 874 875 wpa_hexdump_key(MSG_DEBUG, "FT: Selected PMK-R1", pmk_r1, PMK_LEN); 876 sm->pmk_r1_name_valid = 1; 877 os_memcpy(sm->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN); 878 879 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) { 880 wpa_printf(MSG_DEBUG, "FT: Failed to get random data for " 881 "ANonce"); 882 return WLAN_STATUS_UNSPECIFIED_FAILURE; 883 } 884 885 wpa_hexdump(MSG_DEBUG, "FT: Received SNonce", 886 sm->SNonce, WPA_NONCE_LEN); 887 wpa_hexdump(MSG_DEBUG, "FT: Generated ANonce", 888 sm->ANonce, WPA_NONCE_LEN); 889 890 ptk_len = pairwise == WPA_CIPHER_TKIP ? 64 : 48; 891 wpa_pmk_r1_to_ptk(pmk_r1, sm->SNonce, sm->ANonce, sm->addr, 892 sm->wpa_auth->addr, pmk_r1_name, 893 (u8 *) &sm->PTK, ptk_len, ptk_name); 894 wpa_hexdump_key(MSG_DEBUG, "FT: PTK", 895 (u8 *) &sm->PTK, ptk_len); 896 wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN); 897 898 sm->pairwise = pairwise; 899 sm->tk_already_set = FALSE; 900 wpa_ft_install_ptk(sm); 901 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; 907 } 908 909 pos = *resp_ies; 910 end = *resp_ies + buflen; 911 912 ret = wpa_write_rsn_ie(conf, pos, end - pos, parse.rsn_pmkid); 913 if (ret < 0) { 914 os_free(*resp_ies); 915 *resp_ies = NULL; 916 return WLAN_STATUS_UNSPECIFIED_FAILURE; 917 } 918 pos += ret; 919 920 ret = wpa_write_mdie(conf, pos, end - pos); 921 if (ret < 0) { 922 os_free(*resp_ies); 923 *resp_ies = NULL; 924 return WLAN_STATUS_UNSPECIFIED_FAILURE; 925 } 926 pos += ret; 927 928 ret = wpa_write_ftie(conf, parse.r0kh_id, parse.r0kh_id_len, 929 sm->ANonce, sm->SNonce, pos, end - pos, NULL, 0); 930 if (ret < 0) { 931 os_free(*resp_ies); 932 *resp_ies = NULL; 933 return WLAN_STATUS_UNSPECIFIED_FAILURE; 934 } 935 pos += ret; 936 937 *resp_ies_len = pos - *resp_ies; 938 939 return WLAN_STATUS_SUCCESS; 940} 941 942 943void 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), 948 void *ctx) 949{ 950 u16 status; 951 u8 *resp_ies; 952 size_t resp_ies_len; 953 954 if (sm == NULL) { 955 wpa_printf(MSG_DEBUG, "FT: Received authentication frame, but " 956 "WPA SM not available"); 957 return; 958 } 959 960 wpa_printf(MSG_DEBUG, "FT: Received authentication frame: STA=" MACSTR 961 " BSSID=" MACSTR " transaction=%d", 962 MAC2STR(sm->addr), MAC2STR(bssid), auth_transaction); 963 status = wpa_ft_process_auth_req(sm, ies, ies_len, &resp_ies, 964 &resp_ies_len); 965 966 wpa_printf(MSG_DEBUG, "FT: FT authentication response: dst=" MACSTR 967 " auth_transaction=%d status=%d", 968 MAC2STR(sm->addr), auth_transaction + 1, status); 969 wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len); 970 cb(ctx, sm->addr, bssid, auth_transaction + 1, status, 971 resp_ies, resp_ies_len); 972 os_free(resp_ies); 973} 974 975 976u16 wpa_ft_validate_reassoc(struct wpa_state_machine *sm, const u8 *ies, 977 size_t ies_len) 978{ 979 struct wpa_ft_ies parse; 980 struct rsn_mdie *mdie; 981 struct rsn_ftie *ftie; 982 u8 mic[16]; 983 unsigned int count; 984 985 if (sm == NULL) 986 return WLAN_STATUS_UNSPECIFIED_FAILURE; 987 988 wpa_hexdump(MSG_DEBUG, "FT: Reassoc Req IEs", ies, ies_len); 989 990 if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) { 991 wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs"); 992 return WLAN_STATUS_UNSPECIFIED_FAILURE; 993 } 994 995 if (parse.rsn == NULL) { 996 wpa_printf(MSG_DEBUG, "FT: No RSNIE in Reassoc Req"); 997 return WLAN_STATUS_UNSPECIFIED_FAILURE; 998 } 999 1000 if (parse.rsn_pmkid == NULL) { 1001 wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE"); 1002 return WLAN_STATUS_INVALID_PMKID; 1003 } 1004 1005 if (os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0) 1006 { 1007 wpa_printf(MSG_DEBUG, "FT: PMKID in Reassoc Req did not match " 1008 "with the PMKR1Name derived from auth request"); 1009 return WLAN_STATUS_INVALID_PMKID; 1010 } 1011 1012 mdie = (struct rsn_mdie *) parse.mdie; 1013 if (mdie == NULL || parse.mdie_len < sizeof(*mdie) || 1014 os_memcmp(mdie->mobility_domain, 1015 sm->wpa_auth->conf.mobility_domain, 1016 MOBILITY_DOMAIN_ID_LEN) != 0) { 1017 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE"); 1018 return WLAN_STATUS_INVALID_MDIE; 1019 } 1020 1021 ftie = (struct rsn_ftie *) parse.ftie; 1022 if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) { 1023 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE"); 1024 return WLAN_STATUS_INVALID_FTIE; 1025 } 1026 1027 if (os_memcmp(ftie->snonce, sm->SNonce, WPA_NONCE_LEN) != 0) { 1028 wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE"); 1029 wpa_hexdump(MSG_DEBUG, "FT: Received SNonce", 1030 ftie->snonce, WPA_NONCE_LEN); 1031 wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce", 1032 sm->SNonce, WPA_NONCE_LEN); 1033 return -1; 1034 } 1035 1036 if (os_memcmp(ftie->anonce, sm->ANonce, WPA_NONCE_LEN) != 0) { 1037 wpa_printf(MSG_DEBUG, "FT: ANonce mismatch in FTIE"); 1038 wpa_hexdump(MSG_DEBUG, "FT: Received ANonce", 1039 ftie->anonce, WPA_NONCE_LEN); 1040 wpa_hexdump(MSG_DEBUG, "FT: Expected ANonce", 1041 sm->ANonce, WPA_NONCE_LEN); 1042 return -1; 1043 } 1044 1045 1046 if (parse.r0kh_id == NULL) { 1047 wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE"); 1048 return -1; 1049 } 1050 1051 if (parse.r0kh_id_len != sm->r0kh_id_len || 1052 os_memcmp(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0) { 1053 wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with " 1054 "the current R0KH-ID"); 1055 wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE", 1056 parse.r0kh_id, parse.r0kh_id_len); 1057 wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID", 1058 sm->r0kh_id, sm->r0kh_id_len); 1059 return -1; 1060 } 1061 1062 if (parse.r1kh_id == NULL) { 1063 wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE"); 1064 return -1; 1065 } 1066 1067 if (os_memcmp(parse.r1kh_id, sm->wpa_auth->conf.r1_key_holder, 1068 FT_R1KH_ID_LEN) != 0) { 1069 wpa_printf(MSG_DEBUG, "FT: Unknown R1KH-ID used in " 1070 "ReassocReq"); 1071 wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID in FTIE", 1072 parse.r1kh_id, FT_R1KH_ID_LEN); 1073 wpa_hexdump(MSG_DEBUG, "FT: Expected R1KH-ID", 1074 sm->wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN); 1075 return -1; 1076 } 1077 1078 if (parse.rsn_pmkid == NULL || 1079 os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN)) { 1080 wpa_printf(MSG_DEBUG, "FT: No matching PMKR1Name (PMKID) in " 1081 "RSNIE (pmkid=%d)", !!parse.rsn_pmkid); 1082 return -1; 1083 } 1084 1085 count = 3; 1086 if (parse.ric) 1087 count += ieee802_11_ie_count(parse.ric, parse.ric_len); 1088 if (ftie->mic_control[1] != count) { 1089 wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in MIC " 1090 "Control: received %u expected %u", 1091 ftie->mic_control[1], count); 1092 return -1; 1093 } 1094 1095 if (wpa_ft_mic(sm->PTK.kck, sm->addr, sm->wpa_auth->addr, 5, 1096 parse.mdie - 2, parse.mdie_len + 2, 1097 parse.ftie - 2, parse.ftie_len + 2, 1098 parse.rsn - 2, parse.rsn_len + 2, 1099 parse.ric, parse.ric_len, 1100 mic) < 0) { 1101 wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC"); 1102 return WLAN_STATUS_UNSPECIFIED_FAILURE; 1103 } 1104 1105 if (os_memcmp(mic, ftie->mic, 16) != 0) { 1106 wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE"); 1107 wpa_printf(MSG_DEBUG, "FT: addr=" MACSTR " auth_addr=" MACSTR, 1108 MAC2STR(sm->addr), MAC2STR(sm->wpa_auth->addr)); 1109 wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", ftie->mic, 16); 1110 wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16); 1111 wpa_hexdump(MSG_MSGDUMP, "FT: MDIE", 1112 parse.mdie - 2, parse.mdie_len + 2); 1113 wpa_hexdump(MSG_MSGDUMP, "FT: FTIE", 1114 parse.ftie - 2, parse.ftie_len + 2); 1115 wpa_hexdump(MSG_MSGDUMP, "FT: RSN", 1116 parse.rsn - 2, parse.rsn_len + 2); 1117 return WLAN_STATUS_INVALID_FTIE; 1118 } 1119 1120 return WLAN_STATUS_SUCCESS; 1121} 1122 1123 1124int wpa_ft_action_rx(struct wpa_state_machine *sm, const u8 *data, size_t len) 1125{ 1126 const u8 *sta_addr, *target_ap; 1127 const u8 *ies; 1128 size_t ies_len; 1129 u8 action; 1130 struct ft_rrb_frame *frame; 1131 1132 if (sm == NULL) 1133 return -1; 1134 1135 /* 1136 * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6] 1137 * FT Request action frame body[variable] 1138 */ 1139 1140 if (len < 14) { 1141 wpa_printf(MSG_DEBUG, "FT: Too short FT Action frame " 1142 "(len=%lu)", (unsigned long) len); 1143 return -1; 1144 } 1145 1146 action = data[1]; 1147 sta_addr = data + 2; 1148 target_ap = data + 8; 1149 ies = data + 14; 1150 ies_len = len - 14; 1151 1152 wpa_printf(MSG_DEBUG, "FT: Received FT Action frame (STA=" MACSTR 1153 " Target AP=" MACSTR " Action=%d)", 1154 MAC2STR(sta_addr), MAC2STR(target_ap), action); 1155 1156 if (os_memcmp(sta_addr, sm->addr, ETH_ALEN) != 0) { 1157 wpa_printf(MSG_DEBUG, "FT: Mismatch in FT Action STA address: " 1158 "STA=" MACSTR " STA-Address=" MACSTR, 1159 MAC2STR(sm->addr), MAC2STR(sta_addr)); 1160 return -1; 1161 } 1162 1163 /* 1164 * Do some sanity checking on the target AP address (not own and not 1165 * broadcast. This could be extended to filter based on a list of known 1166 * APs in the MD (if such a list were configured). 1167 */ 1168 if ((target_ap[0] & 0x01) || 1169 os_memcmp(target_ap, sm->wpa_auth->addr, ETH_ALEN) == 0) { 1170 wpa_printf(MSG_DEBUG, "FT: Invalid Target AP in FT Action " 1171 "frame"); 1172 return -1; 1173 } 1174 1175 wpa_hexdump(MSG_MSGDUMP, "FT: Action frame body", ies, ies_len); 1176 1177 /* RRB - Forward action frame to the target AP */ 1178 frame = os_malloc(sizeof(*frame) + len); 1179 frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB; 1180 frame->packet_type = FT_PACKET_REQUEST; 1181 frame->action_length = host_to_le16(len); 1182 os_memcpy(frame->ap_address, sm->wpa_auth->addr, ETH_ALEN); 1183 os_memcpy(frame + 1, data, len); 1184 1185 wpa_ft_rrb_send(sm->wpa_auth, target_ap, (u8 *) frame, 1186 sizeof(*frame) + len); 1187 os_free(frame); 1188 1189 return 0; 1190} 1191 1192 1193static int wpa_ft_rrb_rx_request(struct wpa_authenticator *wpa_auth, 1194 const u8 *current_ap, const u8 *sta_addr, 1195 const u8 *body, size_t len) 1196{ 1197 struct wpa_state_machine *sm; 1198 u16 status; 1199 u8 *resp_ies, *pos; 1200 size_t resp_ies_len, rlen; 1201 struct ft_rrb_frame *frame; 1202 1203 sm = wpa_ft_add_sta(wpa_auth, sta_addr); 1204 if (sm == NULL) { 1205 wpa_printf(MSG_DEBUG, "FT: Failed to add new STA based on " 1206 "RRB Request"); 1207 return -1; 1208 } 1209 1210 wpa_hexdump(MSG_MSGDUMP, "FT: RRB Request Frame body", body, len); 1211 1212 status = wpa_ft_process_auth_req(sm, body, len, &resp_ies, 1213 &resp_ies_len); 1214 1215 wpa_printf(MSG_DEBUG, "FT: RRB authentication response: STA=" MACSTR 1216 " CurrentAP=" MACSTR " status=%d", 1217 MAC2STR(sm->addr), MAC2STR(current_ap), status); 1218 wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len); 1219 1220 /* RRB - Forward action frame response to the Current AP */ 1221 1222 /* 1223 * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6] 1224 * Status_Code[2] FT Request action frame body[variable] 1225 */ 1226 rlen = 2 + 2 * ETH_ALEN + 2 + resp_ies_len; 1227 1228 frame = os_malloc(sizeof(*frame) + rlen); 1229 frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB; 1230 frame->packet_type = FT_PACKET_RESPONSE; 1231 frame->action_length = host_to_le16(rlen); 1232 os_memcpy(frame->ap_address, wpa_auth->addr, ETH_ALEN); 1233 pos = (u8 *) (frame + 1); 1234 *pos++ = WLAN_ACTION_FT; 1235 *pos++ = 2; /* Action: Response */ 1236 os_memcpy(pos, sta_addr, ETH_ALEN); 1237 pos += ETH_ALEN; 1238 os_memcpy(pos, wpa_auth->addr, ETH_ALEN); 1239 pos += ETH_ALEN; 1240 WPA_PUT_LE16(pos, status); 1241 pos += 2; 1242 if (resp_ies) { 1243 os_memcpy(pos, resp_ies, resp_ies_len); 1244 os_free(resp_ies); 1245 } 1246 1247 wpa_ft_rrb_send(wpa_auth, current_ap, (u8 *) frame, 1248 sizeof(*frame) + rlen); 1249 os_free(frame); 1250 1251 return 0; 1252} 1253 1254 1255static int wpa_ft_rrb_rx_pull(struct wpa_authenticator *wpa_auth, 1256 const u8 *src_addr, 1257 const u8 *data, size_t data_len) 1258{ 1259 struct ft_r0kh_r1kh_pull_frame *frame, f; 1260 struct ft_remote_r1kh *r1kh; 1261 struct ft_r0kh_r1kh_resp_frame resp, r; 1262 u8 pmk_r0[PMK_LEN]; 1263 int pairwise; 1264 1265 wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull"); 1266 1267 if (data_len < sizeof(*frame)) 1268 return -1; 1269 1270 r1kh = wpa_auth->conf.r1kh_list; 1271 while (r1kh) { 1272 if (os_memcmp(r1kh->addr, src_addr, ETH_ALEN) == 0) 1273 break; 1274 r1kh = r1kh->next; 1275 } 1276 if (r1kh == NULL) { 1277 wpa_printf(MSG_DEBUG, "FT: No matching R1KH address found for " 1278 "PMK-R1 pull source address " MACSTR, 1279 MAC2STR(src_addr)); 1280 return -1; 1281 } 1282 1283 frame = (struct ft_r0kh_r1kh_pull_frame *) data; 1284 /* aes_unwrap() does not support inplace decryption, so use a temporary 1285 * buffer for the data. */ 1286 if (aes_unwrap(r1kh->key, (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8, 1287 frame->nonce, f.nonce) < 0) { 1288 wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull " 1289 "request from " MACSTR, MAC2STR(src_addr)); 1290 return -1; 1291 } 1292 1293 wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce", 1294 f.nonce, sizeof(f.nonce)); 1295 wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR0Name", 1296 f.pmk_r0_name, WPA_PMK_NAME_LEN); 1297 wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID=" 1298 MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id)); 1299 1300 os_memset(&resp, 0, sizeof(resp)); 1301 resp.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB; 1302 resp.packet_type = FT_PACKET_R0KH_R1KH_RESP; 1303 resp.data_length = host_to_le16(FT_R0KH_R1KH_RESP_DATA_LEN); 1304 os_memcpy(resp.ap_address, wpa_auth->addr, ETH_ALEN); 1305 1306 /* aes_wrap() does not support inplace encryption, so use a temporary 1307 * buffer for the data. */ 1308 os_memcpy(r.nonce, f.nonce, sizeof(f.nonce)); 1309 os_memcpy(r.r1kh_id, f.r1kh_id, FT_R1KH_ID_LEN); 1310 os_memcpy(r.s1kh_id, f.s1kh_id, ETH_ALEN); 1311 if (wpa_ft_fetch_pmk_r0(wpa_auth, f.s1kh_id, f.pmk_r0_name, pmk_r0, 1312 &pairwise) < 0) { 1313 wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name found for " 1314 "PMK-R1 pull"); 1315 return -1; 1316 } 1317 1318 wpa_derive_pmk_r1(pmk_r0, f.pmk_r0_name, f.r1kh_id, f.s1kh_id, 1319 r.pmk_r1, r.pmk_r1_name); 1320 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", r.pmk_r1, PMK_LEN); 1321 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", r.pmk_r1_name, 1322 WPA_PMK_NAME_LEN); 1323 r.pairwise = host_to_le16(pairwise); 1324 1325 if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8, 1326 r.nonce, resp.nonce) < 0) { 1327 os_memset(pmk_r0, 0, PMK_LEN); 1328 return -1; 1329 } 1330 1331 os_memset(pmk_r0, 0, PMK_LEN); 1332 1333 wpa_ft_rrb_send(wpa_auth, src_addr, (u8 *) &resp, sizeof(resp)); 1334 1335 return 0; 1336} 1337 1338 1339static int wpa_ft_rrb_rx_resp(struct wpa_authenticator *wpa_auth, 1340 const u8 *src_addr, 1341 const u8 *data, size_t data_len) 1342{ 1343 struct ft_r0kh_r1kh_resp_frame *frame, f; 1344 struct ft_remote_r0kh *r0kh; 1345 int pairwise; 1346 1347 wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull response"); 1348 1349 if (data_len < sizeof(*frame)) 1350 return -1; 1351 1352 r0kh = wpa_auth->conf.r0kh_list; 1353 while (r0kh) { 1354 if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0) 1355 break; 1356 r0kh = r0kh->next; 1357 } 1358 if (r0kh == NULL) { 1359 wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for " 1360 "PMK-R0 pull response source address " MACSTR, 1361 MAC2STR(src_addr)); 1362 return -1; 1363 } 1364 1365 frame = (struct ft_r0kh_r1kh_resp_frame *) data; 1366 /* aes_unwrap() does not support inplace decryption, so use a temporary 1367 * buffer for the data. */ 1368 if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8, 1369 frame->nonce, f.nonce) < 0) { 1370 wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull " 1371 "response from " MACSTR, MAC2STR(src_addr)); 1372 return -1; 1373 } 1374 1375 if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN) 1376 != 0) { 1377 wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull response did not use a " 1378 "matching R1KH-ID"); 1379 return -1; 1380 } 1381 1382 /* TODO: verify that <nonce,s1kh_id> matches with a pending request 1383 * and call this requests callback function to finish request 1384 * processing */ 1385 1386 pairwise = le_to_host16(f.pairwise); 1387 wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce", 1388 f.nonce, sizeof(f.nonce)); 1389 wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID=" 1390 MACSTR " pairwise=0x%x", 1391 MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id), pairwise); 1392 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 pull - PMK-R1", 1393 f.pmk_r1, PMK_LEN); 1394 wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR1Name", 1395 f.pmk_r1_name, WPA_PMK_NAME_LEN); 1396 1397 wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name, 1398 pairwise); 1399 os_memset(f.pmk_r1, 0, PMK_LEN); 1400 1401 return 0; 1402} 1403 1404 1405static int wpa_ft_rrb_rx_push(struct wpa_authenticator *wpa_auth, 1406 const u8 *src_addr, 1407 const u8 *data, size_t data_len) 1408{ 1409 struct ft_r0kh_r1kh_push_frame *frame, f; 1410 struct ft_remote_r0kh *r0kh; 1411 struct os_time now; 1412 os_time_t tsend; 1413 int pairwise; 1414 1415 wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 push"); 1416 1417 if (data_len < sizeof(*frame)) 1418 return -1; 1419 1420 r0kh = wpa_auth->conf.r0kh_list; 1421 while (r0kh) { 1422 if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0) 1423 break; 1424 r0kh = r0kh->next; 1425 } 1426 if (r0kh == NULL) { 1427 wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for " 1428 "PMK-R0 push source address " MACSTR, 1429 MAC2STR(src_addr)); 1430 return -1; 1431 } 1432 1433 frame = (struct ft_r0kh_r1kh_push_frame *) data; 1434 /* aes_unwrap() does not support inplace decryption, so use a temporary 1435 * buffer for the data. */ 1436 if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8, 1437 frame->timestamp, f.timestamp) < 0) { 1438 wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 push from " 1439 MACSTR, MAC2STR(src_addr)); 1440 return -1; 1441 } 1442 1443 os_get_time(&now); 1444 tsend = WPA_GET_LE32(f.timestamp); 1445 if ((now.sec > tsend && now.sec - tsend > 60) || 1446 (now.sec < tsend && tsend - now.sec > 60)) { 1447 wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not have a valid " 1448 "timestamp: sender time %d own time %d\n", 1449 (int) tsend, (int) now.sec); 1450 return -1; 1451 } 1452 1453 if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN) 1454 != 0) { 1455 wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not use a matching " 1456 "R1KH-ID (received " MACSTR " own " MACSTR ")", 1457 MAC2STR(f.r1kh_id), 1458 MAC2STR(wpa_auth->conf.r1_key_holder)); 1459 return -1; 1460 } 1461 1462 pairwise = le_to_host16(f.pairwise); 1463 wpa_printf(MSG_DEBUG, "FT: PMK-R1 push - R1KH-ID=" MACSTR " S1KH-ID=" 1464 MACSTR " pairwise=0x%x", 1465 MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id), pairwise); 1466 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 push - PMK-R1", 1467 f.pmk_r1, PMK_LEN); 1468 wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 push - PMKR1Name", 1469 f.pmk_r1_name, WPA_PMK_NAME_LEN); 1470 1471 wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name, 1472 pairwise); 1473 os_memset(f.pmk_r1, 0, PMK_LEN); 1474 1475 return 0; 1476} 1477 1478 1479int wpa_ft_rrb_rx(struct wpa_authenticator *wpa_auth, const u8 *src_addr, 1480 const u8 *data, size_t data_len) 1481{ 1482 struct ft_rrb_frame *frame; 1483 u16 alen; 1484 const u8 *pos, *end, *start; 1485 u8 action; 1486 const u8 *sta_addr, *target_ap_addr; 1487 1488 wpa_printf(MSG_DEBUG, "FT: RRB received frame from remote AP " MACSTR, 1489 MAC2STR(src_addr)); 1490 1491 if (data_len < sizeof(*frame)) { 1492 wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (data_len=%lu)", 1493 (unsigned long) data_len); 1494 return -1; 1495 } 1496 1497 pos = data; 1498 frame = (struct ft_rrb_frame *) pos; 1499 pos += sizeof(*frame); 1500 1501 alen = le_to_host16(frame->action_length); 1502 wpa_printf(MSG_DEBUG, "FT: RRB frame - frame_type=%d packet_type=%d " 1503 "action_length=%d ap_address=" MACSTR, 1504 frame->frame_type, frame->packet_type, alen, 1505 MAC2STR(frame->ap_address)); 1506 1507 if (frame->frame_type != RSN_REMOTE_FRAME_TYPE_FT_RRB) { 1508 /* Discard frame per IEEE Std 802.11r-2008, 11A.10.3 */ 1509 wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with " 1510 "unrecognized type %d", frame->frame_type); 1511 return -1; 1512 } 1513 1514 if (alen > data_len - sizeof(*frame)) { 1515 wpa_printf(MSG_DEBUG, "FT: RRB frame too short for action " 1516 "frame"); 1517 return -1; 1518 } 1519 1520 if (frame->packet_type == FT_PACKET_R0KH_R1KH_PULL) 1521 return wpa_ft_rrb_rx_pull(wpa_auth, src_addr, data, data_len); 1522 if (frame->packet_type == FT_PACKET_R0KH_R1KH_RESP) 1523 return wpa_ft_rrb_rx_resp(wpa_auth, src_addr, data, data_len); 1524 if (frame->packet_type == FT_PACKET_R0KH_R1KH_PUSH) 1525 return wpa_ft_rrb_rx_push(wpa_auth, src_addr, data, data_len); 1526 1527 wpa_hexdump(MSG_MSGDUMP, "FT: RRB - FT Action frame", pos, alen); 1528 1529 if (alen < 1 + 1 + 2 * ETH_ALEN) { 1530 wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (not enough " 1531 "room for Action Frame body); alen=%lu", 1532 (unsigned long) alen); 1533 return -1; 1534 } 1535 start = pos; 1536 end = pos + alen; 1537 1538 if (*pos != WLAN_ACTION_FT) { 1539 wpa_printf(MSG_DEBUG, "FT: Unexpected Action frame category " 1540 "%d", *pos); 1541 return -1; 1542 } 1543 1544 pos++; 1545 action = *pos++; 1546 sta_addr = pos; 1547 pos += ETH_ALEN; 1548 target_ap_addr = pos; 1549 pos += ETH_ALEN; 1550 wpa_printf(MSG_DEBUG, "FT: RRB Action Frame: action=%d sta_addr=" 1551 MACSTR " target_ap_addr=" MACSTR, 1552 action, MAC2STR(sta_addr), MAC2STR(target_ap_addr)); 1553 1554 if (frame->packet_type == FT_PACKET_REQUEST) { 1555 wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Request"); 1556 1557 if (action != 1) { 1558 wpa_printf(MSG_DEBUG, "FT: Unexpected Action %d in " 1559 "RRB Request", action); 1560 return -1; 1561 } 1562 1563 if (os_memcmp(target_ap_addr, wpa_auth->addr, ETH_ALEN) != 0) { 1564 wpa_printf(MSG_DEBUG, "FT: Target AP address in the " 1565 "RRB Request does not match with own " 1566 "address"); 1567 return -1; 1568 } 1569 1570 if (wpa_ft_rrb_rx_request(wpa_auth, frame->ap_address, 1571 sta_addr, pos, end - pos) < 0) 1572 return -1; 1573 } else if (frame->packet_type == FT_PACKET_RESPONSE) { 1574 u16 status_code; 1575 1576 if (end - pos < 2) { 1577 wpa_printf(MSG_DEBUG, "FT: Not enough room for status " 1578 "code in RRB Response"); 1579 return -1; 1580 } 1581 status_code = WPA_GET_LE16(pos); 1582 pos += 2; 1583 1584 wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Response " 1585 "(status_code=%d)", status_code); 1586 1587 if (wpa_ft_action_send(wpa_auth, sta_addr, start, alen) < 0) 1588 return -1; 1589 } else { 1590 wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with unknown " 1591 "packet_type %d", frame->packet_type); 1592 return -1; 1593 } 1594 1595 return 0; 1596} 1597 1598 1599static void wpa_ft_generate_pmk_r1(struct wpa_authenticator *wpa_auth, 1600 struct wpa_ft_pmk_r0_sa *pmk_r0, 1601 struct ft_remote_r1kh *r1kh, 1602 const u8 *s1kh_id, int pairwise) 1603{ 1604 struct ft_r0kh_r1kh_push_frame frame, f; 1605 struct os_time now; 1606 1607 os_memset(&frame, 0, sizeof(frame)); 1608 frame.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB; 1609 frame.packet_type = FT_PACKET_R0KH_R1KH_PUSH; 1610 frame.data_length = host_to_le16(FT_R0KH_R1KH_PUSH_DATA_LEN); 1611 os_memcpy(frame.ap_address, wpa_auth->addr, ETH_ALEN); 1612 1613 /* aes_wrap() does not support inplace encryption, so use a temporary 1614 * buffer for the data. */ 1615 os_memcpy(f.r1kh_id, r1kh->id, FT_R1KH_ID_LEN); 1616 os_memcpy(f.s1kh_id, s1kh_id, ETH_ALEN); 1617 os_memcpy(f.pmk_r0_name, pmk_r0->pmk_r0_name, WPA_PMK_NAME_LEN); 1618 wpa_derive_pmk_r1(pmk_r0->pmk_r0, pmk_r0->pmk_r0_name, r1kh->id, 1619 s1kh_id, f.pmk_r1, f.pmk_r1_name); 1620 wpa_printf(MSG_DEBUG, "FT: R1KH-ID " MACSTR, MAC2STR(r1kh->id)); 1621 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", f.pmk_r1, PMK_LEN); 1622 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", f.pmk_r1_name, 1623 WPA_PMK_NAME_LEN); 1624 os_get_time(&now); 1625 WPA_PUT_LE32(f.timestamp, now.sec); 1626 f.pairwise = host_to_le16(pairwise); 1627 if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8, 1628 f.timestamp, frame.timestamp) < 0) 1629 return; 1630 1631 wpa_ft_rrb_send(wpa_auth, r1kh->addr, (u8 *) &frame, sizeof(frame)); 1632} 1633 1634 1635void wpa_ft_push_pmk_r1(struct wpa_authenticator *wpa_auth, const u8 *addr) 1636{ 1637 struct wpa_ft_pmk_r0_sa *r0; 1638 struct ft_remote_r1kh *r1kh; 1639 1640 if (!wpa_auth->conf.pmk_r1_push) 1641 return; 1642 1643 r0 = wpa_auth->ft_pmk_cache->pmk_r0; 1644 while (r0) { 1645 if (os_memcmp(r0->spa, addr, ETH_ALEN) == 0) 1646 break; 1647 r0 = r0->next; 1648 } 1649 1650 if (r0 == NULL || r0->pmk_r1_pushed) 1651 return; 1652 r0->pmk_r1_pushed = 1; 1653 1654 wpa_printf(MSG_DEBUG, "FT: Deriving and pushing PMK-R1 keys to R1KHs " 1655 "for STA " MACSTR, MAC2STR(addr)); 1656 1657 r1kh = wpa_auth->conf.r1kh_list; 1658 while (r1kh) { 1659 wpa_ft_generate_pmk_r1(wpa_auth, r0, r1kh, addr, r0->pairwise); 1660 r1kh = r1kh->next; 1661 } 1662} 1663 1664#endif /* CONFIG_IEEE80211R */ 1665