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