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