wpa_auth_ft.c
Go to the documentation of this file.
00001 /*
00002  * hostapd - IEEE 802.11r - Fast BSS Transition
00003  * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 2 as
00007  * published by the Free Software Foundation.
00008  *
00009  * Alternatively, this software may be distributed under the terms of BSD
00010  * license.
00011  *
00012  * See README and COPYING for more details.
00013  */
00014 
00015 #include "utils/includes.h"
00016 
00017 #include "utils/common.h"
00018 #include "common/ieee802_11_defs.h"
00019 #include "common/ieee802_11_common.h"
00020 #include "crypto/aes_wrap.h"
00021 #include "ap_config.h"
00022 #include "ieee802_11.h"
00023 #include "wmm.h"
00024 #include "wpa_auth.h"
00025 #include "wpa_auth_i.h"
00026 #include "wpa_auth_ie.h"
00027 
00028 
00029 #ifdef CONFIG_IEEE80211R
00030 
00031 struct wpa_ft_ies {
00032         const u8 *mdie;
00033         size_t mdie_len;
00034         const u8 *ftie;
00035         size_t ftie_len;
00036         const u8 *r1kh_id;
00037         const u8 *gtk;
00038         size_t gtk_len;
00039         const u8 *r0kh_id;
00040         size_t r0kh_id_len;
00041         const u8 *rsn;
00042         size_t rsn_len;
00043         const u8 *rsn_pmkid;
00044         const u8 *ric;
00045         size_t ric_len;
00046 };
00047 
00048 
00049 static int wpa_ft_parse_ies(const u8 *ies, size_t ies_len,
00050                             struct wpa_ft_ies *parse);
00051 
00052 
00053 static int wpa_ft_rrb_send(struct wpa_authenticator *wpa_auth, const u8 *dst,
00054                            const u8 *data, size_t data_len)
00055 {
00056         if (wpa_auth->cb.send_ether == NULL)
00057                 return -1;
00058         return wpa_auth->cb.send_ether(wpa_auth->cb.ctx, dst, ETH_P_RRB,
00059                                        data, data_len);
00060 }
00061 
00062 
00063 static int wpa_ft_action_send(struct wpa_authenticator *wpa_auth,
00064                               const u8 *dst, const u8 *data, size_t data_len)
00065 {
00066         if (wpa_auth->cb.send_ft_action == NULL)
00067                 return -1;
00068         return wpa_auth->cb.send_ft_action(wpa_auth->cb.ctx, dst,
00069                                            data, data_len);
00070 }
00071 
00072 
00073 static struct wpa_state_machine *
00074 wpa_ft_add_sta(struct wpa_authenticator *wpa_auth, const u8 *sta_addr)
00075 {
00076         if (wpa_auth->cb.add_sta == NULL)
00077                 return NULL;
00078         return wpa_auth->cb.add_sta(wpa_auth->cb.ctx, sta_addr);
00079 }
00080 
00081 
00082 int wpa_write_mdie(struct wpa_auth_config *conf, u8 *buf, size_t len)
00083 {
00084         u8 *pos = buf;
00085         u8 capab;
00086         if (len < 2 + sizeof(struct rsn_mdie))
00087                 return -1;
00088 
00089         *pos++ = WLAN_EID_MOBILITY_DOMAIN;
00090         *pos++ = MOBILITY_DOMAIN_ID_LEN + 1;
00091         os_memcpy(pos, conf->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
00092         pos += MOBILITY_DOMAIN_ID_LEN;
00093         capab = RSN_FT_CAPAB_FT_OVER_DS;
00094         *pos++ = capab;
00095 
00096         return pos - buf;
00097 }
00098 
00099 
00100 int wpa_write_ftie(struct wpa_auth_config *conf, const u8 *r0kh_id,
00101                    size_t r0kh_id_len,
00102                    const u8 *anonce, const u8 *snonce,
00103                    u8 *buf, size_t len, const u8 *subelem,
00104                    size_t subelem_len)
00105 {
00106         u8 *pos = buf, *ielen;
00107         struct rsn_ftie *hdr;
00108 
00109         if (len < 2 + sizeof(*hdr) + 2 + FT_R1KH_ID_LEN + 2 + r0kh_id_len +
00110             subelem_len)
00111                 return -1;
00112 
00113         *pos++ = WLAN_EID_FAST_BSS_TRANSITION;
00114         ielen = pos++;
00115 
00116         hdr = (struct rsn_ftie *) pos;
00117         os_memset(hdr, 0, sizeof(*hdr));
00118         pos += sizeof(*hdr);
00119         WPA_PUT_LE16(hdr->mic_control, 0);
00120         if (anonce)
00121                 os_memcpy(hdr->anonce, anonce, WPA_NONCE_LEN);
00122         if (snonce)
00123                 os_memcpy(hdr->snonce, snonce, WPA_NONCE_LEN);
00124 
00125         /* Optional Parameters */
00126         *pos++ = FTIE_SUBELEM_R1KH_ID;
00127         *pos++ = FT_R1KH_ID_LEN;
00128         os_memcpy(pos, conf->r1_key_holder, FT_R1KH_ID_LEN);
00129         pos += FT_R1KH_ID_LEN;
00130 
00131         if (r0kh_id) {
00132                 *pos++ = FTIE_SUBELEM_R0KH_ID;
00133                 *pos++ = r0kh_id_len;
00134                 os_memcpy(pos, r0kh_id, r0kh_id_len);
00135                 pos += r0kh_id_len;
00136         }
00137 
00138         if (subelem) {
00139                 os_memcpy(pos, subelem, subelem_len);
00140                 pos += subelem_len;
00141         }
00142 
00143         *ielen = pos - buf - 2;
00144 
00145         return pos - buf;
00146 }
00147 
00148 
00149 struct wpa_ft_pmk_r0_sa {
00150         struct wpa_ft_pmk_r0_sa *next;
00151         u8 pmk_r0[PMK_LEN];
00152         u8 pmk_r0_name[WPA_PMK_NAME_LEN];
00153         u8 spa[ETH_ALEN];
00154         int pairwise; /* Pairwise cipher suite, WPA_CIPHER_* */
00155         /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */
00156         int pmk_r1_pushed;
00157 };
00158 
00159 struct wpa_ft_pmk_r1_sa {
00160         struct wpa_ft_pmk_r1_sa *next;
00161         u8 pmk_r1[PMK_LEN];
00162         u8 pmk_r1_name[WPA_PMK_NAME_LEN];
00163         u8 spa[ETH_ALEN];
00164         int pairwise; /* Pairwise cipher suite, WPA_CIPHER_* */
00165         /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */
00166 };
00167 
00168 struct wpa_ft_pmk_cache {
00169         struct wpa_ft_pmk_r0_sa *pmk_r0;
00170         struct wpa_ft_pmk_r1_sa *pmk_r1;
00171 };
00172 
00173 struct wpa_ft_pmk_cache * wpa_ft_pmk_cache_init(void)
00174 {
00175         struct wpa_ft_pmk_cache *cache;
00176 
00177         cache = os_zalloc(sizeof(*cache));
00178 
00179         return cache;
00180 }
00181 
00182 
00183 void wpa_ft_pmk_cache_deinit(struct wpa_ft_pmk_cache *cache)
00184 {
00185         struct wpa_ft_pmk_r0_sa *r0, *r0prev;
00186         struct wpa_ft_pmk_r1_sa *r1, *r1prev;
00187 
00188         r0 = cache->pmk_r0;
00189         while (r0) {
00190                 r0prev = r0;
00191                 r0 = r0->next;
00192                 os_memset(r0prev->pmk_r0, 0, PMK_LEN);
00193                 os_free(r0prev);
00194         }
00195 
00196         r1 = cache->pmk_r1;
00197         while (r1) {
00198                 r1prev = r1;
00199                 r1 = r1->next;
00200                 os_memset(r1prev->pmk_r1, 0, PMK_LEN);
00201                 os_free(r1prev);
00202         }
00203 
00204         os_free(cache);
00205 }
00206 
00207 
00208 static int wpa_ft_store_pmk_r0(struct wpa_authenticator *wpa_auth,
00209                                const u8 *spa, const u8 *pmk_r0,
00210                                const u8 *pmk_r0_name, int pairwise)
00211 {
00212         struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
00213         struct wpa_ft_pmk_r0_sa *r0;
00214 
00215         /* TODO: add expiration and limit on number of entries in cache */
00216 
00217         r0 = os_zalloc(sizeof(*r0));
00218         if (r0 == NULL)
00219                 return -1;
00220 
00221         os_memcpy(r0->pmk_r0, pmk_r0, PMK_LEN);
00222         os_memcpy(r0->pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN);
00223         os_memcpy(r0->spa, spa, ETH_ALEN);
00224         r0->pairwise = pairwise;
00225 
00226         r0->next = cache->pmk_r0;
00227         cache->pmk_r0 = r0;
00228 
00229         return 0;
00230 }
00231 
00232 
00233 static int wpa_ft_fetch_pmk_r0(struct wpa_authenticator *wpa_auth,
00234                                const u8 *spa, const u8 *pmk_r0_name,
00235                                u8 *pmk_r0, int *pairwise)
00236 {
00237         struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
00238         struct wpa_ft_pmk_r0_sa *r0;
00239 
00240         r0 = cache->pmk_r0;
00241         while (r0) {
00242                 if (os_memcmp(r0->spa, spa, ETH_ALEN) == 0 &&
00243                     os_memcmp(r0->pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN)
00244                     == 0) {
00245                         os_memcpy(pmk_r0, r0->pmk_r0, PMK_LEN);
00246                         if (pairwise)
00247                                 *pairwise = r0->pairwise;
00248                         return 0;
00249                 }
00250 
00251                 r0 = r0->next;
00252         }
00253 
00254         return -1;
00255 }
00256 
00257 
00258 static int wpa_ft_store_pmk_r1(struct wpa_authenticator *wpa_auth,
00259                                const u8 *spa, const u8 *pmk_r1,
00260                                const u8 *pmk_r1_name, int pairwise)
00261 {
00262         struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
00263         struct wpa_ft_pmk_r1_sa *r1;
00264 
00265         /* TODO: add expiration and limit on number of entries in cache */
00266 
00267         r1 = os_zalloc(sizeof(*r1));
00268         if (r1 == NULL)
00269                 return -1;
00270 
00271         os_memcpy(r1->pmk_r1, pmk_r1, PMK_LEN);
00272         os_memcpy(r1->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN);
00273         os_memcpy(r1->spa, spa, ETH_ALEN);
00274         r1->pairwise = pairwise;
00275 
00276         r1->next = cache->pmk_r1;
00277         cache->pmk_r1 = r1;
00278 
00279         return 0;
00280 }
00281 
00282 
00283 static int wpa_ft_fetch_pmk_r1(struct wpa_authenticator *wpa_auth,
00284                                const u8 *spa, const u8 *pmk_r1_name,
00285                                u8 *pmk_r1, int *pairwise)
00286 {
00287         struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
00288         struct wpa_ft_pmk_r1_sa *r1;
00289 
00290         r1 = cache->pmk_r1;
00291         while (r1) {
00292                 if (os_memcmp(r1->spa, spa, ETH_ALEN) == 0 &&
00293                     os_memcmp(r1->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN)
00294                     == 0) {
00295                         os_memcpy(pmk_r1, r1->pmk_r1, PMK_LEN);
00296                         if (pairwise)
00297                                 *pairwise = r1->pairwise;
00298                         return 0;
00299                 }
00300 
00301                 r1 = r1->next;
00302         }
00303 
00304         return -1;
00305 }
00306 
00307 
00308 static int wpa_ft_pull_pmk_r1(struct wpa_authenticator *wpa_auth,
00309                               const u8 *s1kh_id, const u8 *r0kh_id,
00310                               size_t r0kh_id_len, const u8 *pmk_r0_name)
00311 {
00312         struct ft_remote_r0kh *r0kh;
00313         struct ft_r0kh_r1kh_pull_frame frame, f;
00314 
00315         r0kh = wpa_auth->conf.r0kh_list;
00316         while (r0kh) {
00317                 if (r0kh->id_len == r0kh_id_len &&
00318                     os_memcmp(r0kh->id, r0kh_id, r0kh_id_len) == 0)
00319                         break;
00320                 r0kh = r0kh->next;
00321         }
00322         if (r0kh == NULL)
00323                 return -1;
00324 
00325         wpa_printf(MSG_DEBUG, "FT: Send PMK-R1 pull request to remote R0KH "
00326                    "address " MACSTR, MAC2STR(r0kh->addr));
00327 
00328         os_memset(&frame, 0, sizeof(frame));
00329         frame.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
00330         frame.packet_type = FT_PACKET_R0KH_R1KH_PULL;
00331         frame.data_length = host_to_le16(FT_R0KH_R1KH_PULL_DATA_LEN);
00332         os_memcpy(frame.ap_address, wpa_auth->addr, ETH_ALEN);
00333 
00334         /* aes_wrap() does not support inplace encryption, so use a temporary
00335          * buffer for the data. */
00336         if (os_get_random(f.nonce, sizeof(f.nonce))) {
00337                 wpa_printf(MSG_DEBUG, "FT: Failed to get random data for "
00338                            "nonce");
00339                 return -1;
00340         }
00341         os_memcpy(f.pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN);
00342         os_memcpy(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN);
00343         os_memcpy(f.s1kh_id, s1kh_id, ETH_ALEN);
00344 
00345         if (aes_wrap(r0kh->key, (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8,
00346                      f.nonce, frame.nonce) < 0)
00347                 return -1;
00348 
00349         wpa_ft_rrb_send(wpa_auth, r0kh->addr, (u8 *) &frame, sizeof(frame));
00350 
00351         return 0;
00352 }
00353 
00354 
00355 int wpa_auth_derive_ptk_ft(struct wpa_state_machine *sm, const u8 *pmk,
00356                            struct wpa_ptk *ptk, size_t ptk_len)
00357 {
00358         u8 pmk_r0[PMK_LEN], pmk_r0_name[WPA_PMK_NAME_LEN];
00359         u8 pmk_r1[PMK_LEN];
00360         u8 ptk_name[WPA_PMK_NAME_LEN];
00361         const u8 *mdid = sm->wpa_auth->conf.mobility_domain;
00362         const u8 *r0kh = sm->wpa_auth->conf.r0_key_holder;
00363         size_t r0kh_len = sm->wpa_auth->conf.r0_key_holder_len;
00364         const u8 *r1kh = sm->wpa_auth->conf.r1_key_holder;
00365         const u8 *ssid = sm->wpa_auth->conf.ssid;
00366         size_t ssid_len = sm->wpa_auth->conf.ssid_len;
00367 
00368 
00369         if (sm->xxkey_len == 0) {
00370                 wpa_printf(MSG_DEBUG, "FT: XXKey not available for key "
00371                            "derivation");
00372                 return -1;
00373         }
00374 
00375         wpa_derive_pmk_r0(sm->xxkey, sm->xxkey_len, ssid, ssid_len, mdid,
00376                           r0kh, r0kh_len, sm->addr, pmk_r0, pmk_r0_name);
00377         wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", pmk_r0, PMK_LEN);
00378         wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name", pmk_r0_name, WPA_PMK_NAME_LEN);
00379         wpa_ft_store_pmk_r0(sm->wpa_auth, sm->addr, pmk_r0, pmk_r0_name,
00380                             sm->pairwise);
00381 
00382         wpa_derive_pmk_r1(pmk_r0, pmk_r0_name, r1kh, sm->addr,
00383                           pmk_r1, sm->pmk_r1_name);
00384         wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", pmk_r1, PMK_LEN);
00385         wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", sm->pmk_r1_name,
00386                     WPA_PMK_NAME_LEN);
00387         wpa_ft_store_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1, sm->pmk_r1_name,
00388                             sm->pairwise);
00389 
00390         wpa_pmk_r1_to_ptk(pmk_r1, sm->SNonce, sm->ANonce, sm->addr,
00391                           sm->wpa_auth->addr, sm->pmk_r1_name,
00392                           (u8 *) ptk, ptk_len, ptk_name);
00393         wpa_hexdump_key(MSG_DEBUG, "FT: PTK", (u8 *) ptk, ptk_len);
00394         wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
00395 
00396         return 0;
00397 }
00398 
00399 
00400 static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
00401                                       const u8 *addr, int idx, u8 *seq)
00402 {
00403         if (wpa_auth->cb.get_seqnum == NULL)
00404                 return -1;
00405         return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
00406 }
00407 
00408 
00409 static u8 * wpa_ft_gtk_subelem(struct wpa_state_machine *sm, size_t *len)
00410 {
00411         u8 *subelem;
00412         struct wpa_group *gsm = sm->group;
00413         size_t subelem_len, pad_len;
00414         const u8 *key;
00415         size_t key_len;
00416         u8 keybuf[32];
00417 
00418         key_len = gsm->GTK_len;
00419         if (key_len > sizeof(keybuf))
00420                 return NULL;
00421 
00422         /*
00423          * Pad key for AES Key Wrap if it is not multiple of 8 bytes or is less
00424          * than 16 bytes.
00425          */
00426         pad_len = key_len % 8;
00427         if (pad_len)
00428                 pad_len = 8 - pad_len;
00429         if (key_len + pad_len < 16)
00430                 pad_len += 8;
00431         if (pad_len) {
00432                 os_memcpy(keybuf, gsm->GTK[gsm->GN - 1], key_len);
00433                 os_memset(keybuf + key_len, 0, pad_len);
00434                 keybuf[key_len] = 0xdd;
00435                 key_len += pad_len;
00436                 key = keybuf;
00437         } else
00438                 key = gsm->GTK[gsm->GN - 1];
00439 
00440         /*
00441          * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
00442          * Key[5..32].
00443          */
00444         subelem_len = 13 + key_len + 8;
00445         subelem = os_zalloc(subelem_len);
00446         if (subelem == NULL)
00447                 return NULL;
00448 
00449         subelem[0] = FTIE_SUBELEM_GTK;
00450         subelem[1] = 11 + key_len + 8;
00451         /* Key ID in B0-B1 of Key Info */
00452         WPA_PUT_LE16(&subelem[2], gsm->GN & 0x03);
00453         subelem[4] = gsm->GTK_len;
00454         wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, subelem + 5);
00455         if (aes_wrap(sm->PTK.kek, key_len / 8, key, subelem + 13)) {
00456                 os_free(subelem);
00457                 return NULL;
00458         }
00459 
00460         *len = subelem_len;
00461         return subelem;
00462 }
00463 
00464 
00465 #ifdef CONFIG_IEEE80211W
00466 static u8 * wpa_ft_igtk_subelem(struct wpa_state_machine *sm, size_t *len)
00467 {
00468         u8 *subelem, *pos;
00469         struct wpa_group *gsm = sm->group;
00470         size_t subelem_len;
00471 
00472         /* Sub-elem ID[1] | Length[1] | KeyID[2] | IPN[6] | Key Length[1] |
00473          * Key[16+8] */
00474         subelem_len = 1 + 1 + 2 + 6 + 1 + WPA_IGTK_LEN + 8;
00475         subelem = os_zalloc(subelem_len);
00476         if (subelem == NULL)
00477                 return NULL;
00478 
00479         pos = subelem;
00480         *pos++ = FTIE_SUBELEM_IGTK;
00481         *pos++ = subelem_len - 2;
00482         WPA_PUT_LE16(pos, gsm->GN_igtk);
00483         pos += 2;
00484         wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos);
00485         pos += 6;
00486         *pos++ = WPA_IGTK_LEN;
00487         if (aes_wrap(sm->PTK.kek, WPA_IGTK_LEN / 8,
00488                      gsm->IGTK[gsm->GN_igtk - 4], pos)) {
00489                 os_free(subelem);
00490                 return NULL;
00491         }
00492 
00493         *len = subelem_len;
00494         return subelem;
00495 }
00496 #endif /* CONFIG_IEEE80211W */
00497 
00498 
00499 static u8 * wpa_ft_process_rdie(u8 *pos, u8 *end, u8 id, u8 descr_count,
00500                                 const u8 *ies, size_t ies_len)
00501 {
00502         struct ieee802_11_elems parse;
00503         struct rsn_rdie *rdie;
00504 
00505         wpa_printf(MSG_DEBUG, "FT: Resource Request: id=%d descr_count=%d",
00506                    id, descr_count);
00507         wpa_hexdump(MSG_MSGDUMP, "FT: Resource descriptor IE(s)",
00508                     ies, ies_len);
00509 
00510         if (end - pos < (int) sizeof(*rdie)) {
00511                 wpa_printf(MSG_ERROR, "FT: Not enough room for response RDIE");
00512                 return pos;
00513         }
00514 
00515         *pos++ = WLAN_EID_RIC_DATA;
00516         *pos++ = sizeof(*rdie);
00517         rdie = (struct rsn_rdie *) pos;
00518         rdie->id = id;
00519         rdie->descr_count = 0;
00520         rdie->status_code = host_to_le16(WLAN_STATUS_SUCCESS);
00521         pos += sizeof(*rdie);
00522 
00523         if (ieee802_11_parse_elems((u8 *) ies, ies_len, &parse, 1) ==
00524             ParseFailed) {
00525                 wpa_printf(MSG_DEBUG, "FT: Failed to parse request IEs");
00526                 rdie->status_code =
00527                         host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
00528                 return pos;
00529         }
00530 
00531 #ifdef NEED_AP_MLME
00532         if (parse.wmm_tspec) {
00533                 struct wmm_tspec_element *tspec;
00534                 int res;
00535 
00536                 if (parse.wmm_tspec_len + 2 < (int) sizeof(*tspec)) {
00537                         wpa_printf(MSG_DEBUG, "FT: Too short WMM TSPEC IE "
00538                                    "(%d)", (int) parse.wmm_tspec_len);
00539                         rdie->status_code =
00540                                 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
00541                         return pos;
00542                 }
00543                 if (end - pos < (int) sizeof(*tspec)) {
00544                         wpa_printf(MSG_ERROR, "FT: Not enough room for "
00545                                    "response TSPEC");
00546                         rdie->status_code =
00547                                 host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
00548                         return pos;
00549                 }
00550                 tspec = (struct wmm_tspec_element *) pos;
00551                 os_memcpy(tspec, parse.wmm_tspec - 2, sizeof(*tspec));
00552                 res = wmm_process_tspec(tspec);
00553                 wpa_printf(MSG_DEBUG, "FT: ADDTS processing result: %d", res);
00554                 if (res == WMM_ADDTS_STATUS_INVALID_PARAMETERS)
00555                         rdie->status_code =
00556                                 host_to_le16(WLAN_STATUS_INVALID_PARAMETERS);
00557                 else if (res == WMM_ADDTS_STATUS_REFUSED)
00558                         rdie->status_code =
00559                                 host_to_le16(WLAN_STATUS_REQUEST_DECLINED);
00560                 else {
00561                         /* TSPEC accepted; include updated TSPEC in response */
00562                         rdie->descr_count = 1;
00563                         pos += sizeof(*tspec);
00564                 }
00565                 return pos;
00566         }
00567 #endif /* NEED_AP_MLME */
00568 
00569         wpa_printf(MSG_DEBUG, "FT: No supported resource requested");
00570         rdie->status_code = host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
00571         return pos;
00572 }
00573 
00574 
00575 static u8 * wpa_ft_process_ric(u8 *pos, u8 *end, const u8 *ric, size_t ric_len)
00576 {
00577         const u8 *rpos, *start;
00578         const struct rsn_rdie *rdie;
00579 
00580         wpa_hexdump(MSG_MSGDUMP, "FT: RIC Request", ric, ric_len);
00581 
00582         rpos = ric;
00583         while (rpos + sizeof(*rdie) < ric + ric_len) {
00584                 if (rpos[0] != WLAN_EID_RIC_DATA || rpos[1] < sizeof(*rdie) ||
00585                     rpos + 2 + rpos[1] > ric + ric_len)
00586                         break;
00587                 rdie = (const struct rsn_rdie *) (rpos + 2);
00588                 rpos += 2 + rpos[1];
00589                 start = rpos;
00590 
00591                 while (rpos + 2 <= ric + ric_len &&
00592                        rpos + 2 + rpos[1] <= ric + ric_len) {
00593                         if (rpos[0] == WLAN_EID_RIC_DATA)
00594                                 break;
00595                         rpos += 2 + rpos[1];
00596                 }
00597                 pos = wpa_ft_process_rdie(pos, end, rdie->id,
00598                                           rdie->descr_count,
00599                                           start, rpos - start);
00600         }
00601 
00602         return pos;
00603 }
00604 
00605 
00606 u8 * wpa_sm_write_assoc_resp_ies(struct wpa_state_machine *sm, u8 *pos,
00607                                  size_t max_len, int auth_alg,
00608                                  const u8 *req_ies, size_t req_ies_len)
00609 {
00610         u8 *end, *mdie, *ftie, *rsnie = NULL, *r0kh_id, *subelem = NULL;
00611         size_t mdie_len, ftie_len, rsnie_len = 0, r0kh_id_len, subelem_len = 0;
00612         int res;
00613         struct wpa_auth_config *conf;
00614         struct rsn_ftie *_ftie;
00615         struct wpa_ft_ies parse;
00616         u8 *ric_start;
00617         u8 *anonce, *snonce;
00618 
00619         if (sm == NULL)
00620                 return pos;
00621 
00622         conf = &sm->wpa_auth->conf;
00623 
00624         if (sm->wpa_key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
00625             sm->wpa_key_mgmt != WPA_KEY_MGMT_FT_PSK)
00626                 return pos;
00627 
00628         end = pos + max_len;
00629 
00630         if (auth_alg == WLAN_AUTH_FT) {
00631                 /*
00632                  * RSN (only present if this is a Reassociation Response and
00633                  * part of a fast BSS transition)
00634                  */
00635                 res = wpa_write_rsn_ie(conf, pos, end - pos, sm->pmk_r1_name);
00636                 if (res < 0)
00637                         return pos;
00638                 rsnie = pos;
00639                 rsnie_len = res;
00640                 pos += res;
00641         }
00642 
00643         /* Mobility Domain Information */
00644         res = wpa_write_mdie(conf, pos, end - pos);
00645         if (res < 0)
00646                 return pos;
00647         mdie = pos;
00648         mdie_len = res;
00649         pos += res;
00650 
00651         /* Fast BSS Transition Information */
00652         if (auth_alg == WLAN_AUTH_FT) {
00653                 subelem = wpa_ft_gtk_subelem(sm, &subelem_len);
00654                 r0kh_id = sm->r0kh_id;
00655                 r0kh_id_len = sm->r0kh_id_len;
00656                 anonce = sm->ANonce;
00657                 snonce = sm->SNonce;
00658 #ifdef CONFIG_IEEE80211W
00659                 if (sm->mgmt_frame_prot) {
00660                         u8 *igtk;
00661                         size_t igtk_len;
00662                         u8 *nbuf;
00663                         igtk = wpa_ft_igtk_subelem(sm, &igtk_len);
00664                         if (igtk == NULL) {
00665                                 os_free(subelem);
00666                                 return pos;
00667                         }
00668                         nbuf = os_realloc(subelem, subelem_len + igtk_len);
00669                         if (nbuf == NULL) {
00670                                 os_free(subelem);
00671                                 os_free(igtk);
00672                                 return pos;
00673                         }
00674                         subelem = nbuf;
00675                         os_memcpy(subelem + subelem_len, igtk, igtk_len);
00676                         subelem_len += igtk_len;
00677                         os_free(igtk);
00678                 }
00679 #endif /* CONFIG_IEEE80211W */
00680         } else {
00681                 r0kh_id = conf->r0_key_holder;
00682                 r0kh_id_len = conf->r0_key_holder_len;
00683                 anonce = NULL;
00684                 snonce = NULL;
00685         }
00686         res = wpa_write_ftie(conf, r0kh_id, r0kh_id_len, anonce, snonce, pos,
00687                              end - pos, subelem, subelem_len);
00688         os_free(subelem);
00689         if (res < 0)
00690                 return pos;
00691         ftie = pos;
00692         ftie_len = res;
00693         pos += res;
00694 
00695         os_free(sm->assoc_resp_ftie);
00696         sm->assoc_resp_ftie = os_malloc(ftie_len);
00697         if (sm->assoc_resp_ftie)
00698                 os_memcpy(sm->assoc_resp_ftie, ftie, ftie_len);
00699 
00700         _ftie = (struct rsn_ftie *) (ftie + 2);
00701         if (auth_alg == WLAN_AUTH_FT)
00702                 _ftie->mic_control[1] = 3; /* Information element count */
00703 
00704         ric_start = pos;
00705         if (wpa_ft_parse_ies(req_ies, req_ies_len, &parse) == 0 && parse.ric) {
00706                 pos = wpa_ft_process_ric(pos, end, parse.ric, parse.ric_len);
00707                 if (auth_alg == WLAN_AUTH_FT)
00708                         _ftie->mic_control[1] +=
00709                                 ieee802_11_ie_count(ric_start,
00710                                                     pos - ric_start);
00711         }
00712         if (ric_start == pos)
00713                 ric_start = NULL;
00714 
00715         if (auth_alg == WLAN_AUTH_FT &&
00716             wpa_ft_mic(sm->PTK.kck, sm->addr, sm->wpa_auth->addr, 6,
00717                        mdie, mdie_len, ftie, ftie_len,
00718                        rsnie, rsnie_len,
00719                        ric_start, ric_start ? pos - ric_start : 0,
00720                        _ftie->mic) < 0)
00721                 wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
00722 
00723         return pos;
00724 }
00725 
00726 
00727 static int wpa_ft_parse_ftie(const u8 *ie, size_t ie_len,
00728                              struct wpa_ft_ies *parse)
00729 {
00730         const u8 *end, *pos;
00731 
00732         parse->ftie = ie;
00733         parse->ftie_len = ie_len;
00734 
00735         pos = ie + sizeof(struct rsn_ftie);
00736         end = ie + ie_len;
00737 
00738         while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
00739                 switch (pos[0]) {
00740                 case FTIE_SUBELEM_R1KH_ID:
00741                         if (pos[1] != FT_R1KH_ID_LEN) {
00742                                 wpa_printf(MSG_DEBUG, "FT: Invalid R1KH-ID "
00743                                            "length in FTIE: %d", pos[1]);
00744                                 return -1;
00745                         }
00746                         parse->r1kh_id = pos + 2;
00747                         break;
00748                 case FTIE_SUBELEM_GTK:
00749                         parse->gtk = pos + 2;
00750                         parse->gtk_len = pos[1];
00751                         break;
00752                 case FTIE_SUBELEM_R0KH_ID:
00753                         if (pos[1] < 1 || pos[1] > FT_R0KH_ID_MAX_LEN) {
00754                                 wpa_printf(MSG_DEBUG, "FT: Invalid R0KH-ID "
00755                                            "length in FTIE: %d", pos[1]);
00756                                 return -1;
00757                         }
00758                         parse->r0kh_id = pos + 2;
00759                         parse->r0kh_id_len = pos[1];
00760                         break;
00761                 }
00762 
00763                 pos += 2 + pos[1];
00764         }
00765 
00766         return 0;
00767 }
00768 
00769 
00770 static int wpa_ft_parse_ies(const u8 *ies, size_t ies_len,
00771                             struct wpa_ft_ies *parse)
00772 {
00773         const u8 *end, *pos;
00774         struct wpa_ie_data data;
00775         int ret;
00776         const struct rsn_ftie *ftie;
00777         int prot_ie_count = 0;
00778 
00779         os_memset(parse, 0, sizeof(*parse));
00780         if (ies == NULL)
00781                 return 0;
00782 
00783         pos = ies;
00784         end = ies + ies_len;
00785         while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
00786                 switch (pos[0]) {
00787                 case WLAN_EID_RSN:
00788                         parse->rsn = pos + 2;
00789                         parse->rsn_len = pos[1];
00790                         ret = wpa_parse_wpa_ie_rsn(parse->rsn - 2,
00791                                                    parse->rsn_len + 2,
00792                                                    &data);
00793                         if (ret < 0) {
00794                                 wpa_printf(MSG_DEBUG, "FT: Failed to parse "
00795                                            "RSN IE: %d", ret);
00796                                 return -1;
00797                         }
00798                         if (data.num_pmkid == 1 && data.pmkid)
00799                                 parse->rsn_pmkid = data.pmkid;
00800                         break;
00801                 case WLAN_EID_MOBILITY_DOMAIN:
00802                         parse->mdie = pos + 2;
00803                         parse->mdie_len = pos[1];
00804                         break;
00805                 case WLAN_EID_FAST_BSS_TRANSITION:
00806                         if (pos[1] < sizeof(*ftie))
00807                                 return -1;
00808                         ftie = (const struct rsn_ftie *) (pos + 2);
00809                         prot_ie_count = ftie->mic_control[1];
00810                         if (wpa_ft_parse_ftie(pos + 2, pos[1], parse) < 0)
00811                                 return -1;
00812                         break;
00813                 case WLAN_EID_RIC_DATA:
00814                         if (parse->ric == NULL)
00815                                 parse->ric = pos;
00816                 }
00817 
00818                 pos += 2 + pos[1];
00819         }
00820 
00821         if (prot_ie_count == 0)
00822                 return 0; /* no MIC */
00823 
00824         /*
00825          * Check that the protected IE count matches with IEs included in the
00826          * frame.
00827          */
00828         if (parse->rsn)
00829                 prot_ie_count--;
00830         if (parse->mdie)
00831                 prot_ie_count--;
00832         if (parse->ftie)
00833                 prot_ie_count--;
00834         if (prot_ie_count < 0) {
00835                 wpa_printf(MSG_DEBUG, "FT: Some required IEs not included in "
00836                            "the protected IE count");
00837                 return -1;
00838         }
00839 
00840         if (prot_ie_count == 0 && parse->ric) {
00841                 wpa_printf(MSG_DEBUG, "FT: RIC IE(s) in the frame, but not "
00842                            "included in protected IE count");
00843                 return -1;
00844         }
00845 
00846         /* Determine the end of the RIC IE(s) */
00847         pos = parse->ric;
00848         while (pos && pos + 2 <= end && pos + 2 + pos[1] <= end &&
00849                prot_ie_count) {
00850                 prot_ie_count--;
00851                 pos += 2 + pos[1];
00852         }
00853         parse->ric_len = pos - parse->ric;
00854         if (prot_ie_count) {
00855                 wpa_printf(MSG_DEBUG, "FT: %d protected IEs missing from "
00856                            "frame", (int) prot_ie_count);
00857                 return -1;
00858         }
00859 
00860         return 0;
00861 }
00862 
00863 
00864 static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
00865                                    int vlan_id,
00866                                    enum wpa_alg alg, const u8 *addr, int idx,
00867                                    u8 *key, size_t key_len)
00868 {
00869         if (wpa_auth->cb.set_key == NULL)
00870                 return -1;
00871         return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
00872                                     key, key_len);
00873 }
00874 
00875 
00876 void wpa_ft_install_ptk(struct wpa_state_machine *sm)
00877 {
00878         enum wpa_alg alg;
00879         int klen;
00880 
00881         /* MLME-SETKEYS.request(PTK) */
00882         if (sm->pairwise == WPA_CIPHER_TKIP) {
00883                 alg = WPA_ALG_TKIP;
00884                 klen = 32;
00885         } else if (sm->pairwise == WPA_CIPHER_CCMP) {
00886                 alg = WPA_ALG_CCMP;
00887                 klen = 16;
00888         } else {
00889                 wpa_printf(MSG_DEBUG, "FT: Unknown pairwise alg 0x%x - skip "
00890                            "PTK configuration", sm->pairwise);
00891                 return;
00892         }
00893 
00894         /* FIX: add STA entry to kernel/driver here? The set_key will fail
00895          * most likely without this.. At the moment, STA entry is added only
00896          * after association has been completed. This function will be called
00897          * again after association to get the PTK configured, but that could be
00898          * optimized by adding the STA entry earlier.
00899          */
00900         if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
00901                              sm->PTK.tk1, klen))
00902                 return;
00903 
00904         /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
00905         sm->pairwise_set = TRUE;
00906 }
00907 
00908 
00909 static u16 wpa_ft_process_auth_req(struct wpa_state_machine *sm,
00910                                    const u8 *ies, size_t ies_len,
00911                                    u8 **resp_ies, size_t *resp_ies_len)
00912 {
00913         struct rsn_mdie *mdie;
00914         struct rsn_ftie *ftie;
00915         u8 pmk_r1[PMK_LEN], pmk_r1_name[WPA_PMK_NAME_LEN];
00916         u8 ptk_name[WPA_PMK_NAME_LEN];
00917         struct wpa_auth_config *conf;
00918         struct wpa_ft_ies parse;
00919         size_t buflen, ptk_len;
00920         int ret;
00921         u8 *pos, *end;
00922         int pairwise;
00923 
00924         *resp_ies = NULL;
00925         *resp_ies_len = 0;
00926 
00927         sm->pmk_r1_name_valid = 0;
00928         conf = &sm->wpa_auth->conf;
00929 
00930         wpa_hexdump(MSG_DEBUG, "FT: Received authentication frame IEs",
00931                     ies, ies_len);
00932 
00933         if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
00934                 wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
00935                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
00936         }
00937 
00938         mdie = (struct rsn_mdie *) parse.mdie;
00939         if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
00940             os_memcmp(mdie->mobility_domain,
00941                       sm->wpa_auth->conf.mobility_domain,
00942                       MOBILITY_DOMAIN_ID_LEN) != 0) {
00943                 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
00944                 return WLAN_STATUS_INVALID_MDIE;
00945         }
00946 
00947         ftie = (struct rsn_ftie *) parse.ftie;
00948         if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
00949                 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
00950                 return WLAN_STATUS_INVALID_FTIE;
00951         }
00952 
00953         os_memcpy(sm->SNonce, ftie->snonce, WPA_NONCE_LEN);
00954 
00955         if (parse.r0kh_id == NULL) {
00956                 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE - no R0KH-ID");
00957                 return WLAN_STATUS_INVALID_FTIE;
00958         }
00959 
00960         wpa_hexdump(MSG_DEBUG, "FT: STA R0KH-ID",
00961                     parse.r0kh_id, parse.r0kh_id_len);
00962         os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
00963         sm->r0kh_id_len = parse.r0kh_id_len;
00964 
00965         if (parse.rsn_pmkid == NULL) {
00966                 wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
00967                 return WLAN_STATUS_INVALID_PMKID;
00968         }
00969 
00970         wpa_hexdump(MSG_DEBUG, "FT: Requested PMKR0Name",
00971                     parse.rsn_pmkid, WPA_PMK_NAME_LEN);
00972         wpa_derive_pmk_r1_name(parse.rsn_pmkid,
00973                                sm->wpa_auth->conf.r1_key_holder, sm->addr,
00974                                pmk_r1_name);
00975         wpa_hexdump(MSG_DEBUG, "FT: Derived requested PMKR1Name",
00976                     pmk_r1_name, WPA_PMK_NAME_LEN);
00977 
00978         if (wpa_ft_fetch_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1_name, pmk_r1,
00979                     &pairwise) < 0) {
00980                 if (wpa_ft_pull_pmk_r1(sm->wpa_auth, sm->addr, sm->r0kh_id,
00981                                        sm->r0kh_id_len, parse.rsn_pmkid) < 0) {
00982                         wpa_printf(MSG_DEBUG, "FT: Did not have matching "
00983                                    "PMK-R1 and unknown R0KH-ID");
00984                         return WLAN_STATUS_INVALID_PMKID;
00985                 }
00986 
00987                 /*
00988                  * TODO: Should return "status pending" (and the caller should
00989                  * not send out response now). The real response will be sent
00990                  * once the response from R0KH is received.
00991                  */
00992                 return WLAN_STATUS_INVALID_PMKID;
00993         }
00994 
00995         wpa_hexdump_key(MSG_DEBUG, "FT: Selected PMK-R1", pmk_r1, PMK_LEN);
00996         sm->pmk_r1_name_valid = 1;
00997         os_memcpy(sm->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN);
00998 
00999         if (os_get_random(sm->ANonce, WPA_NONCE_LEN)) {
01000                 wpa_printf(MSG_DEBUG, "FT: Failed to get random data for "
01001                            "ANonce");
01002                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
01003         }
01004 
01005         wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
01006                     sm->SNonce, WPA_NONCE_LEN);
01007         wpa_hexdump(MSG_DEBUG, "FT: Generated ANonce",
01008                     sm->ANonce, WPA_NONCE_LEN);
01009 
01010         ptk_len = pairwise != WPA_CIPHER_CCMP ? 64 : 48;
01011         wpa_pmk_r1_to_ptk(pmk_r1, sm->SNonce, sm->ANonce, sm->addr,
01012                           sm->wpa_auth->addr, pmk_r1_name,
01013                           (u8 *) &sm->PTK, ptk_len, ptk_name);
01014         wpa_hexdump_key(MSG_DEBUG, "FT: PTK",
01015                         (u8 *) &sm->PTK, ptk_len);
01016         wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
01017 
01018         sm->pairwise = pairwise;
01019         wpa_ft_install_ptk(sm);
01020 
01021         buflen = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) +
01022                 2 + FT_R1KH_ID_LEN + 200;
01023         *resp_ies = os_zalloc(buflen);
01024         if (*resp_ies == NULL) {
01025                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
01026         }
01027 
01028         pos = *resp_ies;
01029         end = *resp_ies + buflen;
01030 
01031         ret = wpa_write_rsn_ie(conf, pos, end - pos, parse.rsn_pmkid);
01032         if (ret < 0) {
01033                 os_free(*resp_ies);
01034                 *resp_ies = NULL;
01035                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
01036         }
01037         pos += ret;
01038 
01039         ret = wpa_write_mdie(conf, pos, end - pos);
01040         if (ret < 0) {
01041                 os_free(*resp_ies);
01042                 *resp_ies = NULL;
01043                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
01044         }
01045         pos += ret;
01046 
01047         ret = wpa_write_ftie(conf, parse.r0kh_id, parse.r0kh_id_len,
01048                              sm->ANonce, sm->SNonce, pos, end - pos, NULL, 0);
01049         if (ret < 0) {
01050                 os_free(*resp_ies);
01051                 *resp_ies = NULL;
01052                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
01053         }
01054         pos += ret;
01055 
01056         *resp_ies_len = pos - *resp_ies;
01057 
01058         return WLAN_STATUS_SUCCESS;
01059 }
01060 
01061 
01062 void wpa_ft_process_auth(struct wpa_state_machine *sm, const u8 *bssid,
01063                          u16 auth_transaction, const u8 *ies, size_t ies_len,
01064                          void (*cb)(void *ctx, const u8 *dst, const u8 *bssid,
01065                                     u16 auth_transaction, u16 status,
01066                                     const u8 *ies, size_t ies_len),
01067                          void *ctx)
01068 {
01069         u16 status;
01070         u8 *resp_ies;
01071         size_t resp_ies_len;
01072 
01073         if (sm == NULL) {
01074                 wpa_printf(MSG_DEBUG, "FT: Received authentication frame, but "
01075                            "WPA SM not available");
01076                 return;
01077         }
01078 
01079         wpa_printf(MSG_DEBUG, "FT: Received authentication frame: STA=" MACSTR
01080                    " BSSID=" MACSTR " transaction=%d",
01081                    MAC2STR(sm->addr), MAC2STR(bssid), auth_transaction);
01082         status = wpa_ft_process_auth_req(sm, ies, ies_len, &resp_ies,
01083                                          &resp_ies_len);
01084 
01085         wpa_printf(MSG_DEBUG, "FT: FT authentication response: dst=" MACSTR
01086                    " auth_transaction=%d status=%d",
01087                    MAC2STR(sm->addr), auth_transaction + 1, status);
01088         wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
01089         cb(ctx, sm->addr, bssid, auth_transaction + 1, status,
01090            resp_ies, resp_ies_len);
01091         os_free(resp_ies);
01092 }
01093 
01094 
01095 u16 wpa_ft_validate_reassoc(struct wpa_state_machine *sm, const u8 *ies,
01096                             size_t ies_len)
01097 {
01098         struct wpa_ft_ies parse;
01099         struct rsn_mdie *mdie;
01100         struct rsn_ftie *ftie;
01101         u8 mic[16];
01102         unsigned int count;
01103 
01104         if (sm == NULL)
01105                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
01106 
01107         wpa_hexdump(MSG_DEBUG, "FT: Reassoc Req IEs", ies, ies_len);
01108 
01109         if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
01110                 wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
01111                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
01112         }
01113 
01114         if (parse.rsn == NULL) {
01115                 wpa_printf(MSG_DEBUG, "FT: No RSNIE in Reassoc Req");
01116                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
01117         }
01118 
01119         if (parse.rsn_pmkid == NULL) {
01120                 wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
01121                 return WLAN_STATUS_INVALID_PMKID;
01122         }
01123 
01124         if (os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
01125         {
01126                 wpa_printf(MSG_DEBUG, "FT: PMKID in Reassoc Req did not match "
01127                            "with the PMKR1Name derived from auth request");
01128                 return WLAN_STATUS_INVALID_PMKID;
01129         }
01130 
01131         mdie = (struct rsn_mdie *) parse.mdie;
01132         if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
01133             os_memcmp(mdie->mobility_domain,
01134                       sm->wpa_auth->conf.mobility_domain,
01135                       MOBILITY_DOMAIN_ID_LEN) != 0) {
01136                 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
01137                 return WLAN_STATUS_INVALID_MDIE;
01138         }
01139 
01140         ftie = (struct rsn_ftie *) parse.ftie;
01141         if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
01142                 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
01143                 return WLAN_STATUS_INVALID_FTIE;
01144         }
01145 
01146         if (os_memcmp(ftie->snonce, sm->SNonce, WPA_NONCE_LEN) != 0) {
01147                 wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
01148                 wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
01149                             ftie->snonce, WPA_NONCE_LEN);
01150                 wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
01151                             sm->SNonce, WPA_NONCE_LEN);
01152                 return -1;
01153         }
01154 
01155         if (os_memcmp(ftie->anonce, sm->ANonce, WPA_NONCE_LEN) != 0) {
01156                 wpa_printf(MSG_DEBUG, "FT: ANonce mismatch in FTIE");
01157                 wpa_hexdump(MSG_DEBUG, "FT: Received ANonce",
01158                             ftie->anonce, WPA_NONCE_LEN);
01159                 wpa_hexdump(MSG_DEBUG, "FT: Expected ANonce",
01160                             sm->ANonce, WPA_NONCE_LEN);
01161                 return -1;
01162         }
01163 
01164 
01165         if (parse.r0kh_id == NULL) {
01166                 wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
01167                 return -1;
01168         }
01169 
01170         if (parse.r0kh_id_len != sm->r0kh_id_len ||
01171             os_memcmp(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0) {
01172                 wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
01173                            "the current R0KH-ID");
01174                 wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
01175                             parse.r0kh_id, parse.r0kh_id_len);
01176                 wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
01177                             sm->r0kh_id, sm->r0kh_id_len);
01178                 return -1;
01179         }
01180 
01181         if (parse.r1kh_id == NULL) {
01182                 wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
01183                 return -1;
01184         }
01185 
01186         if (os_memcmp(parse.r1kh_id, sm->wpa_auth->conf.r1_key_holder,
01187                       FT_R1KH_ID_LEN) != 0) {
01188                 wpa_printf(MSG_DEBUG, "FT: Unknown R1KH-ID used in "
01189                            "ReassocReq");
01190                 wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID in FTIE",
01191                             parse.r1kh_id, FT_R1KH_ID_LEN);
01192                 wpa_hexdump(MSG_DEBUG, "FT: Expected R1KH-ID",
01193                             sm->wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN);
01194                 return -1;
01195         }
01196 
01197         if (parse.rsn_pmkid == NULL ||
01198             os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN)) {
01199                 wpa_printf(MSG_DEBUG, "FT: No matching PMKR1Name (PMKID) in "
01200                            "RSNIE (pmkid=%d)", !!parse.rsn_pmkid);
01201                 return -1;
01202         }
01203 
01204         count = 3;
01205         if (parse.ric)
01206                 count++;
01207         if (ftie->mic_control[1] != count) {
01208                 wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in MIC "
01209                            "Control: received %u expected %u",
01210                            ftie->mic_control[1], count);
01211                 return -1;
01212         }
01213 
01214         if (wpa_ft_mic(sm->PTK.kck, sm->addr, sm->wpa_auth->addr, 5,
01215                        parse.mdie - 2, parse.mdie_len + 2,
01216                        parse.ftie - 2, parse.ftie_len + 2,
01217                        parse.rsn - 2, parse.rsn_len + 2,
01218                        parse.ric, parse.ric_len,
01219                        mic) < 0) {
01220                 wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
01221                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
01222         }
01223 
01224         if (os_memcmp(mic, ftie->mic, 16) != 0) {
01225                 wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
01226                 wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", ftie->mic, 16);
01227                 wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16);
01228                 return WLAN_STATUS_INVALID_FTIE;
01229         }
01230 
01231         return WLAN_STATUS_SUCCESS;
01232 }
01233 
01234 
01235 int wpa_ft_action_rx(struct wpa_state_machine *sm, const u8 *data, size_t len)
01236 {
01237         const u8 *sta_addr, *target_ap;
01238         const u8 *ies;
01239         size_t ies_len;
01240         u8 action;
01241         struct ft_rrb_frame *frame;
01242 
01243         if (sm == NULL)
01244                 return -1;
01245 
01246         /*
01247          * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
01248          * FT Request action frame body[variable]
01249          */
01250 
01251         if (len < 14) {
01252                 wpa_printf(MSG_DEBUG, "FT: Too short FT Action frame "
01253                            "(len=%lu)", (unsigned long) len);
01254                 return -1;
01255         }
01256 
01257         action = data[1];
01258         sta_addr = data + 2;
01259         target_ap = data + 8;
01260         ies = data + 14;
01261         ies_len = len - 14;
01262 
01263         wpa_printf(MSG_DEBUG, "FT: Received FT Action frame (STA=" MACSTR
01264                    " Target AP=" MACSTR " Action=%d)",
01265                    MAC2STR(sta_addr), MAC2STR(target_ap), action);
01266 
01267         if (os_memcmp(sta_addr, sm->addr, ETH_ALEN) != 0) {
01268                 wpa_printf(MSG_DEBUG, "FT: Mismatch in FT Action STA address: "
01269                            "STA=" MACSTR " STA-Address=" MACSTR,
01270                            MAC2STR(sm->addr), MAC2STR(sta_addr));
01271                 return -1;
01272         }
01273 
01274         /*
01275          * Do some sanity checking on the target AP address (not own and not
01276          * broadcast. This could be extended to filter based on a list of known
01277          * APs in the MD (if such a list were configured).
01278          */
01279         if ((target_ap[0] & 0x01) ||
01280             os_memcmp(target_ap, sm->wpa_auth->addr, ETH_ALEN) == 0) {
01281                 wpa_printf(MSG_DEBUG, "FT: Invalid Target AP in FT Action "
01282                            "frame");
01283                 return -1;
01284         }
01285 
01286         wpa_hexdump(MSG_MSGDUMP, "FT: Action frame body", ies, ies_len);
01287 
01288         /* RRB - Forward action frame to the target AP */
01289         frame = os_malloc(sizeof(*frame) + len);
01290         frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
01291         frame->packet_type = FT_PACKET_REQUEST;
01292         frame->action_length = host_to_le16(len);
01293         os_memcpy(frame->ap_address, sm->wpa_auth->addr, ETH_ALEN);
01294         os_memcpy(frame + 1, data, len);
01295 
01296         wpa_ft_rrb_send(sm->wpa_auth, target_ap, (u8 *) frame,
01297                         sizeof(*frame) + len);
01298         os_free(frame);
01299 
01300         return 0;
01301 }
01302 
01303 
01304 static int wpa_ft_rrb_rx_request(struct wpa_authenticator *wpa_auth,
01305                                  const u8 *current_ap, const u8 *sta_addr,
01306                                  const u8 *body, size_t len)
01307 {
01308         struct wpa_state_machine *sm;
01309         u16 status;
01310         u8 *resp_ies, *pos;
01311         size_t resp_ies_len, rlen;
01312         struct ft_rrb_frame *frame;
01313 
01314         sm = wpa_ft_add_sta(wpa_auth, sta_addr);
01315         if (sm == NULL) {
01316                 wpa_printf(MSG_DEBUG, "FT: Failed to add new STA based on "
01317                            "RRB Request");
01318                 return -1;
01319         }
01320 
01321         wpa_hexdump(MSG_MSGDUMP, "FT: RRB Request Frame body", body, len);
01322 
01323         status = wpa_ft_process_auth_req(sm, body, len, &resp_ies,
01324                                          &resp_ies_len);
01325 
01326         wpa_printf(MSG_DEBUG, "FT: RRB authentication response: STA=" MACSTR
01327                    " CurrentAP=" MACSTR " status=%d",
01328                    MAC2STR(sm->addr), MAC2STR(current_ap), status);
01329         wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
01330 
01331         /* RRB - Forward action frame response to the Current AP */
01332 
01333         /*
01334          * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
01335          * Status_Code[2] FT Request action frame body[variable]
01336          */
01337         rlen = 2 + 2 * ETH_ALEN + 2 + resp_ies_len;
01338 
01339         frame = os_malloc(sizeof(*frame) + rlen);
01340         frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
01341         frame->packet_type = FT_PACKET_RESPONSE;
01342         frame->action_length = host_to_le16(rlen);
01343         os_memcpy(frame->ap_address, wpa_auth->addr, ETH_ALEN);
01344         pos = (u8 *) (frame + 1);
01345         *pos++ = WLAN_ACTION_FT;
01346         *pos++ = 2; /* Action: Response */
01347         os_memcpy(pos, sta_addr, ETH_ALEN);
01348         pos += ETH_ALEN;
01349         os_memcpy(pos, wpa_auth->addr, ETH_ALEN);
01350         pos += ETH_ALEN;
01351         WPA_PUT_LE16(pos, status);
01352         pos += 2;
01353         if (resp_ies) {
01354                 os_memcpy(pos, resp_ies, resp_ies_len);
01355                 os_free(resp_ies);
01356         }
01357 
01358         wpa_ft_rrb_send(wpa_auth, current_ap, (u8 *) frame,
01359                         sizeof(*frame) + rlen);
01360         os_free(frame);
01361 
01362         return 0;
01363 }
01364 
01365 
01366 static int wpa_ft_rrb_rx_pull(struct wpa_authenticator *wpa_auth,
01367                               const u8 *src_addr,
01368                               const u8 *data, size_t data_len)
01369 {
01370         struct ft_r0kh_r1kh_pull_frame *frame, f;
01371         struct ft_remote_r1kh *r1kh;
01372         struct ft_r0kh_r1kh_resp_frame resp, r;
01373         u8 pmk_r0[PMK_LEN];
01374         int pairwise;
01375 
01376         wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull");
01377 
01378         if (data_len < sizeof(*frame))
01379                 return -1;
01380 
01381         r1kh = wpa_auth->conf.r1kh_list;
01382         while (r1kh) {
01383                 if (os_memcmp(r1kh->addr, src_addr, ETH_ALEN) == 0)
01384                         break;
01385                 r1kh = r1kh->next;
01386         }
01387         if (r1kh == NULL) {
01388                 wpa_printf(MSG_DEBUG, "FT: No matching R1KH address found for "
01389                            "PMK-R1 pull source address " MACSTR,
01390                            MAC2STR(src_addr));
01391                 return -1;
01392         }
01393 
01394         frame = (struct ft_r0kh_r1kh_pull_frame *) data;
01395         /* aes_unwrap() does not support inplace decryption, so use a temporary
01396          * buffer for the data. */
01397         if (aes_unwrap(r1kh->key, (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8,
01398                        frame->nonce, f.nonce) < 0) {
01399                 wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull "
01400                            "request from " MACSTR, MAC2STR(src_addr));
01401                 return -1;
01402         }
01403 
01404         wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce",
01405                     f.nonce, sizeof(f.nonce));
01406         wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR0Name",
01407                     f.pmk_r0_name, WPA_PMK_NAME_LEN);
01408         wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID="
01409                    MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));
01410 
01411         os_memset(&resp, 0, sizeof(resp));
01412         resp.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
01413         resp.packet_type = FT_PACKET_R0KH_R1KH_RESP;
01414         resp.data_length = host_to_le16(FT_R0KH_R1KH_RESP_DATA_LEN);
01415         os_memcpy(resp.ap_address, wpa_auth->addr, ETH_ALEN);
01416 
01417         /* aes_wrap() does not support inplace encryption, so use a temporary
01418          * buffer for the data. */
01419         os_memcpy(r.nonce, f.nonce, sizeof(f.nonce));
01420         os_memcpy(r.r1kh_id, f.r1kh_id, FT_R1KH_ID_LEN);
01421         os_memcpy(r.s1kh_id, f.s1kh_id, ETH_ALEN);
01422         if (wpa_ft_fetch_pmk_r0(wpa_auth, f.s1kh_id, f.pmk_r0_name, pmk_r0,
01423                                 &pairwise) < 0) {
01424                 wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name found for "
01425                            "PMK-R1 pull");
01426                 return -1;
01427         }
01428 
01429         wpa_derive_pmk_r1(pmk_r0, f.pmk_r0_name, f.r1kh_id, f.s1kh_id,
01430                           r.pmk_r1, r.pmk_r1_name);
01431         wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", r.pmk_r1, PMK_LEN);
01432         wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", r.pmk_r1_name,
01433                     WPA_PMK_NAME_LEN);
01434         r.pairwise = pairwise;
01435 
01436         if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8,
01437                      r.nonce, resp.nonce) < 0) {
01438                 os_memset(pmk_r0, 0, PMK_LEN);
01439                 return -1;
01440         }
01441 
01442         os_memset(pmk_r0, 0, PMK_LEN);
01443 
01444         wpa_ft_rrb_send(wpa_auth, src_addr, (u8 *) &resp, sizeof(resp));
01445 
01446         return 0;
01447 }
01448 
01449 
01450 static int wpa_ft_rrb_rx_resp(struct wpa_authenticator *wpa_auth,
01451                               const u8 *src_addr,
01452                               const u8 *data, size_t data_len)
01453 {
01454         struct ft_r0kh_r1kh_resp_frame *frame, f;
01455         struct ft_remote_r0kh *r0kh;
01456         int pairwise;
01457 
01458         wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull response");
01459 
01460         if (data_len < sizeof(*frame))
01461                 return -1;
01462 
01463         r0kh = wpa_auth->conf.r0kh_list;
01464         while (r0kh) {
01465                 if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0)
01466                         break;
01467                 r0kh = r0kh->next;
01468         }
01469         if (r0kh == NULL) {
01470                 wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for "
01471                            "PMK-R0 pull response source address " MACSTR,
01472                            MAC2STR(src_addr));
01473                 return -1;
01474         }
01475 
01476         frame = (struct ft_r0kh_r1kh_resp_frame *) data;
01477         /* aes_unwrap() does not support inplace decryption, so use a temporary
01478          * buffer for the data. */
01479         if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8,
01480                        frame->nonce, f.nonce) < 0) {
01481                 wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull "
01482                            "response from " MACSTR, MAC2STR(src_addr));
01483                 return -1;
01484         }
01485 
01486         if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN)
01487             != 0) {
01488                 wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull response did not use a "
01489                            "matching R1KH-ID");
01490                 return -1;
01491         }
01492 
01493         /* TODO: verify that <nonce,s1kh_id> matches with a pending request
01494          * and call this requests callback function to finish request
01495          * processing */
01496 
01497         pairwise = le_to_host16(f.pairwise);
01498         wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce",
01499                     f.nonce, sizeof(f.nonce));
01500         wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID="
01501                    MACSTR " pairwise=0x%x",
01502                    MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id), pairwise);
01503         wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 pull - PMK-R1",
01504                         f.pmk_r1, PMK_LEN);
01505         wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR1Name",
01506                         f.pmk_r1_name, WPA_PMK_NAME_LEN);
01507 
01508         wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name,
01509                             pairwise);
01510         os_memset(f.pmk_r1, 0, PMK_LEN);
01511 
01512         return 0;
01513 }
01514 
01515 
01516 static int wpa_ft_rrb_rx_push(struct wpa_authenticator *wpa_auth,
01517                               const u8 *src_addr,
01518                               const u8 *data, size_t data_len)
01519 {
01520         struct ft_r0kh_r1kh_push_frame *frame, f;
01521         struct ft_remote_r0kh *r0kh;
01522         struct os_time now;
01523         os_time_t tsend;
01524         int pairwise;
01525 
01526         wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 push");
01527 
01528         if (data_len < sizeof(*frame))
01529                 return -1;
01530 
01531         r0kh = wpa_auth->conf.r0kh_list;
01532         while (r0kh) {
01533                 if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0)
01534                         break;
01535                 r0kh = r0kh->next;
01536         }
01537         if (r0kh == NULL) {
01538                 wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for "
01539                            "PMK-R0 push source address " MACSTR,
01540                            MAC2STR(src_addr));
01541                 return -1;
01542         }
01543 
01544         frame = (struct ft_r0kh_r1kh_push_frame *) data;
01545         /* aes_unwrap() does not support inplace decryption, so use a temporary
01546          * buffer for the data. */
01547         if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8,
01548                        frame->timestamp, f.timestamp) < 0) {
01549                 wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 push from "
01550                            MACSTR, MAC2STR(src_addr));
01551                 return -1;
01552         }
01553 
01554         os_get_time(&now);
01555         tsend = WPA_GET_LE32(f.timestamp);
01556         if ((now.sec > tsend && now.sec - tsend > 60) ||
01557             (now.sec < tsend && tsend - now.sec > 60)) {
01558                 wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not have a valid "
01559                            "timestamp: sender time %d own time %d\n",
01560                            (int) tsend, (int) now.sec);
01561                 return -1;
01562         }
01563 
01564         if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN)
01565             != 0) {
01566                 wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not use a matching "
01567                            "R1KH-ID (received " MACSTR " own " MACSTR ")",
01568                            MAC2STR(f.r1kh_id),
01569                            MAC2STR(wpa_auth->conf.r1_key_holder));
01570                 return -1;
01571         }
01572 
01573         pairwise = le_to_host16(f.pairwise);
01574         wpa_printf(MSG_DEBUG, "FT: PMK-R1 push - R1KH-ID=" MACSTR " S1KH-ID="
01575                    MACSTR " pairwise=0x%x",
01576                    MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id), pairwise);
01577         wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 push - PMK-R1",
01578                         f.pmk_r1, PMK_LEN);
01579         wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 push - PMKR1Name",
01580                         f.pmk_r1_name, WPA_PMK_NAME_LEN);
01581 
01582         wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name,
01583                             pairwise);
01584         os_memset(f.pmk_r1, 0, PMK_LEN);
01585 
01586         return 0;
01587 }
01588 
01589 
01590 int wpa_ft_rrb_rx(struct wpa_authenticator *wpa_auth, const u8 *src_addr,
01591                   const u8 *data, size_t data_len)
01592 {
01593         struct ft_rrb_frame *frame;
01594         u16 alen;
01595         const u8 *pos, *end, *start;
01596         u8 action;
01597         const u8 *sta_addr, *target_ap_addr;
01598 
01599         wpa_printf(MSG_DEBUG, "FT: RRB received frame from remote AP " MACSTR,
01600                    MAC2STR(src_addr));
01601 
01602         if (data_len < sizeof(*frame)) {
01603                 wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (data_len=%lu)",
01604                            (unsigned long) data_len);
01605                 return -1;
01606         }
01607 
01608         pos = data;
01609         frame = (struct ft_rrb_frame *) pos;
01610         pos += sizeof(*frame);
01611 
01612         alen = le_to_host16(frame->action_length);
01613         wpa_printf(MSG_DEBUG, "FT: RRB frame - frame_type=%d packet_type=%d "
01614                    "action_length=%d ap_address=" MACSTR,
01615                    frame->frame_type, frame->packet_type, alen,
01616                    MAC2STR(frame->ap_address));
01617 
01618         if (frame->frame_type != RSN_REMOTE_FRAME_TYPE_FT_RRB) {
01619                 /* Discard frame per IEEE Std 802.11r-2008, 11A.10.3 */
01620                 wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with "
01621                            "unrecognized type %d", frame->frame_type);
01622                 return -1;
01623         }
01624 
01625         if (alen > data_len - sizeof(*frame)) {
01626                 wpa_printf(MSG_DEBUG, "FT: RRB frame too short for action "
01627                            "frame");
01628                 return -1;
01629         }
01630 
01631         if (frame->packet_type == FT_PACKET_R0KH_R1KH_PULL)
01632                 return wpa_ft_rrb_rx_pull(wpa_auth, src_addr, data, data_len);
01633         if (frame->packet_type == FT_PACKET_R0KH_R1KH_RESP)
01634                 return wpa_ft_rrb_rx_resp(wpa_auth, src_addr, data, data_len);
01635         if (frame->packet_type == FT_PACKET_R0KH_R1KH_PUSH)
01636                 return wpa_ft_rrb_rx_push(wpa_auth, src_addr, data, data_len);
01637 
01638         wpa_hexdump(MSG_MSGDUMP, "FT: RRB - FT Action frame", pos, alen);
01639 
01640         if (alen < 1 + 1 + 2 * ETH_ALEN) {
01641                 wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (not enough "
01642                            "room for Action Frame body); alen=%lu",
01643                            (unsigned long) alen);
01644                 return -1;
01645         }
01646         start = pos;
01647         end = pos + alen;
01648 
01649         if (*pos != WLAN_ACTION_FT) {
01650                 wpa_printf(MSG_DEBUG, "FT: Unexpected Action frame category "
01651                            "%d", *pos);
01652                 return -1;
01653         }
01654 
01655         pos++;
01656         action = *pos++;
01657         sta_addr = pos;
01658         pos += ETH_ALEN;
01659         target_ap_addr = pos;
01660         pos += ETH_ALEN;
01661         wpa_printf(MSG_DEBUG, "FT: RRB Action Frame: action=%d sta_addr="
01662                    MACSTR " target_ap_addr=" MACSTR,
01663                    action, MAC2STR(sta_addr), MAC2STR(target_ap_addr));
01664 
01665         if (frame->packet_type == FT_PACKET_REQUEST) {
01666                 wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Request");
01667 
01668                 if (action != 1) {
01669                         wpa_printf(MSG_DEBUG, "FT: Unexpected Action %d in "
01670                                    "RRB Request", action);
01671                         return -1;
01672                 }
01673 
01674                 if (os_memcmp(target_ap_addr, wpa_auth->addr, ETH_ALEN) != 0) {
01675                         wpa_printf(MSG_DEBUG, "FT: Target AP address in the "
01676                                    "RRB Request does not match with own "
01677                                    "address");
01678                         return -1;
01679                 }
01680 
01681                 if (wpa_ft_rrb_rx_request(wpa_auth, frame->ap_address,
01682                                           sta_addr, pos, end - pos) < 0)
01683                         return -1;
01684         } else if (frame->packet_type == FT_PACKET_RESPONSE) {
01685                 u16 status_code;
01686 
01687                 if (end - pos < 2) {
01688                         wpa_printf(MSG_DEBUG, "FT: Not enough room for status "
01689                                    "code in RRB Response");
01690                         return -1;
01691                 }
01692                 status_code = WPA_GET_LE16(pos);
01693                 pos += 2;
01694 
01695                 wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Response "
01696                            "(status_code=%d)", status_code);
01697 
01698                 if (wpa_ft_action_send(wpa_auth, sta_addr, start, alen) < 0)
01699                         return -1;
01700         } else {
01701                 wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with unknown "
01702                            "packet_type %d", frame->packet_type);
01703                 return -1;
01704         }
01705 
01706         return 0;
01707 }
01708 
01709 
01710 static void wpa_ft_generate_pmk_r1(struct wpa_authenticator *wpa_auth,
01711                                    struct wpa_ft_pmk_r0_sa *pmk_r0,
01712                                    struct ft_remote_r1kh *r1kh,
01713                                    const u8 *s1kh_id, int pairwise)
01714 {
01715         struct ft_r0kh_r1kh_push_frame frame, f;
01716         struct os_time now;
01717 
01718         os_memset(&frame, 0, sizeof(frame));
01719         frame.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
01720         frame.packet_type = FT_PACKET_R0KH_R1KH_PUSH;
01721         frame.data_length = host_to_le16(FT_R0KH_R1KH_PUSH_DATA_LEN);
01722         os_memcpy(frame.ap_address, wpa_auth->addr, ETH_ALEN);
01723 
01724         /* aes_wrap() does not support inplace encryption, so use a temporary
01725          * buffer for the data. */
01726         os_memcpy(f.r1kh_id, r1kh->id, FT_R1KH_ID_LEN);
01727         os_memcpy(f.s1kh_id, s1kh_id, ETH_ALEN);
01728         os_memcpy(f.pmk_r0_name, pmk_r0->pmk_r0_name, WPA_PMK_NAME_LEN);
01729         wpa_derive_pmk_r1(pmk_r0->pmk_r0, pmk_r0->pmk_r0_name, r1kh->id,
01730                           s1kh_id, f.pmk_r1, f.pmk_r1_name);
01731         wpa_printf(MSG_DEBUG, "FT: R1KH-ID " MACSTR, MAC2STR(r1kh->id));
01732         wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", f.pmk_r1, PMK_LEN);
01733         wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", f.pmk_r1_name,
01734                     WPA_PMK_NAME_LEN);
01735         os_get_time(&now);
01736         WPA_PUT_LE32(f.timestamp, now.sec);
01737         f.pairwise = pairwise;
01738         if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8,
01739                      f.timestamp, frame.timestamp) < 0)
01740                 return;
01741 
01742         wpa_ft_rrb_send(wpa_auth, r1kh->addr, (u8 *) &frame, sizeof(frame));
01743 }
01744 
01745 
01746 void wpa_ft_push_pmk_r1(struct wpa_authenticator *wpa_auth, const u8 *addr)
01747 {
01748         struct wpa_ft_pmk_r0_sa *r0;
01749         struct ft_remote_r1kh *r1kh;
01750 
01751         if (!wpa_auth->conf.pmk_r1_push)
01752                 return;
01753 
01754         r0 = wpa_auth->ft_pmk_cache->pmk_r0;
01755         while (r0) {
01756                 if (os_memcmp(r0->spa, addr, ETH_ALEN) == 0)
01757                         break;
01758                 r0 = r0->next;
01759         }
01760 
01761         if (r0 == NULL || r0->pmk_r1_pushed)
01762                 return;
01763         r0->pmk_r1_pushed = 1;
01764 
01765         wpa_printf(MSG_DEBUG, "FT: Deriving and pushing PMK-R1 keys to R1KHs "
01766                    "for STA " MACSTR, MAC2STR(addr));
01767 
01768         r1kh = wpa_auth->conf.r1kh_list;
01769         while (r1kh) {
01770                 wpa_ft_generate_pmk_r1(wpa_auth, r0, r1kh, addr, r0->pairwise);
01771                 r1kh = r1kh->next;
01772         }
01773 }
01774 
01775 #endif /* CONFIG_IEEE80211R */


wpa_supplicant
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Apr 24 2014 15:34:36