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