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