xref: /linux/net/wireless/chan.c (revision b1a54551dd9ed5ef1763b97b35a0999ca002b95c)
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 bool
519 cfg80211_dfs_permissive_check_wdev(struct cfg80211_registered_device *rdev,
520 				   enum nl80211_iftype iftype,
521 				   struct wireless_dev *wdev,
522 				   struct ieee80211_channel *chan)
523 {
524 	unsigned int link_id;
525 
526 	for_each_valid_link(wdev, link_id) {
527 		struct ieee80211_channel *other_chan = NULL;
528 		struct cfg80211_chan_def chandef = {};
529 		int ret;
530 
531 		/* In order to avoid daisy chaining only allow BSS STA */
532 		if (wdev->iftype != NL80211_IFTYPE_STATION ||
533 		    !wdev->links[link_id].client.current_bss)
534 			continue;
535 
536 		other_chan =
537 			wdev->links[link_id].client.current_bss->pub.channel;
538 
539 		if (!other_chan)
540 			continue;
541 
542 		if (chan == other_chan)
543 			return true;
544 
545 		/* continue if we can't get the channel */
546 		ret = rdev_get_channel(rdev, wdev, link_id, &chandef);
547 		if (ret)
548 			continue;
549 
550 		if (cfg80211_is_sub_chan(&chandef, chan, false))
551 			return true;
552 	}
553 
554 	return false;
555 }
556 
557 /*
558  * Check if P2P GO is allowed to operate on a DFS channel
559  */
560 static bool cfg80211_dfs_permissive_chan(struct wiphy *wiphy,
561 					 enum nl80211_iftype iftype,
562 					 struct ieee80211_channel *chan)
563 {
564 	struct wireless_dev *wdev;
565 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
566 
567 	lockdep_assert_held(&rdev->wiphy.mtx);
568 
569 	if (!wiphy_ext_feature_isset(&rdev->wiphy,
570 				     NL80211_EXT_FEATURE_DFS_CONCURRENT) ||
571 	    !(chan->flags & IEEE80211_CHAN_DFS_CONCURRENT))
572 		return false;
573 
574 	/* only valid for P2P GO */
575 	if (iftype != NL80211_IFTYPE_P2P_GO)
576 		return false;
577 
578 	/*
579 	 * Allow only if there's a concurrent BSS
580 	 */
581 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
582 		bool ret = cfg80211_dfs_permissive_check_wdev(rdev, iftype,
583 							      wdev, chan);
584 		if (ret)
585 			return ret;
586 	}
587 
588 	return false;
589 }
590 
591 static int cfg80211_get_chans_dfs_required(struct wiphy *wiphy,
592 					    u32 center_freq,
593 					    u32 bandwidth,
594 					    enum nl80211_iftype iftype)
595 {
596 	struct ieee80211_channel *c;
597 	u32 freq, start_freq, end_freq;
598 
599 	start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
600 	end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
601 
602 	for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
603 		c = ieee80211_get_channel_khz(wiphy, freq);
604 		if (!c)
605 			return -EINVAL;
606 
607 		if (c->flags & IEEE80211_CHAN_RADAR &&
608 		    !cfg80211_dfs_permissive_chan(wiphy, iftype, c))
609 			return 1;
610 	}
611 
612 	return 0;
613 }
614 
615 
616 int cfg80211_chandef_dfs_required(struct wiphy *wiphy,
617 				  const struct cfg80211_chan_def *chandef,
618 				  enum nl80211_iftype iftype)
619 {
620 	int width;
621 	int ret;
622 
623 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
624 		return -EINVAL;
625 
626 	switch (iftype) {
627 	case NL80211_IFTYPE_ADHOC:
628 	case NL80211_IFTYPE_AP:
629 	case NL80211_IFTYPE_P2P_GO:
630 	case NL80211_IFTYPE_MESH_POINT:
631 		width = cfg80211_chandef_get_width(chandef);
632 		if (width < 0)
633 			return -EINVAL;
634 
635 		ret = cfg80211_get_chans_dfs_required(wiphy,
636 					ieee80211_chandef_to_khz(chandef),
637 					width, iftype);
638 		if (ret < 0)
639 			return ret;
640 		else if (ret > 0)
641 			return BIT(chandef->width);
642 
643 		if (!chandef->center_freq2)
644 			return 0;
645 
646 		ret = cfg80211_get_chans_dfs_required(wiphy,
647 					MHZ_TO_KHZ(chandef->center_freq2),
648 					width, iftype);
649 		if (ret < 0)
650 			return ret;
651 		else if (ret > 0)
652 			return BIT(chandef->width);
653 
654 		break;
655 	case NL80211_IFTYPE_STATION:
656 	case NL80211_IFTYPE_OCB:
657 	case NL80211_IFTYPE_P2P_CLIENT:
658 	case NL80211_IFTYPE_MONITOR:
659 	case NL80211_IFTYPE_AP_VLAN:
660 	case NL80211_IFTYPE_P2P_DEVICE:
661 	case NL80211_IFTYPE_NAN:
662 		break;
663 	case NL80211_IFTYPE_WDS:
664 	case NL80211_IFTYPE_UNSPECIFIED:
665 	case NUM_NL80211_IFTYPES:
666 		WARN_ON(1);
667 	}
668 
669 	return 0;
670 }
671 EXPORT_SYMBOL(cfg80211_chandef_dfs_required);
672 
673 static int cfg80211_get_chans_dfs_usable(struct wiphy *wiphy,
674 					 u32 center_freq,
675 					 u32 bandwidth)
676 {
677 	struct ieee80211_channel *c;
678 	u32 freq, start_freq, end_freq;
679 	int count = 0;
680 
681 	start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
682 	end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
683 
684 	/*
685 	 * Check entire range of channels for the bandwidth.
686 	 * Check all channels are DFS channels (DFS_USABLE or
687 	 * DFS_AVAILABLE). Return number of usable channels
688 	 * (require CAC). Allow DFS and non-DFS channel mix.
689 	 */
690 	for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
691 		c = ieee80211_get_channel_khz(wiphy, freq);
692 		if (!c)
693 			return -EINVAL;
694 
695 		if (c->flags & IEEE80211_CHAN_DISABLED)
696 			return -EINVAL;
697 
698 		if (c->flags & IEEE80211_CHAN_RADAR) {
699 			if (c->dfs_state == NL80211_DFS_UNAVAILABLE)
700 				return -EINVAL;
701 
702 			if (c->dfs_state == NL80211_DFS_USABLE)
703 				count++;
704 		}
705 	}
706 
707 	return count;
708 }
709 
710 bool cfg80211_chandef_dfs_usable(struct wiphy *wiphy,
711 				 const struct cfg80211_chan_def *chandef)
712 {
713 	int width;
714 	int r1, r2 = 0;
715 
716 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
717 		return false;
718 
719 	width = cfg80211_chandef_get_width(chandef);
720 	if (width < 0)
721 		return false;
722 
723 	r1 = cfg80211_get_chans_dfs_usable(wiphy,
724 					   MHZ_TO_KHZ(chandef->center_freq1),
725 					   width);
726 
727 	if (r1 < 0)
728 		return false;
729 
730 	switch (chandef->width) {
731 	case NL80211_CHAN_WIDTH_80P80:
732 		WARN_ON(!chandef->center_freq2);
733 		r2 = cfg80211_get_chans_dfs_usable(wiphy,
734 					MHZ_TO_KHZ(chandef->center_freq2),
735 					width);
736 		if (r2 < 0)
737 			return false;
738 		break;
739 	default:
740 		WARN_ON(chandef->center_freq2);
741 		break;
742 	}
743 
744 	return (r1 + r2 > 0);
745 }
746 EXPORT_SYMBOL(cfg80211_chandef_dfs_usable);
747 
748 /*
749  * Checks if center frequency of chan falls with in the bandwidth
750  * range of chandef.
751  */
752 bool cfg80211_is_sub_chan(struct cfg80211_chan_def *chandef,
753 			  struct ieee80211_channel *chan,
754 			  bool primary_only)
755 {
756 	int width;
757 	u32 freq;
758 
759 	if (!chandef->chan)
760 		return false;
761 
762 	if (chandef->chan->center_freq == chan->center_freq)
763 		return true;
764 
765 	if (primary_only)
766 		return false;
767 
768 	width = cfg80211_chandef_get_width(chandef);
769 	if (width <= 20)
770 		return false;
771 
772 	for (freq = chandef->center_freq1 - width / 2 + 10;
773 	     freq <= chandef->center_freq1 + width / 2 - 10; freq += 20) {
774 		if (chan->center_freq == freq)
775 			return true;
776 	}
777 
778 	if (!chandef->center_freq2)
779 		return false;
780 
781 	for (freq = chandef->center_freq2 - width / 2 + 10;
782 	     freq <= chandef->center_freq2 + width / 2 - 10; freq += 20) {
783 		if (chan->center_freq == freq)
784 			return true;
785 	}
786 
787 	return false;
788 }
789 
790 bool cfg80211_beaconing_iface_active(struct wireless_dev *wdev)
791 {
792 	unsigned int link;
793 
794 	lockdep_assert_wiphy(wdev->wiphy);
795 
796 	switch (wdev->iftype) {
797 	case NL80211_IFTYPE_AP:
798 	case NL80211_IFTYPE_P2P_GO:
799 		for_each_valid_link(wdev, link) {
800 			if (wdev->links[link].ap.beacon_interval)
801 				return true;
802 		}
803 		break;
804 	case NL80211_IFTYPE_ADHOC:
805 		if (wdev->u.ibss.ssid_len)
806 			return true;
807 		break;
808 	case NL80211_IFTYPE_MESH_POINT:
809 		if (wdev->u.mesh.id_len)
810 			return true;
811 		break;
812 	case NL80211_IFTYPE_STATION:
813 	case NL80211_IFTYPE_OCB:
814 	case NL80211_IFTYPE_P2P_CLIENT:
815 	case NL80211_IFTYPE_MONITOR:
816 	case NL80211_IFTYPE_AP_VLAN:
817 	case NL80211_IFTYPE_P2P_DEVICE:
818 	/* Can NAN type be considered as beaconing interface? */
819 	case NL80211_IFTYPE_NAN:
820 		break;
821 	case NL80211_IFTYPE_UNSPECIFIED:
822 	case NL80211_IFTYPE_WDS:
823 	case NUM_NL80211_IFTYPES:
824 		WARN_ON(1);
825 	}
826 
827 	return false;
828 }
829 
830 bool cfg80211_wdev_on_sub_chan(struct wireless_dev *wdev,
831 			       struct ieee80211_channel *chan,
832 			       bool primary_only)
833 {
834 	unsigned int link;
835 
836 	switch (wdev->iftype) {
837 	case NL80211_IFTYPE_AP:
838 	case NL80211_IFTYPE_P2P_GO:
839 		for_each_valid_link(wdev, link) {
840 			if (cfg80211_is_sub_chan(&wdev->links[link].ap.chandef,
841 						 chan, primary_only))
842 				return true;
843 		}
844 		break;
845 	case NL80211_IFTYPE_ADHOC:
846 		return cfg80211_is_sub_chan(&wdev->u.ibss.chandef, chan,
847 					    primary_only);
848 	case NL80211_IFTYPE_MESH_POINT:
849 		return cfg80211_is_sub_chan(&wdev->u.mesh.chandef, chan,
850 					    primary_only);
851 	default:
852 		break;
853 	}
854 
855 	return false;
856 }
857 
858 static bool cfg80211_is_wiphy_oper_chan(struct wiphy *wiphy,
859 					struct ieee80211_channel *chan)
860 {
861 	struct wireless_dev *wdev;
862 
863 	lockdep_assert_wiphy(wiphy);
864 
865 	list_for_each_entry(wdev, &wiphy->wdev_list, list) {
866 		if (!cfg80211_beaconing_iface_active(wdev))
867 			continue;
868 
869 		if (cfg80211_wdev_on_sub_chan(wdev, chan, false))
870 			return true;
871 	}
872 
873 	return false;
874 }
875 
876 static bool
877 cfg80211_offchan_chain_is_active(struct cfg80211_registered_device *rdev,
878 				 struct ieee80211_channel *channel)
879 {
880 	if (!rdev->background_radar_wdev)
881 		return false;
882 
883 	if (!cfg80211_chandef_valid(&rdev->background_radar_chandef))
884 		return false;
885 
886 	return cfg80211_is_sub_chan(&rdev->background_radar_chandef, channel,
887 				    false);
888 }
889 
890 bool cfg80211_any_wiphy_oper_chan(struct wiphy *wiphy,
891 				  struct ieee80211_channel *chan)
892 {
893 	struct cfg80211_registered_device *rdev;
894 
895 	ASSERT_RTNL();
896 
897 	if (!(chan->flags & IEEE80211_CHAN_RADAR))
898 		return false;
899 
900 	for_each_rdev(rdev) {
901 		bool found;
902 
903 		if (!reg_dfs_domain_same(wiphy, &rdev->wiphy))
904 			continue;
905 
906 		wiphy_lock(&rdev->wiphy);
907 		found = cfg80211_is_wiphy_oper_chan(&rdev->wiphy, chan) ||
908 			cfg80211_offchan_chain_is_active(rdev, chan);
909 		wiphy_unlock(&rdev->wiphy);
910 
911 		if (found)
912 			return true;
913 	}
914 
915 	return false;
916 }
917 
918 static bool cfg80211_get_chans_dfs_available(struct wiphy *wiphy,
919 					     u32 center_freq,
920 					     u32 bandwidth)
921 {
922 	struct ieee80211_channel *c;
923 	u32 freq, start_freq, end_freq;
924 	bool dfs_offload;
925 
926 	dfs_offload = wiphy_ext_feature_isset(wiphy,
927 					      NL80211_EXT_FEATURE_DFS_OFFLOAD);
928 
929 	start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
930 	end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
931 
932 	/*
933 	 * Check entire range of channels for the bandwidth.
934 	 * If any channel in between is disabled or has not
935 	 * had gone through CAC return false
936 	 */
937 	for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
938 		c = ieee80211_get_channel_khz(wiphy, freq);
939 		if (!c)
940 			return false;
941 
942 		if (c->flags & IEEE80211_CHAN_DISABLED)
943 			return false;
944 
945 		if ((c->flags & IEEE80211_CHAN_RADAR) &&
946 		    (c->dfs_state != NL80211_DFS_AVAILABLE) &&
947 		    !(c->dfs_state == NL80211_DFS_USABLE && dfs_offload))
948 			return false;
949 	}
950 
951 	return true;
952 }
953 
954 static bool cfg80211_chandef_dfs_available(struct wiphy *wiphy,
955 				const struct cfg80211_chan_def *chandef)
956 {
957 	int width;
958 	int r;
959 
960 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
961 		return false;
962 
963 	width = cfg80211_chandef_get_width(chandef);
964 	if (width < 0)
965 		return false;
966 
967 	r = cfg80211_get_chans_dfs_available(wiphy,
968 					     MHZ_TO_KHZ(chandef->center_freq1),
969 					     width);
970 
971 	/* If any of channels unavailable for cf1 just return */
972 	if (!r)
973 		return r;
974 
975 	switch (chandef->width) {
976 	case NL80211_CHAN_WIDTH_80P80:
977 		WARN_ON(!chandef->center_freq2);
978 		r = cfg80211_get_chans_dfs_available(wiphy,
979 					MHZ_TO_KHZ(chandef->center_freq2),
980 					width);
981 		break;
982 	default:
983 		WARN_ON(chandef->center_freq2);
984 		break;
985 	}
986 
987 	return r;
988 }
989 
990 static unsigned int cfg80211_get_chans_dfs_cac_time(struct wiphy *wiphy,
991 						    u32 center_freq,
992 						    u32 bandwidth)
993 {
994 	struct ieee80211_channel *c;
995 	u32 start_freq, end_freq, freq;
996 	unsigned int dfs_cac_ms = 0;
997 
998 	start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
999 	end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
1000 
1001 	for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
1002 		c = ieee80211_get_channel_khz(wiphy, freq);
1003 		if (!c)
1004 			return 0;
1005 
1006 		if (c->flags & IEEE80211_CHAN_DISABLED)
1007 			return 0;
1008 
1009 		if (!(c->flags & IEEE80211_CHAN_RADAR))
1010 			continue;
1011 
1012 		if (c->dfs_cac_ms > dfs_cac_ms)
1013 			dfs_cac_ms = c->dfs_cac_ms;
1014 	}
1015 
1016 	return dfs_cac_ms;
1017 }
1018 
1019 unsigned int
1020 cfg80211_chandef_dfs_cac_time(struct wiphy *wiphy,
1021 			      const struct cfg80211_chan_def *chandef)
1022 {
1023 	int width;
1024 	unsigned int t1 = 0, t2 = 0;
1025 
1026 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
1027 		return 0;
1028 
1029 	width = cfg80211_chandef_get_width(chandef);
1030 	if (width < 0)
1031 		return 0;
1032 
1033 	t1 = cfg80211_get_chans_dfs_cac_time(wiphy,
1034 					     MHZ_TO_KHZ(chandef->center_freq1),
1035 					     width);
1036 
1037 	if (!chandef->center_freq2)
1038 		return t1;
1039 
1040 	t2 = cfg80211_get_chans_dfs_cac_time(wiphy,
1041 					     MHZ_TO_KHZ(chandef->center_freq2),
1042 					     width);
1043 
1044 	return max(t1, t2);
1045 }
1046 EXPORT_SYMBOL(cfg80211_chandef_dfs_cac_time);
1047 
1048 static bool cfg80211_secondary_chans_ok(struct wiphy *wiphy,
1049 					u32 center_freq, u32 bandwidth,
1050 					u32 prohibited_flags)
1051 {
1052 	struct ieee80211_channel *c;
1053 	u32 freq, start_freq, end_freq;
1054 
1055 	start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
1056 	end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
1057 
1058 	for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
1059 		c = ieee80211_get_channel_khz(wiphy, freq);
1060 		if (!c || c->flags & prohibited_flags)
1061 			return false;
1062 	}
1063 
1064 	return true;
1065 }
1066 
1067 /* check if the operating channels are valid and supported */
1068 static bool cfg80211_edmg_usable(struct wiphy *wiphy, u8 edmg_channels,
1069 				 enum ieee80211_edmg_bw_config edmg_bw_config,
1070 				 int primary_channel,
1071 				 struct ieee80211_edmg *edmg_cap)
1072 {
1073 	struct ieee80211_channel *chan;
1074 	int i, freq;
1075 	int channels_counter = 0;
1076 
1077 	if (!edmg_channels && !edmg_bw_config)
1078 		return true;
1079 
1080 	if ((!edmg_channels && edmg_bw_config) ||
1081 	    (edmg_channels && !edmg_bw_config))
1082 		return false;
1083 
1084 	if (!(edmg_channels & BIT(primary_channel - 1)))
1085 		return false;
1086 
1087 	/* 60GHz channels 1..6 */
1088 	for (i = 0; i < 6; i++) {
1089 		if (!(edmg_channels & BIT(i)))
1090 			continue;
1091 
1092 		if (!(edmg_cap->channels & BIT(i)))
1093 			return false;
1094 
1095 		channels_counter++;
1096 
1097 		freq = ieee80211_channel_to_frequency(i + 1,
1098 						      NL80211_BAND_60GHZ);
1099 		chan = ieee80211_get_channel(wiphy, freq);
1100 		if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1101 			return false;
1102 	}
1103 
1104 	/* IEEE802.11 allows max 4 channels */
1105 	if (channels_counter > 4)
1106 		return false;
1107 
1108 	/* check bw_config is a subset of what driver supports
1109 	 * (see IEEE P802.11ay/D4.0 section 9.4.2.251, Table 13)
1110 	 */
1111 	if ((edmg_bw_config % 4) > (edmg_cap->bw_config % 4))
1112 		return false;
1113 
1114 	if (edmg_bw_config > edmg_cap->bw_config)
1115 		return false;
1116 
1117 	return true;
1118 }
1119 
1120 bool cfg80211_chandef_usable(struct wiphy *wiphy,
1121 			     const struct cfg80211_chan_def *chandef,
1122 			     u32 prohibited_flags)
1123 {
1124 	struct ieee80211_sta_ht_cap *ht_cap;
1125 	struct ieee80211_sta_vht_cap *vht_cap;
1126 	struct ieee80211_edmg *edmg_cap;
1127 	u32 width, control_freq, cap;
1128 	bool ext_nss_cap, support_80_80 = false, support_320 = false;
1129 	const struct ieee80211_sband_iftype_data *iftd;
1130 	struct ieee80211_supported_band *sband;
1131 	int i;
1132 
1133 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
1134 		return false;
1135 
1136 	ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap;
1137 	vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap;
1138 	edmg_cap = &wiphy->bands[chandef->chan->band]->edmg_cap;
1139 	ext_nss_cap = __le16_to_cpu(vht_cap->vht_mcs.tx_highest) &
1140 			IEEE80211_VHT_EXT_NSS_BW_CAPABLE;
1141 
1142 	if (edmg_cap->channels &&
1143 	    !cfg80211_edmg_usable(wiphy,
1144 				  chandef->edmg.channels,
1145 				  chandef->edmg.bw_config,
1146 				  chandef->chan->hw_value,
1147 				  edmg_cap))
1148 		return false;
1149 
1150 	control_freq = chandef->chan->center_freq;
1151 
1152 	switch (chandef->width) {
1153 	case NL80211_CHAN_WIDTH_1:
1154 		width = 1;
1155 		break;
1156 	case NL80211_CHAN_WIDTH_2:
1157 		width = 2;
1158 		break;
1159 	case NL80211_CHAN_WIDTH_4:
1160 		width = 4;
1161 		break;
1162 	case NL80211_CHAN_WIDTH_8:
1163 		width = 8;
1164 		break;
1165 	case NL80211_CHAN_WIDTH_16:
1166 		width = 16;
1167 		break;
1168 	case NL80211_CHAN_WIDTH_5:
1169 		width = 5;
1170 		break;
1171 	case NL80211_CHAN_WIDTH_10:
1172 		prohibited_flags |= IEEE80211_CHAN_NO_10MHZ;
1173 		width = 10;
1174 		break;
1175 	case NL80211_CHAN_WIDTH_20:
1176 		if (!ht_cap->ht_supported &&
1177 		    chandef->chan->band != NL80211_BAND_6GHZ)
1178 			return false;
1179 		fallthrough;
1180 	case NL80211_CHAN_WIDTH_20_NOHT:
1181 		prohibited_flags |= IEEE80211_CHAN_NO_20MHZ;
1182 		width = 20;
1183 		break;
1184 	case NL80211_CHAN_WIDTH_40:
1185 		width = 40;
1186 		if (chandef->chan->band == NL80211_BAND_6GHZ)
1187 			break;
1188 		if (!ht_cap->ht_supported)
1189 			return false;
1190 		if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
1191 		    ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)
1192 			return false;
1193 		if (chandef->center_freq1 < control_freq &&
1194 		    chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
1195 			return false;
1196 		if (chandef->center_freq1 > control_freq &&
1197 		    chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
1198 			return false;
1199 		break;
1200 	case NL80211_CHAN_WIDTH_80P80:
1201 		cap = vht_cap->cap;
1202 		support_80_80 =
1203 			(cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) ||
1204 			(cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
1205 			 cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) ||
1206 			(ext_nss_cap &&
1207 			 u32_get_bits(cap, IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) > 1);
1208 		if (chandef->chan->band != NL80211_BAND_6GHZ && !support_80_80)
1209 			return false;
1210 		fallthrough;
1211 	case NL80211_CHAN_WIDTH_80:
1212 		prohibited_flags |= IEEE80211_CHAN_NO_80MHZ;
1213 		width = 80;
1214 		if (chandef->chan->band == NL80211_BAND_6GHZ)
1215 			break;
1216 		if (!vht_cap->vht_supported)
1217 			return false;
1218 		break;
1219 	case NL80211_CHAN_WIDTH_160:
1220 		prohibited_flags |= IEEE80211_CHAN_NO_160MHZ;
1221 		width = 160;
1222 		if (chandef->chan->band == NL80211_BAND_6GHZ)
1223 			break;
1224 		if (!vht_cap->vht_supported)
1225 			return false;
1226 		cap = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
1227 		if (cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
1228 		    cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ &&
1229 		    !(ext_nss_cap &&
1230 		      (vht_cap->cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK)))
1231 			return false;
1232 		break;
1233 	case NL80211_CHAN_WIDTH_320:
1234 		prohibited_flags |= IEEE80211_CHAN_NO_320MHZ;
1235 		width = 320;
1236 
1237 		if (chandef->chan->band != NL80211_BAND_6GHZ)
1238 			return false;
1239 
1240 		sband = wiphy->bands[NL80211_BAND_6GHZ];
1241 		if (!sband)
1242 			return false;
1243 
1244 		for_each_sband_iftype_data(sband, i, iftd) {
1245 			if (!iftd->eht_cap.has_eht)
1246 				continue;
1247 
1248 			if (iftd->eht_cap.eht_cap_elem.phy_cap_info[0] &
1249 			    IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ) {
1250 				support_320 = true;
1251 				break;
1252 			}
1253 		}
1254 
1255 		if (!support_320)
1256 			return false;
1257 		break;
1258 	default:
1259 		WARN_ON_ONCE(1);
1260 		return false;
1261 	}
1262 
1263 	/*
1264 	 * TODO: What if there are only certain 80/160/80+80 MHz channels
1265 	 *	 allowed by the driver, or only certain combinations?
1266 	 *	 For 40 MHz the driver can set the NO_HT40 flags, but for
1267 	 *	 80/160 MHz and in particular 80+80 MHz this isn't really
1268 	 *	 feasible and we only have NO_80MHZ/NO_160MHZ so far but
1269 	 *	 no way to cover 80+80 MHz or more complex restrictions.
1270 	 *	 Note that such restrictions also need to be advertised to
1271 	 *	 userspace, for example for P2P channel selection.
1272 	 */
1273 
1274 	if (width > 20)
1275 		prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
1276 
1277 	/* 5 and 10 MHz are only defined for the OFDM PHY */
1278 	if (width < 20)
1279 		prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
1280 
1281 
1282 	if (!cfg80211_secondary_chans_ok(wiphy,
1283 					 ieee80211_chandef_to_khz(chandef),
1284 					 width, prohibited_flags))
1285 		return false;
1286 
1287 	if (!chandef->center_freq2)
1288 		return true;
1289 	return cfg80211_secondary_chans_ok(wiphy,
1290 					   MHZ_TO_KHZ(chandef->center_freq2),
1291 					   width, prohibited_flags);
1292 }
1293 EXPORT_SYMBOL(cfg80211_chandef_usable);
1294 
1295 static bool cfg80211_ir_permissive_check_wdev(enum nl80211_iftype iftype,
1296 					      struct wireless_dev *wdev,
1297 					      struct ieee80211_channel *chan)
1298 {
1299 	struct ieee80211_channel *other_chan = NULL;
1300 	unsigned int link_id;
1301 	int r1, r2;
1302 
1303 	for_each_valid_link(wdev, link_id) {
1304 		if (wdev->iftype == NL80211_IFTYPE_STATION &&
1305 		    wdev->links[link_id].client.current_bss)
1306 			other_chan = wdev->links[link_id].client.current_bss->pub.channel;
1307 
1308 		/*
1309 		 * If a GO already operates on the same GO_CONCURRENT channel,
1310 		 * this one (maybe the same one) can beacon as well. We allow
1311 		 * the operation even if the station we relied on with
1312 		 * GO_CONCURRENT is disconnected now. But then we must make sure
1313 		 * we're not outdoor on an indoor-only channel.
1314 		 */
1315 		if (iftype == NL80211_IFTYPE_P2P_GO &&
1316 		    wdev->iftype == NL80211_IFTYPE_P2P_GO &&
1317 		    wdev->links[link_id].ap.beacon_interval &&
1318 		    !(chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
1319 			other_chan = wdev->links[link_id].ap.chandef.chan;
1320 
1321 		if (!other_chan)
1322 			continue;
1323 
1324 		if (chan == other_chan)
1325 			return true;
1326 
1327 		if (chan->band != NL80211_BAND_5GHZ &&
1328 		    chan->band != NL80211_BAND_6GHZ)
1329 			continue;
1330 
1331 		r1 = cfg80211_get_unii(chan->center_freq);
1332 		r2 = cfg80211_get_unii(other_chan->center_freq);
1333 
1334 		if (r1 != -EINVAL && r1 == r2) {
1335 			/*
1336 			 * At some locations channels 149-165 are considered a
1337 			 * bundle, but at other locations, e.g., Indonesia,
1338 			 * channels 149-161 are considered a bundle while
1339 			 * channel 165 is left out and considered to be in a
1340 			 * different bundle. Thus, in case that there is a
1341 			 * station interface connected to an AP on channel 165,
1342 			 * it is assumed that channels 149-161 are allowed for
1343 			 * GO operations. However, having a station interface
1344 			 * connected to an AP on channels 149-161, does not
1345 			 * allow GO operation on channel 165.
1346 			 */
1347 			if (chan->center_freq == 5825 &&
1348 			    other_chan->center_freq != 5825)
1349 				continue;
1350 			return true;
1351 		}
1352 	}
1353 
1354 	return false;
1355 }
1356 
1357 /*
1358  * Check if the channel can be used under permissive conditions mandated by
1359  * some regulatory bodies, i.e., the channel is marked with
1360  * IEEE80211_CHAN_IR_CONCURRENT and there is an additional station interface
1361  * associated to an AP on the same channel or on the same UNII band
1362  * (assuming that the AP is an authorized master).
1363  * In addition allow operation on a channel on which indoor operation is
1364  * allowed, iff we are currently operating in an indoor environment.
1365  */
1366 static bool cfg80211_ir_permissive_chan(struct wiphy *wiphy,
1367 					enum nl80211_iftype iftype,
1368 					struct ieee80211_channel *chan)
1369 {
1370 	struct wireless_dev *wdev;
1371 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1372 
1373 	lockdep_assert_held(&rdev->wiphy.mtx);
1374 
1375 	if (!IS_ENABLED(CONFIG_CFG80211_REG_RELAX_NO_IR) ||
1376 	    !(wiphy->regulatory_flags & REGULATORY_ENABLE_RELAX_NO_IR))
1377 		return false;
1378 
1379 	/* only valid for GO and TDLS off-channel (station/p2p-CL) */
1380 	if (iftype != NL80211_IFTYPE_P2P_GO &&
1381 	    iftype != NL80211_IFTYPE_STATION &&
1382 	    iftype != NL80211_IFTYPE_P2P_CLIENT)
1383 		return false;
1384 
1385 	if (regulatory_indoor_allowed() &&
1386 	    (chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
1387 		return true;
1388 
1389 	if (!(chan->flags & IEEE80211_CHAN_IR_CONCURRENT))
1390 		return false;
1391 
1392 	/*
1393 	 * Generally, it is possible to rely on another device/driver to allow
1394 	 * the IR concurrent relaxation, however, since the device can further
1395 	 * enforce the relaxation (by doing a similar verifications as this),
1396 	 * and thus fail the GO instantiation, consider only the interfaces of
1397 	 * the current registered device.
1398 	 */
1399 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
1400 		bool ret;
1401 
1402 		ret = cfg80211_ir_permissive_check_wdev(iftype, wdev, chan);
1403 		if (ret)
1404 			return ret;
1405 	}
1406 
1407 	return false;
1408 }
1409 
1410 static bool _cfg80211_reg_can_beacon(struct wiphy *wiphy,
1411 				     struct cfg80211_chan_def *chandef,
1412 				     enum nl80211_iftype iftype,
1413 				     bool check_no_ir)
1414 {
1415 	bool res;
1416 	u32 prohibited_flags = IEEE80211_CHAN_DISABLED;
1417 	int dfs_required;
1418 
1419 	trace_cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir);
1420 
1421 	if (check_no_ir)
1422 		prohibited_flags |= IEEE80211_CHAN_NO_IR;
1423 
1424 	dfs_required = cfg80211_chandef_dfs_required(wiphy, chandef, iftype);
1425 	if (dfs_required != 0)
1426 		prohibited_flags |= IEEE80211_CHAN_RADAR;
1427 
1428 	if (dfs_required > 0 &&
1429 	    cfg80211_chandef_dfs_available(wiphy, chandef)) {
1430 		/* We can skip IEEE80211_CHAN_NO_IR if chandef dfs available */
1431 		prohibited_flags = IEEE80211_CHAN_DISABLED;
1432 	}
1433 
1434 	res = cfg80211_chandef_usable(wiphy, chandef, prohibited_flags);
1435 
1436 	trace_cfg80211_return_bool(res);
1437 	return res;
1438 }
1439 
1440 bool cfg80211_reg_can_beacon(struct wiphy *wiphy,
1441 			     struct cfg80211_chan_def *chandef,
1442 			     enum nl80211_iftype iftype)
1443 {
1444 	return _cfg80211_reg_can_beacon(wiphy, chandef, iftype, true);
1445 }
1446 EXPORT_SYMBOL(cfg80211_reg_can_beacon);
1447 
1448 bool cfg80211_reg_can_beacon_relax(struct wiphy *wiphy,
1449 				   struct cfg80211_chan_def *chandef,
1450 				   enum nl80211_iftype iftype)
1451 {
1452 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1453 	bool check_no_ir;
1454 
1455 	lockdep_assert_held(&rdev->wiphy.mtx);
1456 
1457 	/*
1458 	 * Under certain conditions suggested by some regulatory bodies a
1459 	 * GO/STA can IR on channels marked with IEEE80211_NO_IR. Set this flag
1460 	 * only if such relaxations are not enabled and the conditions are not
1461 	 * met.
1462 	 */
1463 	check_no_ir = !cfg80211_ir_permissive_chan(wiphy, iftype,
1464 						   chandef->chan);
1465 
1466 	return _cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir);
1467 }
1468 EXPORT_SYMBOL(cfg80211_reg_can_beacon_relax);
1469 
1470 int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
1471 				 struct cfg80211_chan_def *chandef)
1472 {
1473 	if (!rdev->ops->set_monitor_channel)
1474 		return -EOPNOTSUPP;
1475 	if (!cfg80211_has_monitors_only(rdev))
1476 		return -EBUSY;
1477 
1478 	return rdev_set_monitor_channel(rdev, chandef);
1479 }
1480 
1481 bool cfg80211_any_usable_channels(struct wiphy *wiphy,
1482 				  unsigned long sband_mask,
1483 				  u32 prohibited_flags)
1484 {
1485 	int idx;
1486 
1487 	prohibited_flags |= IEEE80211_CHAN_DISABLED;
1488 
1489 	for_each_set_bit(idx, &sband_mask, NUM_NL80211_BANDS) {
1490 		struct ieee80211_supported_band *sband = wiphy->bands[idx];
1491 		int chanidx;
1492 
1493 		if (!sband)
1494 			continue;
1495 
1496 		for (chanidx = 0; chanidx < sband->n_channels; chanidx++) {
1497 			struct ieee80211_channel *chan;
1498 
1499 			chan = &sband->channels[chanidx];
1500 
1501 			if (chan->flags & prohibited_flags)
1502 				continue;
1503 
1504 			return true;
1505 		}
1506 	}
1507 
1508 	return false;
1509 }
1510 EXPORT_SYMBOL(cfg80211_any_usable_channels);
1511 
1512 struct cfg80211_chan_def *wdev_chandef(struct wireless_dev *wdev,
1513 				       unsigned int link_id)
1514 {
1515 	lockdep_assert_wiphy(wdev->wiphy);
1516 
1517 	WARN_ON(wdev->valid_links && !(wdev->valid_links & BIT(link_id)));
1518 	WARN_ON(!wdev->valid_links && link_id > 0);
1519 
1520 	switch (wdev->iftype) {
1521 	case NL80211_IFTYPE_MESH_POINT:
1522 		return &wdev->u.mesh.chandef;
1523 	case NL80211_IFTYPE_ADHOC:
1524 		return &wdev->u.ibss.chandef;
1525 	case NL80211_IFTYPE_OCB:
1526 		return &wdev->u.ocb.chandef;
1527 	case NL80211_IFTYPE_AP:
1528 	case NL80211_IFTYPE_P2P_GO:
1529 		return &wdev->links[link_id].ap.chandef;
1530 	default:
1531 		return NULL;
1532 	}
1533 }
1534 EXPORT_SYMBOL(wdev_chandef);
1535 
1536 struct cfg80211_per_bw_puncturing_values {
1537 	u8 len;
1538 	const u16 *valid_values;
1539 };
1540 
1541 static const u16 puncturing_values_80mhz[] = {
1542 	0x8, 0x4, 0x2, 0x1
1543 };
1544 
1545 static const u16 puncturing_values_160mhz[] = {
1546 	 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1, 0xc0, 0x30, 0xc, 0x3
1547 };
1548 
1549 static const u16 puncturing_values_320mhz[] = {
1550 	0xc000, 0x3000, 0xc00, 0x300, 0xc0, 0x30, 0xc, 0x3, 0xf000, 0xf00,
1551 	0xf0, 0xf, 0xfc00, 0xf300, 0xf0c0, 0xf030, 0xf00c, 0xf003, 0xc00f,
1552 	0x300f, 0xc0f, 0x30f, 0xcf, 0x3f
1553 };
1554 
1555 #define CFG80211_PER_BW_VALID_PUNCTURING_VALUES(_bw) \
1556 	{ \
1557 		.len = ARRAY_SIZE(puncturing_values_ ## _bw ## mhz), \
1558 		.valid_values = puncturing_values_ ## _bw ## mhz \
1559 	}
1560 
1561 static const struct cfg80211_per_bw_puncturing_values per_bw_puncturing[] = {
1562 	CFG80211_PER_BW_VALID_PUNCTURING_VALUES(80),
1563 	CFG80211_PER_BW_VALID_PUNCTURING_VALUES(160),
1564 	CFG80211_PER_BW_VALID_PUNCTURING_VALUES(320)
1565 };
1566 
1567 bool cfg80211_valid_disable_subchannel_bitmap(u16 *bitmap,
1568 					      const struct cfg80211_chan_def *chandef)
1569 {
1570 	u32 idx, i, start_freq;
1571 
1572 	switch (chandef->width) {
1573 	case NL80211_CHAN_WIDTH_80:
1574 		idx = 0;
1575 		start_freq = chandef->center_freq1 - 40;
1576 		break;
1577 	case NL80211_CHAN_WIDTH_160:
1578 		idx = 1;
1579 		start_freq = chandef->center_freq1 - 80;
1580 		break;
1581 	case NL80211_CHAN_WIDTH_320:
1582 		idx = 2;
1583 		start_freq = chandef->center_freq1 - 160;
1584 		break;
1585 	default:
1586 		*bitmap = 0;
1587 		break;
1588 	}
1589 
1590 	if (!*bitmap)
1591 		return true;
1592 
1593 	/* check if primary channel is punctured */
1594 	if (*bitmap & (u16)BIT((chandef->chan->center_freq - start_freq) / 20))
1595 		return false;
1596 
1597 	for (i = 0; i < per_bw_puncturing[idx].len; i++)
1598 		if (per_bw_puncturing[idx].valid_values[i] == *bitmap)
1599 			return true;
1600 
1601 	return false;
1602 }
1603 EXPORT_SYMBOL(cfg80211_valid_disable_subchannel_bitmap);
1604