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