• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Common hostapd/wpa_supplicant HW features
3  * Copyright (c) 2002-2013, Jouni Malinen <j@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 
hw_get_channel_chan(struct hostapd_hw_modes * mode,int chan,int * freq)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 *
hw_mode_get_channel(struct hostapd_hw_modes * mode,int freq,int * chan)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 *
hw_get_channel_freq(enum hostapd_hw_mode mode,int freq,int * chan,struct hostapd_hw_modes * hw_features,int num_hw_features)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 
hw_get_freq(struct hostapd_hw_modes * mode,int chan)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 
hw_get_chan(enum hostapd_hw_mode mode,int freq,struct hostapd_hw_modes * hw_features,int num_hw_features)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 
allowed_ht40_channel_pair(enum hostapd_hw_mode mode,struct hostapd_channel_data * p_chan,struct hostapd_channel_data * s_chan)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 
get_pri_sec_chan(struct wpa_scan_res * bss,int * pri_chan,int * sec_chan)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 	ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0);
187 	if (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 
check_40mhz_5g(struct wpa_scan_results * scan_res,struct hostapd_channel_data * pri_chan,struct hostapd_channel_data * sec_chan)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 
check_20mhz_bss(struct wpa_scan_res * bss,int pri_freq,int start,int end)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 	ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0);
277 	if (!elems.ht_capabilities) {
278 		wpa_printf(MSG_DEBUG, "Found overlapping legacy BSS: "
279 			   MACSTR " freq=%d", MAC2STR(bss->bssid), bss->freq);
280 		return 1;
281 	}
282 
283 	if (elems.ht_operation) {
284 		oper = (struct ieee80211_ht_operation *) elems.ht_operation;
285 		if (oper->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)
286 			return 0;
287 
288 		wpa_printf(MSG_DEBUG, "Found overlapping 20 MHz HT BSS: "
289 			   MACSTR " freq=%d", MAC2STR(bss->bssid), bss->freq);
290 		return 1;
291 	}
292 	return 0;
293 }
294 
295 
check_40mhz_2g4(struct hostapd_hw_modes * mode,struct wpa_scan_results * scan_res,int pri_chan,int sec_chan)296 int check_40mhz_2g4(struct hostapd_hw_modes *mode,
297 		    struct wpa_scan_results *scan_res, int pri_chan,
298 		    int sec_chan)
299 {
300 	int pri_freq, sec_freq;
301 	int affected_start, affected_end;
302 	size_t i;
303 
304 	if (!mode || !scan_res || !pri_chan || !sec_chan ||
305 	    pri_chan == sec_chan)
306 		return 0;
307 
308 	pri_freq = hw_get_freq(mode, pri_chan);
309 	sec_freq = hw_get_freq(mode, sec_chan);
310 
311 	affected_start = (pri_freq + sec_freq) / 2 - 25;
312 	affected_end = (pri_freq + sec_freq) / 2 + 25;
313 	wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
314 		   affected_start, affected_end);
315 	for (i = 0; i < scan_res->num; i++) {
316 		struct wpa_scan_res *bss = scan_res->res[i];
317 		int pri = bss->freq;
318 		int sec = pri;
319 		struct ieee802_11_elems elems;
320 
321 		/* Check for overlapping 20 MHz BSS */
322 		if (check_20mhz_bss(bss, pri_freq, affected_start,
323 				    affected_end)) {
324 			wpa_printf(MSG_DEBUG,
325 				   "Overlapping 20 MHz BSS is found");
326 			return 0;
327 		}
328 
329 		get_pri_sec_chan(bss, &pri_chan, &sec_chan);
330 
331 		if (sec_chan) {
332 			if (sec_chan < pri_chan)
333 				sec = pri - 20;
334 			else
335 				sec = pri + 20;
336 		}
337 
338 		if ((pri < affected_start || pri > affected_end) &&
339 		    (sec < affected_start || sec > affected_end))
340 			continue; /* not within affected channel range */
341 
342 		wpa_printf(MSG_DEBUG, "Neighboring BSS: " MACSTR
343 			   " freq=%d pri=%d sec=%d",
344 			   MAC2STR(bss->bssid), bss->freq, pri_chan, sec_chan);
345 
346 		if (sec_chan) {
347 			if (pri_freq != pri || sec_freq != sec) {
348 				wpa_printf(MSG_DEBUG,
349 					   "40 MHz pri/sec mismatch with BSS "
350 					   MACSTR
351 					   " <%d,%d> (chan=%d%c) vs. <%d,%d>",
352 					   MAC2STR(bss->bssid),
353 					   pri, sec, pri_chan,
354 					   sec > pri ? '+' : '-',
355 					   pri_freq, sec_freq);
356 				return 0;
357 			}
358 		}
359 
360 		ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems,
361 				       0);
362 		if (elems.ht_capabilities) {
363 			struct ieee80211_ht_capabilities *ht_cap =
364 				(struct ieee80211_ht_capabilities *)
365 				elems.ht_capabilities;
366 
367 			if (le_to_host16(ht_cap->ht_capabilities_info) &
368 			    HT_CAP_INFO_40MHZ_INTOLERANT) {
369 				wpa_printf(MSG_DEBUG,
370 					   "40 MHz Intolerant is set on channel %d in BSS "
371 					   MACSTR, pri, MAC2STR(bss->bssid));
372 				return 0;
373 			}
374 		}
375 	}
376 
377 	return 1;
378 }
379 
380 
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,bool eht_enabled,int sec_channel_offset,enum oper_chan_width oper_chwidth,int center_segment0,int center_segment1,u32 vht_caps,struct he_capabilities * he_cap,struct eht_capabilities * eht_cap)381 int hostapd_set_freq_params(struct hostapd_freq_params *data,
382 			    enum hostapd_hw_mode mode,
383 			    int freq, int channel, int enable_edmg,
384 			    u8 edmg_channel, int ht_enabled,
385 			    int vht_enabled, int he_enabled,
386 			    bool eht_enabled, int sec_channel_offset,
387 			    enum oper_chan_width oper_chwidth,
388 			    int center_segment0,
389 			    int center_segment1, u32 vht_caps,
390 			    struct he_capabilities *he_cap,
391 			    struct eht_capabilities *eht_cap)
392 {
393 	if (!he_cap || !he_cap->he_supported)
394 		he_enabled = 0;
395 	if (!eht_cap || !eht_cap->eht_supported)
396 		eht_enabled = 0;
397 	os_memset(data, 0, sizeof(*data));
398 	data->mode = mode;
399 	data->freq = freq;
400 	data->channel = channel;
401 	data->ht_enabled = ht_enabled;
402 	data->vht_enabled = vht_enabled;
403 	data->he_enabled = he_enabled;
404 	data->eht_enabled = eht_enabled;
405 	data->sec_channel_offset = sec_channel_offset;
406 	data->center_freq1 = freq + sec_channel_offset * 10;
407 	data->center_freq2 = 0;
408 	if (oper_chwidth == CONF_OPER_CHWIDTH_80MHZ)
409 		data->bandwidth = 80;
410 	else if (oper_chwidth == CONF_OPER_CHWIDTH_160MHZ ||
411 		 oper_chwidth == CONF_OPER_CHWIDTH_80P80MHZ)
412 		data->bandwidth = 160;
413 	else if (oper_chwidth == CONF_OPER_CHWIDTH_320MHZ)
414 		data->bandwidth = 320;
415 	else if (sec_channel_offset)
416 		data->bandwidth = 40;
417 	else
418 		data->bandwidth = 20;
419 
420 
421 	hostapd_encode_edmg_chan(enable_edmg, edmg_channel, channel,
422 				 &data->edmg);
423 
424 	if (is_6ghz_freq(freq)) {
425 		if (!data->he_enabled && !data->eht_enabled) {
426 			wpa_printf(MSG_ERROR,
427 				   "Can't set 6 GHz mode - HE or EHT aren't enabled");
428 			return -1;
429 		}
430 
431 		if (center_idx_to_bw_6ghz(channel) < 0) {
432 			wpa_printf(MSG_ERROR,
433 				   "Invalid control channel for 6 GHz band");
434 			return -1;
435 		}
436 
437 		if (!center_segment0) {
438 			if (center_segment1) {
439 				wpa_printf(MSG_ERROR,
440 					   "Segment 0 center frequency isn't set");
441 				return -1;
442 			}
443 			if (!sec_channel_offset)
444 				data->center_freq1 = data->freq;
445 		} else {
446 			int freq1, freq2 = 0;
447 			int bw = center_idx_to_bw_6ghz(center_segment0);
448 
449 			if (bw < 0) {
450 				wpa_printf(MSG_ERROR,
451 					   "Invalid center frequency index for 6 GHz");
452 				return -1;
453 			}
454 
455 			freq1 = ieee80211_chan_to_freq(NULL, 131,
456 						       center_segment0);
457 			if (freq1 < 0) {
458 				wpa_printf(MSG_ERROR,
459 					   "Invalid segment 0 center frequency for 6 GHz");
460 				return -1;
461 			}
462 
463 			if (center_segment1) {
464 				if (center_idx_to_bw_6ghz(center_segment1) != 2 ||
465 				    bw != 2) {
466 					wpa_printf(MSG_ERROR,
467 						   "6 GHz 80+80 MHz configuration doesn't use valid 80 MHz channels");
468 					return -1;
469 				}
470 
471 				freq2 = ieee80211_chan_to_freq(NULL, 131,
472 							       center_segment1);
473 				if (freq2 < 0) {
474 					wpa_printf(MSG_ERROR,
475 						   "Invalid segment 1 center frequency for UHB");
476 					return -1;
477 				}
478 			}
479 
480 			data->bandwidth = (1 << (u8) bw) * 20;
481 			data->center_freq1 = freq1;
482 			data->center_freq2 = freq2;
483 		}
484 		data->ht_enabled = 0;
485 		data->vht_enabled = 0;
486 
487 		return 0;
488 	}
489 
490 	if (data->eht_enabled) switch (oper_chwidth) {
491 	case CONF_OPER_CHWIDTH_320MHZ:
492 		if (!(eht_cap->phy_cap[EHT_PHYCAP_320MHZ_IN_6GHZ_SUPPORT_IDX] &
493 		      EHT_PHYCAP_320MHZ_IN_6GHZ_SUPPORT_MASK)) {
494 			wpa_printf(MSG_ERROR,
495 				   "320 MHz channel width is not supported in 5 or 6 GHz");
496 			return -1;
497 		}
498 		break;
499 	default:
500 		break;
501 	}
502 
503 	if (data->he_enabled || data->eht_enabled) switch (oper_chwidth) {
504 	case CONF_OPER_CHWIDTH_USE_HT:
505 		if (sec_channel_offset == 0)
506 			break;
507 
508 		if (mode == HOSTAPD_MODE_IEEE80211G) {
509 			if (he_cap &&
510 			    !(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
511 			      HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G)) {
512 				wpa_printf(MSG_ERROR,
513 					   "40 MHz channel width is not supported in 2.4 GHz");
514 				return -1;
515 			}
516 			break;
517 		}
518 		/* fall through */
519 	case CONF_OPER_CHWIDTH_80MHZ:
520 		if (mode == HOSTAPD_MODE_IEEE80211A) {
521 			if (he_cap &&
522 			    !(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
523 			      HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
524 				wpa_printf(MSG_ERROR,
525 					   "40/80 MHz channel width is not supported in 5/6 GHz");
526 				return -1;
527 			}
528 		}
529 		break;
530 	case CONF_OPER_CHWIDTH_80P80MHZ:
531 		if (he_cap &&
532 		    !(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
533 		      HE_PHYCAP_CHANNEL_WIDTH_SET_80PLUS80MHZ_IN_5G)) {
534 			wpa_printf(MSG_ERROR,
535 				   "80+80 MHz channel width is not supported in 5/6 GHz");
536 			return -1;
537 		}
538 		break;
539 	case CONF_OPER_CHWIDTH_160MHZ:
540 		if (he_cap &&
541 		    !(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
542 		      HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G)) {
543 			wpa_printf(MSG_ERROR,
544 				   "160 MHz channel width is not supported in 5 / 6GHz");
545 			return -1;
546 		}
547 		break;
548 	default:
549 		break;
550 	} else if (data->vht_enabled) switch (oper_chwidth) {
551 	case CONF_OPER_CHWIDTH_USE_HT:
552 		break;
553 	case CONF_OPER_CHWIDTH_80P80MHZ:
554 		if (!(vht_caps & VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)) {
555 			wpa_printf(MSG_ERROR,
556 				   "80+80 channel width is not supported!");
557 			return -1;
558 		}
559 		/* fall through */
560 	case CONF_OPER_CHWIDTH_80MHZ:
561 		break;
562 	case CONF_OPER_CHWIDTH_160MHZ:
563 		if (!(vht_caps & (VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
564 				  VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))) {
565 			wpa_printf(MSG_ERROR,
566 				   "160 MHz channel width is not supported!");
567 			return -1;
568 		}
569 		break;
570 	default:
571 		break;
572 	}
573 
574 	if (data->eht_enabled || data->he_enabled ||
575 	    data->vht_enabled) switch (oper_chwidth) {
576 	case CONF_OPER_CHWIDTH_USE_HT:
577 		if (center_segment1 ||
578 		    (center_segment0 != 0 &&
579 		     5000 + center_segment0 * 5 != data->center_freq1 &&
580 		     2407 + center_segment0 * 5 != data->center_freq1)) {
581 			wpa_printf(MSG_ERROR,
582 				   "20/40 MHz: center segment 0 (=%d) and center freq 1 (=%d) not in sync",
583 				   center_segment0, data->center_freq1);
584 			return -1;
585 		}
586 		break;
587 	case CONF_OPER_CHWIDTH_80P80MHZ:
588 		if (center_segment1 == center_segment0 + 4 ||
589 		    center_segment1 == center_segment0 - 4) {
590 			wpa_printf(MSG_ERROR,
591 				   "80+80 MHz: center segment 1 only 20 MHz apart");
592 			return -1;
593 		}
594 		data->center_freq2 = 5000 + center_segment1 * 5;
595 		/* fall through */
596 	case CONF_OPER_CHWIDTH_80MHZ:
597 		data->bandwidth = 80;
598 		if (!sec_channel_offset) {
599 			wpa_printf(MSG_ERROR,
600 				   "80/80+80 MHz: no second channel offset");
601 			return -1;
602 		}
603 		if (oper_chwidth == CONF_OPER_CHWIDTH_80MHZ &&
604 		    center_segment1) {
605 			wpa_printf(MSG_ERROR,
606 				   "80 MHz: center segment 1 configured");
607 			return -1;
608 		}
609 		if (oper_chwidth == CONF_OPER_CHWIDTH_80P80MHZ &&
610 		    !center_segment1) {
611 			wpa_printf(MSG_ERROR,
612 				   "80+80 MHz: center segment 1 not configured");
613 			return -1;
614 		}
615 		if (!center_segment0) {
616 			if (channel <= 48)
617 				center_segment0 = 42;
618 			else if (channel <= 64)
619 				center_segment0 = 58;
620 			else if (channel <= 112)
621 				center_segment0 = 106;
622 			else if (channel <= 128)
623 				center_segment0 = 122;
624 			else if (channel <= 144)
625 				center_segment0 = 138;
626 			else if (channel <= 161)
627 				center_segment0 = 155;
628 			else if (channel <= 177)
629 				center_segment0 = 171;
630 			data->center_freq1 = 5000 + center_segment0 * 5;
631 		} else {
632 			/*
633 			 * Note: HT/VHT config and params are coupled. Check if
634 			 * HT40 channel band is in VHT80 Pri channel band
635 			 * configuration.
636 			 */
637 			if (center_segment0 == channel + 6 ||
638 			    center_segment0 == channel + 2 ||
639 			    center_segment0 == channel - 2 ||
640 			    center_segment0 == channel - 6)
641 				data->center_freq1 = 5000 + center_segment0 * 5;
642 			else {
643 				wpa_printf(MSG_ERROR,
644 					   "Wrong coupling between HT and VHT/HE channel setting");
645 				return -1;
646 			}
647 		}
648 		break;
649 	case CONF_OPER_CHWIDTH_160MHZ:
650 		data->bandwidth = 160;
651 		if (center_segment1) {
652 			wpa_printf(MSG_ERROR,
653 				   "160 MHz: center segment 1 should not be set");
654 			return -1;
655 		}
656 		if (!sec_channel_offset) {
657 			wpa_printf(MSG_ERROR,
658 				   "160 MHz: second channel offset not set");
659 			return -1;
660 		}
661 		/*
662 		 * Note: HT/VHT config and params are coupled. Check if
663 		 * HT40 channel band is in VHT160 channel band configuration.
664 		 */
665 		if (center_segment0 == channel + 14 ||
666 		    center_segment0 == channel + 10 ||
667 		    center_segment0 == channel + 6 ||
668 		    center_segment0 == channel + 2 ||
669 		    center_segment0 == channel - 2 ||
670 		    center_segment0 == channel - 6 ||
671 		    center_segment0 == channel - 10 ||
672 		    center_segment0 == channel - 14)
673 			data->center_freq1 = 5000 + center_segment0 * 5;
674 		else {
675 			wpa_printf(MSG_ERROR,
676 				   "160 MHz: HT40 channel band is not in 160 MHz band");
677 			return -1;
678 		}
679 		break;
680 	case CONF_OPER_CHWIDTH_320MHZ:
681 		data->bandwidth = 320;
682 		if (!data->eht_enabled || !is_6ghz_freq(freq)) {
683 			wpa_printf(MSG_ERROR,
684 				   "320 MHz: EHT not enabled or not a 6 GHz channel");
685 			return -1;
686 		}
687 		if (center_segment1) {
688 			wpa_printf(MSG_ERROR,
689 				   "320 MHz: center segment 1 should not be set");
690 			return -1;
691 		}
692 		if (center_segment0 == channel + 30 ||
693 		    center_segment0 == channel + 26 ||
694 		    center_segment0 == channel + 22 ||
695 		    center_segment0 == channel + 18 ||
696 		    center_segment0 == channel + 14 ||
697 		    center_segment0 == channel + 10 ||
698 		    center_segment0 == channel + 6 ||
699 		    center_segment0 == channel + 2 ||
700 		    center_segment0 == channel - 2 ||
701 		    center_segment0 == channel - 6 ||
702 		    center_segment0 == channel - 10 ||
703 		    center_segment0 == channel - 14 ||
704 		    center_segment0 == channel - 18 ||
705 		    center_segment0 == channel - 22 ||
706 		    center_segment0 == channel - 26 ||
707 		    center_segment0 == channel - 30)
708 			data->center_freq1 = 5000 + center_segment0 * 5;
709 		else {
710 			wpa_printf(MSG_ERROR,
711 				   "320 MHz: wrong center segment 0");
712 			return -1;
713 		}
714 		break;
715 	default:
716 		break;
717 	}
718 
719 	return 0;
720 }
721 
722 
set_disable_ht40(struct ieee80211_ht_capabilities * htcaps,int disabled)723 void set_disable_ht40(struct ieee80211_ht_capabilities *htcaps,
724 		      int disabled)
725 {
726 	/* Masking these out disables HT40 */
727 	le16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
728 				HT_CAP_INFO_SHORT_GI40MHZ);
729 
730 	if (disabled)
731 		htcaps->ht_capabilities_info &= ~msk;
732 	else
733 		htcaps->ht_capabilities_info |= msk;
734 }
735 
736 
737 #ifdef CONFIG_IEEE80211AC
738 
_ieee80211ac_cap_check(u32 hw,u32 conf,u32 cap,const char * name)739 static int _ieee80211ac_cap_check(u32 hw, u32 conf, u32 cap,
740 				  const char *name)
741 {
742 	u32 req_cap = conf & cap;
743 
744 	/*
745 	 * Make sure we support all requested capabilities.
746 	 * NOTE: We assume that 'cap' represents a capability mask,
747 	 * not a discrete value.
748 	 */
749 	if ((hw & req_cap) != req_cap) {
750 		wpa_printf(MSG_ERROR,
751 			   "Driver does not support configured VHT capability [%s]",
752 			   name);
753 		return 0;
754 	}
755 	return 1;
756 }
757 
758 
ieee80211ac_cap_check_max(u32 hw,u32 conf,u32 mask,unsigned int shift,const char * name)759 static int ieee80211ac_cap_check_max(u32 hw, u32 conf, u32 mask,
760 				     unsigned int shift,
761 				     const char *name)
762 {
763 	u32 hw_max = hw & mask;
764 	u32 conf_val = conf & mask;
765 
766 	if (conf_val > hw_max) {
767 		wpa_printf(MSG_ERROR,
768 			   "Configured VHT capability [%s] exceeds max value supported by the driver (%d > %d)",
769 			   name, conf_val >> shift, hw_max >> shift);
770 		return 0;
771 	}
772 	return 1;
773 }
774 
775 
ieee80211ac_cap_check(u32 hw,u32 conf)776 int ieee80211ac_cap_check(u32 hw, u32 conf)
777 {
778 #define VHT_CAP_CHECK(cap) \
779 	do { \
780 		if (!_ieee80211ac_cap_check(hw, conf, cap, #cap)) \
781 			return 0; \
782 	} while (0)
783 
784 #define VHT_CAP_CHECK_MAX(cap) \
785 	do { \
786 		if (!ieee80211ac_cap_check_max(hw, conf, cap, cap ## _SHIFT, \
787 					       #cap)) \
788 			return 0; \
789 	} while (0)
790 
791 	VHT_CAP_CHECK_MAX(VHT_CAP_MAX_MPDU_LENGTH_MASK);
792 	VHT_CAP_CHECK_MAX(VHT_CAP_SUPP_CHAN_WIDTH_MASK);
793 	VHT_CAP_CHECK(VHT_CAP_RXLDPC);
794 	VHT_CAP_CHECK(VHT_CAP_SHORT_GI_80);
795 	VHT_CAP_CHECK(VHT_CAP_SHORT_GI_160);
796 	VHT_CAP_CHECK(VHT_CAP_TXSTBC);
797 	VHT_CAP_CHECK_MAX(VHT_CAP_RXSTBC_MASK);
798 	VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMER_CAPABLE);
799 	VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMEE_CAPABLE);
800 	VHT_CAP_CHECK_MAX(VHT_CAP_BEAMFORMEE_STS_MAX);
801 	VHT_CAP_CHECK_MAX(VHT_CAP_SOUNDING_DIMENSION_MAX);
802 	VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMER_CAPABLE);
803 	VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMEE_CAPABLE);
804 	VHT_CAP_CHECK(VHT_CAP_VHT_TXOP_PS);
805 	VHT_CAP_CHECK(VHT_CAP_HTC_VHT);
806 	VHT_CAP_CHECK_MAX(VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX);
807 	VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_UNSOL_MFB);
808 	VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_MRQ_MFB);
809 	VHT_CAP_CHECK(VHT_CAP_RX_ANTENNA_PATTERN);
810 	VHT_CAP_CHECK(VHT_CAP_TX_ANTENNA_PATTERN);
811 
812 #undef VHT_CAP_CHECK
813 #undef VHT_CAP_CHECK_MAX
814 
815 	return 1;
816 }
817 
818 #endif /* CONFIG_IEEE80211AC */
819 
820 
num_chan_to_bw(int num_chans)821 u32 num_chan_to_bw(int num_chans)
822 {
823 	switch (num_chans) {
824 	case 2:
825 	case 4:
826 	case 8:
827 	case 16:
828 		return num_chans * 20;
829 	default:
830 		return 20;
831 	}
832 }
833 
834 
835 /* check if BW is applicable for channel */
chan_bw_allowed(const struct hostapd_channel_data * chan,u32 bw,int ht40_plus,int pri)836 int chan_bw_allowed(const struct hostapd_channel_data *chan, u32 bw,
837 		    int ht40_plus, int pri)
838 {
839 	u32 bw_mask;
840 
841 	switch (bw) {
842 	case 20:
843 		bw_mask = HOSTAPD_CHAN_WIDTH_20;
844 		break;
845 	case 40:
846 		/* HT 40 MHz support declared only for primary channel,
847 		 * just skip 40 MHz secondary checking */
848 		if (pri && ht40_plus)
849 			bw_mask = HOSTAPD_CHAN_WIDTH_40P;
850 		else if (pri && !ht40_plus)
851 			bw_mask = HOSTAPD_CHAN_WIDTH_40M;
852 		else
853 			bw_mask = 0;
854 		break;
855 	case 80:
856 		bw_mask = HOSTAPD_CHAN_WIDTH_80;
857 		break;
858 	case 160:
859 		bw_mask = HOSTAPD_CHAN_WIDTH_160;
860 		break;
861 	case 320:
862 		bw_mask = HOSTAPD_CHAN_WIDTH_320;
863 		break;
864 	default:
865 		bw_mask = 0;
866 		break;
867 	}
868 
869 	return (chan->allowed_bw & bw_mask) == bw_mask;
870 }
871 
872 
873 /* check if channel is allowed to be used as primary */
chan_pri_allowed(const struct hostapd_channel_data * chan)874 int chan_pri_allowed(const struct hostapd_channel_data *chan)
875 {
876 	return !(chan->flag & HOSTAPD_CHAN_DISABLED) &&
877 		(chan->allowed_bw & HOSTAPD_CHAN_WIDTH_20);
878 }
879 
880 
881 /* IEEE P802.11be/D3.0, Table 36-30 - Definition of the Punctured Channel
882  * Information field in the U-SIG for an EHT MU PPDU using non-OFDMA
883  * transmissions */
884 static const u16 punct_bitmap_80[] = { 0xF, 0xE, 0xD, 0xB, 0x7 };
885 static const u16 punct_bitmap_160[] = {
886 	0xFF, 0xFE, 0xFD, 0xFB, 0xF7, 0xEF, 0xDF, 0xBF,
887 	0x7F, 0xFC, 0xF3, 0xCF, 0x3F
888 };
889 static const u16 punct_bitmap_320[] = {
890 	0xFFFF, 0xFFFC, 0xFFF3, 0xFFCF, 0xFF3F, 0xFCFF, 0xF3FF, 0xCFFF,
891 	0x3FFF, 0xFFF0, 0xFF0F, 0xF0FF, 0x0FFF, 0xFFC0, 0xFF30, 0xFCF0,
892 	0xF3F0, 0xCFF0, 0x3FF0, 0x0FFC, 0x0FF3, 0x0FCF, 0x0F3F, 0x0CFF,
893 	0x03FF
894 };
895 
896 
is_punct_bitmap_valid(u16 bw,u16 pri_ch_bit_pos,u16 punct_bitmap)897 bool is_punct_bitmap_valid(u16 bw, u16 pri_ch_bit_pos, u16 punct_bitmap)
898 {
899 	u8 i, count;
900 	u16 bitmap;
901 	const u16 *valid_bitmaps;
902 
903 	if (!punct_bitmap) /* All channels active */
904 		return true;
905 
906 	bitmap = ~punct_bitmap;
907 
908 	switch (bw) {
909 	case 80:
910 		bitmap &= 0xF;
911 		valid_bitmaps = punct_bitmap_80;
912 		count = ARRAY_SIZE(punct_bitmap_80);
913 		break;
914 
915 	case 160:
916 		bitmap &= 0xFF;
917 		valid_bitmaps = punct_bitmap_160;
918 		count = ARRAY_SIZE(punct_bitmap_160);
919 		break;
920 
921 	case 320:
922 		bitmap &= 0xFFFF;
923 		valid_bitmaps = punct_bitmap_320;
924 		count = ARRAY_SIZE(punct_bitmap_320);
925 		break;
926 
927 	default:
928 		return false;
929 	}
930 
931 	if (!bitmap) /* No channel active */
932 		return false;
933 
934 	if (!(bitmap & BIT(pri_ch_bit_pos))) {
935 		wpa_printf(MSG_DEBUG, "Primary channel cannot be punctured");
936 		return false;
937 	}
938 
939 	for (i = 0; i < count; i++) {
940 		if (valid_bitmaps[i] == bitmap)
941 			return true;
942 	}
943 
944 	return false;
945 }
946