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-2022 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
cfg80211_valid_60g_freq(u32 freq)18 static bool cfg80211_valid_60g_freq(u32 freq)
19 {
20 return freq >= 58320 && freq <= 70200;
21 }
22
cfg80211_chandef_create(struct cfg80211_chan_def * chandef,struct ieee80211_channel * chan,enum nl80211_channel_type chan_type)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
cfg80211_edmg_chandef_valid(const struct cfg80211_chan_def * chandef)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
nl80211_chan_width_to_mhz(enum nl80211_chan_width chan_width)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
cfg80211_chandef_get_width(const struct cfg80211_chan_def * c)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
cfg80211_chandef_valid(const struct cfg80211_chan_def * chandef)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
chandef_primary_freqs(const struct cfg80211_chan_def * c,u32 * pri40,u32 * pri80,u32 * pri160)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 *
cfg80211_chandef_compatible(const struct cfg80211_chan_def * c1,const struct cfg80211_chan_def * c2)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
cfg80211_set_chans_dfs_state(struct wiphy * wiphy,u32 center_freq,u32 bandwidth,enum nl80211_dfs_state dfs_state)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
cfg80211_set_dfs_state(struct wiphy * wiphy,const struct cfg80211_chan_def * chandef,enum nl80211_dfs_state dfs_state)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
cfg80211_get_start_freq(u32 center_freq,u32 bandwidth)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
cfg80211_get_end_freq(u32 center_freq,u32 bandwidth)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 bool
cfg80211_dfs_permissive_check_wdev(struct cfg80211_registered_device * rdev,enum nl80211_iftype iftype,struct wireless_dev * wdev,struct ieee80211_channel * chan)518 cfg80211_dfs_permissive_check_wdev(struct cfg80211_registered_device *rdev,
519 enum nl80211_iftype iftype,
520 struct wireless_dev *wdev,
521 struct ieee80211_channel *chan)
522 {
523 unsigned int link_id;
524
525 for_each_valid_link(wdev, link_id) {
526 struct ieee80211_channel *other_chan = NULL;
527 struct cfg80211_chan_def chandef = {};
528 int ret;
529
530 /* In order to avoid daisy chaining only allow BSS STA */
531 if (wdev->iftype != NL80211_IFTYPE_STATION ||
532 !wdev->links[link_id].client.current_bss)
533 continue;
534
535 other_chan =
536 wdev->links[link_id].client.current_bss->pub.channel;
537
538 if (!other_chan)
539 continue;
540
541 if (chan == other_chan)
542 return true;
543
544 /* continue if we can't get the channel */
545 ret = rdev_get_channel(rdev, wdev, link_id, &chandef);
546 if (ret)
547 continue;
548
549 if (cfg80211_is_sub_chan(&chandef, chan, false))
550 return true;
551 }
552
553 return false;
554 }
555
556 /*
557 * Check if P2P GO is allowed to operate on a DFS channel
558 */
cfg80211_dfs_permissive_chan(struct wiphy * wiphy,enum nl80211_iftype iftype,struct ieee80211_channel * chan)559 static bool cfg80211_dfs_permissive_chan(struct wiphy *wiphy,
560 enum nl80211_iftype iftype,
561 struct ieee80211_channel *chan)
562 {
563 struct wireless_dev *wdev;
564 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
565
566 lockdep_assert_held(&rdev->wiphy.mtx);
567
568 if (!wiphy_ext_feature_isset(&rdev->wiphy,
569 NL80211_EXT_FEATURE_DFS_CONCURRENT) ||
570 !(chan->flags & IEEE80211_CHAN_DFS_CONCURRENT))
571 return false;
572
573 /* only valid for P2P GO */
574 if (iftype != NL80211_IFTYPE_P2P_GO)
575 return false;
576
577 /*
578 * Allow only if there's a concurrent BSS
579 */
580 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
581 bool ret = cfg80211_dfs_permissive_check_wdev(rdev, iftype,
582 wdev, chan);
583 if (ret)
584 return ret;
585 }
586
587 return false;
588 }
589
cfg80211_get_chans_dfs_required(struct wiphy * wiphy,u32 center_freq,u32 bandwidth,enum nl80211_iftype iftype)590 static int cfg80211_get_chans_dfs_required(struct wiphy *wiphy,
591 u32 center_freq,
592 u32 bandwidth,
593 enum nl80211_iftype iftype)
594 {
595 struct ieee80211_channel *c;
596 u32 freq, start_freq, end_freq;
597
598 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
599 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
600
601 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
602 c = ieee80211_get_channel_khz(wiphy, freq);
603 if (!c)
604 return -EINVAL;
605
606 if (c->flags & IEEE80211_CHAN_RADAR &&
607 !cfg80211_dfs_permissive_chan(wiphy, iftype, c))
608 return 1;
609 }
610
611 return 0;
612 }
613
614
cfg80211_chandef_dfs_required(struct wiphy * wiphy,const struct cfg80211_chan_def * chandef,enum nl80211_iftype iftype)615 int cfg80211_chandef_dfs_required(struct wiphy *wiphy,
616 const struct cfg80211_chan_def *chandef,
617 enum nl80211_iftype iftype)
618 {
619 int width;
620 int ret;
621
622 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
623 return -EINVAL;
624
625 switch (iftype) {
626 case NL80211_IFTYPE_ADHOC:
627 case NL80211_IFTYPE_AP:
628 case NL80211_IFTYPE_P2P_GO:
629 case NL80211_IFTYPE_MESH_POINT:
630 width = cfg80211_chandef_get_width(chandef);
631 if (width < 0)
632 return -EINVAL;
633
634 ret = cfg80211_get_chans_dfs_required(wiphy,
635 ieee80211_chandef_to_khz(chandef),
636 width, iftype);
637 if (ret < 0)
638 return ret;
639 else if (ret > 0)
640 return BIT(chandef->width);
641
642 if (!chandef->center_freq2)
643 return 0;
644
645 ret = cfg80211_get_chans_dfs_required(wiphy,
646 MHZ_TO_KHZ(chandef->center_freq2),
647 width, iftype);
648 if (ret < 0)
649 return ret;
650 else if (ret > 0)
651 return BIT(chandef->width);
652
653 break;
654 case NL80211_IFTYPE_STATION:
655 case NL80211_IFTYPE_OCB:
656 case NL80211_IFTYPE_P2P_CLIENT:
657 case NL80211_IFTYPE_MONITOR:
658 case NL80211_IFTYPE_AP_VLAN:
659 case NL80211_IFTYPE_P2P_DEVICE:
660 case NL80211_IFTYPE_NAN:
661 break;
662 case NL80211_IFTYPE_WDS:
663 case NL80211_IFTYPE_UNSPECIFIED:
664 case NUM_NL80211_IFTYPES:
665 WARN_ON(1);
666 }
667
668 return 0;
669 }
670 EXPORT_SYMBOL(cfg80211_chandef_dfs_required);
671
cfg80211_get_chans_dfs_usable(struct wiphy * wiphy,u32 center_freq,u32 bandwidth)672 static int cfg80211_get_chans_dfs_usable(struct wiphy *wiphy,
673 u32 center_freq,
674 u32 bandwidth)
675 {
676 struct ieee80211_channel *c;
677 u32 freq, start_freq, end_freq;
678 int count = 0;
679
680 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
681 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
682
683 /*
684 * Check entire range of channels for the bandwidth.
685 * Check all channels are DFS channels (DFS_USABLE or
686 * DFS_AVAILABLE). Return number of usable channels
687 * (require CAC). Allow DFS and non-DFS channel mix.
688 */
689 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
690 c = ieee80211_get_channel_khz(wiphy, freq);
691 if (!c)
692 return -EINVAL;
693
694 if (c->flags & IEEE80211_CHAN_DISABLED)
695 return -EINVAL;
696
697 if (c->flags & IEEE80211_CHAN_RADAR) {
698 if (c->dfs_state == NL80211_DFS_UNAVAILABLE)
699 return -EINVAL;
700
701 if (c->dfs_state == NL80211_DFS_USABLE)
702 count++;
703 }
704 }
705
706 return count;
707 }
708
cfg80211_chandef_dfs_usable(struct wiphy * wiphy,const struct cfg80211_chan_def * chandef)709 bool cfg80211_chandef_dfs_usable(struct wiphy *wiphy,
710 const struct cfg80211_chan_def *chandef)
711 {
712 int width;
713 int r1, r2 = 0;
714
715 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
716 return false;
717
718 width = cfg80211_chandef_get_width(chandef);
719 if (width < 0)
720 return false;
721
722 r1 = cfg80211_get_chans_dfs_usable(wiphy,
723 MHZ_TO_KHZ(chandef->center_freq1),
724 width);
725
726 if (r1 < 0)
727 return false;
728
729 switch (chandef->width) {
730 case NL80211_CHAN_WIDTH_80P80:
731 WARN_ON(!chandef->center_freq2);
732 r2 = cfg80211_get_chans_dfs_usable(wiphy,
733 MHZ_TO_KHZ(chandef->center_freq2),
734 width);
735 if (r2 < 0)
736 return false;
737 break;
738 default:
739 WARN_ON(chandef->center_freq2);
740 break;
741 }
742
743 return (r1 + r2 > 0);
744 }
745
746 /*
747 * Checks if center frequency of chan falls with in the bandwidth
748 * range of chandef.
749 */
cfg80211_is_sub_chan(struct cfg80211_chan_def * chandef,struct ieee80211_channel * chan,bool primary_only)750 bool cfg80211_is_sub_chan(struct cfg80211_chan_def *chandef,
751 struct ieee80211_channel *chan,
752 bool primary_only)
753 {
754 int width;
755 u32 freq;
756
757 if (!chandef->chan)
758 return false;
759
760 if (chandef->chan->center_freq == chan->center_freq)
761 return true;
762
763 if (primary_only)
764 return false;
765
766 width = cfg80211_chandef_get_width(chandef);
767 if (width <= 20)
768 return false;
769
770 for (freq = chandef->center_freq1 - width / 2 + 10;
771 freq <= chandef->center_freq1 + width / 2 - 10; freq += 20) {
772 if (chan->center_freq == freq)
773 return true;
774 }
775
776 if (!chandef->center_freq2)
777 return false;
778
779 for (freq = chandef->center_freq2 - width / 2 + 10;
780 freq <= chandef->center_freq2 + width / 2 - 10; freq += 20) {
781 if (chan->center_freq == freq)
782 return true;
783 }
784
785 return false;
786 }
787
cfg80211_beaconing_iface_active(struct wireless_dev * wdev)788 bool cfg80211_beaconing_iface_active(struct wireless_dev *wdev)
789 {
790 unsigned int link;
791
792 ASSERT_WDEV_LOCK(wdev);
793
794 switch (wdev->iftype) {
795 case NL80211_IFTYPE_AP:
796 case NL80211_IFTYPE_P2P_GO:
797 for_each_valid_link(wdev, link) {
798 if (wdev->links[link].ap.beacon_interval)
799 return true;
800 }
801 break;
802 case NL80211_IFTYPE_ADHOC:
803 if (wdev->u.ibss.ssid_len)
804 return true;
805 break;
806 case NL80211_IFTYPE_MESH_POINT:
807 if (wdev->u.mesh.id_len)
808 return true;
809 break;
810 case NL80211_IFTYPE_STATION:
811 case NL80211_IFTYPE_OCB:
812 case NL80211_IFTYPE_P2P_CLIENT:
813 case NL80211_IFTYPE_MONITOR:
814 case NL80211_IFTYPE_AP_VLAN:
815 case NL80211_IFTYPE_P2P_DEVICE:
816 /* Can NAN type be considered as beaconing interface? */
817 case NL80211_IFTYPE_NAN:
818 break;
819 case NL80211_IFTYPE_UNSPECIFIED:
820 case NL80211_IFTYPE_WDS:
821 case NUM_NL80211_IFTYPES:
822 WARN_ON(1);
823 }
824
825 return false;
826 }
827
cfg80211_wdev_on_sub_chan(struct wireless_dev * wdev,struct ieee80211_channel * chan,bool primary_only)828 bool cfg80211_wdev_on_sub_chan(struct wireless_dev *wdev,
829 struct ieee80211_channel *chan,
830 bool primary_only)
831 {
832 unsigned int link;
833
834 switch (wdev->iftype) {
835 case NL80211_IFTYPE_AP:
836 case NL80211_IFTYPE_P2P_GO:
837 for_each_valid_link(wdev, link) {
838 if (cfg80211_is_sub_chan(&wdev->links[link].ap.chandef,
839 chan, primary_only))
840 return true;
841 }
842 break;
843 case NL80211_IFTYPE_ADHOC:
844 return cfg80211_is_sub_chan(&wdev->u.ibss.chandef, chan,
845 primary_only);
846 case NL80211_IFTYPE_MESH_POINT:
847 return cfg80211_is_sub_chan(&wdev->u.mesh.chandef, chan,
848 primary_only);
849 default:
850 break;
851 }
852
853 return false;
854 }
855
cfg80211_is_wiphy_oper_chan(struct wiphy * wiphy,struct ieee80211_channel * chan)856 static bool cfg80211_is_wiphy_oper_chan(struct wiphy *wiphy,
857 struct ieee80211_channel *chan)
858 {
859 struct wireless_dev *wdev;
860
861 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
862 wdev_lock(wdev);
863 if (!cfg80211_beaconing_iface_active(wdev)) {
864 wdev_unlock(wdev);
865 continue;
866 }
867
868 if (cfg80211_wdev_on_sub_chan(wdev, chan, false)) {
869 wdev_unlock(wdev);
870 return true;
871 }
872 wdev_unlock(wdev);
873 }
874
875 return false;
876 }
877
878 static bool
cfg80211_offchan_chain_is_active(struct cfg80211_registered_device * rdev,struct ieee80211_channel * channel)879 cfg80211_offchan_chain_is_active(struct cfg80211_registered_device *rdev,
880 struct ieee80211_channel *channel)
881 {
882 if (!rdev->background_radar_wdev)
883 return false;
884
885 if (!cfg80211_chandef_valid(&rdev->background_radar_chandef))
886 return false;
887
888 return cfg80211_is_sub_chan(&rdev->background_radar_chandef, channel,
889 false);
890 }
891
cfg80211_any_wiphy_oper_chan(struct wiphy * wiphy,struct ieee80211_channel * chan)892 bool cfg80211_any_wiphy_oper_chan(struct wiphy *wiphy,
893 struct ieee80211_channel *chan)
894 {
895 struct cfg80211_registered_device *rdev;
896
897 ASSERT_RTNL();
898
899 if (!(chan->flags & IEEE80211_CHAN_RADAR))
900 return false;
901
902 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
903 if (!reg_dfs_domain_same(wiphy, &rdev->wiphy))
904 continue;
905
906 if (cfg80211_is_wiphy_oper_chan(&rdev->wiphy, chan))
907 return true;
908
909 if (cfg80211_offchan_chain_is_active(rdev, chan))
910 return true;
911 }
912
913 return false;
914 }
915
cfg80211_get_chans_dfs_available(struct wiphy * wiphy,u32 center_freq,u32 bandwidth)916 static bool cfg80211_get_chans_dfs_available(struct wiphy *wiphy,
917 u32 center_freq,
918 u32 bandwidth)
919 {
920 struct ieee80211_channel *c;
921 u32 freq, start_freq, end_freq;
922 bool dfs_offload;
923
924 dfs_offload = wiphy_ext_feature_isset(wiphy,
925 NL80211_EXT_FEATURE_DFS_OFFLOAD);
926
927 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
928 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
929
930 /*
931 * Check entire range of channels for the bandwidth.
932 * If any channel in between is disabled or has not
933 * had gone through CAC return false
934 */
935 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
936 c = ieee80211_get_channel_khz(wiphy, freq);
937 if (!c)
938 return false;
939
940 if (c->flags & IEEE80211_CHAN_DISABLED)
941 return false;
942
943 if ((c->flags & IEEE80211_CHAN_RADAR) &&
944 (c->dfs_state != NL80211_DFS_AVAILABLE) &&
945 !(c->dfs_state == NL80211_DFS_USABLE && dfs_offload))
946 return false;
947 }
948
949 return true;
950 }
951
cfg80211_chandef_dfs_available(struct wiphy * wiphy,const struct cfg80211_chan_def * chandef)952 static bool cfg80211_chandef_dfs_available(struct wiphy *wiphy,
953 const struct cfg80211_chan_def *chandef)
954 {
955 int width;
956 int r;
957
958 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
959 return false;
960
961 width = cfg80211_chandef_get_width(chandef);
962 if (width < 0)
963 return false;
964
965 r = cfg80211_get_chans_dfs_available(wiphy,
966 MHZ_TO_KHZ(chandef->center_freq1),
967 width);
968
969 /* If any of channels unavailable for cf1 just return */
970 if (!r)
971 return r;
972
973 switch (chandef->width) {
974 case NL80211_CHAN_WIDTH_80P80:
975 WARN_ON(!chandef->center_freq2);
976 r = cfg80211_get_chans_dfs_available(wiphy,
977 MHZ_TO_KHZ(chandef->center_freq2),
978 width);
979 break;
980 default:
981 WARN_ON(chandef->center_freq2);
982 break;
983 }
984
985 return r;
986 }
987
cfg80211_get_chans_dfs_cac_time(struct wiphy * wiphy,u32 center_freq,u32 bandwidth)988 static unsigned int cfg80211_get_chans_dfs_cac_time(struct wiphy *wiphy,
989 u32 center_freq,
990 u32 bandwidth)
991 {
992 struct ieee80211_channel *c;
993 u32 start_freq, end_freq, freq;
994 unsigned int dfs_cac_ms = 0;
995
996 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
997 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
998
999 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
1000 c = ieee80211_get_channel_khz(wiphy, freq);
1001 if (!c)
1002 return 0;
1003
1004 if (c->flags & IEEE80211_CHAN_DISABLED)
1005 return 0;
1006
1007 if (!(c->flags & IEEE80211_CHAN_RADAR))
1008 continue;
1009
1010 if (c->dfs_cac_ms > dfs_cac_ms)
1011 dfs_cac_ms = c->dfs_cac_ms;
1012 }
1013
1014 return dfs_cac_ms;
1015 }
1016
1017 unsigned int
cfg80211_chandef_dfs_cac_time(struct wiphy * wiphy,const struct cfg80211_chan_def * chandef)1018 cfg80211_chandef_dfs_cac_time(struct wiphy *wiphy,
1019 const struct cfg80211_chan_def *chandef)
1020 {
1021 int width;
1022 unsigned int t1 = 0, t2 = 0;
1023
1024 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
1025 return 0;
1026
1027 width = cfg80211_chandef_get_width(chandef);
1028 if (width < 0)
1029 return 0;
1030
1031 t1 = cfg80211_get_chans_dfs_cac_time(wiphy,
1032 MHZ_TO_KHZ(chandef->center_freq1),
1033 width);
1034
1035 if (!chandef->center_freq2)
1036 return t1;
1037
1038 t2 = cfg80211_get_chans_dfs_cac_time(wiphy,
1039 MHZ_TO_KHZ(chandef->center_freq2),
1040 width);
1041
1042 return max(t1, t2);
1043 }
1044
cfg80211_secondary_chans_ok(struct wiphy * wiphy,u32 center_freq,u32 bandwidth,u32 prohibited_flags)1045 static bool cfg80211_secondary_chans_ok(struct wiphy *wiphy,
1046 u32 center_freq, u32 bandwidth,
1047 u32 prohibited_flags)
1048 {
1049 struct ieee80211_channel *c;
1050 u32 freq, start_freq, end_freq;
1051
1052 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
1053 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
1054
1055 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
1056 c = ieee80211_get_channel_khz(wiphy, freq);
1057 if (!c || c->flags & prohibited_flags)
1058 return false;
1059 }
1060
1061 return true;
1062 }
1063
1064 /* 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)1065 static bool cfg80211_edmg_usable(struct wiphy *wiphy, u8 edmg_channels,
1066 enum ieee80211_edmg_bw_config edmg_bw_config,
1067 int primary_channel,
1068 struct ieee80211_edmg *edmg_cap)
1069 {
1070 struct ieee80211_channel *chan;
1071 int i, freq;
1072 int channels_counter = 0;
1073
1074 if (!edmg_channels && !edmg_bw_config)
1075 return true;
1076
1077 if ((!edmg_channels && edmg_bw_config) ||
1078 (edmg_channels && !edmg_bw_config))
1079 return false;
1080
1081 if (!(edmg_channels & BIT(primary_channel - 1)))
1082 return false;
1083
1084 /* 60GHz channels 1..6 */
1085 for (i = 0; i < 6; i++) {
1086 if (!(edmg_channels & BIT(i)))
1087 continue;
1088
1089 if (!(edmg_cap->channels & BIT(i)))
1090 return false;
1091
1092 channels_counter++;
1093
1094 freq = ieee80211_channel_to_frequency(i + 1,
1095 NL80211_BAND_60GHZ);
1096 chan = ieee80211_get_channel(wiphy, freq);
1097 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1098 return false;
1099 }
1100
1101 /* IEEE802.11 allows max 4 channels */
1102 if (channels_counter > 4)
1103 return false;
1104
1105 /* check bw_config is a subset of what driver supports
1106 * (see IEEE P802.11ay/D4.0 section 9.4.2.251, Table 13)
1107 */
1108 if ((edmg_bw_config % 4) > (edmg_cap->bw_config % 4))
1109 return false;
1110
1111 if (edmg_bw_config > edmg_cap->bw_config)
1112 return false;
1113
1114 return true;
1115 }
1116
cfg80211_chandef_usable(struct wiphy * wiphy,const struct cfg80211_chan_def * chandef,u32 prohibited_flags)1117 bool cfg80211_chandef_usable(struct wiphy *wiphy,
1118 const struct cfg80211_chan_def *chandef,
1119 u32 prohibited_flags)
1120 {
1121 struct ieee80211_sta_ht_cap *ht_cap;
1122 struct ieee80211_sta_vht_cap *vht_cap;
1123 struct ieee80211_edmg *edmg_cap;
1124 u32 width, control_freq, cap;
1125 bool ext_nss_cap, support_80_80 = false, support_320 = false;
1126 const struct ieee80211_sband_iftype_data *iftd;
1127 struct ieee80211_supported_band *sband;
1128 int i;
1129
1130 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
1131 return false;
1132
1133 ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap;
1134 vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap;
1135 edmg_cap = &wiphy->bands[chandef->chan->band]->edmg_cap;
1136 ext_nss_cap = __le16_to_cpu(vht_cap->vht_mcs.tx_highest) &
1137 IEEE80211_VHT_EXT_NSS_BW_CAPABLE;
1138
1139 if (edmg_cap->channels &&
1140 !cfg80211_edmg_usable(wiphy,
1141 chandef->edmg.channels,
1142 chandef->edmg.bw_config,
1143 chandef->chan->hw_value,
1144 edmg_cap))
1145 return false;
1146
1147 control_freq = chandef->chan->center_freq;
1148
1149 switch (chandef->width) {
1150 case NL80211_CHAN_WIDTH_1:
1151 width = 1;
1152 break;
1153 case NL80211_CHAN_WIDTH_2:
1154 width = 2;
1155 break;
1156 case NL80211_CHAN_WIDTH_4:
1157 width = 4;
1158 break;
1159 case NL80211_CHAN_WIDTH_8:
1160 width = 8;
1161 break;
1162 case NL80211_CHAN_WIDTH_16:
1163 width = 16;
1164 break;
1165 case NL80211_CHAN_WIDTH_5:
1166 width = 5;
1167 break;
1168 case NL80211_CHAN_WIDTH_10:
1169 prohibited_flags |= IEEE80211_CHAN_NO_10MHZ;
1170 width = 10;
1171 break;
1172 case NL80211_CHAN_WIDTH_20:
1173 if (!ht_cap->ht_supported &&
1174 chandef->chan->band != NL80211_BAND_6GHZ)
1175 return false;
1176 fallthrough;
1177 case NL80211_CHAN_WIDTH_20_NOHT:
1178 prohibited_flags |= IEEE80211_CHAN_NO_20MHZ;
1179 width = 20;
1180 break;
1181 case NL80211_CHAN_WIDTH_40:
1182 width = 40;
1183 if (chandef->chan->band == NL80211_BAND_6GHZ)
1184 break;
1185 if (!ht_cap->ht_supported)
1186 return false;
1187 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
1188 ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)
1189 return false;
1190 if (chandef->center_freq1 < control_freq &&
1191 chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
1192 return false;
1193 if (chandef->center_freq1 > control_freq &&
1194 chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
1195 return false;
1196 break;
1197 case NL80211_CHAN_WIDTH_80P80:
1198 cap = vht_cap->cap;
1199 support_80_80 =
1200 (cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) ||
1201 (cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
1202 cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) ||
1203 (ext_nss_cap &&
1204 u32_get_bits(cap, IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) > 1);
1205 if (chandef->chan->band != NL80211_BAND_6GHZ && !support_80_80)
1206 return false;
1207 fallthrough;
1208 case NL80211_CHAN_WIDTH_80:
1209 prohibited_flags |= IEEE80211_CHAN_NO_80MHZ;
1210 width = 80;
1211 if (chandef->chan->band == NL80211_BAND_6GHZ)
1212 break;
1213 if (!vht_cap->vht_supported)
1214 return false;
1215 break;
1216 case NL80211_CHAN_WIDTH_160:
1217 prohibited_flags |= IEEE80211_CHAN_NO_160MHZ;
1218 width = 160;
1219 if (chandef->chan->band == NL80211_BAND_6GHZ)
1220 break;
1221 if (!vht_cap->vht_supported)
1222 return false;
1223 cap = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
1224 if (cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
1225 cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ &&
1226 !(ext_nss_cap &&
1227 (vht_cap->cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK)))
1228 return false;
1229 break;
1230 case NL80211_CHAN_WIDTH_320:
1231 prohibited_flags |= IEEE80211_CHAN_NO_320MHZ;
1232 width = 320;
1233
1234 if (chandef->chan->band != NL80211_BAND_6GHZ)
1235 return false;
1236
1237 sband = wiphy->bands[NL80211_BAND_6GHZ];
1238 if (!sband)
1239 return false;
1240
1241 for (i = 0; i < sband->n_iftype_data; i++) {
1242 iftd = &sband->iftype_data[i];
1243 if (!iftd->eht_cap.has_eht)
1244 continue;
1245
1246 if (iftd->eht_cap.eht_cap_elem.phy_cap_info[0] &
1247 IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ) {
1248 support_320 = true;
1249 break;
1250 }
1251 }
1252
1253 if (!support_320)
1254 return false;
1255 break;
1256 default:
1257 WARN_ON_ONCE(1);
1258 return false;
1259 }
1260
1261 /*
1262 * TODO: What if there are only certain 80/160/80+80 MHz channels
1263 * allowed by the driver, or only certain combinations?
1264 * For 40 MHz the driver can set the NO_HT40 flags, but for
1265 * 80/160 MHz and in particular 80+80 MHz this isn't really
1266 * feasible and we only have NO_80MHZ/NO_160MHZ so far but
1267 * no way to cover 80+80 MHz or more complex restrictions.
1268 * Note that such restrictions also need to be advertised to
1269 * userspace, for example for P2P channel selection.
1270 */
1271
1272 if (width > 20)
1273 prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
1274
1275 /* 5 and 10 MHz are only defined for the OFDM PHY */
1276 if (width < 20)
1277 prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
1278
1279
1280 if (!cfg80211_secondary_chans_ok(wiphy,
1281 ieee80211_chandef_to_khz(chandef),
1282 width, prohibited_flags))
1283 return false;
1284
1285 if (!chandef->center_freq2)
1286 return true;
1287 return cfg80211_secondary_chans_ok(wiphy,
1288 MHZ_TO_KHZ(chandef->center_freq2),
1289 width, prohibited_flags);
1290 }
1291 EXPORT_SYMBOL(cfg80211_chandef_usable);
1292
cfg80211_ir_permissive_check_wdev(enum nl80211_iftype iftype,struct wireless_dev * wdev,struct ieee80211_channel * chan)1293 static bool cfg80211_ir_permissive_check_wdev(enum nl80211_iftype iftype,
1294 struct wireless_dev *wdev,
1295 struct ieee80211_channel *chan)
1296 {
1297 struct ieee80211_channel *other_chan = NULL;
1298 unsigned int link_id;
1299 int r1, r2;
1300
1301 for_each_valid_link(wdev, link_id) {
1302 if (wdev->iftype == NL80211_IFTYPE_STATION &&
1303 wdev->links[link_id].client.current_bss)
1304 other_chan = wdev->links[link_id].client.current_bss->pub.channel;
1305
1306 /*
1307 * If a GO already operates on the same GO_CONCURRENT channel,
1308 * this one (maybe the same one) can beacon as well. We allow
1309 * the operation even if the station we relied on with
1310 * GO_CONCURRENT is disconnected now. But then we must make sure
1311 * we're not outdoor on an indoor-only channel.
1312 */
1313 if (iftype == NL80211_IFTYPE_P2P_GO &&
1314 wdev->iftype == NL80211_IFTYPE_P2P_GO &&
1315 wdev->links[link_id].ap.beacon_interval &&
1316 !(chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
1317 other_chan = wdev->links[link_id].ap.chandef.chan;
1318
1319 if (!other_chan)
1320 continue;
1321
1322 if (chan == other_chan)
1323 return true;
1324
1325 if (chan->band != NL80211_BAND_5GHZ &&
1326 chan->band != NL80211_BAND_6GHZ)
1327 continue;
1328
1329 r1 = cfg80211_get_unii(chan->center_freq);
1330 r2 = cfg80211_get_unii(other_chan->center_freq);
1331
1332 if (r1 != -EINVAL && r1 == r2) {
1333 /*
1334 * At some locations channels 149-165 are considered a
1335 * bundle, but at other locations, e.g., Indonesia,
1336 * channels 149-161 are considered a bundle while
1337 * channel 165 is left out and considered to be in a
1338 * different bundle. Thus, in case that there is a
1339 * station interface connected to an AP on channel 165,
1340 * it is assumed that channels 149-161 are allowed for
1341 * GO operations. However, having a station interface
1342 * connected to an AP on channels 149-161, does not
1343 * allow GO operation on channel 165.
1344 */
1345 if (chan->center_freq == 5825 &&
1346 other_chan->center_freq != 5825)
1347 continue;
1348 return true;
1349 }
1350 }
1351
1352 return false;
1353 }
1354
1355 /*
1356 * Check if the channel can be used under permissive conditions mandated by
1357 * some regulatory bodies, i.e., the channel is marked with
1358 * IEEE80211_CHAN_IR_CONCURRENT and there is an additional station interface
1359 * associated to an AP on the same channel or on the same UNII band
1360 * (assuming that the AP is an authorized master).
1361 * In addition allow operation on a channel on which indoor operation is
1362 * allowed, iff we are currently operating in an indoor environment.
1363 */
cfg80211_ir_permissive_chan(struct wiphy * wiphy,enum nl80211_iftype iftype,struct ieee80211_channel * chan)1364 static bool cfg80211_ir_permissive_chan(struct wiphy *wiphy,
1365 enum nl80211_iftype iftype,
1366 struct ieee80211_channel *chan)
1367 {
1368 struct wireless_dev *wdev;
1369 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1370
1371 lockdep_assert_held(&rdev->wiphy.mtx);
1372
1373 if (!IS_ENABLED(CONFIG_CFG80211_REG_RELAX_NO_IR) ||
1374 !(wiphy->regulatory_flags & REGULATORY_ENABLE_RELAX_NO_IR))
1375 return false;
1376
1377 /* only valid for GO and TDLS off-channel (station/p2p-CL) */
1378 if (iftype != NL80211_IFTYPE_P2P_GO &&
1379 iftype != NL80211_IFTYPE_STATION &&
1380 iftype != NL80211_IFTYPE_P2P_CLIENT)
1381 return false;
1382
1383 if (regulatory_indoor_allowed() &&
1384 (chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
1385 return true;
1386
1387 if (!(chan->flags & IEEE80211_CHAN_IR_CONCURRENT))
1388 return false;
1389
1390 /*
1391 * Generally, it is possible to rely on another device/driver to allow
1392 * the IR concurrent relaxation, however, since the device can further
1393 * enforce the relaxation (by doing a similar verifications as this),
1394 * and thus fail the GO instantiation, consider only the interfaces of
1395 * the current registered device.
1396 */
1397 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
1398 bool ret;
1399
1400 wdev_lock(wdev);
1401 ret = cfg80211_ir_permissive_check_wdev(iftype, wdev, chan);
1402 wdev_unlock(wdev);
1403
1404 if (ret)
1405 return ret;
1406 }
1407
1408 return false;
1409 }
1410
_cfg80211_reg_can_beacon(struct wiphy * wiphy,struct cfg80211_chan_def * chandef,enum nl80211_iftype iftype,bool check_no_ir)1411 static bool _cfg80211_reg_can_beacon(struct wiphy *wiphy,
1412 struct cfg80211_chan_def *chandef,
1413 enum nl80211_iftype iftype,
1414 bool check_no_ir)
1415 {
1416 bool res;
1417 u32 prohibited_flags = IEEE80211_CHAN_DISABLED;
1418 int dfs_required;
1419
1420 trace_cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir);
1421
1422 if (check_no_ir)
1423 prohibited_flags |= IEEE80211_CHAN_NO_IR;
1424
1425 dfs_required = cfg80211_chandef_dfs_required(wiphy, chandef, iftype);
1426 if (dfs_required != 0)
1427 prohibited_flags |= IEEE80211_CHAN_RADAR;
1428
1429 if (dfs_required > 0 &&
1430 cfg80211_chandef_dfs_available(wiphy, chandef)) {
1431 /* We can skip IEEE80211_CHAN_NO_IR if chandef dfs available */
1432 prohibited_flags = IEEE80211_CHAN_DISABLED;
1433 }
1434
1435 res = cfg80211_chandef_usable(wiphy, chandef, prohibited_flags);
1436
1437 trace_cfg80211_return_bool(res);
1438 return res;
1439 }
1440
cfg80211_reg_can_beacon(struct wiphy * wiphy,struct cfg80211_chan_def * chandef,enum nl80211_iftype iftype)1441 bool cfg80211_reg_can_beacon(struct wiphy *wiphy,
1442 struct cfg80211_chan_def *chandef,
1443 enum nl80211_iftype iftype)
1444 {
1445 return _cfg80211_reg_can_beacon(wiphy, chandef, iftype, true);
1446 }
1447 EXPORT_SYMBOL(cfg80211_reg_can_beacon);
1448
cfg80211_reg_can_beacon_relax(struct wiphy * wiphy,struct cfg80211_chan_def * chandef,enum nl80211_iftype iftype)1449 bool cfg80211_reg_can_beacon_relax(struct wiphy *wiphy,
1450 struct cfg80211_chan_def *chandef,
1451 enum nl80211_iftype iftype)
1452 {
1453 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1454 bool check_no_ir;
1455
1456 lockdep_assert_held(&rdev->wiphy.mtx);
1457
1458 /*
1459 * Under certain conditions suggested by some regulatory bodies a
1460 * GO/STA can IR on channels marked with IEEE80211_NO_IR. Set this flag
1461 * only if such relaxations are not enabled and the conditions are not
1462 * met.
1463 */
1464 check_no_ir = !cfg80211_ir_permissive_chan(wiphy, iftype,
1465 chandef->chan);
1466
1467 return _cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir);
1468 }
1469 EXPORT_SYMBOL(cfg80211_reg_can_beacon_relax);
1470
cfg80211_set_monitor_channel(struct cfg80211_registered_device * rdev,struct cfg80211_chan_def * chandef)1471 int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
1472 struct cfg80211_chan_def *chandef)
1473 {
1474 if (!rdev->ops->set_monitor_channel)
1475 return -EOPNOTSUPP;
1476 if (!cfg80211_has_monitors_only(rdev))
1477 return -EBUSY;
1478
1479 return rdev_set_monitor_channel(rdev, chandef);
1480 }
1481
cfg80211_any_usable_channels(struct wiphy * wiphy,unsigned long sband_mask,u32 prohibited_flags)1482 bool cfg80211_any_usable_channels(struct wiphy *wiphy,
1483 unsigned long sband_mask,
1484 u32 prohibited_flags)
1485 {
1486 int idx;
1487
1488 prohibited_flags |= IEEE80211_CHAN_DISABLED;
1489
1490 for_each_set_bit(idx, &sband_mask, NUM_NL80211_BANDS) {
1491 struct ieee80211_supported_band *sband = wiphy->bands[idx];
1492 int chanidx;
1493
1494 if (!sband)
1495 continue;
1496
1497 for (chanidx = 0; chanidx < sband->n_channels; chanidx++) {
1498 struct ieee80211_channel *chan;
1499
1500 chan = &sband->channels[chanidx];
1501
1502 if (chan->flags & prohibited_flags)
1503 continue;
1504
1505 return true;
1506 }
1507 }
1508
1509 return false;
1510 }
1511 EXPORT_SYMBOL(cfg80211_any_usable_channels);
1512
wdev_chandef(struct wireless_dev * wdev,unsigned int link_id)1513 struct cfg80211_chan_def *wdev_chandef(struct wireless_dev *wdev,
1514 unsigned int link_id)
1515 {
1516 /*
1517 * We need to sort out the locking here - in some cases
1518 * where we get here we really just don't care (yet)
1519 * about the valid links, but in others we do. But we
1520 * get here with various driver cases, so we cannot
1521 * easily require the wdev mutex.
1522 */
1523 if (link_id || wdev->valid_links & BIT(0)) {
1524 ASSERT_WDEV_LOCK(wdev);
1525 WARN_ON(!(wdev->valid_links & BIT(link_id)));
1526 }
1527
1528 switch (wdev->iftype) {
1529 case NL80211_IFTYPE_MESH_POINT:
1530 return &wdev->u.mesh.chandef;
1531 case NL80211_IFTYPE_ADHOC:
1532 return &wdev->u.ibss.chandef;
1533 case NL80211_IFTYPE_OCB:
1534 return &wdev->u.ocb.chandef;
1535 case NL80211_IFTYPE_AP:
1536 case NL80211_IFTYPE_P2P_GO:
1537 return &wdev->links[link_id].ap.chandef;
1538 default:
1539 return NULL;
1540 }
1541 }
1542 EXPORT_SYMBOL(wdev_chandef);
1543
1544 struct cfg80211_per_bw_puncturing_values {
1545 u8 len;
1546 const u16 *valid_values;
1547 };
1548
1549 static const u16 puncturing_values_80mhz[] = {
1550 0x8, 0x4, 0x2, 0x1
1551 };
1552
1553 static const u16 puncturing_values_160mhz[] = {
1554 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1, 0xc0, 0x30, 0xc, 0x3
1555 };
1556
1557 static const u16 puncturing_values_320mhz[] = {
1558 0xc000, 0x3000, 0xc00, 0x300, 0xc0, 0x30, 0xc, 0x3, 0xf000, 0xf00,
1559 0xf0, 0xf, 0xfc00, 0xf300, 0xf0c0, 0xf030, 0xf00c, 0xf003, 0xc00f,
1560 0x300f, 0xc0f, 0x30f, 0xcf, 0x3f
1561 };
1562
1563 #define CFG80211_PER_BW_VALID_PUNCTURING_VALUES(_bw) \
1564 { \
1565 .len = ARRAY_SIZE(puncturing_values_ ## _bw ## mhz), \
1566 .valid_values = puncturing_values_ ## _bw ## mhz \
1567 }
1568
1569 static const struct cfg80211_per_bw_puncturing_values per_bw_puncturing[] = {
1570 CFG80211_PER_BW_VALID_PUNCTURING_VALUES(80),
1571 CFG80211_PER_BW_VALID_PUNCTURING_VALUES(160),
1572 CFG80211_PER_BW_VALID_PUNCTURING_VALUES(320)
1573 };
1574
cfg80211_valid_disable_subchannel_bitmap(u16 * bitmap,const struct cfg80211_chan_def * chandef)1575 bool cfg80211_valid_disable_subchannel_bitmap(u16 *bitmap,
1576 const struct cfg80211_chan_def *chandef)
1577 {
1578 u32 idx, i, start_freq;
1579
1580 switch (chandef->width) {
1581 case NL80211_CHAN_WIDTH_80:
1582 idx = 0;
1583 start_freq = chandef->center_freq1 - 40;
1584 break;
1585 case NL80211_CHAN_WIDTH_160:
1586 idx = 1;
1587 start_freq = chandef->center_freq1 - 80;
1588 break;
1589 case NL80211_CHAN_WIDTH_320:
1590 idx = 2;
1591 start_freq = chandef->center_freq1 - 160;
1592 break;
1593 default:
1594 *bitmap = 0;
1595 break;
1596 }
1597
1598 if (!*bitmap)
1599 return true;
1600
1601 /* check if primary channel is punctured */
1602 if (*bitmap & (u16)BIT((chandef->chan->center_freq - start_freq) / 20))
1603 return false;
1604
1605 for (i = 0; i < per_bw_puncturing[idx].len; i++)
1606 if (per_bw_puncturing[idx].valid_values[i] == *bitmap)
1607 return true;
1608
1609 return false;
1610 }
1611 EXPORT_SYMBOL(cfg80211_valid_disable_subchannel_bitmap);
1612