xref: /linux/net/wireless/chan.c (revision 77de28cd7cf172e782319a144bf64e693794d78b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * This file contains helper code to handle channel
4  * settings and keeping track of what is possible at
5  * any point in time.
6  *
7  * Copyright 2009	Johannes Berg <johannes@sipsolutions.net>
8  * Copyright 2013-2014  Intel Mobile Communications GmbH
9  * Copyright 2018-2025	Intel Corporation
10  */
11 
12 #include <linux/export.h>
13 #include <linux/bitfield.h>
14 #include <net/cfg80211.h>
15 #include "core.h"
16 #include "rdev-ops.h"
17 
18 static bool cfg80211_valid_60g_freq(u32 freq)
19 {
20 	return freq >= 58320 && freq <= 70200;
21 }
22 
23 void cfg80211_chandef_create(struct cfg80211_chan_def *chandef,
24 			     struct ieee80211_channel *chan,
25 			     enum nl80211_channel_type chan_type)
26 {
27 	if (WARN_ON(!chan))
28 		return;
29 
30 	*chandef = (struct cfg80211_chan_def) {
31 		.chan = chan,
32 		.freq1_offset = chan->freq_offset,
33 	};
34 
35 	switch (chan_type) {
36 	case NL80211_CHAN_NO_HT:
37 		chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
38 		chandef->center_freq1 = chan->center_freq;
39 		break;
40 	case NL80211_CHAN_HT20:
41 		chandef->width = NL80211_CHAN_WIDTH_20;
42 		chandef->center_freq1 = chan->center_freq;
43 		break;
44 	case NL80211_CHAN_HT40PLUS:
45 		chandef->width = NL80211_CHAN_WIDTH_40;
46 		chandef->center_freq1 = chan->center_freq + 10;
47 		break;
48 	case NL80211_CHAN_HT40MINUS:
49 		chandef->width = NL80211_CHAN_WIDTH_40;
50 		chandef->center_freq1 = chan->center_freq - 10;
51 		break;
52 	default:
53 		WARN_ON(1);
54 	}
55 }
56 EXPORT_SYMBOL(cfg80211_chandef_create);
57 
58 static u32 cfg80211_get_start_freq(const struct cfg80211_chan_def *chandef,
59 				   u32 cf)
60 {
61 	u32 start_freq, center_freq, bandwidth;
62 
63 	center_freq = MHZ_TO_KHZ((cf == 1) ?
64 			chandef->center_freq1 : chandef->center_freq2);
65 	bandwidth = MHZ_TO_KHZ(cfg80211_chandef_get_width(chandef));
66 
67 	if (bandwidth <= MHZ_TO_KHZ(20))
68 		start_freq = center_freq;
69 	else
70 		start_freq = center_freq - bandwidth / 2 + MHZ_TO_KHZ(10);
71 
72 	return start_freq;
73 }
74 
75 static u32 cfg80211_get_end_freq(const struct cfg80211_chan_def *chandef,
76 				 u32 cf)
77 {
78 	u32 end_freq, center_freq, bandwidth;
79 
80 	center_freq = MHZ_TO_KHZ((cf == 1) ?
81 			chandef->center_freq1 : chandef->center_freq2);
82 	bandwidth = MHZ_TO_KHZ(cfg80211_chandef_get_width(chandef));
83 
84 	if (bandwidth <= MHZ_TO_KHZ(20))
85 		end_freq = center_freq;
86 	else
87 		end_freq = center_freq + bandwidth / 2 - MHZ_TO_KHZ(10);
88 
89 	return end_freq;
90 }
91 
92 #define for_each_subchan(chandef, freq, cf)				\
93 	for (u32 punctured = chandef->punctured,			\
94 	     cf = 1, freq = cfg80211_get_start_freq(chandef, cf);	\
95 	     freq <= cfg80211_get_end_freq(chandef, cf);		\
96 	     freq += MHZ_TO_KHZ(20),					\
97 	     ((cf == 1 && chandef->center_freq2 != 0 &&			\
98 	       freq > cfg80211_get_end_freq(chandef, cf)) ?		\
99 	      (cf++, freq = cfg80211_get_start_freq(chandef, cf),	\
100 	       punctured = 0) : (punctured >>= 1)))			\
101 		if (!(punctured & 1))
102 
103 #define for_each_s1g_subchan(chandef, freq_khz)                   \
104 	for (freq_khz = cfg80211_s1g_get_start_freq_khz(chandef); \
105 	     freq_khz <= cfg80211_s1g_get_end_freq_khz(chandef);  \
106 	     freq_khz += MHZ_TO_KHZ(1))
107 
108 struct cfg80211_per_bw_puncturing_values {
109 	u8 len;
110 	const u16 *valid_values;
111 };
112 
113 static const u16 puncturing_values_80mhz[] = {
114 	0x8, 0x4, 0x2, 0x1
115 };
116 
117 static const u16 puncturing_values_160mhz[] = {
118 	 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1, 0xc0, 0x30, 0xc, 0x3
119 };
120 
121 static const u16 puncturing_values_320mhz[] = {
122 	0xc000, 0x3000, 0xc00, 0x300, 0xc0, 0x30, 0xc, 0x3, 0xf000, 0xf00,
123 	0xf0, 0xf, 0xfc00, 0xf300, 0xf0c0, 0xf030, 0xf00c, 0xf003, 0xc00f,
124 	0x300f, 0xc0f, 0x30f, 0xcf, 0x3f
125 };
126 
127 #define CFG80211_PER_BW_VALID_PUNCTURING_VALUES(_bw) \
128 	{ \
129 		.len = ARRAY_SIZE(puncturing_values_ ## _bw ## mhz), \
130 		.valid_values = puncturing_values_ ## _bw ## mhz \
131 	}
132 
133 static const struct cfg80211_per_bw_puncturing_values per_bw_puncturing[] = {
134 	CFG80211_PER_BW_VALID_PUNCTURING_VALUES(80),
135 	CFG80211_PER_BW_VALID_PUNCTURING_VALUES(160),
136 	CFG80211_PER_BW_VALID_PUNCTURING_VALUES(320)
137 };
138 
139 static bool valid_puncturing_bitmap(const struct cfg80211_chan_def *chandef)
140 {
141 	u32 idx, i, start_freq, primary_center = chandef->chan->center_freq;
142 
143 	switch (chandef->width) {
144 	case NL80211_CHAN_WIDTH_80:
145 		idx = 0;
146 		start_freq = chandef->center_freq1 - 40;
147 		break;
148 	case NL80211_CHAN_WIDTH_160:
149 		idx = 1;
150 		start_freq = chandef->center_freq1 - 80;
151 		break;
152 	case NL80211_CHAN_WIDTH_320:
153 		idx = 2;
154 		start_freq = chandef->center_freq1 - 160;
155 		break;
156 	default:
157 		return chandef->punctured == 0;
158 	}
159 
160 	if (!chandef->punctured)
161 		return true;
162 
163 	/* check if primary channel is punctured */
164 	if (chandef->punctured & (u16)BIT((primary_center - start_freq) / 20))
165 		return false;
166 
167 	for (i = 0; i < per_bw_puncturing[idx].len; i++) {
168 		if (per_bw_puncturing[idx].valid_values[i] == chandef->punctured)
169 			return true;
170 	}
171 
172 	return false;
173 }
174 
175 static bool cfg80211_edmg_chandef_valid(const struct cfg80211_chan_def *chandef)
176 {
177 	int max_contiguous = 0;
178 	int num_of_enabled = 0;
179 	int contiguous = 0;
180 	int i;
181 
182 	if (!chandef->edmg.channels || !chandef->edmg.bw_config)
183 		return false;
184 
185 	if (!cfg80211_valid_60g_freq(chandef->chan->center_freq))
186 		return false;
187 
188 	for (i = 0; i < 6; i++) {
189 		if (chandef->edmg.channels & BIT(i)) {
190 			contiguous++;
191 			num_of_enabled++;
192 		} else {
193 			contiguous = 0;
194 		}
195 
196 		max_contiguous = max(contiguous, max_contiguous);
197 	}
198 	/* basic verification of edmg configuration according to
199 	 * IEEE P802.11ay/D4.0 section 9.4.2.251
200 	 */
201 	/* check bw_config against contiguous edmg channels */
202 	switch (chandef->edmg.bw_config) {
203 	case IEEE80211_EDMG_BW_CONFIG_4:
204 	case IEEE80211_EDMG_BW_CONFIG_8:
205 	case IEEE80211_EDMG_BW_CONFIG_12:
206 		if (max_contiguous < 1)
207 			return false;
208 		break;
209 	case IEEE80211_EDMG_BW_CONFIG_5:
210 	case IEEE80211_EDMG_BW_CONFIG_9:
211 	case IEEE80211_EDMG_BW_CONFIG_13:
212 		if (max_contiguous < 2)
213 			return false;
214 		break;
215 	case IEEE80211_EDMG_BW_CONFIG_6:
216 	case IEEE80211_EDMG_BW_CONFIG_10:
217 	case IEEE80211_EDMG_BW_CONFIG_14:
218 		if (max_contiguous < 3)
219 			return false;
220 		break;
221 	case IEEE80211_EDMG_BW_CONFIG_7:
222 	case IEEE80211_EDMG_BW_CONFIG_11:
223 	case IEEE80211_EDMG_BW_CONFIG_15:
224 		if (max_contiguous < 4)
225 			return false;
226 		break;
227 
228 	default:
229 		return false;
230 	}
231 
232 	/* check bw_config against aggregated (non contiguous) edmg channels */
233 	switch (chandef->edmg.bw_config) {
234 	case IEEE80211_EDMG_BW_CONFIG_4:
235 	case IEEE80211_EDMG_BW_CONFIG_5:
236 	case IEEE80211_EDMG_BW_CONFIG_6:
237 	case IEEE80211_EDMG_BW_CONFIG_7:
238 		break;
239 	case IEEE80211_EDMG_BW_CONFIG_8:
240 	case IEEE80211_EDMG_BW_CONFIG_9:
241 	case IEEE80211_EDMG_BW_CONFIG_10:
242 	case IEEE80211_EDMG_BW_CONFIG_11:
243 		if (num_of_enabled < 2)
244 			return false;
245 		break;
246 	case IEEE80211_EDMG_BW_CONFIG_12:
247 	case IEEE80211_EDMG_BW_CONFIG_13:
248 	case IEEE80211_EDMG_BW_CONFIG_14:
249 	case IEEE80211_EDMG_BW_CONFIG_15:
250 		if (num_of_enabled < 4 || max_contiguous < 2)
251 			return false;
252 		break;
253 	default:
254 		return false;
255 	}
256 
257 	return true;
258 }
259 
260 int nl80211_chan_width_to_mhz(enum nl80211_chan_width chan_width)
261 {
262 	int mhz;
263 
264 	switch (chan_width) {
265 	case NL80211_CHAN_WIDTH_1:
266 		mhz = 1;
267 		break;
268 	case NL80211_CHAN_WIDTH_2:
269 		mhz = 2;
270 		break;
271 	case NL80211_CHAN_WIDTH_4:
272 		mhz = 4;
273 		break;
274 	case NL80211_CHAN_WIDTH_8:
275 		mhz = 8;
276 		break;
277 	case NL80211_CHAN_WIDTH_16:
278 		mhz = 16;
279 		break;
280 	case NL80211_CHAN_WIDTH_5:
281 		mhz = 5;
282 		break;
283 	case NL80211_CHAN_WIDTH_10:
284 		mhz = 10;
285 		break;
286 	case NL80211_CHAN_WIDTH_20:
287 	case NL80211_CHAN_WIDTH_20_NOHT:
288 		mhz = 20;
289 		break;
290 	case NL80211_CHAN_WIDTH_40:
291 		mhz = 40;
292 		break;
293 	case NL80211_CHAN_WIDTH_80P80:
294 	case NL80211_CHAN_WIDTH_80:
295 		mhz = 80;
296 		break;
297 	case NL80211_CHAN_WIDTH_160:
298 		mhz = 160;
299 		break;
300 	case NL80211_CHAN_WIDTH_320:
301 		mhz = 320;
302 		break;
303 	default:
304 		WARN_ON_ONCE(1);
305 		return -1;
306 	}
307 	return mhz;
308 }
309 EXPORT_SYMBOL(nl80211_chan_width_to_mhz);
310 
311 static bool cfg80211_valid_center_freq(u32 center,
312 				       enum nl80211_chan_width width)
313 {
314 	int bw;
315 	int step;
316 
317 	/* We only do strict verification on 6 GHz */
318 	if (center < 5955 || center > 7115)
319 		return true;
320 
321 	bw = nl80211_chan_width_to_mhz(width);
322 	if (bw < 0)
323 		return false;
324 
325 	/* Validate that the channels bw is entirely within the 6 GHz band */
326 	if (center - bw / 2 < 5945 || center + bw / 2 > 7125)
327 		return false;
328 
329 	/* With 320 MHz the permitted channels overlap */
330 	if (bw == 320)
331 		step = 160;
332 	else
333 		step = bw;
334 
335 	/*
336 	 * Valid channels are packed from lowest frequency towards higher ones.
337 	 * So test that the lower frequency aligns with one of these steps.
338 	 */
339 	return (center - bw / 2 - 5945) % step == 0;
340 }
341 
342 bool cfg80211_chandef_valid(const struct cfg80211_chan_def *chandef)
343 {
344 	u32 control_freq, control_freq_khz, start_khz, end_khz;
345 
346 	if (!chandef->chan)
347 		return false;
348 
349 	if (chandef->freq1_offset >= 1000)
350 		return false;
351 
352 	control_freq = chandef->chan->center_freq;
353 
354 	switch (chandef->width) {
355 	case NL80211_CHAN_WIDTH_5:
356 	case NL80211_CHAN_WIDTH_10:
357 	case NL80211_CHAN_WIDTH_20:
358 	case NL80211_CHAN_WIDTH_20_NOHT:
359 		if (ieee80211_chandef_to_khz(chandef) !=
360 		    ieee80211_channel_to_khz(chandef->chan))
361 			return false;
362 		if (chandef->center_freq2)
363 			return false;
364 		break;
365 	case NL80211_CHAN_WIDTH_1:
366 	case NL80211_CHAN_WIDTH_2:
367 	case NL80211_CHAN_WIDTH_4:
368 	case NL80211_CHAN_WIDTH_8:
369 	case NL80211_CHAN_WIDTH_16:
370 		if (!cfg80211_chandef_is_s1g(chandef))
371 			return false;
372 		if (chandef->center_freq2)
373 			return false;
374 
375 		control_freq_khz = ieee80211_channel_to_khz(chandef->chan);
376 		start_khz = cfg80211_s1g_get_start_freq_khz(chandef);
377 		end_khz = cfg80211_s1g_get_end_freq_khz(chandef);
378 
379 		if (control_freq_khz < start_khz || control_freq_khz > end_khz)
380 			return false;
381 		break;
382 	case NL80211_CHAN_WIDTH_80P80:
383 		if (!chandef->center_freq2)
384 			return false;
385 		/* adjacent is not allowed -- that's a 160 MHz channel */
386 		if (chandef->center_freq1 - chandef->center_freq2 == 80 ||
387 		    chandef->center_freq2 - chandef->center_freq1 == 80)
388 			return false;
389 		break;
390 	default:
391 		if (chandef->center_freq2)
392 			return false;
393 		break;
394 	}
395 
396 	switch (chandef->width) {
397 	case NL80211_CHAN_WIDTH_5:
398 	case NL80211_CHAN_WIDTH_10:
399 	case NL80211_CHAN_WIDTH_20:
400 	case NL80211_CHAN_WIDTH_20_NOHT:
401 	case NL80211_CHAN_WIDTH_1:
402 	case NL80211_CHAN_WIDTH_2:
403 	case NL80211_CHAN_WIDTH_4:
404 	case NL80211_CHAN_WIDTH_8:
405 	case NL80211_CHAN_WIDTH_16:
406 		/* all checked above */
407 		break;
408 	case NL80211_CHAN_WIDTH_320:
409 		if (chandef->center_freq1 == control_freq + 150 ||
410 		    chandef->center_freq1 == control_freq + 130 ||
411 		    chandef->center_freq1 == control_freq + 110 ||
412 		    chandef->center_freq1 == control_freq + 90 ||
413 		    chandef->center_freq1 == control_freq - 90 ||
414 		    chandef->center_freq1 == control_freq - 110 ||
415 		    chandef->center_freq1 == control_freq - 130 ||
416 		    chandef->center_freq1 == control_freq - 150)
417 			break;
418 		fallthrough;
419 	case NL80211_CHAN_WIDTH_160:
420 		if (chandef->center_freq1 == control_freq + 70 ||
421 		    chandef->center_freq1 == control_freq + 50 ||
422 		    chandef->center_freq1 == control_freq - 50 ||
423 		    chandef->center_freq1 == control_freq - 70)
424 			break;
425 		fallthrough;
426 	case NL80211_CHAN_WIDTH_80P80:
427 	case NL80211_CHAN_WIDTH_80:
428 		if (chandef->center_freq1 == control_freq + 30 ||
429 		    chandef->center_freq1 == control_freq - 30)
430 			break;
431 		fallthrough;
432 	case NL80211_CHAN_WIDTH_40:
433 		if (chandef->center_freq1 == control_freq + 10 ||
434 		    chandef->center_freq1 == control_freq - 10)
435 			break;
436 		fallthrough;
437 	default:
438 		return false;
439 	}
440 
441 	if (!cfg80211_valid_center_freq(chandef->center_freq1, chandef->width))
442 		return false;
443 
444 	if (chandef->width == NL80211_CHAN_WIDTH_80P80 &&
445 	    !cfg80211_valid_center_freq(chandef->center_freq2, chandef->width))
446 		return false;
447 
448 	/* channel 14 is only for IEEE 802.11b */
449 	if (chandef->center_freq1 == 2484 &&
450 	    chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
451 		return false;
452 
453 	if (cfg80211_chandef_is_edmg(chandef) &&
454 	    !cfg80211_edmg_chandef_valid(chandef))
455 		return false;
456 
457 	if (!cfg80211_chandef_is_s1g(chandef) && chandef->s1g_primary_2mhz)
458 		return false;
459 
460 	return valid_puncturing_bitmap(chandef);
461 }
462 EXPORT_SYMBOL(cfg80211_chandef_valid);
463 
464 int cfg80211_chandef_primary(const struct cfg80211_chan_def *c,
465 			     enum nl80211_chan_width primary_chan_width,
466 			     u16 *punctured)
467 {
468 	int pri_width = nl80211_chan_width_to_mhz(primary_chan_width);
469 	int width = cfg80211_chandef_get_width(c);
470 	u32 control = c->chan->center_freq;
471 	u32 center = c->center_freq1;
472 	u16 _punct = 0;
473 
474 	if (WARN_ON_ONCE(pri_width < 0 || width < 0))
475 		return -1;
476 
477 	/* not intended to be called this way, can't determine */
478 	if (WARN_ON_ONCE(pri_width > width))
479 		return -1;
480 
481 	if (!punctured)
482 		punctured = &_punct;
483 
484 	*punctured = c->punctured;
485 
486 	while (width > pri_width) {
487 		unsigned int bits_to_drop = width / 20 / 2;
488 
489 		if (control > center) {
490 			center += width / 4;
491 			*punctured >>= bits_to_drop;
492 		} else {
493 			center -= width / 4;
494 			*punctured &= (1 << bits_to_drop) - 1;
495 		}
496 		width /= 2;
497 	}
498 
499 	return center;
500 }
501 EXPORT_SYMBOL(cfg80211_chandef_primary);
502 
503 static const struct cfg80211_chan_def *
504 check_chandef_primary_compat(const struct cfg80211_chan_def *c1,
505 			     const struct cfg80211_chan_def *c2,
506 			     enum nl80211_chan_width primary_chan_width)
507 {
508 	u16 punct_c1 = 0, punct_c2 = 0;
509 
510 	/* check primary is compatible -> error if not */
511 	if (cfg80211_chandef_primary(c1, primary_chan_width, &punct_c1) !=
512 	    cfg80211_chandef_primary(c2, primary_chan_width, &punct_c2))
513 		return ERR_PTR(-EINVAL);
514 
515 	if (punct_c1 != punct_c2)
516 		return ERR_PTR(-EINVAL);
517 
518 	/* assumes c1 is smaller width, if that was just checked -> done */
519 	if (c1->width == primary_chan_width)
520 		return c2;
521 
522 	/* otherwise continue checking the next width */
523 	return NULL;
524 }
525 
526 static const struct cfg80211_chan_def *
527 _cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1,
528 			     const struct cfg80211_chan_def *c2)
529 {
530 	const struct cfg80211_chan_def *ret;
531 
532 	/* If they are identical, return */
533 	if (cfg80211_chandef_identical(c1, c2))
534 		return c2;
535 
536 	/* otherwise, must have same control channel */
537 	if (c1->chan != c2->chan)
538 		return NULL;
539 
540 	/*
541 	 * If they have the same width, but aren't identical,
542 	 * then they can't be compatible.
543 	 */
544 	if (c1->width == c2->width)
545 		return NULL;
546 
547 	/*
548 	 * can't be compatible if one of them is 5/10 MHz or S1G
549 	 * but they don't have the same width.
550 	 */
551 #define NARROW_OR_S1G(width)	((width) == NL80211_CHAN_WIDTH_5 || \
552 				 (width) == NL80211_CHAN_WIDTH_10 || \
553 				 (width) == NL80211_CHAN_WIDTH_1 || \
554 				 (width) == NL80211_CHAN_WIDTH_2 || \
555 				 (width) == NL80211_CHAN_WIDTH_4 || \
556 				 (width) == NL80211_CHAN_WIDTH_8 || \
557 				 (width) == NL80211_CHAN_WIDTH_16)
558 
559 	if (NARROW_OR_S1G(c1->width) || NARROW_OR_S1G(c2->width))
560 		return NULL;
561 
562 	/*
563 	 * Make sure that c1 is always the narrower one, so that later
564 	 * we either return NULL or c2 and don't have to check both
565 	 * directions.
566 	 */
567 	if (c1->width > c2->width)
568 		swap(c1, c2);
569 
570 	/*
571 	 * No further checks needed if the "narrower" one is only 20 MHz.
572 	 * Here "narrower" includes being a 20 MHz non-HT channel vs. a
573 	 * 20 MHz HT (or later) one.
574 	 */
575 	if (c1->width <= NL80211_CHAN_WIDTH_20)
576 		return c2;
577 
578 	ret = check_chandef_primary_compat(c1, c2, NL80211_CHAN_WIDTH_40);
579 	if (ret)
580 		return ret;
581 
582 	ret = check_chandef_primary_compat(c1, c2, NL80211_CHAN_WIDTH_80);
583 	if (ret)
584 		return ret;
585 
586 	/*
587 	 * If c1 is 80+80, then c2 is 160 or higher, but that cannot
588 	 * match. If c2 was also 80+80 it was already either accepted
589 	 * or rejected above (identical or not, respectively.)
590 	 */
591 	if (c1->width == NL80211_CHAN_WIDTH_80P80)
592 		return NULL;
593 
594 	ret = check_chandef_primary_compat(c1, c2, NL80211_CHAN_WIDTH_160);
595 	if (ret)
596 		return ret;
597 
598 	/*
599 	 * Getting here would mean they're both wider than 160, have the
600 	 * same primary 160, but are not identical - this cannot happen
601 	 * since they must be 320 (no wider chandefs exist, at least yet.)
602 	 */
603 	WARN_ON_ONCE(1);
604 
605 	return NULL;
606 }
607 
608 const struct cfg80211_chan_def *
609 cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1,
610 			    const struct cfg80211_chan_def *c2)
611 {
612 	const struct cfg80211_chan_def *ret;
613 
614 	ret = _cfg80211_chandef_compatible(c1, c2);
615 	if (IS_ERR(ret))
616 		return NULL;
617 	return ret;
618 }
619 EXPORT_SYMBOL(cfg80211_chandef_compatible);
620 
621 void cfg80211_set_dfs_state(struct wiphy *wiphy,
622 			    const struct cfg80211_chan_def *chandef,
623 			    enum nl80211_dfs_state dfs_state)
624 {
625 	struct ieee80211_channel *c;
626 	int width;
627 
628 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
629 		return;
630 
631 	width = cfg80211_chandef_get_width(chandef);
632 	if (width < 0)
633 		return;
634 
635 	for_each_subchan(chandef, freq, cf) {
636 		c = ieee80211_get_channel_khz(wiphy, freq);
637 		if (!c || !(c->flags & IEEE80211_CHAN_RADAR))
638 			continue;
639 
640 		c->dfs_state = dfs_state;
641 		c->dfs_state_entered = jiffies;
642 	}
643 }
644 
645 void cfg80211_set_cac_state(struct wiphy *wiphy,
646 			    const struct cfg80211_chan_def *chandef,
647 			    bool cac_ongoing)
648 {
649 	struct ieee80211_channel *c;
650 	int width;
651 	u64 cac_time;
652 
653 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
654 		return;
655 
656 	width = cfg80211_chandef_get_width(chandef);
657 	if (width < 0)
658 		return;
659 
660 	/* Get the same timestamp for all subchannels */
661 	cac_time = cac_ongoing ? ktime_get_boottime_ns() : 0;
662 
663 	for_each_subchan(chandef, freq, cf) {
664 		c = ieee80211_get_channel_khz(wiphy, freq);
665 		if (!c)
666 			continue;
667 
668 		c->cac_start_time = cac_time;
669 	}
670 }
671 
672 static bool
673 cfg80211_dfs_permissive_check_wdev(struct cfg80211_registered_device *rdev,
674 				   enum nl80211_iftype iftype,
675 				   struct wireless_dev *wdev,
676 				   struct ieee80211_channel *chan)
677 {
678 	unsigned int link_id;
679 
680 	for_each_valid_link(wdev, link_id) {
681 		struct ieee80211_channel *other_chan = NULL;
682 		struct cfg80211_chan_def chandef = {};
683 		int ret;
684 
685 		/* In order to avoid daisy chaining only allow BSS STA */
686 		if (wdev->iftype != NL80211_IFTYPE_STATION ||
687 		    !wdev->links[link_id].client.current_bss)
688 			continue;
689 
690 		other_chan =
691 			wdev->links[link_id].client.current_bss->pub.channel;
692 
693 		if (!other_chan)
694 			continue;
695 
696 		if (chan == other_chan)
697 			return true;
698 
699 		/* continue if we can't get the channel */
700 		ret = rdev_get_channel(rdev, wdev, link_id, &chandef);
701 		if (ret)
702 			continue;
703 
704 		if (cfg80211_is_sub_chan(&chandef, chan, false))
705 			return true;
706 	}
707 
708 	return false;
709 }
710 
711 /*
712  * Check if P2P GO is allowed to operate on a DFS channel
713  */
714 static bool cfg80211_dfs_permissive_chan(struct wiphy *wiphy,
715 					 enum nl80211_iftype iftype,
716 					 struct ieee80211_channel *chan)
717 {
718 	struct wireless_dev *wdev;
719 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
720 
721 	lockdep_assert_held(&rdev->wiphy.mtx);
722 
723 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
724 				     NL80211_EXT_FEATURE_DFS_CONCURRENT) ||
725 	    !(chan->flags & IEEE80211_CHAN_DFS_CONCURRENT))
726 		return false;
727 
728 	/* only valid for P2P GO */
729 	if (iftype != NL80211_IFTYPE_P2P_GO)
730 		return false;
731 
732 	/*
733 	 * Allow only if there's a concurrent BSS
734 	 */
735 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
736 		bool ret = cfg80211_dfs_permissive_check_wdev(rdev, iftype,
737 							      wdev, chan);
738 		if (ret)
739 			return ret;
740 	}
741 
742 	return false;
743 }
744 
745 static int cfg80211_get_chans_dfs_required(struct wiphy *wiphy,
746 					   const struct cfg80211_chan_def *chandef,
747 					   enum nl80211_iftype iftype)
748 {
749 	struct ieee80211_channel *c;
750 
751 	/* DFS is not required for S1G */
752 	if (cfg80211_chandef_is_s1g(chandef))
753 		return 0;
754 
755 	for_each_subchan(chandef, freq, cf) {
756 		c = ieee80211_get_channel_khz(wiphy, freq);
757 		if (!c)
758 			return -EINVAL;
759 
760 		if (c->flags & IEEE80211_CHAN_RADAR &&
761 		    !cfg80211_dfs_permissive_chan(wiphy, iftype, c))
762 			return 1;
763 	}
764 
765 	return 0;
766 }
767 
768 
769 int cfg80211_chandef_dfs_required(struct wiphy *wiphy,
770 				  const struct cfg80211_chan_def *chandef,
771 				  enum nl80211_iftype iftype)
772 {
773 	int width;
774 	int ret;
775 
776 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
777 		return -EINVAL;
778 
779 	switch (iftype) {
780 	case NL80211_IFTYPE_ADHOC:
781 	case NL80211_IFTYPE_AP:
782 	case NL80211_IFTYPE_P2P_GO:
783 	case NL80211_IFTYPE_MESH_POINT:
784 	case NL80211_IFTYPE_NAN:
785 		width = cfg80211_chandef_get_width(chandef);
786 		if (width < 0)
787 			return -EINVAL;
788 
789 		ret = cfg80211_get_chans_dfs_required(wiphy, chandef, iftype);
790 
791 		return (ret > 0) ? BIT(chandef->width) : ret;
792 		break;
793 	case NL80211_IFTYPE_STATION:
794 	case NL80211_IFTYPE_OCB:
795 	case NL80211_IFTYPE_P2P_CLIENT:
796 	case NL80211_IFTYPE_MONITOR:
797 	case NL80211_IFTYPE_AP_VLAN:
798 	case NL80211_IFTYPE_P2P_DEVICE:
799 		break;
800 	case NL80211_IFTYPE_WDS:
801 	case NL80211_IFTYPE_UNSPECIFIED:
802 	case NUM_NL80211_IFTYPES:
803 		WARN_ON(1);
804 	}
805 
806 	return 0;
807 }
808 EXPORT_SYMBOL(cfg80211_chandef_dfs_required);
809 
810 bool cfg80211_chandef_dfs_usable(struct wiphy *wiphy,
811 				 const struct cfg80211_chan_def *chandef)
812 {
813 	struct ieee80211_channel *c;
814 	int width, count = 0;
815 
816 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
817 		return false;
818 
819 	width = cfg80211_chandef_get_width(chandef);
820 	if (width < 0)
821 		return false;
822 
823 	/*
824 	 * Check entire range of channels for the bandwidth.
825 	 * Check all channels are DFS channels (DFS_USABLE or
826 	 * DFS_AVAILABLE). Return number of usable channels
827 	 * (require CAC). Allow DFS and non-DFS channel mix.
828 	 */
829 	for_each_subchan(chandef, freq, cf) {
830 		c = ieee80211_get_channel_khz(wiphy, freq);
831 		if (!c)
832 			return false;
833 
834 		if (c->flags & IEEE80211_CHAN_DISABLED)
835 			return false;
836 
837 		if (c->flags & IEEE80211_CHAN_RADAR) {
838 			if (c->dfs_state == NL80211_DFS_UNAVAILABLE)
839 				return false;
840 
841 			if (c->dfs_state == NL80211_DFS_USABLE)
842 				count++;
843 		}
844 	}
845 
846 	return count > 0;
847 }
848 EXPORT_SYMBOL(cfg80211_chandef_dfs_usable);
849 
850 /*
851  * Checks if center frequency of chan falls with in the bandwidth
852  * range of chandef.
853  */
854 bool cfg80211_is_sub_chan(struct cfg80211_chan_def *chandef,
855 			  struct ieee80211_channel *chan,
856 			  bool primary_only)
857 {
858 	int width;
859 	u32 freq;
860 
861 	if (!chandef->chan)
862 		return false;
863 
864 	if (chandef->chan->center_freq == chan->center_freq)
865 		return true;
866 
867 	if (primary_only)
868 		return false;
869 
870 	width = cfg80211_chandef_get_width(chandef);
871 	if (width <= 20)
872 		return false;
873 
874 	for (freq = chandef->center_freq1 - width / 2 + 10;
875 	     freq <= chandef->center_freq1 + width / 2 - 10; freq += 20) {
876 		if (chan->center_freq == freq)
877 			return true;
878 	}
879 
880 	if (!chandef->center_freq2)
881 		return false;
882 
883 	for (freq = chandef->center_freq2 - width / 2 + 10;
884 	     freq <= chandef->center_freq2 + width / 2 - 10; freq += 20) {
885 		if (chan->center_freq == freq)
886 			return true;
887 	}
888 
889 	return false;
890 }
891 
892 bool cfg80211_beaconing_iface_active(struct wireless_dev *wdev)
893 {
894 	unsigned int link;
895 
896 	lockdep_assert_wiphy(wdev->wiphy);
897 
898 	switch (wdev->iftype) {
899 	case NL80211_IFTYPE_AP:
900 	case NL80211_IFTYPE_P2P_GO:
901 		for_each_valid_link(wdev, link) {
902 			if (wdev->links[link].ap.beacon_interval)
903 				return true;
904 		}
905 		break;
906 	case NL80211_IFTYPE_ADHOC:
907 		if (wdev->u.ibss.ssid_len)
908 			return true;
909 		break;
910 	case NL80211_IFTYPE_MESH_POINT:
911 		if (wdev->u.mesh.id_len)
912 			return true;
913 		break;
914 	case NL80211_IFTYPE_STATION:
915 	case NL80211_IFTYPE_OCB:
916 	case NL80211_IFTYPE_P2P_CLIENT:
917 	case NL80211_IFTYPE_MONITOR:
918 	case NL80211_IFTYPE_AP_VLAN:
919 	case NL80211_IFTYPE_P2P_DEVICE:
920 	/* Can NAN type be considered as beaconing interface? */
921 	case NL80211_IFTYPE_NAN:
922 		break;
923 	case NL80211_IFTYPE_UNSPECIFIED:
924 	case NL80211_IFTYPE_WDS:
925 	case NUM_NL80211_IFTYPES:
926 		WARN_ON(1);
927 	}
928 
929 	return false;
930 }
931 
932 bool cfg80211_wdev_on_sub_chan(struct wireless_dev *wdev,
933 			       struct ieee80211_channel *chan,
934 			       bool primary_only)
935 {
936 	unsigned int link;
937 
938 	switch (wdev->iftype) {
939 	case NL80211_IFTYPE_AP:
940 	case NL80211_IFTYPE_P2P_GO:
941 		for_each_valid_link(wdev, link) {
942 			if (cfg80211_is_sub_chan(&wdev->links[link].ap.chandef,
943 						 chan, primary_only))
944 				return true;
945 		}
946 		break;
947 	case NL80211_IFTYPE_ADHOC:
948 		return cfg80211_is_sub_chan(&wdev->u.ibss.chandef, chan,
949 					    primary_only);
950 	case NL80211_IFTYPE_MESH_POINT:
951 		return cfg80211_is_sub_chan(&wdev->u.mesh.chandef, chan,
952 					    primary_only);
953 	default:
954 		break;
955 	}
956 
957 	return false;
958 }
959 
960 static bool cfg80211_is_wiphy_oper_chan(struct wiphy *wiphy,
961 					struct ieee80211_channel *chan)
962 {
963 	struct wireless_dev *wdev;
964 
965 	lockdep_assert_wiphy(wiphy);
966 
967 	list_for_each_entry(wdev, &wiphy->wdev_list, list) {
968 		if (!cfg80211_beaconing_iface_active(wdev))
969 			continue;
970 
971 		if (cfg80211_wdev_on_sub_chan(wdev, chan, false))
972 			return true;
973 	}
974 
975 	return false;
976 }
977 
978 static bool
979 cfg80211_offchan_chain_is_active(struct cfg80211_registered_device *rdev,
980 				 struct ieee80211_channel *channel)
981 {
982 	if (!rdev->background_radar_wdev)
983 		return false;
984 
985 	if (!cfg80211_chandef_valid(&rdev->background_radar_chandef))
986 		return false;
987 
988 	return cfg80211_is_sub_chan(&rdev->background_radar_chandef, channel,
989 				    false);
990 }
991 
992 bool cfg80211_any_wiphy_oper_chan(struct wiphy *wiphy,
993 				  struct ieee80211_channel *chan)
994 {
995 	struct cfg80211_registered_device *rdev;
996 
997 	ASSERT_RTNL();
998 
999 	if (!(chan->flags & IEEE80211_CHAN_RADAR))
1000 		return false;
1001 
1002 	for_each_rdev(rdev) {
1003 		bool found;
1004 
1005 		if (!reg_dfs_domain_same(wiphy, &rdev->wiphy))
1006 			continue;
1007 
1008 		guard(wiphy)(&rdev->wiphy);
1009 
1010 		found = cfg80211_is_wiphy_oper_chan(&rdev->wiphy, chan) ||
1011 			cfg80211_offchan_chain_is_active(rdev, chan);
1012 
1013 		if (found)
1014 			return true;
1015 	}
1016 
1017 	return false;
1018 }
1019 
1020 static bool cfg80211_chandef_dfs_available(struct wiphy *wiphy,
1021 				const struct cfg80211_chan_def *chandef)
1022 {
1023 	struct ieee80211_channel *c;
1024 	int width;
1025 	bool dfs_offload;
1026 
1027 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
1028 		return false;
1029 
1030 	width = cfg80211_chandef_get_width(chandef);
1031 	if (width < 0)
1032 		return false;
1033 
1034 	dfs_offload = wiphy_ext_feature_isset(wiphy,
1035 					      NL80211_EXT_FEATURE_DFS_OFFLOAD);
1036 
1037 	/*
1038 	 * Check entire range of channels for the bandwidth.
1039 	 * If any channel in between is disabled or has not
1040 	 * had gone through CAC return false
1041 	 */
1042 	for_each_subchan(chandef, freq, cf) {
1043 		c = ieee80211_get_channel_khz(wiphy, freq);
1044 		if (!c)
1045 			return false;
1046 
1047 		if (c->flags & IEEE80211_CHAN_DISABLED)
1048 			return false;
1049 
1050 		if ((c->flags & IEEE80211_CHAN_RADAR) &&
1051 		    (c->dfs_state != NL80211_DFS_AVAILABLE) &&
1052 		    !(c->dfs_state == NL80211_DFS_USABLE && dfs_offload))
1053 			return false;
1054 	}
1055 
1056 	return true;
1057 }
1058 
1059 unsigned int
1060 cfg80211_chandef_dfs_cac_time(struct wiphy *wiphy,
1061 			      const struct cfg80211_chan_def *chandef)
1062 {
1063 	struct ieee80211_channel *c;
1064 	int width;
1065 	unsigned int t1 = 0, t2 = 0;
1066 
1067 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
1068 		return 0;
1069 
1070 	width = cfg80211_chandef_get_width(chandef);
1071 	if (width < 0)
1072 		return 0;
1073 
1074 	for_each_subchan(chandef, freq, cf) {
1075 		c = ieee80211_get_channel_khz(wiphy, freq);
1076 		if (!c || (c->flags & IEEE80211_CHAN_DISABLED)) {
1077 			if (cf == 1)
1078 				t1 = INT_MAX;
1079 			else
1080 				t2 = INT_MAX;
1081 			continue;
1082 		}
1083 
1084 		if (!(c->flags & IEEE80211_CHAN_RADAR))
1085 			continue;
1086 
1087 		if (cf == 1 && c->dfs_cac_ms > t1)
1088 			t1 = c->dfs_cac_ms;
1089 
1090 		if (cf == 2 && c->dfs_cac_ms > t2)
1091 			t2 = c->dfs_cac_ms;
1092 	}
1093 
1094 	if (t1 == INT_MAX && t2 == INT_MAX)
1095 		return 0;
1096 
1097 	if (t1 == INT_MAX)
1098 		return t2;
1099 
1100 	if (t2 == INT_MAX)
1101 		return t1;
1102 
1103 	return max(t1, t2);
1104 }
1105 EXPORT_SYMBOL(cfg80211_chandef_dfs_cac_time);
1106 
1107 /* check if the operating channels are valid and supported */
1108 static bool cfg80211_edmg_usable(struct wiphy *wiphy, u8 edmg_channels,
1109 				 enum ieee80211_edmg_bw_config edmg_bw_config,
1110 				 int primary_channel,
1111 				 struct ieee80211_edmg *edmg_cap)
1112 {
1113 	struct ieee80211_channel *chan;
1114 	int i, freq;
1115 	int channels_counter = 0;
1116 
1117 	if (!edmg_channels && !edmg_bw_config)
1118 		return true;
1119 
1120 	if ((!edmg_channels && edmg_bw_config) ||
1121 	    (edmg_channels && !edmg_bw_config))
1122 		return false;
1123 
1124 	if (!(edmg_channels & BIT(primary_channel - 1)))
1125 		return false;
1126 
1127 	/* 60GHz channels 1..6 */
1128 	for (i = 0; i < 6; i++) {
1129 		if (!(edmg_channels & BIT(i)))
1130 			continue;
1131 
1132 		if (!(edmg_cap->channels & BIT(i)))
1133 			return false;
1134 
1135 		channels_counter++;
1136 
1137 		freq = ieee80211_channel_to_frequency(i + 1,
1138 						      NL80211_BAND_60GHZ);
1139 		chan = ieee80211_get_channel(wiphy, freq);
1140 		if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1141 			return false;
1142 	}
1143 
1144 	/* IEEE802.11 allows max 4 channels */
1145 	if (channels_counter > 4)
1146 		return false;
1147 
1148 	/* check bw_config is a subset of what driver supports
1149 	 * (see IEEE P802.11ay/D4.0 section 9.4.2.251, Table 13)
1150 	 */
1151 	if ((edmg_bw_config % 4) > (edmg_cap->bw_config % 4))
1152 		return false;
1153 
1154 	if (edmg_bw_config > edmg_cap->bw_config)
1155 		return false;
1156 
1157 	return true;
1158 }
1159 
1160 static bool cfg80211_s1g_usable(struct wiphy *wiphy,
1161 				const struct cfg80211_chan_def *chandef)
1162 {
1163 	u32 freq_khz;
1164 	const struct ieee80211_channel *chan;
1165 	u32 pri_khz = ieee80211_channel_to_khz(chandef->chan);
1166 	u32 end_khz = cfg80211_s1g_get_end_freq_khz(chandef);
1167 	u32 start_khz = cfg80211_s1g_get_start_freq_khz(chandef);
1168 	int width_mhz = cfg80211_chandef_get_width(chandef);
1169 	u32 prohibited_flags = IEEE80211_CHAN_DISABLED;
1170 
1171 	if (width_mhz >= 16)
1172 		prohibited_flags |= IEEE80211_CHAN_NO_16MHZ;
1173 	if (width_mhz >= 8)
1174 		prohibited_flags |= IEEE80211_CHAN_NO_8MHZ;
1175 	if (width_mhz >= 4)
1176 		prohibited_flags |= IEEE80211_CHAN_NO_4MHZ;
1177 
1178 	if (chandef->chan->flags & IEEE80211_CHAN_S1G_NO_PRIMARY)
1179 		return false;
1180 
1181 	if (pri_khz < start_khz || pri_khz > end_khz)
1182 		return false;
1183 
1184 	for_each_s1g_subchan(chandef, freq_khz) {
1185 		chan = ieee80211_get_channel_khz(wiphy, freq_khz);
1186 		if (!chan || (chan->flags & prohibited_flags))
1187 			return false;
1188 	}
1189 
1190 	if (chandef->s1g_primary_2mhz) {
1191 		u32 sib_khz;
1192 		const struct ieee80211_channel *sibling;
1193 
1194 		sibling = cfg80211_s1g_get_primary_sibling(wiphy, chandef);
1195 		if (!sibling)
1196 			return false;
1197 
1198 		if (sibling->flags & IEEE80211_CHAN_S1G_NO_PRIMARY)
1199 			return false;
1200 
1201 		sib_khz = ieee80211_channel_to_khz(sibling);
1202 		if (sib_khz < start_khz || sib_khz > end_khz)
1203 			return false;
1204 	}
1205 
1206 	return true;
1207 }
1208 
1209 bool _cfg80211_chandef_usable(struct wiphy *wiphy,
1210 			      const struct cfg80211_chan_def *chandef,
1211 			      u32 prohibited_flags,
1212 			      u32 permitting_flags)
1213 {
1214 	struct ieee80211_sta_ht_cap *ht_cap;
1215 	struct ieee80211_sta_vht_cap *vht_cap;
1216 	struct ieee80211_edmg *edmg_cap;
1217 	u32 width, control_freq, cap;
1218 	bool ext_nss_cap, support_80_80 = false, support_320 = false;
1219 	const struct ieee80211_sband_iftype_data *iftd;
1220 	struct ieee80211_supported_band *sband;
1221 	struct ieee80211_channel *c;
1222 	int i;
1223 
1224 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
1225 		return false;
1226 
1227 	ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap;
1228 	vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap;
1229 	edmg_cap = &wiphy->bands[chandef->chan->band]->edmg_cap;
1230 	ext_nss_cap = __le16_to_cpu(vht_cap->vht_mcs.tx_highest) &
1231 			IEEE80211_VHT_EXT_NSS_BW_CAPABLE;
1232 
1233 	if (cfg80211_chandef_is_s1g(chandef))
1234 		return cfg80211_s1g_usable(wiphy, chandef);
1235 
1236 	if (edmg_cap->channels &&
1237 	    !cfg80211_edmg_usable(wiphy,
1238 				  chandef->edmg.channels,
1239 				  chandef->edmg.bw_config,
1240 				  chandef->chan->hw_value,
1241 				  edmg_cap))
1242 		return false;
1243 
1244 	control_freq = chandef->chan->center_freq;
1245 
1246 	switch (chandef->width) {
1247 	case NL80211_CHAN_WIDTH_5:
1248 		width = 5;
1249 		break;
1250 	case NL80211_CHAN_WIDTH_10:
1251 		prohibited_flags |= IEEE80211_CHAN_NO_10MHZ;
1252 		width = 10;
1253 		break;
1254 	case NL80211_CHAN_WIDTH_20:
1255 		if (!ht_cap->ht_supported &&
1256 		    chandef->chan->band != NL80211_BAND_6GHZ)
1257 			return false;
1258 		fallthrough;
1259 	case NL80211_CHAN_WIDTH_20_NOHT:
1260 		prohibited_flags |= IEEE80211_CHAN_NO_20MHZ;
1261 		width = 20;
1262 		break;
1263 	case NL80211_CHAN_WIDTH_40:
1264 		width = 40;
1265 		if (chandef->chan->band == NL80211_BAND_6GHZ)
1266 			break;
1267 		if (!ht_cap->ht_supported)
1268 			return false;
1269 		if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
1270 		    ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)
1271 			return false;
1272 		if (chandef->center_freq1 < control_freq &&
1273 		    chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
1274 			return false;
1275 		if (chandef->center_freq1 > control_freq &&
1276 		    chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
1277 			return false;
1278 		break;
1279 	case NL80211_CHAN_WIDTH_80P80:
1280 		cap = vht_cap->cap;
1281 		support_80_80 =
1282 			(cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) ||
1283 			(cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
1284 			 cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) ||
1285 			(ext_nss_cap &&
1286 			 u32_get_bits(cap, IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) > 1);
1287 		if (chandef->chan->band != NL80211_BAND_6GHZ && !support_80_80)
1288 			return false;
1289 		fallthrough;
1290 	case NL80211_CHAN_WIDTH_80:
1291 		prohibited_flags |= IEEE80211_CHAN_NO_80MHZ;
1292 		width = 80;
1293 		if (chandef->chan->band == NL80211_BAND_6GHZ)
1294 			break;
1295 		if (!vht_cap->vht_supported)
1296 			return false;
1297 		break;
1298 	case NL80211_CHAN_WIDTH_160:
1299 		prohibited_flags |= IEEE80211_CHAN_NO_160MHZ;
1300 		width = 160;
1301 		if (chandef->chan->band == NL80211_BAND_6GHZ)
1302 			break;
1303 		if (!vht_cap->vht_supported)
1304 			return false;
1305 		cap = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
1306 		if (cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
1307 		    cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ &&
1308 		    !(ext_nss_cap &&
1309 		      (vht_cap->cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK)))
1310 			return false;
1311 		break;
1312 	case NL80211_CHAN_WIDTH_320:
1313 		prohibited_flags |= IEEE80211_CHAN_NO_320MHZ;
1314 		width = 320;
1315 
1316 		if (chandef->chan->band != NL80211_BAND_6GHZ)
1317 			return false;
1318 
1319 		sband = wiphy->bands[NL80211_BAND_6GHZ];
1320 		if (!sband)
1321 			return false;
1322 
1323 		for_each_sband_iftype_data(sband, i, iftd) {
1324 			if (!iftd->eht_cap.has_eht)
1325 				continue;
1326 
1327 			if (iftd->eht_cap.eht_cap_elem.phy_cap_info[0] &
1328 			    IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ) {
1329 				support_320 = true;
1330 				break;
1331 			}
1332 		}
1333 
1334 		if (!support_320)
1335 			return false;
1336 		break;
1337 	default:
1338 		WARN_ON_ONCE(1);
1339 		return false;
1340 	}
1341 
1342 	/*
1343 	 * TODO: What if there are only certain 80/160/80+80 MHz channels
1344 	 *	 allowed by the driver, or only certain combinations?
1345 	 *	 For 40 MHz the driver can set the NO_HT40 flags, but for
1346 	 *	 80/160 MHz and in particular 80+80 MHz this isn't really
1347 	 *	 feasible and we only have NO_80MHZ/NO_160MHZ so far but
1348 	 *	 no way to cover 80+80 MHz or more complex restrictions.
1349 	 *	 Note that such restrictions also need to be advertised to
1350 	 *	 userspace, for example for P2P channel selection.
1351 	 */
1352 
1353 	if (width > 20)
1354 		prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
1355 
1356 	/* 5 and 10 MHz are only defined for the OFDM PHY */
1357 	if (width < 20)
1358 		prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
1359 
1360 	for_each_subchan(chandef, freq, cf) {
1361 		c = ieee80211_get_channel_khz(wiphy, freq);
1362 		if (!c)
1363 			return false;
1364 		if (c->flags & permitting_flags)
1365 			continue;
1366 		if (c->flags & prohibited_flags)
1367 			return false;
1368 	}
1369 
1370 	return true;
1371 }
1372 
1373 bool cfg80211_chandef_usable(struct wiphy *wiphy,
1374 			     const struct cfg80211_chan_def *chandef,
1375 			     u32 prohibited_flags)
1376 {
1377 	return _cfg80211_chandef_usable(wiphy, chandef, prohibited_flags, 0);
1378 }
1379 EXPORT_SYMBOL(cfg80211_chandef_usable);
1380 
1381 static bool cfg80211_ir_permissive_check_wdev(enum nl80211_iftype iftype,
1382 					      struct wireless_dev *wdev,
1383 					      struct ieee80211_channel *chan)
1384 {
1385 	struct ieee80211_channel *other_chan = NULL;
1386 	unsigned int link_id;
1387 	int r1, r2;
1388 
1389 	for_each_valid_link(wdev, link_id) {
1390 		if (wdev->iftype == NL80211_IFTYPE_STATION &&
1391 		    wdev->links[link_id].client.current_bss)
1392 			other_chan = wdev->links[link_id].client.current_bss->pub.channel;
1393 
1394 		/*
1395 		 * If a GO already operates on the same GO_CONCURRENT channel,
1396 		 * this one (maybe the same one) can beacon as well. We allow
1397 		 * the operation even if the station we relied on with
1398 		 * GO_CONCURRENT is disconnected now. But then we must make sure
1399 		 * we're not outdoor on an indoor-only channel.
1400 		 */
1401 		if (iftype == NL80211_IFTYPE_P2P_GO &&
1402 		    wdev->iftype == NL80211_IFTYPE_P2P_GO &&
1403 		    wdev->links[link_id].ap.beacon_interval &&
1404 		    !(chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
1405 			other_chan = wdev->links[link_id].ap.chandef.chan;
1406 
1407 		if (!other_chan)
1408 			continue;
1409 
1410 		if (chan == other_chan)
1411 			return true;
1412 
1413 		if (chan->band != NL80211_BAND_5GHZ &&
1414 		    chan->band != NL80211_BAND_6GHZ)
1415 			continue;
1416 
1417 		r1 = cfg80211_get_unii(chan->center_freq);
1418 		r2 = cfg80211_get_unii(other_chan->center_freq);
1419 
1420 		if (r1 != -EINVAL && r1 == r2) {
1421 			/*
1422 			 * At some locations channels 149-165 are considered a
1423 			 * bundle, but at other locations, e.g., Indonesia,
1424 			 * channels 149-161 are considered a bundle while
1425 			 * channel 165 is left out and considered to be in a
1426 			 * different bundle. Thus, in case that there is a
1427 			 * station interface connected to an AP on channel 165,
1428 			 * it is assumed that channels 149-161 are allowed for
1429 			 * GO operations. However, having a station interface
1430 			 * connected to an AP on channels 149-161, does not
1431 			 * allow GO operation on channel 165.
1432 			 */
1433 			if (chan->center_freq == 5825 &&
1434 			    other_chan->center_freq != 5825)
1435 				continue;
1436 			return true;
1437 		}
1438 	}
1439 
1440 	return false;
1441 }
1442 
1443 /*
1444  * Check if the channel can be used under permissive conditions mandated by
1445  * some regulatory bodies, i.e., the channel is marked with
1446  * IEEE80211_CHAN_IR_CONCURRENT and there is an additional station interface
1447  * associated to an AP on the same channel or on the same UNII band
1448  * (assuming that the AP is an authorized master).
1449  * In addition allow operation on a channel on which indoor operation is
1450  * allowed, iff we are currently operating in an indoor environment.
1451  */
1452 static bool cfg80211_ir_permissive_chan(struct wiphy *wiphy,
1453 					enum nl80211_iftype iftype,
1454 					struct ieee80211_channel *chan)
1455 {
1456 	struct wireless_dev *wdev;
1457 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1458 
1459 	lockdep_assert_held(&rdev->wiphy.mtx);
1460 
1461 	if (!IS_ENABLED(CONFIG_CFG80211_REG_RELAX_NO_IR) ||
1462 	    !(wiphy->regulatory_flags & REGULATORY_ENABLE_RELAX_NO_IR))
1463 		return false;
1464 
1465 	/* only valid for GO and TDLS off-channel (station/p2p-CL) */
1466 	if (iftype != NL80211_IFTYPE_P2P_GO &&
1467 	    iftype != NL80211_IFTYPE_STATION &&
1468 	    iftype != NL80211_IFTYPE_P2P_CLIENT)
1469 		return false;
1470 
1471 	if (regulatory_indoor_allowed() &&
1472 	    (chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
1473 		return true;
1474 
1475 	if (!(chan->flags & IEEE80211_CHAN_IR_CONCURRENT))
1476 		return false;
1477 
1478 	/*
1479 	 * Generally, it is possible to rely on another device/driver to allow
1480 	 * the IR concurrent relaxation, however, since the device can further
1481 	 * enforce the relaxation (by doing a similar verifications as this),
1482 	 * and thus fail the GO instantiation, consider only the interfaces of
1483 	 * the current registered device.
1484 	 */
1485 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
1486 		bool ret;
1487 
1488 		ret = cfg80211_ir_permissive_check_wdev(iftype, wdev, chan);
1489 		if (ret)
1490 			return ret;
1491 	}
1492 
1493 	return false;
1494 }
1495 
1496 static bool _cfg80211_reg_can_beacon(struct wiphy *wiphy,
1497 				     struct cfg80211_chan_def *chandef,
1498 				     enum nl80211_iftype iftype,
1499 				     u32 prohibited_flags,
1500 				     u32 permitting_flags)
1501 {
1502 	bool res, check_radar;
1503 	int dfs_required;
1504 
1505 	trace_cfg80211_reg_can_beacon(wiphy, chandef, iftype,
1506 				      prohibited_flags,
1507 				      permitting_flags);
1508 
1509 	if (!_cfg80211_chandef_usable(wiphy, chandef,
1510 				      IEEE80211_CHAN_DISABLED, 0))
1511 		return false;
1512 
1513 	dfs_required = cfg80211_chandef_dfs_required(wiphy, chandef, iftype);
1514 	check_radar = dfs_required != 0;
1515 
1516 	if (dfs_required > 0 &&
1517 	    cfg80211_chandef_dfs_available(wiphy, chandef)) {
1518 		/* We can skip IEEE80211_CHAN_NO_IR if chandef dfs available */
1519 		prohibited_flags &= ~IEEE80211_CHAN_NO_IR;
1520 		check_radar = false;
1521 	}
1522 
1523 	if (check_radar &&
1524 	    !_cfg80211_chandef_usable(wiphy, chandef,
1525 				      IEEE80211_CHAN_RADAR, 0))
1526 		return false;
1527 
1528 	res = _cfg80211_chandef_usable(wiphy, chandef,
1529 				       prohibited_flags,
1530 				       permitting_flags);
1531 
1532 	trace_cfg80211_return_bool(res);
1533 	return res;
1534 }
1535 
1536 bool cfg80211_reg_check_beaconing(struct wiphy *wiphy,
1537 				  struct cfg80211_chan_def *chandef,
1538 				  struct cfg80211_beaconing_check_config *cfg)
1539 {
1540 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1541 	u32 permitting_flags = 0;
1542 	bool check_no_ir = true;
1543 
1544 	/*
1545 	 * Under certain conditions suggested by some regulatory bodies a
1546 	 * GO/STA can IR on channels marked with IEEE80211_NO_IR. Set this flag
1547 	 * only if such relaxations are not enabled and the conditions are not
1548 	 * met.
1549 	 */
1550 	if (cfg->relax) {
1551 		lockdep_assert_held(&rdev->wiphy.mtx);
1552 		check_no_ir = !cfg80211_ir_permissive_chan(wiphy, cfg->iftype,
1553 							   chandef->chan);
1554 	}
1555 
1556 	if (cfg->reg_power == IEEE80211_REG_VLP_AP)
1557 		permitting_flags |= IEEE80211_CHAN_ALLOW_6GHZ_VLP_AP;
1558 
1559 	if ((cfg->iftype == NL80211_IFTYPE_P2P_GO ||
1560 	     cfg->iftype == NL80211_IFTYPE_AP) &&
1561 	    (chandef->width == NL80211_CHAN_WIDTH_20_NOHT ||
1562 	     chandef->width == NL80211_CHAN_WIDTH_20))
1563 		permitting_flags |= IEEE80211_CHAN_ALLOW_20MHZ_ACTIVITY;
1564 
1565 	return _cfg80211_reg_can_beacon(wiphy, chandef, cfg->iftype,
1566 					check_no_ir ? IEEE80211_CHAN_NO_IR : 0,
1567 					permitting_flags);
1568 }
1569 EXPORT_SYMBOL(cfg80211_reg_check_beaconing);
1570 
1571 int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
1572 				 struct net_device *dev,
1573 				 struct cfg80211_chan_def *chandef)
1574 {
1575 	if (!rdev->ops->set_monitor_channel)
1576 		return -EOPNOTSUPP;
1577 	if (!cfg80211_has_monitors_only(rdev))
1578 		return -EBUSY;
1579 
1580 	return rdev_set_monitor_channel(rdev, dev, chandef);
1581 }
1582 
1583 bool cfg80211_any_usable_channels(struct wiphy *wiphy,
1584 				  unsigned long sband_mask,
1585 				  u32 prohibited_flags)
1586 {
1587 	int idx;
1588 
1589 	prohibited_flags |= IEEE80211_CHAN_DISABLED;
1590 
1591 	for_each_set_bit(idx, &sband_mask, NUM_NL80211_BANDS) {
1592 		struct ieee80211_supported_band *sband = wiphy->bands[idx];
1593 		int chanidx;
1594 
1595 		if (!sband)
1596 			continue;
1597 
1598 		for (chanidx = 0; chanidx < sband->n_channels; chanidx++) {
1599 			struct ieee80211_channel *chan;
1600 
1601 			chan = &sband->channels[chanidx];
1602 
1603 			if (chan->flags & prohibited_flags)
1604 				continue;
1605 
1606 			return true;
1607 		}
1608 	}
1609 
1610 	return false;
1611 }
1612 EXPORT_SYMBOL(cfg80211_any_usable_channels);
1613 
1614 struct cfg80211_chan_def *wdev_chandef(struct wireless_dev *wdev,
1615 				       unsigned int link_id)
1616 {
1617 	lockdep_assert_wiphy(wdev->wiphy);
1618 
1619 	WARN_ON(wdev->valid_links && !(wdev->valid_links & BIT(link_id)));
1620 	WARN_ON(!wdev->valid_links && link_id > 0);
1621 
1622 	switch (wdev->iftype) {
1623 	case NL80211_IFTYPE_MESH_POINT:
1624 		return &wdev->u.mesh.chandef;
1625 	case NL80211_IFTYPE_ADHOC:
1626 		return &wdev->u.ibss.chandef;
1627 	case NL80211_IFTYPE_OCB:
1628 		return &wdev->u.ocb.chandef;
1629 	case NL80211_IFTYPE_AP:
1630 	case NL80211_IFTYPE_P2P_GO:
1631 		return &wdev->links[link_id].ap.chandef;
1632 	default:
1633 		return NULL;
1634 	}
1635 }
1636 EXPORT_SYMBOL(wdev_chandef);
1637