Home | History | Annotate | Line # | Download | only in common
      1 /*
      2  * Common hostapd/wpa_supplicant HW features
      3  * Copyright (c) 2002-2013, Jouni Malinen <j (at) w1.fi>
      4  * Copyright (c) 2015, Qualcomm Atheros, Inc.
      5  *
      6  * This software may be distributed under the terms of the BSD license.
      7  * See README for more details.
      8  */
      9 
     10 #include "includes.h"
     11 
     12 #include "common.h"
     13 #include "defs.h"
     14 #include "ieee802_11_defs.h"
     15 #include "ieee802_11_common.h"
     16 #include "hw_features_common.h"
     17 
     18 
     19 struct hostapd_channel_data * hw_get_channel_chan(struct hostapd_hw_modes *mode,
     20 						  int chan, int *freq)
     21 {
     22 	int i;
     23 
     24 	if (freq)
     25 		*freq = 0;
     26 
     27 	if (!mode)
     28 		return NULL;
     29 
     30 	for (i = 0; i < mode->num_channels; i++) {
     31 		struct hostapd_channel_data *ch = &mode->channels[i];
     32 		if (ch->chan == chan) {
     33 			if (freq)
     34 				*freq = ch->freq;
     35 			return ch;
     36 		}
     37 	}
     38 
     39 	return NULL;
     40 }
     41 
     42 
     43 struct hostapd_channel_data *
     44 hw_mode_get_channel(struct hostapd_hw_modes *mode, int freq, int *chan)
     45 {
     46 	int i;
     47 
     48 	for (i = 0; i < mode->num_channels; i++) {
     49 		struct hostapd_channel_data *ch = &mode->channels[i];
     50 
     51 		if (ch->freq == freq) {
     52 			if (chan)
     53 				*chan = ch->chan;
     54 			return ch;
     55 		}
     56 	}
     57 
     58 	return NULL;
     59 }
     60 
     61 
     62 struct hostapd_channel_data *
     63 hw_get_channel_freq(enum hostapd_hw_mode mode, int freq, int *chan,
     64 		    struct hostapd_hw_modes *hw_features, int num_hw_features)
     65 {
     66 	struct hostapd_channel_data *chan_data;
     67 	int i;
     68 
     69 	if (chan)
     70 		*chan = 0;
     71 
     72 	if (!hw_features)
     73 		return NULL;
     74 
     75 	for (i = 0; i < num_hw_features; i++) {
     76 		struct hostapd_hw_modes *curr_mode = &hw_features[i];
     77 
     78 		if (curr_mode->mode != mode)
     79 			continue;
     80 
     81 		chan_data = hw_mode_get_channel(curr_mode, freq, chan);
     82 		if (chan_data)
     83 			return chan_data;
     84 	}
     85 
     86 	return NULL;
     87 }
     88 
     89 
     90 int hw_get_freq(struct hostapd_hw_modes *mode, int chan)
     91 {
     92 	int freq;
     93 
     94 	hw_get_channel_chan(mode, chan, &freq);
     95 
     96 	return freq;
     97 }
     98 
     99 
    100 int hw_get_chan(enum hostapd_hw_mode mode, int freq,
    101 		struct hostapd_hw_modes *hw_features, int num_hw_features)
    102 {
    103 	int chan;
    104 
    105 	hw_get_channel_freq(mode, freq, &chan, hw_features, num_hw_features);
    106 
    107 	return chan;
    108 }
    109 
    110 
    111 int allowed_ht40_channel_pair(enum hostapd_hw_mode mode,
    112 			      struct hostapd_channel_data *p_chan,
    113 			      struct hostapd_channel_data *s_chan)
    114 {
    115 	int ok, first;
    116 	int allowed[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 140,
    117 			  149, 157, 165, 173, 184, 192 };
    118 	size_t k;
    119 	int ht40_plus, pri_chan, sec_chan;
    120 
    121 	if (!p_chan || !s_chan)
    122 		return 0;
    123 	pri_chan = p_chan->chan;
    124 	sec_chan = s_chan->chan;
    125 
    126 	ht40_plus = pri_chan < sec_chan;
    127 
    128 	if (pri_chan == sec_chan || !sec_chan) {
    129 		if (chan_pri_allowed(p_chan))
    130 			return 1; /* HT40 not used */
    131 
    132 		wpa_printf(MSG_ERROR, "Channel %d is not allowed as primary",
    133 			   pri_chan);
    134 		return 0;
    135 	}
    136 
    137 	wpa_printf(MSG_DEBUG,
    138 		   "HT40: control channel: %d (%d MHz), secondary channel: %d (%d MHz)",
    139 		   pri_chan, p_chan->freq, sec_chan, s_chan->freq);
    140 
    141 	/* Verify that HT40 secondary channel is an allowed 20 MHz
    142 	 * channel */
    143 	if ((s_chan->flag & HOSTAPD_CHAN_DISABLED) ||
    144 	    (ht40_plus && !(p_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40P)) ||
    145 	    (!ht40_plus && !(p_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40M))) {
    146 		wpa_printf(MSG_ERROR, "HT40 secondary channel %d not allowed",
    147 			   sec_chan);
    148 		return 0;
    149 	}
    150 
    151 	/*
    152 	 * Verify that HT40 primary,secondary channel pair is allowed per
    153 	 * IEEE 802.11n Annex J. This is only needed for 5 GHz band since
    154 	 * 2.4 GHz rules allow all cases where the secondary channel fits into
    155 	 * the list of allowed channels (already checked above).
    156 	 */
    157 	if (mode != HOSTAPD_MODE_IEEE80211A)
    158 		return 1;
    159 
    160 	first = pri_chan < sec_chan ? pri_chan : sec_chan;
    161 
    162 	ok = 0;
    163 	for (k = 0; k < ARRAY_SIZE(allowed); k++) {
    164 		if (first == allowed[k]) {
    165 			ok = 1;
    166 			break;
    167 		}
    168 	}
    169 	if (!ok) {
    170 		wpa_printf(MSG_ERROR, "HT40 channel pair (%d, %d) not allowed",
    171 			   pri_chan, sec_chan);
    172 		return 0;
    173 	}
    174 
    175 	return 1;
    176 }
    177 
    178 
    179 void get_pri_sec_chan(struct wpa_scan_res *bss, int *pri_chan, int *sec_chan)
    180 {
    181 	struct ieee80211_ht_operation *oper;
    182 	struct ieee802_11_elems elems;
    183 
    184 	*pri_chan = *sec_chan = 0;
    185 
    186 	if (ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0) !=
    187 	    ParseFailed && elems.ht_operation) {
    188 		oper = (struct ieee80211_ht_operation *) elems.ht_operation;
    189 		*pri_chan = oper->primary_chan;
    190 		if (oper->ht_param & HT_INFO_HT_PARAM_STA_CHNL_WIDTH) {
    191 			int sec = oper->ht_param &
    192 				HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
    193 			if (sec == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
    194 				*sec_chan = *pri_chan + 4;
    195 			else if (sec == HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
    196 				*sec_chan = *pri_chan - 4;
    197 		}
    198 	}
    199 }
    200 
    201 
    202 int check_40mhz_5g(struct wpa_scan_results *scan_res,
    203 		   struct hostapd_channel_data *pri_chan,
    204 		   struct hostapd_channel_data *sec_chan)
    205 {
    206 	int pri_bss, sec_bss;
    207 	int bss_pri_chan, bss_sec_chan;
    208 	size_t i;
    209 	int match;
    210 
    211 	if (!scan_res || !pri_chan || !sec_chan ||
    212 	    pri_chan->freq == sec_chan->freq)
    213 		return 0;
    214 
    215 	/*
    216 	 * Switch PRI/SEC channels if Beacons were detected on selected SEC
    217 	 * channel, but not on selected PRI channel.
    218 	 */
    219 	pri_bss = sec_bss = 0;
    220 	for (i = 0; i < scan_res->num; i++) {
    221 		struct wpa_scan_res *bss = scan_res->res[i];
    222 		if (bss->freq == pri_chan->freq)
    223 			pri_bss++;
    224 		else if (bss->freq == sec_chan->freq)
    225 			sec_bss++;
    226 	}
    227 	if (sec_bss && !pri_bss) {
    228 		wpa_printf(MSG_INFO,
    229 			   "Switch own primary and secondary channel to get secondary channel with no Beacons from other BSSes");
    230 		return 2;
    231 	}
    232 
    233 	/*
    234 	 * Match PRI/SEC channel with any existing HT40 BSS on the same
    235 	 * channels that we are about to use (if already mixed order in
    236 	 * existing BSSes, use own preference).
    237 	 */
    238 	match = 0;
    239 	for (i = 0; i < scan_res->num; i++) {
    240 		struct wpa_scan_res *bss = scan_res->res[i];
    241 		get_pri_sec_chan(bss, &bss_pri_chan, &bss_sec_chan);
    242 		if (pri_chan->chan == bss_pri_chan &&
    243 		    sec_chan->chan == bss_sec_chan) {
    244 			match = 1;
    245 			break;
    246 		}
    247 	}
    248 	if (!match) {
    249 		for (i = 0; i < scan_res->num; i++) {
    250 			struct wpa_scan_res *bss = scan_res->res[i];
    251 			get_pri_sec_chan(bss, &bss_pri_chan, &bss_sec_chan);
    252 			if (pri_chan->chan == bss_sec_chan &&
    253 			    sec_chan->chan == bss_pri_chan) {
    254 				wpa_printf(MSG_INFO, "Switch own primary and "
    255 					   "secondary channel due to BSS "
    256 					   "overlap with " MACSTR,
    257 					   MAC2STR(bss->bssid));
    258 				return 2;
    259 			}
    260 		}
    261 	}
    262 
    263 	return 1;
    264 }
    265 
    266 
    267 static int check_20mhz_bss(struct wpa_scan_res *bss, int pri_freq, int start,
    268 			   int end)
    269 {
    270 	struct ieee802_11_elems elems;
    271 	struct ieee80211_ht_operation *oper;
    272 
    273 	if (bss->freq < start || bss->freq > end || bss->freq == pri_freq)
    274 		return 0;
    275 
    276 	if (ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0) ==
    277 	    ParseFailed)
    278 		return 0;
    279 
    280 	if (!elems.ht_capabilities) {
    281 		wpa_printf(MSG_DEBUG, "Found overlapping legacy BSS: "
    282 			   MACSTR " freq=%d", MAC2STR(bss->bssid), bss->freq);
    283 		return 1;
    284 	}
    285 
    286 	if (elems.ht_operation) {
    287 		oper = (struct ieee80211_ht_operation *) elems.ht_operation;
    288 		if (oper->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)
    289 			return 0;
    290 
    291 		wpa_printf(MSG_DEBUG, "Found overlapping 20 MHz HT BSS: "
    292 			   MACSTR " freq=%d", MAC2STR(bss->bssid), bss->freq);
    293 		return 1;
    294 	}
    295 	return 0;
    296 }
    297 
    298 
    299 int check_40mhz_2g4(struct hostapd_hw_modes *mode,
    300 		    struct wpa_scan_results *scan_res, int pri_chan,
    301 		    int sec_chan)
    302 {
    303 	int pri_freq, sec_freq;
    304 	int affected_start, affected_end;
    305 	size_t i;
    306 
    307 	if (!mode || !scan_res || !pri_chan || !sec_chan ||
    308 	    pri_chan == sec_chan)
    309 		return 0;
    310 
    311 	pri_freq = hw_get_freq(mode, pri_chan);
    312 	sec_freq = hw_get_freq(mode, sec_chan);
    313 
    314 	affected_start = (pri_freq + sec_freq) / 2 - 25;
    315 	affected_end = (pri_freq + sec_freq) / 2 + 25;
    316 	wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
    317 		   affected_start, affected_end);
    318 	for (i = 0; i < scan_res->num; i++) {
    319 		struct wpa_scan_res *bss = scan_res->res[i];
    320 		int pri = bss->freq;
    321 		int sec = pri;
    322 		struct ieee802_11_elems elems;
    323 
    324 		/* Check for overlapping 20 MHz BSS */
    325 		if (check_20mhz_bss(bss, pri_freq, affected_start,
    326 				    affected_end)) {
    327 			wpa_printf(MSG_DEBUG,
    328 				   "Overlapping 20 MHz BSS is found");
    329 			return 0;
    330 		}
    331 
    332 		get_pri_sec_chan(bss, &pri_chan, &sec_chan);
    333 
    334 		if (sec_chan) {
    335 			if (sec_chan < pri_chan)
    336 				sec = pri - 20;
    337 			else
    338 				sec = pri + 20;
    339 		}
    340 
    341 		if ((pri < affected_start || pri > affected_end) &&
    342 		    (sec < affected_start || sec > affected_end))
    343 			continue; /* not within affected channel range */
    344 
    345 		wpa_printf(MSG_DEBUG, "Neighboring BSS: " MACSTR
    346 			   " freq=%d pri=%d sec=%d",
    347 			   MAC2STR(bss->bssid), bss->freq, pri_chan, sec_chan);
    348 
    349 		if (sec_chan) {
    350 			if (pri_freq != pri || sec_freq != sec) {
    351 				wpa_printf(MSG_DEBUG,
    352 					   "40 MHz pri/sec mismatch with BSS "
    353 					   MACSTR
    354 					   " <%d,%d> (chan=%d%c) vs. <%d,%d>",
    355 					   MAC2STR(bss->bssid),
    356 					   pri, sec, pri_chan,
    357 					   sec > pri ? '+' : '-',
    358 					   pri_freq, sec_freq);
    359 				return 0;
    360 			}
    361 		}
    362 
    363 		if (ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len,
    364 					   &elems, 0) != ParseFailed &&
    365 		    elems.ht_capabilities) {
    366 			struct ieee80211_ht_capabilities *ht_cap =
    367 				(struct ieee80211_ht_capabilities *)
    368 				elems.ht_capabilities;
    369 
    370 			if (le_to_host16(ht_cap->ht_capabilities_info) &
    371 			    HT_CAP_INFO_40MHZ_INTOLERANT) {
    372 				wpa_printf(MSG_DEBUG,
    373 					   "40 MHz Intolerant is set on channel %d in BSS "
    374 					   MACSTR, pri, MAC2STR(bss->bssid));
    375 				return 0;
    376 			}
    377 		}
    378 	}
    379 
    380 	return 1;
    381 }
    382 
    383 
    384 static void punct_update_legacy_bw_80(u8 bitmap, u8 pri_chan, u8 *seg0)
    385 {
    386 	u8 first_chan = *seg0 - 6, sec_chan;
    387 
    388 	switch (bitmap) {
    389 	case 0x6:
    390 		*seg0 = 0;
    391 		return;
    392 	case 0x8:
    393 	case 0x4:
    394 	case 0x2:
    395 	case 0x1:
    396 	case 0xC:
    397 	case 0x3:
    398 		if (pri_chan < *seg0)
    399 			*seg0 -= 4;
    400 		else
    401 			*seg0 += 4;
    402 		break;
    403 	}
    404 
    405 	if (pri_chan < *seg0)
    406 		sec_chan = pri_chan + 4;
    407 	else
    408 		sec_chan = pri_chan - 4;
    409 
    410 	if (bitmap & BIT((sec_chan - first_chan) / 4))
    411 		*seg0 = 0;
    412 }
    413 
    414 
    415 static void punct_update_legacy_bw_160(u8 bitmap, u8 pri,
    416 				       enum oper_chan_width *width, u8 *seg0)
    417 {
    418 	if (pri < *seg0) {
    419 		*seg0 -= 8;
    420 		if (bitmap & 0x0F) {
    421 			*width = 0;
    422 			punct_update_legacy_bw_80(bitmap & 0xF, pri, seg0);
    423 		}
    424 	} else {
    425 		*seg0 += 8;
    426 		if (bitmap & 0xF0) {
    427 			*width = 0;
    428 			punct_update_legacy_bw_80((bitmap & 0xF0) >> 4, pri,
    429 						  seg0);
    430 		}
    431 	}
    432 }
    433 
    434 
    435 void punct_update_legacy_bw(u16 bitmap, u8 pri, enum oper_chan_width *width,
    436 			    u8 *seg0, u8 *seg1)
    437 {
    438 	if (*width == CONF_OPER_CHWIDTH_80MHZ && (bitmap & 0xF)) {
    439 		*width = CONF_OPER_CHWIDTH_USE_HT;
    440 		punct_update_legacy_bw_80(bitmap & 0xF, pri, seg0);
    441 	}
    442 
    443 	if (*width == CONF_OPER_CHWIDTH_160MHZ && (bitmap & 0xFF)) {
    444 		*width = CONF_OPER_CHWIDTH_80MHZ;
    445 		*seg1 = 0;
    446 		punct_update_legacy_bw_160(bitmap & 0xFF, pri, width, seg0);
    447 	}
    448 
    449 	/* TODO: 320 MHz */
    450 }
    451 
    452 
    453 int hostapd_set_freq_params(struct hostapd_freq_params *data,
    454 			    enum hostapd_hw_mode mode,
    455 			    int freq, int channel, int enable_edmg,
    456 			    u8 edmg_channel, int ht_enabled,
    457 			    int vht_enabled, int he_enabled,
    458 			    bool eht_enabled, int sec_channel_offset,
    459 			    enum oper_chan_width oper_chwidth,
    460 			    int center_segment0,
    461 			    int center_segment1, u32 vht_caps,
    462 			    struct he_capabilities *he_cap,
    463 			    struct eht_capabilities *eht_cap,
    464 			    u16 punct_bitmap)
    465 {
    466 	enum oper_chan_width oper_chwidth_legacy;
    467 	u8 seg0_legacy, seg1_legacy;
    468 
    469 	if (!he_cap || !he_cap->he_supported)
    470 		he_enabled = 0;
    471 	if (!eht_cap || !eht_cap->eht_supported)
    472 		eht_enabled = 0;
    473 	os_memset(data, 0, sizeof(*data));
    474 	data->mode = mode;
    475 	data->freq = freq;
    476 	data->channel = channel;
    477 	data->ht_enabled = ht_enabled;
    478 	data->vht_enabled = vht_enabled;
    479 	data->he_enabled = he_enabled;
    480 	data->eht_enabled = eht_enabled;
    481 	data->sec_channel_offset = sec_channel_offset;
    482 	data->center_freq1 = freq + sec_channel_offset * 10;
    483 	data->center_freq2 = 0;
    484 	if (oper_chwidth == CONF_OPER_CHWIDTH_80MHZ)
    485 		data->bandwidth = 80;
    486 	else if (oper_chwidth == CONF_OPER_CHWIDTH_160MHZ ||
    487 		 oper_chwidth == CONF_OPER_CHWIDTH_80P80MHZ)
    488 		data->bandwidth = 160;
    489 	else if (oper_chwidth == CONF_OPER_CHWIDTH_320MHZ)
    490 		data->bandwidth = 320;
    491 	else if (sec_channel_offset)
    492 		data->bandwidth = 40;
    493 	else
    494 		data->bandwidth = 20;
    495 
    496 
    497 	hostapd_encode_edmg_chan(enable_edmg, edmg_channel, channel,
    498 				 &data->edmg);
    499 
    500 	if (is_6ghz_freq(freq)) {
    501 		if (!data->he_enabled && !data->eht_enabled) {
    502 			wpa_printf(MSG_ERROR,
    503 				   "Can't set 6 GHz mode - HE or EHT aren't enabled");
    504 			return -1;
    505 		}
    506 
    507 		if (center_idx_to_bw_6ghz(channel) < 0) {
    508 			wpa_printf(MSG_ERROR,
    509 				   "Invalid control channel for 6 GHz band");
    510 			return -1;
    511 		}
    512 
    513 		if (!center_segment0) {
    514 			if (center_segment1) {
    515 				wpa_printf(MSG_ERROR,
    516 					   "Segment 0 center frequency isn't set");
    517 				return -1;
    518 			}
    519 			if (!sec_channel_offset)
    520 				data->center_freq1 = data->freq;
    521 		} else {
    522 			int freq1, freq2 = 0;
    523 			int bw = center_idx_to_bw_6ghz(center_segment0);
    524 			int opclass;
    525 
    526 			if (bw < 0) {
    527 				wpa_printf(MSG_ERROR,
    528 					   "Invalid center frequency index for 6 GHz");
    529 				return -1;
    530 			}
    531 
    532 			/* The 6 GHz channel 2 uses a different operating class
    533 			 */
    534 			opclass = center_segment0 == 2 ? 136 : 131;
    535 			freq1 = ieee80211_chan_to_freq(NULL, opclass,
    536 						       center_segment0);
    537 			if (freq1 < 0) {
    538 				wpa_printf(MSG_ERROR,
    539 					   "Invalid segment 0 center frequency for 6 GHz");
    540 				return -1;
    541 			}
    542 
    543 			if (center_segment1) {
    544 				if (center_idx_to_bw_6ghz(center_segment1) != 2 ||
    545 				    bw != 2) {
    546 					wpa_printf(MSG_ERROR,
    547 						   "6 GHz 80+80 MHz configuration doesn't use valid 80 MHz channels");
    548 					return -1;
    549 				}
    550 
    551 				freq2 = ieee80211_chan_to_freq(NULL, 131,
    552 							       center_segment1);
    553 				if (freq2 < 0) {
    554 					wpa_printf(MSG_ERROR,
    555 						   "Invalid segment 1 center frequency for UHB");
    556 					return -1;
    557 				}
    558 			}
    559 
    560 			data->bandwidth = (1 << (u8) bw) * 20;
    561 			data->center_freq1 = freq1;
    562 			data->center_freq2 = freq2;
    563 		}
    564 		data->ht_enabled = 0;
    565 		data->vht_enabled = 0;
    566 
    567 		return 0;
    568 	}
    569 
    570 	if (data->eht_enabled) switch (oper_chwidth) {
    571 	case CONF_OPER_CHWIDTH_320MHZ:
    572 		if (!(eht_cap->phy_cap[EHT_PHYCAP_320MHZ_IN_6GHZ_SUPPORT_IDX] &
    573 		      EHT_PHYCAP_320MHZ_IN_6GHZ_SUPPORT_MASK)) {
    574 			wpa_printf(MSG_ERROR,
    575 				   "320 MHz channel width is not supported in 5 or 6 GHz");
    576 			return -1;
    577 		}
    578 		break;
    579 	default:
    580 		break;
    581 	}
    582 
    583 	if (data->he_enabled || data->eht_enabled) switch (oper_chwidth) {
    584 	case CONF_OPER_CHWIDTH_USE_HT:
    585 		if (sec_channel_offset == 0)
    586 			break;
    587 
    588 		if (mode == HOSTAPD_MODE_IEEE80211G) {
    589 			if (he_cap &&
    590 			    !(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
    591 			      HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G)) {
    592 				wpa_printf(MSG_ERROR,
    593 					   "40 MHz channel width is not supported in 2.4 GHz");
    594 				return -1;
    595 			}
    596 			break;
    597 		}
    598 		/* fall through */
    599 	case CONF_OPER_CHWIDTH_80MHZ:
    600 		if (mode == HOSTAPD_MODE_IEEE80211A) {
    601 			if (he_cap &&
    602 			    !(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
    603 			      HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
    604 				wpa_printf(MSG_ERROR,
    605 					   "40/80 MHz channel width is not supported in 5/6 GHz");
    606 				return -1;
    607 			}
    608 		}
    609 		break;
    610 	case CONF_OPER_CHWIDTH_80P80MHZ:
    611 		if (he_cap &&
    612 		    !(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
    613 		      HE_PHYCAP_CHANNEL_WIDTH_SET_80PLUS80MHZ_IN_5G)) {
    614 			wpa_printf(MSG_ERROR,
    615 				   "80+80 MHz channel width is not supported in 5/6 GHz");
    616 			return -1;
    617 		}
    618 		break;
    619 	case CONF_OPER_CHWIDTH_160MHZ:
    620 		if (he_cap &&
    621 		    !(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
    622 		      HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G)) {
    623 			wpa_printf(MSG_ERROR,
    624 				   "160 MHz channel width is not supported in 5 / 6GHz");
    625 			return -1;
    626 		}
    627 		break;
    628 	default:
    629 		break;
    630 	} else if (data->vht_enabled) switch (oper_chwidth) {
    631 	case CONF_OPER_CHWIDTH_USE_HT:
    632 		break;
    633 	case CONF_OPER_CHWIDTH_80P80MHZ:
    634 		if (!(vht_caps & VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)) {
    635 			wpa_printf(MSG_ERROR,
    636 				   "80+80 channel width is not supported!");
    637 			return -1;
    638 		}
    639 		/* fall through */
    640 	case CONF_OPER_CHWIDTH_80MHZ:
    641 		break;
    642 	case CONF_OPER_CHWIDTH_160MHZ:
    643 		if (!(vht_caps & (VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
    644 				  VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))) {
    645 			wpa_printf(MSG_ERROR,
    646 				   "160 MHz channel width is not supported!");
    647 			return -1;
    648 		}
    649 		break;
    650 	default:
    651 		break;
    652 	}
    653 
    654 	oper_chwidth_legacy = oper_chwidth;
    655 	seg0_legacy = center_segment0;
    656 	seg1_legacy = center_segment1;
    657 	if (punct_bitmap)
    658 		punct_update_legacy_bw(punct_bitmap, channel,
    659 				       &oper_chwidth_legacy,
    660 				       &seg0_legacy, &seg1_legacy);
    661 
    662 	if (data->eht_enabled || data->he_enabled ||
    663 	    data->vht_enabled) switch (oper_chwidth) {
    664 	case CONF_OPER_CHWIDTH_USE_HT:
    665 		if (center_segment1 ||
    666 		    (center_segment0 != 0 &&
    667 		     5000 + center_segment0 * 5 != data->center_freq1 &&
    668 		     2407 + center_segment0 * 5 != data->center_freq1)) {
    669 			wpa_printf(MSG_ERROR,
    670 				   "20/40 MHz: center segment 0 (=%d) and center freq 1 (=%d) not in sync",
    671 				   center_segment0, data->center_freq1);
    672 			return -1;
    673 		}
    674 		break;
    675 	case CONF_OPER_CHWIDTH_80P80MHZ:
    676 		if (center_segment1 == center_segment0 + 4 ||
    677 		    center_segment1 == center_segment0 - 4) {
    678 			wpa_printf(MSG_ERROR,
    679 				   "80+80 MHz: center segment 1 only 20 MHz apart");
    680 			return -1;
    681 		}
    682 		data->center_freq2 = 5000 + center_segment1 * 5;
    683 		/* fall through */
    684 	case CONF_OPER_CHWIDTH_80MHZ:
    685 		data->bandwidth = 80;
    686 		if (!sec_channel_offset &&
    687 		    oper_chwidth_legacy != CONF_OPER_CHWIDTH_USE_HT) {
    688 			wpa_printf(MSG_ERROR,
    689 				   "80/80+80 MHz: no second channel offset");
    690 			return -1;
    691 		}
    692 		if (oper_chwidth == CONF_OPER_CHWIDTH_80MHZ &&
    693 		    center_segment1) {
    694 			wpa_printf(MSG_ERROR,
    695 				   "80 MHz: center segment 1 configured");
    696 			return -1;
    697 		}
    698 		if (oper_chwidth == CONF_OPER_CHWIDTH_80P80MHZ &&
    699 		    !center_segment1) {
    700 			wpa_printf(MSG_ERROR,
    701 				   "80+80 MHz: center segment 1 not configured");
    702 			return -1;
    703 		}
    704 		if (!center_segment0) {
    705 			if (channel <= 48)
    706 				center_segment0 = 42;
    707 			else if (channel <= 64)
    708 				center_segment0 = 58;
    709 			else if (channel <= 112)
    710 				center_segment0 = 106;
    711 			else if (channel <= 128)
    712 				center_segment0 = 122;
    713 			else if (channel <= 144)
    714 				center_segment0 = 138;
    715 			else if (channel <= 161)
    716 				center_segment0 = 155;
    717 			else if (channel <= 177)
    718 				center_segment0 = 171;
    719 			data->center_freq1 = 5000 + center_segment0 * 5;
    720 		} else {
    721 			/*
    722 			 * Note: HT/VHT config and params are coupled. Check if
    723 			 * HT40 channel band is in VHT80 Pri channel band
    724 			 * configuration.
    725 			 */
    726 			if (center_segment0 == channel + 6 ||
    727 			    center_segment0 == channel + 2 ||
    728 			    center_segment0 == channel - 2 ||
    729 			    center_segment0 == channel - 6)
    730 				data->center_freq1 = 5000 + center_segment0 * 5;
    731 			else {
    732 				wpa_printf(MSG_ERROR,
    733 					   "Wrong coupling between HT and VHT/HE channel setting");
    734 				return -1;
    735 			}
    736 		}
    737 		break;
    738 	case CONF_OPER_CHWIDTH_160MHZ:
    739 		data->bandwidth = 160;
    740 		if (center_segment1) {
    741 			wpa_printf(MSG_ERROR,
    742 				   "160 MHz: center segment 1 should not be set");
    743 			return -1;
    744 		}
    745 		if (!sec_channel_offset &&
    746 		    oper_chwidth_legacy != CONF_OPER_CHWIDTH_USE_HT) {
    747 			wpa_printf(MSG_ERROR,
    748 				   "160 MHz: second channel offset not set");
    749 			return -1;
    750 		}
    751 		/*
    752 		 * Note: HT/VHT config and params are coupled. Check if
    753 		 * HT40 channel band is in VHT160 channel band configuration.
    754 		 */
    755 		if (center_segment0 == channel + 14 ||
    756 		    center_segment0 == channel + 10 ||
    757 		    center_segment0 == channel + 6 ||
    758 		    center_segment0 == channel + 2 ||
    759 		    center_segment0 == channel - 2 ||
    760 		    center_segment0 == channel - 6 ||
    761 		    center_segment0 == channel - 10 ||
    762 		    center_segment0 == channel - 14)
    763 			data->center_freq1 = 5000 + center_segment0 * 5;
    764 		else {
    765 			wpa_printf(MSG_ERROR,
    766 				   "160 MHz: HT40 channel band is not in 160 MHz band");
    767 			return -1;
    768 		}
    769 		break;
    770 	case CONF_OPER_CHWIDTH_320MHZ:
    771 		data->bandwidth = 320;
    772 		if (!data->eht_enabled || !is_6ghz_freq(freq)) {
    773 			wpa_printf(MSG_ERROR,
    774 				   "320 MHz: EHT not enabled or not a 6 GHz channel");
    775 			return -1;
    776 		}
    777 		if (center_segment1) {
    778 			wpa_printf(MSG_ERROR,
    779 				   "320 MHz: center segment 1 should not be set");
    780 			return -1;
    781 		}
    782 		if (center_segment0 == channel + 30 ||
    783 		    center_segment0 == channel + 26 ||
    784 		    center_segment0 == channel + 22 ||
    785 		    center_segment0 == channel + 18 ||
    786 		    center_segment0 == channel + 14 ||
    787 		    center_segment0 == channel + 10 ||
    788 		    center_segment0 == channel + 6 ||
    789 		    center_segment0 == channel + 2 ||
    790 		    center_segment0 == channel - 2 ||
    791 		    center_segment0 == channel - 6 ||
    792 		    center_segment0 == channel - 10 ||
    793 		    center_segment0 == channel - 14 ||
    794 		    center_segment0 == channel - 18 ||
    795 		    center_segment0 == channel - 22 ||
    796 		    center_segment0 == channel - 26 ||
    797 		    center_segment0 == channel - 30)
    798 			data->center_freq1 = 5000 + center_segment0 * 5;
    799 		else {
    800 			wpa_printf(MSG_ERROR,
    801 				   "320 MHz: wrong center segment 0");
    802 			return -1;
    803 		}
    804 		break;
    805 	default:
    806 		break;
    807 	}
    808 
    809 	return 0;
    810 }
    811 
    812 
    813 void set_disable_ht40(struct ieee80211_ht_capabilities *htcaps,
    814 		      int disabled)
    815 {
    816 	/* Masking these out disables HT40 */
    817 	le16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
    818 				HT_CAP_INFO_SHORT_GI40MHZ);
    819 
    820 	if (disabled)
    821 		htcaps->ht_capabilities_info &= ~msk;
    822 	else
    823 		htcaps->ht_capabilities_info |= msk;
    824 }
    825 
    826 
    827 #ifdef CONFIG_IEEE80211AC
    828 
    829 static int _ieee80211ac_cap_check(u32 hw, u32 conf, u32 cap,
    830 				  const char *name)
    831 {
    832 	u32 req_cap = conf & cap;
    833 
    834 	/*
    835 	 * Make sure we support all requested capabilities.
    836 	 * NOTE: We assume that 'cap' represents a capability mask,
    837 	 * not a discrete value.
    838 	 */
    839 	if ((hw & req_cap) != req_cap) {
    840 		wpa_printf(MSG_ERROR,
    841 			   "Driver does not support configured VHT capability [%s]",
    842 			   name);
    843 		return 0;
    844 	}
    845 	return 1;
    846 }
    847 
    848 
    849 static int ieee80211ac_cap_check_max(u32 hw, u32 conf, u32 mask,
    850 				     unsigned int shift,
    851 				     const char *name)
    852 {
    853 	u32 hw_max = hw & mask;
    854 	u32 conf_val = conf & mask;
    855 
    856 	if (conf_val > hw_max) {
    857 		wpa_printf(MSG_ERROR,
    858 			   "Configured VHT capability [%s] exceeds max value supported by the driver (%d > %d)",
    859 			   name, conf_val >> shift, hw_max >> shift);
    860 		return 0;
    861 	}
    862 	return 1;
    863 }
    864 
    865 
    866 int ieee80211ac_cap_check(u32 hw, u32 conf)
    867 {
    868 #define VHT_CAP_CHECK(cap) \
    869 	do { \
    870 		if (!_ieee80211ac_cap_check(hw, conf, cap, #cap)) \
    871 			return 0; \
    872 	} while (0)
    873 
    874 #define VHT_CAP_CHECK_MAX(cap) \
    875 	do { \
    876 		if (!ieee80211ac_cap_check_max(hw, conf, cap, cap ## _SHIFT, \
    877 					       #cap)) \
    878 			return 0; \
    879 	} while (0)
    880 
    881 	VHT_CAP_CHECK_MAX(VHT_CAP_MAX_MPDU_LENGTH_MASK);
    882 	VHT_CAP_CHECK_MAX(VHT_CAP_SUPP_CHAN_WIDTH_MASK);
    883 	VHT_CAP_CHECK(VHT_CAP_RXLDPC);
    884 	VHT_CAP_CHECK(VHT_CAP_SHORT_GI_80);
    885 	VHT_CAP_CHECK(VHT_CAP_SHORT_GI_160);
    886 	VHT_CAP_CHECK(VHT_CAP_TXSTBC);
    887 	VHT_CAP_CHECK_MAX(VHT_CAP_RXSTBC_MASK);
    888 	VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMER_CAPABLE);
    889 	VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMEE_CAPABLE);
    890 	VHT_CAP_CHECK_MAX(VHT_CAP_BEAMFORMEE_STS_MAX);
    891 	VHT_CAP_CHECK_MAX(VHT_CAP_SOUNDING_DIMENSION_MAX);
    892 	VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMER_CAPABLE);
    893 	VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMEE_CAPABLE);
    894 	VHT_CAP_CHECK(VHT_CAP_VHT_TXOP_PS);
    895 	VHT_CAP_CHECK(VHT_CAP_HTC_VHT);
    896 	VHT_CAP_CHECK_MAX(VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX);
    897 	VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_UNSOL_MFB);
    898 	VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_MRQ_MFB);
    899 	VHT_CAP_CHECK(VHT_CAP_RX_ANTENNA_PATTERN);
    900 	VHT_CAP_CHECK(VHT_CAP_TX_ANTENNA_PATTERN);
    901 
    902 #undef VHT_CAP_CHECK
    903 #undef VHT_CAP_CHECK_MAX
    904 
    905 	return 1;
    906 }
    907 
    908 #endif /* CONFIG_IEEE80211AC */
    909 
    910 
    911 u32 num_chan_to_bw(int num_chans)
    912 {
    913 	switch (num_chans) {
    914 	case 2:
    915 	case 4:
    916 	case 8:
    917 	case 16:
    918 		return num_chans * 20;
    919 	default:
    920 		return 20;
    921 	}
    922 }
    923 
    924 
    925 /* check if BW is applicable for channel */
    926 int chan_bw_allowed(const struct hostapd_channel_data *chan, u32 bw,
    927 		    int ht40_plus, int pri)
    928 {
    929 	u32 bw_mask;
    930 
    931 	switch (bw) {
    932 	case 20:
    933 		bw_mask = HOSTAPD_CHAN_WIDTH_20;
    934 		break;
    935 	case 40:
    936 		/* HT 40 MHz support declared only for primary channel,
    937 		 * just skip 40 MHz secondary checking */
    938 		if (pri && ht40_plus)
    939 			bw_mask = HOSTAPD_CHAN_WIDTH_40P;
    940 		else if (pri && !ht40_plus)
    941 			bw_mask = HOSTAPD_CHAN_WIDTH_40M;
    942 		else
    943 			bw_mask = 0;
    944 		break;
    945 	case 80:
    946 		bw_mask = HOSTAPD_CHAN_WIDTH_80;
    947 		break;
    948 	case 160:
    949 		bw_mask = HOSTAPD_CHAN_WIDTH_160;
    950 		break;
    951 	case 320:
    952 		bw_mask = HOSTAPD_CHAN_WIDTH_320;
    953 		break;
    954 	default:
    955 		bw_mask = 0;
    956 		break;
    957 	}
    958 
    959 	return (chan->allowed_bw & bw_mask) == bw_mask;
    960 }
    961 
    962 
    963 /* check if channel is allowed to be used as primary */
    964 int chan_pri_allowed(const struct hostapd_channel_data *chan)
    965 {
    966 	return !(chan->flag & HOSTAPD_CHAN_DISABLED) &&
    967 		(chan->allowed_bw & HOSTAPD_CHAN_WIDTH_20);
    968 }
    969 
    970 
    971 /* IEEE P802.11be/D3.0, Table 36-30 - Definition of the Punctured Channel
    972  * Information field in the U-SIG for an EHT MU PPDU using non-OFDMA
    973  * transmissions */
    974 static const u16 punct_bitmap_80[] = { 0xF, 0xE, 0xD, 0xB, 0x7 };
    975 static const u16 punct_bitmap_160[] = {
    976 	0xFF, 0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF,
    977 	0x7F, 0xFC, 0xF3, 0xCF, 0x3F
    978 };
    979 static const u16 punct_bitmap_320[] = {
    980 	0xFFFF, 0xFFFC, 0xFFF3, 0xFFCF, 0xFF3F, 0xFCFF, 0xF3FF, 0xCFFF,
    981 	0x3FFF, 0xFFF0, 0xFF0F, 0xF0FF, 0x0FFF, 0xFFC0, 0xFF30, 0xFCF0,
    982 	0xF3F0, 0xCFF0, 0x3FF0, 0x0FFC, 0x0FF3, 0x0FCF, 0x0F3F, 0x0CFF,
    983 	0x03FF
    984 };
    985 
    986 
    987 bool is_punct_bitmap_valid(u16 bw, u16 pri_ch_bit_pos, u16 punct_bitmap)
    988 {
    989 	u8 i, count;
    990 	u16 bitmap;
    991 	const u16 *valid_bitmaps;
    992 
    993 	if (!punct_bitmap) /* All channels active */
    994 		return true;
    995 
    996 	bitmap = ~punct_bitmap;
    997 
    998 	switch (bw) {
    999 	case 80:
   1000 		bitmap &= 0xF;
   1001 		valid_bitmaps = punct_bitmap_80;
   1002 		count = ARRAY_SIZE(punct_bitmap_80);
   1003 		break;
   1004 
   1005 	case 160:
   1006 		bitmap &= 0xFF;
   1007 		valid_bitmaps = punct_bitmap_160;
   1008 		count = ARRAY_SIZE(punct_bitmap_160);
   1009 		break;
   1010 
   1011 	case 320:
   1012 		bitmap &= 0xFFFF;
   1013 		valid_bitmaps = punct_bitmap_320;
   1014 		count = ARRAY_SIZE(punct_bitmap_320);
   1015 		break;
   1016 
   1017 	default:
   1018 		return false;
   1019 	}
   1020 
   1021 	if (!bitmap) /* No channel active */
   1022 		return false;
   1023 
   1024 	if (!(bitmap & BIT(pri_ch_bit_pos))) {
   1025 		wpa_printf(MSG_DEBUG, "Primary channel cannot be punctured");
   1026 		return false;
   1027 	}
   1028 
   1029 	for (i = 0; i < count; i++) {
   1030 		if (valid_bitmaps[i] == bitmap)
   1031 			return true;
   1032 	}
   1033 
   1034 	return false;
   1035 }
   1036