xref: /openbmc/linux/net/wireless/chan.c (revision 884caada)
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       Intel Corporation
10  */
11 
12 #include <linux/export.h>
13 #include <net/cfg80211.h>
14 #include "core.h"
15 #include "rdev-ops.h"
16 
17 static bool cfg80211_valid_60g_freq(u32 freq)
18 {
19 	return freq >= 58320 && freq <= 70200;
20 }
21 
22 void cfg80211_chandef_create(struct cfg80211_chan_def *chandef,
23 			     struct ieee80211_channel *chan,
24 			     enum nl80211_channel_type chan_type)
25 {
26 	if (WARN_ON(!chan))
27 		return;
28 
29 	chandef->chan = chan;
30 	chandef->center_freq2 = 0;
31 	chandef->edmg.bw_config = 0;
32 	chandef->edmg.channels = 0;
33 
34 	switch (chan_type) {
35 	case NL80211_CHAN_NO_HT:
36 		chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
37 		chandef->center_freq1 = chan->center_freq;
38 		break;
39 	case NL80211_CHAN_HT20:
40 		chandef->width = NL80211_CHAN_WIDTH_20;
41 		chandef->center_freq1 = chan->center_freq;
42 		break;
43 	case NL80211_CHAN_HT40PLUS:
44 		chandef->width = NL80211_CHAN_WIDTH_40;
45 		chandef->center_freq1 = chan->center_freq + 10;
46 		break;
47 	case NL80211_CHAN_HT40MINUS:
48 		chandef->width = NL80211_CHAN_WIDTH_40;
49 		chandef->center_freq1 = chan->center_freq - 10;
50 		break;
51 	default:
52 		WARN_ON(1);
53 	}
54 }
55 EXPORT_SYMBOL(cfg80211_chandef_create);
56 
57 static bool cfg80211_edmg_chandef_valid(const struct cfg80211_chan_def *chandef)
58 {
59 	int max_contiguous = 0;
60 	int num_of_enabled = 0;
61 	int contiguous = 0;
62 	int i;
63 
64 	if (!chandef->edmg.channels || !chandef->edmg.bw_config)
65 		return false;
66 
67 	if (!cfg80211_valid_60g_freq(chandef->chan->center_freq))
68 		return false;
69 
70 	for (i = 0; i < 6; i++) {
71 		if (chandef->edmg.channels & BIT(i)) {
72 			contiguous++;
73 			num_of_enabled++;
74 		} else {
75 			contiguous = 0;
76 		}
77 
78 		max_contiguous = max(contiguous, max_contiguous);
79 	}
80 	/* basic verification of edmg configuration according to
81 	 * IEEE P802.11ay/D4.0 section 9.4.2.251
82 	 */
83 	/* check bw_config against contiguous edmg channels */
84 	switch (chandef->edmg.bw_config) {
85 	case IEEE80211_EDMG_BW_CONFIG_4:
86 	case IEEE80211_EDMG_BW_CONFIG_8:
87 	case IEEE80211_EDMG_BW_CONFIG_12:
88 		if (max_contiguous < 1)
89 			return false;
90 		break;
91 	case IEEE80211_EDMG_BW_CONFIG_5:
92 	case IEEE80211_EDMG_BW_CONFIG_9:
93 	case IEEE80211_EDMG_BW_CONFIG_13:
94 		if (max_contiguous < 2)
95 			return false;
96 		break;
97 	case IEEE80211_EDMG_BW_CONFIG_6:
98 	case IEEE80211_EDMG_BW_CONFIG_10:
99 	case IEEE80211_EDMG_BW_CONFIG_14:
100 		if (max_contiguous < 3)
101 			return false;
102 		break;
103 	case IEEE80211_EDMG_BW_CONFIG_7:
104 	case IEEE80211_EDMG_BW_CONFIG_11:
105 	case IEEE80211_EDMG_BW_CONFIG_15:
106 		if (max_contiguous < 4)
107 			return false;
108 		break;
109 
110 	default:
111 		return false;
112 	}
113 
114 	/* check bw_config against aggregated (non contiguous) edmg channels */
115 	switch (chandef->edmg.bw_config) {
116 	case IEEE80211_EDMG_BW_CONFIG_4:
117 	case IEEE80211_EDMG_BW_CONFIG_5:
118 	case IEEE80211_EDMG_BW_CONFIG_6:
119 	case IEEE80211_EDMG_BW_CONFIG_7:
120 		break;
121 	case IEEE80211_EDMG_BW_CONFIG_8:
122 	case IEEE80211_EDMG_BW_CONFIG_9:
123 	case IEEE80211_EDMG_BW_CONFIG_10:
124 	case IEEE80211_EDMG_BW_CONFIG_11:
125 		if (num_of_enabled < 2)
126 			return false;
127 		break;
128 	case IEEE80211_EDMG_BW_CONFIG_12:
129 	case IEEE80211_EDMG_BW_CONFIG_13:
130 	case IEEE80211_EDMG_BW_CONFIG_14:
131 	case IEEE80211_EDMG_BW_CONFIG_15:
132 		if (num_of_enabled < 4 || max_contiguous < 2)
133 			return false;
134 		break;
135 	default:
136 		return false;
137 	}
138 
139 	return true;
140 }
141 
142 bool cfg80211_chandef_valid(const struct cfg80211_chan_def *chandef)
143 {
144 	u32 control_freq;
145 
146 	if (!chandef->chan)
147 		return false;
148 
149 	control_freq = chandef->chan->center_freq;
150 
151 	switch (chandef->width) {
152 	case NL80211_CHAN_WIDTH_5:
153 	case NL80211_CHAN_WIDTH_10:
154 	case NL80211_CHAN_WIDTH_20:
155 	case NL80211_CHAN_WIDTH_20_NOHT:
156 		if (chandef->center_freq1 != control_freq)
157 			return false;
158 		if (chandef->center_freq2)
159 			return false;
160 		break;
161 	case NL80211_CHAN_WIDTH_40:
162 		if (chandef->center_freq1 != control_freq + 10 &&
163 		    chandef->center_freq1 != control_freq - 10)
164 			return false;
165 		if (chandef->center_freq2)
166 			return false;
167 		break;
168 	case NL80211_CHAN_WIDTH_80P80:
169 		if (chandef->center_freq1 != control_freq + 30 &&
170 		    chandef->center_freq1 != control_freq + 10 &&
171 		    chandef->center_freq1 != control_freq - 10 &&
172 		    chandef->center_freq1 != control_freq - 30)
173 			return false;
174 		if (!chandef->center_freq2)
175 			return false;
176 		/* adjacent is not allowed -- that's a 160 MHz channel */
177 		if (chandef->center_freq1 - chandef->center_freq2 == 80 ||
178 		    chandef->center_freq2 - chandef->center_freq1 == 80)
179 			return false;
180 		break;
181 	case NL80211_CHAN_WIDTH_80:
182 		if (chandef->center_freq1 != control_freq + 30 &&
183 		    chandef->center_freq1 != control_freq + 10 &&
184 		    chandef->center_freq1 != control_freq - 10 &&
185 		    chandef->center_freq1 != control_freq - 30)
186 			return false;
187 		if (chandef->center_freq2)
188 			return false;
189 		break;
190 	case NL80211_CHAN_WIDTH_160:
191 		if (chandef->center_freq1 != control_freq + 70 &&
192 		    chandef->center_freq1 != control_freq + 50 &&
193 		    chandef->center_freq1 != control_freq + 30 &&
194 		    chandef->center_freq1 != control_freq + 10 &&
195 		    chandef->center_freq1 != control_freq - 10 &&
196 		    chandef->center_freq1 != control_freq - 30 &&
197 		    chandef->center_freq1 != control_freq - 50 &&
198 		    chandef->center_freq1 != control_freq - 70)
199 			return false;
200 		if (chandef->center_freq2)
201 			return false;
202 		break;
203 	default:
204 		return false;
205 	}
206 
207 	if (cfg80211_chandef_is_edmg(chandef) &&
208 	    !cfg80211_edmg_chandef_valid(chandef))
209 		return false;
210 
211 	return true;
212 }
213 EXPORT_SYMBOL(cfg80211_chandef_valid);
214 
215 static void chandef_primary_freqs(const struct cfg80211_chan_def *c,
216 				  u32 *pri40, u32 *pri80)
217 {
218 	int tmp;
219 
220 	switch (c->width) {
221 	case NL80211_CHAN_WIDTH_40:
222 		*pri40 = c->center_freq1;
223 		*pri80 = 0;
224 		break;
225 	case NL80211_CHAN_WIDTH_80:
226 	case NL80211_CHAN_WIDTH_80P80:
227 		*pri80 = c->center_freq1;
228 		/* n_P20 */
229 		tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
230 		/* n_P40 */
231 		tmp /= 2;
232 		/* freq_P40 */
233 		*pri40 = c->center_freq1 - 20 + 40 * tmp;
234 		break;
235 	case NL80211_CHAN_WIDTH_160:
236 		/* n_P20 */
237 		tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
238 		/* n_P40 */
239 		tmp /= 2;
240 		/* freq_P40 */
241 		*pri40 = c->center_freq1 - 60 + 40 * tmp;
242 		/* n_P80 */
243 		tmp /= 2;
244 		*pri80 = c->center_freq1 - 40 + 80 * tmp;
245 		break;
246 	default:
247 		WARN_ON_ONCE(1);
248 	}
249 }
250 
251 static int cfg80211_chandef_get_width(const struct cfg80211_chan_def *c)
252 {
253 	int width;
254 
255 	switch (c->width) {
256 	case NL80211_CHAN_WIDTH_5:
257 		width = 5;
258 		break;
259 	case NL80211_CHAN_WIDTH_10:
260 		width = 10;
261 		break;
262 	case NL80211_CHAN_WIDTH_20:
263 	case NL80211_CHAN_WIDTH_20_NOHT:
264 		width = 20;
265 		break;
266 	case NL80211_CHAN_WIDTH_40:
267 		width = 40;
268 		break;
269 	case NL80211_CHAN_WIDTH_80P80:
270 	case NL80211_CHAN_WIDTH_80:
271 		width = 80;
272 		break;
273 	case NL80211_CHAN_WIDTH_160:
274 		width = 160;
275 		break;
276 	default:
277 		WARN_ON_ONCE(1);
278 		return -1;
279 	}
280 	return width;
281 }
282 
283 const struct cfg80211_chan_def *
284 cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1,
285 			    const struct cfg80211_chan_def *c2)
286 {
287 	u32 c1_pri40, c1_pri80, c2_pri40, c2_pri80;
288 
289 	/* If they are identical, return */
290 	if (cfg80211_chandef_identical(c1, c2))
291 		return c1;
292 
293 	/* otherwise, must have same control channel */
294 	if (c1->chan != c2->chan)
295 		return NULL;
296 
297 	/*
298 	 * If they have the same width, but aren't identical,
299 	 * then they can't be compatible.
300 	 */
301 	if (c1->width == c2->width)
302 		return NULL;
303 
304 	/*
305 	 * can't be compatible if one of them is 5 or 10 MHz,
306 	 * but they don't have the same width.
307 	 */
308 	if (c1->width == NL80211_CHAN_WIDTH_5 ||
309 	    c1->width == NL80211_CHAN_WIDTH_10 ||
310 	    c2->width == NL80211_CHAN_WIDTH_5 ||
311 	    c2->width == NL80211_CHAN_WIDTH_10)
312 		return NULL;
313 
314 	if (c1->width == NL80211_CHAN_WIDTH_20_NOHT ||
315 	    c1->width == NL80211_CHAN_WIDTH_20)
316 		return c2;
317 
318 	if (c2->width == NL80211_CHAN_WIDTH_20_NOHT ||
319 	    c2->width == NL80211_CHAN_WIDTH_20)
320 		return c1;
321 
322 	chandef_primary_freqs(c1, &c1_pri40, &c1_pri80);
323 	chandef_primary_freqs(c2, &c2_pri40, &c2_pri80);
324 
325 	if (c1_pri40 != c2_pri40)
326 		return NULL;
327 
328 	WARN_ON(!c1_pri80 && !c2_pri80);
329 	if (c1_pri80 && c2_pri80 && c1_pri80 != c2_pri80)
330 		return NULL;
331 
332 	if (c1->width > c2->width)
333 		return c1;
334 	return c2;
335 }
336 EXPORT_SYMBOL(cfg80211_chandef_compatible);
337 
338 static void cfg80211_set_chans_dfs_state(struct wiphy *wiphy, u32 center_freq,
339 					 u32 bandwidth,
340 					 enum nl80211_dfs_state dfs_state)
341 {
342 	struct ieee80211_channel *c;
343 	u32 freq;
344 
345 	for (freq = center_freq - bandwidth/2 + 10;
346 	     freq <= center_freq + bandwidth/2 - 10;
347 	     freq += 20) {
348 		c = ieee80211_get_channel(wiphy, freq);
349 		if (!c || !(c->flags & IEEE80211_CHAN_RADAR))
350 			continue;
351 
352 		c->dfs_state = dfs_state;
353 		c->dfs_state_entered = jiffies;
354 	}
355 }
356 
357 void cfg80211_set_dfs_state(struct wiphy *wiphy,
358 			    const struct cfg80211_chan_def *chandef,
359 			    enum nl80211_dfs_state dfs_state)
360 {
361 	int width;
362 
363 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
364 		return;
365 
366 	width = cfg80211_chandef_get_width(chandef);
367 	if (width < 0)
368 		return;
369 
370 	cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq1,
371 				     width, dfs_state);
372 
373 	if (!chandef->center_freq2)
374 		return;
375 	cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq2,
376 				     width, dfs_state);
377 }
378 
379 static u32 cfg80211_get_start_freq(u32 center_freq,
380 				   u32 bandwidth)
381 {
382 	u32 start_freq;
383 
384 	if (bandwidth <= 20)
385 		start_freq = center_freq;
386 	else
387 		start_freq = center_freq - bandwidth/2 + 10;
388 
389 	return start_freq;
390 }
391 
392 static u32 cfg80211_get_end_freq(u32 center_freq,
393 				 u32 bandwidth)
394 {
395 	u32 end_freq;
396 
397 	if (bandwidth <= 20)
398 		end_freq = center_freq;
399 	else
400 		end_freq = center_freq + bandwidth/2 - 10;
401 
402 	return end_freq;
403 }
404 
405 static int cfg80211_get_chans_dfs_required(struct wiphy *wiphy,
406 					    u32 center_freq,
407 					    u32 bandwidth)
408 {
409 	struct ieee80211_channel *c;
410 	u32 freq, start_freq, end_freq;
411 
412 	start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
413 	end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
414 
415 	for (freq = start_freq; freq <= end_freq; freq += 20) {
416 		c = ieee80211_get_channel(wiphy, freq);
417 		if (!c)
418 			return -EINVAL;
419 
420 		if (c->flags & IEEE80211_CHAN_RADAR)
421 			return 1;
422 	}
423 	return 0;
424 }
425 
426 
427 int cfg80211_chandef_dfs_required(struct wiphy *wiphy,
428 				  const struct cfg80211_chan_def *chandef,
429 				  enum nl80211_iftype iftype)
430 {
431 	int width;
432 	int ret;
433 
434 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
435 		return -EINVAL;
436 
437 	switch (iftype) {
438 	case NL80211_IFTYPE_ADHOC:
439 	case NL80211_IFTYPE_AP:
440 	case NL80211_IFTYPE_P2P_GO:
441 	case NL80211_IFTYPE_MESH_POINT:
442 		width = cfg80211_chandef_get_width(chandef);
443 		if (width < 0)
444 			return -EINVAL;
445 
446 		ret = cfg80211_get_chans_dfs_required(wiphy,
447 						      chandef->center_freq1,
448 						      width);
449 		if (ret < 0)
450 			return ret;
451 		else if (ret > 0)
452 			return BIT(chandef->width);
453 
454 		if (!chandef->center_freq2)
455 			return 0;
456 
457 		ret = cfg80211_get_chans_dfs_required(wiphy,
458 						      chandef->center_freq2,
459 						      width);
460 		if (ret < 0)
461 			return ret;
462 		else if (ret > 0)
463 			return BIT(chandef->width);
464 
465 		break;
466 	case NL80211_IFTYPE_STATION:
467 	case NL80211_IFTYPE_OCB:
468 	case NL80211_IFTYPE_P2P_CLIENT:
469 	case NL80211_IFTYPE_MONITOR:
470 	case NL80211_IFTYPE_AP_VLAN:
471 	case NL80211_IFTYPE_WDS:
472 	case NL80211_IFTYPE_P2P_DEVICE:
473 	case NL80211_IFTYPE_NAN:
474 		break;
475 	case NL80211_IFTYPE_UNSPECIFIED:
476 	case NUM_NL80211_IFTYPES:
477 		WARN_ON(1);
478 	}
479 
480 	return 0;
481 }
482 EXPORT_SYMBOL(cfg80211_chandef_dfs_required);
483 
484 static int cfg80211_get_chans_dfs_usable(struct wiphy *wiphy,
485 					 u32 center_freq,
486 					 u32 bandwidth)
487 {
488 	struct ieee80211_channel *c;
489 	u32 freq, start_freq, end_freq;
490 	int count = 0;
491 
492 	start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
493 	end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
494 
495 	/*
496 	 * Check entire range of channels for the bandwidth.
497 	 * Check all channels are DFS channels (DFS_USABLE or
498 	 * DFS_AVAILABLE). Return number of usable channels
499 	 * (require CAC). Allow DFS and non-DFS channel mix.
500 	 */
501 	for (freq = start_freq; freq <= end_freq; freq += 20) {
502 		c = ieee80211_get_channel(wiphy, freq);
503 		if (!c)
504 			return -EINVAL;
505 
506 		if (c->flags & IEEE80211_CHAN_DISABLED)
507 			return -EINVAL;
508 
509 		if (c->flags & IEEE80211_CHAN_RADAR) {
510 			if (c->dfs_state == NL80211_DFS_UNAVAILABLE)
511 				return -EINVAL;
512 
513 			if (c->dfs_state == NL80211_DFS_USABLE)
514 				count++;
515 		}
516 	}
517 
518 	return count;
519 }
520 
521 bool cfg80211_chandef_dfs_usable(struct wiphy *wiphy,
522 				 const struct cfg80211_chan_def *chandef)
523 {
524 	int width;
525 	int r1, r2 = 0;
526 
527 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
528 		return false;
529 
530 	width = cfg80211_chandef_get_width(chandef);
531 	if (width < 0)
532 		return false;
533 
534 	r1 = cfg80211_get_chans_dfs_usable(wiphy, chandef->center_freq1,
535 					  width);
536 
537 	if (r1 < 0)
538 		return false;
539 
540 	switch (chandef->width) {
541 	case NL80211_CHAN_WIDTH_80P80:
542 		WARN_ON(!chandef->center_freq2);
543 		r2 = cfg80211_get_chans_dfs_usable(wiphy,
544 						   chandef->center_freq2,
545 						   width);
546 		if (r2 < 0)
547 			return false;
548 		break;
549 	default:
550 		WARN_ON(chandef->center_freq2);
551 		break;
552 	}
553 
554 	return (r1 + r2 > 0);
555 }
556 
557 /*
558  * Checks if center frequency of chan falls with in the bandwidth
559  * range of chandef.
560  */
561 bool cfg80211_is_sub_chan(struct cfg80211_chan_def *chandef,
562 			  struct ieee80211_channel *chan)
563 {
564 	int width;
565 	u32 freq;
566 
567 	if (chandef->chan->center_freq == chan->center_freq)
568 		return true;
569 
570 	width = cfg80211_chandef_get_width(chandef);
571 	if (width <= 20)
572 		return false;
573 
574 	for (freq = chandef->center_freq1 - width / 2 + 10;
575 	     freq <= chandef->center_freq1 + width / 2 - 10; freq += 20) {
576 		if (chan->center_freq == freq)
577 			return true;
578 	}
579 
580 	if (!chandef->center_freq2)
581 		return false;
582 
583 	for (freq = chandef->center_freq2 - width / 2 + 10;
584 	     freq <= chandef->center_freq2 + width / 2 - 10; freq += 20) {
585 		if (chan->center_freq == freq)
586 			return true;
587 	}
588 
589 	return false;
590 }
591 
592 bool cfg80211_beaconing_iface_active(struct wireless_dev *wdev)
593 {
594 	bool active = false;
595 
596 	ASSERT_WDEV_LOCK(wdev);
597 
598 	if (!wdev->chandef.chan)
599 		return false;
600 
601 	switch (wdev->iftype) {
602 	case NL80211_IFTYPE_AP:
603 	case NL80211_IFTYPE_P2P_GO:
604 		active = wdev->beacon_interval != 0;
605 		break;
606 	case NL80211_IFTYPE_ADHOC:
607 		active = wdev->ssid_len != 0;
608 		break;
609 	case NL80211_IFTYPE_MESH_POINT:
610 		active = wdev->mesh_id_len != 0;
611 		break;
612 	case NL80211_IFTYPE_STATION:
613 	case NL80211_IFTYPE_OCB:
614 	case NL80211_IFTYPE_P2P_CLIENT:
615 	case NL80211_IFTYPE_MONITOR:
616 	case NL80211_IFTYPE_AP_VLAN:
617 	case NL80211_IFTYPE_WDS:
618 	case NL80211_IFTYPE_P2P_DEVICE:
619 	/* Can NAN type be considered as beaconing interface? */
620 	case NL80211_IFTYPE_NAN:
621 		break;
622 	case NL80211_IFTYPE_UNSPECIFIED:
623 	case NUM_NL80211_IFTYPES:
624 		WARN_ON(1);
625 	}
626 
627 	return active;
628 }
629 
630 static bool cfg80211_is_wiphy_oper_chan(struct wiphy *wiphy,
631 					struct ieee80211_channel *chan)
632 {
633 	struct wireless_dev *wdev;
634 
635 	list_for_each_entry(wdev, &wiphy->wdev_list, list) {
636 		wdev_lock(wdev);
637 		if (!cfg80211_beaconing_iface_active(wdev)) {
638 			wdev_unlock(wdev);
639 			continue;
640 		}
641 
642 		if (cfg80211_is_sub_chan(&wdev->chandef, chan)) {
643 			wdev_unlock(wdev);
644 			return true;
645 		}
646 		wdev_unlock(wdev);
647 	}
648 
649 	return false;
650 }
651 
652 bool cfg80211_any_wiphy_oper_chan(struct wiphy *wiphy,
653 				  struct ieee80211_channel *chan)
654 {
655 	struct cfg80211_registered_device *rdev;
656 
657 	ASSERT_RTNL();
658 
659 	if (!(chan->flags & IEEE80211_CHAN_RADAR))
660 		return false;
661 
662 	list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
663 		if (!reg_dfs_domain_same(wiphy, &rdev->wiphy))
664 			continue;
665 
666 		if (cfg80211_is_wiphy_oper_chan(&rdev->wiphy, chan))
667 			return true;
668 	}
669 
670 	return false;
671 }
672 
673 static bool cfg80211_get_chans_dfs_available(struct wiphy *wiphy,
674 					     u32 center_freq,
675 					     u32 bandwidth)
676 {
677 	struct ieee80211_channel *c;
678 	u32 freq, start_freq, end_freq;
679 	bool dfs_offload;
680 
681 	dfs_offload = wiphy_ext_feature_isset(wiphy,
682 					      NL80211_EXT_FEATURE_DFS_OFFLOAD);
683 
684 	start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
685 	end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
686 
687 	/*
688 	 * Check entire range of channels for the bandwidth.
689 	 * If any channel in between is disabled or has not
690 	 * had gone through CAC return false
691 	 */
692 	for (freq = start_freq; freq <= end_freq; freq += 20) {
693 		c = ieee80211_get_channel(wiphy, freq);
694 		if (!c)
695 			return false;
696 
697 		if (c->flags & IEEE80211_CHAN_DISABLED)
698 			return false;
699 
700 		if ((c->flags & IEEE80211_CHAN_RADAR) &&
701 		    (c->dfs_state != NL80211_DFS_AVAILABLE) &&
702 		    !(c->dfs_state == NL80211_DFS_USABLE && dfs_offload))
703 			return false;
704 	}
705 
706 	return true;
707 }
708 
709 static bool cfg80211_chandef_dfs_available(struct wiphy *wiphy,
710 				const struct cfg80211_chan_def *chandef)
711 {
712 	int width;
713 	int r;
714 
715 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
716 		return false;
717 
718 	width = cfg80211_chandef_get_width(chandef);
719 	if (width < 0)
720 		return false;
721 
722 	r = cfg80211_get_chans_dfs_available(wiphy, chandef->center_freq1,
723 					     width);
724 
725 	/* If any of channels unavailable for cf1 just return */
726 	if (!r)
727 		return r;
728 
729 	switch (chandef->width) {
730 	case NL80211_CHAN_WIDTH_80P80:
731 		WARN_ON(!chandef->center_freq2);
732 		r = cfg80211_get_chans_dfs_available(wiphy,
733 						     chandef->center_freq2,
734 						     width);
735 		break;
736 	default:
737 		WARN_ON(chandef->center_freq2);
738 		break;
739 	}
740 
741 	return r;
742 }
743 
744 static unsigned int cfg80211_get_chans_dfs_cac_time(struct wiphy *wiphy,
745 						    u32 center_freq,
746 						    u32 bandwidth)
747 {
748 	struct ieee80211_channel *c;
749 	u32 start_freq, end_freq, freq;
750 	unsigned int dfs_cac_ms = 0;
751 
752 	start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
753 	end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
754 
755 	for (freq = start_freq; freq <= end_freq; freq += 20) {
756 		c = ieee80211_get_channel(wiphy, freq);
757 		if (!c)
758 			return 0;
759 
760 		if (c->flags & IEEE80211_CHAN_DISABLED)
761 			return 0;
762 
763 		if (!(c->flags & IEEE80211_CHAN_RADAR))
764 			continue;
765 
766 		if (c->dfs_cac_ms > dfs_cac_ms)
767 			dfs_cac_ms = c->dfs_cac_ms;
768 	}
769 
770 	return dfs_cac_ms;
771 }
772 
773 unsigned int
774 cfg80211_chandef_dfs_cac_time(struct wiphy *wiphy,
775 			      const struct cfg80211_chan_def *chandef)
776 {
777 	int width;
778 	unsigned int t1 = 0, t2 = 0;
779 
780 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
781 		return 0;
782 
783 	width = cfg80211_chandef_get_width(chandef);
784 	if (width < 0)
785 		return 0;
786 
787 	t1 = cfg80211_get_chans_dfs_cac_time(wiphy,
788 					     chandef->center_freq1,
789 					     width);
790 
791 	if (!chandef->center_freq2)
792 		return t1;
793 
794 	t2 = cfg80211_get_chans_dfs_cac_time(wiphy,
795 					     chandef->center_freq2,
796 					     width);
797 
798 	return max(t1, t2);
799 }
800 
801 static bool cfg80211_secondary_chans_ok(struct wiphy *wiphy,
802 					u32 center_freq, u32 bandwidth,
803 					u32 prohibited_flags)
804 {
805 	struct ieee80211_channel *c;
806 	u32 freq, start_freq, end_freq;
807 
808 	start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
809 	end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
810 
811 	for (freq = start_freq; freq <= end_freq; freq += 20) {
812 		c = ieee80211_get_channel(wiphy, freq);
813 		if (!c || c->flags & prohibited_flags)
814 			return false;
815 	}
816 
817 	return true;
818 }
819 
820 /* check if the operating channels are valid and supported */
821 static bool cfg80211_edmg_usable(struct wiphy *wiphy, u8 edmg_channels,
822 				 enum ieee80211_edmg_bw_config edmg_bw_config,
823 				 int primary_channel,
824 				 struct ieee80211_edmg *edmg_cap)
825 {
826 	struct ieee80211_channel *chan;
827 	int i, freq;
828 	int channels_counter = 0;
829 
830 	if (!edmg_channels && !edmg_bw_config)
831 		return true;
832 
833 	if ((!edmg_channels && edmg_bw_config) ||
834 	    (edmg_channels && !edmg_bw_config))
835 		return false;
836 
837 	if (!(edmg_channels & BIT(primary_channel - 1)))
838 		return false;
839 
840 	/* 60GHz channels 1..6 */
841 	for (i = 0; i < 6; i++) {
842 		if (!(edmg_channels & BIT(i)))
843 			continue;
844 
845 		if (!(edmg_cap->channels & BIT(i)))
846 			return false;
847 
848 		channels_counter++;
849 
850 		freq = ieee80211_channel_to_frequency(i + 1,
851 						      NL80211_BAND_60GHZ);
852 		chan = ieee80211_get_channel(wiphy, freq);
853 		if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
854 			return false;
855 	}
856 
857 	/* IEEE802.11 allows max 4 channels */
858 	if (channels_counter > 4)
859 		return false;
860 
861 	/* check bw_config is a subset of what driver supports
862 	 * (see IEEE P802.11ay/D4.0 section 9.4.2.251, Table 13)
863 	 */
864 	if ((edmg_bw_config % 4) > (edmg_cap->bw_config % 4))
865 		return false;
866 
867 	if (edmg_bw_config > edmg_cap->bw_config)
868 		return false;
869 
870 	return true;
871 }
872 
873 bool cfg80211_chandef_usable(struct wiphy *wiphy,
874 			     const struct cfg80211_chan_def *chandef,
875 			     u32 prohibited_flags)
876 {
877 	struct ieee80211_sta_ht_cap *ht_cap;
878 	struct ieee80211_sta_vht_cap *vht_cap;
879 	struct ieee80211_edmg *edmg_cap;
880 	u32 width, control_freq, cap;
881 
882 	if (WARN_ON(!cfg80211_chandef_valid(chandef)))
883 		return false;
884 
885 	ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap;
886 	vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap;
887 	edmg_cap = &wiphy->bands[chandef->chan->band]->edmg_cap;
888 
889 	if (edmg_cap->channels &&
890 	    !cfg80211_edmg_usable(wiphy,
891 				  chandef->edmg.channels,
892 				  chandef->edmg.bw_config,
893 				  chandef->chan->hw_value,
894 				  edmg_cap))
895 		return false;
896 
897 	control_freq = chandef->chan->center_freq;
898 
899 	switch (chandef->width) {
900 	case NL80211_CHAN_WIDTH_5:
901 		width = 5;
902 		break;
903 	case NL80211_CHAN_WIDTH_10:
904 		prohibited_flags |= IEEE80211_CHAN_NO_10MHZ;
905 		width = 10;
906 		break;
907 	case NL80211_CHAN_WIDTH_20:
908 		if (!ht_cap->ht_supported)
909 			return false;
910 		/* fall through */
911 	case NL80211_CHAN_WIDTH_20_NOHT:
912 		prohibited_flags |= IEEE80211_CHAN_NO_20MHZ;
913 		width = 20;
914 		break;
915 	case NL80211_CHAN_WIDTH_40:
916 		width = 40;
917 		if (!ht_cap->ht_supported)
918 			return false;
919 		if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
920 		    ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)
921 			return false;
922 		if (chandef->center_freq1 < control_freq &&
923 		    chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
924 			return false;
925 		if (chandef->center_freq1 > control_freq &&
926 		    chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
927 			return false;
928 		break;
929 	case NL80211_CHAN_WIDTH_80P80:
930 		cap = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
931 		if (cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
932 			return false;
933 		/* fall through */
934 	case NL80211_CHAN_WIDTH_80:
935 		if (!vht_cap->vht_supported)
936 			return false;
937 		prohibited_flags |= IEEE80211_CHAN_NO_80MHZ;
938 		width = 80;
939 		break;
940 	case NL80211_CHAN_WIDTH_160:
941 		if (!vht_cap->vht_supported)
942 			return false;
943 		cap = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
944 		if (cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
945 		    cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
946 			return false;
947 		prohibited_flags |= IEEE80211_CHAN_NO_160MHZ;
948 		width = 160;
949 		break;
950 	default:
951 		WARN_ON_ONCE(1);
952 		return false;
953 	}
954 
955 	/*
956 	 * TODO: What if there are only certain 80/160/80+80 MHz channels
957 	 *	 allowed by the driver, or only certain combinations?
958 	 *	 For 40 MHz the driver can set the NO_HT40 flags, but for
959 	 *	 80/160 MHz and in particular 80+80 MHz this isn't really
960 	 *	 feasible and we only have NO_80MHZ/NO_160MHZ so far but
961 	 *	 no way to cover 80+80 MHz or more complex restrictions.
962 	 *	 Note that such restrictions also need to be advertised to
963 	 *	 userspace, for example for P2P channel selection.
964 	 */
965 
966 	if (width > 20)
967 		prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
968 
969 	/* 5 and 10 MHz are only defined for the OFDM PHY */
970 	if (width < 20)
971 		prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
972 
973 
974 	if (!cfg80211_secondary_chans_ok(wiphy, chandef->center_freq1,
975 					 width, prohibited_flags))
976 		return false;
977 
978 	if (!chandef->center_freq2)
979 		return true;
980 	return cfg80211_secondary_chans_ok(wiphy, chandef->center_freq2,
981 					   width, prohibited_flags);
982 }
983 EXPORT_SYMBOL(cfg80211_chandef_usable);
984 
985 /*
986  * Check if the channel can be used under permissive conditions mandated by
987  * some regulatory bodies, i.e., the channel is marked with
988  * IEEE80211_CHAN_IR_CONCURRENT and there is an additional station interface
989  * associated to an AP on the same channel or on the same UNII band
990  * (assuming that the AP is an authorized master).
991  * In addition allow operation on a channel on which indoor operation is
992  * allowed, iff we are currently operating in an indoor environment.
993  */
994 static bool cfg80211_ir_permissive_chan(struct wiphy *wiphy,
995 					enum nl80211_iftype iftype,
996 					struct ieee80211_channel *chan)
997 {
998 	struct wireless_dev *wdev;
999 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1000 
1001 	ASSERT_RTNL();
1002 
1003 	if (!IS_ENABLED(CONFIG_CFG80211_REG_RELAX_NO_IR) ||
1004 	    !(wiphy->regulatory_flags & REGULATORY_ENABLE_RELAX_NO_IR))
1005 		return false;
1006 
1007 	/* only valid for GO and TDLS off-channel (station/p2p-CL) */
1008 	if (iftype != NL80211_IFTYPE_P2P_GO &&
1009 	    iftype != NL80211_IFTYPE_STATION &&
1010 	    iftype != NL80211_IFTYPE_P2P_CLIENT)
1011 		return false;
1012 
1013 	if (regulatory_indoor_allowed() &&
1014 	    (chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
1015 		return true;
1016 
1017 	if (!(chan->flags & IEEE80211_CHAN_IR_CONCURRENT))
1018 		return false;
1019 
1020 	/*
1021 	 * Generally, it is possible to rely on another device/driver to allow
1022 	 * the IR concurrent relaxation, however, since the device can further
1023 	 * enforce the relaxation (by doing a similar verifications as this),
1024 	 * and thus fail the GO instantiation, consider only the interfaces of
1025 	 * the current registered device.
1026 	 */
1027 	list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
1028 		struct ieee80211_channel *other_chan = NULL;
1029 		int r1, r2;
1030 
1031 		wdev_lock(wdev);
1032 		if (wdev->iftype == NL80211_IFTYPE_STATION &&
1033 		    wdev->current_bss)
1034 			other_chan = wdev->current_bss->pub.channel;
1035 
1036 		/*
1037 		 * If a GO already operates on the same GO_CONCURRENT channel,
1038 		 * this one (maybe the same one) can beacon as well. We allow
1039 		 * the operation even if the station we relied on with
1040 		 * GO_CONCURRENT is disconnected now. But then we must make sure
1041 		 * we're not outdoor on an indoor-only channel.
1042 		 */
1043 		if (iftype == NL80211_IFTYPE_P2P_GO &&
1044 		    wdev->iftype == NL80211_IFTYPE_P2P_GO &&
1045 		    wdev->beacon_interval &&
1046 		    !(chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
1047 			other_chan = wdev->chandef.chan;
1048 		wdev_unlock(wdev);
1049 
1050 		if (!other_chan)
1051 			continue;
1052 
1053 		if (chan == other_chan)
1054 			return true;
1055 
1056 		if (chan->band != NL80211_BAND_5GHZ &&
1057 		    chan->band != NL80211_BAND_6GHZ)
1058 			continue;
1059 
1060 		r1 = cfg80211_get_unii(chan->center_freq);
1061 		r2 = cfg80211_get_unii(other_chan->center_freq);
1062 
1063 		if (r1 != -EINVAL && r1 == r2) {
1064 			/*
1065 			 * At some locations channels 149-165 are considered a
1066 			 * bundle, but at other locations, e.g., Indonesia,
1067 			 * channels 149-161 are considered a bundle while
1068 			 * channel 165 is left out and considered to be in a
1069 			 * different bundle. Thus, in case that there is a
1070 			 * station interface connected to an AP on channel 165,
1071 			 * it is assumed that channels 149-161 are allowed for
1072 			 * GO operations. However, having a station interface
1073 			 * connected to an AP on channels 149-161, does not
1074 			 * allow GO operation on channel 165.
1075 			 */
1076 			if (chan->center_freq == 5825 &&
1077 			    other_chan->center_freq != 5825)
1078 				continue;
1079 			return true;
1080 		}
1081 	}
1082 
1083 	return false;
1084 }
1085 
1086 static bool _cfg80211_reg_can_beacon(struct wiphy *wiphy,
1087 				     struct cfg80211_chan_def *chandef,
1088 				     enum nl80211_iftype iftype,
1089 				     bool check_no_ir)
1090 {
1091 	bool res;
1092 	u32 prohibited_flags = IEEE80211_CHAN_DISABLED |
1093 			       IEEE80211_CHAN_RADAR;
1094 
1095 	trace_cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir);
1096 
1097 	if (check_no_ir)
1098 		prohibited_flags |= IEEE80211_CHAN_NO_IR;
1099 
1100 	if (cfg80211_chandef_dfs_required(wiphy, chandef, iftype) > 0 &&
1101 	    cfg80211_chandef_dfs_available(wiphy, chandef)) {
1102 		/* We can skip IEEE80211_CHAN_NO_IR if chandef dfs available */
1103 		prohibited_flags = IEEE80211_CHAN_DISABLED;
1104 	}
1105 
1106 	res = cfg80211_chandef_usable(wiphy, chandef, prohibited_flags);
1107 
1108 	trace_cfg80211_return_bool(res);
1109 	return res;
1110 }
1111 
1112 bool cfg80211_reg_can_beacon(struct wiphy *wiphy,
1113 			     struct cfg80211_chan_def *chandef,
1114 			     enum nl80211_iftype iftype)
1115 {
1116 	return _cfg80211_reg_can_beacon(wiphy, chandef, iftype, true);
1117 }
1118 EXPORT_SYMBOL(cfg80211_reg_can_beacon);
1119 
1120 bool cfg80211_reg_can_beacon_relax(struct wiphy *wiphy,
1121 				   struct cfg80211_chan_def *chandef,
1122 				   enum nl80211_iftype iftype)
1123 {
1124 	bool check_no_ir;
1125 
1126 	ASSERT_RTNL();
1127 
1128 	/*
1129 	 * Under certain conditions suggested by some regulatory bodies a
1130 	 * GO/STA can IR on channels marked with IEEE80211_NO_IR. Set this flag
1131 	 * only if such relaxations are not enabled and the conditions are not
1132 	 * met.
1133 	 */
1134 	check_no_ir = !cfg80211_ir_permissive_chan(wiphy, iftype,
1135 						   chandef->chan);
1136 
1137 	return _cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir);
1138 }
1139 EXPORT_SYMBOL(cfg80211_reg_can_beacon_relax);
1140 
1141 int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
1142 				 struct cfg80211_chan_def *chandef)
1143 {
1144 	if (!rdev->ops->set_monitor_channel)
1145 		return -EOPNOTSUPP;
1146 	if (!cfg80211_has_monitors_only(rdev))
1147 		return -EBUSY;
1148 
1149 	return rdev_set_monitor_channel(rdev, chandef);
1150 }
1151 
1152 void
1153 cfg80211_get_chan_state(struct wireless_dev *wdev,
1154 		        struct ieee80211_channel **chan,
1155 		        enum cfg80211_chan_mode *chanmode,
1156 		        u8 *radar_detect)
1157 {
1158 	int ret;
1159 
1160 	*chan = NULL;
1161 	*chanmode = CHAN_MODE_UNDEFINED;
1162 
1163 	ASSERT_WDEV_LOCK(wdev);
1164 
1165 	if (wdev->netdev && !netif_running(wdev->netdev))
1166 		return;
1167 
1168 	switch (wdev->iftype) {
1169 	case NL80211_IFTYPE_ADHOC:
1170 		if (wdev->current_bss) {
1171 			*chan = wdev->current_bss->pub.channel;
1172 			*chanmode = (wdev->ibss_fixed &&
1173 				     !wdev->ibss_dfs_possible)
1174 				  ? CHAN_MODE_SHARED
1175 				  : CHAN_MODE_EXCLUSIVE;
1176 
1177 			/* consider worst-case - IBSS can try to return to the
1178 			 * original user-specified channel as creator */
1179 			if (wdev->ibss_dfs_possible)
1180 				*radar_detect |= BIT(wdev->chandef.width);
1181 			return;
1182 		}
1183 		break;
1184 	case NL80211_IFTYPE_STATION:
1185 	case NL80211_IFTYPE_P2P_CLIENT:
1186 		if (wdev->current_bss) {
1187 			*chan = wdev->current_bss->pub.channel;
1188 			*chanmode = CHAN_MODE_SHARED;
1189 			return;
1190 		}
1191 		break;
1192 	case NL80211_IFTYPE_AP:
1193 	case NL80211_IFTYPE_P2P_GO:
1194 		if (wdev->cac_started) {
1195 			*chan = wdev->chandef.chan;
1196 			*chanmode = CHAN_MODE_SHARED;
1197 			*radar_detect |= BIT(wdev->chandef.width);
1198 		} else if (wdev->beacon_interval) {
1199 			*chan = wdev->chandef.chan;
1200 			*chanmode = CHAN_MODE_SHARED;
1201 
1202 			ret = cfg80211_chandef_dfs_required(wdev->wiphy,
1203 							    &wdev->chandef,
1204 							    wdev->iftype);
1205 			WARN_ON(ret < 0);
1206 			if (ret > 0)
1207 				*radar_detect |= BIT(wdev->chandef.width);
1208 		}
1209 		return;
1210 	case NL80211_IFTYPE_MESH_POINT:
1211 		if (wdev->mesh_id_len) {
1212 			*chan = wdev->chandef.chan;
1213 			*chanmode = CHAN_MODE_SHARED;
1214 
1215 			ret = cfg80211_chandef_dfs_required(wdev->wiphy,
1216 							    &wdev->chandef,
1217 							    wdev->iftype);
1218 			WARN_ON(ret < 0);
1219 			if (ret > 0)
1220 				*radar_detect |= BIT(wdev->chandef.width);
1221 		}
1222 		return;
1223 	case NL80211_IFTYPE_OCB:
1224 		if (wdev->chandef.chan) {
1225 			*chan = wdev->chandef.chan;
1226 			*chanmode = CHAN_MODE_SHARED;
1227 			return;
1228 		}
1229 		break;
1230 	case NL80211_IFTYPE_MONITOR:
1231 	case NL80211_IFTYPE_AP_VLAN:
1232 	case NL80211_IFTYPE_WDS:
1233 	case NL80211_IFTYPE_P2P_DEVICE:
1234 	case NL80211_IFTYPE_NAN:
1235 		/* these interface types don't really have a channel */
1236 		return;
1237 	case NL80211_IFTYPE_UNSPECIFIED:
1238 	case NUM_NL80211_IFTYPES:
1239 		WARN_ON(1);
1240 	}
1241 }
1242