15b9c547cSRui Paulo /*
25b9c547cSRui Paulo  * Common hostapd/wpa_supplicant HW features
35b9c547cSRui Paulo  * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
45b9c547cSRui Paulo  * Copyright (c) 2015, Qualcomm Atheros, Inc.
55b9c547cSRui Paulo  *
65b9c547cSRui Paulo  * This software may be distributed under the terms of the BSD license.
75b9c547cSRui Paulo  * See README for more details.
85b9c547cSRui Paulo  */
95b9c547cSRui Paulo 
105b9c547cSRui Paulo #include "includes.h"
115b9c547cSRui Paulo 
125b9c547cSRui Paulo #include "common.h"
135b9c547cSRui Paulo #include "defs.h"
145b9c547cSRui Paulo #include "ieee802_11_defs.h"
155b9c547cSRui Paulo #include "ieee802_11_common.h"
165b9c547cSRui Paulo #include "hw_features_common.h"
175b9c547cSRui Paulo 
185b9c547cSRui Paulo 
hw_get_channel_chan(struct hostapd_hw_modes * mode,int chan,int * freq)195b9c547cSRui Paulo struct hostapd_channel_data * hw_get_channel_chan(struct hostapd_hw_modes *mode,
205b9c547cSRui Paulo 						  int chan, int *freq)
215b9c547cSRui Paulo {
225b9c547cSRui Paulo 	int i;
235b9c547cSRui Paulo 
245b9c547cSRui Paulo 	if (freq)
255b9c547cSRui Paulo 		*freq = 0;
265b9c547cSRui Paulo 
275b9c547cSRui Paulo 	if (!mode)
285b9c547cSRui Paulo 		return NULL;
295b9c547cSRui Paulo 
305b9c547cSRui Paulo 	for (i = 0; i < mode->num_channels; i++) {
315b9c547cSRui Paulo 		struct hostapd_channel_data *ch = &mode->channels[i];
325b9c547cSRui Paulo 		if (ch->chan == chan) {
335b9c547cSRui Paulo 			if (freq)
345b9c547cSRui Paulo 				*freq = ch->freq;
355b9c547cSRui Paulo 			return ch;
365b9c547cSRui Paulo 		}
375b9c547cSRui Paulo 	}
385b9c547cSRui Paulo 
395b9c547cSRui Paulo 	return NULL;
405b9c547cSRui Paulo }
415b9c547cSRui Paulo 
425b9c547cSRui Paulo 
43c1d255d3SCy Schubert struct hostapd_channel_data *
hw_mode_get_channel(struct hostapd_hw_modes * mode,int freq,int * chan)44c1d255d3SCy Schubert hw_mode_get_channel(struct hostapd_hw_modes *mode, int freq, int *chan)
455b9c547cSRui Paulo {
465b9c547cSRui Paulo 	int i;
475b9c547cSRui Paulo 
485b9c547cSRui Paulo 	for (i = 0; i < mode->num_channels; i++) {
495b9c547cSRui Paulo 		struct hostapd_channel_data *ch = &mode->channels[i];
50c1d255d3SCy Schubert 
515b9c547cSRui Paulo 		if (ch->freq == freq) {
525b9c547cSRui Paulo 			if (chan)
535b9c547cSRui Paulo 				*chan = ch->chan;
545b9c547cSRui Paulo 			return ch;
555b9c547cSRui Paulo 		}
565b9c547cSRui Paulo 	}
575b9c547cSRui Paulo 
585b9c547cSRui Paulo 	return NULL;
595b9c547cSRui Paulo }
605b9c547cSRui Paulo 
615b9c547cSRui Paulo 
62c1d255d3SCy Schubert struct hostapd_channel_data *
hw_get_channel_freq(enum hostapd_hw_mode mode,int freq,int * chan,struct hostapd_hw_modes * hw_features,int num_hw_features)63c1d255d3SCy Schubert hw_get_channel_freq(enum hostapd_hw_mode mode, int freq, int *chan,
64c1d255d3SCy Schubert 		    struct hostapd_hw_modes *hw_features, int num_hw_features)
65c1d255d3SCy Schubert {
66c1d255d3SCy Schubert 	struct hostapd_channel_data *chan_data;
67c1d255d3SCy Schubert 	int i;
68c1d255d3SCy Schubert 
69c1d255d3SCy Schubert 	if (chan)
70c1d255d3SCy Schubert 		*chan = 0;
71c1d255d3SCy Schubert 
72c1d255d3SCy Schubert 	if (!hw_features)
73c1d255d3SCy Schubert 		return NULL;
74c1d255d3SCy Schubert 
75c1d255d3SCy Schubert 	for (i = 0; i < num_hw_features; i++) {
76c1d255d3SCy Schubert 		struct hostapd_hw_modes *curr_mode = &hw_features[i];
77c1d255d3SCy Schubert 
78c1d255d3SCy Schubert 		if (curr_mode->mode != mode)
79c1d255d3SCy Schubert 			continue;
80c1d255d3SCy Schubert 
81c1d255d3SCy Schubert 		chan_data = hw_mode_get_channel(curr_mode, freq, chan);
82c1d255d3SCy Schubert 		if (chan_data)
83c1d255d3SCy Schubert 			return chan_data;
84c1d255d3SCy Schubert 	}
85c1d255d3SCy Schubert 
86c1d255d3SCy Schubert 	return NULL;
87c1d255d3SCy Schubert }
88c1d255d3SCy Schubert 
89c1d255d3SCy Schubert 
hw_get_freq(struct hostapd_hw_modes * mode,int chan)905b9c547cSRui Paulo int hw_get_freq(struct hostapd_hw_modes *mode, int chan)
915b9c547cSRui Paulo {
925b9c547cSRui Paulo 	int freq;
935b9c547cSRui Paulo 
945b9c547cSRui Paulo 	hw_get_channel_chan(mode, chan, &freq);
955b9c547cSRui Paulo 
965b9c547cSRui Paulo 	return freq;
975b9c547cSRui Paulo }
985b9c547cSRui Paulo 
995b9c547cSRui Paulo 
hw_get_chan(enum hostapd_hw_mode mode,int freq,struct hostapd_hw_modes * hw_features,int num_hw_features)100c1d255d3SCy Schubert int hw_get_chan(enum hostapd_hw_mode mode, int freq,
101c1d255d3SCy Schubert 		struct hostapd_hw_modes *hw_features, int num_hw_features)
1025b9c547cSRui Paulo {
1035b9c547cSRui Paulo 	int chan;
1045b9c547cSRui Paulo 
105c1d255d3SCy Schubert 	hw_get_channel_freq(mode, freq, &chan, hw_features, num_hw_features);
1065b9c547cSRui Paulo 
1075b9c547cSRui Paulo 	return chan;
1085b9c547cSRui Paulo }
1095b9c547cSRui Paulo 
1105b9c547cSRui Paulo 
allowed_ht40_channel_pair(enum hostapd_hw_mode mode,struct hostapd_channel_data * p_chan,struct hostapd_channel_data * s_chan)111c1d255d3SCy Schubert int allowed_ht40_channel_pair(enum hostapd_hw_mode mode,
112c1d255d3SCy Schubert 			      struct hostapd_channel_data *p_chan,
113c1d255d3SCy Schubert 			      struct hostapd_channel_data *s_chan)
1145b9c547cSRui Paulo {
1154bc52338SCy Schubert 	int ok, first;
116325151a3SRui Paulo 	int allowed[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 140,
1174b72b91aSCy Schubert 			  149, 157, 165, 173, 184, 192 };
1185b9c547cSRui Paulo 	size_t k;
119c1d255d3SCy Schubert 	int ht40_plus, pri_chan, sec_chan;
1205b9c547cSRui Paulo 
121c1d255d3SCy Schubert 	if (!p_chan || !s_chan)
1224bc52338SCy Schubert 		return 0;
123c1d255d3SCy Schubert 	pri_chan = p_chan->chan;
124c1d255d3SCy Schubert 	sec_chan = s_chan->chan;
125c1d255d3SCy Schubert 
126c1d255d3SCy Schubert 	ht40_plus = pri_chan < sec_chan;
1274bc52338SCy Schubert 
1284bc52338SCy Schubert 	if (pri_chan == sec_chan || !sec_chan) {
1294bc52338SCy Schubert 		if (chan_pri_allowed(p_chan))
1305b9c547cSRui Paulo 			return 1; /* HT40 not used */
1315b9c547cSRui Paulo 
1324bc52338SCy Schubert 		wpa_printf(MSG_ERROR, "Channel %d is not allowed as primary",
1334bc52338SCy Schubert 			   pri_chan);
1344bc52338SCy Schubert 		return 0;
1354bc52338SCy Schubert 	}
1364bc52338SCy Schubert 
1375b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG,
138c1d255d3SCy Schubert 		   "HT40: control channel: %d (%d MHz), secondary channel: %d (%d MHz)",
139c1d255d3SCy Schubert 		   pri_chan, p_chan->freq, sec_chan, s_chan->freq);
1405b9c547cSRui Paulo 
1415b9c547cSRui Paulo 	/* Verify that HT40 secondary channel is an allowed 20 MHz
1425b9c547cSRui Paulo 	 * channel */
1434bc52338SCy Schubert 	if ((s_chan->flag & HOSTAPD_CHAN_DISABLED) ||
1444bc52338SCy Schubert 	    (ht40_plus && !(p_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40P)) ||
1454bc52338SCy Schubert 	    (!ht40_plus && !(p_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40M))) {
1465b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "HT40 secondary channel %d not allowed",
1475b9c547cSRui Paulo 			   sec_chan);
1485b9c547cSRui Paulo 		return 0;
1495b9c547cSRui Paulo 	}
1505b9c547cSRui Paulo 
1515b9c547cSRui Paulo 	/*
1525b9c547cSRui Paulo 	 * Verify that HT40 primary,secondary channel pair is allowed per
1535b9c547cSRui Paulo 	 * IEEE 802.11n Annex J. This is only needed for 5 GHz band since
1545b9c547cSRui Paulo 	 * 2.4 GHz rules allow all cases where the secondary channel fits into
1555b9c547cSRui Paulo 	 * the list of allowed channels (already checked above).
1565b9c547cSRui Paulo 	 */
157c1d255d3SCy Schubert 	if (mode != HOSTAPD_MODE_IEEE80211A)
1585b9c547cSRui Paulo 		return 1;
1595b9c547cSRui Paulo 
1605b9c547cSRui Paulo 	first = pri_chan < sec_chan ? pri_chan : sec_chan;
1615b9c547cSRui Paulo 
1625b9c547cSRui Paulo 	ok = 0;
1635b9c547cSRui Paulo 	for (k = 0; k < ARRAY_SIZE(allowed); k++) {
1645b9c547cSRui Paulo 		if (first == allowed[k]) {
1655b9c547cSRui Paulo 			ok = 1;
1665b9c547cSRui Paulo 			break;
1675b9c547cSRui Paulo 		}
1685b9c547cSRui Paulo 	}
1695b9c547cSRui Paulo 	if (!ok) {
1705b9c547cSRui Paulo 		wpa_printf(MSG_ERROR, "HT40 channel pair (%d, %d) not allowed",
1715b9c547cSRui Paulo 			   pri_chan, sec_chan);
1725b9c547cSRui Paulo 		return 0;
1735b9c547cSRui Paulo 	}
1745b9c547cSRui Paulo 
1755b9c547cSRui Paulo 	return 1;
1765b9c547cSRui Paulo }
1775b9c547cSRui Paulo 
1785b9c547cSRui Paulo 
get_pri_sec_chan(struct wpa_scan_res * bss,int * pri_chan,int * sec_chan)1795b9c547cSRui Paulo void get_pri_sec_chan(struct wpa_scan_res *bss, int *pri_chan, int *sec_chan)
1805b9c547cSRui Paulo {
1815b9c547cSRui Paulo 	struct ieee80211_ht_operation *oper;
1825b9c547cSRui Paulo 	struct ieee802_11_elems elems;
1835b9c547cSRui Paulo 
1845b9c547cSRui Paulo 	*pri_chan = *sec_chan = 0;
1855b9c547cSRui Paulo 
1865b9c547cSRui Paulo 	ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0);
187325151a3SRui Paulo 	if (elems.ht_operation) {
1885b9c547cSRui Paulo 		oper = (struct ieee80211_ht_operation *) elems.ht_operation;
1895b9c547cSRui Paulo 		*pri_chan = oper->primary_chan;
1905b9c547cSRui Paulo 		if (oper->ht_param & HT_INFO_HT_PARAM_STA_CHNL_WIDTH) {
1915b9c547cSRui Paulo 			int sec = oper->ht_param &
1925b9c547cSRui Paulo 				HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
1935b9c547cSRui Paulo 			if (sec == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
1945b9c547cSRui Paulo 				*sec_chan = *pri_chan + 4;
1955b9c547cSRui Paulo 			else if (sec == HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
1965b9c547cSRui Paulo 				*sec_chan = *pri_chan - 4;
1975b9c547cSRui Paulo 		}
1985b9c547cSRui Paulo 	}
1995b9c547cSRui Paulo }
2005b9c547cSRui Paulo 
2015b9c547cSRui Paulo 
check_40mhz_5g(struct wpa_scan_results * scan_res,struct hostapd_channel_data * pri_chan,struct hostapd_channel_data * sec_chan)202c1d255d3SCy Schubert int check_40mhz_5g(struct wpa_scan_results *scan_res,
203c1d255d3SCy Schubert 		   struct hostapd_channel_data *pri_chan,
204c1d255d3SCy Schubert 		   struct hostapd_channel_data *sec_chan)
2055b9c547cSRui Paulo {
206c1d255d3SCy Schubert 	int pri_bss, sec_bss;
2075b9c547cSRui Paulo 	int bss_pri_chan, bss_sec_chan;
2085b9c547cSRui Paulo 	size_t i;
2095b9c547cSRui Paulo 	int match;
2105b9c547cSRui Paulo 
211c1d255d3SCy Schubert 	if (!scan_res || !pri_chan || !sec_chan ||
212c1d255d3SCy Schubert 	    pri_chan->freq == sec_chan->freq)
2135b9c547cSRui Paulo 		return 0;
2145b9c547cSRui Paulo 
2155b9c547cSRui Paulo 	/*
2165b9c547cSRui Paulo 	 * Switch PRI/SEC channels if Beacons were detected on selected SEC
2175b9c547cSRui Paulo 	 * channel, but not on selected PRI channel.
2185b9c547cSRui Paulo 	 */
2195b9c547cSRui Paulo 	pri_bss = sec_bss = 0;
2205b9c547cSRui Paulo 	for (i = 0; i < scan_res->num; i++) {
2215b9c547cSRui Paulo 		struct wpa_scan_res *bss = scan_res->res[i];
222c1d255d3SCy Schubert 		if (bss->freq == pri_chan->freq)
2235b9c547cSRui Paulo 			pri_bss++;
224c1d255d3SCy Schubert 		else if (bss->freq == sec_chan->freq)
2255b9c547cSRui Paulo 			sec_bss++;
2265b9c547cSRui Paulo 	}
2275b9c547cSRui Paulo 	if (sec_bss && !pri_bss) {
2285b9c547cSRui Paulo 		wpa_printf(MSG_INFO,
2295b9c547cSRui Paulo 			   "Switch own primary and secondary channel to get secondary channel with no Beacons from other BSSes");
2305b9c547cSRui Paulo 		return 2;
2315b9c547cSRui Paulo 	}
2325b9c547cSRui Paulo 
2335b9c547cSRui Paulo 	/*
2345b9c547cSRui Paulo 	 * Match PRI/SEC channel with any existing HT40 BSS on the same
2355b9c547cSRui Paulo 	 * channels that we are about to use (if already mixed order in
2365b9c547cSRui Paulo 	 * existing BSSes, use own preference).
2375b9c547cSRui Paulo 	 */
2385b9c547cSRui Paulo 	match = 0;
2395b9c547cSRui Paulo 	for (i = 0; i < scan_res->num; i++) {
2405b9c547cSRui Paulo 		struct wpa_scan_res *bss = scan_res->res[i];
2415b9c547cSRui Paulo 		get_pri_sec_chan(bss, &bss_pri_chan, &bss_sec_chan);
242c1d255d3SCy Schubert 		if (pri_chan->chan == bss_pri_chan &&
243c1d255d3SCy Schubert 		    sec_chan->chan == bss_sec_chan) {
2445b9c547cSRui Paulo 			match = 1;
2455b9c547cSRui Paulo 			break;
2465b9c547cSRui Paulo 		}
2475b9c547cSRui Paulo 	}
2485b9c547cSRui Paulo 	if (!match) {
2495b9c547cSRui Paulo 		for (i = 0; i < scan_res->num; i++) {
2505b9c547cSRui Paulo 			struct wpa_scan_res *bss = scan_res->res[i];
2515b9c547cSRui Paulo 			get_pri_sec_chan(bss, &bss_pri_chan, &bss_sec_chan);
252c1d255d3SCy Schubert 			if (pri_chan->chan == bss_sec_chan &&
253c1d255d3SCy Schubert 			    sec_chan->chan == bss_pri_chan) {
2545b9c547cSRui Paulo 				wpa_printf(MSG_INFO, "Switch own primary and "
2555b9c547cSRui Paulo 					   "secondary channel due to BSS "
2565b9c547cSRui Paulo 					   "overlap with " MACSTR,
2575b9c547cSRui Paulo 					   MAC2STR(bss->bssid));
2585b9c547cSRui Paulo 				return 2;
2595b9c547cSRui Paulo 			}
2605b9c547cSRui Paulo 		}
2615b9c547cSRui Paulo 	}
2625b9c547cSRui Paulo 
2635b9c547cSRui Paulo 	return 1;
2645b9c547cSRui Paulo }
2655b9c547cSRui Paulo 
2665b9c547cSRui Paulo 
check_20mhz_bss(struct wpa_scan_res * bss,int pri_freq,int start,int end)267325151a3SRui Paulo static int check_20mhz_bss(struct wpa_scan_res *bss, int pri_freq, int start,
268325151a3SRui Paulo 			   int end)
2695b9c547cSRui Paulo {
2705b9c547cSRui Paulo 	struct ieee802_11_elems elems;
2715b9c547cSRui Paulo 	struct ieee80211_ht_operation *oper;
2725b9c547cSRui Paulo 
2735b9c547cSRui Paulo 	if (bss->freq < start || bss->freq > end || bss->freq == pri_freq)
2745b9c547cSRui Paulo 		return 0;
2755b9c547cSRui Paulo 
2765b9c547cSRui Paulo 	ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0);
2775b9c547cSRui Paulo 	if (!elems.ht_capabilities) {
2785b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Found overlapping legacy BSS: "
2795b9c547cSRui Paulo 			   MACSTR " freq=%d", MAC2STR(bss->bssid), bss->freq);
2805b9c547cSRui Paulo 		return 1;
2815b9c547cSRui Paulo 	}
2825b9c547cSRui Paulo 
283325151a3SRui Paulo 	if (elems.ht_operation) {
2845b9c547cSRui Paulo 		oper = (struct ieee80211_ht_operation *) elems.ht_operation;
2855b9c547cSRui Paulo 		if (oper->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)
2865b9c547cSRui Paulo 			return 0;
2875b9c547cSRui Paulo 
2885b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Found overlapping 20 MHz HT BSS: "
2895b9c547cSRui Paulo 			   MACSTR " freq=%d", MAC2STR(bss->bssid), bss->freq);
2905b9c547cSRui Paulo 		return 1;
2915b9c547cSRui Paulo 	}
2925b9c547cSRui Paulo 	return 0;
2935b9c547cSRui Paulo }
2945b9c547cSRui Paulo 
2955b9c547cSRui Paulo 
check_40mhz_2g4(struct hostapd_hw_modes * mode,struct wpa_scan_results * scan_res,int pri_chan,int sec_chan)29632a95656SCy Schubert int check_40mhz_2g4(struct hostapd_hw_modes *mode,
29732a95656SCy Schubert 		    struct wpa_scan_results *scan_res, int pri_chan,
29832a95656SCy Schubert 		    int sec_chan)
2995b9c547cSRui Paulo {
30032a95656SCy Schubert 	int pri_freq, sec_freq;
3015b9c547cSRui Paulo 	int affected_start, affected_end;
30232a95656SCy Schubert 	size_t i;
3035b9c547cSRui Paulo 
30432a95656SCy Schubert 	if (!mode || !scan_res || !pri_chan || !sec_chan ||
30532a95656SCy Schubert 	    pri_chan == sec_chan)
30632a95656SCy Schubert 		return 0;
30732a95656SCy Schubert 
30832a95656SCy Schubert 	pri_freq = hw_get_freq(mode, pri_chan);
30932a95656SCy Schubert 	sec_freq = hw_get_freq(mode, sec_chan);
3105b9c547cSRui Paulo 
3115b9c547cSRui Paulo 	affected_start = (pri_freq + sec_freq) / 2 - 25;
3125b9c547cSRui Paulo 	affected_end = (pri_freq + sec_freq) / 2 + 25;
31332a95656SCy Schubert 	wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
31432a95656SCy Schubert 		   affected_start, affected_end);
31532a95656SCy Schubert 	for (i = 0; i < scan_res->num; i++) {
31632a95656SCy Schubert 		struct wpa_scan_res *bss = scan_res->res[i];
31732a95656SCy Schubert 		int pri = bss->freq;
31832a95656SCy Schubert 		int sec = pri;
31932a95656SCy Schubert 		struct ieee802_11_elems elems;
3205b9c547cSRui Paulo 
3215b9c547cSRui Paulo 		/* Check for overlapping 20 MHz BSS */
32232a95656SCy Schubert 		if (check_20mhz_bss(bss, pri_freq, affected_start,
32332a95656SCy Schubert 				    affected_end)) {
32432a95656SCy Schubert 			wpa_printf(MSG_DEBUG,
32532a95656SCy Schubert 				   "Overlapping 20 MHz BSS is found");
32632a95656SCy Schubert 			return 0;
3275b9c547cSRui Paulo 		}
3285b9c547cSRui Paulo 
3295b9c547cSRui Paulo 		get_pri_sec_chan(bss, &pri_chan, &sec_chan);
3305b9c547cSRui Paulo 
3315b9c547cSRui Paulo 		if (sec_chan) {
3325b9c547cSRui Paulo 			if (sec_chan < pri_chan)
3335b9c547cSRui Paulo 				sec = pri - 20;
3345b9c547cSRui Paulo 			else
3355b9c547cSRui Paulo 				sec = pri + 20;
3365b9c547cSRui Paulo 		}
3375b9c547cSRui Paulo 
3385b9c547cSRui Paulo 		if ((pri < affected_start || pri > affected_end) &&
3395b9c547cSRui Paulo 		    (sec < affected_start || sec > affected_end))
34032a95656SCy Schubert 			continue; /* not within affected channel range */
3415b9c547cSRui Paulo 
3425b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "Neighboring BSS: " MACSTR
3435b9c547cSRui Paulo 			   " freq=%d pri=%d sec=%d",
3445b9c547cSRui Paulo 			   MAC2STR(bss->bssid), bss->freq, pri_chan, sec_chan);
3455b9c547cSRui Paulo 
3465b9c547cSRui Paulo 		if (sec_chan) {
3475b9c547cSRui Paulo 			if (pri_freq != pri || sec_freq != sec) {
3485b9c547cSRui Paulo 				wpa_printf(MSG_DEBUG,
3495b9c547cSRui Paulo 					   "40 MHz pri/sec mismatch with BSS "
3505b9c547cSRui Paulo 					   MACSTR
3515b9c547cSRui Paulo 					   " <%d,%d> (chan=%d%c) vs. <%d,%d>",
3525b9c547cSRui Paulo 					   MAC2STR(bss->bssid),
3535b9c547cSRui Paulo 					   pri, sec, pri_chan,
3545b9c547cSRui Paulo 					   sec > pri ? '+' : '-',
3555b9c547cSRui Paulo 					   pri_freq, sec_freq);
35632a95656SCy Schubert 				return 0;
3575b9c547cSRui Paulo 			}
3585b9c547cSRui Paulo 		}
3595b9c547cSRui Paulo 
36032a95656SCy Schubert 		ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems,
36132a95656SCy Schubert 				       0);
362325151a3SRui Paulo 		if (elems.ht_capabilities) {
3635b9c547cSRui Paulo 			struct ieee80211_ht_capabilities *ht_cap =
3645b9c547cSRui Paulo 				(struct ieee80211_ht_capabilities *)
3655b9c547cSRui Paulo 				elems.ht_capabilities;
3665b9c547cSRui Paulo 
3675b9c547cSRui Paulo 			if (le_to_host16(ht_cap->ht_capabilities_info) &
3685b9c547cSRui Paulo 			    HT_CAP_INFO_40MHZ_INTOLERANT) {
3695b9c547cSRui Paulo 				wpa_printf(MSG_DEBUG,
3705b9c547cSRui Paulo 					   "40 MHz Intolerant is set on channel %d in BSS "
3715b9c547cSRui Paulo 					   MACSTR, pri, MAC2STR(bss->bssid));
3725b9c547cSRui Paulo 				return 0;
3735b9c547cSRui Paulo 			}
37432a95656SCy Schubert 		}
3755b9c547cSRui Paulo 	}
3765b9c547cSRui Paulo 
3775b9c547cSRui Paulo 	return 1;
3785b9c547cSRui Paulo }
3795b9c547cSRui Paulo 
3805b9c547cSRui Paulo 
hostapd_set_freq_params(struct hostapd_freq_params * data,enum hostapd_hw_mode mode,int freq,int channel,int enable_edmg,u8 edmg_channel,int ht_enabled,int vht_enabled,int he_enabled,int sec_channel_offset,int oper_chwidth,int center_segment0,int center_segment1,u32 vht_caps,struct he_capabilities * he_cap)3815b9c547cSRui Paulo int hostapd_set_freq_params(struct hostapd_freq_params *data,
3825b9c547cSRui Paulo 			    enum hostapd_hw_mode mode,
383c1d255d3SCy Schubert 			    int freq, int channel, int enable_edmg,
384c1d255d3SCy Schubert 			    u8 edmg_channel, int ht_enabled,
385206b73d0SCy Schubert 			    int vht_enabled, int he_enabled,
386206b73d0SCy Schubert 			    int sec_channel_offset,
387206b73d0SCy Schubert 			    int oper_chwidth, int center_segment0,
388206b73d0SCy Schubert 			    int center_segment1, u32 vht_caps,
389206b73d0SCy Schubert 			    struct he_capabilities *he_cap)
3905b9c547cSRui Paulo {
3914b72b91aSCy Schubert 	if (!he_cap || !he_cap->he_supported)
392206b73d0SCy Schubert 		he_enabled = 0;
3935b9c547cSRui Paulo 	os_memset(data, 0, sizeof(*data));
3945b9c547cSRui Paulo 	data->mode = mode;
3955b9c547cSRui Paulo 	data->freq = freq;
3965b9c547cSRui Paulo 	data->channel = channel;
3975b9c547cSRui Paulo 	data->ht_enabled = ht_enabled;
3985b9c547cSRui Paulo 	data->vht_enabled = vht_enabled;
399206b73d0SCy Schubert 	data->he_enabled = he_enabled;
4005b9c547cSRui Paulo 	data->sec_channel_offset = sec_channel_offset;
4015b9c547cSRui Paulo 	data->center_freq1 = freq + sec_channel_offset * 10;
4025b9c547cSRui Paulo 	data->center_freq2 = 0;
4034b72b91aSCy Schubert 	if (oper_chwidth == CHANWIDTH_80MHZ)
4044b72b91aSCy Schubert 		data->bandwidth = 80;
4054b72b91aSCy Schubert 	else if (oper_chwidth == CHANWIDTH_160MHZ ||
4064b72b91aSCy Schubert 		 oper_chwidth == CHANWIDTH_80P80MHZ)
4074b72b91aSCy Schubert 		data->bandwidth = 160;
4084b72b91aSCy Schubert 	else if (sec_channel_offset)
4094b72b91aSCy Schubert 		data->bandwidth = 40;
4104b72b91aSCy Schubert 	else
4114b72b91aSCy Schubert 		data->bandwidth = 20;
4124b72b91aSCy Schubert 
4135b9c547cSRui Paulo 
414c1d255d3SCy Schubert 	hostapd_encode_edmg_chan(enable_edmg, edmg_channel, channel,
415c1d255d3SCy Schubert 				 &data->edmg);
416c1d255d3SCy Schubert 
417c1d255d3SCy Schubert 	if (is_6ghz_freq(freq)) {
418c1d255d3SCy Schubert 		if (!data->he_enabled) {
419c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
420c1d255d3SCy Schubert 				   "Can't set 6 GHz mode - HE isn't enabled");
4215b9c547cSRui Paulo 			return -1;
422c1d255d3SCy Schubert 		}
423c1d255d3SCy Schubert 
424c1d255d3SCy Schubert 		if (center_idx_to_bw_6ghz(channel) < 0) {
425c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
426c1d255d3SCy Schubert 				   "Invalid control channel for 6 GHz band");
427c1d255d3SCy Schubert 			return -1;
428c1d255d3SCy Schubert 		}
429c1d255d3SCy Schubert 
430c1d255d3SCy Schubert 		if (!center_segment0) {
431c1d255d3SCy Schubert 			if (center_segment1) {
432c1d255d3SCy Schubert 				wpa_printf(MSG_ERROR,
433c1d255d3SCy Schubert 					   "Segment 0 center frequency isn't set");
434c1d255d3SCy Schubert 				return -1;
435c1d255d3SCy Schubert 			}
4364b72b91aSCy Schubert 			if (!sec_channel_offset)
437c1d255d3SCy Schubert 				data->center_freq1 = data->freq;
438c1d255d3SCy Schubert 		} else {
439c1d255d3SCy Schubert 			int freq1, freq2 = 0;
440c1d255d3SCy Schubert 			int bw = center_idx_to_bw_6ghz(center_segment0);
441c1d255d3SCy Schubert 
442c1d255d3SCy Schubert 			if (bw < 0) {
443c1d255d3SCy Schubert 				wpa_printf(MSG_ERROR,
444c1d255d3SCy Schubert 					   "Invalid center frequency index for 6 GHz");
445c1d255d3SCy Schubert 				return -1;
446c1d255d3SCy Schubert 			}
447c1d255d3SCy Schubert 
448c1d255d3SCy Schubert 			freq1 = ieee80211_chan_to_freq(NULL, 131,
449c1d255d3SCy Schubert 						       center_segment0);
450c1d255d3SCy Schubert 			if (freq1 < 0) {
451c1d255d3SCy Schubert 				wpa_printf(MSG_ERROR,
452c1d255d3SCy Schubert 					   "Invalid segment 0 center frequency for 6 GHz");
453c1d255d3SCy Schubert 				return -1;
454c1d255d3SCy Schubert 			}
455c1d255d3SCy Schubert 
456c1d255d3SCy Schubert 			if (center_segment1) {
457c1d255d3SCy Schubert 				if (center_idx_to_bw_6ghz(center_segment1) != 2 ||
458c1d255d3SCy Schubert 				    bw != 2) {
459c1d255d3SCy Schubert 					wpa_printf(MSG_ERROR,
460c1d255d3SCy Schubert 						   "6 GHz 80+80 MHz configuration doesn't use valid 80 MHz channels");
461c1d255d3SCy Schubert 					return -1;
462c1d255d3SCy Schubert 				}
463c1d255d3SCy Schubert 
464c1d255d3SCy Schubert 				freq2 = ieee80211_chan_to_freq(NULL, 131,
465c1d255d3SCy Schubert 							       center_segment1);
466c1d255d3SCy Schubert 				if (freq2 < 0) {
467c1d255d3SCy Schubert 					wpa_printf(MSG_ERROR,
468c1d255d3SCy Schubert 						   "Invalid segment 1 center frequency for UHB");
469c1d255d3SCy Schubert 					return -1;
470c1d255d3SCy Schubert 				}
471c1d255d3SCy Schubert 			}
472c1d255d3SCy Schubert 
473c1d255d3SCy Schubert 			data->bandwidth = (1 << (u8) bw) * 20;
474c1d255d3SCy Schubert 			data->center_freq1 = freq1;
475c1d255d3SCy Schubert 			data->center_freq2 = freq2;
476c1d255d3SCy Schubert 		}
477c1d255d3SCy Schubert 		data->ht_enabled = 0;
478c1d255d3SCy Schubert 		data->vht_enabled = 0;
479c1d255d3SCy Schubert 
480c1d255d3SCy Schubert 		return 0;
481c1d255d3SCy Schubert 	}
482c1d255d3SCy Schubert 
483c1d255d3SCy Schubert 	if (data->he_enabled) switch (oper_chwidth) {
484c1d255d3SCy Schubert 	case CHANWIDTH_USE_HT:
4854b72b91aSCy Schubert 		if (sec_channel_offset == 0)
4864b72b91aSCy Schubert 			break;
4874b72b91aSCy Schubert 
4884b72b91aSCy Schubert 		if (mode == HOSTAPD_MODE_IEEE80211G) {
489c1d255d3SCy Schubert 			if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
490c1d255d3SCy Schubert 			      HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G)) {
491c1d255d3SCy Schubert 				wpa_printf(MSG_ERROR,
492c1d255d3SCy Schubert 					   "40 MHz channel width is not supported in 2.4 GHz");
493c1d255d3SCy Schubert 				return -1;
494c1d255d3SCy Schubert 			}
495c1d255d3SCy Schubert 			break;
496c1d255d3SCy Schubert 		}
497c1d255d3SCy Schubert 		/* fall through */
498c1d255d3SCy Schubert 	case CHANWIDTH_80MHZ:
499c1d255d3SCy Schubert 		if (mode == HOSTAPD_MODE_IEEE80211A) {
500c1d255d3SCy Schubert 			if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
501c1d255d3SCy Schubert 			      HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
502c1d255d3SCy Schubert 				wpa_printf(MSG_ERROR,
503c1d255d3SCy Schubert 					   "40/80 MHz channel width is not supported in 5/6 GHz");
504c1d255d3SCy Schubert 				return -1;
505c1d255d3SCy Schubert 			}
506c1d255d3SCy Schubert 		}
507c1d255d3SCy Schubert 		break;
508c1d255d3SCy Schubert 	case CHANWIDTH_80P80MHZ:
509c1d255d3SCy Schubert 		if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
510c1d255d3SCy Schubert 		      HE_PHYCAP_CHANNEL_WIDTH_SET_80PLUS80MHZ_IN_5G)) {
511c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
512c1d255d3SCy Schubert 				   "80+80 MHz channel width is not supported in 5/6 GHz");
513c1d255d3SCy Schubert 			return -1;
514c1d255d3SCy Schubert 		}
515c1d255d3SCy Schubert 		break;
516c1d255d3SCy Schubert 	case CHANWIDTH_160MHZ:
517c1d255d3SCy Schubert 		if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
518c1d255d3SCy Schubert 		      HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G)) {
519c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
520c1d255d3SCy Schubert 				   "160 MHz channel width is not supported in 5 / 6GHz");
521c1d255d3SCy Schubert 			return -1;
522c1d255d3SCy Schubert 		}
523c1d255d3SCy Schubert 		break;
524c1d255d3SCy Schubert 	} else if (data->vht_enabled) switch (oper_chwidth) {
525c1d255d3SCy Schubert 	case CHANWIDTH_USE_HT:
5265b9c547cSRui Paulo 		break;
527206b73d0SCy Schubert 	case CHANWIDTH_80P80MHZ:
5285b9c547cSRui Paulo 		if (!(vht_caps & VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)) {
5295b9c547cSRui Paulo 			wpa_printf(MSG_ERROR,
5305b9c547cSRui Paulo 				   "80+80 channel width is not supported!");
5315b9c547cSRui Paulo 			return -1;
5325b9c547cSRui Paulo 		}
533c1d255d3SCy Schubert 		/* fall through */
534c1d255d3SCy Schubert 	case CHANWIDTH_80MHZ:
535c1d255d3SCy Schubert 		break;
536c1d255d3SCy Schubert 	case CHANWIDTH_160MHZ:
537c1d255d3SCy Schubert 		if (!(vht_caps & (VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
538c1d255d3SCy Schubert 				  VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))) {
539c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
540c1d255d3SCy Schubert 				   "160 MHz channel width is not supported!");
5415b9c547cSRui Paulo 			return -1;
542c1d255d3SCy Schubert 		}
543c1d255d3SCy Schubert 		break;
544c1d255d3SCy Schubert 	}
545c1d255d3SCy Schubert 
546c1d255d3SCy Schubert 	if (data->he_enabled || data->vht_enabled) switch (oper_chwidth) {
547c1d255d3SCy Schubert 	case CHANWIDTH_USE_HT:
548c1d255d3SCy Schubert 		if (center_segment1 ||
549c1d255d3SCy Schubert 		    (center_segment0 != 0 &&
550c1d255d3SCy Schubert 		     5000 + center_segment0 * 5 != data->center_freq1 &&
551c1d255d3SCy Schubert 		     2407 + center_segment0 * 5 != data->center_freq1)) {
552c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
553c1d255d3SCy Schubert 				   "20/40 MHz: center segment 0 (=%d) and center freq 1 (=%d) not in sync",
554c1d255d3SCy Schubert 				   center_segment0, data->center_freq1);
555c1d255d3SCy Schubert 			return -1;
556c1d255d3SCy Schubert 		}
557c1d255d3SCy Schubert 		break;
558c1d255d3SCy Schubert 	case CHANWIDTH_80P80MHZ:
559c1d255d3SCy Schubert 		if (center_segment1 == center_segment0 + 4 ||
560c1d255d3SCy Schubert 		    center_segment1 == center_segment0 - 4) {
561c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
562c1d255d3SCy Schubert 				   "80+80 MHz: center segment 1 only 20 MHz apart");
563c1d255d3SCy Schubert 			return -1;
564c1d255d3SCy Schubert 		}
5655b9c547cSRui Paulo 		data->center_freq2 = 5000 + center_segment1 * 5;
5665b9c547cSRui Paulo 		/* fall through */
567206b73d0SCy Schubert 	case CHANWIDTH_80MHZ:
5685b9c547cSRui Paulo 		data->bandwidth = 80;
569c1d255d3SCy Schubert 		if (!sec_channel_offset) {
570c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
571c1d255d3SCy Schubert 				   "80/80+80 MHz: no second channel offset");
5725b9c547cSRui Paulo 			return -1;
573c1d255d3SCy Schubert 		}
574c1d255d3SCy Schubert 		if (oper_chwidth == CHANWIDTH_80MHZ && center_segment1) {
575c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
576c1d255d3SCy Schubert 				   "80 MHz: center segment 1 configured");
577c1d255d3SCy Schubert 			return -1;
578c1d255d3SCy Schubert 		}
579c1d255d3SCy Schubert 		if (oper_chwidth == CHANWIDTH_80P80MHZ && !center_segment1) {
580c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
581c1d255d3SCy Schubert 				   "80+80 MHz: center segment 1 not configured");
582c1d255d3SCy Schubert 			return -1;
583c1d255d3SCy Schubert 		}
584325151a3SRui Paulo 		if (!center_segment0) {
585325151a3SRui Paulo 			if (channel <= 48)
586325151a3SRui Paulo 				center_segment0 = 42;
587325151a3SRui Paulo 			else if (channel <= 64)
588325151a3SRui Paulo 				center_segment0 = 58;
589325151a3SRui Paulo 			else if (channel <= 112)
590325151a3SRui Paulo 				center_segment0 = 106;
591325151a3SRui Paulo 			else if (channel <= 128)
592325151a3SRui Paulo 				center_segment0 = 122;
593325151a3SRui Paulo 			else if (channel <= 144)
594325151a3SRui Paulo 				center_segment0 = 138;
595325151a3SRui Paulo 			else if (channel <= 161)
596325151a3SRui Paulo 				center_segment0 = 155;
597c1d255d3SCy Schubert 			else if (channel <= 177)
598c1d255d3SCy Schubert 				center_segment0 = 171;
5995b9c547cSRui Paulo 			data->center_freq1 = 5000 + center_segment0 * 5;
600325151a3SRui Paulo 		} else {
601325151a3SRui Paulo 			/*
602325151a3SRui Paulo 			 * Note: HT/VHT config and params are coupled. Check if
603325151a3SRui Paulo 			 * HT40 channel band is in VHT80 Pri channel band
604325151a3SRui Paulo 			 * configuration.
605325151a3SRui Paulo 			 */
606325151a3SRui Paulo 			if (center_segment0 == channel + 6 ||
607325151a3SRui Paulo 			    center_segment0 == channel + 2 ||
608325151a3SRui Paulo 			    center_segment0 == channel - 2 ||
609325151a3SRui Paulo 			    center_segment0 == channel - 6)
610325151a3SRui Paulo 				data->center_freq1 = 5000 + center_segment0 * 5;
611c1d255d3SCy Schubert 			else {
612c1d255d3SCy Schubert 				wpa_printf(MSG_ERROR,
613c1d255d3SCy Schubert 					   "Wrong coupling between HT and VHT/HE channel setting");
614325151a3SRui Paulo 				return -1;
615325151a3SRui Paulo 			}
616c1d255d3SCy Schubert 		}
6175b9c547cSRui Paulo 		break;
618206b73d0SCy Schubert 	case CHANWIDTH_160MHZ:
6195b9c547cSRui Paulo 		data->bandwidth = 160;
620c1d255d3SCy Schubert 		if (center_segment1) {
6215b9c547cSRui Paulo 			wpa_printf(MSG_ERROR,
622c1d255d3SCy Schubert 				   "160 MHz: center segment 1 should not be set");
6235b9c547cSRui Paulo 			return -1;
6245b9c547cSRui Paulo 		}
625c1d255d3SCy Schubert 		if (!sec_channel_offset) {
626c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
627c1d255d3SCy Schubert 				   "160 MHz: second channel offset not set");
6285b9c547cSRui Paulo 			return -1;
629c1d255d3SCy Schubert 		}
630325151a3SRui Paulo 		/*
631325151a3SRui Paulo 		 * Note: HT/VHT config and params are coupled. Check if
632325151a3SRui Paulo 		 * HT40 channel band is in VHT160 channel band configuration.
633325151a3SRui Paulo 		 */
634325151a3SRui Paulo 		if (center_segment0 == channel + 14 ||
635325151a3SRui Paulo 		    center_segment0 == channel + 10 ||
636325151a3SRui Paulo 		    center_segment0 == channel + 6 ||
637325151a3SRui Paulo 		    center_segment0 == channel + 2 ||
638325151a3SRui Paulo 		    center_segment0 == channel - 2 ||
639325151a3SRui Paulo 		    center_segment0 == channel - 6 ||
640325151a3SRui Paulo 		    center_segment0 == channel - 10 ||
641325151a3SRui Paulo 		    center_segment0 == channel - 14)
6425b9c547cSRui Paulo 			data->center_freq1 = 5000 + center_segment0 * 5;
643c1d255d3SCy Schubert 		else {
644c1d255d3SCy Schubert 			wpa_printf(MSG_ERROR,
645c1d255d3SCy Schubert 				   "160 MHz: HT40 channel band is not in 160 MHz band");
646325151a3SRui Paulo 			return -1;
647c1d255d3SCy Schubert 		}
6485b9c547cSRui Paulo 		break;
6495b9c547cSRui Paulo 	}
6505b9c547cSRui Paulo 
6515b9c547cSRui Paulo 	return 0;
6525b9c547cSRui Paulo }
65385732ac8SCy Schubert 
65485732ac8SCy Schubert 
set_disable_ht40(struct ieee80211_ht_capabilities * htcaps,int disabled)65585732ac8SCy Schubert void set_disable_ht40(struct ieee80211_ht_capabilities *htcaps,
65685732ac8SCy Schubert 		      int disabled)
65785732ac8SCy Schubert {
65885732ac8SCy Schubert 	/* Masking these out disables HT40 */
65985732ac8SCy Schubert 	le16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
66085732ac8SCy Schubert 				HT_CAP_INFO_SHORT_GI40MHZ);
66185732ac8SCy Schubert 
66285732ac8SCy Schubert 	if (disabled)
66385732ac8SCy Schubert 		htcaps->ht_capabilities_info &= ~msk;
66485732ac8SCy Schubert 	else
66585732ac8SCy Schubert 		htcaps->ht_capabilities_info |= msk;
66685732ac8SCy Schubert }
66785732ac8SCy Schubert 
66885732ac8SCy Schubert 
66985732ac8SCy Schubert #ifdef CONFIG_IEEE80211AC
67085732ac8SCy Schubert 
_ieee80211ac_cap_check(u32 hw,u32 conf,u32 cap,const char * name)67185732ac8SCy Schubert static int _ieee80211ac_cap_check(u32 hw, u32 conf, u32 cap,
67285732ac8SCy Schubert 				  const char *name)
67385732ac8SCy Schubert {
67485732ac8SCy Schubert 	u32 req_cap = conf & cap;
67585732ac8SCy Schubert 
67685732ac8SCy Schubert 	/*
67785732ac8SCy Schubert 	 * Make sure we support all requested capabilities.
67885732ac8SCy Schubert 	 * NOTE: We assume that 'cap' represents a capability mask,
67985732ac8SCy Schubert 	 * not a discrete value.
68085732ac8SCy Schubert 	 */
68185732ac8SCy Schubert 	if ((hw & req_cap) != req_cap) {
68285732ac8SCy Schubert 		wpa_printf(MSG_ERROR,
68385732ac8SCy Schubert 			   "Driver does not support configured VHT capability [%s]",
68485732ac8SCy Schubert 			   name);
68585732ac8SCy Schubert 		return 0;
68685732ac8SCy Schubert 	}
68785732ac8SCy Schubert 	return 1;
68885732ac8SCy Schubert }
68985732ac8SCy Schubert 
69085732ac8SCy Schubert 
ieee80211ac_cap_check_max(u32 hw,u32 conf,u32 mask,unsigned int shift,const char * name)69185732ac8SCy Schubert static int ieee80211ac_cap_check_max(u32 hw, u32 conf, u32 mask,
69285732ac8SCy Schubert 				     unsigned int shift,
69385732ac8SCy Schubert 				     const char *name)
69485732ac8SCy Schubert {
69585732ac8SCy Schubert 	u32 hw_max = hw & mask;
69685732ac8SCy Schubert 	u32 conf_val = conf & mask;
69785732ac8SCy Schubert 
69885732ac8SCy Schubert 	if (conf_val > hw_max) {
69985732ac8SCy Schubert 		wpa_printf(MSG_ERROR,
70085732ac8SCy Schubert 			   "Configured VHT capability [%s] exceeds max value supported by the driver (%d > %d)",
70185732ac8SCy Schubert 			   name, conf_val >> shift, hw_max >> shift);
70285732ac8SCy Schubert 		return 0;
70385732ac8SCy Schubert 	}
70485732ac8SCy Schubert 	return 1;
70585732ac8SCy Schubert }
70685732ac8SCy Schubert 
70785732ac8SCy Schubert 
ieee80211ac_cap_check(u32 hw,u32 conf)70885732ac8SCy Schubert int ieee80211ac_cap_check(u32 hw, u32 conf)
70985732ac8SCy Schubert {
71085732ac8SCy Schubert #define VHT_CAP_CHECK(cap) \
71185732ac8SCy Schubert 	do { \
71285732ac8SCy Schubert 		if (!_ieee80211ac_cap_check(hw, conf, cap, #cap)) \
71385732ac8SCy Schubert 			return 0; \
71485732ac8SCy Schubert 	} while (0)
71585732ac8SCy Schubert 
71685732ac8SCy Schubert #define VHT_CAP_CHECK_MAX(cap) \
71785732ac8SCy Schubert 	do { \
71885732ac8SCy Schubert 		if (!ieee80211ac_cap_check_max(hw, conf, cap, cap ## _SHIFT, \
71985732ac8SCy Schubert 					       #cap)) \
72085732ac8SCy Schubert 			return 0; \
72185732ac8SCy Schubert 	} while (0)
72285732ac8SCy Schubert 
72385732ac8SCy Schubert 	VHT_CAP_CHECK_MAX(VHT_CAP_MAX_MPDU_LENGTH_MASK);
72485732ac8SCy Schubert 	VHT_CAP_CHECK_MAX(VHT_CAP_SUPP_CHAN_WIDTH_MASK);
72585732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_RXLDPC);
72685732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_SHORT_GI_80);
72785732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_SHORT_GI_160);
72885732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_TXSTBC);
72985732ac8SCy Schubert 	VHT_CAP_CHECK_MAX(VHT_CAP_RXSTBC_MASK);
73085732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMER_CAPABLE);
73185732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMEE_CAPABLE);
73285732ac8SCy Schubert 	VHT_CAP_CHECK_MAX(VHT_CAP_BEAMFORMEE_STS_MAX);
73385732ac8SCy Schubert 	VHT_CAP_CHECK_MAX(VHT_CAP_SOUNDING_DIMENSION_MAX);
73485732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMER_CAPABLE);
73585732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMEE_CAPABLE);
73685732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_VHT_TXOP_PS);
73785732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_HTC_VHT);
73885732ac8SCy Schubert 	VHT_CAP_CHECK_MAX(VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX);
73985732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_UNSOL_MFB);
74085732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_MRQ_MFB);
74185732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_RX_ANTENNA_PATTERN);
74285732ac8SCy Schubert 	VHT_CAP_CHECK(VHT_CAP_TX_ANTENNA_PATTERN);
74385732ac8SCy Schubert 
74485732ac8SCy Schubert #undef VHT_CAP_CHECK
74585732ac8SCy Schubert #undef VHT_CAP_CHECK_MAX
74685732ac8SCy Schubert 
74785732ac8SCy Schubert 	return 1;
74885732ac8SCy Schubert }
74985732ac8SCy Schubert 
75085732ac8SCy Schubert #endif /* CONFIG_IEEE80211AC */
7514bc52338SCy Schubert 
7524bc52338SCy Schubert 
num_chan_to_bw(int num_chans)7534bc52338SCy Schubert u32 num_chan_to_bw(int num_chans)
7544bc52338SCy Schubert {
7554bc52338SCy Schubert 	switch (num_chans) {
7564bc52338SCy Schubert 	case 2:
7574bc52338SCy Schubert 	case 4:
7584bc52338SCy Schubert 	case 8:
7594bc52338SCy Schubert 		return num_chans * 20;
7604bc52338SCy Schubert 	default:
7614bc52338SCy Schubert 		return 20;
7624bc52338SCy Schubert 	}
7634bc52338SCy Schubert }
7644bc52338SCy Schubert 
7654bc52338SCy Schubert 
7664bc52338SCy Schubert /* check if BW is applicable for channel */
chan_bw_allowed(const struct hostapd_channel_data * chan,u32 bw,int ht40_plus,int pri)7674bc52338SCy Schubert int chan_bw_allowed(const struct hostapd_channel_data *chan, u32 bw,
7684bc52338SCy Schubert 		    int ht40_plus, int pri)
7694bc52338SCy Schubert {
7704bc52338SCy Schubert 	u32 bw_mask;
7714bc52338SCy Schubert 
7724bc52338SCy Schubert 	switch (bw) {
7734bc52338SCy Schubert 	case 20:
7744bc52338SCy Schubert 		bw_mask = HOSTAPD_CHAN_WIDTH_20;
7754bc52338SCy Schubert 		break;
7764bc52338SCy Schubert 	case 40:
7774bc52338SCy Schubert 		/* HT 40 MHz support declared only for primary channel,
7784bc52338SCy Schubert 		 * just skip 40 MHz secondary checking */
7794bc52338SCy Schubert 		if (pri && ht40_plus)
7804bc52338SCy Schubert 			bw_mask = HOSTAPD_CHAN_WIDTH_40P;
7814bc52338SCy Schubert 		else if (pri && !ht40_plus)
7824bc52338SCy Schubert 			bw_mask = HOSTAPD_CHAN_WIDTH_40M;
7834bc52338SCy Schubert 		else
7844bc52338SCy Schubert 			bw_mask = 0;
7854bc52338SCy Schubert 		break;
7864bc52338SCy Schubert 	case 80:
7874bc52338SCy Schubert 		bw_mask = HOSTAPD_CHAN_WIDTH_80;
7884bc52338SCy Schubert 		break;
7894bc52338SCy Schubert 	case 160:
7904bc52338SCy Schubert 		bw_mask = HOSTAPD_CHAN_WIDTH_160;
7914bc52338SCy Schubert 		break;
7924bc52338SCy Schubert 	default:
7934bc52338SCy Schubert 		bw_mask = 0;
7944bc52338SCy Schubert 		break;
7954bc52338SCy Schubert 	}
7964bc52338SCy Schubert 
7974bc52338SCy Schubert 	return (chan->allowed_bw & bw_mask) == bw_mask;
7984bc52338SCy Schubert }
7994bc52338SCy Schubert 
8004bc52338SCy Schubert 
8014bc52338SCy Schubert /* check if channel is allowed to be used as primary */
chan_pri_allowed(const struct hostapd_channel_data * chan)8024bc52338SCy Schubert int chan_pri_allowed(const struct hostapd_channel_data *chan)
8034bc52338SCy Schubert {
8044bc52338SCy Schubert 	return !(chan->flag & HOSTAPD_CHAN_DISABLED) &&
8054bc52338SCy Schubert 		(chan->allowed_bw & HOSTAPD_CHAN_WIDTH_20);
8064bc52338SCy Schubert }
807