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