2 * hostapd / IEEE 802.11 Management
3 * Copyright (c) 2002-2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #ifndef CONFIG_NATIVE_WINDOWS
13 #include "utils/common.h"
14 #include "utils/eloop.h"
15 #include "crypto/crypto.h"
16 #include "drivers/driver.h"
17 #include "common/ieee802_11_defs.h"
18 #include "common/ieee802_11_common.h"
19 #include "common/wpa_ctrl.h"
20 #include "radius/radius.h"
21 #include "radius/radius_client.h"
26 #include "ieee802_11_auth.h"
28 #include "ieee802_1x.h"
32 #include "accounting.h"
33 #include "ap_config.h"
35 #include "p2p_hostapd.h"
36 #include "ap_drv_ops.h"
37 #include "ieee802_11.h"
40 u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
45 if (hapd->iface->current_rates == NULL)
48 *pos++ = WLAN_EID_SUPP_RATES;
49 num = hapd->iface->num_rates;
50 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
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)
69 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
70 hapd->iface->num_rates < 8)
71 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
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;
86 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht)
92 *pos++ = WLAN_EID_EXT_SUPP_RATES;
95 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
99 continue; /* already in SuppRates IE */
100 *pos = hapd->iface->current_rates[i].rate / 5;
101 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
106 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
107 hapd->iface->num_rates >= 8)
108 *pos++ = 0x80 | BSS_MEMBERSHIP_SELECTOR_HT_PHY;
114 u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
117 int capab = WLAN_CAPABILITY_ESS;
120 if (hapd->iface->num_sta_no_short_preamble == 0 &&
121 hapd->iconf->preamble == SHORT_PREAMBLE)
122 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
124 privacy = hapd->conf->ssid.wep.keys_set;
126 if (hapd->conf->ieee802_1x &&
127 (hapd->conf->default_wep_key_len ||
128 hapd->conf->individual_wep_key_len))
135 int policy, def_klen;
136 if (probe && sta->ssid_probe) {
137 policy = sta->ssid_probe->security_policy;
138 def_klen = sta->ssid_probe->wep.default_len;
140 policy = sta->ssid->security_policy;
141 def_klen = sta->ssid->wep.default_len;
143 privacy = policy != SECURITY_PLAINTEXT;
144 if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
149 capab |= WLAN_CAPABILITY_PRIVACY;
151 if (hapd->iface->current_mode &&
152 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
153 hapd->iface->num_sta_no_short_slot_time == 0)
154 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
160 void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
163 if (len > HOSTAPD_MAX_SSID_LEN)
164 len = HOSTAPD_MAX_SSID_LEN;
165 for (i = 0; i < len; i++) {
166 if (ssid[i] >= 32 && ssid[i] < 127)
175 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
176 u16 auth_transaction, const u8 *challenge,
179 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
181 "authentication (shared key, transaction %d)",
184 if (auth_transaction == 1) {
185 if (!sta->challenge) {
186 /* Generate a pseudo-random challenge */
190 sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
191 if (sta->challenge == NULL)
192 return WLAN_STATUS_UNSPECIFIED_FAILURE;
196 os_memcpy(key, &now.sec, 4);
197 os_memcpy(key + 4, &r, 4);
198 rc4_skip(key, sizeof(key), 0,
199 sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
204 if (auth_transaction != 3)
205 return WLAN_STATUS_UNSPECIFIED_FAILURE;
208 if (!iswep || !sta->challenge || !challenge ||
209 os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
210 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
212 "shared key authentication - invalid "
213 "challenge-response");
214 return WLAN_STATUS_CHALLENGE_FAIL;
217 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
219 "authentication OK (shared key)");
220 #ifdef IEEE80211_REQUIRE_AUTH_ACK
221 /* Station will be marked authenticated if it ACKs the
222 * authentication reply. */
224 sta->flags |= WLAN_STA_AUTH;
225 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
227 os_free(sta->challenge);
228 sta->challenge = NULL;
234 static void send_auth_reply(struct hostapd_data *hapd,
235 const u8 *dst, const u8 *bssid,
236 u16 auth_alg, u16 auth_transaction, u16 resp,
237 const u8 *ies, size_t ies_len)
239 struct ieee80211_mgmt *reply;
243 rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
244 buf = os_zalloc(rlen);
248 reply = (struct ieee80211_mgmt *) buf;
249 reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
251 os_memcpy(reply->da, dst, ETH_ALEN);
252 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
253 os_memcpy(reply->bssid, bssid, ETH_ALEN);
255 reply->u.auth.auth_alg = host_to_le16(auth_alg);
256 reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
257 reply->u.auth.status_code = host_to_le16(resp);
260 os_memcpy(reply->u.auth.variable, ies, ies_len);
262 wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
263 " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
264 MAC2STR(dst), auth_alg, auth_transaction,
265 resp, (unsigned long) ies_len);
266 if (hostapd_drv_send_mlme(hapd, reply, rlen, 0) < 0)
267 perror("send_auth_reply: send");
273 #ifdef CONFIG_IEEE80211R
274 static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
275 u16 auth_transaction, u16 status,
276 const u8 *ies, size_t ies_len)
278 struct hostapd_data *hapd = ctx;
279 struct sta_info *sta;
281 send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
282 status, ies, ies_len);
284 if (status != WLAN_STATUS_SUCCESS)
287 sta = ap_get_sta(hapd, dst);
291 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
292 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
293 sta->flags |= WLAN_STA_AUTH;
294 mlme_authenticate_indication(hapd, sta);
296 #endif /* CONFIG_IEEE80211R */
299 static void handle_auth(struct hostapd_data *hapd,
300 const struct ieee80211_mgmt *mgmt, size_t len)
302 u16 auth_alg, auth_transaction, status_code;
303 u16 resp = WLAN_STATUS_SUCCESS;
304 struct sta_info *sta = NULL;
307 const u8 *challenge = NULL;
308 u32 session_timeout, acct_interim_interval;
312 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
313 size_t resp_ies_len = 0;
315 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
316 printf("handle_auth - too short payload (len=%lu)\n",
317 (unsigned long) len);
321 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
322 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
323 status_code = le_to_host16(mgmt->u.auth.status_code);
324 fc = le_to_host16(mgmt->frame_control);
326 if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
327 2 + WLAN_AUTH_CHALLENGE_LEN &&
328 mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
329 mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
330 challenge = &mgmt->u.auth.variable[2];
332 wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
333 "auth_transaction=%d status_code=%d wep=%d%s",
334 MAC2STR(mgmt->sa), auth_alg, auth_transaction,
335 status_code, !!(fc & WLAN_FC_ISWEP),
336 challenge ? " challenge" : "");
338 if (hapd->tkip_countermeasures) {
339 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
343 if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
344 auth_alg == WLAN_AUTH_OPEN) ||
345 #ifdef CONFIG_IEEE80211R
346 (hapd->conf->wpa && wpa_key_mgmt_ft(hapd->conf->wpa_key_mgmt) &&
347 auth_alg == WLAN_AUTH_FT) ||
348 #endif /* CONFIG_IEEE80211R */
349 ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
350 auth_alg == WLAN_AUTH_SHARED_KEY))) {
351 printf("Unsupported authentication algorithm (%d)\n",
353 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
357 if (!(auth_transaction == 1 ||
358 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
359 printf("Unknown authentication transaction number (%d)\n",
361 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
365 if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
366 printf("Station " MACSTR " not allowed to authenticate.\n",
368 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
372 res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
374 &acct_interim_interval, &vlan_id,
377 if (res == HOSTAPD_ACL_REJECT) {
378 printf("Station " MACSTR " not allowed to authenticate.\n",
380 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
383 if (res == HOSTAPD_ACL_PENDING) {
384 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
385 " waiting for an external authentication",
387 /* Authentication code will re-send the authentication frame
388 * after it has received (and cached) information from the
389 * external source. */
393 sta = ap_sta_add(hapd, mgmt->sa);
395 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
400 if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
402 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
403 HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
404 "%d received from RADIUS server",
406 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
409 sta->vlan_id = vlan_id;
410 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
411 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
414 if (has_psk && hapd->conf->wpa_psk_radius != PSK_RADIUS_IGNORED) {
416 sta->psk = os_malloc(PMK_LEN);
418 os_memcpy(sta->psk, psk, PMK_LEN);
424 sta->flags &= ~WLAN_STA_PREAUTH;
425 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
427 if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
428 sta->acct_interim_interval = acct_interim_interval;
429 if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
430 ap_sta_session_timeout(hapd, sta, session_timeout);
432 ap_sta_no_session_timeout(hapd, sta);
436 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
438 "authentication OK (open system)");
439 #ifdef IEEE80211_REQUIRE_AUTH_ACK
440 /* Station will be marked authenticated if it ACKs the
441 * authentication reply. */
443 sta->flags |= WLAN_STA_AUTH;
444 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
445 sta->auth_alg = WLAN_AUTH_OPEN;
446 mlme_authenticate_indication(hapd, sta);
449 case WLAN_AUTH_SHARED_KEY:
450 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
452 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
453 mlme_authenticate_indication(hapd, sta);
454 if (sta->challenge && auth_transaction == 1) {
455 resp_ies[0] = WLAN_EID_CHALLENGE;
456 resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
457 os_memcpy(resp_ies + 2, sta->challenge,
458 WLAN_AUTH_CHALLENGE_LEN);
459 resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
462 #ifdef CONFIG_IEEE80211R
464 sta->auth_alg = WLAN_AUTH_FT;
465 if (sta->wpa_sm == NULL)
466 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
468 if (sta->wpa_sm == NULL) {
469 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
471 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
474 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
475 auth_transaction, mgmt->u.auth.variable,
476 len - IEEE80211_HDRLEN -
477 sizeof(mgmt->u.auth),
478 handle_auth_ft_finish, hapd);
479 /* handle_auth_ft_finish() callback will complete auth. */
481 #endif /* CONFIG_IEEE80211R */
485 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
486 auth_transaction + 1, resp, resp_ies, resp_ies_len);
490 static int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
494 /* get a unique AID */
496 wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
500 for (i = 0; i < AID_WORDS; i++) {
501 if (hapd->sta_aid[i] == (u32) -1)
503 for (j = 0; j < 32; j++) {
504 if (!(hapd->sta_aid[i] & BIT(j)))
512 aid = i * 32 + j + 1;
517 hapd->sta_aid[i] |= BIT(j);
518 wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
523 static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
524 const u8 *ssid_ie, size_t ssid_ie_len)
527 return WLAN_STATUS_UNSPECIFIED_FAILURE;
529 if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
530 os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
532 ieee802_11_print_ssid(ssid_txt, ssid_ie, ssid_ie_len);
533 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
535 "Station tried to associate with unknown SSID "
537 return WLAN_STATUS_UNSPECIFIED_FAILURE;
540 return WLAN_STATUS_SUCCESS;
544 static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
545 const u8 *wmm_ie, size_t wmm_ie_len)
547 sta->flags &= ~WLAN_STA_WMM;
549 if (wmm_ie && hapd->conf->wmm_enabled) {
550 struct wmm_information_element *wmm;
552 if (!hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len)) {
553 hostapd_logger(hapd, sta->addr,
556 "invalid WMM element in association "
558 return WLAN_STATUS_UNSPECIFIED_FAILURE;
561 sta->flags |= WLAN_STA_WMM;
562 wmm = (struct wmm_information_element *) wmm_ie;
563 sta->qosinfo = wmm->qos_info;
565 return WLAN_STATUS_SUCCESS;
569 static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
570 struct ieee802_11_elems *elems)
572 if (!elems->supp_rates) {
573 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
575 "No supported rates element in AssocReq");
576 return WLAN_STATUS_UNSPECIFIED_FAILURE;
579 if (elems->supp_rates_len > sizeof(sta->supported_rates)) {
580 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
582 "Invalid supported rates element length %d",
583 elems->supp_rates_len);
584 return WLAN_STATUS_UNSPECIFIED_FAILURE;
587 os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
588 os_memcpy(sta->supported_rates, elems->supp_rates,
589 elems->supp_rates_len);
590 sta->supported_rates_len = elems->supp_rates_len;
592 if (elems->ext_supp_rates) {
593 if (elems->supp_rates_len + elems->ext_supp_rates_len >
594 sizeof(sta->supported_rates)) {
595 hostapd_logger(hapd, sta->addr,
596 HOSTAPD_MODULE_IEEE80211,
598 "Invalid supported rates element length"
599 " %d+%d", elems->supp_rates_len,
600 elems->ext_supp_rates_len);
601 return WLAN_STATUS_UNSPECIFIED_FAILURE;
604 os_memcpy(sta->supported_rates + elems->supp_rates_len,
605 elems->ext_supp_rates, elems->ext_supp_rates_len);
606 sta->supported_rates_len += elems->ext_supp_rates_len;
609 return WLAN_STATUS_SUCCESS;
613 static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
614 const u8 *ies, size_t ies_len, int reassoc)
616 struct ieee802_11_elems elems;
621 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
622 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
623 HOSTAPD_LEVEL_INFO, "Station sent an invalid "
624 "association request");
625 return WLAN_STATUS_UNSPECIFIED_FAILURE;
628 resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
629 if (resp != WLAN_STATUS_SUCCESS)
631 resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
632 if (resp != WLAN_STATUS_SUCCESS)
634 resp = copy_supp_rates(hapd, sta, &elems);
635 if (resp != WLAN_STATUS_SUCCESS)
637 #ifdef CONFIG_IEEE80211N
638 resp = copy_sta_ht_capab(hapd, sta, elems.ht_capabilities,
639 elems.ht_capabilities_len);
640 if (resp != WLAN_STATUS_SUCCESS)
642 if (hapd->iconf->ieee80211n && hapd->iconf->require_ht &&
643 !(sta->flags & WLAN_STA_HT)) {
644 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
645 HOSTAPD_LEVEL_INFO, "Station does not support "
646 "mandatory HT PHY - reject association");
647 return WLAN_STATUS_ASSOC_DENIED_NO_HT;
649 #endif /* CONFIG_IEEE80211N */
651 #ifdef CONFIG_IEEE80211AC
652 resp = copy_sta_vht_capab(hapd, sta, elems.vht_capabilities,
653 elems.vht_capabilities_len);
654 if (resp != WLAN_STATUS_SUCCESS)
656 #endif /* CONFIG_IEEE80211AC */
658 if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
659 wpa_ie = elems.rsn_ie;
660 wpa_ie_len = elems.rsn_ie_len;
661 } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
663 wpa_ie = elems.wpa_ie;
664 wpa_ie_len = elems.wpa_ie_len;
671 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
672 if (hapd->conf->wps_state && elems.wps_ie) {
673 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
674 "Request - assume WPS is used");
675 sta->flags |= WLAN_STA_WPS;
676 wpabuf_free(sta->wps_ie);
677 sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
679 if (sta->wps_ie && wps_is_20(sta->wps_ie)) {
680 wpa_printf(MSG_DEBUG, "WPS: STA supports WPS 2.0");
681 sta->flags |= WLAN_STA_WPS2;
685 if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
686 wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
687 "(Re)Association Request - reject");
688 return WLAN_STATUS_INVALID_IE;
690 } else if (hapd->conf->wps_state && wpa_ie == NULL) {
691 wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
692 "(Re)Association Request - possible WPS use");
693 sta->flags |= WLAN_STA_MAYBE_WPS;
695 #endif /* CONFIG_WPS */
696 if (hapd->conf->wpa && wpa_ie == NULL) {
697 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
699 "No WPA/RSN IE in association request");
700 return WLAN_STATUS_INVALID_IE;
703 if (hapd->conf->wpa && wpa_ie) {
707 if (sta->wpa_sm == NULL)
708 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
710 if (sta->wpa_sm == NULL) {
711 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
713 return WLAN_STATUS_UNSPECIFIED_FAILURE;
715 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
717 elems.mdie, elems.mdie_len);
718 if (res == WPA_INVALID_GROUP)
719 resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
720 else if (res == WPA_INVALID_PAIRWISE)
721 resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
722 else if (res == WPA_INVALID_AKMP)
723 resp = WLAN_STATUS_AKMP_NOT_VALID;
724 else if (res == WPA_ALLOC_FAIL)
725 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
726 #ifdef CONFIG_IEEE80211W
727 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
728 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
729 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
730 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
731 #endif /* CONFIG_IEEE80211W */
732 else if (res == WPA_INVALID_MDIE)
733 resp = WLAN_STATUS_INVALID_MDIE;
734 else if (res != WPA_IE_OK)
735 resp = WLAN_STATUS_INVALID_IE;
736 if (resp != WLAN_STATUS_SUCCESS)
738 #ifdef CONFIG_IEEE80211W
739 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
740 sta->sa_query_count > 0)
741 ap_check_sa_query_timeout(hapd, sta);
742 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
743 (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
745 * STA has already been associated with MFP and SA
746 * Query timeout has not been reached. Reject the
747 * association attempt temporarily and start SA Query,
748 * if one is not pending.
751 if (sta->sa_query_count == 0)
752 ap_sta_start_sa_query(hapd, sta);
754 return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
757 if (wpa_auth_uses_mfp(sta->wpa_sm))
758 sta->flags |= WLAN_STA_MFP;
760 sta->flags &= ~WLAN_STA_MFP;
761 #endif /* CONFIG_IEEE80211W */
763 #ifdef CONFIG_IEEE80211R
764 if (sta->auth_alg == WLAN_AUTH_FT) {
766 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
767 "to use association (not "
768 "re-association) with FT auth_alg",
770 return WLAN_STATUS_UNSPECIFIED_FAILURE;
773 resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
775 if (resp != WLAN_STATUS_SUCCESS)
778 #endif /* CONFIG_IEEE80211R */
780 #ifdef CONFIG_IEEE80211N
781 if ((sta->flags & WLAN_STA_HT) &&
782 wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
783 hostapd_logger(hapd, sta->addr,
784 HOSTAPD_MODULE_IEEE80211,
786 "Station tried to use TKIP with HT "
788 return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
790 #endif /* CONFIG_IEEE80211N */
792 wpa_auth_sta_no_wpa(sta->wpa_sm);
796 wpabuf_free(sta->p2p_ie);
797 sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
801 wpabuf_free(sta->p2p_ie);
805 p2p_group_notif_assoc(hapd->p2p_group, sta->addr, ies, ies_len);
806 #endif /* CONFIG_P2P */
808 return WLAN_STATUS_SUCCESS;
812 static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
816 struct ieee80211_mgmt reply;
818 os_memset(&reply, 0, sizeof(reply));
819 reply.frame_control =
820 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
821 os_memcpy(reply.da, addr, ETH_ALEN);
822 os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
823 os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
825 send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
826 reply.u.deauth.reason_code = host_to_le16(reason_code);
828 if (hostapd_drv_send_mlme(hapd, &reply, send_len, 0) < 0)
829 wpa_printf(MSG_INFO, "Failed to send deauth: %s",
834 static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
835 u16 status_code, int reassoc, const u8 *ies,
839 u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
840 struct ieee80211_mgmt *reply;
843 os_memset(buf, 0, sizeof(buf));
844 reply = (struct ieee80211_mgmt *) buf;
845 reply->frame_control =
846 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
847 (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
848 WLAN_FC_STYPE_ASSOC_RESP));
849 os_memcpy(reply->da, sta->addr, ETH_ALEN);
850 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
851 os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
853 send_len = IEEE80211_HDRLEN;
854 send_len += sizeof(reply->u.assoc_resp);
855 reply->u.assoc_resp.capab_info =
856 host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
857 reply->u.assoc_resp.status_code = host_to_le16(status_code);
858 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
859 | BIT(14) | BIT(15));
860 /* Supported rates */
861 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
862 /* Extended supported rates */
863 p = hostapd_eid_ext_supp_rates(hapd, p);
865 #ifdef CONFIG_IEEE80211R
866 if (status_code == WLAN_STATUS_SUCCESS) {
867 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
868 * Transition Information, RSN, [RIC Response] */
869 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
870 buf + sizeof(buf) - p,
871 sta->auth_alg, ies, ies_len);
873 #endif /* CONFIG_IEEE80211R */
875 #ifdef CONFIG_IEEE80211W
876 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
877 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
878 #endif /* CONFIG_IEEE80211W */
880 #ifdef CONFIG_IEEE80211N
881 p = hostapd_eid_ht_capabilities(hapd, p);
882 p = hostapd_eid_ht_operation(hapd, p);
883 #endif /* CONFIG_IEEE80211N */
885 p = hostapd_eid_ext_capab(hapd, p);
886 p = hostapd_eid_bss_max_idle_period(hapd, p);
888 if (sta->flags & WLAN_STA_WMM)
889 p = hostapd_eid_wmm(hapd, p);
892 if ((sta->flags & WLAN_STA_WPS) ||
893 ((sta->flags & WLAN_STA_MAYBE_WPS) && hapd->conf->wpa)) {
894 struct wpabuf *wps = wps_build_assoc_resp_ie();
896 os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
897 p += wpabuf_len(wps);
901 #endif /* CONFIG_WPS */
905 struct wpabuf *p2p_resp_ie;
906 enum p2p_status_code status;
907 switch (status_code) {
908 case WLAN_STATUS_SUCCESS:
909 status = P2P_SC_SUCCESS;
911 case WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA:
912 status = P2P_SC_FAIL_LIMIT_REACHED;
915 status = P2P_SC_FAIL_INVALID_PARAMS;
918 p2p_resp_ie = p2p_group_assoc_resp_ie(hapd->p2p_group, status);
920 os_memcpy(p, wpabuf_head(p2p_resp_ie),
921 wpabuf_len(p2p_resp_ie));
922 p += wpabuf_len(p2p_resp_ie);
923 wpabuf_free(p2p_resp_ie);
926 #endif /* CONFIG_P2P */
928 #ifdef CONFIG_P2P_MANAGER
929 if (hapd->conf->p2p & P2P_MANAGE)
930 p = hostapd_eid_p2p_manage(hapd, p);
931 #endif /* CONFIG_P2P_MANAGER */
933 send_len += p - reply->u.assoc_resp.variable;
935 if (hostapd_drv_send_mlme(hapd, reply, send_len, 0) < 0)
936 wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
941 static void handle_assoc(struct hostapd_data *hapd,
942 const struct ieee80211_mgmt *mgmt, size_t len,
945 u16 capab_info, listen_interval;
946 u16 resp = WLAN_STATUS_SUCCESS;
949 struct sta_info *sta;
951 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
952 sizeof(mgmt->u.assoc_req))) {
953 printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
954 "\n", reassoc, (unsigned long) len);
959 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
960 listen_interval = le_to_host16(
961 mgmt->u.reassoc_req.listen_interval);
962 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
963 " capab_info=0x%02x listen_interval=%d current_ap="
965 MAC2STR(mgmt->sa), capab_info, listen_interval,
966 MAC2STR(mgmt->u.reassoc_req.current_ap));
967 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
968 pos = mgmt->u.reassoc_req.variable;
970 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
971 listen_interval = le_to_host16(
972 mgmt->u.assoc_req.listen_interval);
973 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
974 " capab_info=0x%02x listen_interval=%d",
975 MAC2STR(mgmt->sa), capab_info, listen_interval);
976 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
977 pos = mgmt->u.assoc_req.variable;
980 sta = ap_get_sta(hapd, mgmt->sa);
981 #ifdef CONFIG_IEEE80211R
982 if (sta && sta->auth_alg == WLAN_AUTH_FT &&
983 (sta->flags & WLAN_STA_AUTH) == 0) {
984 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
985 "prior to authentication since it is using "
986 "over-the-DS FT", MAC2STR(mgmt->sa));
988 #endif /* CONFIG_IEEE80211R */
989 if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
990 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
991 HOSTAPD_LEVEL_INFO, "Station tried to "
992 "associate before authentication "
993 "(aid=%d flags=0x%x)",
995 sta ? sta->flags : 0);
996 send_deauth(hapd, mgmt->sa,
997 WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
1001 if (hapd->tkip_countermeasures) {
1002 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
1006 if (listen_interval > hapd->conf->max_listen_interval) {
1007 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1008 HOSTAPD_LEVEL_DEBUG,
1009 "Too large Listen Interval (%d)",
1011 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
1015 /* followed by SSID and Supported rates; and HT capabilities if 802.11n
1017 resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
1018 if (resp != WLAN_STATUS_SUCCESS)
1021 if (hostapd_get_aid(hapd, sta) < 0) {
1022 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1023 HOSTAPD_LEVEL_INFO, "No room for more AIDs");
1024 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1028 sta->capability = capab_info;
1029 sta->listen_interval = listen_interval;
1031 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
1032 sta->flags |= WLAN_STA_NONERP;
1033 for (i = 0; i < sta->supported_rates_len; i++) {
1034 if ((sta->supported_rates[i] & 0x7f) > 22) {
1035 sta->flags &= ~WLAN_STA_NONERP;
1039 if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
1040 sta->nonerp_set = 1;
1041 hapd->iface->num_sta_non_erp++;
1042 if (hapd->iface->num_sta_non_erp == 1)
1043 ieee802_11_set_beacons(hapd->iface);
1046 if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
1047 !sta->no_short_slot_time_set) {
1048 sta->no_short_slot_time_set = 1;
1049 hapd->iface->num_sta_no_short_slot_time++;
1050 if (hapd->iface->current_mode->mode ==
1051 HOSTAPD_MODE_IEEE80211G &&
1052 hapd->iface->num_sta_no_short_slot_time == 1)
1053 ieee802_11_set_beacons(hapd->iface);
1056 if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1057 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
1059 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
1061 if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
1062 !sta->no_short_preamble_set) {
1063 sta->no_short_preamble_set = 1;
1064 hapd->iface->num_sta_no_short_preamble++;
1065 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
1066 && hapd->iface->num_sta_no_short_preamble == 1)
1067 ieee802_11_set_beacons(hapd->iface);
1070 #ifdef CONFIG_IEEE80211N
1071 update_ht_state(hapd, sta);
1072 #endif /* CONFIG_IEEE80211N */
1074 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1075 HOSTAPD_LEVEL_DEBUG,
1076 "association OK (aid %d)", sta->aid);
1077 /* Station will be marked associated, after it acknowledges AssocResp
1079 sta->flags |= WLAN_STA_ASSOC_REQ_OK;
1081 #ifdef CONFIG_IEEE80211W
1082 if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
1083 wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
1084 "SA Query procedure", reassoc ? "re" : "");
1085 /* TODO: Send a protected Disassociate frame to the STA using
1086 * the old key and Reason Code "Previous Authentication no
1087 * longer valid". Make sure this is only sent protected since
1088 * unprotected frame would be received by the STA that is now
1089 * trying to associate.
1092 #endif /* CONFIG_IEEE80211W */
1095 os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
1099 if (sta->last_assoc_req)
1100 os_free(sta->last_assoc_req);
1101 sta->last_assoc_req = os_malloc(len);
1102 if (sta->last_assoc_req)
1103 os_memcpy(sta->last_assoc_req, mgmt, len);
1105 /* Make sure that the previously registered inactivity timer will not
1106 * remove the STA immediately. */
1107 sta->timeout_next = STA_NULLFUNC;
1110 send_assoc_resp(hapd, sta, resp, reassoc, pos, left);
1114 static void handle_disassoc(struct hostapd_data *hapd,
1115 const struct ieee80211_mgmt *mgmt, size_t len)
1117 struct sta_info *sta;
1119 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1120 printf("handle_disassoc - too short payload (len=%lu)\n",
1121 (unsigned long) len);
1125 wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1127 le_to_host16(mgmt->u.disassoc.reason_code));
1129 sta = ap_get_sta(hapd, mgmt->sa);
1131 printf("Station " MACSTR " trying to disassociate, but it "
1132 "is not associated.\n", MAC2STR(mgmt->sa));
1136 ap_sta_set_authorized(hapd, sta, 0);
1137 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
1138 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1139 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1140 HOSTAPD_LEVEL_INFO, "disassociated");
1141 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1142 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1143 /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1145 accounting_sta_stop(hapd, sta);
1146 ieee802_1x_free_station(sta);
1147 hostapd_drv_sta_remove(hapd, sta->addr);
1149 if (sta->timeout_next == STA_NULLFUNC ||
1150 sta->timeout_next == STA_DISASSOC) {
1151 sta->timeout_next = STA_DEAUTH;
1152 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1153 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1157 mlme_disassociate_indication(
1158 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1162 static void handle_deauth(struct hostapd_data *hapd,
1163 const struct ieee80211_mgmt *mgmt, size_t len)
1165 struct sta_info *sta;
1167 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
1168 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "handle_deauth - too short "
1169 "payload (len=%lu)", (unsigned long) len);
1173 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "deauthentication: STA=" MACSTR
1175 MAC2STR(mgmt->sa), le_to_host16(mgmt->u.deauth.reason_code));
1177 sta = ap_get_sta(hapd, mgmt->sa);
1179 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR " trying "
1180 "to deauthenticate, but it is not authenticated",
1185 ap_sta_set_authorized(hapd, sta, 0);
1186 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1187 WLAN_STA_ASSOC_REQ_OK);
1188 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1189 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1190 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1191 mlme_deauthenticate_indication(
1192 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1193 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1194 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1195 ap_free_sta(hapd, sta);
1199 static void handle_beacon(struct hostapd_data *hapd,
1200 const struct ieee80211_mgmt *mgmt, size_t len,
1201 struct hostapd_frame_info *fi)
1203 struct ieee802_11_elems elems;
1205 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1206 printf("handle_beacon - too short payload (len=%lu)\n",
1207 (unsigned long) len);
1211 (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
1212 len - (IEEE80211_HDRLEN +
1213 sizeof(mgmt->u.beacon)), &elems,
1216 ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1220 #ifdef CONFIG_IEEE80211W
1222 static void hostapd_sa_query_action(struct hostapd_data *hapd,
1223 const struct ieee80211_mgmt *mgmt,
1228 end = mgmt->u.action.u.sa_query_resp.trans_id +
1229 WLAN_SA_QUERY_TR_ID_LEN;
1230 if (((u8 *) mgmt) + len < end) {
1231 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
1232 "frame (len=%lu)", (unsigned long) len);
1236 ieee802_11_sa_query_action(hapd, mgmt->sa,
1237 mgmt->u.action.u.sa_query_resp.action,
1238 mgmt->u.action.u.sa_query_resp.trans_id);
1242 static int robust_action_frame(u8 category)
1244 return category != WLAN_ACTION_PUBLIC &&
1245 category != WLAN_ACTION_HT;
1247 #endif /* CONFIG_IEEE80211W */
1250 static void handle_action(struct hostapd_data *hapd,
1251 const struct ieee80211_mgmt *mgmt, size_t len)
1253 #if defined(CONFIG_IEEE80211W) || defined(CONFIG_IEEE80211R)
1254 struct sta_info *sta;
1255 sta = ap_get_sta(hapd, mgmt->sa);
1256 #endif /* CONFIG_IEEE80211W || CONFIG_IEEE80211R */
1258 if (len < IEEE80211_HDRLEN + 1) {
1259 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1260 HOSTAPD_LEVEL_DEBUG,
1261 "handle_action - too short payload (len=%lu)",
1262 (unsigned long) len);
1266 #ifdef CONFIG_IEEE80211W
1267 if (sta && (sta->flags & WLAN_STA_MFP) &&
1268 !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP) &&
1269 robust_action_frame(mgmt->u.action.category))) {
1270 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1271 HOSTAPD_LEVEL_DEBUG,
1272 "Dropped unprotected Robust Action frame from "
1276 #endif /* CONFIG_IEEE80211W */
1278 switch (mgmt->u.action.category) {
1279 #ifdef CONFIG_IEEE80211R
1280 case WLAN_ACTION_FT:
1282 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1283 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
1284 "frame from unassociated STA " MACSTR,
1289 if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1290 len - IEEE80211_HDRLEN))
1295 #endif /* CONFIG_IEEE80211R */
1296 case WLAN_ACTION_WMM:
1297 hostapd_wmm_action(hapd, mgmt, len);
1299 #ifdef CONFIG_IEEE80211W
1300 case WLAN_ACTION_SA_QUERY:
1301 hostapd_sa_query_action(hapd, mgmt, len);
1303 #endif /* CONFIG_IEEE80211W */
1304 case WLAN_ACTION_PUBLIC:
1305 if (hapd->public_action_cb) {
1306 hapd->public_action_cb(hapd->public_action_cb_ctx,
1312 case WLAN_ACTION_VENDOR_SPECIFIC:
1313 if (hapd->vendor_action_cb) {
1314 if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
1316 hapd->iface->freq) == 0)
1322 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1323 HOSTAPD_LEVEL_DEBUG,
1324 "handle_action - unknown action category %d or invalid "
1326 mgmt->u.action.category);
1327 if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1328 !(mgmt->sa[0] & 0x01)) {
1329 struct ieee80211_mgmt *resp;
1332 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1333 * Return the Action frame to the source without change
1334 * except that MSB of the Category set to 1.
1336 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1337 "frame back to sender");
1338 resp = os_malloc(len);
1341 os_memcpy(resp, mgmt, len);
1342 os_memcpy(resp->da, resp->sa, ETH_ALEN);
1343 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
1344 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
1345 resp->u.action.category |= 0x80;
1347 hostapd_drv_send_mlme(hapd, resp, len, 0);
1354 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1355 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1357 * @buf: management frame data (starting from IEEE 802.11 header)
1358 * @len: length of frame data in octets
1359 * @fi: meta data about received frame (signal level, etc.)
1361 * Process all incoming IEEE 802.11 management frames. This will be called for
1362 * each frame received from the kernel driver through wlan#ap interface. In
1363 * addition, it can be called to re-inserted pending frames (e.g., when using
1364 * external RADIUS server as an MAC ACL).
1366 void ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
1367 struct hostapd_frame_info *fi)
1369 struct ieee80211_mgmt *mgmt;
1376 mgmt = (struct ieee80211_mgmt *) buf;
1377 fc = le_to_host16(mgmt->frame_control);
1378 stype = WLAN_FC_GET_STYPE(fc);
1380 if (stype == WLAN_FC_STYPE_BEACON) {
1381 handle_beacon(hapd, mgmt, len, fi);
1385 broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
1386 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
1387 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
1391 /* Invitation responses can be sent with the peer MAC as BSSID */
1392 !((hapd->conf->p2p & P2P_GROUP_OWNER) &&
1393 stype == WLAN_FC_STYPE_ACTION) &&
1394 #endif /* CONFIG_P2P */
1395 os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
1396 printf("MGMT: BSSID=" MACSTR " not our address\n",
1397 MAC2STR(mgmt->bssid));
1402 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
1403 handle_probe_req(hapd, mgmt, len, fi->ssi_signal);
1407 if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
1408 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1409 HOSTAPD_LEVEL_DEBUG,
1410 "MGMT: DA=" MACSTR " not our address",
1416 case WLAN_FC_STYPE_AUTH:
1417 wpa_printf(MSG_DEBUG, "mgmt::auth");
1418 handle_auth(hapd, mgmt, len);
1420 case WLAN_FC_STYPE_ASSOC_REQ:
1421 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
1422 handle_assoc(hapd, mgmt, len, 0);
1424 case WLAN_FC_STYPE_REASSOC_REQ:
1425 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
1426 handle_assoc(hapd, mgmt, len, 1);
1428 case WLAN_FC_STYPE_DISASSOC:
1429 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
1430 handle_disassoc(hapd, mgmt, len);
1432 case WLAN_FC_STYPE_DEAUTH:
1433 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "mgmt::deauth");
1434 handle_deauth(hapd, mgmt, len);
1436 case WLAN_FC_STYPE_ACTION:
1437 wpa_printf(MSG_DEBUG, "mgmt::action");
1438 handle_action(hapd, mgmt, len);
1441 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1442 HOSTAPD_LEVEL_DEBUG,
1443 "unknown mgmt frame subtype %d", stype);
1449 static void handle_auth_cb(struct hostapd_data *hapd,
1450 const struct ieee80211_mgmt *mgmt,
1453 u16 auth_alg, auth_transaction, status_code;
1454 struct sta_info *sta;
1457 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1458 HOSTAPD_LEVEL_NOTICE,
1459 "did not acknowledge authentication response");
1463 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
1464 printf("handle_auth_cb - too short payload (len=%lu)\n",
1465 (unsigned long) len);
1469 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1470 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1471 status_code = le_to_host16(mgmt->u.auth.status_code);
1473 sta = ap_get_sta(hapd, mgmt->da);
1475 printf("handle_auth_cb: STA " MACSTR " not found\n",
1480 if (status_code == WLAN_STATUS_SUCCESS &&
1481 ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
1482 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
1483 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1484 HOSTAPD_LEVEL_INFO, "authenticated");
1485 sta->flags |= WLAN_STA_AUTH;
1490 static void handle_assoc_cb(struct hostapd_data *hapd,
1491 const struct ieee80211_mgmt *mgmt,
1492 size_t len, int reassoc, int ok)
1495 struct sta_info *sta;
1497 struct ieee80211_ht_capabilities ht_cap;
1500 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1501 HOSTAPD_LEVEL_DEBUG,
1502 "did not acknowledge association response");
1506 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
1507 sizeof(mgmt->u.assoc_resp))) {
1508 printf("handle_assoc_cb(reassoc=%d) - too short payload "
1509 "(len=%lu)\n", reassoc, (unsigned long) len);
1514 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
1516 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1518 sta = ap_get_sta(hapd, mgmt->da);
1520 printf("handle_assoc_cb: STA " MACSTR " not found\n",
1525 if (status != WLAN_STATUS_SUCCESS)
1528 /* Stop previous accounting session, if one is started, and allocate
1529 * new session id for the new session. */
1530 accounting_sta_stop(hapd, sta);
1532 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1534 "associated (aid %d)",
1537 if (sta->flags & WLAN_STA_ASSOC)
1539 sta->flags |= WLAN_STA_ASSOC;
1540 if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa) ||
1541 sta->auth_alg == WLAN_AUTH_FT) {
1543 * Open, static WEP, or FT protocol; no separate authorization
1546 ap_sta_set_authorized(hapd, sta, 1);
1550 mlme_reassociate_indication(hapd, sta);
1552 mlme_associate_indication(hapd, sta);
1554 #ifdef CONFIG_IEEE80211W
1555 sta->sa_query_timed_out = 0;
1556 #endif /* CONFIG_IEEE80211W */
1559 * Remove the STA entry in order to make sure the STA PS state gets
1560 * cleared and configuration gets updated in case of reassociation back
1563 hostapd_drv_sta_remove(hapd, sta->addr);
1565 #ifdef CONFIG_IEEE80211N
1566 if (sta->flags & WLAN_STA_HT)
1567 hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
1568 #endif /* CONFIG_IEEE80211N */
1570 if (hostapd_sta_add(hapd, sta->addr, sta->aid, sta->capability,
1571 sta->supported_rates, sta->supported_rates_len,
1572 sta->listen_interval,
1573 sta->flags & WLAN_STA_HT ? &ht_cap : NULL,
1574 sta->flags, sta->qosinfo)) {
1575 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1576 HOSTAPD_LEVEL_NOTICE,
1577 "Could not add STA to kernel driver");
1579 ap_sta_disconnect(hapd, sta, sta->addr,
1580 WLAN_REASON_DISASSOC_AP_BUSY);
1585 if (sta->flags & WLAN_STA_WDS)
1586 hostapd_set_wds_sta(hapd, sta->addr, sta->aid, 1);
1588 if (sta->eapol_sm == NULL) {
1590 * This STA does not use RADIUS server for EAP authentication,
1591 * so bind it to the selected VLAN interface now, since the
1592 * interface selection is not going to change anymore.
1594 if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
1596 } else if (sta->vlan_id) {
1597 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
1598 if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
1602 hostapd_set_sta_flags(hapd, sta);
1604 if (sta->auth_alg == WLAN_AUTH_FT)
1605 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
1607 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
1608 hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
1610 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
1613 /* Copy of the association request is not needed anymore */
1614 if (sta->last_assoc_req) {
1615 os_free(sta->last_assoc_req);
1616 sta->last_assoc_req = NULL;
1621 static void handle_deauth_cb(struct hostapd_data *hapd,
1622 const struct ieee80211_mgmt *mgmt,
1625 struct sta_info *sta;
1626 if (mgmt->da[0] & 0x01)
1628 sta = ap_get_sta(hapd, mgmt->da);
1630 wpa_printf(MSG_DEBUG, "handle_deauth_cb: STA " MACSTR
1631 " not found", MAC2STR(mgmt->da));
1635 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged deauth",
1636 MAC2STR(sta->addr));
1638 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
1639 "deauth", MAC2STR(sta->addr));
1641 ap_sta_deauth_cb(hapd, sta);
1645 static void handle_disassoc_cb(struct hostapd_data *hapd,
1646 const struct ieee80211_mgmt *mgmt,
1649 struct sta_info *sta;
1650 if (mgmt->da[0] & 0x01)
1652 sta = ap_get_sta(hapd, mgmt->da);
1654 wpa_printf(MSG_DEBUG, "handle_disassoc_cb: STA " MACSTR
1655 " not found", MAC2STR(mgmt->da));
1659 wpa_printf(MSG_DEBUG, "STA " MACSTR " acknowledged disassoc",
1660 MAC2STR(sta->addr));
1662 wpa_printf(MSG_DEBUG, "STA " MACSTR " did not acknowledge "
1663 "disassoc", MAC2STR(sta->addr));
1665 ap_sta_disassoc_cb(hapd, sta);
1670 * ieee802_11_mgmt_cb - Process management frame TX status callback
1671 * @hapd: hostapd BSS data structure (the BSS from which the management frame
1673 * @buf: management frame data (starting from IEEE 802.11 header)
1674 * @len: length of frame data in octets
1675 * @stype: management frame subtype from frame control field
1676 * @ok: Whether the frame was ACK'ed
1678 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
1681 const struct ieee80211_mgmt *mgmt;
1682 mgmt = (const struct ieee80211_mgmt *) buf;
1685 case WLAN_FC_STYPE_AUTH:
1686 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
1687 handle_auth_cb(hapd, mgmt, len, ok);
1689 case WLAN_FC_STYPE_ASSOC_RESP:
1690 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
1691 handle_assoc_cb(hapd, mgmt, len, 0, ok);
1693 case WLAN_FC_STYPE_REASSOC_RESP:
1694 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
1695 handle_assoc_cb(hapd, mgmt, len, 1, ok);
1697 case WLAN_FC_STYPE_PROBE_RESP:
1698 wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb");
1700 case WLAN_FC_STYPE_DEAUTH:
1701 wpa_printf(MSG_DEBUG, "mgmt::deauth cb");
1702 handle_deauth_cb(hapd, mgmt, len, ok);
1704 case WLAN_FC_STYPE_DISASSOC:
1705 wpa_printf(MSG_DEBUG, "mgmt::disassoc cb");
1706 handle_disassoc_cb(hapd, mgmt, len, ok);
1708 case WLAN_FC_STYPE_ACTION:
1709 wpa_printf(MSG_DEBUG, "mgmt::action cb");
1712 printf("unknown mgmt cb frame subtype %d\n", stype);
1718 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1725 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1726 char *buf, size_t buflen)
1733 void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
1734 const u8 *buf, size_t len, int ack)
1736 struct sta_info *sta;
1737 struct hostapd_iface *iface = hapd->iface;
1739 sta = ap_get_sta(hapd, addr);
1740 if (sta == NULL && iface->num_bss > 1) {
1742 for (j = 0; j < iface->num_bss; j++) {
1743 hapd = iface->bss[j];
1744 sta = ap_get_sta(hapd, addr);
1749 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC))
1751 if (sta->flags & WLAN_STA_PENDING_POLL) {
1752 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
1753 "activity poll", MAC2STR(sta->addr),
1754 ack ? "ACKed" : "did not ACK");
1756 sta->flags &= ~WLAN_STA_PENDING_POLL;
1759 ieee802_1x_tx_status(hapd, sta, buf, len, ack);
1763 void hostapd_eapol_tx_status(struct hostapd_data *hapd, const u8 *dst,
1764 const u8 *data, size_t len, int ack)
1766 struct sta_info *sta;
1767 struct hostapd_iface *iface = hapd->iface;
1769 sta = ap_get_sta(hapd, dst);
1770 if (sta == NULL && iface->num_bss > 1) {
1772 for (j = 0; j < iface->num_bss; j++) {
1773 hapd = iface->bss[j];
1774 sta = ap_get_sta(hapd, dst);
1779 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1780 wpa_printf(MSG_DEBUG, "Ignore TX status for Data frame to STA "
1781 MACSTR " that is not currently associated",
1786 ieee802_1x_eapol_tx_status(hapd, sta, data, len, ack);
1790 void hostapd_client_poll_ok(struct hostapd_data *hapd, const u8 *addr)
1792 struct sta_info *sta;
1793 struct hostapd_iface *iface = hapd->iface;
1795 sta = ap_get_sta(hapd, addr);
1796 if (sta == NULL && iface->num_bss > 1) {
1798 for (j = 0; j < iface->num_bss; j++) {
1799 hapd = iface->bss[j];
1800 sta = ap_get_sta(hapd, addr);
1807 if (!(sta->flags & WLAN_STA_PENDING_POLL))
1810 wpa_printf(MSG_DEBUG, "STA " MACSTR " ACKed pending "
1811 "activity poll", MAC2STR(sta->addr));
1812 sta->flags &= ~WLAN_STA_PENDING_POLL;
1816 void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
1819 struct sta_info *sta;
1821 sta = ap_get_sta(hapd, src);
1822 if (sta && (sta->flags & WLAN_STA_ASSOC)) {
1823 if (wds && !(sta->flags & WLAN_STA_WDS)) {
1824 wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
1825 "STA " MACSTR " (aid %u)",
1826 MAC2STR(sta->addr), sta->aid);
1827 sta->flags |= WLAN_STA_WDS;
1828 hostapd_set_wds_sta(hapd, sta->addr, sta->aid, 1);
1833 wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
1834 MACSTR, MAC2STR(src));
1835 if (src[0] & 0x01) {
1836 /* Broadcast bit set in SA?! Ignore the frame silently. */
1840 if (sta && (sta->flags & WLAN_STA_ASSOC_REQ_OK)) {
1841 wpa_printf(MSG_DEBUG, "Association Response to the STA has "
1842 "already been sent, but no TX status yet known - "
1843 "ignore Class 3 frame issue with " MACSTR,
1848 if (sta && (sta->flags & WLAN_STA_AUTH))
1849 hostapd_drv_sta_disassoc(
1851 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1853 hostapd_drv_sta_deauth(
1855 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1859 #endif /* CONFIG_NATIVE_WINDOWS */