hostapd: Update vendor branch to 0.6.10
[dragonfly.git] / contrib / hostapd / hostapd / wpa.c
blob19b11d59d3400171ff9ed9c88cd49ad5daf21538
1 /*
2 * hostapd - IEEE 802.11i-2004 / WPA Authenticator
3 * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #ifndef CONFIG_NATIVE_WINDOWS
19 #include "common.h"
20 #include "config.h"
21 #include "eapol_sm.h"
22 #include "wpa.h"
23 #include "sha1.h"
24 #include "sha256.h"
25 #include "rc4.h"
26 #include "aes_wrap.h"
27 #include "crypto.h"
28 #include "eloop.h"
29 #include "ieee802_11.h"
30 #include "pmksa_cache.h"
31 #include "state_machine.h"
32 #include "wpa_auth_i.h"
33 #include "wpa_auth_ie.h"
35 #define STATE_MACHINE_DATA struct wpa_state_machine
36 #define STATE_MACHINE_DEBUG_PREFIX "WPA"
37 #define STATE_MACHINE_ADDR sm->addr
40 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
41 static void wpa_sm_step(struct wpa_state_machine *sm);
42 static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len);
43 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
44 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
45 struct wpa_group *group);
46 static void wpa_request_new_ptk(struct wpa_state_machine *sm);
47 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
48 struct wpa_group *group);
50 static const u32 dot11RSNAConfigGroupUpdateCount = 4;
51 static const u32 dot11RSNAConfigPairwiseUpdateCount = 4;
52 static const u32 eapol_key_timeout_first = 100; /* ms */
53 static const u32 eapol_key_timeout_subseq = 1000; /* ms */
55 /* TODO: make these configurable */
56 static const int dot11RSNAConfigPMKLifetime = 43200;
57 static const int dot11RSNAConfigPMKReauthThreshold = 70;
58 static const int dot11RSNAConfigSATimeout = 60;
61 static inline void wpa_auth_mic_failure_report(
62 struct wpa_authenticator *wpa_auth, const u8 *addr)
64 if (wpa_auth->cb.mic_failure_report)
65 wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr);
69 static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
70 const u8 *addr, wpa_eapol_variable var,
71 int value)
73 if (wpa_auth->cb.set_eapol)
74 wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value);
78 static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
79 const u8 *addr, wpa_eapol_variable var)
81 if (wpa_auth->cb.get_eapol == NULL)
82 return -1;
83 return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var);
87 static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
88 const u8 *addr, const u8 *prev_psk)
90 if (wpa_auth->cb.get_psk == NULL)
91 return NULL;
92 return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, prev_psk);
96 static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
97 const u8 *addr, u8 *msk, size_t *len)
99 if (wpa_auth->cb.get_msk == NULL)
100 return -1;
101 return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len);
105 static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
106 int vlan_id,
107 const char *alg, const u8 *addr, int idx,
108 u8 *key, size_t key_len)
110 if (wpa_auth->cb.set_key == NULL)
111 return -1;
112 return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
113 key, key_len);
117 static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
118 const u8 *addr, int idx, u8 *seq)
120 if (wpa_auth->cb.get_seqnum == NULL)
121 return -1;
122 return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
126 static inline int wpa_auth_get_seqnum_igtk(struct wpa_authenticator *wpa_auth,
127 const u8 *addr, int idx, u8 *seq)
129 if (wpa_auth->cb.get_seqnum_igtk == NULL)
130 return -1;
131 return wpa_auth->cb.get_seqnum_igtk(wpa_auth->cb.ctx, addr, idx, seq);
135 static inline int
136 wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
137 const u8 *data, size_t data_len, int encrypt)
139 if (wpa_auth->cb.send_eapol == NULL)
140 return -1;
141 return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
142 encrypt);
146 int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
147 int (*cb)(struct wpa_state_machine *sm, void *ctx),
148 void *cb_ctx)
150 if (wpa_auth->cb.for_each_sta == NULL)
151 return 0;
152 return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
156 int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
157 int (*cb)(struct wpa_authenticator *a, void *ctx),
158 void *cb_ctx)
160 if (wpa_auth->cb.for_each_auth == NULL)
161 return 0;
162 return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
166 void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
167 logger_level level, const char *txt)
169 if (wpa_auth->cb.logger == NULL)
170 return;
171 wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
175 void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
176 logger_level level, const char *fmt, ...)
178 char *format;
179 int maxlen;
180 va_list ap;
182 if (wpa_auth->cb.logger == NULL)
183 return;
185 maxlen = os_strlen(fmt) + 100;
186 format = os_malloc(maxlen);
187 if (!format)
188 return;
190 va_start(ap, fmt);
191 vsnprintf(format, maxlen, fmt, ap);
192 va_end(ap);
194 wpa_auth_logger(wpa_auth, addr, level, format);
196 os_free(format);
200 static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
201 const u8 *addr)
203 if (wpa_auth->cb.disconnect == NULL)
204 return;
205 wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
206 WLAN_REASON_PREV_AUTH_NOT_VALID);
210 static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
212 int ret = 0;
213 #ifdef CONFIG_IEEE80211R
214 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
215 ret = 1;
216 #endif /* CONFIG_IEEE80211R */
217 #ifdef CONFIG_IEEE80211W
218 if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
219 ret = 1;
220 #endif /* CONFIG_IEEE80211W */
221 return ret;
225 static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
227 struct wpa_authenticator *wpa_auth = eloop_ctx;
229 if (os_get_random(wpa_auth->group->GMK, WPA_GMK_LEN)) {
230 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
231 "initialization.");
232 } else {
233 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
236 if (wpa_auth->conf.wpa_gmk_rekey) {
237 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
238 wpa_rekey_gmk, wpa_auth, NULL);
243 static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
245 struct wpa_authenticator *wpa_auth = eloop_ctx;
246 struct wpa_group *group;
248 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
249 for (group = wpa_auth->group; group; group = group->next) {
250 group->GTKReKey = TRUE;
251 do {
252 group->changed = FALSE;
253 wpa_group_sm_step(wpa_auth, group);
254 } while (group->changed);
257 if (wpa_auth->conf.wpa_group_rekey) {
258 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
259 0, wpa_rekey_gtk, wpa_auth, NULL);
264 static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
266 struct wpa_authenticator *wpa_auth = eloop_ctx;
267 struct wpa_state_machine *sm = timeout_ctx;
269 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
270 wpa_request_new_ptk(sm);
271 wpa_sm_step(sm);
275 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
277 if (sm->pmksa == ctx)
278 sm->pmksa = NULL;
279 return 0;
283 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
284 void *ctx)
286 struct wpa_authenticator *wpa_auth = ctx;
287 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
291 static void wpa_group_set_key_len(struct wpa_group *group, int cipher)
293 switch (cipher) {
294 case WPA_CIPHER_CCMP:
295 group->GTK_len = 16;
296 break;
297 case WPA_CIPHER_TKIP:
298 group->GTK_len = 32;
299 break;
300 case WPA_CIPHER_WEP104:
301 group->GTK_len = 13;
302 break;
303 case WPA_CIPHER_WEP40:
304 group->GTK_len = 5;
305 break;
310 static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
311 int vlan_id)
313 struct wpa_group *group;
314 u8 buf[ETH_ALEN + 8 + sizeof(group)];
315 u8 rkey[32];
317 group = os_zalloc(sizeof(struct wpa_group));
318 if (group == NULL)
319 return NULL;
321 group->GTKAuthenticator = TRUE;
322 group->vlan_id = vlan_id;
324 wpa_group_set_key_len(group, wpa_auth->conf.wpa_group);
326 /* Counter = PRF-256(Random number, "Init Counter",
327 * Local MAC Address || Time)
329 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
330 wpa_get_ntp_timestamp(buf + ETH_ALEN);
331 os_memcpy(buf + ETH_ALEN + 8, &group, sizeof(group));
332 if (os_get_random(rkey, sizeof(rkey)) ||
333 os_get_random(group->GMK, WPA_GMK_LEN)) {
334 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
335 "initialization.");
336 os_free(group);
337 return NULL;
340 sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
341 group->Counter, WPA_NONCE_LEN);
343 group->GInit = TRUE;
344 wpa_group_sm_step(wpa_auth, group);
345 group->GInit = FALSE;
346 wpa_group_sm_step(wpa_auth, group);
348 return group;
353 * wpa_init - Initialize WPA authenticator
354 * @addr: Authenticator address
355 * @conf: Configuration for WPA authenticator
356 * @cb: Callback functions for WPA authenticator
357 * Returns: Pointer to WPA authenticator data or %NULL on failure
359 struct wpa_authenticator * wpa_init(const u8 *addr,
360 struct wpa_auth_config *conf,
361 struct wpa_auth_callbacks *cb)
363 struct wpa_authenticator *wpa_auth;
365 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
366 if (wpa_auth == NULL)
367 return NULL;
368 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
369 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
370 os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
372 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
373 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
374 os_free(wpa_auth);
375 return NULL;
378 wpa_auth->group = wpa_group_init(wpa_auth, 0);
379 if (wpa_auth->group == NULL) {
380 os_free(wpa_auth->wpa_ie);
381 os_free(wpa_auth);
382 return NULL;
385 wpa_auth->pmksa = pmksa_cache_init(wpa_auth_pmksa_free_cb, wpa_auth);
386 if (wpa_auth->pmksa == NULL) {
387 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
388 os_free(wpa_auth->wpa_ie);
389 os_free(wpa_auth);
390 return NULL;
393 #ifdef CONFIG_IEEE80211R
394 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
395 if (wpa_auth->ft_pmk_cache == NULL) {
396 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
397 os_free(wpa_auth->wpa_ie);
398 pmksa_cache_deinit(wpa_auth->pmksa);
399 os_free(wpa_auth);
400 return NULL;
402 #endif /* CONFIG_IEEE80211R */
404 if (wpa_auth->conf.wpa_gmk_rekey) {
405 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
406 wpa_rekey_gmk, wpa_auth, NULL);
409 if (wpa_auth->conf.wpa_group_rekey) {
410 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
411 wpa_rekey_gtk, wpa_auth, NULL);
414 return wpa_auth;
419 * wpa_deinit - Deinitialize WPA authenticator
420 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
422 void wpa_deinit(struct wpa_authenticator *wpa_auth)
424 struct wpa_group *group, *prev;
426 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
427 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
429 #ifdef CONFIG_PEERKEY
430 while (wpa_auth->stsl_negotiations)
431 wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
432 #endif /* CONFIG_PEERKEY */
434 pmksa_cache_deinit(wpa_auth->pmksa);
436 #ifdef CONFIG_IEEE80211R
437 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
438 wpa_auth->ft_pmk_cache = NULL;
439 #endif /* CONFIG_IEEE80211R */
441 os_free(wpa_auth->wpa_ie);
443 group = wpa_auth->group;
444 while (group) {
445 prev = group;
446 group = group->next;
447 os_free(prev);
450 os_free(wpa_auth);
455 * wpa_reconfig - Update WPA authenticator configuration
456 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
457 * @conf: Configuration for WPA authenticator
459 int wpa_reconfig(struct wpa_authenticator *wpa_auth,
460 struct wpa_auth_config *conf)
462 struct wpa_group *group;
463 if (wpa_auth == NULL)
464 return 0;
466 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
467 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
468 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
469 return -1;
473 * Reinitialize GTK to make sure it is suitable for the new
474 * configuration.
476 group = wpa_auth->group;
477 wpa_group_set_key_len(group, wpa_auth->conf.wpa_group);
478 group->GInit = TRUE;
479 wpa_group_sm_step(wpa_auth, group);
480 group->GInit = FALSE;
481 wpa_group_sm_step(wpa_auth, group);
483 return 0;
487 struct wpa_state_machine *
488 wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr)
490 struct wpa_state_machine *sm;
492 sm = os_zalloc(sizeof(struct wpa_state_machine));
493 if (sm == NULL)
494 return NULL;
495 os_memcpy(sm->addr, addr, ETH_ALEN);
497 sm->wpa_auth = wpa_auth;
498 sm->group = wpa_auth->group;
500 return sm;
504 void wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
505 struct wpa_state_machine *sm)
507 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
508 return;
510 #ifdef CONFIG_IEEE80211R
511 if (sm->ft_completed) {
512 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
513 "FT authentication already completed - do not "
514 "start 4-way handshake");
515 return;
517 #endif /* CONFIG_IEEE80211R */
519 if (sm->started) {
520 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
521 sm->ReAuthenticationRequest = TRUE;
522 wpa_sm_step(sm);
523 return;
526 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
527 "start authentication");
528 sm->started = 1;
530 sm->Init = TRUE;
531 wpa_sm_step(sm);
532 sm->Init = FALSE;
533 sm->AuthenticationRequest = TRUE;
534 wpa_sm_step(sm);
538 void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
540 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
541 * reassociates back to the same AP while the previous entry for the
542 * STA has not yet been removed. */
543 if (sm == NULL)
544 return;
546 sm->wpa_key_mgmt = 0;
550 static void wpa_free_sta_sm(struct wpa_state_machine *sm)
552 os_free(sm->last_rx_eapol_key);
553 os_free(sm->wpa_ie);
554 os_free(sm);
558 void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
560 if (sm == NULL)
561 return;
563 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
564 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
565 "strict rekeying - force GTK rekey since STA "
566 "is leaving");
567 eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
568 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
569 NULL);
572 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
573 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
574 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
575 if (sm->in_step_loop) {
576 /* Must not free state machine while wpa_sm_step() is running.
577 * Freeing will be completed in the end of wpa_sm_step(). */
578 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
579 "machine deinit for " MACSTR, MAC2STR(sm->addr));
580 sm->pending_deinit = 1;
581 } else
582 wpa_free_sta_sm(sm);
586 static void wpa_request_new_ptk(struct wpa_state_machine *sm)
588 if (sm == NULL)
589 return;
591 sm->PTKRequest = TRUE;
592 sm->PTK_valid = 0;
596 static int wpa_replay_counter_valid(struct wpa_state_machine *sm,
597 const u8 *replay_counter)
599 int i;
600 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
601 if (!sm->key_replay[i].valid)
602 break;
603 if (os_memcmp(replay_counter, sm->key_replay[i].counter,
604 WPA_REPLAY_COUNTER_LEN) == 0)
605 return 1;
607 return 0;
611 void wpa_receive(struct wpa_authenticator *wpa_auth,
612 struct wpa_state_machine *sm,
613 u8 *data, size_t data_len)
615 struct ieee802_1x_hdr *hdr;
616 struct wpa_eapol_key *key;
617 u16 key_info, key_data_length;
618 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
619 SMK_M1, SMK_M3, SMK_ERROR } msg;
620 char *msgtxt;
621 struct wpa_eapol_ie_parse kde;
623 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
624 return;
626 if (data_len < sizeof(*hdr) + sizeof(*key))
627 return;
629 hdr = (struct ieee802_1x_hdr *) data;
630 key = (struct wpa_eapol_key *) (hdr + 1);
631 key_info = WPA_GET_BE16(key->key_info);
632 key_data_length = WPA_GET_BE16(key->key_data_length);
633 if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) {
634 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
635 "key_data overflow (%d > %lu)",
636 key_data_length,
637 (unsigned long) (data_len - sizeof(*hdr) -
638 sizeof(*key)));
639 return;
642 if (sm->wpa == WPA_VERSION_WPA2) {
643 if (key->type != EAPOL_KEY_TYPE_RSN) {
644 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
645 "unexpected type %d in RSN mode",
646 key->type);
647 return;
649 } else {
650 if (key->type != EAPOL_KEY_TYPE_WPA) {
651 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
652 "unexpected type %d in WPA mode",
653 key->type);
654 return;
658 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
659 * are set */
661 if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
662 (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
663 if (key_info & WPA_KEY_INFO_ERROR) {
664 msg = SMK_ERROR;
665 msgtxt = "SMK Error";
666 } else {
667 msg = SMK_M1;
668 msgtxt = "SMK M1";
670 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
671 msg = SMK_M3;
672 msgtxt = "SMK M3";
673 } else if (key_info & WPA_KEY_INFO_REQUEST) {
674 msg = REQUEST;
675 msgtxt = "Request";
676 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
677 msg = GROUP_2;
678 msgtxt = "2/2 Group";
679 } else if (key_data_length == 0) {
680 msg = PAIRWISE_4;
681 msgtxt = "4/4 Pairwise";
682 } else {
683 msg = PAIRWISE_2;
684 msgtxt = "2/4 Pairwise";
687 /* TODO: key_info type validation for PeerKey */
688 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
689 msg == GROUP_2) {
690 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
691 if (sm->pairwise == WPA_CIPHER_CCMP) {
692 if (wpa_use_aes_cmac(sm) &&
693 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
694 wpa_auth_logger(wpa_auth, sm->addr,
695 LOGGER_WARNING,
696 "advertised support for "
697 "AES-128-CMAC, but did not "
698 "use it");
699 return;
702 if (!wpa_use_aes_cmac(sm) &&
703 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
704 wpa_auth_logger(wpa_auth, sm->addr,
705 LOGGER_WARNING,
706 "did not use HMAC-SHA1-AES "
707 "with CCMP");
708 return;
713 if (key_info & WPA_KEY_INFO_REQUEST) {
714 if (sm->req_replay_counter_used &&
715 os_memcmp(key->replay_counter, sm->req_replay_counter,
716 WPA_REPLAY_COUNTER_LEN) <= 0) {
717 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
718 "received EAPOL-Key request with "
719 "replayed counter");
720 return;
724 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
725 !wpa_replay_counter_valid(sm, key->replay_counter)) {
726 int i;
727 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
728 "received EAPOL-Key %s with unexpected "
729 "replay counter", msgtxt);
730 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
731 if (!sm->key_replay[i].valid)
732 break;
733 wpa_hexdump(MSG_DEBUG, "pending replay counter",
734 sm->key_replay[i].counter,
735 WPA_REPLAY_COUNTER_LEN);
737 wpa_hexdump(MSG_DEBUG, "received replay counter",
738 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
739 return;
742 switch (msg) {
743 case PAIRWISE_2:
744 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
745 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING) {
746 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
747 "received EAPOL-Key msg 2/4 in "
748 "invalid state (%d) - dropped",
749 sm->wpa_ptk_state);
750 return;
752 if (sm->wpa_ie == NULL ||
753 sm->wpa_ie_len != key_data_length ||
754 os_memcmp(sm->wpa_ie, key + 1, key_data_length) != 0) {
755 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
756 "WPA IE from (Re)AssocReq did not "
757 "match with msg 2/4");
758 if (sm->wpa_ie) {
759 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
760 sm->wpa_ie, sm->wpa_ie_len);
762 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
763 (u8 *) (key + 1), key_data_length);
764 /* MLME-DEAUTHENTICATE.request */
765 wpa_sta_disconnect(wpa_auth, sm->addr);
766 return;
768 break;
769 case PAIRWISE_4:
770 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
771 !sm->PTK_valid) {
772 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
773 "received EAPOL-Key msg 4/4 in "
774 "invalid state (%d) - dropped",
775 sm->wpa_ptk_state);
776 return;
778 break;
779 case GROUP_2:
780 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
781 || !sm->PTK_valid) {
782 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
783 "received EAPOL-Key msg 2/2 in "
784 "invalid state (%d) - dropped",
785 sm->wpa_ptk_group_state);
786 return;
788 break;
789 #ifdef CONFIG_PEERKEY
790 case SMK_M1:
791 case SMK_M3:
792 case SMK_ERROR:
793 if (!wpa_auth->conf.peerkey) {
794 wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
795 "PeerKey use disabled - ignoring message");
796 return;
798 if (!sm->PTK_valid) {
799 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
800 "received EAPOL-Key msg SMK in "
801 "invalid state - dropped");
802 return;
804 break;
805 #else /* CONFIG_PEERKEY */
806 case SMK_M1:
807 case SMK_M3:
808 case SMK_ERROR:
809 return; /* STSL disabled - ignore SMK messages */
810 #endif /* CONFIG_PEERKEY */
811 case REQUEST:
812 break;
815 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
816 "received EAPOL-Key frame (%s)", msgtxt);
818 if (key_info & WPA_KEY_INFO_ACK) {
819 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
820 "received invalid EAPOL-Key: Key Ack set");
821 return;
824 if (!(key_info & WPA_KEY_INFO_MIC)) {
825 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
826 "received invalid EAPOL-Key: Key MIC not set");
827 return;
830 sm->MICVerified = FALSE;
831 if (sm->PTK_valid) {
832 if (wpa_verify_key_mic(&sm->PTK, data, data_len)) {
833 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
834 "received EAPOL-Key with invalid MIC");
835 return;
837 sm->MICVerified = TRUE;
838 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
841 if (key_info & WPA_KEY_INFO_REQUEST) {
842 if (sm->MICVerified) {
843 sm->req_replay_counter_used = 1;
844 os_memcpy(sm->req_replay_counter, key->replay_counter,
845 WPA_REPLAY_COUNTER_LEN);
846 } else {
847 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
848 "received EAPOL-Key request with "
849 "invalid MIC");
850 return;
854 * TODO: should decrypt key data field if encryption was used;
855 * even though MAC address KDE is not normally encrypted,
856 * supplicant is allowed to encrypt it.
858 if (msg == SMK_ERROR) {
859 #ifdef CONFIG_PEERKEY
860 wpa_smk_error(wpa_auth, sm, key);
861 #endif /* CONFIG_PEERKEY */
862 return;
863 } else if (key_info & WPA_KEY_INFO_ERROR) {
864 /* Supplicant reported a Michael MIC error */
865 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
866 "received EAPOL-Key Error Request "
867 "(STA detected Michael MIC failure)");
868 wpa_auth_mic_failure_report(wpa_auth, sm->addr);
869 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
870 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
871 /* Error report is not a request for a new key
872 * handshake, but since Authenticator may do it, let's
873 * change the keys now anyway. */
874 wpa_request_new_ptk(sm);
875 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
876 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
877 "received EAPOL-Key Request for new "
878 "4-Way Handshake");
879 wpa_request_new_ptk(sm);
880 #ifdef CONFIG_PEERKEY
881 } else if (msg == SMK_M1) {
882 wpa_smk_m1(wpa_auth, sm, key);
883 #endif /* CONFIG_PEERKEY */
884 } else if (key_data_length > 0 &&
885 wpa_parse_kde_ies((const u8 *) (key + 1),
886 key_data_length, &kde) == 0 &&
887 kde.mac_addr) {
888 } else {
889 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
890 "received EAPOL-Key Request for GTK "
891 "rekeying");
892 /* FIX: why was this triggering PTK rekeying for the
893 * STA that requested Group Key rekeying?? */
894 /* wpa_request_new_ptk(sta->wpa_sm); */
895 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
896 wpa_rekey_gtk(wpa_auth, NULL);
898 } else {
899 /* Do not allow the same key replay counter to be reused. This
900 * does also invalidate all other pending replay counters if
901 * retransmissions were used, i.e., we will only process one of
902 * the pending replies and ignore rest if more than one is
903 * received. */
904 sm->key_replay[0].valid = FALSE;
907 #ifdef CONFIG_PEERKEY
908 if (msg == SMK_M3) {
909 wpa_smk_m3(wpa_auth, sm, key);
910 return;
912 #endif /* CONFIG_PEERKEY */
914 os_free(sm->last_rx_eapol_key);
915 sm->last_rx_eapol_key = os_malloc(data_len);
916 if (sm->last_rx_eapol_key == NULL)
917 return;
918 os_memcpy(sm->last_rx_eapol_key, data, data_len);
919 sm->last_rx_eapol_key_len = data_len;
921 sm->EAPOLKeyReceived = TRUE;
922 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
923 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
924 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
925 wpa_sm_step(sm);
929 static void wpa_gmk_to_gtk(const u8 *gmk, const u8 *addr, const u8 *gnonce,
930 u8 *gtk, size_t gtk_len)
932 u8 data[ETH_ALEN + WPA_NONCE_LEN];
934 /* GTK = PRF-X(GMK, "Group key expansion", AA || GNonce) */
935 os_memcpy(data, addr, ETH_ALEN);
936 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
938 #ifdef CONFIG_IEEE80211W
939 sha256_prf(gmk, WPA_GMK_LEN, "Group key expansion",
940 data, sizeof(data), gtk, gtk_len);
941 #else /* CONFIG_IEEE80211W */
942 sha1_prf(gmk, WPA_GMK_LEN, "Group key expansion",
943 data, sizeof(data), gtk, gtk_len);
944 #endif /* CONFIG_IEEE80211W */
946 wpa_hexdump_key(MSG_DEBUG, "GMK", gmk, WPA_GMK_LEN);
947 wpa_hexdump_key(MSG_DEBUG, "GTK", gtk, gtk_len);
951 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
953 struct wpa_authenticator *wpa_auth = eloop_ctx;
954 struct wpa_state_machine *sm = timeout_ctx;
956 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
957 sm->TimeoutEvt = TRUE;
958 wpa_sm_step(sm);
962 void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
963 struct wpa_state_machine *sm, int key_info,
964 const u8 *key_rsc, const u8 *nonce,
965 const u8 *kde, size_t kde_len,
966 int keyidx, int encr, int force_version)
968 struct ieee802_1x_hdr *hdr;
969 struct wpa_eapol_key *key;
970 size_t len;
971 int alg;
972 int key_data_len, pad_len = 0;
973 u8 *buf, *pos;
974 int version, pairwise;
975 int i;
977 len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
979 if (force_version)
980 version = force_version;
981 else if (wpa_use_aes_cmac(sm))
982 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
983 else if (sm->pairwise == WPA_CIPHER_CCMP)
984 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
985 else
986 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
988 pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
990 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
991 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
992 "encr=%d)",
993 version,
994 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
995 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
996 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
997 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
998 pairwise, (unsigned long) kde_len, keyidx, encr);
1000 key_data_len = kde_len;
1002 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1003 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1004 pad_len = key_data_len % 8;
1005 if (pad_len)
1006 pad_len = 8 - pad_len;
1007 key_data_len += pad_len + 8;
1010 len += key_data_len;
1012 hdr = os_zalloc(len);
1013 if (hdr == NULL)
1014 return;
1015 hdr->version = wpa_auth->conf.eapol_version;
1016 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1017 hdr->length = host_to_be16(len - sizeof(*hdr));
1018 key = (struct wpa_eapol_key *) (hdr + 1);
1020 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1021 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1022 key_info |= version;
1023 if (encr && sm->wpa == WPA_VERSION_WPA2)
1024 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1025 if (sm->wpa != WPA_VERSION_WPA2)
1026 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1027 WPA_PUT_BE16(key->key_info, key_info);
1029 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
1030 switch (alg) {
1031 case WPA_CIPHER_CCMP:
1032 WPA_PUT_BE16(key->key_length, 16);
1033 break;
1034 case WPA_CIPHER_TKIP:
1035 WPA_PUT_BE16(key->key_length, 32);
1036 break;
1037 case WPA_CIPHER_WEP40:
1038 WPA_PUT_BE16(key->key_length, 5);
1039 break;
1040 case WPA_CIPHER_WEP104:
1041 WPA_PUT_BE16(key->key_length, 13);
1042 break;
1044 if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
1045 WPA_PUT_BE16(key->key_length, 0);
1047 /* FIX: STSL: what to use as key_replay_counter? */
1048 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1049 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1050 os_memcpy(sm->key_replay[i].counter,
1051 sm->key_replay[i - 1].counter,
1052 WPA_REPLAY_COUNTER_LEN);
1054 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1055 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1056 WPA_REPLAY_COUNTER_LEN);
1057 sm->key_replay[0].valid = TRUE;
1059 if (nonce)
1060 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1062 if (key_rsc)
1063 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1065 if (kde && !encr) {
1066 os_memcpy(key + 1, kde, kde_len);
1067 WPA_PUT_BE16(key->key_data_length, kde_len);
1068 } else if (encr && kde) {
1069 buf = os_zalloc(key_data_len);
1070 if (buf == NULL) {
1071 os_free(hdr);
1072 return;
1074 pos = buf;
1075 os_memcpy(pos, kde, kde_len);
1076 pos += kde_len;
1078 if (pad_len)
1079 *pos++ = 0xdd;
1081 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1082 buf, key_data_len);
1083 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1084 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1085 if (aes_wrap(sm->PTK.kek, (key_data_len - 8) / 8, buf,
1086 (u8 *) (key + 1))) {
1087 os_free(hdr);
1088 os_free(buf);
1089 return;
1091 WPA_PUT_BE16(key->key_data_length, key_data_len);
1092 } else {
1093 u8 ek[32];
1094 os_memcpy(key->key_iv,
1095 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1096 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1097 os_memcpy(ek, key->key_iv, 16);
1098 os_memcpy(ek + 16, sm->PTK.kek, 16);
1099 os_memcpy(key + 1, buf, key_data_len);
1100 rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
1101 WPA_PUT_BE16(key->key_data_length, key_data_len);
1103 os_free(buf);
1106 if (key_info & WPA_KEY_INFO_MIC) {
1107 if (!sm->PTK_valid) {
1108 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1109 "PTK not valid when sending EAPOL-Key "
1110 "frame");
1111 os_free(hdr);
1112 return;
1114 wpa_eapol_key_mic(sm->PTK.kck, version, (u8 *) hdr, len,
1115 key->key_mic);
1118 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1120 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1121 sm->pairwise_set);
1122 os_free(hdr);
1126 static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1127 struct wpa_state_machine *sm, int key_info,
1128 const u8 *key_rsc, const u8 *nonce,
1129 const u8 *kde, size_t kde_len,
1130 int keyidx, int encr)
1132 int timeout_ms;
1133 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1134 int ctr;
1136 if (sm == NULL)
1137 return;
1139 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1140 keyidx, encr, 0);
1142 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1143 if (ctr == 1)
1144 timeout_ms = eapol_key_timeout_first;
1145 else
1146 timeout_ms = eapol_key_timeout_subseq;
1147 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1148 wpa_send_eapol_timeout, wpa_auth, sm);
1152 static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len)
1154 struct ieee802_1x_hdr *hdr;
1155 struct wpa_eapol_key *key;
1156 u16 key_info;
1157 int ret = 0;
1158 u8 mic[16];
1160 if (data_len < sizeof(*hdr) + sizeof(*key))
1161 return -1;
1163 hdr = (struct ieee802_1x_hdr *) data;
1164 key = (struct wpa_eapol_key *) (hdr + 1);
1165 key_info = WPA_GET_BE16(key->key_info);
1166 os_memcpy(mic, key->key_mic, 16);
1167 os_memset(key->key_mic, 0, 16);
1168 if (wpa_eapol_key_mic(PTK->kck, key_info & WPA_KEY_INFO_TYPE_MASK,
1169 data, data_len, key->key_mic) ||
1170 os_memcmp(mic, key->key_mic, 16) != 0)
1171 ret = -1;
1172 os_memcpy(key->key_mic, mic, 16);
1173 return ret;
1177 void wpa_remove_ptk(struct wpa_state_machine *sm)
1179 sm->PTK_valid = FALSE;
1180 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1181 wpa_auth_set_key(sm->wpa_auth, 0, "none", sm->addr, 0, (u8 *) "", 0);
1182 sm->pairwise_set = FALSE;
1183 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1187 void wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
1189 int remove_ptk = 1;
1191 if (sm == NULL)
1192 return;
1194 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1195 "event %d notification", event);
1197 switch (event) {
1198 case WPA_AUTH:
1199 case WPA_ASSOC:
1200 break;
1201 case WPA_DEAUTH:
1202 case WPA_DISASSOC:
1203 sm->DeauthenticationRequest = TRUE;
1204 break;
1205 case WPA_REAUTH:
1206 case WPA_REAUTH_EAPOL:
1207 if (sm->GUpdateStationKeys) {
1209 * Reauthentication cancels the pending group key
1210 * update for this STA.
1212 sm->group->GKeyDoneStations--;
1213 sm->GUpdateStationKeys = FALSE;
1214 sm->PtkGroupInit = TRUE;
1216 sm->ReAuthenticationRequest = TRUE;
1217 break;
1218 case WPA_ASSOC_FT:
1219 #ifdef CONFIG_IEEE80211R
1220 /* Using FT protocol, not WPA auth state machine */
1221 sm->ft_completed = 1;
1222 return;
1223 #else /* CONFIG_IEEE80211R */
1224 break;
1225 #endif /* CONFIG_IEEE80211R */
1228 #ifdef CONFIG_IEEE80211R
1229 sm->ft_completed = 0;
1230 #endif /* CONFIG_IEEE80211R */
1232 #ifdef CONFIG_IEEE80211W
1233 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1234 remove_ptk = 0;
1235 #endif /* CONFIG_IEEE80211W */
1237 if (remove_ptk) {
1238 sm->PTK_valid = FALSE;
1239 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1241 if (event != WPA_REAUTH_EAPOL)
1242 wpa_remove_ptk(sm);
1245 wpa_sm_step(sm);
1249 static const char * wpa_alg_txt(int alg)
1251 switch (alg) {
1252 case WPA_CIPHER_CCMP:
1253 return "CCMP";
1254 case WPA_CIPHER_TKIP:
1255 return "TKIP";
1256 case WPA_CIPHER_WEP104:
1257 case WPA_CIPHER_WEP40:
1258 return "WEP";
1259 default:
1260 return "";
1265 SM_STATE(WPA_PTK, INITIALIZE)
1267 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1268 if (sm->Init) {
1269 /* Init flag is not cleared here, so avoid busy
1270 * loop by claiming nothing changed. */
1271 sm->changed = FALSE;
1274 sm->keycount = 0;
1275 if (sm->GUpdateStationKeys)
1276 sm->group->GKeyDoneStations--;
1277 sm->GUpdateStationKeys = FALSE;
1278 if (sm->wpa == WPA_VERSION_WPA)
1279 sm->PInitAKeys = FALSE;
1280 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1281 * Local AA > Remote AA)) */) {
1282 sm->Pair = TRUE;
1284 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1285 wpa_remove_ptk(sm);
1286 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1287 sm->TimeoutCtr = 0;
1288 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1289 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1290 WPA_EAPOL_authorized, 0);
1295 SM_STATE(WPA_PTK, DISCONNECT)
1297 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1298 sm->Disconnect = FALSE;
1299 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1303 SM_STATE(WPA_PTK, DISCONNECTED)
1305 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1306 sm->DeauthenticationRequest = FALSE;
1310 SM_STATE(WPA_PTK, AUTHENTICATION)
1312 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1313 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1314 sm->PTK_valid = FALSE;
1315 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1317 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1318 sm->AuthenticationRequest = FALSE;
1322 SM_STATE(WPA_PTK, AUTHENTICATION2)
1324 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1325 os_memcpy(sm->ANonce, sm->group->Counter, WPA_NONCE_LEN);
1326 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1327 sm->ReAuthenticationRequest = FALSE;
1328 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1329 * logical place than INITIALIZE since AUTHENTICATION2 can be
1330 * re-entered on ReAuthenticationRequest without going through
1331 * INITIALIZE. */
1332 sm->TimeoutCtr = 0;
1336 SM_STATE(WPA_PTK, INITPMK)
1338 u8 msk[2 * PMK_LEN];
1339 size_t len = 2 * PMK_LEN;
1341 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1342 #ifdef CONFIG_IEEE80211R
1343 sm->xxkey_len = 0;
1344 #endif /* CONFIG_IEEE80211R */
1345 if (sm->pmksa) {
1346 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1347 os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
1348 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1349 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1350 "(len=%lu)", (unsigned long) len);
1351 os_memcpy(sm->PMK, msk, PMK_LEN);
1352 #ifdef CONFIG_IEEE80211R
1353 if (len >= 2 * PMK_LEN) {
1354 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1355 sm->xxkey_len = PMK_LEN;
1357 #endif /* CONFIG_IEEE80211R */
1358 } else {
1359 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
1362 sm->req_replay_counter_used = 0;
1363 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1364 * will break reauthentication since EAPOL state machines may not be
1365 * get into AUTHENTICATING state that clears keyRun before WPA state
1366 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1367 * state and takes PMK from the previously used AAA Key. This will
1368 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1369 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1370 * be good workaround for this issue. */
1371 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1375 SM_STATE(WPA_PTK, INITPSK)
1377 const u8 *psk;
1378 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
1379 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL);
1380 if (psk) {
1381 os_memcpy(sm->PMK, psk, PMK_LEN);
1382 #ifdef CONFIG_IEEE80211R
1383 os_memcpy(sm->xxkey, psk, PMK_LEN);
1384 sm->xxkey_len = PMK_LEN;
1385 #endif /* CONFIG_IEEE80211R */
1387 sm->req_replay_counter_used = 0;
1391 SM_STATE(WPA_PTK, PTKSTART)
1393 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1394 size_t pmkid_len = 0;
1396 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
1397 sm->PTKRequest = FALSE;
1398 sm->TimeoutEvt = FALSE;
1400 sm->TimeoutCtr++;
1401 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1402 /* No point in sending the EAPOL-Key - we will disconnect
1403 * immediately following this. */
1404 return;
1407 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1408 "sending 1/4 msg of 4-Way Handshake");
1410 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1411 * one possible PSK for this STA.
1413 if (sm->wpa == WPA_VERSION_WPA2 &&
1414 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt)) {
1415 pmkid = buf;
1416 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
1417 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
1418 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
1419 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
1420 if (sm->pmksa)
1421 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
1422 sm->pmksa->pmkid, PMKID_LEN);
1423 else {
1425 * Calculate PMKID since no PMKSA cache entry was
1426 * available with pre-calculated PMKID.
1428 rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
1429 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
1430 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1433 wpa_send_eapol(sm->wpa_auth, sm,
1434 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
1435 sm->ANonce, pmkid, pmkid_len, 0, 0);
1439 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk,
1440 struct wpa_ptk *ptk)
1442 size_t ptk_len = sm->pairwise == WPA_CIPHER_CCMP ? 48 : 64;
1443 #ifdef CONFIG_IEEE80211R
1444 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
1445 return wpa_auth_derive_ptk_ft(sm, pmk, ptk, ptk_len);
1446 #endif /* CONFIG_IEEE80211R */
1448 wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
1449 sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce,
1450 (u8 *) ptk, ptk_len,
1451 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1453 return 0;
1457 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
1459 struct wpa_ptk PTK;
1460 int ok = 0;
1461 const u8 *pmk = NULL;
1463 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
1464 sm->EAPOLKeyReceived = FALSE;
1466 /* WPA with IEEE 802.1X: use the derived PMK from EAP
1467 * WPA-PSK: iterate through possible PSKs and select the one matching
1468 * the packet */
1469 for (;;) {
1470 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1471 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, pmk);
1472 if (pmk == NULL)
1473 break;
1474 } else
1475 pmk = sm->PMK;
1477 wpa_derive_ptk(sm, pmk, &PTK);
1479 if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key,
1480 sm->last_rx_eapol_key_len) == 0) {
1481 ok = 1;
1482 break;
1485 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
1486 break;
1489 if (!ok) {
1490 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1491 "invalid MIC in msg 2/4 of 4-Way Handshake");
1492 return;
1495 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
1497 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1498 /* PSK may have changed from the previous choice, so update
1499 * state machine data based on whatever PSK was selected here.
1501 os_memcpy(sm->PMK, pmk, PMK_LEN);
1504 sm->MICVerified = TRUE;
1506 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
1507 sm->PTK_valid = TRUE;
1511 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
1513 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
1514 sm->TimeoutCtr = 0;
1518 #ifdef CONFIG_IEEE80211W
1520 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1522 if (sm->mgmt_frame_prot) {
1523 return 2 + RSN_SELECTOR_LEN + sizeof(struct wpa_igtk_kde);
1526 return 0;
1530 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1532 struct wpa_igtk_kde igtk;
1533 struct wpa_group *gsm = sm->group;
1535 if (!sm->mgmt_frame_prot)
1536 return pos;
1538 igtk.keyid[0] = gsm->GN_igtk;
1539 igtk.keyid[1] = 0;
1540 if (wpa_auth_get_seqnum_igtk(sm->wpa_auth, NULL, gsm->GN_igtk, igtk.pn)
1541 < 0)
1542 os_memset(igtk.pn, 0, sizeof(igtk.pn));
1543 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
1544 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
1545 (const u8 *) &igtk, sizeof(igtk), NULL, 0);
1547 return pos;
1550 #else /* CONFIG_IEEE80211W */
1552 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1554 return 0;
1558 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1560 return pos;
1563 #endif /* CONFIG_IEEE80211W */
1566 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
1568 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
1569 size_t gtk_len, kde_len;
1570 struct wpa_group *gsm = sm->group;
1571 u8 *wpa_ie;
1572 int wpa_ie_len, secure, keyidx, encr = 0;
1574 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
1575 sm->TimeoutEvt = FALSE;
1577 sm->TimeoutCtr++;
1578 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1579 /* No point in sending the EAPOL-Key - we will disconnect
1580 * immediately following this. */
1581 return;
1584 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, GTK[GN])
1586 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
1587 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
1588 wpa_ie = sm->wpa_auth->wpa_ie;
1589 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
1590 if (sm->wpa == WPA_VERSION_WPA &&
1591 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
1592 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
1593 /* WPA-only STA, remove RSN IE */
1594 wpa_ie = wpa_ie + wpa_ie[1] + 2;
1595 wpa_ie_len = wpa_ie[1] + 2;
1597 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1598 "sending 3/4 msg of 4-Way Handshake");
1599 if (sm->wpa == WPA_VERSION_WPA2) {
1600 /* WPA2 send GTK in the 4-way handshake */
1601 secure = 1;
1602 gtk = gsm->GTK[gsm->GN - 1];
1603 gtk_len = gsm->GTK_len;
1604 keyidx = gsm->GN;
1605 _rsc = rsc;
1606 encr = 1;
1607 } else {
1608 /* WPA does not include GTK in msg 3/4 */
1609 secure = 0;
1610 gtk = NULL;
1611 gtk_len = 0;
1612 keyidx = 0;
1613 _rsc = NULL;
1616 kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
1617 if (gtk)
1618 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
1619 kde = os_malloc(kde_len);
1620 if (kde == NULL)
1621 return;
1623 pos = kde;
1624 os_memcpy(pos, wpa_ie, wpa_ie_len);
1625 pos += wpa_ie_len;
1626 if (gtk) {
1627 u8 hdr[2];
1628 hdr[0] = keyidx & 0x03;
1629 hdr[1] = 0;
1630 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
1631 gtk, gtk_len);
1633 pos = ieee80211w_kde_add(sm, pos);
1635 wpa_send_eapol(sm->wpa_auth, sm,
1636 (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
1637 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
1638 WPA_KEY_INFO_KEY_TYPE,
1639 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
1640 os_free(kde);
1644 SM_STATE(WPA_PTK, PTKINITDONE)
1646 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
1647 sm->EAPOLKeyReceived = FALSE;
1648 if (sm->Pair) {
1649 char *alg;
1650 int klen;
1651 if (sm->pairwise == WPA_CIPHER_TKIP) {
1652 alg = "TKIP";
1653 klen = 32;
1654 } else {
1655 alg = "CCMP";
1656 klen = 16;
1658 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
1659 sm->PTK.tk1, klen)) {
1660 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1661 return;
1663 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
1664 sm->pairwise_set = TRUE;
1666 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
1667 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1668 eloop_register_timeout(sm->wpa_auth->conf.
1669 wpa_ptk_rekey, 0, wpa_rekey_ptk,
1670 sm->wpa_auth, sm);
1673 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1674 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1675 WPA_EAPOL_authorized, 1);
1679 if (0 /* IBSS == TRUE */) {
1680 sm->keycount++;
1681 if (sm->keycount == 2) {
1682 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1683 WPA_EAPOL_portValid, 1);
1685 } else {
1686 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
1689 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
1690 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
1691 if (sm->wpa == WPA_VERSION_WPA)
1692 sm->PInitAKeys = TRUE;
1693 else
1694 sm->has_GTK = TRUE;
1695 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1696 "pairwise key handshake completed (%s)",
1697 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
1699 #ifdef CONFIG_IEEE80211R
1700 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
1701 #endif /* CONFIG_IEEE80211R */
1705 SM_STEP(WPA_PTK)
1707 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
1709 if (sm->Init)
1710 SM_ENTER(WPA_PTK, INITIALIZE);
1711 else if (sm->Disconnect
1712 /* || FIX: dot11RSNAConfigSALifetime timeout */)
1713 SM_ENTER(WPA_PTK, DISCONNECT);
1714 else if (sm->DeauthenticationRequest)
1715 SM_ENTER(WPA_PTK, DISCONNECTED);
1716 else if (sm->AuthenticationRequest)
1717 SM_ENTER(WPA_PTK, AUTHENTICATION);
1718 else if (sm->ReAuthenticationRequest)
1719 SM_ENTER(WPA_PTK, AUTHENTICATION2);
1720 else if (sm->PTKRequest)
1721 SM_ENTER(WPA_PTK, PTKSTART);
1722 else switch (sm->wpa_ptk_state) {
1723 case WPA_PTK_INITIALIZE:
1724 break;
1725 case WPA_PTK_DISCONNECT:
1726 SM_ENTER(WPA_PTK, DISCONNECTED);
1727 break;
1728 case WPA_PTK_DISCONNECTED:
1729 SM_ENTER(WPA_PTK, INITIALIZE);
1730 break;
1731 case WPA_PTK_AUTHENTICATION:
1732 SM_ENTER(WPA_PTK, AUTHENTICATION2);
1733 break;
1734 case WPA_PTK_AUTHENTICATION2:
1735 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
1736 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
1737 WPA_EAPOL_keyRun) > 0)
1738 SM_ENTER(WPA_PTK, INITPMK);
1739 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
1740 /* FIX: && 802.1X::keyRun */)
1741 SM_ENTER(WPA_PTK, INITPSK);
1742 break;
1743 case WPA_PTK_INITPMK:
1744 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
1745 WPA_EAPOL_keyAvailable) > 0)
1746 SM_ENTER(WPA_PTK, PTKSTART);
1747 else {
1748 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1749 SM_ENTER(WPA_PTK, DISCONNECT);
1751 break;
1752 case WPA_PTK_INITPSK:
1753 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL))
1754 SM_ENTER(WPA_PTK, PTKSTART);
1755 else {
1756 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1757 "no PSK configured for the STA");
1758 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1759 SM_ENTER(WPA_PTK, DISCONNECT);
1761 break;
1762 case WPA_PTK_PTKSTART:
1763 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1764 sm->EAPOLKeyPairwise)
1765 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
1766 else if (sm->TimeoutCtr >
1767 (int) dot11RSNAConfigPairwiseUpdateCount) {
1768 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1769 SM_ENTER(WPA_PTK, DISCONNECT);
1770 } else if (sm->TimeoutEvt)
1771 SM_ENTER(WPA_PTK, PTKSTART);
1772 break;
1773 case WPA_PTK_PTKCALCNEGOTIATING:
1774 if (sm->MICVerified)
1775 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
1776 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1777 sm->EAPOLKeyPairwise)
1778 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
1779 else if (sm->TimeoutEvt)
1780 SM_ENTER(WPA_PTK, PTKSTART);
1781 break;
1782 case WPA_PTK_PTKCALCNEGOTIATING2:
1783 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
1784 break;
1785 case WPA_PTK_PTKINITNEGOTIATING:
1786 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1787 sm->EAPOLKeyPairwise && sm->MICVerified)
1788 SM_ENTER(WPA_PTK, PTKINITDONE);
1789 else if (sm->TimeoutCtr >
1790 (int) dot11RSNAConfigPairwiseUpdateCount) {
1791 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1792 SM_ENTER(WPA_PTK, DISCONNECT);
1793 } else if (sm->TimeoutEvt)
1794 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
1795 break;
1796 case WPA_PTK_PTKINITDONE:
1797 break;
1802 SM_STATE(WPA_PTK_GROUP, IDLE)
1804 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
1805 if (sm->Init) {
1806 /* Init flag is not cleared here, so avoid busy
1807 * loop by claiming nothing changed. */
1808 sm->changed = FALSE;
1810 sm->GTimeoutCtr = 0;
1814 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
1816 u8 rsc[WPA_KEY_RSC_LEN];
1817 struct wpa_group *gsm = sm->group;
1818 u8 *kde, *pos, hdr[2];
1819 size_t kde_len;
1821 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
1823 sm->GTimeoutCtr++;
1824 if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
1825 /* No point in sending the EAPOL-Key - we will disconnect
1826 * immediately following this. */
1827 return;
1830 if (sm->wpa == WPA_VERSION_WPA)
1831 sm->PInitAKeys = FALSE;
1832 sm->TimeoutEvt = FALSE;
1833 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
1834 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
1835 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
1836 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
1837 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1838 "sending 1/2 msg of Group Key Handshake");
1840 if (sm->wpa == WPA_VERSION_WPA2) {
1841 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
1842 ieee80211w_kde_len(sm);
1843 kde = os_malloc(kde_len);
1844 if (kde == NULL)
1845 return;
1847 pos = kde;
1848 hdr[0] = gsm->GN & 0x03;
1849 hdr[1] = 0;
1850 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
1851 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
1852 pos = ieee80211w_kde_add(sm, pos);
1853 } else {
1854 kde = gsm->GTK[gsm->GN - 1];
1855 pos = kde + gsm->GTK_len;
1858 wpa_send_eapol(sm->wpa_auth, sm,
1859 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
1860 WPA_KEY_INFO_ACK |
1861 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
1862 rsc, gsm->GNonce, kde, pos - kde, gsm->GN, 1);
1863 if (sm->wpa == WPA_VERSION_WPA2)
1864 os_free(kde);
1868 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
1870 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
1871 sm->EAPOLKeyReceived = FALSE;
1872 if (sm->GUpdateStationKeys)
1873 sm->group->GKeyDoneStations--;
1874 sm->GUpdateStationKeys = FALSE;
1875 sm->GTimeoutCtr = 0;
1876 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
1877 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1878 "group key handshake completed (%s)",
1879 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
1880 sm->has_GTK = TRUE;
1884 SM_STATE(WPA_PTK_GROUP, KEYERROR)
1886 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
1887 if (sm->GUpdateStationKeys)
1888 sm->group->GKeyDoneStations--;
1889 sm->GUpdateStationKeys = FALSE;
1890 sm->Disconnect = TRUE;
1894 SM_STEP(WPA_PTK_GROUP)
1896 if (sm->Init || sm->PtkGroupInit) {
1897 SM_ENTER(WPA_PTK_GROUP, IDLE);
1898 sm->PtkGroupInit = FALSE;
1899 } else switch (sm->wpa_ptk_group_state) {
1900 case WPA_PTK_GROUP_IDLE:
1901 if (sm->GUpdateStationKeys ||
1902 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
1903 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
1904 break;
1905 case WPA_PTK_GROUP_REKEYNEGOTIATING:
1906 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1907 !sm->EAPOLKeyPairwise && sm->MICVerified)
1908 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
1909 else if (sm->GTimeoutCtr >
1910 (int) dot11RSNAConfigGroupUpdateCount)
1911 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
1912 else if (sm->TimeoutEvt)
1913 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
1914 break;
1915 case WPA_PTK_GROUP_KEYERROR:
1916 SM_ENTER(WPA_PTK_GROUP, IDLE);
1917 break;
1918 case WPA_PTK_GROUP_REKEYESTABLISHED:
1919 SM_ENTER(WPA_PTK_GROUP, IDLE);
1920 break;
1925 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
1926 struct wpa_group *group)
1928 int ret = 0;
1930 /* FIX: is this the correct way of getting GNonce? */
1931 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
1932 inc_byte_array(group->Counter, WPA_NONCE_LEN);
1933 wpa_gmk_to_gtk(group->GMK, wpa_auth->addr, group->GNonce,
1934 group->GTK[group->GN - 1], group->GTK_len);
1936 #ifdef CONFIG_IEEE80211W
1937 if (wpa_auth->conf.ieee80211w != WPA_NO_IEEE80211W) {
1938 if (os_get_random(group->IGTK[group->GN_igtk - 4],
1939 WPA_IGTK_LEN) < 0) {
1940 wpa_printf(MSG_INFO, "RSN: Failed to get new random "
1941 "IGTK");
1942 ret = -1;
1944 wpa_hexdump_key(MSG_DEBUG, "IGTK",
1945 group->IGTK[group->GN_igtk - 4], WPA_IGTK_LEN);
1947 #endif /* CONFIG_IEEE80211W */
1949 return ret;
1953 static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
1954 struct wpa_group *group)
1956 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
1957 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
1958 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
1959 group->wpa_group_state = WPA_GROUP_GTK_INIT;
1961 /* GTK[0..N] = 0 */
1962 os_memset(group->GTK, 0, sizeof(group->GTK));
1963 group->GN = 1;
1964 group->GM = 2;
1965 #ifdef CONFIG_IEEE80211W
1966 group->GN_igtk = 4;
1967 group->GM_igtk = 5;
1968 #endif /* CONFIG_IEEE80211W */
1969 /* GTK[GN] = CalcGTK() */
1970 wpa_gtk_update(wpa_auth, group);
1974 static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
1976 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
1977 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1978 "Not in PTKINITDONE; skip Group Key update");
1979 return 0;
1981 if (sm->GUpdateStationKeys) {
1983 * This should not really happen, but just in case, make sure
1984 * we do not count the same STA twice in GKeyDoneStations.
1986 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1987 "GUpdateStationKeys already set - do not "
1988 "increment GKeyDoneStations");
1989 } else {
1990 sm->group->GKeyDoneStations++;
1991 sm->GUpdateStationKeys = TRUE;
1993 wpa_sm_step(sm);
1994 return 0;
1998 static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
1999 struct wpa_group *group)
2001 int tmp;
2003 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2004 "SETKEYS (VLAN-ID %d)", group->vlan_id);
2005 group->changed = TRUE;
2006 group->wpa_group_state = WPA_GROUP_SETKEYS;
2007 group->GTKReKey = FALSE;
2008 tmp = group->GM;
2009 group->GM = group->GN;
2010 group->GN = tmp;
2011 #ifdef CONFIG_IEEE80211W
2012 tmp = group->GM_igtk;
2013 group->GM_igtk = group->GN_igtk;
2014 group->GN_igtk = tmp;
2015 #endif /* CONFIG_IEEE80211W */
2016 /* "GKeyDoneStations = GNoStations" is done in more robust way by
2017 * counting the STAs that are marked with GUpdateStationKeys instead of
2018 * including all STAs that could be in not-yet-completed state. */
2019 wpa_gtk_update(wpa_auth, group);
2021 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, NULL);
2022 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
2023 group->GKeyDoneStations);
2027 static void wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
2028 struct wpa_group *group)
2030 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2031 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
2032 group->changed = TRUE;
2033 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
2034 wpa_auth_set_key(wpa_auth, group->vlan_id,
2035 wpa_alg_txt(wpa_auth->conf.wpa_group),
2036 NULL, group->GN, group->GTK[group->GN - 1],
2037 group->GTK_len);
2039 #ifdef CONFIG_IEEE80211W
2040 if (wpa_auth->conf.ieee80211w != WPA_NO_IEEE80211W) {
2041 wpa_auth_set_key(wpa_auth, group->vlan_id, "IGTK",
2042 NULL, group->GN_igtk,
2043 group->IGTK[group->GN_igtk - 4],
2044 WPA_IGTK_LEN);
2046 #endif /* CONFIG_IEEE80211W */
2050 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
2051 struct wpa_group *group)
2053 if (group->GInit) {
2054 wpa_group_gtk_init(wpa_auth, group);
2055 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
2056 group->GTKAuthenticator) {
2057 wpa_group_setkeysdone(wpa_auth, group);
2058 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
2059 group->GTKReKey) {
2060 wpa_group_setkeys(wpa_auth, group);
2061 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
2062 if (group->GKeyDoneStations == 0)
2063 wpa_group_setkeysdone(wpa_auth, group);
2064 else if (group->GTKReKey)
2065 wpa_group_setkeys(wpa_auth, group);
2070 static void wpa_sm_step(struct wpa_state_machine *sm)
2072 if (sm == NULL)
2073 return;
2075 if (sm->in_step_loop) {
2076 /* This should not happen, but if it does, make sure we do not
2077 * end up freeing the state machine too early by exiting the
2078 * recursive call. */
2079 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
2080 return;
2083 sm->in_step_loop = 1;
2084 do {
2085 if (sm->pending_deinit)
2086 break;
2088 sm->changed = FALSE;
2089 sm->wpa_auth->group->changed = FALSE;
2091 SM_STEP_RUN(WPA_PTK);
2092 if (sm->pending_deinit)
2093 break;
2094 SM_STEP_RUN(WPA_PTK_GROUP);
2095 if (sm->pending_deinit)
2096 break;
2097 wpa_group_sm_step(sm->wpa_auth, sm->group);
2098 } while (sm->changed || sm->wpa_auth->group->changed);
2099 sm->in_step_loop = 0;
2101 if (sm->pending_deinit) {
2102 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
2103 "machine deinit for " MACSTR, MAC2STR(sm->addr));
2104 wpa_free_sta_sm(sm);
2109 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
2111 struct wpa_state_machine *sm = eloop_ctx;
2112 wpa_sm_step(sm);
2116 void wpa_auth_sm_notify(struct wpa_state_machine *sm)
2118 if (sm == NULL)
2119 return;
2120 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
2124 void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
2126 int tmp, i;
2127 struct wpa_group *group;
2129 if (wpa_auth == NULL)
2130 return;
2132 group = wpa_auth->group;
2134 for (i = 0; i < 2; i++) {
2135 tmp = group->GM;
2136 group->GM = group->GN;
2137 group->GN = tmp;
2138 #ifdef CONFIG_IEEE80211W
2139 tmp = group->GM_igtk;
2140 group->GM_igtk = group->GN_igtk;
2141 group->GN_igtk = tmp;
2142 #endif /* CONFIG_IEEE80211W */
2143 wpa_gtk_update(wpa_auth, group);
2148 static const char * wpa_bool_txt(int bool)
2150 return bool ? "TRUE" : "FALSE";
2154 static int wpa_cipher_bits(int cipher)
2156 switch (cipher) {
2157 case WPA_CIPHER_CCMP:
2158 return 128;
2159 case WPA_CIPHER_TKIP:
2160 return 256;
2161 case WPA_CIPHER_WEP104:
2162 return 104;
2163 case WPA_CIPHER_WEP40:
2164 return 40;
2165 default:
2166 return 0;
2171 #define RSN_SUITE "%02x-%02x-%02x-%d"
2172 #define RSN_SUITE_ARG(s) \
2173 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2175 int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
2177 int len = 0, ret;
2178 char pmkid_txt[PMKID_LEN * 2 + 1];
2180 if (wpa_auth == NULL)
2181 return len;
2183 ret = os_snprintf(buf + len, buflen - len,
2184 "dot11RSNAOptionImplemented=TRUE\n"
2185 #ifdef CONFIG_RSN_PREAUTH
2186 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2187 #else /* CONFIG_RSN_PREAUTH */
2188 "dot11RSNAPreauthenticationImplemented=FALSE\n"
2189 #endif /* CONFIG_RSN_PREAUTH */
2190 "dot11RSNAEnabled=%s\n"
2191 "dot11RSNAPreauthenticationEnabled=%s\n",
2192 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
2193 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
2194 if (ret < 0 || (size_t) ret >= buflen - len)
2195 return len;
2196 len += ret;
2198 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2199 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
2201 ret = os_snprintf(
2202 buf + len, buflen - len,
2203 "dot11RSNAConfigVersion=%u\n"
2204 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
2205 /* FIX: dot11RSNAConfigGroupCipher */
2206 /* FIX: dot11RSNAConfigGroupRekeyMethod */
2207 /* FIX: dot11RSNAConfigGroupRekeyTime */
2208 /* FIX: dot11RSNAConfigGroupRekeyPackets */
2209 "dot11RSNAConfigGroupRekeyStrict=%u\n"
2210 "dot11RSNAConfigGroupUpdateCount=%u\n"
2211 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
2212 "dot11RSNAConfigGroupCipherSize=%u\n"
2213 "dot11RSNAConfigPMKLifetime=%u\n"
2214 "dot11RSNAConfigPMKReauthThreshold=%u\n"
2215 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
2216 "dot11RSNAConfigSATimeout=%u\n"
2217 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2218 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2219 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2220 "dot11RSNAPMKIDUsed=%s\n"
2221 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2222 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2223 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2224 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
2225 "dot11RSNA4WayHandshakeFailures=%u\n"
2226 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
2227 RSN_VERSION,
2228 !!wpa_auth->conf.wpa_strict_rekey,
2229 dot11RSNAConfigGroupUpdateCount,
2230 dot11RSNAConfigPairwiseUpdateCount,
2231 wpa_cipher_bits(wpa_auth->conf.wpa_group),
2232 dot11RSNAConfigPMKLifetime,
2233 dot11RSNAConfigPMKReauthThreshold,
2234 dot11RSNAConfigSATimeout,
2235 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
2236 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
2237 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
2238 pmkid_txt,
2239 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
2240 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
2241 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
2242 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
2243 wpa_auth->dot11RSNA4WayHandshakeFailures);
2244 if (ret < 0 || (size_t) ret >= buflen - len)
2245 return len;
2246 len += ret;
2248 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
2249 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
2251 /* Private MIB */
2252 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
2253 wpa_auth->group->wpa_group_state);
2254 if (ret < 0 || (size_t) ret >= buflen - len)
2255 return len;
2256 len += ret;
2258 return len;
2262 int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
2264 int len = 0, ret;
2265 u32 pairwise = 0;
2267 if (sm == NULL)
2268 return 0;
2270 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
2272 /* dot11RSNAStatsEntry */
2274 if (sm->wpa == WPA_VERSION_WPA) {
2275 if (sm->pairwise == WPA_CIPHER_CCMP)
2276 pairwise = WPA_CIPHER_SUITE_CCMP;
2277 else if (sm->pairwise == WPA_CIPHER_TKIP)
2278 pairwise = WPA_CIPHER_SUITE_TKIP;
2279 else if (sm->pairwise == WPA_CIPHER_WEP104)
2280 pairwise = WPA_CIPHER_SUITE_WEP104;
2281 else if (sm->pairwise == WPA_CIPHER_WEP40)
2282 pairwise = WPA_CIPHER_SUITE_WEP40;
2283 else if (sm->pairwise == WPA_CIPHER_NONE)
2284 pairwise = WPA_CIPHER_SUITE_NONE;
2285 } else if (sm->wpa == WPA_VERSION_WPA2) {
2286 if (sm->pairwise == WPA_CIPHER_CCMP)
2287 pairwise = RSN_CIPHER_SUITE_CCMP;
2288 else if (sm->pairwise == WPA_CIPHER_TKIP)
2289 pairwise = RSN_CIPHER_SUITE_TKIP;
2290 else if (sm->pairwise == WPA_CIPHER_WEP104)
2291 pairwise = RSN_CIPHER_SUITE_WEP104;
2292 else if (sm->pairwise == WPA_CIPHER_WEP40)
2293 pairwise = RSN_CIPHER_SUITE_WEP40;
2294 else if (sm->pairwise == WPA_CIPHER_NONE)
2295 pairwise = RSN_CIPHER_SUITE_NONE;
2296 } else
2297 return 0;
2299 ret = os_snprintf(
2300 buf + len, buflen - len,
2301 /* TODO: dot11RSNAStatsIndex */
2302 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
2303 "dot11RSNAStatsVersion=1\n"
2304 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
2305 /* TODO: dot11RSNAStatsTKIPICVErrors */
2306 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
2307 "dot11RSNAStatsTKIPRemoveMICFailures=%u\n"
2308 /* TODO: dot11RSNAStatsCCMPReplays */
2309 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
2310 /* TODO: dot11RSNAStatsTKIPReplays */,
2311 MAC2STR(sm->addr),
2312 RSN_SUITE_ARG(pairwise),
2313 sm->dot11RSNAStatsTKIPLocalMICFailures,
2314 sm->dot11RSNAStatsTKIPRemoteMICFailures);
2315 if (ret < 0 || (size_t) ret >= buflen - len)
2316 return len;
2317 len += ret;
2319 /* Private MIB */
2320 ret = os_snprintf(buf + len, buflen - len,
2321 "hostapdWPAPTKState=%d\n"
2322 "hostapdWPAPTKGroupState=%d\n",
2323 sm->wpa_ptk_state,
2324 sm->wpa_ptk_group_state);
2325 if (ret < 0 || (size_t) ret >= buflen - len)
2326 return len;
2327 len += ret;
2329 return len;
2333 void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
2335 if (wpa_auth)
2336 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
2340 int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
2342 return sm && sm->pairwise_set;
2346 int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
2348 return sm->pairwise;
2352 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
2354 if (sm == NULL)
2355 return -1;
2356 return sm->wpa_key_mgmt;
2360 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
2362 if (sm == NULL)
2363 return 0;
2364 return sm->wpa;
2368 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
2369 struct rsn_pmksa_cache_entry *entry)
2371 if (sm == NULL || sm->pmksa != entry)
2372 return -1;
2373 sm->pmksa = NULL;
2374 return 0;
2378 struct rsn_pmksa_cache_entry *
2379 wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
2381 return sm ? sm->pmksa : NULL;
2385 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
2387 if (sm)
2388 sm->dot11RSNAStatsTKIPLocalMICFailures++;
2392 const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
2394 if (wpa_auth == NULL)
2395 return NULL;
2396 *len = wpa_auth->wpa_ie_len;
2397 return wpa_auth->wpa_ie;
2401 int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
2402 int session_timeout, struct eapol_state_machine *eapol)
2404 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2)
2405 return -1;
2407 if (pmksa_cache_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
2408 sm->wpa_auth->addr, sm->addr, session_timeout,
2409 eapol, sm->wpa_key_mgmt))
2410 return 0;
2412 return -1;
2416 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
2417 const u8 *pmk, size_t len, const u8 *sta_addr,
2418 int session_timeout,
2419 struct eapol_state_machine *eapol)
2421 if (wpa_auth == NULL)
2422 return -1;
2424 if (pmksa_cache_add(wpa_auth->pmksa, pmk, len, wpa_auth->addr,
2425 sta_addr, session_timeout, eapol,
2426 WPA_KEY_MGMT_IEEE8021X))
2427 return 0;
2429 return -1;
2433 static struct wpa_group *
2434 wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
2436 struct wpa_group *group;
2438 if (wpa_auth == NULL || wpa_auth->group == NULL)
2439 return NULL;
2441 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
2442 vlan_id);
2443 group = wpa_group_init(wpa_auth, vlan_id);
2444 if (group == NULL)
2445 return NULL;
2447 group->next = wpa_auth->group->next;
2448 wpa_auth->group->next = group;
2450 return group;
2454 int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
2456 struct wpa_group *group;
2458 if (sm == NULL || sm->wpa_auth == NULL)
2459 return 0;
2461 group = sm->wpa_auth->group;
2462 while (group) {
2463 if (group->vlan_id == vlan_id)
2464 break;
2465 group = group->next;
2468 if (group == NULL) {
2469 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
2470 if (group == NULL)
2471 return -1;
2474 if (sm->group == group)
2475 return 0;
2477 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
2478 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
2480 sm->group = group;
2481 return 0;
2484 #endif /* CONFIG_NATIVE_WINDOWS */