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