• 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, 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 
296 /*
297  * Returns:
298  * 0: no impact
299  * 1: overlapping BSS
300  * 2: overlapping BSS with 40 MHz intolerant advertisement
301  */
check_bss_coex_40mhz(struct wpa_scan_res * bss,int pri_freq,int sec_freq)302 int check_bss_coex_40mhz(struct wpa_scan_res *bss, int pri_freq, int sec_freq)
303 {
304 	int affected_start, affected_end;
305 	struct ieee802_11_elems elems;
306 	int pri_chan, sec_chan;
307 	int pri = bss->freq;
308 	int sec = pri;
309 
310 	if (pri_freq == sec_freq)
311 		return 1;
312 
313 	affected_start = (pri_freq + sec_freq) / 2 - 25;
314 	affected_end = (pri_freq + sec_freq) / 2 + 25;
315 
316 	/* Check for overlapping 20 MHz BSS */
317 	if (check_20mhz_bss(bss, pri_freq, affected_start, affected_end)) {
318 		wpa_printf(MSG_DEBUG, "Overlapping 20 MHz BSS is found");
319 		return 1;
320 	}
321 
322 	get_pri_sec_chan(bss, &pri_chan, &sec_chan);
323 
324 	if (sec_chan) {
325 		if (sec_chan < pri_chan)
326 			sec = pri - 20;
327 		else
328 			sec = pri + 20;
329 	}
330 
331 	if ((pri < affected_start || pri > affected_end) &&
332 	    (sec < affected_start || sec > affected_end))
333 		return 0; /* not within affected channel range */
334 
335 	wpa_printf(MSG_DEBUG, "Neighboring BSS: " MACSTR
336 		   " freq=%d pri=%d sec=%d",
337 		   MAC2STR(bss->bssid), bss->freq, pri_chan, sec_chan);
338 
339 	if (sec_chan) {
340 		if (pri_freq != pri || sec_freq != sec) {
341 			wpa_printf(MSG_DEBUG,
342 				   "40 MHz pri/sec mismatch with BSS "
343 				   MACSTR
344 				   " <%d,%d> (chan=%d%c) vs. <%d,%d>",
345 				   MAC2STR(bss->bssid),
346 				   pri, sec, pri_chan,
347 				   sec > pri ? '+' : '-',
348 				   pri_freq, sec_freq);
349 			return 1;
350 		}
351 	}
352 
353 	ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0);
354 	if (elems.ht_capabilities) {
355 		struct ieee80211_ht_capabilities *ht_cap =
356 			(struct ieee80211_ht_capabilities *)
357 			elems.ht_capabilities;
358 
359 		if (le_to_host16(ht_cap->ht_capabilities_info) &
360 		    HT_CAP_INFO_40MHZ_INTOLERANT) {
361 			wpa_printf(MSG_DEBUG,
362 				   "40 MHz Intolerant is set on channel %d in BSS "
363 				   MACSTR, pri, MAC2STR(bss->bssid));
364 			return 2;
365 		}
366 	}
367 
368 	return 0;
369 }
370 
371 
check_40mhz_2g4(struct hostapd_hw_modes * mode,struct wpa_scan_results * scan_res,int pri_chan,int sec_chan)372 int check_40mhz_2g4(struct hostapd_hw_modes *mode,
373 		    struct wpa_scan_results *scan_res, int pri_chan,
374 		    int sec_chan)
375 {
376 	int pri_freq, sec_freq;
377 	size_t i;
378 
379 	if (!mode || !scan_res || !pri_chan || !sec_chan ||
380 	    pri_chan == sec_chan)
381 		return 0;
382 
383 	pri_freq = hw_get_freq(mode, pri_chan);
384 	sec_freq = hw_get_freq(mode, sec_chan);
385 
386 	wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
387 		   (pri_freq + sec_freq) / 2 - 25,
388 		   (pri_freq + sec_freq) / 2 + 25);
389 	for (i = 0; i < scan_res->num; i++) {
390 		if (check_bss_coex_40mhz(scan_res->res[i], pri_freq, sec_freq))
391 			return 0;
392 	}
393 
394 	return 1;
395 }
396 
397 
hostapd_set_freq_params(struct hostapd_freq_params * data,enum hostapd_hw_mode mode,int freq,int channel,int enable_edmg,u8 edmg_channel,int ht_enabled,int vht_enabled,int he_enabled,int sec_channel_offset,int oper_chwidth,int center_segment0,int center_segment1,u32 vht_caps,struct he_capabilities * he_cap)398 int hostapd_set_freq_params(struct hostapd_freq_params *data,
399 			    enum hostapd_hw_mode mode,
400 			    int freq, int channel, int enable_edmg,
401 			    u8 edmg_channel, int ht_enabled,
402 			    int vht_enabled, int he_enabled,
403 			    int sec_channel_offset,
404 			    int oper_chwidth, int center_segment0,
405 			    int center_segment1, u32 vht_caps,
406 			    struct he_capabilities *he_cap)
407 {
408 	if (!he_cap)
409 		he_enabled = 0;
410 	os_memset(data, 0, sizeof(*data));
411 	data->mode = mode;
412 	data->freq = freq;
413 	data->channel = channel;
414 	data->ht_enabled = ht_enabled;
415 	data->vht_enabled = vht_enabled;
416 	data->he_enabled = he_enabled;
417 	data->sec_channel_offset = sec_channel_offset;
418 	data->center_freq1 = freq + sec_channel_offset * 10;
419 	data->center_freq2 = 0;
420 	data->bandwidth = sec_channel_offset ? 40 : 20;
421 
422 	hostapd_encode_edmg_chan(enable_edmg, edmg_channel, channel,
423 				 &data->edmg);
424 
425 	if (is_6ghz_freq(freq)) {
426 		if (!data->he_enabled) {
427 			wpa_printf(MSG_ERROR,
428 				   "Can't set 6 GHz mode - HE isn't enabled");
429 			return -1;
430 		}
431 
432 		if (center_idx_to_bw_6ghz(channel) < 0) {
433 			wpa_printf(MSG_ERROR,
434 				   "Invalid control channel for 6 GHz band");
435 			return -1;
436 		}
437 
438 		if (!center_segment0) {
439 			if (center_segment1) {
440 				wpa_printf(MSG_ERROR,
441 					   "Segment 0 center frequency isn't set");
442 				return -1;
443 			}
444 
445 			data->center_freq1 = data->freq;
446 			data->bandwidth = 20;
447 		} else {
448 			int freq1, freq2 = 0;
449 			int bw = center_idx_to_bw_6ghz(center_segment0);
450 
451 			if (bw < 0) {
452 				wpa_printf(MSG_ERROR,
453 					   "Invalid center frequency index for 6 GHz");
454 				return -1;
455 			}
456 
457 			freq1 = ieee80211_chan_to_freq(NULL, 131,
458 						       center_segment0);
459 			if (freq1 < 0) {
460 				wpa_printf(MSG_ERROR,
461 					   "Invalid segment 0 center frequency for 6 GHz");
462 				return -1;
463 			}
464 
465 			if (center_segment1) {
466 				if (center_idx_to_bw_6ghz(center_segment1) != 2 ||
467 				    bw != 2) {
468 					wpa_printf(MSG_ERROR,
469 						   "6 GHz 80+80 MHz configuration doesn't use valid 80 MHz channels");
470 					return -1;
471 				}
472 
473 				freq2 = ieee80211_chan_to_freq(NULL, 131,
474 							       center_segment1);
475 				if (freq2 < 0) {
476 					wpa_printf(MSG_ERROR,
477 						   "Invalid segment 1 center frequency for UHB");
478 					return -1;
479 				}
480 			}
481 
482 			data->bandwidth = (1 << (u8) bw) * 20;
483 			data->center_freq1 = freq1;
484 			data->center_freq2 = freq2;
485 		}
486 		data->ht_enabled = 0;
487 		data->vht_enabled = 0;
488 
489 		return 0;
490 	}
491 
492 	if (data->he_enabled) switch (oper_chwidth) {
493 	case CHANWIDTH_USE_HT:
494 		if (mode == HOSTAPD_MODE_IEEE80211G && sec_channel_offset) {
495 			if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
496 			      HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G)) {
497 				wpa_printf(MSG_ERROR,
498 					   "40 MHz channel width is not supported in 2.4 GHz");
499 				return -1;
500 			}
501 			break;
502 		}
503 		/* fall through */
504 	case CHANWIDTH_80MHZ:
505 		if (mode == HOSTAPD_MODE_IEEE80211A) {
506 			if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
507 			      HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
508 				wpa_printf(MSG_ERROR,
509 					   "40/80 MHz channel width is not supported in 5/6 GHz");
510 				return -1;
511 			}
512 		}
513 		break;
514 	case CHANWIDTH_80P80MHZ:
515 		if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
516 		      HE_PHYCAP_CHANNEL_WIDTH_SET_80PLUS80MHZ_IN_5G)) {
517 			wpa_printf(MSG_ERROR,
518 				   "80+80 MHz channel width is not supported in 5/6 GHz");
519 			return -1;
520 		}
521 		break;
522 	case CHANWIDTH_160MHZ:
523 		if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
524 		      HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G)) {
525 			wpa_printf(MSG_ERROR,
526 				   "160 MHz channel width is not supported in 5 / 6GHz");
527 			return -1;
528 		}
529 		break;
530 	} else if (data->vht_enabled) switch (oper_chwidth) {
531 	case CHANWIDTH_USE_HT:
532 		break;
533 	case CHANWIDTH_80P80MHZ:
534 		if (!(vht_caps & VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)) {
535 			wpa_printf(MSG_ERROR,
536 				   "80+80 channel width is not supported!");
537 			return -1;
538 		}
539 		/* fall through */
540 	case CHANWIDTH_80MHZ:
541 		break;
542 	case CHANWIDTH_160MHZ:
543 		if (!(vht_caps & (VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
544 				  VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))) {
545 			wpa_printf(MSG_ERROR,
546 				   "160 MHz channel width is not supported!");
547 			return -1;
548 		}
549 		break;
550 	}
551 
552 	if (data->he_enabled || data->vht_enabled) switch (oper_chwidth) {
553 	case CHANWIDTH_USE_HT:
554 		if (center_segment1 ||
555 		    (center_segment0 != 0 &&
556 		     5000 + center_segment0 * 5 != data->center_freq1 &&
557 		     2407 + center_segment0 * 5 != data->center_freq1)) {
558 			wpa_printf(MSG_ERROR,
559 				   "20/40 MHz: center segment 0 (=%d) and center freq 1 (=%d) not in sync",
560 				   center_segment0, data->center_freq1);
561 			return -1;
562 		}
563 		break;
564 	case CHANWIDTH_80P80MHZ:
565 		if (center_segment1 == center_segment0 + 4 ||
566 		    center_segment1 == center_segment0 - 4) {
567 			wpa_printf(MSG_ERROR,
568 				   "80+80 MHz: center segment 1 only 20 MHz apart");
569 			return -1;
570 		}
571 		data->center_freq2 = 5000 + center_segment1 * 5;
572 		/* fall through */
573 	case CHANWIDTH_80MHZ:
574 		data->bandwidth = 80;
575 		if ((oper_chwidth == CHANWIDTH_80MHZ &&
576 		     center_segment1) ||
577 		    (oper_chwidth == CHANWIDTH_80P80MHZ &&
578 		     !center_segment1) ||
579 		    !sec_channel_offset) {
580 			wpa_printf(MSG_ERROR,
581 				   "80/80+80 MHz: center segment 1 wrong or no second channel offset");
582 			return -1;
583 		}
584 		if (!center_segment0) {
585 			if (channel <= 48)
586 				center_segment0 = 42;
587 			else if (channel <= 64)
588 				center_segment0 = 58;
589 			else if (channel <= 112)
590 				center_segment0 = 106;
591 			else if (channel <= 128)
592 				center_segment0 = 122;
593 			else if (channel <= 144)
594 				center_segment0 = 138;
595 			else if (channel <= 161)
596 				center_segment0 = 155;
597 			else if (channel <= 177)
598 				center_segment0 = 171;
599 			data->center_freq1 = 5000 + center_segment0 * 5;
600 		} else {
601 			/*
602 			 * Note: HT/VHT config and params are coupled. Check if
603 			 * HT40 channel band is in VHT80 Pri channel band
604 			 * configuration.
605 			 */
606 			if (center_segment0 == channel + 6 ||
607 			    center_segment0 == channel + 2 ||
608 			    center_segment0 == channel - 2 ||
609 			    center_segment0 == channel - 6)
610 				data->center_freq1 = 5000 + center_segment0 * 5;
611 			else {
612 				wpa_printf(MSG_ERROR,
613 					   "Wrong coupling between HT and VHT/HE channel setting");
614 				return -1;
615 			}
616 		}
617 		break;
618 	case CHANWIDTH_160MHZ:
619 		data->bandwidth = 160;
620 		if (center_segment1) {
621 			wpa_printf(MSG_ERROR,
622 				   "160 MHz: center segment 1 should not be set");
623 			return -1;
624 		}
625 		if (!sec_channel_offset) {
626 			wpa_printf(MSG_ERROR,
627 				   "160 MHz: second channel offset not set");
628 			return -1;
629 		}
630 		/*
631 		 * Note: HT/VHT config and params are coupled. Check if
632 		 * HT40 channel band is in VHT160 channel band configuration.
633 		 */
634 		if (center_segment0 == channel + 14 ||
635 		    center_segment0 == channel + 10 ||
636 		    center_segment0 == channel + 6 ||
637 		    center_segment0 == channel + 2 ||
638 		    center_segment0 == channel - 2 ||
639 		    center_segment0 == channel - 6 ||
640 		    center_segment0 == channel - 10 ||
641 		    center_segment0 == channel - 14)
642 			data->center_freq1 = 5000 + center_segment0 * 5;
643 		else {
644 			wpa_printf(MSG_ERROR,
645 				   "160 MHz: HT40 channel band is not in 160 MHz band");
646 			return -1;
647 		}
648 		break;
649 	}
650 
651 	return 0;
652 }
653 
654 
set_disable_ht40(struct ieee80211_ht_capabilities * htcaps,int disabled)655 void set_disable_ht40(struct ieee80211_ht_capabilities *htcaps,
656 		      int disabled)
657 {
658 	/* Masking these out disables HT40 */
659 	le16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
660 				HT_CAP_INFO_SHORT_GI40MHZ);
661 
662 	if (disabled)
663 		htcaps->ht_capabilities_info &= ~msk;
664 	else
665 		htcaps->ht_capabilities_info |= msk;
666 }
667 
668 
669 #ifdef CONFIG_IEEE80211AC
670 
_ieee80211ac_cap_check(u32 hw,u32 conf,u32 cap,const char * name)671 static int _ieee80211ac_cap_check(u32 hw, u32 conf, u32 cap,
672 				  const char *name)
673 {
674 	u32 req_cap = conf & cap;
675 
676 	/*
677 	 * Make sure we support all requested capabilities.
678 	 * NOTE: We assume that 'cap' represents a capability mask,
679 	 * not a discrete value.
680 	 */
681 	if ((hw & req_cap) != req_cap) {
682 		wpa_printf(MSG_ERROR,
683 			   "Driver does not support configured VHT capability [%s]",
684 			   name);
685 		return 0;
686 	}
687 	return 1;
688 }
689 
690 
ieee80211ac_cap_check_max(u32 hw,u32 conf,u32 mask,unsigned int shift,const char * name)691 static int ieee80211ac_cap_check_max(u32 hw, u32 conf, u32 mask,
692 				     unsigned int shift,
693 				     const char *name)
694 {
695 	u32 hw_max = hw & mask;
696 	u32 conf_val = conf & mask;
697 
698 	if (conf_val > hw_max) {
699 		wpa_printf(MSG_ERROR,
700 			   "Configured VHT capability [%s] exceeds max value supported by the driver (%d > %d)",
701 			   name, conf_val >> shift, hw_max >> shift);
702 		return 0;
703 	}
704 	return 1;
705 }
706 
707 
ieee80211ac_cap_check(u32 hw,u32 conf)708 int ieee80211ac_cap_check(u32 hw, u32 conf)
709 {
710 #define VHT_CAP_CHECK(cap) \
711 	do { \
712 		if (!_ieee80211ac_cap_check(hw, conf, cap, #cap)) \
713 			return 0; \
714 	} while (0)
715 
716 #define VHT_CAP_CHECK_MAX(cap) \
717 	do { \
718 		if (!ieee80211ac_cap_check_max(hw, conf, cap, cap ## _SHIFT, \
719 					       #cap)) \
720 			return 0; \
721 	} while (0)
722 
723 	VHT_CAP_CHECK_MAX(VHT_CAP_MAX_MPDU_LENGTH_MASK);
724 	VHT_CAP_CHECK_MAX(VHT_CAP_SUPP_CHAN_WIDTH_MASK);
725 	VHT_CAP_CHECK(VHT_CAP_RXLDPC);
726 	VHT_CAP_CHECK(VHT_CAP_SHORT_GI_80);
727 	VHT_CAP_CHECK(VHT_CAP_SHORT_GI_160);
728 	VHT_CAP_CHECK(VHT_CAP_TXSTBC);
729 	VHT_CAP_CHECK_MAX(VHT_CAP_RXSTBC_MASK);
730 	VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMER_CAPABLE);
731 	VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMEE_CAPABLE);
732 	VHT_CAP_CHECK_MAX(VHT_CAP_BEAMFORMEE_STS_MAX);
733 	VHT_CAP_CHECK_MAX(VHT_CAP_SOUNDING_DIMENSION_MAX);
734 	VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMER_CAPABLE);
735 	VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMEE_CAPABLE);
736 	VHT_CAP_CHECK(VHT_CAP_VHT_TXOP_PS);
737 	VHT_CAP_CHECK(VHT_CAP_HTC_VHT);
738 	VHT_CAP_CHECK_MAX(VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX);
739 	VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_UNSOL_MFB);
740 	VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_MRQ_MFB);
741 	VHT_CAP_CHECK(VHT_CAP_RX_ANTENNA_PATTERN);
742 	VHT_CAP_CHECK(VHT_CAP_TX_ANTENNA_PATTERN);
743 
744 #undef VHT_CAP_CHECK
745 #undef VHT_CAP_CHECK_MAX
746 
747 	return 1;
748 }
749 
750 #endif /* CONFIG_IEEE80211AC */
751 
752 
num_chan_to_bw(int num_chans)753 u32 num_chan_to_bw(int num_chans)
754 {
755 	switch (num_chans) {
756 	case 2:
757 	case 4:
758 	case 8:
759 		return num_chans * 20;
760 	default:
761 		return 20;
762 	}
763 }
764 
765 
766 /* check if BW is applicable for channel */
chan_bw_allowed(const struct hostapd_channel_data * chan,u32 bw,int ht40_plus,int pri)767 int chan_bw_allowed(const struct hostapd_channel_data *chan, u32 bw,
768 		    int ht40_plus, int pri)
769 {
770 	u32 bw_mask;
771 
772 	switch (bw) {
773 	case 20:
774 		bw_mask = HOSTAPD_CHAN_WIDTH_20;
775 		break;
776 	case 40:
777 		/* HT 40 MHz support declared only for primary channel,
778 		 * just skip 40 MHz secondary checking */
779 		if (pri && ht40_plus)
780 			bw_mask = HOSTAPD_CHAN_WIDTH_40P;
781 		else if (pri && !ht40_plus)
782 			bw_mask = HOSTAPD_CHAN_WIDTH_40M;
783 		else
784 			bw_mask = 0;
785 		break;
786 	case 80:
787 		bw_mask = HOSTAPD_CHAN_WIDTH_80;
788 		break;
789 	case 160:
790 		bw_mask = HOSTAPD_CHAN_WIDTH_160;
791 		break;
792 	default:
793 		bw_mask = 0;
794 		break;
795 	}
796 
797 	return (chan->allowed_bw & bw_mask) == bw_mask;
798 }
799 
800 
801 /* check if channel is allowed to be used as primary */
chan_pri_allowed(const struct hostapd_channel_data * chan)802 int chan_pri_allowed(const struct hostapd_channel_data *chan)
803 {
804 	return !(chan->flag & HOSTAPD_CHAN_DISABLED) &&
805 		(chan->allowed_bw & HOSTAPD_CHAN_WIDTH_20);
806 }
807