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