2 * Host AP (software wireless LAN access point) user space daemon for
3 * Host AP kernel driver / WPA Authenticator
4 * Copyright (c) 2004-2005, Jouni Malinen <jkmaline@cc.hut.fi>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Alternatively, this software may be distributed under the terms of BSD
13 * See README and COPYING for more details.
21 #include <netinet/in.h>
34 #include "ieee802_1x.h"
35 #include "ieee802_11.h"
38 #include "l2_packet.h"
39 #include "accounting.h"
40 #include "hostap_common.h"
43 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
44 static void wpa_sm_step(struct wpa_state_machine *sm);
45 static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len);
46 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
47 static void wpa_group_sm_step(struct hostapd_data *hapd);
48 static void pmksa_cache_free(struct hostapd_data *hapd);
49 static struct rsn_pmksa_cache * pmksa_cache_get(struct hostapd_data *hapd,
53 /* Default timeouts are 100 ms, but this seems to be a bit too fast for most
54 * WPA Supplicants, so use a bit longer timeout. */
55 static const u32 dot11RSNAConfigGroupUpdateTimeOut = 1000; /* ms */
56 static const u32 dot11RSNAConfigGroupUpdateCount = 3;
57 static const u32 dot11RSNAConfigPairwiseUpdateTimeOut = 1000; /* ms */
58 static const u32 dot11RSNAConfigPairwiseUpdateCount = 3;
60 /* TODO: make these configurable */
61 static const int dot11RSNAConfigPMKLifetime = 43200;
62 static const int dot11RSNAConfigPMKReauthThreshold = 70;
63 static const int dot11RSNAConfigSATimeout = 60;
64 static const int pmksa_cache_max_entries = 1024;
67 static const int WPA_SELECTOR_LEN = 4;
68 static const u8 WPA_OUI_TYPE[] = { 0x00, 0x50, 0xf2, 1 };
69 static const u16 WPA_VERSION = 1;
70 static const u8 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X[] = { 0x00, 0x50, 0xf2, 1 };
71 static const u8 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X[] = { 0x00, 0x50, 0xf2, 2 };
72 static const u8 WPA_CIPHER_SUITE_NONE[] = { 0x00, 0x50, 0xf2, 0 };
73 static const u8 WPA_CIPHER_SUITE_WEP40[] = { 0x00, 0x50, 0xf2, 1 };
74 static const u8 WPA_CIPHER_SUITE_TKIP[] = { 0x00, 0x50, 0xf2, 2 };
75 static const u8 WPA_CIPHER_SUITE_WRAP[] = { 0x00, 0x50, 0xf2, 3 };
76 static const u8 WPA_CIPHER_SUITE_CCMP[] = { 0x00, 0x50, 0xf2, 4 };
77 static const u8 WPA_CIPHER_SUITE_WEP104[] = { 0x00, 0x50, 0xf2, 5 };
79 static const int RSN_SELECTOR_LEN = 4;
80 static const u16 RSN_VERSION = 1;
81 static const u8 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X[] = { 0x00, 0x0f, 0xac, 1 };
82 static const u8 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X[] = { 0x00, 0x0f, 0xac, 2 };
83 static const u8 RSN_CIPHER_SUITE_NONE[] = { 0x00, 0x0f, 0xac, 0 };
84 static const u8 RSN_CIPHER_SUITE_WEP40[] = { 0x00, 0x0f, 0xac, 1 };
85 static const u8 RSN_CIPHER_SUITE_TKIP[] = { 0x00, 0x0f, 0xac, 2 };
86 static const u8 RSN_CIPHER_SUITE_WRAP[] = { 0x00, 0x0f, 0xac, 3 };
87 static const u8 RSN_CIPHER_SUITE_CCMP[] = { 0x00, 0x0f, 0xac, 4 };
88 static const u8 RSN_CIPHER_SUITE_WEP104[] = { 0x00, 0x0f, 0xac, 5 };
90 /* EAPOL-Key Key Data Encapsulation
91 * GroupKey and STAKey require encryption, otherwise, encryption is optional.
93 static const u8 RSN_KEY_DATA_GROUPKEY[] = { 0x00, 0x0f, 0xac, 1 };
94 static const u8 RSN_KEY_DATA_STAKEY[] = { 0x00, 0x0f, 0xac, 2 };
95 static const u8 RSN_KEY_DATA_MAC_ADDR[] = { 0x00, 0x0f, 0xac, 3 };
96 static const u8 RSN_KEY_DATA_PMKID[] = { 0x00, 0x0f, 0xac, 4 };
99 * 00-50-f2:1 (OUI:OUI type)
100 * 0x01 0x00 (version; little endian)
101 * (all following fields are optional:)
102 * Group Suite Selector (4 octets) (default: TKIP)
103 * Pairwise Suite Count (2 octets, little endian) (default: 1)
104 * Pairwise Suite List (4 * n octets) (default: TKIP)
105 * Authenticated Key Management Suite Count (2 octets, little endian)
107 * Authenticated Key Management Suite List (4 * n octets)
108 * (default: unspec 802.1X)
109 * WPA Capabilities (2 octets, little endian) (default: 0)
118 } __attribute__ ((packed));
122 * 0x01 0x00 (version; little endian)
123 * (all following fields are optional:)
124 * Group Suite Selector (4 octets) (default: CCMP)
125 * Pairwise Suite Count (2 octets, little endian) (default: 1)
126 * Pairwise Suite List (4 * n octets) (default: CCMP)
127 * Authenticated Key Management Suite Count (2 octets, little endian)
129 * Authenticated Key Management Suite List (4 * n octets)
130 * (default: unspec 802.1X)
131 * RSN Capabilities (2 octets, little endian) (default: 0)
132 * PMKID Count (2 octets) (default: 0)
133 * PMKID List (16 * n octets)
137 u8 elem_id; /* WLAN_EID_RSN */
140 } __attribute__ ((packed));
143 static int wpa_write_wpa_ie(struct hostapd_data *hapd, u8 *buf, size_t len)
145 struct wpa_ie_hdr *hdr;
149 hdr = (struct wpa_ie_hdr *) buf;
150 hdr->elem_id = WLAN_EID_GENERIC;
151 memcpy(&hdr->oui, WPA_OUI_TYPE, WPA_SELECTOR_LEN);
152 hdr->version = host_to_le16(WPA_VERSION);
153 pos = (u8 *) (hdr + 1);
155 if (hapd->conf->wpa_group == WPA_CIPHER_CCMP) {
156 memcpy(pos, WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN);
157 } else if (hapd->conf->wpa_group == WPA_CIPHER_TKIP) {
158 memcpy(pos, WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN);
159 } else if (hapd->conf->wpa_group == WPA_CIPHER_WEP104) {
160 memcpy(pos, WPA_CIPHER_SUITE_WEP104, WPA_SELECTOR_LEN);
161 } else if (hapd->conf->wpa_group == WPA_CIPHER_WEP40) {
162 memcpy(pos, WPA_CIPHER_SUITE_WEP40, WPA_SELECTOR_LEN);
164 printf("Invalid group cipher (%d).\n", hapd->conf->wpa_group);
167 pos += WPA_SELECTOR_LEN;
173 if (hapd->conf->wpa_pairwise & WPA_CIPHER_CCMP) {
174 memcpy(pos, WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN);
175 pos += WPA_SELECTOR_LEN;
178 if (hapd->conf->wpa_pairwise & WPA_CIPHER_TKIP) {
179 memcpy(pos, WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN);
180 pos += WPA_SELECTOR_LEN;
183 if (hapd->conf->wpa_pairwise & WPA_CIPHER_NONE) {
184 memcpy(pos, WPA_CIPHER_SUITE_NONE, WPA_SELECTOR_LEN);
185 pos += WPA_SELECTOR_LEN;
189 if (num_suites == 0) {
190 printf("Invalid pairwise cipher (%d).\n",
191 hapd->conf->wpa_pairwise);
194 *count++ = num_suites & 0xff;
195 *count = (num_suites >> 8) & 0xff;
201 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
202 memcpy(pos, WPA_AUTH_KEY_MGMT_UNSPEC_802_1X, WPA_SELECTOR_LEN);
203 pos += WPA_SELECTOR_LEN;
206 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
207 memcpy(pos, WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X,
209 pos += WPA_SELECTOR_LEN;
213 if (num_suites == 0) {
214 printf("Invalid key management type (%d).\n",
215 hapd->conf->wpa_key_mgmt);
218 *count++ = num_suites & 0xff;
219 *count = (num_suites >> 8) & 0xff;
221 /* WPA Capabilities; use defaults, so no need to include it */
223 hdr->len = (pos - buf) - 2;
229 static int wpa_write_rsn_ie(struct hostapd_data *hapd, u8 *buf, size_t len)
231 struct rsn_ie_hdr *hdr;
235 hdr = (struct rsn_ie_hdr *) buf;
236 hdr->elem_id = WLAN_EID_RSN;
237 pos = (u8 *) &hdr->version;
238 *pos++ = RSN_VERSION & 0xff;
239 *pos++ = RSN_VERSION >> 8;
240 pos = (u8 *) (hdr + 1);
242 if (hapd->conf->wpa_group == WPA_CIPHER_CCMP) {
243 memcpy(pos, RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN);
244 } else if (hapd->conf->wpa_group == WPA_CIPHER_TKIP) {
245 memcpy(pos, RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN);
246 } else if (hapd->conf->wpa_group == WPA_CIPHER_WEP104) {
247 memcpy(pos, RSN_CIPHER_SUITE_WEP104, RSN_SELECTOR_LEN);
248 } else if (hapd->conf->wpa_group == WPA_CIPHER_WEP40) {
249 memcpy(pos, RSN_CIPHER_SUITE_WEP40, RSN_SELECTOR_LEN);
251 printf("Invalid group cipher (%d).\n", hapd->conf->wpa_group);
254 pos += RSN_SELECTOR_LEN;
260 if (hapd->conf->wpa_pairwise & WPA_CIPHER_CCMP) {
261 memcpy(pos, RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN);
262 pos += RSN_SELECTOR_LEN;
265 if (hapd->conf->wpa_pairwise & WPA_CIPHER_TKIP) {
266 memcpy(pos, RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN);
267 pos += RSN_SELECTOR_LEN;
270 if (hapd->conf->wpa_pairwise & WPA_CIPHER_NONE) {
271 memcpy(pos, RSN_CIPHER_SUITE_NONE, RSN_SELECTOR_LEN);
272 pos += RSN_SELECTOR_LEN;
276 if (num_suites == 0) {
277 printf("Invalid pairwise cipher (%d).\n",
278 hapd->conf->wpa_pairwise);
281 *count++ = num_suites & 0xff;
282 *count = (num_suites >> 8) & 0xff;
288 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
289 memcpy(pos, RSN_AUTH_KEY_MGMT_UNSPEC_802_1X, RSN_SELECTOR_LEN);
290 pos += RSN_SELECTOR_LEN;
293 if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
294 memcpy(pos, RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X,
296 pos += RSN_SELECTOR_LEN;
300 if (num_suites == 0) {
301 printf("Invalid key management type (%d).\n",
302 hapd->conf->wpa_key_mgmt);
305 *count++ = num_suites & 0xff;
306 *count = (num_suites >> 8) & 0xff;
308 /* RSN Capabilities */
309 *pos++ = hapd->conf->rsn_preauth ? BIT(0) : 0;
312 hdr->len = (pos - buf) - 2;
318 static int wpa_gen_wpa_ie(struct hostapd_data *hapd)
325 if (hapd->conf->wpa & HOSTAPD_WPA_VERSION_WPA2) {
326 res = wpa_write_rsn_ie(hapd, pos, buf + sizeof(buf) - pos);
331 if (hapd->conf->wpa & HOSTAPD_WPA_VERSION_WPA) {
332 res = wpa_write_wpa_ie(hapd, pos, buf + sizeof(buf) - pos);
339 hapd->wpa_ie = malloc(pos - buf);
340 if (hapd->wpa_ie == NULL)
342 memcpy(hapd->wpa_ie, buf, pos - buf);
343 hapd->wpa_ie_len = pos - buf;
349 static void wpa_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta)
351 hostapd_sta_deauth(hapd, sta->addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
352 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
353 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
354 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
355 sta->timeout_next = STA_REMOVE;
359 static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
361 struct hostapd_data *hapd = eloop_ctx;
363 if (hapd->wpa_auth) {
364 if (hostapd_get_rand(hapd->wpa_auth->GMK, WPA_GMK_LEN)) {
365 printf("Failed to get random data for WPA "
366 "initialization.\n");
368 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_WPA,
374 if (hapd->conf->wpa_gmk_rekey) {
375 eloop_register_timeout(hapd->conf->wpa_gmk_rekey, 0,
376 wpa_rekey_gmk, hapd, NULL);
381 static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
383 struct hostapd_data *hapd = eloop_ctx;
385 if (hapd->wpa_auth) {
386 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_WPA,
387 HOSTAPD_LEVEL_DEBUG, "rekeying GTK");
388 hapd->wpa_auth->GTKReKey = TRUE;
390 hapd->wpa_auth->changed = FALSE;
391 wpa_group_sm_step(hapd);
392 } while (hapd->wpa_auth->changed);
394 if (hapd->conf->wpa_group_rekey) {
395 eloop_register_timeout(hapd->conf->wpa_group_rekey, 0,
396 wpa_rekey_gtk, hapd, NULL);
401 #ifdef CONFIG_RSN_PREAUTH
403 static void rsn_preauth_receive(void *ctx, const u8 *src_addr,
404 const u8 *buf, size_t len)
406 struct rsn_preauth_interface *piface = ctx;
407 struct hostapd_data *hapd = piface->hapd;
408 struct ieee802_1x_hdr *hdr;
409 struct sta_info *sta;
410 struct l2_ethhdr *ethhdr;
412 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "RSN: receive pre-auth packet "
413 "from interface '%s'\n", piface->ifname);
414 if (len < sizeof(*ethhdr) + sizeof(*hdr)) {
415 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "RSN: too short pre-auth "
416 "packet (len=%lu)\n", (unsigned long) len);
420 ethhdr = (struct l2_ethhdr *) buf;
421 hdr = (struct ieee802_1x_hdr *) (ethhdr + 1);
423 if (memcmp(ethhdr->h_dest, hapd->own_addr, ETH_ALEN) != 0) {
424 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "RSN: pre-auth for "
425 "foreign address " MACSTR "\n",
426 MAC2STR(ethhdr->h_dest));
430 sta = ap_get_sta(hapd, ethhdr->h_source);
431 if (sta && (sta->flags & WLAN_STA_ASSOC)) {
432 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "RSN: pre-auth for "
433 "already association STA " MACSTR "\n",
437 if (!sta && hdr->type == IEEE802_1X_TYPE_EAPOL_START) {
438 sta = (struct sta_info *) malloc(sizeof(struct sta_info));
441 memset(sta, 0, sizeof(*sta));
442 memcpy(sta->addr, ethhdr->h_source, ETH_ALEN);
443 sta->flags = WLAN_STA_PREAUTH;
444 sta->next = hapd->sta_list;
445 sta->wpa = WPA_VERSION_WPA2;
446 hapd->sta_list = sta;
448 ap_sta_hash_add(hapd, sta);
450 ieee802_1x_new_station(hapd, sta);
451 if (sta->eapol_sm == NULL) {
452 ap_free_sta(hapd, sta);
455 sta->eapol_sm->radius_identifier = -1;
456 sta->eapol_sm->portValid = TRUE;
457 sta->eapol_sm->flags |= EAPOL_SM_PREAUTH;
462 sta->preauth_iface = piface;
463 ieee802_1x_receive(hapd, ethhdr->h_source, (u8 *) (ethhdr + 1),
464 len - sizeof(*ethhdr));
468 static int rsn_preauth_iface_add(struct hostapd_data *hapd, const char *ifname)
470 struct rsn_preauth_interface *piface;
472 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "RSN pre-auth interface '%s'\n",
475 piface = malloc(sizeof(*piface));
478 memset(piface, 0, sizeof(*piface));
481 piface->ifname = strdup(ifname);
482 if (piface->ifname == NULL) {
486 piface->l2 = l2_packet_init(piface->ifname, NULL, ETH_P_PREAUTH,
487 rsn_preauth_receive, piface, 1);
488 if (piface->l2 == NULL) {
489 printf("Failed to open register layer 2 access to "
494 piface->next = hapd->preauth_iface;
495 hapd->preauth_iface = piface;
499 free(piface->ifname);
506 static void rsn_preauth_iface_deinit(struct hostapd_data *hapd)
508 struct rsn_preauth_interface *piface, *prev;
510 piface = hapd->preauth_iface;
511 hapd->preauth_iface = NULL;
514 piface = piface->next;
515 l2_packet_deinit(prev->l2);
522 static int rsn_preauth_iface_init(struct hostapd_data *hapd)
524 char *tmp, *start, *end;
526 if (hapd->conf->rsn_preauth_interfaces == NULL)
529 tmp = strdup(hapd->conf->rsn_preauth_interfaces);
534 while (*start == ' ')
538 end = strchr(start, ' ');
542 if (rsn_preauth_iface_add(hapd, start)) {
543 rsn_preauth_iface_deinit(hapd);
557 static void rsn_preauth_finished_cb(void *eloop_ctx, void *timeout_ctx)
559 struct hostapd_data *hapd = eloop_ctx;
560 struct sta_info *sta = timeout_ctx;
561 wpa_printf(MSG_DEBUG, "RSN: Removing pre-authentication STA entry for "
562 MACSTR, MAC2STR(sta->addr));
563 ap_free_sta(hapd, sta);
567 void rsn_preauth_finished(struct hostapd_data *hapd, struct sta_info *sta,
572 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
573 HOSTAPD_LEVEL_INFO, "pre-authentication %s",
574 success ? "succeeded" : "failed");
576 key = ieee802_1x_get_key_crypt(sta->eapol_sm, &len);
577 if (success && key) {
578 pmksa_cache_add(hapd, sta, key, dot11RSNAConfigPMKLifetime);
582 * Finish STA entry removal from timeout in order to avoid freeing
583 * STA data before the caller has finished processing.
585 eloop_register_timeout(0, 0, rsn_preauth_finished_cb, hapd, sta);
589 void rsn_preauth_send(struct hostapd_data *hapd, struct sta_info *sta,
592 struct rsn_preauth_interface *piface;
593 struct l2_ethhdr *ethhdr;
595 piface = hapd->preauth_iface;
597 if (piface == sta->preauth_iface)
599 piface = piface->next;
602 if (piface == NULL) {
603 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "RSN: Could not find "
604 "pre-authentication interface for " MACSTR "\n",
609 ethhdr = malloc(sizeof(*ethhdr) + len);
613 memcpy(ethhdr->h_dest, sta->addr, ETH_ALEN);
614 memcpy(ethhdr->h_source, hapd->own_addr, ETH_ALEN);
615 ethhdr->h_proto = htons(ETH_P_PREAUTH);
616 memcpy(ethhdr + 1, buf, len);
618 if (l2_packet_send(piface->l2, sta->addr, ETH_P_PREAUTH, (u8 *) ethhdr,
619 sizeof(*ethhdr) + len) < 0) {
620 printf("Failed to send preauth packet using l2_packet_send\n");
625 #else /* CONFIG_RSN_PREAUTH */
627 static inline int rsn_preauth_iface_init(struct hostapd_data *hapd)
632 static inline void rsn_preauth_iface_deinit(struct hostapd_data *hapd)
636 static void rsn_preauth_finished_cb(void *eloop_ctx, void *timeout_ctx)
640 void rsn_preauth_finished(struct hostapd_data *hapd, struct sta_info *sta,
645 void rsn_preauth_send(struct hostapd_data *hapd, struct sta_info *sta,
650 #endif /* CONFIG_RSN_PREAUTH */
653 int wpa_init(struct hostapd_data *hapd)
656 u8 buf[ETH_ALEN + 8];
658 if (rsn_preauth_iface_init(hapd))
661 if (hostapd_set_privacy(hapd, 1)) {
662 printf("Could not set PrivacyInvoked for interface %s\n",
667 if (wpa_gen_wpa_ie(hapd)) {
668 printf("Could not generate WPA IE.\n");
672 if (hostapd_set_generic_elem(hapd, hapd->wpa_ie, hapd->wpa_ie_len)) {
673 printf("Failed to configure WPA IE for the kernel driver.\n");
677 hapd->wpa_auth = malloc(sizeof(struct wpa_authenticator));
678 if (hapd->wpa_auth == NULL)
680 memset(hapd->wpa_auth, 0, sizeof(struct wpa_authenticator));
681 hapd->wpa_auth->GTKAuthenticator = TRUE;
682 switch (hapd->conf->wpa_group) {
683 case WPA_CIPHER_CCMP:
684 hapd->wpa_auth->GTK_len = 16;
686 case WPA_CIPHER_TKIP:
687 hapd->wpa_auth->GTK_len = 32;
689 case WPA_CIPHER_WEP104:
690 hapd->wpa_auth->GTK_len = 13;
692 case WPA_CIPHER_WEP40:
693 hapd->wpa_auth->GTK_len = 5;
697 /* Counter = PRF-256(Random number, "Init Counter",
698 * Local MAC Address || Time)
700 memcpy(buf, hapd->own_addr, ETH_ALEN);
701 hostapd_get_ntp_timestamp(buf + ETH_ALEN);
702 if (hostapd_get_rand(rkey, sizeof(rkey)) ||
703 hostapd_get_rand(hapd->wpa_auth->GMK, WPA_GMK_LEN)) {
704 printf("Failed to get random data for WPA initialization.\n");
705 free(hapd->wpa_auth);
706 hapd->wpa_auth = NULL;
710 sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
711 hapd->wpa_auth->Counter, WPA_NONCE_LEN);
713 if (hapd->conf->wpa_gmk_rekey) {
714 eloop_register_timeout(hapd->conf->wpa_gmk_rekey, 0,
715 wpa_rekey_gmk, hapd, NULL);
718 if (hapd->conf->wpa_group_rekey) {
719 eloop_register_timeout(hapd->conf->wpa_group_rekey, 0,
720 wpa_rekey_gtk, hapd, NULL);
723 hapd->wpa_auth->GInit = TRUE;
724 wpa_group_sm_step(hapd);
725 hapd->wpa_auth->GInit = FALSE;
726 wpa_group_sm_step(hapd);
732 void wpa_deinit(struct hostapd_data *hapd)
734 rsn_preauth_iface_deinit(hapd);
736 eloop_cancel_timeout(wpa_rekey_gmk, hapd, NULL);
737 eloop_cancel_timeout(wpa_rekey_gtk, hapd, NULL);
739 if (hostapd_set_privacy(hapd, 0)) {
740 printf("Could not disable PrivacyInvoked for interface %s\n",
744 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
745 printf("Could not remove generic information element from "
746 "interface %s\n", hapd->conf->iface);
751 free(hapd->wpa_auth);
752 hapd->wpa_auth = NULL;
754 pmksa_cache_free(hapd);
758 static int wpa_selector_to_bitfield(u8 *s)
760 if (memcmp(s, WPA_CIPHER_SUITE_NONE, WPA_SELECTOR_LEN) == 0)
761 return WPA_CIPHER_NONE;
762 if (memcmp(s, WPA_CIPHER_SUITE_WEP40, WPA_SELECTOR_LEN) == 0)
763 return WPA_CIPHER_WEP40;
764 if (memcmp(s, WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN) == 0)
765 return WPA_CIPHER_TKIP;
766 if (memcmp(s, WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN) == 0)
767 return WPA_CIPHER_CCMP;
768 if (memcmp(s, WPA_CIPHER_SUITE_WEP104, WPA_SELECTOR_LEN) == 0)
769 return WPA_CIPHER_WEP104;
774 static int wpa_key_mgmt_to_bitfield(u8 *s)
776 if (memcmp(s, WPA_AUTH_KEY_MGMT_UNSPEC_802_1X, WPA_SELECTOR_LEN) == 0)
777 return WPA_KEY_MGMT_IEEE8021X;
778 if (memcmp(s, WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X, WPA_SELECTOR_LEN) ==
780 return WPA_KEY_MGMT_PSK;
785 static int rsn_selector_to_bitfield(u8 *s)
787 if (memcmp(s, RSN_CIPHER_SUITE_NONE, RSN_SELECTOR_LEN) == 0)
788 return WPA_CIPHER_NONE;
789 if (memcmp(s, RSN_CIPHER_SUITE_WEP40, RSN_SELECTOR_LEN) == 0)
790 return WPA_CIPHER_WEP40;
791 if (memcmp(s, RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN) == 0)
792 return WPA_CIPHER_TKIP;
793 if (memcmp(s, RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN) == 0)
794 return WPA_CIPHER_CCMP;
795 if (memcmp(s, RSN_CIPHER_SUITE_WEP104, RSN_SELECTOR_LEN) == 0)
796 return WPA_CIPHER_WEP104;
801 static int rsn_key_mgmt_to_bitfield(u8 *s)
803 if (memcmp(s, RSN_AUTH_KEY_MGMT_UNSPEC_802_1X, RSN_SELECTOR_LEN) == 0)
804 return WPA_KEY_MGMT_IEEE8021X;
805 if (memcmp(s, RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X, RSN_SELECTOR_LEN) ==
807 return WPA_KEY_MGMT_PSK;
812 static void rsn_pmkid(const u8 *pmk, const u8 *aa, const u8 *spa, u8 *pmkid)
814 char *title = "PMK Name";
816 const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
817 unsigned char hash[SHA1_MAC_LEN];
819 addr[0] = (u8 *) title;
823 hmac_sha1_vector(pmk, PMK_LEN, 3, addr, len, hash);
824 memcpy(pmkid, hash, PMKID_LEN);
828 static void pmksa_cache_set_expiration(struct hostapd_data *hapd);
831 static void _pmksa_cache_free_entry(struct rsn_pmksa_cache *entry)
835 free(entry->identity);
836 ieee802_1x_free_radius_class(&entry->radius_class);
841 static void pmksa_cache_free_entry(struct hostapd_data *hapd,
842 struct rsn_pmksa_cache *entry)
844 struct sta_info *sta;
845 struct rsn_pmksa_cache *pos, *prev;
847 for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
848 if (sta->pmksa == entry)
851 pos = hapd->pmkid[PMKID_HASH(entry->pmkid)];
856 prev->hnext = pos->hnext;
858 hapd->pmkid[PMKID_HASH(entry->pmkid)] =
872 prev->next = pos->next;
874 hapd->pmksa = pos->next;
880 _pmksa_cache_free_entry(entry);
884 static void pmksa_cache_expire(void *eloop_ctx, void *timeout_ctx)
886 struct hostapd_data *hapd = eloop_ctx;
890 while (hapd->pmksa && hapd->pmksa->expiration <= now) {
891 struct rsn_pmksa_cache *entry = hapd->pmksa;
892 hapd->pmksa = entry->next;
893 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
894 "RSN: expired PMKSA cache entry for "
895 MACSTR, MAC2STR(entry->spa));
896 pmksa_cache_free_entry(hapd, entry);
899 pmksa_cache_set_expiration(hapd);
903 static void pmksa_cache_set_expiration(struct hostapd_data *hapd)
906 eloop_cancel_timeout(pmksa_cache_expire, hapd, NULL);
907 if (hapd->pmksa == NULL)
909 sec = hapd->pmksa->expiration - time(NULL);
912 eloop_register_timeout(sec + 1, 0, pmksa_cache_expire, hapd, NULL);
916 static void pmksa_cache_from_eapol_data(struct rsn_pmksa_cache *entry,
917 struct eapol_state_machine *eapol)
922 if (eapol->identity) {
923 entry->identity = malloc(eapol->identity_len);
924 if (entry->identity) {
925 entry->identity_len = eapol->identity_len;
926 memcpy(entry->identity, eapol->identity,
927 eapol->identity_len);
931 ieee802_1x_copy_radius_class(&entry->radius_class,
932 &eapol->radius_class);
936 static void pmksa_cache_to_eapol_data(struct rsn_pmksa_cache *entry,
937 struct eapol_state_machine *eapol)
939 if (entry == NULL || eapol == NULL)
942 if (entry->identity) {
943 free(eapol->identity);
944 eapol->identity = malloc(entry->identity_len);
945 if (eapol->identity) {
946 eapol->identity_len = entry->identity_len;
947 memcpy(eapol->identity, entry->identity,
948 entry->identity_len);
950 wpa_hexdump_ascii(MSG_DEBUG, "STA identity from PMKSA",
951 eapol->identity, eapol->identity_len);
954 ieee802_1x_free_radius_class(&eapol->radius_class);
955 ieee802_1x_copy_radius_class(&eapol->radius_class,
956 &entry->radius_class);
957 if (eapol->radius_class.attr) {
958 wpa_printf(MSG_DEBUG, "Copied %lu Class attribute(s) from "
959 "PMKSA", (unsigned long) eapol->radius_class.count);
964 void pmksa_cache_add(struct hostapd_data *hapd, struct sta_info *sta, u8 *pmk,
967 struct rsn_pmksa_cache *entry, *pos, *prev;
969 if (sta->wpa != WPA_VERSION_WPA2)
972 entry = malloc(sizeof(*entry));
975 memset(entry, 0, sizeof(*entry));
976 memcpy(entry->pmk, pmk, PMK_LEN);
977 rsn_pmkid(pmk, hapd->own_addr, sta->addr, entry->pmkid);
978 time(&entry->expiration);
979 if (session_timeout > 0)
980 entry->expiration += session_timeout;
982 entry->expiration += dot11RSNAConfigPMKLifetime;
983 entry->akmp = WPA_KEY_MGMT_IEEE8021X;
984 memcpy(entry->spa, sta->addr, ETH_ALEN);
985 pmksa_cache_from_eapol_data(entry, sta->eapol_sm);
987 /* Replace an old entry for the same STA (if found) with the new entry
989 pos = pmksa_cache_get(hapd, sta->addr, NULL);
991 pmksa_cache_free_entry(hapd, pos);
993 if (hapd->pmksa_count >= pmksa_cache_max_entries && hapd->pmksa) {
994 /* Remove the oldest entry to make room for the new entry */
995 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
996 "RSN: removed the oldest PMKSA cache entry (for "
997 MACSTR ") to make room for new one",
998 MAC2STR(hapd->pmksa->spa));
999 pmksa_cache_free_entry(hapd, hapd->pmksa);
1002 /* Add the new entry; order by expiration time */
1006 if (pos->expiration > entry->expiration)
1012 entry->next = hapd->pmksa;
1013 hapd->pmksa = entry;
1015 entry->next = prev->next;
1018 entry->hnext = hapd->pmkid[PMKID_HASH(entry->pmkid)];
1019 hapd->pmkid[PMKID_HASH(entry->pmkid)] = entry;
1021 hapd->pmksa_count++;
1022 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1023 HOSTAPD_LEVEL_DEBUG,
1024 "added PMKSA cache entry");
1025 if (HOSTAPD_DEBUG_COND(HOSTAPD_DEBUG_MINIMAL)) {
1026 hostapd_hexdump("RSN: added PMKID", entry->pmkid, PMKID_LEN);
1031 static void pmksa_cache_free(struct hostapd_data *hapd)
1033 struct rsn_pmksa_cache *entry, *prev;
1035 struct sta_info *sta;
1037 entry = hapd->pmksa;
1041 entry = entry->next;
1042 _pmksa_cache_free_entry(prev);
1044 eloop_cancel_timeout(pmksa_cache_expire, hapd, NULL);
1045 for (i = 0; i < PMKID_HASH_SIZE; i++)
1046 hapd->pmkid[i] = NULL;
1047 for (sta = hapd->sta_list; sta; sta = sta->next)
1052 static struct rsn_pmksa_cache * pmksa_cache_get(struct hostapd_data *hapd,
1055 struct rsn_pmksa_cache *entry;
1058 entry = hapd->pmkid[PMKID_HASH(pmkid)];
1060 entry = hapd->pmksa;
1062 if ((spa == NULL || memcmp(entry->spa, spa, ETH_ALEN) == 0) &&
1064 memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0))
1066 entry = pmkid ? entry->hnext : entry->next;
1072 struct wpa_ie_data {
1073 int pairwise_cipher;
1082 static int wpa_parse_wpa_ie_wpa(const u8 *wpa_ie, size_t wpa_ie_len,
1083 struct wpa_ie_data *data)
1085 struct wpa_ie_hdr *hdr;
1090 memset(data, 0, sizeof(*data));
1091 data->pairwise_cipher = WPA_CIPHER_TKIP;
1092 data->group_cipher = WPA_CIPHER_TKIP;
1093 data->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1095 if (wpa_ie_len < sizeof(struct wpa_ie_hdr))
1098 hdr = (struct wpa_ie_hdr *) wpa_ie;
1100 if (hdr->elem_id != WLAN_EID_GENERIC ||
1101 hdr->len != wpa_ie_len - 2 ||
1102 memcmp(&hdr->oui, WPA_OUI_TYPE, WPA_SELECTOR_LEN) != 0 ||
1103 le_to_host16(hdr->version) != WPA_VERSION) {
1107 pos = (u8 *) (hdr + 1);
1108 left = wpa_ie_len - sizeof(*hdr);
1110 if (left >= WPA_SELECTOR_LEN) {
1111 data->group_cipher = wpa_selector_to_bitfield(pos);
1112 pos += WPA_SELECTOR_LEN;
1113 left -= WPA_SELECTOR_LEN;
1114 } else if (left > 0)
1118 data->pairwise_cipher = 0;
1119 count = pos[0] | (pos[1] << 8);
1122 if (count == 0 || left < count * WPA_SELECTOR_LEN)
1124 for (i = 0; i < count; i++) {
1125 data->pairwise_cipher |= wpa_selector_to_bitfield(pos);
1126 pos += WPA_SELECTOR_LEN;
1127 left -= WPA_SELECTOR_LEN;
1129 } else if (left == 1)
1134 count = pos[0] | (pos[1] << 8);
1137 if (count == 0 || left < count * WPA_SELECTOR_LEN)
1139 for (i = 0; i < count; i++) {
1140 data->key_mgmt |= wpa_key_mgmt_to_bitfield(pos);
1141 pos += WPA_SELECTOR_LEN;
1142 left -= WPA_SELECTOR_LEN;
1144 } else if (left == 1)
1148 data->capabilities = pos[0] | (pos[1] << 8);
1161 static int wpa_parse_wpa_ie_rsn(const u8 *rsn_ie, size_t rsn_ie_len,
1162 struct wpa_ie_data *data)
1164 struct rsn_ie_hdr *hdr;
1169 memset(data, 0, sizeof(*data));
1170 data->pairwise_cipher = WPA_CIPHER_CCMP;
1171 data->group_cipher = WPA_CIPHER_CCMP;
1172 data->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1174 if (rsn_ie_len < sizeof(struct rsn_ie_hdr))
1177 hdr = (struct rsn_ie_hdr *) rsn_ie;
1179 if (hdr->elem_id != WLAN_EID_RSN ||
1180 hdr->len != rsn_ie_len - 2 ||
1181 le_to_host16(hdr->version) != RSN_VERSION) {
1185 pos = (u8 *) (hdr + 1);
1186 left = rsn_ie_len - sizeof(*hdr);
1188 if (left >= RSN_SELECTOR_LEN) {
1189 data->group_cipher = rsn_selector_to_bitfield(pos);
1190 pos += RSN_SELECTOR_LEN;
1191 left -= RSN_SELECTOR_LEN;
1192 } else if (left > 0)
1196 data->pairwise_cipher = 0;
1197 count = pos[0] | (pos[1] << 8);
1200 if (count == 0 || left < count * RSN_SELECTOR_LEN)
1202 for (i = 0; i < count; i++) {
1203 data->pairwise_cipher |= rsn_selector_to_bitfield(pos);
1204 pos += RSN_SELECTOR_LEN;
1205 left -= RSN_SELECTOR_LEN;
1207 } else if (left == 1)
1212 count = pos[0] | (pos[1] << 8);
1215 if (count == 0 || left < count * RSN_SELECTOR_LEN)
1217 for (i = 0; i < count; i++) {
1218 data->key_mgmt |= rsn_key_mgmt_to_bitfield(pos);
1219 pos += RSN_SELECTOR_LEN;
1220 left -= RSN_SELECTOR_LEN;
1222 } else if (left == 1)
1226 data->capabilities = pos[0] | (pos[1] << 8);
1232 data->num_pmkid = pos[0] | (pos[1] << 8);
1235 if (left < data->num_pmkid * PMKID_LEN) {
1236 printf("RSN: too short RSN IE for PMKIDs "
1237 "(num=%lu, left=%d)\n",
1238 (unsigned long) data->num_pmkid, left);
1242 pos += data->num_pmkid * PMKID_LEN;
1243 left -= data->num_pmkid * PMKID_LEN;
1254 int wpa_validate_wpa_ie(struct hostapd_data *hapd, struct sta_info *sta,
1255 const u8 *wpa_ie, size_t wpa_ie_len, int version)
1257 struct wpa_ie_data data;
1258 int ciphers, key_mgmt, res, i;
1261 if (version == HOSTAPD_WPA_VERSION_WPA2) {
1262 res = wpa_parse_wpa_ie_rsn(wpa_ie, wpa_ie_len, &data);
1264 selector = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
1265 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1266 selector = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
1267 else if (data.key_mgmt & WPA_KEY_MGMT_PSK)
1268 selector = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
1269 memcpy(hapd->wpa_auth->dot11RSNAAuthenticationSuiteSelected,
1270 selector, RSN_SELECTOR_LEN);
1272 selector = RSN_CIPHER_SUITE_CCMP;
1273 if (data.pairwise_cipher & WPA_CIPHER_CCMP)
1274 selector = RSN_CIPHER_SUITE_CCMP;
1275 else if (data.pairwise_cipher & WPA_CIPHER_TKIP)
1276 selector = RSN_CIPHER_SUITE_TKIP;
1277 else if (data.pairwise_cipher & WPA_CIPHER_WEP104)
1278 selector = RSN_CIPHER_SUITE_WEP104;
1279 else if (data.pairwise_cipher & WPA_CIPHER_WEP40)
1280 selector = RSN_CIPHER_SUITE_WEP40;
1281 else if (data.pairwise_cipher & WPA_CIPHER_NONE)
1282 selector = RSN_CIPHER_SUITE_NONE;
1283 memcpy(hapd->wpa_auth->dot11RSNAPairwiseCipherSelected,
1284 selector, RSN_SELECTOR_LEN);
1286 selector = RSN_CIPHER_SUITE_CCMP;
1287 if (data.group_cipher & WPA_CIPHER_CCMP)
1288 selector = RSN_CIPHER_SUITE_CCMP;
1289 else if (data.group_cipher & WPA_CIPHER_TKIP)
1290 selector = RSN_CIPHER_SUITE_TKIP;
1291 else if (data.group_cipher & WPA_CIPHER_WEP104)
1292 selector = RSN_CIPHER_SUITE_WEP104;
1293 else if (data.group_cipher & WPA_CIPHER_WEP40)
1294 selector = RSN_CIPHER_SUITE_WEP40;
1295 else if (data.group_cipher & WPA_CIPHER_NONE)
1296 selector = RSN_CIPHER_SUITE_NONE;
1297 memcpy(hapd->wpa_auth->dot11RSNAGroupCipherSelected,
1298 selector, RSN_SELECTOR_LEN);
1300 res = wpa_parse_wpa_ie_wpa(wpa_ie, wpa_ie_len, &data);
1302 selector = WPA_AUTH_KEY_MGMT_UNSPEC_802_1X;
1303 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1304 selector = WPA_AUTH_KEY_MGMT_UNSPEC_802_1X;
1305 else if (data.key_mgmt & WPA_KEY_MGMT_PSK)
1306 selector = WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X;
1307 memcpy(hapd->wpa_auth->dot11RSNAAuthenticationSuiteSelected,
1308 selector, WPA_SELECTOR_LEN);
1310 selector = WPA_CIPHER_SUITE_TKIP;
1311 if (data.pairwise_cipher & WPA_CIPHER_CCMP)
1312 selector = WPA_CIPHER_SUITE_CCMP;
1313 else if (data.pairwise_cipher & WPA_CIPHER_TKIP)
1314 selector = WPA_CIPHER_SUITE_TKIP;
1315 else if (data.pairwise_cipher & WPA_CIPHER_WEP104)
1316 selector = WPA_CIPHER_SUITE_WEP104;
1317 else if (data.pairwise_cipher & WPA_CIPHER_WEP40)
1318 selector = WPA_CIPHER_SUITE_WEP40;
1319 else if (data.pairwise_cipher & WPA_CIPHER_NONE)
1320 selector = WPA_CIPHER_SUITE_NONE;
1321 memcpy(hapd->wpa_auth->dot11RSNAPairwiseCipherSelected,
1322 selector, WPA_SELECTOR_LEN);
1324 selector = WPA_CIPHER_SUITE_TKIP;
1325 if (data.group_cipher & WPA_CIPHER_CCMP)
1326 selector = WPA_CIPHER_SUITE_CCMP;
1327 else if (data.group_cipher & WPA_CIPHER_TKIP)
1328 selector = WPA_CIPHER_SUITE_TKIP;
1329 else if (data.group_cipher & WPA_CIPHER_WEP104)
1330 selector = WPA_CIPHER_SUITE_WEP104;
1331 else if (data.group_cipher & WPA_CIPHER_WEP40)
1332 selector = WPA_CIPHER_SUITE_WEP40;
1333 else if (data.group_cipher & WPA_CIPHER_NONE)
1334 selector = WPA_CIPHER_SUITE_NONE;
1335 memcpy(hapd->wpa_auth->dot11RSNAGroupCipherSelected,
1336 selector, WPA_SELECTOR_LEN);
1339 printf("Failed to parse WPA/RSN IE from " MACSTR " (res=%d)\n",
1340 MAC2STR(sta->addr), res);
1341 hostapd_hexdump("WPA/RSN IE", wpa_ie, wpa_ie_len);
1342 return WPA_INVALID_IE;
1345 if (data.group_cipher != hapd->conf->wpa_group) {
1346 printf("Invalid WPA group cipher (0x%x) from " MACSTR "\n",
1347 data.group_cipher, MAC2STR(sta->addr));
1348 return WPA_INVALID_GROUP;
1351 key_mgmt = data.key_mgmt & hapd->conf->wpa_key_mgmt;
1353 printf("Invalid WPA key mgmt (0x%x) from " MACSTR "\n",
1354 data.key_mgmt, MAC2STR(sta->addr));
1355 return WPA_INVALID_AKMP;
1357 if (key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1358 sta->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1360 sta->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
1362 ciphers = data.pairwise_cipher & hapd->conf->wpa_pairwise;
1364 printf("Invalid WPA pairwise cipher (0x%x) from " MACSTR "\n",
1365 data.pairwise_cipher, MAC2STR(sta->addr));
1366 return WPA_INVALID_PAIRWISE;
1369 if (ciphers & WPA_CIPHER_CCMP)
1370 sta->pairwise = WPA_CIPHER_CCMP;
1372 sta->pairwise = WPA_CIPHER_TKIP;
1374 /* TODO: clear WPA/WPA2 state if STA changes from one to another */
1375 if (wpa_ie[0] == WLAN_EID_RSN)
1376 sta->wpa = WPA_VERSION_WPA2;
1378 sta->wpa = WPA_VERSION_WPA;
1380 for (i = 0; i < data.num_pmkid; i++) {
1381 if (HOSTAPD_DEBUG_COND(HOSTAPD_DEBUG_MINIMAL)) {
1382 hostapd_hexdump("RSN IE: STA PMKID",
1383 &data.pmkid[i * PMKID_LEN], PMKID_LEN);
1385 sta->pmksa = pmksa_cache_get(hapd, sta->addr,
1386 &data.pmkid[i * PMKID_LEN]);
1388 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1389 HOSTAPD_LEVEL_DEBUG,
1390 "PMKID found from PMKSA cache");
1391 if (hapd->wpa_auth) {
1392 memcpy(hapd->wpa_auth->dot11RSNAPMKIDUsed,
1393 sta->pmksa->pmkid, PMKID_LEN);
1403 void wpa_new_station(struct hostapd_data *hapd, struct sta_info *sta)
1405 struct wpa_state_machine *sm;
1407 if (!hapd->conf->wpa)
1412 memset(sm->key_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
1413 sm->ReAuthenticationRequest = TRUE;
1418 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1419 HOSTAPD_LEVEL_DEBUG, "start authentication");
1420 sm = malloc(sizeof(struct wpa_state_machine));
1423 memset(sm, 0, sizeof(struct wpa_state_machine));
1432 sm->AuthenticationRequest = TRUE;
1437 void wpa_free_station(struct sta_info *sta)
1439 struct wpa_state_machine *sm = sta->wpa_sm;
1444 if (sm->hapd->conf->wpa_strict_rekey && sm->has_GTK) {
1445 hostapd_logger(sm->hapd, sta->addr, HOSTAPD_MODULE_WPA,
1446 HOSTAPD_LEVEL_DEBUG, "strict rekeying - force "
1447 "GTK rekey since STA is leaving");
1448 eloop_cancel_timeout(wpa_rekey_gtk, sm->hapd, NULL);
1449 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->hapd,
1453 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->hapd, sta);
1454 eloop_cancel_timeout(wpa_sm_call_step, sm->hapd, sta->wpa_sm);
1455 eloop_cancel_timeout(rsn_preauth_finished_cb, sm->hapd, sta);
1456 free(sm->last_rx_eapol_key);
1462 static void wpa_request_new_ptk(struct hostapd_data *hapd,
1463 struct sta_info *sta)
1465 struct wpa_state_machine *sm = sta->wpa_sm;
1470 sm->PTKRequest = TRUE;
1475 void wpa_receive(struct hostapd_data *hapd, struct sta_info *sta,
1476 u8 *data, size_t data_len)
1478 struct wpa_state_machine *sm = sta->wpa_sm;
1479 struct ieee802_1x_hdr *hdr;
1480 struct wpa_eapol_key *key;
1481 u16 key_info, key_data_length;
1482 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST } msg;
1485 if (!hapd->conf->wpa)
1491 if (data_len < sizeof(*hdr) + sizeof(*key))
1494 hdr = (struct ieee802_1x_hdr *) data;
1495 key = (struct wpa_eapol_key *) (hdr + 1);
1496 key_info = ntohs(key->key_info);
1497 key_data_length = ntohs(key->key_data_length);
1498 if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) {
1499 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
1500 "key_data overflow (%d > %lu)",
1502 (unsigned long) (data_len - sizeof(*hdr) -
1507 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
1510 if (key_info & WPA_KEY_INFO_REQUEST) {
1513 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1515 msgtxt = "2/2 Group";
1516 } else if (key_data_length == 0) {
1518 msgtxt = "4/4 Pairwise";
1521 msgtxt = "2/4 Pairwise";
1524 if (key_info & WPA_KEY_INFO_REQUEST) {
1525 if (sta->req_replay_counter_used &&
1526 memcmp(key->replay_counter, sta->req_replay_counter,
1527 WPA_REPLAY_COUNTER_LEN) <= 0) {
1528 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1529 HOSTAPD_LEVEL_WARNING,
1530 "received EAPOL-Key request with "
1531 "replayed counter");
1536 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
1537 (!sm->key_replay_counter_valid ||
1538 memcmp(key->replay_counter, sm->key_replay_counter,
1539 WPA_REPLAY_COUNTER_LEN) != 0)) {
1540 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1542 "received EAPOL-Key %s with unexpected replay "
1544 if (HOSTAPD_DEBUG_COND(HOSTAPD_DEBUG_MINIMAL)) {
1545 hostapd_hexdump("expected replay counter",
1546 sm->key_replay_counter,
1547 WPA_REPLAY_COUNTER_LEN);
1548 hostapd_hexdump("received replay counter",
1549 key->replay_counter,
1550 WPA_REPLAY_COUNTER_LEN);
1557 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
1558 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING) {
1559 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1561 "received EAPOL-Key msg 2/4 in invalid"
1562 " state (%d) - dropped",
1566 if (sta->wpa_ie == NULL ||
1567 sta->wpa_ie_len != key_data_length ||
1568 memcmp(sta->wpa_ie, key + 1, key_data_length) != 0) {
1569 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1571 "WPA IE from (Re)AssocReq did not match"
1573 if (HOSTAPD_DEBUG_COND(HOSTAPD_DEBUG_MINIMAL)) {
1575 hostapd_hexdump("WPA IE in AssocReq",
1579 hostapd_hexdump("WPA IE in msg 2/4",
1583 /* MLME-DEAUTHENTICATE.request */
1584 wpa_sta_disconnect(hapd, sta);
1589 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1591 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1593 "received EAPOL-Key msg 4/4 in invalid"
1594 " state (%d) - dropped",
1600 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1601 || !sm->PTK_valid) {
1602 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1604 "received EAPOL-Key msg 2/2 in invalid"
1605 " state (%d) - dropped",
1606 sm->wpa_ptk_group_state);
1614 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1615 HOSTAPD_LEVEL_DEBUG, "received EAPOL-Key frame (%s)",
1618 if (key_info & WPA_KEY_INFO_ACK) {
1619 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1621 "received invalid EAPOL-Key: Key Ack set");
1625 if (!(key_info & WPA_KEY_INFO_MIC)) {
1626 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1628 "received invalid EAPOL-Key: Key MIC not set");
1632 sm->MICVerified = FALSE;
1633 if (sm->PTK_valid) {
1634 if (wpa_verify_key_mic(&sm->PTK, data, data_len)) {
1635 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1637 "received EAPOL-Key with invalid MIC");
1640 sm->MICVerified = TRUE;
1641 eloop_cancel_timeout(wpa_send_eapol_timeout, sta->wpa_sm->hapd,
1645 if (key_info & WPA_KEY_INFO_REQUEST) {
1646 if (sm->MICVerified) {
1647 sta->req_replay_counter_used = 1;
1648 memcpy(sta->req_replay_counter, key->replay_counter,
1649 WPA_REPLAY_COUNTER_LEN);
1651 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1653 "received EAPOL-Key request with "
1658 if (key_info & WPA_KEY_INFO_ERROR) {
1659 /* Supplicant reported a Michael MIC error */
1660 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1662 "received EAPOL-Key Error Request "
1663 "(STA detected Michael MIC failure)");
1664 ieee80211_michael_mic_failure(hapd, sta->addr, 0);
1665 sta->dot11RSNAStatsTKIPRemoteMICFailures++;
1666 hapd->wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
1667 /* Error report is not a request for a new key
1668 * handshake, but since Authenticator may do it, let's
1669 * change the keys now anyway. */
1670 wpa_request_new_ptk(hapd, sta);
1671 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1672 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1674 "received EAPOL-Key Request for new "
1676 wpa_request_new_ptk(hapd, sta);
1678 /* TODO: this could also be a request for STAKey
1679 * if Key Data fields contains peer MAC address KDE.
1680 * STAKey request should have 0xdd <len> 00-0F-AC:2 in
1681 * the beginning of Key Data */
1682 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1684 "received EAPOL-Key Request for GTK "
1686 wpa_request_new_ptk(hapd, sta);
1687 eloop_cancel_timeout(wpa_rekey_gtk, hapd, NULL);
1688 wpa_rekey_gtk(hapd, NULL);
1691 /* Do not allow the same key replay counter to be reused. */
1692 sm->key_replay_counter_valid = FALSE;
1695 free(sm->last_rx_eapol_key);
1696 sm->last_rx_eapol_key = malloc(data_len);
1697 if (sm->last_rx_eapol_key == NULL)
1699 memcpy(sm->last_rx_eapol_key, data, data_len);
1700 sm->last_rx_eapol_key_len = data_len;
1702 sm->EAPOLKeyReceived = TRUE;
1703 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1704 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1705 memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1710 static void wpa_pmk_to_ptk(struct hostapd_data *hapd, const u8 *pmk,
1711 const u8 *addr1, const u8 *addr2,
1712 const u8 *nonce1, const u8 *nonce2,
1713 u8 *ptk, size_t ptk_len)
1715 u8 data[2 * ETH_ALEN + 2 * WPA_NONCE_LEN];
1717 /* PTK = PRF-X(PMK, "Pairwise key expansion",
1718 * Min(AA, SA) || Max(AA, SA) ||
1719 * Min(ANonce, SNonce) || Max(ANonce, SNonce)) */
1721 if (memcmp(addr1, addr2, ETH_ALEN) < 0) {
1722 memcpy(data, addr1, ETH_ALEN);
1723 memcpy(data + ETH_ALEN, addr2, ETH_ALEN);
1725 memcpy(data, addr2, ETH_ALEN);
1726 memcpy(data + ETH_ALEN, addr1, ETH_ALEN);
1729 if (memcmp(nonce1, nonce2, WPA_NONCE_LEN) < 0) {
1730 memcpy(data + 2 * ETH_ALEN, nonce1, WPA_NONCE_LEN);
1731 memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce2,
1734 memcpy(data + 2 * ETH_ALEN, nonce2, WPA_NONCE_LEN);
1735 memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce1,
1739 sha1_prf(pmk, WPA_PMK_LEN, "Pairwise key expansion",
1740 data, sizeof(data), ptk, ptk_len);
1742 if (HOSTAPD_DEBUG_COND(HOSTAPD_DEBUG_MINIMAL)) {
1743 hostapd_hexdump("PMK", pmk, WPA_PMK_LEN);
1744 hostapd_hexdump("PTK", ptk, ptk_len);
1749 static void wpa_gmk_to_gtk(struct hostapd_data *hapd, u8 *gmk,
1750 u8 *addr, u8 *gnonce, u8 *gtk, size_t gtk_len)
1752 u8 data[ETH_ALEN + WPA_NONCE_LEN];
1754 /* GTK = PRF-X(GMK, "Group key expansion", AA || GNonce) */
1755 memcpy(data, addr, ETH_ALEN);
1756 memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
1758 sha1_prf(gmk, WPA_GMK_LEN, "Group key expansion",
1759 data, sizeof(data), gtk, gtk_len);
1761 if (HOSTAPD_DEBUG_COND(HOSTAPD_DEBUG_MINIMAL)) {
1762 hostapd_hexdump("GMK", gmk, WPA_GMK_LEN);
1763 hostapd_hexdump("GTK", gtk, gtk_len);
1768 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1770 struct hostapd_data *hapd = eloop_ctx;
1771 struct sta_info *sta = timeout_ctx;
1773 if (!sta->wpa_sm || !(sta->flags & WLAN_STA_ASSOC))
1776 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1777 HOSTAPD_LEVEL_DEBUG, "EAPOL-Key timeout");
1778 sta->wpa_sm->TimeoutEvt = TRUE;
1779 wpa_sm_step(sta->wpa_sm);
1783 static int wpa_calc_eapol_key_mic(int ver, u8 *key, u8 *data, size_t len,
1786 u8 hash[SHA1_MAC_LEN];
1789 case WPA_KEY_INFO_TYPE_HMAC_MD5_RC4:
1790 hmac_md5(key, 16, data, len, mic);
1792 case WPA_KEY_INFO_TYPE_HMAC_SHA1_AES:
1793 hmac_sha1(key, 16, data, len, hash);
1794 memcpy(mic, hash, MD5_MAC_LEN);
1803 static void wpa_send_eapol(struct hostapd_data *hapd, struct sta_info *sta,
1804 int secure, int mic, int ack, int install,
1805 int pairwise, u8 *key_rsc, u8 *nonce,
1806 u8 *ie, size_t ie_len, u8 *gtk, size_t gtk_len,
1809 struct wpa_state_machine *sm = sta->wpa_sm;
1810 struct ieee802_1x_hdr *hdr;
1811 struct wpa_eapol_key *key;
1815 int key_data_len, pad_len = 0;
1821 len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
1823 if (sta->wpa == WPA_VERSION_WPA2) {
1824 key_data_len = ie_len + gtk_len;
1826 key_data_len += 2 + RSN_SELECTOR_LEN + 2;
1829 /* WPA does not include GTK in 4-Way Handshake */
1833 /* key_rsc is for group key, so mask it out in case of
1834 * WPA Pairwise key negotiation. */
1837 key_data_len = ie_len + gtk_len;
1840 if (sta->pairwise == WPA_CIPHER_CCMP) {
1841 key_info = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1843 pad_len = key_data_len % 8;
1845 pad_len = 8 - pad_len;
1846 key_data_len += pad_len + 8;
1849 key_info = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1852 len += key_data_len;
1857 memset(hdr, 0, len);
1858 hdr->version = EAPOL_VERSION;
1859 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1860 hdr->length = htons(len - sizeof(*hdr));
1861 key = (struct wpa_eapol_key *) (hdr + 1);
1863 key->type = sta->wpa == WPA_VERSION_WPA2 ?
1864 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1866 key_info |= WPA_KEY_INFO_SECURE;
1868 key_info |= WPA_KEY_INFO_MIC;
1870 key_info |= WPA_KEY_INFO_ACK;
1872 key_info |= WPA_KEY_INFO_INSTALL;
1874 key_info |= WPA_KEY_INFO_KEY_TYPE;
1875 if (gtk && sta->wpa == WPA_VERSION_WPA2)
1876 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1877 if (sta->wpa != WPA_VERSION_WPA2) {
1880 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1882 key->key_info = htons(key_info);
1884 alg = pairwise ? sta->pairwise : hapd->conf->wpa_group;
1886 case WPA_CIPHER_CCMP:
1887 key->key_length = htons(16);
1889 case WPA_CIPHER_TKIP:
1890 key->key_length = htons(32);
1892 case WPA_CIPHER_WEP40:
1893 key->key_length = htons(5);
1895 case WPA_CIPHER_WEP104:
1896 key->key_length = htons(13);
1900 inc_byte_array(sm->key_replay_counter, WPA_REPLAY_COUNTER_LEN);
1901 memcpy(key->replay_counter, sm->key_replay_counter,
1902 WPA_REPLAY_COUNTER_LEN);
1903 sm->key_replay_counter_valid = TRUE;
1906 memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1909 memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1912 memcpy(key + 1, ie, ie_len);
1913 key->key_data_length = htons(ie_len);
1915 buf = malloc(key_data_len);
1920 memset(buf, 0, key_data_len);
1923 memcpy(pos, ie, ie_len);
1926 if (sta->wpa == WPA_VERSION_WPA2) {
1927 *pos++ = WLAN_EID_GENERIC;
1928 *pos++ = RSN_SELECTOR_LEN + 2 + gtk_len;
1929 memcpy(pos, RSN_KEY_DATA_GROUPKEY, RSN_SELECTOR_LEN);
1930 pos += RSN_SELECTOR_LEN;
1931 *pos++ = keyidx & 0x03;
1934 memcpy(pos, gtk, gtk_len);
1939 if (HOSTAPD_DEBUG_COND(HOSTAPD_DEBUG_MINIMAL)) {
1940 hostapd_hexdump("Plaintext EAPOL-Key Key Data",
1943 if (key_info & WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1944 aes_wrap(sm->PTK.encr_key, (key_data_len - 8) / 8, buf,
1946 key->key_data_length = htons(key_data_len);
1950 hapd->wpa_auth->Counter + WPA_NONCE_LEN - 16,
1952 inc_byte_array(hapd->wpa_auth->Counter, WPA_NONCE_LEN);
1953 memcpy(ek, key->key_iv, 16);
1954 memcpy(ek + 16, sm->PTK.encr_key, 16);
1955 memcpy(key + 1, buf, key_data_len);
1956 rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
1957 key->key_data_length = htons(key_data_len);
1963 if (!sm->PTK_valid) {
1964 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1965 HOSTAPD_LEVEL_DEBUG, "PTK not valid "
1966 "when sending EAPOL-Key frame");
1970 wpa_calc_eapol_key_mic(key_info & WPA_KEY_INFO_TYPE_MASK,
1971 sm->PTK.mic_key, (u8 *) hdr, len,
1976 sta->eapol_sm->dot1xAuthEapolFramesTx++;
1977 hostapd_send_eapol(hapd, sta->addr, (u8 *) hdr, len, sm->pairwise_set);
1980 timeout_ms = pairwise ? dot11RSNAConfigPairwiseUpdateTimeOut :
1981 dot11RSNAConfigGroupUpdateTimeOut;
1982 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1983 wpa_send_eapol_timeout, hapd, sta);
1987 static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len)
1989 struct ieee802_1x_hdr *hdr;
1990 struct wpa_eapol_key *key;
1995 if (data_len < sizeof(*hdr) + sizeof(*key))
1998 hdr = (struct ieee802_1x_hdr *) data;
1999 key = (struct wpa_eapol_key *) (hdr + 1);
2000 key_info = ntohs(key->key_info);
2001 type = key_info & WPA_KEY_INFO_TYPE_MASK;
2002 memcpy(mic, key->key_mic, 16);
2003 memset(key->key_mic, 0, 16);
2004 if (wpa_calc_eapol_key_mic(key_info & WPA_KEY_INFO_TYPE_MASK,
2005 PTK->mic_key, data, data_len, key->key_mic)
2006 || memcmp(mic, key->key_mic, 16) != 0)
2008 memcpy(key->key_mic, mic, 16);
2013 void wpa_sm_event(struct hostapd_data *hapd, struct sta_info *sta,
2016 struct wpa_state_machine *sm = sta->wpa_sm;
2017 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
2018 HOSTAPD_LEVEL_DEBUG,
2019 "event %d notification", event);
2029 sm->DeauthenticationRequest = TRUE;
2032 case WPA_REAUTH_EAPOL:
2033 sm->ReAuthenticationRequest = TRUE;
2037 sm->PTK_valid = FALSE;
2038 memset(&sm->PTK, 0, sizeof(sm->PTK));
2040 if (event != WPA_REAUTH_EAPOL) {
2041 sm->pairwise_set = FALSE;
2042 hostapd_set_encryption(sm->hapd, "none", sm->sta->addr, 0,
2050 static const char * wpa_alg_txt(int alg)
2053 case WPA_CIPHER_CCMP:
2055 case WPA_CIPHER_TKIP:
2057 case WPA_CIPHER_WEP104:
2058 case WPA_CIPHER_WEP40:
2066 /* Definitions for clarifying state machine implementation */
2067 #define SM_STATE(machine, state) \
2068 static void sm_ ## machine ## _ ## state ## _Enter(struct wpa_state_machine \
2071 #define SM_ENTRY(machine, _state, _data) \
2072 sm->changed = TRUE; \
2073 sm->_data ## _ ## state = machine ## _ ## _state; \
2074 if (sm->hapd->conf->debug >= HOSTAPD_DEBUG_MINIMAL) \
2075 printf("WPA: " MACSTR " " #machine " entering state " #_state \
2076 "\n", MAC2STR(sm->sta->addr));
2078 #define SM_ENTER(machine, state) sm_ ## machine ## _ ## state ## _Enter(sm)
2080 #define SM_STEP(machine) \
2081 static void sm_ ## machine ## _Step(struct wpa_state_machine *sm)
2083 #define SM_STEP_RUN(machine) sm_ ## machine ## _Step(sm)
2086 SM_STATE(WPA_PTK, INITIALIZE)
2088 struct hostapd_data *hapd = sm->hapd;
2090 SM_ENTRY(WPA_PTK, INITIALIZE, wpa_ptk);
2092 /* Init flag is not cleared here, so avoid busy
2093 * loop by claiming nothing changed. */
2094 sm->changed = FALSE;
2098 if (sm->GUpdateStationKeys)
2099 hapd->wpa_auth->GKeyDoneStations--;
2100 sm->GUpdateStationKeys = FALSE;
2101 if (sm->sta->wpa == WPA_VERSION_WPA)
2102 sm->PInitAKeys = FALSE;
2103 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
2104 * Local AA > Remote AA)) */) {
2107 ieee802_1x_notify_port_enabled(sm->sta->eapol_sm, 0);
2108 hostapd_set_encryption(sm->hapd, "none", sm->sta->addr, 0, (u8 *) "",
2110 sm->pairwise_set = FALSE;
2111 sm->PTK_valid = FALSE;
2112 memset(&sm->PTK, 0, sizeof(sm->PTK));
2113 ieee802_1x_notify_port_valid(sm->sta->eapol_sm, 0);
2115 if (sm->sta->wpa_key_mgmt == WPA_KEY_MGMT_PSK)
2116 ieee802_1x_set_sta_authorized(sm->hapd, sm->sta, 0);
2120 SM_STATE(WPA_PTK, DISCONNECT)
2122 SM_ENTRY(WPA_PTK, DISCONNECT, wpa_ptk);
2123 sm->Disconnect = FALSE;
2124 wpa_sta_disconnect(sm->hapd, sm->sta);
2128 SM_STATE(WPA_PTK, DISCONNECTED)
2130 SM_ENTRY(WPA_PTK, DISCONNECTED, wpa_ptk);
2131 sm->hapd->wpa_auth->GNoStations--;
2132 sm->DeauthenticationRequest = FALSE;
2136 SM_STATE(WPA_PTK, AUTHENTICATION)
2138 SM_ENTRY(WPA_PTK, AUTHENTICATION, wpa_ptk);
2139 sm->hapd->wpa_auth->GNoStations++;
2140 memset(&sm->PTK, 0, sizeof(sm->PTK));
2141 sm->PTK_valid = FALSE;
2142 if (sm->sta->eapol_sm) {
2143 sm->sta->eapol_sm->portControl = Auto;
2144 sm->sta->eapol_sm->portEnabled = TRUE;
2146 sm->AuthenticationRequest = FALSE;
2150 SM_STATE(WPA_PTK, AUTHENTICATION2)
2152 SM_ENTRY(WPA_PTK, AUTHENTICATION2, wpa_ptk);
2153 memcpy(sm->ANonce, sm->hapd->wpa_auth->Counter, WPA_NONCE_LEN);
2154 inc_byte_array(sm->hapd->wpa_auth->Counter, WPA_NONCE_LEN);
2155 sm->ReAuthenticationRequest = FALSE;
2156 /* IEEE 802.11i/D9.0 does not clear TimeoutCtr here, but this is more
2157 * logical place than INITIALIZE since AUTHENTICATION2 can be
2158 * re-entered on ReAuthenticationRequest without going through
2164 SM_STATE(WPA_PTK, INITPMK)
2168 SM_ENTRY(WPA_PTK, INITPMK, wpa_ptk);
2169 if (sm->sta->pmksa) {
2170 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
2171 memcpy(sm->PMK, sm->sta->pmksa->pmk, WPA_PMK_LEN);
2172 pmksa_cache_to_eapol_data(sm->sta->pmksa, sm->sta->eapol_sm);
2173 } else if ((key = ieee802_1x_get_key_crypt(sm->sta->eapol_sm, &len))) {
2174 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
2175 "(len=%lu)", (unsigned long) len);
2176 if (len > WPA_PMK_LEN)
2178 memcpy(sm->PMK, key, len);
2180 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
2182 sm->sta->req_replay_counter_used = 0;
2183 /* IEEE 802.11i/D9.0 does not set keyRun to FALSE, but not doing this
2184 * will break reauthentication since EAPOL state machines may not be
2185 * get into AUTHENTICATING state that clears keyRun before WPA state
2186 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
2187 * state and takes PMK from the previously used AAA Key. This will
2188 * eventually fail in 4-Way Handshake because Supplicant uses PMK
2189 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
2190 * be good workaround for this issue. */
2191 if (sm->sta->eapol_sm)
2192 sm->sta->eapol_sm->keyRun = FALSE;
2196 SM_STATE(WPA_PTK, INITPSK)
2199 SM_ENTRY(WPA_PTK, INITPSK, wpa_ptk);
2200 psk = hostapd_get_psk(sm->hapd->conf, sm->sta->addr, NULL);
2202 memcpy(sm->PMK, psk, WPA_PMK_LEN);
2203 sm->sta->req_replay_counter_used = 0;
2207 SM_STATE(WPA_PTK, PTKSTART)
2210 size_t pmkid_len = 0;
2212 SM_ENTRY(WPA_PTK, PTKSTART, wpa_ptk);
2213 sm->PTKRequest = FALSE;
2214 sm->TimeoutEvt = FALSE;
2215 hostapd_logger(sm->hapd, sm->sta->addr, HOSTAPD_MODULE_WPA,
2216 HOSTAPD_LEVEL_DEBUG,
2217 "sending 1/4 msg of 4-Way Handshake");
2218 if (sm->sta->pmksa &&
2219 (pmkid = malloc(2 + RSN_SELECTOR_LEN + PMKID_LEN))) {
2220 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
2221 pmkid[0] = WLAN_EID_GENERIC;
2222 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
2223 memcpy(&pmkid[2], RSN_KEY_DATA_PMKID, RSN_SELECTOR_LEN);
2224 memcpy(&pmkid[2 + RSN_SELECTOR_LEN], sm->sta->pmksa->pmkid,
2227 wpa_send_eapol(sm->hapd, sm->sta, 0, 0, 1, 0, 1, NULL, sm->ANonce,
2228 pmkid, pmkid_len, NULL, 0, 0);
2234 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
2238 const u8 *pmk = NULL;
2240 SM_ENTRY(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
2241 sm->EAPOLKeyReceived = FALSE;
2243 /* WPA with IEEE 802.1X: use the derived PMK from EAP
2244 * WPA-PSK: iterate through possible PSKs and select the one matching
2247 if (sm->sta->wpa_key_mgmt == WPA_KEY_MGMT_PSK) {
2248 pmk = hostapd_get_psk(sm->hapd->conf, sm->sta->addr,
2255 wpa_pmk_to_ptk(sm->hapd, pmk, sm->hapd->own_addr,
2256 sm->sta->addr, sm->ANonce, sm->SNonce,
2257 (u8 *) &PTK, sizeof(PTK));
2259 if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key,
2260 sm->last_rx_eapol_key_len) == 0) {
2265 if (sm->sta->wpa_key_mgmt != WPA_KEY_MGMT_PSK)
2270 hostapd_logger(sm->hapd, sm->sta->addr, HOSTAPD_MODULE_WPA,
2271 HOSTAPD_LEVEL_DEBUG, "invalid MIC in msg 2/4 "
2272 "of 4-Way Handshake");
2276 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->hapd, sm->sta);
2278 if (sm->sta->wpa_key_mgmt == WPA_KEY_MGMT_PSK) {
2279 /* PSK may have changed from the previous choice, so update
2280 * state machine data based on whatever PSK was selected here.
2282 memcpy(sm->PMK, pmk, WPA_PMK_LEN);
2285 sm->MICVerified = TRUE;
2287 memcpy(&sm->PTK, &PTK, sizeof(PTK));
2288 sm->PTK_valid = TRUE;
2292 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
2294 SM_ENTRY(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
2299 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
2301 u8 rsc[WPA_KEY_RSC_LEN];
2302 struct wpa_authenticator *gsm = sm->hapd->wpa_auth;
2306 SM_ENTRY(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
2307 sm->TimeoutEvt = FALSE;
2308 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, GTK[GN])
2310 memset(rsc, 0, WPA_KEY_RSC_LEN);
2311 hostapd_get_seqnum(sm->hapd, NULL, gsm->GN, rsc);
2312 wpa_ie = sm->hapd->wpa_ie;
2313 wpa_ie_len = sm->hapd->wpa_ie_len;
2314 if (sm->sta->wpa == WPA_VERSION_WPA &&
2315 (sm->hapd->conf->wpa & HOSTAPD_WPA_VERSION_WPA2) &&
2316 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
2317 /* WPA-only STA, remove RSN IE */
2318 wpa_ie = wpa_ie + wpa_ie[1] + 2;
2319 wpa_ie_len = wpa_ie[1] + 2;
2321 hostapd_logger(sm->hapd, sm->sta->addr, HOSTAPD_MODULE_WPA,
2322 HOSTAPD_LEVEL_DEBUG,
2323 "sending 3/4 msg of 4-Way Handshake");
2324 wpa_send_eapol(sm->hapd, sm->sta,
2325 sm->sta->wpa == WPA_VERSION_WPA2 ? 1 : 0,
2326 1, 1, 1, 1, rsc, sm->ANonce,
2328 gsm->GTK[gsm->GN - 1], gsm->GTK_len, gsm->GN);
2333 SM_STATE(WPA_PTK, PTKINITDONE)
2335 SM_ENTRY(WPA_PTK, PTKINITDONE, wpa_ptk);
2336 sm->EAPOLKeyReceived = FALSE;
2340 if (sm->sta->pairwise == WPA_CIPHER_TKIP) {
2347 if (hostapd_set_encryption(sm->hapd, alg, sm->sta->addr, 0,
2348 sm->PTK.tk1, klen)) {
2349 wpa_sta_disconnect(sm->hapd, sm->sta);
2352 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
2353 sm->pairwise_set = TRUE;
2355 if (sm->sta->wpa_key_mgmt == WPA_KEY_MGMT_PSK)
2356 ieee802_1x_set_sta_authorized(sm->hapd, sm->sta, 1);
2359 if (0 /* IBSS == TRUE */) {
2361 if (sm->keycount == 2) {
2362 ieee802_1x_notify_port_valid(sm->sta->eapol_sm, 1);
2365 ieee802_1x_notify_port_valid(sm->sta->eapol_sm, 1);
2367 if (sm->sta->eapol_sm) {
2368 sm->sta->eapol_sm->keyAvailable = FALSE;
2369 sm->sta->eapol_sm->keyDone = TRUE;
2371 if (sm->sta->wpa == WPA_VERSION_WPA)
2372 sm->PInitAKeys = TRUE;
2375 hostapd_logger(sm->hapd, sm->sta->addr, HOSTAPD_MODULE_WPA,
2376 HOSTAPD_LEVEL_INFO, "pairwise key handshake completed "
2378 sm->sta->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2379 if (sm->sta->wpa_key_mgmt == WPA_KEY_MGMT_PSK)
2380 accounting_sta_start(sm->hapd, sm->sta);
2386 struct wpa_authenticator *wpa_auth = sm->hapd->wpa_auth;
2389 SM_ENTER(WPA_PTK, INITIALIZE);
2390 else if (sm->Disconnect
2391 /* || FIX: dot11RSNAConfigSALifetime timeout */)
2392 SM_ENTER(WPA_PTK, DISCONNECT);
2393 else if (sm->DeauthenticationRequest)
2394 SM_ENTER(WPA_PTK, DISCONNECTED);
2395 else if (sm->AuthenticationRequest)
2396 SM_ENTER(WPA_PTK, AUTHENTICATION);
2397 else if (sm->ReAuthenticationRequest)
2398 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2399 else if (sm->PTKRequest)
2400 SM_ENTER(WPA_PTK, PTKSTART);
2401 else switch (sm->wpa_ptk_state) {
2402 case WPA_PTK_INITIALIZE:
2404 case WPA_PTK_DISCONNECT:
2405 SM_ENTER(WPA_PTK, DISCONNECTED);
2407 case WPA_PTK_DISCONNECTED:
2408 SM_ENTER(WPA_PTK, INITIALIZE);
2410 case WPA_PTK_AUTHENTICATION:
2411 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2413 case WPA_PTK_AUTHENTICATION2:
2414 if ((sm->sta->wpa_key_mgmt == WPA_KEY_MGMT_IEEE8021X) &&
2415 sm->sta->eapol_sm && sm->sta->eapol_sm->keyRun)
2416 SM_ENTER(WPA_PTK, INITPMK);
2417 else if ((sm->sta->wpa_key_mgmt == WPA_KEY_MGMT_PSK)
2418 /* FIX: && 802.1X::keyRun */)
2419 SM_ENTER(WPA_PTK, INITPSK);
2421 case WPA_PTK_INITPMK:
2422 if (sm->sta->eapol_sm && sm->sta->eapol_sm->keyAvailable)
2423 SM_ENTER(WPA_PTK, PTKSTART);
2425 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2426 SM_ENTER(WPA_PTK, DISCONNECT);
2429 case WPA_PTK_INITPSK:
2430 if (hostapd_get_psk(sm->hapd->conf, sm->sta->addr, NULL))
2431 SM_ENTER(WPA_PTK, PTKSTART);
2433 hostapd_logger(sm->hapd, sm->sta->addr,
2436 "no PSK configured for the STA");
2437 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2438 SM_ENTER(WPA_PTK, DISCONNECT);
2441 case WPA_PTK_PTKSTART:
2442 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2443 sm->EAPOLKeyPairwise)
2444 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2445 else if (sm->TimeoutCtr > dot11RSNAConfigPairwiseUpdateCount) {
2446 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2447 SM_ENTER(WPA_PTK, DISCONNECT);
2448 } else if (sm->TimeoutEvt)
2449 SM_ENTER(WPA_PTK, PTKSTART);
2451 case WPA_PTK_PTKCALCNEGOTIATING:
2452 if (sm->MICVerified)
2453 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
2454 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2455 sm->EAPOLKeyPairwise)
2456 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2457 else if (sm->TimeoutEvt)
2458 SM_ENTER(WPA_PTK, PTKSTART);
2460 case WPA_PTK_PTKCALCNEGOTIATING2:
2461 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2463 case WPA_PTK_PTKINITNEGOTIATING:
2464 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2465 sm->EAPOLKeyPairwise && sm->MICVerified)
2466 SM_ENTER(WPA_PTK, PTKINITDONE);
2467 else if (sm->TimeoutCtr > dot11RSNAConfigPairwiseUpdateCount) {
2468 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2469 SM_ENTER(WPA_PTK, DISCONNECT);
2470 } else if (sm->TimeoutEvt)
2471 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2473 case WPA_PTK_PTKINITDONE:
2479 SM_STATE(WPA_PTK_GROUP, IDLE)
2481 SM_ENTRY(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
2483 /* Init flag is not cleared here, so avoid busy
2484 * loop by claiming nothing changed. */
2485 sm->changed = FALSE;
2487 sm->GTimeoutCtr = 0;
2491 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
2493 u8 rsc[WPA_KEY_RSC_LEN];
2494 struct wpa_authenticator *gsm = sm->hapd->wpa_auth;
2496 SM_ENTRY(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
2497 if (sm->sta->wpa == WPA_VERSION_WPA)
2498 sm->PInitAKeys = FALSE;
2499 sm->TimeoutEvt = FALSE;
2500 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
2501 memset(rsc, 0, WPA_KEY_RSC_LEN);
2502 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
2503 hostapd_get_seqnum(sm->hapd, NULL, gsm->GN, rsc);
2504 hostapd_logger(sm->hapd, sm->sta->addr, HOSTAPD_MODULE_WPA,
2505 HOSTAPD_LEVEL_DEBUG,
2506 "sending 1/2 msg of Group Key Handshake");
2507 wpa_send_eapol(sm->hapd, sm->sta, 1, 1, 1, !sm->Pair, 0, rsc,
2508 gsm->GNonce, NULL, 0,
2509 gsm->GTK[gsm->GN - 1], gsm->GTK_len, gsm->GN);
2514 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
2516 SM_ENTRY(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
2517 sm->EAPOLKeyReceived = FALSE;
2518 sm->GUpdateStationKeys = FALSE;
2519 sm->hapd->wpa_auth->GKeyDoneStations--;
2520 sm->GTimeoutCtr = 0;
2521 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
2522 hostapd_logger(sm->hapd, sm->sta->addr, HOSTAPD_MODULE_WPA,
2523 HOSTAPD_LEVEL_INFO, "group key handshake completed "
2525 sm->sta->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2530 SM_STATE(WPA_PTK_GROUP, KEYERROR)
2532 SM_ENTRY(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
2533 sm->hapd->wpa_auth->GKeyDoneStations--;
2534 sm->GUpdateStationKeys = FALSE;
2535 sm->Disconnect = TRUE;
2539 SM_STEP(WPA_PTK_GROUP)
2542 SM_ENTER(WPA_PTK_GROUP, IDLE);
2543 else switch (sm->wpa_ptk_group_state) {
2544 case WPA_PTK_GROUP_IDLE:
2545 if (sm->GUpdateStationKeys ||
2546 (sm->sta->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
2547 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2549 case WPA_PTK_GROUP_REKEYNEGOTIATING:
2550 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2551 !sm->EAPOLKeyPairwise && sm->MICVerified)
2552 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
2553 else if (sm->GTimeoutCtr > dot11RSNAConfigGroupUpdateCount)
2554 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
2555 else if (sm->TimeoutEvt)
2556 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2558 case WPA_PTK_GROUP_KEYERROR:
2559 SM_ENTER(WPA_PTK_GROUP, IDLE);
2561 case WPA_PTK_GROUP_REKEYESTABLISHED:
2562 SM_ENTER(WPA_PTK_GROUP, IDLE);
2568 static void wpa_group_gtk_init(struct hostapd_data *hapd)
2570 struct wpa_authenticator *sm = hapd->wpa_auth;
2571 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "WPA: group state machine "
2572 "entering state GTK_INIT\n");
2573 sm->changed = FALSE; /* GInit is not cleared here; avoid loop */
2574 sm->wpa_group_state = WPA_GROUP_GTK_INIT;
2577 memset(sm->GTK, 0, sizeof(sm->GTK));
2580 /* GTK[GN] = CalcGTK() */
2581 /* FIX: is this the correct way of getting GNonce? */
2582 memcpy(sm->GNonce, sm->Counter, WPA_NONCE_LEN);
2583 inc_byte_array(sm->Counter, WPA_NONCE_LEN);
2584 wpa_gmk_to_gtk(hapd, sm->GMK, hapd->own_addr, sm->GNonce,
2585 sm->GTK[sm->GN - 1], sm->GTK_len);
2589 static void wpa_group_setkeys(struct hostapd_data *hapd)
2591 struct wpa_authenticator *sm = hapd->wpa_auth;
2592 struct sta_info *sta;
2595 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "WPA: group state machine "
2596 "entering state SETKEYS\n");
2598 sm->wpa_group_state = WPA_GROUP_SETKEYS;
2599 sm->GTKReKey = FALSE;
2603 sm->GKeyDoneStations = sm->GNoStations;
2604 /* FIX: is this the correct way of getting GNonce? */
2605 memcpy(sm->GNonce, sm->Counter, WPA_NONCE_LEN);
2606 inc_byte_array(sm->Counter, WPA_NONCE_LEN);
2607 wpa_gmk_to_gtk(hapd, sm->GMK, hapd->own_addr, sm->GNonce,
2608 sm->GTK[sm->GN - 1], sm->GTK_len);
2610 sta = hapd->sta_list;
2613 sta->wpa_sm->GUpdateStationKeys = TRUE;
2614 wpa_sm_step(sta->wpa_sm);
2621 static void wpa_group_setkeysdone(struct hostapd_data *hapd)
2623 struct wpa_authenticator *sm = hapd->wpa_auth;
2625 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "WPA: group state machine "
2626 "entering state SETKEYSDONE\n");
2628 sm->wpa_group_state = WPA_GROUP_SETKEYSDONE;
2629 hostapd_set_encryption(hapd, wpa_alg_txt(hapd->conf->wpa_group),
2630 NULL, sm->GN, sm->GTK[sm->GN - 1], sm->GTK_len);
2634 static void wpa_group_sm_step(struct hostapd_data *hapd)
2636 struct wpa_authenticator *sm = hapd->wpa_auth;
2639 wpa_group_gtk_init(hapd);
2640 } else if (sm->wpa_group_state == WPA_GROUP_GTK_INIT &&
2641 sm->GTKAuthenticator) {
2642 wpa_group_setkeysdone(hapd);
2643 } else if (sm->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
2645 wpa_group_setkeys(hapd);
2646 } else if (sm->wpa_group_state == WPA_GROUP_SETKEYS) {
2647 if (sm->GKeyDoneStations == 0)
2648 wpa_group_setkeysdone(hapd);
2649 else if (sm->GTKReKey)
2650 wpa_group_setkeys(hapd);
2655 static int wpa_sm_sta_entry_alive(struct hostapd_data *hapd, u8 *addr)
2657 struct sta_info *sta;
2658 sta = ap_get_sta(hapd, addr);
2659 if (sta == NULL || sta->wpa_sm == NULL)
2665 static void wpa_sm_step(struct wpa_state_machine *sm)
2667 struct hostapd_data *hapd = sm->hapd;
2669 if (sm == NULL || sm->sta == NULL || sm->sta->wpa_sm == NULL)
2672 memcpy(addr, sm->sta->addr, 6);
2674 sm->changed = FALSE;
2675 sm->hapd->wpa_auth->changed = FALSE;
2677 SM_STEP_RUN(WPA_PTK);
2678 if (!wpa_sm_sta_entry_alive(hapd, addr))
2680 SM_STEP_RUN(WPA_PTK_GROUP);
2681 if (!wpa_sm_sta_entry_alive(hapd, addr))
2683 wpa_group_sm_step(sm->hapd);
2684 if (!wpa_sm_sta_entry_alive(hapd, addr))
2686 } while (sm->changed || sm->hapd->wpa_auth->changed);
2690 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
2692 struct wpa_state_machine *sm = timeout_ctx;
2697 void wpa_sm_notify(struct hostapd_data *hapd, struct sta_info *sta)
2699 if (sta->wpa_sm == NULL)
2701 eloop_register_timeout(0, 0, wpa_sm_call_step, hapd, sta->wpa_sm);
2705 void wpa_gtk_rekey(struct hostapd_data *hapd)
2707 struct wpa_authenticator *sm = hapd->wpa_auth;
2713 for (i = 0; i < 2; i++) {
2717 memcpy(sm->GNonce, sm->Counter, WPA_NONCE_LEN);
2718 inc_byte_array(sm->Counter, WPA_NONCE_LEN);
2719 wpa_gmk_to_gtk(hapd, sm->GMK, hapd->own_addr, sm->GNonce,
2720 sm->GTK[sm->GN - 1], sm->GTK_len);
2725 static const char * wpa_bool_txt(int bool)
2727 return bool ? "TRUE" : "FALSE";
2731 static int wpa_cipher_bits(int cipher)
2734 case WPA_CIPHER_CCMP:
2736 case WPA_CIPHER_TKIP:
2738 case WPA_CIPHER_WEP104:
2740 case WPA_CIPHER_WEP40:
2748 #define RSN_SUITE "%02x-%02x-%02x-%d"
2749 #define RSN_SUITE_ARG(s) (s)[0], (s)[1], (s)[2], (s)[3]
2751 int wpa_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2754 char pmkid_txt[PMKID_LEN * 2 + 1], *pos;
2756 len += snprintf(buf + len, buflen - len,
2757 "dot11RSNAOptionImplemented=TRUE\n"
2758 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2759 "dot11RSNAEnabled=%s\n"
2760 "dot11RSNAPreauthenticationEnabled=%s\n",
2761 wpa_bool_txt(hapd->conf->wpa &
2762 HOSTAPD_WPA_VERSION_WPA2),
2763 wpa_bool_txt(hapd->conf->rsn_preauth));
2765 if (hapd->wpa_auth == NULL)
2769 for (i = 0; i < PMKID_LEN; i++) {
2770 pos += sprintf(pos, "%02x",
2771 hapd->wpa_auth->dot11RSNAPMKIDUsed[i]);
2774 len += snprintf(buf + len, buflen - len,
2775 "dot11RSNAConfigVersion=%u\n"
2776 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
2777 /* FIX: dot11RSNAConfigGroupCipher */
2778 /* FIX: dot11RSNAConfigGroupRekeyMethod */
2779 /* FIX: dot11RSNAConfigGroupRekeyTime */
2780 /* FIX: dot11RSNAConfigGroupRekeyPackets */
2781 "dot11RSNAConfigGroupRekeyStrict=%u\n"
2782 "dot11RSNAConfigGroupUpdateCount=%u\n"
2783 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
2784 "dot11RSNAConfigGroupCipherSize=%u\n"
2785 "dot11RSNAConfigPMKLifetime=%u\n"
2786 "dot11RSNAConfigPMKReauthThreshold=%u\n"
2787 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
2788 "dot11RSNAConfigSATimeout=%u\n"
2789 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2790 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2791 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2792 "dot11RSNAPMKIDUsed=%s\n"
2793 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2794 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2795 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2796 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
2797 "dot11RSNA4WayHandshakeFailures=%u\n"
2798 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
2800 !!hapd->conf->wpa_strict_rekey,
2801 dot11RSNAConfigGroupUpdateCount,
2802 dot11RSNAConfigPairwiseUpdateCount,
2803 wpa_cipher_bits(hapd->conf->wpa_group),
2804 dot11RSNAConfigPMKLifetime,
2805 dot11RSNAConfigPMKReauthThreshold,
2806 dot11RSNAConfigSATimeout,
2807 RSN_SUITE_ARG(hapd->wpa_auth->
2808 dot11RSNAAuthenticationSuiteSelected),
2809 RSN_SUITE_ARG(hapd->wpa_auth->
2810 dot11RSNAPairwiseCipherSelected),
2811 RSN_SUITE_ARG(hapd->wpa_auth->
2812 dot11RSNAGroupCipherSelected),
2814 RSN_SUITE_ARG(hapd->wpa_auth->
2815 dot11RSNAAuthenticationSuiteRequested),
2816 RSN_SUITE_ARG(hapd->wpa_auth->
2817 dot11RSNAPairwiseCipherRequested),
2818 RSN_SUITE_ARG(hapd->wpa_auth->
2819 dot11RSNAGroupCipherRequested),
2820 hapd->wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
2821 hapd->wpa_auth->dot11RSNA4WayHandshakeFailures);
2823 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
2824 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
2827 len += snprintf(buf + len, buflen - len,
2828 "hostapdWPAGroupState=%d\n",
2829 hapd->wpa_auth->wpa_group_state);
2835 int wpa_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2836 char *buf, size_t buflen)
2839 u8 not_used[4] = { 0, 0, 0, 0 };
2840 const u8 *pairwise = not_used;
2842 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
2844 /* dot11RSNAStatsEntry */
2846 if (sta->wpa == WPA_VERSION_WPA) {
2847 if (sta->pairwise == WPA_CIPHER_CCMP)
2848 pairwise = WPA_CIPHER_SUITE_CCMP;
2849 else if (sta->pairwise == WPA_CIPHER_TKIP)
2850 pairwise = WPA_CIPHER_SUITE_TKIP;
2851 else if (sta->pairwise == WPA_CIPHER_WEP104)
2852 pairwise = WPA_CIPHER_SUITE_WEP104;
2853 else if (sta->pairwise == WPA_CIPHER_WEP40)
2854 pairwise = WPA_CIPHER_SUITE_WEP40;
2855 else if (sta->pairwise == WPA_CIPHER_NONE)
2856 pairwise = WPA_CIPHER_SUITE_NONE;
2857 } else if (sta->wpa == WPA_VERSION_WPA2) {
2858 if (sta->pairwise == WPA_CIPHER_CCMP)
2859 pairwise = RSN_CIPHER_SUITE_CCMP;
2860 else if (sta->pairwise == WPA_CIPHER_TKIP)
2861 pairwise = RSN_CIPHER_SUITE_TKIP;
2862 else if (sta->pairwise == WPA_CIPHER_WEP104)
2863 pairwise = RSN_CIPHER_SUITE_WEP104;
2864 else if (sta->pairwise == WPA_CIPHER_WEP40)
2865 pairwise = RSN_CIPHER_SUITE_WEP40;
2866 else if (sta->pairwise == WPA_CIPHER_NONE)
2867 pairwise = RSN_CIPHER_SUITE_NONE;
2871 len += snprintf(buf + len, buflen - len,
2872 /* TODO: dot11RSNAStatsIndex */
2873 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
2874 "dot11RSNAStatsVersion=1\n"
2875 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
2876 /* TODO: dot11RSNAStatsTKIPICVErrors */
2877 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
2878 "dot11RSNAStatsTKIPRemoveMICFailures=%u\n"
2879 /* TODO: dot11RSNAStatsCCMPReplays */
2880 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
2881 /* TODO: dot11RSNAStatsTKIPReplays */,
2883 RSN_SUITE_ARG(pairwise),
2884 sta->dot11RSNAStatsTKIPLocalMICFailures,
2885 sta->dot11RSNAStatsTKIPRemoteMICFailures);
2887 if (sta->wpa_sm == NULL)
2891 len += snprintf(buf + len, buflen - len,
2892 "hostapdWPAPTKState=%d\n"
2893 "hostapdWPAPTKGroupState=%d\n",
2894 sta->wpa_sm->wpa_ptk_state,
2895 sta->wpa_sm->wpa_ptk_group_state);