1 // SPDX-License-Identifier: GPL-2.0
3 * This file contains helper code to handle channel
4 * settings and keeping track of what is possible at
7 * Copyright 2009 Johannes Berg <johannes@sipsolutions.net>
8 * Copyright 2013-2014 Intel Mobile Communications GmbH
9 * Copyright 2018-2022 Intel Corporation
12 #include <linux/export.h>
13 #include <linux/bitfield.h>
14 #include <net/cfg80211.h>
18 static bool cfg80211_valid_60g_freq(u32 freq)
20 return freq >= 58320 && freq <= 70200;
23 void cfg80211_chandef_create(struct cfg80211_chan_def *chandef,
24 struct ieee80211_channel *chan,
25 enum nl80211_channel_type chan_type)
31 chandef->freq1_offset = chan->freq_offset;
32 chandef->center_freq2 = 0;
33 chandef->edmg.bw_config = 0;
34 chandef->edmg.channels = 0;
37 case NL80211_CHAN_NO_HT:
38 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
39 chandef->center_freq1 = chan->center_freq;
41 case NL80211_CHAN_HT20:
42 chandef->width = NL80211_CHAN_WIDTH_20;
43 chandef->center_freq1 = chan->center_freq;
45 case NL80211_CHAN_HT40PLUS:
46 chandef->width = NL80211_CHAN_WIDTH_40;
47 chandef->center_freq1 = chan->center_freq + 10;
49 case NL80211_CHAN_HT40MINUS:
50 chandef->width = NL80211_CHAN_WIDTH_40;
51 chandef->center_freq1 = chan->center_freq - 10;
57 EXPORT_SYMBOL(cfg80211_chandef_create);
59 static bool cfg80211_edmg_chandef_valid(const struct cfg80211_chan_def *chandef)
61 int max_contiguous = 0;
62 int num_of_enabled = 0;
66 if (!chandef->edmg.channels || !chandef->edmg.bw_config)
69 if (!cfg80211_valid_60g_freq(chandef->chan->center_freq))
72 for (i = 0; i < 6; i++) {
73 if (chandef->edmg.channels & BIT(i)) {
80 max_contiguous = max(contiguous, max_contiguous);
82 /* basic verification of edmg configuration according to
83 * IEEE P802.11ay/D4.0 section 9.4.2.251
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)
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)
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)
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)
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:
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)
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)
144 static int nl80211_chan_width_to_mhz(enum nl80211_chan_width chan_width)
148 switch (chan_width) {
149 case NL80211_CHAN_WIDTH_1:
152 case NL80211_CHAN_WIDTH_2:
155 case NL80211_CHAN_WIDTH_4:
158 case NL80211_CHAN_WIDTH_8:
161 case NL80211_CHAN_WIDTH_16:
164 case NL80211_CHAN_WIDTH_5:
167 case NL80211_CHAN_WIDTH_10:
170 case NL80211_CHAN_WIDTH_20:
171 case NL80211_CHAN_WIDTH_20_NOHT:
174 case NL80211_CHAN_WIDTH_40:
177 case NL80211_CHAN_WIDTH_80P80:
178 case NL80211_CHAN_WIDTH_80:
181 case NL80211_CHAN_WIDTH_160:
184 case NL80211_CHAN_WIDTH_320:
194 static int cfg80211_chandef_get_width(const struct cfg80211_chan_def *c)
196 return nl80211_chan_width_to_mhz(c->width);
199 bool cfg80211_chandef_valid(const struct cfg80211_chan_def *chandef)
201 u32 control_freq, oper_freq;
202 int oper_width, control_width;
207 if (chandef->freq1_offset >= 1000)
210 control_freq = chandef->chan->center_freq;
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))
220 if (chandef->center_freq2)
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)
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(
236 oper_width = cfg80211_chandef_get_width(chandef);
238 if (oper_width < 0 || control_width < 0)
240 if (chandef->center_freq2)
243 if (control_freq + MHZ_TO_KHZ(control_width) / 2 >
244 oper_freq + MHZ_TO_KHZ(oper_width) / 2)
247 if (control_freq - MHZ_TO_KHZ(control_width) / 2 <
248 oper_freq - MHZ_TO_KHZ(oper_width) / 2)
251 case NL80211_CHAN_WIDTH_80P80:
252 if (!chandef->center_freq2)
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)
260 if (chandef->center_freq2)
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 */
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)
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)
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)
301 case NL80211_CHAN_WIDTH_40:
302 if (chandef->center_freq1 == control_freq + 10 ||
303 chandef->center_freq1 == control_freq - 10)
310 /* channel 14 is only for IEEE 802.11b */
311 if (chandef->center_freq1 == 2484 &&
312 chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
315 if (cfg80211_chandef_is_edmg(chandef) &&
316 !cfg80211_edmg_chandef_valid(chandef))
321 EXPORT_SYMBOL(cfg80211_chandef_valid);
323 static void chandef_primary_freqs(const struct cfg80211_chan_def *c,
324 u32 *pri40, u32 *pri80, u32 *pri160)
329 case NL80211_CHAN_WIDTH_40:
330 *pri40 = c->center_freq1;
334 case NL80211_CHAN_WIDTH_80:
335 case NL80211_CHAN_WIDTH_80P80:
337 *pri80 = c->center_freq1;
339 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
343 *pri40 = c->center_freq1 - 20 + 40 * tmp;
345 case NL80211_CHAN_WIDTH_160:
346 *pri160 = c->center_freq1;
348 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
352 *pri40 = c->center_freq1 - 60 + 40 * tmp;
355 *pri80 = c->center_freq1 - 40 + 80 * tmp;
357 case NL80211_CHAN_WIDTH_320:
359 tmp = (150 + c->chan->center_freq - c->center_freq1) / 20;
363 *pri40 = c->center_freq1 - 140 + 40 * tmp;
366 *pri80 = c->center_freq1 - 120 + 80 * tmp;
369 *pri160 = c->center_freq1 - 80 + 160 * tmp;
376 const struct cfg80211_chan_def *
377 cfg80211_chandef_compatible(const struct cfg80211_chan_def *c1,
378 const struct cfg80211_chan_def *c2)
380 u32 c1_pri40, c1_pri80, c2_pri40, c2_pri80, c1_pri160, c2_pri160;
382 /* If they are identical, return */
383 if (cfg80211_chandef_identical(c1, c2))
386 /* otherwise, must have same control channel */
387 if (c1->chan != c2->chan)
391 * If they have the same width, but aren't identical,
392 * then they can't be compatible.
394 if (c1->width == c2->width)
398 * can't be compatible if one of them is 5 or 10 MHz,
399 * but they don't have the same width.
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)
407 if (c1->width == NL80211_CHAN_WIDTH_20_NOHT ||
408 c1->width == NL80211_CHAN_WIDTH_20)
411 if (c2->width == NL80211_CHAN_WIDTH_20_NOHT ||
412 c2->width == NL80211_CHAN_WIDTH_20)
415 chandef_primary_freqs(c1, &c1_pri40, &c1_pri80, &c1_pri160);
416 chandef_primary_freqs(c2, &c2_pri40, &c2_pri80, &c2_pri160);
418 if (c1_pri40 != c2_pri40)
421 if (c1->width == NL80211_CHAN_WIDTH_40)
424 if (c2->width == NL80211_CHAN_WIDTH_40)
427 if (c1_pri80 != c2_pri80)
430 if (c1->width == NL80211_CHAN_WIDTH_80 &&
431 c2->width > NL80211_CHAN_WIDTH_80)
434 if (c2->width == NL80211_CHAN_WIDTH_80 &&
435 c1->width > NL80211_CHAN_WIDTH_80)
438 WARN_ON(!c1_pri160 && !c2_pri160);
439 if (c1_pri160 && c2_pri160 && c1_pri160 != c2_pri160)
442 if (c1->width > c2->width)
446 EXPORT_SYMBOL(cfg80211_chandef_compatible);
448 static void cfg80211_set_chans_dfs_state(struct wiphy *wiphy, u32 center_freq,
450 enum nl80211_dfs_state dfs_state)
452 struct ieee80211_channel *c;
455 for (freq = center_freq - bandwidth/2 + 10;
456 freq <= center_freq + bandwidth/2 - 10;
458 c = ieee80211_get_channel(wiphy, freq);
459 if (!c || !(c->flags & IEEE80211_CHAN_RADAR))
462 c->dfs_state = dfs_state;
463 c->dfs_state_entered = jiffies;
467 void cfg80211_set_dfs_state(struct wiphy *wiphy,
468 const struct cfg80211_chan_def *chandef,
469 enum nl80211_dfs_state dfs_state)
473 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
476 width = cfg80211_chandef_get_width(chandef);
480 cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq1,
483 if (!chandef->center_freq2)
485 cfg80211_set_chans_dfs_state(wiphy, chandef->center_freq2,
489 static u32 cfg80211_get_start_freq(u32 center_freq,
494 bandwidth = MHZ_TO_KHZ(bandwidth);
495 if (bandwidth <= MHZ_TO_KHZ(20))
496 start_freq = center_freq;
498 start_freq = center_freq - bandwidth / 2 + MHZ_TO_KHZ(10);
503 static u32 cfg80211_get_end_freq(u32 center_freq,
508 bandwidth = MHZ_TO_KHZ(bandwidth);
509 if (bandwidth <= MHZ_TO_KHZ(20))
510 end_freq = center_freq;
512 end_freq = center_freq + bandwidth / 2 - MHZ_TO_KHZ(10);
517 static int cfg80211_get_chans_dfs_required(struct wiphy *wiphy,
521 struct ieee80211_channel *c;
522 u32 freq, start_freq, end_freq;
524 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
525 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
527 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
528 c = ieee80211_get_channel_khz(wiphy, freq);
532 if (c->flags & IEEE80211_CHAN_RADAR)
539 int cfg80211_chandef_dfs_required(struct wiphy *wiphy,
540 const struct cfg80211_chan_def *chandef,
541 enum nl80211_iftype iftype)
546 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
550 case NL80211_IFTYPE_ADHOC:
551 case NL80211_IFTYPE_AP:
552 case NL80211_IFTYPE_P2P_GO:
553 case NL80211_IFTYPE_MESH_POINT:
554 width = cfg80211_chandef_get_width(chandef);
558 ret = cfg80211_get_chans_dfs_required(wiphy,
559 ieee80211_chandef_to_khz(chandef),
564 return BIT(chandef->width);
566 if (!chandef->center_freq2)
569 ret = cfg80211_get_chans_dfs_required(wiphy,
570 MHZ_TO_KHZ(chandef->center_freq2),
575 return BIT(chandef->width);
578 case NL80211_IFTYPE_STATION:
579 case NL80211_IFTYPE_OCB:
580 case NL80211_IFTYPE_P2P_CLIENT:
581 case NL80211_IFTYPE_MONITOR:
582 case NL80211_IFTYPE_AP_VLAN:
583 case NL80211_IFTYPE_P2P_DEVICE:
584 case NL80211_IFTYPE_NAN:
586 case NL80211_IFTYPE_WDS:
587 case NL80211_IFTYPE_UNSPECIFIED:
588 case NUM_NL80211_IFTYPES:
594 EXPORT_SYMBOL(cfg80211_chandef_dfs_required);
596 static int cfg80211_get_chans_dfs_usable(struct wiphy *wiphy,
600 struct ieee80211_channel *c;
601 u32 freq, start_freq, end_freq;
604 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
605 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
608 * Check entire range of channels for the bandwidth.
609 * Check all channels are DFS channels (DFS_USABLE or
610 * DFS_AVAILABLE). Return number of usable channels
611 * (require CAC). Allow DFS and non-DFS channel mix.
613 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
614 c = ieee80211_get_channel_khz(wiphy, freq);
618 if (c->flags & IEEE80211_CHAN_DISABLED)
621 if (c->flags & IEEE80211_CHAN_RADAR) {
622 if (c->dfs_state == NL80211_DFS_UNAVAILABLE)
625 if (c->dfs_state == NL80211_DFS_USABLE)
633 bool cfg80211_chandef_dfs_usable(struct wiphy *wiphy,
634 const struct cfg80211_chan_def *chandef)
639 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
642 width = cfg80211_chandef_get_width(chandef);
646 r1 = cfg80211_get_chans_dfs_usable(wiphy,
647 MHZ_TO_KHZ(chandef->center_freq1),
653 switch (chandef->width) {
654 case NL80211_CHAN_WIDTH_80P80:
655 WARN_ON(!chandef->center_freq2);
656 r2 = cfg80211_get_chans_dfs_usable(wiphy,
657 MHZ_TO_KHZ(chandef->center_freq2),
663 WARN_ON(chandef->center_freq2);
667 return (r1 + r2 > 0);
671 * Checks if center frequency of chan falls with in the bandwidth
674 bool cfg80211_is_sub_chan(struct cfg80211_chan_def *chandef,
675 struct ieee80211_channel *chan,
684 if (chandef->chan->center_freq == chan->center_freq)
690 width = cfg80211_chandef_get_width(chandef);
694 for (freq = chandef->center_freq1 - width / 2 + 10;
695 freq <= chandef->center_freq1 + width / 2 - 10; freq += 20) {
696 if (chan->center_freq == freq)
700 if (!chandef->center_freq2)
703 for (freq = chandef->center_freq2 - width / 2 + 10;
704 freq <= chandef->center_freq2 + width / 2 - 10; freq += 20) {
705 if (chan->center_freq == freq)
712 bool cfg80211_beaconing_iface_active(struct wireless_dev *wdev)
716 ASSERT_WDEV_LOCK(wdev);
718 switch (wdev->iftype) {
719 case NL80211_IFTYPE_AP:
720 case NL80211_IFTYPE_P2P_GO:
721 for_each_valid_link(wdev, link) {
722 if (wdev->links[link].ap.beacon_interval)
726 case NL80211_IFTYPE_ADHOC:
727 if (wdev->u.ibss.ssid_len)
730 case NL80211_IFTYPE_MESH_POINT:
731 if (wdev->u.mesh.id_len)
734 case NL80211_IFTYPE_STATION:
735 case NL80211_IFTYPE_OCB:
736 case NL80211_IFTYPE_P2P_CLIENT:
737 case NL80211_IFTYPE_MONITOR:
738 case NL80211_IFTYPE_AP_VLAN:
739 case NL80211_IFTYPE_P2P_DEVICE:
740 /* Can NAN type be considered as beaconing interface? */
741 case NL80211_IFTYPE_NAN:
743 case NL80211_IFTYPE_UNSPECIFIED:
744 case NL80211_IFTYPE_WDS:
745 case NUM_NL80211_IFTYPES:
752 bool cfg80211_wdev_on_sub_chan(struct wireless_dev *wdev,
753 struct ieee80211_channel *chan,
758 switch (wdev->iftype) {
759 case NL80211_IFTYPE_AP:
760 case NL80211_IFTYPE_P2P_GO:
761 for_each_valid_link(wdev, link) {
762 if (cfg80211_is_sub_chan(&wdev->links[link].ap.chandef,
767 case NL80211_IFTYPE_ADHOC:
768 return cfg80211_is_sub_chan(&wdev->u.ibss.chandef, chan,
770 case NL80211_IFTYPE_MESH_POINT:
771 return cfg80211_is_sub_chan(&wdev->u.mesh.chandef, chan,
780 static bool cfg80211_is_wiphy_oper_chan(struct wiphy *wiphy,
781 struct ieee80211_channel *chan)
783 struct wireless_dev *wdev;
785 list_for_each_entry(wdev, &wiphy->wdev_list, list) {
787 if (!cfg80211_beaconing_iface_active(wdev)) {
792 if (cfg80211_wdev_on_sub_chan(wdev, chan, false)) {
803 cfg80211_offchan_chain_is_active(struct cfg80211_registered_device *rdev,
804 struct ieee80211_channel *channel)
806 if (!rdev->background_radar_wdev)
809 if (!cfg80211_chandef_valid(&rdev->background_radar_chandef))
812 return cfg80211_is_sub_chan(&rdev->background_radar_chandef, channel,
816 bool cfg80211_any_wiphy_oper_chan(struct wiphy *wiphy,
817 struct ieee80211_channel *chan)
819 struct cfg80211_registered_device *rdev;
823 if (!(chan->flags & IEEE80211_CHAN_RADAR))
826 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
827 if (!reg_dfs_domain_same(wiphy, &rdev->wiphy))
830 if (cfg80211_is_wiphy_oper_chan(&rdev->wiphy, chan))
833 if (cfg80211_offchan_chain_is_active(rdev, chan))
840 static bool cfg80211_get_chans_dfs_available(struct wiphy *wiphy,
844 struct ieee80211_channel *c;
845 u32 freq, start_freq, end_freq;
848 dfs_offload = wiphy_ext_feature_isset(wiphy,
849 NL80211_EXT_FEATURE_DFS_OFFLOAD);
851 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
852 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
855 * Check entire range of channels for the bandwidth.
856 * If any channel in between is disabled or has not
857 * had gone through CAC return false
859 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
860 c = ieee80211_get_channel_khz(wiphy, freq);
864 if (c->flags & IEEE80211_CHAN_DISABLED)
867 if ((c->flags & IEEE80211_CHAN_RADAR) &&
868 (c->dfs_state != NL80211_DFS_AVAILABLE) &&
869 !(c->dfs_state == NL80211_DFS_USABLE && dfs_offload))
876 static bool cfg80211_chandef_dfs_available(struct wiphy *wiphy,
877 const struct cfg80211_chan_def *chandef)
882 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
885 width = cfg80211_chandef_get_width(chandef);
889 r = cfg80211_get_chans_dfs_available(wiphy,
890 MHZ_TO_KHZ(chandef->center_freq1),
893 /* If any of channels unavailable for cf1 just return */
897 switch (chandef->width) {
898 case NL80211_CHAN_WIDTH_80P80:
899 WARN_ON(!chandef->center_freq2);
900 r = cfg80211_get_chans_dfs_available(wiphy,
901 MHZ_TO_KHZ(chandef->center_freq2),
905 WARN_ON(chandef->center_freq2);
912 static unsigned int cfg80211_get_chans_dfs_cac_time(struct wiphy *wiphy,
916 struct ieee80211_channel *c;
917 u32 start_freq, end_freq, freq;
918 unsigned int dfs_cac_ms = 0;
920 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
921 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
923 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
924 c = ieee80211_get_channel_khz(wiphy, freq);
928 if (c->flags & IEEE80211_CHAN_DISABLED)
931 if (!(c->flags & IEEE80211_CHAN_RADAR))
934 if (c->dfs_cac_ms > dfs_cac_ms)
935 dfs_cac_ms = c->dfs_cac_ms;
942 cfg80211_chandef_dfs_cac_time(struct wiphy *wiphy,
943 const struct cfg80211_chan_def *chandef)
946 unsigned int t1 = 0, t2 = 0;
948 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
951 width = cfg80211_chandef_get_width(chandef);
955 t1 = cfg80211_get_chans_dfs_cac_time(wiphy,
956 MHZ_TO_KHZ(chandef->center_freq1),
959 if (!chandef->center_freq2)
962 t2 = cfg80211_get_chans_dfs_cac_time(wiphy,
963 MHZ_TO_KHZ(chandef->center_freq2),
969 static bool cfg80211_secondary_chans_ok(struct wiphy *wiphy,
970 u32 center_freq, u32 bandwidth,
971 u32 prohibited_flags)
973 struct ieee80211_channel *c;
974 u32 freq, start_freq, end_freq;
976 start_freq = cfg80211_get_start_freq(center_freq, bandwidth);
977 end_freq = cfg80211_get_end_freq(center_freq, bandwidth);
979 for (freq = start_freq; freq <= end_freq; freq += MHZ_TO_KHZ(20)) {
980 c = ieee80211_get_channel_khz(wiphy, freq);
981 if (!c || c->flags & prohibited_flags)
988 /* check if the operating channels are valid and supported */
989 static bool cfg80211_edmg_usable(struct wiphy *wiphy, u8 edmg_channels,
990 enum ieee80211_edmg_bw_config edmg_bw_config,
992 struct ieee80211_edmg *edmg_cap)
994 struct ieee80211_channel *chan;
996 int channels_counter = 0;
998 if (!edmg_channels && !edmg_bw_config)
1001 if ((!edmg_channels && edmg_bw_config) ||
1002 (edmg_channels && !edmg_bw_config))
1005 if (!(edmg_channels & BIT(primary_channel - 1)))
1008 /* 60GHz channels 1..6 */
1009 for (i = 0; i < 6; i++) {
1010 if (!(edmg_channels & BIT(i)))
1013 if (!(edmg_cap->channels & BIT(i)))
1018 freq = ieee80211_channel_to_frequency(i + 1,
1019 NL80211_BAND_60GHZ);
1020 chan = ieee80211_get_channel(wiphy, freq);
1021 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
1025 /* IEEE802.11 allows max 4 channels */
1026 if (channels_counter > 4)
1029 /* check bw_config is a subset of what driver supports
1030 * (see IEEE P802.11ay/D4.0 section 9.4.2.251, Table 13)
1032 if ((edmg_bw_config % 4) > (edmg_cap->bw_config % 4))
1035 if (edmg_bw_config > edmg_cap->bw_config)
1041 bool cfg80211_chandef_usable(struct wiphy *wiphy,
1042 const struct cfg80211_chan_def *chandef,
1043 u32 prohibited_flags)
1045 struct ieee80211_sta_ht_cap *ht_cap;
1046 struct ieee80211_sta_vht_cap *vht_cap;
1047 struct ieee80211_edmg *edmg_cap;
1048 u32 width, control_freq, cap;
1049 bool ext_nss_cap, support_80_80 = false, support_320 = false;
1050 const struct ieee80211_sband_iftype_data *iftd;
1051 struct ieee80211_supported_band *sband;
1054 if (WARN_ON(!cfg80211_chandef_valid(chandef)))
1057 ht_cap = &wiphy->bands[chandef->chan->band]->ht_cap;
1058 vht_cap = &wiphy->bands[chandef->chan->band]->vht_cap;
1059 edmg_cap = &wiphy->bands[chandef->chan->band]->edmg_cap;
1060 ext_nss_cap = __le16_to_cpu(vht_cap->vht_mcs.tx_highest) &
1061 IEEE80211_VHT_EXT_NSS_BW_CAPABLE;
1063 if (edmg_cap->channels &&
1064 !cfg80211_edmg_usable(wiphy,
1065 chandef->edmg.channels,
1066 chandef->edmg.bw_config,
1067 chandef->chan->hw_value,
1071 control_freq = chandef->chan->center_freq;
1073 switch (chandef->width) {
1074 case NL80211_CHAN_WIDTH_1:
1077 case NL80211_CHAN_WIDTH_2:
1080 case NL80211_CHAN_WIDTH_4:
1083 case NL80211_CHAN_WIDTH_8:
1086 case NL80211_CHAN_WIDTH_16:
1089 case NL80211_CHAN_WIDTH_5:
1092 case NL80211_CHAN_WIDTH_10:
1093 prohibited_flags |= IEEE80211_CHAN_NO_10MHZ;
1096 case NL80211_CHAN_WIDTH_20:
1097 if (!ht_cap->ht_supported &&
1098 chandef->chan->band != NL80211_BAND_6GHZ)
1101 case NL80211_CHAN_WIDTH_20_NOHT:
1102 prohibited_flags |= IEEE80211_CHAN_NO_20MHZ;
1105 case NL80211_CHAN_WIDTH_40:
1107 if (chandef->chan->band == NL80211_BAND_6GHZ)
1109 if (!ht_cap->ht_supported)
1111 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
1112 ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)
1114 if (chandef->center_freq1 < control_freq &&
1115 chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
1117 if (chandef->center_freq1 > control_freq &&
1118 chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
1121 case NL80211_CHAN_WIDTH_80P80:
1124 (cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ) ||
1125 (cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
1126 cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) ||
1128 u32_get_bits(cap, IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) > 1);
1129 if (chandef->chan->band != NL80211_BAND_6GHZ && !support_80_80)
1132 case NL80211_CHAN_WIDTH_80:
1133 prohibited_flags |= IEEE80211_CHAN_NO_80MHZ;
1135 if (chandef->chan->band == NL80211_BAND_6GHZ)
1137 if (!vht_cap->vht_supported)
1140 case NL80211_CHAN_WIDTH_160:
1141 prohibited_flags |= IEEE80211_CHAN_NO_160MHZ;
1143 if (chandef->chan->band == NL80211_BAND_6GHZ)
1145 if (!vht_cap->vht_supported)
1147 cap = vht_cap->cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
1148 if (cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ &&
1149 cap != IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ &&
1151 (vht_cap->cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK)))
1154 case NL80211_CHAN_WIDTH_320:
1155 prohibited_flags |= IEEE80211_CHAN_NO_320MHZ;
1158 if (chandef->chan->band != NL80211_BAND_6GHZ)
1161 sband = wiphy->bands[NL80211_BAND_6GHZ];
1165 for (i = 0; i < sband->n_iftype_data; i++) {
1166 iftd = &sband->iftype_data[i];
1167 if (!iftd->eht_cap.has_eht)
1170 if (iftd->eht_cap.eht_cap_elem.phy_cap_info[0] &
1171 IEEE80211_EHT_PHY_CAP0_320MHZ_IN_6GHZ) {
1186 * TODO: What if there are only certain 80/160/80+80 MHz channels
1187 * allowed by the driver, or only certain combinations?
1188 * For 40 MHz the driver can set the NO_HT40 flags, but for
1189 * 80/160 MHz and in particular 80+80 MHz this isn't really
1190 * feasible and we only have NO_80MHZ/NO_160MHZ so far but
1191 * no way to cover 80+80 MHz or more complex restrictions.
1192 * Note that such restrictions also need to be advertised to
1193 * userspace, for example for P2P channel selection.
1197 prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
1199 /* 5 and 10 MHz are only defined for the OFDM PHY */
1201 prohibited_flags |= IEEE80211_CHAN_NO_OFDM;
1204 if (!cfg80211_secondary_chans_ok(wiphy,
1205 ieee80211_chandef_to_khz(chandef),
1206 width, prohibited_flags))
1209 if (!chandef->center_freq2)
1211 return cfg80211_secondary_chans_ok(wiphy,
1212 MHZ_TO_KHZ(chandef->center_freq2),
1213 width, prohibited_flags);
1215 EXPORT_SYMBOL(cfg80211_chandef_usable);
1217 static bool cfg80211_ir_permissive_check_wdev(enum nl80211_iftype iftype,
1218 struct wireless_dev *wdev,
1219 struct ieee80211_channel *chan)
1221 struct ieee80211_channel *other_chan = NULL;
1222 unsigned int link_id;
1225 for_each_valid_link(wdev, link_id) {
1226 if (wdev->iftype == NL80211_IFTYPE_STATION &&
1227 wdev->links[link_id].client.current_bss)
1228 other_chan = wdev->links[link_id].client.current_bss->pub.channel;
1231 * If a GO already operates on the same GO_CONCURRENT channel,
1232 * this one (maybe the same one) can beacon as well. We allow
1233 * the operation even if the station we relied on with
1234 * GO_CONCURRENT is disconnected now. But then we must make sure
1235 * we're not outdoor on an indoor-only channel.
1237 if (iftype == NL80211_IFTYPE_P2P_GO &&
1238 wdev->iftype == NL80211_IFTYPE_P2P_GO &&
1239 wdev->links[link_id].ap.beacon_interval &&
1240 !(chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
1241 other_chan = wdev->links[link_id].ap.chandef.chan;
1246 if (chan == other_chan)
1249 if (chan->band != NL80211_BAND_5GHZ &&
1250 chan->band != NL80211_BAND_6GHZ)
1253 r1 = cfg80211_get_unii(chan->center_freq);
1254 r2 = cfg80211_get_unii(other_chan->center_freq);
1256 if (r1 != -EINVAL && r1 == r2) {
1258 * At some locations channels 149-165 are considered a
1259 * bundle, but at other locations, e.g., Indonesia,
1260 * channels 149-161 are considered a bundle while
1261 * channel 165 is left out and considered to be in a
1262 * different bundle. Thus, in case that there is a
1263 * station interface connected to an AP on channel 165,
1264 * it is assumed that channels 149-161 are allowed for
1265 * GO operations. However, having a station interface
1266 * connected to an AP on channels 149-161, does not
1267 * allow GO operation on channel 165.
1269 if (chan->center_freq == 5825 &&
1270 other_chan->center_freq != 5825)
1280 * Check if the channel can be used under permissive conditions mandated by
1281 * some regulatory bodies, i.e., the channel is marked with
1282 * IEEE80211_CHAN_IR_CONCURRENT and there is an additional station interface
1283 * associated to an AP on the same channel or on the same UNII band
1284 * (assuming that the AP is an authorized master).
1285 * In addition allow operation on a channel on which indoor operation is
1286 * allowed, iff we are currently operating in an indoor environment.
1288 static bool cfg80211_ir_permissive_chan(struct wiphy *wiphy,
1289 enum nl80211_iftype iftype,
1290 struct ieee80211_channel *chan)
1292 struct wireless_dev *wdev;
1293 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1295 lockdep_assert_held(&rdev->wiphy.mtx);
1297 if (!IS_ENABLED(CONFIG_CFG80211_REG_RELAX_NO_IR) ||
1298 !(wiphy->regulatory_flags & REGULATORY_ENABLE_RELAX_NO_IR))
1301 /* only valid for GO and TDLS off-channel (station/p2p-CL) */
1302 if (iftype != NL80211_IFTYPE_P2P_GO &&
1303 iftype != NL80211_IFTYPE_STATION &&
1304 iftype != NL80211_IFTYPE_P2P_CLIENT)
1307 if (regulatory_indoor_allowed() &&
1308 (chan->flags & IEEE80211_CHAN_INDOOR_ONLY))
1311 if (!(chan->flags & IEEE80211_CHAN_IR_CONCURRENT))
1315 * Generally, it is possible to rely on another device/driver to allow
1316 * the IR concurrent relaxation, however, since the device can further
1317 * enforce the relaxation (by doing a similar verifications as this),
1318 * and thus fail the GO instantiation, consider only the interfaces of
1319 * the current registered device.
1321 list_for_each_entry(wdev, &rdev->wiphy.wdev_list, list) {
1325 ret = cfg80211_ir_permissive_check_wdev(iftype, wdev, chan);
1335 static bool _cfg80211_reg_can_beacon(struct wiphy *wiphy,
1336 struct cfg80211_chan_def *chandef,
1337 enum nl80211_iftype iftype,
1341 u32 prohibited_flags = IEEE80211_CHAN_DISABLED |
1342 IEEE80211_CHAN_RADAR;
1344 trace_cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir);
1347 prohibited_flags |= IEEE80211_CHAN_NO_IR;
1349 if (cfg80211_chandef_dfs_required(wiphy, chandef, iftype) > 0 &&
1350 cfg80211_chandef_dfs_available(wiphy, chandef)) {
1351 /* We can skip IEEE80211_CHAN_NO_IR if chandef dfs available */
1352 prohibited_flags = IEEE80211_CHAN_DISABLED;
1355 res = cfg80211_chandef_usable(wiphy, chandef, prohibited_flags);
1357 trace_cfg80211_return_bool(res);
1361 bool cfg80211_reg_can_beacon(struct wiphy *wiphy,
1362 struct cfg80211_chan_def *chandef,
1363 enum nl80211_iftype iftype)
1365 return _cfg80211_reg_can_beacon(wiphy, chandef, iftype, true);
1367 EXPORT_SYMBOL(cfg80211_reg_can_beacon);
1369 bool cfg80211_reg_can_beacon_relax(struct wiphy *wiphy,
1370 struct cfg80211_chan_def *chandef,
1371 enum nl80211_iftype iftype)
1373 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
1376 lockdep_assert_held(&rdev->wiphy.mtx);
1379 * Under certain conditions suggested by some regulatory bodies a
1380 * GO/STA can IR on channels marked with IEEE80211_NO_IR. Set this flag
1381 * only if such relaxations are not enabled and the conditions are not
1384 check_no_ir = !cfg80211_ir_permissive_chan(wiphy, iftype,
1387 return _cfg80211_reg_can_beacon(wiphy, chandef, iftype, check_no_ir);
1389 EXPORT_SYMBOL(cfg80211_reg_can_beacon_relax);
1391 int cfg80211_set_monitor_channel(struct cfg80211_registered_device *rdev,
1392 struct cfg80211_chan_def *chandef)
1394 if (!rdev->ops->set_monitor_channel)
1396 if (!cfg80211_has_monitors_only(rdev))
1399 return rdev_set_monitor_channel(rdev, chandef);
1402 bool cfg80211_any_usable_channels(struct wiphy *wiphy,
1403 unsigned long sband_mask,
1404 u32 prohibited_flags)
1408 prohibited_flags |= IEEE80211_CHAN_DISABLED;
1410 for_each_set_bit(idx, &sband_mask, NUM_NL80211_BANDS) {
1411 struct ieee80211_supported_band *sband = wiphy->bands[idx];
1417 for (chanidx = 0; chanidx < sband->n_channels; chanidx++) {
1418 struct ieee80211_channel *chan;
1420 chan = &sband->channels[chanidx];
1422 if (chan->flags & prohibited_flags)
1431 EXPORT_SYMBOL(cfg80211_any_usable_channels);
1433 struct cfg80211_chan_def *wdev_chandef(struct wireless_dev *wdev,
1434 unsigned int link_id)
1437 * We need to sort out the locking here - in some cases
1438 * where we get here we really just don't care (yet)
1439 * about the valid links, but in others we do. But we
1440 * get here with various driver cases, so we cannot
1441 * easily require the wdev mutex.
1443 if (link_id || wdev->valid_links & BIT(0)) {
1444 ASSERT_WDEV_LOCK(wdev);
1445 WARN_ON(!(wdev->valid_links & BIT(link_id)));
1448 switch (wdev->iftype) {
1449 case NL80211_IFTYPE_MESH_POINT:
1450 return &wdev->u.mesh.chandef;
1451 case NL80211_IFTYPE_ADHOC:
1452 return &wdev->u.ibss.chandef;
1453 case NL80211_IFTYPE_OCB:
1454 return &wdev->u.ocb.chandef;
1455 case NL80211_IFTYPE_AP:
1456 case NL80211_IFTYPE_P2P_GO:
1457 return &wdev->links[link_id].ap.chandef;
1462 EXPORT_SYMBOL(wdev_chandef);
1464 struct cfg80211_per_bw_puncturing_values {
1466 const u16 *valid_values;
1469 static const u16 puncturing_values_80mhz[] = {
1473 static const u16 puncturing_values_160mhz[] = {
1474 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1, 0xc0, 0x30, 0xc, 0x3
1477 static const u16 puncturing_values_320mhz[] = {
1478 0xc000, 0x3000, 0xc00, 0x300, 0xc0, 0x30, 0xc, 0x3, 0xf000, 0xf00,
1479 0xf0, 0xf, 0xfc00, 0xf300, 0xf0c0, 0xf030, 0xf00c, 0xf003, 0xc00f,
1480 0x300f, 0xc0f, 0x30f, 0xcf, 0x3f
1483 #define CFG80211_PER_BW_VALID_PUNCTURING_VALUES(_bw) \
1485 .len = ARRAY_SIZE(puncturing_values_ ## _bw ## mhz), \
1486 .valid_values = puncturing_values_ ## _bw ## mhz \
1489 static const struct cfg80211_per_bw_puncturing_values per_bw_puncturing[] = {
1490 CFG80211_PER_BW_VALID_PUNCTURING_VALUES(80),
1491 CFG80211_PER_BW_VALID_PUNCTURING_VALUES(160),
1492 CFG80211_PER_BW_VALID_PUNCTURING_VALUES(320)
1495 bool cfg80211_valid_disable_subchannel_bitmap(u16 *bitmap,
1496 const struct cfg80211_chan_def *chandef)
1498 u32 idx, i, start_freq;
1500 switch (chandef->width) {
1501 case NL80211_CHAN_WIDTH_80:
1503 start_freq = chandef->center_freq1 - 40;
1505 case NL80211_CHAN_WIDTH_160:
1507 start_freq = chandef->center_freq1 - 80;
1509 case NL80211_CHAN_WIDTH_320:
1511 start_freq = chandef->center_freq1 - 160;
1521 /* check if primary channel is punctured */
1522 if (*bitmap & (u16)BIT((chandef->chan->center_freq - start_freq) / 20))
1525 for (i = 0; i < per_bw_puncturing[idx].len; i++)
1526 if (per_bw_puncturing[idx].valid_values[i] == *bitmap)
1531 EXPORT_SYMBOL(cfg80211_valid_disable_subchannel_bitmap);