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