]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/ap/wpa_auth.c
Make struct wpa_eapol_key easier to use with variable length MIC
[thirdparty/hostap.git] / src / ap / wpa_auth.c
1 /*
2 * IEEE 802.11 RSN / WPA Authenticator
3 * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "utils/state_machine.h"
14 #include "utils/bitfield.h"
15 #include "common/ieee802_11_defs.h"
16 #include "crypto/aes_wrap.h"
17 #include "crypto/crypto.h"
18 #include "crypto/sha1.h"
19 #include "crypto/sha256.h"
20 #include "crypto/random.h"
21 #include "eapol_auth/eapol_auth_sm.h"
22 #include "ap_config.h"
23 #include "ieee802_11.h"
24 #include "wpa_auth.h"
25 #include "pmksa_cache_auth.h"
26 #include "wpa_auth_i.h"
27 #include "wpa_auth_ie.h"
28
29 #define STATE_MACHINE_DATA struct wpa_state_machine
30 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
31 #define STATE_MACHINE_ADDR sm->addr
32
33
34 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
35 static int wpa_sm_step(struct wpa_state_machine *sm);
36 static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
37 size_t data_len);
38 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
39 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
40 struct wpa_group *group);
41 static void wpa_request_new_ptk(struct wpa_state_machine *sm);
42 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
43 struct wpa_group *group);
44 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
45 struct wpa_group *group);
46 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
47 const u8 *pmk, unsigned int pmk_len,
48 struct wpa_ptk *ptk);
49 static void wpa_group_free(struct wpa_authenticator *wpa_auth,
50 struct wpa_group *group);
51 static void wpa_group_get(struct wpa_authenticator *wpa_auth,
52 struct wpa_group *group);
53 static void wpa_group_put(struct wpa_authenticator *wpa_auth,
54 struct wpa_group *group);
55
56 static const u32 dot11RSNAConfigGroupUpdateCount = 4;
57 static const u32 dot11RSNAConfigPairwiseUpdateCount = 4;
58 static const u32 eapol_key_timeout_first = 100; /* ms */
59 static const u32 eapol_key_timeout_subseq = 1000; /* ms */
60 static const u32 eapol_key_timeout_first_group = 500; /* ms */
61
62 /* TODO: make these configurable */
63 static const int dot11RSNAConfigPMKLifetime = 43200;
64 static const int dot11RSNAConfigPMKReauthThreshold = 70;
65 static const int dot11RSNAConfigSATimeout = 60;
66
67
68 static inline int wpa_auth_mic_failure_report(
69 struct wpa_authenticator *wpa_auth, const u8 *addr)
70 {
71 if (wpa_auth->cb.mic_failure_report)
72 return wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr);
73 return 0;
74 }
75
76
77 static inline void wpa_auth_psk_failure_report(
78 struct wpa_authenticator *wpa_auth, const u8 *addr)
79 {
80 if (wpa_auth->cb.psk_failure_report)
81 wpa_auth->cb.psk_failure_report(wpa_auth->cb.ctx, addr);
82 }
83
84
85 static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
86 const u8 *addr, wpa_eapol_variable var,
87 int value)
88 {
89 if (wpa_auth->cb.set_eapol)
90 wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value);
91 }
92
93
94 static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
95 const u8 *addr, wpa_eapol_variable var)
96 {
97 if (wpa_auth->cb.get_eapol == NULL)
98 return -1;
99 return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var);
100 }
101
102
103 static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
104 const u8 *addr,
105 const u8 *p2p_dev_addr,
106 const u8 *prev_psk)
107 {
108 if (wpa_auth->cb.get_psk == NULL)
109 return NULL;
110 return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, p2p_dev_addr,
111 prev_psk);
112 }
113
114
115 static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
116 const u8 *addr, u8 *msk, size_t *len)
117 {
118 if (wpa_auth->cb.get_msk == NULL)
119 return -1;
120 return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len);
121 }
122
123
124 static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
125 int vlan_id,
126 enum wpa_alg alg, const u8 *addr, int idx,
127 u8 *key, size_t key_len)
128 {
129 if (wpa_auth->cb.set_key == NULL)
130 return -1;
131 return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
132 key, key_len);
133 }
134
135
136 static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
137 const u8 *addr, int idx, u8 *seq)
138 {
139 if (wpa_auth->cb.get_seqnum == NULL)
140 return -1;
141 return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
142 }
143
144
145 static inline int
146 wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
147 const u8 *data, size_t data_len, int encrypt)
148 {
149 if (wpa_auth->cb.send_eapol == NULL)
150 return -1;
151 return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
152 encrypt);
153 }
154
155
156 #ifdef CONFIG_MESH
157 static inline int wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth,
158 const u8 *addr)
159 {
160 if (wpa_auth->cb.start_ampe == NULL)
161 return -1;
162 return wpa_auth->cb.start_ampe(wpa_auth->cb.ctx, addr);
163 }
164 #endif /* CONFIG_MESH */
165
166
167 int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
168 int (*cb)(struct wpa_state_machine *sm, void *ctx),
169 void *cb_ctx)
170 {
171 if (wpa_auth->cb.for_each_sta == NULL)
172 return 0;
173 return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
174 }
175
176
177 int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
178 int (*cb)(struct wpa_authenticator *a, void *ctx),
179 void *cb_ctx)
180 {
181 if (wpa_auth->cb.for_each_auth == NULL)
182 return 0;
183 return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
184 }
185
186
187 void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
188 logger_level level, const char *txt)
189 {
190 if (wpa_auth->cb.logger == NULL)
191 return;
192 wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
193 }
194
195
196 void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
197 logger_level level, const char *fmt, ...)
198 {
199 char *format;
200 int maxlen;
201 va_list ap;
202
203 if (wpa_auth->cb.logger == NULL)
204 return;
205
206 maxlen = os_strlen(fmt) + 100;
207 format = os_malloc(maxlen);
208 if (!format)
209 return;
210
211 va_start(ap, fmt);
212 vsnprintf(format, maxlen, fmt, ap);
213 va_end(ap);
214
215 wpa_auth_logger(wpa_auth, addr, level, format);
216
217 os_free(format);
218 }
219
220
221 static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
222 const u8 *addr)
223 {
224 if (wpa_auth->cb.disconnect == NULL)
225 return;
226 wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR, MAC2STR(addr));
227 wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
228 WLAN_REASON_PREV_AUTH_NOT_VALID);
229 }
230
231
232 static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
233 {
234 int ret = 0;
235 #ifdef CONFIG_IEEE80211R
236 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
237 ret = 1;
238 #endif /* CONFIG_IEEE80211R */
239 #ifdef CONFIG_IEEE80211W
240 if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
241 ret = 1;
242 #endif /* CONFIG_IEEE80211W */
243 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN)
244 ret = 1;
245 return ret;
246 }
247
248
249 static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
250 {
251 struct wpa_authenticator *wpa_auth = eloop_ctx;
252
253 if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
254 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
255 "initialization.");
256 } else {
257 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
258 wpa_hexdump_key(MSG_DEBUG, "GMK",
259 wpa_auth->group->GMK, WPA_GMK_LEN);
260 }
261
262 if (wpa_auth->conf.wpa_gmk_rekey) {
263 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
264 wpa_rekey_gmk, wpa_auth, NULL);
265 }
266 }
267
268
269 static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
270 {
271 struct wpa_authenticator *wpa_auth = eloop_ctx;
272 struct wpa_group *group, *next;
273
274 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
275 group = wpa_auth->group;
276 while (group) {
277 wpa_group_get(wpa_auth, group);
278
279 group->GTKReKey = TRUE;
280 do {
281 group->changed = FALSE;
282 wpa_group_sm_step(wpa_auth, group);
283 } while (group->changed);
284
285 next = group->next;
286 wpa_group_put(wpa_auth, group);
287 group = next;
288 }
289
290 if (wpa_auth->conf.wpa_group_rekey) {
291 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
292 0, wpa_rekey_gtk, wpa_auth, NULL);
293 }
294 }
295
296
297 static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
298 {
299 struct wpa_authenticator *wpa_auth = eloop_ctx;
300 struct wpa_state_machine *sm = timeout_ctx;
301
302 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
303 wpa_request_new_ptk(sm);
304 wpa_sm_step(sm);
305 }
306
307
308 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
309 {
310 if (sm->pmksa == ctx)
311 sm->pmksa = NULL;
312 return 0;
313 }
314
315
316 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
317 void *ctx)
318 {
319 struct wpa_authenticator *wpa_auth = ctx;
320 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
321 }
322
323
324 static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
325 struct wpa_group *group)
326 {
327 u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
328 u8 rkey[32];
329 unsigned long ptr;
330
331 if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
332 return -1;
333 wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
334
335 /*
336 * Counter = PRF-256(Random number, "Init Counter",
337 * Local MAC Address || Time)
338 */
339 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
340 wpa_get_ntp_timestamp(buf + ETH_ALEN);
341 ptr = (unsigned long) group;
342 os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
343 if (random_get_bytes(rkey, sizeof(rkey)) < 0)
344 return -1;
345
346 if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
347 group->Counter, WPA_NONCE_LEN) < 0)
348 return -1;
349 wpa_hexdump_key(MSG_DEBUG, "Key Counter",
350 group->Counter, WPA_NONCE_LEN);
351
352 return 0;
353 }
354
355
356 static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
357 int vlan_id, int delay_init)
358 {
359 struct wpa_group *group;
360
361 group = os_zalloc(sizeof(struct wpa_group));
362 if (group == NULL)
363 return NULL;
364
365 group->GTKAuthenticator = TRUE;
366 group->vlan_id = vlan_id;
367 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
368
369 if (random_pool_ready() != 1) {
370 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
371 "for secure operations - update keys later when "
372 "the first station connects");
373 }
374
375 /*
376 * Set initial GMK/Counter value here. The actual values that will be
377 * used in negotiations will be set once the first station tries to
378 * connect. This allows more time for collecting additional randomness
379 * on embedded devices.
380 */
381 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
382 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
383 "initialization.");
384 os_free(group);
385 return NULL;
386 }
387
388 group->GInit = TRUE;
389 if (delay_init) {
390 wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start "
391 "until Beacon frames have been configured");
392 /* Initialization is completed in wpa_init_keys(). */
393 } else {
394 wpa_group_sm_step(wpa_auth, group);
395 group->GInit = FALSE;
396 wpa_group_sm_step(wpa_auth, group);
397 }
398
399 return group;
400 }
401
402
403 /**
404 * wpa_init - Initialize WPA authenticator
405 * @addr: Authenticator address
406 * @conf: Configuration for WPA authenticator
407 * @cb: Callback functions for WPA authenticator
408 * Returns: Pointer to WPA authenticator data or %NULL on failure
409 */
410 struct wpa_authenticator * wpa_init(const u8 *addr,
411 struct wpa_auth_config *conf,
412 struct wpa_auth_callbacks *cb)
413 {
414 struct wpa_authenticator *wpa_auth;
415
416 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
417 if (wpa_auth == NULL)
418 return NULL;
419 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
420 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
421 os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
422
423 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
424 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
425 os_free(wpa_auth);
426 return NULL;
427 }
428
429 wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
430 if (wpa_auth->group == NULL) {
431 os_free(wpa_auth->wpa_ie);
432 os_free(wpa_auth);
433 return NULL;
434 }
435
436 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
437 wpa_auth);
438 if (wpa_auth->pmksa == NULL) {
439 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
440 os_free(wpa_auth->group);
441 os_free(wpa_auth->wpa_ie);
442 os_free(wpa_auth);
443 return NULL;
444 }
445
446 #ifdef CONFIG_IEEE80211R
447 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
448 if (wpa_auth->ft_pmk_cache == NULL) {
449 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
450 os_free(wpa_auth->group);
451 os_free(wpa_auth->wpa_ie);
452 pmksa_cache_auth_deinit(wpa_auth->pmksa);
453 os_free(wpa_auth);
454 return NULL;
455 }
456 #endif /* CONFIG_IEEE80211R */
457
458 if (wpa_auth->conf.wpa_gmk_rekey) {
459 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
460 wpa_rekey_gmk, wpa_auth, NULL);
461 }
462
463 if (wpa_auth->conf.wpa_group_rekey) {
464 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
465 wpa_rekey_gtk, wpa_auth, NULL);
466 }
467
468 #ifdef CONFIG_P2P
469 if (WPA_GET_BE32(conf->ip_addr_start)) {
470 int count = WPA_GET_BE32(conf->ip_addr_end) -
471 WPA_GET_BE32(conf->ip_addr_start) + 1;
472 if (count > 1000)
473 count = 1000;
474 if (count > 0)
475 wpa_auth->ip_pool = bitfield_alloc(count);
476 }
477 #endif /* CONFIG_P2P */
478
479 return wpa_auth;
480 }
481
482
483 int wpa_init_keys(struct wpa_authenticator *wpa_auth)
484 {
485 struct wpa_group *group = wpa_auth->group;
486
487 wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial "
488 "keys");
489 wpa_group_sm_step(wpa_auth, group);
490 group->GInit = FALSE;
491 wpa_group_sm_step(wpa_auth, group);
492 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
493 return -1;
494 return 0;
495 }
496
497
498 /**
499 * wpa_deinit - Deinitialize WPA authenticator
500 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
501 */
502 void wpa_deinit(struct wpa_authenticator *wpa_auth)
503 {
504 struct wpa_group *group, *prev;
505
506 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
507 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
508
509 #ifdef CONFIG_PEERKEY
510 while (wpa_auth->stsl_negotiations)
511 wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
512 #endif /* CONFIG_PEERKEY */
513
514 pmksa_cache_auth_deinit(wpa_auth->pmksa);
515
516 #ifdef CONFIG_IEEE80211R
517 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
518 wpa_auth->ft_pmk_cache = NULL;
519 #endif /* CONFIG_IEEE80211R */
520
521 #ifdef CONFIG_P2P
522 bitfield_free(wpa_auth->ip_pool);
523 #endif /* CONFIG_P2P */
524
525
526 os_free(wpa_auth->wpa_ie);
527
528 group = wpa_auth->group;
529 while (group) {
530 prev = group;
531 group = group->next;
532 os_free(prev);
533 }
534
535 os_free(wpa_auth);
536 }
537
538
539 /**
540 * wpa_reconfig - Update WPA authenticator configuration
541 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
542 * @conf: Configuration for WPA authenticator
543 */
544 int wpa_reconfig(struct wpa_authenticator *wpa_auth,
545 struct wpa_auth_config *conf)
546 {
547 struct wpa_group *group;
548 if (wpa_auth == NULL)
549 return 0;
550
551 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
552 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
553 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
554 return -1;
555 }
556
557 /*
558 * Reinitialize GTK to make sure it is suitable for the new
559 * configuration.
560 */
561 group = wpa_auth->group;
562 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
563 group->GInit = TRUE;
564 wpa_group_sm_step(wpa_auth, group);
565 group->GInit = FALSE;
566 wpa_group_sm_step(wpa_auth, group);
567
568 return 0;
569 }
570
571
572 struct wpa_state_machine *
573 wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
574 const u8 *p2p_dev_addr)
575 {
576 struct wpa_state_machine *sm;
577
578 if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
579 return NULL;
580
581 sm = os_zalloc(sizeof(struct wpa_state_machine));
582 if (sm == NULL)
583 return NULL;
584 os_memcpy(sm->addr, addr, ETH_ALEN);
585 if (p2p_dev_addr)
586 os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
587
588 sm->wpa_auth = wpa_auth;
589 sm->group = wpa_auth->group;
590 wpa_group_get(sm->wpa_auth, sm->group);
591
592 return sm;
593 }
594
595
596 int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
597 struct wpa_state_machine *sm)
598 {
599 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
600 return -1;
601
602 #ifdef CONFIG_IEEE80211R
603 if (sm->ft_completed) {
604 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
605 "FT authentication already completed - do not "
606 "start 4-way handshake");
607 /* Go to PTKINITDONE state to allow GTK rekeying */
608 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
609 return 0;
610 }
611 #endif /* CONFIG_IEEE80211R */
612
613 if (sm->started) {
614 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
615 sm->ReAuthenticationRequest = TRUE;
616 return wpa_sm_step(sm);
617 }
618
619 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
620 "start authentication");
621 sm->started = 1;
622
623 sm->Init = TRUE;
624 if (wpa_sm_step(sm) == 1)
625 return 1; /* should not really happen */
626 sm->Init = FALSE;
627 sm->AuthenticationRequest = TRUE;
628 return wpa_sm_step(sm);
629 }
630
631
632 void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
633 {
634 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
635 * reassociates back to the same AP while the previous entry for the
636 * STA has not yet been removed. */
637 if (sm == NULL)
638 return;
639
640 sm->wpa_key_mgmt = 0;
641 }
642
643
644 static void wpa_free_sta_sm(struct wpa_state_machine *sm)
645 {
646 #ifdef CONFIG_P2P
647 if (WPA_GET_BE32(sm->ip_addr)) {
648 u32 start;
649 wpa_printf(MSG_DEBUG, "P2P: Free assigned IP "
650 "address %u.%u.%u.%u from " MACSTR,
651 sm->ip_addr[0], sm->ip_addr[1],
652 sm->ip_addr[2], sm->ip_addr[3],
653 MAC2STR(sm->addr));
654 start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start);
655 bitfield_clear(sm->wpa_auth->ip_pool,
656 WPA_GET_BE32(sm->ip_addr) - start);
657 }
658 #endif /* CONFIG_P2P */
659 if (sm->GUpdateStationKeys) {
660 sm->group->GKeyDoneStations--;
661 sm->GUpdateStationKeys = FALSE;
662 }
663 #ifdef CONFIG_IEEE80211R
664 os_free(sm->assoc_resp_ftie);
665 wpabuf_free(sm->ft_pending_req_ies);
666 #endif /* CONFIG_IEEE80211R */
667 os_free(sm->last_rx_eapol_key);
668 os_free(sm->wpa_ie);
669 wpa_group_put(sm->wpa_auth, sm->group);
670 os_free(sm);
671 }
672
673
674 void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
675 {
676 if (sm == NULL)
677 return;
678
679 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
680 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
681 "strict rekeying - force GTK rekey since STA "
682 "is leaving");
683 eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
684 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
685 NULL);
686 }
687
688 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
689 sm->pending_1_of_4_timeout = 0;
690 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
691 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
692 if (sm->in_step_loop) {
693 /* Must not free state machine while wpa_sm_step() is running.
694 * Freeing will be completed in the end of wpa_sm_step(). */
695 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
696 "machine deinit for " MACSTR, MAC2STR(sm->addr));
697 sm->pending_deinit = 1;
698 } else
699 wpa_free_sta_sm(sm);
700 }
701
702
703 static void wpa_request_new_ptk(struct wpa_state_machine *sm)
704 {
705 if (sm == NULL)
706 return;
707
708 sm->PTKRequest = TRUE;
709 sm->PTK_valid = 0;
710 }
711
712
713 static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
714 const u8 *replay_counter)
715 {
716 int i;
717 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
718 if (!ctr[i].valid)
719 break;
720 if (os_memcmp(replay_counter, ctr[i].counter,
721 WPA_REPLAY_COUNTER_LEN) == 0)
722 return 1;
723 }
724 return 0;
725 }
726
727
728 static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
729 const u8 *replay_counter)
730 {
731 int i;
732 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
733 if (ctr[i].valid &&
734 (replay_counter == NULL ||
735 os_memcmp(replay_counter, ctr[i].counter,
736 WPA_REPLAY_COUNTER_LEN) == 0))
737 ctr[i].valid = FALSE;
738 }
739 }
740
741
742 #ifdef CONFIG_IEEE80211R
743 static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
744 struct wpa_state_machine *sm,
745 struct wpa_eapol_ie_parse *kde)
746 {
747 struct wpa_ie_data ie;
748 struct rsn_mdie *mdie;
749
750 if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
751 ie.num_pmkid != 1 || ie.pmkid == NULL) {
752 wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
753 "FT 4-way handshake message 2/4");
754 return -1;
755 }
756
757 os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
758 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
759 sm->sup_pmk_r1_name, PMKID_LEN);
760
761 if (!kde->mdie || !kde->ftie) {
762 wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
763 "message 2/4", kde->mdie ? "FTIE" : "MDIE");
764 return -1;
765 }
766
767 mdie = (struct rsn_mdie *) (kde->mdie + 2);
768 if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
769 os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
770 MOBILITY_DOMAIN_ID_LEN) != 0) {
771 wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
772 return -1;
773 }
774
775 if (sm->assoc_resp_ftie &&
776 (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
777 os_memcmp(kde->ftie, sm->assoc_resp_ftie,
778 2 + sm->assoc_resp_ftie[1]) != 0)) {
779 wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
780 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
781 kde->ftie, kde->ftie_len);
782 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
783 sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
784 return -1;
785 }
786
787 return 0;
788 }
789 #endif /* CONFIG_IEEE80211R */
790
791
792 static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
793 struct wpa_state_machine *sm, int group)
794 {
795 /* Supplicant reported a Michael MIC error */
796 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
797 "received EAPOL-Key Error Request "
798 "(STA detected Michael MIC failure (group=%d))",
799 group);
800
801 if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
802 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
803 "ignore Michael MIC failure report since "
804 "group cipher is not TKIP");
805 } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
806 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
807 "ignore Michael MIC failure report since "
808 "pairwise cipher is not TKIP");
809 } else {
810 if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
811 return 1; /* STA entry was removed */
812 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
813 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
814 }
815
816 /*
817 * Error report is not a request for a new key handshake, but since
818 * Authenticator may do it, let's change the keys now anyway.
819 */
820 wpa_request_new_ptk(sm);
821 return 0;
822 }
823
824
825 static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data,
826 size_t data_len)
827 {
828 struct wpa_ptk PTK;
829 int ok = 0;
830 const u8 *pmk = NULL;
831 unsigned int pmk_len;
832
833 for (;;) {
834 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
835 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
836 sm->p2p_dev_addr, pmk);
837 if (pmk == NULL)
838 break;
839 pmk_len = PMK_LEN;
840 } else {
841 pmk = sm->PMK;
842 pmk_len = sm->pmk_len;
843 }
844
845 wpa_derive_ptk(sm, sm->alt_SNonce, pmk, pmk_len, &PTK);
846
847 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK, data, data_len)
848 == 0) {
849 ok = 1;
850 break;
851 }
852
853 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
854 break;
855 }
856
857 if (!ok) {
858 wpa_printf(MSG_DEBUG,
859 "WPA: Earlier SNonce did not result in matching MIC");
860 return -1;
861 }
862
863 wpa_printf(MSG_DEBUG,
864 "WPA: Earlier SNonce resulted in matching MIC");
865 sm->alt_snonce_valid = 0;
866 os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN);
867 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
868 sm->PTK_valid = TRUE;
869
870 return 0;
871 }
872
873
874 void wpa_receive(struct wpa_authenticator *wpa_auth,
875 struct wpa_state_machine *sm,
876 u8 *data, size_t data_len)
877 {
878 struct ieee802_1x_hdr *hdr;
879 struct wpa_eapol_key *key;
880 u16 key_info, key_data_length;
881 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
882 SMK_M1, SMK_M3, SMK_ERROR } msg;
883 char *msgtxt;
884 struct wpa_eapol_ie_parse kde;
885 int ft;
886 const u8 *eapol_key_ie, *key_data;
887 size_t eapol_key_ie_len, keyhdrlen, mic_len;
888 u8 *mic;
889
890 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
891 return;
892
893 mic_len = wpa_mic_len(sm->wpa_key_mgmt);
894 keyhdrlen = sizeof(*key) + mic_len + 2;
895
896 if (data_len < sizeof(*hdr) + keyhdrlen)
897 return;
898
899 hdr = (struct ieee802_1x_hdr *) data;
900 key = (struct wpa_eapol_key *) (hdr + 1);
901 mic = (u8 *) (key + 1);
902 key_info = WPA_GET_BE16(key->key_info);
903 key_data = mic + mic_len + 2;
904 key_data_length = WPA_GET_BE16(mic + mic_len);
905 wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
906 " key_info=0x%x type=%u key_data_length=%u",
907 MAC2STR(sm->addr), key_info, key->type, key_data_length);
908 if (key_data_length > data_len - sizeof(*hdr) - keyhdrlen) {
909 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
910 "key_data overflow (%d > %lu)",
911 key_data_length,
912 (unsigned long) (data_len - sizeof(*hdr) -
913 keyhdrlen));
914 return;
915 }
916
917 if (sm->wpa == WPA_VERSION_WPA2) {
918 if (key->type == EAPOL_KEY_TYPE_WPA) {
919 /*
920 * Some deployed station implementations seem to send
921 * msg 4/4 with incorrect type value in WPA2 mode.
922 */
923 wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
924 "with unexpected WPA type in RSN mode");
925 } else if (key->type != EAPOL_KEY_TYPE_RSN) {
926 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
927 "unexpected type %d in RSN mode",
928 key->type);
929 return;
930 }
931 } else {
932 if (key->type != EAPOL_KEY_TYPE_WPA) {
933 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
934 "unexpected type %d in WPA mode",
935 key->type);
936 return;
937 }
938 }
939
940 wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
941 WPA_NONCE_LEN);
942 wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
943 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
944
945 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
946 * are set */
947
948 if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
949 (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
950 if (key_info & WPA_KEY_INFO_ERROR) {
951 msg = SMK_ERROR;
952 msgtxt = "SMK Error";
953 } else {
954 msg = SMK_M1;
955 msgtxt = "SMK M1";
956 }
957 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
958 msg = SMK_M3;
959 msgtxt = "SMK M3";
960 } else if (key_info & WPA_KEY_INFO_REQUEST) {
961 msg = REQUEST;
962 msgtxt = "Request";
963 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
964 msg = GROUP_2;
965 msgtxt = "2/2 Group";
966 } else if (key_data_length == 0) {
967 msg = PAIRWISE_4;
968 msgtxt = "4/4 Pairwise";
969 } else {
970 msg = PAIRWISE_2;
971 msgtxt = "2/4 Pairwise";
972 }
973
974 /* TODO: key_info type validation for PeerKey */
975 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
976 msg == GROUP_2) {
977 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
978 if (sm->pairwise == WPA_CIPHER_CCMP ||
979 sm->pairwise == WPA_CIPHER_GCMP) {
980 if (wpa_use_aes_cmac(sm) &&
981 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN &&
982 !wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) &&
983 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
984 wpa_auth_logger(wpa_auth, sm->addr,
985 LOGGER_WARNING,
986 "advertised support for "
987 "AES-128-CMAC, but did not "
988 "use it");
989 return;
990 }
991
992 if (!wpa_use_aes_cmac(sm) &&
993 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
994 wpa_auth_logger(wpa_auth, sm->addr,
995 LOGGER_WARNING,
996 "did not use HMAC-SHA1-AES "
997 "with CCMP/GCMP");
998 return;
999 }
1000 }
1001
1002 if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) &&
1003 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1004 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1005 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
1006 return;
1007 }
1008 }
1009
1010 if (key_info & WPA_KEY_INFO_REQUEST) {
1011 if (sm->req_replay_counter_used &&
1012 os_memcmp(key->replay_counter, sm->req_replay_counter,
1013 WPA_REPLAY_COUNTER_LEN) <= 0) {
1014 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1015 "received EAPOL-Key request with "
1016 "replayed counter");
1017 return;
1018 }
1019 }
1020
1021 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
1022 !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
1023 int i;
1024
1025 if (msg == PAIRWISE_2 &&
1026 wpa_replay_counter_valid(sm->prev_key_replay,
1027 key->replay_counter) &&
1028 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1029 os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
1030 {
1031 /*
1032 * Some supplicant implementations (e.g., Windows XP
1033 * WZC) update SNonce for each EAPOL-Key 2/4. This
1034 * breaks the workaround on accepting any of the
1035 * pending requests, so allow the SNonce to be updated
1036 * even if we have already sent out EAPOL-Key 3/4.
1037 */
1038 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1039 "Process SNonce update from STA "
1040 "based on retransmitted EAPOL-Key "
1041 "1/4");
1042 sm->update_snonce = 1;
1043 os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN);
1044 sm->alt_snonce_valid = TRUE;
1045 os_memcpy(sm->alt_replay_counter,
1046 sm->key_replay[0].counter,
1047 WPA_REPLAY_COUNTER_LEN);
1048 goto continue_processing;
1049 }
1050
1051 if (msg == PAIRWISE_4 && sm->alt_snonce_valid &&
1052 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1053 os_memcmp(key->replay_counter, sm->alt_replay_counter,
1054 WPA_REPLAY_COUNTER_LEN) == 0) {
1055 /*
1056 * Supplicant may still be using the old SNonce since
1057 * there was two EAPOL-Key 2/4 messages and they had
1058 * different SNonce values.
1059 */
1060 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1061 "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
1062 goto continue_processing;
1063 }
1064
1065 if (msg == PAIRWISE_2 &&
1066 wpa_replay_counter_valid(sm->prev_key_replay,
1067 key->replay_counter) &&
1068 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
1069 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1070 "ignore retransmitted EAPOL-Key %s - "
1071 "SNonce did not change", msgtxt);
1072 } else {
1073 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1074 "received EAPOL-Key %s with "
1075 "unexpected replay counter", msgtxt);
1076 }
1077 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
1078 if (!sm->key_replay[i].valid)
1079 break;
1080 wpa_hexdump(MSG_DEBUG, "pending replay counter",
1081 sm->key_replay[i].counter,
1082 WPA_REPLAY_COUNTER_LEN);
1083 }
1084 wpa_hexdump(MSG_DEBUG, "received replay counter",
1085 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1086 return;
1087 }
1088
1089 continue_processing:
1090 switch (msg) {
1091 case PAIRWISE_2:
1092 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
1093 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
1094 (!sm->update_snonce ||
1095 sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
1096 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1097 "received EAPOL-Key msg 2/4 in "
1098 "invalid state (%d) - dropped",
1099 sm->wpa_ptk_state);
1100 return;
1101 }
1102 random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
1103 if (sm->group->reject_4way_hs_for_entropy) {
1104 /*
1105 * The system did not have enough entropy to generate
1106 * strong random numbers. Reject the first 4-way
1107 * handshake(s) and collect some entropy based on the
1108 * information from it. Once enough entropy is
1109 * available, the next atempt will trigger GMK/Key
1110 * Counter update and the station will be allowed to
1111 * continue.
1112 */
1113 wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
1114 "collect more entropy for random number "
1115 "generation");
1116 random_mark_pool_ready();
1117 wpa_sta_disconnect(wpa_auth, sm->addr);
1118 return;
1119 }
1120 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
1121 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1122 "received EAPOL-Key msg 2/4 with "
1123 "invalid Key Data contents");
1124 return;
1125 }
1126 if (kde.rsn_ie) {
1127 eapol_key_ie = kde.rsn_ie;
1128 eapol_key_ie_len = kde.rsn_ie_len;
1129 } else if (kde.osen) {
1130 eapol_key_ie = kde.osen;
1131 eapol_key_ie_len = kde.osen_len;
1132 } else {
1133 eapol_key_ie = kde.wpa_ie;
1134 eapol_key_ie_len = kde.wpa_ie_len;
1135 }
1136 ft = sm->wpa == WPA_VERSION_WPA2 &&
1137 wpa_key_mgmt_ft(sm->wpa_key_mgmt);
1138 if (sm->wpa_ie == NULL ||
1139 wpa_compare_rsn_ie(ft,
1140 sm->wpa_ie, sm->wpa_ie_len,
1141 eapol_key_ie, eapol_key_ie_len)) {
1142 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1143 "WPA IE from (Re)AssocReq did not "
1144 "match with msg 2/4");
1145 if (sm->wpa_ie) {
1146 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
1147 sm->wpa_ie, sm->wpa_ie_len);
1148 }
1149 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
1150 eapol_key_ie, eapol_key_ie_len);
1151 /* MLME-DEAUTHENTICATE.request */
1152 wpa_sta_disconnect(wpa_auth, sm->addr);
1153 return;
1154 }
1155 #ifdef CONFIG_IEEE80211R
1156 if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
1157 wpa_sta_disconnect(wpa_auth, sm->addr);
1158 return;
1159 }
1160 #endif /* CONFIG_IEEE80211R */
1161 #ifdef CONFIG_P2P
1162 if (kde.ip_addr_req && kde.ip_addr_req[0] &&
1163 wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
1164 int idx;
1165 wpa_printf(MSG_DEBUG, "P2P: IP address requested in "
1166 "EAPOL-Key exchange");
1167 idx = bitfield_get_first_zero(wpa_auth->ip_pool);
1168 if (idx >= 0) {
1169 u32 start = WPA_GET_BE32(wpa_auth->conf.
1170 ip_addr_start);
1171 bitfield_set(wpa_auth->ip_pool, idx);
1172 WPA_PUT_BE32(sm->ip_addr, start + idx);
1173 wpa_printf(MSG_DEBUG, "P2P: Assigned IP "
1174 "address %u.%u.%u.%u to " MACSTR,
1175 sm->ip_addr[0], sm->ip_addr[1],
1176 sm->ip_addr[2], sm->ip_addr[3],
1177 MAC2STR(sm->addr));
1178 }
1179 }
1180 #endif /* CONFIG_P2P */
1181 break;
1182 case PAIRWISE_4:
1183 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1184 !sm->PTK_valid) {
1185 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1186 "received EAPOL-Key msg 4/4 in "
1187 "invalid state (%d) - dropped",
1188 sm->wpa_ptk_state);
1189 return;
1190 }
1191 break;
1192 case GROUP_2:
1193 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1194 || !sm->PTK_valid) {
1195 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1196 "received EAPOL-Key msg 2/2 in "
1197 "invalid state (%d) - dropped",
1198 sm->wpa_ptk_group_state);
1199 return;
1200 }
1201 break;
1202 #ifdef CONFIG_PEERKEY
1203 case SMK_M1:
1204 case SMK_M3:
1205 case SMK_ERROR:
1206 if (!wpa_auth->conf.peerkey) {
1207 wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
1208 "PeerKey use disabled - ignoring message");
1209 return;
1210 }
1211 if (!sm->PTK_valid) {
1212 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1213 "received EAPOL-Key msg SMK in "
1214 "invalid state - dropped");
1215 return;
1216 }
1217 break;
1218 #else /* CONFIG_PEERKEY */
1219 case SMK_M1:
1220 case SMK_M3:
1221 case SMK_ERROR:
1222 return; /* STSL disabled - ignore SMK messages */
1223 #endif /* CONFIG_PEERKEY */
1224 case REQUEST:
1225 break;
1226 }
1227
1228 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1229 "received EAPOL-Key frame (%s)", msgtxt);
1230
1231 if (key_info & WPA_KEY_INFO_ACK) {
1232 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1233 "received invalid EAPOL-Key: Key Ack set");
1234 return;
1235 }
1236
1237 if (!(key_info & WPA_KEY_INFO_MIC)) {
1238 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1239 "received invalid EAPOL-Key: Key MIC not set");
1240 return;
1241 }
1242
1243 sm->MICVerified = FALSE;
1244 if (sm->PTK_valid && !sm->update_snonce) {
1245 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &sm->PTK, data,
1246 data_len) &&
1247 (msg != PAIRWISE_4 || !sm->alt_snonce_valid ||
1248 wpa_try_alt_snonce(sm, data, data_len))) {
1249 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1250 "received EAPOL-Key with invalid MIC");
1251 return;
1252 }
1253 sm->MICVerified = TRUE;
1254 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
1255 sm->pending_1_of_4_timeout = 0;
1256 }
1257
1258 if (key_info & WPA_KEY_INFO_REQUEST) {
1259 if (sm->MICVerified) {
1260 sm->req_replay_counter_used = 1;
1261 os_memcpy(sm->req_replay_counter, key->replay_counter,
1262 WPA_REPLAY_COUNTER_LEN);
1263 } else {
1264 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1265 "received EAPOL-Key request with "
1266 "invalid MIC");
1267 return;
1268 }
1269
1270 /*
1271 * TODO: should decrypt key data field if encryption was used;
1272 * even though MAC address KDE is not normally encrypted,
1273 * supplicant is allowed to encrypt it.
1274 */
1275 if (msg == SMK_ERROR) {
1276 #ifdef CONFIG_PEERKEY
1277 wpa_smk_error(wpa_auth, sm, key_data, key_data_length);
1278 #endif /* CONFIG_PEERKEY */
1279 return;
1280 } else if (key_info & WPA_KEY_INFO_ERROR) {
1281 if (wpa_receive_error_report(
1282 wpa_auth, sm,
1283 !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
1284 return; /* STA entry was removed */
1285 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1286 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1287 "received EAPOL-Key Request for new "
1288 "4-Way Handshake");
1289 wpa_request_new_ptk(sm);
1290 #ifdef CONFIG_PEERKEY
1291 } else if (msg == SMK_M1) {
1292 wpa_smk_m1(wpa_auth, sm, key, key_data,
1293 key_data_length);
1294 #endif /* CONFIG_PEERKEY */
1295 } else if (key_data_length > 0 &&
1296 wpa_parse_kde_ies(key_data, key_data_length,
1297 &kde) == 0 &&
1298 kde.mac_addr) {
1299 } else {
1300 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1301 "received EAPOL-Key Request for GTK "
1302 "rekeying");
1303 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
1304 wpa_rekey_gtk(wpa_auth, NULL);
1305 }
1306 } else {
1307 /* Do not allow the same key replay counter to be reused. */
1308 wpa_replay_counter_mark_invalid(sm->key_replay,
1309 key->replay_counter);
1310
1311 if (msg == PAIRWISE_2) {
1312 /*
1313 * Maintain a copy of the pending EAPOL-Key frames in
1314 * case the EAPOL-Key frame was retransmitted. This is
1315 * needed to allow EAPOL-Key msg 2/4 reply to another
1316 * pending msg 1/4 to update the SNonce to work around
1317 * unexpected supplicant behavior.
1318 */
1319 os_memcpy(sm->prev_key_replay, sm->key_replay,
1320 sizeof(sm->key_replay));
1321 } else {
1322 os_memset(sm->prev_key_replay, 0,
1323 sizeof(sm->prev_key_replay));
1324 }
1325
1326 /*
1327 * Make sure old valid counters are not accepted anymore and
1328 * do not get copied again.
1329 */
1330 wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
1331 }
1332
1333 #ifdef CONFIG_PEERKEY
1334 if (msg == SMK_M3) {
1335 wpa_smk_m3(wpa_auth, sm, key, key_data, key_data_length);
1336 return;
1337 }
1338 #endif /* CONFIG_PEERKEY */
1339
1340 os_free(sm->last_rx_eapol_key);
1341 sm->last_rx_eapol_key = os_malloc(data_len);
1342 if (sm->last_rx_eapol_key == NULL)
1343 return;
1344 os_memcpy(sm->last_rx_eapol_key, data, data_len);
1345 sm->last_rx_eapol_key_len = data_len;
1346
1347 sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
1348 sm->EAPOLKeyReceived = TRUE;
1349 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1350 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1351 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1352 wpa_sm_step(sm);
1353 }
1354
1355
1356 static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
1357 const u8 *gnonce, u8 *gtk, size_t gtk_len)
1358 {
1359 u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + 16];
1360 u8 *pos;
1361 int ret = 0;
1362
1363 /* GTK = PRF-X(GMK, "Group key expansion",
1364 * AA || GNonce || Time || random data)
1365 * The example described in the IEEE 802.11 standard uses only AA and
1366 * GNonce as inputs here. Add some more entropy since this derivation
1367 * is done only at the Authenticator and as such, does not need to be
1368 * exactly same.
1369 */
1370 os_memcpy(data, addr, ETH_ALEN);
1371 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
1372 pos = data + ETH_ALEN + WPA_NONCE_LEN;
1373 wpa_get_ntp_timestamp(pos);
1374 pos += 8;
1375 if (random_get_bytes(pos, 16) < 0)
1376 ret = -1;
1377
1378 #ifdef CONFIG_IEEE80211W
1379 sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len);
1380 #else /* CONFIG_IEEE80211W */
1381 if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len)
1382 < 0)
1383 ret = -1;
1384 #endif /* CONFIG_IEEE80211W */
1385
1386 return ret;
1387 }
1388
1389
1390 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1391 {
1392 struct wpa_authenticator *wpa_auth = eloop_ctx;
1393 struct wpa_state_machine *sm = timeout_ctx;
1394
1395 sm->pending_1_of_4_timeout = 0;
1396 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
1397 sm->TimeoutEvt = TRUE;
1398 wpa_sm_step(sm);
1399 }
1400
1401
1402 void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1403 struct wpa_state_machine *sm, int key_info,
1404 const u8 *key_rsc, const u8 *nonce,
1405 const u8 *kde, size_t kde_len,
1406 int keyidx, int encr, int force_version)
1407 {
1408 struct ieee802_1x_hdr *hdr;
1409 struct wpa_eapol_key *key;
1410 size_t len, mic_len, keyhdrlen;
1411 int alg;
1412 int key_data_len, pad_len = 0;
1413 u8 *buf, *pos;
1414 int version, pairwise;
1415 int i;
1416 u8 *key_mic, *key_data;
1417
1418 mic_len = wpa_mic_len(sm->wpa_key_mgmt);
1419 keyhdrlen = sizeof(*key) + mic_len + 2;
1420
1421 len = sizeof(struct ieee802_1x_hdr) + keyhdrlen;
1422
1423 if (force_version)
1424 version = force_version;
1425 else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
1426 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt))
1427 version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
1428 else if (wpa_use_aes_cmac(sm))
1429 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
1430 else if (sm->pairwise != WPA_CIPHER_TKIP)
1431 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1432 else
1433 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1434
1435 pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1436
1437 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1438 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1439 "encr=%d)",
1440 version,
1441 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
1442 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
1443 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
1444 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
1445 pairwise, (unsigned long) kde_len, keyidx, encr);
1446
1447 key_data_len = kde_len;
1448
1449 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1450 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
1451 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
1452 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1453 pad_len = key_data_len % 8;
1454 if (pad_len)
1455 pad_len = 8 - pad_len;
1456 key_data_len += pad_len + 8;
1457 }
1458
1459 len += key_data_len;
1460
1461 hdr = os_zalloc(len);
1462 if (hdr == NULL)
1463 return;
1464 hdr->version = wpa_auth->conf.eapol_version;
1465 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1466 hdr->length = host_to_be16(len - sizeof(*hdr));
1467 key = (struct wpa_eapol_key *) (hdr + 1);
1468 key_mic = (u8 *) (key + 1);
1469 key_data = ((u8 *) (hdr + 1)) + keyhdrlen;
1470
1471 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1472 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1473 key_info |= version;
1474 if (encr && sm->wpa == WPA_VERSION_WPA2)
1475 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1476 if (sm->wpa != WPA_VERSION_WPA2)
1477 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1478 WPA_PUT_BE16(key->key_info, key_info);
1479
1480 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
1481 WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
1482 if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
1483 WPA_PUT_BE16(key->key_length, 0);
1484
1485 /* FIX: STSL: what to use as key_replay_counter? */
1486 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1487 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1488 os_memcpy(sm->key_replay[i].counter,
1489 sm->key_replay[i - 1].counter,
1490 WPA_REPLAY_COUNTER_LEN);
1491 }
1492 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1493 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1494 WPA_REPLAY_COUNTER_LEN);
1495 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter",
1496 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1497 sm->key_replay[0].valid = TRUE;
1498
1499 if (nonce)
1500 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1501
1502 if (key_rsc)
1503 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1504
1505 if (kde && !encr) {
1506 os_memcpy(key_data, kde, kde_len);
1507 WPA_PUT_BE16(key_mic + mic_len, kde_len);
1508 } else if (encr && kde) {
1509 buf = os_zalloc(key_data_len);
1510 if (buf == NULL) {
1511 os_free(hdr);
1512 return;
1513 }
1514 pos = buf;
1515 os_memcpy(pos, kde, kde_len);
1516 pos += kde_len;
1517
1518 if (pad_len)
1519 *pos++ = 0xdd;
1520
1521 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1522 buf, key_data_len);
1523 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1524 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
1525 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
1526 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1527 if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len,
1528 (key_data_len - 8) / 8, buf, key_data)) {
1529 os_free(hdr);
1530 os_free(buf);
1531 return;
1532 }
1533 WPA_PUT_BE16(key_mic + mic_len, key_data_len);
1534 #ifndef CONFIG_NO_RC4
1535 } else if (sm->PTK.kek_len == 16) {
1536 u8 ek[32];
1537 os_memcpy(key->key_iv,
1538 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1539 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1540 os_memcpy(ek, key->key_iv, 16);
1541 os_memcpy(ek + 16, sm->PTK.kek, sm->PTK.kek_len);
1542 os_memcpy(key_data, buf, key_data_len);
1543 rc4_skip(ek, 32, 256, key_data, key_data_len);
1544 WPA_PUT_BE16(key_mic + mic_len, key_data_len);
1545 #endif /* CONFIG_NO_RC4 */
1546 } else {
1547 os_free(hdr);
1548 os_free(buf);
1549 return;
1550 }
1551 os_free(buf);
1552 }
1553
1554 if (key_info & WPA_KEY_INFO_MIC) {
1555 if (!sm->PTK_valid) {
1556 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1557 "PTK not valid when sending EAPOL-Key "
1558 "frame");
1559 os_free(hdr);
1560 return;
1561 }
1562
1563 wpa_eapol_key_mic(sm->PTK.kck, sm->PTK.kck_len,
1564 sm->wpa_key_mgmt, version,
1565 (u8 *) hdr, len, key_mic);
1566 #ifdef CONFIG_TESTING_OPTIONS
1567 if (!pairwise &&
1568 wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0 &&
1569 drand48() <
1570 wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
1571 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1572 "Corrupting group EAPOL-Key Key MIC");
1573 key_mic[0]++;
1574 }
1575 #endif /* CONFIG_TESTING_OPTIONS */
1576 }
1577
1578 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1579 1);
1580 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1581 sm->pairwise_set);
1582 os_free(hdr);
1583 }
1584
1585
1586 static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1587 struct wpa_state_machine *sm, int key_info,
1588 const u8 *key_rsc, const u8 *nonce,
1589 const u8 *kde, size_t kde_len,
1590 int keyidx, int encr)
1591 {
1592 int timeout_ms;
1593 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1594 int ctr;
1595
1596 if (sm == NULL)
1597 return;
1598
1599 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1600 keyidx, encr, 0);
1601
1602 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1603 if (ctr == 1 && wpa_auth->conf.tx_status)
1604 timeout_ms = pairwise ? eapol_key_timeout_first :
1605 eapol_key_timeout_first_group;
1606 else
1607 timeout_ms = eapol_key_timeout_subseq;
1608 if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
1609 sm->pending_1_of_4_timeout = 1;
1610 wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1611 "counter %d)", timeout_ms, ctr);
1612 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1613 wpa_send_eapol_timeout, wpa_auth, sm);
1614 }
1615
1616
1617 static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
1618 size_t data_len)
1619 {
1620 struct ieee802_1x_hdr *hdr;
1621 struct wpa_eapol_key *key;
1622 u16 key_info;
1623 int ret = 0;
1624 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN], *mic_pos;
1625 size_t mic_len = wpa_mic_len(akmp);
1626
1627 if (data_len < sizeof(*hdr) + sizeof(*key))
1628 return -1;
1629
1630 hdr = (struct ieee802_1x_hdr *) data;
1631 key = (struct wpa_eapol_key *) (hdr + 1);
1632 mic_pos = (u8 *) (key + 1);
1633 key_info = WPA_GET_BE16(key->key_info);
1634 os_memcpy(mic, mic_pos, mic_len);
1635 os_memset(mic_pos, 0, mic_len);
1636 if (wpa_eapol_key_mic(PTK->kck, PTK->kck_len, akmp,
1637 key_info & WPA_KEY_INFO_TYPE_MASK,
1638 data, data_len, mic_pos) ||
1639 os_memcmp_const(mic, mic_pos, mic_len) != 0)
1640 ret = -1;
1641 os_memcpy(mic_pos, mic, mic_len);
1642 return ret;
1643 }
1644
1645
1646 void wpa_remove_ptk(struct wpa_state_machine *sm)
1647 {
1648 sm->PTK_valid = FALSE;
1649 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1650 wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL, 0);
1651 sm->pairwise_set = FALSE;
1652 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1653 }
1654
1655
1656 int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event)
1657 {
1658 int remove_ptk = 1;
1659
1660 if (sm == NULL)
1661 return -1;
1662
1663 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1664 "event %d notification", event);
1665
1666 switch (event) {
1667 case WPA_AUTH:
1668 #ifdef CONFIG_MESH
1669 /* PTKs are derived through AMPE */
1670 if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) {
1671 /* not mesh */
1672 break;
1673 }
1674 return 0;
1675 #endif /* CONFIG_MESH */
1676 case WPA_ASSOC:
1677 break;
1678 case WPA_DEAUTH:
1679 case WPA_DISASSOC:
1680 sm->DeauthenticationRequest = TRUE;
1681 break;
1682 case WPA_REAUTH:
1683 case WPA_REAUTH_EAPOL:
1684 if (!sm->started) {
1685 /*
1686 * When using WPS, we may end up here if the STA
1687 * manages to re-associate without the previous STA
1688 * entry getting removed. Consequently, we need to make
1689 * sure that the WPA state machines gets initialized
1690 * properly at this point.
1691 */
1692 wpa_printf(MSG_DEBUG, "WPA state machine had not been "
1693 "started - initialize now");
1694 sm->started = 1;
1695 sm->Init = TRUE;
1696 if (wpa_sm_step(sm) == 1)
1697 return 1; /* should not really happen */
1698 sm->Init = FALSE;
1699 sm->AuthenticationRequest = TRUE;
1700 break;
1701 }
1702 if (sm->GUpdateStationKeys) {
1703 /*
1704 * Reauthentication cancels the pending group key
1705 * update for this STA.
1706 */
1707 sm->group->GKeyDoneStations--;
1708 sm->GUpdateStationKeys = FALSE;
1709 sm->PtkGroupInit = TRUE;
1710 }
1711 sm->ReAuthenticationRequest = TRUE;
1712 break;
1713 case WPA_ASSOC_FT:
1714 #ifdef CONFIG_IEEE80211R
1715 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1716 "after association");
1717 wpa_ft_install_ptk(sm);
1718
1719 /* Using FT protocol, not WPA auth state machine */
1720 sm->ft_completed = 1;
1721 return 0;
1722 #else /* CONFIG_IEEE80211R */
1723 break;
1724 #endif /* CONFIG_IEEE80211R */
1725 }
1726
1727 #ifdef CONFIG_IEEE80211R
1728 sm->ft_completed = 0;
1729 #endif /* CONFIG_IEEE80211R */
1730
1731 #ifdef CONFIG_IEEE80211W
1732 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1733 remove_ptk = 0;
1734 #endif /* CONFIG_IEEE80211W */
1735
1736 if (remove_ptk) {
1737 sm->PTK_valid = FALSE;
1738 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1739
1740 if (event != WPA_REAUTH_EAPOL)
1741 wpa_remove_ptk(sm);
1742 }
1743
1744 if (sm->in_step_loop) {
1745 /*
1746 * wpa_sm_step() is already running - avoid recursive call to
1747 * it by making the existing loop process the new update.
1748 */
1749 sm->changed = TRUE;
1750 return 0;
1751 }
1752 return wpa_sm_step(sm);
1753 }
1754
1755
1756 SM_STATE(WPA_PTK, INITIALIZE)
1757 {
1758 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1759 if (sm->Init) {
1760 /* Init flag is not cleared here, so avoid busy
1761 * loop by claiming nothing changed. */
1762 sm->changed = FALSE;
1763 }
1764
1765 sm->keycount = 0;
1766 if (sm->GUpdateStationKeys)
1767 sm->group->GKeyDoneStations--;
1768 sm->GUpdateStationKeys = FALSE;
1769 if (sm->wpa == WPA_VERSION_WPA)
1770 sm->PInitAKeys = FALSE;
1771 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1772 * Local AA > Remote AA)) */) {
1773 sm->Pair = TRUE;
1774 }
1775 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1776 wpa_remove_ptk(sm);
1777 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1778 sm->TimeoutCtr = 0;
1779 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1780 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1781 WPA_EAPOL_authorized, 0);
1782 }
1783 }
1784
1785
1786 SM_STATE(WPA_PTK, DISCONNECT)
1787 {
1788 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1789 sm->Disconnect = FALSE;
1790 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1791 }
1792
1793
1794 SM_STATE(WPA_PTK, DISCONNECTED)
1795 {
1796 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1797 sm->DeauthenticationRequest = FALSE;
1798 }
1799
1800
1801 SM_STATE(WPA_PTK, AUTHENTICATION)
1802 {
1803 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1804 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1805 sm->PTK_valid = FALSE;
1806 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1807 1);
1808 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1809 sm->AuthenticationRequest = FALSE;
1810 }
1811
1812
1813 static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
1814 struct wpa_group *group)
1815 {
1816 if (group->first_sta_seen)
1817 return;
1818 /*
1819 * System has run bit further than at the time hostapd was started
1820 * potentially very early during boot up. This provides better chances
1821 * of collecting more randomness on embedded systems. Re-initialize the
1822 * GMK and Counter here to improve their strength if there was not
1823 * enough entropy available immediately after system startup.
1824 */
1825 wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
1826 "station");
1827 if (random_pool_ready() != 1) {
1828 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
1829 "to proceed - reject first 4-way handshake");
1830 group->reject_4way_hs_for_entropy = TRUE;
1831 } else {
1832 group->first_sta_seen = TRUE;
1833 group->reject_4way_hs_for_entropy = FALSE;
1834 }
1835
1836 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0 ||
1837 wpa_gtk_update(wpa_auth, group) < 0 ||
1838 wpa_group_config_group_keys(wpa_auth, group) < 0) {
1839 wpa_printf(MSG_INFO, "WPA: GMK/GTK setup failed");
1840 group->first_sta_seen = FALSE;
1841 group->reject_4way_hs_for_entropy = TRUE;
1842 }
1843 }
1844
1845
1846 SM_STATE(WPA_PTK, AUTHENTICATION2)
1847 {
1848 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1849
1850 wpa_group_ensure_init(sm->wpa_auth, sm->group);
1851 sm->ReAuthenticationRequest = FALSE;
1852
1853 /*
1854 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1855 * ambiguous. The Authenticator state machine uses a counter that is
1856 * incremented by one for each 4-way handshake. However, the security
1857 * analysis of 4-way handshake points out that unpredictable nonces
1858 * help in preventing precomputation attacks. Instead of the state
1859 * machine definition, use an unpredictable nonce value here to provide
1860 * stronger protection against potential precomputation attacks.
1861 */
1862 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1863 wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
1864 "ANonce.");
1865 sm->Disconnect = TRUE;
1866 return;
1867 }
1868 wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
1869 WPA_NONCE_LEN);
1870 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1871 * logical place than INITIALIZE since AUTHENTICATION2 can be
1872 * re-entered on ReAuthenticationRequest without going through
1873 * INITIALIZE. */
1874 sm->TimeoutCtr = 0;
1875 }
1876
1877
1878 SM_STATE(WPA_PTK, INITPMK)
1879 {
1880 u8 msk[2 * PMK_LEN];
1881 size_t len = 2 * PMK_LEN;
1882
1883 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1884 #ifdef CONFIG_IEEE80211R
1885 sm->xxkey_len = 0;
1886 #endif /* CONFIG_IEEE80211R */
1887 if (sm->pmksa) {
1888 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1889 os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
1890 sm->pmk_len = sm->pmksa->pmk_len;
1891 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1892 unsigned int pmk_len;
1893
1894 if (sm->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
1895 pmk_len = PMK_LEN_SUITE_B_192;
1896 else
1897 pmk_len = PMK_LEN;
1898 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1899 "(MSK len=%lu PMK len=%u)", (unsigned long) len,
1900 pmk_len);
1901 if (len < pmk_len) {
1902 wpa_printf(MSG_DEBUG,
1903 "WPA: MSK not long enough (%u) to create PMK (%u)",
1904 (unsigned int) len, (unsigned int) pmk_len);
1905 sm->Disconnect = TRUE;
1906 return;
1907 }
1908 os_memcpy(sm->PMK, msk, pmk_len);
1909 sm->pmk_len = pmk_len;
1910 #ifdef CONFIG_IEEE80211R
1911 if (len >= 2 * PMK_LEN) {
1912 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1913 sm->xxkey_len = PMK_LEN;
1914 }
1915 #endif /* CONFIG_IEEE80211R */
1916 } else {
1917 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK, get_msk: %p",
1918 sm->wpa_auth->cb.get_msk);
1919 sm->Disconnect = TRUE;
1920 return;
1921 }
1922 os_memset(msk, 0, sizeof(msk));
1923
1924 sm->req_replay_counter_used = 0;
1925 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1926 * will break reauthentication since EAPOL state machines may not be
1927 * get into AUTHENTICATING state that clears keyRun before WPA state
1928 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1929 * state and takes PMK from the previously used AAA Key. This will
1930 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1931 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1932 * be good workaround for this issue. */
1933 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1934 }
1935
1936
1937 SM_STATE(WPA_PTK, INITPSK)
1938 {
1939 const u8 *psk;
1940 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
1941 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL);
1942 if (psk) {
1943 os_memcpy(sm->PMK, psk, PMK_LEN);
1944 sm->pmk_len = PMK_LEN;
1945 #ifdef CONFIG_IEEE80211R
1946 os_memcpy(sm->xxkey, psk, PMK_LEN);
1947 sm->xxkey_len = PMK_LEN;
1948 #endif /* CONFIG_IEEE80211R */
1949 }
1950 sm->req_replay_counter_used = 0;
1951 }
1952
1953
1954 SM_STATE(WPA_PTK, PTKSTART)
1955 {
1956 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1957 size_t pmkid_len = 0;
1958
1959 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
1960 sm->PTKRequest = FALSE;
1961 sm->TimeoutEvt = FALSE;
1962 sm->alt_snonce_valid = FALSE;
1963
1964 sm->TimeoutCtr++;
1965 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1966 /* No point in sending the EAPOL-Key - we will disconnect
1967 * immediately following this. */
1968 return;
1969 }
1970
1971 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1972 "sending 1/4 msg of 4-Way Handshake");
1973 /*
1974 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1975 * one possible PSK for this STA.
1976 */
1977 if (sm->wpa == WPA_VERSION_WPA2 &&
1978 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
1979 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
1980 pmkid = buf;
1981 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
1982 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
1983 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
1984 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
1985 if (sm->pmksa) {
1986 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
1987 sm->pmksa->pmkid, PMKID_LEN);
1988 } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) {
1989 /* No KCK available to derive PMKID */
1990 pmkid = NULL;
1991 } else {
1992 /*
1993 * Calculate PMKID since no PMKSA cache entry was
1994 * available with pre-calculated PMKID.
1995 */
1996 rsn_pmkid(sm->PMK, sm->pmk_len, sm->wpa_auth->addr,
1997 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
1998 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1999 }
2000 }
2001 wpa_send_eapol(sm->wpa_auth, sm,
2002 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
2003 sm->ANonce, pmkid, pmkid_len, 0, 0);
2004 }
2005
2006
2007 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
2008 const u8 *pmk, unsigned int pmk_len,
2009 struct wpa_ptk *ptk)
2010 {
2011 #ifdef CONFIG_IEEE80211R
2012 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
2013 return wpa_auth_derive_ptk_ft(sm, pmk, ptk);
2014 #endif /* CONFIG_IEEE80211R */
2015
2016 return wpa_pmk_to_ptk(pmk, pmk_len, "Pairwise key expansion",
2017 sm->wpa_auth->addr, sm->addr, sm->ANonce, snonce,
2018 ptk, sm->wpa_key_mgmt, sm->pairwise);
2019 }
2020
2021
2022 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
2023 {
2024 struct wpa_ptk PTK;
2025 int ok = 0, psk_found = 0;
2026 const u8 *pmk = NULL;
2027 unsigned int pmk_len;
2028
2029 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
2030 sm->EAPOLKeyReceived = FALSE;
2031 sm->update_snonce = FALSE;
2032
2033 /* WPA with IEEE 802.1X: use the derived PMK from EAP
2034 * WPA-PSK: iterate through possible PSKs and select the one matching
2035 * the packet */
2036 for (;;) {
2037 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2038 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
2039 sm->p2p_dev_addr, pmk);
2040 if (pmk == NULL)
2041 break;
2042 psk_found = 1;
2043 pmk_len = PMK_LEN;
2044 } else {
2045 pmk = sm->PMK;
2046 pmk_len = sm->pmk_len;
2047 }
2048
2049 wpa_derive_ptk(sm, sm->SNonce, pmk, pmk_len, &PTK);
2050
2051 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK,
2052 sm->last_rx_eapol_key,
2053 sm->last_rx_eapol_key_len) == 0) {
2054 ok = 1;
2055 break;
2056 }
2057
2058 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
2059 break;
2060 }
2061
2062 if (!ok) {
2063 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2064 "invalid MIC in msg 2/4 of 4-Way Handshake");
2065 if (psk_found)
2066 wpa_auth_psk_failure_report(sm->wpa_auth, sm->addr);
2067 return;
2068 }
2069
2070 #ifdef CONFIG_IEEE80211R
2071 if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2072 /*
2073 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
2074 * with the value we derived.
2075 */
2076 if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name,
2077 WPA_PMK_NAME_LEN) != 0) {
2078 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2079 "PMKR1Name mismatch in FT 4-way "
2080 "handshake");
2081 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
2082 "Supplicant",
2083 sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
2084 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
2085 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
2086 return;
2087 }
2088 }
2089 #endif /* CONFIG_IEEE80211R */
2090
2091 sm->pending_1_of_4_timeout = 0;
2092 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
2093
2094 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2095 /* PSK may have changed from the previous choice, so update
2096 * state machine data based on whatever PSK was selected here.
2097 */
2098 os_memcpy(sm->PMK, pmk, PMK_LEN);
2099 sm->pmk_len = PMK_LEN;
2100 }
2101
2102 sm->MICVerified = TRUE;
2103
2104 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
2105 sm->PTK_valid = TRUE;
2106 }
2107
2108
2109 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
2110 {
2111 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
2112 sm->TimeoutCtr = 0;
2113 }
2114
2115
2116 #ifdef CONFIG_IEEE80211W
2117
2118 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2119 {
2120 if (sm->mgmt_frame_prot) {
2121 size_t len;
2122 len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2123 return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len;
2124 }
2125
2126 return 0;
2127 }
2128
2129
2130 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2131 {
2132 struct wpa_igtk_kde igtk;
2133 struct wpa_group *gsm = sm->group;
2134 u8 rsc[WPA_KEY_RSC_LEN];
2135 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2136
2137 if (!sm->mgmt_frame_prot)
2138 return pos;
2139
2140 igtk.keyid[0] = gsm->GN_igtk;
2141 igtk.keyid[1] = 0;
2142 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
2143 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
2144 os_memset(igtk.pn, 0, sizeof(igtk.pn));
2145 else
2146 os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
2147 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
2148 if (sm->wpa_auth->conf.disable_gtk) {
2149 /*
2150 * Provide unique random IGTK to each STA to prevent use of
2151 * IGTK in the BSS.
2152 */
2153 if (random_get_bytes(igtk.igtk, len) < 0)
2154 return pos;
2155 }
2156 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
2157 (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
2158 NULL, 0);
2159
2160 return pos;
2161 }
2162
2163 #else /* CONFIG_IEEE80211W */
2164
2165 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2166 {
2167 return 0;
2168 }
2169
2170
2171 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2172 {
2173 return pos;
2174 }
2175
2176 #endif /* CONFIG_IEEE80211W */
2177
2178
2179 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
2180 {
2181 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
2182 size_t gtk_len, kde_len;
2183 struct wpa_group *gsm = sm->group;
2184 u8 *wpa_ie;
2185 int wpa_ie_len, secure, keyidx, encr = 0;
2186
2187 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
2188 sm->TimeoutEvt = FALSE;
2189
2190 sm->TimeoutCtr++;
2191 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
2192 /* No point in sending the EAPOL-Key - we will disconnect
2193 * immediately following this. */
2194 return;
2195 }
2196
2197 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
2198 GTK[GN], IGTK, [FTIE], [TIE * 2])
2199 */
2200 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2201 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2202 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
2203 wpa_ie = sm->wpa_auth->wpa_ie;
2204 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
2205 if (sm->wpa == WPA_VERSION_WPA &&
2206 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
2207 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
2208 /* WPA-only STA, remove RSN IE and possible MDIE */
2209 wpa_ie = wpa_ie + wpa_ie[1] + 2;
2210 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
2211 wpa_ie = wpa_ie + wpa_ie[1] + 2;
2212 wpa_ie_len = wpa_ie[1] + 2;
2213 }
2214 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2215 "sending 3/4 msg of 4-Way Handshake");
2216 if (sm->wpa == WPA_VERSION_WPA2) {
2217 /* WPA2 send GTK in the 4-way handshake */
2218 secure = 1;
2219 gtk = gsm->GTK[gsm->GN - 1];
2220 gtk_len = gsm->GTK_len;
2221 if (sm->wpa_auth->conf.disable_gtk) {
2222 /*
2223 * Provide unique random GTK to each STA to prevent use
2224 * of GTK in the BSS.
2225 */
2226 if (random_get_bytes(dummy_gtk, gtk_len) < 0)
2227 return;
2228 gtk = dummy_gtk;
2229 }
2230 keyidx = gsm->GN;
2231 _rsc = rsc;
2232 encr = 1;
2233 } else {
2234 /* WPA does not include GTK in msg 3/4 */
2235 secure = 0;
2236 gtk = NULL;
2237 gtk_len = 0;
2238 keyidx = 0;
2239 _rsc = NULL;
2240 if (sm->rx_eapol_key_secure) {
2241 /*
2242 * It looks like Windows 7 supplicant tries to use
2243 * Secure bit in msg 2/4 after having reported Michael
2244 * MIC failure and it then rejects the 4-way handshake
2245 * if msg 3/4 does not set Secure bit. Work around this
2246 * by setting the Secure bit here even in the case of
2247 * WPA if the supplicant used it first.
2248 */
2249 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2250 "STA used Secure bit in WPA msg 2/4 - "
2251 "set Secure for 3/4 as workaround");
2252 secure = 1;
2253 }
2254 }
2255
2256 kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
2257 if (gtk)
2258 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
2259 #ifdef CONFIG_IEEE80211R
2260 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2261 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
2262 kde_len += 300; /* FTIE + 2 * TIE */
2263 }
2264 #endif /* CONFIG_IEEE80211R */
2265 #ifdef CONFIG_P2P
2266 if (WPA_GET_BE32(sm->ip_addr) > 0)
2267 kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
2268 #endif /* CONFIG_P2P */
2269 kde = os_malloc(kde_len);
2270 if (kde == NULL)
2271 return;
2272
2273 pos = kde;
2274 os_memcpy(pos, wpa_ie, wpa_ie_len);
2275 pos += wpa_ie_len;
2276 #ifdef CONFIG_IEEE80211R
2277 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2278 int res;
2279 size_t elen;
2280
2281 elen = pos - kde;
2282 res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
2283 if (res < 0) {
2284 wpa_printf(MSG_ERROR, "FT: Failed to insert "
2285 "PMKR1Name into RSN IE in EAPOL-Key data");
2286 os_free(kde);
2287 return;
2288 }
2289 pos -= wpa_ie_len;
2290 pos += elen;
2291 }
2292 #endif /* CONFIG_IEEE80211R */
2293 if (gtk) {
2294 u8 hdr[2];
2295 hdr[0] = keyidx & 0x03;
2296 hdr[1] = 0;
2297 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2298 gtk, gtk_len);
2299 }
2300 pos = ieee80211w_kde_add(sm, pos);
2301
2302 #ifdef CONFIG_IEEE80211R
2303 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2304 int res;
2305 struct wpa_auth_config *conf;
2306
2307 conf = &sm->wpa_auth->conf;
2308 if (sm->assoc_resp_ftie &&
2309 kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
2310 os_memcpy(pos, sm->assoc_resp_ftie,
2311 2 + sm->assoc_resp_ftie[1]);
2312 res = 2 + sm->assoc_resp_ftie[1];
2313 } else {
2314 res = wpa_write_ftie(conf, conf->r0_key_holder,
2315 conf->r0_key_holder_len,
2316 NULL, NULL, pos,
2317 kde + kde_len - pos,
2318 NULL, 0);
2319 }
2320 if (res < 0) {
2321 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
2322 "into EAPOL-Key Key Data");
2323 os_free(kde);
2324 return;
2325 }
2326 pos += res;
2327
2328 /* TIE[ReassociationDeadline] (TU) */
2329 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2330 *pos++ = 5;
2331 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
2332 WPA_PUT_LE32(pos, conf->reassociation_deadline);
2333 pos += 4;
2334
2335 /* TIE[KeyLifetime] (seconds) */
2336 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2337 *pos++ = 5;
2338 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
2339 WPA_PUT_LE32(pos, conf->r0_key_lifetime * 60);
2340 pos += 4;
2341 }
2342 #endif /* CONFIG_IEEE80211R */
2343 #ifdef CONFIG_P2P
2344 if (WPA_GET_BE32(sm->ip_addr) > 0) {
2345 u8 addr[3 * 4];
2346 os_memcpy(addr, sm->ip_addr, 4);
2347 os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
2348 os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
2349 pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
2350 addr, sizeof(addr), NULL, 0);
2351 }
2352 #endif /* CONFIG_P2P */
2353
2354 wpa_send_eapol(sm->wpa_auth, sm,
2355 (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
2356 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
2357 WPA_KEY_INFO_KEY_TYPE,
2358 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
2359 os_free(kde);
2360 }
2361
2362
2363 SM_STATE(WPA_PTK, PTKINITDONE)
2364 {
2365 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
2366 sm->EAPOLKeyReceived = FALSE;
2367 if (sm->Pair) {
2368 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
2369 int klen = wpa_cipher_key_len(sm->pairwise);
2370 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
2371 sm->PTK.tk, klen)) {
2372 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
2373 return;
2374 }
2375 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
2376 sm->pairwise_set = TRUE;
2377
2378 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
2379 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
2380 eloop_register_timeout(sm->wpa_auth->conf.
2381 wpa_ptk_rekey, 0, wpa_rekey_ptk,
2382 sm->wpa_auth, sm);
2383 }
2384
2385 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2386 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2387 WPA_EAPOL_authorized, 1);
2388 }
2389 }
2390
2391 if (0 /* IBSS == TRUE */) {
2392 sm->keycount++;
2393 if (sm->keycount == 2) {
2394 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2395 WPA_EAPOL_portValid, 1);
2396 }
2397 } else {
2398 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
2399 1);
2400 }
2401 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
2402 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
2403 if (sm->wpa == WPA_VERSION_WPA)
2404 sm->PInitAKeys = TRUE;
2405 else
2406 sm->has_GTK = TRUE;
2407 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2408 "pairwise key handshake completed (%s)",
2409 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2410
2411 #ifdef CONFIG_IEEE80211R
2412 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
2413 #endif /* CONFIG_IEEE80211R */
2414 }
2415
2416
2417 SM_STEP(WPA_PTK)
2418 {
2419 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2420
2421 if (sm->Init)
2422 SM_ENTER(WPA_PTK, INITIALIZE);
2423 else if (sm->Disconnect
2424 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
2425 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
2426 "WPA_PTK: sm->Disconnect");
2427 SM_ENTER(WPA_PTK, DISCONNECT);
2428 }
2429 else if (sm->DeauthenticationRequest)
2430 SM_ENTER(WPA_PTK, DISCONNECTED);
2431 else if (sm->AuthenticationRequest)
2432 SM_ENTER(WPA_PTK, AUTHENTICATION);
2433 else if (sm->ReAuthenticationRequest)
2434 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2435 else if (sm->PTKRequest)
2436 SM_ENTER(WPA_PTK, PTKSTART);
2437 else switch (sm->wpa_ptk_state) {
2438 case WPA_PTK_INITIALIZE:
2439 break;
2440 case WPA_PTK_DISCONNECT:
2441 SM_ENTER(WPA_PTK, DISCONNECTED);
2442 break;
2443 case WPA_PTK_DISCONNECTED:
2444 SM_ENTER(WPA_PTK, INITIALIZE);
2445 break;
2446 case WPA_PTK_AUTHENTICATION:
2447 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2448 break;
2449 case WPA_PTK_AUTHENTICATION2:
2450 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
2451 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2452 WPA_EAPOL_keyRun) > 0)
2453 SM_ENTER(WPA_PTK, INITPMK);
2454 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
2455 /* FIX: && 802.1X::keyRun */)
2456 SM_ENTER(WPA_PTK, INITPSK);
2457 break;
2458 case WPA_PTK_INITPMK:
2459 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2460 WPA_EAPOL_keyAvailable) > 0)
2461 SM_ENTER(WPA_PTK, PTKSTART);
2462 else {
2463 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2464 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2465 "INITPMK - keyAvailable = false");
2466 SM_ENTER(WPA_PTK, DISCONNECT);
2467 }
2468 break;
2469 case WPA_PTK_INITPSK:
2470 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
2471 NULL))
2472 SM_ENTER(WPA_PTK, PTKSTART);
2473 else {
2474 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2475 "no PSK configured for the STA");
2476 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2477 SM_ENTER(WPA_PTK, DISCONNECT);
2478 }
2479 break;
2480 case WPA_PTK_PTKSTART:
2481 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2482 sm->EAPOLKeyPairwise)
2483 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2484 else if (sm->TimeoutCtr >
2485 (int) dot11RSNAConfigPairwiseUpdateCount) {
2486 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2487 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2488 "PTKSTART: Retry limit %d reached",
2489 dot11RSNAConfigPairwiseUpdateCount);
2490 SM_ENTER(WPA_PTK, DISCONNECT);
2491 } else if (sm->TimeoutEvt)
2492 SM_ENTER(WPA_PTK, PTKSTART);
2493 break;
2494 case WPA_PTK_PTKCALCNEGOTIATING:
2495 if (sm->MICVerified)
2496 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
2497 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2498 sm->EAPOLKeyPairwise)
2499 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2500 else if (sm->TimeoutEvt)
2501 SM_ENTER(WPA_PTK, PTKSTART);
2502 break;
2503 case WPA_PTK_PTKCALCNEGOTIATING2:
2504 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2505 break;
2506 case WPA_PTK_PTKINITNEGOTIATING:
2507 if (sm->update_snonce)
2508 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2509 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2510 sm->EAPOLKeyPairwise && sm->MICVerified)
2511 SM_ENTER(WPA_PTK, PTKINITDONE);
2512 else if (sm->TimeoutCtr >
2513 (int) dot11RSNAConfigPairwiseUpdateCount) {
2514 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2515 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2516 "PTKINITNEGOTIATING: Retry limit %d "
2517 "reached",
2518 dot11RSNAConfigPairwiseUpdateCount);
2519 SM_ENTER(WPA_PTK, DISCONNECT);
2520 } else if (sm->TimeoutEvt)
2521 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2522 break;
2523 case WPA_PTK_PTKINITDONE:
2524 break;
2525 }
2526 }
2527
2528
2529 SM_STATE(WPA_PTK_GROUP, IDLE)
2530 {
2531 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
2532 if (sm->Init) {
2533 /* Init flag is not cleared here, so avoid busy
2534 * loop by claiming nothing changed. */
2535 sm->changed = FALSE;
2536 }
2537 sm->GTimeoutCtr = 0;
2538 }
2539
2540
2541 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
2542 {
2543 u8 rsc[WPA_KEY_RSC_LEN];
2544 struct wpa_group *gsm = sm->group;
2545 const u8 *kde;
2546 u8 *kde_buf = NULL, *pos, hdr[2];
2547 size_t kde_len;
2548 u8 *gtk, dummy_gtk[32];
2549
2550 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
2551
2552 sm->GTimeoutCtr++;
2553 if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
2554 /* No point in sending the EAPOL-Key - we will disconnect
2555 * immediately following this. */
2556 return;
2557 }
2558
2559 if (sm->wpa == WPA_VERSION_WPA)
2560 sm->PInitAKeys = FALSE;
2561 sm->TimeoutEvt = FALSE;
2562 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
2563 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2564 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
2565 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2566 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2567 "sending 1/2 msg of Group Key Handshake");
2568
2569 gtk = gsm->GTK[gsm->GN - 1];
2570 if (sm->wpa_auth->conf.disable_gtk) {
2571 /*
2572 * Provide unique random GTK to each STA to prevent use
2573 * of GTK in the BSS.
2574 */
2575 if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
2576 return;
2577 gtk = dummy_gtk;
2578 }
2579 if (sm->wpa == WPA_VERSION_WPA2) {
2580 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
2581 ieee80211w_kde_len(sm);
2582 kde_buf = os_malloc(kde_len);
2583 if (kde_buf == NULL)
2584 return;
2585
2586 kde = pos = kde_buf;
2587 hdr[0] = gsm->GN & 0x03;
2588 hdr[1] = 0;
2589 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2590 gtk, gsm->GTK_len);
2591 pos = ieee80211w_kde_add(sm, pos);
2592 kde_len = pos - kde;
2593 } else {
2594 kde = gtk;
2595 kde_len = gsm->GTK_len;
2596 }
2597
2598 wpa_send_eapol(sm->wpa_auth, sm,
2599 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
2600 WPA_KEY_INFO_ACK |
2601 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
2602 rsc, gsm->GNonce, kde, kde_len, gsm->GN, 1);
2603
2604 os_free(kde_buf);
2605 }
2606
2607
2608 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
2609 {
2610 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
2611 sm->EAPOLKeyReceived = FALSE;
2612 if (sm->GUpdateStationKeys)
2613 sm->group->GKeyDoneStations--;
2614 sm->GUpdateStationKeys = FALSE;
2615 sm->GTimeoutCtr = 0;
2616 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
2617 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2618 "group key handshake completed (%s)",
2619 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2620 sm->has_GTK = TRUE;
2621 }
2622
2623
2624 SM_STATE(WPA_PTK_GROUP, KEYERROR)
2625 {
2626 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
2627 if (sm->GUpdateStationKeys)
2628 sm->group->GKeyDoneStations--;
2629 sm->GUpdateStationKeys = FALSE;
2630 sm->Disconnect = TRUE;
2631 }
2632
2633
2634 SM_STEP(WPA_PTK_GROUP)
2635 {
2636 if (sm->Init || sm->PtkGroupInit) {
2637 SM_ENTER(WPA_PTK_GROUP, IDLE);
2638 sm->PtkGroupInit = FALSE;
2639 } else switch (sm->wpa_ptk_group_state) {
2640 case WPA_PTK_GROUP_IDLE:
2641 if (sm->GUpdateStationKeys ||
2642 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
2643 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2644 break;
2645 case WPA_PTK_GROUP_REKEYNEGOTIATING:
2646 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2647 !sm->EAPOLKeyPairwise && sm->MICVerified)
2648 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
2649 else if (sm->GTimeoutCtr >
2650 (int) dot11RSNAConfigGroupUpdateCount)
2651 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
2652 else if (sm->TimeoutEvt)
2653 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2654 break;
2655 case WPA_PTK_GROUP_KEYERROR:
2656 SM_ENTER(WPA_PTK_GROUP, IDLE);
2657 break;
2658 case WPA_PTK_GROUP_REKEYESTABLISHED:
2659 SM_ENTER(WPA_PTK_GROUP, IDLE);
2660 break;
2661 }
2662 }
2663
2664
2665 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
2666 struct wpa_group *group)
2667 {
2668 int ret = 0;
2669
2670 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
2671 inc_byte_array(group->Counter, WPA_NONCE_LEN);
2672 if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
2673 wpa_auth->addr, group->GNonce,
2674 group->GTK[group->GN - 1], group->GTK_len) < 0)
2675 ret = -1;
2676 wpa_hexdump_key(MSG_DEBUG, "GTK",
2677 group->GTK[group->GN - 1], group->GTK_len);
2678
2679 #ifdef CONFIG_IEEE80211W
2680 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
2681 size_t len;
2682 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
2683 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
2684 inc_byte_array(group->Counter, WPA_NONCE_LEN);
2685 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
2686 wpa_auth->addr, group->GNonce,
2687 group->IGTK[group->GN_igtk - 4], len) < 0)
2688 ret = -1;
2689 wpa_hexdump_key(MSG_DEBUG, "IGTK",
2690 group->IGTK[group->GN_igtk - 4], len);
2691 }
2692 #endif /* CONFIG_IEEE80211W */
2693
2694 return ret;
2695 }
2696
2697
2698 static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
2699 struct wpa_group *group)
2700 {
2701 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2702 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
2703 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
2704 group->wpa_group_state = WPA_GROUP_GTK_INIT;
2705
2706 /* GTK[0..N] = 0 */
2707 os_memset(group->GTK, 0, sizeof(group->GTK));
2708 group->GN = 1;
2709 group->GM = 2;
2710 #ifdef CONFIG_IEEE80211W
2711 group->GN_igtk = 4;
2712 group->GM_igtk = 5;
2713 #endif /* CONFIG_IEEE80211W */
2714 /* GTK[GN] = CalcGTK() */
2715 wpa_gtk_update(wpa_auth, group);
2716 }
2717
2718
2719 static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
2720 {
2721 if (ctx != NULL && ctx != sm->group)
2722 return 0;
2723
2724 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
2725 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2726 "Not in PTKINITDONE; skip Group Key update");
2727 sm->GUpdateStationKeys = FALSE;
2728 return 0;
2729 }
2730 if (sm->GUpdateStationKeys) {
2731 /*
2732 * This should not really happen, so add a debug log entry.
2733 * Since we clear the GKeyDoneStations before the loop, the
2734 * station needs to be counted here anyway.
2735 */
2736 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2737 "GUpdateStationKeys was already set when "
2738 "marking station for GTK rekeying");
2739 }
2740
2741 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
2742 if (sm->is_wnmsleep)
2743 return 0;
2744
2745 sm->group->GKeyDoneStations++;
2746 sm->GUpdateStationKeys = TRUE;
2747
2748 wpa_sm_step(sm);
2749 return 0;
2750 }
2751
2752
2753 #ifdef CONFIG_WNM
2754 /* update GTK when exiting WNM-Sleep Mode */
2755 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
2756 {
2757 if (sm == NULL || sm->is_wnmsleep)
2758 return;
2759
2760 wpa_group_update_sta(sm, NULL);
2761 }
2762
2763
2764 void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
2765 {
2766 if (sm)
2767 sm->is_wnmsleep = !!flag;
2768 }
2769
2770
2771 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
2772 {
2773 struct wpa_group *gsm = sm->group;
2774 u8 *start = pos;
2775
2776 /*
2777 * GTK subelement:
2778 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
2779 * Key[5..32]
2780 */
2781 *pos++ = WNM_SLEEP_SUBELEM_GTK;
2782 *pos++ = 11 + gsm->GTK_len;
2783 /* Key ID in B0-B1 of Key Info */
2784 WPA_PUT_LE16(pos, gsm->GN & 0x03);
2785 pos += 2;
2786 *pos++ = gsm->GTK_len;
2787 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
2788 return 0;
2789 pos += 8;
2790 os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
2791 pos += gsm->GTK_len;
2792
2793 wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
2794 gsm->GN);
2795 wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
2796 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
2797
2798 return pos - start;
2799 }
2800
2801
2802 #ifdef CONFIG_IEEE80211W
2803 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
2804 {
2805 struct wpa_group *gsm = sm->group;
2806 u8 *start = pos;
2807 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2808
2809 /*
2810 * IGTK subelement:
2811 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
2812 */
2813 *pos++ = WNM_SLEEP_SUBELEM_IGTK;
2814 *pos++ = 2 + 6 + len;
2815 WPA_PUT_LE16(pos, gsm->GN_igtk);
2816 pos += 2;
2817 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
2818 return 0;
2819 pos += 6;
2820
2821 os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
2822 pos += len;
2823
2824 wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
2825 gsm->GN_igtk);
2826 wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
2827 gsm->IGTK[gsm->GN_igtk - 4], len);
2828
2829 return pos - start;
2830 }
2831 #endif /* CONFIG_IEEE80211W */
2832 #endif /* CONFIG_WNM */
2833
2834
2835 static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
2836 struct wpa_group *group)
2837 {
2838 int tmp;
2839
2840 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2841 "SETKEYS (VLAN-ID %d)", group->vlan_id);
2842 group->changed = TRUE;
2843 group->wpa_group_state = WPA_GROUP_SETKEYS;
2844 group->GTKReKey = FALSE;
2845 tmp = group->GM;
2846 group->GM = group->GN;
2847 group->GN = tmp;
2848 #ifdef CONFIG_IEEE80211W
2849 tmp = group->GM_igtk;
2850 group->GM_igtk = group->GN_igtk;
2851 group->GN_igtk = tmp;
2852 #endif /* CONFIG_IEEE80211W */
2853 /* "GKeyDoneStations = GNoStations" is done in more robust way by
2854 * counting the STAs that are marked with GUpdateStationKeys instead of
2855 * including all STAs that could be in not-yet-completed state. */
2856 wpa_gtk_update(wpa_auth, group);
2857
2858 if (group->GKeyDoneStations) {
2859 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
2860 "GKeyDoneStations=%d when starting new GTK rekey",
2861 group->GKeyDoneStations);
2862 group->GKeyDoneStations = 0;
2863 }
2864 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
2865 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
2866 group->GKeyDoneStations);
2867 }
2868
2869
2870 static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
2871 struct wpa_group *group)
2872 {
2873 int ret = 0;
2874
2875 if (wpa_auth_set_key(wpa_auth, group->vlan_id,
2876 wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
2877 broadcast_ether_addr, group->GN,
2878 group->GTK[group->GN - 1], group->GTK_len) < 0)
2879 ret = -1;
2880
2881 #ifdef CONFIG_IEEE80211W
2882 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
2883 enum wpa_alg alg;
2884 size_t len;
2885
2886 alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher);
2887 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
2888
2889 if (ret == 0 &&
2890 wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
2891 broadcast_ether_addr, group->GN_igtk,
2892 group->IGTK[group->GN_igtk - 4], len) < 0)
2893 ret = -1;
2894 }
2895 #endif /* CONFIG_IEEE80211W */
2896
2897 return ret;
2898 }
2899
2900
2901 static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
2902 {
2903 if (sm->group == ctx) {
2904 wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
2905 " for discconnection due to fatal failure",
2906 MAC2STR(sm->addr));
2907 sm->Disconnect = TRUE;
2908 }
2909
2910 return 0;
2911 }
2912
2913
2914 static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
2915 struct wpa_group *group)
2916 {
2917 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
2918 group->changed = TRUE;
2919 group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
2920 wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
2921 }
2922
2923
2924 static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
2925 struct wpa_group *group)
2926 {
2927 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2928 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
2929 group->changed = TRUE;
2930 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
2931
2932 if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
2933 wpa_group_fatal_failure(wpa_auth, group);
2934 return -1;
2935 }
2936
2937 return 0;
2938 }
2939
2940
2941 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
2942 struct wpa_group *group)
2943 {
2944 if (group->GInit) {
2945 wpa_group_gtk_init(wpa_auth, group);
2946 } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
2947 /* Do not allow group operations */
2948 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
2949 group->GTKAuthenticator) {
2950 wpa_group_setkeysdone(wpa_auth, group);
2951 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
2952 group->GTKReKey) {
2953 wpa_group_setkeys(wpa_auth, group);
2954 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
2955 if (group->GKeyDoneStations == 0)
2956 wpa_group_setkeysdone(wpa_auth, group);
2957 else if (group->GTKReKey)
2958 wpa_group_setkeys(wpa_auth, group);
2959 }
2960 }
2961
2962
2963 static int wpa_sm_step(struct wpa_state_machine *sm)
2964 {
2965 if (sm == NULL)
2966 return 0;
2967
2968 if (sm->in_step_loop) {
2969 /* This should not happen, but if it does, make sure we do not
2970 * end up freeing the state machine too early by exiting the
2971 * recursive call. */
2972 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
2973 return 0;
2974 }
2975
2976 sm->in_step_loop = 1;
2977 do {
2978 if (sm->pending_deinit)
2979 break;
2980
2981 sm->changed = FALSE;
2982 sm->wpa_auth->group->changed = FALSE;
2983
2984 SM_STEP_RUN(WPA_PTK);
2985 if (sm->pending_deinit)
2986 break;
2987 SM_STEP_RUN(WPA_PTK_GROUP);
2988 if (sm->pending_deinit)
2989 break;
2990 wpa_group_sm_step(sm->wpa_auth, sm->group);
2991 } while (sm->changed || sm->wpa_auth->group->changed);
2992 sm->in_step_loop = 0;
2993
2994 if (sm->pending_deinit) {
2995 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
2996 "machine deinit for " MACSTR, MAC2STR(sm->addr));
2997 wpa_free_sta_sm(sm);
2998 return 1;
2999 }
3000 return 0;
3001 }
3002
3003
3004 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
3005 {
3006 struct wpa_state_machine *sm = eloop_ctx;
3007 wpa_sm_step(sm);
3008 }
3009
3010
3011 void wpa_auth_sm_notify(struct wpa_state_machine *sm)
3012 {
3013 if (sm == NULL)
3014 return;
3015 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
3016 }
3017
3018
3019 void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
3020 {
3021 int tmp, i;
3022 struct wpa_group *group;
3023
3024 if (wpa_auth == NULL)
3025 return;
3026
3027 group = wpa_auth->group;
3028
3029 for (i = 0; i < 2; i++) {
3030 tmp = group->GM;
3031 group->GM = group->GN;
3032 group->GN = tmp;
3033 #ifdef CONFIG_IEEE80211W
3034 tmp = group->GM_igtk;
3035 group->GM_igtk = group->GN_igtk;
3036 group->GN_igtk = tmp;
3037 #endif /* CONFIG_IEEE80211W */
3038 wpa_gtk_update(wpa_auth, group);
3039 wpa_group_config_group_keys(wpa_auth, group);
3040 }
3041 }
3042
3043
3044 static const char * wpa_bool_txt(int val)
3045 {
3046 return val ? "TRUE" : "FALSE";
3047 }
3048
3049
3050 #define RSN_SUITE "%02x-%02x-%02x-%d"
3051 #define RSN_SUITE_ARG(s) \
3052 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
3053
3054 int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
3055 {
3056 int len = 0, ret;
3057 char pmkid_txt[PMKID_LEN * 2 + 1];
3058 #ifdef CONFIG_RSN_PREAUTH
3059 const int preauth = 1;
3060 #else /* CONFIG_RSN_PREAUTH */
3061 const int preauth = 0;
3062 #endif /* CONFIG_RSN_PREAUTH */
3063
3064 if (wpa_auth == NULL)
3065 return len;
3066
3067 ret = os_snprintf(buf + len, buflen - len,
3068 "dot11RSNAOptionImplemented=TRUE\n"
3069 "dot11RSNAPreauthenticationImplemented=%s\n"
3070 "dot11RSNAEnabled=%s\n"
3071 "dot11RSNAPreauthenticationEnabled=%s\n",
3072 wpa_bool_txt(preauth),
3073 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
3074 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
3075 if (os_snprintf_error(buflen - len, ret))
3076 return len;
3077 len += ret;
3078
3079 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
3080 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
3081
3082 ret = os_snprintf(
3083 buf + len, buflen - len,
3084 "dot11RSNAConfigVersion=%u\n"
3085 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
3086 /* FIX: dot11RSNAConfigGroupCipher */
3087 /* FIX: dot11RSNAConfigGroupRekeyMethod */
3088 /* FIX: dot11RSNAConfigGroupRekeyTime */
3089 /* FIX: dot11RSNAConfigGroupRekeyPackets */
3090 "dot11RSNAConfigGroupRekeyStrict=%u\n"
3091 "dot11RSNAConfigGroupUpdateCount=%u\n"
3092 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
3093 "dot11RSNAConfigGroupCipherSize=%u\n"
3094 "dot11RSNAConfigPMKLifetime=%u\n"
3095 "dot11RSNAConfigPMKReauthThreshold=%u\n"
3096 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
3097 "dot11RSNAConfigSATimeout=%u\n"
3098 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
3099 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
3100 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
3101 "dot11RSNAPMKIDUsed=%s\n"
3102 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
3103 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
3104 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
3105 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
3106 "dot11RSNA4WayHandshakeFailures=%u\n"
3107 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
3108 RSN_VERSION,
3109 !!wpa_auth->conf.wpa_strict_rekey,
3110 dot11RSNAConfigGroupUpdateCount,
3111 dot11RSNAConfigPairwiseUpdateCount,
3112 wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
3113 dot11RSNAConfigPMKLifetime,
3114 dot11RSNAConfigPMKReauthThreshold,
3115 dot11RSNAConfigSATimeout,
3116 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
3117 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
3118 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
3119 pmkid_txt,
3120 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
3121 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
3122 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
3123 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
3124 wpa_auth->dot11RSNA4WayHandshakeFailures);
3125 if (os_snprintf_error(buflen - len, ret))
3126 return len;
3127 len += ret;
3128
3129 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
3130 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
3131
3132 /* Private MIB */
3133 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
3134 wpa_auth->group->wpa_group_state);
3135 if (os_snprintf_error(buflen - len, ret))
3136 return len;
3137 len += ret;
3138
3139 return len;
3140 }
3141
3142
3143 int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
3144 {
3145 int len = 0, ret;
3146 u32 pairwise = 0;
3147
3148 if (sm == NULL)
3149 return 0;
3150
3151 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
3152
3153 /* dot11RSNAStatsEntry */
3154
3155 pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
3156 WPA_PROTO_RSN : WPA_PROTO_WPA,
3157 sm->pairwise);
3158 if (pairwise == 0)
3159 return 0;
3160
3161 ret = os_snprintf(
3162 buf + len, buflen - len,
3163 /* TODO: dot11RSNAStatsIndex */
3164 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
3165 "dot11RSNAStatsVersion=1\n"
3166 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
3167 /* TODO: dot11RSNAStatsTKIPICVErrors */
3168 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
3169 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
3170 /* TODO: dot11RSNAStatsCCMPReplays */
3171 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
3172 /* TODO: dot11RSNAStatsTKIPReplays */,
3173 MAC2STR(sm->addr),
3174 RSN_SUITE_ARG(pairwise),
3175 sm->dot11RSNAStatsTKIPLocalMICFailures,
3176 sm->dot11RSNAStatsTKIPRemoteMICFailures);
3177 if (os_snprintf_error(buflen - len, ret))
3178 return len;
3179 len += ret;
3180
3181 /* Private MIB */
3182 ret = os_snprintf(buf + len, buflen - len,
3183 "hostapdWPAPTKState=%d\n"
3184 "hostapdWPAPTKGroupState=%d\n",
3185 sm->wpa_ptk_state,
3186 sm->wpa_ptk_group_state);
3187 if (os_snprintf_error(buflen - len, ret))
3188 return len;
3189 len += ret;
3190
3191 return len;
3192 }
3193
3194
3195 void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
3196 {
3197 if (wpa_auth)
3198 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
3199 }
3200
3201
3202 int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
3203 {
3204 return sm && sm->pairwise_set;
3205 }
3206
3207
3208 int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
3209 {
3210 return sm->pairwise;
3211 }
3212
3213
3214 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
3215 {
3216 if (sm == NULL)
3217 return -1;
3218 return sm->wpa_key_mgmt;
3219 }
3220
3221
3222 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
3223 {
3224 if (sm == NULL)
3225 return 0;
3226 return sm->wpa;
3227 }
3228
3229
3230 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
3231 struct rsn_pmksa_cache_entry *entry)
3232 {
3233 if (sm == NULL || sm->pmksa != entry)
3234 return -1;
3235 sm->pmksa = NULL;
3236 return 0;
3237 }
3238
3239
3240 struct rsn_pmksa_cache_entry *
3241 wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
3242 {
3243 return sm ? sm->pmksa : NULL;
3244 }
3245
3246
3247 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
3248 {
3249 if (sm)
3250 sm->dot11RSNAStatsTKIPLocalMICFailures++;
3251 }
3252
3253
3254 const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
3255 {
3256 if (wpa_auth == NULL)
3257 return NULL;
3258 *len = wpa_auth->wpa_ie_len;
3259 return wpa_auth->wpa_ie;
3260 }
3261
3262
3263 int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
3264 unsigned int pmk_len,
3265 int session_timeout, struct eapol_state_machine *eapol)
3266 {
3267 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
3268 sm->wpa_auth->conf.disable_pmksa_caching)
3269 return -1;
3270
3271 if (sm->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
3272 if (pmk_len > PMK_LEN_SUITE_B_192)
3273 pmk_len = PMK_LEN_SUITE_B_192;
3274 } else if (pmk_len > PMK_LEN) {
3275 pmk_len = PMK_LEN;
3276 }
3277
3278 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, pmk_len, NULL,
3279 sm->PTK.kck, sm->PTK.kck_len,
3280 sm->wpa_auth->addr, sm->addr, session_timeout,
3281 eapol, sm->wpa_key_mgmt))
3282 return 0;
3283
3284 return -1;
3285 }
3286
3287
3288 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
3289 const u8 *pmk, size_t len, const u8 *sta_addr,
3290 int session_timeout,
3291 struct eapol_state_machine *eapol)
3292 {
3293 if (wpa_auth == NULL)
3294 return -1;
3295
3296 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, NULL,
3297 NULL, 0,
3298 wpa_auth->addr,
3299 sta_addr, session_timeout, eapol,
3300 WPA_KEY_MGMT_IEEE8021X))
3301 return 0;
3302
3303 return -1;
3304 }
3305
3306
3307 int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
3308 const u8 *pmk, const u8 *pmkid)
3309 {
3310 if (wpa_auth->conf.disable_pmksa_caching)
3311 return -1;
3312
3313 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN, pmkid,
3314 NULL, 0,
3315 wpa_auth->addr, addr, 0, NULL,
3316 WPA_KEY_MGMT_SAE))
3317 return 0;
3318
3319 return -1;
3320 }
3321
3322
3323 void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
3324 const u8 *sta_addr)
3325 {
3326 struct rsn_pmksa_cache_entry *pmksa;
3327
3328 if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
3329 return;
3330 pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
3331 if (pmksa) {
3332 wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
3333 MACSTR " based on request", MAC2STR(sta_addr));
3334 pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
3335 }
3336 }
3337
3338
3339 int wpa_auth_pmksa_list(struct wpa_authenticator *wpa_auth, char *buf,
3340 size_t len)
3341 {
3342 if (!wpa_auth || !wpa_auth->pmksa)
3343 return 0;
3344 return pmksa_cache_auth_list(wpa_auth->pmksa, buf, len);
3345 }
3346
3347
3348 void wpa_auth_pmksa_flush(struct wpa_authenticator *wpa_auth)
3349 {
3350 if (wpa_auth && wpa_auth->pmksa)
3351 pmksa_cache_auth_flush(wpa_auth->pmksa);
3352 }
3353
3354
3355 struct rsn_pmksa_cache_entry *
3356 wpa_auth_pmksa_get(struct wpa_authenticator *wpa_auth, const u8 *sta_addr)
3357 {
3358 if (!wpa_auth || !wpa_auth->pmksa)
3359 return NULL;
3360 return pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
3361 }
3362
3363
3364 void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry *pmksa,
3365 struct wpa_state_machine *sm,
3366 struct wpa_authenticator *wpa_auth,
3367 u8 *pmkid, u8 *pmk)
3368 {
3369 if (!sm)
3370 return;
3371
3372 sm->pmksa = pmksa;
3373 os_memcpy(pmk, pmksa->pmk, PMK_LEN);
3374 os_memcpy(pmkid, pmksa->pmkid, PMKID_LEN);
3375 os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmksa->pmkid, PMKID_LEN);
3376 }
3377
3378
3379 /*
3380 * Remove and free the group from wpa_authenticator. This is triggered by a
3381 * callback to make sure nobody is currently iterating the group list while it
3382 * gets modified.
3383 */
3384 static void wpa_group_free(struct wpa_authenticator *wpa_auth,
3385 struct wpa_group *group)
3386 {
3387 struct wpa_group *prev = wpa_auth->group;
3388
3389 wpa_printf(MSG_DEBUG, "WPA: Remove group state machine for VLAN-ID %d",
3390 group->vlan_id);
3391
3392 while (prev) {
3393 if (prev->next == group) {
3394 /* This never frees the special first group as needed */
3395 prev->next = group->next;
3396 os_free(group);
3397 break;
3398 }
3399 prev = prev->next;
3400 }
3401
3402 }
3403
3404
3405 /* Increase the reference counter for group */
3406 static void wpa_group_get(struct wpa_authenticator *wpa_auth,
3407 struct wpa_group *group)
3408 {
3409 /* Skip the special first group */
3410 if (wpa_auth->group == group)
3411 return;
3412
3413 group->references++;
3414 }
3415
3416
3417 /* Decrease the reference counter and maybe free the group */
3418 static void wpa_group_put(struct wpa_authenticator *wpa_auth,
3419 struct wpa_group *group)
3420 {
3421 /* Skip the special first group */
3422 if (wpa_auth->group == group)
3423 return;
3424
3425 group->references--;
3426 if (group->references)
3427 return;
3428 wpa_group_free(wpa_auth, group);
3429 }
3430
3431
3432 /*
3433 * Add a group that has its references counter set to zero. Caller needs to
3434 * call wpa_group_get() on the return value to mark the entry in use.
3435 */
3436 static struct wpa_group *
3437 wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
3438 {
3439 struct wpa_group *group;
3440
3441 if (wpa_auth == NULL || wpa_auth->group == NULL)
3442 return NULL;
3443
3444 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
3445 vlan_id);
3446 group = wpa_group_init(wpa_auth, vlan_id, 0);
3447 if (group == NULL)
3448 return NULL;
3449
3450 group->next = wpa_auth->group->next;
3451 wpa_auth->group->next = group;
3452
3453 return group;
3454 }
3455
3456
3457 /*
3458 * Enforce that the group state machine for the VLAN is running, increase
3459 * reference counter as interface is up. References might have been increased
3460 * even if a negative value is returned.
3461 * Returns: -1 on error (group missing, group already failed); otherwise, 0
3462 */
3463 int wpa_auth_ensure_group(struct wpa_authenticator *wpa_auth, int vlan_id)
3464 {
3465 struct wpa_group *group;
3466
3467 if (wpa_auth == NULL)
3468 return 0;
3469
3470 group = wpa_auth->group;
3471 while (group) {
3472 if (group->vlan_id == vlan_id)
3473 break;
3474 group = group->next;
3475 }
3476
3477 if (group == NULL) {
3478 group = wpa_auth_add_group(wpa_auth, vlan_id);
3479 if (group == NULL)
3480 return -1;
3481 }
3482
3483 wpa_printf(MSG_DEBUG,
3484 "WPA: Ensure group state machine running for VLAN ID %d",
3485 vlan_id);
3486
3487 wpa_group_get(wpa_auth, group);
3488 group->num_setup_iface++;
3489
3490 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
3491 return -1;
3492
3493 return 0;
3494 }
3495
3496
3497 /*
3498 * Decrease reference counter, expected to be zero afterwards.
3499 * returns: -1 on error (group not found, group in fail state)
3500 * -2 if wpa_group is still referenced
3501 * 0 else
3502 */
3503 int wpa_auth_release_group(struct wpa_authenticator *wpa_auth, int vlan_id)
3504 {
3505 struct wpa_group *group;
3506 int ret = 0;
3507
3508 if (wpa_auth == NULL)
3509 return 0;
3510
3511 group = wpa_auth->group;
3512 while (group) {
3513 if (group->vlan_id == vlan_id)
3514 break;
3515 group = group->next;
3516 }
3517
3518 if (group == NULL)
3519 return -1;
3520
3521 wpa_printf(MSG_DEBUG,
3522 "WPA: Try stopping group state machine for VLAN ID %d",
3523 vlan_id);
3524
3525 if (group->num_setup_iface <= 0) {
3526 wpa_printf(MSG_ERROR,
3527 "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
3528 vlan_id);
3529 return -1;
3530 }
3531 group->num_setup_iface--;
3532
3533 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
3534 ret = -1;
3535
3536 if (group->references > 1) {
3537 wpa_printf(MSG_DEBUG,
3538 "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
3539 vlan_id);
3540 ret = -2;
3541 }
3542
3543 wpa_group_put(wpa_auth, group);
3544
3545 return ret;
3546 }
3547
3548
3549 int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
3550 {
3551 struct wpa_group *group;
3552
3553 if (sm == NULL || sm->wpa_auth == NULL)
3554 return 0;
3555
3556 group = sm->wpa_auth->group;
3557 while (group) {
3558 if (group->vlan_id == vlan_id)
3559 break;
3560 group = group->next;
3561 }
3562
3563 if (group == NULL) {
3564 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
3565 if (group == NULL)
3566 return -1;
3567 }
3568
3569 if (sm->group == group)
3570 return 0;
3571
3572 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
3573 return -1;
3574
3575 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
3576 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
3577
3578 wpa_group_get(sm->wpa_auth, group);
3579 wpa_group_put(sm->wpa_auth, sm->group);
3580 sm->group = group;
3581
3582 return 0;
3583 }
3584
3585
3586 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
3587 struct wpa_state_machine *sm, int ack)
3588 {
3589 if (wpa_auth == NULL || sm == NULL)
3590 return;
3591 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
3592 " ack=%d", MAC2STR(sm->addr), ack);
3593 if (sm->pending_1_of_4_timeout && ack) {
3594 /*
3595 * Some deployed supplicant implementations update their SNonce
3596 * for each EAPOL-Key 2/4 message even within the same 4-way
3597 * handshake and then fail to use the first SNonce when
3598 * deriving the PTK. This results in unsuccessful 4-way
3599 * handshake whenever the relatively short initial timeout is
3600 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
3601 * around this by increasing the timeout now that we know that
3602 * the station has received the frame.
3603 */
3604 int timeout_ms = eapol_key_timeout_subseq;
3605 wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
3606 "timeout by %u ms because of acknowledged frame",
3607 timeout_ms);
3608 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
3609 eloop_register_timeout(timeout_ms / 1000,
3610 (timeout_ms % 1000) * 1000,
3611 wpa_send_eapol_timeout, wpa_auth, sm);
3612 }
3613 }
3614
3615
3616 int wpa_auth_uses_sae(struct wpa_state_machine *sm)
3617 {
3618 if (sm == NULL)
3619 return 0;
3620 return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
3621 }
3622
3623
3624 int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
3625 {
3626 if (sm == NULL)
3627 return 0;
3628 return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
3629 }
3630
3631
3632 #ifdef CONFIG_P2P
3633 int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
3634 {
3635 if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
3636 return -1;
3637 os_memcpy(addr, sm->ip_addr, 4);
3638 return 0;
3639 }
3640 #endif /* CONFIG_P2P */
3641
3642
3643 int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth,
3644 struct radius_das_attrs *attr)
3645 {
3646 return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr);
3647 }
3648
3649
3650 void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth)
3651 {
3652 struct wpa_group *group;
3653
3654 if (!wpa_auth)
3655 return;
3656 for (group = wpa_auth->group; group; group = group->next)
3657 wpa_group_config_group_keys(wpa_auth, group);
3658 }