xref: /freebsd/contrib/wpa/src/ap/ieee802_11_ht.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1e28a4053SRui Paulo /*
2e28a4053SRui Paulo  * hostapd / IEEE 802.11n HT
3e28a4053SRui Paulo  * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
4e28a4053SRui Paulo  * Copyright (c) 2007-2008, Intel Corporation
5e28a4053SRui Paulo  *
65b9c547cSRui Paulo  * This software may be distributed under the terms of the BSD license.
75b9c547cSRui Paulo  * See README for more details.
8e28a4053SRui Paulo  */
9e28a4053SRui Paulo 
10e28a4053SRui Paulo #include "utils/includes.h"
11e28a4053SRui Paulo 
12e28a4053SRui Paulo #include "utils/common.h"
135b9c547cSRui Paulo #include "utils/eloop.h"
14e28a4053SRui Paulo #include "common/ieee802_11_defs.h"
15e28a4053SRui Paulo #include "hostapd.h"
16e28a4053SRui Paulo #include "ap_config.h"
17e28a4053SRui Paulo #include "sta_info.h"
18e28a4053SRui Paulo #include "beacon.h"
19e28a4053SRui Paulo #include "ieee802_11.h"
205b9c547cSRui Paulo #include "hw_features.h"
215b9c547cSRui Paulo #include "ap_drv_ops.h"
22e28a4053SRui Paulo 
23e28a4053SRui Paulo 
hostapd_eid_ht_capabilities(struct hostapd_data * hapd,u8 * eid)24e28a4053SRui Paulo u8 * hostapd_eid_ht_capabilities(struct hostapd_data *hapd, u8 *eid)
25e28a4053SRui Paulo {
26e28a4053SRui Paulo 	struct ieee80211_ht_capabilities *cap;
27e28a4053SRui Paulo 	u8 *pos = eid;
28e28a4053SRui Paulo 
29f05cddf9SRui Paulo 	if (!hapd->iconf->ieee80211n || !hapd->iface->current_mode ||
30c1d255d3SCy Schubert 	    hapd->conf->disable_11n || is_6ghz_op_class(hapd->iconf->op_class))
31e28a4053SRui Paulo 		return eid;
32e28a4053SRui Paulo 
33e28a4053SRui Paulo 	*pos++ = WLAN_EID_HT_CAP;
34e28a4053SRui Paulo 	*pos++ = sizeof(*cap);
35e28a4053SRui Paulo 
36e28a4053SRui Paulo 	cap = (struct ieee80211_ht_capabilities *) pos;
37e28a4053SRui Paulo 	os_memset(cap, 0, sizeof(*cap));
38e28a4053SRui Paulo 	cap->ht_capabilities_info = host_to_le16(hapd->iconf->ht_capab);
39e28a4053SRui Paulo 	cap->a_mpdu_params = hapd->iface->current_mode->a_mpdu_params;
40e28a4053SRui Paulo 	os_memcpy(cap->supported_mcs_set, hapd->iface->current_mode->mcs_set,
41e28a4053SRui Paulo 		  16);
42e28a4053SRui Paulo 
43e28a4053SRui Paulo 	/* TODO: ht_extended_capabilities (now fully disabled) */
44e28a4053SRui Paulo 	/* TODO: tx_bf_capability_info (now fully disabled) */
45e28a4053SRui Paulo 	/* TODO: asel_capabilities (now fully disabled) */
46e28a4053SRui Paulo 
47e28a4053SRui Paulo  	pos += sizeof(*cap);
48e28a4053SRui Paulo 
495b9c547cSRui Paulo 	if (hapd->iconf->obss_interval) {
505b9c547cSRui Paulo 		struct ieee80211_obss_scan_parameters *scan_params;
515b9c547cSRui Paulo 
525b9c547cSRui Paulo 		*pos++ = WLAN_EID_OVERLAPPING_BSS_SCAN_PARAMS;
535b9c547cSRui Paulo 		*pos++ = sizeof(*scan_params);
545b9c547cSRui Paulo 
555b9c547cSRui Paulo 		scan_params = (struct ieee80211_obss_scan_parameters *) pos;
565b9c547cSRui Paulo 		os_memset(scan_params, 0, sizeof(*scan_params));
575b9c547cSRui Paulo 		scan_params->width_trigger_scan_interval =
585b9c547cSRui Paulo 			host_to_le16(hapd->iconf->obss_interval);
595b9c547cSRui Paulo 
605b9c547cSRui Paulo 		/* Fill in default values for remaining parameters
615b9c547cSRui Paulo 		 * (IEEE Std 802.11-2012, 8.4.2.61 and MIB defval) */
625b9c547cSRui Paulo 		scan_params->scan_passive_dwell =
635b9c547cSRui Paulo 			host_to_le16(20);
645b9c547cSRui Paulo 		scan_params->scan_active_dwell =
655b9c547cSRui Paulo 			host_to_le16(10);
665b9c547cSRui Paulo 		scan_params->scan_passive_total_per_channel =
675b9c547cSRui Paulo 			host_to_le16(200);
685b9c547cSRui Paulo 		scan_params->scan_active_total_per_channel =
695b9c547cSRui Paulo 			host_to_le16(20);
705b9c547cSRui Paulo 		scan_params->channel_transition_delay_factor =
715b9c547cSRui Paulo 			host_to_le16(5);
725b9c547cSRui Paulo 		scan_params->scan_activity_threshold =
735b9c547cSRui Paulo 			host_to_le16(25);
745b9c547cSRui Paulo 
755b9c547cSRui Paulo 		pos += sizeof(*scan_params);
765b9c547cSRui Paulo 	}
775b9c547cSRui Paulo 
78e28a4053SRui Paulo 	return pos;
79e28a4053SRui Paulo }
80e28a4053SRui Paulo 
81e28a4053SRui Paulo 
hostapd_eid_ht_operation(struct hostapd_data * hapd,u8 * eid)82e28a4053SRui Paulo u8 * hostapd_eid_ht_operation(struct hostapd_data *hapd, u8 *eid)
83e28a4053SRui Paulo {
84e28a4053SRui Paulo 	struct ieee80211_ht_operation *oper;
85e28a4053SRui Paulo 	u8 *pos = eid;
86e28a4053SRui Paulo 
87c1d255d3SCy Schubert 	if (!hapd->iconf->ieee80211n || hapd->conf->disable_11n ||
88c1d255d3SCy Schubert 	    is_6ghz_op_class(hapd->iconf->op_class))
89e28a4053SRui Paulo 		return eid;
90e28a4053SRui Paulo 
91e28a4053SRui Paulo 	*pos++ = WLAN_EID_HT_OPERATION;
92e28a4053SRui Paulo 	*pos++ = sizeof(*oper);
93e28a4053SRui Paulo 
94e28a4053SRui Paulo 	oper = (struct ieee80211_ht_operation *) pos;
95e28a4053SRui Paulo 	os_memset(oper, 0, sizeof(*oper));
96e28a4053SRui Paulo 
975b9c547cSRui Paulo 	oper->primary_chan = hapd->iconf->channel;
98e28a4053SRui Paulo 	oper->operation_mode = host_to_le16(hapd->iface->ht_op_mode);
99e28a4053SRui Paulo 	if (hapd->iconf->secondary_channel == 1)
100e28a4053SRui Paulo 		oper->ht_param |= HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE |
1015b9c547cSRui Paulo 			HT_INFO_HT_PARAM_STA_CHNL_WIDTH;
102e28a4053SRui Paulo 	if (hapd->iconf->secondary_channel == -1)
103e28a4053SRui Paulo 		oper->ht_param |= HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW |
1045b9c547cSRui Paulo 			HT_INFO_HT_PARAM_STA_CHNL_WIDTH;
105e28a4053SRui Paulo 
106e28a4053SRui Paulo 	pos += sizeof(*oper);
107e28a4053SRui Paulo 
108e28a4053SRui Paulo 	return pos;
109e28a4053SRui Paulo }
110e28a4053SRui Paulo 
111e28a4053SRui Paulo 
112e28a4053SRui Paulo /*
113e28a4053SRui Paulo op_mode
114c1d255d3SCy Schubert Set to 0 (HT pure) under the following conditions
115e28a4053SRui Paulo 	- all STAs in the BSS are 20/40 MHz HT in 20/40 MHz BSS or
116e28a4053SRui Paulo 	- all STAs in the BSS are 20 MHz HT in 20 MHz BSS
117e28a4053SRui Paulo Set to 1 (HT non-member protection) if there may be non-HT STAs
118e28a4053SRui Paulo 	in both the primary and the secondary channel
119e28a4053SRui Paulo Set to 2 if only HT STAs are associated in BSS,
120e28a4053SRui Paulo 	however and at least one 20 MHz HT STA is associated
121e28a4053SRui Paulo Set to 3 (HT mixed mode) when one or more non-HT STAs are associated
122e28a4053SRui Paulo */
hostapd_ht_operation_update(struct hostapd_iface * iface)123e28a4053SRui Paulo int hostapd_ht_operation_update(struct hostapd_iface *iface)
124e28a4053SRui Paulo {
125e28a4053SRui Paulo 	u16 cur_op_mode, new_op_mode;
126e28a4053SRui Paulo 	int op_mode_changes = 0;
127e28a4053SRui Paulo 
128e28a4053SRui Paulo 	if (!iface->conf->ieee80211n || iface->conf->ht_op_mode_fixed)
129e28a4053SRui Paulo 		return 0;
130e28a4053SRui Paulo 
131e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "%s current operation mode=0x%X",
132e28a4053SRui Paulo 		   __func__, iface->ht_op_mode);
133e28a4053SRui Paulo 
1345b9c547cSRui Paulo 	if (!(iface->ht_op_mode & HT_OPER_OP_MODE_NON_GF_HT_STAS_PRESENT)
135e28a4053SRui Paulo 	    && iface->num_sta_ht_no_gf) {
1365b9c547cSRui Paulo 		iface->ht_op_mode |= HT_OPER_OP_MODE_NON_GF_HT_STAS_PRESENT;
137e28a4053SRui Paulo 		op_mode_changes++;
138e28a4053SRui Paulo 	} else if ((iface->ht_op_mode &
1395b9c547cSRui Paulo 		    HT_OPER_OP_MODE_NON_GF_HT_STAS_PRESENT) &&
140e28a4053SRui Paulo 		   iface->num_sta_ht_no_gf == 0) {
1415b9c547cSRui Paulo 		iface->ht_op_mode &= ~HT_OPER_OP_MODE_NON_GF_HT_STAS_PRESENT;
142e28a4053SRui Paulo 		op_mode_changes++;
143e28a4053SRui Paulo 	}
144e28a4053SRui Paulo 
1455b9c547cSRui Paulo 	if (!(iface->ht_op_mode & HT_OPER_OP_MODE_OBSS_NON_HT_STAS_PRESENT) &&
146e28a4053SRui Paulo 	    (iface->num_sta_no_ht || iface->olbc_ht)) {
1475b9c547cSRui Paulo 		iface->ht_op_mode |= HT_OPER_OP_MODE_OBSS_NON_HT_STAS_PRESENT;
148e28a4053SRui Paulo 		op_mode_changes++;
149e28a4053SRui Paulo 	} else if ((iface->ht_op_mode &
1505b9c547cSRui Paulo 		    HT_OPER_OP_MODE_OBSS_NON_HT_STAS_PRESENT) &&
151e28a4053SRui Paulo 		   (iface->num_sta_no_ht == 0 && !iface->olbc_ht)) {
1525b9c547cSRui Paulo 		iface->ht_op_mode &= ~HT_OPER_OP_MODE_OBSS_NON_HT_STAS_PRESENT;
153e28a4053SRui Paulo 		op_mode_changes++;
154e28a4053SRui Paulo 	}
155e28a4053SRui Paulo 
156f05cddf9SRui Paulo 	if (iface->num_sta_no_ht)
1575b9c547cSRui Paulo 		new_op_mode = HT_PROT_NON_HT_MIXED;
1585b9c547cSRui Paulo 	else if (iface->conf->secondary_channel && iface->num_sta_ht_20mhz)
1595b9c547cSRui Paulo 		new_op_mode = HT_PROT_20MHZ_PROTECTION;
160e28a4053SRui Paulo 	else if (iface->olbc_ht)
1615b9c547cSRui Paulo 		new_op_mode = HT_PROT_NONMEMBER_PROTECTION;
162e28a4053SRui Paulo 	else
1635b9c547cSRui Paulo 		new_op_mode = HT_PROT_NO_PROTECTION;
164e28a4053SRui Paulo 
1655b9c547cSRui Paulo 	cur_op_mode = iface->ht_op_mode & HT_OPER_OP_MODE_HT_PROT_MASK;
166e28a4053SRui Paulo 	if (cur_op_mode != new_op_mode) {
1675b9c547cSRui Paulo 		iface->ht_op_mode &= ~HT_OPER_OP_MODE_HT_PROT_MASK;
168e28a4053SRui Paulo 		iface->ht_op_mode |= new_op_mode;
169e28a4053SRui Paulo 		op_mode_changes++;
170e28a4053SRui Paulo 	}
171e28a4053SRui Paulo 
172e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "%s new operation mode=0x%X changes=%d",
173e28a4053SRui Paulo 		   __func__, iface->ht_op_mode, op_mode_changes);
174e28a4053SRui Paulo 
175e28a4053SRui Paulo 	return op_mode_changes;
176e28a4053SRui Paulo }
177e28a4053SRui Paulo 
178e28a4053SRui Paulo 
is_40_allowed(struct hostapd_iface * iface,int channel)1795b9c547cSRui Paulo static int is_40_allowed(struct hostapd_iface *iface, int channel)
1805b9c547cSRui Paulo {
1815b9c547cSRui Paulo 	int pri_freq, sec_freq;
1825b9c547cSRui Paulo 	int affected_start, affected_end;
1835b9c547cSRui Paulo 	int pri = 2407 + 5 * channel;
1845b9c547cSRui Paulo 
1855b9c547cSRui Paulo 	if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
1865b9c547cSRui Paulo 		return 1;
1875b9c547cSRui Paulo 
1885b9c547cSRui Paulo 	pri_freq = hostapd_hw_get_freq(iface->bss[0], iface->conf->channel);
1895b9c547cSRui Paulo 
1905b9c547cSRui Paulo 	if (iface->conf->secondary_channel > 0)
1915b9c547cSRui Paulo 		sec_freq = pri_freq + 20;
1925b9c547cSRui Paulo 	else
1935b9c547cSRui Paulo 		sec_freq = pri_freq - 20;
1945b9c547cSRui Paulo 
1955b9c547cSRui Paulo 	affected_start = (pri_freq + sec_freq) / 2 - 25;
1965b9c547cSRui Paulo 	affected_end = (pri_freq + sec_freq) / 2 + 25;
1975b9c547cSRui Paulo 	if ((pri < affected_start || pri > affected_end))
1985b9c547cSRui Paulo 		return 1; /* not within affected channel range */
1995b9c547cSRui Paulo 
2005b9c547cSRui Paulo 	wpa_printf(MSG_ERROR, "40 MHz affected channel range: [%d,%d] MHz",
2015b9c547cSRui Paulo 		   affected_start, affected_end);
2025b9c547cSRui Paulo 	wpa_printf(MSG_ERROR, "Neighboring BSS: freq=%d", pri);
2035b9c547cSRui Paulo 	return 0;
2045b9c547cSRui Paulo }
2055b9c547cSRui Paulo 
2065b9c547cSRui Paulo 
hostapd_2040_coex_action(struct hostapd_data * hapd,const struct ieee80211_mgmt * mgmt,size_t len)2075b9c547cSRui Paulo void hostapd_2040_coex_action(struct hostapd_data *hapd,
2085b9c547cSRui Paulo 			      const struct ieee80211_mgmt *mgmt, size_t len)
2095b9c547cSRui Paulo {
2105b9c547cSRui Paulo 	struct hostapd_iface *iface = hapd->iface;
2115b9c547cSRui Paulo 	struct ieee80211_2040_bss_coex_ie *bc_ie;
2125b9c547cSRui Paulo 	struct ieee80211_2040_intol_chan_report *ic_report;
213325151a3SRui Paulo 	int is_ht40_allowed = 1;
2145b9c547cSRui Paulo 	int i;
2155b9c547cSRui Paulo 	const u8 *start = (const u8 *) mgmt;
2165b9c547cSRui Paulo 	const u8 *data = start + IEEE80211_HDRLEN + 2;
21785732ac8SCy Schubert 	struct sta_info *sta;
21885732ac8SCy Schubert 
21985732ac8SCy Schubert 	wpa_printf(MSG_DEBUG,
22085732ac8SCy Schubert 		   "HT: Received 20/40 BSS Coexistence Management frame from "
22185732ac8SCy Schubert 		   MACSTR, MAC2STR(mgmt->sa));
2225b9c547cSRui Paulo 
2235b9c547cSRui Paulo 	hostapd_logger(hapd, mgmt->sa, HOSTAPD_MODULE_IEEE80211,
2245b9c547cSRui Paulo 		       HOSTAPD_LEVEL_DEBUG, "hostapd_public_action - action=%d",
2255b9c547cSRui Paulo 		       mgmt->u.action.u.public_action.action);
2265b9c547cSRui Paulo 
22785732ac8SCy Schubert 	if (!(iface->conf->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
22885732ac8SCy Schubert 		wpa_printf(MSG_DEBUG,
22985732ac8SCy Schubert 			   "Ignore 20/40 BSS Coexistence Management frame since 40 MHz capability is not enabled");
2305b9c547cSRui Paulo 		return;
23185732ac8SCy Schubert 	}
2325b9c547cSRui Paulo 
23385732ac8SCy Schubert 	if (len < IEEE80211_HDRLEN + 2 + sizeof(*bc_ie)) {
23485732ac8SCy Schubert 		wpa_printf(MSG_DEBUG,
23585732ac8SCy Schubert 			   "Ignore too short 20/40 BSS Coexistence Management frame");
2365b9c547cSRui Paulo 		return;
23785732ac8SCy Schubert 	}
2385b9c547cSRui Paulo 
23985732ac8SCy Schubert 	/* 20/40 BSS Coexistence element */
2405b9c547cSRui Paulo 	bc_ie = (struct ieee80211_2040_bss_coex_ie *) data;
2415b9c547cSRui Paulo 	if (bc_ie->element_id != WLAN_EID_20_40_BSS_COEXISTENCE ||
2425b9c547cSRui Paulo 	    bc_ie->length < 1) {
2435b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Unexpected IE (%u,%u) in coex report",
2445b9c547cSRui Paulo 			   bc_ie->element_id, bc_ie->length);
2455b9c547cSRui Paulo 		return;
2465b9c547cSRui Paulo 	}
24785732ac8SCy Schubert 	if (len < IEEE80211_HDRLEN + 2 + 2 + bc_ie->length) {
24885732ac8SCy Schubert 		wpa_printf(MSG_DEBUG,
24985732ac8SCy Schubert 			   "Truncated 20/40 BSS Coexistence element");
2505b9c547cSRui Paulo 		return;
25185732ac8SCy Schubert 	}
2525b9c547cSRui Paulo 	data += 2 + bc_ie->length;
2535b9c547cSRui Paulo 
25485732ac8SCy Schubert 	wpa_printf(MSG_DEBUG,
25585732ac8SCy Schubert 		   "20/40 BSS Coexistence Information field: 0x%x (%s%s%s%s%s%s)",
25685732ac8SCy Schubert 		   bc_ie->coex_param,
25785732ac8SCy Schubert 		   (bc_ie->coex_param & BIT(0)) ? "[InfoReq]" : "",
25885732ac8SCy Schubert 		   (bc_ie->coex_param & BIT(1)) ? "[40MHzIntolerant]" : "",
25985732ac8SCy Schubert 		   (bc_ie->coex_param & BIT(2)) ? "[20MHzBSSWidthReq]" : "",
26085732ac8SCy Schubert 		   (bc_ie->coex_param & BIT(3)) ? "[OBSSScanExemptionReq]" : "",
26185732ac8SCy Schubert 		   (bc_ie->coex_param & BIT(4)) ?
26285732ac8SCy Schubert 		   "[OBSSScanExemptionGrant]" : "",
26385732ac8SCy Schubert 		   (bc_ie->coex_param & (BIT(5) | BIT(6) | BIT(7))) ?
26485732ac8SCy Schubert 		   "[Reserved]" : "");
26585732ac8SCy Schubert 
2665b9c547cSRui Paulo 	if (bc_ie->coex_param & WLAN_20_40_BSS_COEX_20MHZ_WIDTH_REQ) {
26785732ac8SCy Schubert 		/* Intra-BSS communication prohibiting 20/40 MHz BSS operation
26885732ac8SCy Schubert 		 */
26985732ac8SCy Schubert 		sta = ap_get_sta(hapd, mgmt->sa);
27085732ac8SCy Schubert 		if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
27185732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
27285732ac8SCy Schubert 				   "Ignore intra-BSS 20/40 BSS Coexistence Management frame from not-associated STA");
27385732ac8SCy Schubert 			return;
27485732ac8SCy Schubert 		}
27585732ac8SCy Schubert 
2765b9c547cSRui Paulo 		hostapd_logger(hapd, mgmt->sa,
2775b9c547cSRui Paulo 			       HOSTAPD_MODULE_IEEE80211,
2785b9c547cSRui Paulo 			       HOSTAPD_LEVEL_DEBUG,
2795b9c547cSRui Paulo 			       "20 MHz BSS width request bit is set in BSS coexistence information field");
280325151a3SRui Paulo 		is_ht40_allowed = 0;
2815b9c547cSRui Paulo 	}
2825b9c547cSRui Paulo 
2835b9c547cSRui Paulo 	if (bc_ie->coex_param & WLAN_20_40_BSS_COEX_40MHZ_INTOL) {
28485732ac8SCy Schubert 		/* Inter-BSS communication prohibiting 20/40 MHz BSS operation
28585732ac8SCy Schubert 		 */
2865b9c547cSRui Paulo 		hostapd_logger(hapd, mgmt->sa,
2875b9c547cSRui Paulo 			       HOSTAPD_MODULE_IEEE80211,
2885b9c547cSRui Paulo 			       HOSTAPD_LEVEL_DEBUG,
2895b9c547cSRui Paulo 			       "40 MHz intolerant bit is set in BSS coexistence information field");
290325151a3SRui Paulo 		is_ht40_allowed = 0;
2915b9c547cSRui Paulo 	}
2925b9c547cSRui Paulo 
29385732ac8SCy Schubert 	/* 20/40 BSS Intolerant Channel Report element (zero or more times) */
29485732ac8SCy Schubert 	while (start + len - data >= 3 &&
2955b9c547cSRui Paulo 	       data[0] == WLAN_EID_20_40_BSS_INTOLERANT && data[1] >= 1) {
2965b9c547cSRui Paulo 		u8 ielen = data[1];
2975b9c547cSRui Paulo 
29885732ac8SCy Schubert 		if (ielen > start + len - data - 2) {
29985732ac8SCy Schubert 			wpa_printf(MSG_DEBUG,
30085732ac8SCy Schubert 				   "Truncated 20/40 BSS Intolerant Channel Report element");
3015b9c547cSRui Paulo 			return;
30285732ac8SCy Schubert 		}
3035b9c547cSRui Paulo 		ic_report = (struct ieee80211_2040_intol_chan_report *) data;
3045b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
3055b9c547cSRui Paulo 			   "20/40 BSS Intolerant Channel Report: Operating Class %u",
3065b9c547cSRui Paulo 			   ic_report->op_class);
3075b9c547cSRui Paulo 
3085b9c547cSRui Paulo 		/* Go through the channel report to find any BSS there in the
3095b9c547cSRui Paulo 		 * affected channel range */
3105b9c547cSRui Paulo 		for (i = 0; i < ielen - 1; i++) {
3115b9c547cSRui Paulo 			u8 chan = ic_report->variable[i];
3125b9c547cSRui Paulo 
31385732ac8SCy Schubert 			if (chan == iface->conf->channel)
31485732ac8SCy Schubert 				continue; /* matching own primary channel */
3155b9c547cSRui Paulo 			if (is_40_allowed(iface, chan))
31685732ac8SCy Schubert 				continue; /* not within affected channels */
3175b9c547cSRui Paulo 			hostapd_logger(hapd, mgmt->sa,
3185b9c547cSRui Paulo 				       HOSTAPD_MODULE_IEEE80211,
3195b9c547cSRui Paulo 				       HOSTAPD_LEVEL_DEBUG,
3205b9c547cSRui Paulo 				       "20_40_INTOLERANT channel %d reported",
3215b9c547cSRui Paulo 				       chan);
322325151a3SRui Paulo 			is_ht40_allowed = 0;
3235b9c547cSRui Paulo 		}
32485732ac8SCy Schubert 
32585732ac8SCy Schubert 		data += 2 + ielen;
3265b9c547cSRui Paulo 	}
327325151a3SRui Paulo 	wpa_printf(MSG_DEBUG, "is_ht40_allowed=%d num_sta_ht40_intolerant=%d",
328325151a3SRui Paulo 		   is_ht40_allowed, iface->num_sta_ht40_intolerant);
3295b9c547cSRui Paulo 
330325151a3SRui Paulo 	if (!is_ht40_allowed &&
3315b9c547cSRui Paulo 	    (iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX)) {
3325b9c547cSRui Paulo 		if (iface->conf->secondary_channel) {
3335b9c547cSRui Paulo 			hostapd_logger(hapd, mgmt->sa,
3345b9c547cSRui Paulo 				       HOSTAPD_MODULE_IEEE80211,
3355b9c547cSRui Paulo 				       HOSTAPD_LEVEL_INFO,
3365b9c547cSRui Paulo 				       "Switching to 20 MHz operation");
3375b9c547cSRui Paulo 			iface->conf->secondary_channel = 0;
3385b9c547cSRui Paulo 			ieee802_11_set_beacons(iface);
3395b9c547cSRui Paulo 		}
3405b9c547cSRui Paulo 		if (!iface->num_sta_ht40_intolerant &&
3415b9c547cSRui Paulo 		    iface->conf->obss_interval) {
3425b9c547cSRui Paulo 			unsigned int delay_time;
3435b9c547cSRui Paulo 			delay_time = OVERLAPPING_BSS_TRANS_DELAY_FACTOR *
3445b9c547cSRui Paulo 				iface->conf->obss_interval;
3455b9c547cSRui Paulo 			eloop_cancel_timeout(ap_ht2040_timeout, hapd->iface,
3465b9c547cSRui Paulo 					     NULL);
3475b9c547cSRui Paulo 			eloop_register_timeout(delay_time, 0, ap_ht2040_timeout,
3485b9c547cSRui Paulo 					       hapd->iface, NULL);
3495b9c547cSRui Paulo 			wpa_printf(MSG_DEBUG,
3505b9c547cSRui Paulo 				   "Reschedule HT 20/40 timeout to occur in %u seconds",
3515b9c547cSRui Paulo 				   delay_time);
3525b9c547cSRui Paulo 		}
3535b9c547cSRui Paulo 	}
3545b9c547cSRui Paulo }
3555b9c547cSRui Paulo 
3565b9c547cSRui Paulo 
copy_sta_ht_capab(struct hostapd_data * hapd,struct sta_info * sta,const u8 * ht_capab)357f05cddf9SRui Paulo u16 copy_sta_ht_capab(struct hostapd_data *hapd, struct sta_info *sta,
358325151a3SRui Paulo 		      const u8 *ht_capab)
359e28a4053SRui Paulo {
360325151a3SRui Paulo 	/*
361325151a3SRui Paulo 	 * Disable HT caps for STAs associated to no-HT BSSes, or for stations
362325151a3SRui Paulo 	 * that did not specify a valid WMM IE in the (Re)Association Request
363325151a3SRui Paulo 	 * frame.
364325151a3SRui Paulo 	 */
36585732ac8SCy Schubert 	if (!ht_capab || !(sta->flags & WLAN_STA_WMM) ||
36685732ac8SCy Schubert 	    !hapd->iconf->ieee80211n || hapd->conf->disable_11n) {
367e28a4053SRui Paulo 		sta->flags &= ~WLAN_STA_HT;
368e28a4053SRui Paulo 		os_free(sta->ht_capabilities);
369e28a4053SRui Paulo 		sta->ht_capabilities = NULL;
370e28a4053SRui Paulo 		return WLAN_STATUS_SUCCESS;
371e28a4053SRui Paulo 	}
372e28a4053SRui Paulo 
373e28a4053SRui Paulo 	if (sta->ht_capabilities == NULL) {
374e28a4053SRui Paulo 		sta->ht_capabilities =
375e28a4053SRui Paulo 			os_zalloc(sizeof(struct ieee80211_ht_capabilities));
376e28a4053SRui Paulo 		if (sta->ht_capabilities == NULL)
377e28a4053SRui Paulo 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
378e28a4053SRui Paulo 	}
379e28a4053SRui Paulo 
380e28a4053SRui Paulo 	sta->flags |= WLAN_STA_HT;
381e28a4053SRui Paulo 	os_memcpy(sta->ht_capabilities, ht_capab,
382e28a4053SRui Paulo 		  sizeof(struct ieee80211_ht_capabilities));
383e28a4053SRui Paulo 
384e28a4053SRui Paulo 	return WLAN_STATUS_SUCCESS;
385e28a4053SRui Paulo }
386e28a4053SRui Paulo 
387e28a4053SRui Paulo 
ht40_intolerant_add(struct hostapd_iface * iface,struct sta_info * sta)3885b9c547cSRui Paulo void ht40_intolerant_add(struct hostapd_iface *iface, struct sta_info *sta)
3895b9c547cSRui Paulo {
3905b9c547cSRui Paulo 	if (iface->current_mode->mode != HOSTAPD_MODE_IEEE80211G)
3915b9c547cSRui Paulo 		return;
3925b9c547cSRui Paulo 
3935b9c547cSRui Paulo 	wpa_printf(MSG_INFO, "HT: Forty MHz Intolerant is set by STA " MACSTR
3945b9c547cSRui Paulo 		   " in Association Request", MAC2STR(sta->addr));
3955b9c547cSRui Paulo 
3965b9c547cSRui Paulo 	if (sta->ht40_intolerant_set)
3975b9c547cSRui Paulo 		return;
3985b9c547cSRui Paulo 
3995b9c547cSRui Paulo 	sta->ht40_intolerant_set = 1;
4005b9c547cSRui Paulo 	iface->num_sta_ht40_intolerant++;
4015b9c547cSRui Paulo 	eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
4025b9c547cSRui Paulo 
4035b9c547cSRui Paulo 	if (iface->conf->secondary_channel &&
4045b9c547cSRui Paulo 	    (iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX)) {
4055b9c547cSRui Paulo 		iface->conf->secondary_channel = 0;
4065b9c547cSRui Paulo 		ieee802_11_set_beacons(iface);
4075b9c547cSRui Paulo 	}
4085b9c547cSRui Paulo }
4095b9c547cSRui Paulo 
4105b9c547cSRui Paulo 
ht40_intolerant_remove(struct hostapd_iface * iface,struct sta_info * sta)4115b9c547cSRui Paulo void ht40_intolerant_remove(struct hostapd_iface *iface, struct sta_info *sta)
4125b9c547cSRui Paulo {
4135b9c547cSRui Paulo 	if (!sta->ht40_intolerant_set)
4145b9c547cSRui Paulo 		return;
4155b9c547cSRui Paulo 
4165b9c547cSRui Paulo 	sta->ht40_intolerant_set = 0;
4175b9c547cSRui Paulo 	iface->num_sta_ht40_intolerant--;
4185b9c547cSRui Paulo 
4195b9c547cSRui Paulo 	if (iface->num_sta_ht40_intolerant == 0 &&
4205b9c547cSRui Paulo 	    (iface->conf->ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) &&
4215b9c547cSRui Paulo 	    (iface->drv_flags & WPA_DRIVER_FLAGS_HT_2040_COEX)) {
4225b9c547cSRui Paulo 		unsigned int delay_time = OVERLAPPING_BSS_TRANS_DELAY_FACTOR *
4235b9c547cSRui Paulo 			iface->conf->obss_interval;
4245b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG,
4255b9c547cSRui Paulo 			   "HT: Start 20->40 MHz transition timer (%d seconds)",
4265b9c547cSRui Paulo 			   delay_time);
4275b9c547cSRui Paulo 		eloop_cancel_timeout(ap_ht2040_timeout, iface, NULL);
4285b9c547cSRui Paulo 		eloop_register_timeout(delay_time, 0, ap_ht2040_timeout,
4295b9c547cSRui Paulo 				       iface, NULL);
4305b9c547cSRui Paulo 	}
4315b9c547cSRui Paulo }
4325b9c547cSRui Paulo 
4335b9c547cSRui Paulo 
update_sta_ht(struct hostapd_data * hapd,struct sta_info * sta)434e28a4053SRui Paulo static void update_sta_ht(struct hostapd_data *hapd, struct sta_info *sta)
435e28a4053SRui Paulo {
436e28a4053SRui Paulo 	u16 ht_capab;
437e28a4053SRui Paulo 
438e28a4053SRui Paulo 	ht_capab = le_to_host16(sta->ht_capabilities->ht_capabilities_info);
439e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "HT: STA " MACSTR " HT Capabilities Info: "
440e28a4053SRui Paulo 		   "0x%04x", MAC2STR(sta->addr), ht_capab);
441e28a4053SRui Paulo 	if ((ht_capab & HT_CAP_INFO_GREEN_FIELD) == 0) {
442e28a4053SRui Paulo 		if (!sta->no_ht_gf_set) {
443e28a4053SRui Paulo 			sta->no_ht_gf_set = 1;
444e28a4053SRui Paulo 			hapd->iface->num_sta_ht_no_gf++;
445e28a4053SRui Paulo 		}
446e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no greenfield, num "
447e28a4053SRui Paulo 			   "of non-gf stations %d",
448e28a4053SRui Paulo 			   __func__, MAC2STR(sta->addr),
449e28a4053SRui Paulo 			   hapd->iface->num_sta_ht_no_gf);
450e28a4053SRui Paulo 	}
451e28a4053SRui Paulo 	if ((ht_capab & HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0) {
452e28a4053SRui Paulo 		if (!sta->ht_20mhz_set) {
453e28a4053SRui Paulo 			sta->ht_20mhz_set = 1;
454e28a4053SRui Paulo 			hapd->iface->num_sta_ht_20mhz++;
455e28a4053SRui Paulo 		}
456e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - 20 MHz HT, num of "
457e28a4053SRui Paulo 			   "20MHz HT STAs %d",
458e28a4053SRui Paulo 			   __func__, MAC2STR(sta->addr),
459e28a4053SRui Paulo 			   hapd->iface->num_sta_ht_20mhz);
460e28a4053SRui Paulo 	}
4615b9c547cSRui Paulo 
4625b9c547cSRui Paulo 	if (ht_capab & HT_CAP_INFO_40MHZ_INTOLERANT)
4635b9c547cSRui Paulo 		ht40_intolerant_add(hapd->iface, sta);
464e28a4053SRui Paulo }
465e28a4053SRui Paulo 
466e28a4053SRui Paulo 
update_sta_no_ht(struct hostapd_data * hapd,struct sta_info * sta)467e28a4053SRui Paulo static void update_sta_no_ht(struct hostapd_data *hapd, struct sta_info *sta)
468e28a4053SRui Paulo {
469e28a4053SRui Paulo 	if (!sta->no_ht_set) {
470e28a4053SRui Paulo 		sta->no_ht_set = 1;
471e28a4053SRui Paulo 		hapd->iface->num_sta_no_ht++;
472e28a4053SRui Paulo 	}
473e28a4053SRui Paulo 	if (hapd->iconf->ieee80211n) {
474e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "%s STA " MACSTR " - no HT, num of "
475e28a4053SRui Paulo 			   "non-HT stations %d",
476e28a4053SRui Paulo 			   __func__, MAC2STR(sta->addr),
477e28a4053SRui Paulo 			   hapd->iface->num_sta_no_ht);
478e28a4053SRui Paulo 	}
479e28a4053SRui Paulo }
480e28a4053SRui Paulo 
481e28a4053SRui Paulo 
update_ht_state(struct hostapd_data * hapd,struct sta_info * sta)482*a90b9d01SCy Schubert int update_ht_state(struct hostapd_data *hapd, struct sta_info *sta)
483e28a4053SRui Paulo {
484e28a4053SRui Paulo 	if ((sta->flags & WLAN_STA_HT) && sta->ht_capabilities)
485e28a4053SRui Paulo 		update_sta_ht(hapd, sta);
486e28a4053SRui Paulo 	else
487e28a4053SRui Paulo 		update_sta_no_ht(hapd, sta);
488e28a4053SRui Paulo 
489*a90b9d01SCy Schubert 	return hostapd_ht_operation_update(hapd->iface);
490e28a4053SRui Paulo }
491e28a4053SRui Paulo 
492e28a4053SRui Paulo 
hostapd_get_ht_capab(struct hostapd_data * hapd,struct ieee80211_ht_capabilities * ht_cap,struct ieee80211_ht_capabilities * neg_ht_cap)493e28a4053SRui Paulo void hostapd_get_ht_capab(struct hostapd_data *hapd,
494e28a4053SRui Paulo 			  struct ieee80211_ht_capabilities *ht_cap,
495e28a4053SRui Paulo 			  struct ieee80211_ht_capabilities *neg_ht_cap)
496e28a4053SRui Paulo {
497e28a4053SRui Paulo 	u16 cap;
498e28a4053SRui Paulo 
499e28a4053SRui Paulo 	if (ht_cap == NULL)
500e28a4053SRui Paulo 		return;
501e28a4053SRui Paulo 	os_memcpy(neg_ht_cap, ht_cap, sizeof(*neg_ht_cap));
502e28a4053SRui Paulo 	cap = le_to_host16(neg_ht_cap->ht_capabilities_info);
503f05cddf9SRui Paulo 
504f05cddf9SRui Paulo 	/*
505f05cddf9SRui Paulo 	 * Mask out HT features we don't support, but don't overwrite
506f05cddf9SRui Paulo 	 * non-symmetric features like STBC and SMPS. Just because
507f05cddf9SRui Paulo 	 * we're not in dynamic SMPS mode the STA might still be.
508f05cddf9SRui Paulo 	 */
509f05cddf9SRui Paulo 	cap &= (hapd->iconf->ht_capab | HT_CAP_INFO_RX_STBC_MASK |
510f05cddf9SRui Paulo 		HT_CAP_INFO_TX_STBC | HT_CAP_INFO_SMPS_MASK);
511e28a4053SRui Paulo 
512e28a4053SRui Paulo 	/*
513e28a4053SRui Paulo 	 * STBC needs to be handled specially
514e28a4053SRui Paulo 	 * if we don't support RX STBC, mask out TX STBC in the STA's HT caps
515e28a4053SRui Paulo 	 * if we don't support TX STBC, mask out RX STBC in the STA's HT caps
516e28a4053SRui Paulo 	 */
517e28a4053SRui Paulo 	if (!(hapd->iconf->ht_capab & HT_CAP_INFO_RX_STBC_MASK))
518e28a4053SRui Paulo 		cap &= ~HT_CAP_INFO_TX_STBC;
519e28a4053SRui Paulo 	if (!(hapd->iconf->ht_capab & HT_CAP_INFO_TX_STBC))
520e28a4053SRui Paulo 		cap &= ~HT_CAP_INFO_RX_STBC_MASK;
521e28a4053SRui Paulo 
522e28a4053SRui Paulo 	neg_ht_cap->ht_capabilities_info = host_to_le16(cap);
523e28a4053SRui Paulo }
5245b9c547cSRui Paulo 
5255b9c547cSRui Paulo 
ap_ht2040_timeout(void * eloop_data,void * user_data)5265b9c547cSRui Paulo void ap_ht2040_timeout(void *eloop_data, void *user_data)
5275b9c547cSRui Paulo {
5285b9c547cSRui Paulo 	struct hostapd_iface *iface = eloop_data;
5295b9c547cSRui Paulo 
5305b9c547cSRui Paulo 	wpa_printf(MSG_INFO, "Switching to 40 MHz operation");
5315b9c547cSRui Paulo 
5325b9c547cSRui Paulo 	iface->conf->secondary_channel = iface->secondary_ch;
5335b9c547cSRui Paulo 	ieee802_11_set_beacons(iface);
5345b9c547cSRui Paulo }
535