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