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 "ieee802_11.h"
44 u8 * hostapd_eid_supp_rates(struct hostapd_data *hapd, u8 *eid)
49 if (hapd->iface->current_rates == NULL)
52 *pos++ = WLAN_EID_SUPP_RATES;
53 num = hapd->iface->num_rates;
55 /* rest of the rates are encoded in Extended supported
62 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num;
65 *pos = hapd->iface->current_rates[i].rate / 5;
66 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
75 u8 * hostapd_eid_ext_supp_rates(struct hostapd_data *hapd, u8 *eid)
80 if (hapd->iface->current_rates == NULL)
83 num = hapd->iface->num_rates;
88 *pos++ = WLAN_EID_EXT_SUPP_RATES;
91 for (i = 0, count = 0; i < hapd->iface->num_rates && count < num + 8;
95 continue; /* already in SuppRates IE */
96 *pos = hapd->iface->current_rates[i].rate / 5;
97 if (hapd->iface->current_rates[i].flags & HOSTAPD_RATE_BASIC)
106 u16 hostapd_own_capab_info(struct hostapd_data *hapd, struct sta_info *sta,
109 int capab = WLAN_CAPABILITY_ESS;
112 if (hapd->iface->num_sta_no_short_preamble == 0 &&
113 hapd->iconf->preamble == SHORT_PREAMBLE)
114 capab |= WLAN_CAPABILITY_SHORT_PREAMBLE;
116 privacy = hapd->conf->ssid.wep.keys_set;
118 if (hapd->conf->ieee802_1x &&
119 (hapd->conf->default_wep_key_len ||
120 hapd->conf->individual_wep_key_len))
127 int policy, def_klen;
128 if (probe && sta->ssid_probe) {
129 policy = sta->ssid_probe->security_policy;
130 def_klen = sta->ssid_probe->wep.default_len;
132 policy = sta->ssid->security_policy;
133 def_klen = sta->ssid->wep.default_len;
135 privacy = policy != SECURITY_PLAINTEXT;
136 if (policy == SECURITY_IEEE_802_1X && def_klen == 0)
141 capab |= WLAN_CAPABILITY_PRIVACY;
143 if (hapd->iface->current_mode &&
144 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G &&
145 hapd->iface->num_sta_no_short_slot_time == 0)
146 capab |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
152 #ifdef CONFIG_IEEE80211W
153 static u8 * hostapd_eid_assoc_comeback_time(struct hostapd_data *hapd,
154 struct sta_info *sta, u8 *eid)
158 struct os_time now, passed;
160 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
162 *pos++ = WLAN_TIMEOUT_ASSOC_COMEBACK;
164 os_time_sub(&now, &sta->sa_query_start, &passed);
165 tu = (passed.sec * 1000000 + passed.usec) / 1024;
166 if (hapd->conf->assoc_sa_query_max_timeout > tu)
167 timeout = hapd->conf->assoc_sa_query_max_timeout - tu;
170 if (timeout < hapd->conf->assoc_sa_query_max_timeout)
171 timeout++; /* add some extra time for local timers */
172 WPA_PUT_LE32(pos, timeout);
177 #endif /* CONFIG_IEEE80211W */
180 void ieee802_11_print_ssid(char *buf, const u8 *ssid, u8 len)
183 if (len > HOSTAPD_MAX_SSID_LEN)
184 len = HOSTAPD_MAX_SSID_LEN;
185 for (i = 0; i < len; i++) {
186 if (ssid[i] >= 32 && ssid[i] < 127)
195 static u16 auth_shared_key(struct hostapd_data *hapd, struct sta_info *sta,
196 u16 auth_transaction, const u8 *challenge,
199 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
201 "authentication (shared key, transaction %d)",
204 if (auth_transaction == 1) {
205 if (!sta->challenge) {
206 /* Generate a pseudo-random challenge */
210 sta->challenge = os_zalloc(WLAN_AUTH_CHALLENGE_LEN);
211 if (sta->challenge == NULL)
212 return WLAN_STATUS_UNSPECIFIED_FAILURE;
216 os_memcpy(key, &now, 4);
217 os_memcpy(key + 4, &r, 4);
218 rc4_skip(key, sizeof(key), 0,
219 sta->challenge, WLAN_AUTH_CHALLENGE_LEN);
224 if (auth_transaction != 3)
225 return WLAN_STATUS_UNSPECIFIED_FAILURE;
228 if (!iswep || !sta->challenge || !challenge ||
229 os_memcmp(sta->challenge, challenge, WLAN_AUTH_CHALLENGE_LEN)) {
230 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
232 "shared key authentication - invalid "
233 "challenge-response");
234 return WLAN_STATUS_CHALLENGE_FAIL;
237 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
239 "authentication OK (shared key)");
240 #ifdef IEEE80211_REQUIRE_AUTH_ACK
241 /* Station will be marked authenticated if it ACKs the
242 * authentication reply. */
244 sta->flags |= WLAN_STA_AUTH;
245 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
247 os_free(sta->challenge);
248 sta->challenge = NULL;
254 static void send_auth_reply(struct hostapd_data *hapd,
255 const u8 *dst, const u8 *bssid,
256 u16 auth_alg, u16 auth_transaction, u16 resp,
257 const u8 *ies, size_t ies_len)
259 struct ieee80211_mgmt *reply;
263 rlen = IEEE80211_HDRLEN + sizeof(reply->u.auth) + ies_len;
264 buf = os_zalloc(rlen);
268 reply = (struct ieee80211_mgmt *) buf;
269 reply->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
271 os_memcpy(reply->da, dst, ETH_ALEN);
272 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
273 os_memcpy(reply->bssid, bssid, ETH_ALEN);
275 reply->u.auth.auth_alg = host_to_le16(auth_alg);
276 reply->u.auth.auth_transaction = host_to_le16(auth_transaction);
277 reply->u.auth.status_code = host_to_le16(resp);
280 os_memcpy(reply->u.auth.variable, ies, ies_len);
282 wpa_printf(MSG_DEBUG, "authentication reply: STA=" MACSTR
283 " auth_alg=%d auth_transaction=%d resp=%d (IE len=%lu)",
284 MAC2STR(dst), auth_alg, auth_transaction,
285 resp, (unsigned long) ies_len);
286 if (hapd->drv.send_mgmt_frame(hapd, reply, rlen) < 0)
287 perror("send_auth_reply: send");
293 #ifdef CONFIG_IEEE80211R
294 static void handle_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid,
295 u16 auth_transaction, u16 status,
296 const u8 *ies, size_t ies_len)
298 struct hostapd_data *hapd = ctx;
299 struct sta_info *sta;
301 send_auth_reply(hapd, dst, bssid, WLAN_AUTH_FT, auth_transaction,
302 status, ies, ies_len);
304 if (status != WLAN_STATUS_SUCCESS)
307 sta = ap_get_sta(hapd, dst);
311 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
312 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
313 sta->flags |= WLAN_STA_AUTH;
314 mlme_authenticate_indication(hapd, sta);
316 #endif /* CONFIG_IEEE80211R */
319 static void handle_auth(struct hostapd_data *hapd,
320 const struct ieee80211_mgmt *mgmt, size_t len)
322 u16 auth_alg, auth_transaction, status_code;
323 u16 resp = WLAN_STATUS_SUCCESS;
324 struct sta_info *sta = NULL;
327 const u8 *challenge = NULL;
328 u32 session_timeout, acct_interim_interval;
330 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
331 size_t resp_ies_len = 0;
333 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
334 printf("handle_auth - too short payload (len=%lu)\n",
335 (unsigned long) len);
339 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
340 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
341 status_code = le_to_host16(mgmt->u.auth.status_code);
342 fc = le_to_host16(mgmt->frame_control);
344 if (len >= IEEE80211_HDRLEN + sizeof(mgmt->u.auth) +
345 2 + WLAN_AUTH_CHALLENGE_LEN &&
346 mgmt->u.auth.variable[0] == WLAN_EID_CHALLENGE &&
347 mgmt->u.auth.variable[1] == WLAN_AUTH_CHALLENGE_LEN)
348 challenge = &mgmt->u.auth.variable[2];
350 wpa_printf(MSG_DEBUG, "authentication: STA=" MACSTR " auth_alg=%d "
351 "auth_transaction=%d status_code=%d wep=%d%s",
352 MAC2STR(mgmt->sa), auth_alg, auth_transaction,
353 status_code, !!(fc & WLAN_FC_ISWEP),
354 challenge ? " challenge" : "");
356 if (hapd->tkip_countermeasures) {
357 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
361 if (!(((hapd->conf->auth_algs & WPA_AUTH_ALG_OPEN) &&
362 auth_alg == WLAN_AUTH_OPEN) ||
363 #ifdef CONFIG_IEEE80211R
365 (hapd->conf->wpa_key_mgmt &
366 (WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_FT_PSK)) &&
367 auth_alg == WLAN_AUTH_FT) ||
368 #endif /* CONFIG_IEEE80211R */
369 ((hapd->conf->auth_algs & WPA_AUTH_ALG_SHARED) &&
370 auth_alg == WLAN_AUTH_SHARED_KEY))) {
371 printf("Unsupported authentication algorithm (%d)\n",
373 resp = WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG;
377 if (!(auth_transaction == 1 ||
378 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 3))) {
379 printf("Unknown authentication transaction number (%d)\n",
381 resp = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
385 if (os_memcmp(mgmt->sa, hapd->own_addr, ETH_ALEN) == 0) {
386 printf("Station " MACSTR " not allowed to authenticate.\n",
388 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
392 res = hostapd_allowed_address(hapd, mgmt->sa, (u8 *) mgmt, len,
394 &acct_interim_interval, &vlan_id);
395 if (res == HOSTAPD_ACL_REJECT) {
396 printf("Station " MACSTR " not allowed to authenticate.\n",
398 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
401 if (res == HOSTAPD_ACL_PENDING) {
402 wpa_printf(MSG_DEBUG, "Authentication frame from " MACSTR
403 " waiting for an external authentication",
405 /* Authentication code will re-send the authentication frame
406 * after it has received (and cached) information from the
407 * external source. */
411 sta = ap_sta_add(hapd, mgmt->sa);
413 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
418 if (hostapd_get_vlan_id_ifname(hapd->conf->vlan,
420 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
421 HOSTAPD_LEVEL_INFO, "Invalid VLAN ID "
422 "%d received from RADIUS server",
424 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
427 sta->vlan_id = vlan_id;
428 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_RADIUS,
429 HOSTAPD_LEVEL_INFO, "VLAN ID %d", sta->vlan_id);
432 sta->flags &= ~WLAN_STA_PREAUTH;
433 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
435 if (hapd->conf->acct_interim_interval == 0 && acct_interim_interval)
436 sta->acct_interim_interval = acct_interim_interval;
437 if (res == HOSTAPD_ACL_ACCEPT_TIMEOUT)
438 ap_sta_session_timeout(hapd, sta, session_timeout);
440 ap_sta_no_session_timeout(hapd, sta);
444 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
446 "authentication OK (open system)");
447 #ifdef IEEE80211_REQUIRE_AUTH_ACK
448 /* Station will be marked authenticated if it ACKs the
449 * authentication reply. */
451 sta->flags |= WLAN_STA_AUTH;
452 wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
453 sta->auth_alg = WLAN_AUTH_OPEN;
454 mlme_authenticate_indication(hapd, sta);
457 case WLAN_AUTH_SHARED_KEY:
458 resp = auth_shared_key(hapd, sta, auth_transaction, challenge,
460 sta->auth_alg = WLAN_AUTH_SHARED_KEY;
461 mlme_authenticate_indication(hapd, sta);
462 if (sta->challenge && auth_transaction == 1) {
463 resp_ies[0] = WLAN_EID_CHALLENGE;
464 resp_ies[1] = WLAN_AUTH_CHALLENGE_LEN;
465 os_memcpy(resp_ies + 2, sta->challenge,
466 WLAN_AUTH_CHALLENGE_LEN);
467 resp_ies_len = 2 + WLAN_AUTH_CHALLENGE_LEN;
470 #ifdef CONFIG_IEEE80211R
472 sta->auth_alg = WLAN_AUTH_FT;
473 if (sta->wpa_sm == NULL)
474 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
476 if (sta->wpa_sm == NULL) {
477 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
479 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
482 wpa_ft_process_auth(sta->wpa_sm, mgmt->bssid,
483 auth_transaction, mgmt->u.auth.variable,
484 len - IEEE80211_HDRLEN -
485 sizeof(mgmt->u.auth),
486 handle_auth_ft_finish, hapd);
487 /* handle_auth_ft_finish() callback will complete auth. */
489 #endif /* CONFIG_IEEE80211R */
493 send_auth_reply(hapd, mgmt->sa, mgmt->bssid, auth_alg,
494 auth_transaction + 1, resp, resp_ies, resp_ies_len);
498 static int hostapd_get_aid(struct hostapd_data *hapd, struct sta_info *sta)
502 /* get a unique AID */
504 wpa_printf(MSG_DEBUG, " old AID %d", sta->aid);
508 for (i = 0; i < AID_WORDS; i++) {
509 if (hapd->sta_aid[i] == (u32) -1)
511 for (j = 0; j < 32; j++) {
512 if (!(hapd->sta_aid[i] & BIT(j)))
520 aid = i * 32 + j + 1;
525 hapd->sta_aid[i] |= BIT(j);
526 wpa_printf(MSG_DEBUG, " new AID %d", sta->aid);
531 static u16 check_ssid(struct hostapd_data *hapd, struct sta_info *sta,
532 const u8 *ssid_ie, size_t ssid_ie_len)
535 return WLAN_STATUS_UNSPECIFIED_FAILURE;
537 if (ssid_ie_len != hapd->conf->ssid.ssid_len ||
538 os_memcmp(ssid_ie, hapd->conf->ssid.ssid, ssid_ie_len) != 0) {
540 ieee802_11_print_ssid(ssid_txt, ssid_ie, ssid_ie_len);
541 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
543 "Station tried to associate with unknown SSID "
545 return WLAN_STATUS_UNSPECIFIED_FAILURE;
548 return WLAN_STATUS_SUCCESS;
552 static u16 check_wmm(struct hostapd_data *hapd, struct sta_info *sta,
553 const u8 *wmm_ie, size_t wmm_ie_len)
555 sta->flags &= ~WLAN_STA_WMM;
556 if (wmm_ie && hapd->conf->wmm_enabled) {
557 if (hostapd_eid_wmm_valid(hapd, wmm_ie, wmm_ie_len))
558 hostapd_logger(hapd, sta->addr,
561 "invalid WMM element in association "
564 sta->flags |= WLAN_STA_WMM;
566 return WLAN_STATUS_SUCCESS;
570 static u16 copy_supp_rates(struct hostapd_data *hapd, struct sta_info *sta,
571 struct ieee802_11_elems *elems)
573 if (!elems->supp_rates) {
574 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
576 "No supported rates element in AssocReq");
577 return WLAN_STATUS_UNSPECIFIED_FAILURE;
580 if (elems->supp_rates_len > sizeof(sta->supported_rates)) {
581 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
583 "Invalid supported rates element length %d",
584 elems->supp_rates_len);
585 return WLAN_STATUS_UNSPECIFIED_FAILURE;
588 os_memset(sta->supported_rates, 0, sizeof(sta->supported_rates));
589 os_memcpy(sta->supported_rates, elems->supp_rates,
590 elems->supp_rates_len);
591 sta->supported_rates_len = elems->supp_rates_len;
593 if (elems->ext_supp_rates) {
594 if (elems->supp_rates_len + elems->ext_supp_rates_len >
595 sizeof(sta->supported_rates)) {
596 hostapd_logger(hapd, sta->addr,
597 HOSTAPD_MODULE_IEEE80211,
599 "Invalid supported rates element length"
600 " %d+%d", elems->supp_rates_len,
601 elems->ext_supp_rates_len);
602 return WLAN_STATUS_UNSPECIFIED_FAILURE;
605 os_memcpy(sta->supported_rates + elems->supp_rates_len,
606 elems->ext_supp_rates, elems->ext_supp_rates_len);
607 sta->supported_rates_len += elems->ext_supp_rates_len;
610 return WLAN_STATUS_SUCCESS;
614 static u16 check_assoc_ies(struct hostapd_data *hapd, struct sta_info *sta,
615 const u8 *ies, size_t ies_len, int reassoc)
617 struct ieee802_11_elems elems;
622 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
623 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
624 HOSTAPD_LEVEL_INFO, "Station sent an invalid "
625 "association request");
626 return WLAN_STATUS_UNSPECIFIED_FAILURE;
629 resp = check_ssid(hapd, sta, elems.ssid, elems.ssid_len);
630 if (resp != WLAN_STATUS_SUCCESS)
632 resp = check_wmm(hapd, sta, elems.wmm, elems.wmm_len);
633 if (resp != WLAN_STATUS_SUCCESS)
635 resp = copy_supp_rates(hapd, sta, &elems);
636 if (resp != WLAN_STATUS_SUCCESS)
638 #ifdef CONFIG_IEEE80211N
639 resp = copy_sta_ht_capab(sta, elems.ht_capabilities,
640 elems.ht_capabilities_len);
641 if (resp != WLAN_STATUS_SUCCESS)
643 #endif /* CONFIG_IEEE80211N */
645 if ((hapd->conf->wpa & WPA_PROTO_RSN) && elems.rsn_ie) {
646 wpa_ie = elems.rsn_ie;
647 wpa_ie_len = elems.rsn_ie_len;
648 } else if ((hapd->conf->wpa & WPA_PROTO_WPA) &&
650 wpa_ie = elems.wpa_ie;
651 wpa_ie_len = elems.wpa_ie_len;
658 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
659 if (hapd->conf->wps_state && elems.wps_ie) {
660 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)Association "
661 "Request - assume WPS is used");
662 sta->flags |= WLAN_STA_WPS;
663 wpabuf_free(sta->wps_ie);
664 sta->wps_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
668 if (sta->wps_ie && wps_validate_assoc_req(sta->wps_ie) < 0) {
669 wpa_printf(MSG_DEBUG, "WPS: Invalid WPS IE in "
670 "(Re)Association Request - reject");
671 return WLAN_STATUS_INVALID_IE;
673 } else if (hapd->conf->wps_state && wpa_ie == NULL) {
674 wpa_printf(MSG_DEBUG, "STA did not include WPA/RSN IE in "
675 "(Re)Association Request - possible WPS use");
676 sta->flags |= WLAN_STA_MAYBE_WPS;
678 #endif /* CONFIG_WPS */
679 if (hapd->conf->wpa && wpa_ie == NULL) {
680 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
682 "No WPA/RSN IE in association request");
683 return WLAN_STATUS_INVALID_IE;
686 if (hapd->conf->wpa && wpa_ie) {
690 if (sta->wpa_sm == NULL)
691 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
693 if (sta->wpa_sm == NULL) {
694 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
696 return WLAN_STATUS_UNSPECIFIED_FAILURE;
698 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
700 elems.mdie, elems.mdie_len);
701 if (res == WPA_INVALID_GROUP)
702 resp = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
703 else if (res == WPA_INVALID_PAIRWISE)
704 resp = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
705 else if (res == WPA_INVALID_AKMP)
706 resp = WLAN_STATUS_AKMP_NOT_VALID;
707 else if (res == WPA_ALLOC_FAIL)
708 resp = WLAN_STATUS_UNSPECIFIED_FAILURE;
709 #ifdef CONFIG_IEEE80211W
710 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION)
711 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
712 else if (res == WPA_INVALID_MGMT_GROUP_CIPHER)
713 resp = WLAN_STATUS_ROBUST_MGMT_FRAME_POLICY_VIOLATION;
714 #endif /* CONFIG_IEEE80211W */
715 else if (res == WPA_INVALID_MDIE)
716 resp = WLAN_STATUS_INVALID_MDIE;
717 else if (res != WPA_IE_OK)
718 resp = WLAN_STATUS_INVALID_IE;
719 if (resp != WLAN_STATUS_SUCCESS)
721 #ifdef CONFIG_IEEE80211W
722 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
723 sta->sa_query_count > 0)
724 ap_check_sa_query_timeout(hapd, sta);
725 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
726 (!reassoc || sta->auth_alg != WLAN_AUTH_FT)) {
728 * STA has already been associated with MFP and SA
729 * Query timeout has not been reached. Reject the
730 * association attempt temporarily and start SA Query,
731 * if one is not pending.
734 if (sta->sa_query_count == 0)
735 ap_sta_start_sa_query(hapd, sta);
737 return WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
740 if (wpa_auth_uses_mfp(sta->wpa_sm))
741 sta->flags |= WLAN_STA_MFP;
743 sta->flags &= ~WLAN_STA_MFP;
744 #endif /* CONFIG_IEEE80211W */
746 #ifdef CONFIG_IEEE80211R
747 if (sta->auth_alg == WLAN_AUTH_FT) {
749 wpa_printf(MSG_DEBUG, "FT: " MACSTR " tried "
750 "to use association (not "
751 "re-association) with FT auth_alg",
753 return WLAN_STATUS_UNSPECIFIED_FAILURE;
756 resp = wpa_ft_validate_reassoc(sta->wpa_sm, ies,
758 if (resp != WLAN_STATUS_SUCCESS)
761 #endif /* CONFIG_IEEE80211R */
763 #ifdef CONFIG_IEEE80211N
764 if ((sta->flags & WLAN_STA_HT) &&
765 wpa_auth_get_pairwise(sta->wpa_sm) == WPA_CIPHER_TKIP) {
766 hostapd_logger(hapd, sta->addr,
767 HOSTAPD_MODULE_IEEE80211,
769 "Station tried to use TKIP with HT "
771 return WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
773 #endif /* CONFIG_IEEE80211N */
775 wpa_auth_sta_no_wpa(sta->wpa_sm);
779 wpabuf_free(sta->p2p_ie);
780 sta->p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
783 wpabuf_free(sta->p2p_ie);
786 #endif /* CONFIG_P2P */
788 return WLAN_STATUS_SUCCESS;
792 static void send_deauth(struct hostapd_data *hapd, const u8 *addr,
796 struct ieee80211_mgmt reply;
798 os_memset(&reply, 0, sizeof(reply));
799 reply.frame_control =
800 IEEE80211_FC(WLAN_FC_TYPE_MGMT, WLAN_FC_STYPE_DEAUTH);
801 os_memcpy(reply.da, addr, ETH_ALEN);
802 os_memcpy(reply.sa, hapd->own_addr, ETH_ALEN);
803 os_memcpy(reply.bssid, hapd->own_addr, ETH_ALEN);
805 send_len = IEEE80211_HDRLEN + sizeof(reply.u.deauth);
806 reply.u.deauth.reason_code = host_to_le16(reason_code);
808 if (hapd->drv.send_mgmt_frame(hapd, &reply, send_len) < 0)
809 wpa_printf(MSG_INFO, "Failed to send deauth: %s",
814 static void send_assoc_resp(struct hostapd_data *hapd, struct sta_info *sta,
815 u16 status_code, int reassoc, const u8 *ies,
819 u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
820 struct ieee80211_mgmt *reply;
823 os_memset(buf, 0, sizeof(buf));
824 reply = (struct ieee80211_mgmt *) buf;
825 reply->frame_control =
826 IEEE80211_FC(WLAN_FC_TYPE_MGMT,
827 (reassoc ? WLAN_FC_STYPE_REASSOC_RESP :
828 WLAN_FC_STYPE_ASSOC_RESP));
829 os_memcpy(reply->da, sta->addr, ETH_ALEN);
830 os_memcpy(reply->sa, hapd->own_addr, ETH_ALEN);
831 os_memcpy(reply->bssid, hapd->own_addr, ETH_ALEN);
833 send_len = IEEE80211_HDRLEN;
834 send_len += sizeof(reply->u.assoc_resp);
835 reply->u.assoc_resp.capab_info =
836 host_to_le16(hostapd_own_capab_info(hapd, sta, 0));
837 reply->u.assoc_resp.status_code = host_to_le16(status_code);
838 reply->u.assoc_resp.aid = host_to_le16((sta ? sta->aid : 0)
839 | BIT(14) | BIT(15));
840 /* Supported rates */
841 p = hostapd_eid_supp_rates(hapd, reply->u.assoc_resp.variable);
842 /* Extended supported rates */
843 p = hostapd_eid_ext_supp_rates(hapd, p);
845 #ifdef CONFIG_IEEE80211R
846 if (status_code == WLAN_STATUS_SUCCESS) {
847 /* IEEE 802.11r: Mobility Domain Information, Fast BSS
848 * Transition Information, RSN, [RIC Response] */
849 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, p,
850 buf + sizeof(buf) - p,
851 sta->auth_alg, ies, ies_len);
853 #endif /* CONFIG_IEEE80211R */
855 #ifdef CONFIG_IEEE80211W
856 if (status_code == WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY)
857 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
858 #endif /* CONFIG_IEEE80211W */
860 #ifdef CONFIG_IEEE80211N
861 p = hostapd_eid_ht_capabilities(hapd, p);
862 p = hostapd_eid_ht_operation(hapd, p);
863 #endif /* CONFIG_IEEE80211N */
865 if (sta->flags & WLAN_STA_WMM)
866 p = hostapd_eid_wmm(hapd, p);
869 if (sta->flags & WLAN_STA_WPS) {
870 struct wpabuf *wps = wps_build_assoc_resp_ie();
872 os_memcpy(p, wpabuf_head(wps), wpabuf_len(wps));
873 p += wpabuf_len(wps);
877 #endif /* CONFIG_WPS */
879 send_len += p - reply->u.assoc_resp.variable;
881 if (hapd->drv.send_mgmt_frame(hapd, reply, send_len) < 0)
882 wpa_printf(MSG_INFO, "Failed to send assoc resp: %s",
887 static void handle_assoc(struct hostapd_data *hapd,
888 const struct ieee80211_mgmt *mgmt, size_t len,
891 u16 capab_info, listen_interval;
892 u16 resp = WLAN_STATUS_SUCCESS;
895 struct sta_info *sta;
897 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_req) :
898 sizeof(mgmt->u.assoc_req))) {
899 printf("handle_assoc(reassoc=%d) - too short payload (len=%lu)"
900 "\n", reassoc, (unsigned long) len);
905 capab_info = le_to_host16(mgmt->u.reassoc_req.capab_info);
906 listen_interval = le_to_host16(
907 mgmt->u.reassoc_req.listen_interval);
908 wpa_printf(MSG_DEBUG, "reassociation request: STA=" MACSTR
909 " capab_info=0x%02x listen_interval=%d current_ap="
911 MAC2STR(mgmt->sa), capab_info, listen_interval,
912 MAC2STR(mgmt->u.reassoc_req.current_ap));
913 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.reassoc_req));
914 pos = mgmt->u.reassoc_req.variable;
916 capab_info = le_to_host16(mgmt->u.assoc_req.capab_info);
917 listen_interval = le_to_host16(
918 mgmt->u.assoc_req.listen_interval);
919 wpa_printf(MSG_DEBUG, "association request: STA=" MACSTR
920 " capab_info=0x%02x listen_interval=%d",
921 MAC2STR(mgmt->sa), capab_info, listen_interval);
922 left = len - (IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_req));
923 pos = mgmt->u.assoc_req.variable;
926 sta = ap_get_sta(hapd, mgmt->sa);
927 #ifdef CONFIG_IEEE80211R
928 if (sta && sta->auth_alg == WLAN_AUTH_FT &&
929 (sta->flags & WLAN_STA_AUTH) == 0) {
930 wpa_printf(MSG_DEBUG, "FT: Allow STA " MACSTR " to associate "
931 "prior to authentication since it is using "
932 "over-the-DS FT", MAC2STR(mgmt->sa));
934 #endif /* CONFIG_IEEE80211R */
935 if (sta == NULL || (sta->flags & WLAN_STA_AUTH) == 0) {
936 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
937 HOSTAPD_LEVEL_INFO, "Station tried to "
938 "associate before authentication "
939 "(aid=%d flags=0x%x)",
941 sta ? sta->flags : 0);
942 send_deauth(hapd, mgmt->sa,
943 WLAN_REASON_CLASS2_FRAME_FROM_NONAUTH_STA);
947 if (hapd->tkip_countermeasures) {
948 resp = WLAN_REASON_MICHAEL_MIC_FAILURE;
952 if (listen_interval > hapd->conf->max_listen_interval) {
953 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
955 "Too large Listen Interval (%d)",
957 resp = WLAN_STATUS_ASSOC_DENIED_LISTEN_INT_TOO_LARGE;
961 /* followed by SSID and Supported rates; and HT capabilities if 802.11n
963 resp = check_assoc_ies(hapd, sta, pos, left, reassoc);
964 if (resp != WLAN_STATUS_SUCCESS)
967 if (hostapd_get_aid(hapd, sta) < 0) {
968 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
969 HOSTAPD_LEVEL_INFO, "No room for more AIDs");
970 resp = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
974 sta->capability = capab_info;
975 sta->listen_interval = listen_interval;
977 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G)
978 sta->flags |= WLAN_STA_NONERP;
979 for (i = 0; i < sta->supported_rates_len; i++) {
980 if ((sta->supported_rates[i] & 0x7f) > 22) {
981 sta->flags &= ~WLAN_STA_NONERP;
985 if (sta->flags & WLAN_STA_NONERP && !sta->nonerp_set) {
987 hapd->iface->num_sta_non_erp++;
988 if (hapd->iface->num_sta_non_erp == 1)
989 ieee802_11_set_beacons(hapd->iface);
992 if (!(sta->capability & WLAN_CAPABILITY_SHORT_SLOT_TIME) &&
993 !sta->no_short_slot_time_set) {
994 sta->no_short_slot_time_set = 1;
995 hapd->iface->num_sta_no_short_slot_time++;
996 if (hapd->iface->current_mode->mode ==
997 HOSTAPD_MODE_IEEE80211G &&
998 hapd->iface->num_sta_no_short_slot_time == 1)
999 ieee802_11_set_beacons(hapd->iface);
1002 if (sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
1003 sta->flags |= WLAN_STA_SHORT_PREAMBLE;
1005 sta->flags &= ~WLAN_STA_SHORT_PREAMBLE;
1007 if (!(sta->capability & WLAN_CAPABILITY_SHORT_PREAMBLE) &&
1008 !sta->no_short_preamble_set) {
1009 sta->no_short_preamble_set = 1;
1010 hapd->iface->num_sta_no_short_preamble++;
1011 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
1012 && hapd->iface->num_sta_no_short_preamble == 1)
1013 ieee802_11_set_beacons(hapd->iface);
1016 #ifdef CONFIG_IEEE80211N
1017 update_ht_state(hapd, sta);
1018 #endif /* CONFIG_IEEE80211N */
1020 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1021 HOSTAPD_LEVEL_DEBUG,
1022 "association OK (aid %d)", sta->aid);
1023 /* Station will be marked associated, after it acknowledges AssocResp
1026 #ifdef CONFIG_IEEE80211W
1027 if ((sta->flags & WLAN_STA_MFP) && sta->sa_query_timed_out) {
1028 wpa_printf(MSG_DEBUG, "Allowing %sassociation after timed out "
1029 "SA Query procedure", reassoc ? "re" : "");
1030 /* TODO: Send a protected Disassociate frame to the STA using
1031 * the old key and Reason Code "Previous Authentication no
1032 * longer valid". Make sure this is only sent protected since
1033 * unprotected frame would be received by the STA that is now
1034 * trying to associate.
1037 #endif /* CONFIG_IEEE80211W */
1040 os_memcpy(sta->previous_ap, mgmt->u.reassoc_req.current_ap,
1044 if (sta->last_assoc_req)
1045 os_free(sta->last_assoc_req);
1046 sta->last_assoc_req = os_malloc(len);
1047 if (sta->last_assoc_req)
1048 os_memcpy(sta->last_assoc_req, mgmt, len);
1050 /* Make sure that the previously registered inactivity timer will not
1051 * remove the STA immediately. */
1052 sta->timeout_next = STA_NULLFUNC;
1055 send_assoc_resp(hapd, sta, resp, reassoc, pos, left);
1059 static void handle_disassoc(struct hostapd_data *hapd,
1060 const struct ieee80211_mgmt *mgmt, size_t len)
1062 struct sta_info *sta;
1064 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.disassoc)) {
1065 printf("handle_disassoc - too short payload (len=%lu)\n",
1066 (unsigned long) len);
1070 wpa_printf(MSG_DEBUG, "disassocation: STA=" MACSTR " reason_code=%d",
1072 le_to_host16(mgmt->u.disassoc.reason_code));
1074 sta = ap_get_sta(hapd, mgmt->sa);
1076 printf("Station " MACSTR " trying to disassociate, but it "
1077 "is not associated.\n", MAC2STR(mgmt->sa));
1081 sta->flags &= ~WLAN_STA_ASSOC;
1082 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
1083 MAC2STR(sta->addr));
1084 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
1085 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1086 HOSTAPD_LEVEL_INFO, "disassociated");
1087 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1088 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1089 /* Stop Accounting and IEEE 802.1X sessions, but leave the STA
1091 accounting_sta_stop(hapd, sta);
1092 ieee802_1x_free_station(sta);
1093 hapd->drv.sta_remove(hapd, sta->addr);
1095 if (sta->timeout_next == STA_NULLFUNC ||
1096 sta->timeout_next == STA_DISASSOC) {
1097 sta->timeout_next = STA_DEAUTH;
1098 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1099 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
1103 mlme_disassociate_indication(
1104 hapd, sta, le_to_host16(mgmt->u.disassoc.reason_code));
1108 static void handle_deauth(struct hostapd_data *hapd,
1109 const struct ieee80211_mgmt *mgmt, size_t len)
1111 struct sta_info *sta;
1113 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.deauth)) {
1114 printf("handle_deauth - too short payload (len=%lu)\n",
1115 (unsigned long) len);
1119 wpa_printf(MSG_DEBUG, "deauthentication: STA=" MACSTR
1122 le_to_host16(mgmt->u.deauth.reason_code));
1124 sta = ap_get_sta(hapd, mgmt->sa);
1126 printf("Station " MACSTR " trying to deauthenticate, but it "
1127 "is not authenticated.\n", MAC2STR(mgmt->sa));
1131 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1132 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
1133 MAC2STR(sta->addr));
1134 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1135 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1136 HOSTAPD_LEVEL_DEBUG, "deauthenticated");
1137 mlme_deauthenticate_indication(
1138 hapd, sta, le_to_host16(mgmt->u.deauth.reason_code));
1139 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
1140 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1141 ap_free_sta(hapd, sta);
1145 static void handle_beacon(struct hostapd_data *hapd,
1146 const struct ieee80211_mgmt *mgmt, size_t len,
1147 struct hostapd_frame_info *fi)
1149 struct ieee802_11_elems elems;
1151 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.beacon)) {
1152 printf("handle_beacon - too short payload (len=%lu)\n",
1153 (unsigned long) len);
1157 (void) ieee802_11_parse_elems(mgmt->u.beacon.variable,
1158 len - (IEEE80211_HDRLEN +
1159 sizeof(mgmt->u.beacon)), &elems,
1162 ap_list_process_beacon(hapd->iface, mgmt, &elems, fi);
1166 #ifdef CONFIG_IEEE80211W
1168 /* MLME-SAQuery.request */
1169 void ieee802_11_send_sa_query_req(struct hostapd_data *hapd,
1170 const u8 *addr, const u8 *trans_id)
1172 struct ieee80211_mgmt mgmt;
1175 wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Request to "
1176 MACSTR, MAC2STR(addr));
1177 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
1178 trans_id, WLAN_SA_QUERY_TR_ID_LEN);
1180 os_memset(&mgmt, 0, sizeof(mgmt));
1181 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1182 WLAN_FC_STYPE_ACTION);
1183 os_memcpy(mgmt.da, addr, ETH_ALEN);
1184 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
1185 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
1186 mgmt.u.action.category = WLAN_ACTION_SA_QUERY;
1187 mgmt.u.action.u.sa_query_req.action = WLAN_SA_QUERY_REQUEST;
1188 os_memcpy(mgmt.u.action.u.sa_query_req.trans_id, trans_id,
1189 WLAN_SA_QUERY_TR_ID_LEN);
1190 end = mgmt.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
1191 if (hapd->drv.send_mgmt_frame(hapd, &mgmt, end - (u8 *) &mgmt) < 0)
1192 perror("ieee802_11_send_sa_query_req: send");
1196 static void hostapd_sa_query_request(struct hostapd_data *hapd,
1197 const struct ieee80211_mgmt *mgmt)
1199 struct sta_info *sta;
1200 struct ieee80211_mgmt resp;
1203 wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Request from "
1204 MACSTR, MAC2STR(mgmt->sa));
1205 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
1206 mgmt->u.action.u.sa_query_resp.trans_id,
1207 WLAN_SA_QUERY_TR_ID_LEN);
1209 sta = ap_get_sta(hapd, mgmt->sa);
1210 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1211 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignore SA Query Request "
1212 "from unassociated STA " MACSTR, MAC2STR(mgmt->sa));
1216 wpa_printf(MSG_DEBUG, "IEEE 802.11: Sending SA Query Response to "
1217 MACSTR, MAC2STR(mgmt->sa));
1219 os_memset(&resp, 0, sizeof(resp));
1220 resp.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1221 WLAN_FC_STYPE_ACTION);
1222 os_memcpy(resp.da, mgmt->sa, ETH_ALEN);
1223 os_memcpy(resp.sa, hapd->own_addr, ETH_ALEN);
1224 os_memcpy(resp.bssid, hapd->own_addr, ETH_ALEN);
1225 resp.u.action.category = WLAN_ACTION_SA_QUERY;
1226 resp.u.action.u.sa_query_req.action = WLAN_SA_QUERY_RESPONSE;
1227 os_memcpy(resp.u.action.u.sa_query_req.trans_id,
1228 mgmt->u.action.u.sa_query_req.trans_id,
1229 WLAN_SA_QUERY_TR_ID_LEN);
1230 end = resp.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
1231 if (hapd->drv.send_mgmt_frame(hapd, &resp, end - (u8 *) &resp) < 0)
1232 perror("hostapd_sa_query_request: send");
1236 static void hostapd_sa_query_action(struct hostapd_data *hapd,
1237 const struct ieee80211_mgmt *mgmt,
1240 struct sta_info *sta;
1244 end = mgmt->u.action.u.sa_query_resp.trans_id +
1245 WLAN_SA_QUERY_TR_ID_LEN;
1246 if (((u8 *) mgmt) + len < end) {
1247 wpa_printf(MSG_DEBUG, "IEEE 802.11: Too short SA Query Action "
1248 "frame (len=%lu)", (unsigned long) len);
1252 if (mgmt->u.action.u.sa_query_resp.action == WLAN_SA_QUERY_REQUEST) {
1253 hostapd_sa_query_request(hapd, mgmt);
1257 if (mgmt->u.action.u.sa_query_resp.action != WLAN_SA_QUERY_RESPONSE) {
1258 wpa_printf(MSG_DEBUG, "IEEE 802.11: Unexpected SA Query "
1259 "Action %d", mgmt->u.action.u.sa_query_resp.action);
1263 wpa_printf(MSG_DEBUG, "IEEE 802.11: Received SA Query Response from "
1264 MACSTR, MAC2STR(mgmt->sa));
1265 wpa_hexdump(MSG_DEBUG, "IEEE 802.11: SA Query Transaction ID",
1266 mgmt->u.action.u.sa_query_resp.trans_id,
1267 WLAN_SA_QUERY_TR_ID_LEN);
1269 /* MLME-SAQuery.confirm */
1271 sta = ap_get_sta(hapd, mgmt->sa);
1272 if (sta == NULL || sta->sa_query_trans_id == NULL) {
1273 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching STA with "
1274 "pending SA Query request found");
1278 for (i = 0; i < sta->sa_query_count; i++) {
1279 if (os_memcmp(sta->sa_query_trans_id +
1280 i * WLAN_SA_QUERY_TR_ID_LEN,
1281 mgmt->u.action.u.sa_query_resp.trans_id,
1282 WLAN_SA_QUERY_TR_ID_LEN) == 0)
1286 if (i >= sta->sa_query_count) {
1287 wpa_printf(MSG_DEBUG, "IEEE 802.11: No matching SA Query "
1288 "transaction identifier found");
1292 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1293 HOSTAPD_LEVEL_DEBUG,
1294 "Reply to pending SA Query received");
1295 ap_sta_stop_sa_query(hapd, sta);
1299 static int robust_action_frame(u8 category)
1301 return category != WLAN_ACTION_PUBLIC &&
1302 category != WLAN_ACTION_HT;
1304 #endif /* CONFIG_IEEE80211W */
1307 static void handle_action(struct hostapd_data *hapd,
1308 const struct ieee80211_mgmt *mgmt, size_t len)
1310 struct sta_info *sta;
1312 if (len < IEEE80211_HDRLEN + 1) {
1313 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1314 HOSTAPD_LEVEL_DEBUG,
1315 "handle_action - too short payload (len=%lu)",
1316 (unsigned long) len);
1320 sta = ap_get_sta(hapd, mgmt->sa);
1321 #ifdef CONFIG_IEEE80211W
1322 if (sta && (sta->flags & WLAN_STA_MFP) &&
1323 !(mgmt->frame_control & host_to_le16(WLAN_FC_ISWEP) &&
1324 robust_action_frame(mgmt->u.action.category))) {
1325 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1326 HOSTAPD_LEVEL_DEBUG,
1327 "Dropped unprotected Robust Action frame from "
1331 #endif /* CONFIG_IEEE80211W */
1333 switch (mgmt->u.action.category) {
1334 #ifdef CONFIG_IEEE80211R
1335 case WLAN_ACTION_FT:
1337 if (sta == NULL || !(sta->flags & WLAN_STA_ASSOC)) {
1338 wpa_printf(MSG_DEBUG, "IEEE 802.11: Ignored FT Action "
1339 "frame from unassociated STA " MACSTR,
1344 if (wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action,
1345 len - IEEE80211_HDRLEN))
1350 #endif /* CONFIG_IEEE80211R */
1351 case WLAN_ACTION_WMM:
1352 hostapd_wmm_action(hapd, mgmt, len);
1354 #ifdef CONFIG_IEEE80211W
1355 case WLAN_ACTION_SA_QUERY:
1356 hostapd_sa_query_action(hapd, mgmt, len);
1358 #endif /* CONFIG_IEEE80211W */
1359 case WLAN_ACTION_PUBLIC:
1360 if (hapd->public_action_cb) {
1361 hapd->public_action_cb(hapd->public_action_cb_ctx,
1367 case WLAN_ACTION_VENDOR_SPECIFIC:
1368 if (hapd->vendor_action_cb) {
1369 if (hapd->vendor_action_cb(hapd->vendor_action_cb_ctx,
1371 hapd->iface->freq) == 0)
1377 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1378 HOSTAPD_LEVEL_DEBUG,
1379 "handle_action - unknown action category %d or invalid "
1381 mgmt->u.action.category);
1382 if (!(mgmt->da[0] & 0x01) && !(mgmt->u.action.category & 0x80) &&
1383 !(mgmt->sa[0] & 0x01)) {
1384 struct ieee80211_mgmt *resp;
1387 * IEEE 802.11-REVma/D9.0 - 7.3.1.11
1388 * Return the Action frame to the source without change
1389 * except that MSB of the Category set to 1.
1391 wpa_printf(MSG_DEBUG, "IEEE 802.11: Return unknown Action "
1392 "frame back to sender");
1393 resp = os_malloc(len);
1396 os_memcpy(resp, mgmt, len);
1397 os_memcpy(resp->da, resp->sa, ETH_ALEN);
1398 os_memcpy(resp->sa, hapd->own_addr, ETH_ALEN);
1399 os_memcpy(resp->bssid, hapd->own_addr, ETH_ALEN);
1400 resp->u.action.category |= 0x80;
1402 hapd->drv.send_mgmt_frame(hapd, resp, len);
1409 * ieee802_11_mgmt - process incoming IEEE 802.11 management frames
1410 * @hapd: hostapd BSS data structure (the BSS to which the management frame was
1412 * @buf: management frame data (starting from IEEE 802.11 header)
1413 * @len: length of frame data in octets
1414 * @fi: meta data about received frame (signal level, etc.)
1416 * Process all incoming IEEE 802.11 management frames. This will be called for
1417 * each frame received from the kernel driver through wlan#ap interface. In
1418 * addition, it can be called to re-inserted pending frames (e.g., when using
1419 * external RADIUS server as an MAC ACL).
1421 void ieee802_11_mgmt(struct hostapd_data *hapd, const u8 *buf, size_t len,
1422 struct hostapd_frame_info *fi)
1424 struct ieee80211_mgmt *mgmt;
1428 mgmt = (struct ieee80211_mgmt *) buf;
1429 fc = le_to_host16(mgmt->frame_control);
1430 stype = WLAN_FC_GET_STYPE(fc);
1432 if (stype == WLAN_FC_STYPE_BEACON) {
1433 handle_beacon(hapd, mgmt, len, fi);
1437 broadcast = mgmt->bssid[0] == 0xff && mgmt->bssid[1] == 0xff &&
1438 mgmt->bssid[2] == 0xff && mgmt->bssid[3] == 0xff &&
1439 mgmt->bssid[4] == 0xff && mgmt->bssid[5] == 0xff;
1442 os_memcmp(mgmt->bssid, hapd->own_addr, ETH_ALEN) != 0) {
1443 printf("MGMT: BSSID=" MACSTR " not our address\n",
1444 MAC2STR(mgmt->bssid));
1449 if (stype == WLAN_FC_STYPE_PROBE_REQ) {
1450 handle_probe_req(hapd, mgmt, len);
1454 if (os_memcmp(mgmt->da, hapd->own_addr, ETH_ALEN) != 0) {
1455 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1456 HOSTAPD_LEVEL_DEBUG,
1457 "MGMT: DA=" MACSTR " not our address",
1463 case WLAN_FC_STYPE_AUTH:
1464 wpa_printf(MSG_DEBUG, "mgmt::auth");
1465 handle_auth(hapd, mgmt, len);
1467 case WLAN_FC_STYPE_ASSOC_REQ:
1468 wpa_printf(MSG_DEBUG, "mgmt::assoc_req");
1469 handle_assoc(hapd, mgmt, len, 0);
1471 case WLAN_FC_STYPE_REASSOC_REQ:
1472 wpa_printf(MSG_DEBUG, "mgmt::reassoc_req");
1473 handle_assoc(hapd, mgmt, len, 1);
1475 case WLAN_FC_STYPE_DISASSOC:
1476 wpa_printf(MSG_DEBUG, "mgmt::disassoc");
1477 handle_disassoc(hapd, mgmt, len);
1479 case WLAN_FC_STYPE_DEAUTH:
1480 wpa_printf(MSG_DEBUG, "mgmt::deauth");
1481 handle_deauth(hapd, mgmt, len);
1483 case WLAN_FC_STYPE_ACTION:
1484 wpa_printf(MSG_DEBUG, "mgmt::action");
1485 handle_action(hapd, mgmt, len);
1488 hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
1489 HOSTAPD_LEVEL_DEBUG,
1490 "unknown mgmt frame subtype %d", stype);
1496 static void handle_auth_cb(struct hostapd_data *hapd,
1497 const struct ieee80211_mgmt *mgmt,
1500 u16 auth_alg, auth_transaction, status_code;
1501 struct sta_info *sta;
1504 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1505 HOSTAPD_LEVEL_NOTICE,
1506 "did not acknowledge authentication response");
1510 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.auth)) {
1511 printf("handle_auth_cb - too short payload (len=%lu)\n",
1512 (unsigned long) len);
1516 auth_alg = le_to_host16(mgmt->u.auth.auth_alg);
1517 auth_transaction = le_to_host16(mgmt->u.auth.auth_transaction);
1518 status_code = le_to_host16(mgmt->u.auth.status_code);
1520 sta = ap_get_sta(hapd, mgmt->da);
1522 printf("handle_auth_cb: STA " MACSTR " not found\n",
1527 if (status_code == WLAN_STATUS_SUCCESS &&
1528 ((auth_alg == WLAN_AUTH_OPEN && auth_transaction == 2) ||
1529 (auth_alg == WLAN_AUTH_SHARED_KEY && auth_transaction == 4))) {
1530 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1531 HOSTAPD_LEVEL_INFO, "authenticated");
1532 sta->flags |= WLAN_STA_AUTH;
1537 static void handle_assoc_cb(struct hostapd_data *hapd,
1538 const struct ieee80211_mgmt *mgmt,
1539 size_t len, int reassoc, int ok)
1542 struct sta_info *sta;
1544 struct ieee80211_ht_capabilities ht_cap;
1547 hostapd_logger(hapd, mgmt->da, HOSTAPD_MODULE_IEEE80211,
1548 HOSTAPD_LEVEL_DEBUG,
1549 "did not acknowledge association response");
1553 if (len < IEEE80211_HDRLEN + (reassoc ? sizeof(mgmt->u.reassoc_resp) :
1554 sizeof(mgmt->u.assoc_resp))) {
1555 printf("handle_assoc_cb(reassoc=%d) - too short payload "
1556 "(len=%lu)\n", reassoc, (unsigned long) len);
1561 status = le_to_host16(mgmt->u.reassoc_resp.status_code);
1563 status = le_to_host16(mgmt->u.assoc_resp.status_code);
1565 sta = ap_get_sta(hapd, mgmt->da);
1567 printf("handle_assoc_cb: STA " MACSTR " not found\n",
1572 if (status != WLAN_STATUS_SUCCESS)
1575 /* Stop previous accounting session, if one is started, and allocate
1576 * new session id for the new session. */
1577 accounting_sta_stop(hapd, sta);
1579 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1581 "associated (aid %d)",
1584 if (sta->flags & WLAN_STA_ASSOC)
1586 sta->flags |= WLAN_STA_ASSOC;
1587 if ((!hapd->conf->ieee802_1x && !hapd->conf->wpa) ||
1588 sta->auth_alg == WLAN_AUTH_FT) {
1590 * Open, static WEP, or FT protocol; no separate authorization
1593 sta->flags |= WLAN_STA_AUTHORIZED;
1594 wpa_msg(hapd->msg_ctx, MSG_INFO,
1595 AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
1599 mlme_reassociate_indication(hapd, sta);
1601 mlme_associate_indication(hapd, sta);
1603 #ifdef CONFIG_IEEE80211W
1604 sta->sa_query_timed_out = 0;
1605 #endif /* CONFIG_IEEE80211W */
1608 * Remove the STA entry in order to make sure the STA PS state gets
1609 * cleared and configuration gets updated in case of reassociation back
1612 hapd->drv.sta_remove(hapd, sta->addr);
1614 #ifdef CONFIG_IEEE80211N
1615 if (sta->flags & WLAN_STA_HT)
1616 hostapd_get_ht_capab(hapd, sta->ht_capabilities, &ht_cap);
1617 #endif /* CONFIG_IEEE80211N */
1619 if (hapd->drv.sta_add(hapd, sta->addr, sta->aid, sta->capability,
1620 sta->supported_rates, sta->supported_rates_len,
1621 sta->listen_interval,
1622 sta->flags & WLAN_STA_HT ? &ht_cap : NULL)) {
1623 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1624 HOSTAPD_LEVEL_NOTICE,
1625 "Could not add STA to kernel driver");
1628 if (sta->eapol_sm == NULL) {
1630 * This STA does not use RADIUS server for EAP authentication,
1631 * so bind it to the selected VLAN interface now, since the
1632 * interface selection is not going to change anymore.
1634 if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
1636 } else if (sta->vlan_id) {
1637 /* VLAN ID already set (e.g., by PMKSA caching), so bind STA */
1638 if (ap_sta_bind_vlan(hapd, sta, 0) < 0)
1642 hapd->drv.set_sta_flags(hapd, sta);
1644 if (sta->auth_alg == WLAN_AUTH_FT)
1645 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
1647 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
1648 hapd->new_assoc_sta_cb(hapd, sta, !new_assoc);
1650 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
1653 /* Copy of the association request is not needed anymore */
1654 if (sta->last_assoc_req) {
1655 os_free(sta->last_assoc_req);
1656 sta->last_assoc_req = NULL;
1662 * ieee802_11_mgmt_cb - Process management frame TX status callback
1663 * @hapd: hostapd BSS data structure (the BSS from which the management frame
1665 * @buf: management frame data (starting from IEEE 802.11 header)
1666 * @len: length of frame data in octets
1667 * @stype: management frame subtype from frame control field
1668 * @ok: Whether the frame was ACK'ed
1670 void ieee802_11_mgmt_cb(struct hostapd_data *hapd, const u8 *buf, size_t len,
1673 const struct ieee80211_mgmt *mgmt;
1674 mgmt = (const struct ieee80211_mgmt *) buf;
1677 case WLAN_FC_STYPE_AUTH:
1678 wpa_printf(MSG_DEBUG, "mgmt::auth cb");
1679 handle_auth_cb(hapd, mgmt, len, ok);
1681 case WLAN_FC_STYPE_ASSOC_RESP:
1682 wpa_printf(MSG_DEBUG, "mgmt::assoc_resp cb");
1683 handle_assoc_cb(hapd, mgmt, len, 0, ok);
1685 case WLAN_FC_STYPE_REASSOC_RESP:
1686 wpa_printf(MSG_DEBUG, "mgmt::reassoc_resp cb");
1687 handle_assoc_cb(hapd, mgmt, len, 1, ok);
1689 case WLAN_FC_STYPE_PROBE_RESP:
1690 wpa_printf(MSG_EXCESSIVE, "mgmt::proberesp cb");
1692 case WLAN_FC_STYPE_DEAUTH:
1695 case WLAN_FC_STYPE_ACTION:
1696 wpa_printf(MSG_DEBUG, "mgmt::action cb");
1699 printf("unknown mgmt cb frame subtype %d\n", stype);
1705 int ieee802_11_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
1712 int ieee802_11_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
1713 char *buf, size_t buflen)
1720 void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
1721 const u8 *buf, size_t len, int ack)
1723 struct sta_info *sta;
1724 struct hostapd_iface *iface = hapd->iface;
1726 sta = ap_get_sta(hapd, addr);
1727 if (sta == NULL && iface->num_bss > 1) {
1729 for (j = 0; j < iface->num_bss; j++) {
1730 hapd = iface->bss[j];
1731 sta = ap_get_sta(hapd, addr);
1738 if (sta->flags & WLAN_STA_PENDING_POLL) {
1739 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
1740 "activity poll", MAC2STR(sta->addr),
1741 ack ? "ACKed" : "did not ACK");
1743 sta->flags &= ~WLAN_STA_PENDING_POLL;
1746 ieee802_1x_tx_status(hapd, sta, buf, len, ack);
1750 void ieee802_11_rx_from_unknown(struct hostapd_data *hapd, const u8 *src,
1753 struct sta_info *sta;
1755 sta = ap_get_sta(hapd, src);
1756 if (sta && (sta->flags & WLAN_STA_ASSOC)) {
1757 if (wds && !(sta->flags & WLAN_STA_WDS)) {
1758 wpa_printf(MSG_DEBUG, "Enable 4-address WDS mode for "
1759 "STA " MACSTR " (aid %u)",
1760 MAC2STR(sta->addr), sta->aid);
1761 sta->flags |= WLAN_STA_WDS;
1762 hapd->drv.set_wds_sta(hapd, sta->addr, sta->aid, 1);
1767 wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated STA "
1768 MACSTR, MAC2STR(src));
1769 if (sta && (sta->flags & WLAN_STA_AUTH))
1770 hapd->drv.sta_disassoc(
1772 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1774 hapd->drv.sta_deauth(
1776 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
1780 #endif /* CONFIG_NATIVE_WINDOWS */