nl80211: Fix a typo
[hostap-gosc2009.git] / src / ap / wpa_auth.c
blob39ad8815bbb8aa2e13c0fcdee5be49b7c832a556
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 "utils/includes.h"
17 #ifndef CONFIG_NATIVE_WINDOWS
19 #include "utils/common.h"
20 #include "utils/eloop.h"
21 #include "utils/state_machine.h"
22 #include "common/ieee802_11_defs.h"
23 #include "crypto/aes_wrap.h"
24 #include "crypto/crypto.h"
25 #include "crypto/sha1.h"
26 #include "crypto/sha256.h"
27 #include "eapol_auth/eapol_auth_sm.h"
28 #include "ap_config.h"
29 #include "ieee802_11.h"
30 #include "wpa_auth.h"
31 #include "pmksa_cache_auth.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 enum wpa_alg 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
127 wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
128 const u8 *data, size_t data_len, int encrypt)
130 if (wpa_auth->cb.send_eapol == NULL)
131 return -1;
132 return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
133 encrypt);
137 int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
138 int (*cb)(struct wpa_state_machine *sm, void *ctx),
139 void *cb_ctx)
141 if (wpa_auth->cb.for_each_sta == NULL)
142 return 0;
143 return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
147 int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
148 int (*cb)(struct wpa_authenticator *a, void *ctx),
149 void *cb_ctx)
151 if (wpa_auth->cb.for_each_auth == NULL)
152 return 0;
153 return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
157 void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
158 logger_level level, const char *txt)
160 if (wpa_auth->cb.logger == NULL)
161 return;
162 wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
166 void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
167 logger_level level, const char *fmt, ...)
169 char *format;
170 int maxlen;
171 va_list ap;
173 if (wpa_auth->cb.logger == NULL)
174 return;
176 maxlen = os_strlen(fmt) + 100;
177 format = os_malloc(maxlen);
178 if (!format)
179 return;
181 va_start(ap, fmt);
182 vsnprintf(format, maxlen, fmt, ap);
183 va_end(ap);
185 wpa_auth_logger(wpa_auth, addr, level, format);
187 os_free(format);
191 static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
192 const u8 *addr)
194 if (wpa_auth->cb.disconnect == NULL)
195 return;
196 wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
197 WLAN_REASON_PREV_AUTH_NOT_VALID);
201 static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
203 int ret = 0;
204 #ifdef CONFIG_IEEE80211R
205 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
206 ret = 1;
207 #endif /* CONFIG_IEEE80211R */
208 #ifdef CONFIG_IEEE80211W
209 if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
210 ret = 1;
211 #endif /* CONFIG_IEEE80211W */
212 return ret;
216 static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
218 struct wpa_authenticator *wpa_auth = eloop_ctx;
220 if (os_get_random(wpa_auth->group->GMK, WPA_GMK_LEN)) {
221 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
222 "initialization.");
223 } else {
224 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
227 if (wpa_auth->conf.wpa_gmk_rekey) {
228 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
229 wpa_rekey_gmk, wpa_auth, NULL);
234 static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
236 struct wpa_authenticator *wpa_auth = eloop_ctx;
237 struct wpa_group *group;
239 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
240 for (group = wpa_auth->group; group; group = group->next) {
241 group->GTKReKey = TRUE;
242 do {
243 group->changed = FALSE;
244 wpa_group_sm_step(wpa_auth, group);
245 } while (group->changed);
248 if (wpa_auth->conf.wpa_group_rekey) {
249 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
250 0, wpa_rekey_gtk, wpa_auth, NULL);
255 static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
257 struct wpa_authenticator *wpa_auth = eloop_ctx;
258 struct wpa_state_machine *sm = timeout_ctx;
260 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
261 wpa_request_new_ptk(sm);
262 wpa_sm_step(sm);
266 static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
268 if (sm->pmksa == ctx)
269 sm->pmksa = NULL;
270 return 0;
274 static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
275 void *ctx)
277 struct wpa_authenticator *wpa_auth = ctx;
278 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
282 static void wpa_group_set_key_len(struct wpa_group *group, int cipher)
284 switch (cipher) {
285 case WPA_CIPHER_CCMP:
286 group->GTK_len = 16;
287 break;
288 case WPA_CIPHER_TKIP:
289 group->GTK_len = 32;
290 break;
291 case WPA_CIPHER_WEP104:
292 group->GTK_len = 13;
293 break;
294 case WPA_CIPHER_WEP40:
295 group->GTK_len = 5;
296 break;
301 static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
302 int vlan_id)
304 struct wpa_group *group;
305 u8 buf[ETH_ALEN + 8 + sizeof(group)];
306 u8 rkey[32];
308 group = os_zalloc(sizeof(struct wpa_group));
309 if (group == NULL)
310 return NULL;
312 group->GTKAuthenticator = TRUE;
313 group->vlan_id = vlan_id;
315 wpa_group_set_key_len(group, wpa_auth->conf.wpa_group);
317 /* Counter = PRF-256(Random number, "Init Counter",
318 * Local MAC Address || Time)
320 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
321 wpa_get_ntp_timestamp(buf + ETH_ALEN);
322 os_memcpy(buf + ETH_ALEN + 8, &group, sizeof(group));
323 if (os_get_random(rkey, sizeof(rkey)) ||
324 os_get_random(group->GMK, WPA_GMK_LEN)) {
325 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
326 "initialization.");
327 os_free(group);
328 return NULL;
331 sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
332 group->Counter, WPA_NONCE_LEN);
334 group->GInit = TRUE;
335 wpa_group_sm_step(wpa_auth, group);
336 group->GInit = FALSE;
337 wpa_group_sm_step(wpa_auth, group);
339 return group;
344 * wpa_init - Initialize WPA authenticator
345 * @addr: Authenticator address
346 * @conf: Configuration for WPA authenticator
347 * @cb: Callback functions for WPA authenticator
348 * Returns: Pointer to WPA authenticator data or %NULL on failure
350 struct wpa_authenticator * wpa_init(const u8 *addr,
351 struct wpa_auth_config *conf,
352 struct wpa_auth_callbacks *cb)
354 struct wpa_authenticator *wpa_auth;
356 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
357 if (wpa_auth == NULL)
358 return NULL;
359 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
360 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
361 os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
363 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
364 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
365 os_free(wpa_auth);
366 return NULL;
369 wpa_auth->group = wpa_group_init(wpa_auth, 0);
370 if (wpa_auth->group == NULL) {
371 os_free(wpa_auth->wpa_ie);
372 os_free(wpa_auth);
373 return NULL;
376 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
377 wpa_auth);
378 if (wpa_auth->pmksa == NULL) {
379 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
380 os_free(wpa_auth->wpa_ie);
381 os_free(wpa_auth);
382 return NULL;
385 #ifdef CONFIG_IEEE80211R
386 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
387 if (wpa_auth->ft_pmk_cache == NULL) {
388 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
389 os_free(wpa_auth->wpa_ie);
390 pmksa_cache_auth_deinit(wpa_auth->pmksa);
391 os_free(wpa_auth);
392 return NULL;
394 #endif /* CONFIG_IEEE80211R */
396 if (wpa_auth->conf.wpa_gmk_rekey) {
397 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
398 wpa_rekey_gmk, wpa_auth, NULL);
401 if (wpa_auth->conf.wpa_group_rekey) {
402 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
403 wpa_rekey_gtk, wpa_auth, NULL);
406 return wpa_auth;
411 * wpa_deinit - Deinitialize WPA authenticator
412 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
414 void wpa_deinit(struct wpa_authenticator *wpa_auth)
416 struct wpa_group *group, *prev;
418 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
419 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
421 #ifdef CONFIG_PEERKEY
422 while (wpa_auth->stsl_negotiations)
423 wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
424 #endif /* CONFIG_PEERKEY */
426 pmksa_cache_auth_deinit(wpa_auth->pmksa);
428 #ifdef CONFIG_IEEE80211R
429 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
430 wpa_auth->ft_pmk_cache = NULL;
431 #endif /* CONFIG_IEEE80211R */
433 os_free(wpa_auth->wpa_ie);
435 group = wpa_auth->group;
436 while (group) {
437 prev = group;
438 group = group->next;
439 os_free(prev);
442 os_free(wpa_auth);
447 * wpa_reconfig - Update WPA authenticator configuration
448 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
449 * @conf: Configuration for WPA authenticator
451 int wpa_reconfig(struct wpa_authenticator *wpa_auth,
452 struct wpa_auth_config *conf)
454 struct wpa_group *group;
455 if (wpa_auth == NULL)
456 return 0;
458 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
459 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
460 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
461 return -1;
465 * Reinitialize GTK to make sure it is suitable for the new
466 * configuration.
468 group = wpa_auth->group;
469 wpa_group_set_key_len(group, wpa_auth->conf.wpa_group);
470 group->GInit = TRUE;
471 wpa_group_sm_step(wpa_auth, group);
472 group->GInit = FALSE;
473 wpa_group_sm_step(wpa_auth, group);
475 return 0;
479 struct wpa_state_machine *
480 wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr)
482 struct wpa_state_machine *sm;
484 sm = os_zalloc(sizeof(struct wpa_state_machine));
485 if (sm == NULL)
486 return NULL;
487 os_memcpy(sm->addr, addr, ETH_ALEN);
489 sm->wpa_auth = wpa_auth;
490 sm->group = wpa_auth->group;
492 return sm;
496 void wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
497 struct wpa_state_machine *sm)
499 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
500 return;
502 #ifdef CONFIG_IEEE80211R
503 if (sm->ft_completed) {
504 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
505 "FT authentication already completed - do not "
506 "start 4-way handshake");
507 return;
509 #endif /* CONFIG_IEEE80211R */
511 if (sm->started) {
512 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
513 sm->ReAuthenticationRequest = TRUE;
514 wpa_sm_step(sm);
515 return;
518 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
519 "start authentication");
520 sm->started = 1;
522 sm->Init = TRUE;
523 wpa_sm_step(sm);
524 sm->Init = FALSE;
525 sm->AuthenticationRequest = TRUE;
526 wpa_sm_step(sm);
530 void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
532 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
533 * reassociates back to the same AP while the previous entry for the
534 * STA has not yet been removed. */
535 if (sm == NULL)
536 return;
538 sm->wpa_key_mgmt = 0;
542 static void wpa_free_sta_sm(struct wpa_state_machine *sm)
544 os_free(sm->last_rx_eapol_key);
545 os_free(sm->wpa_ie);
546 os_free(sm);
550 void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
552 if (sm == NULL)
553 return;
555 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
556 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
557 "strict rekeying - force GTK rekey since STA "
558 "is leaving");
559 eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
560 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
561 NULL);
564 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
565 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
566 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
567 if (sm->in_step_loop) {
568 /* Must not free state machine while wpa_sm_step() is running.
569 * Freeing will be completed in the end of wpa_sm_step(). */
570 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
571 "machine deinit for " MACSTR, MAC2STR(sm->addr));
572 sm->pending_deinit = 1;
573 } else
574 wpa_free_sta_sm(sm);
578 static void wpa_request_new_ptk(struct wpa_state_machine *sm)
580 if (sm == NULL)
581 return;
583 sm->PTKRequest = TRUE;
584 sm->PTK_valid = 0;
588 static int wpa_replay_counter_valid(struct wpa_state_machine *sm,
589 const u8 *replay_counter)
591 int i;
592 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
593 if (!sm->key_replay[i].valid)
594 break;
595 if (os_memcmp(replay_counter, sm->key_replay[i].counter,
596 WPA_REPLAY_COUNTER_LEN) == 0)
597 return 1;
599 return 0;
603 void wpa_receive(struct wpa_authenticator *wpa_auth,
604 struct wpa_state_machine *sm,
605 u8 *data, size_t data_len)
607 struct ieee802_1x_hdr *hdr;
608 struct wpa_eapol_key *key;
609 u16 key_info, key_data_length;
610 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
611 SMK_M1, SMK_M3, SMK_ERROR } msg;
612 char *msgtxt;
613 struct wpa_eapol_ie_parse kde;
615 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
616 return;
618 if (data_len < sizeof(*hdr) + sizeof(*key))
619 return;
621 hdr = (struct ieee802_1x_hdr *) data;
622 key = (struct wpa_eapol_key *) (hdr + 1);
623 key_info = WPA_GET_BE16(key->key_info);
624 key_data_length = WPA_GET_BE16(key->key_data_length);
625 if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) {
626 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
627 "key_data overflow (%d > %lu)",
628 key_data_length,
629 (unsigned long) (data_len - sizeof(*hdr) -
630 sizeof(*key)));
631 return;
634 if (sm->wpa == WPA_VERSION_WPA2) {
635 if (key->type != EAPOL_KEY_TYPE_RSN) {
636 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
637 "unexpected type %d in RSN mode",
638 key->type);
639 return;
641 } else {
642 if (key->type != EAPOL_KEY_TYPE_WPA) {
643 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
644 "unexpected type %d in WPA mode",
645 key->type);
646 return;
650 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
651 * are set */
653 if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
654 (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
655 if (key_info & WPA_KEY_INFO_ERROR) {
656 msg = SMK_ERROR;
657 msgtxt = "SMK Error";
658 } else {
659 msg = SMK_M1;
660 msgtxt = "SMK M1";
662 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
663 msg = SMK_M3;
664 msgtxt = "SMK M3";
665 } else if (key_info & WPA_KEY_INFO_REQUEST) {
666 msg = REQUEST;
667 msgtxt = "Request";
668 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
669 msg = GROUP_2;
670 msgtxt = "2/2 Group";
671 } else if (key_data_length == 0) {
672 msg = PAIRWISE_4;
673 msgtxt = "4/4 Pairwise";
674 } else {
675 msg = PAIRWISE_2;
676 msgtxt = "2/4 Pairwise";
679 /* TODO: key_info type validation for PeerKey */
680 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
681 msg == GROUP_2) {
682 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
683 if (sm->pairwise == WPA_CIPHER_CCMP) {
684 if (wpa_use_aes_cmac(sm) &&
685 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
686 wpa_auth_logger(wpa_auth, sm->addr,
687 LOGGER_WARNING,
688 "advertised support for "
689 "AES-128-CMAC, but did not "
690 "use it");
691 return;
694 if (!wpa_use_aes_cmac(sm) &&
695 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
696 wpa_auth_logger(wpa_auth, sm->addr,
697 LOGGER_WARNING,
698 "did not use HMAC-SHA1-AES "
699 "with CCMP");
700 return;
705 if (key_info & WPA_KEY_INFO_REQUEST) {
706 if (sm->req_replay_counter_used &&
707 os_memcmp(key->replay_counter, sm->req_replay_counter,
708 WPA_REPLAY_COUNTER_LEN) <= 0) {
709 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
710 "received EAPOL-Key request with "
711 "replayed counter");
712 return;
716 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
717 !wpa_replay_counter_valid(sm, key->replay_counter)) {
718 int i;
719 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
720 "received EAPOL-Key %s with unexpected "
721 "replay counter", msgtxt);
722 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
723 if (!sm->key_replay[i].valid)
724 break;
725 wpa_hexdump(MSG_DEBUG, "pending replay counter",
726 sm->key_replay[i].counter,
727 WPA_REPLAY_COUNTER_LEN);
729 wpa_hexdump(MSG_DEBUG, "received replay counter",
730 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
731 return;
734 switch (msg) {
735 case PAIRWISE_2:
736 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
737 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING) {
738 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
739 "received EAPOL-Key msg 2/4 in "
740 "invalid state (%d) - dropped",
741 sm->wpa_ptk_state);
742 return;
744 if (sm->wpa_ie == NULL ||
745 sm->wpa_ie_len != key_data_length ||
746 os_memcmp(sm->wpa_ie, key + 1, key_data_length) != 0) {
747 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
748 "WPA IE from (Re)AssocReq did not "
749 "match with msg 2/4");
750 if (sm->wpa_ie) {
751 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
752 sm->wpa_ie, sm->wpa_ie_len);
754 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
755 (u8 *) (key + 1), key_data_length);
756 /* MLME-DEAUTHENTICATE.request */
757 wpa_sta_disconnect(wpa_auth, sm->addr);
758 return;
760 break;
761 case PAIRWISE_4:
762 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
763 !sm->PTK_valid) {
764 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
765 "received EAPOL-Key msg 4/4 in "
766 "invalid state (%d) - dropped",
767 sm->wpa_ptk_state);
768 return;
770 break;
771 case GROUP_2:
772 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
773 || !sm->PTK_valid) {
774 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
775 "received EAPOL-Key msg 2/2 in "
776 "invalid state (%d) - dropped",
777 sm->wpa_ptk_group_state);
778 return;
780 break;
781 #ifdef CONFIG_PEERKEY
782 case SMK_M1:
783 case SMK_M3:
784 case SMK_ERROR:
785 if (!wpa_auth->conf.peerkey) {
786 wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
787 "PeerKey use disabled - ignoring message");
788 return;
790 if (!sm->PTK_valid) {
791 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
792 "received EAPOL-Key msg SMK in "
793 "invalid state - dropped");
794 return;
796 break;
797 #else /* CONFIG_PEERKEY */
798 case SMK_M1:
799 case SMK_M3:
800 case SMK_ERROR:
801 return; /* STSL disabled - ignore SMK messages */
802 #endif /* CONFIG_PEERKEY */
803 case REQUEST:
804 break;
807 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
808 "received EAPOL-Key frame (%s)", msgtxt);
810 if (key_info & WPA_KEY_INFO_ACK) {
811 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
812 "received invalid EAPOL-Key: Key Ack set");
813 return;
816 if (!(key_info & WPA_KEY_INFO_MIC)) {
817 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
818 "received invalid EAPOL-Key: Key MIC not set");
819 return;
822 sm->MICVerified = FALSE;
823 if (sm->PTK_valid) {
824 if (wpa_verify_key_mic(&sm->PTK, data, data_len)) {
825 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
826 "received EAPOL-Key with invalid MIC");
827 return;
829 sm->MICVerified = TRUE;
830 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
833 if (key_info & WPA_KEY_INFO_REQUEST) {
834 if (sm->MICVerified) {
835 sm->req_replay_counter_used = 1;
836 os_memcpy(sm->req_replay_counter, key->replay_counter,
837 WPA_REPLAY_COUNTER_LEN);
838 } else {
839 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
840 "received EAPOL-Key request with "
841 "invalid MIC");
842 return;
846 * TODO: should decrypt key data field if encryption was used;
847 * even though MAC address KDE is not normally encrypted,
848 * supplicant is allowed to encrypt it.
850 if (msg == SMK_ERROR) {
851 #ifdef CONFIG_PEERKEY
852 wpa_smk_error(wpa_auth, sm, key);
853 #endif /* CONFIG_PEERKEY */
854 return;
855 } else if (key_info & WPA_KEY_INFO_ERROR) {
856 /* Supplicant reported a Michael MIC error */
857 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
858 "received EAPOL-Key Error Request "
859 "(STA detected Michael MIC failure)");
860 wpa_auth_mic_failure_report(wpa_auth, sm->addr);
861 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
862 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
863 /* Error report is not a request for a new key
864 * handshake, but since Authenticator may do it, let's
865 * change the keys now anyway. */
866 wpa_request_new_ptk(sm);
867 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
868 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
869 "received EAPOL-Key Request for new "
870 "4-Way Handshake");
871 wpa_request_new_ptk(sm);
872 #ifdef CONFIG_PEERKEY
873 } else if (msg == SMK_M1) {
874 wpa_smk_m1(wpa_auth, sm, key);
875 #endif /* CONFIG_PEERKEY */
876 } else if (key_data_length > 0 &&
877 wpa_parse_kde_ies((const u8 *) (key + 1),
878 key_data_length, &kde) == 0 &&
879 kde.mac_addr) {
880 } else {
881 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
882 "received EAPOL-Key Request for GTK "
883 "rekeying");
884 /* FIX: why was this triggering PTK rekeying for the
885 * STA that requested Group Key rekeying?? */
886 /* wpa_request_new_ptk(sta->wpa_sm); */
887 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
888 wpa_rekey_gtk(wpa_auth, NULL);
890 } else {
891 /* Do not allow the same key replay counter to be reused. This
892 * does also invalidate all other pending replay counters if
893 * retransmissions were used, i.e., we will only process one of
894 * the pending replies and ignore rest if more than one is
895 * received. */
896 sm->key_replay[0].valid = FALSE;
899 #ifdef CONFIG_PEERKEY
900 if (msg == SMK_M3) {
901 wpa_smk_m3(wpa_auth, sm, key);
902 return;
904 #endif /* CONFIG_PEERKEY */
906 os_free(sm->last_rx_eapol_key);
907 sm->last_rx_eapol_key = os_malloc(data_len);
908 if (sm->last_rx_eapol_key == NULL)
909 return;
910 os_memcpy(sm->last_rx_eapol_key, data, data_len);
911 sm->last_rx_eapol_key_len = data_len;
913 sm->EAPOLKeyReceived = TRUE;
914 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
915 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
916 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
917 wpa_sm_step(sm);
921 static void wpa_gmk_to_gtk(const u8 *gmk, const u8 *addr, const u8 *gnonce,
922 u8 *gtk, size_t gtk_len)
924 u8 data[ETH_ALEN + WPA_NONCE_LEN];
926 /* GTK = PRF-X(GMK, "Group key expansion", AA || GNonce) */
927 os_memcpy(data, addr, ETH_ALEN);
928 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
930 #ifdef CONFIG_IEEE80211W
931 sha256_prf(gmk, WPA_GMK_LEN, "Group key expansion",
932 data, sizeof(data), gtk, gtk_len);
933 #else /* CONFIG_IEEE80211W */
934 sha1_prf(gmk, WPA_GMK_LEN, "Group key expansion",
935 data, sizeof(data), gtk, gtk_len);
936 #endif /* CONFIG_IEEE80211W */
938 wpa_hexdump_key(MSG_DEBUG, "GMK", gmk, WPA_GMK_LEN);
939 wpa_hexdump_key(MSG_DEBUG, "GTK", gtk, gtk_len);
943 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
945 struct wpa_authenticator *wpa_auth = eloop_ctx;
946 struct wpa_state_machine *sm = timeout_ctx;
948 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
949 sm->TimeoutEvt = TRUE;
950 wpa_sm_step(sm);
954 void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
955 struct wpa_state_machine *sm, int key_info,
956 const u8 *key_rsc, const u8 *nonce,
957 const u8 *kde, size_t kde_len,
958 int keyidx, int encr, int force_version)
960 struct ieee802_1x_hdr *hdr;
961 struct wpa_eapol_key *key;
962 size_t len;
963 int alg;
964 int key_data_len, pad_len = 0;
965 u8 *buf, *pos;
966 int version, pairwise;
967 int i;
969 len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
971 if (force_version)
972 version = force_version;
973 else if (wpa_use_aes_cmac(sm))
974 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
975 else if (sm->pairwise == WPA_CIPHER_CCMP)
976 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
977 else
978 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
980 pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
982 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
983 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
984 "encr=%d)",
985 version,
986 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
987 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
988 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
989 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
990 pairwise, (unsigned long) kde_len, keyidx, encr);
992 key_data_len = kde_len;
994 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
995 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
996 pad_len = key_data_len % 8;
997 if (pad_len)
998 pad_len = 8 - pad_len;
999 key_data_len += pad_len + 8;
1002 len += key_data_len;
1004 hdr = os_zalloc(len);
1005 if (hdr == NULL)
1006 return;
1007 hdr->version = wpa_auth->conf.eapol_version;
1008 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1009 hdr->length = host_to_be16(len - sizeof(*hdr));
1010 key = (struct wpa_eapol_key *) (hdr + 1);
1012 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1013 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1014 key_info |= version;
1015 if (encr && sm->wpa == WPA_VERSION_WPA2)
1016 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1017 if (sm->wpa != WPA_VERSION_WPA2)
1018 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1019 WPA_PUT_BE16(key->key_info, key_info);
1021 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
1022 switch (alg) {
1023 case WPA_CIPHER_CCMP:
1024 WPA_PUT_BE16(key->key_length, 16);
1025 break;
1026 case WPA_CIPHER_TKIP:
1027 WPA_PUT_BE16(key->key_length, 32);
1028 break;
1029 case WPA_CIPHER_WEP40:
1030 WPA_PUT_BE16(key->key_length, 5);
1031 break;
1032 case WPA_CIPHER_WEP104:
1033 WPA_PUT_BE16(key->key_length, 13);
1034 break;
1036 if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
1037 WPA_PUT_BE16(key->key_length, 0);
1039 /* FIX: STSL: what to use as key_replay_counter? */
1040 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1041 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1042 os_memcpy(sm->key_replay[i].counter,
1043 sm->key_replay[i - 1].counter,
1044 WPA_REPLAY_COUNTER_LEN);
1046 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1047 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1048 WPA_REPLAY_COUNTER_LEN);
1049 sm->key_replay[0].valid = TRUE;
1051 if (nonce)
1052 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1054 if (key_rsc)
1055 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1057 if (kde && !encr) {
1058 os_memcpy(key + 1, kde, kde_len);
1059 WPA_PUT_BE16(key->key_data_length, kde_len);
1060 } else if (encr && kde) {
1061 buf = os_zalloc(key_data_len);
1062 if (buf == NULL) {
1063 os_free(hdr);
1064 return;
1066 pos = buf;
1067 os_memcpy(pos, kde, kde_len);
1068 pos += kde_len;
1070 if (pad_len)
1071 *pos++ = 0xdd;
1073 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1074 buf, key_data_len);
1075 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1076 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1077 if (aes_wrap(sm->PTK.kek, (key_data_len - 8) / 8, buf,
1078 (u8 *) (key + 1))) {
1079 os_free(hdr);
1080 os_free(buf);
1081 return;
1083 WPA_PUT_BE16(key->key_data_length, key_data_len);
1084 } else {
1085 u8 ek[32];
1086 os_memcpy(key->key_iv,
1087 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1088 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1089 os_memcpy(ek, key->key_iv, 16);
1090 os_memcpy(ek + 16, sm->PTK.kek, 16);
1091 os_memcpy(key + 1, buf, key_data_len);
1092 rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
1093 WPA_PUT_BE16(key->key_data_length, key_data_len);
1095 os_free(buf);
1098 if (key_info & WPA_KEY_INFO_MIC) {
1099 if (!sm->PTK_valid) {
1100 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1101 "PTK not valid when sending EAPOL-Key "
1102 "frame");
1103 os_free(hdr);
1104 return;
1106 wpa_eapol_key_mic(sm->PTK.kck, version, (u8 *) hdr, len,
1107 key->key_mic);
1110 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1112 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1113 sm->pairwise_set);
1114 os_free(hdr);
1118 static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1119 struct wpa_state_machine *sm, int key_info,
1120 const u8 *key_rsc, const u8 *nonce,
1121 const u8 *kde, size_t kde_len,
1122 int keyidx, int encr)
1124 int timeout_ms;
1125 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1126 int ctr;
1128 if (sm == NULL)
1129 return;
1131 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1132 keyidx, encr, 0);
1134 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1135 if (ctr == 1)
1136 timeout_ms = eapol_key_timeout_first;
1137 else
1138 timeout_ms = eapol_key_timeout_subseq;
1139 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1140 wpa_send_eapol_timeout, wpa_auth, sm);
1144 static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len)
1146 struct ieee802_1x_hdr *hdr;
1147 struct wpa_eapol_key *key;
1148 u16 key_info;
1149 int ret = 0;
1150 u8 mic[16];
1152 if (data_len < sizeof(*hdr) + sizeof(*key))
1153 return -1;
1155 hdr = (struct ieee802_1x_hdr *) data;
1156 key = (struct wpa_eapol_key *) (hdr + 1);
1157 key_info = WPA_GET_BE16(key->key_info);
1158 os_memcpy(mic, key->key_mic, 16);
1159 os_memset(key->key_mic, 0, 16);
1160 if (wpa_eapol_key_mic(PTK->kck, key_info & WPA_KEY_INFO_TYPE_MASK,
1161 data, data_len, key->key_mic) ||
1162 os_memcmp(mic, key->key_mic, 16) != 0)
1163 ret = -1;
1164 os_memcpy(key->key_mic, mic, 16);
1165 return ret;
1169 void wpa_remove_ptk(struct wpa_state_machine *sm)
1171 sm->PTK_valid = FALSE;
1172 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1173 wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, (u8 *) "",
1175 sm->pairwise_set = FALSE;
1176 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1180 void wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
1182 int remove_ptk = 1;
1184 if (sm == NULL)
1185 return;
1187 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1188 "event %d notification", event);
1190 switch (event) {
1191 case WPA_AUTH:
1192 case WPA_ASSOC:
1193 break;
1194 case WPA_DEAUTH:
1195 case WPA_DISASSOC:
1196 sm->DeauthenticationRequest = TRUE;
1197 break;
1198 case WPA_REAUTH:
1199 case WPA_REAUTH_EAPOL:
1200 if (sm->GUpdateStationKeys) {
1202 * Reauthentication cancels the pending group key
1203 * update for this STA.
1205 sm->group->GKeyDoneStations--;
1206 sm->GUpdateStationKeys = FALSE;
1207 sm->PtkGroupInit = TRUE;
1209 sm->ReAuthenticationRequest = TRUE;
1210 break;
1211 case WPA_ASSOC_FT:
1212 #ifdef CONFIG_IEEE80211R
1213 /* Using FT protocol, not WPA auth state machine */
1214 sm->ft_completed = 1;
1215 return;
1216 #else /* CONFIG_IEEE80211R */
1217 break;
1218 #endif /* CONFIG_IEEE80211R */
1221 #ifdef CONFIG_IEEE80211R
1222 sm->ft_completed = 0;
1223 #endif /* CONFIG_IEEE80211R */
1225 #ifdef CONFIG_IEEE80211W
1226 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1227 remove_ptk = 0;
1228 #endif /* CONFIG_IEEE80211W */
1230 if (remove_ptk) {
1231 sm->PTK_valid = FALSE;
1232 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1234 if (event != WPA_REAUTH_EAPOL)
1235 wpa_remove_ptk(sm);
1238 wpa_sm_step(sm);
1242 static enum wpa_alg wpa_alg_enum(int alg)
1244 switch (alg) {
1245 case WPA_CIPHER_CCMP:
1246 return WPA_ALG_CCMP;
1247 case WPA_CIPHER_TKIP:
1248 return WPA_ALG_TKIP;
1249 case WPA_CIPHER_WEP104:
1250 case WPA_CIPHER_WEP40:
1251 return WPA_ALG_WEP;
1252 default:
1253 return WPA_ALG_NONE;
1258 SM_STATE(WPA_PTK, INITIALIZE)
1260 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1261 if (sm->Init) {
1262 /* Init flag is not cleared here, so avoid busy
1263 * loop by claiming nothing changed. */
1264 sm->changed = FALSE;
1267 sm->keycount = 0;
1268 if (sm->GUpdateStationKeys)
1269 sm->group->GKeyDoneStations--;
1270 sm->GUpdateStationKeys = FALSE;
1271 if (sm->wpa == WPA_VERSION_WPA)
1272 sm->PInitAKeys = FALSE;
1273 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1274 * Local AA > Remote AA)) */) {
1275 sm->Pair = TRUE;
1277 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1278 wpa_remove_ptk(sm);
1279 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1280 sm->TimeoutCtr = 0;
1281 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1282 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1283 WPA_EAPOL_authorized, 0);
1288 SM_STATE(WPA_PTK, DISCONNECT)
1290 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1291 sm->Disconnect = FALSE;
1292 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1296 SM_STATE(WPA_PTK, DISCONNECTED)
1298 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1299 sm->DeauthenticationRequest = FALSE;
1303 SM_STATE(WPA_PTK, AUTHENTICATION)
1305 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1306 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1307 sm->PTK_valid = FALSE;
1308 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1310 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1311 sm->AuthenticationRequest = FALSE;
1315 SM_STATE(WPA_PTK, AUTHENTICATION2)
1317 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1318 os_memcpy(sm->ANonce, sm->group->Counter, WPA_NONCE_LEN);
1319 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1320 sm->ReAuthenticationRequest = FALSE;
1321 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1322 * logical place than INITIALIZE since AUTHENTICATION2 can be
1323 * re-entered on ReAuthenticationRequest without going through
1324 * INITIALIZE. */
1325 sm->TimeoutCtr = 0;
1329 SM_STATE(WPA_PTK, INITPMK)
1331 u8 msk[2 * PMK_LEN];
1332 size_t len = 2 * PMK_LEN;
1334 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1335 #ifdef CONFIG_IEEE80211R
1336 sm->xxkey_len = 0;
1337 #endif /* CONFIG_IEEE80211R */
1338 if (sm->pmksa) {
1339 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1340 os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
1341 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1342 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1343 "(len=%lu)", (unsigned long) len);
1344 os_memcpy(sm->PMK, msk, PMK_LEN);
1345 #ifdef CONFIG_IEEE80211R
1346 if (len >= 2 * PMK_LEN) {
1347 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1348 sm->xxkey_len = PMK_LEN;
1350 #endif /* CONFIG_IEEE80211R */
1351 } else {
1352 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
1355 sm->req_replay_counter_used = 0;
1356 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1357 * will break reauthentication since EAPOL state machines may not be
1358 * get into AUTHENTICATING state that clears keyRun before WPA state
1359 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1360 * state and takes PMK from the previously used AAA Key. This will
1361 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1362 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1363 * be good workaround for this issue. */
1364 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1368 SM_STATE(WPA_PTK, INITPSK)
1370 const u8 *psk;
1371 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
1372 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL);
1373 if (psk) {
1374 os_memcpy(sm->PMK, psk, PMK_LEN);
1375 #ifdef CONFIG_IEEE80211R
1376 os_memcpy(sm->xxkey, psk, PMK_LEN);
1377 sm->xxkey_len = PMK_LEN;
1378 #endif /* CONFIG_IEEE80211R */
1380 sm->req_replay_counter_used = 0;
1384 SM_STATE(WPA_PTK, PTKSTART)
1386 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1387 size_t pmkid_len = 0;
1389 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
1390 sm->PTKRequest = FALSE;
1391 sm->TimeoutEvt = FALSE;
1393 sm->TimeoutCtr++;
1394 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1395 /* No point in sending the EAPOL-Key - we will disconnect
1396 * immediately following this. */
1397 return;
1400 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1401 "sending 1/4 msg of 4-Way Handshake");
1403 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1404 * one possible PSK for this STA.
1406 if (sm->wpa == WPA_VERSION_WPA2 &&
1407 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt)) {
1408 pmkid = buf;
1409 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
1410 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
1411 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
1412 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
1413 if (sm->pmksa)
1414 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
1415 sm->pmksa->pmkid, PMKID_LEN);
1416 else {
1418 * Calculate PMKID since no PMKSA cache entry was
1419 * available with pre-calculated PMKID.
1421 rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
1422 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
1423 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1426 wpa_send_eapol(sm->wpa_auth, sm,
1427 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
1428 sm->ANonce, pmkid, pmkid_len, 0, 0);
1432 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk,
1433 struct wpa_ptk *ptk)
1435 size_t ptk_len = sm->pairwise == WPA_CIPHER_CCMP ? 48 : 64;
1436 #ifdef CONFIG_IEEE80211R
1437 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
1438 return wpa_auth_derive_ptk_ft(sm, pmk, ptk, ptk_len);
1439 #endif /* CONFIG_IEEE80211R */
1441 wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
1442 sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce,
1443 (u8 *) ptk, ptk_len,
1444 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1446 return 0;
1450 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
1452 struct wpa_ptk PTK;
1453 int ok = 0;
1454 const u8 *pmk = NULL;
1456 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
1457 sm->EAPOLKeyReceived = FALSE;
1459 /* WPA with IEEE 802.1X: use the derived PMK from EAP
1460 * WPA-PSK: iterate through possible PSKs and select the one matching
1461 * the packet */
1462 for (;;) {
1463 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1464 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, pmk);
1465 if (pmk == NULL)
1466 break;
1467 } else
1468 pmk = sm->PMK;
1470 wpa_derive_ptk(sm, pmk, &PTK);
1472 if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key,
1473 sm->last_rx_eapol_key_len) == 0) {
1474 ok = 1;
1475 break;
1478 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
1479 break;
1482 if (!ok) {
1483 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1484 "invalid MIC in msg 2/4 of 4-Way Handshake");
1485 return;
1488 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
1490 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1491 /* PSK may have changed from the previous choice, so update
1492 * state machine data based on whatever PSK was selected here.
1494 os_memcpy(sm->PMK, pmk, PMK_LEN);
1497 sm->MICVerified = TRUE;
1499 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
1500 sm->PTK_valid = TRUE;
1504 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
1506 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
1507 sm->TimeoutCtr = 0;
1511 #ifdef CONFIG_IEEE80211W
1513 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1515 if (sm->mgmt_frame_prot) {
1516 return 2 + RSN_SELECTOR_LEN + sizeof(struct wpa_igtk_kde);
1519 return 0;
1523 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1525 struct wpa_igtk_kde igtk;
1526 struct wpa_group *gsm = sm->group;
1528 if (!sm->mgmt_frame_prot)
1529 return pos;
1531 igtk.keyid[0] = gsm->GN_igtk;
1532 igtk.keyid[1] = 0;
1533 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, igtk.pn) < 0)
1534 os_memset(igtk.pn, 0, sizeof(igtk.pn));
1535 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
1536 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
1537 (const u8 *) &igtk, sizeof(igtk), NULL, 0);
1539 return pos;
1542 #else /* CONFIG_IEEE80211W */
1544 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1546 return 0;
1550 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1552 return pos;
1555 #endif /* CONFIG_IEEE80211W */
1558 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
1560 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
1561 size_t gtk_len, kde_len;
1562 struct wpa_group *gsm = sm->group;
1563 u8 *wpa_ie;
1564 int wpa_ie_len, secure, keyidx, encr = 0;
1566 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
1567 sm->TimeoutEvt = FALSE;
1569 sm->TimeoutCtr++;
1570 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1571 /* No point in sending the EAPOL-Key - we will disconnect
1572 * immediately following this. */
1573 return;
1576 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, GTK[GN])
1578 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
1579 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
1580 wpa_ie = sm->wpa_auth->wpa_ie;
1581 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
1582 if (sm->wpa == WPA_VERSION_WPA &&
1583 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
1584 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
1585 /* WPA-only STA, remove RSN IE */
1586 wpa_ie = wpa_ie + wpa_ie[1] + 2;
1587 wpa_ie_len = wpa_ie[1] + 2;
1589 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1590 "sending 3/4 msg of 4-Way Handshake");
1591 if (sm->wpa == WPA_VERSION_WPA2) {
1592 /* WPA2 send GTK in the 4-way handshake */
1593 secure = 1;
1594 gtk = gsm->GTK[gsm->GN - 1];
1595 gtk_len = gsm->GTK_len;
1596 keyidx = gsm->GN;
1597 _rsc = rsc;
1598 encr = 1;
1599 } else {
1600 /* WPA does not include GTK in msg 3/4 */
1601 secure = 0;
1602 gtk = NULL;
1603 gtk_len = 0;
1604 keyidx = 0;
1605 _rsc = NULL;
1608 kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
1609 if (gtk)
1610 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
1611 kde = os_malloc(kde_len);
1612 if (kde == NULL)
1613 return;
1615 pos = kde;
1616 os_memcpy(pos, wpa_ie, wpa_ie_len);
1617 pos += wpa_ie_len;
1618 if (gtk) {
1619 u8 hdr[2];
1620 hdr[0] = keyidx & 0x03;
1621 hdr[1] = 0;
1622 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
1623 gtk, gtk_len);
1625 pos = ieee80211w_kde_add(sm, pos);
1627 wpa_send_eapol(sm->wpa_auth, sm,
1628 (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
1629 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
1630 WPA_KEY_INFO_KEY_TYPE,
1631 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
1632 os_free(kde);
1636 SM_STATE(WPA_PTK, PTKINITDONE)
1638 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
1639 sm->EAPOLKeyReceived = FALSE;
1640 if (sm->Pair) {
1641 enum wpa_alg alg;
1642 int klen;
1643 if (sm->pairwise == WPA_CIPHER_TKIP) {
1644 alg = WPA_ALG_TKIP;
1645 klen = 32;
1646 } else {
1647 alg = WPA_ALG_CCMP;
1648 klen = 16;
1650 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
1651 sm->PTK.tk1, klen)) {
1652 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1653 return;
1655 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
1656 sm->pairwise_set = TRUE;
1658 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
1659 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1660 eloop_register_timeout(sm->wpa_auth->conf.
1661 wpa_ptk_rekey, 0, wpa_rekey_ptk,
1662 sm->wpa_auth, sm);
1665 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1666 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1667 WPA_EAPOL_authorized, 1);
1671 if (0 /* IBSS == TRUE */) {
1672 sm->keycount++;
1673 if (sm->keycount == 2) {
1674 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1675 WPA_EAPOL_portValid, 1);
1677 } else {
1678 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
1681 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
1682 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
1683 if (sm->wpa == WPA_VERSION_WPA)
1684 sm->PInitAKeys = TRUE;
1685 else
1686 sm->has_GTK = TRUE;
1687 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1688 "pairwise key handshake completed (%s)",
1689 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
1691 #ifdef CONFIG_IEEE80211R
1692 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
1693 #endif /* CONFIG_IEEE80211R */
1697 SM_STEP(WPA_PTK)
1699 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
1701 if (sm->Init)
1702 SM_ENTER(WPA_PTK, INITIALIZE);
1703 else if (sm->Disconnect
1704 /* || FIX: dot11RSNAConfigSALifetime timeout */)
1705 SM_ENTER(WPA_PTK, DISCONNECT);
1706 else if (sm->DeauthenticationRequest)
1707 SM_ENTER(WPA_PTK, DISCONNECTED);
1708 else if (sm->AuthenticationRequest)
1709 SM_ENTER(WPA_PTK, AUTHENTICATION);
1710 else if (sm->ReAuthenticationRequest)
1711 SM_ENTER(WPA_PTK, AUTHENTICATION2);
1712 else if (sm->PTKRequest)
1713 SM_ENTER(WPA_PTK, PTKSTART);
1714 else switch (sm->wpa_ptk_state) {
1715 case WPA_PTK_INITIALIZE:
1716 break;
1717 case WPA_PTK_DISCONNECT:
1718 SM_ENTER(WPA_PTK, DISCONNECTED);
1719 break;
1720 case WPA_PTK_DISCONNECTED:
1721 SM_ENTER(WPA_PTK, INITIALIZE);
1722 break;
1723 case WPA_PTK_AUTHENTICATION:
1724 SM_ENTER(WPA_PTK, AUTHENTICATION2);
1725 break;
1726 case WPA_PTK_AUTHENTICATION2:
1727 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
1728 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
1729 WPA_EAPOL_keyRun) > 0)
1730 SM_ENTER(WPA_PTK, INITPMK);
1731 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
1732 /* FIX: && 802.1X::keyRun */)
1733 SM_ENTER(WPA_PTK, INITPSK);
1734 break;
1735 case WPA_PTK_INITPMK:
1736 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
1737 WPA_EAPOL_keyAvailable) > 0)
1738 SM_ENTER(WPA_PTK, PTKSTART);
1739 else {
1740 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1741 SM_ENTER(WPA_PTK, DISCONNECT);
1743 break;
1744 case WPA_PTK_INITPSK:
1745 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL))
1746 SM_ENTER(WPA_PTK, PTKSTART);
1747 else {
1748 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1749 "no PSK configured for the STA");
1750 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1751 SM_ENTER(WPA_PTK, DISCONNECT);
1753 break;
1754 case WPA_PTK_PTKSTART:
1755 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1756 sm->EAPOLKeyPairwise)
1757 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
1758 else if (sm->TimeoutCtr >
1759 (int) dot11RSNAConfigPairwiseUpdateCount) {
1760 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1761 SM_ENTER(WPA_PTK, DISCONNECT);
1762 } else if (sm->TimeoutEvt)
1763 SM_ENTER(WPA_PTK, PTKSTART);
1764 break;
1765 case WPA_PTK_PTKCALCNEGOTIATING:
1766 if (sm->MICVerified)
1767 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
1768 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1769 sm->EAPOLKeyPairwise)
1770 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
1771 else if (sm->TimeoutEvt)
1772 SM_ENTER(WPA_PTK, PTKSTART);
1773 break;
1774 case WPA_PTK_PTKCALCNEGOTIATING2:
1775 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
1776 break;
1777 case WPA_PTK_PTKINITNEGOTIATING:
1778 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1779 sm->EAPOLKeyPairwise && sm->MICVerified)
1780 SM_ENTER(WPA_PTK, PTKINITDONE);
1781 else if (sm->TimeoutCtr >
1782 (int) dot11RSNAConfigPairwiseUpdateCount) {
1783 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1784 SM_ENTER(WPA_PTK, DISCONNECT);
1785 } else if (sm->TimeoutEvt)
1786 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
1787 break;
1788 case WPA_PTK_PTKINITDONE:
1789 break;
1794 SM_STATE(WPA_PTK_GROUP, IDLE)
1796 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
1797 if (sm->Init) {
1798 /* Init flag is not cleared here, so avoid busy
1799 * loop by claiming nothing changed. */
1800 sm->changed = FALSE;
1802 sm->GTimeoutCtr = 0;
1806 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
1808 u8 rsc[WPA_KEY_RSC_LEN];
1809 struct wpa_group *gsm = sm->group;
1810 u8 *kde, *pos, hdr[2];
1811 size_t kde_len;
1813 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
1815 sm->GTimeoutCtr++;
1816 if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
1817 /* No point in sending the EAPOL-Key - we will disconnect
1818 * immediately following this. */
1819 return;
1822 if (sm->wpa == WPA_VERSION_WPA)
1823 sm->PInitAKeys = FALSE;
1824 sm->TimeoutEvt = FALSE;
1825 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
1826 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
1827 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
1828 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
1829 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1830 "sending 1/2 msg of Group Key Handshake");
1832 if (sm->wpa == WPA_VERSION_WPA2) {
1833 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
1834 ieee80211w_kde_len(sm);
1835 kde = os_malloc(kde_len);
1836 if (kde == NULL)
1837 return;
1839 pos = kde;
1840 hdr[0] = gsm->GN & 0x03;
1841 hdr[1] = 0;
1842 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
1843 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
1844 pos = ieee80211w_kde_add(sm, pos);
1845 } else {
1846 kde = gsm->GTK[gsm->GN - 1];
1847 pos = kde + gsm->GTK_len;
1850 wpa_send_eapol(sm->wpa_auth, sm,
1851 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
1852 WPA_KEY_INFO_ACK |
1853 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
1854 rsc, gsm->GNonce, kde, pos - kde, gsm->GN, 1);
1855 if (sm->wpa == WPA_VERSION_WPA2)
1856 os_free(kde);
1860 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
1862 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
1863 sm->EAPOLKeyReceived = FALSE;
1864 if (sm->GUpdateStationKeys)
1865 sm->group->GKeyDoneStations--;
1866 sm->GUpdateStationKeys = FALSE;
1867 sm->GTimeoutCtr = 0;
1868 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
1869 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1870 "group key handshake completed (%s)",
1871 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
1872 sm->has_GTK = TRUE;
1876 SM_STATE(WPA_PTK_GROUP, KEYERROR)
1878 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
1879 if (sm->GUpdateStationKeys)
1880 sm->group->GKeyDoneStations--;
1881 sm->GUpdateStationKeys = FALSE;
1882 sm->Disconnect = TRUE;
1886 SM_STEP(WPA_PTK_GROUP)
1888 if (sm->Init || sm->PtkGroupInit) {
1889 SM_ENTER(WPA_PTK_GROUP, IDLE);
1890 sm->PtkGroupInit = FALSE;
1891 } else switch (sm->wpa_ptk_group_state) {
1892 case WPA_PTK_GROUP_IDLE:
1893 if (sm->GUpdateStationKeys ||
1894 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
1895 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
1896 break;
1897 case WPA_PTK_GROUP_REKEYNEGOTIATING:
1898 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1899 !sm->EAPOLKeyPairwise && sm->MICVerified)
1900 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
1901 else if (sm->GTimeoutCtr >
1902 (int) dot11RSNAConfigGroupUpdateCount)
1903 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
1904 else if (sm->TimeoutEvt)
1905 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
1906 break;
1907 case WPA_PTK_GROUP_KEYERROR:
1908 SM_ENTER(WPA_PTK_GROUP, IDLE);
1909 break;
1910 case WPA_PTK_GROUP_REKEYESTABLISHED:
1911 SM_ENTER(WPA_PTK_GROUP, IDLE);
1912 break;
1917 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
1918 struct wpa_group *group)
1920 int ret = 0;
1922 /* FIX: is this the correct way of getting GNonce? */
1923 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
1924 inc_byte_array(group->Counter, WPA_NONCE_LEN);
1925 wpa_gmk_to_gtk(group->GMK, wpa_auth->addr, group->GNonce,
1926 group->GTK[group->GN - 1], group->GTK_len);
1928 #ifdef CONFIG_IEEE80211W
1929 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
1930 if (os_get_random(group->IGTK[group->GN_igtk - 4],
1931 WPA_IGTK_LEN) < 0) {
1932 wpa_printf(MSG_INFO, "RSN: Failed to get new random "
1933 "IGTK");
1934 ret = -1;
1936 wpa_hexdump_key(MSG_DEBUG, "IGTK",
1937 group->IGTK[group->GN_igtk - 4], WPA_IGTK_LEN);
1939 #endif /* CONFIG_IEEE80211W */
1941 return ret;
1945 static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
1946 struct wpa_group *group)
1948 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
1949 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
1950 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
1951 group->wpa_group_state = WPA_GROUP_GTK_INIT;
1953 /* GTK[0..N] = 0 */
1954 os_memset(group->GTK, 0, sizeof(group->GTK));
1955 group->GN = 1;
1956 group->GM = 2;
1957 #ifdef CONFIG_IEEE80211W
1958 group->GN_igtk = 4;
1959 group->GM_igtk = 5;
1960 #endif /* CONFIG_IEEE80211W */
1961 /* GTK[GN] = CalcGTK() */
1962 wpa_gtk_update(wpa_auth, group);
1966 static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
1968 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
1969 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1970 "Not in PTKINITDONE; skip Group Key update");
1971 return 0;
1973 if (sm->GUpdateStationKeys) {
1975 * This should not really happen, but just in case, make sure
1976 * we do not count the same STA twice in GKeyDoneStations.
1978 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1979 "GUpdateStationKeys already set - do not "
1980 "increment GKeyDoneStations");
1981 } else {
1982 sm->group->GKeyDoneStations++;
1983 sm->GUpdateStationKeys = TRUE;
1985 wpa_sm_step(sm);
1986 return 0;
1990 static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
1991 struct wpa_group *group)
1993 int tmp;
1995 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
1996 "SETKEYS (VLAN-ID %d)", group->vlan_id);
1997 group->changed = TRUE;
1998 group->wpa_group_state = WPA_GROUP_SETKEYS;
1999 group->GTKReKey = FALSE;
2000 tmp = group->GM;
2001 group->GM = group->GN;
2002 group->GN = tmp;
2003 #ifdef CONFIG_IEEE80211W
2004 tmp = group->GM_igtk;
2005 group->GM_igtk = group->GN_igtk;
2006 group->GN_igtk = tmp;
2007 #endif /* CONFIG_IEEE80211W */
2008 /* "GKeyDoneStations = GNoStations" is done in more robust way by
2009 * counting the STAs that are marked with GUpdateStationKeys instead of
2010 * including all STAs that could be in not-yet-completed state. */
2011 wpa_gtk_update(wpa_auth, group);
2013 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, NULL);
2014 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
2015 group->GKeyDoneStations);
2019 static void wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
2020 struct wpa_group *group)
2022 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2023 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
2024 group->changed = TRUE;
2025 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
2026 wpa_auth_set_key(wpa_auth, group->vlan_id,
2027 wpa_alg_enum(wpa_auth->conf.wpa_group),
2028 NULL, group->GN, group->GTK[group->GN - 1],
2029 group->GTK_len);
2031 #ifdef CONFIG_IEEE80211W
2032 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
2033 wpa_auth_set_key(wpa_auth, group->vlan_id, WPA_ALG_IGTK,
2034 NULL, group->GN_igtk,
2035 group->IGTK[group->GN_igtk - 4],
2036 WPA_IGTK_LEN);
2038 #endif /* CONFIG_IEEE80211W */
2042 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
2043 struct wpa_group *group)
2045 if (group->GInit) {
2046 wpa_group_gtk_init(wpa_auth, group);
2047 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
2048 group->GTKAuthenticator) {
2049 wpa_group_setkeysdone(wpa_auth, group);
2050 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
2051 group->GTKReKey) {
2052 wpa_group_setkeys(wpa_auth, group);
2053 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
2054 if (group->GKeyDoneStations == 0)
2055 wpa_group_setkeysdone(wpa_auth, group);
2056 else if (group->GTKReKey)
2057 wpa_group_setkeys(wpa_auth, group);
2062 static void wpa_sm_step(struct wpa_state_machine *sm)
2064 if (sm == NULL)
2065 return;
2067 if (sm->in_step_loop) {
2068 /* This should not happen, but if it does, make sure we do not
2069 * end up freeing the state machine too early by exiting the
2070 * recursive call. */
2071 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
2072 return;
2075 sm->in_step_loop = 1;
2076 do {
2077 if (sm->pending_deinit)
2078 break;
2080 sm->changed = FALSE;
2081 sm->wpa_auth->group->changed = FALSE;
2083 SM_STEP_RUN(WPA_PTK);
2084 if (sm->pending_deinit)
2085 break;
2086 SM_STEP_RUN(WPA_PTK_GROUP);
2087 if (sm->pending_deinit)
2088 break;
2089 wpa_group_sm_step(sm->wpa_auth, sm->group);
2090 } while (sm->changed || sm->wpa_auth->group->changed);
2091 sm->in_step_loop = 0;
2093 if (sm->pending_deinit) {
2094 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
2095 "machine deinit for " MACSTR, MAC2STR(sm->addr));
2096 wpa_free_sta_sm(sm);
2101 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
2103 struct wpa_state_machine *sm = eloop_ctx;
2104 wpa_sm_step(sm);
2108 void wpa_auth_sm_notify(struct wpa_state_machine *sm)
2110 if (sm == NULL)
2111 return;
2112 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
2116 void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
2118 int tmp, i;
2119 struct wpa_group *group;
2121 if (wpa_auth == NULL)
2122 return;
2124 group = wpa_auth->group;
2126 for (i = 0; i < 2; i++) {
2127 tmp = group->GM;
2128 group->GM = group->GN;
2129 group->GN = tmp;
2130 #ifdef CONFIG_IEEE80211W
2131 tmp = group->GM_igtk;
2132 group->GM_igtk = group->GN_igtk;
2133 group->GN_igtk = tmp;
2134 #endif /* CONFIG_IEEE80211W */
2135 wpa_gtk_update(wpa_auth, group);
2140 static const char * wpa_bool_txt(int bool)
2142 return bool ? "TRUE" : "FALSE";
2146 static int wpa_cipher_bits(int cipher)
2148 switch (cipher) {
2149 case WPA_CIPHER_CCMP:
2150 return 128;
2151 case WPA_CIPHER_TKIP:
2152 return 256;
2153 case WPA_CIPHER_WEP104:
2154 return 104;
2155 case WPA_CIPHER_WEP40:
2156 return 40;
2157 default:
2158 return 0;
2163 #define RSN_SUITE "%02x-%02x-%02x-%d"
2164 #define RSN_SUITE_ARG(s) \
2165 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2167 int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
2169 int len = 0, ret;
2170 char pmkid_txt[PMKID_LEN * 2 + 1];
2172 if (wpa_auth == NULL)
2173 return len;
2175 ret = os_snprintf(buf + len, buflen - len,
2176 "dot11RSNAOptionImplemented=TRUE\n"
2177 #ifdef CONFIG_RSN_PREAUTH
2178 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2179 #else /* CONFIG_RSN_PREAUTH */
2180 "dot11RSNAPreauthenticationImplemented=FALSE\n"
2181 #endif /* CONFIG_RSN_PREAUTH */
2182 "dot11RSNAEnabled=%s\n"
2183 "dot11RSNAPreauthenticationEnabled=%s\n",
2184 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
2185 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
2186 if (ret < 0 || (size_t) ret >= buflen - len)
2187 return len;
2188 len += ret;
2190 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2191 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
2193 ret = os_snprintf(
2194 buf + len, buflen - len,
2195 "dot11RSNAConfigVersion=%u\n"
2196 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
2197 /* FIX: dot11RSNAConfigGroupCipher */
2198 /* FIX: dot11RSNAConfigGroupRekeyMethod */
2199 /* FIX: dot11RSNAConfigGroupRekeyTime */
2200 /* FIX: dot11RSNAConfigGroupRekeyPackets */
2201 "dot11RSNAConfigGroupRekeyStrict=%u\n"
2202 "dot11RSNAConfigGroupUpdateCount=%u\n"
2203 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
2204 "dot11RSNAConfigGroupCipherSize=%u\n"
2205 "dot11RSNAConfigPMKLifetime=%u\n"
2206 "dot11RSNAConfigPMKReauthThreshold=%u\n"
2207 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
2208 "dot11RSNAConfigSATimeout=%u\n"
2209 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2210 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2211 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2212 "dot11RSNAPMKIDUsed=%s\n"
2213 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2214 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2215 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2216 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
2217 "dot11RSNA4WayHandshakeFailures=%u\n"
2218 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
2219 RSN_VERSION,
2220 !!wpa_auth->conf.wpa_strict_rekey,
2221 dot11RSNAConfigGroupUpdateCount,
2222 dot11RSNAConfigPairwiseUpdateCount,
2223 wpa_cipher_bits(wpa_auth->conf.wpa_group),
2224 dot11RSNAConfigPMKLifetime,
2225 dot11RSNAConfigPMKReauthThreshold,
2226 dot11RSNAConfigSATimeout,
2227 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
2228 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
2229 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
2230 pmkid_txt,
2231 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
2232 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
2233 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
2234 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
2235 wpa_auth->dot11RSNA4WayHandshakeFailures);
2236 if (ret < 0 || (size_t) ret >= buflen - len)
2237 return len;
2238 len += ret;
2240 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
2241 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
2243 /* Private MIB */
2244 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
2245 wpa_auth->group->wpa_group_state);
2246 if (ret < 0 || (size_t) ret >= buflen - len)
2247 return len;
2248 len += ret;
2250 return len;
2254 int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
2256 int len = 0, ret;
2257 u32 pairwise = 0;
2259 if (sm == NULL)
2260 return 0;
2262 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
2264 /* dot11RSNAStatsEntry */
2266 if (sm->wpa == WPA_VERSION_WPA) {
2267 if (sm->pairwise == WPA_CIPHER_CCMP)
2268 pairwise = WPA_CIPHER_SUITE_CCMP;
2269 else if (sm->pairwise == WPA_CIPHER_TKIP)
2270 pairwise = WPA_CIPHER_SUITE_TKIP;
2271 else if (sm->pairwise == WPA_CIPHER_WEP104)
2272 pairwise = WPA_CIPHER_SUITE_WEP104;
2273 else if (sm->pairwise == WPA_CIPHER_WEP40)
2274 pairwise = WPA_CIPHER_SUITE_WEP40;
2275 else if (sm->pairwise == WPA_CIPHER_NONE)
2276 pairwise = WPA_CIPHER_SUITE_NONE;
2277 } else if (sm->wpa == WPA_VERSION_WPA2) {
2278 if (sm->pairwise == WPA_CIPHER_CCMP)
2279 pairwise = RSN_CIPHER_SUITE_CCMP;
2280 else if (sm->pairwise == WPA_CIPHER_TKIP)
2281 pairwise = RSN_CIPHER_SUITE_TKIP;
2282 else if (sm->pairwise == WPA_CIPHER_WEP104)
2283 pairwise = RSN_CIPHER_SUITE_WEP104;
2284 else if (sm->pairwise == WPA_CIPHER_WEP40)
2285 pairwise = RSN_CIPHER_SUITE_WEP40;
2286 else if (sm->pairwise == WPA_CIPHER_NONE)
2287 pairwise = RSN_CIPHER_SUITE_NONE;
2288 } else
2289 return 0;
2291 ret = os_snprintf(
2292 buf + len, buflen - len,
2293 /* TODO: dot11RSNAStatsIndex */
2294 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
2295 "dot11RSNAStatsVersion=1\n"
2296 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
2297 /* TODO: dot11RSNAStatsTKIPICVErrors */
2298 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
2299 "dot11RSNAStatsTKIPRemoveMICFailures=%u\n"
2300 /* TODO: dot11RSNAStatsCCMPReplays */
2301 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
2302 /* TODO: dot11RSNAStatsTKIPReplays */,
2303 MAC2STR(sm->addr),
2304 RSN_SUITE_ARG(pairwise),
2305 sm->dot11RSNAStatsTKIPLocalMICFailures,
2306 sm->dot11RSNAStatsTKIPRemoteMICFailures);
2307 if (ret < 0 || (size_t) ret >= buflen - len)
2308 return len;
2309 len += ret;
2311 /* Private MIB */
2312 ret = os_snprintf(buf + len, buflen - len,
2313 "hostapdWPAPTKState=%d\n"
2314 "hostapdWPAPTKGroupState=%d\n",
2315 sm->wpa_ptk_state,
2316 sm->wpa_ptk_group_state);
2317 if (ret < 0 || (size_t) ret >= buflen - len)
2318 return len;
2319 len += ret;
2321 return len;
2325 void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
2327 if (wpa_auth)
2328 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
2332 int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
2334 return sm && sm->pairwise_set;
2338 int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
2340 return sm->pairwise;
2344 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
2346 if (sm == NULL)
2347 return -1;
2348 return sm->wpa_key_mgmt;
2352 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
2354 if (sm == NULL)
2355 return 0;
2356 return sm->wpa;
2360 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
2361 struct rsn_pmksa_cache_entry *entry)
2363 if (sm == NULL || sm->pmksa != entry)
2364 return -1;
2365 sm->pmksa = NULL;
2366 return 0;
2370 struct rsn_pmksa_cache_entry *
2371 wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
2373 return sm ? sm->pmksa : NULL;
2377 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
2379 if (sm)
2380 sm->dot11RSNAStatsTKIPLocalMICFailures++;
2384 const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
2386 if (wpa_auth == NULL)
2387 return NULL;
2388 *len = wpa_auth->wpa_ie_len;
2389 return wpa_auth->wpa_ie;
2393 int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
2394 int session_timeout, struct eapol_state_machine *eapol)
2396 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2)
2397 return -1;
2399 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
2400 sm->wpa_auth->addr, sm->addr, session_timeout,
2401 eapol, sm->wpa_key_mgmt))
2402 return 0;
2404 return -1;
2408 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
2409 const u8 *pmk, size_t len, const u8 *sta_addr,
2410 int session_timeout,
2411 struct eapol_state_machine *eapol)
2413 if (wpa_auth == NULL)
2414 return -1;
2416 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, wpa_auth->addr,
2417 sta_addr, session_timeout, eapol,
2418 WPA_KEY_MGMT_IEEE8021X))
2419 return 0;
2421 return -1;
2425 static struct wpa_group *
2426 wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
2428 struct wpa_group *group;
2430 if (wpa_auth == NULL || wpa_auth->group == NULL)
2431 return NULL;
2433 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
2434 vlan_id);
2435 group = wpa_group_init(wpa_auth, vlan_id);
2436 if (group == NULL)
2437 return NULL;
2439 group->next = wpa_auth->group->next;
2440 wpa_auth->group->next = group;
2442 return group;
2446 int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
2448 struct wpa_group *group;
2450 if (sm == NULL || sm->wpa_auth == NULL)
2451 return 0;
2453 group = sm->wpa_auth->group;
2454 while (group) {
2455 if (group->vlan_id == vlan_id)
2456 break;
2457 group = group->next;
2460 if (group == NULL) {
2461 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
2462 if (group == NULL)
2463 return -1;
2466 if (sm->group == group)
2467 return 0;
2469 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
2470 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
2472 sm->group = group;
2473 return 0;
2476 #endif /* CONFIG_NATIVE_WINDOWS */