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