]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/wpa/hostapd/wpa_ft.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / contrib / wpa / 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 #ifdef CONFIG_IEEE80211W
375 static inline int wpa_auth_get_seqnum_igtk(struct wpa_authenticator *wpa_auth,
376                                            const u8 *addr, int idx, u8 *seq)
377 {
378         if (wpa_auth->cb.get_seqnum_igtk == NULL)
379                 return -1;
380         return wpa_auth->cb.get_seqnum_igtk(wpa_auth->cb.ctx, addr, idx, seq);
381 }
382 #endif /* CONFIG_IEEE80211W */
383
384
385 static u8 * wpa_ft_gtk_subelem(struct wpa_state_machine *sm, size_t *len)
386 {
387         u8 *subelem;
388         struct wpa_group *gsm = sm->group;
389         size_t subelem_len, pad_len;
390         const u8 *key;
391         size_t key_len;
392         u8 keybuf[32];
393
394         key_len = gsm->GTK_len;
395         if (key_len > sizeof(keybuf))
396                 return NULL;
397
398         /*
399          * Pad key for AES Key Wrap if it is not multiple of 8 bytes or is less
400          * than 16 bytes.
401          */
402         pad_len = key_len % 8;
403         if (pad_len)
404                 pad_len = 8 - pad_len;
405         if (key_len + pad_len < 16)
406                 pad_len += 8;
407         if (pad_len) {
408                 os_memcpy(keybuf, gsm->GTK[gsm->GN - 1], key_len);
409                 os_memset(keybuf + key_len, 0, pad_len);
410                 keybuf[key_len] = 0xdd;
411                 key_len += pad_len;
412                 key = keybuf;
413         } else
414                 key = gsm->GTK[gsm->GN - 1];
415
416         /*
417          * Sub-elem ID[1] | Length[1] | Key Info[1] | Key Length[1] | RSC[8] |
418          * Key[5..32].
419          */
420         subelem_len = 12 + key_len + 8;
421         subelem = os_zalloc(subelem_len);
422         if (subelem == NULL)
423                 return NULL;
424
425         subelem[0] = FTIE_SUBELEM_GTK;
426         subelem[1] = 10 + key_len + 8;
427         subelem[2] = gsm->GN & 0x03; /* Key ID in B0-B1 of Key Info */
428         subelem[3] = gsm->GTK_len;
429         wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, subelem + 4);
430         if (aes_wrap(sm->PTK.kek, key_len / 8, key, subelem + 12)) {
431                 os_free(subelem);
432                 return NULL;
433         }
434
435         *len = subelem_len;
436         return subelem;
437 }
438
439
440 #ifdef CONFIG_IEEE80211W
441 static u8 * wpa_ft_igtk_subelem(struct wpa_state_machine *sm, size_t *len)
442 {
443         u8 *subelem, *pos;
444         struct wpa_group *gsm = sm->group;
445         size_t subelem_len;
446
447         /* Sub-elem ID[1] | Length[1] | KeyID[2] | IPN[6] | Key Length[1] |
448          * Key[16+8] */
449         subelem_len = 1 + 1 + 2 + 6 + 1 + WPA_IGTK_LEN + 8;
450         subelem = os_zalloc(subelem_len);
451         if (subelem == NULL)
452                 return NULL;
453
454         pos = subelem;
455         *pos++ = FTIE_SUBELEM_IGTK;
456         *pos++ = subelem_len - 2;
457         WPA_PUT_LE16(pos, gsm->GN_igtk);
458         pos += 2;
459         wpa_auth_get_seqnum_igtk(sm->wpa_auth, NULL, gsm->GN_igtk, pos);
460         pos += 6;
461         *pos++ = WPA_IGTK_LEN;
462         if (aes_wrap(sm->PTK.kek, WPA_IGTK_LEN / 8,
463                      gsm->IGTK[gsm->GN_igtk - 4], pos)) {
464                 os_free(subelem);
465                 return NULL;
466         }
467
468         *len = subelem_len;
469         return subelem;
470 }
471 #endif /* CONFIG_IEEE80211W */
472
473
474 u8 * wpa_sm_write_assoc_resp_ies(struct wpa_state_machine *sm, u8 *pos,
475                                  size_t max_len, int auth_alg)
476 {
477         u8 *end, *mdie, *ftie, *rsnie, *r0kh_id, *subelem = NULL;
478         size_t mdie_len, ftie_len, rsnie_len, r0kh_id_len, subelem_len = 0;
479         int res;
480         struct wpa_auth_config *conf;
481         struct rsn_ftie *_ftie;
482
483         if (sm == NULL)
484                 return pos;
485
486         conf = &sm->wpa_auth->conf;
487
488         if (sm->wpa_key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
489             sm->wpa_key_mgmt != WPA_KEY_MGMT_FT_PSK)
490                 return pos;
491
492         end = pos + max_len;
493
494         /* RSN */
495         res = wpa_write_rsn_ie(conf, pos, end - pos, sm->pmk_r1_name);
496         if (res < 0)
497                 return pos;
498         rsnie = pos;
499         rsnie_len = res;
500         pos += res;
501
502         /* Mobility Domain Information */
503         res = wpa_write_mdie(conf, pos, end - pos);
504         if (res < 0)
505                 return pos;
506         mdie = pos;
507         mdie_len = res;
508         pos += res;
509
510         /* Fast BSS Transition Information */
511         if (auth_alg == WLAN_AUTH_FT) {
512                 subelem = wpa_ft_gtk_subelem(sm, &subelem_len);
513                 r0kh_id = sm->r0kh_id;
514                 r0kh_id_len = sm->r0kh_id_len;
515 #ifdef CONFIG_IEEE80211W
516                 if (sm->mgmt_frame_prot) {
517                         u8 *igtk;
518                         size_t igtk_len;
519                         u8 *nbuf;
520                         igtk = wpa_ft_igtk_subelem(sm, &igtk_len);
521                         if (igtk == NULL) {
522                                 os_free(subelem);
523                                 return pos;
524                         }
525                         nbuf = os_realloc(subelem, subelem_len + igtk_len);
526                         if (nbuf == NULL) {
527                                 os_free(subelem);
528                                 os_free(igtk);
529                                 return pos;
530                         }
531                         subelem = nbuf;
532                         os_memcpy(subelem + subelem_len, igtk, igtk_len);
533                         subelem_len += igtk_len;
534                         os_free(igtk);
535                 }
536 #endif /* CONFIG_IEEE80211W */
537         } else {
538                 r0kh_id = conf->r0_key_holder;
539                 r0kh_id_len = conf->r0_key_holder_len;
540         }
541         res = wpa_write_ftie(conf, r0kh_id, r0kh_id_len, NULL, NULL, pos,
542                              end - pos, subelem, subelem_len);
543         os_free(subelem);
544         if (res < 0)
545                 return pos;
546         ftie = pos;
547         ftie_len = res;
548         pos += res;
549
550         _ftie = (struct rsn_ftie *) (ftie + 2);
551         _ftie->mic_control[1] = 3; /* Information element count */
552         if (wpa_ft_mic(sm->PTK.kck, sm->addr, sm->wpa_auth->addr, 6,
553                        mdie, mdie_len, ftie, ftie_len,
554                        rsnie, rsnie_len, NULL, 0, _ftie->mic) < 0)
555                 wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
556
557         return pos;
558 }
559
560
561 struct wpa_ft_ies {
562         const u8 *mdie;
563         size_t mdie_len;
564         const u8 *ftie;
565         size_t ftie_len;
566         const u8 *r1kh_id;
567         const u8 *gtk;
568         size_t gtk_len;
569         const u8 *r0kh_id;
570         size_t r0kh_id_len;
571         const u8 *rsn;
572         size_t rsn_len;
573         const u8 *rsn_pmkid;
574 };
575
576
577 static int wpa_ft_parse_ftie(const u8 *ie, size_t ie_len,
578                              struct wpa_ft_ies *parse)
579 {
580         const u8 *end, *pos;
581
582         parse->ftie = ie;
583         parse->ftie_len = ie_len;
584
585         pos = ie + sizeof(struct rsn_ftie);
586         end = ie + ie_len;
587
588         while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
589                 switch (pos[0]) {
590                 case FTIE_SUBELEM_R1KH_ID:
591                         if (pos[1] != FT_R1KH_ID_LEN) {
592                                 wpa_printf(MSG_DEBUG, "FT: Invalid R1KH-ID "
593                                            "length in FTIE: %d", pos[1]);
594                                 return -1;
595                         }
596                         parse->r1kh_id = pos + 2;
597                         break;
598                 case FTIE_SUBELEM_GTK:
599                         parse->gtk = pos + 2;
600                         parse->gtk_len = pos[1];
601                         break;
602                 case FTIE_SUBELEM_R0KH_ID:
603                         if (pos[1] < 1 || pos[1] > FT_R0KH_ID_MAX_LEN) {
604                                 wpa_printf(MSG_DEBUG, "FT: Invalid R0KH-ID "
605                                            "length in FTIE: %d", pos[1]);
606                                 return -1;
607                         }
608                         parse->r0kh_id = pos + 2;
609                         parse->r0kh_id_len = pos[1];
610                         break;
611                 }
612
613                 pos += 2 + pos[1];
614         }
615
616         return 0;
617 }
618
619
620 static int wpa_ft_parse_ies(const u8 *ies, size_t ies_len,
621                             struct wpa_ft_ies *parse)
622 {
623         const u8 *end, *pos;
624         struct wpa_ie_data data;
625         int ret;
626
627         os_memset(parse, 0, sizeof(*parse));
628         if (ies == NULL)
629                 return 0;
630
631         pos = ies;
632         end = ies + ies_len;
633         while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
634                 switch (pos[0]) {
635                 case WLAN_EID_RSN:
636                         parse->rsn = pos + 2;
637                         parse->rsn_len = pos[1];
638                         ret = wpa_parse_wpa_ie_rsn(parse->rsn - 2,
639                                                    parse->rsn_len + 2,
640                                                    &data);
641                         if (ret < 0) {
642                                 wpa_printf(MSG_DEBUG, "FT: Failed to parse "
643                                            "RSN IE: %d", ret);
644                                 return -1;
645                         }
646                         if (data.num_pmkid == 1 && data.pmkid)
647                                 parse->rsn_pmkid = data.pmkid;
648                         break;
649                 case WLAN_EID_MOBILITY_DOMAIN:
650                         parse->mdie = pos + 2;
651                         parse->mdie_len = pos[1];
652                         break;
653                 case WLAN_EID_FAST_BSS_TRANSITION:
654                         if (wpa_ft_parse_ftie(pos + 2, pos[1], parse) < 0)
655                                 return -1;
656                         break;
657                 }
658
659                 pos += 2 + pos[1];
660         }
661
662         return 0;
663 }
664
665
666 static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
667                                    int vlan_id,
668                                    const char *alg, const u8 *addr, int idx,
669                                    u8 *key, size_t key_len)
670 {
671         if (wpa_auth->cb.set_key == NULL)
672                 return -1;
673         return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
674                                     key, key_len);
675 }
676
677
678 static void wpa_ft_install_ptk(struct wpa_state_machine *sm)
679 {
680         char *alg;
681         int klen;
682
683         /* MLME-SETKEYS.request(PTK) */
684         if (sm->pairwise == WPA_CIPHER_TKIP) {
685                 alg = "TKIP";
686                 klen = 32;
687         } else if (sm->pairwise == WPA_CIPHER_CCMP) {
688                 alg = "CCMP";
689                 klen = 16;
690         } else
691                 return;
692
693         /* FIX: add STA entry to kernel/driver here? The set_key will fail
694          * most likely without this.. At the moment, STA entry is added only
695          * after association has been completed. Alternatively, could
696          * re-configure PTK at that point(?).
697          */
698         if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
699                              sm->PTK.tk1, klen))
700                 return;
701
702         /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
703         sm->pairwise_set = TRUE;
704 }
705
706
707 static u16 wpa_ft_process_auth_req(struct wpa_state_machine *sm,
708                                    const u8 *ies, size_t ies_len,
709                                    u8 **resp_ies, size_t *resp_ies_len)
710 {
711         struct rsn_mdie *mdie;
712         struct rsn_ftie *ftie;
713         u8 pmk_r1[PMK_LEN], pmk_r1_name[WPA_PMK_NAME_LEN];
714         u8 ptk_name[WPA_PMK_NAME_LEN];
715         struct wpa_auth_config *conf;
716         struct wpa_ft_ies parse;
717         size_t buflen;
718         int ret;
719         u8 *pos, *end;
720
721         *resp_ies = NULL;
722         *resp_ies_len = 0;
723
724         sm->pmk_r1_name_valid = 0;
725         conf = &sm->wpa_auth->conf;
726
727         wpa_hexdump(MSG_DEBUG, "FT: Received authentication frame IEs",
728                     ies, ies_len);
729
730         if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
731                 wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
732                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
733         }
734
735         mdie = (struct rsn_mdie *) parse.mdie;
736         if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
737             os_memcmp(mdie->mobility_domain,
738                       sm->wpa_auth->conf.mobility_domain,
739                       MOBILITY_DOMAIN_ID_LEN) != 0) {
740                 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
741                 return WLAN_STATUS_INVALID_MDIE;
742         }
743
744         ftie = (struct rsn_ftie *) parse.ftie;
745         if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
746                 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
747                 return WLAN_STATUS_INVALID_FTIE;
748         }
749
750         os_memcpy(sm->SNonce, ftie->snonce, WPA_NONCE_LEN);
751
752         if (parse.r0kh_id == NULL) {
753                 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE - no R0KH-ID");
754                 return WLAN_STATUS_INVALID_FTIE;
755         }
756
757         wpa_hexdump(MSG_DEBUG, "FT: STA R0KH-ID",
758                     parse.r0kh_id, parse.r0kh_id_len);
759         os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
760         sm->r0kh_id_len = parse.r0kh_id_len;
761
762         if (parse.rsn_pmkid == NULL) {
763                 wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
764                 return WLAN_STATUS_INVALID_PMKID;
765         }
766
767         wpa_hexdump(MSG_DEBUG, "FT: Requested PMKR0Name",
768                     parse.rsn_pmkid, WPA_PMK_NAME_LEN);
769         wpa_derive_pmk_r1_name(parse.rsn_pmkid,
770                                sm->wpa_auth->conf.r1_key_holder, sm->addr,
771                                pmk_r1_name);
772         wpa_hexdump(MSG_DEBUG, "FT: Derived requested PMKR1Name",
773                     pmk_r1_name, WPA_PMK_NAME_LEN);
774
775         if (wpa_ft_fetch_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1_name, pmk_r1) <
776             0) {
777                 if (wpa_ft_pull_pmk_r1(sm->wpa_auth, sm->addr, sm->r0kh_id,
778                                        sm->r0kh_id_len, parse.rsn_pmkid) < 0) {
779                         wpa_printf(MSG_DEBUG, "FT: Did not have matching "
780                                    "PMK-R1 and unknown R0KH-ID");
781                         return WLAN_STATUS_INVALID_PMKID;
782                 }
783
784                 /*
785                  * TODO: Should return "status pending" (and the caller should
786                  * not send out response now). The real response will be sent
787                  * once the response from R0KH is received.
788                  */
789                 return WLAN_STATUS_INVALID_PMKID;
790         }
791
792         wpa_hexdump_key(MSG_DEBUG, "FT: Selected PMK-R1", pmk_r1, PMK_LEN);
793         sm->pmk_r1_name_valid = 1;
794         os_memcpy(sm->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN);
795
796         if (os_get_random(sm->ANonce, WPA_NONCE_LEN)) {
797                 wpa_printf(MSG_DEBUG, "FT: Failed to get random data for "
798                            "ANonce");
799                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
800         }
801
802         wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
803                     sm->SNonce, WPA_NONCE_LEN);
804         wpa_hexdump(MSG_DEBUG, "FT: Generated ANonce",
805                     sm->ANonce, WPA_NONCE_LEN);
806
807         wpa_pmk_r1_to_ptk(pmk_r1, sm->SNonce, sm->ANonce, sm->addr,
808                           sm->wpa_auth->addr, pmk_r1_name,
809                           (u8 *) &sm->PTK, sizeof(sm->PTK), ptk_name);
810         wpa_hexdump_key(MSG_DEBUG, "FT: PTK",
811                         (u8 *) &sm->PTK, sizeof(sm->PTK));
812         wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
813
814         wpa_ft_install_ptk(sm);
815
816         buflen = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) +
817                 2 + FT_R1KH_ID_LEN + 200;
818         *resp_ies = os_zalloc(buflen);
819         if (*resp_ies == NULL) {
820                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
821         }
822
823         pos = *resp_ies;
824         end = *resp_ies + buflen;
825
826         ret = wpa_write_rsn_ie(conf, pos, end - pos, parse.rsn_pmkid);
827         if (ret < 0) {
828                 os_free(*resp_ies);
829                 *resp_ies = NULL;
830                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
831         }
832         pos += ret;
833
834         ret = wpa_write_mdie(conf, pos, end - pos);
835         if (ret < 0) {
836                 os_free(*resp_ies);
837                 *resp_ies = NULL;
838                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
839         }
840         pos += ret;
841
842         ret = wpa_write_ftie(conf, parse.r0kh_id, parse.r0kh_id_len,
843                              sm->ANonce, sm->SNonce, pos, end - pos, NULL, 0);
844         if (ret < 0) {
845                 os_free(*resp_ies);
846                 *resp_ies = NULL;
847                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
848         }
849         pos += ret;
850
851         *resp_ies_len = pos - *resp_ies;
852
853         return WLAN_STATUS_SUCCESS;
854 }
855
856
857 void wpa_ft_process_auth(struct wpa_state_machine *sm, const u8 *bssid,
858                          u16 auth_transaction, const u8 *ies, size_t ies_len,
859                          void (*cb)(void *ctx, const u8 *dst, const u8 *bssid,
860                                     u16 auth_transaction, u16 status,
861                                     const u8 *ies, size_t ies_len),
862                          void *ctx)
863 {
864         u16 status;
865         u8 *resp_ies;
866         size_t resp_ies_len;
867
868         if (sm == NULL) {
869                 wpa_printf(MSG_DEBUG, "FT: Received authentication frame, but "
870                            "WPA SM not available");
871                 return;
872         }
873
874         wpa_printf(MSG_DEBUG, "FT: Received authentication frame: STA=" MACSTR
875                    " BSSID=" MACSTR " transaction=%d",
876                    MAC2STR(sm->addr), MAC2STR(bssid), auth_transaction);
877         status = wpa_ft_process_auth_req(sm, ies, ies_len, &resp_ies,
878                                          &resp_ies_len);
879
880         wpa_printf(MSG_DEBUG, "FT: FT authentication response: dst=" MACSTR
881                    " auth_transaction=%d status=%d",
882                    MAC2STR(sm->addr), auth_transaction + 1, status);
883         wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
884         cb(ctx, sm->addr, bssid, auth_transaction + 1, status,
885            resp_ies, resp_ies_len);
886         os_free(resp_ies);
887 }
888
889
890 u16 wpa_ft_validate_reassoc(struct wpa_state_machine *sm, const u8 *ies,
891                             size_t ies_len)
892 {
893         struct wpa_ft_ies parse;
894         struct rsn_mdie *mdie;
895         struct rsn_ftie *ftie;
896         u8 mic[16];
897
898         if (sm == NULL)
899                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
900
901         wpa_hexdump(MSG_DEBUG, "FT: Reassoc Req IEs", ies, ies_len);
902
903         if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
904                 wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
905                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
906         }
907
908         if (parse.rsn == NULL) {
909                 wpa_printf(MSG_DEBUG, "FT: No RSNIE in Reassoc Req");
910                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
911         }
912
913         if (parse.rsn_pmkid == NULL) {
914                 wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
915                 return WLAN_STATUS_INVALID_PMKID;
916         }
917
918         if (os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
919         {
920                 wpa_printf(MSG_DEBUG, "FT: PMKID in Reassoc Req did not match "
921                            "with the PMKR1Name derived from auth request");
922                 return WLAN_STATUS_INVALID_PMKID;
923         }
924
925         mdie = (struct rsn_mdie *) parse.mdie;
926         if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
927             os_memcmp(mdie->mobility_domain,
928                       sm->wpa_auth->conf.mobility_domain,
929                       MOBILITY_DOMAIN_ID_LEN) != 0) {
930                 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
931                 return WLAN_STATUS_INVALID_MDIE;
932         }
933
934         ftie = (struct rsn_ftie *) parse.ftie;
935         if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
936                 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
937                 return WLAN_STATUS_INVALID_FTIE;
938         }
939
940         /*
941          * Assume that MDIE, FTIE, and RSN IE are protected and that there is
942          * no RIC, so total of 3 protected IEs.
943          */
944         if (ftie->mic_control[1] != 3) {
945                 wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in FTIE (%d)",
946                            ftie->mic_control[1]);
947                 return WLAN_STATUS_INVALID_FTIE;
948         }
949
950         if (wpa_ft_mic(sm->PTK.kck, sm->addr, sm->wpa_auth->addr, 5,
951                        parse.mdie - 2, parse.mdie_len + 2,
952                        parse.ftie - 2, parse.ftie_len + 2,
953                        parse.rsn - 2, parse.rsn_len + 2, NULL, 0,
954                        mic) < 0) {
955                 wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
956                 return WLAN_STATUS_UNSPECIFIED_FAILURE;
957         }
958
959         if (os_memcmp(mic, ftie->mic, 16) != 0) {
960                 wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
961                 wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", ftie->mic, 16);
962                 wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16);
963                 return WLAN_STATUS_INVALID_FTIE;
964         }
965
966         return WLAN_STATUS_SUCCESS;
967 }
968
969
970 int wpa_ft_action_rx(struct wpa_state_machine *sm, const u8 *data, size_t len)
971 {
972         const u8 *sta_addr, *target_ap;
973         const u8 *ies;
974         size_t ies_len;
975         u8 action;
976         struct ft_rrb_frame *frame;
977
978         if (sm == NULL)
979                 return -1;
980
981         /*
982          * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
983          * FT Request action frame body[variable]
984          */
985
986         if (len < 14) {
987                 wpa_printf(MSG_DEBUG, "FT: Too short FT Action frame "
988                            "(len=%lu)", (unsigned long) len);
989                 return -1;
990         }
991
992         action = data[1];
993         sta_addr = data + 2;
994         target_ap = data + 8;
995         ies = data + 14;
996         ies_len = len - 14;
997
998         wpa_printf(MSG_DEBUG, "FT: Received FT Action frame (STA=" MACSTR
999                    " Target AP=" MACSTR " Action=%d)",
1000                    MAC2STR(sta_addr), MAC2STR(target_ap), action);
1001
1002         if (os_memcmp(sta_addr, sm->addr, ETH_ALEN) != 0) {
1003                 wpa_printf(MSG_DEBUG, "FT: Mismatch in FT Action STA address: "
1004                            "STA=" MACSTR " STA-Address=" MACSTR,
1005                            MAC2STR(sm->addr), MAC2STR(sta_addr));
1006                 return -1;
1007         }
1008
1009         /*
1010          * Do some sanity checking on the target AP address (not own and not
1011          * broadcast. This could be extended to filter based on a list of known
1012          * APs in the MD (if such a list were configured).
1013          */
1014         if ((target_ap[0] & 0x01) ||
1015             os_memcmp(target_ap, sm->wpa_auth->addr, ETH_ALEN) == 0) {
1016                 wpa_printf(MSG_DEBUG, "FT: Invalid Target AP in FT Action "
1017                            "frame");
1018                 return -1;
1019         }
1020
1021         wpa_hexdump(MSG_MSGDUMP, "FT: Action frame body", ies, ies_len);
1022
1023         /* RRB - Forward action frame to the target AP */
1024         frame = os_malloc(sizeof(*frame) + len);
1025         frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
1026         frame->packet_type = FT_PACKET_REQUEST;
1027         frame->action_length = host_to_le16(len);
1028         os_memcpy(frame->ap_address, sm->wpa_auth->addr, ETH_ALEN);
1029         os_memcpy(frame + 1, data, len);
1030
1031         wpa_ft_rrb_send(sm->wpa_auth, target_ap, (u8 *) frame,
1032                         sizeof(*frame) + len);
1033         os_free(frame);
1034
1035         return 0;
1036 }
1037
1038
1039 static int wpa_ft_rrb_rx_request(struct wpa_authenticator *wpa_auth,
1040                                  const u8 *current_ap, const u8 *sta_addr,
1041                                  const u8 *body, size_t len)
1042 {
1043         struct wpa_state_machine *sm;
1044         u16 status;
1045         u8 *resp_ies, *pos;
1046         size_t resp_ies_len, rlen;
1047         struct ft_rrb_frame *frame;
1048
1049         sm = wpa_ft_add_sta(wpa_auth, sta_addr);
1050         if (sm == NULL) {
1051                 wpa_printf(MSG_DEBUG, "FT: Failed to add new STA based on "
1052                            "RRB Request");
1053                 return -1;
1054         }
1055
1056         wpa_hexdump(MSG_MSGDUMP, "FT: RRB Request Frame body", body, len);
1057
1058         status = wpa_ft_process_auth_req(sm, body, len, &resp_ies,
1059                                          &resp_ies_len);
1060
1061         wpa_printf(MSG_DEBUG, "FT: RRB authentication response: STA=" MACSTR
1062                    " CurrentAP=" MACSTR " status=%d",
1063                    MAC2STR(sm->addr), MAC2STR(current_ap), status);
1064         wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
1065
1066         /* RRB - Forward action frame response to the Current AP */
1067
1068         /*
1069          * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
1070          * Status_Code[2] FT Request action frame body[variable]
1071          */
1072         rlen = 2 + 2 * ETH_ALEN + 2 + resp_ies_len;
1073
1074         frame = os_malloc(sizeof(*frame) + rlen);
1075         frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
1076         frame->packet_type = FT_PACKET_RESPONSE;
1077         frame->action_length = host_to_le16(rlen);
1078         os_memcpy(frame->ap_address, wpa_auth->addr, ETH_ALEN);
1079         pos = (u8 *) (frame + 1);
1080         *pos++ = WLAN_ACTION_FT;
1081         *pos++ = 2; /* Action: Response */
1082         os_memcpy(pos, sta_addr, ETH_ALEN);
1083         pos += ETH_ALEN;
1084         os_memcpy(pos, wpa_auth->addr, ETH_ALEN);
1085         pos += ETH_ALEN;
1086         WPA_PUT_LE16(pos, status);
1087         pos += 2;
1088         if (resp_ies) {
1089                 os_memcpy(pos, resp_ies, resp_ies_len);
1090                 os_free(resp_ies);
1091         }
1092
1093         wpa_ft_rrb_send(wpa_auth, current_ap, (u8 *) frame,
1094                         sizeof(*frame) + rlen);
1095         os_free(frame);
1096
1097         return 0;
1098 }
1099
1100
1101 static int wpa_ft_rrb_rx_pull(struct wpa_authenticator *wpa_auth,
1102                               const u8 *src_addr,
1103                               const u8 *data, size_t data_len)
1104 {
1105         struct ft_r0kh_r1kh_pull_frame *frame, f;
1106         struct ft_remote_r1kh *r1kh;
1107         struct ft_r0kh_r1kh_resp_frame resp, r;
1108         u8 pmk_r0[PMK_LEN];
1109
1110         wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull");
1111
1112         if (data_len < sizeof(*frame))
1113                 return -1;
1114
1115         r1kh = wpa_auth->conf.r1kh_list;
1116         while (r1kh) {
1117                 if (os_memcmp(r1kh->addr, src_addr, ETH_ALEN) == 0)
1118                         break;
1119                 r1kh = r1kh->next;
1120         }
1121         if (r1kh == NULL) {
1122                 wpa_printf(MSG_DEBUG, "FT: No matching R1KH address found for "
1123                            "PMK-R1 pull source address " MACSTR,
1124                            MAC2STR(src_addr));
1125                 return -1;
1126         }
1127
1128         frame = (struct ft_r0kh_r1kh_pull_frame *) data;
1129         /* aes_unwrap() does not support inplace decryption, so use a temporary
1130          * buffer for the data. */
1131         if (aes_unwrap(r1kh->key, (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8,
1132                        frame->nonce, f.nonce) < 0) {
1133                 wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull "
1134                            "request from " MACSTR, MAC2STR(src_addr));
1135                 return -1;
1136         }
1137
1138         wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce",
1139                     f.nonce, sizeof(f.nonce));
1140         wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR0Name",
1141                     f.pmk_r0_name, WPA_PMK_NAME_LEN);
1142         wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID="
1143                    MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));
1144
1145         os_memset(&resp, 0, sizeof(resp));
1146         resp.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
1147         resp.packet_type = FT_PACKET_R0KH_R1KH_RESP;
1148         resp.data_length = host_to_le16(FT_R0KH_R1KH_RESP_DATA_LEN);
1149         os_memcpy(resp.ap_address, wpa_auth->addr, ETH_ALEN);
1150
1151         /* aes_wrap() does not support inplace encryption, so use a temporary
1152          * buffer for the data. */
1153         os_memcpy(r.nonce, f.nonce, sizeof(f.nonce));
1154         os_memcpy(r.r1kh_id, f.r1kh_id, FT_R1KH_ID_LEN);
1155         os_memcpy(r.s1kh_id, f.s1kh_id, ETH_ALEN);
1156         if (wpa_ft_fetch_pmk_r0(wpa_auth, f.s1kh_id, f.pmk_r0_name, pmk_r0) <
1157             0) {
1158                 wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name found for "
1159                            "PMK-R1 pull");
1160                 return -1;
1161         }
1162
1163         wpa_derive_pmk_r1(pmk_r0, f.pmk_r0_name, f.r1kh_id, f.s1kh_id,
1164                           r.pmk_r1, r.pmk_r1_name);
1165         wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", r.pmk_r1, PMK_LEN);
1166         wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", r.pmk_r1_name,
1167                     WPA_PMK_NAME_LEN);
1168
1169         if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8,
1170                      r.nonce, resp.nonce) < 0) {
1171                 os_memset(pmk_r0, 0, PMK_LEN);
1172                 return -1;
1173         }
1174
1175         os_memset(pmk_r0, 0, PMK_LEN);
1176
1177         wpa_ft_rrb_send(wpa_auth, src_addr, (u8 *) &resp, sizeof(resp));
1178
1179         return 0;
1180 }
1181
1182
1183 static int wpa_ft_rrb_rx_resp(struct wpa_authenticator *wpa_auth,
1184                               const u8 *src_addr,
1185                               const u8 *data, size_t data_len)
1186 {
1187         struct ft_r0kh_r1kh_resp_frame *frame, f;
1188         struct ft_remote_r0kh *r0kh;
1189
1190         wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull response");
1191
1192         if (data_len < sizeof(*frame))
1193                 return -1;
1194
1195         r0kh = wpa_auth->conf.r0kh_list;
1196         while (r0kh) {
1197                 if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0)
1198                         break;
1199                 r0kh = r0kh->next;
1200         }
1201         if (r0kh == NULL) {
1202                 wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for "
1203                            "PMK-R0 pull response source address " MACSTR,
1204                            MAC2STR(src_addr));
1205                 return -1;
1206         }
1207
1208         frame = (struct ft_r0kh_r1kh_resp_frame *) data;
1209         /* aes_unwrap() does not support inplace decryption, so use a temporary
1210          * buffer for the data. */
1211         if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8,
1212                        frame->nonce, f.nonce) < 0) {
1213                 wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull "
1214                            "response from " MACSTR, MAC2STR(src_addr));
1215                 return -1;
1216         }
1217
1218         if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN)
1219             != 0) {
1220                 wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull response did not use a "
1221                            "matching R1KH-ID");
1222                 return -1;
1223         }
1224
1225         /* TODO: verify that <nonce,s1kh_id> matches with a pending request
1226          * and call this requests callback function to finish request
1227          * processing */
1228
1229         wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce",
1230                     f.nonce, sizeof(f.nonce));
1231         wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID="
1232                    MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));
1233         wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 pull - PMK-R1",
1234                         f.pmk_r1, PMK_LEN);
1235         wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR1Name",
1236                         f.pmk_r1_name, WPA_PMK_NAME_LEN);
1237
1238         wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name);
1239         os_memset(f.pmk_r1, 0, PMK_LEN);
1240
1241         return 0;
1242 }
1243
1244
1245 static int wpa_ft_rrb_rx_push(struct wpa_authenticator *wpa_auth,
1246                               const u8 *src_addr,
1247                               const u8 *data, size_t data_len)
1248 {
1249         struct ft_r0kh_r1kh_push_frame *frame, f;
1250         struct ft_remote_r0kh *r0kh;
1251         struct os_time now;
1252         os_time_t tsend;
1253
1254         wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 push");
1255
1256         if (data_len < sizeof(*frame))
1257                 return -1;
1258
1259         r0kh = wpa_auth->conf.r0kh_list;
1260         while (r0kh) {
1261                 if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0)
1262                         break;
1263                 r0kh = r0kh->next;
1264         }
1265         if (r0kh == NULL) {
1266                 wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for "
1267                            "PMK-R0 push source address " MACSTR,
1268                            MAC2STR(src_addr));
1269                 return -1;
1270         }
1271
1272         frame = (struct ft_r0kh_r1kh_push_frame *) data;
1273         /* aes_unwrap() does not support inplace decryption, so use a temporary
1274          * buffer for the data. */
1275         if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8,
1276                        frame->timestamp, f.timestamp) < 0) {
1277                 wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 push from "
1278                            MACSTR, MAC2STR(src_addr));
1279                 return -1;
1280         }
1281
1282         os_get_time(&now);
1283         tsend = WPA_GET_LE32(f.timestamp);
1284         if ((now.sec > tsend && now.sec - tsend > 60) ||
1285             (now.sec < tsend && tsend - now.sec > 60)) {
1286                 wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not have a valid "
1287                            "timestamp: sender time %d own time %d\n",
1288                            (int) tsend, (int) now.sec);
1289                 return -1;
1290         }
1291
1292         if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN)
1293             != 0) {
1294                 wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not use a matching "
1295                            "R1KH-ID (received " MACSTR " own " MACSTR ")",
1296                            MAC2STR(f.r1kh_id),
1297                            MAC2STR(wpa_auth->conf.r1_key_holder));
1298                 return -1;
1299         }
1300
1301         wpa_printf(MSG_DEBUG, "FT: PMK-R1 push - R1KH-ID=" MACSTR " S1KH-ID="
1302                    MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));
1303         wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 push - PMK-R1",
1304                         f.pmk_r1, PMK_LEN);
1305         wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 push - PMKR1Name",
1306                         f.pmk_r1_name, WPA_PMK_NAME_LEN);
1307
1308         wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name);
1309         os_memset(f.pmk_r1, 0, PMK_LEN);
1310
1311         return 0;
1312 }
1313
1314
1315 int wpa_ft_rrb_rx(struct wpa_authenticator *wpa_auth, const u8 *src_addr,
1316                   const u8 *data, size_t data_len)
1317 {
1318         struct ft_rrb_frame *frame;
1319         u16 alen;
1320         const u8 *pos, *end, *start;
1321         u8 action;
1322         const u8 *sta_addr, *target_ap_addr;
1323
1324         wpa_printf(MSG_DEBUG, "FT: RRB received frame from remote AP " MACSTR,
1325                    MAC2STR(src_addr));
1326
1327         if (data_len < sizeof(*frame)) {
1328                 wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (data_len=%lu)",
1329                            (unsigned long) data_len);
1330                 return -1;
1331         }
1332
1333         pos = data;
1334         frame = (struct ft_rrb_frame *) pos;
1335         pos += sizeof(*frame);
1336
1337         alen = le_to_host16(frame->action_length);
1338         wpa_printf(MSG_DEBUG, "FT: RRB frame - frame_type=%d packet_type=%d "
1339                    "action_length=%d ap_address=" MACSTR,
1340                    frame->frame_type, frame->packet_type, alen,
1341                    MAC2STR(frame->ap_address));
1342
1343         if (frame->frame_type != RSN_REMOTE_FRAME_TYPE_FT_RRB) {
1344                 /* Discard frame per IEEE Std 802.11r-2008, 11A.10.3 */
1345                 wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with "
1346                            "unrecognized type %d", frame->frame_type);
1347                 return -1;
1348         }
1349
1350         if (alen > data_len - sizeof(*frame)) {
1351                 wpa_printf(MSG_DEBUG, "FT: RRB frame too short for action "
1352                            "frame");
1353                 return -1;
1354         }
1355
1356         if (frame->packet_type == FT_PACKET_R0KH_R1KH_PULL)
1357                 return wpa_ft_rrb_rx_pull(wpa_auth, src_addr, data, data_len);
1358         if (frame->packet_type == FT_PACKET_R0KH_R1KH_RESP)
1359                 return wpa_ft_rrb_rx_resp(wpa_auth, src_addr, data, data_len);
1360         if (frame->packet_type == FT_PACKET_R0KH_R1KH_PUSH)
1361                 return wpa_ft_rrb_rx_push(wpa_auth, src_addr, data, data_len);
1362
1363         wpa_hexdump(MSG_MSGDUMP, "FT: RRB - FT Action frame", pos, alen);
1364
1365         if (alen < 1 + 1 + 2 * ETH_ALEN) {
1366                 wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (not enough "
1367                            "room for Action Frame body); alen=%lu",
1368                            (unsigned long) alen);
1369                 return -1;
1370         }
1371         start = pos;
1372         end = pos + alen;
1373
1374         if (*pos != WLAN_ACTION_FT) {
1375                 wpa_printf(MSG_DEBUG, "FT: Unexpected Action frame category "
1376                            "%d", *pos);
1377                 return -1;
1378         }
1379
1380         pos++;
1381         action = *pos++;
1382         sta_addr = pos;
1383         pos += ETH_ALEN;
1384         target_ap_addr = pos;
1385         pos += ETH_ALEN;
1386         wpa_printf(MSG_DEBUG, "FT: RRB Action Frame: action=%d sta_addr="
1387                    MACSTR " target_ap_addr=" MACSTR,
1388                    action, MAC2STR(sta_addr), MAC2STR(target_ap_addr));
1389
1390         if (frame->packet_type == FT_PACKET_REQUEST) {
1391                 wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Request");
1392
1393                 if (action != 1) {
1394                         wpa_printf(MSG_DEBUG, "FT: Unexpected Action %d in "
1395                                    "RRB Request", action);
1396                         return -1;
1397                 }
1398
1399                 if (os_memcmp(target_ap_addr, wpa_auth->addr, ETH_ALEN) != 0) {
1400                         wpa_printf(MSG_DEBUG, "FT: Target AP address in the "
1401                                    "RRB Request does not match with own "
1402                                    "address");
1403                         return -1;
1404                 }
1405
1406                 if (wpa_ft_rrb_rx_request(wpa_auth, frame->ap_address,
1407                                           sta_addr, pos, end - pos) < 0)
1408                         return -1;
1409         } else if (frame->packet_type == FT_PACKET_RESPONSE) {
1410                 u16 status_code;
1411
1412                 if (end - pos < 2) {
1413                         wpa_printf(MSG_DEBUG, "FT: Not enough room for status "
1414                                    "code in RRB Response");
1415                         return -1;
1416                 }
1417                 status_code = WPA_GET_LE16(pos);
1418                 pos += 2;
1419
1420                 wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Response "
1421                            "(status_code=%d)", status_code);
1422
1423                 if (wpa_ft_action_send(wpa_auth, sta_addr, start, alen) < 0)
1424                         return -1;
1425         } else {
1426                 wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with unknown "
1427                            "packet_type %d", frame->packet_type);
1428                 return -1;
1429         }
1430
1431         return 0;
1432 }
1433
1434
1435 static void wpa_ft_generate_pmk_r1(struct wpa_authenticator *wpa_auth,
1436                                    struct wpa_ft_pmk_r0_sa *pmk_r0,
1437                                    struct ft_remote_r1kh *r1kh,
1438                                    const u8 *s1kh_id)
1439 {
1440         struct ft_r0kh_r1kh_push_frame frame, f;
1441         struct os_time now;
1442
1443         os_memset(&frame, 0, sizeof(frame));
1444         frame.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
1445         frame.packet_type = FT_PACKET_R0KH_R1KH_PUSH;
1446         frame.data_length = host_to_le16(FT_R0KH_R1KH_PUSH_DATA_LEN);
1447         os_memcpy(frame.ap_address, wpa_auth->addr, ETH_ALEN);
1448
1449         /* aes_wrap() does not support inplace encryption, so use a temporary
1450          * buffer for the data. */
1451         os_memcpy(f.r1kh_id, r1kh->id, FT_R1KH_ID_LEN);
1452         os_memcpy(f.s1kh_id, s1kh_id, ETH_ALEN);
1453         os_memcpy(f.pmk_r0_name, pmk_r0->pmk_r0_name, WPA_PMK_NAME_LEN);
1454         wpa_derive_pmk_r1(pmk_r0->pmk_r0, pmk_r0->pmk_r0_name, r1kh->id,
1455                           s1kh_id, f.pmk_r1, f.pmk_r1_name);
1456         wpa_printf(MSG_DEBUG, "FT: R1KH-ID " MACSTR, MAC2STR(r1kh->id));
1457         wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", f.pmk_r1, PMK_LEN);
1458         wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", f.pmk_r1_name,
1459                     WPA_PMK_NAME_LEN);
1460         os_get_time(&now);
1461         WPA_PUT_LE32(f.timestamp, now.sec);
1462         if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8,
1463                      f.timestamp, frame.timestamp) < 0)
1464                 return;
1465
1466         wpa_ft_rrb_send(wpa_auth, r1kh->addr, (u8 *) &frame, sizeof(frame));
1467 }
1468
1469
1470 void wpa_ft_push_pmk_r1(struct wpa_authenticator *wpa_auth, const u8 *addr)
1471 {
1472         struct wpa_ft_pmk_r0_sa *r0;
1473         struct ft_remote_r1kh *r1kh;
1474
1475         if (!wpa_auth->conf.pmk_r1_push)
1476                 return;
1477
1478         r0 = wpa_auth->ft_pmk_cache->pmk_r0;
1479         while (r0) {
1480                 if (os_memcmp(r0->spa, addr, ETH_ALEN) == 0)
1481                         break;
1482                 r0 = r0->next;
1483         }
1484
1485         if (r0 == NULL || r0->pmk_r1_pushed)
1486                 return;
1487         r0->pmk_r1_pushed = 1;
1488
1489         wpa_printf(MSG_DEBUG, "FT: Deriving and pushing PMK-R1 keys to R1KHs "
1490                    "for STA " MACSTR, MAC2STR(addr));
1491
1492         r1kh = wpa_auth->conf.r1kh_list;
1493         while (r1kh) {
1494                 wpa_ft_generate_pmk_r1(wpa_auth, r0, r1kh, addr);
1495                 r1kh = r1kh->next;
1496         }
1497 }
1498
1499 #endif /* CONFIG_IEEE80211R */