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"
32 #include "ieee802_11_auth.h"
34 #include "ieee802_1x.h"
38 #include "accounting.h"
39 #include "ap_config.h"
41 #include "p2p_hostapd.h"
42 #include "ap_drv_ops.h"
43 #include "ieee802_11.h"
46 u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
51 if (hapd->iface->current_rates == NULL)
54 *pos++ = WLAN_EID_SUPP_RATES;
55 num = hapd->iface->num_rates;
57 /* rest of the rates are encoded in Extended supported
64 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
67 *pos = hapd->iface->current_rates[i].rate / 5;
68 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
77 u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
82 if (hapd->iface->current_rates == NULL)
85 num = hapd->iface->num_rates;
90 *pos++ = WLAN_EID_EXT_SUPP_RATES;
93 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
97 continue; /* already in SuppRates IE */
98 *pos = hapd->iface->current_rates[i].rate / 5;
99 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
108 u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
111 int capab = WLAN_CAPABILITY_ESS;
114 if (hapd->iface->num_sta_no_short_preamble == 0 &&
115 hapd->iconf->preamble == SHORT_PREAMBLE)
116 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
118 privacy = hapd->conf->ssid.wep.keys_set;
120 if (hapd->conf->ieee802_1x &&
121 (hapd->conf->default_wep_key_len ||
122 hapd->conf->individual_wep_key_len))
129 int policy, def_klen;
130 if (probe && sta->ssid_probe) {
131 policy = sta->ssid_probe->security_policy;
132 def_klen = sta->ssid_probe->wep.default_len;
134 policy = sta->ssid->security_policy;
135 def_klen = sta->ssid->wep.default_len;
137 privacy = policy != SECURITY_PLAINTEXT;
138 if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
143 capab |= WLAN_CAPABILITY_PRIVACY;
145 if (hapd->iface->current_mode &&
146 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
147 hapd->iface->num_sta_no_short_slot_time == 0)
148 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
154 #ifdef CONFIG_IEEE80211W
155 static u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
156 struct sta_info *sta, u8 *eid)
160 struct os_time now, passed;
162 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
164 *pos++ = WLAN_TIMEOUT_ASSOC_COMEBACK;
166 os_time_sub(&now, &sta->sa_query_start, &passed);
167 tu = (passed.sec * 1000000 + passed.usec) / 1024;
168 if (hapd->conf->assoc_sa_query_max_timeout > tu)
169 timeout = hapd->conf->assoc_sa_query_max_timeout - tu;
172 if (timeout < hapd->conf->assoc_sa_query_max_timeout)
173 timeout++; /* add some extra time for local timers */
174 WPA_PUT_LE32(pos, timeout);
179 #endif /* CONFIG_IEEE80211W */
182 void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
185 if (len > HOSTAPD_MAX_SSID_LEN)
186 len = HOSTAPD_MAX_SSID_LEN;
187 for (i = 0; i < len; i++) {
188 if (ssid[i] >= 32 && ssid[i] < 127)
197 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
198 u16 auth_transaction, const u8 *challenge,
201 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
203 "authentication (shared key, transaction %d)",
206 if (auth_transaction == 1) {
207 if (!sta->challenge) {
208 /* Generate a pseudo-random challenge */
212 sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
213 if (sta->challenge == NULL)
214 return WLAN_STATUS_UNSPECIFIED_FAILURE;
218 os_memcpy(key, &now, 4);
219 os_memcpy(key + 4, &r, 4);
220 rc4_skip(key, sizeof(key), 0,
221 sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
226 if (auth_transaction != 3)
227 return WLAN_STATUS_UNSPECIFIED_FAILURE;
230 if (!iswep || !sta->challenge || !challenge ||
231 os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
232 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
234 "shared key authentication - invalid "
235 "challenge-response");
236 return WLAN_STATUS_CHALLENGE_FAIL;
239 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
241 "authentication OK (shared key)");
242 #ifdef IEEE80211_REQUIRE_AUTH_ACK
243 /* Station will be marked authenticated if it ACKs the
244 * authentication reply. */
246 sta->flags |= WLAN_STA_AUTH;
247 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
249 os_free(sta->challenge);
250 sta->challenge = NULL;
256 static void send_auth_reply(struct hostapd_data *hapd,
257 const u8 *dst, const u8 *bssid,
258 u16 auth_alg, u16 auth_transaction, u16 resp,
259 const u8 *ies, size_t ies_len)
261 struct ieee80211_mgmt *reply;
265 rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
266 buf = os_zalloc(rlen);
270 reply = (struct ieee80211_mgmt *) buf;
271 reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
273 os_memcpy(reply->da, dst, ETH_ALEN);
274 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
275 os_memcpy(reply->bssid, bssid, ETH_ALEN);
277 reply->u.auth.auth_alg = host_to_le16(auth_alg);
278 reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
279 reply->u.auth.status_code = host_to_le16(resp);
282 os_memcpy(reply->u.auth.variable, ies, ies_len);
284 wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
285 " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
286 MAC2STR(dst), auth_alg, auth_transaction,
287 resp, (unsigned long) ies_len);
288 if (hostapd_drv_send_mlme(hapd, reply, rlen) < 0)
289 perror("send_auth_reply: send");
295 #ifdef CONFIG_IEEE80211R
296 static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
297 u16 auth_transaction, u16 status,
298 const u8 *ies, size_t ies_len)
300 struct hostapd_data *hapd = ctx;
301 struct sta_info *sta;
303 send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
304 status, ies, ies_len);
306 if (status != WLAN_STATUS_SUCCESS)
309 sta = ap_get_sta(hapd, dst);
313 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
314 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
315 sta->flags |= WLAN_STA_AUTH;
316 mlme_authenticate_indication(hapd, sta);
318 #endif /* CONFIG_IEEE80211R */
321 static void handle_auth(struct hostapd_data *hapd,
322 const struct ieee80211_mgmt *mgmt, size_t len)
324 u16 auth_alg, auth_transaction, status_code;
325 u16 resp = WLAN_STATUS_SUCCESS;
326 struct sta_info *sta = NULL;
329 const u8 *challenge = NULL;
330 u32 session_timeout, acct_interim_interval;
332 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
333 size_t resp_ies_len = 0;
335 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
336 printf("handle_auth - too short payload (len=%lu)\n",
337 (unsigned long) len);
341 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
342 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
343 status_code = le_to_host16(mgmt->u.auth.status_code);
344 fc = le_to_host16(mgmt->frame_control);
346 if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
347 2 + WLAN_AUTH_CHALLENGE_LEN &&
348 mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
349 mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
350 challenge = &mgmt->u.auth.variable[2];
352 wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
353 "auth_transaction=%d status_code=%d wep=%d%s",
354 MAC2STR(mgmt->sa), auth_alg, auth_transaction,
355 status_code, !!(fc & WLAN_FC_ISWEP),
356 challenge ? " challenge" : "");
358 if (hapd->tkip_countermeasures) {
359 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
363 if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
364 auth_alg == WLAN_AUTH_OPEN) ||
365 #ifdef CONFIG_IEEE80211R
367 (hapd->conf->wpa_key_mgmt &
368 (WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK)) &&
369 auth_alg == WLAN_AUTH_FT) ||
370 #endif /* CONFIG_IEEE80211R */
371 ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
372 auth_alg == WLAN_AUTH_SHARED_KEY))) {
373 printf("Unsupported authentication algorithm (%d)\n",
375 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
379 if (!(auth_transaction == 1 ||
380 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
381 printf("Unknown authentication transaction number (%d)\n",
383 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
387 if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
388 printf("Station " MACSTR " not allowed to authenticate.\n",
390 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
394 res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
396 &acct_interim_interval, &vlan_id);
397 if (res == HOSTAPD_ACL_REJECT) {
398 printf("Station " MACSTR " not allowed to authenticate.\n",
400 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
403 if (res == HOSTAPD_ACL_PENDING) {
404 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
405 " waiting for an external authentication",
407 /* Authentication code will re-send the authentication frame
408 * after it has received (and cached) information from the
409 * external source. */
413 sta = ap_sta_add(hapd, mgmt->sa);
415 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
420 if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
422 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
423 HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
424 "%d received from RADIUS server",
426 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
429 sta->vlan_id = vlan_id;
430 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
431 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
434 sta->flags &= ~WLAN_STA_PREAUTH;
435 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
437 if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
438 sta->acct_interim_interval = acct_interim_interval;
439 if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
440 ap_sta_session_timeout(hapd, sta, session_timeout);
442 ap_sta_no_session_timeout(hapd, sta);
446 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
448 "authentication OK (open system)");
449 #ifdef IEEE80211_REQUIRE_AUTH_ACK
450 /* Station will be marked authenticated if it ACKs the
451 * authentication reply. */
453 sta->flags |= WLAN_STA_AUTH;
454 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
455 sta->auth_alg = WLAN_AUTH_OPEN;
456 mlme_authenticate_indication(hapd, sta);
459 case WLAN_AUTH_SHARED_KEY:
460 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
462 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
463 mlme_authenticate_indication(hapd, sta);
464 if (sta->challenge && auth_transaction == 1) {
465 resp_ies[0] = WLAN_EID_CHALLENGE;
466 resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
467 os_memcpy(resp_ies + 2, sta->challenge,
468 WLAN_AUTH_CHALLENGE_LEN);
469 resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
472 #ifdef CONFIG_IEEE80211R
474 sta->auth_alg = WLAN_AUTH_FT;
475 if (sta->wpa_sm == NULL)
476 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
478 if (sta->wpa_sm == NULL) {
479 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
481 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
484 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
485 auth_transaction, mgmt->u.auth.variable,
486 len - IEEE80211_HDRLEN -
487 sizeof(mgmt->u.auth),
488 handle_auth_ft_finish, hapd);
489 /* handle_auth_ft_finish() callback will complete auth. */
491 #endif /* CONFIG_IEEE80211R */
495 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
496 auth_transaction + 1, resp, resp_ies, resp_ies_len);
500 static int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
504 /* get a unique AID */
506 wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
510 for (i = 0; i < AID_WORDS; i++) {
511 if (hapd->sta_aid[i] == (u32) -1)
513 for (j = 0; j < 32; j++) {
514 if (!(hapd->sta_aid[i] & BIT(j)))
522 aid = i * 32 + j + 1;
527 hapd->sta_aid[i] |= BIT(j);
528 wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
533 static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
534 const u8 *ssid_ie, size_t ssid_ie_len)
537 return WLAN_STATUS_UNSPECIFIED_FAILURE;
539 if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
540 os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
542 ieee802_11_print_ssid(ssid_txt, ssid_ie, ssid_ie_len);
543 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
545 "Station tried to associate with unknown SSID "
547 return WLAN_STATUS_UNSPECIFIED_FAILURE;
550 return WLAN_STATUS_SUCCESS;
554 static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
555 const u8 *wmm_ie, size_t wmm_ie_len)
557 sta->flags &= ~WLAN_STA_WMM;
558 if (wmm_ie && hapd->conf->wmm_enabled) {
559 if (hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len))
560 hostapd_logger(hapd, sta->addr,
563 "invalid WMM element in association "
566 sta->flags |= WLAN_STA_WMM;
568 return WLAN_STATUS_SUCCESS;
572 static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
573 struct ieee802_11_elems *elems)
575 if (!elems->supp_rates) {
576 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
578 "No supported rates element in AssocReq");
579 return WLAN_STATUS_UNSPECIFIED_FAILURE;
582 if (elems->supp_rates_len > sizeof(sta->supported_rates)) {
583 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
585 "Invalid supported rates element length %d",
586 elems->supp_rates_len);
587 return WLAN_STATUS_UNSPECIFIED_FAILURE;
590 os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
591 os_memcpy(sta->supported_rates, elems->supp_rates,
592 elems->supp_rates_len);
593 sta->supported_rates_len = elems->supp_rates_len;
595 if (elems->ext_supp_rates) {
596 if (elems->supp_rates_len + elems->ext_supp_rates_len >
597 sizeof(sta->supported_rates)) {
598 hostapd_logger(hapd, sta->addr,
599 HOSTAPD_MODULE_IEEE80211,
601 "Invalid supported rates element length"
602 " %d+%d", elems->supp_rates_len,
603 elems->ext_supp_rates_len);
604 return WLAN_STATUS_UNSPECIFIED_FAILURE;
607 os_memcpy(sta->supported_rates + elems->supp_rates_len,
608 elems->ext_supp_rates, elems->ext_supp_rates_len);
609 sta->supported_rates_len += elems->ext_supp_rates_len;
612 return WLAN_STATUS_SUCCESS;
616 static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
617 const u8 *ies, size_t ies_len, int reassoc)
619 struct ieee802_11_elems elems;
624 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
625 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
626 HOSTAPD_LEVEL_INFO, "Station sent an invalid "
627 "association request");
628 return WLAN_STATUS_UNSPECIFIED_FAILURE;
631 resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
632 if (resp != WLAN_STATUS_SUCCESS)
634 resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
635 if (resp != WLAN_STATUS_SUCCESS)
637 resp = copy_supp_rates(hapd, sta, &elems);
638 if (resp != WLAN_STATUS_SUCCESS)
640 #ifdef CONFIG_IEEE80211N
641 resp = copy_sta_ht_capab(sta, elems.ht_capabilities,
642 elems.ht_capabilities_len);
643 if (resp != WLAN_STATUS_SUCCESS)
645 #endif /* CONFIG_IEEE80211N */
647 if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
648 wpa_ie = elems.rsn_ie;
649 wpa_ie_len = elems.rsn_ie_len;
650 } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
652 wpa_ie = elems.wpa_ie;
653 wpa_ie_len = elems.wpa_ie_len;
660 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
661 if (hapd->conf->wps_state && elems.wps_ie) {
662 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
663 "Request - assume WPS is used");
664 sta->flags |= WLAN_STA_WPS;
665 wpabuf_free(sta->wps_ie);
666 sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
670 if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
671 wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
672 "(Re)Association Request - reject");
673 return WLAN_STATUS_INVALID_IE;
675 } else if (hapd->conf->wps_state && wpa_ie == NULL) {
676 wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
677 "(Re)Association Request - possible WPS use");
678 sta->flags |= WLAN_STA_MAYBE_WPS;
680 #endif /* CONFIG_WPS */
681 if (hapd->conf->wpa && wpa_ie == NULL) {
682 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
684 "No WPA/RSN IE in association request");
685 return WLAN_STATUS_INVALID_IE;
688 if (hapd->conf->wpa && wpa_ie) {
692 if (sta->wpa_sm == NULL)
693 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
695 if (sta->wpa_sm == NULL) {
696 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
698 return WLAN_STATUS_UNSPECIFIED_FAILURE;
700 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
702 elems.mdie, elems.mdie_len);
703 if (res == WPA_INVALID_GROUP)
704 resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
705 else if (res == WPA_INVALID_PAIRWISE)
706 resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
707 else if (res == WPA_INVALID_AKMP)
708 resp = WLAN_STATUS_AKMP_NOT_VALID;
709 else if (res == WPA_ALLOC_FAIL)
710 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
711 #ifdef CONFIG_IEEE80211W
712 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
713 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
714 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
715 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
716 #endif /* CONFIG_IEEE80211W */
717 else if (res == WPA_INVALID_MDIE)
718 resp = WLAN_STATUS_INVALID_MDIE;
719 else if (res != WPA_IE_OK)
720 resp = WLAN_STATUS_INVALID_IE;
721 if (resp != WLAN_STATUS_SUCCESS)
723 #ifdef CONFIG_IEEE80211W
724 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
725 sta->sa_query_count > 0)
726 ap_check_sa_query_timeout(hapd, sta);
727 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
728 (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
730 * STA has already been associated with MFP and SA
731 * Query timeout has not been reached. Reject the
732 * association attempt temporarily and start SA Query,
733 * if one is not pending.
736 if (sta->sa_query_count == 0)
737 ap_sta_start_sa_query(hapd, sta);
739 return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
742 if (wpa_auth_uses_mfp(sta->wpa_sm))
743 sta->flags |= WLAN_STA_MFP;
745 sta->flags &= ~WLAN_STA_MFP;
746 #endif /* CONFIG_IEEE80211W */
748 #ifdef CONFIG_IEEE80211R
749 if (sta->auth_alg == WLAN_AUTH_FT) {
751 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
752 "to use association (not "
753 "re-association) with FT auth_alg",
755 return WLAN_STATUS_UNSPECIFIED_FAILURE;
758 resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
760 if (resp != WLAN_STATUS_SUCCESS)
763 #endif /* CONFIG_IEEE80211R */
765 #ifdef CONFIG_IEEE80211N
766 if ((sta->flags & WLAN_STA_HT) &&
767 wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
768 hostapd_logger(hapd, sta->addr,
769 HOSTAPD_MODULE_IEEE80211,
771 "Station tried to use TKIP with HT "
773 return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
775 #endif /* CONFIG_IEEE80211N */
777 wpa_auth_sta_no_wpa(sta->wpa_sm);
781 wpabuf_free(sta->p2p_ie);
782 sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
786 wpabuf_free(sta->p2p_ie);
790 p2p_group_notif_assoc(hapd->p2p_group, sta->addr, ies, ies_len);
791 #endif /* CONFIG_P2P */
793 return WLAN_STATUS_SUCCESS;
797 static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
801 struct ieee80211_mgmt reply;
803 os_memset(&reply, 0, sizeof(reply));
804 reply.frame_control =
805 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
806 os_memcpy(reply.da, addr, ETH_ALEN);
807 os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
808 os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
810 send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
811 reply.u.deauth.reason_code = host_to_le16(reason_code);
813 if (hostapd_drv_send_mlme(hapd, &reply, send_len) < 0)
814 wpa_printf(MSG_INFO, "Failed to send deauth: %s",
819 static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
820 u16 status_code, int reassoc, const u8 *ies,
824 u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
825 struct ieee80211_mgmt *reply;
828 os_memset(buf, 0, sizeof(buf));
829 reply = (struct ieee80211_mgmt *) buf;
830 reply->frame_control =
831 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
832 (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
833 WLAN_FC_STYPE_ASSOC_RESP));
834 os_memcpy(reply->da, sta->addr, ETH_ALEN);
835 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
836 os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
838 send_len = IEEE80211_HDRLEN;
839 send_len += sizeof(reply->u.assoc_resp);
840 reply->u.assoc_resp.capab_info =
841 host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
842 reply->u.assoc_resp.status_code = host_to_le16(status_code);
843 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
844 | BIT(14) | BIT(15));
845 /* Supported rates */
846 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
847 /* Extended supported rates */
848 p = hostapd_eid_ext_supp_rates(hapd, p);
850 #ifdef CONFIG_IEEE80211R
851 if (status_code == WLAN_STATUS_SUCCESS) {
852 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
853 * Transition Information, RSN, [RIC Response] */
854 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
855 buf + sizeof(buf) - p,
856 sta->auth_alg, ies, ies_len);
858 #endif /* CONFIG_IEEE80211R */
860 #ifdef CONFIG_IEEE80211W
861 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
862 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
863 #endif /* CONFIG_IEEE80211W */
865 #ifdef CONFIG_IEEE80211N
866 p = hostapd_eid_ht_capabilities(hapd, p);
867 p = hostapd_eid_ht_operation(hapd, p);
868 #endif /* CONFIG_IEEE80211N */
870 if (sta->flags & WLAN_STA_WMM)
871 p = hostapd_eid_wmm(hapd, p);
874 if (sta->flags & WLAN_STA_WPS) {
875 struct wpabuf *wps = wps_build_assoc_resp_ie();
877 os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
878 p += wpabuf_len(wps);
882 #endif /* CONFIG_WPS */
886 struct wpabuf *p2p_resp_ie;
887 enum p2p_status_code status;
888 switch (status_code) {
889 case WLAN_STATUS_SUCCESS:
890 status = P2P_SC_SUCCESS;
892 case WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
893 status = P2P_SC_FAIL_LIMIT_REACHED;
896 status = P2P_SC_FAIL_INVALID_PARAMS;
899 p2p_resp_ie = p2p_group_assoc_resp_ie(hapd->p2p_group, status);
901 os_memcpy(p, wpabuf_head(p2p_resp_ie),
902 wpabuf_len(p2p_resp_ie));
903 p += wpabuf_len(p2p_resp_ie);
904 wpabuf_free(p2p_resp_ie);
907 #endif /* CONFIG_P2P */
909 #ifdef CONFIG_P2P_MANAGER
910 if (hapd->conf->p2p & P2P_MANAGE)
911 p = hostapd_eid_p2p_manage(hapd, p);
912 #endif /* CONFIG_P2P_MANAGER */
914 send_len += p - reply->u.assoc_resp.variable;
916 if (hostapd_drv_send_mlme(hapd, reply, send_len) < 0)
917 wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
922 static void handle_assoc(struct hostapd_data *hapd,
923 const struct ieee80211_mgmt *mgmt, size_t len,
926 u16 capab_info, listen_interval;
927 u16 resp = WLAN_STATUS_SUCCESS;
930 struct sta_info *sta;
932 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
933 sizeof(mgmt->u.assoc_req))) {
934 printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
935 "\n", reassoc, (unsigned long) len);
940 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
941 listen_interval = le_to_host16(
942 mgmt->u.reassoc_req.listen_interval);
943 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
944 " capab_info=0x%02x listen_interval=%d current_ap="
946 MAC2STR(mgmt->sa), capab_info, listen_interval,
947 MAC2STR(mgmt->u.reassoc_req.current_ap));
948 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
949 pos = mgmt->u.reassoc_req.variable;
951 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
952 listen_interval = le_to_host16(
953 mgmt->u.assoc_req.listen_interval);
954 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
955 " capab_info=0x%02x listen_interval=%d",
956 MAC2STR(mgmt->sa), capab_info, listen_interval);
957 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
958 pos = mgmt->u.assoc_req.variable;
961 sta = ap_get_sta(hapd, mgmt->sa);
962 #ifdef CONFIG_IEEE80211R
963 if (sta && sta->auth_alg == WLAN_AUTH_FT &&
964 (sta->flags & WLAN_STA_AUTH) == 0) {
965 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
966 "prior to authentication since it is using "
967 "over-the-DS FT", MAC2STR(mgmt->sa));
969 #endif /* CONFIG_IEEE80211R */
970 if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
971 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
972 HOSTAPD_LEVEL_INFO, "Station tried to "
973 "associate before authentication "
974 "(aid=%d flags=0x%x)",
976 sta ? sta->flags : 0);
977 send_deauth(hapd, mgmt->sa,
978 WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
982 if (hapd->tkip_countermeasures) {
983 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
987 if (listen_interval > hapd->conf->max_listen_interval) {
988 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
990 "Too large Listen Interval (%d)",
992 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
996 /* followed by SSID and Supported rates; and HT capabilities if 802.11n
998 resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
999 if (resp != WLAN_STATUS_SUCCESS)
1002 if (hostapd_get_aid(hapd, sta) < 0) {
1003 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1004 HOSTAPD_LEVEL_INFO, "No room for more AIDs");
1005 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1009 sta->capability = capab_info;
1010 sta->listen_interval = listen_interval;
1012 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
1013 sta->flags |= WLAN_STA_NONERP;
1014 for (i = 0; i < sta->supported_rates_len; i++) {
1015 if ((sta->supported_rates[i] & 0x7f) > 22) {
1016 sta->flags &= ~WLAN_STA_NONERP;
1020 if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
1021 sta->nonerp_set = 1;
1022 hapd->iface->num_sta_non_erp++;
1023 if (hapd->iface->num_sta_non_erp == 1)
1024 ieee802_11_set_beacons(hapd->iface);
1027 if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
1028 !sta->no_short_slot_time_set) {
1029 sta->no_short_slot_time_set = 1;
1030 hapd->iface->num_sta_no_short_slot_time++;
1031 if (hapd->iface->current_mode->mode ==
1032 HOSTAPD_MODE_IEEE80211G &&
1033 hapd->iface->num_sta_no_short_slot_time == 1)
1034 ieee802_11_set_beacons(hapd->iface);
1037 if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1038 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
1040 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
1042 if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
1043 !sta->no_short_preamble_set) {
1044 sta->no_short_preamble_set = 1;
1045 hapd->iface->num_sta_no_short_preamble++;
1046 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
1047 && hapd->iface->num_sta_no_short_preamble == 1)
1048 ieee802_11_set_beacons(hapd->iface);
1051 #ifdef CONFIG_IEEE80211N
1052 update_ht_state(hapd, sta);
1053 #endif /* CONFIG_IEEE80211N */
1055 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1056 HOSTAPD_LEVEL_DEBUG,
1057 "association OK (aid %d)", sta->aid);
1058 /* Station will be marked associated, after it acknowledges AssocResp
1061 #ifdef CONFIG_IEEE80211W
1062 if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
1063 wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
1064 "SA Query procedure", reassoc ? "re" : "");
1065 /* TODO: Send a protected Disassociate frame to the STA using
1066 * the old key and Reason Code "Previous Authentication no
1067 * longer valid". Make sure this is only sent protected since
1068 * unprotected frame would be received by the STA that is now
1069 * trying to associate.
1072 #endif /* CONFIG_IEEE80211W */
1075 os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
1079 if (sta->last_assoc_req)
1080 os_free(sta->last_assoc_req);
1081 sta->last_assoc_req = os_malloc(len);
1082 if (sta->last_assoc_req)
1083 os_memcpy(sta->last_assoc_req, mgmt, len);
1085 /* Make sure that the previously registered inactivity timer will not
1086 * remove the STA immediately. */
1087 sta->timeout_next = STA_NULLFUNC;
1090 send_assoc_resp(hapd, sta, resp, reassoc, pos, left);
1094 static void handle_disassoc(struct hostapd_data *hapd,
1095 const struct ieee80211_mgmt *mgmt, size_t len)
1097 struct sta_info *sta;
1099 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1100 printf("handle_disassoc - too short payload (len=%lu)\n",
1101 (unsigned long) len);
1105 wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1107 le_to_host16(mgmt->u.disassoc.reason_code));
1109 sta = ap_get_sta(hapd, mgmt->sa);
1111 printf("Station " MACSTR " trying to disassociate, but it "
1112 "is not associated.\n", MAC2STR(mgmt->sa));
1116 sta->flags &= ~WLAN_STA_ASSOC;
1117 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
1118 MAC2STR(sta->addr));
1119 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1120 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1121 HOSTAPD_LEVEL_INFO, "disassociated");
1122 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1123 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1124 /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1126 accounting_sta_stop(hapd, sta);
1127 ieee802_1x_free_station(sta);
1128 hostapd_drv_sta_remove(hapd, sta->addr);
1130 if (sta->timeout_next == STA_NULLFUNC ||
1131 sta->timeout_next == STA_DISASSOC) {
1132 sta->timeout_next = STA_DEAUTH;
1133 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1134 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1138 mlme_disassociate_indication(
1139 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1143 static void handle_deauth(struct hostapd_data *hapd,
1144 const struct ieee80211_mgmt *mgmt, size_t len)
1146 struct sta_info *sta;
1148 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
1149 printf("handle_deauth - too short payload (len=%lu)\n",
1150 (unsigned long) len);
1154 wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
1157 le_to_host16(mgmt->u.deauth.reason_code));
1159 sta = ap_get_sta(hapd, mgmt->sa);
1161 printf("Station " MACSTR " trying to deauthenticate, but it "
1162 "is not authenticated.\n", MAC2STR(mgmt->sa));
1166 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1167 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
1168 MAC2STR(sta->addr));
1169 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1170 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1171 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1172 mlme_deauthenticate_indication(
1173 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1174 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1175 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1176 ap_free_sta(hapd, sta);
1180 static void handle_beacon(struct hostapd_data *hapd,
1181 const struct ieee80211_mgmt *mgmt, size_t len,
1182 struct hostapd_frame_info *fi)
1184 struct ieee802_11_elems elems;
1186 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1187 printf("handle_beacon - too short payload (len=%lu)\n",
1188 (unsigned long) len);
1192 (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
1193 len - (IEEE80211_HDRLEN +
1194 sizeof(mgmt->u.beacon)), &elems,
1197 ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1201 #ifdef CONFIG_IEEE80211W
1203 /* MLME-SAQuery.request */
1204 void ieee802_11_send_sa_query_req(struct hostapd_data *hapd,
1205 const u8 *addr, const u8 *trans_id)
1207 struct ieee80211_mgmt mgmt;
1210 wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Request to "
1211 MACSTR, MAC2STR(addr));
1212 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
1213 trans_id, WLAN_SA_QUERY_TR_ID_LEN);
1215 os_memset(&mgmt, 0, sizeof(mgmt));
1216 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1217 WLAN_FC_STYPE_ACTION);
1218 os_memcpy(mgmt.da, addr, ETH_ALEN);
1219 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
1220 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
1221 mgmt.u.action.category = WLAN_ACTION_SA_QUERY;
1222 mgmt.u.action.u.sa_query_req.action = WLAN_SA_QUERY_REQUEST;
1223 os_memcpy(mgmt.u.action.u.sa_query_req.trans_id, trans_id,
1224 WLAN_SA_QUERY_TR_ID_LEN);
1225 end = mgmt.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
1226 if (hostapd_drv_send_mlme(hapd, &mgmt, end - (u8 *) &mgmt) < 0)
1227 perror("ieee802_11_send_sa_query_req: send");
1231 static void hostapd_sa_query_request(struct hostapd_data *hapd,
1232 const struct ieee80211_mgmt *mgmt)
1234 struct sta_info *sta;
1235 struct ieee80211_mgmt resp;
1238 wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Request from "
1239 MACSTR, MAC2STR(mgmt->sa));
1240 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
1241 mgmt->u.action.u.sa_query_resp.trans_id,
1242 WLAN_SA_QUERY_TR_ID_LEN);
1244 sta = ap_get_sta(hapd, mgmt->sa);
1245 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1246 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignore SA Query Request "
1247 "from unassociated STA " MACSTR, MAC2STR(mgmt->sa));
1251 wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Response to "
1252 MACSTR, MAC2STR(mgmt->sa));
1254 os_memset(&resp, 0, sizeof(resp));
1255 resp.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1256 WLAN_FC_STYPE_ACTION);
1257 os_memcpy(resp.da, mgmt->sa, ETH_ALEN);
1258 os_memcpy(resp.sa, hapd->own_addr, ETH_ALEN);
1259 os_memcpy(resp.bssid, hapd->own_addr, ETH_ALEN);
1260 resp.u.action.category = WLAN_ACTION_SA_QUERY;
1261 resp.u.action.u.sa_query_req.action = WLAN_SA_QUERY_RESPONSE;
1262 os_memcpy(resp.u.action.u.sa_query_req.trans_id,
1263 mgmt->u.action.u.sa_query_req.trans_id,
1264 WLAN_SA_QUERY_TR_ID_LEN);
1265 end = resp.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
1266 if (hostapd_drv_send_mlme(hapd, &resp, end - (u8 *) &resp) < 0)
1267 perror("hostapd_sa_query_request: send");
1271 static void hostapd_sa_query_action(struct hostapd_data *hapd,
1272 const struct ieee80211_mgmt *mgmt,
1275 struct sta_info *sta;
1279 end = mgmt->u.action.u.sa_query_resp.trans_id +
1280 WLAN_SA_QUERY_TR_ID_LEN;
1281 if (((u8 *) mgmt) + len < end) {
1282 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
1283 "frame (len=%lu)", (unsigned long) len);
1287 if (mgmt->u.action.u.sa_query_resp.action == WLAN_SA_QUERY_REQUEST) {
1288 hostapd_sa_query_request(hapd, mgmt);
1292 if (mgmt->u.action.u.sa_query_resp.action != WLAN_SA_QUERY_RESPONSE) {
1293 wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected SA Query "
1294 "Action %d", mgmt->u.action.u.sa_query_resp.action);
1298 wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Response from "
1299 MACSTR, MAC2STR(mgmt->sa));
1300 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
1301 mgmt->u.action.u.sa_query_resp.trans_id,
1302 WLAN_SA_QUERY_TR_ID_LEN);
1304 /* MLME-SAQuery.confirm */
1306 sta = ap_get_sta(hapd, mgmt->sa);
1307 if (sta == NULL || sta->sa_query_trans_id == NULL) {
1308 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
1309 "pending SA Query request found");
1313 for (i = 0; i < sta->sa_query_count; i++) {
1314 if (os_memcmp(sta->sa_query_trans_id +
1315 i * WLAN_SA_QUERY_TR_ID_LEN,
1316 mgmt->u.action.u.sa_query_resp.trans_id,
1317 WLAN_SA_QUERY_TR_ID_LEN) == 0)
1321 if (i >= sta->sa_query_count) {
1322 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching SA Query "
1323 "transaction identifier found");
1327 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1328 HOSTAPD_LEVEL_DEBUG,
1329 "Reply to pending SA Query received");
1330 ap_sta_stop_sa_query(hapd, sta);
1334 static int robust_action_frame(u8 category)
1336 return category != WLAN_ACTION_PUBLIC &&
1337 category != WLAN_ACTION_HT;
1339 #endif /* CONFIG_IEEE80211W */
1342 static void handle_action(struct hostapd_data *hapd,
1343 const struct ieee80211_mgmt *mgmt, size_t len)
1345 struct sta_info *sta;
1347 if (len < IEEE80211_HDRLEN + 1) {
1348 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1349 HOSTAPD_LEVEL_DEBUG,
1350 "handle_action - too short payload (len=%lu)",
1351 (unsigned long) len);
1355 sta = ap_get_sta(hapd, mgmt->sa);
1356 #ifdef CONFIG_IEEE80211W
1357 if (sta && (sta->flags & WLAN_STA_MFP) &&
1358 !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP) &&
1359 robust_action_frame(mgmt->u.action.category))) {
1360 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1361 HOSTAPD_LEVEL_DEBUG,
1362 "Dropped unprotected Robust Action frame from "
1366 #endif /* CONFIG_IEEE80211W */
1368 switch (mgmt->u.action.category) {
1369 #ifdef CONFIG_IEEE80211R
1370 case WLAN_ACTION_FT:
1372 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1373 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
1374 "frame from unassociated STA " MACSTR,
1379 if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1380 len - IEEE80211_HDRLEN))
1385 #endif /* CONFIG_IEEE80211R */
1386 case WLAN_ACTION_WMM:
1387 hostapd_wmm_action(hapd, mgmt, len);
1389 #ifdef CONFIG_IEEE80211W
1390 case WLAN_ACTION_SA_QUERY:
1391 hostapd_sa_query_action(hapd, mgmt, len);
1393 #endif /* CONFIG_IEEE80211W */
1394 case WLAN_ACTION_PUBLIC:
1395 if (hapd->public_action_cb) {
1396 hapd->public_action_cb(hapd->public_action_cb_ctx,
1402 case WLAN_ACTION_VENDOR_SPECIFIC:
1403 if (hapd->vendor_action_cb) {
1404 if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
1406 hapd->iface->freq) == 0)
1412 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1413 HOSTAPD_LEVEL_DEBUG,
1414 "handle_action - unknown action category %d or invalid "
1416 mgmt->u.action.category);
1417 if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1418 !(mgmt->sa[0] & 0x01)) {
1419 struct ieee80211_mgmt *resp;
1422 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1423 * Return the Action frame to the source without change
1424 * except that MSB of the Category set to 1.
1426 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1427 "frame back to sender");
1428 resp = os_malloc(len);
1431 os_memcpy(resp, mgmt, len);
1432 os_memcpy(resp->da, resp->sa, ETH_ALEN);
1433 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
1434 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
1435 resp->u.action.category |= 0x80;
1437 hostapd_drv_send_mlme(hapd, resp, len);
1444 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1445 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1447 * @buf: management frame data (starting from IEEE 802.11 header)
1448 * @len: length of frame data in octets
1449 * @fi: meta data about received frame (signal level, etc.)
1451 * Process all incoming IEEE 802.11 management frames. This will be called for
1452 * each frame received from the kernel driver through wlan#ap interface. In
1453 * addition, it can be called to re-inserted pending frames (e.g., when using
1454 * external RADIUS server as an MAC ACL).
1456 void ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
1457 struct hostapd_frame_info *fi)
1459 struct ieee80211_mgmt *mgmt;
1463 mgmt = (struct ieee80211_mgmt *) buf;
1464 fc = le_to_host16(mgmt->frame_control);
1465 stype = WLAN_FC_GET_STYPE(fc);
1467 if (stype == WLAN_FC_STYPE_BEACON) {
1468 handle_beacon(hapd, mgmt, len, fi);
1472 broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
1473 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
1474 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
1477 os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
1478 printf("MGMT: BSSID=" MACSTR " not our address\n",
1479 MAC2STR(mgmt->bssid));
1484 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
1485 handle_probe_req(hapd, mgmt, len);
1489 if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
1490 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1491 HOSTAPD_LEVEL_DEBUG,
1492 "MGMT: DA=" MACSTR " not our address",
1498 case WLAN_FC_STYPE_AUTH:
1499 wpa_printf(MSG_DEBUG, "mgmt::auth");
1500 handle_auth(hapd, mgmt, len);
1502 case WLAN_FC_STYPE_ASSOC_REQ:
1503 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
1504 handle_assoc(hapd, mgmt, len, 0);
1506 case WLAN_FC_STYPE_REASSOC_REQ:
1507 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
1508 handle_assoc(hapd, mgmt, len, 1);
1510 case WLAN_FC_STYPE_DISASSOC:
1511 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
1512 handle_disassoc(hapd, mgmt, len);
1514 case WLAN_FC_STYPE_DEAUTH:
1515 wpa_printf(MSG_DEBUG, "mgmt::deauth");
1516 handle_deauth(hapd, mgmt, len);
1518 case WLAN_FC_STYPE_ACTION:
1519 wpa_printf(MSG_DEBUG, "mgmt::action");
1520 handle_action(hapd, mgmt, len);
1523 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1524 HOSTAPD_LEVEL_DEBUG,
1525 "unknown mgmt frame subtype %d", stype);
1531 static void handle_auth_cb(struct hostapd_data *hapd,
1532 const struct ieee80211_mgmt *mgmt,
1535 u16 auth_alg, auth_transaction, status_code;
1536 struct sta_info *sta;
1539 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1540 HOSTAPD_LEVEL_NOTICE,
1541 "did not acknowledge authentication response");
1545 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
1546 printf("handle_auth_cb - too short payload (len=%lu)\n",
1547 (unsigned long) len);
1551 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1552 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1553 status_code = le_to_host16(mgmt->u.auth.status_code);
1555 sta = ap_get_sta(hapd, mgmt->da);
1557 printf("handle_auth_cb: STA " MACSTR " not found\n",
1562 if (status_code == WLAN_STATUS_SUCCESS &&
1563 ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
1564 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
1565 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1566 HOSTAPD_LEVEL_INFO, "authenticated");
1567 sta->flags |= WLAN_STA_AUTH;
1572 static void handle_assoc_cb(struct hostapd_data *hapd,
1573 const struct ieee80211_mgmt *mgmt,
1574 size_t len, int reassoc, int ok)
1577 struct sta_info *sta;
1579 struct ieee80211_ht_capabilities ht_cap;
1582 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1583 HOSTAPD_LEVEL_DEBUG,
1584 "did not acknowledge association response");
1588 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
1589 sizeof(mgmt->u.assoc_resp))) {
1590 printf("handle_assoc_cb(reassoc=%d) - too short payload "
1591 "(len=%lu)\n", reassoc, (unsigned long) len);
1596 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
1598 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1600 sta = ap_get_sta(hapd, mgmt->da);
1602 printf("handle_assoc_cb: STA " MACSTR " not found\n",
1607 if (status != WLAN_STATUS_SUCCESS)
1610 /* Stop previous accounting session, if one is started, and allocate
1611 * new session id for the new session. */
1612 accounting_sta_stop(hapd, sta);
1614 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1616 "associated (aid %d)",
1619 if (sta->flags & WLAN_STA_ASSOC)
1621 sta->flags |= WLAN_STA_ASSOC;
1622 if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa) ||
1623 sta->auth_alg == WLAN_AUTH_FT) {
1625 * Open, static WEP, or FT protocol; no separate authorization
1628 sta->flags |= WLAN_STA_AUTHORIZED;
1629 wpa_msg(hapd->msg_ctx, MSG_INFO,
1630 AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
1634 mlme_reassociate_indication(hapd, sta);
1636 mlme_associate_indication(hapd, sta);
1638 #ifdef CONFIG_IEEE80211W
1639 sta->sa_query_timed_out = 0;
1640 #endif /* CONFIG_IEEE80211W */
1643 * Remove the STA entry in order to make sure the STA PS state gets
1644 * cleared and configuration gets updated in case of reassociation back
1647 hostapd_drv_sta_remove(hapd, sta->addr);
1649 #ifdef CONFIG_IEEE80211N
1650 if (sta->flags & WLAN_STA_HT)
1651 hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
1652 #endif /* CONFIG_IEEE80211N */
1654 if (hapd->drv.sta_add(hapd, sta->addr, sta->aid, sta->capability,
1655 sta->supported_rates, sta->supported_rates_len,
1656 sta->listen_interval,
1657 sta->flags & WLAN_STA_HT ? &ht_cap : NULL)) {
1658 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1659 HOSTAPD_LEVEL_NOTICE,
1660 "Could not add STA to kernel driver");
1663 if (sta->flags & WLAN_STA_WDS)
1664 hapd->drv.set_wds_sta(hapd, sta->addr, sta->aid, 1);
1666 if (sta->eapol_sm == NULL) {
1668 * This STA does not use RADIUS server for EAP authentication,
1669 * so bind it to the selected VLAN interface now, since the
1670 * interface selection is not going to change anymore.
1672 if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
1674 } else if (sta->vlan_id) {
1675 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
1676 if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
1680 hapd->drv.set_sta_flags(hapd, sta);
1682 if (sta->auth_alg == WLAN_AUTH_FT)
1683 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
1685 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
1686 hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
1688 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
1691 /* Copy of the association request is not needed anymore */
1692 if (sta->last_assoc_req) {
1693 os_free(sta->last_assoc_req);
1694 sta->last_assoc_req = NULL;
1700 * ieee802_11_mgmt_cb - Process management frame TX status callback
1701 * @hapd: hostapd BSS data structure (the BSS from which the management frame
1703 * @buf: management frame data (starting from IEEE 802.11 header)
1704 * @len: length of frame data in octets
1705 * @stype: management frame subtype from frame control field
1706 * @ok: Whether the frame was ACK'ed
1708 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
1711 const struct ieee80211_mgmt *mgmt;
1712 mgmt = (const struct ieee80211_mgmt *) buf;
1715 case WLAN_FC_STYPE_AUTH:
1716 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
1717 handle_auth_cb(hapd, mgmt, len, ok);
1719 case WLAN_FC_STYPE_ASSOC_RESP:
1720 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
1721 handle_assoc_cb(hapd, mgmt, len, 0, ok);
1723 case WLAN_FC_STYPE_REASSOC_RESP:
1724 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
1725 handle_assoc_cb(hapd, mgmt, len, 1, ok);
1727 case WLAN_FC_STYPE_PROBE_RESP:
1728 wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb");
1730 case WLAN_FC_STYPE_DEAUTH:
1733 case WLAN_FC_STYPE_ACTION:
1734 wpa_printf(MSG_DEBUG, "mgmt::action cb");
1737 printf("unknown mgmt cb frame subtype %d\n", stype);
1743 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1750 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1751 char *buf, size_t buflen)
1758 void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
1759 const u8 *buf, size_t len, int ack)
1761 struct sta_info *sta;
1762 struct hostapd_iface *iface = hapd->iface;
1764 sta = ap_get_sta(hapd, addr);
1765 if (sta == NULL && iface->num_bss > 1) {
1767 for (j = 0; j < iface->num_bss; j++) {
1768 hapd = iface->bss[j];
1769 sta = ap_get_sta(hapd, addr);
1776 if (sta->flags & WLAN_STA_PENDING_POLL) {
1777 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
1778 "activity poll", MAC2STR(sta->addr),
1779 ack ? "ACKed" : "did not ACK");
1781 sta->flags &= ~WLAN_STA_PENDING_POLL;
1784 ieee802_1x_tx_status(hapd, sta, buf, len, ack);
1788 void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
1791 struct sta_info *sta;
1793 sta = ap_get_sta(hapd, src);
1794 if (sta && (sta->flags & WLAN_STA_ASSOC)) {
1795 if (wds && !(sta->flags & WLAN_STA_WDS)) {
1796 wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
1797 "STA " MACSTR " (aid %u)",
1798 MAC2STR(sta->addr), sta->aid);
1799 sta->flags |= WLAN_STA_WDS;
1800 hapd->drv.set_wds_sta(hapd, sta->addr, sta->aid, 1);
1805 wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
1806 MACSTR, MAC2STR(src));
1807 if (sta && (sta->flags & WLAN_STA_AUTH))
1808 hostapd_drv_sta_disassoc(
1810 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1812 hostapd_drv_sta_deauth(
1814 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1818 #endif /* CONFIG_NATIVE_WINDOWS */