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