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-2020 Intel Corporation
10 */
11
12 #include <linux/export.h>
13 #include <net/cfg80211.h>
14 #include "core.h"
15 #include "rdev-ops.h"
16
cfg80211_valid_60g_freq(u32 freq)17 static bool cfg80211_valid_60g_freq(u32 freq)
18 {
19 return freq >= 58320 && freq <= 70200;
20 }
21
cfg80211_chandef_create(struct cfg80211_chan_def * chandef,struct ieee80211_channel * chan,enum nl80211_channel_type chan_type)22 void cfg80211_chandef_create(struct cfg80211_chan_def *chandef,
23 struct ieee80211_channel *chan,
24 enum nl80211_channel_type chan_type)
25 {
26 if (WARN_ON(!chan))
27 return;
28
29 chandef->chan = chan;
30 chandef->freq1_offset = chan->freq_offset;
31 chandef->center_freq2 = 0;
32 chandef->edmg.bw_config = 0;
33 chandef->edmg.channels = 0;
34
35 switch (chan_type) {
36 case NL80211_CHAN_NO_HT:
37 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
38 chandef->center_freq1 = chan->center_freq;
39 break;
40 case NL80211_CHAN_HT20:
41 chandef->width = NL80211_CHAN_WIDTH_20;
42 chandef->center_freq1 = chan->center_freq;
43 break;
44 case NL80211_CHAN_HT40PLUS:
45 chandef->width = NL80211_CHAN_WIDTH_40;
46 chandef->center_freq1 = chan->center_freq + 10;
47 break;
48 case NL80211_CHAN_HT40MINUS:
49 chandef->width = NL80211_CHAN_WIDTH_40;
50 chandef->center_freq1 = chan->center_freq - 10;
51 break;
52 default:
53 WARN_ON(1);
54 }
55 }
56 EXPORT_SYMBOL(cfg80211_chandef_create);
57
cfg80211_edmg_chandef_valid(const struct cfg80211_chan_def * chandef)58 static bool cfg80211_edmg_chandef_valid(const struct cfg80211_chan_def *chandef)
59 {
60 int max_contiguous = 0;
61 int num_of_enabled = 0;
62 int contiguous = 0;
63 int i;
64
65 if (!chandef->edmg.channels || !chandef->edmg.bw_config)
66 return false;
67
68 if (!cfg80211_valid_60g_freq(chandef->chan->center_freq))
69 return false;
70
71 for (i = 0; i < 6; i++) {
72 if (chandef->edmg.channels & BIT(i)) {
73 contiguous++;
74 num_of_enabled++;
75 } else {
76 contiguous = 0;
77 }
78
79 max_contiguous = max(contiguous, max_contiguous);
80 }
81 /* basic verification of edmg configuration according to
82 * IEEE P802.11ay/D4.0 section 9.4.2.251
83 */
84 /* check bw_config against contiguous edmg channels */
85 switch (chandef->edmg.bw_config) {
86 case IEEE80211_EDMG_BW_CONFIG_4:
87 case IEEE80211_EDMG_BW_CONFIG_8:
88 case IEEE80211_EDMG_BW_CONFIG_12:
89 if (max_contiguous < 1)
90 return false;
91 break;
92 case IEEE80211_EDMG_BW_CONFIG_5:
93 case IEEE80211_EDMG_BW_CONFIG_9:
94 case IEEE80211_EDMG_BW_CONFIG_13:
95 if (max_contiguous < 2)
96 return false;
97 break;
98 case IEEE80211_EDMG_BW_CONFIG_6:
99 case IEEE80211_EDMG_BW_CONFIG_10:
100 case IEEE80211_EDMG_BW_CONFIG_14:
101 if (max_contiguous < 3)
102 return false;
103 break;
104 case IEEE80211_EDMG_BW_CONFIG_7:
105 case IEEE80211_EDMG_BW_CONFIG_11:
106 case IEEE80211_EDMG_BW_CONFIG_15:
107 if (max_contiguous < 4)
108 return false;
109 break;
110
111 default:
112 return false;
113 }
114
115 /* check bw_config against aggregated (non contiguous) edmg channels */
116 switch (chandef->edmg.bw_config) {
117 case IEEE80211_EDMG_BW_CONFIG_4:
118 case IEEE80211_EDMG_BW_CONFIG_5:
119 case IEEE80211_EDMG_BW_CONFIG_6:
120 case IEEE80211_EDMG_BW_CONFIG_7:
121 break;
122 case IEEE80211_EDMG_BW_CONFIG_8:
123 case IEEE80211_EDMG_BW_CONFIG_9:
124 case IEEE80211_EDMG_BW_CONFIG_10:
125 case IEEE80211_EDMG_BW_CONFIG_11:
126 if (num_of_enabled < 2)
127 return false;
128 break;
129 case IEEE80211_EDMG_BW_CONFIG_12:
130 case IEEE80211_EDMG_BW_CONFIG_13:
131 case IEEE80211_EDMG_BW_CONFIG_14:
132 case IEEE80211_EDMG_BW_CONFIG_15:
133 if (num_of_enabled < 4 || max_contiguous < 2)
134 return false;
135 break;
136 default:
137 return false;
138 }
139
140 return true;
141 }
142
cfg80211_chandef_valid(const struct cfg80211_chan_def * chandef)143 bool cfg80211_chandef_valid(const struct cfg80211_chan_def *chandef)
144 {
145 u32 control_freq;
146
147 if (!chandef->chan)
148 return false;
149
150 control_freq = chandef->chan->center_freq;
151
152 switch (chandef->width) {
153 case NL80211_CHAN_WIDTH_5:
154 case NL80211_CHAN_WIDTH_10:
155 case NL80211_CHAN_WIDTH_20:
156 case NL80211_CHAN_WIDTH_20_NOHT:
157 if (ieee80211_chandef_to_khz(chandef) !=
158 ieee80211_channel_to_khz(chandef->chan))
159 return false;
160 if (chandef->center_freq2)
161 return false;
162 break;
163 case NL80211_CHAN_WIDTH_40:
164 if (chandef->center_freq1 != control_freq + 10 &&
165 chandef->center_freq1 != control_freq - 10)
166 return false;
167 if (chandef->center_freq2)
168 return false;
169 break;
170 case NL80211_CHAN_WIDTH_80P80:
171 if (chandef->center_freq1 != control_freq + 30 &&
172 chandef->center_freq1 != control_freq + 10 &&
173 chandef->center_freq1 != control_freq - 10 &&
174 chandef->center_freq1 != control_freq - 30)
175 return false;
176 if (!chandef->center_freq2)
177 return false;
178 /* adjacent is not allowed -- that's a 160 MHz channel */
179 if (chandef->center_freq1 - chandef->center_freq2 == 80 ||
180 chandef->center_freq2 - chandef->center_freq1 == 80)
181 return false;
182 break;
183 case NL80211_CHAN_WIDTH_80:
184 if (chandef->center_freq1 != control_freq + 30 &&
185 chandef->center_freq1 != control_freq + 10 &&
186 chandef->center_freq1 != control_freq - 10 &&
187 chandef->center_freq1 != control_freq - 30)
188 return false;
189 if (chandef->center_freq2)
190 return false;
191 break;
192 case NL80211_CHAN_WIDTH_160:
193 if (chandef->center_freq1 != control_freq + 70 &&
194 chandef->center_freq1 != control_freq + 50 &&
195 chandef->center_freq1 != control_freq + 30 &&
196 chandef->center_freq1 != control_freq + 10 &&
197 chandef->center_freq1 != control_freq - 10 &&
198 chandef->center_freq1 != control_freq - 30 &&
199 chandef->center_freq1 != control_freq - 50 &&
200 chandef->center_freq1 != control_freq - 70)
201 return false;
202 if (chandef->center_freq2)
203 return false;
204 break;
205 default:
206 return false;
207 }
208
209 /* channel 14 is only for IEEE 802.11b */
210 if (chandef->center_freq1 == 2484 &&
211 chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
212 return false;
213
214 if (cfg80211_chandef_is_edmg(chandef) &&
215 !cfg80211_edmg_chandef_valid(chandef))
216 return false;
217
218 return true;
219 }
220 EXPORT_SYMBOL(cfg80211_chandef_valid);
221
chandef_primary_freqs(const struct cfg80211_chan_def * c,u32 * pri40,u32 * pri80)222 static void chandef_primary_freqs(const struct cfg80211_chan_def *c,
223 u32 *pri40, u32 *pri80)
224 {
225 int tmp;
226
227 switch (c->width) {
228 case NL80211_CHAN_WIDTH_40:
229 *pri40 = c->center_freq1;
230 *pri80 = 0;
231 break;
232 case NL80211_CHAN_WIDTH_80:
233 case NL80211_CHAN_WIDTH_80P80:
234 *pri80 = c->center_freq1;
235 /* n_P20 */
236 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
237 /* n_P40 */
238 tmp /= 2;
239 /* freq_P40 */
240 *pri40 = c->center_freq1 - 20 + 40 * tmp;
241 break;
242 case NL80211_CHAN_WIDTH_160:
243 /* n_P20 */
244 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
245 /* n_P40 */
246 tmp /= 2;
247 /* freq_P40 */
248 *pri40 = c->center_freq1 - 60 + 40 * tmp;
249 /* n_P80 */
250 tmp /= 2;
251 *pri80 = c->center_freq1 - 40 + 80 * tmp;
252 break;
253 default:
254 WARN_ON_ONCE(1);
255 }
256 }
257
cfg80211_chandef_get_width(const struct cfg80211_chan_def * c)258 static int cfg80211_chandef_get_width(const struct cfg80211_chan_def *c)
259 {
260 int width;
261
262 switch (c->width) {
263 case NL80211_CHAN_WIDTH_5:
264 width = 5;
265 break;
266 case NL80211_CHAN_WIDTH_10:
267 width = 10;
268 break;
269 case NL80211_CHAN_WIDTH_20:
270 case NL80211_CHAN_WIDTH_20_NOHT:
271 width = 20;
272 break;
273 case NL80211_CHAN_WIDTH_40:
274 width = 40;
275 break;
276 case NL80211_CHAN_WIDTH_80P80:
277 case NL80211_CHAN_WIDTH_80:
278 width = 80;
279 break;
280 case NL80211_CHAN_WIDTH_160:
281 width = 160;
282 break;
283 default:
284 WARN_ON_ONCE(1);
285 return -1;
286 }
287 return width;
288 }
289
290 const struct cfg80211_chan_def *
cfg80211_chandef_compatible(const struct cfg80211_chan_def * c1,const struct cfg80211_chan_def * c2)291 cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1,
292 const struct cfg80211_chan_def *c2)
293 {
294 u32 c1_pri40, c1_pri80, c2_pri40, c2_pri80;
295
296 /* If they are identical, return */
297 if (cfg80211_chandef_identical(c1, c2))
298 return c1;
299
300 /* otherwise, must have same control channel */
301 if (c1->chan != c2->chan)
302 return NULL;
303
304 /*
305 * If they have the same width, but aren't identical,
306 * then they can't be compatible.
307 */
308 if (c1->width == c2->width)
309 return NULL;
310
311 /*
312 * can't be compatible if one of them is 5 or 10 MHz,
313 * but they don't have the same width.
314 */
315 if (c1->width == NL80211_CHAN_WIDTH_5 ||
316 c1->width == NL80211_CHAN_WIDTH_10 ||
317 c2->width == NL80211_CHAN_WIDTH_5 ||
318 c2->width == NL80211_CHAN_WIDTH_10)
319 return NULL;
320
321 if (c1->width == NL80211_CHAN_WIDTH_20_NOHT ||
322 c1->width == NL80211_CHAN_WIDTH_20)
323 return c2;
324
325 if (c2->width == NL80211_CHAN_WIDTH_20_NOHT ||
326 c2->width == NL80211_CHAN_WIDTH_20)
327 return c1;
328
329 chandef_primary_freqs(c1, &c1_pri40, &c1_pri80);
330 chandef_primary_freqs(c2, &c2_pri40, &c2_pri80);
331
332 if (c1_pri40 != c2_pri40)
333 return NULL;
334
335 WARN_ON(!c1_pri80 && !c2_pri80);
336 if (c1_pri80 && c2_pri80 && c1_pri80 != c2_pri80)
337 return NULL;
338
339 if (c1->width > c2->width)
340 return c1;
341 return c2;
342 }
343 EXPORT_SYMBOL(cfg80211_chandef_compatible);
344
cfg80211_set_chans_dfs_state(struct wiphy * wiphy,u32 center_freq,u32 bandwidth,enum nl80211_dfs_state dfs_state)345 static void cfg80211_set_chans_dfs_state(struct wiphy *wiphy, u32 center_freq,
346 u32 bandwidth,
347 enum nl80211_dfs_state dfs_state)
348 {
349 struct ieee80211_channel *c;
350 u32 freq;
351
352 for (freq = center_freq - bandwidth/2 + 10;
353 freq <= center_freq + bandwidth/2 - 10;
354 freq += 20) {
355 c = ieee80211_get_channel(wiphy, freq);
356 if (!c || !(c->flags & IEEE80211_CHAN_RADAR))
357 continue;
358
359 c->dfs_state = dfs_state;
360 c->dfs_state_entered = jiffies;
361 }
362 }
363
cfg80211_set_dfs_state(struct wiphy * wiphy,const struct cfg80211_chan_def * chandef,enum nl80211_dfs_state dfs_state)364 void cfg80211_set_dfs_state(struct wiphy *wiphy,
365 const struct cfg80211_chan_def *chandef,
366 enum nl80211_dfs_state dfs_state)
367 {
368 int width;
369
370 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
371 return;
372
373 width = cfg80211_chandef_get_width(chandef);
374 if (width < 0)
375 return;
376
377 cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq1,
378 width, dfs_state);
379
380 if (!chandef->center_freq2)
381 return;
382 cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq2,
383 width, dfs_state);
384 }
385
cfg80211_get_start_freq(u32 center_freq,u32 bandwidth)386 static u32 cfg80211_get_start_freq(u32 center_freq,
387 u32 bandwidth)
388 {
389 u32 start_freq;
390
391 bandwidth = MHZ_TO_KHZ(bandwidth);
392 if (bandwidth <= MHZ_TO_KHZ(20))
393 start_freq = center_freq;
394 else
395 start_freq = center_freq - bandwidth / 2 + MHZ_TO_KHZ(10);
396
397 return start_freq;
398 }
399
cfg80211_get_end_freq(u32 center_freq,u32 bandwidth)400 static u32 cfg80211_get_end_freq(u32 center_freq,
401 u32 bandwidth)
402 {
403 u32 end_freq;
404
405 bandwidth = MHZ_TO_KHZ(bandwidth);
406 if (bandwidth <= MHZ_TO_KHZ(20))
407 end_freq = center_freq;
408 else
409 end_freq = center_freq + bandwidth / 2 - MHZ_TO_KHZ(10);
410
411 return end_freq;
412 }
413
cfg80211_get_chans_dfs_required(struct wiphy * wiphy,u32 center_freq,u32 bandwidth)414 static int cfg80211_get_chans_dfs_required(struct wiphy *wiphy,
415 u32 center_freq,
416 u32 bandwidth)
417 {
418 struct ieee80211_channel *c;
419 u32 freq, start_freq, end_freq;
420
421 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
422 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
423
424 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
425 c = ieee80211_get_channel_khz(wiphy, freq);
426 if (!c)
427 return -EINVAL;
428
429 if (c->flags & IEEE80211_CHAN_RADAR)
430 return 1;
431 }
432 return 0;
433 }
434
435
cfg80211_chandef_dfs_required(struct wiphy * wiphy,const struct cfg80211_chan_def * chandef,enum nl80211_iftype iftype)436 int cfg80211_chandef_dfs_required(struct wiphy *wiphy,
437 const struct cfg80211_chan_def *chandef,
438 enum nl80211_iftype iftype)
439 {
440 int width;
441 int ret;
442
443 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
444 return -EINVAL;
445
446 switch (iftype) {
447 case NL80211_IFTYPE_ADHOC:
448 case NL80211_IFTYPE_AP:
449 case NL80211_IFTYPE_P2P_GO:
450 case NL80211_IFTYPE_MESH_POINT:
451 width = cfg80211_chandef_get_width(chandef);
452 if (width < 0)
453 return -EINVAL;
454
455 ret = cfg80211_get_chans_dfs_required(wiphy,
456 ieee80211_chandef_to_khz(chandef),
457 width);
458 if (ret < 0)
459 return ret;
460 else if (ret > 0)
461 return BIT(chandef->width);
462
463 if (!chandef->center_freq2)
464 return 0;
465
466 ret = cfg80211_get_chans_dfs_required(wiphy,
467 MHZ_TO_KHZ(chandef->center_freq2),
468 width);
469 if (ret < 0)
470 return ret;
471 else if (ret > 0)
472 return BIT(chandef->width);
473
474 break;
475 case NL80211_IFTYPE_STATION:
476 case NL80211_IFTYPE_OCB:
477 case NL80211_IFTYPE_P2P_CLIENT:
478 case NL80211_IFTYPE_MONITOR:
479 case NL80211_IFTYPE_AP_VLAN:
480 case NL80211_IFTYPE_WDS:
481 case NL80211_IFTYPE_P2P_DEVICE:
482 case NL80211_IFTYPE_NAN:
483 break;
484 case NL80211_IFTYPE_UNSPECIFIED:
485 case NUM_NL80211_IFTYPES:
486 WARN_ON(1);
487 }
488
489 return 0;
490 }
491 EXPORT_SYMBOL(cfg80211_chandef_dfs_required);
492
cfg80211_get_chans_dfs_usable(struct wiphy * wiphy,u32 center_freq,u32 bandwidth)493 static int cfg80211_get_chans_dfs_usable(struct wiphy *wiphy,
494 u32 center_freq,
495 u32 bandwidth)
496 {
497 struct ieee80211_channel *c;
498 u32 freq, start_freq, end_freq;
499 int count = 0;
500
501 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
502 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
503
504 /*
505 * Check entire range of channels for the bandwidth.
506 * Check all channels are DFS channels (DFS_USABLE or
507 * DFS_AVAILABLE). Return number of usable channels
508 * (require CAC). Allow DFS and non-DFS channel mix.
509 */
510 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
511 c = ieee80211_get_channel_khz(wiphy, freq);
512 if (!c)
513 return -EINVAL;
514
515 if (c->flags & IEEE80211_CHAN_DISABLED)
516 return -EINVAL;
517
518 if (c->flags & IEEE80211_CHAN_RADAR) {
519 if (c->dfs_state == NL80211_DFS_UNAVAILABLE)
520 return -EINVAL;
521
522 if (c->dfs_state == NL80211_DFS_USABLE)
523 count++;
524 }
525 }
526
527 return count;
528 }
529
cfg80211_chandef_dfs_usable(struct wiphy * wiphy,const struct cfg80211_chan_def * chandef)530 bool cfg80211_chandef_dfs_usable(struct wiphy *wiphy,
531 const struct cfg80211_chan_def *chandef)
532 {
533 int width;
534 int r1, r2 = 0;
535
536 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
537 return false;
538
539 width = cfg80211_chandef_get_width(chandef);
540 if (width < 0)
541 return false;
542
543 r1 = cfg80211_get_chans_dfs_usable(wiphy,
544 MHZ_TO_KHZ(chandef->center_freq1),
545 width);
546
547 if (r1 < 0)
548 return false;
549
550 switch (chandef->width) {
551 case NL80211_CHAN_WIDTH_80P80:
552 WARN_ON(!chandef->center_freq2);
553 r2 = cfg80211_get_chans_dfs_usable(wiphy,
554 MHZ_TO_KHZ(chandef->center_freq2),
555 width);
556 if (r2 < 0)
557 return false;
558 break;
559 default:
560 WARN_ON(chandef->center_freq2);
561 break;
562 }
563
564 return (r1 + r2 > 0);
565 }
566
567 /*
568 * Checks if center frequency of chan falls with in the bandwidth
569 * range of chandef.
570 */
cfg80211_is_sub_chan(struct cfg80211_chan_def * chandef,struct ieee80211_channel * chan)571 bool cfg80211_is_sub_chan(struct cfg80211_chan_def *chandef,
572 struct ieee80211_channel *chan)
573 {
574 int width;
575 u32 freq;
576
577 if (chandef->chan->center_freq == chan->center_freq)
578 return true;
579
580 width = cfg80211_chandef_get_width(chandef);
581 if (width <= 20)
582 return false;
583
584 for (freq = chandef->center_freq1 - width / 2 + 10;
585 freq <= chandef->center_freq1 + width / 2 - 10; freq += 20) {
586 if (chan->center_freq == freq)
587 return true;
588 }
589
590 if (!chandef->center_freq2)
591 return false;
592
593 for (freq = chandef->center_freq2 - width / 2 + 10;
594 freq <= chandef->center_freq2 + width / 2 - 10; freq += 20) {
595 if (chan->center_freq == freq)
596 return true;
597 }
598
599 return false;
600 }
601
cfg80211_beaconing_iface_active(struct wireless_dev * wdev)602 bool cfg80211_beaconing_iface_active(struct wireless_dev *wdev)
603 {
604 bool active = false;
605
606 ASSERT_WDEV_LOCK(wdev);
607
608 if (!wdev->chandef.chan)
609 return false;
610
611 switch (wdev->iftype) {
612 case NL80211_IFTYPE_AP:
613 case NL80211_IFTYPE_P2P_GO:
614 active = wdev->beacon_interval != 0;
615 break;
616 case NL80211_IFTYPE_ADHOC:
617 active = wdev->ssid_len != 0;
618 break;
619 case NL80211_IFTYPE_MESH_POINT:
620 active = wdev->mesh_id_len != 0;
621 break;
622 case NL80211_IFTYPE_STATION:
623 case NL80211_IFTYPE_OCB:
624 case NL80211_IFTYPE_P2P_CLIENT:
625 case NL80211_IFTYPE_MONITOR:
626 case NL80211_IFTYPE_AP_VLAN:
627 case NL80211_IFTYPE_WDS:
628 case NL80211_IFTYPE_P2P_DEVICE:
629 /* Can NAN type be considered as beaconing interface? */
630 case NL80211_IFTYPE_NAN:
631 break;
632 case NL80211_IFTYPE_UNSPECIFIED:
633 case NUM_NL80211_IFTYPES:
634 WARN_ON(1);
635 }
636
637 return active;
638 }
639
cfg80211_is_wiphy_oper_chan(struct wiphy * wiphy,struct ieee80211_channel * chan)640 static bool cfg80211_is_wiphy_oper_chan(struct wiphy *wiphy,
641 struct ieee80211_channel *chan)
642 {
643 struct wireless_dev *wdev;
644
645 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
646 wdev_lock(wdev);
647 if (!cfg80211_beaconing_iface_active(wdev)) {
648 wdev_unlock(wdev);
649 continue;
650 }
651
652 if (cfg80211_is_sub_chan(&wdev->chandef, chan)) {
653 wdev_unlock(wdev);
654 return true;
655 }
656 wdev_unlock(wdev);
657 }
658
659 return false;
660 }
661
cfg80211_any_wiphy_oper_chan(struct wiphy * wiphy,struct ieee80211_channel * chan)662 bool cfg80211_any_wiphy_oper_chan(struct wiphy *wiphy,
663 struct ieee80211_channel *chan)
664 {
665 struct cfg80211_registered_device *rdev;
666
667 ASSERT_RTNL();
668
669 if (!(chan->flags & IEEE80211_CHAN_RADAR))
670 return false;
671
672 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
673 if (!reg_dfs_domain_same(wiphy, &rdev->wiphy))
674 continue;
675
676 if (cfg80211_is_wiphy_oper_chan(&rdev->wiphy, chan))
677 return true;
678 }
679
680 return false;
681 }
682
cfg80211_get_chans_dfs_available(struct wiphy * wiphy,u32 center_freq,u32 bandwidth)683 static bool cfg80211_get_chans_dfs_available(struct wiphy *wiphy,
684 u32 center_freq,
685 u32 bandwidth)
686 {
687 struct ieee80211_channel *c;
688 u32 freq, start_freq, end_freq;
689 bool dfs_offload;
690
691 dfs_offload = wiphy_ext_feature_isset(wiphy,
692 NL80211_EXT_FEATURE_DFS_OFFLOAD);
693
694 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
695 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
696
697 /*
698 * Check entire range of channels for the bandwidth.
699 * If any channel in between is disabled or has not
700 * had gone through CAC return false
701 */
702 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
703 c = ieee80211_get_channel_khz(wiphy, freq);
704 if (!c)
705 return false;
706
707 if (c->flags & IEEE80211_CHAN_DISABLED)
708 return false;
709
710 if ((c->flags & IEEE80211_CHAN_RADAR) &&
711 (c->dfs_state != NL80211_DFS_AVAILABLE) &&
712 !(c->dfs_state == NL80211_DFS_USABLE && dfs_offload))
713 return false;
714 }
715
716 return true;
717 }
718
cfg80211_chandef_dfs_available(struct wiphy * wiphy,const struct cfg80211_chan_def * chandef)719 static bool cfg80211_chandef_dfs_available(struct wiphy *wiphy,
720 const struct cfg80211_chan_def *chandef)
721 {
722 int width;
723 int r;
724
725 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
726 return false;
727
728 width = cfg80211_chandef_get_width(chandef);
729 if (width < 0)
730 return false;
731
732 r = cfg80211_get_chans_dfs_available(wiphy,
733 MHZ_TO_KHZ(chandef->center_freq1),
734 width);
735
736 /* If any of channels unavailable for cf1 just return */
737 if (!r)
738 return r;
739
740 switch (chandef->width) {
741 case NL80211_CHAN_WIDTH_80P80:
742 WARN_ON(!chandef->center_freq2);
743 r = cfg80211_get_chans_dfs_available(wiphy,
744 MHZ_TO_KHZ(chandef->center_freq2),
745 width);
746 break;
747 default:
748 WARN_ON(chandef->center_freq2);
749 break;
750 }
751
752 return r;
753 }
754
cfg80211_get_chans_dfs_cac_time(struct wiphy * wiphy,u32 center_freq,u32 bandwidth)755 static unsigned int cfg80211_get_chans_dfs_cac_time(struct wiphy *wiphy,
756 u32 center_freq,
757 u32 bandwidth)
758 {
759 struct ieee80211_channel *c;
760 u32 start_freq, end_freq, freq;
761 unsigned int dfs_cac_ms = 0;
762
763 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
764 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
765
766 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
767 c = ieee80211_get_channel_khz(wiphy, freq);
768 if (!c)
769 return 0;
770
771 if (c->flags & IEEE80211_CHAN_DISABLED)
772 return 0;
773
774 if (!(c->flags & IEEE80211_CHAN_RADAR))
775 continue;
776
777 if (c->dfs_cac_ms > dfs_cac_ms)
778 dfs_cac_ms = c->dfs_cac_ms;
779 }
780
781 return dfs_cac_ms;
782 }
783
784 unsigned int
cfg80211_chandef_dfs_cac_time(struct wiphy * wiphy,const struct cfg80211_chan_def * chandef)785 cfg80211_chandef_dfs_cac_time(struct wiphy *wiphy,
786 const struct cfg80211_chan_def *chandef)
787 {
788 int width;
789 unsigned int t1 = 0, t2 = 0;
790
791 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
792 return 0;
793
794 width = cfg80211_chandef_get_width(chandef);
795 if (width < 0)
796 return 0;
797
798 t1 = cfg80211_get_chans_dfs_cac_time(wiphy,
799 MHZ_TO_KHZ(chandef->center_freq1),
800 width);
801
802 if (!chandef->center_freq2)
803 return t1;
804
805 t2 = cfg80211_get_chans_dfs_cac_time(wiphy,
806 MHZ_TO_KHZ(chandef->center_freq2),
807 width);
808
809 return max(t1, t2);
810 }
811
cfg80211_secondary_chans_ok(struct wiphy * wiphy,u32 center_freq,u32 bandwidth,u32 prohibited_flags)812 static bool cfg80211_secondary_chans_ok(struct wiphy *wiphy,
813 u32 center_freq, u32 bandwidth,
814 u32 prohibited_flags)
815 {
816 struct ieee80211_channel *c;
817 u32 freq, start_freq, end_freq;
818
819 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
820 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
821
822 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
823 c = ieee80211_get_channel_khz(wiphy, freq);
824 if (!c || c->flags & prohibited_flags)
825 return false;
826 }
827
828 return true;
829 }
830
831 /* check if the operating channels are valid and supported */
cfg80211_edmg_usable(struct wiphy * wiphy,u8 edmg_channels,enum ieee80211_edmg_bw_config edmg_bw_config,int primary_channel,struct ieee80211_edmg * edmg_cap)832 static bool cfg80211_edmg_usable(struct wiphy *wiphy, u8 edmg_channels,
833 enum ieee80211_edmg_bw_config edmg_bw_config,
834 int primary_channel,
835 struct ieee80211_edmg *edmg_cap)
836 {
837 struct ieee80211_channel *chan;
838 int i, freq;
839 int channels_counter = 0;
840
841 if (!edmg_channels && !edmg_bw_config)
842 return true;
843
844 if ((!edmg_channels && edmg_bw_config) ||
845 (edmg_channels && !edmg_bw_config))
846 return false;
847
848 if (!(edmg_channels & BIT(primary_channel - 1)))
849 return false;
850
851 /* 60GHz channels 1..6 */
852 for (i = 0; i < 6; i++) {
853 if (!(edmg_channels & BIT(i)))
854 continue;
855
856 if (!(edmg_cap->channels & BIT(i)))
857 return false;
858
859 channels_counter++;
860
861 freq = ieee80211_channel_to_frequency(i + 1,
862 NL80211_BAND_60GHZ);
863 chan = ieee80211_get_channel(wiphy, freq);
864 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
865 return false;
866 }
867
868 /* IEEE802.11 allows max 4 channels */
869 if (channels_counter > 4)
870 return false;
871
872 /* check bw_config is a subset of what driver supports
873 * (see IEEE P802.11ay/D4.0 section 9.4.2.251, Table 13)
874 */
875 if ((edmg_bw_config % 4) > (edmg_cap->bw_config % 4))
876 return false;
877
878 if (edmg_bw_config > edmg_cap->bw_config)
879 return false;
880
881 return true;
882 }
883
cfg80211_chandef_usable(struct wiphy * wiphy,const struct cfg80211_chan_def * chandef,u32 prohibited_flags)884 bool cfg80211_chandef_usable(struct wiphy *wiphy,
885 const struct cfg80211_chan_def *chandef,
886 u32 prohibited_flags)
887 {
888 struct ieee80211_sta_ht_cap *ht_cap;
889 struct ieee80211_sta_vht_cap *vht_cap;
890 struct ieee80211_edmg *edmg_cap;
891 u32 width, control_freq, cap;
892
893 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
894 return false;
895
896 ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap;
897 vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap;
898 edmg_cap = &wiphy->bands[chandef->chan->band]->edmg_cap;
899
900 if (edmg_cap->channels &&
901 !cfg80211_edmg_usable(wiphy,
902 chandef->edmg.channels,
903 chandef->edmg.bw_config,
904 chandef->chan->hw_value,
905 edmg_cap))
906 return false;
907
908 control_freq = chandef->chan->center_freq;
909
910 switch (chandef->width) {
911 case NL80211_CHAN_WIDTH_5:
912 width = 5;
913 break;
914 case NL80211_CHAN_WIDTH_10:
915 prohibited_flags |= IEEE80211_CHAN_NO_10MHZ;
916 width = 10;
917 break;
918 case NL80211_CHAN_WIDTH_20:
919 if (!ht_cap->ht_supported &&
920 chandef->chan->band != NL80211_BAND_6GHZ)
921 return false;
922 /* fall through */
923 case NL80211_CHAN_WIDTH_20_NOHT:
924 prohibited_flags |= IEEE80211_CHAN_NO_20MHZ;
925 width = 20;
926 break;
927 case NL80211_CHAN_WIDTH_40:
928 width = 40;
929 if (chandef->chan->band == NL80211_BAND_6GHZ)
930 break;
931 if (!ht_cap->ht_supported)
932 return false;
933 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
934 ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)
935 return false;
936 if (chandef->center_freq1 < control_freq &&
937 chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
938 return false;
939 if (chandef->center_freq1 > control_freq &&
940 chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
941 return false;
942 break;
943 case NL80211_CHAN_WIDTH_80P80:
944 cap = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
945 if (chandef->chan->band != NL80211_BAND_6GHZ &&
946 cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
947 return false;
948 /* fall through */
949 case NL80211_CHAN_WIDTH_80:
950 prohibited_flags |= IEEE80211_CHAN_NO_80MHZ;
951 width = 80;
952 if (chandef->chan->band == NL80211_BAND_6GHZ)
953 break;
954 if (!vht_cap->vht_supported)
955 return false;
956 break;
957 case NL80211_CHAN_WIDTH_160:
958 prohibited_flags |= IEEE80211_CHAN_NO_160MHZ;
959 width = 160;
960 if (chandef->chan->band == NL80211_BAND_6GHZ)
961 break;
962 if (!vht_cap->vht_supported)
963 return false;
964 cap = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
965 if (cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
966 cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ)
967 return false;
968 break;
969 default:
970 WARN_ON_ONCE(1);
971 return false;
972 }
973
974 /*
975 * TODO: What if there are only certain 80/160/80+80 MHz channels
976 * allowed by the driver, or only certain combinations?
977 * For 40 MHz the driver can set the NO_HT40 flags, but for
978 * 80/160 MHz and in particular 80+80 MHz this isn't really
979 * feasible and we only have NO_80MHZ/NO_160MHZ so far but
980 * no way to cover 80+80 MHz or more complex restrictions.
981 * Note that such restrictions also need to be advertised to
982 * userspace, for example for P2P channel selection.
983 */
984
985 if (width > 20)
986 prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
987
988 /* 5 and 10 MHz are only defined for the OFDM PHY */
989 if (width < 20)
990 prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
991
992
993 if (!cfg80211_secondary_chans_ok(wiphy,
994 ieee80211_chandef_to_khz(chandef),
995 width, prohibited_flags))
996 return false;
997
998 if (!chandef->center_freq2)
999 return true;
1000 return cfg80211_secondary_chans_ok(wiphy,
1001 MHZ_TO_KHZ(chandef->center_freq2),
1002 width, prohibited_flags);
1003 }
1004 EXPORT_SYMBOL(cfg80211_chandef_usable);
1005
1006 /*
1007 * Check if the channel can be used under permissive conditions mandated by
1008 * some regulatory bodies, i.e., the channel is marked with
1009 * IEEE80211_CHAN_IR_CONCURRENT and there is an additional station interface
1010 * associated to an AP on the same channel or on the same UNII band
1011 * (assuming that the AP is an authorized master).
1012 * In addition allow operation on a channel on which indoor operation is
1013 * allowed, iff we are currently operating in an indoor environment.
1014 */
cfg80211_ir_permissive_chan(struct wiphy * wiphy,enum nl80211_iftype iftype,struct ieee80211_channel * chan)1015 static bool cfg80211_ir_permissive_chan(struct wiphy *wiphy,
1016 enum nl80211_iftype iftype,
1017 struct ieee80211_channel *chan)
1018 {
1019 struct wireless_dev *wdev;
1020 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1021
1022 ASSERT_RTNL();
1023
1024 if (!IS_ENABLED(CONFIG_CFG80211_REG_RELAX_NO_IR) ||
1025 !(wiphy->regulatory_flags & REGULATORY_ENABLE_RELAX_NO_IR))
1026 return false;
1027
1028 /* only valid for GO and TDLS off-channel (station/p2p-CL) */
1029 if (iftype != NL80211_IFTYPE_P2P_GO &&
1030 iftype != NL80211_IFTYPE_STATION &&
1031 iftype != NL80211_IFTYPE_P2P_CLIENT)
1032 return false;
1033
1034 if (regulatory_indoor_allowed() &&
1035 (chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
1036 return true;
1037
1038 if (!(chan->flags & IEEE80211_CHAN_IR_CONCURRENT))
1039 return false;
1040
1041 /*
1042 * Generally, it is possible to rely on another device/driver to allow
1043 * the IR concurrent relaxation, however, since the device can further
1044 * enforce the relaxation (by doing a similar verifications as this),
1045 * and thus fail the GO instantiation, consider only the interfaces of
1046 * the current registered device.
1047 */
1048 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
1049 struct ieee80211_channel *other_chan = NULL;
1050 int r1, r2;
1051
1052 wdev_lock(wdev);
1053 if (wdev->iftype == NL80211_IFTYPE_STATION &&
1054 wdev->current_bss)
1055 other_chan = wdev->current_bss->pub.channel;
1056
1057 /*
1058 * If a GO already operates on the same GO_CONCURRENT channel,
1059 * this one (maybe the same one) can beacon as well. We allow
1060 * the operation even if the station we relied on with
1061 * GO_CONCURRENT is disconnected now. But then we must make sure
1062 * we're not outdoor on an indoor-only channel.
1063 */
1064 if (iftype == NL80211_IFTYPE_P2P_GO &&
1065 wdev->iftype == NL80211_IFTYPE_P2P_GO &&
1066 wdev->beacon_interval &&
1067 !(chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
1068 other_chan = wdev->chandef.chan;
1069 wdev_unlock(wdev);
1070
1071 if (!other_chan)
1072 continue;
1073
1074 if (chan == other_chan)
1075 return true;
1076
1077 if (chan->band != NL80211_BAND_5GHZ &&
1078 chan->band != NL80211_BAND_6GHZ)
1079 continue;
1080
1081 r1 = cfg80211_get_unii(chan->center_freq);
1082 r2 = cfg80211_get_unii(other_chan->center_freq);
1083
1084 if (r1 != -EINVAL && r1 == r2) {
1085 /*
1086 * At some locations channels 149-165 are considered a
1087 * bundle, but at other locations, e.g., Indonesia,
1088 * channels 149-161 are considered a bundle while
1089 * channel 165 is left out and considered to be in a
1090 * different bundle. Thus, in case that there is a
1091 * station interface connected to an AP on channel 165,
1092 * it is assumed that channels 149-161 are allowed for
1093 * GO operations. However, having a station interface
1094 * connected to an AP on channels 149-161, does not
1095 * allow GO operation on channel 165.
1096 */
1097 if (chan->center_freq == 5825 &&
1098 other_chan->center_freq != 5825)
1099 continue;
1100 return true;
1101 }
1102 }
1103
1104 return false;
1105 }
1106
_cfg80211_reg_can_beacon(struct wiphy * wiphy,struct cfg80211_chan_def * chandef,enum nl80211_iftype iftype,bool check_no_ir)1107 static bool _cfg80211_reg_can_beacon(struct wiphy *wiphy,
1108 struct cfg80211_chan_def *chandef,
1109 enum nl80211_iftype iftype,
1110 bool check_no_ir)
1111 {
1112 bool res;
1113 u32 prohibited_flags = IEEE80211_CHAN_DISABLED |
1114 IEEE80211_CHAN_RADAR;
1115
1116 trace_cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir);
1117
1118 if (check_no_ir)
1119 prohibited_flags |= IEEE80211_CHAN_NO_IR;
1120
1121 if (cfg80211_chandef_dfs_required(wiphy, chandef, iftype) > 0 &&
1122 cfg80211_chandef_dfs_available(wiphy, chandef)) {
1123 /* We can skip IEEE80211_CHAN_NO_IR if chandef dfs available */
1124 prohibited_flags = IEEE80211_CHAN_DISABLED;
1125 }
1126
1127 res = cfg80211_chandef_usable(wiphy, chandef, prohibited_flags);
1128
1129 trace_cfg80211_return_bool(res);
1130 return res;
1131 }
1132
cfg80211_reg_can_beacon(struct wiphy * wiphy,struct cfg80211_chan_def * chandef,enum nl80211_iftype iftype)1133 bool cfg80211_reg_can_beacon(struct wiphy *wiphy,
1134 struct cfg80211_chan_def *chandef,
1135 enum nl80211_iftype iftype)
1136 {
1137 return _cfg80211_reg_can_beacon(wiphy, chandef, iftype, true);
1138 }
1139 EXPORT_SYMBOL(cfg80211_reg_can_beacon);
1140
cfg80211_reg_can_beacon_relax(struct wiphy * wiphy,struct cfg80211_chan_def * chandef,enum nl80211_iftype iftype)1141 bool cfg80211_reg_can_beacon_relax(struct wiphy *wiphy,
1142 struct cfg80211_chan_def *chandef,
1143 enum nl80211_iftype iftype)
1144 {
1145 bool check_no_ir;
1146
1147 ASSERT_RTNL();
1148
1149 /*
1150 * Under certain conditions suggested by some regulatory bodies a
1151 * GO/STA can IR on channels marked with IEEE80211_NO_IR. Set this flag
1152 * only if such relaxations are not enabled and the conditions are not
1153 * met.
1154 */
1155 check_no_ir = !cfg80211_ir_permissive_chan(wiphy, iftype,
1156 chandef->chan);
1157
1158 return _cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir);
1159 }
1160 EXPORT_SYMBOL(cfg80211_reg_can_beacon_relax);
1161
cfg80211_set_monitor_channel(struct cfg80211_registered_device * rdev,struct cfg80211_chan_def * chandef)1162 int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
1163 struct cfg80211_chan_def *chandef)
1164 {
1165 if (!rdev->ops->set_monitor_channel)
1166 return -EOPNOTSUPP;
1167 if (!cfg80211_has_monitors_only(rdev))
1168 return -EBUSY;
1169
1170 return rdev_set_monitor_channel(rdev, chandef);
1171 }
1172
1173 void
cfg80211_get_chan_state(struct wireless_dev * wdev,struct ieee80211_channel ** chan,enum cfg80211_chan_mode * chanmode,u8 * radar_detect)1174 cfg80211_get_chan_state(struct wireless_dev *wdev,
1175 struct ieee80211_channel **chan,
1176 enum cfg80211_chan_mode *chanmode,
1177 u8 *radar_detect)
1178 {
1179 int ret;
1180
1181 *chan = NULL;
1182 *chanmode = CHAN_MODE_UNDEFINED;
1183
1184 ASSERT_WDEV_LOCK(wdev);
1185
1186 if (wdev->netdev && !netif_running(wdev->netdev))
1187 return;
1188
1189 switch (wdev->iftype) {
1190 case NL80211_IFTYPE_ADHOC:
1191 if (wdev->current_bss) {
1192 *chan = wdev->current_bss->pub.channel;
1193 *chanmode = (wdev->ibss_fixed &&
1194 !wdev->ibss_dfs_possible)
1195 ? CHAN_MODE_SHARED
1196 : CHAN_MODE_EXCLUSIVE;
1197
1198 /* consider worst-case - IBSS can try to return to the
1199 * original user-specified channel as creator */
1200 if (wdev->ibss_dfs_possible)
1201 *radar_detect |= BIT(wdev->chandef.width);
1202 return;
1203 }
1204 break;
1205 case NL80211_IFTYPE_STATION:
1206 case NL80211_IFTYPE_P2P_CLIENT:
1207 if (wdev->current_bss) {
1208 *chan = wdev->current_bss->pub.channel;
1209 *chanmode = CHAN_MODE_SHARED;
1210 return;
1211 }
1212 break;
1213 case NL80211_IFTYPE_AP:
1214 case NL80211_IFTYPE_P2P_GO:
1215 if (wdev->cac_started) {
1216 *chan = wdev->chandef.chan;
1217 *chanmode = CHAN_MODE_SHARED;
1218 *radar_detect |= BIT(wdev->chandef.width);
1219 } else if (wdev->beacon_interval) {
1220 *chan = wdev->chandef.chan;
1221 *chanmode = CHAN_MODE_SHARED;
1222
1223 ret = cfg80211_chandef_dfs_required(wdev->wiphy,
1224 &wdev->chandef,
1225 wdev->iftype);
1226 WARN_ON(ret < 0);
1227 if (ret > 0)
1228 *radar_detect |= BIT(wdev->chandef.width);
1229 }
1230 return;
1231 case NL80211_IFTYPE_MESH_POINT:
1232 if (wdev->mesh_id_len) {
1233 *chan = wdev->chandef.chan;
1234 *chanmode = CHAN_MODE_SHARED;
1235
1236 ret = cfg80211_chandef_dfs_required(wdev->wiphy,
1237 &wdev->chandef,
1238 wdev->iftype);
1239 WARN_ON(ret < 0);
1240 if (ret > 0)
1241 *radar_detect |= BIT(wdev->chandef.width);
1242 }
1243 return;
1244 case NL80211_IFTYPE_OCB:
1245 if (wdev->chandef.chan) {
1246 *chan = wdev->chandef.chan;
1247 *chanmode = CHAN_MODE_SHARED;
1248 return;
1249 }
1250 break;
1251 case NL80211_IFTYPE_MONITOR:
1252 case NL80211_IFTYPE_AP_VLAN:
1253 case NL80211_IFTYPE_WDS:
1254 case NL80211_IFTYPE_P2P_DEVICE:
1255 case NL80211_IFTYPE_NAN:
1256 /* these interface types don't really have a channel */
1257 return;
1258 case NL80211_IFTYPE_UNSPECIFIED:
1259 case NUM_NL80211_IFTYPES:
1260 WARN_ON(1);
1261 }
1262 }
1263