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"
30 #include "ieee802_11_auth.h"
32 #include "ieee802_1x.h"
36 #include "accounting.h"
37 #include "ap_config.h"
39 #include "ieee802_11.h"
42 u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
47 if (hapd->iface->current_rates == NULL)
50 *pos++ = WLAN_EID_SUPP_RATES;
51 num = hapd->iface->num_rates;
53 /* rest of the rates are encoded in Extended supported
60 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
63 *pos = hapd->iface->current_rates[i].rate / 5;
64 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
73 u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
78 if (hapd->iface->current_rates == NULL)
81 num = hapd->iface->num_rates;
86 *pos++ = WLAN_EID_EXT_SUPP_RATES;
89 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
93 continue; /* already in SuppRates IE */
94 *pos = hapd->iface->current_rates[i].rate / 5;
95 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
104 u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
107 int capab = WLAN_CAPABILITY_ESS;
110 if (hapd->iface->num_sta_no_short_preamble == 0 &&
111 hapd->iconf->preamble == SHORT_PREAMBLE)
112 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
114 privacy = hapd->conf->ssid.wep.keys_set;
116 if (hapd->conf->ieee802_1x &&
117 (hapd->conf->default_wep_key_len ||
118 hapd->conf->individual_wep_key_len))
125 int policy, def_klen;
126 if (probe && sta->ssid_probe) {
127 policy = sta->ssid_probe->security_policy;
128 def_klen = sta->ssid_probe->wep.default_len;
130 policy = sta->ssid->security_policy;
131 def_klen = sta->ssid->wep.default_len;
133 privacy = policy != SECURITY_PLAINTEXT;
134 if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
139 capab |= WLAN_CAPABILITY_PRIVACY;
141 if (hapd->iface->current_mode &&
142 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
143 hapd->iface->num_sta_no_short_slot_time == 0)
144 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
150 #ifdef CONFIG_IEEE80211W
151 static u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
152 struct sta_info *sta, u8 *eid)
156 struct os_time now, passed;
158 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
160 *pos++ = WLAN_TIMEOUT_ASSOC_COMEBACK;
162 os_time_sub(&now, &sta->sa_query_start, &passed);
163 tu = (passed.sec * 1000000 + passed.usec) / 1024;
164 if (hapd->conf->assoc_sa_query_max_timeout > tu)
165 timeout = hapd->conf->assoc_sa_query_max_timeout - tu;
168 if (timeout < hapd->conf->assoc_sa_query_max_timeout)
169 timeout++; /* add some extra time for local timers */
170 WPA_PUT_LE32(pos, timeout);
175 #endif /* CONFIG_IEEE80211W */
178 void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
181 if (len > HOSTAPD_MAX_SSID_LEN)
182 len = HOSTAPD_MAX_SSID_LEN;
183 for (i = 0; i < len; i++) {
184 if (ssid[i] >= 32 && ssid[i] < 127)
194 * ieee802_11_send_deauth - Send Deauthentication frame
195 * @hapd: hostapd BSS data
196 * @addr: Address of the destination STA
197 * @reason: Reason code for Deauthentication
199 void ieee802_11_send_deauth(struct hostapd_data *hapd, const u8 *addr,
202 struct ieee80211_mgmt mgmt;
204 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
206 "deauthenticate - reason %d", reason);
207 os_memset(&mgmt, 0, sizeof(mgmt));
208 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
209 WLAN_FC_STYPE_DEAUTH);
210 os_memcpy(mgmt.da, addr, ETH_ALEN);
211 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
212 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
213 mgmt.u.deauth.reason_code = host_to_le16(reason);
214 if (hapd->drv.send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
215 sizeof(mgmt.u.deauth)) < 0)
216 perror("ieee802_11_send_deauth: send");
220 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
221 u16 auth_transaction, const u8 *challenge,
224 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
226 "authentication (shared key, transaction %d)",
229 if (auth_transaction == 1) {
230 if (!sta->challenge) {
231 /* Generate a pseudo-random challenge */
235 sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
236 if (sta->challenge == NULL)
237 return WLAN_STATUS_UNSPECIFIED_FAILURE;
241 os_memcpy(key, &now, 4);
242 os_memcpy(key + 4, &r, 4);
243 rc4_skip(key, sizeof(key), 0,
244 sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
249 if (auth_transaction != 3)
250 return WLAN_STATUS_UNSPECIFIED_FAILURE;
253 if (!iswep || !sta->challenge || !challenge ||
254 os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
255 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
257 "shared key authentication - invalid "
258 "challenge-response");
259 return WLAN_STATUS_CHALLENGE_FAIL;
262 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
264 "authentication OK (shared key)");
265 #ifdef IEEE80211_REQUIRE_AUTH_ACK
266 /* Station will be marked authenticated if it ACKs the
267 * authentication reply. */
269 sta->flags |= WLAN_STA_AUTH;
270 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
272 os_free(sta->challenge);
273 sta->challenge = NULL;
279 static void send_auth_reply(struct hostapd_data *hapd,
280 const u8 *dst, const u8 *bssid,
281 u16 auth_alg, u16 auth_transaction, u16 resp,
282 const u8 *ies, size_t ies_len)
284 struct ieee80211_mgmt *reply;
288 rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
289 buf = os_zalloc(rlen);
293 reply = (struct ieee80211_mgmt *) buf;
294 reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
296 os_memcpy(reply->da, dst, ETH_ALEN);
297 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
298 os_memcpy(reply->bssid, bssid, ETH_ALEN);
300 reply->u.auth.auth_alg = host_to_le16(auth_alg);
301 reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
302 reply->u.auth.status_code = host_to_le16(resp);
305 os_memcpy(reply->u.auth.variable, ies, ies_len);
307 wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
308 " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
309 MAC2STR(dst), auth_alg, auth_transaction,
310 resp, (unsigned long) ies_len);
311 if (hapd->drv.send_mgmt_frame(hapd, reply, rlen) < 0)
312 perror("send_auth_reply: send");
318 #ifdef CONFIG_IEEE80211R
319 static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
320 u16 auth_transaction, u16 status,
321 const u8 *ies, size_t ies_len)
323 struct hostapd_data *hapd = ctx;
324 struct sta_info *sta;
326 send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
327 status, ies, ies_len);
329 if (status != WLAN_STATUS_SUCCESS)
332 sta = ap_get_sta(hapd, dst);
336 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
337 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
338 sta->flags |= WLAN_STA_AUTH;
339 mlme_authenticate_indication(hapd, sta);
341 #endif /* CONFIG_IEEE80211R */
344 static void handle_auth(struct hostapd_data *hapd,
345 const struct ieee80211_mgmt *mgmt, size_t len)
347 u16 auth_alg, auth_transaction, status_code;
348 u16 resp = WLAN_STATUS_SUCCESS;
349 struct sta_info *sta = NULL;
352 const u8 *challenge = NULL;
353 u32 session_timeout, acct_interim_interval;
355 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
356 size_t resp_ies_len = 0;
358 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
359 printf("handle_auth - too short payload (len=%lu)\n",
360 (unsigned long) len);
364 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
365 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
366 status_code = le_to_host16(mgmt->u.auth.status_code);
367 fc = le_to_host16(mgmt->frame_control);
369 if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
370 2 + WLAN_AUTH_CHALLENGE_LEN &&
371 mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
372 mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
373 challenge = &mgmt->u.auth.variable[2];
375 wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
376 "auth_transaction=%d status_code=%d wep=%d%s",
377 MAC2STR(mgmt->sa), auth_alg, auth_transaction,
378 status_code, !!(fc & WLAN_FC_ISWEP),
379 challenge ? " challenge" : "");
381 if (hapd->tkip_countermeasures) {
382 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
386 if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
387 auth_alg == WLAN_AUTH_OPEN) ||
388 #ifdef CONFIG_IEEE80211R
390 (hapd->conf->wpa_key_mgmt &
391 (WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK)) &&
392 auth_alg == WLAN_AUTH_FT) ||
393 #endif /* CONFIG_IEEE80211R */
394 ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
395 auth_alg == WLAN_AUTH_SHARED_KEY))) {
396 printf("Unsupported authentication algorithm (%d)\n",
398 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
402 if (!(auth_transaction == 1 ||
403 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
404 printf("Unknown authentication transaction number (%d)\n",
406 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
410 if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
411 printf("Station " MACSTR " not allowed to authenticate.\n",
413 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
417 res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
419 &acct_interim_interval, &vlan_id);
420 if (res == HOSTAPD_ACL_REJECT) {
421 printf("Station " MACSTR " not allowed to authenticate.\n",
423 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
426 if (res == HOSTAPD_ACL_PENDING) {
427 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
428 " waiting for an external authentication",
430 /* Authentication code will re-send the authentication frame
431 * after it has received (and cached) information from the
432 * external source. */
436 sta = ap_sta_add(hapd, mgmt->sa);
438 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
443 if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
445 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
446 HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
447 "%d received from RADIUS server",
449 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
452 sta->vlan_id = vlan_id;
453 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
454 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
457 sta->flags &= ~WLAN_STA_PREAUTH;
458 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
460 if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
461 sta->acct_interim_interval = acct_interim_interval;
462 if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
463 ap_sta_session_timeout(hapd, sta, session_timeout);
465 ap_sta_no_session_timeout(hapd, sta);
469 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
471 "authentication OK (open system)");
472 #ifdef IEEE80211_REQUIRE_AUTH_ACK
473 /* Station will be marked authenticated if it ACKs the
474 * authentication reply. */
476 sta->flags |= WLAN_STA_AUTH;
477 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
478 sta->auth_alg = WLAN_AUTH_OPEN;
479 mlme_authenticate_indication(hapd, sta);
482 case WLAN_AUTH_SHARED_KEY:
483 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
485 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
486 mlme_authenticate_indication(hapd, sta);
487 if (sta->challenge && auth_transaction == 1) {
488 resp_ies[0] = WLAN_EID_CHALLENGE;
489 resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
490 os_memcpy(resp_ies + 2, sta->challenge,
491 WLAN_AUTH_CHALLENGE_LEN);
492 resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
495 #ifdef CONFIG_IEEE80211R
497 sta->auth_alg = WLAN_AUTH_FT;
498 if (sta->wpa_sm == NULL)
499 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
501 if (sta->wpa_sm == NULL) {
502 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
504 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
507 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
508 auth_transaction, mgmt->u.auth.variable,
509 len - IEEE80211_HDRLEN -
510 sizeof(mgmt->u.auth),
511 handle_auth_ft_finish, hapd);
512 /* handle_auth_ft_finish() callback will complete auth. */
514 #endif /* CONFIG_IEEE80211R */
518 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
519 auth_transaction + 1, resp, resp_ies, resp_ies_len);
523 static int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
527 /* get a unique AID */
529 wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
533 for (i = 0; i < AID_WORDS; i++) {
534 if (hapd->sta_aid[i] == (u32) -1)
536 for (j = 0; j < 32; j++) {
537 if (!(hapd->sta_aid[i] & BIT(j)))
545 aid = i * 32 + j + 1;
550 hapd->sta_aid[i] |= BIT(j);
551 wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
556 static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
557 const u8 *ssid_ie, size_t ssid_ie_len)
560 return WLAN_STATUS_UNSPECIFIED_FAILURE;
562 if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
563 os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
565 ieee802_11_print_ssid(ssid_txt, ssid_ie, ssid_ie_len);
566 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
568 "Station tried to associate with unknown SSID "
570 return WLAN_STATUS_UNSPECIFIED_FAILURE;
573 return WLAN_STATUS_SUCCESS;
577 static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
578 const u8 *wmm_ie, size_t wmm_ie_len)
580 sta->flags &= ~WLAN_STA_WMM;
581 if (wmm_ie && hapd->conf->wmm_enabled) {
582 if (hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len))
583 hostapd_logger(hapd, sta->addr,
586 "invalid WMM element in association "
589 sta->flags |= WLAN_STA_WMM;
591 return WLAN_STATUS_SUCCESS;
595 static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
596 struct ieee802_11_elems *elems)
598 if (!elems->supp_rates) {
599 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
601 "No supported rates element in AssocReq");
602 return WLAN_STATUS_UNSPECIFIED_FAILURE;
605 if (elems->supp_rates_len > sizeof(sta->supported_rates)) {
606 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
608 "Invalid supported rates element length %d",
609 elems->supp_rates_len);
610 return WLAN_STATUS_UNSPECIFIED_FAILURE;
613 os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
614 os_memcpy(sta->supported_rates, elems->supp_rates,
615 elems->supp_rates_len);
616 sta->supported_rates_len = elems->supp_rates_len;
618 if (elems->ext_supp_rates) {
619 if (elems->supp_rates_len + elems->ext_supp_rates_len >
620 sizeof(sta->supported_rates)) {
621 hostapd_logger(hapd, sta->addr,
622 HOSTAPD_MODULE_IEEE80211,
624 "Invalid supported rates element length"
625 " %d+%d", elems->supp_rates_len,
626 elems->ext_supp_rates_len);
627 return WLAN_STATUS_UNSPECIFIED_FAILURE;
630 os_memcpy(sta->supported_rates + elems->supp_rates_len,
631 elems->ext_supp_rates, elems->ext_supp_rates_len);
632 sta->supported_rates_len += elems->ext_supp_rates_len;
635 return WLAN_STATUS_SUCCESS;
639 static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
640 const u8 *ies, size_t ies_len, int reassoc)
642 struct ieee802_11_elems elems;
647 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
648 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
649 HOSTAPD_LEVEL_INFO, "Station sent an invalid "
650 "association request");
651 return WLAN_STATUS_UNSPECIFIED_FAILURE;
654 resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
655 if (resp != WLAN_STATUS_SUCCESS)
657 resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
658 if (resp != WLAN_STATUS_SUCCESS)
660 resp = copy_supp_rates(hapd, sta, &elems);
661 if (resp != WLAN_STATUS_SUCCESS)
663 #ifdef CONFIG_IEEE80211N
664 resp = copy_sta_ht_capab(sta, elems.ht_capabilities,
665 elems.ht_capabilities_len);
666 if (resp != WLAN_STATUS_SUCCESS)
668 #endif /* CONFIG_IEEE80211N */
670 if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
671 wpa_ie = elems.rsn_ie;
672 wpa_ie_len = elems.rsn_ie_len;
673 } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
675 wpa_ie = elems.wpa_ie;
676 wpa_ie_len = elems.wpa_ie_len;
683 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
684 if (hapd->conf->wps_state && elems.wps_ie) {
685 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
686 "Request - assume WPS is used");
687 sta->flags |= WLAN_STA_WPS;
688 wpabuf_free(sta->wps_ie);
689 sta->wps_ie = wpabuf_alloc_copy(elems.wps_ie + 4,
690 elems.wps_ie_len - 4);
693 } else if (hapd->conf->wps_state && wpa_ie == NULL) {
694 wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
695 "(Re)Association Request - possible WPS use");
696 sta->flags |= WLAN_STA_MAYBE_WPS;
698 #endif /* CONFIG_WPS */
699 if (hapd->conf->wpa && wpa_ie == NULL) {
700 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
702 "No WPA/RSN IE in association request");
703 return WLAN_STATUS_INVALID_IE;
706 if (hapd->conf->wpa && wpa_ie) {
710 if (sta->wpa_sm == NULL)
711 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
713 if (sta->wpa_sm == NULL) {
714 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
716 return WLAN_STATUS_UNSPECIFIED_FAILURE;
718 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
720 elems.mdie, elems.mdie_len);
721 if (res == WPA_INVALID_GROUP)
722 resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
723 else if (res == WPA_INVALID_PAIRWISE)
724 resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
725 else if (res == WPA_INVALID_AKMP)
726 resp = WLAN_STATUS_AKMP_NOT_VALID;
727 else if (res == WPA_ALLOC_FAIL)
728 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
729 #ifdef CONFIG_IEEE80211W
730 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
731 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
732 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
733 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
734 #endif /* CONFIG_IEEE80211W */
735 else if (res == WPA_INVALID_MDIE)
736 resp = WLAN_STATUS_INVALID_MDIE;
737 else if (res != WPA_IE_OK)
738 resp = WLAN_STATUS_INVALID_IE;
739 if (resp != WLAN_STATUS_SUCCESS)
741 #ifdef CONFIG_IEEE80211W
742 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
743 sta->sa_query_count > 0)
744 ap_check_sa_query_timeout(hapd, sta);
745 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
746 (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
748 * STA has already been associated with MFP and SA
749 * Query timeout has not been reached. Reject the
750 * association attempt temporarily and start SA Query,
751 * if one is not pending.
754 if (sta->sa_query_count == 0)
755 ap_sta_start_sa_query(hapd, sta);
757 return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
760 if (wpa_auth_uses_mfp(sta->wpa_sm))
761 sta->flags |= WLAN_STA_MFP;
763 sta->flags &= ~WLAN_STA_MFP;
764 #endif /* CONFIG_IEEE80211W */
766 #ifdef CONFIG_IEEE80211R
767 if (sta->auth_alg == WLAN_AUTH_FT) {
769 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
770 "to use association (not "
771 "re-association) with FT auth_alg",
773 return WLAN_STATUS_UNSPECIFIED_FAILURE;
776 resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
778 if (resp != WLAN_STATUS_SUCCESS)
781 #endif /* CONFIG_IEEE80211R */
783 #ifdef CONFIG_IEEE80211N
784 if ((sta->flags & WLAN_STA_HT) &&
785 wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
786 hostapd_logger(hapd, sta->addr,
787 HOSTAPD_MODULE_IEEE80211,
789 "Station tried to use TKIP with HT "
791 return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
793 #endif /* CONFIG_IEEE80211N */
795 wpa_auth_sta_no_wpa(sta->wpa_sm);
797 return WLAN_STATUS_SUCCESS;
801 static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
805 struct ieee80211_mgmt reply;
807 os_memset(&reply, 0, sizeof(reply));
808 reply.frame_control =
809 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
810 os_memcpy(reply.da, addr, ETH_ALEN);
811 os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
812 os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
814 send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
815 reply.u.deauth.reason_code = host_to_le16(reason_code);
817 if (hapd->drv.send_mgmt_frame(hapd, &reply, send_len) < 0)
818 wpa_printf(MSG_INFO, "Failed to send deauth: %s",
823 static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
824 u16 status_code, int reassoc, const u8 *ies,
828 u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
829 struct ieee80211_mgmt *reply;
832 os_memset(buf, 0, sizeof(buf));
833 reply = (struct ieee80211_mgmt *) buf;
834 reply->frame_control =
835 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
836 (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
837 WLAN_FC_STYPE_ASSOC_RESP));
838 os_memcpy(reply->da, sta->addr, ETH_ALEN);
839 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
840 os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
842 send_len = IEEE80211_HDRLEN;
843 send_len += sizeof(reply->u.assoc_resp);
844 reply->u.assoc_resp.capab_info =
845 host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
846 reply->u.assoc_resp.status_code = host_to_le16(status_code);
847 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
848 | BIT(14) | BIT(15));
849 /* Supported rates */
850 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
851 /* Extended supported rates */
852 p = hostapd_eid_ext_supp_rates(hapd, p);
854 #ifdef CONFIG_IEEE80211R
855 if (status_code == WLAN_STATUS_SUCCESS) {
856 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
857 * Transition Information, RSN, [RIC Response] */
858 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
859 buf + sizeof(buf) - p,
860 sta->auth_alg, ies, ies_len);
862 #endif /* CONFIG_IEEE80211R */
864 #ifdef CONFIG_IEEE80211W
865 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
866 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
867 #endif /* CONFIG_IEEE80211W */
869 #ifdef CONFIG_IEEE80211N
870 p = hostapd_eid_ht_capabilities(hapd, p);
871 p = hostapd_eid_ht_operation(hapd, p);
872 #endif /* CONFIG_IEEE80211N */
874 if (sta->flags & WLAN_STA_WMM)
875 p = hostapd_eid_wmm(hapd, p);
877 send_len += p - reply->u.assoc_resp.variable;
879 if (hapd->drv.send_mgmt_frame(hapd, reply, send_len) < 0)
880 wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
885 static void handle_assoc(struct hostapd_data *hapd,
886 const struct ieee80211_mgmt *mgmt, size_t len,
889 u16 capab_info, listen_interval;
890 u16 resp = WLAN_STATUS_SUCCESS;
893 struct sta_info *sta;
895 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
896 sizeof(mgmt->u.assoc_req))) {
897 printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
898 "\n", reassoc, (unsigned long) len);
903 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
904 listen_interval = le_to_host16(
905 mgmt->u.reassoc_req.listen_interval);
906 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
907 " capab_info=0x%02x listen_interval=%d current_ap="
909 MAC2STR(mgmt->sa), capab_info, listen_interval,
910 MAC2STR(mgmt->u.reassoc_req.current_ap));
911 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
912 pos = mgmt->u.reassoc_req.variable;
914 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
915 listen_interval = le_to_host16(
916 mgmt->u.assoc_req.listen_interval);
917 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
918 " capab_info=0x%02x listen_interval=%d",
919 MAC2STR(mgmt->sa), capab_info, listen_interval);
920 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
921 pos = mgmt->u.assoc_req.variable;
924 sta = ap_get_sta(hapd, mgmt->sa);
925 #ifdef CONFIG_IEEE80211R
926 if (sta && sta->auth_alg == WLAN_AUTH_FT &&
927 (sta->flags & WLAN_STA_AUTH) == 0) {
928 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
929 "prior to authentication since it is using "
930 "over-the-DS FT", MAC2STR(mgmt->sa));
932 #endif /* CONFIG_IEEE80211R */
933 if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
934 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
935 HOSTAPD_LEVEL_INFO, "Station tried to "
936 "associate before authentication "
937 "(aid=%d flags=0x%x)",
939 sta ? sta->flags : 0);
940 send_deauth(hapd, mgmt->sa,
941 WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
945 if (hapd->tkip_countermeasures) {
946 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
950 if (listen_interval > hapd->conf->max_listen_interval) {
951 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
953 "Too large Listen Interval (%d)",
955 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
959 /* followed by SSID and Supported rates; and HT capabilities if 802.11n
961 resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
962 if (resp != WLAN_STATUS_SUCCESS)
965 if (hostapd_get_aid(hapd, sta) < 0) {
966 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
967 HOSTAPD_LEVEL_INFO, "No room for more AIDs");
968 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
972 sta->capability = capab_info;
973 sta->listen_interval = listen_interval;
975 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
976 sta->flags |= WLAN_STA_NONERP;
977 for (i = 0; i < sta->supported_rates_len; i++) {
978 if ((sta->supported_rates[i] & 0x7f) > 22) {
979 sta->flags &= ~WLAN_STA_NONERP;
983 if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
985 hapd->iface->num_sta_non_erp++;
986 if (hapd->iface->num_sta_non_erp == 1)
987 ieee802_11_set_beacons(hapd->iface);
990 if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
991 !sta->no_short_slot_time_set) {
992 sta->no_short_slot_time_set = 1;
993 hapd->iface->num_sta_no_short_slot_time++;
994 if (hapd->iface->current_mode->mode ==
995 HOSTAPD_MODE_IEEE80211G &&
996 hapd->iface->num_sta_no_short_slot_time == 1)
997 ieee802_11_set_beacons(hapd->iface);
1000 if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1001 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
1003 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
1005 if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
1006 !sta->no_short_preamble_set) {
1007 sta->no_short_preamble_set = 1;
1008 hapd->iface->num_sta_no_short_preamble++;
1009 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
1010 && hapd->iface->num_sta_no_short_preamble == 1)
1011 ieee802_11_set_beacons(hapd->iface);
1014 #ifdef CONFIG_IEEE80211N
1015 update_ht_state(hapd, sta);
1016 #endif /* CONFIG_IEEE80211N */
1018 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1019 HOSTAPD_LEVEL_DEBUG,
1020 "association OK (aid %d)", sta->aid);
1021 /* Station will be marked associated, after it acknowledges AssocResp
1024 #ifdef CONFIG_IEEE80211W
1025 if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
1026 wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
1027 "SA Query procedure", reassoc ? "re" : "");
1028 /* TODO: Send a protected Disassociate frame to the STA using
1029 * the old key and Reason Code "Previous Authentication no
1030 * longer valid". Make sure this is only sent protected since
1031 * unprotected frame would be received by the STA that is now
1032 * trying to associate.
1035 #endif /* CONFIG_IEEE80211W */
1038 os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
1042 if (sta->last_assoc_req)
1043 os_free(sta->last_assoc_req);
1044 sta->last_assoc_req = os_malloc(len);
1045 if (sta->last_assoc_req)
1046 os_memcpy(sta->last_assoc_req, mgmt, len);
1048 /* Make sure that the previously registered inactivity timer will not
1049 * remove the STA immediately. */
1050 sta->timeout_next = STA_NULLFUNC;
1053 send_assoc_resp(hapd, sta, resp, reassoc, pos, left);
1057 static void handle_disassoc(struct hostapd_data *hapd,
1058 const struct ieee80211_mgmt *mgmt, size_t len)
1060 struct sta_info *sta;
1062 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1063 printf("handle_disassoc - too short payload (len=%lu)\n",
1064 (unsigned long) len);
1068 wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1070 le_to_host16(mgmt->u.disassoc.reason_code));
1072 sta = ap_get_sta(hapd, mgmt->sa);
1074 printf("Station " MACSTR " trying to disassociate, but it "
1075 "is not associated.\n", MAC2STR(mgmt->sa));
1079 sta->flags &= ~WLAN_STA_ASSOC;
1080 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
1081 MAC2STR(sta->addr));
1082 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1083 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1084 HOSTAPD_LEVEL_INFO, "disassociated");
1085 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1086 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1087 /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1089 accounting_sta_stop(hapd, sta);
1090 ieee802_1x_free_station(sta);
1091 hapd->drv.sta_remove(hapd, sta->addr);
1093 if (sta->timeout_next == STA_NULLFUNC ||
1094 sta->timeout_next == STA_DISASSOC) {
1095 sta->timeout_next = STA_DEAUTH;
1096 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1097 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1101 mlme_disassociate_indication(
1102 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1106 static void handle_deauth(struct hostapd_data *hapd,
1107 const struct ieee80211_mgmt *mgmt, size_t len)
1109 struct sta_info *sta;
1111 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
1112 printf("handle_deauth - too short payload (len=%lu)\n",
1113 (unsigned long) len);
1117 wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
1120 le_to_host16(mgmt->u.deauth.reason_code));
1122 sta = ap_get_sta(hapd, mgmt->sa);
1124 printf("Station " MACSTR " trying to deauthenticate, but it "
1125 "is not authenticated.\n", MAC2STR(mgmt->sa));
1129 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1130 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
1131 MAC2STR(sta->addr));
1132 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1133 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1134 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1135 mlme_deauthenticate_indication(
1136 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1137 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1138 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1139 ap_free_sta(hapd, sta);
1143 static void handle_beacon(struct hostapd_data *hapd,
1144 const struct ieee80211_mgmt *mgmt, size_t len,
1145 struct hostapd_frame_info *fi)
1147 struct ieee802_11_elems elems;
1149 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1150 printf("handle_beacon - too short payload (len=%lu)\n",
1151 (unsigned long) len);
1155 (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
1156 len - (IEEE80211_HDRLEN +
1157 sizeof(mgmt->u.beacon)), &elems,
1160 ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1164 #ifdef CONFIG_IEEE80211W
1166 /* MLME-SAQuery.request */
1167 void ieee802_11_send_sa_query_req(struct hostapd_data *hapd,
1168 const u8 *addr, const u8 *trans_id)
1170 struct ieee80211_mgmt mgmt;
1173 wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Request to "
1174 MACSTR, MAC2STR(addr));
1175 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
1176 trans_id, WLAN_SA_QUERY_TR_ID_LEN);
1178 os_memset(&mgmt, 0, sizeof(mgmt));
1179 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1180 WLAN_FC_STYPE_ACTION);
1181 os_memcpy(mgmt.da, addr, ETH_ALEN);
1182 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
1183 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
1184 mgmt.u.action.category = WLAN_ACTION_SA_QUERY;
1185 mgmt.u.action.u.sa_query_req.action = WLAN_SA_QUERY_REQUEST;
1186 os_memcpy(mgmt.u.action.u.sa_query_req.trans_id, trans_id,
1187 WLAN_SA_QUERY_TR_ID_LEN);
1188 end = mgmt.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
1189 if (hapd->drv.send_mgmt_frame(hapd, &mgmt, end - (u8 *) &mgmt) < 0)
1190 perror("ieee802_11_send_sa_query_req: send");
1194 static void hostapd_sa_query_request(struct hostapd_data *hapd,
1195 const struct ieee80211_mgmt *mgmt)
1197 struct sta_info *sta;
1198 struct ieee80211_mgmt resp;
1201 wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Request from "
1202 MACSTR, MAC2STR(mgmt->sa));
1203 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
1204 mgmt->u.action.u.sa_query_resp.trans_id,
1205 WLAN_SA_QUERY_TR_ID_LEN);
1207 sta = ap_get_sta(hapd, mgmt->sa);
1208 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1209 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignore SA Query Request "
1210 "from unassociated STA " MACSTR, MAC2STR(mgmt->sa));
1214 wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Response to "
1215 MACSTR, MAC2STR(mgmt->sa));
1217 os_memset(&resp, 0, sizeof(resp));
1218 resp.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1219 WLAN_FC_STYPE_ACTION);
1220 os_memcpy(resp.da, mgmt->sa, ETH_ALEN);
1221 os_memcpy(resp.sa, hapd->own_addr, ETH_ALEN);
1222 os_memcpy(resp.bssid, hapd->own_addr, ETH_ALEN);
1223 resp.u.action.category = WLAN_ACTION_SA_QUERY;
1224 resp.u.action.u.sa_query_req.action = WLAN_SA_QUERY_RESPONSE;
1225 os_memcpy(resp.u.action.u.sa_query_req.trans_id,
1226 mgmt->u.action.u.sa_query_req.trans_id,
1227 WLAN_SA_QUERY_TR_ID_LEN);
1228 end = resp.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
1229 if (hapd->drv.send_mgmt_frame(hapd, &resp, end - (u8 *) &resp) < 0)
1230 perror("hostapd_sa_query_request: send");
1234 static void hostapd_sa_query_action(struct hostapd_data *hapd,
1235 const struct ieee80211_mgmt *mgmt,
1238 struct sta_info *sta;
1242 end = mgmt->u.action.u.sa_query_resp.trans_id +
1243 WLAN_SA_QUERY_TR_ID_LEN;
1244 if (((u8 *) mgmt) + len < end) {
1245 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
1246 "frame (len=%lu)", (unsigned long) len);
1250 if (mgmt->u.action.u.sa_query_resp.action == WLAN_SA_QUERY_REQUEST) {
1251 hostapd_sa_query_request(hapd, mgmt);
1255 if (mgmt->u.action.u.sa_query_resp.action != WLAN_SA_QUERY_RESPONSE) {
1256 wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected SA Query "
1257 "Action %d", mgmt->u.action.u.sa_query_resp.action);
1261 wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Response from "
1262 MACSTR, MAC2STR(mgmt->sa));
1263 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
1264 mgmt->u.action.u.sa_query_resp.trans_id,
1265 WLAN_SA_QUERY_TR_ID_LEN);
1267 /* MLME-SAQuery.confirm */
1269 sta = ap_get_sta(hapd, mgmt->sa);
1270 if (sta == NULL || sta->sa_query_trans_id == NULL) {
1271 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
1272 "pending SA Query request found");
1276 for (i = 0; i < sta->sa_query_count; i++) {
1277 if (os_memcmp(sta->sa_query_trans_id +
1278 i * WLAN_SA_QUERY_TR_ID_LEN,
1279 mgmt->u.action.u.sa_query_resp.trans_id,
1280 WLAN_SA_QUERY_TR_ID_LEN) == 0)
1284 if (i >= sta->sa_query_count) {
1285 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching SA Query "
1286 "transaction identifier found");
1290 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1291 HOSTAPD_LEVEL_DEBUG,
1292 "Reply to pending SA Query received");
1293 ap_sta_stop_sa_query(hapd, sta);
1297 static int robust_action_frame(u8 category)
1299 return category != WLAN_ACTION_PUBLIC &&
1300 category != WLAN_ACTION_HT;
1302 #endif /* CONFIG_IEEE80211W */
1305 static void handle_action(struct hostapd_data *hapd,
1306 const struct ieee80211_mgmt *mgmt, size_t len)
1308 struct sta_info *sta;
1310 if (len < IEEE80211_HDRLEN + 1) {
1311 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1312 HOSTAPD_LEVEL_DEBUG,
1313 "handle_action - too short payload (len=%lu)",
1314 (unsigned long) len);
1318 sta = ap_get_sta(hapd, mgmt->sa);
1319 #ifdef CONFIG_IEEE80211W
1320 if (sta && (sta->flags & WLAN_STA_MFP) &&
1321 !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP) &&
1322 robust_action_frame(mgmt->u.action.category))) {
1323 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1324 HOSTAPD_LEVEL_DEBUG,
1325 "Dropped unprotected Robust Action frame from "
1329 #endif /* CONFIG_IEEE80211W */
1331 switch (mgmt->u.action.category) {
1332 #ifdef CONFIG_IEEE80211R
1333 case WLAN_ACTION_FT:
1335 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1336 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
1337 "frame from unassociated STA " MACSTR,
1342 if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1343 len - IEEE80211_HDRLEN))
1348 #endif /* CONFIG_IEEE80211R */
1349 case WLAN_ACTION_WMM:
1350 hostapd_wmm_action(hapd, mgmt, len);
1352 #ifdef CONFIG_IEEE80211W
1353 case WLAN_ACTION_SA_QUERY:
1354 hostapd_sa_query_action(hapd, mgmt, len);
1356 #endif /* CONFIG_IEEE80211W */
1357 case WLAN_ACTION_PUBLIC:
1358 if (hapd->public_action_cb) {
1359 hapd->public_action_cb(hapd->public_action_cb_ctx,
1367 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1368 HOSTAPD_LEVEL_DEBUG,
1369 "handle_action - unknown action category %d or invalid "
1371 mgmt->u.action.category);
1372 if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1373 !(mgmt->sa[0] & 0x01)) {
1374 struct ieee80211_mgmt *resp;
1377 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1378 * Return the Action frame to the source without change
1379 * except that MSB of the Category set to 1.
1381 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1382 "frame back to sender");
1383 resp = os_malloc(len);
1386 os_memcpy(resp, mgmt, len);
1387 os_memcpy(resp->da, resp->sa, ETH_ALEN);
1388 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
1389 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
1390 resp->u.action.category |= 0x80;
1392 hapd->drv.send_mgmt_frame(hapd, resp, len);
1399 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1400 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1402 * @buf: management frame data (starting from IEEE 802.11 header)
1403 * @len: length of frame data in octets
1404 * @fi: meta data about received frame (signal level, etc.)
1406 * Process all incoming IEEE 802.11 management frames. This will be called for
1407 * each frame received from the kernel driver through wlan#ap interface. In
1408 * addition, it can be called to re-inserted pending frames (e.g., when using
1409 * external RADIUS server as an MAC ACL).
1411 void ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
1412 struct hostapd_frame_info *fi)
1414 struct ieee80211_mgmt *mgmt;
1418 mgmt = (struct ieee80211_mgmt *) buf;
1419 fc = le_to_host16(mgmt->frame_control);
1420 stype = WLAN_FC_GET_STYPE(fc);
1422 if (stype == WLAN_FC_STYPE_BEACON) {
1423 handle_beacon(hapd, mgmt, len, fi);
1427 broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
1428 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
1429 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
1432 os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
1433 printf("MGMT: BSSID=" MACSTR " not our address\n",
1434 MAC2STR(mgmt->bssid));
1439 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
1440 handle_probe_req(hapd, mgmt, len);
1444 if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
1445 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1446 HOSTAPD_LEVEL_DEBUG,
1447 "MGMT: DA=" MACSTR " not our address",
1453 case WLAN_FC_STYPE_AUTH:
1454 wpa_printf(MSG_DEBUG, "mgmt::auth");
1455 handle_auth(hapd, mgmt, len);
1457 case WLAN_FC_STYPE_ASSOC_REQ:
1458 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
1459 handle_assoc(hapd, mgmt, len, 0);
1461 case WLAN_FC_STYPE_REASSOC_REQ:
1462 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
1463 handle_assoc(hapd, mgmt, len, 1);
1465 case WLAN_FC_STYPE_DISASSOC:
1466 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
1467 handle_disassoc(hapd, mgmt, len);
1469 case WLAN_FC_STYPE_DEAUTH:
1470 wpa_printf(MSG_DEBUG, "mgmt::deauth");
1471 handle_deauth(hapd, mgmt, len);
1473 case WLAN_FC_STYPE_ACTION:
1474 wpa_printf(MSG_DEBUG, "mgmt::action");
1475 handle_action(hapd, mgmt, len);
1478 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1479 HOSTAPD_LEVEL_DEBUG,
1480 "unknown mgmt frame subtype %d", stype);
1486 static void handle_auth_cb(struct hostapd_data *hapd,
1487 const struct ieee80211_mgmt *mgmt,
1490 u16 auth_alg, auth_transaction, status_code;
1491 struct sta_info *sta;
1494 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1495 HOSTAPD_LEVEL_NOTICE,
1496 "did not acknowledge authentication response");
1500 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
1501 printf("handle_auth_cb - too short payload (len=%lu)\n",
1502 (unsigned long) len);
1506 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1507 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1508 status_code = le_to_host16(mgmt->u.auth.status_code);
1510 sta = ap_get_sta(hapd, mgmt->da);
1512 printf("handle_auth_cb: STA " MACSTR " not found\n",
1517 if (status_code == WLAN_STATUS_SUCCESS &&
1518 ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
1519 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
1520 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1521 HOSTAPD_LEVEL_INFO, "authenticated");
1522 sta->flags |= WLAN_STA_AUTH;
1527 static void handle_assoc_cb(struct hostapd_data *hapd,
1528 const struct ieee80211_mgmt *mgmt,
1529 size_t len, int reassoc, int ok)
1532 struct sta_info *sta;
1534 struct ieee80211_ht_capabilities ht_cap;
1537 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1538 HOSTAPD_LEVEL_DEBUG,
1539 "did not acknowledge association response");
1543 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
1544 sizeof(mgmt->u.assoc_resp))) {
1545 printf("handle_assoc_cb(reassoc=%d) - too short payload "
1546 "(len=%lu)\n", reassoc, (unsigned long) len);
1551 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
1553 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1555 sta = ap_get_sta(hapd, mgmt->da);
1557 printf("handle_assoc_cb: STA " MACSTR " not found\n",
1562 if (status != WLAN_STATUS_SUCCESS)
1565 /* Stop previous accounting session, if one is started, and allocate
1566 * new session id for the new session. */
1567 accounting_sta_stop(hapd, sta);
1569 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1571 "associated (aid %d)",
1574 if (sta->flags & WLAN_STA_ASSOC)
1576 sta->flags |= WLAN_STA_ASSOC;
1577 if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa) ||
1578 sta->auth_alg == WLAN_AUTH_FT) {
1580 * Open, static WEP, or FT protocol; no separate authorization
1583 sta->flags |= WLAN_STA_AUTHORIZED;
1584 wpa_msg(hapd->msg_ctx, MSG_INFO,
1585 AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
1589 mlme_reassociate_indication(hapd, sta);
1591 mlme_associate_indication(hapd, sta);
1593 #ifdef CONFIG_IEEE80211W
1594 sta->sa_query_timed_out = 0;
1595 #endif /* CONFIG_IEEE80211W */
1598 * Remove the STA entry in order to make sure the STA PS state gets
1599 * cleared and configuration gets updated in case of reassociation back
1602 hapd->drv.sta_remove(hapd, sta->addr);
1604 #ifdef CONFIG_IEEE80211N
1605 if (sta->flags & WLAN_STA_HT)
1606 hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
1607 #endif /* CONFIG_IEEE80211N */
1609 if (hapd->drv.sta_add(hapd, sta->addr, sta->aid, sta->capability,
1610 sta->supported_rates, sta->supported_rates_len,
1611 sta->listen_interval,
1612 sta->flags & WLAN_STA_HT ? &ht_cap : NULL)) {
1613 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1614 HOSTAPD_LEVEL_NOTICE,
1615 "Could not add STA to kernel driver");
1618 if (sta->eapol_sm == NULL) {
1620 * This STA does not use RADIUS server for EAP authentication,
1621 * so bind it to the selected VLAN interface now, since the
1622 * interface selection is not going to change anymore.
1624 if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
1626 } else if (sta->vlan_id) {
1627 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
1628 if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
1632 hapd->drv.set_sta_flags(hapd, sta);
1634 if (sta->auth_alg == WLAN_AUTH_FT)
1635 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
1637 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
1638 hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
1640 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
1643 /* Copy of the association request is not needed anymore */
1644 if (sta->last_assoc_req) {
1645 os_free(sta->last_assoc_req);
1646 sta->last_assoc_req = NULL;
1652 * ieee802_11_mgmt_cb - Process management frame TX status callback
1653 * @hapd: hostapd BSS data structure (the BSS from which the management frame
1655 * @buf: management frame data (starting from IEEE 802.11 header)
1656 * @len: length of frame data in octets
1657 * @stype: management frame subtype from frame control field
1658 * @ok: Whether the frame was ACK'ed
1660 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
1663 const struct ieee80211_mgmt *mgmt;
1664 mgmt = (const struct ieee80211_mgmt *) buf;
1667 case WLAN_FC_STYPE_AUTH:
1668 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
1669 handle_auth_cb(hapd, mgmt, len, ok);
1671 case WLAN_FC_STYPE_ASSOC_RESP:
1672 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
1673 handle_assoc_cb(hapd, mgmt, len, 0, ok);
1675 case WLAN_FC_STYPE_REASSOC_RESP:
1676 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
1677 handle_assoc_cb(hapd, mgmt, len, 1, ok);
1679 case WLAN_FC_STYPE_PROBE_RESP:
1680 wpa_printf(MSG_DEBUG, "mgmt::proberesp cb");
1682 case WLAN_FC_STYPE_DEAUTH:
1685 case WLAN_FC_STYPE_ACTION:
1686 wpa_printf(MSG_DEBUG, "mgmt::action cb");
1689 printf("unknown mgmt cb frame subtype %d\n", stype);
1695 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1702 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1703 char *buf, size_t buflen)
1710 void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
1711 const u8 *buf, size_t len, int ack)
1713 struct sta_info *sta;
1714 struct hostapd_iface *iface = hapd->iface;
1716 sta = ap_get_sta(hapd, addr);
1717 if (sta == NULL && iface->num_bss > 1) {
1719 for (j = 0; j < iface->num_bss; j++) {
1720 hapd = iface->bss[j];
1721 sta = ap_get_sta(hapd, addr);
1728 if (sta->flags & WLAN_STA_PENDING_POLL) {
1729 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
1730 "activity poll", MAC2STR(sta->addr),
1731 ack ? "ACKed" : "did not ACK");
1733 sta->flags &= ~WLAN_STA_PENDING_POLL;
1736 ieee802_1x_tx_status(hapd, sta, buf, len, ack);
1740 void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
1743 struct sta_info *sta;
1745 sta = ap_get_sta(hapd, src);
1746 if (sta && (sta->flags & WLAN_STA_ASSOC)) {
1747 if (wds && !(sta->flags & WLAN_STA_WDS)) {
1748 wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
1749 "STA " MACSTR " (aid %u)",
1750 MAC2STR(sta->addr), sta->aid);
1751 sta->flags |= WLAN_STA_WDS;
1752 hapd->drv.set_wds_sta(hapd, sta->addr, sta->aid, 1);
1757 wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
1758 MACSTR, MAC2STR(src));
1759 if (sta && (sta->flags & WLAN_STA_AUTH))
1760 hapd->drv.sta_disassoc(
1762 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1764 hapd->drv.sta_deauth(
1766 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1770 #endif /* CONFIG_NATIVE_WINDOWS */