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