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