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