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