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