Merge tag 'powerpc-6.8-5' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[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
e8c18412 9 * Copyright 2018-2023 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
10fa22b6 144int nl80211_chan_width_to_mhz(enum nl80211_chan_width chan_width)
11b34737
TP
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}
10fa22b6 193EXPORT_SYMBOL(nl80211_chan_width_to_mhz);
11b34737
TP
194
195static int cfg80211_chandef_get_width(const struct cfg80211_chan_def *c)
196{
197 return nl80211_chan_width_to_mhz(c->width);
198}
199
9f5e8f6e 200bool cfg80211_chandef_valid(const struct cfg80211_chan_def *chandef)
3d9d1d66 201{
11b34737
TP
202 u32 control_freq, oper_freq;
203 int oper_width, control_width;
3d9d1d66
JB
204
205 if (!chandef->chan)
206 return false;
207
be689f68
JB
208 if (chandef->freq1_offset >= 1000)
209 return false;
210
3d9d1d66
JB
211 control_freq = chandef->chan->center_freq;
212
213 switch (chandef->width) {
2f301ab2
SW
214 case NL80211_CHAN_WIDTH_5:
215 case NL80211_CHAN_WIDTH_10:
3d9d1d66
JB
216 case NL80211_CHAN_WIDTH_20:
217 case NL80211_CHAN_WIDTH_20_NOHT:
934f4c7d
TP
218 if (ieee80211_chandef_to_khz(chandef) !=
219 ieee80211_channel_to_khz(chandef->chan))
3d9d1d66
JB
220 return false;
221 if (chandef->center_freq2)
222 return false;
223 break;
c1cd35c6 224 case NL80211_CHAN_WIDTH_1:
11b34737
TP
225 case NL80211_CHAN_WIDTH_2:
226 case NL80211_CHAN_WIDTH_4:
227 case NL80211_CHAN_WIDTH_8:
228 case NL80211_CHAN_WIDTH_16:
c1cd35c6
TP
229 if (chandef->chan->band != NL80211_BAND_S1GHZ)
230 return false;
231
11b34737
TP
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;
3d9d1d66 252 case NL80211_CHAN_WIDTH_80P80:
3d9d1d66
JB
253 if (!chandef->center_freq2)
254 return false;
9cab3151
JB
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;
3d9d1d66 259 break;
3bb1ccc4 260 default:
3d9d1d66
JB
261 if (chandef->center_freq2)
262 return false;
263 break;
3bb1ccc4
JB
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 */
3d9d1d66 277 break;
3743bec6
JD
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;
3bb1ccc4
JB
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;
3d9d1d66
JB
307 default:
308 return false;
309 }
310
ec649fed
MH
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
2a38075c
AAL
316 if (cfg80211_chandef_is_edmg(chandef) &&
317 !cfg80211_edmg_chandef_valid(chandef))
318 return false;
319
3d9d1d66
JB
320 return true;
321}
9f5e8f6e 322EXPORT_SYMBOL(cfg80211_chandef_valid);
3d9d1d66
JB
323
324static void chandef_primary_freqs(const struct cfg80211_chan_def *c,
3743bec6 325 u32 *pri40, u32 *pri80, u32 *pri160)
3d9d1d66
JB
326{
327 int tmp;
328
329 switch (c->width) {
330 case NL80211_CHAN_WIDTH_40:
331 *pri40 = c->center_freq1;
332 *pri80 = 0;
3743bec6 333 *pri160 = 0;
3d9d1d66
JB
334 break;
335 case NL80211_CHAN_WIDTH_80:
336 case NL80211_CHAN_WIDTH_80P80:
3743bec6 337 *pri160 = 0;
3d9d1d66
JB
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:
3743bec6 347 *pri160 = c->center_freq1;
3d9d1d66
JB
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;
3743bec6
JD
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;
3d9d1d66
JB
372 default:
373 WARN_ON_ONCE(1);
374 }
375}
376
377const struct cfg80211_chan_def *
378cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1,
379 const struct cfg80211_chan_def *c2)
380{
3743bec6 381 u32 c1_pri40, c1_pri80, c2_pri40, c2_pri80, c1_pri160, c2_pri160;
3d9d1d66
JB
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
2f301ab2
SW
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
3d9d1d66
JB
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
3743bec6
JD
416 chandef_primary_freqs(c1, &c1_pri40, &c1_pri80, &c1_pri160);
417 chandef_primary_freqs(c2, &c2_pri40, &c2_pri80, &c2_pri160);
3d9d1d66
JB
418
419 if (c1_pri40 != c2_pri40)
420 return NULL;
421
3743bec6
JD
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)
3d9d1d66
JB
441 return NULL;
442
443 if (c1->width > c2->width)
444 return c1;
445 return c2;
446}
447EXPORT_SYMBOL(cfg80211_chandef_compatible);
448
04f39047
SW
449static 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
468void 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
40d1ba63
JD
490static u32 cfg80211_get_start_freq(u32 center_freq,
491 u32 bandwidth)
492{
493 u32 start_freq;
494
934f4c7d
TP
495 bandwidth = MHZ_TO_KHZ(bandwidth);
496 if (bandwidth <= MHZ_TO_KHZ(20))
40d1ba63
JD
497 start_freq = center_freq;
498 else
934f4c7d 499 start_freq = center_freq - bandwidth / 2 + MHZ_TO_KHZ(10);
40d1ba63
JD
500
501 return start_freq;
502}
503
504static u32 cfg80211_get_end_freq(u32 center_freq,
505 u32 bandwidth)
506{
507 u32 end_freq;
508
934f4c7d
TP
509 bandwidth = MHZ_TO_KHZ(bandwidth);
510 if (bandwidth <= MHZ_TO_KHZ(20))
40d1ba63
JD
511 end_freq = center_freq;
512 else
934f4c7d 513 end_freq = center_freq + bandwidth / 2 - MHZ_TO_KHZ(10);
40d1ba63
JD
514
515 return end_freq;
516}
517
41a313d8
AO
518static bool
519cfg80211_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 */
560static 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
04f39047
SW
591static int cfg80211_get_chans_dfs_required(struct wiphy *wiphy,
592 u32 center_freq,
41a313d8
AO
593 u32 bandwidth,
594 enum nl80211_iftype iftype)
04f39047
SW
595{
596 struct ieee80211_channel *c;
2f301ab2
SW
597 u32 freq, start_freq, end_freq;
598
40d1ba63
JD
599 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
600 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
04f39047 601
934f4c7d
TP
602 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
603 c = ieee80211_get_channel_khz(wiphy, freq);
04f39047
SW
604 if (!c)
605 return -EINVAL;
606
41a313d8
AO
607 if (c->flags & IEEE80211_CHAN_RADAR &&
608 !cfg80211_dfs_permissive_chan(wiphy, iftype, c))
04f39047
SW
609 return 1;
610 }
41a313d8 611
04f39047
SW
612 return 0;
613}
614
615
616int cfg80211_chandef_dfs_required(struct wiphy *wiphy,
2beb6dab
LC
617 const struct cfg80211_chan_def *chandef,
618 enum nl80211_iftype iftype)
04f39047
SW
619{
620 int width;
2beb6dab 621 int ret;
04f39047
SW
622
623 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
624 return -EINVAL;
625
2beb6dab
LC
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;
04f39047 634
2beb6dab 635 ret = cfg80211_get_chans_dfs_required(wiphy,
934f4c7d 636 ieee80211_chandef_to_khz(chandef),
41a313d8 637 width, iftype);
2beb6dab
LC
638 if (ret < 0)
639 return ret;
640 else if (ret > 0)
641 return BIT(chandef->width);
04f39047 642
2beb6dab
LC
643 if (!chandef->center_freq2)
644 return 0;
645
646 ret = cfg80211_get_chans_dfs_required(wiphy,
934f4c7d 647 MHZ_TO_KHZ(chandef->center_freq2),
41a313d8 648 width, iftype);
2beb6dab
LC
649 if (ret < 0)
650 return ret;
651 else if (ret > 0)
652 return BIT(chandef->width);
04f39047 653
2beb6dab
LC
654 break;
655 case NL80211_IFTYPE_STATION:
6e0bd6c3 656 case NL80211_IFTYPE_OCB:
2beb6dab
LC
657 case NL80211_IFTYPE_P2P_CLIENT:
658 case NL80211_IFTYPE_MONITOR:
659 case NL80211_IFTYPE_AP_VLAN:
2beb6dab 660 case NL80211_IFTYPE_P2P_DEVICE:
cb3b7d87 661 case NL80211_IFTYPE_NAN:
2beb6dab 662 break;
e7e0517c 663 case NL80211_IFTYPE_WDS:
00ec75fc 664 case NL80211_IFTYPE_UNSPECIFIED:
2beb6dab
LC
665 case NUM_NL80211_IFTYPES:
666 WARN_ON(1);
667 }
668
669 return 0;
04f39047 670}
774f0734 671EXPORT_SYMBOL(cfg80211_chandef_dfs_required);
04f39047 672
fe7c3a1f
JD
673static 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 */
934f4c7d
TP
690 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
691 c = ieee80211_get_channel_khz(wiphy, freq);
fe7c3a1f
JD
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
710bool 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
934f4c7d
TP
723 r1 = cfg80211_get_chans_dfs_usable(wiphy,
724 MHZ_TO_KHZ(chandef->center_freq1),
725 width);
fe7c3a1f
JD
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,
934f4c7d
TP
734 MHZ_TO_KHZ(chandef->center_freq2),
735 width);
fe7c3a1f
JD
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}
30ca8b0c 746EXPORT_SYMBOL(cfg80211_chandef_dfs_usable);
fe7c3a1f 747
b35a51c7
VT
748/*
749 * Checks if center frequency of chan falls with in the bandwidth
750 * range of chandef.
751 */
752bool cfg80211_is_sub_chan(struct cfg80211_chan_def *chandef,
7b0a0e3c
JB
753 struct ieee80211_channel *chan,
754 bool primary_only)
b35a51c7
VT
755{
756 int width;
a67a4893 757 u32 freq;
b35a51c7 758
7b0a0e3c
JB
759 if (!chandef->chan)
760 return false;
761
b35a51c7
VT
762 if (chandef->chan->center_freq == chan->center_freq)
763 return true;
764
7b0a0e3c
JB
765 if (primary_only)
766 return false;
767
b35a51c7
VT
768 width = cfg80211_chandef_get_width(chandef);
769 if (width <= 20)
770 return false;
771
b35a51c7
VT
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
790bool cfg80211_beaconing_iface_active(struct wireless_dev *wdev)
791{
7b0a0e3c 792 unsigned int link;
b35a51c7 793
076fc877 794 lockdep_assert_wiphy(wdev->wiphy);
b35a51c7 795
b35a51c7
VT
796 switch (wdev->iftype) {
797 case NL80211_IFTYPE_AP:
798 case NL80211_IFTYPE_P2P_GO:
7b0a0e3c
JB
799 for_each_valid_link(wdev, link) {
800 if (wdev->links[link].ap.beacon_interval)
801 return true;
802 }
b35a51c7
VT
803 break;
804 case NL80211_IFTYPE_ADHOC:
7b0a0e3c
JB
805 if (wdev->u.ibss.ssid_len)
806 return true;
b35a51c7
VT
807 break;
808 case NL80211_IFTYPE_MESH_POINT:
7b0a0e3c
JB
809 if (wdev->u.mesh.id_len)
810 return true;
b35a51c7
VT
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:
b35a51c7
VT
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:
e7e0517c 822 case NL80211_IFTYPE_WDS:
b35a51c7
VT
823 case NUM_NL80211_IFTYPES:
824 WARN_ON(1);
825 }
826
7b0a0e3c
JB
827 return false;
828}
829
830bool 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;
b35a51c7
VT
856}
857
89766727
VT
858static bool cfg80211_is_wiphy_oper_chan(struct wiphy *wiphy,
859 struct ieee80211_channel *chan)
b35a51c7
VT
860{
861 struct wireless_dev *wdev;
862
076fc877
JB
863 lockdep_assert_wiphy(wiphy);
864
b35a51c7 865 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
076fc877 866 if (!cfg80211_beaconing_iface_active(wdev))
b35a51c7 867 continue;
b35a51c7 868
076fc877 869 if (cfg80211_wdev_on_sub_chan(wdev, chan, false))
b35a51c7 870 return true;
b35a51c7
VT
871 }
872
873 return false;
874}
fe7c3a1f 875
84158164
LB
876static bool
877cfg80211_offchan_chain_is_active(struct cfg80211_registered_device *rdev,
878 struct ieee80211_channel *channel)
879{
a95bfb87 880 if (!rdev->background_radar_wdev)
84158164
LB
881 return false;
882
a95bfb87 883 if (!cfg80211_chandef_valid(&rdev->background_radar_chandef))
84158164
LB
884 return false;
885
7b0a0e3c
JB
886 return cfg80211_is_sub_chan(&rdev->background_radar_chandef, channel,
887 false);
84158164
LB
888}
889
89766727
VT
890bool 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
7483a214 900 for_each_rdev(rdev) {
fa8809a5
JB
901 bool found;
902
89766727
VT
903 if (!reg_dfs_domain_same(wiphy, &rdev->wiphy))
904 continue;
905
fa8809a5
JB
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);
84158164 910
fa8809a5 911 if (found)
84158164 912 return true;
89766727
VT
913 }
914
915 return false;
916}
917
6bc54fbc
JD
918static bool cfg80211_get_chans_dfs_available(struct wiphy *wiphy,
919 u32 center_freq,
920 u32 bandwidth)
3d9d1d66
JB
921{
922 struct ieee80211_channel *c;
2f301ab2 923 u32 freq, start_freq, end_freq;
2c390e44
DL
924 bool dfs_offload;
925
926 dfs_offload = wiphy_ext_feature_isset(wiphy,
927 NL80211_EXT_FEATURE_DFS_OFFLOAD);
2f301ab2 928
40d1ba63
JD
929 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
930 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
3d9d1d66 931
6bc54fbc
JD
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 */
934f4c7d
TP
937 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
938 c = ieee80211_get_channel_khz(wiphy, freq);
04f39047
SW
939 if (!c)
940 return false;
941
6bc54fbc
JD
942 if (c->flags & IEEE80211_CHAN_DISABLED)
943 return false;
944
2c390e44
DL
945 if ((c->flags & IEEE80211_CHAN_RADAR) &&
946 (c->dfs_state != NL80211_DFS_AVAILABLE) &&
947 !(c->dfs_state == NL80211_DFS_USABLE && dfs_offload))
04f39047 948 return false;
6bc54fbc
JD
949 }
950
951 return true;
952}
953
954static 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
934f4c7d
TP
967 r = cfg80211_get_chans_dfs_available(wiphy,
968 MHZ_TO_KHZ(chandef->center_freq1),
6bc54fbc
JD
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,
934f4c7d
TP
979 MHZ_TO_KHZ(chandef->center_freq2),
980 width);
680682d4 981 break;
6bc54fbc
JD
982 default:
983 WARN_ON(chandef->center_freq2);
984 break;
985 }
986
987 return r;
988}
989
31559f35
JD
990static 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
934f4c7d
TP
1001 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
1002 c = ieee80211_get_channel_khz(wiphy, freq);
31559f35
JD
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
1019unsigned int
1020cfg80211_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,
934f4c7d 1034 MHZ_TO_KHZ(chandef->center_freq1),
31559f35
JD
1035 width);
1036
1037 if (!chandef->center_freq2)
1038 return t1;
1039
1040 t2 = cfg80211_get_chans_dfs_cac_time(wiphy,
934f4c7d 1041 MHZ_TO_KHZ(chandef->center_freq2),
31559f35
JD
1042 width);
1043
1044 return max(t1, t2);
1045}
30ca8b0c 1046EXPORT_SYMBOL(cfg80211_chandef_dfs_cac_time);
6bc54fbc
JD
1047
1048static 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;
04f39047 1054
6bc54fbc
JD
1055 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
1056 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
1057
934f4c7d
TP
1058 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
1059 c = ieee80211_get_channel_khz(wiphy, freq);
6bc54fbc 1060 if (!c || c->flags & prohibited_flags)
3d9d1d66 1061 return false;
9236d838
LR
1062 }
1063
3d9d1d66
JB
1064 return true;
1065}
1066
2a38075c
AAL
1067/* check if the operating channels are valid and supported */
1068static 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
9f5e8f6e
JB
1120bool cfg80211_chandef_usable(struct wiphy *wiphy,
1121 const struct cfg80211_chan_def *chandef,
1122 u32 prohibited_flags)
3d9d1d66 1123{
9f5e8f6e
JB
1124 struct ieee80211_sta_ht_cap *ht_cap;
1125 struct ieee80211_sta_vht_cap *vht_cap;
2a38075c 1126 struct ieee80211_edmg *edmg_cap;
08f6f147 1127 u32 width, control_freq, cap;
3743bec6
JD
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;
3d9d1d66 1132
9f5e8f6e
JB
1133 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
1134 return false;
3d9d1d66 1135
9f5e8f6e
JB
1136 ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap;
1137 vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap;
2a38075c 1138 edmg_cap = &wiphy->bands[chandef->chan->band]->edmg_cap;
e6ed929b
WG
1139 ext_nss_cap = __le16_to_cpu(vht_cap->vht_mcs.tx_highest) &
1140 IEEE80211_VHT_EXT_NSS_BW_CAPABLE;
2a38075c
AAL
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;
3d9d1d66 1149
9f5e8f6e 1150 control_freq = chandef->chan->center_freq;
9236d838 1151
3d9d1d66 1152 switch (chandef->width) {
df78a0c0
TP
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;
2f301ab2
SW
1168 case NL80211_CHAN_WIDTH_5:
1169 width = 5;
1170 break;
1171 case NL80211_CHAN_WIDTH_10:
ea077c1c 1172 prohibited_flags |= IEEE80211_CHAN_NO_10MHZ;
2f301ab2
SW
1173 width = 10;
1174 break;
3d9d1d66 1175 case NL80211_CHAN_WIDTH_20:
ba8f6a03
JB
1176 if (!ht_cap->ht_supported &&
1177 chandef->chan->band != NL80211_BAND_6GHZ)
9f5e8f6e 1178 return false;
df561f66 1179 fallthrough;
9f5e8f6e 1180 case NL80211_CHAN_WIDTH_20_NOHT:
ea077c1c 1181 prohibited_flags |= IEEE80211_CHAN_NO_20MHZ;
3d9d1d66
JB
1182 width = 20;
1183 break;
1184 case NL80211_CHAN_WIDTH_40:
1185 width = 40;
ba8f6a03
JB
1186 if (chandef->chan->band == NL80211_BAND_6GHZ)
1187 break;
9f5e8f6e
JB
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;
3d9d1d66 1199 break;
3d9d1d66 1200 case NL80211_CHAN_WIDTH_80P80:
35799944
SB
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) ||
e6ed929b
WG
1206 (ext_nss_cap &&
1207 u32_get_bits(cap, IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) > 1);
35799944 1208 if (chandef->chan->band != NL80211_BAND_6GHZ && !support_80_80)
9f5e8f6e 1209 return false;
df561f66 1210 fallthrough;
9f5e8f6e 1211 case NL80211_CHAN_WIDTH_80:
c7a6ee27 1212 prohibited_flags |= IEEE80211_CHAN_NO_80MHZ;
3d9d1d66 1213 width = 80;
ba8f6a03
JB
1214 if (chandef->chan->band == NL80211_BAND_6GHZ)
1215 break;
1216 if (!vht_cap->vht_supported)
1217 return false;
3d9d1d66
JB
1218 break;
1219 case NL80211_CHAN_WIDTH_160:
ba8f6a03
JB
1220 prohibited_flags |= IEEE80211_CHAN_NO_160MHZ;
1221 width = 160;
1222 if (chandef->chan->band == NL80211_BAND_6GHZ)
1223 break;
9f5e8f6e
JB
1224 if (!vht_cap->vht_supported)
1225 return false;
08f6f147
JM
1226 cap = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
1227 if (cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
35799944 1228 cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ &&
e6ed929b
WG
1229 !(ext_nss_cap &&
1230 (vht_cap->cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK)))
9f5e8f6e 1231 return false;
3d9d1d66 1232 break;
3743bec6
JD
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
e8c18412 1244 for_each_sband_iftype_data(sband, i, iftd) {
3743bec6
JD
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;
3d9d1d66
JB
1258 default:
1259 WARN_ON_ONCE(1);
9236d838 1260 return false;
4ee3e063 1261 }
3d9d1d66 1262
c7a6ee27
JB
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 */
9f5e8f6e 1273
a6662dba
JB
1274 if (width > 20)
1275 prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
1276
2f301ab2
SW
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
934f4c7d
TP
1282 if (!cfg80211_secondary_chans_ok(wiphy,
1283 ieee80211_chandef_to_khz(chandef),
9f5e8f6e
JB
1284 width, prohibited_flags))
1285 return false;
1286
1287 if (!chandef->center_freq2)
1288 return true;
934f4c7d
TP
1289 return cfg80211_secondary_chans_ok(wiphy,
1290 MHZ_TO_KHZ(chandef->center_freq2),
9f5e8f6e
JB
1291 width, prohibited_flags);
1292}
1293EXPORT_SYMBOL(cfg80211_chandef_usable);
1294
7b0a0e3c
JB
1295static 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
174e0cd2 1357/*
06f207fc
AN
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
174e0cd2
IP
1361 * associated to an AP on the same channel or on the same UNII band
1362 * (assuming that the AP is an authorized master).
06f207fc 1363 * In addition allow operation on a channel on which indoor operation is
c8866e55 1364 * allowed, iff we are currently operating in an indoor environment.
174e0cd2 1365 */
06f207fc
AN
1366static bool cfg80211_ir_permissive_chan(struct wiphy *wiphy,
1367 enum nl80211_iftype iftype,
174e0cd2
IP
1368 struct ieee80211_channel *chan)
1369{
be69c24a 1370 struct wireless_dev *wdev;
06f207fc 1371 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
174e0cd2 1372
a05829a7 1373 lockdep_assert_held(&rdev->wiphy.mtx);
174e0cd2 1374
97f2645f 1375 if (!IS_ENABLED(CONFIG_CFG80211_REG_RELAX_NO_IR) ||
c8866e55
IP
1376 !(wiphy->regulatory_flags & REGULATORY_ENABLE_RELAX_NO_IR))
1377 return false;
1378
06f207fc
AN
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
c8866e55
IP
1385 if (regulatory_indoor_allowed() &&
1386 (chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
1387 return true;
1388
06f207fc 1389 if (!(chan->flags & IEEE80211_CHAN_IR_CONCURRENT))
174e0cd2
IP
1390 return false;
1391
1392 /*
1393 * Generally, it is possible to rely on another device/driver to allow
06f207fc 1394 * the IR concurrent relaxation, however, since the device can further
174e0cd2
IP
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 */
53873f13 1399 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
7b0a0e3c 1400 bool ret;
174e0cd2 1401
7b0a0e3c 1402 ret = cfg80211_ir_permissive_check_wdev(iftype, wdev, chan);
7b0a0e3c
JB
1403 if (ret)
1404 return ret;
174e0cd2
IP
1405 }
1406
1407 return false;
1408}
1409
923b352f
AN
1410static bool _cfg80211_reg_can_beacon(struct wiphy *wiphy,
1411 struct cfg80211_chan_def *chandef,
1412 enum nl80211_iftype iftype,
1413 bool check_no_ir)
9f5e8f6e
JB
1414{
1415 bool res;
41a313d8
AO
1416 u32 prohibited_flags = IEEE80211_CHAN_DISABLED;
1417 int dfs_required;
9f5e8f6e 1418
923b352f 1419 trace_cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir);
174e0cd2 1420
923b352f 1421 if (check_no_ir)
174e0cd2 1422 prohibited_flags |= IEEE80211_CHAN_NO_IR;
3d9d1d66 1423
41a313d8
AO
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 &&
6bc54fbc
JD
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);
3d9d1d66
JB
1435
1436 trace_cfg80211_return_bool(res);
1437 return res;
9236d838 1438}
923b352f
AN
1439
1440bool 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}
683b6d3b 1446EXPORT_SYMBOL(cfg80211_reg_can_beacon);
9236d838 1447
923b352f
AN
1448bool cfg80211_reg_can_beacon_relax(struct wiphy *wiphy,
1449 struct cfg80211_chan_def *chandef,
1450 enum nl80211_iftype iftype)
1451{
a05829a7 1452 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
923b352f
AN
1453 bool check_no_ir;
1454
a05829a7 1455 lockdep_assert_held(&rdev->wiphy.mtx);
923b352f
AN
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}
1468EXPORT_SYMBOL(cfg80211_reg_can_beacon_relax);
1469
e8c9bd5b 1470int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
683b6d3b 1471 struct cfg80211_chan_def *chandef)
9588bbd5 1472{
e8c9bd5b 1473 if (!rdev->ops->set_monitor_channel)
9588bbd5 1474 return -EOPNOTSUPP;
4f03c1ed
MK
1475 if (!cfg80211_has_monitors_only(rdev))
1476 return -EBUSY;
9588bbd5 1477
683b6d3b 1478 return rdev_set_monitor_channel(rdev, chandef);
59bbb6f7 1479}
26ab9a0c 1480
be989891
JB
1481bool 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}
1510EXPORT_SYMBOL(cfg80211_any_usable_channels);
7b0a0e3c
JB
1511
1512struct cfg80211_chan_def *wdev_chandef(struct wireless_dev *wdev,
1513 unsigned int link_id)
1514{
076fc877
JB
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);
7b0a0e3c
JB
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}
1534EXPORT_SYMBOL(wdev_chandef);
b25413fe
AD
1535
1536struct cfg80211_per_bw_puncturing_values {
1537 u8 len;
1538 const u16 *valid_values;
1539};
1540
1541static const u16 puncturing_values_80mhz[] = {
1542 0x8, 0x4, 0x2, 0x1
1543};
1544
1545static const u16 puncturing_values_160mhz[] = {
1546 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1, 0xc0, 0x30, 0xc, 0x3
1547};
1548
1549static 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
1561static 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
1567bool 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}
1603EXPORT_SYMBOL(cfg80211_valid_disable_subchannel_bitmap);