• 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_get_channel_freq(enum hostapd_hw_mode mode,int freq,int * chan,struct hostapd_hw_modes * hw_features,int num_hw_features)44 hw_get_channel_freq(enum hostapd_hw_mode mode, int freq, int *chan,
45 		    struct hostapd_hw_modes *hw_features, int num_hw_features)
46 {
47 	int i, j;
48 
49 	if (chan)
50 		*chan = 0;
51 
52 	if (!hw_features)
53 		return NULL;
54 
55 	for (j = 0; j < num_hw_features; j++) {
56 		struct hostapd_hw_modes *curr_mode = &hw_features[j];
57 
58 		if (curr_mode->mode != mode)
59 			continue;
60 		for (i = 0; i < curr_mode->num_channels; i++) {
61 			struct hostapd_channel_data *ch =
62 				&curr_mode->channels[i];
63 
64 			if (ch->freq == freq) {
65 				if (chan)
66 					*chan = ch->chan;
67 				return ch;
68 			}
69 		}
70 	}
71 
72 	return NULL;
73 }
74 
75 
hw_get_freq(struct hostapd_hw_modes * mode,int chan)76 int hw_get_freq(struct hostapd_hw_modes *mode, int chan)
77 {
78 	int freq;
79 
80 	hw_get_channel_chan(mode, chan, &freq);
81 
82 	return freq;
83 }
84 
85 
hw_get_chan(enum hostapd_hw_mode mode,int freq,struct hostapd_hw_modes * hw_features,int num_hw_features)86 int hw_get_chan(enum hostapd_hw_mode mode, int freq,
87 		struct hostapd_hw_modes *hw_features, int num_hw_features)
88 {
89 	int chan;
90 
91 	hw_get_channel_freq(mode, freq, &chan, hw_features, num_hw_features);
92 
93 	return chan;
94 }
95 
96 
allowed_ht40_channel_pair(enum hostapd_hw_mode mode,struct hostapd_channel_data * p_chan,struct hostapd_channel_data * s_chan)97 int allowed_ht40_channel_pair(enum hostapd_hw_mode mode,
98 			      struct hostapd_channel_data *p_chan,
99 			      struct hostapd_channel_data *s_chan)
100 {
101 	int ok, first;
102 	int allowed[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 140,
103 			  149, 157, 165, 184, 192 };
104 	size_t k;
105 	int ht40_plus, pri_chan, sec_chan;
106 
107 	if (!p_chan || !s_chan)
108 		return 0;
109 	pri_chan = p_chan->chan;
110 	sec_chan = s_chan->chan;
111 
112 	ht40_plus = pri_chan < sec_chan;
113 
114 	if (pri_chan == sec_chan || !sec_chan) {
115 		if (chan_pri_allowed(p_chan))
116 			return 1; /* HT40 not used */
117 
118 		wpa_printf(MSG_ERROR, "Channel %d is not allowed as primary",
119 			   pri_chan);
120 		return 0;
121 	}
122 
123 	wpa_printf(MSG_DEBUG,
124 		   "HT40: control channel: %d (%d MHz), secondary channel: %d (%d MHz)",
125 		   pri_chan, p_chan->freq, sec_chan, s_chan->freq);
126 
127 	/* Verify that HT40 secondary channel is an allowed 20 MHz
128 	 * channel */
129 	if ((s_chan->flag & HOSTAPD_CHAN_DISABLED) ||
130 	    (ht40_plus && !(p_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40P)) ||
131 	    (!ht40_plus && !(p_chan->allowed_bw & HOSTAPD_CHAN_WIDTH_40M))) {
132 		wpa_printf(MSG_ERROR, "HT40 secondary channel %d not allowed",
133 			   sec_chan);
134 		return 0;
135 	}
136 
137 	/*
138 	 * Verify that HT40 primary,secondary channel pair is allowed per
139 	 * IEEE 802.11n Annex J. This is only needed for 5 GHz band since
140 	 * 2.4 GHz rules allow all cases where the secondary channel fits into
141 	 * the list of allowed channels (already checked above).
142 	 */
143 	if (mode != HOSTAPD_MODE_IEEE80211A)
144 		return 1;
145 
146 	first = pri_chan < sec_chan ? pri_chan : sec_chan;
147 
148 	ok = 0;
149 	for (k = 0; k < ARRAY_SIZE(allowed); k++) {
150 		if (first == allowed[k]) {
151 			ok = 1;
152 			break;
153 		}
154 	}
155 	if (!ok) {
156 		wpa_printf(MSG_ERROR, "HT40 channel pair (%d, %d) not allowed",
157 			   pri_chan, sec_chan);
158 		return 0;
159 	}
160 
161 	return 1;
162 }
163 
164 
get_pri_sec_chan(struct wpa_scan_res * bss,int * pri_chan,int * sec_chan)165 void get_pri_sec_chan(struct wpa_scan_res *bss, int *pri_chan, int *sec_chan)
166 {
167 	struct ieee80211_ht_operation *oper;
168 	struct ieee802_11_elems elems;
169 
170 	*pri_chan = *sec_chan = 0;
171 
172 	ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0);
173 	if (elems.ht_operation) {
174 		oper = (struct ieee80211_ht_operation *) elems.ht_operation;
175 		*pri_chan = oper->primary_chan;
176 		if (oper->ht_param & HT_INFO_HT_PARAM_STA_CHNL_WIDTH) {
177 			int sec = oper->ht_param &
178 				HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK;
179 			if (sec == HT_INFO_HT_PARAM_SECONDARY_CHNL_ABOVE)
180 				*sec_chan = *pri_chan + 4;
181 			else if (sec == HT_INFO_HT_PARAM_SECONDARY_CHNL_BELOW)
182 				*sec_chan = *pri_chan - 4;
183 		}
184 	}
185 }
186 
187 
check_40mhz_5g(struct wpa_scan_results * scan_res,struct hostapd_channel_data * pri_chan,struct hostapd_channel_data * sec_chan)188 int check_40mhz_5g(struct wpa_scan_results *scan_res,
189 		   struct hostapd_channel_data *pri_chan,
190 		   struct hostapd_channel_data *sec_chan)
191 {
192 	int pri_bss, sec_bss;
193 	int bss_pri_chan, bss_sec_chan;
194 	size_t i;
195 	int match;
196 
197 	if (!scan_res || !pri_chan || !sec_chan ||
198 	    pri_chan->freq == sec_chan->freq)
199 		return 0;
200 
201 	/*
202 	 * Switch PRI/SEC channels if Beacons were detected on selected SEC
203 	 * channel, but not on selected PRI channel.
204 	 */
205 	pri_bss = sec_bss = 0;
206 	for (i = 0; i < scan_res->num; i++) {
207 		struct wpa_scan_res *bss = scan_res->res[i];
208 		if (bss->freq == pri_chan->freq)
209 			pri_bss++;
210 		else if (bss->freq == sec_chan->freq)
211 			sec_bss++;
212 	}
213 	if (sec_bss && !pri_bss) {
214 		wpa_printf(MSG_INFO,
215 			   "Switch own primary and secondary channel to get secondary channel with no Beacons from other BSSes");
216 		return 2;
217 	}
218 
219 	/*
220 	 * Match PRI/SEC channel with any existing HT40 BSS on the same
221 	 * channels that we are about to use (if already mixed order in
222 	 * existing BSSes, use own preference).
223 	 */
224 	match = 0;
225 	for (i = 0; i < scan_res->num; i++) {
226 		struct wpa_scan_res *bss = scan_res->res[i];
227 		get_pri_sec_chan(bss, &bss_pri_chan, &bss_sec_chan);
228 		if (pri_chan->chan == bss_pri_chan &&
229 		    sec_chan->chan == bss_sec_chan) {
230 			match = 1;
231 			break;
232 		}
233 	}
234 	if (!match) {
235 		for (i = 0; i < scan_res->num; i++) {
236 			struct wpa_scan_res *bss = scan_res->res[i];
237 			get_pri_sec_chan(bss, &bss_pri_chan, &bss_sec_chan);
238 			if (pri_chan->chan == bss_sec_chan &&
239 			    sec_chan->chan == bss_pri_chan) {
240 				wpa_printf(MSG_INFO, "Switch own primary and "
241 					   "secondary channel due to BSS "
242 					   "overlap with " MACSTR,
243 					   MAC2STR(bss->bssid));
244 				return 2;
245 			}
246 		}
247 	}
248 
249 	return 1;
250 }
251 
252 
check_20mhz_bss(struct wpa_scan_res * bss,int pri_freq,int start,int end)253 static int check_20mhz_bss(struct wpa_scan_res *bss, int pri_freq, int start,
254 			   int end)
255 {
256 	struct ieee802_11_elems elems;
257 	struct ieee80211_ht_operation *oper;
258 
259 	if (bss->freq < start || bss->freq > end || bss->freq == pri_freq)
260 		return 0;
261 
262 	ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0);
263 	if (!elems.ht_capabilities) {
264 		wpa_printf(MSG_DEBUG, "Found overlapping legacy BSS: "
265 			   MACSTR " freq=%d", MAC2STR(bss->bssid), bss->freq);
266 		return 1;
267 	}
268 
269 	if (elems.ht_operation) {
270 		oper = (struct ieee80211_ht_operation *) elems.ht_operation;
271 		if (oper->ht_param & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)
272 			return 0;
273 
274 		wpa_printf(MSG_DEBUG, "Found overlapping 20 MHz HT BSS: "
275 			   MACSTR " freq=%d", MAC2STR(bss->bssid), bss->freq);
276 		return 1;
277 	}
278 	return 0;
279 }
280 
281 
282 /*
283  * Returns:
284  * 0: no impact
285  * 1: overlapping BSS
286  * 2: overlapping BSS with 40 MHz intolerant advertisement
287  */
check_bss_coex_40mhz(struct wpa_scan_res * bss,int pri_freq,int sec_freq)288 int check_bss_coex_40mhz(struct wpa_scan_res *bss, int pri_freq, int sec_freq)
289 {
290 	int affected_start, affected_end;
291 	struct ieee802_11_elems elems;
292 	int pri_chan, sec_chan;
293 	int pri = bss->freq;
294 	int sec = pri;
295 
296 	if (pri_freq == sec_freq)
297 		return 1;
298 
299 	affected_start = (pri_freq + sec_freq) / 2 - 25;
300 	affected_end = (pri_freq + sec_freq) / 2 + 25;
301 
302 	/* Check for overlapping 20 MHz BSS */
303 	if (check_20mhz_bss(bss, pri_freq, affected_start, affected_end)) {
304 		wpa_printf(MSG_DEBUG, "Overlapping 20 MHz BSS is found");
305 		return 1;
306 	}
307 
308 	get_pri_sec_chan(bss, &pri_chan, &sec_chan);
309 
310 	if (sec_chan) {
311 		if (sec_chan < pri_chan)
312 			sec = pri - 20;
313 		else
314 			sec = pri + 20;
315 	}
316 
317 	if ((pri < affected_start || pri > affected_end) &&
318 	    (sec < affected_start || sec > affected_end))
319 		return 0; /* not within affected channel range */
320 
321 	wpa_printf(MSG_DEBUG, "Neighboring BSS: " MACSTR
322 		   " freq=%d pri=%d sec=%d",
323 		   MAC2STR(bss->bssid), bss->freq, pri_chan, sec_chan);
324 
325 	if (sec_chan) {
326 		if (pri_freq != pri || sec_freq != sec) {
327 			wpa_printf(MSG_DEBUG,
328 				   "40 MHz pri/sec mismatch with BSS "
329 				   MACSTR
330 				   " <%d,%d> (chan=%d%c) vs. <%d,%d>",
331 				   MAC2STR(bss->bssid),
332 				   pri, sec, pri_chan,
333 				   sec > pri ? '+' : '-',
334 				   pri_freq, sec_freq);
335 			return 1;
336 		}
337 	}
338 
339 	ieee802_11_parse_elems((u8 *) (bss + 1), bss->ie_len, &elems, 0);
340 	if (elems.ht_capabilities) {
341 		struct ieee80211_ht_capabilities *ht_cap =
342 			(struct ieee80211_ht_capabilities *)
343 			elems.ht_capabilities;
344 
345 		if (le_to_host16(ht_cap->ht_capabilities_info) &
346 		    HT_CAP_INFO_40MHZ_INTOLERANT) {
347 			wpa_printf(MSG_DEBUG,
348 				   "40 MHz Intolerant is set on channel %d in BSS "
349 				   MACSTR, pri, MAC2STR(bss->bssid));
350 			return 2;
351 		}
352 	}
353 
354 	return 0;
355 }
356 
357 
check_40mhz_2g4(struct hostapd_hw_modes * mode,struct wpa_scan_results * scan_res,int pri_chan,int sec_chan)358 int check_40mhz_2g4(struct hostapd_hw_modes *mode,
359 		    struct wpa_scan_results *scan_res, int pri_chan,
360 		    int sec_chan)
361 {
362 	int pri_freq, sec_freq;
363 	size_t i;
364 
365 	if (!mode || !scan_res || !pri_chan || !sec_chan ||
366 	    pri_chan == sec_chan)
367 		return 0;
368 
369 	pri_freq = hw_get_freq(mode, pri_chan);
370 	sec_freq = hw_get_freq(mode, sec_chan);
371 
372 	wpa_printf(MSG_DEBUG, "40 MHz affected channel range: [%d,%d] MHz",
373 		   (pri_freq + sec_freq) / 2 - 25,
374 		   (pri_freq + sec_freq) / 2 + 25);
375 	for (i = 0; i < scan_res->num; i++) {
376 		if (check_bss_coex_40mhz(scan_res->res[i], pri_freq, sec_freq))
377 			return 0;
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,int sec_channel_offset,int oper_chwidth,int center_segment0,int center_segment1,u32 vht_caps,struct he_capabilities * he_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 			    int sec_channel_offset,
390 			    int oper_chwidth, int center_segment0,
391 			    int center_segment1, u32 vht_caps,
392 			    struct he_capabilities *he_cap)
393 {
394 	if (!he_cap)
395 		he_enabled = 0;
396 	os_memset(data, 0, sizeof(*data));
397 	data->mode = mode;
398 	data->freq = freq;
399 	data->channel = channel;
400 	data->ht_enabled = ht_enabled;
401 	data->vht_enabled = vht_enabled;
402 	data->he_enabled = he_enabled;
403 	data->sec_channel_offset = sec_channel_offset;
404 	data->center_freq1 = freq + sec_channel_offset * 10;
405 	data->center_freq2 = 0;
406 	data->bandwidth = sec_channel_offset ? 40 : 20;
407 
408 	hostapd_encode_edmg_chan(enable_edmg, edmg_channel, channel,
409 				 &data->edmg);
410 
411 	if (is_6ghz_freq(freq)) {
412 		if (!data->he_enabled) {
413 			wpa_printf(MSG_ERROR,
414 				   "Can't set 6 GHz mode - HE isn't enabled");
415 			return -1;
416 		}
417 
418 		if (center_idx_to_bw_6ghz(channel) != 0) {
419 			wpa_printf(MSG_ERROR,
420 				   "Invalid control channel for 6 GHz band");
421 			return -1;
422 		}
423 
424 		if (!center_segment0) {
425 			if (center_segment1) {
426 				wpa_printf(MSG_ERROR,
427 					   "Segment 0 center frequency isn't set");
428 				return -1;
429 			}
430 
431 			data->center_freq1 = data->freq;
432 			data->bandwidth = 20;
433 		} else {
434 			int freq1, freq2 = 0;
435 			int bw = center_idx_to_bw_6ghz(center_segment0);
436 
437 			if (bw < 0) {
438 				wpa_printf(MSG_ERROR,
439 					   "Invalid center frequency index for 6 GHz");
440 				return -1;
441 			}
442 
443 			freq1 = ieee80211_chan_to_freq(NULL, 131,
444 						       center_segment0);
445 			if (freq1 < 0) {
446 				wpa_printf(MSG_ERROR,
447 					   "Invalid segment 0 center frequency for 6 GHz");
448 				return -1;
449 			}
450 
451 			if (center_segment1) {
452 				if (center_idx_to_bw_6ghz(center_segment1) != 2 ||
453 				    bw != 2) {
454 					wpa_printf(MSG_ERROR,
455 						   "6 GHz 80+80 MHz configuration doesn't use valid 80 MHz channels");
456 					return -1;
457 				}
458 
459 				freq2 = ieee80211_chan_to_freq(NULL, 131,
460 							       center_segment1);
461 				if (freq2 < 0) {
462 					wpa_printf(MSG_ERROR,
463 						   "Invalid segment 1 center frequency for UHB");
464 					return -1;
465 				}
466 			}
467 
468 			data->bandwidth = (1 << (u8) bw) * 20;
469 			data->center_freq1 = freq1;
470 			data->center_freq2 = freq2;
471 		}
472 		data->ht_enabled = 0;
473 		data->vht_enabled = 0;
474 
475 		return 0;
476 	}
477 
478 	if (data->he_enabled) switch (oper_chwidth) {
479 	case CHANWIDTH_USE_HT:
480 		if (mode == HOSTAPD_MODE_IEEE80211G && sec_channel_offset) {
481 			if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
482 			      HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_IN_2G)) {
483 				wpa_printf(MSG_ERROR,
484 					   "40 MHz channel width is not supported in 2.4 GHz");
485 				return -1;
486 			}
487 			break;
488 		}
489 		/* fall through */
490 	case CHANWIDTH_80MHZ:
491 		if (mode == HOSTAPD_MODE_IEEE80211A) {
492 			if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
493 			      HE_PHYCAP_CHANNEL_WIDTH_SET_40MHZ_80MHZ_IN_5G)) {
494 				wpa_printf(MSG_ERROR,
495 					   "40/80 MHz channel width is not supported in 5/6 GHz");
496 				return -1;
497 			}
498 		}
499 		break;
500 	case CHANWIDTH_80P80MHZ:
501 		if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
502 		      HE_PHYCAP_CHANNEL_WIDTH_SET_80PLUS80MHZ_IN_5G)) {
503 			wpa_printf(MSG_ERROR,
504 				   "80+80 MHz channel width is not supported in 5/6 GHz");
505 			return -1;
506 		}
507 		break;
508 	case CHANWIDTH_160MHZ:
509 		if (!(he_cap->phy_cap[HE_PHYCAP_CHANNEL_WIDTH_SET_IDX] &
510 		      HE_PHYCAP_CHANNEL_WIDTH_SET_160MHZ_IN_5G)) {
511 			wpa_printf(MSG_ERROR,
512 				   "160 MHz channel width is not supported in 5 / 6GHz");
513 			return -1;
514 		}
515 		break;
516 	} else if (data->vht_enabled) switch (oper_chwidth) {
517 	case CHANWIDTH_USE_HT:
518 		break;
519 	case CHANWIDTH_80P80MHZ:
520 		if (!(vht_caps & VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)) {
521 			wpa_printf(MSG_ERROR,
522 				   "80+80 channel width is not supported!");
523 			return -1;
524 		}
525 		/* fall through */
526 	case CHANWIDTH_80MHZ:
527 		break;
528 	case CHANWIDTH_160MHZ:
529 		if (!(vht_caps & (VHT_CAP_SUPP_CHAN_WIDTH_160MHZ |
530 				  VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ))) {
531 			wpa_printf(MSG_ERROR,
532 				   "160 MHz channel width is not supported!");
533 			return -1;
534 		}
535 		break;
536 	}
537 
538 	if (data->he_enabled || data->vht_enabled) switch (oper_chwidth) {
539 	case CHANWIDTH_USE_HT:
540 		if (center_segment1 ||
541 		    (center_segment0 != 0 &&
542 		     5000 + center_segment0 * 5 != data->center_freq1 &&
543 		     2407 + center_segment0 * 5 != data->center_freq1))
544 			return -1;
545 		break;
546 	case CHANWIDTH_80P80MHZ:
547 		if (center_segment1 == center_segment0 + 4 ||
548 		    center_segment1 == center_segment0 - 4)
549 			return -1;
550 		data->center_freq2 = 5000 + center_segment1 * 5;
551 		/* fall through */
552 	case CHANWIDTH_80MHZ:
553 		data->bandwidth = 80;
554 		if ((oper_chwidth == CHANWIDTH_80MHZ &&
555 		     center_segment1) ||
556 		    (oper_chwidth == CHANWIDTH_80P80MHZ &&
557 		     !center_segment1) ||
558 		    !sec_channel_offset)
559 			return -1;
560 		if (!center_segment0) {
561 			if (channel <= 48)
562 				center_segment0 = 42;
563 			else if (channel <= 64)
564 				center_segment0 = 58;
565 			else if (channel <= 112)
566 				center_segment0 = 106;
567 			else if (channel <= 128)
568 				center_segment0 = 122;
569 			else if (channel <= 144)
570 				center_segment0 = 138;
571 			else if (channel <= 161)
572 				center_segment0 = 155;
573 			data->center_freq1 = 5000 + center_segment0 * 5;
574 		} else {
575 			/*
576 			 * Note: HT/VHT config and params are coupled. Check if
577 			 * HT40 channel band is in VHT80 Pri channel band
578 			 * configuration.
579 			 */
580 			if (center_segment0 == channel + 6 ||
581 			    center_segment0 == channel + 2 ||
582 			    center_segment0 == channel - 2 ||
583 			    center_segment0 == channel - 6)
584 				data->center_freq1 = 5000 + center_segment0 * 5;
585 			else
586 				return -1;
587 		}
588 		break;
589 	case CHANWIDTH_160MHZ:
590 		data->bandwidth = 160;
591 		if (center_segment1)
592 			return -1;
593 		if (!sec_channel_offset)
594 			return -1;
595 		/*
596 		 * Note: HT/VHT config and params are coupled. Check if
597 		 * HT40 channel band is in VHT160 channel band configuration.
598 		 */
599 		if (center_segment0 == channel + 14 ||
600 		    center_segment0 == channel + 10 ||
601 		    center_segment0 == channel + 6 ||
602 		    center_segment0 == channel + 2 ||
603 		    center_segment0 == channel - 2 ||
604 		    center_segment0 == channel - 6 ||
605 		    center_segment0 == channel - 10 ||
606 		    center_segment0 == channel - 14)
607 			data->center_freq1 = 5000 + center_segment0 * 5;
608 		else
609 			return -1;
610 		break;
611 	}
612 
613 	return 0;
614 }
615 
616 
set_disable_ht40(struct ieee80211_ht_capabilities * htcaps,int disabled)617 void set_disable_ht40(struct ieee80211_ht_capabilities *htcaps,
618 		      int disabled)
619 {
620 	/* Masking these out disables HT40 */
621 	le16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
622 				HT_CAP_INFO_SHORT_GI40MHZ);
623 
624 	if (disabled)
625 		htcaps->ht_capabilities_info &= ~msk;
626 	else
627 		htcaps->ht_capabilities_info |= msk;
628 }
629 
630 
631 #ifdef CONFIG_IEEE80211AC
632 
_ieee80211ac_cap_check(u32 hw,u32 conf,u32 cap,const char * name)633 static int _ieee80211ac_cap_check(u32 hw, u32 conf, u32 cap,
634 				  const char *name)
635 {
636 	u32 req_cap = conf & cap;
637 
638 	/*
639 	 * Make sure we support all requested capabilities.
640 	 * NOTE: We assume that 'cap' represents a capability mask,
641 	 * not a discrete value.
642 	 */
643 	if ((hw & req_cap) != req_cap) {
644 		wpa_printf(MSG_ERROR,
645 			   "Driver does not support configured VHT capability [%s]",
646 			   name);
647 		return 0;
648 	}
649 	return 1;
650 }
651 
652 
ieee80211ac_cap_check_max(u32 hw,u32 conf,u32 mask,unsigned int shift,const char * name)653 static int ieee80211ac_cap_check_max(u32 hw, u32 conf, u32 mask,
654 				     unsigned int shift,
655 				     const char *name)
656 {
657 	u32 hw_max = hw & mask;
658 	u32 conf_val = conf & mask;
659 
660 	if (conf_val > hw_max) {
661 		wpa_printf(MSG_ERROR,
662 			   "Configured VHT capability [%s] exceeds max value supported by the driver (%d > %d)",
663 			   name, conf_val >> shift, hw_max >> shift);
664 		return 0;
665 	}
666 	return 1;
667 }
668 
669 
ieee80211ac_cap_check(u32 hw,u32 conf)670 int ieee80211ac_cap_check(u32 hw, u32 conf)
671 {
672 #define VHT_CAP_CHECK(cap) \
673 	do { \
674 		if (!_ieee80211ac_cap_check(hw, conf, cap, #cap)) \
675 			return 0; \
676 	} while (0)
677 
678 #define VHT_CAP_CHECK_MAX(cap) \
679 	do { \
680 		if (!ieee80211ac_cap_check_max(hw, conf, cap, cap ## _SHIFT, \
681 					       #cap)) \
682 			return 0; \
683 	} while (0)
684 
685 	VHT_CAP_CHECK_MAX(VHT_CAP_MAX_MPDU_LENGTH_MASK);
686 	VHT_CAP_CHECK_MAX(VHT_CAP_SUPP_CHAN_WIDTH_MASK);
687 	VHT_CAP_CHECK(VHT_CAP_RXLDPC);
688 	VHT_CAP_CHECK(VHT_CAP_SHORT_GI_80);
689 	VHT_CAP_CHECK(VHT_CAP_SHORT_GI_160);
690 	VHT_CAP_CHECK(VHT_CAP_TXSTBC);
691 	VHT_CAP_CHECK_MAX(VHT_CAP_RXSTBC_MASK);
692 	VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMER_CAPABLE);
693 	VHT_CAP_CHECK(VHT_CAP_SU_BEAMFORMEE_CAPABLE);
694 	VHT_CAP_CHECK_MAX(VHT_CAP_BEAMFORMEE_STS_MAX);
695 	VHT_CAP_CHECK_MAX(VHT_CAP_SOUNDING_DIMENSION_MAX);
696 	VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMER_CAPABLE);
697 	VHT_CAP_CHECK(VHT_CAP_MU_BEAMFORMEE_CAPABLE);
698 	VHT_CAP_CHECK(VHT_CAP_VHT_TXOP_PS);
699 	VHT_CAP_CHECK(VHT_CAP_HTC_VHT);
700 	VHT_CAP_CHECK_MAX(VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX);
701 	VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_UNSOL_MFB);
702 	VHT_CAP_CHECK(VHT_CAP_VHT_LINK_ADAPTATION_VHT_MRQ_MFB);
703 	VHT_CAP_CHECK(VHT_CAP_RX_ANTENNA_PATTERN);
704 	VHT_CAP_CHECK(VHT_CAP_TX_ANTENNA_PATTERN);
705 
706 #undef VHT_CAP_CHECK
707 #undef VHT_CAP_CHECK_MAX
708 
709 	return 1;
710 }
711 
712 #endif /* CONFIG_IEEE80211AC */
713 
714 
num_chan_to_bw(int num_chans)715 u32 num_chan_to_bw(int num_chans)
716 {
717 	switch (num_chans) {
718 	case 2:
719 	case 4:
720 	case 8:
721 		return num_chans * 20;
722 	default:
723 		return 20;
724 	}
725 }
726 
727 
728 /* check if BW is applicable for channel */
chan_bw_allowed(const struct hostapd_channel_data * chan,u32 bw,int ht40_plus,int pri)729 int chan_bw_allowed(const struct hostapd_channel_data *chan, u32 bw,
730 		    int ht40_plus, int pri)
731 {
732 	u32 bw_mask;
733 
734 	switch (bw) {
735 	case 20:
736 		bw_mask = HOSTAPD_CHAN_WIDTH_20;
737 		break;
738 	case 40:
739 		/* HT 40 MHz support declared only for primary channel,
740 		 * just skip 40 MHz secondary checking */
741 		if (pri && ht40_plus)
742 			bw_mask = HOSTAPD_CHAN_WIDTH_40P;
743 		else if (pri && !ht40_plus)
744 			bw_mask = HOSTAPD_CHAN_WIDTH_40M;
745 		else
746 			bw_mask = 0;
747 		break;
748 	case 80:
749 		bw_mask = HOSTAPD_CHAN_WIDTH_80;
750 		break;
751 	case 160:
752 		bw_mask = HOSTAPD_CHAN_WIDTH_160;
753 		break;
754 	default:
755 		bw_mask = 0;
756 		break;
757 	}
758 
759 	return (chan->allowed_bw & bw_mask) == bw_mask;
760 }
761 
762 
763 /* check if channel is allowed to be used as primary */
chan_pri_allowed(const struct hostapd_channel_data * chan)764 int chan_pri_allowed(const struct hostapd_channel_data *chan)
765 {
766 	return !(chan->flag & HOSTAPD_CHAN_DISABLED) &&
767 		(chan->allowed_bw & HOSTAPD_CHAN_WIDTH_20);
768 }
769