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