2 * hostapd / IEEE 802.11 Management
3 * Copyright (c) 2002-2010, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
15 #include "utils/includes.h"
17 #ifndef CONFIG_NATIVE_WINDOWS
19 #include "utils/common.h"
20 #include "utils/eloop.h"
21 #include "crypto/crypto.h"
22 #include "drivers/driver.h"
23 #include "common/ieee802_11_defs.h"
24 #include "common/ieee802_11_common.h"
25 #include "common/wpa_ctrl.h"
26 #include "radius/radius.h"
27 #include "radius/radius_client.h"
31 #include "ieee802_11_auth.h"
33 #include "ieee802_1x.h"
37 #include "accounting.h"
38 #include "ap_config.h"
40 #include "ieee802_11.h"
43 u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
48 if (hapd->iface->current_rates == NULL)
51 *pos++ = WLAN_EID_SUPP_RATES;
52 num = hapd->iface->num_rates;
54 /* rest of the rates are encoded in Extended supported
61 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
64 *pos = hapd->iface->current_rates[i].rate / 5;
65 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
74 u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
79 if (hapd->iface->current_rates == NULL)
82 num = hapd->iface->num_rates;
87 *pos++ = WLAN_EID_EXT_SUPP_RATES;
90 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
94 continue; /* already in SuppRates IE */
95 *pos = hapd->iface->current_rates[i].rate / 5;
96 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
105 u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
108 int capab = WLAN_CAPABILITY_ESS;
111 if (hapd->iface->num_sta_no_short_preamble == 0 &&
112 hapd->iconf->preamble == SHORT_PREAMBLE)
113 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
115 privacy = hapd->conf->ssid.wep.keys_set;
117 if (hapd->conf->ieee802_1x &&
118 (hapd->conf->default_wep_key_len ||
119 hapd->conf->individual_wep_key_len))
126 int policy, def_klen;
127 if (probe && sta->ssid_probe) {
128 policy = sta->ssid_probe->security_policy;
129 def_klen = sta->ssid_probe->wep.default_len;
131 policy = sta->ssid->security_policy;
132 def_klen = sta->ssid->wep.default_len;
134 privacy = policy != SECURITY_PLAINTEXT;
135 if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
140 capab |= WLAN_CAPABILITY_PRIVACY;
142 if (hapd->iface->current_mode &&
143 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
144 hapd->iface->num_sta_no_short_slot_time == 0)
145 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
151 #ifdef CONFIG_IEEE80211W
152 static u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
153 struct sta_info *sta, u8 *eid)
157 struct os_time now, passed;
159 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
161 *pos++ = WLAN_TIMEOUT_ASSOC_COMEBACK;
163 os_time_sub(&now, &sta->sa_query_start, &passed);
164 tu = (passed.sec * 1000000 + passed.usec) / 1024;
165 if (hapd->conf->assoc_sa_query_max_timeout > tu)
166 timeout = hapd->conf->assoc_sa_query_max_timeout - tu;
169 if (timeout < hapd->conf->assoc_sa_query_max_timeout)
170 timeout++; /* add some extra time for local timers */
171 WPA_PUT_LE32(pos, timeout);
176 #endif /* CONFIG_IEEE80211W */
179 void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
182 if (len > HOSTAPD_MAX_SSID_LEN)
183 len = HOSTAPD_MAX_SSID_LEN;
184 for (i = 0; i < len; i++) {
185 if (ssid[i] >= 32 && ssid[i] < 127)
195 * ieee802_11_send_deauth - Send Deauthentication frame
196 * @hapd: hostapd BSS data
197 * @addr: Address of the destination STA
198 * @reason: Reason code for Deauthentication
200 void ieee802_11_send_deauth(struct hostapd_data *hapd, const u8 *addr,
203 struct ieee80211_mgmt mgmt;
205 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
207 "deauthenticate - reason %d", reason);
208 os_memset(&mgmt, 0, sizeof(mgmt));
209 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
210 WLAN_FC_STYPE_DEAUTH);
211 os_memcpy(mgmt.da, addr, ETH_ALEN);
212 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
213 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
214 mgmt.u.deauth.reason_code = host_to_le16(reason);
215 if (hapd->drv.send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
216 sizeof(mgmt.u.deauth)) < 0)
217 perror("ieee802_11_send_deauth: send");
221 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
222 u16 auth_transaction, const u8 *challenge,
225 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
227 "authentication (shared key, transaction %d)",
230 if (auth_transaction == 1) {
231 if (!sta->challenge) {
232 /* Generate a pseudo-random challenge */
236 sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
237 if (sta->challenge == NULL)
238 return WLAN_STATUS_UNSPECIFIED_FAILURE;
242 os_memcpy(key, &now, 4);
243 os_memcpy(key + 4, &r, 4);
244 rc4_skip(key, sizeof(key), 0,
245 sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
250 if (auth_transaction != 3)
251 return WLAN_STATUS_UNSPECIFIED_FAILURE;
254 if (!iswep || !sta->challenge || !challenge ||
255 os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
256 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
258 "shared key authentication - invalid "
259 "challenge-response");
260 return WLAN_STATUS_CHALLENGE_FAIL;
263 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
265 "authentication OK (shared key)");
266 #ifdef IEEE80211_REQUIRE_AUTH_ACK
267 /* Station will be marked authenticated if it ACKs the
268 * authentication reply. */
270 sta->flags |= WLAN_STA_AUTH;
271 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
273 os_free(sta->challenge);
274 sta->challenge = NULL;
280 static void send_auth_reply(struct hostapd_data *hapd,
281 const u8 *dst, const u8 *bssid,
282 u16 auth_alg, u16 auth_transaction, u16 resp,
283 const u8 *ies, size_t ies_len)
285 struct ieee80211_mgmt *reply;
289 rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
290 buf = os_zalloc(rlen);
294 reply = (struct ieee80211_mgmt *) buf;
295 reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
297 os_memcpy(reply->da, dst, ETH_ALEN);
298 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
299 os_memcpy(reply->bssid, bssid, ETH_ALEN);
301 reply->u.auth.auth_alg = host_to_le16(auth_alg);
302 reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
303 reply->u.auth.status_code = host_to_le16(resp);
306 os_memcpy(reply->u.auth.variable, ies, ies_len);
308 wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
309 " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
310 MAC2STR(dst), auth_alg, auth_transaction,
311 resp, (unsigned long) ies_len);
312 if (hapd->drv.send_mgmt_frame(hapd, reply, rlen) < 0)
313 perror("send_auth_reply: send");
319 #ifdef CONFIG_IEEE80211R
320 static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
321 u16 auth_transaction, u16 status,
322 const u8 *ies, size_t ies_len)
324 struct hostapd_data *hapd = ctx;
325 struct sta_info *sta;
327 send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
328 status, ies, ies_len);
330 if (status != WLAN_STATUS_SUCCESS)
333 sta = ap_get_sta(hapd, dst);
337 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
338 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
339 sta->flags |= WLAN_STA_AUTH;
340 mlme_authenticate_indication(hapd, sta);
342 #endif /* CONFIG_IEEE80211R */
345 static void handle_auth(struct hostapd_data *hapd,
346 const struct ieee80211_mgmt *mgmt, size_t len)
348 u16 auth_alg, auth_transaction, status_code;
349 u16 resp = WLAN_STATUS_SUCCESS;
350 struct sta_info *sta = NULL;
353 const u8 *challenge = NULL;
354 u32 session_timeout, acct_interim_interval;
356 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
357 size_t resp_ies_len = 0;
359 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
360 printf("handle_auth - too short payload (len=%lu)\n",
361 (unsigned long) len);
365 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
366 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
367 status_code = le_to_host16(mgmt->u.auth.status_code);
368 fc = le_to_host16(mgmt->frame_control);
370 if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
371 2 + WLAN_AUTH_CHALLENGE_LEN &&
372 mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
373 mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
374 challenge = &mgmt->u.auth.variable[2];
376 wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
377 "auth_transaction=%d status_code=%d wep=%d%s",
378 MAC2STR(mgmt->sa), auth_alg, auth_transaction,
379 status_code, !!(fc & WLAN_FC_ISWEP),
380 challenge ? " challenge" : "");
382 if (hapd->tkip_countermeasures) {
383 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
387 if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
388 auth_alg == WLAN_AUTH_OPEN) ||
389 #ifdef CONFIG_IEEE80211R
391 (hapd->conf->wpa_key_mgmt &
392 (WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK)) &&
393 auth_alg == WLAN_AUTH_FT) ||
394 #endif /* CONFIG_IEEE80211R */
395 ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
396 auth_alg == WLAN_AUTH_SHARED_KEY))) {
397 printf("Unsupported authentication algorithm (%d)\n",
399 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
403 if (!(auth_transaction == 1 ||
404 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
405 printf("Unknown authentication transaction number (%d)\n",
407 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
411 if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
412 printf("Station " MACSTR " not allowed to authenticate.\n",
414 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
418 res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
420 &acct_interim_interval, &vlan_id);
421 if (res == HOSTAPD_ACL_REJECT) {
422 printf("Station " MACSTR " not allowed to authenticate.\n",
424 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
427 if (res == HOSTAPD_ACL_PENDING) {
428 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
429 " waiting for an external authentication",
431 /* Authentication code will re-send the authentication frame
432 * after it has received (and cached) information from the
433 * external source. */
437 sta = ap_sta_add(hapd, mgmt->sa);
439 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
444 if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
446 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
447 HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
448 "%d received from RADIUS server",
450 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
453 sta->vlan_id = vlan_id;
454 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
455 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
458 sta->flags &= ~WLAN_STA_PREAUTH;
459 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
461 if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
462 sta->acct_interim_interval = acct_interim_interval;
463 if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
464 ap_sta_session_timeout(hapd, sta, session_timeout);
466 ap_sta_no_session_timeout(hapd, sta);
470 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
472 "authentication OK (open system)");
473 #ifdef IEEE80211_REQUIRE_AUTH_ACK
474 /* Station will be marked authenticated if it ACKs the
475 * authentication reply. */
477 sta->flags |= WLAN_STA_AUTH;
478 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
479 sta->auth_alg = WLAN_AUTH_OPEN;
480 mlme_authenticate_indication(hapd, sta);
483 case WLAN_AUTH_SHARED_KEY:
484 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
486 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
487 mlme_authenticate_indication(hapd, sta);
488 if (sta->challenge && auth_transaction == 1) {
489 resp_ies[0] = WLAN_EID_CHALLENGE;
490 resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
491 os_memcpy(resp_ies + 2, sta->challenge,
492 WLAN_AUTH_CHALLENGE_LEN);
493 resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
496 #ifdef CONFIG_IEEE80211R
498 sta->auth_alg = WLAN_AUTH_FT;
499 if (sta->wpa_sm == NULL)
500 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
502 if (sta->wpa_sm == NULL) {
503 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
505 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
508 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
509 auth_transaction, mgmt->u.auth.variable,
510 len - IEEE80211_HDRLEN -
511 sizeof(mgmt->u.auth),
512 handle_auth_ft_finish, hapd);
513 /* handle_auth_ft_finish() callback will complete auth. */
515 #endif /* CONFIG_IEEE80211R */
519 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
520 auth_transaction + 1, resp, resp_ies, resp_ies_len);
524 static int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
528 /* get a unique AID */
530 wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
534 for (i = 0; i < AID_WORDS; i++) {
535 if (hapd->sta_aid[i] == (u32) -1)
537 for (j = 0; j < 32; j++) {
538 if (!(hapd->sta_aid[i] & BIT(j)))
546 aid = i * 32 + j + 1;
551 hapd->sta_aid[i] |= BIT(j);
552 wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
557 static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
558 const u8 *ssid_ie, size_t ssid_ie_len)
561 return WLAN_STATUS_UNSPECIFIED_FAILURE;
563 if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
564 os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
566 ieee802_11_print_ssid(ssid_txt, ssid_ie, ssid_ie_len);
567 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
569 "Station tried to associate with unknown SSID "
571 return WLAN_STATUS_UNSPECIFIED_FAILURE;
574 return WLAN_STATUS_SUCCESS;
578 static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
579 const u8 *wmm_ie, size_t wmm_ie_len)
581 sta->flags &= ~WLAN_STA_WMM;
582 if (wmm_ie && hapd->conf->wmm_enabled) {
583 if (hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len))
584 hostapd_logger(hapd, sta->addr,
587 "invalid WMM element in association "
590 sta->flags |= WLAN_STA_WMM;
592 return WLAN_STATUS_SUCCESS;
596 static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
597 struct ieee802_11_elems *elems)
599 if (!elems->supp_rates) {
600 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
602 "No supported rates element in AssocReq");
603 return WLAN_STATUS_UNSPECIFIED_FAILURE;
606 if (elems->supp_rates_len > sizeof(sta->supported_rates)) {
607 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
609 "Invalid supported rates element length %d",
610 elems->supp_rates_len);
611 return WLAN_STATUS_UNSPECIFIED_FAILURE;
614 os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
615 os_memcpy(sta->supported_rates, elems->supp_rates,
616 elems->supp_rates_len);
617 sta->supported_rates_len = elems->supp_rates_len;
619 if (elems->ext_supp_rates) {
620 if (elems->supp_rates_len + elems->ext_supp_rates_len >
621 sizeof(sta->supported_rates)) {
622 hostapd_logger(hapd, sta->addr,
623 HOSTAPD_MODULE_IEEE80211,
625 "Invalid supported rates element length"
626 " %d+%d", elems->supp_rates_len,
627 elems->ext_supp_rates_len);
628 return WLAN_STATUS_UNSPECIFIED_FAILURE;
631 os_memcpy(sta->supported_rates + elems->supp_rates_len,
632 elems->ext_supp_rates, elems->ext_supp_rates_len);
633 sta->supported_rates_len += elems->ext_supp_rates_len;
636 return WLAN_STATUS_SUCCESS;
640 static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
641 const u8 *ies, size_t ies_len, int reassoc)
643 struct ieee802_11_elems elems;
648 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
649 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
650 HOSTAPD_LEVEL_INFO, "Station sent an invalid "
651 "association request");
652 return WLAN_STATUS_UNSPECIFIED_FAILURE;
655 resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
656 if (resp != WLAN_STATUS_SUCCESS)
658 resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
659 if (resp != WLAN_STATUS_SUCCESS)
661 resp = copy_supp_rates(hapd, sta, &elems);
662 if (resp != WLAN_STATUS_SUCCESS)
664 #ifdef CONFIG_IEEE80211N
665 resp = copy_sta_ht_capab(sta, elems.ht_capabilities,
666 elems.ht_capabilities_len);
667 if (resp != WLAN_STATUS_SUCCESS)
669 #endif /* CONFIG_IEEE80211N */
671 if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
672 wpa_ie = elems.rsn_ie;
673 wpa_ie_len = elems.rsn_ie_len;
674 } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
676 wpa_ie = elems.wpa_ie;
677 wpa_ie_len = elems.wpa_ie_len;
684 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
685 if (hapd->conf->wps_state && elems.wps_ie) {
686 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
687 "Request - assume WPS is used");
688 sta->flags |= WLAN_STA_WPS;
689 wpabuf_free(sta->wps_ie);
690 sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
694 } else if (hapd->conf->wps_state && wpa_ie == NULL) {
695 wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
696 "(Re)Association Request - possible WPS use");
697 sta->flags |= WLAN_STA_MAYBE_WPS;
699 #endif /* CONFIG_WPS */
700 if (hapd->conf->wpa && wpa_ie == NULL) {
701 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
703 "No WPA/RSN IE in association request");
704 return WLAN_STATUS_INVALID_IE;
707 if (hapd->conf->wpa && wpa_ie) {
711 if (sta->wpa_sm == NULL)
712 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
714 if (sta->wpa_sm == NULL) {
715 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
717 return WLAN_STATUS_UNSPECIFIED_FAILURE;
719 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
721 elems.mdie, elems.mdie_len);
722 if (res == WPA_INVALID_GROUP)
723 resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
724 else if (res == WPA_INVALID_PAIRWISE)
725 resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
726 else if (res == WPA_INVALID_AKMP)
727 resp = WLAN_STATUS_AKMP_NOT_VALID;
728 else if (res == WPA_ALLOC_FAIL)
729 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
730 #ifdef CONFIG_IEEE80211W
731 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
732 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
733 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
734 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
735 #endif /* CONFIG_IEEE80211W */
736 else if (res == WPA_INVALID_MDIE)
737 resp = WLAN_STATUS_INVALID_MDIE;
738 else if (res != WPA_IE_OK)
739 resp = WLAN_STATUS_INVALID_IE;
740 if (resp != WLAN_STATUS_SUCCESS)
742 #ifdef CONFIG_IEEE80211W
743 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
744 sta->sa_query_count > 0)
745 ap_check_sa_query_timeout(hapd, sta);
746 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
747 (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
749 * STA has already been associated with MFP and SA
750 * Query timeout has not been reached. Reject the
751 * association attempt temporarily and start SA Query,
752 * if one is not pending.
755 if (sta->sa_query_count == 0)
756 ap_sta_start_sa_query(hapd, sta);
758 return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
761 if (wpa_auth_uses_mfp(sta->wpa_sm))
762 sta->flags |= WLAN_STA_MFP;
764 sta->flags &= ~WLAN_STA_MFP;
765 #endif /* CONFIG_IEEE80211W */
767 #ifdef CONFIG_IEEE80211R
768 if (sta->auth_alg == WLAN_AUTH_FT) {
770 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
771 "to use association (not "
772 "re-association) with FT auth_alg",
774 return WLAN_STATUS_UNSPECIFIED_FAILURE;
777 resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
779 if (resp != WLAN_STATUS_SUCCESS)
782 #endif /* CONFIG_IEEE80211R */
784 #ifdef CONFIG_IEEE80211N
785 if ((sta->flags & WLAN_STA_HT) &&
786 wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
787 hostapd_logger(hapd, sta->addr,
788 HOSTAPD_MODULE_IEEE80211,
790 "Station tried to use TKIP with HT "
792 return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
794 #endif /* CONFIG_IEEE80211N */
796 wpa_auth_sta_no_wpa(sta->wpa_sm);
798 return WLAN_STATUS_SUCCESS;
802 static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
806 struct ieee80211_mgmt reply;
808 os_memset(&reply, 0, sizeof(reply));
809 reply.frame_control =
810 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
811 os_memcpy(reply.da, addr, ETH_ALEN);
812 os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
813 os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
815 send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
816 reply.u.deauth.reason_code = host_to_le16(reason_code);
818 if (hapd->drv.send_mgmt_frame(hapd, &reply, send_len) < 0)
819 wpa_printf(MSG_INFO, "Failed to send deauth: %s",
824 static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
825 u16 status_code, int reassoc, const u8 *ies,
829 u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
830 struct ieee80211_mgmt *reply;
833 os_memset(buf, 0, sizeof(buf));
834 reply = (struct ieee80211_mgmt *) buf;
835 reply->frame_control =
836 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
837 (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
838 WLAN_FC_STYPE_ASSOC_RESP));
839 os_memcpy(reply->da, sta->addr, ETH_ALEN);
840 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
841 os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
843 send_len = IEEE80211_HDRLEN;
844 send_len += sizeof(reply->u.assoc_resp);
845 reply->u.assoc_resp.capab_info =
846 host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
847 reply->u.assoc_resp.status_code = host_to_le16(status_code);
848 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
849 | BIT(14) | BIT(15));
850 /* Supported rates */
851 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
852 /* Extended supported rates */
853 p = hostapd_eid_ext_supp_rates(hapd, p);
855 #ifdef CONFIG_IEEE80211R
856 if (status_code == WLAN_STATUS_SUCCESS) {
857 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
858 * Transition Information, RSN, [RIC Response] */
859 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
860 buf + sizeof(buf) - p,
861 sta->auth_alg, ies, ies_len);
863 #endif /* CONFIG_IEEE80211R */
865 #ifdef CONFIG_IEEE80211W
866 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
867 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
868 #endif /* CONFIG_IEEE80211W */
870 #ifdef CONFIG_IEEE80211N
871 p = hostapd_eid_ht_capabilities(hapd, p);
872 p = hostapd_eid_ht_operation(hapd, p);
873 #endif /* CONFIG_IEEE80211N */
875 if (sta->flags & WLAN_STA_WMM)
876 p = hostapd_eid_wmm(hapd, p);
879 if (sta->flags & WLAN_STA_WPS) {
880 struct wpabuf *wps = wps_build_assoc_resp_ie();
882 os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
883 p += wpabuf_len(wps);
887 #endif /* CONFIG_WPS */
889 send_len += p - reply->u.assoc_resp.variable;
891 if (hapd->drv.send_mgmt_frame(hapd, reply, send_len) < 0)
892 wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
897 static void handle_assoc(struct hostapd_data *hapd,
898 const struct ieee80211_mgmt *mgmt, size_t len,
901 u16 capab_info, listen_interval;
902 u16 resp = WLAN_STATUS_SUCCESS;
905 struct sta_info *sta;
907 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
908 sizeof(mgmt->u.assoc_req))) {
909 printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
910 "\n", reassoc, (unsigned long) len);
915 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
916 listen_interval = le_to_host16(
917 mgmt->u.reassoc_req.listen_interval);
918 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
919 " capab_info=0x%02x listen_interval=%d current_ap="
921 MAC2STR(mgmt->sa), capab_info, listen_interval,
922 MAC2STR(mgmt->u.reassoc_req.current_ap));
923 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
924 pos = mgmt->u.reassoc_req.variable;
926 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
927 listen_interval = le_to_host16(
928 mgmt->u.assoc_req.listen_interval);
929 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
930 " capab_info=0x%02x listen_interval=%d",
931 MAC2STR(mgmt->sa), capab_info, listen_interval);
932 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
933 pos = mgmt->u.assoc_req.variable;
936 sta = ap_get_sta(hapd, mgmt->sa);
937 #ifdef CONFIG_IEEE80211R
938 if (sta && sta->auth_alg == WLAN_AUTH_FT &&
939 (sta->flags & WLAN_STA_AUTH) == 0) {
940 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
941 "prior to authentication since it is using "
942 "over-the-DS FT", MAC2STR(mgmt->sa));
944 #endif /* CONFIG_IEEE80211R */
945 if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
946 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
947 HOSTAPD_LEVEL_INFO, "Station tried to "
948 "associate before authentication "
949 "(aid=%d flags=0x%x)",
951 sta ? sta->flags : 0);
952 send_deauth(hapd, mgmt->sa,
953 WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
957 if (hapd->tkip_countermeasures) {
958 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
962 if (listen_interval > hapd->conf->max_listen_interval) {
963 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
965 "Too large Listen Interval (%d)",
967 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
971 /* followed by SSID and Supported rates; and HT capabilities if 802.11n
973 resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
974 if (resp != WLAN_STATUS_SUCCESS)
977 if (hostapd_get_aid(hapd, sta) < 0) {
978 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
979 HOSTAPD_LEVEL_INFO, "No room for more AIDs");
980 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
984 sta->capability = capab_info;
985 sta->listen_interval = listen_interval;
987 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
988 sta->flags |= WLAN_STA_NONERP;
989 for (i = 0; i < sta->supported_rates_len; i++) {
990 if ((sta->supported_rates[i] & 0x7f) > 22) {
991 sta->flags &= ~WLAN_STA_NONERP;
995 if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
997 hapd->iface->num_sta_non_erp++;
998 if (hapd->iface->num_sta_non_erp == 1)
999 ieee802_11_set_beacons(hapd->iface);
1002 if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
1003 !sta->no_short_slot_time_set) {
1004 sta->no_short_slot_time_set = 1;
1005 hapd->iface->num_sta_no_short_slot_time++;
1006 if (hapd->iface->current_mode->mode ==
1007 HOSTAPD_MODE_IEEE80211G &&
1008 hapd->iface->num_sta_no_short_slot_time == 1)
1009 ieee802_11_set_beacons(hapd->iface);
1012 if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1013 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
1015 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
1017 if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
1018 !sta->no_short_preamble_set) {
1019 sta->no_short_preamble_set = 1;
1020 hapd->iface->num_sta_no_short_preamble++;
1021 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
1022 && hapd->iface->num_sta_no_short_preamble == 1)
1023 ieee802_11_set_beacons(hapd->iface);
1026 #ifdef CONFIG_IEEE80211N
1027 update_ht_state(hapd, sta);
1028 #endif /* CONFIG_IEEE80211N */
1030 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1031 HOSTAPD_LEVEL_DEBUG,
1032 "association OK (aid %d)", sta->aid);
1033 /* Station will be marked associated, after it acknowledges AssocResp
1036 #ifdef CONFIG_IEEE80211W
1037 if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
1038 wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
1039 "SA Query procedure", reassoc ? "re" : "");
1040 /* TODO: Send a protected Disassociate frame to the STA using
1041 * the old key and Reason Code "Previous Authentication no
1042 * longer valid". Make sure this is only sent protected since
1043 * unprotected frame would be received by the STA that is now
1044 * trying to associate.
1047 #endif /* CONFIG_IEEE80211W */
1050 os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
1054 if (sta->last_assoc_req)
1055 os_free(sta->last_assoc_req);
1056 sta->last_assoc_req = os_malloc(len);
1057 if (sta->last_assoc_req)
1058 os_memcpy(sta->last_assoc_req, mgmt, len);
1060 /* Make sure that the previously registered inactivity timer will not
1061 * remove the STA immediately. */
1062 sta->timeout_next = STA_NULLFUNC;
1065 send_assoc_resp(hapd, sta, resp, reassoc, pos, left);
1069 static void handle_disassoc(struct hostapd_data *hapd,
1070 const struct ieee80211_mgmt *mgmt, size_t len)
1072 struct sta_info *sta;
1074 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1075 printf("handle_disassoc - too short payload (len=%lu)\n",
1076 (unsigned long) len);
1080 wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1082 le_to_host16(mgmt->u.disassoc.reason_code));
1084 sta = ap_get_sta(hapd, mgmt->sa);
1086 printf("Station " MACSTR " trying to disassociate, but it "
1087 "is not associated.\n", MAC2STR(mgmt->sa));
1091 sta->flags &= ~WLAN_STA_ASSOC;
1092 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
1093 MAC2STR(sta->addr));
1094 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1095 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1096 HOSTAPD_LEVEL_INFO, "disassociated");
1097 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1098 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1099 /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1101 accounting_sta_stop(hapd, sta);
1102 ieee802_1x_free_station(sta);
1103 hapd->drv.sta_remove(hapd, sta->addr);
1105 if (sta->timeout_next == STA_NULLFUNC ||
1106 sta->timeout_next == STA_DISASSOC) {
1107 sta->timeout_next = STA_DEAUTH;
1108 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1109 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1113 mlme_disassociate_indication(
1114 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1118 static void handle_deauth(struct hostapd_data *hapd,
1119 const struct ieee80211_mgmt *mgmt, size_t len)
1121 struct sta_info *sta;
1123 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
1124 printf("handle_deauth - too short payload (len=%lu)\n",
1125 (unsigned long) len);
1129 wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
1132 le_to_host16(mgmt->u.deauth.reason_code));
1134 sta = ap_get_sta(hapd, mgmt->sa);
1136 printf("Station " MACSTR " trying to deauthenticate, but it "
1137 "is not authenticated.\n", MAC2STR(mgmt->sa));
1141 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1142 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
1143 MAC2STR(sta->addr));
1144 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1145 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1146 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1147 mlme_deauthenticate_indication(
1148 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1149 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1150 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1151 ap_free_sta(hapd, sta);
1155 static void handle_beacon(struct hostapd_data *hapd,
1156 const struct ieee80211_mgmt *mgmt, size_t len,
1157 struct hostapd_frame_info *fi)
1159 struct ieee802_11_elems elems;
1161 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1162 printf("handle_beacon - too short payload (len=%lu)\n",
1163 (unsigned long) len);
1167 (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
1168 len - (IEEE80211_HDRLEN +
1169 sizeof(mgmt->u.beacon)), &elems,
1172 ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1176 #ifdef CONFIG_IEEE80211W
1178 /* MLME-SAQuery.request */
1179 void ieee802_11_send_sa_query_req(struct hostapd_data *hapd,
1180 const u8 *addr, const u8 *trans_id)
1182 struct ieee80211_mgmt mgmt;
1185 wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Request to "
1186 MACSTR, MAC2STR(addr));
1187 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
1188 trans_id, WLAN_SA_QUERY_TR_ID_LEN);
1190 os_memset(&mgmt, 0, sizeof(mgmt));
1191 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1192 WLAN_FC_STYPE_ACTION);
1193 os_memcpy(mgmt.da, addr, ETH_ALEN);
1194 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
1195 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
1196 mgmt.u.action.category = WLAN_ACTION_SA_QUERY;
1197 mgmt.u.action.u.sa_query_req.action = WLAN_SA_QUERY_REQUEST;
1198 os_memcpy(mgmt.u.action.u.sa_query_req.trans_id, trans_id,
1199 WLAN_SA_QUERY_TR_ID_LEN);
1200 end = mgmt.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
1201 if (hapd->drv.send_mgmt_frame(hapd, &mgmt, end - (u8 *) &mgmt) < 0)
1202 perror("ieee802_11_send_sa_query_req: send");
1206 static void hostapd_sa_query_request(struct hostapd_data *hapd,
1207 const struct ieee80211_mgmt *mgmt)
1209 struct sta_info *sta;
1210 struct ieee80211_mgmt resp;
1213 wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Request from "
1214 MACSTR, MAC2STR(mgmt->sa));
1215 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
1216 mgmt->u.action.u.sa_query_resp.trans_id,
1217 WLAN_SA_QUERY_TR_ID_LEN);
1219 sta = ap_get_sta(hapd, mgmt->sa);
1220 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1221 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignore SA Query Request "
1222 "from unassociated STA " MACSTR, MAC2STR(mgmt->sa));
1226 wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Response to "
1227 MACSTR, MAC2STR(mgmt->sa));
1229 os_memset(&resp, 0, sizeof(resp));
1230 resp.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1231 WLAN_FC_STYPE_ACTION);
1232 os_memcpy(resp.da, mgmt->sa, ETH_ALEN);
1233 os_memcpy(resp.sa, hapd->own_addr, ETH_ALEN);
1234 os_memcpy(resp.bssid, hapd->own_addr, ETH_ALEN);
1235 resp.u.action.category = WLAN_ACTION_SA_QUERY;
1236 resp.u.action.u.sa_query_req.action = WLAN_SA_QUERY_RESPONSE;
1237 os_memcpy(resp.u.action.u.sa_query_req.trans_id,
1238 mgmt->u.action.u.sa_query_req.trans_id,
1239 WLAN_SA_QUERY_TR_ID_LEN);
1240 end = resp.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
1241 if (hapd->drv.send_mgmt_frame(hapd, &resp, end - (u8 *) &resp) < 0)
1242 perror("hostapd_sa_query_request: send");
1246 static void hostapd_sa_query_action(struct hostapd_data *hapd,
1247 const struct ieee80211_mgmt *mgmt,
1250 struct sta_info *sta;
1254 end = mgmt->u.action.u.sa_query_resp.trans_id +
1255 WLAN_SA_QUERY_TR_ID_LEN;
1256 if (((u8 *) mgmt) + len < end) {
1257 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
1258 "frame (len=%lu)", (unsigned long) len);
1262 if (mgmt->u.action.u.sa_query_resp.action == WLAN_SA_QUERY_REQUEST) {
1263 hostapd_sa_query_request(hapd, mgmt);
1267 if (mgmt->u.action.u.sa_query_resp.action != WLAN_SA_QUERY_RESPONSE) {
1268 wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected SA Query "
1269 "Action %d", mgmt->u.action.u.sa_query_resp.action);
1273 wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Response from "
1274 MACSTR, MAC2STR(mgmt->sa));
1275 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
1276 mgmt->u.action.u.sa_query_resp.trans_id,
1277 WLAN_SA_QUERY_TR_ID_LEN);
1279 /* MLME-SAQuery.confirm */
1281 sta = ap_get_sta(hapd, mgmt->sa);
1282 if (sta == NULL || sta->sa_query_trans_id == NULL) {
1283 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
1284 "pending SA Query request found");
1288 for (i = 0; i < sta->sa_query_count; i++) {
1289 if (os_memcmp(sta->sa_query_trans_id +
1290 i * WLAN_SA_QUERY_TR_ID_LEN,
1291 mgmt->u.action.u.sa_query_resp.trans_id,
1292 WLAN_SA_QUERY_TR_ID_LEN) == 0)
1296 if (i >= sta->sa_query_count) {
1297 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching SA Query "
1298 "transaction identifier found");
1302 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1303 HOSTAPD_LEVEL_DEBUG,
1304 "Reply to pending SA Query received");
1305 ap_sta_stop_sa_query(hapd, sta);
1309 static int robust_action_frame(u8 category)
1311 return category != WLAN_ACTION_PUBLIC &&
1312 category != WLAN_ACTION_HT;
1314 #endif /* CONFIG_IEEE80211W */
1317 static void handle_action(struct hostapd_data *hapd,
1318 const struct ieee80211_mgmt *mgmt, size_t len)
1320 struct sta_info *sta;
1322 if (len < IEEE80211_HDRLEN + 1) {
1323 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1324 HOSTAPD_LEVEL_DEBUG,
1325 "handle_action - too short payload (len=%lu)",
1326 (unsigned long) len);
1330 sta = ap_get_sta(hapd, mgmt->sa);
1331 #ifdef CONFIG_IEEE80211W
1332 if (sta && (sta->flags & WLAN_STA_MFP) &&
1333 !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP) &&
1334 robust_action_frame(mgmt->u.action.category))) {
1335 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1336 HOSTAPD_LEVEL_DEBUG,
1337 "Dropped unprotected Robust Action frame from "
1341 #endif /* CONFIG_IEEE80211W */
1343 switch (mgmt->u.action.category) {
1344 #ifdef CONFIG_IEEE80211R
1345 case WLAN_ACTION_FT:
1347 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1348 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
1349 "frame from unassociated STA " MACSTR,
1354 if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1355 len - IEEE80211_HDRLEN))
1360 #endif /* CONFIG_IEEE80211R */
1361 case WLAN_ACTION_WMM:
1362 hostapd_wmm_action(hapd, mgmt, len);
1364 #ifdef CONFIG_IEEE80211W
1365 case WLAN_ACTION_SA_QUERY:
1366 hostapd_sa_query_action(hapd, mgmt, len);
1368 #endif /* CONFIG_IEEE80211W */
1369 case WLAN_ACTION_PUBLIC:
1370 if (hapd->public_action_cb) {
1371 hapd->public_action_cb(hapd->public_action_cb_ctx,
1379 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1380 HOSTAPD_LEVEL_DEBUG,
1381 "handle_action - unknown action category %d or invalid "
1383 mgmt->u.action.category);
1384 if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1385 !(mgmt->sa[0] & 0x01)) {
1386 struct ieee80211_mgmt *resp;
1389 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1390 * Return the Action frame to the source without change
1391 * except that MSB of the Category set to 1.
1393 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1394 "frame back to sender");
1395 resp = os_malloc(len);
1398 os_memcpy(resp, mgmt, len);
1399 os_memcpy(resp->da, resp->sa, ETH_ALEN);
1400 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
1401 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
1402 resp->u.action.category |= 0x80;
1404 hapd->drv.send_mgmt_frame(hapd, resp, len);
1411 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1412 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1414 * @buf: management frame data (starting from IEEE 802.11 header)
1415 * @len: length of frame data in octets
1416 * @fi: meta data about received frame (signal level, etc.)
1418 * Process all incoming IEEE 802.11 management frames. This will be called for
1419 * each frame received from the kernel driver through wlan#ap interface. In
1420 * addition, it can be called to re-inserted pending frames (e.g., when using
1421 * external RADIUS server as an MAC ACL).
1423 void ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
1424 struct hostapd_frame_info *fi)
1426 struct ieee80211_mgmt *mgmt;
1430 mgmt = (struct ieee80211_mgmt *) buf;
1431 fc = le_to_host16(mgmt->frame_control);
1432 stype = WLAN_FC_GET_STYPE(fc);
1434 if (stype == WLAN_FC_STYPE_BEACON) {
1435 handle_beacon(hapd, mgmt, len, fi);
1439 broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
1440 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
1441 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
1444 os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
1445 printf("MGMT: BSSID=" MACSTR " not our address\n",
1446 MAC2STR(mgmt->bssid));
1451 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
1452 handle_probe_req(hapd, mgmt, len);
1456 if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
1457 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1458 HOSTAPD_LEVEL_DEBUG,
1459 "MGMT: DA=" MACSTR " not our address",
1465 case WLAN_FC_STYPE_AUTH:
1466 wpa_printf(MSG_DEBUG, "mgmt::auth");
1467 handle_auth(hapd, mgmt, len);
1469 case WLAN_FC_STYPE_ASSOC_REQ:
1470 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
1471 handle_assoc(hapd, mgmt, len, 0);
1473 case WLAN_FC_STYPE_REASSOC_REQ:
1474 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
1475 handle_assoc(hapd, mgmt, len, 1);
1477 case WLAN_FC_STYPE_DISASSOC:
1478 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
1479 handle_disassoc(hapd, mgmt, len);
1481 case WLAN_FC_STYPE_DEAUTH:
1482 wpa_printf(MSG_DEBUG, "mgmt::deauth");
1483 handle_deauth(hapd, mgmt, len);
1485 case WLAN_FC_STYPE_ACTION:
1486 wpa_printf(MSG_DEBUG, "mgmt::action");
1487 handle_action(hapd, mgmt, len);
1490 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1491 HOSTAPD_LEVEL_DEBUG,
1492 "unknown mgmt frame subtype %d", stype);
1498 static void handle_auth_cb(struct hostapd_data *hapd,
1499 const struct ieee80211_mgmt *mgmt,
1502 u16 auth_alg, auth_transaction, status_code;
1503 struct sta_info *sta;
1506 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1507 HOSTAPD_LEVEL_NOTICE,
1508 "did not acknowledge authentication response");
1512 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
1513 printf("handle_auth_cb - too short payload (len=%lu)\n",
1514 (unsigned long) len);
1518 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1519 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1520 status_code = le_to_host16(mgmt->u.auth.status_code);
1522 sta = ap_get_sta(hapd, mgmt->da);
1524 printf("handle_auth_cb: STA " MACSTR " not found\n",
1529 if (status_code == WLAN_STATUS_SUCCESS &&
1530 ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
1531 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
1532 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1533 HOSTAPD_LEVEL_INFO, "authenticated");
1534 sta->flags |= WLAN_STA_AUTH;
1539 static void handle_assoc_cb(struct hostapd_data *hapd,
1540 const struct ieee80211_mgmt *mgmt,
1541 size_t len, int reassoc, int ok)
1544 struct sta_info *sta;
1546 struct ieee80211_ht_capabilities ht_cap;
1549 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1550 HOSTAPD_LEVEL_DEBUG,
1551 "did not acknowledge association response");
1555 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
1556 sizeof(mgmt->u.assoc_resp))) {
1557 printf("handle_assoc_cb(reassoc=%d) - too short payload "
1558 "(len=%lu)\n", reassoc, (unsigned long) len);
1563 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
1565 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1567 sta = ap_get_sta(hapd, mgmt->da);
1569 printf("handle_assoc_cb: STA " MACSTR " not found\n",
1574 if (status != WLAN_STATUS_SUCCESS)
1577 /* Stop previous accounting session, if one is started, and allocate
1578 * new session id for the new session. */
1579 accounting_sta_stop(hapd, sta);
1581 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1583 "associated (aid %d)",
1586 if (sta->flags & WLAN_STA_ASSOC)
1588 sta->flags |= WLAN_STA_ASSOC;
1589 if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa) ||
1590 sta->auth_alg == WLAN_AUTH_FT) {
1592 * Open, static WEP, or FT protocol; no separate authorization
1595 sta->flags |= WLAN_STA_AUTHORIZED;
1596 wpa_msg(hapd->msg_ctx, MSG_INFO,
1597 AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
1601 mlme_reassociate_indication(hapd, sta);
1603 mlme_associate_indication(hapd, sta);
1605 #ifdef CONFIG_IEEE80211W
1606 sta->sa_query_timed_out = 0;
1607 #endif /* CONFIG_IEEE80211W */
1610 * Remove the STA entry in order to make sure the STA PS state gets
1611 * cleared and configuration gets updated in case of reassociation back
1614 hapd->drv.sta_remove(hapd, sta->addr);
1616 #ifdef CONFIG_IEEE80211N
1617 if (sta->flags & WLAN_STA_HT)
1618 hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
1619 #endif /* CONFIG_IEEE80211N */
1621 if (hapd->drv.sta_add(hapd, sta->addr, sta->aid, sta->capability,
1622 sta->supported_rates, sta->supported_rates_len,
1623 sta->listen_interval,
1624 sta->flags & WLAN_STA_HT ? &ht_cap : NULL)) {
1625 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1626 HOSTAPD_LEVEL_NOTICE,
1627 "Could not add STA to kernel driver");
1630 if (sta->eapol_sm == NULL) {
1632 * This STA does not use RADIUS server for EAP authentication,
1633 * so bind it to the selected VLAN interface now, since the
1634 * interface selection is not going to change anymore.
1636 if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
1638 } else if (sta->vlan_id) {
1639 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
1640 if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
1644 hapd->drv.set_sta_flags(hapd, sta);
1646 if (sta->auth_alg == WLAN_AUTH_FT)
1647 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
1649 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
1650 hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
1652 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
1655 /* Copy of the association request is not needed anymore */
1656 if (sta->last_assoc_req) {
1657 os_free(sta->last_assoc_req);
1658 sta->last_assoc_req = NULL;
1664 * ieee802_11_mgmt_cb - Process management frame TX status callback
1665 * @hapd: hostapd BSS data structure (the BSS from which the management frame
1667 * @buf: management frame data (starting from IEEE 802.11 header)
1668 * @len: length of frame data in octets
1669 * @stype: management frame subtype from frame control field
1670 * @ok: Whether the frame was ACK'ed
1672 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
1675 const struct ieee80211_mgmt *mgmt;
1676 mgmt = (const struct ieee80211_mgmt *) buf;
1679 case WLAN_FC_STYPE_AUTH:
1680 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
1681 handle_auth_cb(hapd, mgmt, len, ok);
1683 case WLAN_FC_STYPE_ASSOC_RESP:
1684 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
1685 handle_assoc_cb(hapd, mgmt, len, 0, ok);
1687 case WLAN_FC_STYPE_REASSOC_RESP:
1688 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
1689 handle_assoc_cb(hapd, mgmt, len, 1, ok);
1691 case WLAN_FC_STYPE_PROBE_RESP:
1692 wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb");
1694 case WLAN_FC_STYPE_DEAUTH:
1697 case WLAN_FC_STYPE_ACTION:
1698 wpa_printf(MSG_DEBUG, "mgmt::action cb");
1701 printf("unknown mgmt cb frame subtype %d\n", stype);
1707 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1714 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1715 char *buf, size_t buflen)
1722 void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
1723 const u8 *buf, size_t len, int ack)
1725 struct sta_info *sta;
1726 struct hostapd_iface *iface = hapd->iface;
1728 sta = ap_get_sta(hapd, addr);
1729 if (sta == NULL && iface->num_bss > 1) {
1731 for (j = 0; j < iface->num_bss; j++) {
1732 hapd = iface->bss[j];
1733 sta = ap_get_sta(hapd, addr);
1740 if (sta->flags & WLAN_STA_PENDING_POLL) {
1741 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
1742 "activity poll", MAC2STR(sta->addr),
1743 ack ? "ACKed" : "did not ACK");
1745 sta->flags &= ~WLAN_STA_PENDING_POLL;
1748 ieee802_1x_tx_status(hapd, sta, buf, len, ack);
1752 void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
1755 struct sta_info *sta;
1757 sta = ap_get_sta(hapd, src);
1758 if (sta && (sta->flags & WLAN_STA_ASSOC)) {
1759 if (wds && !(sta->flags & WLAN_STA_WDS)) {
1760 wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
1761 "STA " MACSTR " (aid %u)",
1762 MAC2STR(sta->addr), sta->aid);
1763 sta->flags |= WLAN_STA_WDS;
1764 hapd->drv.set_wds_sta(hapd, sta->addr, sta->aid, 1);
1769 wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
1770 MACSTR, MAC2STR(src));
1771 if (sta && (sta->flags & WLAN_STA_AUTH))
1772 hapd->drv.sta_disassoc(
1774 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1776 hapd->drv.sta_deauth(
1778 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1782 #endif /* CONFIG_NATIVE_WINDOWS */