2 * hostapd / IEEE 802.11 Management
3 * Copyright (c) 2002-2014, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #ifndef CONFIG_NATIVE_WINDOWS
13 #include "utils/common.h"
14 #include "utils/eloop.h"
15 #include "crypto/crypto.h"
16 #include "crypto/sha256.h"
17 #include "crypto/random.h"
18 #include "common/ieee802_11_defs.h"
19 #include "common/ieee802_11_common.h"
20 #include "common/wpa_ctrl.h"
21 #include "common/sae.h"
22 #include "radius/radius.h"
23 #include "radius/radius_client.h"
28 #include "ieee802_11_auth.h"
30 #include "ieee802_1x.h"
32 #include "pmksa_cache_auth.h"
35 #include "accounting.h"
36 #include "ap_config.h"
38 #include "p2p_hostapd.h"
39 #include "ap_drv_ops.h"
41 #include "ieee802_11.h"
45 u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
50 if (hapd->iface->current_rates == NULL)
53 *pos++ = WLAN_EID_SUPP_RATES;
54 num = hapd->iface->num_rates;
55 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
57 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
60 /* rest of the rates are encoded in Extended supported
66 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
69 *pos = hapd->iface->current_rates[i].rate / 5;
70 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
75 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht && count < 8) {
77 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
80 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht && count < 8) {
82 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
89 u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
94 if (hapd->iface->current_rates == NULL)
97 num = hapd->iface->num_rates;
98 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
100 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht)
106 *pos++ = WLAN_EID_EXT_SUPP_RATES;
108 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
112 continue; /* already in SuppRates IE */
113 *pos = hapd->iface->current_rates[i].rate / 5;
114 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
119 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht) {
122 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
125 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht) {
128 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_VHT_PHY;
135 u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
138 int capab = WLAN_CAPABILITY_ESS;
142 /* Check if any of configured channels require DFS */
143 dfs = hostapd_is_dfs_required(hapd->iface);
145 wpa_printf(MSG_WARNING, "Failed to check if DFS is required; ret=%d",
150 if (hapd->iface->num_sta_no_short_preamble == 0 &&
151 hapd->iconf->preamble == SHORT_PREAMBLE)
152 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
154 privacy = hapd->conf->ssid.wep.keys_set;
156 if (hapd->conf->ieee802_1x &&
157 (hapd->conf->default_wep_key_len ||
158 hapd->conf->individual_wep_key_len))
165 if (hapd->conf->osen)
167 #endif /* CONFIG_HS20 */
170 int policy, def_klen;
171 if (probe && sta->ssid_probe) {
172 policy = sta->ssid_probe->security_policy;
173 def_klen = sta->ssid_probe->wep.default_len;
175 policy = sta->ssid->security_policy;
176 def_klen = sta->ssid->wep.default_len;
178 privacy = policy != SECURITY_PLAINTEXT;
179 if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
184 capab |= WLAN_CAPABILITY_PRIVACY;
186 if (hapd->iface->current_mode &&
187 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
188 hapd->iface->num_sta_no_short_slot_time == 0)
189 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
192 * Currently, Spectrum Management capability bit is set when directly
193 * requested in configuration by spectrum_mgmt_required or when AP is
194 * running on DFS channel.
195 * TODO: Also consider driver support for TPC to set Spectrum Mgmt bit
197 if (hapd->iface->current_mode &&
198 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211A &&
199 (hapd->iconf->spectrum_mgmt_required || dfs))
200 capab |= WLAN_CAPABILITY_SPECTRUM_MGMT;
202 if (hapd->conf->radio_measurements)
203 capab |= IEEE80211_CAP_RRM;
209 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
210 u16 auth_transaction, const u8 *challenge,
213 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
215 "authentication (shared key, transaction %d)",
218 if (auth_transaction == 1) {
219 if (!sta->challenge) {
220 /* Generate a pseudo-random challenge */
224 sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
225 if (sta->challenge == NULL)
226 return WLAN_STATUS_UNSPECIFIED_FAILURE;
230 os_memcpy(key, &now.sec, 4);
231 os_memcpy(key + 4, &r, 4);
232 rc4_skip(key, sizeof(key), 0,
233 sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
238 if (auth_transaction != 3)
239 return WLAN_STATUS_UNSPECIFIED_FAILURE;
242 if (!iswep || !sta->challenge || !challenge ||
243 os_memcmp_const(sta->challenge, challenge,
244 WLAN_AUTH_CHALLENGE_LEN)) {
245 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
247 "shared key authentication - invalid "
248 "challenge-response");
249 return WLAN_STATUS_CHALLENGE_FAIL;
252 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
254 "authentication OK (shared key)");
255 sta->flags |= WLAN_STA_AUTH;
256 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
257 os_free(sta->challenge);
258 sta->challenge = NULL;
264 static void send_auth_reply(struct hostapd_data *hapd,
265 const u8 *dst, const u8 *bssid,
266 u16 auth_alg, u16 auth_transaction, u16 resp,
267 const u8 *ies, size_t ies_len)
269 struct ieee80211_mgmt *reply;
273 rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
274 buf = os_zalloc(rlen);
278 reply = (struct ieee80211_mgmt *) buf;
279 reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
281 os_memcpy(reply->da, dst, ETH_ALEN);
282 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
283 os_memcpy(reply->bssid, bssid, ETH_ALEN);
285 reply->u.auth.auth_alg = host_to_le16(auth_alg);
286 reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
287 reply->u.auth.status_code = host_to_le16(resp);
290 os_memcpy(reply->u.auth.variable, ies, ies_len);
292 wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
293 " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
294 MAC2STR(dst), auth_alg, auth_transaction,
295 resp, (unsigned long) ies_len);
296 if (hostapd_drv_send_mlme(hapd, reply, rlen, 0) < 0)
297 wpa_printf(MSG_INFO, "send_auth_reply: send");
303 #ifdef CONFIG_IEEE80211R
304 static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
305 u16 auth_transaction, u16 status,
306 const u8 *ies, size_t ies_len)
308 struct hostapd_data *hapd = ctx;
309 struct sta_info *sta;
311 send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
312 status, ies, ies_len);
314 if (status != WLAN_STATUS_SUCCESS)
317 sta = ap_get_sta(hapd, dst);
321 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
322 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
323 sta->flags |= WLAN_STA_AUTH;
324 mlme_authenticate_indication(hapd, sta);
326 #endif /* CONFIG_IEEE80211R */
331 static struct wpabuf * auth_build_sae_commit(struct hostapd_data *hapd,
332 struct sta_info *sta, int update)
336 if (hapd->conf->ssid.wpa_passphrase == NULL) {
337 wpa_printf(MSG_DEBUG, "SAE: No password available");
342 sae_prepare_commit(hapd->own_addr, sta->addr,
343 (u8 *) hapd->conf->ssid.wpa_passphrase,
344 os_strlen(hapd->conf->ssid.wpa_passphrase),
346 wpa_printf(MSG_DEBUG, "SAE: Could not pick PWE");
350 buf = wpabuf_alloc(SAE_COMMIT_MAX_LEN);
353 sae_write_commit(sta->sae, buf, sta->sae->tmp ?
354 sta->sae->tmp->anti_clogging_token : NULL);
360 static struct wpabuf * auth_build_sae_confirm(struct hostapd_data *hapd,
361 struct sta_info *sta)
365 buf = wpabuf_alloc(SAE_CONFIRM_MAX_LEN);
369 sae_write_confirm(sta->sae, buf);
375 static int auth_sae_send_commit(struct hostapd_data *hapd,
376 struct sta_info *sta,
377 const u8 *bssid, int update)
381 data = auth_build_sae_commit(hapd, sta, update);
383 return WLAN_STATUS_UNSPECIFIED_FAILURE;
385 send_auth_reply(hapd, sta->addr, bssid,
386 WLAN_AUTH_SAE, 1, WLAN_STATUS_SUCCESS,
387 wpabuf_head(data), wpabuf_len(data));
391 return WLAN_STATUS_SUCCESS;
395 static int auth_sae_send_confirm(struct hostapd_data *hapd,
396 struct sta_info *sta,
401 data = auth_build_sae_confirm(hapd, sta);
403 return WLAN_STATUS_UNSPECIFIED_FAILURE;
405 send_auth_reply(hapd, sta->addr, bssid,
406 WLAN_AUTH_SAE, 2, WLAN_STATUS_SUCCESS,
407 wpabuf_head(data), wpabuf_len(data));
411 return WLAN_STATUS_SUCCESS;
415 static int use_sae_anti_clogging(struct hostapd_data *hapd)
417 struct sta_info *sta;
418 unsigned int open = 0;
420 if (hapd->conf->sae_anti_clogging_threshold == 0)
423 for (sta = hapd->sta_list; sta; sta = sta->next) {
426 if (sta->sae->state != SAE_COMMITTED &&
427 sta->sae->state != SAE_CONFIRMED)
430 if (open >= hapd->conf->sae_anti_clogging_threshold)
438 static int check_sae_token(struct hostapd_data *hapd, const u8 *addr,
439 const u8 *token, size_t token_len)
441 u8 mac[SHA256_MAC_LEN];
443 if (token_len != SHA256_MAC_LEN)
445 if (hmac_sha256(hapd->sae_token_key, sizeof(hapd->sae_token_key),
446 addr, ETH_ALEN, mac) < 0 ||
447 os_memcmp_const(token, mac, SHA256_MAC_LEN) != 0)
454 static struct wpabuf * auth_build_token_req(struct hostapd_data *hapd,
455 int group, const u8 *addr)
459 struct os_reltime now;
461 os_get_reltime(&now);
462 if (!os_reltime_initialized(&hapd->last_sae_token_key_update) ||
463 os_reltime_expired(&now, &hapd->last_sae_token_key_update, 60)) {
464 if (random_get_bytes(hapd->sae_token_key,
465 sizeof(hapd->sae_token_key)) < 0)
467 wpa_hexdump(MSG_DEBUG, "SAE: Updated token key",
468 hapd->sae_token_key, sizeof(hapd->sae_token_key));
469 hapd->last_sae_token_key_update = now;
472 buf = wpabuf_alloc(sizeof(le16) + SHA256_MAC_LEN);
476 wpabuf_put_le16(buf, group); /* Finite Cyclic Group */
478 token = wpabuf_put(buf, SHA256_MAC_LEN);
479 hmac_sha256(hapd->sae_token_key, sizeof(hapd->sae_token_key),
480 addr, ETH_ALEN, token);
486 static int sae_sm_step(struct hostapd_data *hapd, struct sta_info *sta,
487 const u8 *bssid, u8 auth_transaction)
491 if (auth_transaction != 1 && auth_transaction != 2)
492 return WLAN_STATUS_UNSPECIFIED_FAILURE;
494 switch (sta->sae->state) {
496 if (auth_transaction == 1) {
497 ret = auth_sae_send_commit(hapd, sta, bssid, 1);
500 sta->sae->state = SAE_COMMITTED;
502 if (sae_process_commit(sta->sae) < 0)
503 return WLAN_STATUS_UNSPECIFIED_FAILURE;
506 * In mesh case, both Commit and Confirm can be sent
507 * immediately. In infrastructure BSS, only a single
508 * Authentication frame (Commit) is expected from the AP
509 * here and the second one (Confirm) will be sent once
510 * the STA has sent its second Authentication frame
513 if (hapd->conf->mesh & MESH_ENABLED) {
515 * Send both Commit and Confirm immediately
516 * based on SAE finite state machine
517 * Nothing -> Confirm transition.
519 ret = auth_sae_send_confirm(hapd, sta, bssid);
522 sta->sae->state = SAE_CONFIRMED;
525 * For infrastructure BSS, send only the Commit
526 * message now to get alternating sequence of
527 * Authentication frames between the AP and STA.
528 * Confirm will be sent in
529 * Commited -> Confirmed/Accepted transition
530 * when receiving Confirm from STA.
534 hostapd_logger(hapd, sta->addr,
535 HOSTAPD_MODULE_IEEE80211,
537 "SAE confirm before commit");
541 if (auth_transaction == 1) {
542 if (sae_process_commit(sta->sae) < 0)
543 return WLAN_STATUS_UNSPECIFIED_FAILURE;
545 ret = auth_sae_send_confirm(hapd, sta, bssid);
548 sta->sae->state = SAE_CONFIRMED;
549 } else if (hapd->conf->mesh & MESH_ENABLED) {
551 * In mesh case, follow SAE finite state machine and
554 ret = auth_sae_send_commit(hapd, sta, bssid, 1);
559 * For instructure BSS, send the postponed Confirm from
560 * Nothing -> Confirmed transition that was reduced to
561 * Nothing -> Committed above.
563 ret = auth_sae_send_confirm(hapd, sta, bssid);
567 sta->sae->state = SAE_CONFIRMED;
570 * Since this was triggered on Confirm RX, run another
571 * step to get to Accepted without waiting for
574 return sae_sm_step(hapd, sta, bssid, auth_transaction);
578 if (auth_transaction == 1) {
579 ret = auth_sae_send_commit(hapd, sta, bssid, 1);
583 if (sae_process_commit(sta->sae) < 0)
584 return WLAN_STATUS_UNSPECIFIED_FAILURE;
586 ret = auth_sae_send_confirm(hapd, sta, bssid);
590 sta->flags |= WLAN_STA_AUTH;
591 sta->auth_alg = WLAN_AUTH_SAE;
592 mlme_authenticate_indication(hapd, sta);
593 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
594 sta->sae->state = SAE_ACCEPTED;
595 wpa_auth_pmksa_add_sae(hapd->wpa_auth, sta->addr,
600 if (auth_transaction == 1) {
601 wpa_printf(MSG_DEBUG, "SAE: remove the STA (" MACSTR
602 ") doing reauthentication",
604 ap_free_sta(hapd, sta);
606 ret = auth_sae_send_confirm(hapd, sta, bssid);
607 sae_clear_temp_data(sta->sae);
613 wpa_printf(MSG_ERROR, "SAE: invalid state %d",
615 return WLAN_STATUS_UNSPECIFIED_FAILURE;
617 return WLAN_STATUS_SUCCESS;
621 static void handle_auth_sae(struct hostapd_data *hapd, struct sta_info *sta,
622 const struct ieee80211_mgmt *mgmt, size_t len,
623 u16 auth_transaction, u16 status_code)
625 u16 resp = WLAN_STATUS_SUCCESS;
626 struct wpabuf *data = NULL;
629 if (auth_transaction != 1 || status_code != WLAN_STATUS_SUCCESS)
631 sta->sae = os_zalloc(sizeof(*sta->sae));
632 if (sta->sae == NULL)
634 sta->sae->state = SAE_NOTHING;
637 if (auth_transaction == 1) {
638 const u8 *token = NULL, *pos, *end;
639 size_t token_len = 0;
640 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
642 "start SAE authentication (RX commit, status=%u)",
645 if ((hapd->conf->mesh & MESH_ENABLED) &&
646 status_code == WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ &&
648 pos = mgmt->u.auth.variable;
649 end = ((const u8 *) mgmt) + len;
650 if (pos + sizeof(le16) > end) {
651 wpa_printf(MSG_ERROR,
652 "SAE: Too short anti-clogging token request");
653 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
656 resp = sae_group_allowed(sta->sae,
657 hapd->conf->sae_groups,
659 if (resp != WLAN_STATUS_SUCCESS) {
660 wpa_printf(MSG_ERROR,
661 "SAE: Invalid group in anti-clogging token request");
666 wpabuf_free(sta->sae->tmp->anti_clogging_token);
667 sta->sae->tmp->anti_clogging_token =
668 wpabuf_alloc_copy(pos, end - pos);
669 if (sta->sae->tmp->anti_clogging_token == NULL) {
670 wpa_printf(MSG_ERROR,
671 "SAE: Failed to alloc for anti-clogging token");
676 * IEEE Std 802.11-2012, 11.3.8.6.4: If the Status code
677 * is 76, a new Commit Message shall be constructed
678 * with the Anti-Clogging Token from the received
679 * Authentication frame, and the commit-scalar and
680 * COMMIT-ELEMENT previously sent.
682 if (auth_sae_send_commit(hapd, sta, mgmt->bssid, 0)) {
683 wpa_printf(MSG_ERROR,
684 "SAE: Failed to send commit message");
687 sta->sae->state = SAE_COMMITTED;
691 if (status_code != WLAN_STATUS_SUCCESS)
694 resp = sae_parse_commit(sta->sae, mgmt->u.auth.variable,
695 ((const u8 *) mgmt) + len -
696 mgmt->u.auth.variable, &token,
697 &token_len, hapd->conf->sae_groups);
698 if (token && check_sae_token(hapd, sta->addr, token, token_len)
700 wpa_printf(MSG_DEBUG, "SAE: Drop commit message with "
701 "incorrect token from " MACSTR,
706 if (resp != WLAN_STATUS_SUCCESS)
709 if (!token && use_sae_anti_clogging(hapd)) {
710 wpa_printf(MSG_DEBUG,
711 "SAE: Request anti-clogging token from "
712 MACSTR, MAC2STR(sta->addr));
713 data = auth_build_token_req(hapd, sta->sae->group,
715 resp = WLAN_STATUS_ANTI_CLOGGING_TOKEN_REQ;
716 if (hapd->conf->mesh & MESH_ENABLED)
717 sta->sae->state = SAE_NOTHING;
721 resp = sae_sm_step(hapd, sta, mgmt->bssid, auth_transaction);
722 } else if (auth_transaction == 2) {
723 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
725 "SAE authentication (RX confirm, status=%u)",
727 if (status_code != WLAN_STATUS_SUCCESS)
729 if (sta->sae->state >= SAE_CONFIRMED ||
730 !(hapd->conf->mesh & MESH_ENABLED)) {
731 if (sae_check_confirm(sta->sae, mgmt->u.auth.variable,
732 ((u8 *) mgmt) + len -
733 mgmt->u.auth.variable) < 0) {
734 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
738 resp = sae_sm_step(hapd, sta, mgmt->bssid, auth_transaction);
740 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
742 "unexpected SAE authentication transaction %u (status=%u)",
743 auth_transaction, status_code);
744 if (status_code != WLAN_STATUS_SUCCESS)
746 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
750 if (resp != WLAN_STATUS_SUCCESS) {
751 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, WLAN_AUTH_SAE,
752 auth_transaction, resp,
753 data ? wpabuf_head(data) : (u8 *) "",
754 data ? wpabuf_len(data) : 0);
758 #endif /* CONFIG_SAE */
761 static void handle_auth(struct hostapd_data *hapd,
762 const struct ieee80211_mgmt *mgmt, size_t len)
764 u16 auth_alg, auth_transaction, status_code;
765 u16 resp = WLAN_STATUS_SUCCESS;
766 struct sta_info *sta = NULL;
769 const u8 *challenge = NULL;
770 u32 session_timeout, acct_interim_interval;
772 struct hostapd_sta_wpa_psk_short *psk = NULL;
773 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
774 size_t resp_ies_len = 0;
775 char *identity = NULL;
776 char *radius_cui = NULL;
779 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
780 wpa_printf(MSG_INFO, "handle_auth - too short payload (len=%lu)",
781 (unsigned long) len);
785 #ifdef CONFIG_TESTING_OPTIONS
786 if (hapd->iconf->ignore_auth_probability > 0.0 &&
787 drand48() < hapd->iconf->ignore_auth_probability) {
789 "TESTING: ignoring auth frame from " MACSTR,
793 #endif /* CONFIG_TESTING_OPTIONS */
795 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
796 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
797 status_code = le_to_host16(mgmt->u.auth.status_code);
798 fc = le_to_host16(mgmt->frame_control);
799 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
801 if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
802 2 + WLAN_AUTH_CHALLENGE_LEN &&
803 mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
804 mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
805 challenge = &mgmt->u.auth.variable[2];
807 wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
808 "auth_transaction=%d status_code=%d wep=%d%s "
810 MAC2STR(mgmt->sa), auth_alg, auth_transaction,
811 status_code, !!(fc & WLAN_FC_ISWEP),
812 challenge ? " challenge" : "",
813 seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
815 if (hapd->tkip_countermeasures) {
816 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
820 if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
821 auth_alg == WLAN_AUTH_OPEN) ||
822 #ifdef CONFIG_IEEE80211R
823 (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
824 auth_alg == WLAN_AUTH_FT) ||
825 #endif /* CONFIG_IEEE80211R */
827 (hapd->conf->wpa && wpa_key_mgmt_sae(hapd->conf->wpa_key_mgmt) &&
828 auth_alg == WLAN_AUTH_SAE) ||
829 #endif /* CONFIG_SAE */
830 ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
831 auth_alg == WLAN_AUTH_SHARED_KEY))) {
832 wpa_printf(MSG_INFO, "Unsupported authentication algorithm (%d)",
834 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
838 if (!(auth_transaction == 1 || auth_alg == WLAN_AUTH_SAE ||
839 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
840 wpa_printf(MSG_INFO, "Unknown authentication transaction number (%d)",
842 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
846 if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
847 wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
849 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
853 res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
855 &acct_interim_interval, &vlan_id,
856 &psk, &identity, &radius_cui);
858 if (res == HOSTAPD_ACL_REJECT) {
859 wpa_printf(MSG_INFO, "Station " MACSTR " not allowed to authenticate",
861 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
864 if (res == HOSTAPD_ACL_PENDING) {
865 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
866 " waiting for an external authentication",
868 /* Authentication code will re-send the authentication frame
869 * after it has received (and cached) information from the
870 * external source. */
874 sta = ap_get_sta(hapd, mgmt->sa);
876 if ((fc & WLAN_FC_RETRY) &&
877 sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
878 sta->last_seq_ctrl == seq_ctrl &&
879 sta->last_subtype == WLAN_FC_STYPE_AUTH) {
880 hostapd_logger(hapd, sta->addr,
881 HOSTAPD_MODULE_IEEE80211,
883 "Drop repeated authentication frame seq_ctrl=0x%x",
889 if (hapd->conf->mesh & MESH_ENABLED) {
890 /* if the mesh peer is not available, we don't do auth.
894 #endif /* CONFIG_MESH */
896 sta = ap_sta_add(hapd, mgmt->sa);
898 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
902 sta->last_seq_ctrl = seq_ctrl;
903 sta->last_subtype = WLAN_FC_STYPE_AUTH;
906 if (!hostapd_vlan_id_valid(hapd->conf->vlan, vlan_id)) {
907 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
908 HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
909 "%d received from RADIUS server",
911 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
914 sta->vlan_id = vlan_id;
915 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
916 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
919 hostapd_free_psk_list(sta->psk);
920 if (hapd->conf->wpa_psk_radius != PSK_RADIUS_IGNORED) {
927 sta->identity = identity;
929 sta->radius_cui = radius_cui;
932 sta->flags &= ~WLAN_STA_PREAUTH;
933 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
935 if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
936 sta->acct_interim_interval = acct_interim_interval;
937 if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
938 ap_sta_session_timeout(hapd, sta, session_timeout);
940 ap_sta_no_session_timeout(hapd, sta);
944 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
946 "authentication OK (open system)");
947 sta->flags |= WLAN_STA_AUTH;
948 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
949 sta->auth_alg = WLAN_AUTH_OPEN;
950 mlme_authenticate_indication(hapd, sta);
952 case WLAN_AUTH_SHARED_KEY:
953 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
955 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
956 mlme_authenticate_indication(hapd, sta);
957 if (sta->challenge && auth_transaction == 1) {
958 resp_ies[0] = WLAN_EID_CHALLENGE;
959 resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
960 os_memcpy(resp_ies + 2, sta->challenge,
961 WLAN_AUTH_CHALLENGE_LEN);
962 resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
965 #ifdef CONFIG_IEEE80211R
967 sta->auth_alg = WLAN_AUTH_FT;
968 if (sta->wpa_sm == NULL)
969 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
971 if (sta->wpa_sm == NULL) {
972 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
974 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
977 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
978 auth_transaction, mgmt->u.auth.variable,
979 len - IEEE80211_HDRLEN -
980 sizeof(mgmt->u.auth),
981 handle_auth_ft_finish, hapd);
982 /* handle_auth_ft_finish() callback will complete auth. */
984 #endif /* CONFIG_IEEE80211R */
988 if (status_code == WLAN_STATUS_SUCCESS &&
989 hapd->conf->mesh & MESH_ENABLED) {
990 if (sta->wpa_sm == NULL)
992 wpa_auth_sta_init(hapd->wpa_auth,
994 if (sta->wpa_sm == NULL) {
995 wpa_printf(MSG_DEBUG,
996 "SAE: Failed to initialize WPA state machine");
997 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1001 #endif /* CONFIG_MESH */
1002 handle_auth_sae(hapd, sta, mgmt, len, auth_transaction,
1005 #endif /* CONFIG_SAE */
1010 os_free(radius_cui);
1011 hostapd_free_psk_list(psk);
1013 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
1014 auth_transaction + 1, resp, resp_ies, resp_ies_len);
1018 static int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
1022 /* get a unique AID */
1024 wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
1028 for (i = 0; i < AID_WORDS; i++) {
1029 if (hapd->sta_aid[i] == (u32) -1)
1031 for (j = 0; j < 32; j++) {
1032 if (!(hapd->sta_aid[i] & BIT(j)))
1040 aid = i * 32 + j + 1;
1045 hapd->sta_aid[i] |= BIT(j);
1046 wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
1051 static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
1052 const u8 *ssid_ie, size_t ssid_ie_len)
1054 if (ssid_ie == NULL)
1055 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1057 if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
1058 os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
1059 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1061 "Station tried to associate with unknown SSID "
1062 "'%s'", wpa_ssid_txt(ssid_ie, ssid_ie_len));
1063 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1066 return WLAN_STATUS_SUCCESS;
1070 static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
1071 const u8 *wmm_ie, size_t wmm_ie_len)
1073 sta->flags &= ~WLAN_STA_WMM;
1075 if (wmm_ie && hapd->conf->wmm_enabled) {
1076 struct wmm_information_element *wmm;
1078 if (!hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len)) {
1079 hostapd_logger(hapd, sta->addr,
1081 HOSTAPD_LEVEL_DEBUG,
1082 "invalid WMM element in association "
1084 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1087 sta->flags |= WLAN_STA_WMM;
1088 wmm = (struct wmm_information_element *) wmm_ie;
1089 sta->qosinfo = wmm->qos_info;
1091 return WLAN_STATUS_SUCCESS;
1095 static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
1096 struct ieee802_11_elems *elems)
1098 if (!elems->supp_rates) {
1099 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1100 HOSTAPD_LEVEL_DEBUG,
1101 "No supported rates element in AssocReq");
1102 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1105 if (elems->supp_rates_len + elems->ext_supp_rates_len >
1106 sizeof(sta->supported_rates)) {
1107 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1108 HOSTAPD_LEVEL_DEBUG,
1109 "Invalid supported rates element length %d+%d",
1110 elems->supp_rates_len,
1111 elems->ext_supp_rates_len);
1112 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1115 sta->supported_rates_len = merge_byte_arrays(
1116 sta->supported_rates, sizeof(sta->supported_rates),
1117 elems->supp_rates, elems->supp_rates_len,
1118 elems->ext_supp_rates, elems->ext_supp_rates_len);
1120 return WLAN_STATUS_SUCCESS;
1124 static u16 check_ext_capab(struct hostapd_data *hapd, struct sta_info *sta,
1125 const u8 *ext_capab_ie, size_t ext_capab_ie_len)
1127 #ifdef CONFIG_INTERWORKING
1128 /* check for QoS Map support */
1129 if (ext_capab_ie_len >= 5) {
1130 if (ext_capab_ie[4] & 0x01)
1131 sta->qos_map_enabled = 1;
1133 #endif /* CONFIG_INTERWORKING */
1135 return WLAN_STATUS_SUCCESS;
1139 static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
1140 const u8 *ies, size_t ies_len, int reassoc)
1142 struct ieee802_11_elems elems;
1146 const u8 *p2p_dev_addr = NULL;
1148 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
1149 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1150 HOSTAPD_LEVEL_INFO, "Station sent an invalid "
1151 "association request");
1152 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1155 resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
1156 if (resp != WLAN_STATUS_SUCCESS)
1158 resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
1159 if (resp != WLAN_STATUS_SUCCESS)
1161 resp = check_ext_capab(hapd, sta, elems.ext_capab, elems.ext_capab_len);
1162 if (resp != WLAN_STATUS_SUCCESS)
1164 resp = copy_supp_rates(hapd, sta, &elems);
1165 if (resp != WLAN_STATUS_SUCCESS)
1167 #ifdef CONFIG_IEEE80211N
1168 resp = copy_sta_ht_capab(hapd, sta, elems.ht_capabilities,
1169 elems.ht_capabilities_len);
1170 if (resp != WLAN_STATUS_SUCCESS)
1172 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
1173 !(sta->flags & WLAN_STA_HT)) {
1174 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1175 HOSTAPD_LEVEL_INFO, "Station does not support "
1176 "mandatory HT PHY - reject association");
1177 return WLAN_STATUS_ASSOC_DENIED_NO_HT;
1179 #endif /* CONFIG_IEEE80211N */
1181 #ifdef CONFIG_IEEE80211AC
1182 resp = copy_sta_vht_capab(hapd, sta, elems.vht_capabilities,
1183 elems.vht_capabilities_len);
1184 if (resp != WLAN_STATUS_SUCCESS)
1187 resp = set_sta_vht_opmode(hapd, sta, elems.vht_opmode_notif);
1188 if (resp != WLAN_STATUS_SUCCESS)
1191 if (hapd->iconf->ieee80211ac && hapd->iconf->require_vht &&
1192 !(sta->flags & WLAN_STA_VHT)) {
1193 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1194 HOSTAPD_LEVEL_INFO, "Station does not support "
1195 "mandatory VHT PHY - reject association");
1196 return WLAN_STATUS_ASSOC_DENIED_NO_VHT;
1198 #endif /* CONFIG_IEEE80211AC */
1202 wpabuf_free(sta->p2p_ie);
1203 sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
1204 P2P_IE_VENDOR_TYPE);
1206 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
1208 wpabuf_free(sta->p2p_ie);
1211 #endif /* CONFIG_P2P */
1213 if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
1214 wpa_ie = elems.rsn_ie;
1215 wpa_ie_len = elems.rsn_ie_len;
1216 } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
1218 wpa_ie = elems.wpa_ie;
1219 wpa_ie_len = elems.wpa_ie_len;
1226 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
1227 if (hapd->conf->wps_state && elems.wps_ie) {
1228 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
1229 "Request - assume WPS is used");
1230 sta->flags |= WLAN_STA_WPS;
1231 wpabuf_free(sta->wps_ie);
1232 sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
1233 WPS_IE_VENDOR_TYPE);
1234 if (sta->wps_ie && wps_is_20(sta->wps_ie)) {
1235 wpa_printf(MSG_DEBUG, "WPS: STA supports WPS 2.0");
1236 sta->flags |= WLAN_STA_WPS2;
1240 if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
1241 wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
1242 "(Re)Association Request - reject");
1243 return WLAN_STATUS_INVALID_IE;
1245 } else if (hapd->conf->wps_state && wpa_ie == NULL) {
1246 wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
1247 "(Re)Association Request - possible WPS use");
1248 sta->flags |= WLAN_STA_MAYBE_WPS;
1250 #endif /* CONFIG_WPS */
1251 if (hapd->conf->wpa && wpa_ie == NULL) {
1252 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1254 "No WPA/RSN IE in association request");
1255 return WLAN_STATUS_INVALID_IE;
1258 if (hapd->conf->wpa && wpa_ie) {
1262 if (sta->wpa_sm == NULL)
1263 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
1266 if (sta->wpa_sm == NULL) {
1267 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
1269 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1271 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
1273 elems.mdie, elems.mdie_len);
1274 if (res == WPA_INVALID_GROUP)
1275 resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
1276 else if (res == WPA_INVALID_PAIRWISE)
1277 resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
1278 else if (res == WPA_INVALID_AKMP)
1279 resp = WLAN_STATUS_AKMP_NOT_VALID;
1280 else if (res == WPA_ALLOC_FAIL)
1281 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
1282 #ifdef CONFIG_IEEE80211W
1283 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
1284 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
1285 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
1286 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
1287 #endif /* CONFIG_IEEE80211W */
1288 else if (res == WPA_INVALID_MDIE)
1289 resp = WLAN_STATUS_INVALID_MDIE;
1290 else if (res != WPA_IE_OK)
1291 resp = WLAN_STATUS_INVALID_IE;
1292 if (resp != WLAN_STATUS_SUCCESS)
1294 #ifdef CONFIG_IEEE80211W
1295 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
1296 sta->sa_query_count > 0)
1297 ap_check_sa_query_timeout(hapd, sta);
1298 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
1299 (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
1301 * STA has already been associated with MFP and SA
1302 * Query timeout has not been reached. Reject the
1303 * association attempt temporarily and start SA Query,
1304 * if one is not pending.
1307 if (sta->sa_query_count == 0)
1308 ap_sta_start_sa_query(hapd, sta);
1310 return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
1313 if (wpa_auth_uses_mfp(sta->wpa_sm))
1314 sta->flags |= WLAN_STA_MFP;
1316 sta->flags &= ~WLAN_STA_MFP;
1317 #endif /* CONFIG_IEEE80211W */
1319 #ifdef CONFIG_IEEE80211R
1320 if (sta->auth_alg == WLAN_AUTH_FT) {
1322 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
1323 "to use association (not "
1324 "re-association) with FT auth_alg",
1325 MAC2STR(sta->addr));
1326 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1329 resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
1331 if (resp != WLAN_STATUS_SUCCESS)
1334 #endif /* CONFIG_IEEE80211R */
1337 if (wpa_auth_uses_sae(sta->wpa_sm) &&
1338 sta->auth_alg == WLAN_AUTH_OPEN) {
1339 struct rsn_pmksa_cache_entry *sa;
1340 sa = wpa_auth_sta_get_pmksa(sta->wpa_sm);
1341 if (!sa || sa->akmp != WPA_KEY_MGMT_SAE) {
1342 wpa_printf(MSG_DEBUG,
1343 "SAE: No PMKSA cache entry found for "
1344 MACSTR, MAC2STR(sta->addr));
1345 return WLAN_STATUS_INVALID_PMKID;
1347 wpa_printf(MSG_DEBUG, "SAE: " MACSTR
1348 " using PMKSA caching", MAC2STR(sta->addr));
1349 } else if (wpa_auth_uses_sae(sta->wpa_sm) &&
1350 sta->auth_alg != WLAN_AUTH_SAE &&
1351 !(sta->auth_alg == WLAN_AUTH_FT &&
1352 wpa_auth_uses_ft_sae(sta->wpa_sm))) {
1353 wpa_printf(MSG_DEBUG, "SAE: " MACSTR " tried to use "
1354 "SAE AKM after non-SAE auth_alg %u",
1355 MAC2STR(sta->addr), sta->auth_alg);
1356 return WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
1358 #endif /* CONFIG_SAE */
1360 #ifdef CONFIG_IEEE80211N
1361 if ((sta->flags & (WLAN_STA_HT | WLAN_STA_VHT)) &&
1362 wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
1363 hostapd_logger(hapd, sta->addr,
1364 HOSTAPD_MODULE_IEEE80211,
1366 "Station tried to use TKIP with HT "
1368 return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
1370 #endif /* CONFIG_IEEE80211N */
1372 } else if (hapd->conf->osen) {
1373 if (elems.osen == NULL) {
1375 hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1377 "No HS 2.0 OSEN element in association request");
1378 return WLAN_STATUS_INVALID_IE;
1381 wpa_printf(MSG_DEBUG, "HS 2.0: OSEN association");
1382 if (sta->wpa_sm == NULL)
1383 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
1385 if (sta->wpa_sm == NULL) {
1386 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
1388 return WLAN_STATUS_UNSPECIFIED_FAILURE;
1390 if (wpa_validate_osen(hapd->wpa_auth, sta->wpa_sm,
1391 elems.osen - 2, elems.osen_len + 2) < 0)
1392 return WLAN_STATUS_INVALID_IE;
1393 #endif /* CONFIG_HS20 */
1395 wpa_auth_sta_no_wpa(sta->wpa_sm);
1398 p2p_group_notif_assoc(hapd->p2p_group, sta->addr, ies, ies_len);
1399 #endif /* CONFIG_P2P */
1402 wpabuf_free(sta->hs20_ie);
1403 if (elems.hs20 && elems.hs20_len > 4) {
1404 sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
1405 elems.hs20_len - 4);
1407 sta->hs20_ie = NULL;
1408 #endif /* CONFIG_HS20 */
1410 return WLAN_STATUS_SUCCESS;
1414 static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
1418 struct ieee80211_mgmt reply;
1420 os_memset(&reply, 0, sizeof(reply));
1421 reply.frame_control =
1422 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
1423 os_memcpy(reply.da, addr, ETH_ALEN);
1424 os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
1425 os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
1427 send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
1428 reply.u.deauth.reason_code = host_to_le16(reason_code);
1430 if (hostapd_drv_send_mlme(hapd, &reply, send_len, 0) < 0)
1431 wpa_printf(MSG_INFO, "Failed to send deauth: %s",
1436 static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
1437 u16 status_code, int reassoc, const u8 *ies,
1441 u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
1442 struct ieee80211_mgmt *reply;
1445 os_memset(buf, 0, sizeof(buf));
1446 reply = (struct ieee80211_mgmt *) buf;
1447 reply->frame_control =
1448 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1449 (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
1450 WLAN_FC_STYPE_ASSOC_RESP));
1451 os_memcpy(reply->da, sta->addr, ETH_ALEN);
1452 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
1453 os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
1455 send_len = IEEE80211_HDRLEN;
1456 send_len += sizeof(reply->u.assoc_resp);
1457 reply->u.assoc_resp.capab_info =
1458 host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
1459 reply->u.assoc_resp.status_code = host_to_le16(status_code);
1460 reply->u.assoc_resp.aid = host_to_le16(sta->aid | BIT(14) | BIT(15));
1461 /* Supported rates */
1462 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
1463 /* Extended supported rates */
1464 p = hostapd_eid_ext_supp_rates(hapd, p);
1466 #ifdef CONFIG_IEEE80211R
1467 if (status_code == WLAN_STATUS_SUCCESS) {
1468 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
1469 * Transition Information, RSN, [RIC Response] */
1470 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
1471 buf + sizeof(buf) - p,
1472 sta->auth_alg, ies, ies_len);
1474 #endif /* CONFIG_IEEE80211R */
1476 #ifdef CONFIG_IEEE80211W
1477 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
1478 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
1479 #endif /* CONFIG_IEEE80211W */
1481 #ifdef CONFIG_IEEE80211N
1482 p = hostapd_eid_ht_capabilities(hapd, p);
1483 p = hostapd_eid_ht_operation(hapd, p);
1484 #endif /* CONFIG_IEEE80211N */
1486 #ifdef CONFIG_IEEE80211AC
1487 p = hostapd_eid_vht_capabilities(hapd, p);
1488 p = hostapd_eid_vht_operation(hapd, p);
1489 #endif /* CONFIG_IEEE80211AC */
1491 p = hostapd_eid_ext_capab(hapd, p);
1492 p = hostapd_eid_bss_max_idle_period(hapd, p);
1493 if (sta->qos_map_enabled)
1494 p = hostapd_eid_qos_map_set(hapd, p);
1496 if (sta->flags & WLAN_STA_WMM)
1497 p = hostapd_eid_wmm(hapd, p);
1500 if ((sta->flags & WLAN_STA_WPS) ||
1501 ((sta->flags & WLAN_STA_MAYBE_WPS) && hapd->conf->wpa)) {
1502 struct wpabuf *wps = wps_build_assoc_resp_ie();
1504 os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
1505 p += wpabuf_len(wps);
1509 #endif /* CONFIG_WPS */
1513 struct wpabuf *p2p_resp_ie;
1514 enum p2p_status_code status;
1515 switch (status_code) {
1516 case WLAN_STATUS_SUCCESS:
1517 status = P2P_SC_SUCCESS;
1519 case WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
1520 status = P2P_SC_FAIL_LIMIT_REACHED;
1523 status = P2P_SC_FAIL_INVALID_PARAMS;
1526 p2p_resp_ie = p2p_group_assoc_resp_ie(hapd->p2p_group, status);
1528 os_memcpy(p, wpabuf_head(p2p_resp_ie),
1529 wpabuf_len(p2p_resp_ie));
1530 p += wpabuf_len(p2p_resp_ie);
1531 wpabuf_free(p2p_resp_ie);
1534 #endif /* CONFIG_P2P */
1536 #ifdef CONFIG_P2P_MANAGER
1537 if (hapd->conf->p2p & P2P_MANAGE)
1538 p = hostapd_eid_p2p_manage(hapd, p);
1539 #endif /* CONFIG_P2P_MANAGER */
1541 send_len += p - reply->u.assoc_resp.variable;
1543 if (hostapd_drv_send_mlme(hapd, reply, send_len, 0) < 0)
1544 wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
1549 static void handle_assoc(struct hostapd_data *hapd,
1550 const struct ieee80211_mgmt *mgmt, size_t len,
1553 u16 capab_info, listen_interval, seq_ctrl, fc;
1554 u16 resp = WLAN_STATUS_SUCCESS;
1557 struct sta_info *sta;
1559 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
1560 sizeof(mgmt->u.assoc_req))) {
1561 wpa_printf(MSG_INFO, "handle_assoc(reassoc=%d) - too short payload (len=%lu)",
1562 reassoc, (unsigned long) len);
1566 #ifdef CONFIG_TESTING_OPTIONS
1568 if (hapd->iconf->ignore_reassoc_probability > 0.0 &&
1569 drand48() < hapd->iconf->ignore_reassoc_probability) {
1570 wpa_printf(MSG_INFO,
1571 "TESTING: ignoring reassoc request from "
1572 MACSTR, MAC2STR(mgmt->sa));
1576 if (hapd->iconf->ignore_assoc_probability > 0.0 &&
1577 drand48() < hapd->iconf->ignore_assoc_probability) {
1578 wpa_printf(MSG_INFO,
1579 "TESTING: ignoring assoc request from "
1580 MACSTR, MAC2STR(mgmt->sa));
1584 #endif /* CONFIG_TESTING_OPTIONS */
1586 fc = le_to_host16(mgmt->frame_control);
1587 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
1590 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
1591 listen_interval = le_to_host16(
1592 mgmt->u.reassoc_req.listen_interval);
1593 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
1594 " capab_info=0x%02x listen_interval=%d current_ap="
1595 MACSTR " seq_ctrl=0x%x%s",
1596 MAC2STR(mgmt->sa), capab_info, listen_interval,
1597 MAC2STR(mgmt->u.reassoc_req.current_ap),
1598 seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
1599 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
1600 pos = mgmt->u.reassoc_req.variable;
1602 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
1603 listen_interval = le_to_host16(
1604 mgmt->u.assoc_req.listen_interval);
1605 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
1606 " capab_info=0x%02x listen_interval=%d "
1608 MAC2STR(mgmt->sa), capab_info, listen_interval,
1609 seq_ctrl, (fc & WLAN_FC_RETRY) ? " retry" : "");
1610 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
1611 pos = mgmt->u.assoc_req.variable;
1614 sta = ap_get_sta(hapd, mgmt->sa);
1615 #ifdef CONFIG_IEEE80211R
1616 if (sta && sta->auth_alg == WLAN_AUTH_FT &&
1617 (sta->flags & WLAN_STA_AUTH) == 0) {
1618 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
1619 "prior to authentication since it is using "
1620 "over-the-DS FT", MAC2STR(mgmt->sa));
1622 #endif /* CONFIG_IEEE80211R */
1623 if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
1624 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1625 HOSTAPD_LEVEL_INFO, "Station tried to "
1626 "associate before authentication "
1627 "(aid=%d flags=0x%x)",
1628 sta ? sta->aid : -1,
1629 sta ? sta->flags : 0);
1630 send_deauth(hapd, mgmt->sa,
1631 WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
1635 if ((fc & WLAN_FC_RETRY) &&
1636 sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
1637 sta->last_seq_ctrl == seq_ctrl &&
1638 sta->last_subtype == reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
1639 WLAN_FC_STYPE_ASSOC_REQ) {
1640 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1641 HOSTAPD_LEVEL_DEBUG,
1642 "Drop repeated association frame seq_ctrl=0x%x",
1646 sta->last_seq_ctrl = seq_ctrl;
1647 sta->last_subtype = reassoc ? WLAN_FC_STYPE_REASSOC_REQ :
1648 WLAN_FC_STYPE_ASSOC_REQ;
1650 if (hapd->tkip_countermeasures) {
1651 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
1655 if (listen_interval > hapd->conf->max_listen_interval) {
1656 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1657 HOSTAPD_LEVEL_DEBUG,
1658 "Too large Listen Interval (%d)",
1660 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
1664 /* followed by SSID and Supported rates; and HT capabilities if 802.11n
1666 resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
1667 if (resp != WLAN_STATUS_SUCCESS)
1670 if (hostapd_get_aid(hapd, sta) < 0) {
1671 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1672 HOSTAPD_LEVEL_INFO, "No room for more AIDs");
1673 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1677 sta->capability = capab_info;
1678 sta->listen_interval = listen_interval;
1680 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
1681 sta->flags |= WLAN_STA_NONERP;
1682 for (i = 0; i < sta->supported_rates_len; i++) {
1683 if ((sta->supported_rates[i] & 0x7f) > 22) {
1684 sta->flags &= ~WLAN_STA_NONERP;
1688 if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
1689 sta->nonerp_set = 1;
1690 hapd->iface->num_sta_non_erp++;
1691 if (hapd->iface->num_sta_non_erp == 1)
1692 ieee802_11_set_beacons(hapd->iface);
1695 if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
1696 !sta->no_short_slot_time_set) {
1697 sta->no_short_slot_time_set = 1;
1698 hapd->iface->num_sta_no_short_slot_time++;
1699 if (hapd->iface->current_mode->mode ==
1700 HOSTAPD_MODE_IEEE80211G &&
1701 hapd->iface->num_sta_no_short_slot_time == 1)
1702 ieee802_11_set_beacons(hapd->iface);
1705 if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1706 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
1708 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
1710 if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
1711 !sta->no_short_preamble_set) {
1712 sta->no_short_preamble_set = 1;
1713 hapd->iface->num_sta_no_short_preamble++;
1714 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
1715 && hapd->iface->num_sta_no_short_preamble == 1)
1716 ieee802_11_set_beacons(hapd->iface);
1719 #ifdef CONFIG_IEEE80211N
1720 update_ht_state(hapd, sta);
1721 #endif /* CONFIG_IEEE80211N */
1723 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1724 HOSTAPD_LEVEL_DEBUG,
1725 "association OK (aid %d)", sta->aid);
1726 /* Station will be marked associated, after it acknowledges AssocResp
1728 sta->flags |= WLAN_STA_ASSOC_REQ_OK;
1730 #ifdef CONFIG_IEEE80211W
1731 if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
1732 wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
1733 "SA Query procedure", reassoc ? "re" : "");
1734 /* TODO: Send a protected Disassociate frame to the STA using
1735 * the old key and Reason Code "Previous Authentication no
1736 * longer valid". Make sure this is only sent protected since
1737 * unprotected frame would be received by the STA that is now
1738 * trying to associate.
1741 #endif /* CONFIG_IEEE80211W */
1743 /* Make sure that the previously registered inactivity timer will not
1744 * remove the STA immediately. */
1745 sta->timeout_next = STA_NULLFUNC;
1748 send_assoc_resp(hapd, sta, resp, reassoc, pos, left);
1752 static void handle_disassoc(struct hostapd_data *hapd,
1753 const struct ieee80211_mgmt *mgmt, size_t len)
1755 struct sta_info *sta;
1757 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1758 wpa_printf(MSG_INFO, "handle_disassoc - too short payload (len=%lu)",
1759 (unsigned long) len);
1763 wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1765 le_to_host16(mgmt->u.disassoc.reason_code));
1767 sta = ap_get_sta(hapd, mgmt->sa);
1769 wpa_printf(MSG_INFO, "Station " MACSTR " trying to disassociate, but it is not associated",
1774 ap_sta_set_authorized(hapd, sta, 0);
1775 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
1776 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
1777 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1778 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1779 HOSTAPD_LEVEL_INFO, "disassociated");
1780 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1781 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1782 /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1784 accounting_sta_stop(hapd, sta);
1785 ieee802_1x_free_station(sta);
1787 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
1788 ap_sta_ip6addr_del(hapd, sta);
1789 hostapd_drv_sta_remove(hapd, sta->addr);
1791 if (sta->timeout_next == STA_NULLFUNC ||
1792 sta->timeout_next == STA_DISASSOC) {
1793 sta->timeout_next = STA_DEAUTH;
1794 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1795 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1799 mlme_disassociate_indication(
1800 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1804 static void handle_deauth(struct hostapd_data *hapd,
1805 const struct ieee80211_mgmt *mgmt, size_t len)
1807 struct sta_info *sta;
1809 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
1810 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "handle_deauth - too short "
1811 "payload (len=%lu)", (unsigned long) len);
1815 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "deauthentication: STA=" MACSTR
1817 MAC2STR(mgmt->sa), le_to_host16(mgmt->u.deauth.reason_code));
1819 sta = ap_get_sta(hapd, mgmt->sa);
1821 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR " trying "
1822 "to deauthenticate, but it is not authenticated",
1827 ap_sta_set_authorized(hapd, sta, 0);
1828 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
1829 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1830 WLAN_STA_ASSOC_REQ_OK);
1831 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1832 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1833 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1834 mlme_deauthenticate_indication(
1835 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1836 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1837 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1838 ap_free_sta(hapd, sta);
1842 static void handle_beacon(struct hostapd_data *hapd,
1843 const struct ieee80211_mgmt *mgmt, size_t len,
1844 struct hostapd_frame_info *fi)
1846 struct ieee802_11_elems elems;
1848 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1849 wpa_printf(MSG_INFO, "handle_beacon - too short payload (len=%lu)",
1850 (unsigned long) len);
1854 (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
1855 len - (IEEE80211_HDRLEN +
1856 sizeof(mgmt->u.beacon)), &elems,
1859 ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1863 #ifdef CONFIG_IEEE80211W
1865 static int hostapd_sa_query_action(struct hostapd_data *hapd,
1866 const struct ieee80211_mgmt *mgmt,
1871 end = mgmt->u.action.u.sa_query_resp.trans_id +
1872 WLAN_SA_QUERY_TR_ID_LEN;
1873 if (((u8 *) mgmt) + len < end) {
1874 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
1875 "frame (len=%lu)", (unsigned long) len);
1879 ieee802_11_sa_query_action(hapd, mgmt->sa,
1880 mgmt->u.action.u.sa_query_resp.action,
1881 mgmt->u.action.u.sa_query_resp.trans_id);
1886 static int robust_action_frame(u8 category)
1888 return category != WLAN_ACTION_PUBLIC &&
1889 category != WLAN_ACTION_HT;
1891 #endif /* CONFIG_IEEE80211W */
1894 static int handle_action(struct hostapd_data *hapd,
1895 const struct ieee80211_mgmt *mgmt, size_t len)
1897 struct sta_info *sta;
1898 sta = ap_get_sta(hapd, mgmt->sa);
1900 if (len < IEEE80211_HDRLEN + 1) {
1901 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1902 HOSTAPD_LEVEL_DEBUG,
1903 "handle_action - too short payload (len=%lu)",
1904 (unsigned long) len);
1908 if (mgmt->u.action.category != WLAN_ACTION_PUBLIC &&
1909 (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))) {
1910 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored Action "
1911 "frame (category=%u) from unassociated STA " MACSTR,
1912 MAC2STR(mgmt->sa), mgmt->u.action.category);
1916 #ifdef CONFIG_IEEE80211W
1917 if (sta && (sta->flags & WLAN_STA_MFP) &&
1918 !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP)) &&
1919 robust_action_frame(mgmt->u.action.category)) {
1920 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1921 HOSTAPD_LEVEL_DEBUG,
1922 "Dropped unprotected Robust Action frame from "
1926 #endif /* CONFIG_IEEE80211W */
1929 u16 fc = le_to_host16(mgmt->frame_control);
1930 u16 seq_ctrl = le_to_host16(mgmt->seq_ctrl);
1932 if ((fc & WLAN_FC_RETRY) &&
1933 sta->last_seq_ctrl != WLAN_INVALID_MGMT_SEQ &&
1934 sta->last_seq_ctrl == seq_ctrl &&
1935 sta->last_subtype == WLAN_FC_STYPE_ACTION) {
1936 hostapd_logger(hapd, sta->addr,
1937 HOSTAPD_MODULE_IEEE80211,
1938 HOSTAPD_LEVEL_DEBUG,
1939 "Drop repeated action frame seq_ctrl=0x%x",
1944 sta->last_seq_ctrl = seq_ctrl;
1945 sta->last_subtype = WLAN_FC_STYPE_ACTION;
1948 switch (mgmt->u.action.category) {
1949 #ifdef CONFIG_IEEE80211R
1950 case WLAN_ACTION_FT:
1952 wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1953 len - IEEE80211_HDRLEN))
1956 #endif /* CONFIG_IEEE80211R */
1957 case WLAN_ACTION_WMM:
1958 hostapd_wmm_action(hapd, mgmt, len);
1960 #ifdef CONFIG_IEEE80211W
1961 case WLAN_ACTION_SA_QUERY:
1962 return hostapd_sa_query_action(hapd, mgmt, len);
1963 #endif /* CONFIG_IEEE80211W */
1965 case WLAN_ACTION_WNM:
1966 ieee802_11_rx_wnm_action_ap(hapd, mgmt, len);
1968 #endif /* CONFIG_WNM */
1969 case WLAN_ACTION_PUBLIC:
1970 case WLAN_ACTION_PROTECTED_DUAL:
1971 #ifdef CONFIG_IEEE80211N
1972 if (mgmt->u.action.u.public_action.action ==
1973 WLAN_PA_20_40_BSS_COEX) {
1974 wpa_printf(MSG_DEBUG,
1975 "HT20/40 coex mgmt frame received from STA "
1976 MACSTR, MAC2STR(mgmt->sa));
1977 hostapd_2040_coex_action(hapd, mgmt, len);
1979 #endif /* CONFIG_IEEE80211N */
1980 if (hapd->public_action_cb) {
1981 hapd->public_action_cb(hapd->public_action_cb_ctx,
1985 if (hapd->public_action_cb2) {
1986 hapd->public_action_cb2(hapd->public_action_cb2_ctx,
1990 if (hapd->public_action_cb || hapd->public_action_cb2)
1993 case WLAN_ACTION_VENDOR_SPECIFIC:
1994 if (hapd->vendor_action_cb) {
1995 if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
1997 hapd->iface->freq) == 0)
2003 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2004 HOSTAPD_LEVEL_DEBUG,
2005 "handle_action - unknown action category %d or invalid "
2007 mgmt->u.action.category);
2008 if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
2009 !(mgmt->sa[0] & 0x01)) {
2010 struct ieee80211_mgmt *resp;
2013 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
2014 * Return the Action frame to the source without change
2015 * except that MSB of the Category set to 1.
2017 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
2018 "frame back to sender");
2019 resp = os_malloc(len);
2022 os_memcpy(resp, mgmt, len);
2023 os_memcpy(resp->da, resp->sa, ETH_ALEN);
2024 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
2025 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
2026 resp->u.action.category |= 0x80;
2028 if (hostapd_drv_send_mlme(hapd, resp, len, 0) < 0) {
2029 wpa_printf(MSG_ERROR, "IEEE 802.11: Failed to send "
2040 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
2041 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
2043 * @buf: management frame data (starting from IEEE 802.11 header)
2044 * @len: length of frame data in octets
2045 * @fi: meta data about received frame (signal level, etc.)
2047 * Process all incoming IEEE 802.11 management frames. This will be called for
2048 * each frame received from the kernel driver through wlan#ap interface. In
2049 * addition, it can be called to re-inserted pending frames (e.g., when using
2050 * external RADIUS server as an MAC ACL).
2052 int ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
2053 struct hostapd_frame_info *fi)
2055 struct ieee80211_mgmt *mgmt;
2063 mgmt = (struct ieee80211_mgmt *) buf;
2064 fc = le_to_host16(mgmt->frame_control);
2065 stype = WLAN_FC_GET_STYPE(fc);
2067 if (stype == WLAN_FC_STYPE_BEACON) {
2068 handle_beacon(hapd, mgmt, len, fi);
2072 broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
2073 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
2074 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
2078 /* Invitation responses can be sent with the peer MAC as BSSID */
2079 !((hapd->conf->p2p & P2P_GROUP_OWNER) &&
2080 stype == WLAN_FC_STYPE_ACTION) &&
2081 #endif /* CONFIG_P2P */
2083 !(hapd->conf->mesh & MESH_ENABLED) &&
2084 #endif /* CONFIG_MESH */
2085 os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
2086 wpa_printf(MSG_INFO, "MGMT: BSSID=" MACSTR " not our address",
2087 MAC2STR(mgmt->bssid));
2092 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
2093 handle_probe_req(hapd, mgmt, len, fi->ssi_signal);
2097 if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
2098 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2099 HOSTAPD_LEVEL_DEBUG,
2100 "MGMT: DA=" MACSTR " not our address",
2106 case WLAN_FC_STYPE_AUTH:
2107 wpa_printf(MSG_DEBUG, "mgmt::auth");
2108 handle_auth(hapd, mgmt, len);
2111 case WLAN_FC_STYPE_ASSOC_REQ:
2112 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
2113 handle_assoc(hapd, mgmt, len, 0);
2116 case WLAN_FC_STYPE_REASSOC_REQ:
2117 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
2118 handle_assoc(hapd, mgmt, len, 1);
2121 case WLAN_FC_STYPE_DISASSOC:
2122 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
2123 handle_disassoc(hapd, mgmt, len);
2126 case WLAN_FC_STYPE_DEAUTH:
2127 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "mgmt::deauth");
2128 handle_deauth(hapd, mgmt, len);
2131 case WLAN_FC_STYPE_ACTION:
2132 wpa_printf(MSG_DEBUG, "mgmt::action");
2133 ret = handle_action(hapd, mgmt, len);
2136 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2137 HOSTAPD_LEVEL_DEBUG,
2138 "unknown mgmt frame subtype %d", stype);
2146 static void handle_auth_cb(struct hostapd_data *hapd,
2147 const struct ieee80211_mgmt *mgmt,
2150 u16 auth_alg, auth_transaction, status_code;
2151 struct sta_info *sta;
2154 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
2155 HOSTAPD_LEVEL_NOTICE,
2156 "did not acknowledge authentication response");
2160 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
2161 wpa_printf(MSG_INFO, "handle_auth_cb - too short payload (len=%lu)",
2162 (unsigned long) len);
2166 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
2167 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
2168 status_code = le_to_host16(mgmt->u.auth.status_code);
2170 sta = ap_get_sta(hapd, mgmt->da);
2172 wpa_printf(MSG_INFO, "handle_auth_cb: STA " MACSTR " not found",
2177 if (status_code == WLAN_STATUS_SUCCESS &&
2178 ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
2179 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
2180 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2181 HOSTAPD_LEVEL_INFO, "authenticated");
2182 sta->flags |= WLAN_STA_AUTH;
2187 static void hostapd_set_wds_encryption(struct hostapd_data *hapd,
2188 struct sta_info *sta,
2192 struct hostapd_ssid *ssid = sta->ssid;
2194 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
2197 for (i = 0; i < 4; i++) {
2198 if (ssid->wep.key[i] &&
2199 hostapd_drv_set_key(ifname_wds, hapd, WPA_ALG_WEP, NULL, i,
2200 i == ssid->wep.idx, NULL, 0,
2201 ssid->wep.key[i], ssid->wep.len[i])) {
2202 wpa_printf(MSG_WARNING,
2203 "Could not set WEP keys for WDS interface; %s",
2211 static void handle_assoc_cb(struct hostapd_data *hapd,
2212 const struct ieee80211_mgmt *mgmt,
2213 size_t len, int reassoc, int ok)
2216 struct sta_info *sta;
2218 struct ieee80211_ht_capabilities ht_cap;
2219 struct ieee80211_vht_capabilities vht_cap;
2221 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
2222 sizeof(mgmt->u.assoc_resp))) {
2223 wpa_printf(MSG_INFO, "handle_assoc_cb(reassoc=%d) - too short payload (len=%lu)",
2224 reassoc, (unsigned long) len);
2228 sta = ap_get_sta(hapd, mgmt->da);
2230 wpa_printf(MSG_INFO, "handle_assoc_cb: STA " MACSTR " not found",
2236 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
2237 HOSTAPD_LEVEL_DEBUG,
2238 "did not acknowledge association response");
2239 sta->flags &= ~WLAN_STA_ASSOC_REQ_OK;
2244 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
2246 status = le_to_host16(mgmt->u.assoc_resp.status_code);
2248 if (status != WLAN_STATUS_SUCCESS)
2251 /* Stop previous accounting session, if one is started, and allocate
2252 * new session id for the new session. */
2253 accounting_sta_stop(hapd, sta);
2255 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2257 "associated (aid %d)",
2260 if (sta->flags & WLAN_STA_ASSOC)
2262 sta->flags |= WLAN_STA_ASSOC;
2263 sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
2264 if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa && !hapd->conf->osen) ||
2265 sta->auth_alg == WLAN_AUTH_FT) {
2267 * Open, static WEP, or FT protocol; no separate authorization
2270 ap_sta_set_authorized(hapd, sta, 1);
2274 mlme_reassociate_indication(hapd, sta);
2276 mlme_associate_indication(hapd, sta);
2278 #ifdef CONFIG_IEEE80211W
2279 sta->sa_query_timed_out = 0;
2280 #endif /* CONFIG_IEEE80211W */
2283 * Remove the STA entry in order to make sure the STA PS state gets
2284 * cleared and configuration gets updated in case of reassociation back
2287 hostapd_drv_sta_remove(hapd, sta->addr);
2289 #ifdef CONFIG_IEEE80211N
2290 if (sta->flags & WLAN_STA_HT)
2291 hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
2292 #endif /* CONFIG_IEEE80211N */
2293 #ifdef CONFIG_IEEE80211AC
2294 if (sta->flags & WLAN_STA_VHT)
2295 hostapd_get_vht_capab(hapd, sta->vht_capabilities, &vht_cap);
2296 #endif /* CONFIG_IEEE80211AC */
2298 if (hostapd_sta_add(hapd, sta->addr, sta->aid, sta->capability,
2299 sta->supported_rates, sta->supported_rates_len,
2300 sta->listen_interval,
2301 sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
2302 sta->flags & WLAN_STA_VHT ? &vht_cap : NULL,
2303 sta->flags, sta->qosinfo, sta->vht_opmode)) {
2304 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
2305 HOSTAPD_LEVEL_NOTICE,
2306 "Could not add STA to kernel driver");
2308 ap_sta_disconnect(hapd, sta, sta->addr,
2309 WLAN_REASON_DISASSOC_AP_BUSY);
2314 if (sta->flags & WLAN_STA_WDS) {
2316 char ifname_wds[IFNAMSIZ + 1];
2318 ret = hostapd_set_wds_sta(hapd, ifname_wds, sta->addr,
2321 hostapd_set_wds_encryption(hapd, sta, ifname_wds);
2324 if (sta->eapol_sm == NULL) {
2326 * This STA does not use RADIUS server for EAP authentication,
2327 * so bind it to the selected VLAN interface now, since the
2328 * interface selection is not going to change anymore.
2330 if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
2332 } else if (sta->vlan_id) {
2333 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
2334 if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
2338 hostapd_set_sta_flags(hapd, sta);
2340 if (sta->auth_alg == WLAN_AUTH_FT)
2341 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
2343 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
2344 hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
2346 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
2350 static void handle_deauth_cb(struct hostapd_data *hapd,
2351 const struct ieee80211_mgmt *mgmt,
2354 struct sta_info *sta;
2355 if (mgmt->da[0] & 0x01)
2357 sta = ap_get_sta(hapd, mgmt->da);
2359 wpa_printf(MSG_DEBUG, "handle_deauth_cb: STA " MACSTR
2360 " not found", MAC2STR(mgmt->da));
2364 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged deauth",
2365 MAC2STR(sta->addr));
2367 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
2368 "deauth", MAC2STR(sta->addr));
2370 ap_sta_deauth_cb(hapd, sta);
2374 static void handle_disassoc_cb(struct hostapd_data *hapd,
2375 const struct ieee80211_mgmt *mgmt,
2378 struct sta_info *sta;
2379 if (mgmt->da[0] & 0x01)
2381 sta = ap_get_sta(hapd, mgmt->da);
2383 wpa_printf(MSG_DEBUG, "handle_disassoc_cb: STA " MACSTR
2384 " not found", MAC2STR(mgmt->da));
2388 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged disassoc",
2389 MAC2STR(sta->addr));
2391 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
2392 "disassoc", MAC2STR(sta->addr));
2394 ap_sta_disassoc_cb(hapd, sta);
2399 * ieee802_11_mgmt_cb - Process management frame TX status callback
2400 * @hapd: hostapd BSS data structure (the BSS from which the management frame
2402 * @buf: management frame data (starting from IEEE 802.11 header)
2403 * @len: length of frame data in octets
2404 * @stype: management frame subtype from frame control field
2405 * @ok: Whether the frame was ACK'ed
2407 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
2410 const struct ieee80211_mgmt *mgmt;
2411 mgmt = (const struct ieee80211_mgmt *) buf;
2413 #ifdef CONFIG_TESTING_OPTIONS
2414 if (hapd->ext_mgmt_frame_handling) {
2415 wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-TX-STATUS stype=%u ok=%d",
2419 #endif /* CONFIG_TESTING_OPTIONS */
2422 case WLAN_FC_STYPE_AUTH:
2423 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
2424 handle_auth_cb(hapd, mgmt, len, ok);
2426 case WLAN_FC_STYPE_ASSOC_RESP:
2427 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
2428 handle_assoc_cb(hapd, mgmt, len, 0, ok);
2430 case WLAN_FC_STYPE_REASSOC_RESP:
2431 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
2432 handle_assoc_cb(hapd, mgmt, len, 1, ok);
2434 case WLAN_FC_STYPE_PROBE_RESP:
2435 wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb");
2437 case WLAN_FC_STYPE_DEAUTH:
2438 wpa_printf(MSG_DEBUG, "mgmt::deauth cb");
2439 handle_deauth_cb(hapd, mgmt, len, ok);
2441 case WLAN_FC_STYPE_DISASSOC:
2442 wpa_printf(MSG_DEBUG, "mgmt::disassoc cb");
2443 handle_disassoc_cb(hapd, mgmt, len, ok);
2445 case WLAN_FC_STYPE_ACTION:
2446 wpa_printf(MSG_DEBUG, "mgmt::action cb");
2449 wpa_printf(MSG_INFO, "unknown mgmt cb frame subtype %d", stype);
2455 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2462 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2463 char *buf, size_t buflen)
2470 void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
2471 const u8 *buf, size_t len, int ack)
2473 struct sta_info *sta;
2474 struct hostapd_iface *iface = hapd->iface;
2476 sta = ap_get_sta(hapd, addr);
2477 if (sta == NULL && iface->num_bss > 1) {
2479 for (j = 0; j < iface->num_bss; j++) {
2480 hapd = iface->bss[j];
2481 sta = ap_get_sta(hapd, addr);
2486 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))
2488 if (sta->flags & WLAN_STA_PENDING_POLL) {
2489 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
2490 "activity poll", MAC2STR(sta->addr),
2491 ack ? "ACKed" : "did not ACK");
2493 sta->flags &= ~WLAN_STA_PENDING_POLL;
2496 ieee802_1x_tx_status(hapd, sta, buf, len, ack);
2500 void hostapd_eapol_tx_status(struct hostapd_data *hapd, const u8 *dst,
2501 const u8 *data, size_t len, int ack)
2503 struct sta_info *sta;
2504 struct hostapd_iface *iface = hapd->iface;
2506 sta = ap_get_sta(hapd, dst);
2507 if (sta == NULL && iface->num_bss > 1) {
2509 for (j = 0; j < iface->num_bss; j++) {
2510 hapd = iface->bss[j];
2511 sta = ap_get_sta(hapd, dst);
2516 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
2517 wpa_printf(MSG_DEBUG, "Ignore TX status for Data frame to STA "
2518 MACSTR " that is not currently associated",
2523 ieee802_1x_eapol_tx_status(hapd, sta, data, len, ack);
2527 void hostapd_client_poll_ok(struct hostapd_data *hapd, const u8 *addr)
2529 struct sta_info *sta;
2530 struct hostapd_iface *iface = hapd->iface;
2532 sta = ap_get_sta(hapd, addr);
2533 if (sta == NULL && iface->num_bss > 1) {
2535 for (j = 0; j < iface->num_bss; j++) {
2536 hapd = iface->bss[j];
2537 sta = ap_get_sta(hapd, addr);
2544 if (!(sta->flags & WLAN_STA_PENDING_POLL))
2547 wpa_printf(MSG_DEBUG, "STA " MACSTR " ACKed pending "
2548 "activity poll", MAC2STR(sta->addr));
2549 sta->flags &= ~WLAN_STA_PENDING_POLL;
2553 void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
2556 struct sta_info *sta;
2558 sta = ap_get_sta(hapd, src);
2559 if (sta && (sta->flags & WLAN_STA_ASSOC)) {
2560 if (!hapd->conf->wds_sta)
2563 if (wds && !(sta->flags & WLAN_STA_WDS)) {
2565 char ifname_wds[IFNAMSIZ + 1];
2567 wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
2568 "STA " MACSTR " (aid %u)",
2569 MAC2STR(sta->addr), sta->aid);
2570 sta->flags |= WLAN_STA_WDS;
2571 ret = hostapd_set_wds_sta(hapd, ifname_wds,
2572 sta->addr, sta->aid, 1);
2574 hostapd_set_wds_encryption(hapd, sta,
2580 wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
2581 MACSTR, MAC2STR(src));
2582 if (src[0] & 0x01) {
2583 /* Broadcast bit set in SA?! Ignore the frame silently. */
2587 if (sta && (sta->flags & WLAN_STA_ASSOC_REQ_OK)) {
2588 wpa_printf(MSG_DEBUG, "Association Response to the STA has "
2589 "already been sent, but no TX status yet known - "
2590 "ignore Class 3 frame issue with " MACSTR,
2595 if (sta && (sta->flags & WLAN_STA_AUTH))
2596 hostapd_drv_sta_disassoc(
2598 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
2600 hostapd_drv_sta_deauth(
2602 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
2606 #endif /* CONFIG_NATIVE_WINDOWS */